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
yum install diskimage-builder
yum install libguestfs-tools-c libguestfs-tools

设置环境变量, 如何你希望再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

Zstack

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

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

# 设置时区

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

#安装工具

virt-customize -a $image_name --install [net-tools,wget,vim,unzip,qemu-guest-agent,epel-release] 

#启动服务
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-cat -a $image_name /etc/ssh/sshd_config 


#上传优化脚本和运行。可以选择不使用脚本,也是可以完成相关工作。

virt-customize -a $image_name --upload /root/centos.sh:/root/centos.sh 
virt-customize -a $image_name -chmod 755:/root/centos.sh 
virt-customize -a $image_name --run '/root/centos.sh'
virt-customize -a $image_name --run-command 'yum clean all'
virt-customize -a $image_name --run-command 'yum makecache'

不使用脚本,只使用virt-customize

virt-customize -a $image_name --delete '/etc/yum.repos.d/CentOS-*'
virt-customize -a $image_name --upload /root/CentOS.Base.repo:/etc/yum.repos.d/CentOS.Base.repo
virt-customize -a $image_name --upload /root/epel.repo:/etc/yum.repos.d/epel.repo
virt-customize -a $image_name --run-command 'yum clean all'
virt-customize -a $image_name --run-command 'yum makecache'
virt-customize -a $image_name --append-line '/etc/profile:alias vi=vim'

centos.sh 脚本的内容

!/bin/bash

sed -e 's!^metalink=!#metalink=!g' \
    -e 's!^#baseurl=!baseurl=!g' \
    -e 's!//download\.fedoraproject\.org/pub!//mirrors.tuna.tsinghua.edu.cn!g' \
    -e 's!http://mirrors\.tuna!https://mirrors.tuna!g' \
    -i.bak \
         /etc/yum.repos.d/epel.repo

sed -e 's|^mirrorlist=|#mirrorlist=|g' \
         -e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.tuna.tsinghua.edu.cn|g' \
         -i.bak \
         /etc/yum.repos.d/CentOS-Base.repo

最后一步,安装zstack的agent

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

上传镜像

python -m SimpleHTTPServer 80

centos 7.6 repo模版 CentOS-Base.repo

[base]
name=CentOS-7.6.1810 - Base
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/7.6.1810/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#released updates 
[updates]
name=CentOS-7.6.1810 - Updates
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/7.6.1810/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-7.6.1810 - Extras
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/7.6.1810/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centos-vaultplus]
name=CentOS-7.6.1810 - Plus
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/7.6.1810/centos-vaultplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

epel.repo

[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/$basearch
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/$basearch/debug
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1

[epel-source]
name=Extra Packages for Enterprise Linux 7 - $basearch - Source
baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/SRPMS
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-source-7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1

 Leave a Reply

(required)

(required)

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