虚拟生成器 是一个命令行工具,用于构建各种本地或云使用的虚拟机映像,既轻松又快速。 它还具有许多自定义图像的选项。 您可以在 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
如您所见,有多个可用的操作系统模板。
在构建虚拟机映像之前,您可能需要查看来宾操作系统的安装说明以了解其中的内容。
例如,要查看 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%)