7
I am trying to clone a virtual disk image in a fairly manual fashion. The overview of my methodology so far is as follows:
- Create virtual machine in VirtualBox with 120GB HDD (hypervisor and HDD size don't matter, mostly included for completeness and consistency with the rest of my question, e.g. partition sizes)
- Install Ubuntu 12.04.3 on virtual machine
- Close virtual machine
- Mount virtual hard disk associated with virtual machine
- Extract operating system files and data to store in a directory
- Save virtual hard disk metadata
- Create fresh virtual disk and restore partitions and boot information from (6)
- Restore data from (5) to the correct partition
The problem
My duplicated VM won't quite boot. Grub seems to copy, and appears to acknowledge my root partition (with Ubuntu installed on it). I can boot past Grub once and get a purple screen, as if Ubuntu is about to load. Then it stops. After that, I can boot into Grub, select my OS, then I get a blinking command line cursor. No input possible. I suspect there's something I'm missing in the cloning process (see below for more detail). Note: I am using grub2, not legacy.
Why are you doing this?
As part of a contractual requirement, I need to store the virtual disk in version control. Having an enormous binary blob (virtual disk) in version control is a pain, mostly for clone(git)/checkout(svn), but also for diffs. I have considered compressing to multiple files, but I need to be able to manipulate the OS/data extracted in (5) above. Note that my VCS repository still needs all the information required to build a complete VM.
Detail
Detailed instructions to reproduce what I've described:
- Create a VM and boot the Ubuntu Live CD
- Choose "Try Ubuntu"
- Open a terminal
- Create an msdos partition: sudo parted /dev/sda mklabel msdos
- Create a 2GB swap file: sudo parted /dev/sda mkpart primary linux-swap 2048s 4198399s
- Use the rest of the drive for the root partition: sudo parted /dev/sda mkpart primary ext4 4198400s 100%
- Reboot the machine, choose "Install Ubuntu"
- Choose the advanced partitioning option
- Double-click the swap partition, choose to use it as swap
- Double-click the root partition, choose to format it and use it for root (/) mount point
Now, perform the following to clone the disk:
# Set up some parameters
ORIG_DEV="/dev/nbd0"
ORIG_MNT=$(mktemp -d)
ORIG_IMG="orig.vdi"
CLONE_DEV="/dev/nbd1"
CLONE_MNT=$(mktemp -d)
CLONE_IMG="clone.vdi"
qemu-img info $ORIG_IMG # save the "virtual size" output (in bytes) in the
# VIRT_SIZE variable in the next command
VIRT_SIZE="128849018880"
# Create the clone disk
qemu-img create -f vdi $CLONE_IMG $VIRT_SIZE
# Use qemu to make both disks accessible
modprobe nbd
qemu-nbd -c $ORIG_DEV $ORIG_IMG
qemu-nbd -c $CLONE_DEV $CLONE_IMG
# Set up the clone disk partition table and partitions
parted $CLONE_DEV mklabel msdos
parted $CLONE_DEV mkpart primary linux-swap 2048s 4198399s
parted $CLONE_DEV mkpart primary ext4 4198400s 100%
# Format the clone disk partitions and clone the UUIDs
mkswap $CLONE_DEVp1 -U $(blkid $ORIG_DEVp1 -s UUID -o value)
mkfs.ext4 $CLONE_DEVp2 -U $(blkid $ORIG_DEVp2 -s UUID -o value)
# Mount both disks and copy root from the original to the clone
mount $CLONE_DEVp2 $CLONE_MNT
mount $ORIG_DEVp2 $ORIG_MNT
find $ORIG_MNT -maxdepth 1 -mindepth 1 | xargs -I{} cp -ar {} $CLONE_MNT
umount $ORIG_MNT
umount $CLONE_MNT
# Copy the boot sector and partition table from the original
dd if=$ORIG_DEV of=$CLONE_DEV bs=$((2048*512)) count=1
# Disconnect the disks
qemu-nbd -d $CLONE_DEV
qemu-nbd -d $ORIG_DEV
What else have you tried?
- grub-install --root-directory=/path/to/clone/device/boot/ /dev/clone_device. This installed Grub on the correct device, but with my host's device details. The VM would not boot.
- chroot into the clone disk, then grub-install. Encountered trouble because I must be able to use 64 bit hosts to clone 32 bit guests. This seems like a hopeful avenue to investigate, but I'm stuck as to how to achieve this.
- Mount the virtual disk, move all files off the data partition using
mv
, zero the data and swap partitions (dd if=/dev/zero of=/dev/nbd0p2
) and compress the virtual disk (usingVBoxManage modifyhd clone.vdi --compress
). The disk began to expand on my host file system as this was filling it with empty space (hah!). I stoppeddd
when I realised this was happening, then compressed the disk image. It was still over 3GB. (I haven't tried using gzip/bzip, I'll begin to attempt this this evening. I will also attempt letting the dd wipe run to completion, but I'd prefer a less time-consuming solution, even if that works). - e2image. See my other question: e2image restore file system metadata. I have not resolved this. Note that the steps I provide in the Detail section, including partition creation, formatting, and boot sector copy, but before I copy the root partition, produce a very similar sized image file to that created by e2image.
- Booting into another VM to chroot into this one to run grub-install. I haven't actually done this, but I've included it here in case someone suggests it. For my users, I need the recombination of the virtual machine to be scriptable; which precludes an involved setup process.
- Install extlinux instead of Grub. While unsuccessful, this exercise indicates that (I think!) the bootloader is successfully loading the ram disk from my partition, but gets stuck at this point.
If you've come this far, thank you already! Any suggestions for avenues of investigation, however undetailed, will be much appreciated. Thanks in advance.
+1 for the completeness; I wish I could help further though – Canadian Luke – 2013-09-08T17:46:18.843
1
e2image man mentions the
– harrymc – 2013-09-08T18:42:29.397-r
parameter rather than-Q
. You could try to contact its creator Theodore Ts'o, who could surely help you out.Thanks @CanadianLuke, so do I! @harrymc, I hadn't considered using
e2image -r
because in my experience Windows doesn't make a very good job of storing sparse files, meaning a checkout of the repository would take a lot of space if a dev wants to use Windows. However, my aforementioned "experience" with sparse files in Windows is very limited, I have barely pursued this and it certainly appears to warrant further investigation; thanks very much for the suggestion, I'll have a look into it and let you know how it goes. – mkingston – 2013-09-09T08:47:11.883