Openstack: Packer + Cloud-Init



我想创建一个定制的openstack OpenSUSE15-image,其中包含一些自定义软件和一个图形界面。我使用了一个现有的 OpenSUSE15.0 镜像和打包器来构建该镜像。它工作正常。打包程序 json 文件如下所示:

"builders": [
{
"type" : "openstack",
"ssh_username" : "root",
"image_name": "OpenSUSE_15_custom_kde",
"source_image": "OpenSUSE 15",
"flavor": "m1.medium",
"networks": "public-network"
}
],
"provisioners":[
{
"type": "shell",
"inline": [
"sleep 10",
"sudo -s",
"zypper --gpg-auto-import-keys refresh",
"zypper -n up -y",
"zypper -n clean -a",
"zypper -n addrepo -f http://download.opensuse.org/repositories/devel\:/languages\:/R\:/patched/openSUSE_Leap_15.0/ R-patched",
"zypper -n addrepo -f http://download.opensuse.org/repositories/devel\:/languages\:/R\:/released/openSUSE_Leap_15.0/ R-released",
"zypper --gpg-auto-import-keys refresh",
"zypper -n install -y R-base R-base-devel R-recommended-packages rstudio",
"zypper -n clean -a",
"zypper --non-interactive install -y -t pattern kde kde_plasma devel_kernel devel_python3 devel_C_C++ office x11",
"zypper -n install xrdp",
"zypper -n clean -a",
"zypper -n dup -y",
"systemctl enable xrdp",
"systemctl start xrdp",
"cloud-init clean --logs",
"zypper -n install -y cloud-init growpart yast2-network yast2-services-manager acpid",
"cat /dev/null > /etc/udev/rules.d/70-persistent-net.rules",
"systemctl disable cloud-init.service cloud-final.service cloud-init-local.service cloud-config.service",
"systemctl enable cloud-init.service cloud-final.service cloud-init-local.service cloud-config.service sshd",
"sudo systemctl stop firewalld",
"sudo systemctl disable firewalld",
"sed -i 's/GRUB_TIMEOUT=.*$/GRUB_TIMEOUT=0/g' /etc/default/grub",
"exec grub2-mkconfig -o /boot/grub2/grub.cfg '$@'",
"systemctl restart cloud-init",
"systemctl daemon-reload",
"cat /dev/null > ~/.bash_history && history -c && sudo su",
"cat /dev/null > /var/log/wtmp",
"cat /dev/null > /var/log/btmp",
"cat /dev/null > /var/log/lastlog",
"cat /dev/null > /var/run/utmp",
"cat /dev/null > /var/log/auth.log",
"cat /dev/null > /var/log/kern.log",
"cat /dev/null > ~/.bash_history && history -c",
"rm ~/.ssh/authorized_keys"
]
},
{
"type": "file",
"source": "./cloud_init/cloud.cfg",
"destination": "/etc/cloud/cloud.cfg"
}
]
}

使用打包程序在构建和配置阶段没有错误。

在第二阶段,当这个基础镜像通过 openstack 客户端通过热模板生成时,我希望完成一些个性化任务。用户创建,授予 ssh 访问权限(包括调整sshd_config文件...这是通过init_image.sh文件完成的。

#!/bin/bash
useradd -m $USERNAME -p $PASSWD -s /bin/bash
usermod -a -G sudo $USERNAME
tee /etc/ssh/banner <<EOF
You are one lucky user, if you bear the key...
EOF

tee /etc/ssh/sshd_config <<EOF 
##  SOME IMPORTANT SSHD CONFIGURATIONS
EOF
sudo -u $USERNAME -H sh -c 'cd ~;mkdir ~/.ssh/;echo "$SSHPUBKEY" > ~/.ssh/authorized_keys;chmod -R 700 ~/.ssh/;chmod 600 ~/.ssh/authorized_keys;'
systemctl restart sshd.service

voldata_dev="/dev/disk/by-id/virtio-$(echo $VOLDATA | cut -c -20)"
mkfs.ext4 $voldata_dev               
mkdir -pv /home/$USERNAME/share
echo "$voldata_dev /home/$USERNAME/share ext4 defaults 1 2" >> /etc/fstab
mount /home/$USERNAME/share
chown -R $USERNAME:users /home/$USERNAME/share/

systemctl enable xrdp
systemctl start xrdp

为此,我创建了以下加热模板。

heat_template_version: "2018-08-31"
description: "version 2017-09-01 created by HOT Generator at Fri, 05 Jul 2019 12:56:22 GMT."
parameters:
username:
type: string
label: User Name
description: This is the user name, and will  be also the name of the key and the server
default: test
imagename:
type: string
label: Image Name
description: This is the Name of the Image e.g. Ubuntu 18.04
default: "OpenSUSE Leap 15"
ssh_pub_key:
type: string
label: ssh public key

flavorname:
type: string
label: Flavor Name
description: This is the Name of the Flavor e.g. m1.small
default: "m1.small"
vol_size:
type: number
label: Volume Size
description: This is the size of the volume that should be attached in GB
default: 10
password: 
type: string
label: password
description: This is the su password and user password

resources: 
init:
type: OS::Heat::SoftwareConfig
properties:
group: ungrouped
config:
str_replace:
template: 
{get_file: init_image.sh}
params:
$USERNAME: {get_param: username}
$SSHPUBKEY: {get_param: ssh_pub_key}
$PASSWD: {get_param: password}
$VOLDATA: {get_resource: volume}
my_key: 
type: "OS::Nova::KeyPair"
properties: 
name: 
list_join:
["_", [ {get_param: username}, 'key']]  
public_key: {get_param: ssh_pub_key} 
my_server: 
type: "OS::Nova::Server"
properties: 
block_device_mapping_v2: [{ device_name: "vda", image : { get_param : imagename }, delete_on_termination : "false", volume_size: 20 }]
name: {get_param: username}
flavor: {get_param: flavorname}
key_name: {get_resource: my_key}
admin_pass: {get_param: password}
user_data_format: RAW
user_data: {get_resource: init}
networks:
- network: "public-network"
depends_on: 
- my_key
- init
- volume

volume:
type: "OS::Cinder::Volume"
properties:
# Size is given in GB
size: {get_param: vol_size}
name: 
list_join: ["-", ["vol_",{get_param: username }]]
volume_attachment:
type: "OS::Cinder::VolumeAttachment"
properties:
volume_id: { get_resource: volume }
instance_uuid: { get_resource: my_server }
depends_on:
- volume
outputs:
instance_ip:
description: The IP address of the deployed instances
value: { get_attr: [my_server, first_address] }

如果我在模板中使用原始图像,我没有问题(但是,构建过程需要非常长的时间(,我需要重新启动才能获得图形 KDE 界面。

但是,如果我使用带有打包程序的映像构建,我的user_data会被忽略?我无法登录,未创建用户个性化用户...我错过了什么?为什么它不起作用?如您所见,我清理了云初始化,重新启动了服务...我被困住了很长时间...

更新

这是来自计算机的可访问引导日志。

更新 2

这是cloud-init analyze show的输出:

-- Boot Record 01 --
The total time elapsed since completing an event is printed after the "@" character.
The time the event takes is printed after the "+" character.
Starting stage: init-local
|`->no cache found @00.01000s +00.00000s
|`->no local data found from DataSourceOpenStackLocal @00.04700s +15.23000s
Finished stage: (init-local) 15.31200 seconds 
Starting stage: init-network
|`->no cache found @16.01000s +00.00100s
|`->no network data found from DataSourceOpenStack @16.01700s +00.02600s
|`->found network data from DataSourceNone @16.04300s +00.00100s
|`->setting up datasource @16.09000s +00.00000s
|`->reading and applying user-data @16.10000s +00.00200s
|`->reading and applying vendor-data @16.10200s +00.00000s
|`->activating datasource @16.12100s +00.00100s
|`->config-migrator ran successfully @16.17900s +00.00100s
|`->config-seed_random ran successfully @16.18000s +00.00100s
|`->config-bootcmd ran successfully @16.18200s +00.00000s
|`->config-write-files ran successfully @16.18200s +00.00100s
|`->config-growpart ran successfully @16.18300s +00.46100s
|`->config-resizefs ran successfully @16.64500s +01.33400s
|`->config-disk_setup ran successfully @17.98100s +00.00300s
|`->config-mounts ran successfully @17.98500s +00.00400s
|`->config-set_hostname ran successfully @17.99000s +00.09800s
|`->config-update_hostname ran successfully @18.08900s +00.01000s
|`->config-update_etc_hosts ran successfully @18.10000s +00.00100s
|`->config-rsyslog ran successfully @18.10100s +00.00200s
|`->config-users-groups ran successfully @18.10400s +00.00200s
|`->config-ssh ran successfully @18.10700s +00.61400s
Finished stage: (init-network) 02.73600 seconds 
Starting stage: modules-config
|`->config-locale ran successfully @35.00200s +00.00400s
|`->config-set-passwords ran successfully @35.00600s +00.00100s
|`->config-zypper-add-repo ran successfully @35.00700s +00.00200s
|`->config-ntp ran successfully @35.01000s +00.00100s
|`->config-timezone ran successfully @35.01100s +00.00200s
|`->config-disable-ec2-metadata ran successfully @35.01300s +00.00100s
|`->config-runcmd ran successfully @35.01800s +00.00200s
Finished stage: (modules-config) 00.05100 seconds 
Starting stage: modules-final
|`->config-package-update-upgrade-install ran successfully @35.87400s +00.00000s
|`->config-puppet ran successfully @35.87500s +00.00000s
|`->config-chef ran successfully @35.87600s +00.00000s
|`->config-mcollective ran successfully @35.87600s +00.00100s
|`->config-salt-minion ran successfully @35.87700s +00.00100s
|`->config-rightscale_userdata ran successfully @35.87800s +00.00100s
|`->config-scripts-vendor ran successfully @35.87900s +00.00500s
|`->config-scripts-per-once ran successfully @35.88400s +00.00100s
|`->config-scripts-per-boot ran successfully @35.88500s +00.00000s
|`->config-scripts-per-instance ran successfully @35.88500s +00.00100s
|`->config-scripts-user ran successfully @35.88600s +00.00100s
|`->config-ssh-authkey-fingerprints ran successfully @35.88700s +00.00100s
|`->config-keys-to-console ran successfully @35.88800s +00.09000s
|`->config-phone-home ran successfully @35.97900s +00.00100s
|`->config-final-message ran successfully @35.98000s +00.00600s
|`->config-power-state-change ran successfully @35.98700s +00.00100s
Finished stage: (modules-final) 00.13600 seconds 
Total Time: 18.23500 seconds
1 boot records analyzed

更新 3

显然,当一个人没有更新时zypper up,cloud-init 表现良好并找到用户数据。因此,我不会在预配中更新映像。但是,预配后,更新是有意义的。

在预配结束时,应停止云初始化并擦除状态。否则,当映像在云初始化中启动时,认为它已经执行了第一次启动

systemctl stop cloud-init 
rm -rf /var/lib/cloud/

相关内容

  • 没有找到相关文章

最新更新