For CentOS/RHEL 7
For accessing qcow2 disk image of guest on a CentOS/RHEL 7 KVM host, either use qemu-nbd or libguestfs.
Using qemu-nbd
Follow the below steps to mount the qcow2 image in CentOS/RHEL 7 using qemu-nbd.
1. Enable NBD kernel driver on the KVM host.
# modprobe nbd
2. Connect the qcow2 image as network block device.
# qemu-nbd --connect=/dev/nbd0 olseven.qcow2 -f qcow2
3. Find the partitions in the image using 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
4. The device /dev/nbd0p2, is LVM so you would need to locate the new PVs/VGs and LVs in the KVM host machine.
# 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 ]
Refresh the physical volume cache so that host machine recognizes the new 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.
5. Activate the guest OS'es 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
6. Mount the LVM partition
# 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
Once changes are made to the virtual disk image safely disconnect the virtual disk to avoid any issues.
1. Unmount the LVM partition:
# umount /ol7/
2. Deactivate the volume group.
# vgchange -an ol_guestol 0 logical volume(s) in volume group "ol_guestol" now active
3. Disconnect the image from NBD
# qemu-nbd --disconnect /dev/nbd0 /dev/nbd0 disconnected
4. Remove the NBD kernel driver
# rmmod nbd
For CentOS/RHEL 6
The qemu-image that ships with CentOS/RHEL 6, doesn't have NBD support. So, for accessing the virtual disk image in CentOS/RHEL 6 KVM host use libguestfs tool.
1. Install libguestfs tools
# yum install libguestfs-tools
2. Run the guest filesystem 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 manual ‘quit’ to quit the shell [fs]
3. Launch the backend using the "run" command.
[fs] run 100% ⟦#################################################################################⟧ 00:00
4. List the filesystems in the image.
[fs] list-filesystems /dev/sda1: xfs /dev/ol_guestol/root: xfs /dev/ol_guestol/swap: swap
5. Exit from guest filesystem shell and mount the partition seen in the disk image.
# 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
6. Once done with making changes to the image, unmount it.
# umount /ol7/