浏览 Python Package 仓库时,发现了一个用于定制OS镜像的OpenStack工具,diskimagebuilder。根据项目描述可知,这个镜像构建工具虽有有OpenStack推出 主要用于构建适用于OpenStack平台的OS镜像,但所构建的镜像可同时用于虚拟化平台和裸金属结构。

“diskimage-builder is a flexible suite of components for building a wide-range of disk images, filesystem images and ramdisk images for use with OpenStack.

This repository has the core functionality for building such images, both virtual and bare metal. Images are composed using elements; while fundamental elements are provided here, individual projects have the flexibility to customise the image build with their own elements.”

https://pypi.org/project/diskimage-builder/

这意味着,我们也可以利用这个镜像构建工具定制Linux操作系统镜像,如个性化的桌面系统发行版、公司生产环境中的专用OS镜像等。该工具当前还不支持Windows系统镜像的定制,只支持Linux系统。

以下是本人对这个工具的使用体验:

[googlebigtable@localhost ~]$ ls -F

Desktop/ Documents/ Downloads/ Music/ Pictures/ Public/ Templates/ Videos/

[googlebigtable@localhost ~]$ pwd -P

/home/googlebigtable

[googlebigtable@localhost ~]$ echo $PATH

/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/googlebigtable/.local/bin:/home/googlebigtable/bin

[googlebigtable@localhost ~]$ su root

Password:

[root@localhost googlebigtable]# pwd -P

/home/googlebigtable

[root@localhost googlebigtable]# echo $PATH

/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/googlebigtable/.local/bin:/home/googlebigtable/bin

[root@localhost googlebigtable]#

[root@localhost googlebigtable]# yum install -y tree

Loaded plugins: fastestmirror, langpacks

.......................................................................................................

Installed:

tree.x86_64 0:1.6.0-10.el7

Complete!

[root@localhost googlebigtable]#

[root@localhost googlebigtable]# tree -L 1 /opt/

/opt/

├── diskimagebuilder

├── google

└── rh

3 directories, 0 files

[root@localhost googlebigtable]# tree -L 2 /opt/

/opt/

├── diskimagebuilder

├── google

│ └── chrome

└── rh

4 directories, 0 files

[root@localhost googlebigtable]#

创建账户diskimagebuilder并设置其主目录为 /opt/diskimagebuilder/

[root@localhost googlebigtable]# useradd -s /bin/bash -d /opt/diskimagebuilder/ diskimagebuilder

useradd: warning: the home directory already exists.

Not copying any file from skel directory into it.

[root@localhost googlebigtable]# su - diskimagebuilder

-bash-4.2$

-bash-4.2$ pwd -P

/opt/diskimagebuilder

-bash-4.2$ whoami

diskimagebuilder

-bash-4.2$

-bash-4.2$ git clone https://git.openstack.org/openstack/diskimage-builder

fatal: could not create work tree dir 'diskimage-builder'.: Permission denied

-bash-4.2$ su - root

Password:

Last login: Tue Aug 4 08:57:05 EDT 2020 on pts/0

[root@localhost ~]#

[root@localhost ~]# grep -v "^#" /etc/sudoers | grep -v "^$" | cat

Defaults !visiblepw

Defaults always_set_home

Defaults match_group_by_gid

Defaults always_query_group_plugin

Defaults env_reset

Defaults env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS"

Defaults env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"

Defaults env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"

Defaults env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"

Defaults env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"

Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin

root ALL=(ALL) ALL

%wheel ALL=(ALL) ALL

[root@localhost ~]#

[root@localhost ~]# cat /etc/sudoers | grep -v "^[#,;]" |grep -v "^$"

Defaults !visiblepw

Defaults always_set_home

Defaults match_group_by_gid

Defaults always_query_group_plugin

Defaults env_reset

Defaults env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS"

Defaults env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"

Defaults env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"

Defaults env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"

Defaults env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"

Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin

root ALL=(ALL) ALL

%wheel ALL=(ALL) ALL

[root@localhost ~]#

[root@localhost ~]# grep -v "^#" /etc/sudoers | grep -v "^$" | cat -n

 1  Defaults   !visiblepw

 2  Defaults    always_set_home

 3  Defaults    match_group_by_gid

 4  Defaults    always_query_group_plugin

 5  Defaults    env_reset

 6  Defaults    env_keep =  "COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS"

 7  Defaults    env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"

 8  Defaults    env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"

 9  Defaults    env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"

10  Defaults    env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"

11  Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

12  root    ALL=(ALL)       ALL

13  %wheel  ALL=(ALL)       ALL

[root@localhost ~]#

[root@localhost ~]# grep "^\s[^# \t].$" /etc/sudoers | cat -n

 1  Defaults   !visiblepw

 2  Defaults    always_set_home

 3  Defaults    match_group_by_gid

 4  Defaults    always_query_group_plugin

 5  Defaults    env_reset

 6  Defaults    env_keep =  "COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS"

 7  Defaults    env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"

 8  Defaults    env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"

 9  Defaults    env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"

10  Defaults    env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"

11  Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

12  root    ALL=(ALL)       ALL

13  %wheel  ALL=(ALL)       ALL

[root@localhost ~]#

修改sudo配置文件,给予diskimagebuildersudo权限

[root@localhost ~]# grep "^\s[^# \t].$" /etc/sudoers | cat -n

 1  Defaults   !visiblepw

 2  Defaults    always_set_home

 3  Defaults    match_group_by_gid

 4  Defaults    always_query_group_plugin

 5  Defaults    env_reset

 6  Defaults    env_keep =  "COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS"

 7  Defaults    env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"

 8  Defaults    env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"

 9  Defaults    env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"

10  Defaults    env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"

11  Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

12  root    ALL=(ALL)       ALL

13  %wheel  ALL=(ALL)       ALL

14  root    ALL=(ALL)       NOPASSWD: ALL

15  diskimagebuilder ALL=(ALL)       NOPASSWD: ALL

[root@localhost ~]#

[root@localhost ~]# su - diskimagebuilder

Last login: Tue Aug 4 09:06:10 EDT 2020 on pts/0

-bash-4.2$ sudo git clone https://git.openstack.org/openstack/diskimage-builder

Cloning into 'diskimage-builder'...

.................................................................................................................

因为从OpenStack下载太慢,改为从Python Package Index (PyPI)仓库下载tar包安装:

-bash-4.2$ sudo wget https://files.pythonhosted.org/packages/e6/24/2dbeecff4067e860508eff26bb89094f7041aa32a56328aab4d384354da9/diskimage-builder-3.1.0.tar.gz

-bash-4.2$ sudo tar -xzvf diskimage-builder-3.1.0.tar.gz

-bash-4.2$ ls -F

diskimage-builder-3.1.0/ diskimage-builder-3.1.0.tar.gz get-pip.py

-bash-4.2$ cd diskimage-builder-3.1.0/

-bash-4.2$ ls -F

AUTHORS contrib/ LICENSE pylint.cfg roles/ tests/

bin/ diskimage_builder/ lower-constraints.txt README.rst setup.cfg tox.ini

bindep.txt diskimage_builder.egg-info/ PKG-INFO releasenotes/ setup.py

ChangeLog doc/ playbooks/ requirements.txt test-requirements.txt

-bash-4.2$ sudo pip install -r requirements.txt

DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support

Requirement already satisfied: networkx>=1.10 in /usr/lib/python2.7/site-packages (from -r requirements.txt (line 5)) (2.2)

Requirement already satisfied: pbr!=2.1.0,>=2.0.0 in /usr/lib/python2.7/site-packages (from -r requirements.txt (line 6)) (5.4.5)

Requirement already satisfied: PyYAML>=3.12 in /usr/lib64/python2.7/site-packages (from -r requirements.txt (line 7)) (5.3.1)

Requirement already satisfied: stevedore>=1.20.0 in /usr/lib/python2.7/site-packages (from -r requirements.txt (line 8)) (1.32.0)

Requirement already satisfied: flake8<4.0.0,>=3.6.0 in /usr/lib/python2.7/site-packages (from -r requirements.txt (line 10)) (3.8.3)

Requirement already satisfied: decorator>=4.3.0 in /usr/lib/python2.7/site-packages (from networkx>=1.10->-r requirements.txt (line 5)) (4.4.2)

Requirement already satisfied: six>=1.10.0 in /usr/lib/python2.7/site-packages (from stevedore>=1.20.0->-r requirements.txt (line 8)) (1.15.0)

Requirement already satisfied: configparser; python_version < "3.2" in /usr/lib/python2.7/site-packages (from flake8<4.0.0,>=3.6.0->-r requirements.txt (line 10)) (4.0.2)

Requirement already satisfied: pycodestyle<2.7.0,>=2.6.0a1 in /usr/lib/python2.7/site-packages (from flake8<4.0.0,>=3.6.0->-r requirements.txt (line 10)) (2.6.0)

Requirement already satisfied: mccabe<0.7.0,>=0.6.0 in /usr/lib/python2.7/site-packages (from flake8<4.0.0,>=3.6.0->-r requirements.txt (line 10)) (0.6.1)

Requirement already satisfied: pyflakes<2.3.0,>=2.2.0 in /usr/lib/python2.7/site-packages (from flake8<4.0.0,>=3.6.0->-r requirements.txt (line 10)) (2.2.0)

Requirement already satisfied: enum34; python_version < "3.4" in /usr/lib/python2.7/site-packages (from flake8<4.0.0,>=3.6.0->-r requirements.txt (line 10)) (1.1.10)

Requirement already satisfied: functools32; python_version < "3.2" in /usr/lib/python2.7/site-packages (from flake8<4.0.0,>=3.6.0->-r requirements.txt (line 10)) (3.2.3-2)

Requirement already satisfied: importlib-metadata; python_version < "3.8" in /usr/lib/python2.7/site-packages (from flake8<4.0.0,>=3.6.0->-r requirements.txt (line 10)) (1.7.0)

Requirement already satisfied: typing; python_version < "3.5" in /usr/lib/python2.7/site-packages (from flake8<4.0.0,>=3.6.0->-r requirements.txt (line 10)) (3.7.4.3)

Requirement already satisfied: contextlib2; python_version < "3" in /usr/lib/python2.7/site-packages (from importlib-metadata; python_version < "3.8"->flake8<4.0.0,>=3.6.0->-r requirements.txt (line 10)) (0.6.0.post1)

Requirement already satisfied: pathlib2; python_version < "3" in /usr/lib/python2.7/site-packages (from importlib-metadata; python_version < "3.8"->flake8<4.0.0,>=3.6.0->-r requirements.txt (line 10)) (2.3.5)

Requirement already satisfied: zipp>=0.5 in /usr/lib/python2.7/site-packages (from importlib-metadata; python_version < "3.8"->flake8<4.0.0,>=3.6.0->-r requirements.txt (line 10)) (1.2.0)

Requirement already satisfied: scandir; python_version < "3.5" in /usr/lib64/python2.7/site-packages (from pathlib2; python_version < "3"->importlib-metadata; python_version < "3.8"->flake8<4.0.0,>=3.6.0->-r requirements.txt (line 10)) (1.10.0)

-bash-4.2$

-bash-4.2$ sudo python setup.py install

..................................................................................................................

Installing ramdisk-image-create script to /usr/bin

Installing dib-block-device script to /usr/bin

Installing element-info script to /usr/bin

Installing disk-image-create script to /usr/bin

-bash-4.2$

-bash-4.2$ sudo yum install -y qemu kpartx

Loaded plugins: fastestmirror, langpacks

Loading mirror speeds from cached hostfile

......................................................................................................................

Installed:

qemu.x86_64 2:2.0.0-1.el7.6

Dependency Installed:

qemu-common.x86_64 2:2.0.0-1.el7.6 qemu-system-alpha.x86_64 2:2.0.0-1.el7.6 qemu-system-arm.x86_64 2:2.0.0-1.el7.6

qemu-system-cris.x86_64 2:2.0.0-1.el7.6 qemu-system-lm32.x86_64 2:2.0.0-1.el7.6 qemu-system-m68k.x86_64 2:2.0.0-1.el7.6

qemu-system-microblaze.x86_64 2:2.0.0-1.el7.6 qemu-system-mips.x86_64 2:2.0.0-1.el7.6 qemu-system-moxie.x86_64 2:2.0.0-1.el7.6

qemu-system-or32.x86_64 2:2.0.0-1.el7.6 qemu-system-s390x.x86_64 2:2.0.0-1.el7.6 qemu-system-sh4.x86_64 2:2.0.0-1.el7.6

qemu-system-unicore32.x86_64 2:2.0.0-1.el7.6 qemu-system-x86.x86_64 2:2.0.0-1.el7.6 qemu-system-xtensa.x86_64 2:2.0.0-1.el7.6

qemu-user.x86_64 2:2.0.0-1.el7.6 vte3.x86_64 0:0.36.5-1.el7

Complete!

-bash-4.2$

-bash-4.2$ disk-image-create -h

Usage: disk-image-create [OPTION]... [ELEMENT]...

Options:

-a i386|amd64|armhf|arm64 -- set the architecture of the image(default amd64)

-o imagename -- set the imagename of the output image file(default image)

-t qcow2,tar,tgz,squashfs,vhd,docker,aci,raw -- set the image types of the output image files (default qcow2)

   File types should be comma separated. VHD outputting requires the vhd-util

   executable be in your PATH. ACI outputting requires the ACI_MANIFEST

   environment variable be a path to a manifest file.

-x -- turn on tracing (use -x -x for very detailed tracing).

-u -- uncompressed; do not compress the image - larger but faster

-c -- clear environment before starting work

--logfile -- save run output to given logfile (implies DIB_QUIET=1)

--checksum -- generate MD5 and SHA256 checksum files for the created image

--image-size size -- image size in GB for the created image

--image-extra-size size -- extra image size in GB for the created image

--image-cache directory -- location for cached images(default ~/.cache/image-create)

--max-online-resize size -- max number of filesystem blocks to support when resizing.

   Useful if you want a really large root partition when the image is deployed.

   Using a very large value may run into a known bug in resize2fs.

   Setting the value to 274877906944 will get you a 1PB root file system.

   Making this value unnecessarily large will consume extra disk space

   on the root partition with extra file system inodes.

--min-tmpfs size -- minimum size in GB needed in tmpfs to build the image

--mkfs-journal-size -- filesystem journal size in MB to pass to mkfs.

--mkfs-options -- option flags to be passed directly to mkfs.

   Options should be passed as a single string value.

--no-tmpfs -- do not use tmpfs to speed image build

--offline -- do not update cached resources

--qemu-img-options -- option flags to be passed directly to qemu-img.

   Options need to be comma separated, and follow the key=value pattern.

--root-label label -- label for the root filesystem.  Defaults to 'cloudimg-rootfs'.

--ramdisk-element -- specify the main element to be used for building ramdisks.

   Defaults to 'ramdisk'.  Should be set to 'dracut-ramdisk' for platforms such

   as RHEL and CentOS that do not package busybox.

--install-type -- specify the default installation type. Defaults to 'source'. Set to 'package' to use package based installations by default.

--docker-target -- specify the repo and tag to use if the output type is docker. Defaults to the value of output imagename

-n skip the default inclusion of the 'base' element

-p package[,p2...] [-p p3] -- extra packages to install in the image.  Runs once, after 'install.d' phase.  Can be specified multiple times

-h|--help -- display this help and exit

--version -- display version and exit

Environment Variables:

(this is not a complete list)

  • ELEMENTS_PATH: specify external locations for the elements. As for $PATH

  • DIB_NO_TIMESTAMP: no timestamp prefix on output. Useful if capturing output

  • DIB_QUIET: 1=do not output log output to stdout; 0=always ouptut to stdout. See --logfile

NOTE: At least one distribution root element must be specified.

NOTE: If using the VHD output format you need to have a patched version of vhd-util installed for the image

  to be bootable. The patch is available here: https://github.com/emonty/vhd-util/blob/master/debian/patches/citrix

  and a PPA with the patched tool is available here: https://launchpad.net/~openstack-ci-core/+archive/ubuntu/vhd-util

Examples:

disk-image-create -a amd64 -o ubuntu-amd64 vm ubuntu

export ELEMENTS_PATH=~/source/tripleo-image-elements/elements

disk-image-create -a amd64 -o fedora-amd64-heat-cfntools vm fedora heat-cfntools

-bash-4.2$

-bash-4.2$ export DIB_DEV_USER_USERNAME=centos

-bash-4.2$ export DIB_DEV_USER_PASSWORD=centos

-bash-4.2$ export DIB_DEV_USER_PWDLESS_SUDO=YES

-bash-4.2$ export DIB_CLOUD_INIT_DATASOURCES="ConfigDrive, OpenStack"

-bash-4.2$ disk-image-create -a amd64 -t qcow2 -o /opt/stack/images/centos7.qcow2 centos7 vm cloud-init-datasources devuser

mkdir: cannot create directory '/opt/diskimagebuilder//.cache': Permission denied

-bash-4.2$ sudo disk-image-create -a amd64 -t qcow2 -o /opt/stack/images/centos7.qcow2 centos7 vm cloud-init-datasources devuser

2020-08-04 15:36:42.256 | diskimage-builder version 3.1.0

2020-08-04 15:36:42.257 | Building elements: base centos7 vm cloud-init-datasources devuser block-device-mbr

2020-08-04 15:36:42.316 | Expanded element dependencies to: bootloader dib-python rpm-distro install-types install-bin centos redhat-common block-device-mbr sysprep devuser cache-url vm manifests centos7 base pkg-map yum dib-init-system cloud-init-datasources source-repositories package-installs

2020-08-04 15:36:42.340 | WARNING: Not enough RAM to use tmpfs for build. Using /tmp. (3861300 < 4G)

2020-08-04 15:36:42.340 | Building in /tmp/dib_build.hqsx3Twm

...................................................................................................................

2020-08-04 15:40:39.010 | INFO diskimage_builder.block_device.blockdevice [-] State already cleaned - no way to do anything here

-bash-4.2$

试验至此说明Disk Image builder已经可以定制LinuxOS镜像了,只是我本次使用的虚拟机存储空间已经不足,在最终的镜像构建过程中一定会失败罢了。

DIB,一个构建Linux系统镜像的工具_第1张图片