Mar 172021
 

希望一篇文章解决所有问题,看来还是不现实,我这里就把常见问题整理一下。主要是针对DiskImageBuilder(DIB)。制作镜像,网络速度非常关键。

官方镜像

基本的主流linux,DIB都是支持的,我需要用到的就是CentOS和Ubuntu。cloud image,目前国内就中科大提供下载。

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

这个比国外速度应该快很多。

制作CentOS各个版本镜像

默认你使用DIB,是下载最新版本的CentOS镜像进行构建。你可以指定 7,8,但是你没法具体到7.6版本。

你需要知道7.6版本的发布时间,下载相应的镜像到本地,指定镜像构建,这个是最简单的。2003,表示2020年3月发布的,是CentOS 7.8

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

export DIB_LOCAL_IMAGE="/root/cloud-images/CentOS-7-x86_64-GenericCloud-2003.qcow2"

这样第一次构建,下载速度慢的问题,也解决了。直接指定网上的镜像,我验证没有成功。

export DIB_CLOUD_IMAGES="https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud-2009.qcow2"
export DIB_CLOUD_IMAGES="https://mirrors.ustc.edu.cn/ubuntu-cloud-images/${DIB_RELEASE}/current"

国内源构建

构建镜像,需要使用到源,可以指定国内的源,这样加快速度。

export DIB_DISTRIBUTION_MIRROR="http://mirrors.163.com/centos"

Elements

可以理解这就是一个一个模块,你需要对镜像进行那些定制化,就用那些模块。如果是使用rpm包安装,那么位置

/usr/share/diskimage-builder/elements

后续的命令,基本都是这这里面,看一下里面的代码,就明白很多。

  • DIB_RELEASE=bionic
  • DIB_RELEASE=7
  • DIB_RELEASE=8
  • DIB_RELEASE=8-stream

例子

公有云上找一个网络比较好的虚拟机,家里虚拟机也是可以,我使用的上CentOS的。什么都不需要,最小化的系统就可以。

yum install centos-release-openstack-train
yum install diskimage-builder

运行下面代码,我是用root用户,直接根目录下创建。

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

设置环境变量

export DIB_LOCAL_IMAGE="/root/cloud-images/CentOS-7-x86_64-GenericCloud-2003.qcow2"
export DIB_DISTRIBUTION_MIRROR="http://mirrors.163.com/centos"
export DIB_RELEASE=7

这样就可以开始创建镜像。

disk-image-create -a amd64 -o centos7.qcow2 vm base centos7

或者

disk-image-create -a amd64 -o centos7.qcow2 vm base centos

上面两条命令的区别,在于使用的centos的Elements不同,第一个使用centos7,红帽希望日后都使用centos,不需要区分不同版本。后续红帽会淘汰掉centos7的element。

区分CentOS 7 和8,通过上面的 DIB_RELEASE 来设置。

上面的命令,还有一种方式运行

DIB_LOCAL_IMAGE="/root/cloud-images/CentOS-7-x86_64-GenericCloud-2003.qcow2" DIB_DISTRIBUTION_MIRROR="http://mirrors.163.com/centos" DIB_RELEASE=7 disk-image-create -a amd64 -o centos7.qcow2 vm base centos

网络速度的问题,会导致很多意想不到的问题,上面的内容,全部是经过我多次验证。

root 登陆

镜像的默认用户是centos,没有密码,通过sudo,切换到root,可以给用户centos设置密码,不过你是没办法给root设置一个密码,让他登陆。

至少我的习惯是用root,直接登陆。

yum install libguestfs-tools-c

安装工具,后续就可以给镜像的root设置密码,下面是把root的密码设置成chenshake

export LIBGUESTFS_BACKEND=direct
virt-sysprep --root-password password:chenshake -a centos7.qcow2

这个步骤,应该在最后上传镜像前完成就可以。

 Leave a Reply

(required)

(required)

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