Friday 1 July 2011

Moving Physical Linux to Virtual

The problem

My laptop contains several bootable linux distros. I often have a current distro, an old one, and one for exploring stuff.
Just now I want to replace the oldest distro - but there's quite a lot of interesting material in there - half finished projects, whatever. So I would like to clone it to a virtual machine where I can still play with it on some future rainy day.

I have windows vista, mint 7, fedora 13, and mint 10 in various stages of use. I use a master Grub (legacy) boot partition which chainloads to the /boot of whichever actual distro I want to fire up.
So each distro has at least a BOOT and a ROOT partition of its own.

At this time the mint 7 disto is scheduled for the holodeck. It lives on sda7 (BOOT1) sda8 (ROOT1) and sda11 (HOME1). It has Grub legacy installed to BOOT1. Although the /etc/fstab uses named partitions, I cant be absolutely sure that all other software in that system is free of references to /dev/sda-whatever, so I'm going to clone quite a lot of the physical disk structure (although not all of its data).

The plan is:
  • install virtual box on the laptop
  • create a virtual disk with at least 11 partitions, most of which are tiny - only a couple of MB - and the rest map directly to the physical partitions - but will be smaller where possible. I dont intend doing an immense amount of work in this virtual machine, so I'll only need a little flex on the storage.
  • copy the data mostly as files, from physical to virtual world using rsync.
  • copy boot sectors to the virtual disk.
  • install grub into /dev/sda virtually - no need for a separate chain loader.
  • boot into the virtual machine and install guest additions.
  • shutdown and copy the virtual machine image to ... wherever I need it - initially, storage.
  • install something new in the partitions previously allocated to mint7
Details

"Host" = laptop booted into into Fedora 13.
"Target" = virtual machine
Unless otherwise stated, run all commands as root.

On the host:

Use gparted to confirm disk structure and to learn how much of each is in use.

Mount the mint7 partitions and learn how much space is used.

mkdir /mnt/sda7
mkdir/mnt/sda8
mkdir /mnt/sda11
mount /dev/sda7 /mnt/sda7
mount /dev/sda8 /mnt/sda8
mount /dev/sda11 /mnt/sda11
df -h

Check which partition labels are actually used - look in /mnt/sda8/etc/fstab

To get the bridged networking to operate correctly, ensure the laptop is connected to ethernet and to a DHCP server. Note the IP address of the host (referred to as $HOST below)

Install VirtualBox 4

Create a virtual disk of 40GB with expanding rather than fixed storage (I probably won't use all of that.)
The rest of the machine : 512 MB RAM, Bridged networking, 3d graphics enabled.

Download an iso of SystemRescueDisk. Allocate it to the DVD drive of the virtual machine.

Boot the virtual machine, choosing "start graphics mode"

On the target:

Create a password for root - will be needed later for rsync

Use gparted to create 11 partitions, 3 physical and the remained logical.
Set all partition sizes to 10MB except for sda1, sda7, sda8, sda11:
sda1 can be 30M, while the size for the others depend on what we learned from the "df" on the host.
Align all partitions to MB values, except for sda7+ which are aligned to cylinders - matching the physical layout.
sda1 and sda7 of type ext2, sda8 and sda11 of type ext3.
For sda7,8,11 set the labels to match the physical partitions.

Apply all changes to gparted and exit.

Note the IP address of the virtual machine, referred to as $TARGET below.

mkdir /target
mount /dev/sda8 /target
mount /dev/sda7 /target/boot
mount /dev/sda11 /target/home


On the host:

Copy files from physical to virtual (give root password on demand):

rsync -va --progress /mnt/sda8 root@$TARGET:/target
rsync -va --progress /mnt/sda7 root@$TARGET:/target/boot
rsync -va --progress /mnt/sda11 root@TARGET:/target/home

On the target:

Install grub via chroot:

# install grub
# assuming /target/boot/grub contains the right stuff - staging files etc - which it does because that's how mint7 booted.

mount -t proc proc /target/proc
mount -t sysfs sys /target/sys
mount -o bind /dev /target/dev
chroot /target /bin/bash
grub
root (hd0,6)
setup (hd0)
quit

Yes, that says hd0 and 6, not sda and 7. That's just the way grub works.

sync
exit

shutdown and halt the virtual machine.

On the host:

Disconnect the SystemRescueCD iso from the virtual cd drive.
Reboot the virtual machine.
Login and install Guest Additions.

Enjoy.