OpenStack:镜像制作

1. 准备工作

1.1 创建镜像文件

1
2
3
mkdir -p /data/kvms/images/
cd /data/kvms/images/
qemu-img create -f qcow2 centos7.6_video.qcow2 10G

1.2 下载ISO镜像

1
2
wget http://vault.centos.org/centos/6.9/isos/x86_64/CentOS-6.9-x86_64-minimal.iso
wget https://vault.centos.org/centos/8.1.1911/isos/x86_64/CentOS-8.1.1911-x86_64-dvd1.iso

1.3 安装必要工具

1
2
yum install libguestfs-tools -y
yum install virt-install.noarch -y

2. 创建虚拟机

2.1 CentOS 7.6 安装命令

1
2
3
4
5
6
7
8
9
10
11
12
virt-install \
--virt-type kvm \
--name centos7.6 \
--ram 8192 \
--vcpus 2 \
--disk /data/kvms/images/centos7.6_video.qcow2,format=qcow2 \
--network network=default \
--graphics vnc,listen=0.0.0.0,password=123456 \
--noautoconsole \
--os-type=linux \
--os-variant=rhel7.6 \
--location=/data/kvms/images/CentOS-7.6-x86_64-DVD-1810.iso

2.2 CentOS 8.1 安装命令

1
2
3
4
5
6
7
8
9
10
11
12
virt-install \
--virt-type kvm \
--name centos8.1 \
--ram 8192 \
--vcpus 2 \
--disk /data/kvms/images/centos8.qcow2,format=qcow2 \
--network network=default \
--graphics vnc,listen=0.0.0.0,password=123456 \
--noautoconsole \
--os-type=linux \
--os-variant=rhel8.1 \
--location=/data/kvms/images/CentOS-8.1.1911-x86_64-dvd1.iso

2.3 内存分配问题解决

1
2
# 若遇到内存分配错误,执行:
sysctl vm.overcommit_memory=1

2.4 查看VNC端口

1
2
virsh vncdisplay centos8.1
# 输出示例::0 表示5900端口,:1表示5901端口

3. 虚拟机内部配置

3.1 安装必要服务

1
2
yum install -y acpid
chkconfig acpid on

3.2 安装cloud-init

1
2
yum install -y epel-release.noarch
yum install -y cloud-init

3.3 配置cloud-init

1
2
3
4
5
6
7
8
# 添加resolv-conf模块
sed -i '/cloud_init_modules:/ a - resolv-conf' /etc/cloud/cloud.cfg

# 开启SSH密码认证
sed -i 's/^ssh_pwauth:.*/ssh_pwauth:1/g' /etc/cloud/cloud.cfg

# 安装磁盘扩容工具
yum install -y cloud-utils-growpart

3.4 备用密钥注入脚本

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
cat >> /etc/rc.local <<EOF
[ $(rpm -aq |grep -c 'cloud-utils-growpart') -eq 0 ] && sed '/touch \/var\/lock\/subsys\/local/ a
if [ ! -d /root/.ssh ]; then
mkdir -p /root/.ssh
chmod 700 /root/.ssh
fi

# Fetch public key using HTTP
ATTEMPTS=30
FAILED=0
while [ ! -f /root/.ssh/authorized_keys ]; do
curl -f http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key >/tmp/metadata-key 2>/dev/null
if [ \$? -eq 0 ]; then
cat /tmp/metadata-key >> /root/.ssh/authorized_keys
chmod 0600 /root/.ssh/authorized_keys
restorecon /root/.ssh/authorized_keys
rm -f /tmp/metadata-key
echo "Successfully retrieved public key from instance metadata"
echo "*****************"
echo "AUTHORIZED KEYS"
echo "*****************"
cat /root/.ssh/authorized_keys
echo "*****************"
fi
done
' /etc/rc.local
EOF

3.5 网络配置

1
2
3
4
5
# 关闭zeroconf route
echo "NOZEROCONF=yes" >> /etc/sysconfig/network

# 设置网卡自动启动
nmcli c modify ens3 connection.autoconnect yes

3.6 控制台配置

1
2
3
4
5
# 修改GRUB配置
sed -i 's/GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet"/GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap console=tty0 console=ttyS0,115200n8"/g' /etc/default/grub

# 更新GRUB配置
grub2-mkconfig -o /boot/grub2/grub.cfg

3.7 关闭虚拟机

1
shutdown -h now

4. 宿主机操作

4.1 清理镜像

1
2
3
4
5
# 清除网络相关硬件生成信息
virt-sysprep -d centos

# 删除虚拟机定义
virsh undefine centos

4.2 格式转换

1
2
3
4
5
# qcow2转raw格式
qemu-img convert -f qcow2 -O raw centos8.1_x86_basic.qcow2 centos8.1_x86_basic.raw

# raw转qcow2格式(如需)
qemu-img convert -f raw -O qcow2 centos8.1_x86_basic.raw centos8.1_x86_basic.qcow2

5. 上传镜像到OpenStack

5.1 使用glance命令

1
2
3
4
5
6
7
source /root/admin-openrc.sh
glance image-create \
--file /root/centos8.1_x86_basic \
--disk-format raw \
--container-format bare \
--name centos8.1_x86_basic \
--progress

5.2 使用openstack命令

1
2
3
4
5
openstack image create "centos8.1_x86_basic" \
--file /root/centos8.1_x86_basic.raw \
--disk-format raw \
--container-format bare \
--public

关键参数说明

参数 说明 示例值
--ram 内存大小(MB) 8192
--vcpus vCPU数量 2
--disk 磁盘路径和格式 path=/data/kvms/images/centos.qcow2,format=qcow2
--network 网络配置 network=default
--graphics VNC配置 vnc,listen=0.0.0.0,password=123456
--os-variant 操作系统变体 rhel7.6
--location 安装源位置 /path/to/iso