disk-image-builder
1. 简介
diskimage-builder是自动构建用于云和其他环境的自定义操作系统映像的工具。
它支持基于许多主要版本的镜像,并可生成所有常见格式(qcow2,vhd,raw等),裸金属文件系统镜像和ram磁盘镜像。
2. 元素 element[1]
2.1 介绍
需要注意的是,我们在命令中传递了一系列元素到disk-image-create。元素是我们如何决定进入我们镜像的内容以及将要进行的修改。
一些元素提供了一个根文件系统,例如上面我们的示例中的ubuntu或者fedora元素,其他元素修改来创建我们的镜像。执行镜像构建时,必须至少指定其中一个“发布元素”。值得指出的是,有许多发行版元素(甚至可以创建自己的发行版),甚至可以为某些发行版添加倍数。这是因为通常有多种方式来安装非常不同的发行版。例如:一个发行版元素可能使用镜像,而另一个发行版使用包安装工具为同一发行版构建根文件系统。
其他元素以某种方式修改我们的镜像。上例中的’vm’元素确保我们的映像具有正确安装的引导加载程序。这仅适用于某些使用情况和某些输出格式,因此不会默认执行。
2.2 元素构成[2]
2.2.1 阶段子目录
在构建镜像的指定元素时,元素中子目录按此处给出的顺序执行。 子目录中的脚本应使用两位数字前缀命名,并按数字顺序执行。
The phases are:
- root.d
- extra-data.d
- pre-install.d
- install.d
- post-install.d
- block-device.d
- finalise.d
- cleanup.d
2.2.2 元素依赖
2.2.2.1 element-deps
纯文本,以换行符分隔的元素列表,这些列表将添加到创建镜像时在镜像中创建的元素列表中。、
2.2.2.2 element-provides
由此元素提供的纯文本,换行符分隔的元素列表。 在创建镜像时,这些元素将从构建到镜像中的元素中排除。
2.2.3 元素结构
上述全部内容可以进一步细分,鼓励组成元素和组件的可重用性。 一种可能的方法是将元素标记为“驱动程序”,“服务”或“配置”元素。 以下是一些例子。
驱动程序特定的元素应该只包含该驱动程序的必要内容:
elements/ driver-mellanox/ init - modprobe line install.d/ 10-mlx - package installation
安装和配置Nova的元素可能会更复杂一些,包含几个阶段的几个脚本:
elements/ service-nova/ source-repository-nova - register a source repository pre-install.d/ 50-my-ppa - add a PPA install.d/ 10-user - common Nova user accts 50-my-pack - install packages from my PPA 60-nova - install nova and some dependencies
2.2.4 元素变量
部分元素需要设置参数,以执行命令时的环境变量形式传入:
declare -x DIB_CLOUD_INIT_ALLOW_SSH_PWAUTH="enabled"
declare -x DIB_DEV_USER_PASSWORD="123456"
declare -x DIB_DEV_USER_PWDLESS_SUDO="yes"
declare -x DIB_DEV_USER_USERNAME="admin"
declare -x DIB_PYTHON_EXEC="/usr/bin/python"
declare -x DIB_YUM_REPO_CONF="/etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-OpenStack-ocata.repo"
declare -x DIB_LOCAL_IMAGE="CentOS-7-x86_64-GenericCloud-1708.qcow2"
具体需要什么参数,参见各个元素及其依赖的README
2.3 常用元素
2.3.1 package-installs
package-installs
元素允许为镜像构建安装和卸载软件包的声明性方法。 这是通过在元素目录中创建package-installs.yaml
或package-installs.json
文件来完成的。
example package-installs.yaml
libxml2: grub2: phase: pre-install.d networkmanager: uninstall: True os-collect-config: installtype: source linux-image-amd64: arch: amd64 dmidecode: not-arch: ppc64, ppc64le lshw: arch: ppc64, ppc64le python-dev: dib_python_version: 2 python3-dev: dib_python_version: 3
2.3.2 source-repositories
有了这个元素,其他元素可以通过将他们的详细信息放在文件source-repository-*
中来注册他们的安装源。
Example for ironic source from git:
ironic git /usr/local/ironic git://git.openstack.org/openstack/ironic.git
Example for nova configuration from local file:
nova file /etc/nova/ file://localhost/root/nova_test/nova.conf
2.3.3 svc-map
映射服务名称到具体操作系统中的指定服务。
Example format for Nova:
nova-api: default: openstack-nova-api debian: nova-api ubuntu: nova-api nova-cert: default: openstack-nova-cert debian: nova-cert nova-compute: default: openstack-nova-compute debian: nova-compute nova-conductor: default: openstack-nova-conductor debian: nova-conductor nova-consoleauth: default: openstack-nova-console debian: nova-console
2.3.4 yum
在制作镜像时所使用的yum源设置。
设置环境变量DIB_YUM_REPO_CONF
,将repo文件的绝对路径以空格分割传入变量中:
Example:
declare -x DIB_YUM_REPO_CONF="/etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-OpenStack-ocata.repo"
以这种方式能够规避设置网络代理的问题,直接使用公司搭建的yum源环境即可构建镜像。
2.3.5 centos7
使用centos7作为镜像的操作系统。
依赖:
[root@localhost centos7]# cat element-deps
cache-url
dib-run-parts
redhat-common
rpm-distro
source-repositories
yum
其中,redhat-common元素中可以使用变量来指定本地镜像文件:
Environment Variables
---------------------
DIB_LOCAL_IMAGE
:Required: No
:Default: None
:Description: Use the local path of a qcow2 cloud image. This is useful in
that you can use a customized or previously built cloud image from
diskimage-builder as input. The cloud image does not have to have been built
by diskimage-builder. It should be a full disk image, not just a filesystem
image.
:Example: ``DIB_LOCAL_IMAGE=rhel-guest-image-7.1-20150224.0.x86_64.qcow2``
Example:
declare -x DIB_LOCAL_IMAGE="CentOS-7-x86_64-GenericCloud-1708.qcow2"
如果不指定本地文件,默认还需要去下载更新镜像,网速慢的环境下建议去官方镜像源下载好相应的qcow2格式镜像作为本地镜像。
其他操作系统类似,不再赘述。
2.3.6 devuser
创建开发/调试用的用户,以下环境变量可用于配置:
DIB_DEV_USER_USERNAME Required: No Default: devuser Description: Username for the created user. DIB_DEV_USER_SHELL Required: No Default: System default (The useradd default is used) Description: Full path for the shell of the user. This is passed to useradd using the -s parameter. Note that this does not install the (possibly) required shell package. DIB_DEV_USER_PWDLESS_SUDO Required: No Default: No Description: Enable passwordless sudo for the user. DIB_DEV_USER_AUTHORIZED_KEYS Required: No Default: $HOME/.ssh/id_{rsa,dsa}.pub Description: Path to a file to copy into this users’ .ssh/authorized_keys If this is not specified then an attempt is made to use a the building user’s public key. To disable this behavior specify an invalid path for this variable (such as /dev/null). DIB_DEV_USER_PASSWORD Required: No Default: Password is disabled Description: Set the default password for this user. This is a fairly insecure method of setting the password and is not advised.
Example:
declare -x DIB_CLOUD_INIT_ALLOW_SSH_PWAUTH="enabled"
declare -x DIB_DEV_USER_PASSWORD="123456"
declare -x DIB_DEV_USER_PWDLESS_SUDO="yes"
declare -x DIB_DEV_USER_USERNAME="admin"
2.3.7 cloud-init
安装并自启动cloud-init,环境变量:
DIB_CLOUD_INIT_ALLOW_SSH_PWAUTH Required: No Default: password authentication disabled when cloud-init installed Description: customize cloud-init to allow ssh password authentication.
2.3.8 baremetal
制作镜像同时,也会制作镜像对应的ramdisk和kernel镜像
2.3.9 ironic-agent
在镜像中安装ironic-python-agent,一般在制作部署镜像时需要。详见README
2.3.10 proliant-tools
此元素只可以在创建包含ironic-agent的部署镜像中使用,可以对HP Proliant硬件做清理相关的带内操作。
2.3.11 dhcp-all-interfaces
启动时所有网络接口设置为dhcp启动
3. 环境准备
在代理环境下制作镜像,需要注意以下几点:
代理 - 如果yum源不需要代理,请不要配置,否则制作镜像时会报错
export http_proxy="http://li-zr:***@192.168.2.49:8080/"
yaml - 制作镜像默认你已经安装了PyYAML,如果没有安装,需要提前安装好
yum install PyYAML -y
制作镜像的命令 - 当然,根据具体需求,命令和环境变量都会有所不同,下面只供参考
disk-image-create centos-minimal baremetal dhcp-all-interfaces grub2 devuser dynamic-login cloud-init disable-selinux nova yum -o nova_test declare -x DIB_CLOUD_INIT_ALLOW_SSH_PWAUTH="enabled" declare -x DIB_DEV_USER_PASSWORD="123456" declare -x DIB_DEV_USER_PWDLESS_SUDO="yes" declare -x DIB_DEV_USER_USERNAME="admin" declare -x DIB_PYTHON_EXEC="/usr/bin/python" declare -x DIB_YUM_REPO_CONF="/etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-OpenStack-ocata.repo" declare -x DIB_LOCAL_IMAGE="CentOS-7-x86_64-GenericCloud-1708.qcow2"