Bootstrap

virt-customize构建zstack qcow2镜像

背景

由于当前使用centos7.9镜像里面的cloud-init版本低,导致在zstack上指定root密码创建虚机时,密码错误。所以就想尝试将原来镜像里的cloud-init升到最新版本。
还好公司大佬在github仓库里记录了定制镜像的步骤,一步步照着做就能实现。
https://github.com/shake/shell/blob/main/custom-centos-image

镜像构建脚本

## CentOS 7.9 虚拟机,内核版本升级到5.4,这样virt 运行速度快很多。
## 安装virt-customize等工具
yum install centos-release-openstack-train -y
yum install diskimage-builder -y
yum install libguestfs-tools-c libguestfs-tools libguestfs -y

#设置完成后,不需要使用virt都需要export
vi .bash_profile
export LIBGUESTFS_BACKEND=direct
export LANG="en_US.UTF-8"
source .bash_profile

### Zstack for CentOS 7

mkdir cloud-images
cd cloud-images
# CentOS 7.9 镜像
curl -O https://mirrors.ustc.edu.cn/centos-cloud/centos/7/images/CentOS-7-x86_64-GenericCloud-2009.qcow2

# pwd
/root/cloud-images
# 设置镜像变量
export image_name='CentOS-7-x86_64-GenericCloud-2009.qcow2'

#repo        CentOS-7-all.repo内容见下文
virt-customize -a $image_name --run-command 'mkdir /etc/yum.repos.d/backup'
virt-customize -a $image_name --run-command 'mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/'
virt-customize -a $image_name --upload /root/cloud-images/CentOS-7-all.repo:/etc/yum.repos.d/CentOS-7-all.repo


##安装工具
virt-customize -a $image_name --install kernel,vim,qemu-guest-agent
virt-customize -a $image_name --install net-tools,wget,unzip,jq,bash-completion,yum-utils,device-mapper-persistent-data,lvm2,openssl,sysstat,iotop,iftop,nload,bridge-utils,bash-completion,bind-utils,nc,binutils,tree,psmisc,nfs-utils
virt-customize -a $image_name --install socat,conntrack,ebtables,ipset

#启动服务
virt-customize -a $image_name --run-command 'systemctl enable qemu-guest-agent' 

# 设置时区
virt-customize -a $image_name --timezone "Asia/Shanghai" 

#SSH服务
virt-customize -a $image_name --edit '/etc/ssh/sshd_config:s/GSS/#GSS/'
virt-customize -a $image_name --edit '/etc/ssh/sshd_config:s/#UseDNS yes/UseDNS no/'

#vim
virt-customize -a $image_name --append-line '/etc/profile:alias vi=vim'
virt-customize -a $image_name --append-line '/etc/profile:alias c="clear"'
virt-customize -a $image_name --append-line '/etc/profile:unset MAILCHECK'
virt-customize -a $image_name --append-line '/root/.bash_profile:export LANG="en_US.UTF-8"'


#inatall zstack agent

virt-customize -a $image_name --run-command 'mkdir -p /var/lib/cloud/scripts/per-once'
virt-customize -a $image_name --upload /root/cloud-images/zstack.sh:/var/lib/cloud/scripts/per-once/zstack.sh
virt-customize -a $image_name --run-command 'chmod +x var/lib/cloud/scripts/per-once/zstack.sh'

#clean yum,不能运行yum clean all,会导致镜像加大。这样就足够。最终镜像730M大小,
virt-customize -a $image_name --run-command 'rm -rf /var/cache/yum/*' 

##DIB

#pwd
/root

export DIB_LOCAL_IMAGE="/root/cloud-images/CentOS-7-x86_64-GenericCloud-2009.qcow2" 
export DIB_RELEASE=7
export DIB_CLOUD_INIT_ALLOW_SSH_PWAUTH="yes"
export DIB_AVOID_PACKAGES_UPDATE=1
export image_name='CentOS-7.9-x86.qcow2'

#disk builder images
disk-image-create -a amd64 -o  $image_name -x --image-size 80 vm base centos disable-selinux cloud-init dhcp-all-interfaces

使用到的文件

CentOS-7-all.repo

[base]
name=CentOS-$releasever - Base
baseurl=http://mirrors.ustc.edu.cn/centos/$releasever/os/$basearch/
gpgcheck=0

[updates]
name=CentOS-$releasever - Updates
baseurl=http://mirrors.ustc.edu.cn/centos/$releasever/updates/$basearch/
gpgcheck=0

[extras]
name=CentOS-$releasever - Extras
baseurl=http://mirrors.ustc.edu.cn/centos/$releasever/extras/$basearch/
gpgcheck=0


[epel]
name=Centos EPEL
baseurl=http://mirrors.ustc.edu.cn/epel/7/x86_64/
gpgcheck=0

[centos-kernel]
name=CentOS LTS Kernels for $basearch
baseurl=https://mirrors.ustc.edu.cn/centos-altarch/7/kernel/$basearch/
enabled=1
gpgcheck=0

zstack.sh

#!/bin/bash
/bin/bash -c "$(curl -s -S http://169.254.169.254/vm-tools.sh)"
sed -i 's/9100/9104/g' /usr/local/zstack/zwatch-vm-agent/conf.yaml
/bin/systemctl restart zwatch-vm-agent.service
;