VirtualBox Management
VirtualBox Management
Tips and tricks for using Virtual Box (targets version 4.2.6). Use the information provided at
your own risk!
Basics
Installation
A nice feature of VirtualBox is that it does not need special features from the processor, it will
even run on an Atom! Of course, do not expect too much from the performance, but it might be
just enough for some use cases.
Running
think about system security: run virtual box as an unprivileged user (e.g. create a
dedicated virtualbox user in a Linux environment)
created vm's, and all management tools, operate typically on the vm's for the current
user (e.g. you won't see a vm belonging to someone else)
Structure
Virtual Machines's are distinguished by name and UUID (Universally Unique IDentifier:
a monstruous long unique string)
Virtual Hard Disks also have a UUID
VirtualBox keeps a repository of all known 'media' (ISO files, virtual hd's)
Network
Networks can be set up in different ways, the following list shows the most important
characteristics:
Bridged
o IP address independent of the host
NAT
o INcoming traffic from outside host can be routed to the VM using host port
forwarding
HostOnly
o no traffic possible using the host's interface: no connectivity outside the host
(in, out)
So you might want to set up multiple networks, especially if you need connectivity between
multiple VM's and you don't have a full network available at all times (e.g. a development set
up on a laptop). Typically you'll need a HostOnly network alongside NAT and/or Bridged
networks.
Hard disks
Hard disks can easily be managed from the commandline. Keep in mind that VirtualBox keeps
a repository and usage information, so you can't simply mess around with copying and
renaming as you can when you're using that other big virtualization product.
Create
Creating a standard hard disk (actually, a hard disk image) using the commandline is easy.
One word about the commands: they might exist in entirely lower case format, or some mixed
CamelCased variants (e.g. VBoxManage or vboxmanage), or even both.
$> VBoxManage createhd --filename mydisk.vdi --size 10 --format VDI --variant Standard
Add or Attach
To add a harddisk to a virtual machine using the commandline, you need the controller
information, to tell VirtualBox where to attach the hard disk. Controller name, port and device
numbers are needed. This information must be obtained from the vm. Attaching a disk can be
done without first adding it to the repository, attaching a disk will add it to the repo.
# Obtain controller information, look for "SATA" or "IDE" or "SCSI" lines, you can only attach a disk to
an empty 'slot'
$> VBoxManage showvminfo myvm
# Attach the hard disk to the first SATA controller, first port:
$> VBoxManage storageattach myvm --storagectl "SATA" --port 0 --device 0 --type hdd --medium mydisk.vdi
If you don't have a controller, create it (GUI, or again using a VBoxManage command).
Remove
Removing has two levels, because of the VirtualBox repository. Detaching it from a controller
doesn't free it from VirtualBox. So if you want to mess around with the disk (renaming, other
UUID), remove it from the repo.
# Detach from a vm, by setting the medium for a specific controller, port and device to 'none'
$> VBoxManage storageattach myvm --storagectl "SATA" --port 0 --device 0 --type hdd --medium none
# Remove from the repo (really closing the medium for VirtualBox)
$> VBoxManage closemedium disk mydisk.vdi
The commandline is usually much more powerful, especially when you want tot automate
tasks. Commandline examples (the # represent a comment line):
# clone a virtual disk (gets a new UUID) and convert it to dynamic format (Standard)
# this is usually a very lengthy process
$> VBoxManage clonehd /data/vbox/mydisk.vdi /data/vbox/mydisk-clone.vdi --format VDI --variant Standard
You should never make a plain copy of a virtual hard disk, because the UUID doesn't change
when doing so. Use the clonehd command for this. But sometimes there are special cases.
What if you decide to change the 'variant' of the vdi from a fixed size to a dynamically sized
one, and your OS doesn't want to start anymore, or looses a license, or for whatever other
reason? This can be done!
Usually, I just hack a few files to accomplish tasks that are not in the user interface, but with
VirtualBox I have to rely on the tools, because the UUID is stored somewhere in the binary
gigabyte sized virtual disk. No plain text/XML here… Change the allocation type by a series of
actions which can best, and partly only, be performed on the commandline. The recipe, long
but luckily consisting of mostly comments and only a few steps, is shown below.
Notes:
you need enough room for the new to be created disk, which could easily be in the
gigabyte range
I do not know what happens if snapshots are still present when cloning a disk
examples1.sh
# Turn off the virtual machine to which the disk to be operated on is attached.
# If you're not sure which vm to shut down, you can get useful information on disk
# attachment using the following command:
VBoxManage showhdinfo /data/vbox/mydisk-fixed.vdi
# Detach the source disk from the vm (this is accomplished by setting the disk attached to the
controller to 'none');
# but you probably want to remember the attachment information: look for lines containing 'SATA',
'IDE' or 'vdi' in
# the vm info.
# 'grep' could be very convenient, save the information to a file
VBoxManage showvminfo myvm > myvm.info
# Then detach the disk from the controller, using the correct controller name, port and device
numbers:
VBoxManage storageattach myvm --storagectl "SATA" --port 0 --device 0 --type hdd --medium none
# Clone the hard disk, and set the variant for the clone to //Standard//, don't bother about
UUID's yet.
# Note 1: it will be remembered automatically (in some earlier version, this wasn't the case).
# Note 2: cloning may be performed with an attached disk
VBoxManage clonehd /data/vbox/mydisk-fixed.vdi /data/vbox/mydisk-dynamic.vdi --format VDI
--variant Standard
# Get the UUID of the source disk, and make sure you have a note of it. I'd make a note by
redirecting
# the output of the info command to a file. Although this command can be used after removing a
disk from
# the repository, do this now.
VBoxManage showhdinfo /data/vbox/mydisk-fixed.vdi > mydisk-fixed.info
cat mydisk-fixed.info | grep UUID
# We'll have to make VirtualBox forget about the source AND the cloned disks, as they are still in
the
# repository (GUI remembers them in 'VirtualBox.xml', the vm specific .vbox file also contains
this info).
# detach //both// disks (source and clone)
VBoxManage closemedium disk /data/vbox/mydisk-fixed.vdi
# AND
VBoxManage closemedium disk /data/vbox/mydisk-dynamic.vdi
# Do not run the showhdinfo command anymore, running the command immediately adds the disk to the
repo,
# something you do not want at this point, because we still have to mess around with the UUIDs.
# Just to be sure, assign a new UUID to the source disk (so you can never get clashes).
VBoxManage internalcommands sethduuid /data/vbox/mydisk-fixed.vdi
# Set the UUID of the cloned disk to the original one of the source disk (copy-paste the UUID from
the note).
VBoxManage internalcommands sethduuid /data/vbox/mydisk-dynamic.vdi 5ebabd04-39b3-44ed-9450-
1b98357fef6c
# Instead, for the not so faint-at-hart types, you could use a nice one-liner.
VBoxManage internalcommands sethduuid /data/vbox/mydisk-dynamic.vdi `cat mydisk-fixed.info | grep
UUID | awk '{print $2}'`
# If you want to rename the new disk, you could do so now, before attaching it.
# Attach the cloned disk to the vm, make sure you use the correct controller name, and port and
device numbers.
# You should have saved this information in the first step, using the 'showvminfo' command.
VBoxManage storageattach myvm --storagectl "SATA" --port 0 --device 0 --type hdd --medium
/data/vbox/mydisk-dynamic.vdi
This should have done the job, your guest OS won't see the difference!
Console access
VirtualBox offers a nice user interface, and when starting a VM you get a 'console' view, as if
you're looking at your monitor. Another option is to run it without this console view,
in headless mode. You typically want to turn on the VRDE remote desktop option to be able to
get a console view, in case of emergency (network failure/config error of the vm). This works
regardless of the VM being a GUI one (Windows, X) or just a text console (typically bare Linux
servers).
The VRDE option can be turned on in the VirtualBox GUI, but won't work without the
'extension pack' installed (which has a proprietary license)!
For security reasons, always set the 'authentication method' to 'External' (or 'Guest', if
you prefer difficult to set up beta functionality). The username/password will be the
username/password of the host system user that started the VM, when connecting
through an RDP client.
# Remove it
VBoxManage storageattach myvm --storagectl "IDE" --port 0 --device 0 --medium emptydrive
This is a very clean way of working with ISO's, great to completely fool Windows and installers
that insist on being run from the 'original' medium.
Of course, the command way can be used to perform this action even from within your
guest OS remotely, if you can log-in to your host from within the guest/VM.
# And to remove
VBoxManage storageattach myvm --storagectl "IDE" --port 0 --device 0 --medium emptydrive
vbox-dvd
#!/bin/bash
Automatic startup
There seems to be new functionality for autostarting VM's, which I didn't explore yet. What
worked pretty good for me in a Linux host environment was a very simple init script, and a
configuration file. The script is shown below, the config file just contains lines with the names
of the VM's to start. As a luxury you may even comment out vm's in the config file using a #,
instead of completely removing them. The config file can be set in the init script.
A note: I don't work with spaces in file names, and don't know what the script will do in such a
case. It might work, or not. Or try a inserting a \ (backslash) before a space, YMMV.
Another note: stopping the vm's doesn't kill them, so if they do not want to go down, kill them
(or the OS will, during a shutdown procedure). An alternative approach to acpishutdown would
be ssh remote login and perform a shutdown command, no guest additions needed for this to
work.
The vm's will be run as user 'vbox', you should set the vm's up using this account, or change
the name of the user executing the commands.
vbox-autorun.sh
#! /bin/sh
start()
{
[ -f $config ] || exit 6
echo "Starting Virtual Box machines..."
for vm in `cat ${config} | grep -v "#"`; do
echo " Starting: "${vm}
su - vbox -c "vboxheadless --startvm ${vm} &"
sleep 5
done
RETVAL=$?
echo
[ $RETVAL = 0 ]
return $RETVAL
}
stop()
{
[ -f $config ] || exit 6
echo "Stopping Virtual Box machines..."
for vm in `cat ${config} | grep -v "#"`; do
echo " Shutting down: "${vm}
su - vbox -c "vboxmanage controlvm ${vm} acpipowerbutton"
sleep 5
done
RETVAL=$?
list()
{
echo "List of running Virtual Boxes:"
su - vbox -c "vboxmanage list runningvms"
}
case "$1" in
start)
start
;;
stop)
stop
;;
list)
list
;;
*)
echo $"Usage: $0 {start|stop|list}"
RETVAL=2
[ "$1" = 'usage' ] && RETVAL=0
esac
exit $RETVAL
Add the script to the init directory, and enable it (the System V way).
startup.sh
cp -p vbox-autorun /etc/init.d/
chkconfig --add vbox-autorun
chkconfig vbox-autorun on
Now, you can start, stop and list (running) vm's, just using
service vbox-autorun start|stop|list
Display, Windows 8
Display seems to be very difficult, especially when using Windows 8. I've seen too many posts
on the web indicating that the console display is not stable, and I found even the Remote
Desktop (not the VRDE, native guest RDP access) unstable. Disabling 3D and 2D acceleration
helped, but not enough. Removing the WDDM from Windows 8 (also remove the files!), might
help. Update: it looks stable without the WDDM driver. What happens then is that you probably
can't choose the desired resolution for working in full screen, this command helps:
VBoxManage setextradata Windows8 CustomVideoMode1 1920x1200x32
It learns VirtualBox to tell Windows that the "monitor" is capable of 1920x1200 resolution (or
anything else). I'm curious whether this will really solve issues, just trying for now, but it looks
good, so far. Forget about automatic resizing, not important.
For developers this feature could come in handy, as it allows you to preset a few commonly
used resolutions. One may add, for example:
VBoxManage setextradata Windows8 CustomVideoMode2 1440x1050x32
VBoxManage setextradata Windows8 CustomVideoMode3 1280x800x32
Edge detection
Using Windows 8 as a guest, you absolutely need the mouse to 'hit the wall', otherwise mouse
positioning must be done very accurate in the corners. Just turn on full screen mode (make
sure the resolution matches!), or turn off mouse integration, and make sure the Window size
matches your Windows guest's size.