Possible headless end-to-end install and VDI access?

Discussions related to using VirtualBox on Linux hosts.
Post Reply
Joshua Schlichting
Posts: 4
Joined: 8. Feb 2019, 17:36

Possible headless end-to-end install and VDI access?

Post by Joshua Schlichting »

Is it possible to use VirtualBox and its child virtual machines without any sort of GUI? Just pure CLI?

I know that it is possible to install virtual box and work it through the CLI using VBoxManage. What I really want to know is, once I get this far, and my VDI is running, is there a way for me to access that VDI without any graphic user interface?

Example:
>Install VirtualBox via bash (done.)
>User VBoxManage to create, register, configure, and start a VDI via bash (done.)
>access the Guest VDI's shell via bash (???)

My instincts tell me that I should be able to SSH into the guest VDI once it is running...but I don't know its IP address to be able to do this. A bit of research shows that you can access the IP address (VBoxManage guestproperty enumerate <vmname>), but only if you have VBoxGuestAdditions.iso installed on the guest. This presents a recursive issue, because I need to SSH into the guest in order to install the guest additions (but I can't without the IP!)

My end goal is to have a server without a desktop environment hosting several virtual machines, but I'm starting to think this might not be possible (but it has to be, right? VirtualBox is a big name in this virtualization!).

Any help would be appreciated! Thanks in advance!
socratis
Site Moderator
Posts: 27329
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: Possible headless end-to-end install and VDI access?

Post by socratis »

According to the Overview of Networking Modes, the two easiest ways for the host to be able to access the guest are HostOnly and Bridged. With either of them, you can use a couple of tools to find out which IP your guest has. The MAC address of your VM you get it from:
  • 
    VBoxManage showvminfo <VM>
HostOnly
Starting with VirtualBox 6.0.0, you can now find out which MAC address has which IP assigned/leased. On my OSX installation for example, in "~/Library/VirtualBox/HostInterfaceNetworking-vboxnet0-Dhcpd.leases" you can find entries such as:
<Lease mac="08:00:27:4d:dd:3e" network="0.0.0.0" state="expired">
  <Address value="192.168.20.108"/>
  <Time issued="1549534878" expiration="1200"/>
</Lease>
<Lease mac="08:00:27:43:b3:0b" id="0108002743b30b" network="0.0.0.0" state="acked">
  <Address value="192.168.20.107"/>
  <Time issued="1549736263" expiration="1200"/>
</Lease>
Bridged
Use the standard 'arp' tool to find out the IP based on the MAC address.:
  • 
    arp -a -n | grep <myMAC>
I've got a small script that tries to get the IP from the arp table for all running VMs. Since you're on a Linux host, see if it can help you. If it needs any modifications, let me know. It's not 100% proof, because for some reason sometimes the entries aren't there, but it mostly works:

Code: Select all

#!/bin/bash
#
# Translate a MAC address fetched from VirtualBox into a IP address
#

echo `(arp -a -n | wc -l | awk '{print $1}')` "entries in the ARP table to parse."

MY_MACs=();
IFS=$'\t\n';

# Loop for all the running VMs
for RunningVM in `VBoxManage list runningvms | sort | cut -d'"' -f2`; do
    echo -ne $RunningVM;

    # Loop for all 32 possible NICs
    for i in {1..32}; do
        echo -ne "."        # Just a progress indicator ;)

        # Get a string of the form: macaddressNN="080027abcdef"
        #   then cut it at the equal ("=") sign
        #     then cut it at the right length
        #       then make it all lower case
        MY_MAC=$(VBoxManage showvminfo "$RunningVM" --machinereadable | grep macaddress$i | cut -d'=' -f2 | cut -c2-13 | tr '[A-Z]' '[a-z]');

        # If the MY_MAC is not empty, format it so that the leading zeroes (in pairs of 2) are not shown,
        # and separate each pair number by a colon. That's because this is the way that "arp" outputs the
        # MAC addresses as well. Example: "080027ab0def" becomes "8:0:27:ab:d:ef".
        if [ "${MY_MAC}" != "" ]; then
            MY_MAC=$(echo `expr ${MY_MAC:0:2}`:`expr ${MY_MAC:2:2}`:`expr ${MY_MAC:4:2}`:`expr ${MY_MAC:6:2}`:`expr ${MY_MAC:8:2}`:`expr ${MY_MAC:10:2}`)
            MY_MACs[i]="$MY_MAC"
        fi

    # Loop for all 32 possible NICs
    done

    echo "."                # Restore order for the progress indicator

    IFS=$'\n';

    # Loop for all ARP table entries.
    for MY_ARPentry in $(arp -a -n); do
        # Each entry in the "arp -a -n" output looks like (space separated):
        #
        # ? (192.168.20.102) at (incomplete) on vboxnet0 ifscope [ethernet]
        #  1                2  3            4  5        6       7           <---- These are the spaces
        #
        # Use the space as a delimiter to get the MAC, and the parentheses to get the IP.
        MY_ARP_MAC=$(echo ${MY_ARPentry} | cut -d' ' -f4 | cut -d' ' -f3)
        MY_ARP_IP=$( echo ${MY_ARPentry} | cut -d'(' -f2 | cut -d')' -f1)
    
        # Now loop through each of the MAC addresses stored in the 'VBoxManage showvminfo'.
        # If you find a match, print it out.
        for MY_MAC in "${MY_MACs[@]}"; do
            if test "$MY_MAC" = "$MY_ARP_MAC"; then
                unset IFS
                MY_ARP_MAC="$(printf "%02X:%02X:%02X:%02X:%02X:%02X" 0x${MY_ARP_MAC//:/ 0x})"
                echo "    MAC: $MY_ARP_MAC  IP: $MY_ARP_IP"
            fi

        # Loop for all MAC addresses.
        done

    # Loop for all ARP table entries.
    done

# Loop for all the running VMs
done
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
Post Reply