对于 CentOS/RHEL 7
要访问 CentOS/RHEL 7 KVM 主机上来宾的 qcow2 磁盘镜像,请使用 qemu-nbd 或者 libguestfs 。
使用 qemu-nbd
按照以下步骤使用 qemu-nbd 在 CentOS/RHEL 7 中挂载 qcow2 镜像。
- 在 KVM 主机上启用 NBD 内核驱动程序。
# modprobe nbd
- 连接 qcow2 镜像作为网络块设备。
# qemu-nbd --connect=/dev/nbd0 olseven.qcow2 -f qcow2
- 使用fdisk查找镜像中的分区
# fdisk /dev/nbd0 -l Disk /dev/nbd0: 32.2 GB, 32212254720 bytes, 62914560 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x0009e4c2 Device Boot Start End Blocks Id System /dev/nbd0p1 * 2048 2099199 1048576 83 Linux /dev/nbd0p2 2099200 62914559 30407680 8e Linux LVM
- 设备/dev/nbd0p2 是LVM,因此我们需要在KVM 主机中找到新的PV/VG 和LV。
# pvscan PV /dev/sda3 VG ol_kvm lvm2 [<363.23 GiB / 8.00 MiB free] Total: 1 [<363.23 GiB] / in use: 1 [<363.23 GiB] / in no VG: 0 [0 ]
刷新物理卷缓存,以便主机识别新的 PV。
# pvscan --cache
# pvscan PV /dev/sda3 VG ol_kvm lvm2 [<363.23 GiB / 8.00 MiB free] PV /dev/nbd0p2 VG ol_guestol lvm2 [<29.00 GiB / 4.00 MiB free] >> Now its listed. Total: 2 [392.22 GiB] / in use: 2 [392.22 GiB] / in no VG: 0 [0 ]
# vgscan Reading volume groups from cache. Found volume group "ol_kvm" using metadata type lvm2 Found volume group "ol_guestol" using metadata type lvm2 >> The VG in the image.
# lvscan ACTIVE '/dev/ol_kvm/root' [<46.57 GiB] inherit ACTIVE '/dev/ol_kvm/home' [206.75 GiB] inherit ACTIVE '/dev/ol_kvm/var' [<102.45 GiB] inherit ACTIVE '/dev/ol_kvm/swap' [7.45 GiB] inherit inactive '/dev/ol_guestol/swap' [2.00 GiB] inherit >> LV in the image. inactive '/dev/ol_guestol/root' [26.99 GiB] inherit >> LV in the image.
- 激活来宾操作系统的 VG。
# vgchange -ay 4 logical volume(s) in volume group "ol_kvm" now active 2 logical volume(s) in volume group "ol_guestol" now active
# lvscan ACTIVE '/dev/ol_kvm/root' [<46.57 GiB] inherit ACTIVE '/dev/ol_kvm/home' [206.75 GiB] inherit ACTIVE '/dev/ol_kvm/var' [<102.45 GiB] inherit ACTIVE '/dev/ol_kvm/swap' [7.45 GiB] inherit ACTIVE '/dev/ol_guestol/swap' [2.00 GiB] inherit ACTIVE '/dev/ol_guestol/root' [26.99 GiB] inherit
- 挂载LVM分区
# mount /dev/mapper/ol_guestol-root /ol7/ # cd /ol7/ # ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys test tmp usr var
对虚拟磁盘镜像进行更改后,请安全断开虚拟磁盘,以避免出现任何问题。
- 卸载LVM分区:
# umount /ol7/
- 停用卷组。
# vgchange -an ol_guestol 0 logical volume(s) in volume group "ol_guestol" now active
- 断开图像与 NBD 的连接
# qemu-nbd --disconnect /dev/nbd0 /dev/nbd0 disconnected
- 删除NBD内核驱动
# rmmod nbd
欢迎来到之路教程(on itroad-com)
对于 CentOS/RHEL 6
CentOS/RHEL 6 附带的 qemu 镜像不支持 NBD。
因此,要访问 CentOS/RHEL 6 KVM 主机中的虚拟磁盘镜像,请使用 libguestfs 工具。
- 安装libguestfs工具
# yum install libguestfs-tools
- 运行来宾文件系统 shell
# guestfish --rw -a /var/lib/libvirt/images/olseven.qcow2 Welcome to guestfish, the guest filesystem shell for editing virtual machine filesystems and disk images. Type: ‘help’ for help on commands ‘man’ to read the bananaal ‘quit’ to quit the shell [fs]
- 使用“run”命令启动后端。
[fs] run 100% ⟦#################################################################################⟧ 00:00
- 列出镜像中的文件系统。
[fs] list-filesystems /dev/sda1: xfs /dev/ol_guestol/root: xfs /dev/ol_guestol/swap: swap
- 退出来宾文件系统 shell 并挂载磁盘镜像中看到的分区。
# guestmount -a /var/lib/libvirt/images/olseven.qcow2 -m /dev/ol_guestol/root /ol7/
# cd /ol7/ # ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys test tmp usr var
- 完成对镜像的更改后,卸载它。
# umount /ol7/
日期:2020-09-17 00:13:44 来源:oir作者:oir