运行自定义映像。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
相關主題
總結
以上是生活随笔為你收集整理的运行自定义映像。linux,安装和使用virt-customize自定义Qcow2/Raw Linux OS磁盘映像的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 直面互联客户体验的新现实
- 下一篇: 七种必看的盘口语言