5

I am using Virtualbox host-only network, and I have interface vboxnet0.

On host (Ubuntu 14), I have some route staff, several route tables (main and two extra), ip rules set, so on.

Every changes I made on routing tables for vbonxen0 interface on host lost reboot.

How can make ifup-like script for vboxnet0 interface?

4

There are some things to keep in mind when using VirtualBox's host-only networking mode (at least V4.3.36) especially when bridging is no option and manual, custom routes are necessary to be configured afterwards:

  • vboxnet0 device node is not automatically available after boot (!)
    Except when you configure it manually in /etc/network/interfaces which may interfere when virtualbox tries to bring up it's own instance.
  • It's not even available when vbox(pci|netadp|netflt|drv) modules are loaded.
  • Using vboxmanage hostonlyif create will just add the device node but leave it in state DOWN which isn't routable yet.
  • vboxnet0 device state will remain unknown even when the interface is brought up:
    4: vboxnet0: <BROADCAST,MULTICAST,UP,LOWER_UP> ... state UNKNOWN ...
    Any attempt to use up events from /etc/network/interfaces, ifup or /etc/network/if-up.d/MYSCRIPT is useless as that state is never reached.
  • On the host it becomes only available and ready to use when your first VM guest machine starts.

The only way to get between virtualbox and the start of your VM i've found so far is using a udev rule by placing a file eg: /etc/udev/rules.d/80-vboxnet with:

KERNEL=="vboxnet0", SUBSYSTEM=="net", ACTION=="add", RUN+="/bin/bash -c '/bin/sleep 1; /sbin/ip route add x.x.x.x dev vboxnet0'"

which triggers a bash instance. I've put the ip route ... statement directly into the rule file but alternatively it's possible to just bring up a script elsewhere and put additional route configurations there.

1

3ronco's Answer provides some good insight into how VirtualBox screws us over with this, but I found his Udev solution didn't work on my Ubuntu 19.04, as the link was still down at the time the script runs.

I tried adding the interface in netplan with static routes configured there, however, for reasons unknown the routes were ignored.

If your system has networkd-dispatcher you can put a script here

/etc/networkd-dispatcher/routable.d/99-vboxnetworks

Here's mine:

#!/bin/bash

if [ $IFACE == vboxnet2 ]; then
   /sbin/ip route add fdnn:nnnn:nnnn:2::/64 via fdnn:nnnn:nnnn:1::2
   /sbin/ip route add fdnn:nnnn:nnnn:3::/64 via fdnn:nnnn:nnnn:1::3
fi

Now it turns out this script won't be run unless the interface has an entry in netplan. Mine includes one of the ignored routing entries, just to make it syntactically correct (there's probably a tidier way to do this). So I have

/etc/netplan/01-mynet.yaml

Containing:

network:
    ethernets:
            vboxnet2:
                    routes:
                            # These routes are ignored for reasons unknow,
                            # I have included them just so that vboxnet2 gets
                            # a mention here. Actual routes are added by
                            # /etc/networkd-dispatcher/routable.d/99-vboxnetworks
                            #
                            - to: fdnn:nnnn:nnnn:2::/64
                              via: fdnn:nnnn:nnnn:1::2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.