I am creating some new VMs on a fresh install of Ubuntu 9.10 (Karmic/Kreepy Koala), so now is a great time to extract and organize each step used to create a new VM on the command line, and double-check all my documentation in real time. Here we go.
As with my last post, the machine I’m creating in these comments is called “addie_vm” but can be replaced with the machine name of your choice.
I typically start by creating the virtual hard disk for my VM. To keep things from getting confusing, I always name the hard disk with the same name as my VM. Note that renaming virtual hard disks is MUCH harder than renaming a VM, so name carefully!
VBoxManage createhd --filename addie_vm.vdi --size 100000 --format VDI --variant Standard
Some notes: VDI is a standard virtualbox format and what virtual hard disks default to in the GUI. Size is specified in MB, so what I am creating here is a 100 GB hard disk. The “Standard” variant means that the actual space used on your physical hard drive will grow to a cap of 100 GB versus taking up those 100 GB from the get-go. If you’d like your virtual hard drive to take up its capacity on the physical hard drive, use the “Fixed” variant instead (This is a useful option if you’re planning very carefully about how the entire host system’s resources are being used).
Next, create the VM with some basic features configured. The VMs I am creating will have FreeBSD installed on them, so I run:
VBoxManage createvm --name addie_vm --ostype FreeBSD_64 --register
The –register arg allows you to skip a step; you won’t have to run VBoxManage registervm to get the system to recognize your new machine. If you’re using another OS, you’ll need to include a specific string specifying your OS type after the –ostype arg. You can see your options by running:
VBoxManage list ostypes
Now, glue two pieces together, as it were – attach your hard drive to the VM you just created.*
VBoxManage modifyvm addie_vm --hda addie_vm.vdi
At this point the “basics” are in place, but here are a couple of options I set before I go about installing the OS on the new VM (and I will explore some of these in greater detail later on):
VBoxManage modifyvm addie_vm --memory 1024
This command sets the RAM for your machine – the number following –memory should be in megabytes. The VM will be created with a default of 128 MB RAM, which is a bit on the small side, so adjusting it before you get to tinkering around with your machine is recommended. As with hard disk space, you’ll want to know the total RAM of your host system, and the total RAM of your other VMs, and plan accordingly. If too much host RAM is allocated, and the host system is being used for anything other than running VMs, you’ll run into some serious performance problems on your host machine.
Enabling networking is also important. Networking is one of my weakest areas knowledge-wise, and past versions of VirtualBox have made getting internet working on VMs pretty complicated, especially if you are using only the command line. If you’re using an older copy of VirtualBox (2.0), you’ll probably want to look elsewhere for documentation, starting with help.ubuntu.com’s documentation.
Luckily, things are easier in newer versions of VirtualBox. I use a bridged network connection, so the VM uses the same hardware as my host machine. To see the available bridged interfaces, run:
VBoxManage list bridgedifs
If you’re going with system defaults with regards to networking, you’ll probably be using “eth0″. Interface name in hand, you’ll then run:
VBoxManage modifyvm addie_vm --nic1 bridged
VBoxManage modifyvm addie_vm --bridgeadapater1 eth0
The first command configures your machine to use a bridged adapter. The second command points the adapter to the appropriate interface. The “1″ in –nic1 and –bridgeadapter1 specifies that this is the first adapter; you can configure multiple adapters if needed.
Finally, and I will revisit this later, I like enabling vrdp – the VirtualBox Remote Desktop Protocol – and assigning a port for the machine in question (the port is optional – VBoxHeadless will assign one for you if you don’t have one already). Note that these options are only available in the closed source version of VirtualBox (unfortunately).
VBoxManage modifyvm addie_vm --vrdp on
VBoxManage modifyvm addie_vm --vrdpport 3390
To close, let’s trash my creation. Getting rid of a VM is actually somewhat tricky from the command line, because you must first detach the VM from its hard drive, and to make sure you aren’t cluttering up the VirtualBox configuration and leaving around garbage, you’ll want to unregister both VM and hard disk. Here’s the series of steps. Start with your VM powered off:
VBoxManage modifyvm addie_vm --hda1 none
This command disconnects the virtual hard drive from your virtual machine.
You can go ahead and unregister the hard drive and VM now:
VBoxManage unregistervm addie_vm --delete
VBoxManage closemedium disk addie_vm.vdi
You’re done with the VM now – thanks to the –delete flag, your VM is gone. The hard drive is still lingering though, and if you want to save disk space, you’re now in the position to safely delete it. Assuming your VirtualBox configuration lives in .VirtualBox under your home directory, you can remove it this way:
… and that’s all for this entry. Next up: cloning hard drives so you don’t have to go through system setup on several machines in a row.
* If you’re using VirtualBox CSE 3.1.x, you may run into issues here – I have. I’ll try to write about resolving them in a future post; if I do I’ll link to it in this aside.