This is the first article in a multi-part series that describes how to use Oracle’s VirtualBox to automatically set up Debian Squeeze VMs on a host machine. All the scripts used in these articles are available for download.
The articles are available here:
- Headless Debian VirtualBox Setup
- Debian Squeeze PXE Netboot For VirtualBox
- Need An Ultralight WebServer? You Need Mongoose!
- Preseeding a Debian Squeeze Netboot Install
Contents
Resources
List of TCP and UDP Ports
Remote Virtual Machines
VBoxManage Reference
Introduction
It’s often a good idea to have a dedicated computer for performing certain tasks. For example, a build machine that does nothing but build your application in a known, unchanging environment. Or a server that does nothing but run your revision control system.
On the other hand, it’s often impractical to have a dedicated machine for every task. Besides the obvious issues of space – the box itself plus monitors, keyboards, and mice – there are real power consumption issues as well.
Fortunately, the current level of performance of even a basic netbook computer means that there is an alternative – virtual machines.
In this first of three articles, I’ll show you how to set up a Debian Squeeze server running under VirtualBox. We’ll use a simple, easy to modify script to create the VM instead of a more cumbersome GUI. In the next article, I’ll go over setting up a PXE netboot environment for your VMs, and the final article will show you how to do a pre-seeded headless install of Debian Squeeze so that we won’t have to deal with handlholding through a bunch of screenshots.
Why go through the grief of setting up this infrastructure? If you’re ever stuck without an internet connection and need to set up a VM, you can have all the bits you need right on your notebook. Plus, you can set up a custom repository so that it’s easier to update all your machines from a local source instead of going to the mirrors for each one.
Getting VirtualBox
If you’re running Debian Squeeze, you will need to create an additional file in your etc/apt/sources.list.d directory for VirtualBox:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#! /bin/dash # # ----------------------------------------------------------------------------- # startMongoose.sh # # Starts the Mongoose web server # # ----------------------------------------------------------------------------- # Set up a few shell variables to make things cleaner MONGOOSE=$HOME/bin/mongoose MONGOOSE_ROOT=$HOME/.VirtualBox/www/ MONGOOSE_LOG=$HOME/tmp/mongoose/accesslog.txt MONGOOSE_PORT=8080 # ----------------------------------------------------------------------------- # Create a directory (no error if it already exists) mkdir -p $MONGOOSE_ROOT mkdir -p $HOME/tmp/mongoose # ----------------------------------------------------------------------------- # Start Mongoose $MONGOOSE -r $MONGOOSE_ROOT -p $MONGOOSE_PORT -a $MONGOOSE_LOG |
|
1 |
sudo echo "deb http://download.virtualbox.org/virtualbox/debian squeeze contrib non-free" > /etc/apt/sources.list.d/virtualbox.list |
Then install the key for the Oracle site (long line):
|
1 |
sudo wget -q http://download.virtualbox.org/virtualbox/debian/sun_vbox.asc -O- | sudo apt-key add - |
Then update, upgrade, get VirtualBox, and install the latest extension pack (this one works with the latest 4.1.6 Virtualbox):
|
1 2 3 4 5 |
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install virtualbox-4.1
wget http://download.virtualbox.org/virtualbox/4.1.6/Oracle_VM_VirtualBox_Extension_Pack-4.1.6-74713.vbox-extpack
sudo VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-4.1.6-74713.vbox-extpack --replace |
OK, now we’re ready to stasrt creating VMs…
Creating A Debian Squeeze VM
The default directory for VMs is your ~HOME/VirtualBox VMs. You can name your VM anything you like, but to avoid problems with the PXE boot mechanism, do not use spaces in the VM name.
I like to put at least two and sometimes three NICs in my VMs. The first is for bridging to the wired and/or wireless adapters from the host to the local network and the second is for a host-only network that allows me to do NFS or SAMBA shares between the host and the VMs even if the host is not connected to the network. The third one is dedicated to NAT and is used for PXE booting the VM for the initial install.
I also tend to run my VMs in headless mode, which allows me to run minimal installs and to not worry about screen real estate on the host. Sometimes it’s just easier to script repetitive tasks involved in managing a computer – and automating the scripts makes it more likely that regular maintenance like backups will get done.
Creating a VM using the VirtualBoxGUI is relatively simple, but it suffers from at least three problems:
- It’s hard to describe what to do without screenshots
- The instructions may change depending on the verison of VirtualBox
- The screenshots may change depending on the VM settings we need
Some sites will walk you through setting up a VM using the command line by posting one command at a time and then describing what it’s doing in separate text. That’s better than screenshots but has the same problem of keeping the page content in line with the scripts.
Instead, we’ll be providing a complete shell script with embedded comments so that you can follow along and it’s less likely to get out of date. You can copy the script text and modify it however you like.
The following is the shell script that I am currently using to create Debian VMs – hopefully the comments are enoughto help you figure out what’s going on. Ideally, you’ll want to modify at least the VM name – I put that in a variable at the top of the script so you don’t need to change it all over the place. Another variable you’re likely to change is the VRDP port – you’ll want one that’s unique for each of your VMs on a particular host.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
#! /bin/dash # # ----------------------------------------------------------------------------- # createDebianVM.sh # # This file is unique per VM, so it should live in its own directory under # the pxeSqueezeBox project # # ----------------------------------------------------------------------------- # Shell variables for the script: vmName="SqueezeTemplate" vmPath="$HOME/VirtualBox VMs" vmDescription="Squeeze Headless PreSeeded Install Template" vmRAMSize="128" vmDiskSize="4096" vmDiskMedium="$vmPath"/"$vmName"/"$vmName".vdi vmVrdePort="3390" vmNATHostOnlyAddress="192.168.56.50" vmNATHostOnlyNetmask="255.255.255.0" vmNATBootFile="pxelinux.0" # ----------------------------------------------------------------------------- # Create the VM # # Change "Squeeze Template" to whatever suits your needs VBoxManage createvm --name "$vmName" --register # ----------------------------------------------------------------------------- # Set up the VM with memory VBoxManage modifyvm "$vmName" --memory $vmRAMSize \ --acpi on \ --boot1 disk \ --boot2 dvd \ --boot3 net \ --vram 1 \ --vrde on \ --vrdeport $vmVrdePort \ --vrdeauthtype null \ --rtcuseutc on # ----------------------------------------------------------------------------- # Add the NICs, always add NIC1 as host-only. Other NICs depend on existence # of corresponding host adapters... VBoxManage modifyvm "$vmName" --nic1 hostonly \ --hostonlyadapter1 vboxnet0 if test -e "/dev/.udev/db/net:eth0" then VBoxManage modifyvm "$vmName" --nic2 bridged \ --bridgeadapter2 eth0 fi if test -e "/dev/.udev/db/net:eth1" then VBoxManage modifyvm "$vmName" --nic3 bridged \ --bridgeadapter3 eth1 fi VBoxManage modifyvm "$vmName" --nic4 nat \ --nicbootprio4 1 \ --natnet4 10.0.2/24 \ --nattftpfile4 $vmNATBootFile # ----------------------------------------------------------------------------- # Create the hard drive controller and disk image VBoxManage storagectl "$vmName" --name "IDE Controller" --add ide VBoxManage createhd --filename "$vmDiskMedium" \ --size $vmDiskSize # ----------------------------------------------------------------------------- # Attach the disk image VBoxManage storageattach "$vmName" --storagectl "IDE Controller" \ --port 0 \ --device 0 \ --type hdd \ --medium "$vmDiskMedium" # ----------------------------------------------------------------------------- # Set up VM specific data strings that we can query later VBoxManage setextradata "$vmName" vmNATHostOnlyAddress $vmNATHostOnlyAddress VBoxManage setextradata "$vmName" vmNATHostOnlyNetmask $vmNATHostOnlyNetmask |
That’s pretty much it, if you open up VirtualBox on the host machine, you should now have a shiny new VM that’s ready to rock and roll.