Difference between revisions of "VirtualBox"

From Blue-IT.org Wiki

(1. Full suspend)
(Reconnect on lost network connection)
 
(40 intermediate revisions by the same user not shown)
Line 10: Line 10:
 
* ubuntu linux host
 
* ubuntu linux host
 
* a bridged network ( called ''host interface'' in vbox )
 
* a bridged network ( called ''host interface'' in vbox )
 +
 +
== Bugs ==
 +
 +
--[[User:Apos|Apos]] ([[User talk:Apos|talk]]) 09:16, 28 November 2014 (CET)
 +
 +
From Ubuntu 12 to 14.04 there is - as time of writing - a bug in compiz which prevents working in full screen and with multi monitors:
 +
 +
* Unity Launcher and top panel overlays full screen mode - https://www.virtualbox.org/ticket/13365
 +
* Fullscreen sizing incorrect with multiple monitors - https://www.virtualbox.org/ticket/13410
 +
 +
The problem can be solved switching to the old legacy behaviour:
 +
 +
VBoxManage setextradata global GUI/Fullscreen/LegacyMode 1
  
 
==Installation==
 
==Installation==
===Ubuntu Feisty===
+
=== Ubuntu ===
 
Most important informations you will find on the excellent download pages at the [http://www.virtualbox.org/wiki/Downloads wiki of Innotec].
 
Most important informations you will find on the excellent download pages at the [http://www.virtualbox.org/wiki/Downloads wiki of Innotec].
  
Edit the source.list
+
===Prepare the user===
  vim /etc/apt/sources.list
+
Each user has to be part of the ''vboxusers'' group
 +
gpasswd -a your_username vboxusers
 +
 
 +
== Pause virtualmachine on lost focus ==
 +
Sometimes it is handy to pause a virtualmachine if it does not have the focus.
 +
 
 +
You'll get the name of your machine with:
 +
 
 +
VBoxManage list vms
 +
 
 +
The following script pauses the virtual machine, when it was idle (== did not have the focus) for a cerain amount of seconds.
 +
 
 +
Tip: Also add my script for [[VirtualBox#2._Pause_.2F_suspend_machines_per_user|automatically save the machine on suspend or hibernation]]!
 +
 
 +
  #!/bin/bash
 +
#
 +
# Monitor, if a virtual machine has the focus.
 +
# If not, pause the machine.
 +
# If it gets the focus, reenable it.
 
   
 
   
  # Virtual Box
+
  myVirtualMachine="Name of my virtual box Machine"
  deb http://www.virtualbox.org/debian feisty non-free
+
  SLEEP=2
 +
SLEEP_COUNTER=0
 +
IDLE_TIME=360
 +
 +
while(true); do
 +
 +
if xdotool getwindowfocus getwindowname | grep "${myVirtualMachine}"
 +
then
 +
SLEEP_COUNTER=0
 +
VBoxManage controlvm "${myVirtualMachine}" resume
 +
else
 +
SLEEP_COUNTER=$(( $SLEEP_COUNTER + 2 ))
 +
if [ $SLEEP_COUNTER -gt $IDLE_TIME ]
 +
then
 +
SLEEP_COUNTER=0
 +
VBoxManage controlvm "${myVirtualMachine}" pause
 +
fi
 +
fi
 +
 +
sleep ${SLEEP}
 +
done
  
Get the gpg key from innotec
+
== Reconnect guest on lost network connection ==
wget http://www.virtualbox.org/debian/innotek.asc \
+
When your host loses the network connection and and your routes changed (vpn connection lost / reconnected) the only way to reconnect the network within the guest machine is to switch on and of the guest network card. The following script does this:
&& apt-key add innotek.asc \
 
&& apt-get update \
 
&& apt-get install virtualbox
 
  
===Prepare the user===
+
#!/bin/bash
Each user has to be part of the ''vboxusers'' group
+
echo "$0: connecting and reconnecting network of all virtual machines ... "
  gpasswd -a your_username vboxusers
+
VBoxManage list runningvms | awk '{system("echo " $0 " |  cut -d'{' -f1 | sed s/\ $//g ;")}' | awk '{system("VBoxManage controlvm \""$0"\"  setlinkstate1 off; sleep 2; VBoxManage controlvm \""$0"\"  setlinkstate1 on;")}'
 +
  sleep 5
 +
VBoxManage list runningvms | awk '{system("echo " $0 " |  cut -d'{' -f1 | sed s/\ $//g ;")}' | awk '{system("VBoxManage controlvm \""$0"\"  setlinkstate1 off; sleep 2; VBoxManage controlvm \""$0"\"  setlinkstate1 on;")}'
 +
echo "$0: done."
  
 
==Configuring the network for bridging - Prerequisites==
 
==Configuring the network for bridging - Prerequisites==
 +
 +
'''UPDATE:''' --[[User:Apos|Apos]] ([[User talk:Apos|talk]]) 17:15, 28 January 2014 (CET)
 +
 +
'''This section is fairly outdated.
 +
 +
While the theoretically basics are still valid, bridging in Ubuntu is fairly simple nowadays. In Ubuntu 14.04 LTS, which will be available within April 2014 it is part of network manager and can be configured with a gui.
 +
 +
Please google around, you'll find a lot of howtos!'''
  
 
===Assumptions===
 
===Assumptions===
Line 59: Line 118:
 
# Use '''host interface''' networking.
 
# Use '''host interface''' networking.
 
# The device for the first virtual guest is called '''tap0'''.
 
# The device for the first virtual guest is called '''tap0'''.
 +
  
 
==Configuring the network - Concrete==
 
==Configuring the network - Concrete==
Line 164: Line 224:
 
==== With for ... in ====
 
==== With for ... in ====
 
  #!/bin/sh
 
  #!/bin/sh
 +
 +
if VBoxManage list runningvms; then exit 0; fi
 
   
 
   
 
  USR_RNNG=$(ps aux |grep VirtualBox |grep -v grep |cut -f1 -d' '|uniq)
 
  USR_RNNG=$(ps aux |grep VirtualBox |grep -v grep |cut -f1 -d' '|uniq)
Line 185: Line 247:
 
  #
 
  #
 
  # 90virtualbox: scan for active virtual machines and pause them to avoid seizing on host suspend
 
  # 90virtualbox: scan for active virtual machines and pause them to avoid seizing on host suspend
 +
 +
if VBoxManage list runningvms; then exit 0; fi
 
   
 
   
 
  for USR in "$(ps aux |grep VirtualBox |grep -v grep |cut -f1 -d' '| uniq)"; do
 
  for USR in "$(ps aux |grep VirtualBox |grep -v grep |cut -f1 -d' '| uniq)"; do
Line 204: Line 268:
 
           su - $USR -c "VBoxManage controlvm \"$VMS\" resume"
 
           su - $USR -c "VBoxManage controlvm \"$VMS\" resume"
 
           done
 
           done
 +
          ;;
 +
 +
          *) exit $NA
 +
          ;;
 +
      esac
 +
done
 +
 +
==== With awk ====
 +
 +
Under some circumstances (shells) it might be necessary to use '''awk''' instead of ''for ... in ... do ...'' done. BUT: put it in an extra script!
 +
 +
#> vim ~/bin/virtulbox_pause_all
 +
 +
VBoxManage list runningvms | \
 +
  awk '{system("echo " $0 " |  cut -d'{' -f1 | sed s/\\ $//g ;")}' | \
 +
  awk '{system("VBoxManage controlvm \""$0"\" pause ;")}'
 +
 +
Use it like this in ''/etc/pm/sleep.d/90virtualbox'':
 +
 +
#!/bin/sh
 +
#
 +
# 90virtualbox: scan for active virtual machines and pause them to avoid seizing on host suspend
 +
 +
if VBoxManage list runningvms; then exit 0; fi
 +
 +
for USR in "$(ps aux |grep VirtualBox |grep -v grep |cut -f1 -d' '| uniq)"; do
 +
      case "$1" in
 +
          suspend)
 +
          su - $USR -c "~/bin/virtualbox_machines_pause_all"
 +
;;
 +
 +
          hibernate)
 +
          su - $USR -c "~/bin/virtualbox_machines_savestate_all"
 +
          ;;
 +
 +
          thaw|resume)
 +
          su - $USR -c "~/bin/virtualbox_machines_resume_all"
 
           ;;
 
           ;;
 
   
 
   
Line 297: Line 398:
  
 
'''4.''' Create virtual box image which points to you USB drive. Therefore consult dmesg an see which driveletter was associated to your drive (mine was /dev/sdl). You also should know the partition number you like to use (in my case partition number 1)
 
'''4.''' Create virtual box image which points to you USB drive. Therefore consult dmesg an see which driveletter was associated to your drive (mine was /dev/sdl). You also should know the partition number you like to use (in my case partition number 1)
 +
 +
===== Linux  =====
 
  VBoxManage internalcommands createrawvmdk -filename vbox-external-partion-1.vmdk -rawdisk '''"/dev/sdl" -partitions 1 -relative'''
 
  VBoxManage internalcommands createrawvmdk -filename vbox-external-partion-1.vmdk -rawdisk '''"/dev/sdl" -partitions 1 -relative'''
 +
 +
===== Windows =====
 +
Start "cmd" with the administrator account of yout computer:
 +
 +
"c:\Program Files\Oracle\VirtualBox\VBoxManag
 +
e.exe" internalcommands createrawvmdk -filename "C:\Users\apos\VirtualBox VMs\Ub
 +
untu X61s\ubunturaw.vmdk" -rawdisk \\.\PhysicalDrive1
  
 
If you have more than one partition on your drive, use the syntax  
 
If you have more than one partition on your drive, use the syntax  
Line 335: Line 445:
  
 
I did not manage to get an USB stick (attached via an vmdk file inside a virtual machine) to boot this way, unfortunately. But, installing a bootable system onto the a stick mounted as vmdk image within a virtual machine went fine.
 
I did not manage to get an USB stick (attached via an vmdk file inside a virtual machine) to boot this way, unfortunately. But, installing a bootable system onto the a stick mounted as vmdk image within a virtual machine went fine.
 +
 +
== Updates (dkms) ==
 +
When using automatic kernel upgrades from virtual box guest additions it is wise to install dkms.
 +
 +
IMPORTANT: You MUST install dkms BEFORE installing the virtual guest additions.
 +
 +
== Mount a vmdk file in linux ==
 +
Follow the discussion on
 +
 +
* https://forums.virtualbox.org/viewtopic.php?f=26&t=33355
 +
* https://forums.virtualbox.org/viewtopic.php?f=7&t=14280&p=267910#p267910
 +
 +
=== Solution 1: vbfuse ===
 +
This can be achieved by using '''vbfuse''' which is part of the package '''virtualbox-fuse'''.
 +
The problem is: I did not manage to get to mount a snapshot ;-)
 +
 +
The problem is, if you are not using the ose version of virtualbox (GPL), but the "official" version, the installation of this package will force the uninstallation of your '''virtualbox-x.x''' package.
 +
 +
But only mount the image readonly (!), when the machine is running. You will risc severe damage to the filesystem of the virtual machine!
 +
 +
Download the '''virtualbox-fuse''' package without installing it, unpack it an use the "vbfuse"-binary "as is":
 +
 +
sudo apt-get install -d virtualbox-fuse
 +
mkdir /tmp/vboxfuse
 +
cd /tmp/vboxfuse
 +
mv /var/cache/apt/archives/virtualbox-fuse*.deb .
 +
dpkg -x virtualbox-fuse*.deb
 +
cp -av usr/bin/vbfuse //usr/bin/.
 +
cd /tmp
 +
rm -rf vboxfuse
 +
 +
The procedure is the following:
 +
 +
1. mount the virtual vmdk-diskimage (or snapshot) with vbfuse (readonly for a running machine!)
 +
  vdfuse -r -f your.vmdk /mnt/your_vmdk_mountpoint/
 +
 +
2. mount the corresponding partition of the mounted diskimage
 +
  mount /mnt/your_vmdk_mountpoint/Partition_X /mnt/your_vmdk_partition_2
 +
 +
Voilà you have access to your data!
 +
 +
=== Solution 2: guestmount via libguestfs ===
 +
* See: http://libguestfs.org/guestfs-faq.1.html
 +
* http://libguestfs.org/guestmount.1.html
 +
 +
Install '''guestfish''' and '''guestmount'''
 +
apt-get install guestfish guestmount libguestfs-tools
 +
 +
guestmount -i -r -a your.vmdk /mnt/your_vmdk_mountpoint
 +
 +
The same problem here: it is not possible to mount snapshots.
 +
 +
 +
== Snapshots ==
 +
Disk usage increases with the time using snapshots. So the best way is creating a new snapshot from time to time and delete the old one.
 +
 +
The following - quik an dirty ! - script searches for all snapshots of the machine "NAME OF VM", deletes it's snapshots and creates a new one.
 +
 +
#!/bin/bash
 +
# see
 +
#    https://www.virtualbox.org/manual/ch08.html
 +
# for command line options of vboxmanage
 +
 +
# Delete all snapshots
 +
vboxmanage showvminfo "NAME OF VM" | grep "Name:" | grep UUID | cut -d ":" -f3 | sed -e 's/ //' | sed -e 's/)//' | sed -e 's/*//' | sed -e 's/ //' | awk '{system("vboxmanage snapshot \"NAME OF VM\" delete " $0)}'
 +
 +
# . Create a new one
 +
vboxmanage snapshot "${VM}" take $(date +%F) --description "Auto snapshot taken with $0."
 +
 +
 +
==Changelog==
 +
--[[User:Apos|Apos]] ([[User talk:Apos|talk]]) 11:28, 17 November 2015 (CET) Removed duplicate entries in wiki. Added reconnect script.
 +
 +
--[[User:Apos|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.
 +
 +
--[[User:Apos|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.
 +
  
 
[[Category:Virtualisation]]
 
[[Category:Virtualisation]]
 
[[Category:Ubuntu]]
 
[[Category:Ubuntu]]

Latest revision as of 10:40, 17 November 2015

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 )

Bugs

--Apos (talk) 09:16, 28 November 2014 (CET)

From Ubuntu 12 to 14.04 there is - as time of writing - a bug in compiz which prevents working in full screen and with multi monitors:

The problem can be solved switching to the old legacy behaviour:

VBoxManage setextradata global GUI/Fullscreen/LegacyMode 1

Installation

Ubuntu

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

Prepare the user

Each user has to be part of the vboxusers group

gpasswd -a your_username vboxusers

Pause virtualmachine on lost focus

Sometimes it is handy to pause a virtualmachine if it does not have the focus.

You'll get the name of your machine with:

VBoxManage list vms

The following script pauses the virtual machine, when it was idle (== did not have the focus) for a cerain amount of seconds.

Tip: Also add my script for automatically save the machine on suspend or hibernation!

#!/bin/bash
#
# Monitor, if a virtual machine has the focus.
# If not, pause the machine.
# If it gets the focus, reenable it.

myVirtualMachine="Name of my virtual box Machine"
SLEEP=2
SLEEP_COUNTER=0
IDLE_TIME=360

while(true); do

	if xdotool getwindowfocus getwindowname | grep "${myVirtualMachine}"
	then
		SLEEP_COUNTER=0
		VBoxManage controlvm "${myVirtualMachine}" resume
	else
		SLEEP_COUNTER=$(( $SLEEP_COUNTER + 2 ))
		if [ $SLEEP_COUNTER -gt $IDLE_TIME ]
		then
			SLEEP_COUNTER=0
			VBoxManage controlvm "${myVirtualMachine}" pause
		fi
	fi

	sleep ${SLEEP}
done

Reconnect guest on lost network connection

When your host loses the network connection and and your routes changed (vpn connection lost / reconnected) the only way to reconnect the network within the guest machine is to switch on and of the guest network card. The following script does this:

#!/bin/bash
echo "$0: connecting and reconnecting network of all virtual machines ... "
VBoxManage list runningvms | awk '{system("echo " $0 " |  cut -d'{' -f1 | sed s/\ $//g ;")}' | awk '{system("VBoxManage controlvm \""$0"\"  setlinkstate1 off; sleep 2; VBoxManage controlvm \""$0"\"  setlinkstate1 on;")}'
sleep 5
VBoxManage list runningvms | awk '{system("echo " $0 " |  cut -d'{' -f1 | sed s/\ $//g ;")}' | awk '{system("VBoxManage controlvm \""$0"\"  setlinkstate1 off; sleep 2; VBoxManage controlvm \""$0"\"  setlinkstate1 on;")}'
echo "$0: done."

Configuring the network for bridging - Prerequisites

UPDATE: --Apos (talk) 17:15, 28 January 2014 (CET)

This section is fairly outdated.

While the theoretically basics are still valid, bridging in Ubuntu is fairly simple nowadays. In Ubuntu 14.04 LTS, which will be available within April 2014 it is part of network manager and can be configured with a gui.

Please google around, you'll find a lot of howtos!

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 on standby / hibernation

This is tested for Ubuntu Lucid (10.04) and virtualbox 3.2x.

Thanks to blog.neonatus.net, but i had to change the code a little bit.

Edit the file

/etc/pm/sleep.d/90virtualbox

and make it executable

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

There are two ways obtaining the goal:

  1. Doing a full suspend
  2. Only pause the machines (which is enough)

1. Full suspend

This is the save way, because it always suspends the virtual machine. So don't matter what happens to your computer during suspend or hibernation, the machine is in a save state. You can try to use "pause" instead of "savestate" which i could only use when suspending the computer, not hibernating.

With for ... in

#!/bin/sh

if VBoxManage list runningvms; then exit 0; fi

USR_RNNG=$(ps aux |grep VirtualBox |grep -v grep |cut -f1 -d' '|uniq)
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

2. Pause / suspend machines per user

This script adds the support for doing the job per user and differs between suspend (only pauses the machine) and hibernate (suspends the machine).

Everytime you suspend or hibernate your computer and you have virtual machines running, they are paused first. In case you hibernate your pc, the machine is suspended too, not just paused. Pausing in conjunction with hibernation also might work with your computer, try it out. In my case hibernation then refused to work.

#!/bin/sh
#
# 90virtualbox: scan for active virtual machines and pause them to avoid seizing on host suspend

if VBoxManage list runningvms; then exit 0; fi

for USR in "$(ps aux |grep VirtualBox |grep -v grep |cut -f1 -d' '| uniq)"; do
     case "$1" in
         suspend)
         for VMS in "$(su - $USR -c 'VBoxManage list runningvms | egrep ^\"*\" | cut -d"\"" -f2 | sed s/\"//g')"; do
             su - $USR -c "VBoxManage controlvm \"$VMS\" pause"
         done
         ;;

         hibernate)
         for VMS in "$(su - $USR -c 'VBoxManage list runningvms | egrep ^\"*\" | cut -d"\"" -f2 | sed s/\"//g')"; do
             su - $USR -c "VBoxManage controlvm \"$VMS\" savestate"
         done
         ;;

         thaw|resume)
         for VMS in "$(su - $USR -c 'VBoxManage list runningvms | egrep ^\"*\" | cut -d"\"" -f2 | sed s/\"//g')"; do
         su - $USR -c "VBoxManage controlvm \"$VMS\" resume"
         done
         ;;

         *) exit $NA
         ;;
      esac
done

With awk

Under some circumstances (shells) it might be necessary to use awk instead of for ... in ... do ... done. BUT: put it in an extra script!

#> vim ~/bin/virtulbox_pause_all
VBoxManage list runningvms | \
  awk '{system("echo " $0 " |  cut -d'{' -f1 | sed s/\\ $//g ;")}' | \
  awk '{system("VBoxManage controlvm \""$0"\" pause ;")}'

Use it like this in /etc/pm/sleep.d/90virtualbox:

#!/bin/sh
#
# 90virtualbox: scan for active virtual machines and pause them to avoid seizing on host suspend

if VBoxManage list runningvms; then exit 0; fi

for USR in "$(ps aux |grep VirtualBox |grep -v grep |cut -f1 -d' '| uniq)"; do
     case "$1" in
         suspend)
         su - $USR -c "~/bin/virtualbox_machines_pause_all"
	 ;;

         hibernate)
         su - $USR -c "~/bin/virtualbox_machines_savestate_all"
         ;;

         thaw|resume)
         su - $USR -c "~/bin/virtualbox_machines_resume_all"
         ;;

         *) exit $NA
         ;;
      esac
done

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 /PATHTO/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

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

vbox_start() {
 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

Mount and boot an operating system from an external USB Drive

To do this, we use the ability to use raw filesystems with virtualbox. That is: creating and using a virtualbox image file withing virtualbox, that points to an external filesystem.

There is only one drawback: the boot support is not working like expected. But this is very easy to achieve using the Super Grub Disk 2 ISO which will be used as boot device.

In conjunction with the Plop Boot Manager CD you can literally boot your external USB rescue partition from any PC with USB (even USB 1.x) if you have a build in CD/DVD.

Please see and also read: VirtualBox Rawdisk Support.

Warning

Never ever (!!!) simultaneously access or alter files of a partition within your operating system and your virtual machine.

Either do the one or the other. Be warned.

Preparation

If you plan to replace an existing disk image, please read the Troubleshooting-Section before continuing.

1. Add yourself to the group "disk":

usermod -a -G disk your_user_name

2. Change to your virtualbox directory (where your virtualmachines reside).

3. Mount your USB drive. This is important!

4. Create virtual box image which points to you USB drive. Therefore consult dmesg an see which driveletter was associated to your drive (mine was /dev/sdl). You also should know the partition number you like to use (in my case partition number 1)

Linux
VBoxManage internalcommands createrawvmdk -filename vbox-external-partion-1.vmdk -rawdisk "/dev/sdl" -partitions 1 -relative
Windows

Start "cmd" with the administrator account of yout computer:

"c:\Program Files\Oracle\VirtualBox\VBoxManag

e.exe" internalcommands createrawvmdk -filename "C:\Users\apos\VirtualBox VMs\Ub untu X61s\ubunturaw.vmdk" -rawdisk \\.\PhysicalDrive1

If you have more than one partition on your drive, use the syntax

-partitions 1,5,6

to be able to access all partitions within your drive

5. No we alter the created file using the /dev/by-id-Folder because it is not for sure you will get the same drive number (/dev/sdx) everytime you plug in you external drive:

vim vbox-external-partion-1.vmdk

Change device string in the line containing (the "###"'s stand for some numbers, which can differ from your setup, don't touch anything here!)

RW ######## FLAT "/dev/sdj1" 0

Insert the dev-by-id string for the given partition (!!!) instead of the /dev/sdxn name e.g. "/dev/disk/by-id/usb-ST910082_1AS_502EE8888888-0:0-part1" so it looks like this:

RW ######## FLAT "/dev/disk/by-id/usb-ST910082_1AS_502EE8888888-0:0-part1" 0

an save the file.

This was the partition we added using "-partitions 1" in the above [4] created VBoxManage-syntax. If you added more than one partition you should alter the other entries too. This is straight forward.

6. Use the Super Grub Disk 2 ISO to boot your virtual machine, choose the appropriate kernel and you should be presented by your operating system.

Attention: you cannot (until now) boot directly from the partiton with grub! You have to use the Super Grub Disk 2 ISO. Sometimes, after installing the virtual guest additions in the machine, I forgot to add the super-grub2-disk again and was provided by an error, that the machine could not be started. So if this happens to you: just remove the virtual guest additions ISO and readd the super-grub2-disk again!

7. Needless to say to install your virtualbox guest additions into you machine.

Troubleshooting

System does not boot

Did you really use the Super Grub Disk 2 ISO to boot your virtual machine ???!!!

Probably you did forget to remount it after installing the virtual guest additions. Sometimes, after installing the virtual guest additions in the machine, I forgot to add the super-grub2-disk again and was provided by an error, that the machine could not be started. So if this happens to you: just remove the virtual guest additions ISO and readd the super-grub2-disk again!

Alter or removing an existing virtual disk image

First release and remove the virtual disk with the virtual disks manager of virtualbox. Completely delete the image file. You won't delete or loose any data this way, because the image file we created is only a text file containing statistic data about your disk. You can remove an recreate it savely.

! Again: it is not enough to release the image within the setup of your virtual machine's configuration dialog, you have to use the virtual disks manager !

USB Sticks

I did not manage to get an USB stick (attached via an vmdk file inside a virtual machine) to boot this way, unfortunately. But, installing a bootable system onto the a stick mounted as vmdk image within a virtual machine went fine.

Updates (dkms)

When using automatic kernel upgrades from virtual box guest additions it is wise to install dkms.

IMPORTANT: You MUST install dkms BEFORE installing the virtual guest additions.

Mount a vmdk file in linux

Follow the discussion on

Solution 1: vbfuse

This can be achieved by using vbfuse which is part of the package virtualbox-fuse. The problem is: I did not manage to get to mount a snapshot ;-)

The problem is, if you are not using the ose version of virtualbox (GPL), but the "official" version, the installation of this package will force the uninstallation of your virtualbox-x.x package.

But only mount the image readonly (!), when the machine is running. You will risc severe damage to the filesystem of the virtual machine!

Download the virtualbox-fuse package without installing it, unpack it an use the "vbfuse"-binary "as is":

sudo apt-get install -d virtualbox-fuse
mkdir /tmp/vboxfuse
cd /tmp/vboxfuse
mv /var/cache/apt/archives/virtualbox-fuse*.deb .
dpkg -x virtualbox-fuse*.deb
cp -av usr/bin/vbfuse //usr/bin/.
cd /tmp
rm -rf vboxfuse

The procedure is the following:

1. mount the virtual vmdk-diskimage (or snapshot) with vbfuse (readonly for a running machine!)

 vdfuse -r -f your.vmdk /mnt/your_vmdk_mountpoint/

2. mount the corresponding partition of the mounted diskimage

 mount /mnt/your_vmdk_mountpoint/Partition_X /mnt/your_vmdk_partition_2

Voilà you have access to your data!

Solution 2: guestmount via libguestfs

* See: http://libguestfs.org/guestfs-faq.1.html
* http://libguestfs.org/guestmount.1.html

Install guestfish and guestmount

apt-get install guestfish guestmount libguestfs-tools
guestmount -i -r -a your.vmdk /mnt/your_vmdk_mountpoint

The same problem here: it is not possible to mount snapshots.


Snapshots

Disk usage increases with the time using snapshots. So the best way is creating a new snapshot from time to time and delete the old one.

The following - quik an dirty ! - script searches for all snapshots of the machine "NAME OF VM", deletes it's snapshots and creates a new one.

#!/bin/bash
# see 
#    https://www.virtualbox.org/manual/ch08.html
# for command line options of vboxmanage

# Delete all snapshots
vboxmanage showvminfo "NAME OF VM" | grep "Name:" | grep UUID | cut -d ":" -f3 | sed -e 's/ //' | sed -e 's/)//' | sed -e 's/*//' | sed -e 's/ //' | awk '{system("vboxmanage snapshot \"NAME OF VM\" delete " $0)}'

# . Create a new one
vboxmanage snapshot "${VM}" take $(date +%F) --description "Auto snapshot taken with $0."


Changelog

--Apos (talk) 11:28, 17 November 2015 (CET) Removed duplicate entries in wiki. Added reconnect script.

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