Dynamic interface names for Windows batch scripts

On some systems, Windows VirtualBox starts numbering its host-only
interfaces (e.g., VirtualBox Host-Only Ethernet Adapter #2) at higher
numbers even if no such interfaces are already present.

In these cases, our scripts fail because they assume that the interfaces
to use are named
- VirtualBox Host-Only Ethernet Adapter
- VirtualBox Host-Only Ethernet Adapter #2
- VirtualBox Host-Only Ethernet Adapter #3
- [...]

With this changeset, create_hostnet write the names of the host-only
interfaces it creates to a configuration file which is used by the
the script that builds the cluster.

This will work regardless of the interface names returned by VBoxManage.

In order to write the correct variable names in all places, we use the
Unix-type interface names (e.g., vboxnet0) as Windows variable names
(e.g., %vboxnet0%).

Change-Id: Ieb67e646e8eaa96a57ff507283c74307350847e2
This commit is contained in:
Roger Luethi 2017-05-25 11:12:43 +02:00
parent 39d2384bd5
commit 33e507c25f
8 changed files with 43 additions and 44 deletions

View File

@ -171,6 +171,7 @@ function wbatch_mkdirs {
function wbatch_create_hostnet {
wbatch_new_file "create_hostnet.bat"
wbatch_file_header "host-only networks"
cat "$WBATCH_TEMPLATE_DIR/template-begin_hostnet_bat" | wbatch_write_stdin
# Creating networks requires elevated privileges
wbatch_elevate_privileges
wbatch_find_vbm
@ -183,7 +184,7 @@ function wbatch_create_hostnet {
# The host-side interface is the default gateway of the network
if_ip=${NET_GW[index]}
# Translate if_name to Windows-type interface name
win_adapter=$(vboxnet_to_win_adapter_num "$if_name")
win_adapter=$if_name
sed -e "
s,%IFNAME%,${win_adapter},g;
s,%IFIP%,${if_ip},g;
@ -237,7 +238,7 @@ function wbatch_log_vbm {
case "${ARGS[i]}" in
--hostonlyadapter*)
# The next arg is the host-only interface name -> change it
ARGS[i+1]=\"$(vboxnet_to_win_adapter_num "${ARGS[i+1]}")\"
ARGS[i+1]=\"%${ARGS[i+1]}%\"
;;
--hostpath)
# The next arg is the shared dir -> change it
@ -271,20 +272,6 @@ function wbatch_log_vbm {
# Windows path name helpers
#-------------------------------------------------------------------------------
# Translate Unix-style vboxnetX to Windows-type interface name
function vboxnet_to_win_adapter_num {
local vboxname=$1
local win_if="VirtualBox Host-Only Ethernet Adapter"
# Remove leading "vboxnet" to get interface number
local ifnum=${vboxname#vboxnet}
if [ "$ifnum" -ne 0 ]; then
win_if+=" #$((ifnum + 1))"
fi
echo "$win_if"
}
# On Windows, all paths are relative to TOP_DIR
function wbatch_path_to_windows {
local full_path=$1

View File

@ -0,0 +1,6 @@
ECHO %time% Deleting old iface_config.bat
IF EXIST "%~dp0\iface_config.bat" DEL "%~dp0\iface_config.bat"
ECHO.
REM vim: set ai ts=4 sw=4 et ft=dosbatch:

View File

@ -1,3 +1,6 @@
ECHO %time% Loading network interface names
CALL "%~dp0\iface_config.bat"
ECHO %time% Cleaning up autostart and log directories
DEL /S /Q %AUTODIR%
DEL /S /Q %LOGDIR%

View File

@ -1,9 +1,12 @@
ECHO VBoxManage hostonlyif create
"%VBM%" hostonlyif create
REM Using "'" as a delimiter, the second token is the name of the VirtualBox
REM Ethernet interface
FOR /F "tokens=2 delims='" %%A IN ('"%VBM%" hostonlyif create') DO SET IFACE=%%A
IF %errorlevel% NEQ 0 GOTO :vbm_error
ECHO SET %IFNAME%=%IFACE%
SET %IFNAME%=%IFACE%
ECHO SET %IFNAME%=%IFACE%>> "%~dp0\iface_config.bat"
ECHO VBoxManage hostonlyif ipconfig "%IFNAME%" --ip %IFIP% --netmask 255.255.255.0
"%VBM%" hostonlyif ipconfig "%IFNAME%" --ip %IFIP% --netmask 255.255.255.0
IF %errorlevel% NEQ 0 GOTO :vbm_error
ECHO VBoxManage hostonlyif ipconfig "%%IFNAME%%" --ip %IFIP% --netmask 255.255.255.0
"%VBM%" hostonlyif ipconfig "%%IFNAME%%" --ip %IFIP% --netmask 255.255.255.0
REM vim: set ai ts=4 sw=4 et ft=dosbatch:

View File

@ -190,13 +190,13 @@ def wbatch_mkdirs():
def wbatch_begin_hostnet():
wbatch_new_file("create_hostnet.bat")
wbatch_file_header("host-only networks")
wbatch_write_template("template-begin_hostnet_bat")
# Creating networks requires elevated privileges
wbatch_elevate_privileges()
wbatch_find_vbm()
def wbatch_create_hostnet(if_ip, adapter):
adapter = vboxnet_to_win_adapter_num(adapter)
replace = {"IFNAME": adapter,
"IFIP": if_ip}
wbatch_write_template("template-create_hostnet_bat", replace)
@ -237,9 +237,11 @@ def wbatch_log_vbm(*args):
argl = list(*args)
for index, arg in enumerate(argl):
if re.match("--hostonlyadapter", arg):
# The next arg is the host-only interface name -> change it
argl[index+1] = '"' + vboxnet_to_win_adapter_num(argl[index+1]) + \
'"'
# The next arg is the host-only interface name -> change it.
# We use the vboxnet interface name as a variable name for the
# Windows batch scripts. This is a reference to the variable,
# therefore the string must be somethin like "%vboxnet0%".
argl[index+1] = '"' + "%{}%".format(argl[index+1]) + '"'
elif re.match("--hostpath", arg):
# The next arg is the shared dir -> change it
argl[index+1] = r'%SHAREDIR%'
@ -264,20 +266,6 @@ def wbatch_log_vbm(*args):
# -----------------------------------------------------------------------------
def vboxnet_to_win_adapter_num(vboxname):
win_if = "VirtualBox Host-Only Ethernet Adapter"
# Remove leading "vboxnet" to get interface number
if_num = int(vboxname.replace("vboxnet", ""))
if if_num > 0:
# The first numbered "VirtualBox Host-Only Ethernet Adapter" is #2
win_if += " #{}".format(str(if_num + 1))
logger.debug("vboxnet_to_win_adapter_num returns: %s", win_if)
return win_if
def wbatch_path_to_windows(full_path):
rel_path = hf.strip_top_dir(conf.top_dir, full_path)

View File

@ -0,0 +1,6 @@
ECHO %time% Deleting old iface_config.bat
IF EXIST "%~dp0\iface_config.bat" DEL "%~dp0\iface_config.bat"
ECHO.
REM vim: set ai ts=4 sw=4 et ft=dosbatch:

View File

@ -1,3 +1,6 @@
ECHO %time% Loading network interface names
CALL "%~dp0\iface_config.bat"
ECHO %time% Cleaning up autostart and log directories
DEL /S /Q %AUTODIR%
DEL /S /Q %LOGDIR%

View File

@ -1,9 +1,12 @@
ECHO VBoxManage hostonlyif create
"%VBM%" hostonlyif create
REM Using "'" as a delimiter, the second token is the name of the VirtualBox
REM Ethernet interface
FOR /F "tokens=2 delims='" %%A IN ('"%VBM%" hostonlyif create') DO SET IFACE=%%A
IF %errorlevel% NEQ 0 GOTO :vbm_error
ECHO SET #IFNAME=%IFACE%
SET #IFNAME=%IFACE%
ECHO SET #IFNAME=%IFACE%>> "%~dp0\iface_config.bat"
ECHO VBoxManage hostonlyif ipconfig "#IFNAME" --ip #IFIP --netmask 255.255.255.0
"%VBM%" hostonlyif ipconfig "#IFNAME" --ip #IFIP --netmask 255.255.255.0
IF %errorlevel% NEQ 0 GOTO :vbm_error
ECHO VBoxManage hostonlyif ipconfig "%#IFNAME%" --ip #IFIP --netmask 255.255.255.0
"%VBM%" hostonlyif ipconfig "%#IFNAME%" --ip #IFIP --netmask 255.255.255.0
REM vim: set ai ts=4 sw=4 et ft=dosbatch: