VM template creation with oz-install

When you’re managing your infrastructure in the Cloud and are not satisfied with the pre-built VM templates, you will have to create your own templates. Creating templates by hand, especially when you update them regularly, is a very tedious and error prone task that should be avoided.

There are quite some tools around to help automating this process. For example, one could combine the virt-install tool with tools from the libguestfs project or use the scripts from the ubuntu-vm-builder project. One of the benefits of oz-install is its extensive OS support (it even supports creating Windows templates). In this post I will show you how to create a basic Ubuntu template with oz-install.

To efficiently build VM templates, your build machine requires a processor with virtualization extensions. With a recent Linux kernel version you could virtualize these extensions, but that will definitely slow down the whole process.

Package dependencies

To build the oz-install package, your have to make sure the following packages are installed on your system:

  • debhelper (>= 8.0.0)
  • python-all (>= 2.6.6-3)
  • build-essential
  • git-core (if you are fetching the source via Git)

dpkg-buildpackage will complain when these packages are missing.

There are several run-time dependencies, but these will be installed automatically by dpkg when installing oz-install.

Building the package

Building oz-install is not very difficult. The following steps are confirmed to work on Ubuntu 12.04, but will probably work on most Debian based Linux systems.

  1. Fetch the latest source from GitHub:
    mkdir ~/oz
    cd ~/oz
    git clone https://github.com/clalancette/oz.git oz-git
    
  2. Build the deb with dpkg-buildpackage:
    cd ~/oz/oz-git
    dpkg-buildpackage -us -uc
    
  3. Now you can install the package you built:
    cd ~/oz
    dpkg -i oz_*_all.deb
    

Creating your first (simple) template

After installing the package, you can use the following configuration files to create a basic Ubuntu 12.04 template. Feel free to experiment with the various configuration options or supported operating systems (invoke oz-install without any options to view the supported operating systems).

Apart from OS installation, you can add customization options to the template definition file. This feature can be used to run shell commands on the template after it is installed. oz-customize will start the template and use SSH to connect to the machine and run the shell commands.

  1. Create your template definition file with your favourite editor (~/oz/my-template.tdl):
    <template>
      <name>my-template</name>
      <description>My first oz template</description>
      <disk>
        <size>5</size>
      </disk>
      <os>
       <name>Ubuntu</name>
       <version>12.04</version>
       <arch>x86_64</arch>
       <install type='url'>
         <url>http://mirror.nl.leaseweb.net/ubuntu/dists/precise/main/installer-amd64/current/images/netboot</url>
       </install>
       <rootpw>securepassword-oLklpFb8</rootpw>
      </os>
      <commands>
        <command name='hostname'>
          echo 'my-template' > /etc/hostname
        </command>
      </commands>
    </template>
    
  2. Create your preseed file with your favorite editor (~/oz/my-template.preseed):
    d-i debian-installer/locale string en_US.UTF-8
    d-i console-setup/ask_detect boolean false
    d-i console-setup/layoutcode string us
    
    d-i netcfg/choose_interface select auto
    d-i netcfg/get_hostname string unassigned-hostname
    d-i netcfg/get_domain string unassigned-domain
    d-i netcfg/wireless_wep string
    
    d-i clock-setup/utc boolean true
    d-i time/zone string US/Eastern
    
    d-i partman-auto/method string regular
    d-i partman-auto/choose_recipe select home
    d-i partman/confirm_write_new_label boolean true
    d-i partman/choose_partition select finish
    d-i partman/confirm boolean true
    d-i partman/confirm_nooverwrite boolean true
    
    d-i passwd/root-login boolean true
    d-i passwd/make-user boolean false
    d-i passwd/root-password password %ROOTPW%
    d-i passwd/root-password-again password %ROOTPW%
    
    tasksel tasksel/first multiselect standard
    d-i pkgsel/include/install-recommends boolean true
    d-i pkgsel/include string openssh-server python-software-properties wget whois curl acpid
    
    d-i grub-installer/only_debian boolean true
    d-i grub-installer/with_other_os boolean true
    
    d-i apt-setup/security_host string
    base-config apt-setup/security-updates boolean false
    
    ubiquity ubiquity/summary note
    ubiquity ubiquity/reboot boolean true
    
    d-i finish-install/reboot_in_progress note
    
    d-i mirror/country string manual
    d-i mirror/http/hostname string mirror.nl.leaseweb.net
    d-i mirror/http/directory string /ubuntu
    
  3. Run oz-install with customize options:
    cd ~/oz
    oz-install -b virtio -n virtio -p -u\
      -x ~/oz/my-template.xml \
      -a ~/oz/my-template.preseed \
      ~/oz/my-template.tdl
    
  4. (optional) Monitor the installation with virt-viewer:
    virt-viewer my-template
    
  5. (optional) Import the template in libvirt and start it:
    virsh define ~/oz/my-template.xml
    virsh start my-template
    

Take this as an example and start experimenting with all the options available. After playing around with oz-install you should be able to create a structured template creation workflow. When you combine this with a set of custom scripts,
you can integrate the flow with, for example, jenkins to easily add new or updated templates to your virtualization or cloud platform in a modular way.

Share