Skip to content

Migrating Live System to VM

Quick n’ Dirty Steps

modprobe dm-mod
modprobe loop

fallocate -l 40GB -o 1024 /mnt/rex1/backup.img

losetup /dev/loop6 /mnt/rex1/backup.img

Fdisk Steps

fdisk /dev/loop6
  1. DOS partition table
  2. create new partition
  3. type linux
  4. toggle bootable flag
  5. write
  6. exit

Partprobe

partprobe
losetup -d /dev/loop6

Install kpartx

apt-get install kpartx

kpartx -v -a /mnt/rex1/backup.img
> add map loop0p1 (251:0): 0 497664 linear /dev/loop0 2048

ls /dev/mapper/
> control loop0p1


mkfs.ext4 /dev/mapper/loop0p1

mount /dev/mapper/loop0p1 /mnt/virtual
mount | grep /mnt/virtual
> /dev/mapper/loop0p1 on /mnt/virtual type ext4 (rw)

rsync -aAXv /* /mnt/virtual --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found,/home/*/.gvfs}

kpartx -d -v /dev/loop0

Install virtualbox

apt-get -t wheezy-backports install virtualbox
apt-get install qemu-utils

qemu-img convert -c -f raw -O qcow backup.img backup.qcow2
VBoxManage convertfromraw --format VDI backup.img backup.vdi
VBoxManage convertfromraw --format VMDK backup.img backup.vmdk

Running the VM

  1. Create VM with backup.vdi. Virtual Box worked for me flawlessly.
  2. Load-up Super Grub as virtual CD.
  3. Boot into the virtual machine.
  4. Install grub2 if not installed
  5. Generate grub again. (/usr/sbin/update-grub)
  6. Fix /etc/fstab by executing mount and adding the right UUID and removing uneeded mounting points.
  7. Set the new /dev/sda1 as bootable using cfdisk.
  8. Umount the Super Grub CD.
  9. Re-run the VM

It should load up fine.

References

https://wiki.archlinux.org/index.php/Moving_an_existing_install_into_%28or_out_of%29_a_virtual_machine
http://stackoverflow.com/questions/1419489/loopback-mounting-individual-partitions-from-within-a-file-that-contains-a-parti
http://wiki.osdev.org/Loopback_Device
http://unix.stackexchange.com/questions/43903/create-file-image-container
http://codeless.fr/?p=25
https://lists.debian.org/debian-user/2011/08/msg01288.html
http://askubuntu.com/questions/197833/recovering-from-grub-rescue-crash

Other Stuff:

http://www.vmware.com/products/converter
http://www.addictivetips.com/windows-tips/convert-use-your-physical-machine-in-vmware-virtualbox-virtual-pc/
Back to top