Monday, August 13, 2012

Building an Ubuntu guest OS using ubuntu-vm-builder

Building an Ubuntu guest OS image from scratch  using ubuntu-vm-builder requires a package to be installed on the host where you will do the build.

sudo apt-get install ubuntu-vm-builder

Then you run ubuntu-vm-builder to do the work for you. This is great, because it means you can keep the creation scripts under some source code control system to be able to recreate an image exactly, although note that the process does take some time since the tool pulls packages across the network to build the image. I had a few attempts at building images until I got the options right.


There are some advanced options which I've reproduced from here to show how you can provide quite detailed image build instructions:

ubuntu-vm-builder kvm hardy \
                  --domain newvm \
                  --dest newvm \
                  --arch i386 \
                  --hostname hostnameformyvm \
                  --mem 256 \
                  --user john \
                  --pass doe \
                  --ip 192.168.0.12 \
                  --mask 255.255.255.0 \
                  --net 192.168.0.0 \
                  --bcast 192.168.0.255 \
                  --gw 192.168.0.1 \
                  --dns 192.168.0.1 \
                  --mirror http://archive.localubuntumirror.net/ubuntu \
                  --components main,universe \
                  --addpkg acpid \ 
                  --addpkg vim \
                  --addpkg openssh-server \
                  --addpkg avahi-daemon \
                  --libvirt qemu:///system ;

For my image, I eventually went with the below to get a headless server. If you want a desktop, you can add --addpkg ubuntu-desktop to the line below. Alternatively, you can connect to the guest OS later and simply run from within the guest followed by a restart (which you can just do with shutdown from within the guest).

sudo apt-get install ubuntu-desktop
The above worked out of the box for me.

gsw@goat:/media/Data/KVM$ time sudo ubuntu-vm-builder kvm precise --mem 256 --domain vm-test1 --dest vm-test1 --hostname vm-test1 --user test1 --pass test1 --components main,universe,restricted --addpkg acpid --addpkg vim --addpkg openssh-server --addpkg avahi-daemon --libvirt qemu:///system
[sudo] password for gsw:
2012-08-13 01:12:08,664 INFO    : Calling hook: preflight_check
2012-08-13 01:12:08,675 INFO    : Calling hook: set_defaults
2012-08-13 01:12:08,676 INFO    : Calling hook: bootstrap
2012-08-13 01:19:38,737 INFO    : Calling hook: configure_os
Extracting templates from packages: 100%
2012-08-13 01:22:19,034 INFO    : Updating certificates in /etc/ssl/certs... 152 added, 0 removed; done.
2012-08-13 01:22:19,036 INFO    : Running hooks in /etc/ca-certificates/update.d....done.
2012-08-13 01:22:19,225 INFO    : invoke-rc.d: policy-rc.d denied execution of start.
2012-08-13 01:22:19,781 INFO    : invoke-rc.d: policy-rc.d denied execution of start.
2012-08-13 01:22:20,008 INFO    : invoke-rc.d: policy-rc.d denied execution of force-reload.
2012-08-13 01:22:20,034 INFO    : invoke-rc.d: policy-rc.d denied execution of start.
2012-08-13 01:22:20,461 INFO    : Creating SSH2 RSA key; this may take some time ...
2012-08-13 01:22:20,589 INFO    : Creating SSH2 DSA key; this may take some time ...
2012-08-13 01:22:20,600 INFO    : Creating SSH2 ECDSA key; this may take some time ...
2012-08-13 01:22:20,748 INFO    : invoke-rc.d: policy-rc.d denied execution of stop.
2012-08-13 01:22:20,750 INFO    :
2012-08-13 01:22:20,750 INFO    : Warning: Fake initctl called, doing nothing
2012-08-13 01:22:20,751 INFO    :
2012-08-13 01:22:20,752 INFO    : Warning: Fake initctl called, doing nothing
2012-08-13 01:22:22,407 INFO    :
2012-08-13 01:22:22,408 INFO    : Current default time zone: 'Etc/UTC'
2012-08-13 01:22:22,414 INFO    : Local time is now:      Sun Aug 12 15:22:22 UTC 2012.
2012-08-13 01:22:22,414 INFO    : Universal Time is now:  Sun Aug 12 15:22:22 UTC 2012.
2012-08-13 01:22:22,415 INFO    :
2012-08-13 01:22:52,282 INFO    : gpg: key 437D05B5: "Ubuntu Archive Automatic Signing Key " not changed
2012-08-13 01:22:52,288 INFO    : gpg: key FBB75451: "Ubuntu CD Image Automatic Signing Key " not changed
2012-08-13 01:22:52,288 INFO    : gpg: Total number processed: 2
2012-08-13 01:22:52,289 INFO    : gpg:              unchanged: 2
2012-08-13 01:22:53,104 INFO    : invoke-rc.d: policy-rc.d denied execution of stop.
2012-08-13 01:22:54,835 INFO    : invoke-rc.d: policy-rc.d denied execution of stop.
2012-08-13 01:22:55,164 INFO    : invoke-rc.d: policy-rc.d denied execution of start.
2012-08-13 01:22:55,411 INFO    : invoke-rc.d: policy-rc.d denied execution of start.
2012-08-13 01:22:55,578 INFO    : invoke-rc.d: policy-rc.d denied execution of restart.
2012-08-13 01:22:56,133 INFO    : invoke-rc.d: policy-rc.d denied execution of start.
2012-08-13 01:23:04,897 INFO    : Cleaning up
2012-08-13 01:23:04,898 INFO    : Calling hook: preflight_check
2012-08-13 01:23:05,743 INFO    : Calling hook: configure_networking
2012-08-13 01:23:05,775 INFO    : Calling hook: configure_mounting
2012-08-13 01:23:05,780 INFO    : Calling hook: mount_partitions
2012-08-13 01:23:05,780 INFO    : Mounting target filesystems
2012-08-13 01:23:05,780 INFO    : Creating disk image: "/tmp/tmpaHg8oh" of size: 5120MB
2012-08-13 01:23:05,829 INFO    : Adding partition table to disk image: /tmp/tmpaHg8oh
2012-08-13 01:23:06,260 INFO    : Adding type 4 partition to disk image: /tmp/tmpaHg8oh
2012-08-13 01:23:06,260 INFO    : Partition at beginning of disk - reserving first cylinder
2012-08-13 01:23:06,603 INFO    : Adding type 3 partition to disk image: /tmp/tmpaHg8oh
2012-08-13 01:23:06,614 INFO    : [0] ../../libparted/filesys.c:148 (ped_file_system_type_get): File system alias linux-swap(new) is deprecated
2012-08-13 01:23:06,947 INFO    : Creating loop devices corresponding to the created partitions
2012-08-13 01:23:06,963 INFO    : Creating file systems
2012-08-13 01:23:06,972 INFO    : mke2fs 1.42 (29-Nov-2011)
2012-08-13 01:23:07,607 INFO    : mkswap: /dev/mapper/loop0p2: warning: don't erase bootbits sectors
2012-08-13 01:23:07,608 INFO    :         on whole disk. Use -f to force.
2012-08-13 01:23:11,430 INFO    : Calling hook: install_bootloader
2012-08-13 01:23:32,284 INFO    : Removing update-grub hooks from /etc/kernel-img.conf in favour of
2012-08-13 01:23:32,284 INFO    : /etc/kernel/ hooks.
2012-08-13 01:23:32,400 INFO    : Searching for GRUB installation directory ... found: /boot/grub
2012-08-13 01:23:32,474 INFO    : Searching for default file ... Generating /boot/grub/default file and setting the default boot entry to 0
2012-08-13 01:23:32,477 INFO    : Searching for GRUB installation directory ... found: /boot/grub
2012-08-13 01:23:32,487 INFO    : Testing for an existing GRUB menu.lst file ...
2012-08-13 01:23:32,488 INFO    :
2012-08-13 01:23:32,488 INFO    : Could not find /boot/grub/menu.lst file. Would you like /boot/grub/menu.lst generated for you? (y/N) /usr/sbin/update-grub: line 1094: read: read error: 0: Bad file descriptor
2012-08-13 01:23:33,224 INFO    : Searching for GRUB installation directory ... found: /boot/grub
2012-08-13 01:23:33,298 INFO    : Searching for default file ... found: /boot/grub/default
2012-08-13 01:23:33,302 INFO    : Testing for an existing GRUB menu.lst file ...
2012-08-13 01:23:33,302 INFO    :
2012-08-13 01:23:33,303 INFO    : Could not find /boot/grub/menu.lst file.
2012-08-13 01:23:33,303 INFO    : Generating /boot/grub/menu.lst
2012-08-13 01:23:33,426 INFO    : Searching for splash image ... none found, skipping ...
2012-08-13 01:23:33,688 INFO    : grep: /boot/config*: No such file or directory
2012-08-13 01:23:33,851 INFO    : Updating /boot/grub/menu.lst ... done
2012-08-13 01:23:33,851 INFO    :
2012-08-13 01:23:34,060 INFO    : Searching for GRUB installation directory ... found: /boot/grub
2012-08-13 01:23:34,130 INFO    : Searching for default file ... found: /boot/grub/default
2012-08-13 01:23:34,140 INFO    : Testing for an existing GRUB menu.lst file ... found: /boot/grub/menu.lst
2012-08-13 01:23:34,366 INFO    : Searching for splash image ... none found, skipping ...
2012-08-13 01:23:34,405 INFO    : grep: /boot/config*: No such file or directory
2012-08-13 01:23:34,584 INFO    : Updating /boot/grub/menu.lst ... done
2012-08-13 01:23:34,584 INFO    :
2012-08-13 01:23:34,638 INFO    : Searching for GRUB installation directory ... found: /boot/grub
2012-08-13 01:23:34,653 INFO    : Calling hook: install_kernel
2012-08-13 01:25:56,510 INFO    : Done.
2012-08-13 01:26:00,092 INFO    : Running depmod.
2012-08-13 01:26:00,152 INFO    : update-initramfs: deferring update (hook will be called later)
2012-08-13 01:26:00,160 INFO    : Examining /etc/kernel/postinst.d.
2012-08-13 01:26:00,161 INFO    : run-parts: executing /etc/kernel/postinst.d/initramfs-tools 3.2.0-29-virtual /boot/vmlinuz-3.2.0-29-virtual
2012-08-13 01:26:00,163 INFO    : update-initramfs: Generating /boot/initrd.img-3.2.0-29-virtual
2012-08-13 01:26:03,765 INFO    : run-parts: executing /etc/kernel/postinst.d/zz-update-grub 3.2.0-29-virtual /boot/vmlinuz-3.2.0-29-virtual
2012-08-13 01:26:03,888 INFO    : Searching for GRUB installation directory ... found: /boot/grub
2012-08-13 01:26:03,952 INFO    : Searching for default file ... found: /boot/grub/default
2012-08-13 01:26:03,963 INFO    : Testing for an existing GRUB menu.lst file ... found: /boot/grub/menu.lst
2012-08-13 01:26:04,184 INFO    : Searching for splash image ... none found, skipping ...
2012-08-13 01:26:04,296 INFO    : Found kernel: /boot/vmlinuz-3.2.0-29-virtual
2012-08-13 01:26:04,514 INFO    : Replacing config file /run/grub/menu.lst with new version
2012-08-13 01:26:04,564 INFO    : Updating /boot/grub/menu.lst ... done
2012-08-13 01:26:04,564 INFO    :
2012-08-13 01:26:05,059 INFO    : Calling hook: post_install
2012-08-13 01:26:05,060 INFO    : Calling hook: unmount_partitions
2012-08-13 01:26:05,062 INFO    : Unmounting target filesystem
2012-08-13 01:26:08,404 INFO    : Calling hook: convert
2012-08-13 01:26:08,404 INFO    : Converting /tmp/tmpaHg8oh to qcow2, format vm-test1/tmpaHg8oh.qcow2
2012-08-13 01:26:36,008 INFO    : Calling hook: fix_ownership
2012-08-13 01:26:36,010 INFO    : Calling hook: deploy

real    14m32.066s
user    1m4.068s
sys    0m36.362s
From the output of the time command above you can see that there is a bit of time spent creating each image. Perhaps creating a local mirror of the relevant bits of Precise would yield much faster image build times if this is something you will come to do a lot.

After including the desktop package here's what you end up with:

gsw@goat:/media/Data/KVM$ ls -al vm-test1/
total 3453704
drwx------ 1 gsw gsw          0 Aug 13 01:26 .
drwx------ 1 gsw gsw          0 Aug 13 01:26 ..
-rw------- 1 gsw gsw 3536715776 Aug 13 21:00 tmpaHg8oh.qcow2

Starting it is as easy as:

kvm -m 256 -smp 1 -drive file=vm-test1/tmpaHg8oh.qcow2 "$@"

The screen shot at the top of this post shows the end result after logging in using the test1 credentials.

No comments:

Post a Comment