Virtualization Stuff

I created this page to serve as a central place where I can put information about tips and tricks that have come in handy while playing with virtualization. This includes, but is not limited to, virtualization with Xen, VMWare, VirtualPC, and the necessary procedures for setting up partitions, disk images, linux installations, and network stuff. I’ve used others’ instructions on loopback filesystems, creating partitions on disk images, installing grub, and debootstrap to figure this stuff out, but I’m putting a reminder-to-self here.

Network

Changing an adapter’s MAC address:

ifconfig eth0 hw ether aa:bb:cc:dd:ee

Specifying an adapter’s MAC address in a Xen config file:

vif = [ 'type=ioemu, mac=aa:bb:cc:dd:ee:ff, bridge=xenbr0' ]

Creating Disk Images

I’ve been playing with VMMs like Xen and VMWare and OSes like Linux. One thing that can consume a lot of time is repartitioning disks all the time to get the desired number and variety of guest operating systems. I don’t usually do things in these guest OSes which are bound by disk performance, so it makes sense for me to use loopback-mounted disk images. I don’t just want a partition-as-image though, I want a disk-as-image, complete with an MBR and potentially containing multiple partitions.

This simple command will create a 2GB “image” file:

dd if=/dev/zero of=disk-image count=2048 bs=1M

However, we want our image file to behave like a disk (e.g., have an MBR), not just a partition. This command (see partitions on disk images) will create a 2GB file called disk-image, with 4096 cylinders (it also has 16 heads and 63 sectors — not sure where those numbers come from):

dd if=/dev/zero of=/path/to/c.img bs=516096c count=4096

Installing Linux on the disk image

Use fdisk to create a partition (I only created one, things get more complicated w/ additional ones):


losetup /dev/loop0 disk.img
fdisk -u -C4096 -S63 -H16 /dev/loop0
n
p
1

Now we must unmount the whole image and then remount just our partition so we can format it (#blocks is the number of blocks listed in fdisk when it prints partition information:


losetup -d /dev/loop0
losetup -o32256 /dev/loop0 disk.img
mke2fs -b1024 /dev/loop0 #blocks
mount -t ext3 /dev/loop0 /mnt
mkdir /mnt/boot
mkdir /mnt/boot/grub
cp /boot/grub/* /mnt/boot/grub
umount /mnt

Now we want to unmount the loopback device and install the grub bootloader on the MBR of our virtual disk (grub is supposed to be able to handle giving it /dev/loop0 when the disk.img is mounted via the loopback system, but it gives me “error 22: no such partition”).


losetup -d /dev/loop0
grub --no-floppy
grub> device (hd0) disk.img
grub> geometry (hd0) 4096 16 63
grub> root (hd0,0)
grub> setup (hd0)

Mount the formatted partition and install a base Debian system on it:


mount -text3 -oloop=/dev/loop2,offset=32256 disk.img /mnt
/usr/sbin/debootstrap --arch i386 sarge /mnt http://http.us.debian.org/debian

chroot into the new system to complete the install:

LANG= chroot /mnt /bin/bash

vi /etc/fstab
mount -a
mount -t proc proc /proc
dpkg-reconfigure console-data
vi /etc/network/interfaces
vi /etc/resolv.conf
/usr/sbin/base-config new

Installing Windows on the disk image (if you have HVM support)

Make an ISO of your Windows installer CD:
dd if=/dev/cdrom of=/winxp/winxp_installer.iso

Here is my ‘winxp.xm’:

kernel = "/usr/lib/xen/boot/hvmloader"
builder='hvm'
memory = 512
name = "WinXP"
disk = [ 'file:/winxp/c.img,ioemu:hda,w' ]
vif = [ 'type=ioemu, bridge=xenbr0' ]
device_model = '/usr/lib/xen/bin/qemu-dm'
memmap = '/usr/lib/xen/boot/mem-map.sxp'
cdrom='/winxp/winxp_installer.iso'
boot='d'
sdl=1
vnc=0

I simply did xm create -f winxp.xm, and it booted into the Windows Installer. Everything worked! Once the installer wants to reboot, you will need to change boot='d' to boot='c' in your domain config file.

Mounting Disk Images


/sbin/fdisk disk.img
x
p

x is for eXtended mode. p will Print the partition table. “Partition start” as given is in 512 byte blocks.
Compute the start offset in bytes (start block * 512). Then run:

mount -o loop,offset=OFFSET disk.img /mnt/disk

where OFFSET is the byte offset computed above. For the above example image creation, mount with:

mount -text3 -oloop=/dev/loop2,offset=32256 disk.img /mnt

Common Errors

I received an unusual error message when I first started playing with HVM domains which took me a while to debug:

#xm create -f winxp.xm
Using config file "winxp.xm".
Error: Device 768 (vbd) could not be connected. Backend device not found.

It turns out that the only problem was that the module for loopback filesystems was not loaded. In dom0, do a `modprobe loop` and you should be good to go. Note that failure to `modprobe loop` may cripple the system for the entire boot cycle, so reboot, `modprobe loop`, then try to boot your HVM domain.

2 thoughts on “Virtualization Stuff

  1. with respect to:

    “Now we want to unmount the loopback device and install the grub bootloader on the MBR of our virtual disk (grub is supposed to be able to handle giving it /dev/loop0 when the disk.img is mounted via the loopback system, but it gives me “error 22: no such partition”).”

    check out the instructions at:

    http://wiki.xensource.com/xenwiki/InstallGuestImage

    the way to do it is:

    losetup /dev/loop0 disk.img
    losetup -o32256 /dev/loop1 disk.img
    ln -s /dev/loop0 /dev/loop
    grub
    grub> device (hd0) /dev/loop
    grub> root (hd0,0)
    grub> setup (hd0)
    grub> quit
    rm /dev/loop
    losetup -d /dev/loop0
    losetup -d /dev/loop1

    grub expects something like /dev/hda (where the MBR+partition table is) and /dev/hda1 (where the filesystem partition is). this sets it up so that /dev/loop is where the MBR+partition table are, and /dev/loop1 is where the filesystem is. grub is then happy.

  2. A heartfelt *thankyou* for the tips on grub usage. I tried the device (hd0) /dev/loop thing (per lamontg), but this always got “no such partition”.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s