Mar 262021
 

这里面其实就是记录一下做一个指定版本的镜像,所需要做的东西。假设DIB和virt已经装好。下面就是一个实际的操作过程。可以参考我上一篇文章。国内目前好像就中科大提供镜像下载。我这里下载的是7.6

https://mirrors.ustc.edu.cn/centos-cloud/centos/7/images/

我习惯就是

mkdir cloud-images
cd cloud-images
curl -O https://mirrors.ustc.edu.cn/centos-cloud/centos/7/images/CentOS-7-x86_64-GenericCloud-1905.qcow2

安装DIB和Virt

yum install centos-release-openstack-train -y
yum install diskimage-builder -y
yum install libguestfs-tools-c libguestfs-tools libguestfs -y

设置环境变量, 如何你希望再build一个centos 7.9 镜像,那么你只需要把 avoid pacage update 这个去掉就可以。

export LIBGUESTFS_BACKEND=direct
export DIB_LOCAL_IMAGE="/root/cloud-images/CentOS-7-x86_64-GenericCloud-1905.qcow2" 
export DIB_RELEASE=7
export DIB_CLOUD_INIT_ALLOW_SSH_PWAUTH="yes"
export DIB_AVOID_PACKAGES_UPDATE=1
export image_name='CentOS-7.6-x86.qcow2'

设置好环境变量,后续就简单了。

openstack平台

DIB_CLOUD_INIT_DATASOURCES="ConfigDrive, OpenStack"  disk-image-create -a amd64 -o $image_name  -x --image-size 40 vm base centos disable-selinux cloud-init cloud-init-datasources dhcp-all-interfaces growroot epel

Zstack

disk-image-create -a amd64 -o  $image_name -x --image-size 40 vm base centos disable-selinux cloud-init dhcp-all-interfaces epel

这样也就几分钟就可以创建出镜像。

# 设置时区

virt-customize -a $image_name --timezone "Asia/Shanghai" 

#安装工具

virt-customize -a $image_name --install epel-release

virt-customize -a $image_name --install net-tools,wget,vim,unzip,qemu-guest-agent,jq,bash-completion

virt-customize -a $image_name --install net-tools,wget,vim,unzip,qemu-guest-agent,jq,bash-completion,yum-utils,device-mapper-persistent-data,lvm2,openssl,socat,conntrack,ebtables,ipset,sysstat,iotop,iftop,nload,bridge-utils,bash-completion,bind-utils,nc,binutils,iscsi-initiator-utils,kmod-xfs,xfsprogs,sg3_utils-devel.x86_64,sg3_utils-libs.x86_64,sg3_utils.x86_64,psmisc

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


#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:unset MAILCHECK'


#查看修改
virt-cat -a $image_name /etc/ssh/sshd_config 


virt-customize -a $image_name --run-command 'yum clean all'
virt-customize -a $image_name --run-command 'yum makecache'

最后一步,安装zstack的agent

virt-customize -a $image_name --firstboot-command '/bin/bash -c "$(curl -s -S http://169.254.169.254/vm-tools.sh)"'

由于zstack的agent,使用9100端口,如果你也使用普罗米修斯来监控,也必然导致端口冲突,所以我修改端口为9104。当多个启动脚本要运行,需要设置一下他们的优先级,默认是安装字母顺序来运行,所以我把重启agent的改成02。

virt-customize -a $image_name --firstboot-command "sed -i 's/9100/9104/g' /usr/local/zstack/zwatch-vm-agent/conf.yaml

virt-customize -a $image_name --firstboot-command. "/bin/systemctl restart zwatch-vm-agent.service"

virt-customize -a $image_name --run-command 'mv /usr/lib/virt-sysprep/scripts/0001--bin-systemctl-restart-zwatch-vm-agent-service /usr/lib/virt-sysprep/scripts/0002--bin-systemctl-restart-zwatch-vm-agent-service'

查看修改后的结果

virt-ls  -a $image_name  /usr/lib/virt-sysprep/scripts

上传镜像

python -m SimpleHTTPServer 80

如果希望定制内核版本

virt-customize -a $image_name --run-command 'rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org'
virt-customize -a $image_name --run-command 'rpm -Uvh https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm'
virt-customize -a $image_name --run-command 'yum --enablerepo=elrepo-kernel install kernel-lt -y'
virt-customize -a $image_name --run-command 'grub2-set-default 0'
virt-customize -a $image_name --run-command 'grub2-mkconfig -o /boot/grub2/grub.cfg'

elrepo在国外,速度很慢。可以修改使用国内的源

sed -i 's#elrepo.org/linux#mirrors.tuna.tsinghua.edu.cn/elrepo#g' /etc/yum.repos.d/elrepo.repo
sed -i 's|^mirrorlist=|#mirrorlist=|g' /etc/yum.repos.d/elrepo.repo

安装docker

virt-customize -a $image_name --run-command 'yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo'
virt-customize -a $image_name --run-command 'yum install docker-ce-20.10.7 -y'
virt-customize -a $image_name --run-command 'systemctl enable docker'
virt-customize -a $image_name --append-line '/etc/profile:KKZONE=cn'

设置docker代理

virt-customize -a $image_name --run-command 'mkdir -p /etc/docker'
virt-customize -a $image_name --upload /etc/docker/daemon.json:/etc/docker/daemon.json 
sudo sh -c 'echo {\"registry-mirrors\": [\"https://fz1p31b1.mirror.aliyuncs.com\"]} > /etc/docker/daemon.json'

  One Response to “做一个CentOS 7.6镜像”

  1. 您在 /var/spool/mail/root 中有新邮件

    不出现这个信息。

    echo “unset MAILCHECK”>> /etc/profile

 Leave a Reply

(required)

(required)

This site uses Akismet to reduce spam. Learn how your comment data is processed.