To create image we will need a raw HDD for the virtual machine. So, set as much space as you need.
kvm-img create -f raw server.img 5G
Get Ubuntu 11.04 Server downloaded from here,
wget http://releases.ubuntu.com/natty/ubuntu-11.04-server-amd64.iso
To start the installation process, boot a KVM instance with the OS installer ISO in the virtual CD-ROM and set up a VNC port at 0
sudo kvm -m 256 -cdrom ubuntu-11.04-server-amd64.iso -drive file=server.img,if=scsi,index=0 -boot d -net nic -net user -nographic -vnc :0
Connect to the VM using the Client PC’s IP with VNC port 0 and finish the installation i.e. 10.10.10.4 :0
vncviewer 10.10.10.4 :0
Create a single ext4 partition mounted on ‘/’ during the installation of Ubuntu. Do not create a swap partition.
After finishing the installation relaunch the VM by executing the following command,
sudo kvm -m 256 -drive file=server.img,if=scsi,index=0,boot=on -boot c -net nic -net user -nographic -vnc :0
Now you can add any packages you want. So for our test purpose we will just update and upgrade the system.
sudo apt-get update sudo apt-get upgrade
Install the following packages as well
sudo apt-get install openssh-server cloud-init
Remove the network persistent rules to make sure that the new network interface eth0 without creating any problem.
$ sudo rm -rf /etc/udev/rules.d/70-persistent-net.rules
Shutdown the VM.
To upload the image on Eucalyptus, it needs to be an ext4 filesystem image. To obtain an ext4 filesystem image, do the following
sudo losetup -f server.img sudo losetup -a /dev/loop0: [0801]:16908388 ($filepath)
Observe the name of the loop device ( /dev/loop0 in our setup) when $filepath is the path to the mounted .raw file.
Now we need to find out the starting sector of the partition
sudo fdisk -cul /dev/loop0
You should see an output like this
Disk /dev/loop0: 5368 MB, 5368709120 bytes 149 heads, 8 sectors/track, 8796 cylinders, total 10485760 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00072bd4 Device Boot Start End Blocks Id System /dev/loop0p1 * 2048 10483711 5240832 83 Linux
Make a note of the starting sector of the /dev/loop0 partition i.e the partition whose ID is 83. This number should be multiplied by 512 to obtain the correct value. In this case: 2048 x 512 = 1048576
Unmount the loop0 device:
sudo losetup -d /dev/loop0
Now mount only the partition(/dev/loop0) of server.img which we had previously noted down, by adding the -o parameter with value previously calculated value
sudo losetup -f -o 1048576 server.img sudo losetup -a
You’ll see a message like this
/dev/loop0: [0801]:16908388 ($filepath) offset 1048576
Make a note of the mount point of our device(/dev/loop0 in our setup) when $filepath is the path to the mounted .raw file.
Copy the entire partition to a new .raw file
sudo dd if=/dev/loop0 of=serverfinal.img
Now we have our ext4 filesystem image i.e serverfinal.img.
Unmount the loop0 device
sudo losetup -d /dev/loop0
Tweaking /etc/fstab
You will need to tweak /etc/fstab to make it suitable for a cloud instance.
Loop mount the serverfinal.img
sudo mount -o loop serverfinal.img /mnt
Edit /mnt/etc/fstab and modify the line for mounting root partition(which may look like the following)
UUID=e7f5af8d-5d96-45cc-a0fc-d0d1bde8f31c / ext4 errors=remount-ro 0 1
to
LABEL=uec-rootfs / ext4 defaults 0 0
Kernel and Initrd
Copy the kernel and the initrd image from /mnt/boot to user home directory. These will be used later for creating and uploading a complete virtual image to Eucalyptus.
sudo cp /mnt/boot/vmlinuz-2.6.38-7-server /home/shaon sudo cp /mnt/boot/initrd.img-2.6.38-7-server /home/shaon
Unmount the Loop partition
sudo umount /mnt
Change the filesystem label of serverfinal.img to ‘uec-rootfs’
sudo tune2fs -L uec-rootfs serverfinal.img
Now, we have all the components of the image ready to be uploaded to Eucalyptus server.