Monday, January 9, 2012

Scripted vmdk/ova images w/boxgrinder and virtualbox

It wasn't immediately clear to me from boxgrinder or virtualbox documentation how to script the complete creation of a self-contained runnable virtual image. Since it took me some effort to get it all together, I'll post here a complete example. The script creates a Fedora 16 w/graphical UI machine image by first creating a virtualbox vmdk image (pretty straightforward) and then from that, scripted creation of a more self-contained .ova image (less straightforward).

I have the following packages installed:

  • VirtualBox-OSE
  • rubygem-boxgrinder-build

Starting from a boxgrinder appliance definition file...

http://boxgrinder.org/tutorials/appliance-definition/

...the script creates an .ova image that can be easily imported and used by 3rd parties like this:

  $ VBoxManage import f16-xfce.ova
  $ VirtualBox --startvm f16-xfce


And now for the script:

#!/bin/sh
NAME=f16-xfce
OS=Fedora_64
IMAGE=/tmp/${NAME}.vmdk
SZMB=1536
INSTDIR=/tmp/boxes/
BUILDDIR=/tmp/builds/

mkdir -p ${BUILDDIR}
cat > ${BUILDDIR}/${NAME}.appl << EOF
name: f16-xfce
summary: Fedora with xfce
os:
  name: fedora
  version: 16
hardware:
  partitions:
    "/":
      size: 5
packages:
  - @base
  - @base-x
  - @fonts
  - @xfce-desktop
  - @critical-path-xfce
post:
  base:
   - "useradd boxgrinder && echo boxgrinder | passwd boxgrinder --stdin"
   - "ln -s --force /lib/systemd/system/graphical.target /etc/systemd/system/default.target"
EOF

cd ${BUILDDIR}
sudo -E boxgrinder-build -p virtualbox ${NAME}.appl
cp ${BUILDDIR}/build/appliances/x86_64/fedora/16/${NAME}/1.0/virtualbox-plugin/${NAME}.vmdk ${IMAGE}
VBoxManage createvm --name ${NAME} --ostype ${OS} --register --basefolder ${INSTDIR}
VBoxManage modifyvm ${NAME} --memory ${SZMB} --vram 32
VBoxManage storagectl ${NAME} --name "SATA Controller" --add sata --controller IntelAHCI
VBoxManage storageattach ${NAME} --storagectl "SATA Controller" --type hdd --port 0 --device 0 --medium ${IMAGE}
VBoxManage export ${NAME} --manifest -o ${BUILDDIR}/${NAME}.ova
# VirtualBox --startvm ${NAME}

1 comment:

rubiojr said...

Very nice Jason, thanks for the tip!