mounting partitions from within of the vmware’s .vmdk file or kvm’s qcow2

i’m taking backups of vmware esxi vms weekly using ghettovcb. it has been working greatly for many years. once in a while i need to recover a particular file from particular backup. there’s quicker way than just restoring the whole vm!

few tools are needed:

root@host:~# apt-get install qemu-utils mount kpartx

my vmdk files are in the 2gbsparse format:

root@host:~# ls -la app0-1*
-rw-r--r-- 1 ninja 1019  664535040 Aug  9 00:14 app0-1-s001.vmdk
-rw-r--r-- 1 ninja 1019 1065877504 Aug  9 00:14 app0-1-s002.vmdk
-rw-r--r-- 1 ninja 1019  648413184 Aug  9 00:14 app0-1-s003.vmdk
-rw-r--r-- 1 ninja 1019  352387072 Aug  9 00:14 app0-1-s004.vmdk
-rw-r--r-- 1 ninja 1019  316538880 Aug  9 00:14 app0-1-s005.vmdk
-rw-r--r-- 1 ninja 1019  469565440 Aug  9 00:14 app0-1-s006.vmdk
-rw-r--r-- 1 ninja 1019  570818560 Aug  9 00:14 app0-1-s007.vmdk
-rw-r--r-- 1 ninja 1019  573767680 Aug  9 00:14 app0-1-s008.vmdk
-rw-r--r-- 1 ninja 1019        798 Aug  9 00:14 app0-1.vmdk

but monolithic ones should work fine too

first i need to convert the .vmdk format to qemu’s qcow2:

root@host:~# qemu-img convert app0-1.vmdk 1.qemu2

then find partitions within it and mount one that i’m interested in:

root@host:~# losetup /dev/loop7 1.qemu2
root@host:~# kpartx -va /dev/loop7
root@host:~# ls -la /dev/mapper/loop*
lrwxrwxrwx 1 root root 7 Dec  1 18:39 /dev/mapper/loop7p1 -> ../dm-2
root@host:~# mount /dev/mapper/loop7p1 /mnt/tmp

and the cleanup:

root@host:~# umount /mnt/tmp
root@host:~# kpartx -d /dev/loop7
root@host:~# losetup -d /dev/loop7

2017-11-21 edit:

guestmount -a server1-0.vmdk -i --ro /mnt/test/

worked fine on the ghettoVCB-created backup.

2022-12-13 edit:
for qcow2 files:

modprobe nbd max_part=8
# i'm using -r / read only mostly because my backup is likely a mounted user-space file system from borg backup
qemu-nbd -r --connect=/dev/nbd0 /mnt/tmp0/backup/vm-vda.qcow2
fdisk /dev/nbd0 -l
# see the partition names

# also here read-only to let it mount from borg backup
mount -o noload,ro /dev/nbd0p1 /mnt/tmp1/

# once all is done:
umount /mnt/somepoint/
qemu-nbd --disconnect /dev/nbd0
rmmod nbd

based on

Leave a Reply

Your email address will not be published. Required fields are marked *

(Spamcheck Enabled)