虚拟生成器 是一个命令行工具,用于轻松快速地构建各种虚拟机映像以供本地或云使用。 它还具有许多自定义图像的选项。 您可以在 VM 映像上安装新应用程序、设置主机名、设置 root 密码、在来宾 VM 首次启动时运行命令或脚本、在磁盘映像中添加或编辑文件等等。 所有这些任务都可以从命令行完成,不需要 root 权限。
Virt-builder 会下载准备好的、经过数字签名的操作系统模板,因此您不必手动安装操作系统。 您所要做的就是使用 虚拟管理器 图形用户界面或 虚拟安装 使用预定义模板立即启动虚拟机的命令行工具。 Virt-builder 为流行的 Linux 和 Unix 变体提供最小的操作系统模板。 当然,您也可以创建自己的模板。
在 Linux 上安装 Virt-builder
Virt-builder 是 Libguestfs 库的一部分,因此请确保您已按照以下指南中的说明安装它。
- 使用 Libguestfs 访问和修改虚拟机磁盘映像
使用 Virt-builder 构建虚拟机映像
使用 Virt-builder 构建虚拟机映像非常简单直接。
列出可用的虚拟机模板
首先,列出可用的操作系统模板。 为此,请运行:
$ virt-builder --list
在编写本指南时,以下模板可用:
centos-6 x86_64 CentOS 6.6 centos-7.0 x86_64 CentOS 7.0 centos-7.1 x86_64 CentOS 7.1 centos-7.2 aarch64 CentOS 7.2 (aarch64) centos-7.2 x86_64 CentOS 7.2 centos-7.3 x86_64 CentOS 7.3 centos-7.4 x86_64 CentOS 7.4 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-26 aarch64 Fedora® 26 Server (aarch64) fedora-26 armv7l Fedora® 26 Server (armv7l) fedora-26 i686 Fedora® 26 Server (i686) fedora-26 ppc64 Fedora® 26 Server (ppc64) fedora-26 ppc64le Fedora® 26 Server (ppc64le) fedora-26 x86_64 Fedora® 26 Server fedora-27 aarch64 Fedora® 27 Server (aarch64) fedora-27 armv7l Fedora® 27 Server (armv7l) fedora-27 i686 Fedora® 27 Server (i686) fedora-27 ppc64 Fedora® 27 Server (ppc64) fedora-27 ppc64le Fedora® 27 Server (ppc64le) fedora-27 x86_64 Fedora® 27 Server fedora-28 i686 Fedora® 28 Server (i686) fedora-28 x86_64 Fedora® 28 Server fedora-29 aarch64 Fedora® 29 Server (aarch64) fedora-29 i686 Fedora® 29 Server (i686) fedora-29 ppc64le Fedora® 29 Server (ppc64le) fedora-29 x86_64 Fedora® 29 Server fedora-30 aarch64 Fedora® 30 Server (aarch64) fedora-30 i686 Fedora® 30 Server (i686) 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) fedora-18 x86_64 Fedora® 18 fedora-19 x86_64 Fedora® 19 fedora-20 x86_64 Fedora® 20 fedora-21 aarch64 Fedora® 21 Server (aarch64) fedora-21 armv7l Fedora® 21 Server (armv7l) fedora-21 ppc64 Fedora® 21 Server (ppc64) fedora-21 ppc64le Fedora® 21 Server (ppc64le) fedora-21 x86_64 Fedora® 21 Server fedora-22 aarch64 Fedora® 22 Server (aarch64) fedora-22 armv7l Fedora® 22 Server (armv7l) fedora-22 i686 Fedora® 22 Server (i686) fedora-22 x86_64 Fedora® 22 Server fedora-23 aarch64 Fedora® 23 Server (aarch64) fedora-23 armv7l Fedora® 23 Server (armv7l) fedora-23 i686 Fedora® 23 Server (i686) fedora-23 ppc64 Fedora® 23 Server (ppc64) fedora-23 ppc64le Fedora® 23 Server (ppc64le) fedora-23 x86_64 Fedora® 23 Server fedora-24 aarch64 Fedora® 24 Server (aarch64) fedora-24 armv7l Fedora® 24 Server (armv7l) fedora-24 i686 Fedora® 24 Server (i686) fedora-24 x86_64 Fedora® 24 Server fedora-25 aarch64 Fedora® 25 Server (aarch64) fedora-25 armv7l Fedora® 25 Server (armv7l) fedora-25 i686 Fedora® 25 Server (i686) fedora-25 ppc64 Fedora® 25 Server (ppc64) fedora-25 ppc64le Fedora® 25 Server (ppc64le) fedora-25 x86_64 Fedora® 25 Server 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
使用 Virt-builder 列出可用的虚拟机
如您所见,有多个可用的操作系统模板。
在构建虚拟机映像之前,您可能需要查看来宾操作系统的安装说明以了解其中的内容。
为了 example,要查看 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.
构建虚拟机镜像
我想下载特定目录中的操作系统模板,所以我创建了这个目录:
$ mkdir virtbuilder $ cd virtbuilder/
让我们使用命令构建 Debian 10 虚拟机:
$ virt-builder debian-10
样本输出:
[ 4.8] Downloading: https://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%)
在 Linux 中使用 virt-builder 构建虚拟机映像
如您所见,此命令构建了最小的 Debian 10 映像。 它不会有任何用户帐户。 它只有随机的 root 密码和最低安装的软件。
图像的输出名称应与模板名称相同。 您可以根据自己的喜好使用 -o 选项:
$ virt-builder debian-10 -o ostechnix.img
默认情况下,图像格式为 图像. 您可以将其转换为不同的格式,例如 example Qcow2,如下图:
$ virt-builder debian-10 --format qcow2
默认情况下,Virt-builder 将构建与主机操作系统架构相同的映像。 为了 example,如果您的主机操作系统是 64 位,那么它将构建 64 位 VM。 您可以使用以下方法将其更改为 32 位(如果可用) – 拱 选项。
$ virt-builder debian-10 --arch i686
想要构建自定义大小的图像? 这也是可能的。 以下命令将构建一个大小为 50 GB 的 VM:
$ virt-builder debian-10 --size 50G
来宾操作系统使用自动调整大小 虚拟调整大小 命令,因为它被复制到输出。
设置root密码
就像我已经提到的那样,在构建映像时将为 root 用户帐户分配一个随机密码。 如果要为 root 用户设置特定密码,请使用 –root密码 选项如下:
$ virt-builder centos-8.2 --format qcow2 --root-password password:centos
样本输出:
[ 5.1] Downloading: https://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 用户分配密码 “仙人掌”.
您还可以从文本文件中设置密码:
$ virt-builder centos-8.2 --root-password file:~/ostechnix.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
我们可以在解锁root用户后使用root密码 “密码-u” 命令。
创建用户
要在构建虚拟机映像时创建用户帐户,请运行:
$ virt-builder centos-8.2 --firstboot-command 'useradd -m -p "" sk ; chage -d 0 sk'
上面的命令将创建一个没有密码的名为“sk”的用户,并强制他在第一次登录时设置密码。
设置主机名
要将主机名设置为 VM:
$ virt-builder centos-8.2 --hostname virt.ostechnix.local
样本输出:
[ 4.7] Downloading: https://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.ostechnix.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 映像上安装软件
要在 VM 上安装软件包,请运行:
$ virt-builder debian-10 --install vim
样本输出:
[ 5.8] Downloading: https://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"
更新 VM 中的所有包:
$ virt-builder centos-8.2 --update
如果您的 VM 使用 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.ostechnix.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
如果您不想在构建图像时缓存模板,请使用 –无缓存 选项。
要删除所有缓存的模板,请运行:
$ 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