Debian/Ubuntu debootstrap images for VirtualBox


I've been working on building custom debian and ubuntu distributions for use under virtualbox.   One advantage that both have over fedora is debootstrap.  This tool allows you to create a default rootfs from pre-compiled packages inside a directory.  You can then chroot into that directory to install the kernel image, extra packages and do any additional customizations.

Where this gets really cool is using debootstrap with a few additional tools on an image file.  The process is fairly simple.  First you create an image file using dd and add a partition table for two partitions, boot and rootfs, using parted. Loop mount (kpartx) and bind mount boot under rootfs.  Format the partitions (ext3.mkfs).  Now you're ready to install the rootfs using debootstrap.  Once installed, chroot into that directory and run customization scripts.  Finally exit the chroot, unmount and install a bootloader such as grub.  That creates the raw disk image.  You can then use qemu-image to convert that to various VM image formats such as those for VirtualBox, xen and kvm.

I handle this using a front end script that runs 7 steps, most of which are outside of the chroot but a few that are in it.  The seven steps are

  1. Create image file
  2. Create partition table
  3. Loop mount and bind mount
  4. Install rootfs with debootstrap
  5. Copy in chroot scripts and data files
  6. Chroot and run those script and unmount
  7. Convert to VM image

These seven are pretty common for either Debian or Ubuntu.  There are small variations, such as what you include wiht debootstrap and perhaps how you need to do your loop mounts.  The differences come from the chroot scripts and data files.  There is one script that sets up the chroot environment as necessary to perform additional package installations.  Setup can include network interfaces, setting the locale and installing prerequisites necessary to do other installations.

Within the chroot script is the installation of the linux kernel image.  This gets installed under /boot, which was bind mounted outside of the chroot so we have separate boot and rootfs partitions when we boot the image.  What's interesting is that most information you find online about this process assumes you're building debian or ubuntu on debian or ubuntu.  But I'm not.  I'm on Fedora.

This means that when you get to installing grub the instructions you find don't always match what you need to do.  Fedora 21 uses grub2.  Debian uses grub or grub2 and Ubuntu has grub.  Does it matter?  Turns out it doesn't.  All that matters is that the grub from your distro is installed to /boot on the image and the MBR of the image file points to it.  That way qemu-image can create an appropriate image for your VM.

For bonus points the use of qemu-image allows you to convert this raw disk image into a variety of VM image formats.  So a single image build is quickly converted to the VM environment of choice.

In the end I found that testing the VM image under VirtualBox was pretty easy, including getting the guest utilities installed as part of a firstboot-process.  Those utils need to rebuild kernel modules and you can't do that from within the chroot easily.  It's easier to just create a firstboot script hat installs the utils and rebuilds the modules andthat gets run the first time the image boots in the VM and then removes itself aftward.

So now I can quickly bring up a a Debian VM.  The whole process takes about 15 minutes unattended.  The only problem is it's not Fedora.  If I could do this under Fedora or CentOS, I'd be sooo happy.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.