0% found this document useful (0 votes)
49 views

VirtualBox Management

This document provides tips and tricks for using VirtualBox virtualization software. It covers installation, basic usage and management of virtual machines, hard disks, and networks. The document explains how to install and configure VirtualBox, create and manage virtual machines and disks, and set up different types of virtual networks.

Uploaded by

Dusan Markovic
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

VirtualBox Management

This document provides tips and tricks for using VirtualBox virtualization software. It covers installation, basic usage and management of virtual machines, hard disks, and networks. The document explains how to install and configure VirtualBox, create and manage virtual machines and disks, and set up different types of virtual networks.

Uploaded by

Dusan Markovic
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

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.

 install the download


 install the extension pack (otherwise you'll lack the remote console, could be very
inconvenient in headless mode)

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)

 Virtual hd's are registered in the vm based on UUID and filename

 Virtual hd's are assigned to the controller based on UUID

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

o visible as a real host in a network

o full network access outside host (from and to)


o no connectivity to other VM's within the host

o multiple VM's cannot interact without external network

 NAT

o fixed IP address assigned by VirtualBox

o no connectivity to other VM's within the host

o INcoming traffic from outside host can be routed to the VM using host port
forwarding

o OUTgoing traffic uses IP of the host

o multiple VM's cannot interact without external network

 HostOnly

o IP address can be set manually or using the VirtualBox DHCP server

o no traffic possible using the host's interface: no connectivity outside the host
(in, out)

o full connectivity between VM's within host

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

Cloning virtual hard disks


Cloning can easily be done from the GUI. Good to know:

 every cloned hd gets a new UUID


 the uuid is exposed to the operating system, which may cause troubles during startup

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

# assign a //new// uuid to a disk:


$> VBoxManage internalcommands sethduuid /data/vbox/mydisk-clone.vdi

# assign a //specific// UUID to a disk:


$> VBoxManage internalcommands sethduuid /data/vbox/mydisk-clone.vdi bdf7e199-121a-4724-9dfd-782f4656eaa9

Changing the allocation type


while retaining the disk ID.

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

# Get the info:


cat myvm.info | grep vdi

# 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

# this lists for example


# UUID: 5ebabd04-39b3-44ed-9450-1b98357fef6c

# 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

# boot the vm :-)

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.

Attaching ISO files


ISO (ISO9661) DVD/CD images can be easily attached and removed to/from a VM. Use the GUI,
or in headless mode, the following commands:

# Connect an ISO image to the 'DVD' drive of the VM


VBoxManage storageattach myvm --storagectl "IDE" --port 0 --device 0 --type dvddrive --medium
"/home/me/my.iso"

# 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.

Attach host DVD drive (also on OSX host)


I couldn't find out how to attach the host DVD drive by name on my OSX host. By UUID
however, works perfectly well (and all of this should work using another host OS too). The
commands to help you perform this action by means of the commandline (what most of this
page is about), handy if you do have a non graphical VM host server:

# List available DVD drives


VBoxManage list hostdvds

# The following oneliner gives you the first one listed


VBoxManage list hostdvds | grep UUID | awk '{print $2}' | head -n 1

# Attach it to the guest OS


VBoxManage storageattach myvm --storagectl "IDE" --port 0 --device 0 --type dvddrive --medium
host:00445644-2d44-2052-2020-554a2d38354a

# Of course, this can also be done in an even nicer oneliner


VBoxManage storageattach myvm --storagectl "IDE" --port 0 --device 0 --type dvddrive --medium
host:`VBoxManage list hostdvds | grep UUID | awk '{print $2}' | head -n 1`

# And to remove
VBoxManage storageattach myvm --storagectl "IDE" --port 0 --device 0 --medium emptydrive

A handy script could be something like this:

vbox-dvd
#!/bin/bash

if [ "$1" = "--help" ] || [ "$1" = "" ]; then


echo "Usage: vbox-dvd \"Guest Name\""
echo " vbox-dvd --remove \"Guest Name\""
exit 1
fi

if [ "$1" == "--remove" ]; then


MYVM=$2
VBoxManage storageattach "${MYVM}" --storagectl "IDE" --port 0 --device 0 --medium emptydrive
exit $?
else
MYVM=$1
VBoxManage storageattach "${MYVM}" --storagectl "IDE" --port 0 --device 0 --type dvddrive
--medium host:`VBoxManage list hostdvds | grep UUID | awk '{print $2}' | head -n 1`
exit $?
fi

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

# Wouter init file for vbox-autorun


# Copyright (C) 2012-4 Wouter Boasson
# $Id: vbox-autorun.initd,v 1.0

# For RedHat and cousins:


# chkconfig: - 99 01
# description: Script to automatically run and stop Virtual Box machines
# processname: vbox-autorun

### BEGIN INIT INFO


# Provides: vbox-autorun
# Required-Start: $local_fs $network vboxdrv
# Required-Stop: $local_fs $network
# Should-Start:
# Default-Start:
# Default-Stop: 0 1 2 3 4 5 6
# Short-Description: Script to auto-start VirtualBox machines
# Description: This script automatically starts specific VirtualBox machines
# and stops them on shutdown, it will wait for the vm to stop
### END INIT INFO

# This program is free software; you can redistribute it and/or modify it


# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any later
# version.
# You should have received a copy of the GNU General Public License (for
# example COPYING); if not, write to the Free Software Foundation, Inc., 675
# Mass Ave, Cambridge, MA 02139, USA.

# Source function library


. /etc/rc.d/init.d/functions
RETVAL=0
config=/somewhere/myvms-to-start.conf

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=$?

# Wait until they're stopped, at max one minute


w="0"
while [ $w -lt 10 ]
do
sleep 5
if [ "`vboxmanage list runningvms`" == "" ]; then
RETVAL=0
exit 0
fi
i=$[$w+1]
done
echo
[ $RETVAL = 0 ]
return $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

and they'll automatically come up or go down with the host OS.

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.

You might also like