运行自定义映像。linux,安装和使用virt-customize自定义Qcow2/Raw Linux OS磁盘映像

本文将讨论如何使用virt-customize自定义Qcow2映像,QCOW代表(QEMU Copy On Write),是QEMU处理器仿真器支持的磁盘映像格式之一,它使用磁盘存储优化策略,在实际需要之前延迟存储分配,Qcow2旨在取代qcow图像格式,这些图像格式通常用于OpenStack Platform、KVM管理程序和oVirt/RHEV虚拟化平台,在这篇文章中,我们将操作一些自定义Qcow图像格式的示例,同时保持数据完整性和图像大小的小消耗。

virt-customize简介

virt-customize命令行工具由libguestfs-tools包提供,可用于在各种Linux发行版上安装, Virt-customize可以通过安装软件包,编辑配置文件等来自定义虚拟机(磁盘映像),它通过修改guest虚拟机或磁盘映像来实现此目的,它适用于raw和qcow2图像格式。

在Linux上安装virt-customize

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

1、CentOS/RHEL

$ sudo yum -y install libguestfs-tools

2、Ubuntu/Debian

$ sudo apt-get -y install libguestfs-tools

3、Arch/Manjaro

$ yaourt -S --noconfirm --needed libguestfs

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

$ virt-customize --help

如果正在处理正在运行的虚拟机映像,请在进行就地编辑之前将其停止,在KVM上(在Debian 10 Buster系统中安装KVM虚拟化的方法),通过以下方式执行此操作:

sudo virsh shutdown

virt-customize使用示例

我们将介绍一些如何使用virt-customize自定义Qcow2和Raw OS图像格式的示例。

首先,访问集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是要修改图像的名称。

StrongRootPassword是为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]'

注:

[username]-替换为有效的用户名,例如–username=admin。

[password]-替换为提供的用户名的有效密码。

-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.设置时区

# virt-customize -a rhel-server-7.6.qcow2 --timezone "Asia/Shanghai"

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

相关主题

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值