Physical to Virtual Machine Conversion On Fedora

The following is a short write-up of how I accomplished converting my mythtv master-backend to a virtual machine. I did this for the purpose of hardware flexibility, which allowed me to run my backend on my fileserver while I upgraded it's hardware and OS.

This should apply to almost any linux version, but the target machine I performed this on was Fedora 6.

It was performed on a running machine.

Step 1: Prepare the Virtual Disk
I used qemu to create an image of a particular type. Raw is easiest to deal with for native block device handling and it can be converted to another format later.

In my case I created this device on a storage partition that wasn't being converted to the virtual machine. You can easily use an NFS or SMB mount point if need be.

You have 2 options for disk creation; either dd or qemu-img.

#Create a disk of desired size (in this case 10G)
qemu-img create disk.img 10G

or

dd if=/dev/zero of=./mydisk.img bs=1M count=10000

Step 2: Setup loop device for partitioning
The image file needs to be mounted as a block device to allow fdisk and mkfs tools to address it correctly.

losetup /dev/loop0 ./mydisk.img

Step 3: Partition the virtual disk

#Create desired partitions (i.e: /boot, /, swap)
fdisk /dev/loop0

Step 4: Create loop device mappings for partitions

#run kpartx to get loop mappings
kpartx -av /dev/loop0

#Verify mappings exist
ls -al /dev/mapper/loop*

Step 5: Create required file systems on respective devices

#mkfs on appropriate /dev/mapper/loop devices
mkfs.ext3 /dev/mapper/loop0p1 (/boot)
mkfs.ext3 /dev/mapper/loop0p3 (/)
mkswap /dev/mapper/loop0p2 (swap)

Step 6: Prepare directories for file-system mount

#Mount / loop device to /mnt
mount /dev/mapper/loop0p3 /mnt

#create /boot
mkdir /mnt/boot

#mount /boot
mount /dev/mapperl/loop0p1 /mnt/boot

Step 7: Rsync exiting data to virtual devices
You may want to --exclude /proc in the rsync command as you'll see some errors blow by. The synchronization should be sucessful otherwise.

cd /mnt
rsync -av /boot .
rsync -avx / .

Step 8: Make system changes to allow booting in vmware

#Modify /boot/grub/grub.conf & /etc/fstab for proper disk paths
/dev/hda is default for this type of disk image

vi /mnt/etc/grub.conf

vi /mnt/etc/fstab.conf

#Umount the disks
umount /mnt/boot /mnt

Step 9: Install the bootloader

#Install grub (this takes a little magic)

#Create offset loop device for partition1 /boot
#This is required because of how the mbr works
losetup -o32256 /dev/loop1 mydisk.img

#Create a symlink for /dev/loop to satisfy grub
#Link original /dev/loop0
ln -s /dev/loop0 /dev/loop

#Enter grub cli
grub

#Set the device
grub>device (hd0) /dev/loop

#Set root device
grub>root (hd0,0)

#install the bootloader
grub>setup (hd0)

#now exit
grub>quit

#Now clean up the links and loop devices
rm /dev/loop
losetup -d /dev/loop0
losetup -d /dev/loop1

Step 10: Convert the disk to the desired format
If you intend on using the virtual disk in Vmware you'll need to first convert the disk image type to vmdk using the following command. If you intend on using qemu or kqemu conversion should not be necessary.

qemu-img -c -f raw ./mydisk.img -O vmdk ./mydisk.vmdk

If you have any questions or corrections please email me using the link at the top of the page.