Creating a VirtualBox image with a Debian guest

Whenever I want to experiment with Debian, I like to make a virtual machine (VM) with VirtualBox to do so. It's not hard to do so by using it's GUI. But I configure the hardware usually in the same way, so that's a candidate for automation. Fortunately VirtualBox supports that through the vboxmanage command. In this blog, I'll show you how to do that with a bash script.

Since VirtualBox 4.x all files are stored in 1 folder, which gets the same name as the VM and is stored under the default machine folder. In order to create our bash script, we need the default machine folder and the name of the VM to create. The default machine folder is stored in the VirtualBox system settings and can be retrieved in the following way:

vboxmanage list systemproperties | grep 'Default machine folder' | awk '{ print $4 }'

The name of the VM gets passed as an argument to our script and can be retrieved with $1.
We will also create a harddisk file to store our Debian system on and that file needs a full path, otherwise it will be stored in the current directory, instead of the virtual machine folder. I like to give the hard disk file name a bit more descriptive name and do so by appending "Root" to it, to indicate it contains the root (/) folder.

Now we know the variables for our script. let's assign them to our own variables as follows:

#BASEFOLDER is the home folder of VirtualBox's virtual machines
BASEFOLDER=$(vboxmanage list systemproperties | grep 'Default machine' | awk '{ print $4 }')
echo "BASEFOLDER: " "$BASEFOLDER"
#VMNAME will contain the name of our virtual machine VMNAME=$1
echo "VMNAME: " "$VMNAME"
#The root filesystem will be stored in the following file
HDFILENAME="$BASEFOLDER/$VMNAME/$VMNAME"Root.vdi
echo "HDFILENAME: " "$HDFILENAME"

The commands to create our VM, register it with VirtualBox and configure it to my needs are as follows:

#Create the VM and register it with virtualbox
vboxmanage createvm --name "$VMNAME" --ostype Debian --register
#Now change the settings as I want them to be
vboxmanage modifyvm "$VMNAME" --memory 1024 --vram 64 --acpi on --ioapic on --chipset ich9 --nic1 bridged --nictype1 82545EM --boot1 dvd --boot2 disk --boot3 none
#Create the storagecontrollers
vboxmanage storagectl "$VMNAME" --name "IDE Controller" --add ide --controller ICH6 --hostiocache on
vboxmanage storagectl "$VMNAME" --name "SATA Controller" --add sata --controller IntelAHCI
#Create the hdd containing the root file system and make it 8GB big
vboxmanage createhd --filename "$HDFILENAME" --size 8192
#Assign the newly created hdd file to the SATA Controller
vboxmanage storageattach "$VMNAME" --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium $HDFILENAME
#Don't assign something to the IDE Controller
vboxmanage storageattach "$VMNAME" --storagectl "IDE Controller" --port 0 --device 0 --type dvddrive --medium emptydrive

After creating the virtual machine, installing Debian and then booting it up, you will see a warning posted by the kernel like the following:

smbus base address uninitialized upgrade bios or use force_addr=0xaddr

This warning is caused by the VM having no smbus but Debian always tries to load the module. It is a harmless warning, but I don't like to see errors/warnings if they don't have a (negative) effect. You can disable the warning by blacklisting the module that's responsible for it. If you do that and reboot, you'll get another warning, about pcspeaker, so we'll blacklist that as well. To do so, boot up the VM, log in as root and add the following lines to /etc/modprobe.d/blacklist.conf:

blacklist i2c_piix4
blacklist pcspkr

And that's it! We now have a working Debian system in a virtual machine with no warnings during boot and it's ready to install whatever you want to get the system to do what you want it to do.