如何使用Virt-Customize自定义QCOW2 /原始Linux OS磁盘镜像

时间:2020-02-23 14:45:10  来源:igfitidea点击:

本文将讨论如何使用Virt-Customize自定义QCOW2图像。
代表(写入QEMU Copy)的QCow是QEMU处理器仿真器支持的磁盘镜像格式之一。
它使用磁盘存储优化策略,延迟存储的分配,直到实际需要。

Qcow2旨在取代QCOM图像格式。
这些图像格式通常用于OpenStack平台,KVM虚拟机管理程序和Ovirt/Rhev虚拟化平台。
在此帖子中,我们将考虑一些自定义QCOM图像格式的示例,而在图像大小上保留数据完整性和小开销。

什么是virt-customize?

Virt-Customize命令行工具由LibGuestFS-Tools包提供可用于安装各种Linux发行版。
Virt-Customize可以通过安装包,编辑配置文件等来自定义虚拟机(磁盘镜像)。
它通过修改访客或者磁盘镜像到位来实现这一点。
它适用于原始和QCOW2图像格式。

如何在Linux上安装virt-customize

通过libguestfs-tools安装virt-customize命令行工具。

---- CentOS/RHEL ----
$sudo yum -y install libguestfs-tools
---- Ubuntu/Debian ---
$sudo apt-get -y install libguestfs-tools
---- Arch/Manjaro ---
$yaourt -S --noconfirm --needed libguestfs

通过查看帮助页面确认安装。

$virt-customize --help

如果在运行的虚拟机图像上工作,请在进行编辑之前停止它。
在KVM上,通过:

sudo virsh shutdown <domain-name or ID>

right-自定义使用示例

让我们涵盖有关如何使用Virt-Customize自定义QCOW2和RAW OS图像格式的一些示例。

首先,访问SET LIBGUESTFS_BACKEND以直接。

export LIBGUESTFS_BACKEND=direct

1.设置root密码

要设置root密码,请使用命令:

# virt-customize -a rhel-server-7.6.qcow2 --root-password password:StrongRootPassword
[   0.0] Examining the guest ...
[   1.9] Setting a random seed
[   1.9] Setting passwords
[   6.8] Finishing off

其中:RHEL-SERVER-7.6.QCOW2是要修改的图像的名称是要为root用户设置的密码。

2.注册rhel系统

要注册RHEL图像并订阅可用池,请使用命令:

$virt-customize -a overcloud-full.qcow2 --run-command 'subscription-manager register --username=[username] --password=[password]'
[   0.0] Examining the guest ...
[   2.0] Setting a random seed
[   2.0] Running: subscription-manager register --username=user1 --password=mypassword
[  38.5] Finishing off
$virt-customize -a rhel-server-7.6.qcow2 --run-command 'subscription-manager attach --pool [subscription-pool]'

其中:[用户名] - 用有效的用户名替换 - e.g -Username = admin [密码] - 替换为提供的用户名的有效密码

-run-command选项用于在虚拟镜像文件中执行任何命令。

3.在图像中安装软件包

可以使用该命令安装软件包在QCOW2或者原始磁盘镜像中:

$virt-customize -a rhel-server-7.6.qcow2 --install [vim,bash-completion,wget,curl,telnet,unzip]
[   0.0] Examining the guest ...
[   2.1] Setting a random seed
[   2.1] Installing packages: [vim bash-completion wget curl telnet unzip]
[ 563.2] Finishing off
$virt-customize -a rhel-server-7.6.qcow2 --install net-tools

4.上传文件

请参见下面的示例:

$virt-customize -a rhel-server-7.6.qcow2 --upload rhsm.conf:/etc/rhsm/rhsm.conf
[   0.0] Examining the guest ...
[   2.9] Setting a random seed
[   3.0] Setting the machine ID in /etc/machine-id
[   3.0] Uploading: rhsm.conf to /etc/rhsm/rhsm.conf
[   3.4] Finishing off
# virt-customize -a rhel-server-7.6.qcow2 --upload yum.conf:/etc/yum.conf
[   0.0] Examining the guest ...
[   1.9] Setting a random seed
[   1.9] Uploading: yum.conf to /etc/yum.conf
[   2.2] Finishing off
# virt-customize -a rhel-server-7.6.qcow2 --upload proxy.sh:/etc/profile.d/
[   0.0] Examining the guest ...
[   1.9] Setting a random seed
[   1.9] Uploading: proxy.sh to /etc/profile.d/
[   2.3] Finishing off

格式为:local_file_path:image_file_path

5.设置时区

我们还可以在OS镜像文件上设置TimeZone:

# virt-customize -a rhel-server-7.6.qcow2 --timezone "Africa/Nairobi"

6.上传SSH公钥

上传用户的SSH公钥:

$virt-customize -a rhel-server-7.6.qcow2  --ssh-inject jmutai:file:./id_rsa.pub
[   0.0] Examining the guest ...
[   1.9] Setting a random seed
[   2.0] SSH key inject: jmutai
[   3.2] Finishing off

7. Relabel Selinux.

要重新标记SELinux文件上下文,请使用:

$virt-customize -a rhel-server-7.6.qcow2 --selinux-relabel
[   0.0] Examining the guest ...
[   2.0] Setting a random seed
[   2.0] SELinux relabelling
[   8.6] Finishing off

有关更多命令使用选项,请检查:

$man virt-customize
$virt-customize --help