Debian Jessie: restart VirtualBox’s network after wake-up

Systemd is used for services control in Debian Jessie, and, there are some problems with some of the services.
One of thoose problems is: VirtualBox doesn’t restart it’s virtual network adapters after system’s wake-up (from suspension).

When does it happens:

  1. When you are using VirtualBox version 4.3.8 and lower (didn’t tested this on higher versions)
  2. When there are a host-only adapter configured in VirtualBox
  3. You suspended your computer (for example, by closing your laptop), and than woke it up

What are the signs:

  1. Virtual machine doesn’t respond to ping
  2. It’s also impossible to connect to a VM via ssh
  3. If one of VM’s folders mounted by sshfs to a real machine, all porcesses, that used this folder before suspension are freezed badly

What to do?

IP-address of a virtual adapter

To restart adapter manually, you can use command like this: (192.168.100.1 – is my host-only adapter’s address, configured in virtualbox)
[bash] # ifconfig vboxnet0 192.168.100.1 up
[/bash] and I have used it for a while, until one this situation made me mad, and I’ve decided to atomate the process.

For automation of the process, let’s create new systemd service, which would run when “resume” event is happened (the computer is resumed from suspension)

Create bash script, what should restore network

Put it into /opt/bin/resume-vboxnet0.sh. Contents are simple:

[bash] #!/bin/bash
ifconfig vboxnet0 192.168.100.1 up
[/bash]

Create new systemd service:

New systemd service is represented by a file, which contains:
[bash] [Unit] # Service name
Description=Reconfigure vboxnet0 after resume
# When should service start
After=suspend.target
After=hibernate.target
After=hybrid-sleep.target

[Service] # Service type = single launch (i.e. it’s not a daemon,
# what starts an stay on the background,
# it’s just a simple bash script that is runned once)
Type=oneshot
# run script resume-vboxnet.sh
ExecStart=/opt/bin/resume-vboxnet.sh

[Install] # What services are using this service:
WantedBy=suspend.target
WantedBy=hibernate.target
WantedBy=hybrid-sleep.target
[/bash]

This file should be placed in the /lib/systemd/sys/ folder, under some memorable name, for example, resume-vboxnet.service.
There are all installed systemd services placed in this folder.

[bash] # nano /lib/systemd/sys/resume-vboxnet.service
[/bash]

Let’s install newly created service, so that systemd load and run it:

[bash] # systemctl reenable resume-vboxnet.service
[/bash]

Let’s try how it works

Now, try to close laptop’s lid, so that it go to “sleep” (or use some other method to “sleep” your computer), after that wake it up, and lets see, what state is your vboxnet0 network device in.


So, what do you think ?