Skip to Content

Vorkbaard uit de toekomst

Kapitein Vorkbaard to the rescue!

Ahoy, maatjes!

Taal

Scifi

Tech

Twitter

« The Wandering Earth, door Cixin Liu

Dichronauts, door Greg Egan »

How to set up a VirtualBox server in Debian 9: web interface, autostart, backup

Written on February 9, 2018 at 06:16, by Kapitein Vorkbaard

This article describes how to set up a VirtualBox server in Debian 9 (Stretch):

Installing VirtualBox

VirtualBox web interface: phpVirtualBox

Set up and use VMs

Automatically start PhpVirtualBox on boot

Automatically pause VMs on reboot/shutdown and resume them on boot

Set up a backup system

Troubleshooting

If this article has helped you please let me know by clicking some of the ads on this page. If you’re really euphoric there’s a donation button on the right ;) Thanks!

Versions
– Debian 9.3 (Stretch)
– VirtualBox 5.2
– phpVirtualBox 5.0.5

Assumptions
You have an up-to-date Debian 9 server with a working internet connection.

Installing VirtualBox

Create a file /etc/apt/sources.list.d/virtualbox.list and add:

?

1

deb http://download.virtualbox.org/virtualbox/debian stretch contrib

Check here which key you need (opens in a new tab). At the time of writing this is oracle_vbox_2016.asc. Download and install the key so apt doesn’t complain when using the source:

?

1

wget https://www.virtualbox.org/download/oracle_vbox_2016.asc -O- | apt-key add -

Update sources:

?

1

# aptitude update

Check which version of VirtualBox is current:

?

1

2

3

4

5

root@vorkian:/etc/apt/sources.list.d# aptitude search virtualbox

v   virtualbox                      -

p   virtualbox-5.0                  - Oracle VM VirtualBox

p   virtualbox-5.1                  - Oracle VM VirtualBox

p   virtualbox-5.2                  - Oracle VM VirtualBox

Unless you have a reason to install an older version, install the most current:

?

1

# aptitude install VirtualBox-5.2

During installation VirtualBox will inform you it needs some more bits:

?

1

2

3

4

5

6

7

8

9

10

11

12

This system is currently not set up to build kernel modules.

Please install the Linux kernel "header" files matching the current kernel

for adding new hardware support to the system.

The distribution packages containing the headers are probably:

linux-headers-amd64 linux-headers-4.9.0-3-amd64

This system is currently not set up to build kernel modules.

Please install the Linux kernel "header" files matching the current kernel

for adding new hardware support to the system.

The distribution packages containing the headers are probably:

linux-headers-amd64 linux-headers-4.9.0-3-amd64

There were problems setting up VirtualBox.  To re-start the set-up process, run /sbin/vboxconfig

Let’s install them:

?

1

# aptitude install linux-headers-amd64 linux-headers-4.9.0-3-amd64

Check the recommendation (“The distribution packages containing the headers are probably:” etc.) for the actual packages you need to install.

Rerun the VirtualBox configuration, as instructed:

?

1

# vboxconfig

Extension pack
Check here what url you should use for the latest extension pack.

?

1

# wget https://download.virtualbox.org/virtualbox/5.2.6/Oracle_VM_VirtualBox_Extension_Pack-5.2.6-120293.vbox-extpack

Install the extension pack:

?

1

# VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-5.2.6-120293.vbox-extpack

Set up an account for VirtualBox so it doesn’t run as root. If you have multiple users with their own VMs that’s ok too.

?

1

2

# useradd -d /home/vbox -m -g vboxusers -s /bin/bash vbox

# passwd vbox

Remember the password, you’ll need it later on.

VirtualBox web interface: phpVirtualBox

Tell the system which user the web service will run as. In /etc/default/virtualbox add:

?

1

2

VBOXWEB_USER=vbox

VBOXWEB_HOST=127.0.0.1

Start the vboxweb service as the dedicated user:

?

1

# sudo -u vbox vboxwebsrv --background

Install webserver and dependencies

?

1

# aptitude install apache2 php php-soap php-xml unzip

Note: it would go too far to talk about web server security here. You will understand that it is important for internet facing (and any other) servers to have decent security. If you’re building an internet facing production server you must implement supplemental security, otherwise you will find your servers compromised.

Download phpVirtualBox:

?

1

2

3

# cd /var/www/html

# wget http://sourceforge.net/projects/phpvirtualbox/files/latest/download -O phpvirtualbox.zip

# unzip phpvirtualbox.zip

If you can’t unzip phpvirtualbox.zip try different SourceForge mirrors. The German ones tend to work well for me.

Rename the folder for easier typing:

?

1

# mv phpvirtualbox-5.0-5/ phpvirtualbox

Change into the phpVirtualBox folder and copy the initial config file:

?

1

2

# cd /var/www/html/phpvirtualbox/

# cp config.php-example config.php

Open up the config file and add the vboxweb service credentials. In /var/www/html/phpvirtualbox/config.php change:

?

1

2

var $username = 'vbox';

var $password = 'P@55w0rd';

…Using the password you entered for the vbox user earlier.

Point your browser to http://yourserver/phpvirtualbox (use your own server name or IP address). The default credentials are admin/admin.

Optional: work around a version notification
At the time of writing phpVirtualBox and VirtualBox itself are not at the same version. PhpVirtualBox will complain about this but we can work around it. Note that this verion incongruity may result in unexpected behaviour and it’s best to keep an eye out for new versions.

In /var/www/html/phpvirtualbox/endpoints/lib/config.php change:

?

1

define('PHPVBOX_VER', '5.0-0');

to

?

1

define('PHPVBOX_VER', '5.2-0');

or the current version if later than 5.2.

For the wsdl file create a symlink with the same name:

?

1

ln -s /var/www/html/phpvirtualbox/endpoints/lib/vboxwebService-5.0.wsdl /var/www/html/phpvirtualbox/endpoints/lib/vboxwebService-5.2.wsdl

This will stop the version complaints. It should work immediately; no need to restart anything.

Set up and use VMs

Use the VirtualBox web interface to set up a virtual machine like you would with the GUI version.

Once you start it you can connect to it using an RDP session to your server’s host name or IP address on the port defined in the VM’s settings, 9000 by default.

My Debian VirtualBox server has IP address 172.21.135.36. On my Windows 10 box I connect to my Kubuntu VM with:

?

1

mstsc /v:172.21.135.36:9000

Note that you are not connecting to the VM’s address. You are connecting to the server’s address.

Automatically start PhpVirtualBox on boot

Since we’re running VirtualBox with a dedicated user we need to tell VirtualBox web service to use that account: in /etc/systemd/system/multi-user.target.wants/vboxweb-service.service, under the [Service] part, add the following:

?

1

2

User=vbox

Group=vboxusers

The whole file should look like this:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

[Unit]

SourcePath=/usr/lib/virtualbox/vboxweb-service.sh

Description=

Before=runlevel2.target runlevel3.target runlevel4.target runlevel5.target shutdown.target

After=vboxdrv.service

Conflicts=shutdown.target

[Service]

User=vbox

Group=vboxusers

Type=forking

Restart=no

TimeoutSec=5min

IgnoreSIGPIPE=no

KillMode=process

GuessMainPID=no

RemainAfterExit=yes

ExecStart=/usr/lib/virtualbox/vboxweb-service.sh start

ExecStop=/usr/lib/virtualbox/vboxweb-service.sh stop

[Install]

WantedBy=multi-user.target

Reboot the server and verify PhpVirtualBox is started afterwards or just do

?

1

# systemctl daemon-reload

for good measure.

Automatically pause VMs on reboot/shutdown and resume them on boot

I would like my VMs to be automatically paused (‘status=saved’ in VirtualBox’s verbage) when I reboot or shutdown my server. It would also be nice if the VMs in question were automatically resumed when the server is back up. We can do that within Systemd.

We’ll create two scripts: one to pause VMs and one to restart them. Then call them from a Systemd unit. Note that you need to save the pause and restart scripts in a location where the vbox user can reach and execute them. From the unit file we’ll need the vbox user to execute them.

Which VMs do we pause? The ones that are running and the ones that are paused because paused VMs’ states wouldn’t survive a reboot. Which VMs do we resume? Just the ones that were running, not the ones that were paused, because we want the new overall state to resemble the old one as closely as possible (i.e. we do not want paused VMs to be suddenly running, so it’s better to keep them saved than running).

In /home/vbox/pausevms.sh:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

#!/bin/bash

# Pause running VMs

STATUSFILE=/home/vbox/vm-status

# Clear the previous status file if it exists

if [ -e $STATUSFILE ]; then rm $STATUSFILE; fi

# List all VMs

for VM in $(vboxmanage list vms | rev | cut -d' ' -f1 | rev)

do

# Get VM state

STATE=$(vboxmanage showvminfo $VM --machinereadable | grep "VMState=" | cut -d'=' -f2)

# Pause if state is running or paused, write to status file

if [[ $STATE == \"running\" || $STATE == \"paused\" ]]; then

vboxmanage controlvm $VM savestate

# No need to restart paused vm's; just let them remain saved.

if [[ $STATE == \"running\" ]]; then echo "$VM">>$STATUSFILE; fi

fi

done

In /home/vbox/resumevms.sh:

?

1

2

3

4

5

6

7

8

9

10

11

#!/bin/bash

# Restart VMs saved earlier

STATUSFILE=/home/vbox/vm-status

# If no status file exists then apparently there are no VMs to resume.

if [ ! -f $STATUSFILE ]; then exit; fi

while read VM; do

vboxmanage startvm $VM --type headless

done <$STATUSFILE

Make the scripts accessible to the vbox user and executable:

?

1

2

3

# cd /home/vbox

# chown vbox pausevms.sh resumevms.sh

# chmod +x pausevms.sh resumevms.sh

Create a unit file /etc/systemd/system/virtualmachines.service:

?

1

2

3

4

5

6

7

8

9

10

11

12

[Unit]

Description=Save VirtualBox VMs before shutdown or reboot; resume them after boot.

Wants=multi-user.target

After=multi-user.target

[Service]

Type=oneshot

RemainAfterExit=yes

ExecStart=/usr/bin/sudo -u vbox /home/vbox/resumevms.sh

ExecStop=/usr/bin/sudo -u vbox /home/vbox/pausevms.sh

[Install]

WantedBy=multi-user.target

Enable the new daemon:

?

1

2

# systemctl daemon-reload

# systemctl enable virtualmachines.service

…But be aware that if you run it now it will put any running VMs in the saved status. So I recommend you stop/save any running VMs at this point, reboot your server, start one or more VMs, reboot and then check if they came back up. Check /var/log/syslog for troubleshooting hints if it doesn’t work.

The lines containing ‘multi-user.target’ make the script wait after boot until the multi-user.target is reached, which among other things contains the VirtualBox web service. It also makes the script wait on reboot/shutdown until all necessary VMs are saved before shutting down VirtualBox itself and rebooting/shutting down.

I tested this whole contraption on a virtual machine containing virtual machines. On one test run PhpVirtualBox wouldn’t start (but the VMs would) but upon reboot it did. Most of the time it would just work smoothly. Taking into consideration the VM-in-a-VM setup I am quite satisfied with this system.

Set up a backup system

This backup script is taken and adapted from this page (opens in a new tab), which also contains commentary and explanations.

I’ve put this in /home/vbox/backupvms.sh.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

#!/bin/bash

# Loop through all VirtualBox VMs, pause, export,

# and restore them to their original states.

#

# Vorkbaard, 2012-02-01, 2018-02-09

# =============== Set your variables here ===============

EXPORTDIR=/var/vmbackup

LOGFILE=/home/vbox/export.log

MYMAIL=root

VBOXMANAGE="/usr/bin/VBoxManage -q"

# =======================================================

# Generate a list of all VMs

for VM in $(vboxmanage list vms | rev | cut -d' ' -f1 | rev)

do

ERR="nothing"

SECONDS=0

# Get VM's friendly name

FRIENDLYNAME=$(vboxmanage showvminfo --machinereadable $VM | grep "name=" | cut -d'"' -f2)

# Delete old $LOGFILE file if it exists

if [ -e $LOGFILE ]; then rm $LOGFILE; fi

# Get the vm state

VMSTATE=$(vboxmanage showvminfo $VM --machinereadable | grep "VMState=" | cut -f 2 -d "=")

echo "$VM's state is: $VMSTATE."

# If the VM's state is running or paused, save its state

if [[ $VMSTATE == \"running\" || $VMSTATE == \"paused\" ]]; then

vboxmanage controlvm $VM savestate

if [ $? -ne 0 ]; then ERR="saving the state"; fi

fi

# Export the vm as appliance

if [ "$ERR" == "nothing" ]; then

vboxmanage export $VM --output $EXPORTDIR/$VM-new.ova &> $LOGFILE

if [ $? -ne 0 ]; then

ERR="exporting"

else

# Remove old backup and rename new one

if [ -e $EXPORTDIR/$VM.ova ]; then rm $EXPORTDIR/$VM.ova; fi

mv $EXPORTDIR/$VM-new.ova $EXPORTDIR/$VM.ova

# Get file size

FILESIZE=$(du -h $EXPORTDIR/$VM.ova | cut -f 1)

fi

else

echo "Not exporting because the VM's state couldn't be saved." &> $LOGFILE

fi

# Resume the VM to its previous state if that state was paused or running

if [[ $VMSTATE == \"running\" || $VMSTATE == \"paused\" ]]; then

vboxmanage startvm $VM --type headless

if [ $? -ne 0 ]; then ERR="resuming"; fi

if [ $VMSTATE == \"paused\" ]; then

vboxmanage controlvm $VM pause

if [ $? -ne 0 ]; then ERR="pausing"; fi

fi

fi

# Calculate duration

duration=$SECONDS

duration="Operation took $(($duration / 60)) minutes, $(($duration % 60)) seconds."

# Notify the admin

if [ "$ERR" == "nothing" ]; then

MAILBODY="Virtual Machine $FRIENDLYNAME was exported succesfully!"

MAILBODY="$MAILBODY"$'\n'"$duration"

MAILBODY="$MAILBODY"$'\n'"Export filesize: $FILESIZE"

MAILSUBJECT="VM $VM succesfully backed up"

else

MAILBODY="There was an error $ERR VM $VM."

if [ "$ERR" == "exporting" ]; then

MAILBODY=$(echo $MAILBODY && cat $LOGFILE)

fi

MAILSUBJECT="Error exporting VM $VM"

fi

# Send the mail

echo "$MAILBODY" | mail -s "$MAILSUBJECT" $MYMAIL

# Clean up

if [ -e $LOGFILE ]; then rm $LOGFILE; fi

done

Chown the script to the vbox user and make it executable:

?

1

2

# chown vbox /home/vbox/backupvms.sh

# chmod +x /home/vbox/backupvms.sh

Then schedule it using cron or just run it whenever you like.

If you want the mail part to work configure mail or mailx for your system. Otherwise it’ll just send mail to local addresses. You can also use ssmtp if you don’t want to set up EXIM or Postfix; ssmtp is easy to set up and is used mainly for systems that just need to be able to send the occasional outgoing mail.

That’s it!

If you have any questions not related to PhpVirtualBox or VirtualBox itself (they are not my projects) feel free to ask them in the comment section below.

If you found this article helpful please click some of the ads on my page to help me pay for hosting this site, or consider donating any amount using the PayPal donation button on the top right of this page. Thanks :)

Troubleshooting

Different versions of VirtualBox and the extension pack may lead to unexpected behavious and weird errors.

Mouse or keyboard not working in RDP session may be caused by lack of video memory (<128MB) in the VM.

Check /var/log/syslog for messages regarding VirtualBox and PhpVirtualBox.

Unable to install certain packages: perhaps new versions have been published. Stop copying and pasting commands from this site and start actually reading what your server is telling you.

Unable to unzip phpvirtualbox.zip: download from a different server. Try the German ones.

PhpVirtualBox’s default username and password are both ‘admin’.

Tech automatically, autostart, backup, phpvirtualbox, reboot, service, shutdown, systemd, unit, vbox, vboxweb, vboxwebsrv, virtualbox, web if, web interface, webif, webinterface 5 Comments

Recieve new post updates: Entries (RSS)
Recieve follow up comments updates: RSS 2.0

Written by Kapitein Vorkbaard

View all posts by: Kapitein Vorkbaard

« The Wandering Earth, door Cixin Liu

Dichronauts, door Greg Egan »

Leave a comment
5 Comments.

Alboy

2018-04-18

Thanks a lot my friend :)
This is a good tutorial.

Reply

FoRTu

2018-06-14

Thanks, great tutorial!

Reply

Diego

2018-07-29

Hi, and thanks for this tutorial. I have a question: Does the exported vm retains it’s (saved) state? I tried to restore one of the backups and the machine started offline, not in a saved state.

Cheers

Reply

Kapitein Vorkbaard

2018-08-13

I thought it would but its easy to test: just perform the export and restore manually.

Reply

Michael

2018-11-06

Great big thanks.

Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Comment

Name *

Email *

Website

Search for:

Donate

Did one of my articles help you? Help me pay for hosting this site! Any amount is greatly appreciated :)

Recent Posts

Surface Detail (Culture #9), door Iain M. Banks

Cold Welcome (Vatta’s Peace #1), door Elizabeth Moon

The Labyrinth Index (Laundry Files #9), door Charles Stross

A study in honor, door Claire O’Dell

Brief answers to the big questions, door Stephen Hawking

Noumenon, door Marina J. Lostetter

Ball lightning, door Cixin Liu

I only killed him once (Ray Electromatic Mysteries #3), door Adam Christopher

The first fifteen lives of Harry August, door Claire North

84K, door Claire North

Recent Comments

Jalisco Autuick on Installing a mailserver on Debian 8/9 – Part 11: Sources, config files, colouring and comments

abid ali on Installing a mailserver on Debian 8/9 – Part 11: Sources, config files, colouring and comments

jakeroot on How to set up OpenVPN with Google Authenticator on pfSense

Chris on Installing a mailserver on Debian 8/9 – Part 11: Sources, config files, colouring and comments

jhon doe on Getting Shared Folders working properly in Debian 9 in VirtualBox

Tags

actie active directory ad AI aliens backup clamav complete connect culture debian detective Dovecot fail2ban fantasy klassieke scifi kunstmatige intelligentie linux maatschappijkritiek mail mcdevitt military scifi mysql openvpn owncloud pfsense politiek Postfix posthumanisme robots Roundcube Scifi server space opera Spamassassin stretch technothriller Thunderbird transhumanisme twitter ubuntu virtualbox vpn wheezy windows

Categories

Scifi

Taal

Tech

Twitter

Uncategorized

Archives

December 2018

November 2018

October 2018

September 2018

August 2018

July 2018

June 2018

May 2018

March 2018

February 2018

January 2018

December 2017

November 2017

October 2017

September 2017

August 2017

July 2017

June 2017

May 2017

April 2017

March 2017

February 2017

January 2017

December 2016

November 2016

October 2016

September 2016

August 2016

July 2016

June 2016

May 2016

April 2016

March 2016

February 2016

January 2016

December 2015

November 2015

October 2015

September 2015

August 2015

July 2015

June 2015

May 2015

April 2015

March 2015

February 2015

January 2015

December 2014

October 2014

August 2014

April 2014

February 2014

January 2014

November 2013

September 2013

August 2013

July 2013

June 2013

May 2013

June 2012

May 2012

Meta

Log in

Entries RSS

Comments RSS

WordPress.org

Ahoy, maatjes!

Rent-a-beard

Scifi

© 2019 Vorkbaard uit de toekomst. All Rights Reserved.
Theme by Bas Weinans. Powered by WordPress.