Jakub is a Java EE developer since 2005 and occasionally a project manager, working currently with Iterate AS. He's highly interested in developer productivity (and tools like Maven and AOP/AspectJ), web frameworks, Java portals, testing and performance and works a lot with IBM technologies. A native to Czech Republic, he lives now in Oslo, Norway. Jakub is a DZone MVB and is not an employee of DZone and has posted 155 posts at DZone. You can read more from them at their website. View Full User Profile

Manually restoring raw partclone partition image to a VMWare

02.09.2011
| 8663 views |
  • submit to reddit

This post sums up how to manually restore a partition backup create by Clonezilla (using partclone) into a VMware virtual machine, which can be then either executed or its (virtual) disk mounted. The difficult points are “manual” and “partition backup” (would be much easier with a full disk backup). Normally I restore a backup by running Clonezilla from a virtual machine with sufficiently large virtual disk, but at times it isn’t possible, e.g. because the Clonezilla kernel has a buggy USB driver which tends to disconnect at times (at least with my version & flash disk).

Problems with VMware

  1. VMware can boot from a raw partition, but it must be a physical one, not its image stored in a file
  2. VMware doesn’t support loop devices for accessing such a partition (or disk) image – that’s because loop devices don’t behave as true physical disk, i.e. are missing support for some commands that VMware uses (though sb. has implemented a patch for VMware to work around this)
  3. VMware can boot from an image of a raw disk, but it must be a full disk (including MBR, partition table), not just a partition


Failed attempt: Use VirtualBox’s support for partitions

In theory, Virtual Box could be used with few tricks to mount from a raw partition image (by configuring it to boot from the original true raw partition and then changing the device in the configuration to a loop device) but for me VB was very unreliable, managed to boot correctly only occasionally and when it did then the graphics was a bit strange, which prevented me from logging in to the backed-up OS. Anyway, for those who’re interested, I’ll record my steps here.

See http://www.virtualbox.org/manual/ch09.html#rawdisk

0. Mount the restored partition:

sudo mount -t ext4 -o loop,nosuid,nodev /media/jholy1g/sda1.ext4-ptcl-restored.img /mnt

# in my case mounted to /dev/loop0 as revealed by running mount.

1. Create vbox image from a physical disk where the backed-up partition comes from (/dev/sda, partition #5 in my case):

sudo VBoxManage internalcommands createrawvmdk -filename vbox-sda.vmdk -rawdisk /dev/sda -partitions 5 -relative

2. In the generated vbox-sda.vmdk, replace the physical /dev/sda5 with /dev/loop0

3. Start virtualbox from the commandline where you’re sure to have access to /dev/loop0

(adduser myname disk; newgrp disk; virtualbox)

4. Create new VirtualBox virt. machine with an existing hard drive image, i.e. the one created above

Success: Restoration to a mounted vmware partition

See http://cromoteca.com/en/blog/mountflatvmwarediskimagesunderlinux/

Summary:

  1. Create a pre-allocated (non-growing) vmware disk of a sufficient size
  2. Run a virtual machine with e.g. an Ubuntu live CD and the disk, create there a partitiona and set its boot flag
  3. Stop the vmware VM
  4. Mount the virtual vmdk disk as a loop device from the host system
  5. Mount the vmdk’s first partition as a loop device (beware you need to use the correct offset here!)
  6. Use partclone to restore the (joined & decompressed) backup to the partition’s loop device
  7. Umount the partition
  8. Install GRUB into the virtual disk and configure it to boot the 1st partition (either from the host system, or, which may be easier, from within vmware)
  9. Run the virtual machine, booting from the thus prepared disk


Restoring to a virtual vmware disk’s partition

Create a pre-allocated (non-growing) vmware disk of a sufficient size in the VMWare Player.

This part is mostly taken over from Mount Flat VMWare Disk Images Under Linux.

Mount the virtual disk:

sudo losetup /dev/loop0 /media/MyBook-Linux/vmware/Ubuntu-candyRestore/Ubuntu-candyRestore-0-flat.vmdk

Find out where the first partition of the virtual disk starts for correct offset when mounting it:

$ sudo fdisk -lu /dev/loop0

Disk /dev/loop0: 306.0 GB, 306016419840 bytes
97 heads, 12 sectors/track, 513477 cylinders, total 597688320 sectors
Units = sectors of 1 * 512 = 512 bytes
Disk identifier: 0xaa3590f4

Device Boot      Start         End      Blocks   Id  System
/dev/loop0p1   *        2048   597688319   298843136   83  Linux

=> 2048 * 512 = 1048576 bytes

Mount the VMDK partition, using the computed offset (in bytes):

sudo losetup -v -o 1048576 /dev/loop1 /dev/loop0

Restore the (joined and uncompressed) partclone backup to the virtual partition:

sudo partclone.restore -C -s /media/jholy1g/sda1.ext4-ptcl-img -o /dev/loop1

To verify, mount the restored filesystem:

sudo mount /dev/loop1 /tmp/mpoint

Further verification: Run the VM with a live CD, try to manually mount the partition similarly as above (or check whether the OS recognizes it as a valid, mountable one) – if not then perhaps the offset wasn’t right.

Making the virtual disk bootable from the restored partition

Now that we have restored our backup to a VMDK’s partition, we need to make it possible for VMWare to boot from the partition. To achieve that, we would need to install a boot manager such as Grub to the virtual image and configure it to use the partition.

There is a related blog describing how to install grub to a raw disk image and another similar article, which is quite similar to our need here.

TBD: I clearly won’t have an opportunity to try this in the forseeable future but I suppose the easiest way would be to run the virtual machine with the disk booting from a Linux live CD and install Grub there (perhaps via the Grub Customizer GUI).

Installing Grub without vmware, from the host system (having the vmdk partition mounted as a loop device as described above), should be also pretty much possible.

Sorry for leaving this unfinished. If I ever have time to come back to this, I’ll update the post.

Additional notes

  • VMWare comes with a tool for mounting its virtual disks from the host
References
Published at DZone with permission of Jakub Holý, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)