使用Virt-builder快速构建虚拟机镜像
Virt-builder是一种命令行工具,可轻松快速地构建供本地或者云使用的各种虚拟机镜像。
它还具有许多自定义图像的选项。
我们可以在VM镜像上安装新的应用程序,设置主机名,设置root密码,在来宾VM首次引导时运行命令或者脚本,在磁盘镜像中添加或者编辑文件等等。
所有这些任务都可以从命令行完成,并且不需要root权限。
Virt-builder会下载经过精心准备的,经过数字签名的OS模板,因此我们不必手动安装OS。
我们所要做的只是使用Virt-manager GUI或者Virt-install命令行工具立即启动具有预定义模板的VM。
Virt-builder为流行的Linux和Unix变体提供了最少的OS模板。
当然,我们也可以创建自己的模板。
在Linux上安装Virt-builder
Virt-builder是Libguestfs库的一部分,因此请确保已按照以下教程中的说明进行安装。
- 使用Libguestfs访问和修改虚拟机磁盘镜像
使用Virt-builder构建虚拟机镜像
使用Virt-builder构建虚拟机的镜像非常简单明了。
列出可用的虚拟机模板
首先,列出可用的操作系统模板。
为此,请运行:
$virt-builder --list
在撰写本教程时,可以使用以下模板:
centos-7.5 x86_64 CentOS 7.5 centos-7.6 x86_64 CentOS 7.6 centos-7.7 x86_64 CentOS 7.7 centos-7.8 x86_64 CentOS 7.8 centos-8.0 x86_64 CentOS 8.0 centos-8.2 x86_64 CentOS 8.2 cirros-0.3.1 x86_64 CirrOS 0.3.1 cirros-0.3.5 x86_64 CirrOS 0.3.5 debian-10 x86_64 Debian 10 (buster) debian-6 x86_64 Debian 6 (Squeeze) debian-7 sparc64 Debian 7 (Wheezy) (sparc64) debian-7 x86_64 Debian 7 (wheezy) debian-8 x86_64 Debian 8 (jessie) debian-9 x86_64 Debian 9 (stretch) fedora-30 x86_64 Fedora® 30 Server fedora-31 x86_64 Fedora® 31 Server fedora-32 x86_64 Fedora® 32 Server freebsd-11.1 x86_64 FreeBSD 11.1 scientificlinux-6 x86_64 Scientific Linux 6.5 ubuntu-10.04 x86_64 Ubuntu 10.04 (Lucid) ubuntu-12.04 x86_64 Ubuntu 12.04 (Precise) ubuntu-14.04 x86_64 Ubuntu 14.04 (Trusty) ubuntu-16.04 x86_64 Ubuntu 16.04 (Xenial) ubuntu-18.04 x86_64 Ubuntu 18.04 (bionic) opensuse-13.1 x86_64 openSUSE 13.1 opensuse-13.2 x86_64 openSUSE 13.2 opensuse-42.1 x86_64 openSUSE Leap 42.1 opensuse-tumbleweed x86_64 openSUSE Tumbleweed
如我们所见,有多个可用的OS模板。
在构建虚拟机镜像之前,我们可能需要查看来宾OS的安装说明,以了解其中的内容。
例如,要查看Debian 10的安装说明,请运行:
$virt-builder --notes debian-10
输出示例:
Debian 10 (buster) This is a minimal Debian install. This image is so very minimal that it only includes an ssh server This image does not contain SSH host keys. To regenerate them use: --firstboot-command "dpkg-reconfigure openssh-server" This template was generated by a script in the libguestfs source tree: builder/templates/make-template.ml Associated files used to prepare this template can be found in the same directory.
建立虚拟机镜像
我想将OS模板下载到特定目录中,因此创建了以下目录:
$mkdir virtbuilder $cd virtbuilder/
让我们使用以下命令构建Debian 10虚拟机:
$virt-builder debian-10
输出示例:
[ 4.8] Downloading: http://builder.libguestfs.org/debian-10.xz ########################### 100.0% [ 83.2] Planning how to build this image [ 83.2] Uncompressing [ 101.2] Opening the new disk [ 119.8] Setting a random seed virt-builder: warning: random seed could not be set for this type of guest [ 119.9] Setting passwords virt-builder: Setting random password of root to 66xW1CaIqfM8km2v [ 121.5] Finishing off Output file: debian-10.img Output size: 6.0G Output format: raw Total usable space: 5.8G Free space: 4.9G (84%)
如我们所见,此命令已构建了最小的Debian 10镜像。
它不会有任何用户帐户。
它只有随机的root密码和最少安装的软件。
图像的输出名称应与模板名称相同。
我们可以根据需要使用-o选项更改它:
$virt-builder debian-10 -o theitroad.img
默认情况下,图像格式为img。
我们可以将其转换为其他格式,例如Qcow2,如下所示:
$virt-builder debian-10 --format qcow2
默认情况下,Virt-builder将构建与主机OS体系结构相同的镜像。
例如,如果主机操作系统是64位,则它将构建64位VM。
我们可以使用--arch选项将此值更改为32位(如果可用)。
$virt-builder debian-10 --arch i686
是否要构建自定义尺寸的图像?
这也是可能的。
以下命令将构建大小为50 GB的VM:
$virt-builder debian-10 --size 50G
在将客户机OS复制到输出时,将使用virt-resize命令自动调整其大小。
设置root密码
就像我已经提到的那样,在构建镜像时,将为root用户帐户分配一个随机密码。
如果要为root用户设置特定的密码,请使用--root-password选项,如下所示:
$virt-builder centos-8.2 --format qcow2 --root-password password:centos
输出示例:
[ 5.1] Downloading: http://builder.libguestfs.org/centos-8.2.xz ########################### 100.0% [ 249.2] Planning how to build this image [ 249.2] Uncompressing [ 271.3] Converting raw to qcow2 [ 281.1] Opening the new disk [ 319.9] Setting a random seed [ 320.4] Setting passwords [ 323.0] Finishing off Output file: centos-8.2.qcow2 Output size: 6.0G Output format: qcow2 Total usable space: 5.3G Free space: 4.0G (74%)
上面的命令将构建CentOS 8.2镜像并将root用户的密码分配为“ centos”。
我们还可以通过文本文件设置密码:
$virt-builder centos-8.2 --root-password file:~/theitroad.txt
要禁用root密码,请运行:
$virt-builder centos-8.2 --root-password disabled
锁定根帐户:
$virt-builder centos-8.2 --root-password locked
锁定root帐户并禁用root密码:
$virt-builder centos-8.2 --root-password locked:disabled
要分配root密码但锁定root帐户,请使用以下选项:
--root-password locked:file:FILENAME --root-password locked:password:PASSWORD
在使用“ passwd -u”命令解锁root用户之后,我们可以使用root密码。
建立使用者
要在构建虚拟机镜像时创建用户帐户,请运行:
$virt-builder centos-8.2 --firstboot-command 'useradd -m -p "" sk ; chage -d 0 sk'
上面的命令将创建一个没有密码的名为“ sk”的用户,并强制他在首次登录时设置密码。
设置主机名
要将主机名设置为VM:
$virt-builder centos-8.2 --hostname virt.theitroad.local
输出示例:
[ 4.7] Downloading: http://builder.libguestfs.org/centos-8.2.xz [ 7.2] Planning how to build this image [ 7.2] Uncompressing [ 31.0] Opening the new disk [ 41.9] Setting a random seed [ 42.0] Setting the hostname: virt.theitroad.local [ 42.1] Setting passwords virt-builder: Setting random password of root to MRn7fj1GSaeCAHQx [ 44.4] Finishing off Output file: centos-8.2.img Output size: 6.0G Output format: raw Total usable space: 5.3G Free space: 4.0G (74%)
在VM镜像上安装软件
要在虚拟机上安装软件包,请运行:
$virt-builder debian-10 --install vim
输出示例:
[ 5.8] Downloading: http://builder.libguestfs.org/debian-10.xz [ 7.4] Planning how to build this image [ 7.4] Uncompressing [ 25.3] Opening the new disk [ 29.7] Setting a random seed virt-builder: warning: random seed could not be set for this type of guest [ 29.8] Installing packages: vim [ 93.2] Setting passwords virt-builder: Setting random password of root to 45Hj5yxh8vRqLDcu [ 94.9] Finishing off Output file: debian-10.img Output size: 6.0G Output format: raw Total usable space: 5.8G Free space: 4.8G (82%)
要安装多个软件包,请在引号中用逗号分隔,如下所示:
$virt-builder debian-10 --install "apache2,htop"
更新虚拟机中的所有软件包:
$virt-builder centos-8.2 --update
如果虚拟机使用SELinux,则在安装或者更新软件包后需要重新标记SELinux:
$virt-builder centos-8.2 --update --selinux-relabel
自定义VM镜像
Virt-builder有许多选项可以自定义图像。
例如,我们可以在VM首次启动时使用以下命令运行特定的命令/脚本:
$virt-builder debian-10 --firstboot-command 'apt -y update'
要在VM中添加一行,请运行:
$virt-builder centos-8.2 --append-line '/etc/hosts:192.168.225.1 server.theitroad.local'
缓存模板
默认情况下,所有模板都将首次从网络下载。
由于模板的大小很大,因此下载的模板将被缓存在用户的主目录中。
我们可以使用以下命令打印缓存目录的详细信息以及当前缓存的模板:
$virt-builder --print-cache
输出示例:
cache directory: /home/sk/.cache/virt-builder [...] centos-7.8 x86_64 no centos-8.0 x86_64 no centos-8.2 x86_64 cached cirros-0.3.1 x86_64 no cirros-0.3.5 x86_64 no debian-10 x86_64 cached debian-6 x86_64 no debian-7 sparc64 no [...]
我们还可以通过手动查看缓存文件夹来验证它:
$ls $HOME/.cache/virt-builder centos-8.2.x86_64.1 debian-10.x86_64.1
要将所有可用模板下载到本地缓存文件夹,请运行:
$virt-builder --cache-all-templates
如果我们不想在构建镜像时缓存模板,请使用--no-cache选项。
要删除所有缓存的模板,请运行:
$virt-builder --delete-cache [ 0.0] Deleting: /home/sk/.cache/virt-builder
将磁盘镜像导入虚拟机监控程序
好了,我们已经下载了所需的操作系统并根据自己的喜好对其进行了自定义。
然后
只需导入镜像,然后使用新创建的带有虚拟机管理程序的磁盘镜像来运行VM。
我们已经编写了逐步教程,以使用Qcow2镜像创建KVM虚拟机。
本教程是专门为Qcow2编写的,但是它也与导入.img格式的磁盘镜像相同。
- 在Linux中使用Qcow2镜像创建KVM虚拟机
Virt-builder具有数百个命令和选项。
其中我仅介绍了基本命令。
有关更多详细信息,请参见手册使用年龄。
$man virt-builder