HowTo Start VBox Net Interfaces At Boot & Attach Servers

Discussions related to using VirtualBox on Mac OS X hosts.
Post Reply
tErik
Posts: 3
Joined: 23. Jul 2015, 07:59

HowTo Start VBox Net Interfaces At Boot & Attach Servers

Post by tErik »

How To Start VBox Network Interfaces At/During Boot, and Attach Dependent Servers/Services/Daemons etc.

When VirtualBox for Mac OS X is installed (on Mac OS X host computer), it will automatically also load below daemon service in MacOSX system:

/Library/LaunchDaemons/org.virtualbox.startup.plist

Above daemon service initiator executes below bash script .sh file, to unload+load vbox kexts/drivers:

"/Library/Application Support/VirtualBox/LaunchDaemons/VirtualBoxStartup.sh" restart

Default script was not able to load host-only network interface(s) during boot, in two Apple computers where i have tried+tested.

So i had to modify default script file, into below "VirtualBoxStartup.sh" script file, with help of other online users, to load vbox related kexts/drivers at boot time, in MacOSX host computer, and, also to start/load vboxnet* host-only network interfaces/adapters during boot time, and, also to attach various other (including 3rd party) services/daemons with vbox net interface which depend on existence of active vbox net interface(s).

1st make backup copy of your original "VirtualBoxStartup.sh" file, then copy below script source-codes from shown "VirtualBoxStartup.sh" file section.
I prefer to use "BlueFish" text editor for editing such text or source-code files. Get it from http://sourceforge.net/projects/bluefish/
Install it. Open the "/Library/Application Support/VirtualBox/LaunchDaemons/VirtualBoxStartup.sh" file in BlueFish, and delete all previous code lines, and paste entire content of below shown "VirtualBoxStartup.sh" file, into your own "VirtualBoxStartup.sh" file which is opened inside your BlueFish text-editor.

Before saving in BlueFish, use below "chown" command in Terminal 1st, to change script file's OWNERSHIP, so that you can save your changes:

sudo chown Your-user-id-in-Mac "/Library/Application Support/VirtualBox/LaunchDaemons/VirtualBoxStartup.sh"

after saving your changes into the script file, you will have to change file's ownership back to mac osx system, so that system can run/execute it at/during boot time, by running below command in Terminal:

sudo chown root:wheel "/Library/Application Support/VirtualBox/LaunchDaemons/VirtualBoxStartup.sh"

VirtualBoxStartup.sh file:

Code: Select all

#!/bin/sh
# $Id: VirtualBoxStartup.sh 90723 2013-11-18 20:25:07Z fmehnert $
## @file
# Startup service for loading the kernel extensions and select the set of VBox
# binaries that matches the kernel architecture.
#

#
# Copyright (C) 2007-2013 Oracle Corporation
#
# This file is part of VirtualBox Open Source Edition (OSE), as
# available from http://www.virtualbox.org. This file is free software;
# you can redistribute it and/or modify it under the terms of the GNU
# General Public License (GPL) as published by the Free Software
# Foundation, in version 2 as it comes in the "COPYING" file of the
# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
#

#
# Copyright (C) 2015 tErik (at3erik at outlook dot com) (remov prev 3).
# tErik releasing this modified script code, under same License
# as original script file, which is, GPL v2 License.
#
# Code is released with below License agreement as well.
# 

# The MIT License (MIT)
# Copyright (C) 2015 tErik, (at3erik at outlook dot com) (remov prev 3).
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#


VBoxRestart=0
VBoxNIF=0
VBox3rdPrtySvc=0

if false; then
    . /etc/rc.common
else
    # Fake the startup item functions we're using.

    ConsoleMessage()
    {
        if [ "$1" != "-f" ] ; then
            printf "$@\n"
        else
            shift
            printf "Fatal error: $@\n"
            exit 1;
        fi
    }

    MsgWithoutEndNL()
    {
        printf "$@"
    }

    RunService()
    {
        case "$1" in
            "status")
                ShowStatus
                exit $?;
                ;;
            "start")
                StartService
                exit $?;
                ;;
            "start_with_nifs")
                VBoxNIF=1
                StartService
                exit $?;
                ;;
            "start_with_nifs_and_3rdPrtySvc")
                VBoxNIF=1
                VBox3rdPrtySvc=1
                StartService
                exit $?;
                ;;
            "stop")
                StopService
                exit $?;
                ;;
            "stop_also_nifs")
                VBoxNIF=1
                StopService
                exit $?;
                ;;
            "stop_also_nifs_and_3rdPrtySvc")
                VBoxNIF=1
                VBox3rdPrtySvc=1
                StopService
                exit $?;
                ;;
            "restart")
                VBoxRestart=1
                RestartService
                exit $?;
                ;;
            "restart_with_nifs")
                VBoxNIF=1
                VBoxRestart=1
                RestartService
                exit $?;
                ;;
            "restart_with_nifs_and_3rdPrtySvc")
                VBoxNIF=1
                VBox3rdPrtySvc=1
                VBoxRestart=1
                RestartService
                exit $?;
                ;;
            "launchd")
                VBoxRestart=1
                if RestartService; then
                    while true;
                    do
                        sleep 3600
                    done
                fi
                exit $?;
                ;;
            "launchd_with_nifs")
                VBoxNIF=1
                VBoxRestart=1
                if RestartService; then
                    while true;
                    do
                        sleep 3600
                    done
                fi
                exit $?;
                ;;
            "launchd_with_nifs_and_3rdPrtySvc")
                VBoxNIF=1
                VBox3rdPrtySvc=1
                VBoxRestart=1
                if RestartService; then
                    while true;
                    do
                        sleep 3600
                    done
                fi
                exit $?;
                ;;
             **)
                printf "Error: Unknown action '$1'\n"
                printf " Supported parameters are:\n"
                printf "   start, start_with_nifs, start_with_nifs_and_3rdPrtySvc,\n"
                printf "   stop, stop_also_nifs, stop_also_nifs_and_3rdPrtySvc,\n"
                printf "   restart, restart_with_nifs, restart_with_nifs_and_3rdPrtySvc,\n"
                printf "   launchd, launchd_with_nifs, launchd_with_nifs_and_3rdPrtySvc\n"
                printf "   status\n"
                exit 1;
        esac
    }
fi


StartService()
{
    VBOX_RC=0
    VBoxNInm=0
    VBoxNIip=0
    VBOXDRV="VBoxDrv"
    VBOXUSB="VBoxUSB"
    #VBoxHInm=""
    VBoxStr=""
    VBoxCmdOut=""
    VBoxChkDhcp=0
    VBoxLoc="/Applications/VirtualBox.app/Contents/MacOS"
    VBoxFindThis=""

    MsgWithoutEndNL "Loading VirtualBox (vbox) kexts/drivers"; if [ "$VBoxNIF" -eq 1 ] ; then MsgWithoutEndNL " & net interfaces"; fi; ConsoleMessage "..."

    #
    # Switch the binaries to the right architecture.
    #
    VBOX_ARCH=`uname -m`
    if test "$VBOX_ARCH" = "x86_64"; then
        VBOX_ARCH="amd64"
    else
        VBOX_ARCH="x86"
    fi
    for VBOX_TRG in `ls /Applications/VirtualBox.app/Contents/MacOS/*-${VBOX_ARCH}`;
    do
        VBOX_LINKNAME=`echo "$VBOX_TRG" | sed -e 's|-'"${VBOX_ARCH}"'$||' `
        if test "$VBOX_LINKNAME" != "$VBOX_TRG"; then
            rm -f "$VBOX_LINKNAME"
            if ! /bin/ln -vh "$VBOX_TRG" "$VBOX_LINKNAME"; then
                if [ "$VBoxRestart" -eq 0 ] ; then
                    ConsoleMessage " Error: /bin/ln -vh $VBOX_TRG $VBOX_LINKNAME failed"
                    VBOX_RC=1
                else
                    ConsoleMessage " /bin/ln -vh $VBOX_TRG $VBOX_LINKNAME failed"
                fi
            fi
        else
            if [ "$VBoxRestart" -eq 0 ] ; then
                ConsoleMessage " Error: Script error VBOX_TRG=$VBOX_TRG"
                VBOX_RC=1
            else
                ConsoleMessage " Script error VBOX_TRG=$VBOX_TRG"
            fi
        fi
    done

    #
    # Check that all the directories exist first.
    #
    if [ ! -d "/Library/Application Support/VirtualBox/${VBOXDRV}.kext" ] ; then
        ConsoleMessage " Error: /Library/Application Support/VirtualBox/${VBOXDRV}.kext is missing"
        VBOX_RC=1
    fi
    if [ ! -d "/Library/Application Support/VirtualBox/${VBOXUSB}.kext" ] ; then
        ConsoleMessage " Error: /Library/Application Support/VirtualBox/${VBOXUSB}.kext is missing"
        VBOX_RC=1
    fi
    if [ ! -d "/Library/Application Support/VirtualBox/VBoxNetFlt.kext" ] ; then
        ConsoleMessage " Error: /Library/Application Support/VirtualBox/VBoxNetFlt.kext is missing"
        VBOX_RC=1
    fi
    if [ ! -d "/Library/Application Support/VirtualBox/VBoxNetAdp.kext" ] ; then
        ConsoleMessage " Error: /Library/Application Support/VirtualBox/VBoxNetAdp.kext is missing"
        VBOX_RC=1
    fi

    #
    # Check that no drivers are currently running.
    # (Try stop the service if this is the case.)
    #
    if [ "$VBOX_RC" -eq 0 ] ; then
        if kextstat -lb org.virtualbox.kext.VBoxDrv 2>&1 | grep -q org.virtualbox.kext.VBoxDrv; then
            if [ "$VBoxRestart" -eq 0 ] ; then
                ConsoleMessage " Error: ${VBOXDRV}.kext is already loaded"
                VBOX_RC=1
            else
                ConsoleMessage " ${VBOXDRV}.kext is already loaded"
            fi
        fi
        if kextstat -lb org.virtualbox.kext.VBoxUSB 2>&1 | grep -q org.virtualbox.kext.VBoxUSB; then
            if [ "$VBoxRestart" -eq 0 ] ; then
                ConsoleMessage " Error: ${VBOXUSB}.kext is already loaded"
                VBOX_RC=1
            else
                ConsoleMessage " ${VBOXUSB}.kext is already loaded"
            fi
        fi
        if kextstat -lb org.virtualbox.kext.VBoxNetFlt 2>&1 | grep -q org.virtualbox.kext.VBoxNetFlt; then
            if [ "$VBoxRestart" -eq 0 ] ; then
                ConsoleMessage " Error: VBoxNetFlt.kext is already loaded"
                VBOX_RC=1
            else
                ConsoleMessage " VBoxNetFlt.kext is already loaded"
            fi
        fi
        if kextstat -lb org.virtualbox.kext.VBoxNetAdp 2>&1 | grep -q org.virtualbox.kext.VBoxNetAdp; then
            if [ "$VBoxRestart" -eq 0 ] ; then
                ConsoleMessage " Error: VBoxNetAdp.kext is already loaded"
                VBOX_RC=1
            else
                ConsoleMessage " VBoxNetAdp.kext is already loaded"
            fi
        fi
    fi

    #
    # Load the drivers.
    #
    if [ "$VBOX_RC" -eq 0 ] ; then
        ConsoleMessage " Loading ${VBOXDRV}.kext..."
        if ! kextload "/Library/Application Support/VirtualBox/${VBOXDRV}.kext"; then
            if [ "$VBoxRestart" -eq 0 ] ; then
                ConsoleMessage "  Error: Failed to load /Library/Application Support/VirtualBox/${VBOXDRV}.kext"
                VBOX_RC=1
            else
                ConsoleMessage "  ${VBOXDRV}.kext is already loaded"
            fi
        else
            ConsoleMessage "  ${VBOXDRV}.kext is now loaded."
        fi

        ConsoleMessage " Loading ${VBOXUSB}.kext..."
        if ! kextload -d "/Library/Application Support/VirtualBox/${VBOXDRV}.kext" "/Library/Application Support/VirtualBox/${VBOXUSB}.kext"; then
            if [ "$VBoxRestart" -eq 0 ] ; then
                ConsoleMessage "  Error: Failed to load /Library/Application Support/VirtualBox/${VBOXUSB}.kext"
                VBOX_RC=1
            else
                ConsoleMessage "  ${VBOXUSB}.kext is already loaded"
            fi
        else
            ConsoleMessage "  ${VBOXUSB}.kext is now loaded."
        fi

        ConsoleMessage " Loading VBoxNetFlt.kext..."
        if ! kextload -d "/Library/Application Support/VirtualBox/${VBOXDRV}.kext" "/Library/Application Support/VirtualBox/VBoxNetFlt.kext"; then
            if [ "$VBoxRestart" -eq 0 ] ; then
                ConsoleMessage "  Error: Failed to load /Library/Application Support/VirtualBox/VBoxNetFlt.kext"
                VBOX_RC=1
            else
                ConsoleMessage "  VBoxNetFlt.kext is already loaded"
            fi
        else
            ConsoleMessage "  VBoxNetFlt.kext is now loaded."
        fi

        ConsoleMessage " Loading VBoxNetAdp.kext..."
        if ! kextload -d "/Library/Application Support/VirtualBox/${VBOXDRV}.kext" "/Library/Application Support/VirtualBox/VBoxNetAdp.kext"; then
            if [ "$VBoxRestart" -eq 0 ] ; then
                ConsoleMessage "  Error: Failed to load /Library/Application Support/VirtualBox/VBoxNetAdp.kext"
                VBOX_RC=1
            else
                ConsoleMessage "  VBoxNetAdp.kext is already loaded"
            fi
        else
            ConsoleMessage "  VBoxNetAdp.kext is now loaded."
        fi

        if [ "$VBOX_RC" -ne 0 ] ; then
            # unload the drivers (ignoring failures)
            ConsoleMessage " Ignoring failures & unloading all vbox kexts/drivers..."
            kextunload -b org.virtualbox.kext.VBoxNetAdp
            kextunload -b org.virtualbox.kext.VBoxNetFlt
            kextunload -b org.virtualbox.kext.VBoxUSB
            kextunload -b org.virtualbox.kext.VBoxDrv
            ConsoleMessage "  done (unloaded kexts)."
            if [ "$VBoxRestart" -ge 1 ] ; then
                VBOX_RC=0
            fi
        fi
    fi

    #
    # Set the error on failure.
    #
    if [ "$VBOX_RC" -ne 0 ] ; then
        if [ "$VBoxRestart" -eq 0 ] ; then
            MsgWithoutEndNL " Run this script again to reload all vbox kexts"; if [ "$VBoxNIF" -eq 1 ] ; then MsgWithoutEndNL " (and vboxnet* interfaces)"; fi; ConsoleMessage "."
            ConsoleMessage -f VirtualBox
            VBoxNInm=0
            VBoxNIip=0
            #VBoxHInm=""
            VBoxStr=""
            VBoxCmdOut=""
            VBoxChkDhcp=0
            VBoxLoc=""
            VBoxFindThis=""
            exit $VBOX_RC
        fi
    fi

    #
    # Enable vboxnet* network interface, (after VBox kexts are loaded successfully),
    # and, when vboxnet* is/are also pre-defined inside virtualbox.
    # Codes added by tErik (at2erik at outlook dot com) (remove previous 2).
    # Also thanks to other code contributors @irc.freenode.net #bash
    #   contributors: izabera, geirha (gina.it.ntnu.no), mattcen,
    #                 osse, Riviera, kurahaupo_
    # 
    if [ "$VBOX_RC" -eq 0 ] && [ "$VBoxNIF" -eq 1 ] ; then
        VBoxNInm=-1
        VBoxNIip=55
        # for working with vboxnet0 (192.168.56.x) to vboxnet3 (192.168.59.x)
        while [ "$VBoxNInm" -lt 3 ] ; do
            let "VBoxNInm+=1"
            let "VBoxNIip+=1"
            VBoxChkDhcp=0
            ConsoleMessage " Checking if vboxnet$VBoxNInm network interface is pre-defined in VBox..."
            VBoxStr="^Name[:][[:space:]]*vboxnet${VBoxNInm}$"
            if ${VBoxLoc}/VBoxManage list hostonlyifs | grep -aiEoq "$VBoxStr"; then
                ConsoleMessage "  vboxnet$VBoxNInm is found pre-defined in vbox."
                ConsoleMessage "  modifying vboxnet${VBoxNInm}, and changing to 192.168.${VBoxNIip}.1 ip-addresss..."
                ${VBoxLoc}/VBoxManage hostonlyif ipconfig vboxnet$VBoxNInm --ip 192.168.${VBoxNIip}.1 --netmask 255.255.255.0
                VBoxChkDhcp=1
            else
                ConsoleMessage "  vboxnet$VBoxNInm is not pre-defined in vbox."
                ConsoleMessage "  so creating a new interface in vbox..."
                ${VBoxLoc}/VBoxManage hostonlyif create
                ConsoleMessage "  Renaming new interface into vboxnet${VBoxNInm}, and adding (192.168.${VBoxNIip}.1) IP-addresss..."
                ${VBoxLoc}/VBoxManage hostonlyif ipconfig vboxnet$VBoxNInm --ip 192.168.${VBoxNIip}.1 --netmask 255.255.255.0
                VBoxChkDhcp=1
            fi
            # Checking for vboxnet* DHCP presence, and if necessary, Adding & Activating DHCP:
            if [ "$VBoxChkDhcp" -eq 1 ] ; then
                VBoxStr="^IP[:][[:space:]]*192\.168\.${VBoxNIip}\.1$"
                if ${VBoxLoc}/VBoxManage list dhcpservers | grep -aiEoq "$VBoxStr"; then
                    ConsoleMessage "   DHCP (192.168.${VBoxNIip}.1) for vboxnet$VBoxNInm is pre-defined in vbox."
                else
                    ConsoleMessage "   DHCP (192.168.${VBoxNIip}.1) for vboxnet$VBoxNInm is not pre-defined in vbox,\r\n   so Adding DHCP server with vboxnet${VBoxNInm}..."
                    ${VBoxLoc}/VBoxManage dhcpserver modify --ifname vboxnet$VBoxNInm --ip 192.168.${VBoxNIip}.1 --netmask 255.255.255.0 --lowerip 192.168.${VBoxNIip}.100 --upperip 192.168.${VBoxNIip}.199 --enable
                    ConsoleMessage "    done (added dhcp ip-adrs)."
                fi
                ConsoleMessage "   checking if DHCP (for vboxnet${VBoxNInm}) is active or not..."
                VBoxStr="IP[:][[:space:]]192\.168\.${VBoxNIip}\.1[[:space:]].*?Enabled[:][[:space:]](No|Yes)"
                VBoxCmdOut=$(${VBoxLoc}/VBoxManage list dhcpservers | tr -s '\r\n\t\ ' " " | grep -aiEo "$VBoxStr")
                VBoxFindThis=".*Yes$"
                if [[ $VBoxCmdOut =~ $VBoxFindThis ]] ; then
                    ConsoleMessage "    DHCP server for vboxnet$VBoxNInm is already enabled/activated."
                else
                    ConsoleMessage "    DHCP vboxnet$VBoxNInm is not enabled/activated, so enabling..."
                    ${VBoxLoc}/VBoxManage dhcpserver modify --ifname vboxnet$VBoxNInm --ip 192.168.${VBoxNIip}.1 --netmask 255.255.255.0 --lowerip 192.168.${VBoxNIip}.100 --upperip 192.168.${VBoxNIip}.199 --enable
                    VBoxCmdOut=$(${VBoxLoc}/VBoxManage list dhcpservers | tr -s '\r\n\t\ ' " " | grep -aiEo "$VBoxStr")
                    if [[ $VBoxCmdOut =~ $VBoxFindThis ]] ; then
                        ConsoleMessage "     DHCP server for vboxnet$VBoxNInm is now enabled/activated."
                    else
                        ConsoleMessage "     DHCP server for vboxnet$VBoxNInm could not be enabled/activated!"
                        # you may add code here for forcefully enable DHCP for vbox net interface.
                    fi
                fi
            fi
            # Check presence of vbox net interface in system, and, Activate/Enable if necessary:
            ConsoleMessage "   Checking presence of vboxnet$VBoxNInm net interface in system..."
            VBoxStr="^vboxnet${VBoxNInm}[:][[:space:]]"
            if ! /sbin/ifconfig | grep -aiEoq "${VBoxStr}"; then
                ConsoleMessage "    vboxnet$VBoxNInm interface is not detected by this script!\r\n    and, not enabled/up for usage on this system!"
                # you may add code here for forcefully enable vbox net interface in vbox & in system.
            else
                ConsoleMessage "    vboxnet$VBoxNInm interface is detected+present in system.\r\n    checking, if its enabled/up for use or not..."
                VBoxStr="^vboxnet${VBoxNInm}[:][[:space:]]flags[\=][[:alnum:]]*[\<](UP[,\>]|[[:alnum:]]*[,]UP[,\>])"
                if /sbin/ifconfig | grep -aiEoq "$VBoxStr"; then
                    ConsoleMessage "     vboxnet$VBoxNInm interface is already enabled+Up for vbox & system."
                else
                    ConsoleMessage "     vboxnet$VBoxNInm interface is not yet enabled+Up for system, enabling..."
                    VBoxCmdOut=$(/sbin/ifconfig vboxnet$VBoxNInm up 2>&1 | expand | sed -e 's/^/      /')
                    if [[ "$VBoxCmdOut" != "" ]] ; then ConsoleMessage "$VBoxCmdOut"; VBoxCmdOut=""; fi
                    if /sbin/ifconfig | grep -aiEoq "$VBoxStr"; then
                        ConsoleMessage "      vboxnet$VBoxNInm interface is now enabled/up for usage on this system."
                    else
                        ConsoleMessage "      vboxnet$VBoxNInm interface exists but could not be enabled/up for this system!"
                        # you may add code here for forcefully enable vbox net interface in vbox & in system.
                    fi
                fi
            fi
        done
    fi
    # END OF vboxnet* interface Loading section

    VBoxNInm=0
    VBoxNIip=0
    #VBoxHInm=""
    VBoxStr=""
    VBoxCmdOut=""
    VBoxChkDhcp=0
    VBoxLoc=""
    VBoxFindThis=""

    if [ "$VBOX_RC" -eq 0 ] && [ "$VBox3rdPrtySvc" -eq 1 ] ; then
        Start3rdPartyOrOtherServices
    fi

    VBoxRestart=0
    VBoxNIF=0
    VBox3rdPrtySvc=0
}


StopService()
{
    VBOX_RC=0
    VBoxNInm=0
    VBoxNIip=0
    VBOXDRV="VBoxDrv"
    VBOXUSB="VBoxUSB"
    #VBoxHInm=""
    VBoxStr=""
    VBoxCmdOut=""
    VBoxLoc="/Applications/VirtualBox.app/Contents/MacOS"
    VBoxFindThis=""

    if [ "$VBox3rdPrtySvc" -eq 1 ] ; then
        Stop3rdPartyOrOtherServices
    fi

    if [ "$VBoxRestart" -gt 0 ] ; then
        MsgWithoutEndNL "Attempt #${VBoxRestart}: Stop+Unload VirtualBox (vbox) kexts"; if [ "$VBoxNIF" -eq 1 ] ; then MsgWithoutEndNL " & network interfaces"; fi; ConsoleMessage "..."
    else
        MsgWithoutEndNL "Stopping/Disabling VirtualBox (vbox)"; if [ "$VBoxNIF" -eq 1 ] ; then MsgWithoutEndNL " net interfaces & vbox"; fi; ConsoleMessage " kexts..."
    fi

    if kextstat -lb org.virtualbox.kext.VBoxUSB 2>&1 | grep -q org.virtualbox.kext.VBoxUSB; then
        ConsoleMessage " Unloading ${VBOXUSB}.kext..."
        if ! kextunload -m org.virtualbox.kext.VBoxUSB; then
            ConsoleMessage "  Error: Failed to unload VBoxUSB.kext"
            VBOX_RC=1
            ConsoleMessage "  Ignoring failures & attempting to unload VBoxUSB.kext again..."
            kextunload -b org.virtualbox.kext.VBoxUSB
            ConsoleMessage "   done (unloaded kext)."
        else
            ConsoleMessage "  ${VBOXUSB}.kext is now unloaded."
        fi
    fi

    #
    # Disable vboxnet* network interfaces, because VBox kexts are about to be unloaded.
    # added by tErik (at2erik at outlook dot com) (remove previous 2).
    #
    if [ "$VBoxNIF" -eq 1 ] ; then
        VBoxNInm=-1
        VBoxNIip=55
        # for working with vboxnet0 (192.168.56.x) to vboxnet3 (192.168.59.x)
        while [ "$VBoxNInm" -lt 3 ] ; do
            let "VBoxNInm+=1"
            let "VBoxNIip+=1"
            ConsoleMessage " Checking if vboxnet$VBoxNInm net interface is enabled/active/up in this system..."
            VBoxStr="^vboxnet${VBoxNInm}[:][[:space:]]"
            if /sbin/ifconfig | grep -aiEoq "$VBoxStr"; then
                ConsoleMessage "  vboxnet$VBoxNInm is found in system.  Checking if it is up/down..."
                VBoxStr="^vboxnet${VBoxNInm}[:][[:space:]]flags[\=][[:alnum:]]*[\<](UP[,\>]|[[:alnum:]]*[,]UP[,\>])"
                if /sbin/ifconfig | grep -aiEoq "$VBoxStr"; then
                    ConsoleMessage "   vboxnet$VBoxNInm is active/up in system.\r\n   disabling it in system..."
                else
                    ConsoleMessage "   vboxnet$VBoxNInm is not active/up in system, (so it is disabled/down).\r\n   attempting to disable it anyway in system..."
                fi
                VBoxCmdOut=$(/sbin/ifconfig vboxnet$VBoxNInm down 2>&1 | expand | sed -e 's/^/    /')
                if [[ "$VBoxCmdOut" != "" ]] ; then ConsoleMessage "$VBoxCmdOut"; VBoxCmdOut=""; fi
                VBoxCmdOut=$(/sbin/ifconfig vboxnet$VBoxNInm inet 192.168.${VBoxNIip}.1 -alias 2>&1 | expand | sed -e 's/^/    /')
                if [[ "$VBoxCmdOut" != "" ]] ; then ConsoleMessage "$VBoxCmdOut"; VBoxCmdOut=""; fi
                if ! /sbin/ifconfig | grep -aiEoq "$VBoxStr"; then
                    ConsoleMessage "    vboxnet$VBoxNInm is now disabled/down in system."
                else
                    ConsoleMessage "    attempting to disable vboxnet$VBoxNInm (192.168.${VBoxNIip}.1) in system, again..."
                    VBoxCmdOut=$(/sbin/ifconfig vboxnet$VBoxNInm down 2>&1 | expand | sed -e 's/^/     /')
                    if [[ "$VBoxCmdOut" != "" ]] ; then ConsoleMessage "$VBoxCmdOut"; VBoxCmdOut=""; fi
                    VBoxCmdOut=$(/sbin/ifconfig vboxnet$VBoxNInm inet 192.168.${VBoxNIip}.1 -alias 2>&1 | expand | sed -e 's/^/     /')
                    if [[ "$VBoxCmdOut" != "" ]] ; then ConsoleMessage "$VBoxCmdOut"; VBoxCmdOut=""; fi
                    VBoxCmdOut=$(/sbin/ifconfig vboxnet$VBoxNInm destroy 2>&1 | expand | sed -e 's/^/     /')
                    if [[ "$VBoxCmdOut" != "" ]] ; then ConsoleMessage "$VBoxCmdOut"; VBoxCmdOut=""; fi
                    if ! /sbin/ifconfig | grep -aiEoq "$VBoxStr"; then
                        ConsoleMessage "     vboxnet$VBoxNInm is now disabled/down in system."
                    else
                        ConsoleMessage "     vboxnet$VBoxNInm could not be disabled in system!"
                    fi
                fi
            else
                ConsoleMessage "  vboxnet$VBoxNInm interface, not found in system."
            fi
        done

        VBoxNInm=3
        VBoxNIip=59
        # for working with vboxnet4 (192.168.60.x) to vboxnet15 (192.168.71.x)
        while [ "$VBoxNInm" -lt 15 ] ; do
            let "VBoxNInm+=1"
            let "VBoxNIip+=1"
            ConsoleMessage " Checking if vboxnet$VBoxNInm net interface already enabled/active in this system..."
            VBoxStr="^vboxnet${VBoxNInm}[:][[:space:]]"
            if /sbin/ifconfig | grep -aiEoq "$VBoxStr"; then
                ConsoleMessage "  vboxnet$VBoxNInm is found in system."
                VBoxStr="^vboxnet${VBoxNInm}[:][[:space:]]flags[\=][[:alnum:]]*[\<](UP[,\>]|[[:alnum:]]*[,]UP[,\>])"
                if /sbin/ifconfig | grep -aiEoq "$VBoxStr"; then
                    ConsoleMessage "   vboxnet$VBoxNInm is active/up in system.\r\n   disabling it in system..."
                else
                    ConsoleMessage "   vboxnet$VBoxNInm is not active/up in system, (so it is disabled/down).\r\n   attempting to disable it anyway in system..."
                fi
                VBoxCmdOut=$(/sbin/ifconfig vboxnet$VBoxNInm down 2>&1 | expand | sed -e 's/^/    /')
                if [[ "$VBoxCmdOut" != "" ]] ; then ConsoleMessage "$VBoxCmdOut"; VBoxCmdOut=""; fi
                VBoxCmdOut=$(/sbin/ifconfig vboxnet$VBoxNInm inet 192.168.${VBoxNIip}.1 -alias 2>&1 | expand | sed -e 's/^/    /')
                if [[ "$VBoxCmdOut" != "" ]] ; then ConsoleMessage "$VBoxCmdOut"; VBoxCmdOut=""; fi
                if ! /sbin/ifconfig | grep -aiEoq "$VBoxStr"; then
                    ConsoleMessage "    vboxnet$VBoxNInm is now disabled/down in system."
                else
                    ConsoleMessage "    attempting to disable vboxnet$VBoxNInm (192.168.${VBoxNIip}.1) in system, again..."
                    VBoxCmdOut=$(/sbin/ifconfig vboxnet$VBoxNInm down 2>&1 | expand | sed -e 's/^/     /')
                    if [[ "$VBoxCmdOut" != "" ]] ; then ConsoleMessage "$VBoxCmdOut"; VBoxCmdOut=""; fi
                    VBoxCmdOut=$(/sbin/ifconfig vboxnet$VBoxNInm inet 192.168.${VBoxNIip}.1 -alias 2>&1 | expand | sed -e 's/^/     /')
                    if [[ "$VBoxCmdOut" != "" ]] ; then ConsoleMessage "$VBoxCmdOut"; VBoxCmdOut=""; fi
                    VBoxCmdOut=$(/sbin/ifconfig vboxnet$VBoxNInm destroy 2>&1 | expand | sed -e 's/^/     /')
                    if [[ "$VBoxCmdOut" != "" ]] ; then ConsoleMessage "$VBoxCmdOut"; VBoxCmdOut=""; fi
                    if ! /sbin/ifconfig | grep -aiEoq "$VBoxStr"; then
                        ConsoleMessage "     vboxnet$VBoxNInm is now disabled/down in system."
                    else
                        ConsoleMessage "     vboxnet$VBoxNInm could not be disabled in system!"
                    fi
                fi
            else
                ConsoleMessage "  vboxnet$VBoxNInm interface, not found in system."
            fi
        done
    fi
    # END of vboxnet* interface Unloading section

    if kextstat -lb org.virtualbox.kext.VBoxNetFlt 2>&1 | grep -q org.virtualbox.kext.VBoxNetFlt; then
        ConsoleMessage " Unloading VBoxNetFlt.kext..."
        if ! kextunload -m org.virtualbox.kext.VBoxNetFlt; then
            ConsoleMessage "  Error: Failed to unload VBoxNetFlt.kext"
            VBOX_RC=1
            ConsoleMessage "  Ignoring failures & attempting to unload VBoxNetFlt.kext again..."
            kextunload -b org.virtualbox.kext.VBoxNetFlt
            ConsoleMessage "   done (unloaded kext)."
        else
            ConsoleMessage "  VBoxNetFlt.kext is now unloaded."
        fi
    fi

    if kextstat -lb org.virtualbox.kext.VBoxNetAdp 2>&1 | grep -q org.virtualbox.kext.VBoxNetAdp; then
        ConsoleMessage " Unloading VBoxNetAdp.kext..."
        if ! kextunload -m org.virtualbox.kext.VBoxNetAdp; then
            ConsoleMessage "  Error: Failed to unload VBoxNetAdp.kext"
            VBOX_RC=1
            ConsoleMessage "  Ignoring failures & attempting to unload VBoxNetAdp.kext again..."
            kextunload -b org.virtualbox.kext.VBoxNetAdp
            ConsoleMessage "   done (unloaded kext)."
        else
            ConsoleMessage "  VBoxNetAdp.kext is now unloaded."
        fi
    fi

    # This must come last because of dependencies.
    if kextstat -lb org.virtualbox.kext.VBoxDrv 2>&1 | grep -q org.virtualbox.kext.VBoxDrv; then
        ConsoleMessage " Unloading ${VBOXDRV}.kext..."
        if ! kextunload -m org.virtualbox.kext.VBoxDrv; then
            ConsoleMessage "  Error: Failed to unload VBoxDrv.kext"
            VBOX_RC=1
            ConsoleMessage "  Ignoring failures & attempting to unload VBoxDrv.kext again..."
            kextunload -b org.virtualbox.kext.VBoxDrv
            ConsoleMessage "   done (unloaded kext)."
        else
            ConsoleMessage "  ${VBOXDRV}.kext is now unloaded."
        fi
    fi

    VBoxNInm=0
    VBoxNIip=0
    #VBoxHInm=""
    VBoxStr=""
    VBoxCmdOut=""
    VBoxLoc=""
    VBoxFindThis=""

    # Set the error on failure.
    if [ "$VBOX_RC" -ne 0 ] ; then
        if [ "$VBoxRestart" -eq 0 ] ; then
            MsgWithoutEndNL " Run this script again to unload vbox kexts, and then reload all vbox kexts"; if [ "$VBoxNIF" -eq 1 ] ; then MsgWithoutEndNL " (and vboxnet* interfaces)"; fi; ConsoleMessage "."
            ConsoleMessage -f VirtualBox
            VBoxRestart=0
            VBoxNIF=0
            VBox3rdPrtySvc=0
            exit $VBOX_RC
        fi
    fi
}


RestartService()
{
    StopService
    if [ "$VBoxRestart" -gt 0 ] ; then
        let "VBoxRestart++"
    fi
    StopService
    if [ "$VBoxRestart" -gt 0 ] ; then
        let "VBoxRestart++"
    fi
    StartService
}


Start3rdPartyOrOtherServices()
{
    # by tErik
    # Load/Run Scripts or Programs or Services, which depend on VBox Kexts
    #  and Net-Interfaces:
    #
    # Note: Only add or enable or modify here those specific services/daemons,
    #       which must have to be modified/updated by using a specific command,
    #       to activate support for vbox net interfaces.
    #
    ConsoleMessage "Starting/Loading 3rd-party or other scripts/programs/services/servers,\r\n  which depend on VBox kexts & network-interfaces..."

    # Load Unbound DNSSEC DNS Server/Resolver section:
    if ps ax | grep -v grep | grep -aiEoq "/usr/local/opt/unbound/sbin/unbound"; then
        ConsoleMessage " Unbound DNSSEC DNS Server is already running.\r\n  Enabling unbound support for vbox net interfaces..."
        /usr/local/opt/unbound/sbin/unbound-control -q -c /usr/local/etc/unbound/unbound_for_vbox.conf reload
        if ps ax | grep -v grep | grep -aiEoq "/usr/local/opt/unbound/sbin/unbound"; then
            ConsoleMessage "   done."
        else
            ConsoleMessage "   failed to load unbound service!"
        fi
    else
        ConsoleMessage " Unbound DNSSEC DNS Server is not running.\r\n  Starting unbound server with support for vbox net interfaces..."
        /usr/local/opt/unbound/sbin/unbound -d -c /usr/local/etc/unbound/unbound_for_vbox.conf &
        if ps ax | grep -v grep | grep -aiEoq "/usr/local/opt/unbound/sbin/unbound"; then
            ConsoleMessage "   done."
        else
            ConsoleMessage "   failed to load unbound service!"
        fi
    fi
    # END of Unbound DNSSEC DNS Server/Resolver (Load) section

    ConsoleMessage " done (starting/loading 3rd-party/other services)."
    # END of Load/Run Scripts or Programs or Services, which use VBox Kexts & Net-Interfaces.
}


Stop3rdPartyOrOtherServices()
{
    # by tErik
    # Stop 3rd-Party Scripts or Programs or Services, which depend on VBox 
    #  Kexts & Net-Interfaces:
    #
    # Note: Do not stop-or-disable here those services/daemons, which are not 
    #       affected when any vbox net interface is down or does-not-exist or
    #       removed.  That is, those services are smart enough to respond
    #       properly for missing/disabled/removed vbox net-interfaces.
    #
    ConsoleMessage "Stopping/Disabling 3rd-party or other scripts/programs/services/servers,\r\n which depend on VBox kexts & network-interfaces..."

    # Unload Unbound DNSSEC DNS Server/Resolver, section:
    # /usr/local/opt/unbound/sbin/unbound-control -q -c /usr/local/etc/unbound/unbound.conf reload
    # We are not removing vboxnet0 IP-address support from Unbound,
    # because Unbound service can still function, even if vboxnet0 is removed from system.
    # END of Unbound DNSSEC DNS Server/Resolver (Unload) section

    ConsoleMessage " done (stopping/disabling 3rd-party/other services)."
    # END of Stop 3rd-Party Scripts or Programs or Services, which use VBox Kexts & Net-Interfaces.
}


ShowStatus()
{
    # by tErik
    # Show/Display running condition/STATUS & presence of vbox kexts
    # & vbox network interfaces, etc,
    # and also show running STATUS of related 3rd party services.
    #

    #VBOX_RC=0
    VBoxNoKext=0
    VBoxNInm=0
    VBoxNIip=0
    VBOXDRV="VBoxDrv"
    VBOXUSB="VBoxUSB"
    #VBoxHInm=""
    VBoxStr=""
    VBoxCmdOut=""
    VBoxChkDhcp=0
    VBoxLoc="/Applications/VirtualBox.app/Contents/MacOS"
    VBoxFindThis=""

    VBoxNIF=1

    ConsoleMessage "Displaying VirtualBox (vbox) kexts/drivers & NIF (net interfaces) running status...";

    #
    # Check if the vbox kext directories exist or not.
    #
    if [ ! -d "/Library/Application Support/VirtualBox/${VBOXDRV}.kext" ] ; then
        ConsoleMessage " /Library/Application Support/VirtualBox/${VBOXDRV}.kext is missing !"
        VBoxNoKext=1
    else
        ConsoleMessage " /Library/Application Support/VirtualBox/${VBOXDRV}.kext is found."
    fi
    if [ ! -d "/Library/Application Support/VirtualBox/${VBOXUSB}.kext" ] ; then
        ConsoleMessage " /Library/Application Support/VirtualBox/${VBOXUSB}.kext is missing !"
        VBoxNoKext=1
    else
        ConsoleMessage " /Library/Application Support/VirtualBox/${VBOXUSB}.kext is found."
    fi
    if [ ! -d "/Library/Application Support/VirtualBox/VBoxNetFlt.kext" ] ; then
        ConsoleMessage " /Library/Application Support/VirtualBox/VBoxNetFlt.kext is missing !"
        VBoxNoKext=1
    else
        ConsoleMessage " /Library/Application Support/VirtualBox/VBoxNetFlt.kext is found."
    fi
    if [ ! -d "/Library/Application Support/VirtualBox/VBoxNetAdp.kext" ] ; then
        ConsoleMessage " /Library/Application Support/VirtualBox/VBoxNetAdp.kext is missing !"
        VBoxNoKext=1
    else
        ConsoleMessage " /Library/Application Support/VirtualBox/VBoxNetAdp.kext is found."
    fi

    #
    # Check, if kexts/drivers are currently running or not.
    #
    if kextstat -lb org.virtualbox.kext.VBoxDrv 2>&1 | grep -q org.virtualbox.kext.VBoxDrv; then
        ConsoleMessage "  ${VBOXDRV}.kext is already loaded in system"
    else
        ConsoleMessage "  ${VBOXDRV}.kext is not loaded in system"
    fi
    if kextstat -lb org.virtualbox.kext.VBoxUSB 2>&1 | grep -q org.virtualbox.kext.VBoxUSB; then
        ConsoleMessage "  ${VBOXUSB}.kext is already loaded in system"
    else
        ConsoleMessage "  ${VBOXUSB}.kext is not loaded in system"
    fi
    if kextstat -lb org.virtualbox.kext.VBoxNetFlt 2>&1 | grep -q org.virtualbox.kext.VBoxNetFlt; then
        ConsoleMessage "  VBoxNetFlt.kext is already loaded in system"
    else
        ConsoleMessage "  VBoxNetFlt.kext is not loaded in system"
    fi
    if kextstat -lb org.virtualbox.kext.VBoxNetAdp 2>&1 | grep -q org.virtualbox.kext.VBoxNetAdp; then
        ConsoleMessage "  VBoxNetAdp.kext is already loaded in system"
    else
        ConsoleMessage "  VBoxNetAdp.kext is not loaded in system"
    fi

    ConsoleMessage " Status of network interfaces in system...\r\n  Each network interface begins with * symbol..."
    VBoxStr=$(ifconfig -av | expand | sed -E -e 's/^([[:alnum:]]+\:) flags\=/* \1 flags=/g' -e 's/^[\ ]+(nd6 options\=[[:alnum:]]+)\<([[:alnum:]\,]*)\>/\1, \2/g' -e 's/^[\ ]+options.+//g' -e 's/^[\ ]+media\:.+$//g' -e 's/^[\ ]+eflags\=.+$//g' -e 's/ index [[:alnum:]]+$//g' -e 's/ family\: [[:alnum:]]+ subfamily\: [[:alnum:]]+//g' -e 's/ (flags\=[[:alnum:]]+\<[[:alnum:]\,]+)\>/ \1,>/g' -e 's/ flags\=[[:alnum:]]+\<([[:alnum:]\,]*)\>/ \1/g' | tr -s '\t\f\v\b\ ' " " | sed -E -e 's/^ $//g' -e '/^$/d' -e 's/ $//g' -e 's/$/, /g' | tr -s '\r\n\t\f\v\b\ ' " " | sed -E -e 's/\, \* ([[:alnum:]])/ * \1/g' -e 's/\,[\ ]*\, /, /g' | tr -s '\ ' " " | sed -E 's/\,[\ ]*$//' )
    ConsoleMessage "  $VBoxStr"

    #
    # Check which vbox nif (network interface) is defined inside vbox settings,
    #   chk if dhcp is defined for those vbox nif in vbox settings,
    #   chk if vbox nif is available in system or not,
    #   chk if vbox nif is up/active/usable in system or not,
    #   chk if dhcp for nif is defined & working in system, or not.
    #
    # VBoxStr=$(${VBoxLoc}/VBoxManage list hostonlyifs)
    # ... coming soon ...

    # END of ShowStatus function-call
}


RunService "$1"
In mac osx Terminal, you can test this entire script, or test portion of it's various functions, if its working or not.

Below command will do: kext/driver unloading/loading, Network Interface disable/enable, 3rd Party Service unloading/loading, etc:

sudo "/Library/Application Support/VirtualBox/LaunchDaemons/VirtualBoxStartup.sh" restart_with_nifs_and_3rdPrtySvc

you can test this script, if kexts/drivers unloading/loading, Network Interface enable/disable, etc are working or not:

sudo "/Library/Application Support/VirtualBox/LaunchDaemons/VirtualBoxStartup.sh" restart_with_nifs

you can test this script, if kexts/drivers unloading/loading is working or not:

sudo "/Library/Application Support/VirtualBox/LaunchDaemons/VirtualBoxStartup.sh" restart

You can also test the above bash script with these other parameters:
stop
stop_also_nifs
stop_also_nifs_and_3rdPrtySvc
start
start_with_nifs
start_with_nifs_and_3rdPrtySvc
launchd
launchd_with_nifs
launchd_with_nifs_and_3rdPrtySvc
status


To Start VBox (kexts and) Network Interfaces & 3rd Party Services At Boot:
Download "Lingon" from http://sourceforge.net/projects/lingon/ website. Install it. Start Lingon.
Inside Lingon, locate "org.virtualbox.startup" under USER DAEMONS section.
In "What" textbox, it suppose to show data code like below:
"/Library/Application Support/VirtualBox/LaunchDaemons/VirtualBoxStartup.sh" restart
change above data codes into below data code:
"/Library/Application Support/VirtualBox/LaunchDaemons/VirtualBoxStartup.sh" restart_with_nifs_and_3rdPrtySvc
then click on any other service/daemon once, lingon will auto initiate & display Mac OSX's privilege user password & consent window, to help saving the changes, so enter an Admin level privileged user-name/id & that user's pass, click on ok button.

Reboot once, or execute below command once in Terminal:

sudo "/Library/Application Support/VirtualBox/LaunchDaemons/VirtualBoxStartup.sh" restart_with_nifs_and_3rdPrtySvc

Then in Terminal, run below dns test command:

dig @192.168.56.1 in TLSA _443._tcp.www.isc.org. +dnssec

If above dns query result shows "ad" in flag, and "NOERROR" in status, then it is indicating, unbound DNS daemon/service is responding to DNS query sent toward vboxnet0 interface ip address 192.168.56.1, and DNSSEC based validated DNS resolving is also working fine.
Note: If you do not have a local unbound or bind DNSSEC enabled DNS Server running-at or listening-to vboxnet0 interface ip-adrs 192.168.56.1, then above command will not work. See next post, how you can load "unbound" dnssec dns server in host OS, for host itself and for all VMs.

So from now on, VBox kexts & vbox network interfaces (NIFS) & 3rd party services (if specified inside appropriate section of bash script) will auto start at boot (a.k.a. during boot of Mac OSX host computer).

These codes are tested & passed in my Mac OSX computer:
at the time of writing these posts & testing & developing this script for virtualvox...
i have used VirtualBox-4.3.30-101610-OSX, my test seems to have worked, and i will use at-least for a week.
VirtualBox v5.0.0-101573 (July, 2015) seems to be buggy or very likely buggy, when 2nd adapter (a host-only) was added into a guest/VM, then VM's both adapters loose DHCP features & stop working !
VirtualBox v5.0.0-RC1-101436 (July, 2015) could not even load the VM which was created using v4.3.28 !
In VirtualBox v4.3.28 r100309 (2015), my test was successful (i observed no bugs related to this posting/context), and i used it also for a week, fully functional.

My host computer running Mac OS X, it is Yosemite 10.10.4 (July, 2015).
Script file also worked on Mac OSX 10.10.3.
Related tests were also done on one of my VM, which guest-OS is WinXP Professional (32bit), with SP3.

Disclaimer: Some of the codes & paragraphs of information i have shown here, are directly copied from my own notes, or from other authors (either from their websites, or from contributed info webpages on various project websites, blogging websites, etc), or from Manual/Document webpages, or from public mailing list, or from public IRC channels, etc. And i have modified some of them slightly. Worked on my computers. But you may have different result as overall configuration might not be same, so use with caution & with your own decision & risk.
Last edited by tErik on 8. Aug 2015, 09:15, edited 4 times in total.
tErik
Posts: 3
Joined: 23. Jul 2015, 07:59

Re: HowTo Start VBox Net Interfaces At Boot & Attach Servers

Post by tErik »

I have installed homebrew (a package manager for Mac OSX) in host MacOSX computer, then installed openssl & the "unbound" dnssec dns server by using homebrew, to use "unbound" as my local full dnssec supporting dns resolver, for all VMs and for the host itself.

I have showed & followed instruction on how to do just those, here:
https://stackoverflow.com/questions/31043217/

My all VM guest OS are currently using that "unbound" dnssec dns resolver from Mac OSX host, to resolve DNS.

NIF = Network Interface.
NIC = Network Interface Card, it also means, Network Interface Adapter (NIA).
VBox = VirtualBox.
NetAdp = net-adp = Network Adapter.
nw = network.
NIF ~=~ NetAdp ~=~ NIC.
Host-Only = HO.
PAT = Port Address Translation.
NAT = Network Address Translation. When used with "NAT network adapter" or "NAT adapter" or "NAT interface", it usually indicates a network interface adapter which uses both NAT & PAT.
Adrs = Address.
subnet mask = s.m. = sm = snm = mask.
cidr /24 = sm 255.255.255.0
cidr /16 = sm 255.0.0.0

TO ACHIEVE ABOVE:

Add a Host-Only Network in VBox, And Add a Host-Only NIF in VM : i had to add a Host-Only network inside VBox > Preferences > Network > Host-only Networks, GUI window. By default, 1st Host-only network will use ip-address 192.168.56.1 (and, subnet mask 255.255.255.0, a.k.a /24), and it is also known as "vboxnet0" host-only network. VBox should also automatically add a DHCP server & a DHCP range for "vboxnet0" network. If in your case it doesn't automatically do that, then manually do these : goto DHCP tab of "vboxnet0" Host-only-Networks gui window, and type-in 192.168.56.100 ip-address in "DHCP Server" text-field, and set subnet mask to 255.255.255.0, then add a dhcp range, from lowest 192.168.56.101 to highest 192.168.56.254, and keep it enabled.
Then i have configured & enabled a 2nd network adapter (VM Net Adpater #2), a.k.a. 2nd NIF, inside my specific vm "VM-Name-Win-XP" > Settings > Network > Adapter 2, like this : i 1st selected "Enabled" option, then selected the "Host-only" network-mode option, inside "attached-to" option-list. And then i selected the host-only network "vboxnet0", from "name" option-list.

Note: my most VM's 1st NIF (aka, Adapter-1) is in default network mode, what is provided by VBox software by default. Which is "NAT" mode Network Interface Adapter-1 (Adapter-1). It is attached with "natnet1" network inside VM. So it is basically a NAT(+PAT) network. A bit more details are shown in next paragraph.

Network configuration inside VM's Guest OS : Usually, guest OS inside VM will always have it's own localloop nif "lo" with ip-address 127.0.0.1, and because of a default NAT mode vm adapter (which is vm Adapter-1), guest-OS will have a NIF (aka, network-adapter) with a private non-internet routable ip-address, (like 127.0.0.1). This NAT mode vm Adapter-1 usually becomes "Local Area Network" (LAN-1) network adapter, inside a VM with Windows XP guest OS, (or it becomes "eth0" nif, inside a VM with Linux guest-OS, etc). And by default, in a WinXP guest-OS, any LAN configuration settings are pre-set with : "Obtain IP-Address Automatically" & "Obtain DNS Automatically", which means, net-adapter uses DHCP feature to auto obtain ip-address & other network settings from DHCP server. By default, usually, for 1st NAT nif inside VBox VM, it is pre-defined to allocate the ip-address 10.0.2.15 (DHCP obtained/based) to LAN-1 nif, while it's gateway, dhcp, dns being also set as 10.0.2.2, (in some cases dns may be set to 10.0.2.3).
And in my/our case, i/we have a host-only mode nif, as 2nd nif, in same VM as Adapter-2, and inside WinXP guest-OS, this 2nd NIF will appear as "Local Area Network 2" (LAN-2) network adapter, and this 2nd nif will usually also have "auto-obtain" type of pre-set configuration settings, similar to LAN-1. And because of Adapter-2 is attached with "voxnet0", a host-only network, as a result, 1st VM's guest OS with a 2nd net adapter (appearing as, LAN-2), will usually obtain ip-address 192.168.56.101 (from DHCP Server 192.168.56.100), while it's gateway & dns are pre-set to 192.168.56.1, and DHCP server is ofcourse pre-set to 192.168.56.100. (In some cases, DHCP server could also be set to 192.168.56.1, for VM Adapter-2 & for the VM LAN-2).

Then, to force all software (inside VM's guest-OS) to use the common DNSSEC DNS Server service, running-at or listening-on ip-address 192.168.56.1 port 53, i have changed inside VM guest-OS LAN-1 DNS settings : from auto-obtain type of DNS settings, into a specific Primary DNS ip-address 192.168.56.1, and i kept only one (same) DNS ip adrs in Primary DNS field settings.
And in my case, my host OS is also using a DNSSEC DNS Server (for it's all software by default, including VBox software in host OS), so i also tested VM guest-OS side LAN-1 with these two DNS addresses : 10.0.2.2 as 1st DNS (or in some case you may have to use 10.0.2.3 or whatever DNS IP-address was default DNS for LAN-1 allocated by VM side "natnet1" network), and also specified this 192.168.56.1 as 2nd DNS.

And as LAN-2 obtains pre-defined network settings from DHCP server in "vboxnet0" network, in my side the DNS appeared as 192.168.56.1, (same IP-adrs as where the DNSSEC DNS Server is running-at or listening-on), so i did not need to assign this ip-adrs as a specific one dns ip-adrs inside LAN-2 DNS settings. If that is not the case in your side, for example if you have a different default DNS like 192.168.56.100 for LAN-2, then edit LAN-2 settings, select Specify fix/specific DNS option, and set 192.168.56.1 as one DNS in LAN-2. If that does not work, then set these two ip addresses as DNS servers in LAN-2 : set 192.168.56.100 as 1st DNS (or set whatever IP-address was by default a DNS for LAN-2 allocated by "vboxnet0" network), and then also set this 192.168.56.1 as 2nd DNS.

gw = gateway . LAN = Local Area Network .

Note : If both LAN-1 & LAN-2 are working properly, then SKIP this paragraph : if your guest-OS LAN-1 & LAN-2 both or one is failing to auto-obtain IP address related settings from VBox DHCP, only in such case do these : shutdown guest-OS inside VM & exit/quit from VirtualBox . Open Terminal in host OS, run below command:
/Applications/VirtualBox.app/Contents/MacOS/VBoxManage modifyvm "VM-Name-Win-XP" --natnet1 "10.0.2.0/24"
Start VirtualBox application. Start guest-OS "VM-Name-Win-XP" vm . Goto Control Panel > Network Connections & Properties > you will find both LAN-1 & LAN-2 network adapters here . Right click on each LAN (do these one LAN by one LAN, go thru this paragrapgh multiple times) > select "Properties" option > General > scroll down in item list & find out "Internet Protocol (TCP/IP or TCP/IPv4)" > click on it to select it > click on "Properties" button > General > keep "obtain ip auto..." option , keep "obtain dns auto..." option , inside guest-OS both net adapter LAN-1 & LAN-2 . Then add an "Alternative configuration" profile for LAN-1 with these info : ip-adrs 10.0.2.15 , mask 255.255.255.0 , gw & dns-1 10.0.2.2 , dns-2 192.168.56.1 . And in LAN-2's Alternative configuration profile, set these : ip-adrs 192.168.56.101 , mask 255.255.255.0 , gw 192.168.56.100 , dns 192.168.56.1 . Inside LAN-1 > Advanced > add a gw 10.0.2.2 , and in LAN-2 > Advanced > add a gw 192.168.56.100 . Save settings ( click on: ok > ok ) . Goto Control Panel > Network Connections & Properties > Advanced > Advanced Settings > "Adapters & Bindings" tab > sort & keep LAN-1/LAN as top-most 1st item, and keep LAN-2 as 2nd item, other items should be below LAN-2 > goto "Provider Order" tab > sort & keep "VirtualBox Shared Folders" as top-most 1st item , keep "Microsoft Windows Network" as 2nd item , others should be below MS-Win-Net , Save changes ( click on OK ) . Must reboot VM guest OS, once .

Create Unbound DNSSEC DNS Configuration For Host OS And For Host OS With VBox Net Interfaces:
To attach the "unbound" dns server service with 192.168.56.1/24 (vboxnet0) network ip-address, i have copied the 1st/initial "unbound.conf" file (in Mac OSX host) in same folder, as "unbound_for_vbox.conf" file.
Then i used BlueFish to open both files for text-editing, in host OS:
/usr/local/etc/unbound/unbound_for_vbox.conf
/usr/local/etc/unbound/unbound.conf


unbound.conf file, should have at-least these below lines:

Code: Select all

server:
    verbosity: 1
    num-threads: 2
    interface: 127.0.0.1
    interface: ::1
    #interface: 192.168.56.1
    port: 53
    #ip-transparent: yes
    do-ip4: yes
    do-ip6: yes
    do-udp: yes
    do-tcp: yes
    do-daemonize: yes
    access-control: 127.0.0.0/8 allow
    access-control: ::1 allow
    access-control: 192.168.56.1/24 allow
    access-control: 0.0.0.0/0 refuse
    access-control: ::0/0 refuse
    #module-config: "[dns64] [validator] iterator"
    module-config: "validator iterator"
    auto-trust-anchor-file: "/usr/local/etc/unbound/root.key"
    #dlv-anchor-file: "/usr/local/etc/unbound/dlv.isc.org.key"
remote-control:
    control-enable: yes
    control-use-cert: no
    # Specifying only 1 IP-address which can control DNS Resolver
    control-interface: 127.0.0.1
    control-port:8953
    #server-key-file: "/usr/local/etc/unbound/unbound_server.key"
    #server-cert-file: "/usr/local/etc/unbound/unbound_server.pem"
    #control-key-file: "/usr/local/etc/unbound/unbound_control.key"
    #control-cert-file: "/usr/local/etc/unbound/unbound_control.pem"
if your unbound.conf is not same as above, copy appropriate config code lines from above into your side.
Then edit "unbound_for_vbox.conf" file, only one line need to be edited/changed into it, all other above lines should exist at-least.
Only remove the "#" symbol, from in front of the "#interface: 192.168.56.1" line in "unbound_for_vbox.conf" file. That is it.

If you are not going to use the command "unbound-control" to control unbound dns server/resolver features from mac osx host's local ip 127.0.0.1, in such case you may disable that "remote-control" section, by placing a single "#" symbol in front of each line, under the "remote-control" section, only.

Save both files:
unbound.conf & unbound_for_vbox.conf

Activate Unbound DNSSSEC DNS SERVER section:
this section exists inside the "VirtualBoxStartup.sh" file (a bash script file), shown in above/previous/top/op post.
Open "VirtualBoxStartup.sh" file inside BlueFish for text-editing.
Find/search (press Cmd+F) for below exact line:
grep -aiEoq "/usr/local/opt/unbound/sbin/unbound"
and remove 17(seventeen) "#" symbols, from in front of 17 lines of code there, to activate Unbound loading section.
Save, ok. (To save changes into this specific bash script file, you will have to change file's ownership temporarily, which is explained in top/1st/op post).

Reboot once, OR, run below command once, inside host os Terminal shell:

sudo "/Library/Application Support/VirtualBox/LaunchDaemons/VirtualBoxStartup.sh" restart_with_nifs_and_3rdPrtySvc

From now on, Unbound DNSSEC DNS Server/Resolver (on host OS) will auto start & attach itself with vboxnet0 network (192.168.56.1/255.255.255.0) in host-OS side. And because of unbound support, when any VM network adapter is attached-to (the vbox Host-Only) "vboxnet0" network, then in such case, those guest-OS (inside VM) will be able to use the unbound dns resolver/server, which exists & running in host OS.
And, as all VMs & host will use a common DNS Server in host OS, so total DNS traffic will also be much lesser, and, dns resolving will be much faster, and, all software will be using a very secured DNSSEC DNS Server, which is a very advanced & secured next edition (of older) DNS Server & standards.

Configure and Add More Server Services Inside "VirtualBoxStartup.sh" bash script file on host:
Various other common server/daemon services, which can be run on host OS, are now possible, and can attach with host-side VBox (Host-Only) network vboxnet*, and these services can also be shared among more VM guest OSes.

If you are using Windows-OS as guest OS inside VM, then do these steps:
ISC BIND Utilities bundled file, includes the "dig" tool, which allows to test DNS related settings & information. So obtain BIND 32-bit for windows, a zip file, from https://www.ISC.org/ website. Decompress zip file by using 7-Zip ( http://www.7-zip.org/ ) software and rename decompressed subfolder into "BIND-UTILS", then move it inside this new folder C:\Programs\
Inside guest OS, Windows > press these buttons: WinFlag + R > type in & press "ok" button after this: sysdm.cpl
or go into: Control Panel > System > Advanced Properties (Advanced tab) > click on "Environment Variables" button > scroll down in list shown under "System Variables" section > find the line which has "Path" or PATH environment variable > click once on the line to select it & then click on "Edit" button > press the right-arrow button once, to goto end position in "Path" textbox.
Triple click on below code line which is showing bind-utils full path, and Copy (press Ctrl+C) it into buffer memory, and then go back to the window which showing "Path" textbox & paste (press Ctrl+V) code (at end of PATH environment variable), then click on three OK buttons one by one, related to this editing, to save changes. BIND-UTILS full path code, for PATH variable:

;C:\Programs\BIND-UTILS\

After adding new path in PATH variable inside VM guest OS, either logout from guest-OS windows and log back into your windows profile, or reboot once the guest-OS windows VM computer.

Then inside guest OS ("VM-Name-Win-XP") run Command-Prompt shell window (press WinFlag+R > type & press Enter button after this: cmd.exe), run below DNS test command, three or four times:

dig in TLSA _443._tcp.www.isc.org. +dnssec

If dig dns query result shows "ad" in flags, and if status codes showing "NOERROR", and if any one of the DNS ip-address (which were specified inside guest-OS WinXP LAN-1 DNS in previous/above steps, like, 192.168.56.1, and/or, 10.0.2.2) are shown in "SERVER" line, (which is under the "Query Time" line), then, DNSSEC based DNS query from guest-OS succeeded successfully.
Or else, your configuration is not working properly, so check+read these messages again carefully, or check this forum, or check the #vbox freenode irc channel, or check the vbox mailing-list, for a solution.

Disclaimer: Some of the codes & paragraphs of information i have shown here, are directly copied from my own notes, or from other authors (either from their websites, or from contributed info webpages on various project websites, blogging websites, etc), or from Manual/Document webpages, or from public mailing list, or from public IRC channels, etc. And i have modified some of them slightly. Worked on my computers. But you may have different result as overall configuration might not be same, so use with caution & with your own decision & risk.
Last edited by tErik on 9. Aug 2015, 14:02, edited 7 times in total.
tErik
Posts: 3
Joined: 23. Jul 2015, 07:59

Re: HowTo Start VBox Net Interfaces At Boot & Attach Servers

Post by tErik »

reserving this 3rd post for adding more info
Post Reply