Openstack制作glance镜像(Linux/Windows)

Linux:

虚拟机中执行

安装ACPI服务

虚拟机需要根据接收的信号执行指定的操作,比如关机、重启等。需要开启acpid服务(Advanced Configuration and Power Interface event daemon)

1
2
3
# yum install acpid
# systemctl enable acpid
# systemctl start acpid

安装Qemu Guest Agent服务

1
2
3
# yum -y install qemu-guest-agent
# systemctl enable qemu-guest-agent
# systemctl start qemu-guest-agent

安装并配置cloud-init

cloud-init用来通过从nova metadata服务或者config drive中获取metadata,并完成相关的初始化工作。

  • 安装cloud-init并设置开机启动
1
2
# yum install cloud-init
# systemctl enable cloud-init
  • 开启允许root用户以ssh方式登录
1
2
3
# vi /etc/cloud/cloud.cfg
disable_root: 0
ssh_pwauth: 1
  • 禁用cloud-init接管网络

    当Cloud-init版本等于高于0.7.9版本时,需要禁用Cloud-init接管网络

1
2
3
# vi /etc/cloud/cloud.cfg
network:
config: disabled
  • 去除云主机不带.novalocal后缀的主机名
1
2
3
# vi /usr/lib/python2.7/site-packages/cloudinit/sources/__init__.py
# rm -f /usr/lib/python2.7/site-packages/cloudinit/sources/__init__.pyc
# rm -f /usr/lib/python2.7/site-packages/cloudinit/sources/__init__.pyo

  • 检查Cloud-init工具相关配置是否成功(返回版本信息且无报错则正常)
1
2
cloud-init init --local
Cloud-init v. 0.7.9 running 'init-local' at Wed, 12 Sep 2018 02:49:06 +0000. Up 1464.55 seconds.

安装cloud-utils-growpart以允许调整分区大小

1
# yum install cloud-utils-growpart

禁用zeroconf 路由

CentOS、Red Hat、Oracle、EulerOS操作系统云服务器必须要禁用默认的zeroconf路由,以便精确访问OpenStack数据源。

1
# echo "NOZEROCONF=yes" >> /etc/sysconfig/network

配置控制台

grub中去除rhgb quiet并新增console=tty0 console=ttyS0,115200n8

1
2
3
# vi /etc/default/grub
GRUB_CMDLINE_LINUX="console=tty0 console=ttyS0,115200n8"
# grub2-mkconfig -o /boot/grub2/grub.cfg

其他相关的定制配置

1
2
3
# systemctl disable firewalld
# vi /etc/selinux/config
SELINUX=disabled

宿主机中执行

virt-sysprep会清除crontab记录,若添加了crontab则需要指定--operations参数排除cron-spool选项。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# yum -y install guestfish
# virt-sysprep -d centos7.5
[ 0.0] Examining the guest ...
[ 5.0] Performing "abrt-data" ...
[ 5.0] Performing "backup-files" ...
[ 6.1] Performing "bash-history" ...
[ 6.1] Performing "blkid-tab" ...
[ 6.1] Performing "crash-data" ...
[ 6.1] Performing "cron-spool" ...
[ 6.1] Performing "dhcp-client-state" ...
[ 6.1] Performing "dhcp-server-state" ...
[ 6.1] Performing "dovecot-data" ...
[ 6.1] Performing "logfiles" ...
[ 6.1] Performing "machine-id" ...
[ 6.1] Performing "mail-spool" ...
[ 6.1] Performing "net-hostname" ...
[ 6.1] Performing "net-hwaddr" ...
[ 6.2] Performing "pacct-log" ...
[ 6.2] Performing "package-manager-cache" ...
[ 6.2] Performing "pam-data" ...
[ 6.2] Performing "passwd-backups" ...
[ 6.2] Performing "puppet-data-log" ...
[ 6.2] Performing "rh-subscription-manager" ...
[ 6.2] Performing "rhn-systemid" ...
[ 6.2] Performing "rpm-db" ...
[ 6.2] Performing "samba-db-log" ...
[ 6.2] Performing "script" ...
[ 6.2] Performing "smolt-uuid" ...
[ 6.2] Performing "ssh-hostkeys" ...
[ 6.2] Performing "ssh-userdir" ...
[ 6.2] Performing "sssd-db-log" ...
[ 6.2] Performing "tmp-files" ...
[ 6.2] Performing "udev-persistent-net" ...
[ 6.2] Performing "utmp" ...
[ 6.2] Performing "yum-uuid" ...
[ 6.2] Performing "customize" ...
[ 6.2] Setting a random seed
[ 6.2] Setting the machine ID in /etc/machine-id
[ 6.3] Performing "lvm-uuids" ...
# virt-sparsify --compress centos7.5.qcow2 centos75.qcow2

Windows:

虚拟机上执行

系统安装完成后需要确保Adminstrator帐号未禁用,且以Administrator用户登录完成后续操作

安装相关的驱动

  • 安装virtio相关的驱动(可在安装系统时安装)
  • 安装qemu-ga(guest-agent)
  • 安装balloon驱动(需要先将驱动文件拷贝到本地磁盘)

    激活系统并开启远程桌面

    win7使用暴风激活工具后,可能出现创建实例出现黑屏的情况,换个激活工具可解决

定制相关配置(关闭防火墙等等)

安装配置cloudbase-init

安装过程


将会创建一个cloudbase-init的用户,且属于Administrators组,用于运行cloudbase-init服务。
安装完成之前可选择完成以下配置

配置cloudbase-init

配置Windows系统的hostname长度支持到63个字符、关闭首次登陆修改密码

C:\Program Files\Cloudbase Solutions\Cloudbase-Init\conf\cloudbase-init.conf文件增加如下配置

1
2
netbios_host_name_compatibility=false
first_logon_behaviour=NEVER_CHANGE

执行Sysprep

执行Sysprep操作可以确保在云服务器加入域后SID唯一,但必须确保系统已激活,且剩余Windows重置技数≥1,sysprep可以在cloudbase安装完成后勾选”Run Sysprep to create a generalized image”选项自动完成,也可以手动通过在cmd命令行执行Sysprep。



宿主机上执行

通过virt-sparsify压缩镜像

1
# virt-sparsify --compress win7.qcow2 win7.img

上传到glance

上传Windows的glance镜像时,需要指定os_type=windows的参数,否则时区会设置为utc的,将会相差8个小时。

1
# openstack image create --file win7.img  --disk-format qcow2 --container-format bare --property os_type=windows --property hw_qemu_guest_agent=yes --public Win7

创建实例

由于windows7默认禁用Administrator,且执行Sysprep后创建出来的实例默认仍然会自动禁用Administrator用户,因此可在创建实例时指定以下userdata激活Administrator用户并设置密码

1
2
3
rem cmd
net user administrator /active:yes
net user administrator password
坚持原创技术分享,您的支持将鼓励我继续创作!
0%