Difference between revisions of "VirtualBox"

From Blue-IT.org Wiki

(Use Case)
(Start and Stop script)
Line 143: Line 143:
  
 
My Tip: '''Simply stay with samba'''.
 
My Tip: '''Simply stay with samba'''.
 +
 +
== Auto suspend virtual machines == 
 +
Edit the file
 +
/etc/pm/sleep.d/90virtualbox
 +
 +
#!/bin/sh
 +
 +
USR_RNNG=$(ps aux |grep VirtualBox |grep -v grep |cut -f1 -d' ')
 +
if [ "x$USR_RNNG" != "x" ]; then
 +
    if  [ $(id -u) -eq 0 ]; then
 +
        su $USR_RNNG -c $0
 +
    else
 +
        for VMS in "$(VBoxManage list runningvms | egrep ^\"*\" | cut -d"\"" -f2 | sed s/\"//g)"; do
 +
            VBoxManage controlvm "$VMS" savestate
 +
        done
 +
    fi
 +
fi
 +
exit 0
 +
 +
and make it executable
 +
chmod +x /etc/pm/sleep.d/90virtualbox
 +
  
 
==Start and Stop script==
 
==Start and Stop script==

Revision as of 12:24, 8 June 2010

Changelog

--Apos 20:33, 11 September 2007 (CEST) Encountered some problems with the network inside of windows guest when doing a snapshop. Could not connect to the host machine, but to the internet and all other machines in the network. The next day the problem was gone.

--Apos 18:11, 10 September 2007 (CEST) All tested with ubuntu feisty host machine and VirtualBox version 1.5. The machine is a 2 GHZ AMD Athlon with two network cards.

Motivation

I am not explaining the installation of a guest virtual machine here.

This guide's only purpose is to point out the major pitfalls using

  • ubuntu linux host
  • a bridged network ( called host interface in vbox )

Installation

Ubuntu Feisty

Most important informations you will find on the excellent download pages at the wiki of Innotec.

Edit the source.list

vim /etc/apt/sources.list

# Virtual Box
deb http://www.virtualbox.org/debian feisty non-free

Get the gpg key from innotec

wget http://www.virtualbox.org/debian/innotek.asc \
&& apt-key add innotek.asc \
&& apt-get update \
&& apt-get install virtualbox

Prepare the user

Each user has to be part of the vboxusers group

gpasswd -a your_username vboxusers

Configuring the network for bridging - Prerequisites

Assumptions

Two network cards.

Our machine has two network cards: eth0 and eth1.

  • eth0 will be used for the bridged network. It connects internal to the brigde.
  • eth1 will be left alone. It is e.g. a gigabit network card for the heavier internal network traffic.

Important to know

The bridge takes over a physical NIC

  • You should understand, that the bridged network (e.g. br0) will completely superseding the original network card (e.g. eth0). This NIC will change to promiscuous mode.
  • All settings of the connected physical network card, including ipaddress, routes, gateway setting etc. must be transferred to this bridge's settings. This assures, that the system is reachable from the outside like before.

Virtual network configuration on the ubuntu/debian host

We need - at least - two new devices for bridged networking.

  1. The bridge - e.g. br0
  2. At least one virtual network card - e.g. tap0

br0 is the name of the bridge. It gets the IP of eth0, but this is just for convenience, it can be configured like any other network card. Eth0 will be internally connected to this software network switch.

tap0 is the name of the virtual network card, that connects internal to br0. It will be used for connection inside of the virtual machine. If you run multiple virtual machines in parallel, you should configure more tap devices and add them to the bridge.

Configure the virtual machines settings for the network adapter:

  1. Use host interface networking.
  2. The device for the first virtual guest is called tap0.

Configuring the network - Concrete

Additional packages (ubuntu feisty)

Links: the best ..., interesting ..., be careful ....

Install the uml and bridge utilities:

apt-get install uml-utilities bridge-utils

Alter the default way, ubuntu assigns the rights for

vim /etc/network/if-pre-up.d/uml-utilities
#chown root:uml-net /dev/net/tun
chown root:vboxusers /dev/net/tun

Configuration files (ubuntu feisty)

The tap and bridge devices will be configured using the default ubuntu network scripts. I commented out the old device settings for eth1 so that you can see the changes to the original ones.

/etc/init.d/network stop
vim /etc/network/interfaces
# this_gigabit_server
auto eth0
 iface eth0 inet static
 address 192.168.0.4
 netmask 255.255.255.0
 up route add another_gigabit_server gw this_gigabit_server eth0
 up route add another_server gw this_gigabit_server eth0

# this_server
auto eth1
iface eth1 inet manual
# iface eth1 inet static
#  address 192.168.0.3
#  netmask 255.255.255.0
#  up route add notebook gw this_server eth1
#  up route add notebook_wlan gw this_server eth1
#  up route add server_two gw this_gigabit_server eth1
#  gateway 192.168.0.254

# New section for bridging

# tap devices
auto tap0
iface tap0 inet manual
 tunctl_user your_username
 uml_proxy_arp this_server
 uml_proxy_ether eth1

# - install more if necessary
# - don't forget to add them to br0
# auto tap1
# iface tap1 inet manual
#  tunctl_user your_username
#  uml_proxy_arp this_server
#  uml_proxy_ether eth1
   
# bridge
auto br0
iface br0 inet static
 address 192.168.0.3
 netmask 255.255.255.0
 up route add notebook gw this_server br0
 up route add notebook_wlan gw this_server br0
 up route add server_two gw this_gigabit_server br0
 gateway 192.168.0.254
 bridge_ports eth1 tap0
 # bridge_ports eth1 tap0 tap1
 bridge_maxwait 0
/etc/init.d/networking start

Filesharing with samba

According to the Ubuntu Starter Guide install samba and feel free to ether connect via network disks or the connection wizard.

For using network disks inside of Windows NT/XP do

net use x: //ip_samba_server/name_of_share /PERSISTENT:yes

inside of a command terminal.

Filesharing with NFS

Is really a mess inside of windows ... You could try cygwin or mingw but the result is not worth the effort.

Microsoft offers a package called Windows Services for Unix. It is a +200MB package really not worth installing - and configuring! I tried several hours on Windows 2000 professional - you have to map your unix usernames to windows ...

My Tip: Simply stay with samba.

Auto suspend virtual machines

Edit the file

/etc/pm/sleep.d/90virtualbox
#!/bin/sh

USR_RNNG=$(ps aux |grep VirtualBox |grep -v grep |cut -f1 -d' ')
if [ "x$USR_RNNG" != "x" ]; then
    if  [ $(id -u) -eq 0 ]; then
        su $USR_RNNG -c $0
    else
        for VMS in "$(VBoxManage list runningvms | egrep ^\"*\" | cut -d"\"" -f2 | sed s/\"//g)"; do
            VBoxManage controlvm "$VMS" savestate
        done
    fi
fi
exit 0

and make it executable

chmod +x /etc/pm/sleep.d/90virtualbox


Start and Stop script

Use Case

Nick likes to just press a starter in gnome panel, and his favorite virtual machine should pop up automatically. Pressing this button again should save the virtual machines state immediately.

Toggle virtual machine script

Edit a script called "toggle_vbox_machine". For PATHTO you can use e.g., /root/bin, /$USER/bin, /usr/bin, /usr/local/bin

mkdir -p /PATHTO/bin
vim /root/bin/toggle_vbox_machine
chmod 755 /PATHTO/bin/toggle_vbox_machine

The script for a special virtual machine will be called like this:

/PATHTO/bin/toggle_vbox_machine "Name of virtual Machine"

Here's the script

# !/bin/bash
#
# Toggle start of a vbox 

VBOX="$1"

if $VBOX 
then 
       echo Using $VBOX ...
else
       zenity  --info --title "Virtual machine" \
          --text "No name for vbox given.\nExiting..."
       exit 1
fi

# If a routename (/etc/hosts!) is given
# the routes are reinitialized
ROUTENAME="$2"
REINIT_ROUTES=/root/bin/reinit_routes.sh

# Machine powered off
INFO=$(VBoxManage showvminfo "${VBOX}" | \
       grep State | \
       cut -d ":" -f2 | \
       cut -d"(" -f1 | \
       sed s/" "/""/g)

VBOX_START="VBoxManage startvm"
VBOX_CONTROL="VBoxManage controlvm"

reinit_routes() {
 if $ROUTENAME 
 then
   if route | grep $ROUTENAME
   then
     echo Check of route seams ok ...
   else
     echo Setting route ...
     zenity  --info --title "Virtual machine" \
        --text "Route not set.\n\nPlease enter password." && \
     /usr/bin/gksudo "/bin/bash $REINIT_ROUTES"
   fi
 else
   echo No routes ...
 fi
}

vbox_start() {
 echo "Reinitialise routes ..."
 reinit_routes
 echo "Starting ${VBOX} ..."
 $VBOX_START "${VBOX}"
}

vbox_save() {
 echo "Saving state of ${VBOX} ..."
 $VBOX_CONTROL "${VBOX}" savestate
}


case $INFO in
       "powered off")  vbox_start;;
       "aborted")      vbox_start;;
       "saved")        vbox_start;;
       "running")      vbox_save;;
       *)              vbox_start;;
esac

Routing with more than one network card on host

As you can see, the script uses also a parameter for checking the routes. This is only necessary, if you are using more than one network card in your HOST system.

The reinit_routes.sh scans the /etc/network/interfaces for route commands and just eeinit's them - that's all.

All you have to to is

  • enter correct routes for EACH virtual machine
  • and it's correct network alias in /etc/hosts
vim /PATHTO/bin/reinit_routes.sh
chmod 755 /PATHTO/bin/reinit_routes.sh
#!/bin/sh
#
# For ubuntu / debian way of
# /etc/network/interfaces

cat /etc/network/interfaces | grep route | \
    grep -v "#" | sed s/up//g | \
    awk '{system($0)}'

Call the toggle_vbox_machine script like this

/PATHTO/bin/toggle_vbox_machine "Name of virtual machine" "network_alias"