Skip to content

Instantly share code, notes, and snippets.

@PhrozenByte
Last active April 11, 2019 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PhrozenByte/429f7a0cb626ef6c73bca43f22e9dcc4 to your computer and use it in GitHub Desktop.
Save PhrozenByte/429f7a0cb626ef6c73bca43f22e9dcc4 to your computer and use it in GitHub Desktop.
Runs applications inside a VirtualBox VM (VBoxGuestRun.bat) and toggles a VirtualBox VM on/off (VBoxToggle.bat)
@echo off & setlocal
REM Runs applications inside a VirtualBox VM
REM Version 1.1 (build 20160904)
REM
REM Copyright (C) 2016 Daniel Rudolf <www.daniel-rudolf.de>
REM
REM This program is free software: you can redistribute it and/or modify
REM it under the terms of the GNU General Public License as published by
REM the Free Software Foundation, version 3 of the License only.
REM
REM This program is distributed in the hope that it will be useful,
REM but WITHOUT ANY WARRANTY; without even the implied warranty of
REM MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
REM GNU General Public License for more details.
REM
REM See <http://www.gnu.org/licenses/> to receive a full-text-copy of
REM the GNU General Public License.
if "%~1" == "" (
set ERROR=You must pass a VirtualBox VM name or ID as first argument
goto :error
)
if "%~2" == "" (
set ERROR=You must pass a path to an executable on the guest as second argument
goto :error
)
cd "%~dp0"
set VBOX_FOUND=true
if not exist VirtualBox.exe set VBOX_FOUND=false
if not exist VBoxManage.exe set VBOX_FOUND=false
if not %VBOX_FOUND% == true (
cd "%PROGRAMFILES%\Oracle\VirtualBox\" 2>NUL
if errorlevel 1 cd "%PROGRAMFILES(X86)%\Oracle\VirtualBox\" 2>NUL
if errorlevel 1 cd "%PROGRAMFILES%\VirtualBox\" 2>NUL
if errorlevel 1 cd "%PROGRAMFILES(X86)%\VirtualBox\" 2>NUL
if not exist VirtualBox.exe (
set ERROR=VirtualBox.exe not found
goto :error
)
if not exist VBoxManage.exe (
set ERROR=VBoxManage.exe not found
goto :error
)
)
VBoxManage.exe list vms | findstr /C:"%~1" >NUL
if errorlevel 1 (
set ERROR=Unable to start VirtualBox VM "%~1": Invalid VM name or ID given
goto :error
)
VBoxManage.exe list runningvms | findstr /C:"%~1" >NUL
if not errorlevel 1 goto :vm_running
start VirtualBox.exe --startvm "%~1" --seamless
<NUL set /p =Starting VM
set WAIT=30
:until_vm_running
if %WAIT% == 0 (
echo Failure!
set ERROR=Unable to start VirtualBox VM "%~1": Timeout while waiting for startup
goto :error
)
VBoxManage.exe list runningvms | findstr /C:"%~1" >NUL
if errorlevel 1 (
<NUL set /p =.
set /a WAIT-=1
timeout /T 1 /NOBREAK >NUL
goto :until_vm_running
)
<NUL set /p =.
timeout /T 1 /NOBREAK >NUL
echo Success!
:vm_running
set CMD=VBoxManage.exe guestproperty get "%~1" "/VirtualBox/GuestInfo/OS/LoggedInUsers"
for /f "usebackq tokens=*" %%o in (`%CMD%`) do set LOGGEDIN=%%o
if not "%LOGGEDIN%" == "No value set!" if not "%LOGGEDIN:Value: =%" == "0" goto :logged_in
<NUL set /p =Awaiting login
set WAIT=120
:until_logged_in
if %WAIT% == 0 (
echo Failure!
set ERROR=Unable to start VirtualBox VM "%~1": Timeout while waiting for login
goto :error
)
VBoxManage.exe list runningvms | findstr /C:"%~1" >NUL
if errorlevel 1 (
echo Failure!
set ERROR=Unable to start VirtualBox VM "%~1": VM suddenly stopped running
goto :error
)
for /f "usebackq tokens=*" %%o in (`%CMD%`) do set LOGGEDIN=%%o
if "%LOGGEDIN%" == "No value set!" (
<NUL set /p =.
set /a WAIT-=1
timeout /T 1 /NOBREAK >NUL
goto :until_logged_in
) else (
if "%LOGGEDIN:Value: =%" == "0" (
<NUL set /p =.
set /a WAIT-=1
timeout /T 1 /NOBREAK >NUL
goto :until_logged_in
)
)
echo Success!
:logged_in
echo Running executable...
VBoxManage.exe guestcontrol "%~1" run --exe "%~2" --username "VirtualBox"
exit /B 0
:error
echo ERROR: %ERROR%
start cmd /c "echo VirtualBox: %ERROR% && pause"
exit /B 1
#!/bin/bash
# Runs applications inside a VirtualBox VM
# Version 1.2 (build 20190411)
#
# Copyright (C) 2016-2019 Daniel Rudolf <www.daniel-rudolf.de>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# See <http://www.gnu.org/licenses/> to receive a full-text-copy of
# the GNU General Public License.
set -e
APP_NAME="$(basename "$0")"
print_usage() {
echo "Usage:"
echo " $APP_NAME [--user USERNAME] VM_NAME EXEC [ARG]..."
}
gui_error() {
echo "$APP_NAME: $1" >&2
if [ -x "$(which zenity)" ]; then
zenity --error --title="$APP_NAME" --text="$1"
fi
}
is_vm_running() {
if ! VBoxManage list runningvms | grep -q "$VM"; then
return 1
fi
}
is_user_logged_in() {
LOGGED_IN="$(VBoxManage guestproperty get "$VM" "/VirtualBox/GuestInfo/OS/LoggedInUsers")"
if ! [[ "$LOGGED_IN" =~ ^Value:\ [0-9]+$ ]] || [ "$LOGGED_IN" == "Value: 0" ]; then
return 1
fi
}
VM=""
EXEC=""
ARGS=()
USER="VirtualBox"
while [ $# -gt 0 ]; do
if [ "$1" == "--user" ]; then
if [ -z "$2" ]; then
echo "$APP_NAME: Invalid VirtualBox VM guest username '$2'" >&2
exit 1
fi
USER="$2"
shift
elif [ -z "$VM" ]; then
VM="$1"
elif [ -z "$EXEC" ]; then
EXEC="$1"
else
ARGS+=( "$1" )
fi
shift
done
if [ -z "$VM" ]; then
echo "$APP_NAME: You must pass a VirtualBox VM name or ID as first argument" >&2
print_usage >&2
exit 1
fi
if [ -z "$EXEC" ]; then
echo "$APP_NAME: You must pass a path to an executable on the guest as second argument" >&2
print_usage >&2
exit 1
fi
if ! [ -x "$(which VBoxManage)" ]; then
echo "$APP_NAME: VBoxManage executable not found" >&2
exit 1
fi
if ! VBoxManage list vms | grep -q "$VM"; then
echo "$APP_NAME: Unable to start VirtualBox VM '$VM': Invalid VM name or ID given" >&2
exit 1
fi
if ! is_vm_running; then
VBoxManage startvm "$VM"
echo -n "Starting VM"
TIMEOUT=60
while ! is_vm_running; do
if [ $TIMEOUT -eq 0 ]; then
echo " Failed!"
gui_error "Unable to start VirtualBox VM '$VM': Timeout while waiting for startup"
exit 1
fi
echo -n "."
((--TIMEOUT))
sleep 0.5
done
if [ $TIMEOUT -gt 57 ]; then
seq $((60-$TIMEOUT-2)) 0 | xargs printf '.%.0s'
fi
echo " Success!"
fi
if ! is_user_logged_in; then
echo -n "Awaiting login"
TIMEOUT=240
LOGIN_TIMEOUT=6
RESET_LOGIN_TIMEOUT=$LOGIN_TIMEOUT
while true; do
if [ $TIMEOUT -eq 0 ]; then
echo " Failed!"
gui_error "Unable to start VirtualBox VM '$VM': Timeout while waiting for login"
exit 1
fi
if ! is_vm_running; then
echo " Failed!"
gui_error "Unable to start VirtualBox VM '$VM': VM suddenly stopped running"
exit 1
fi
if is_user_logged_in; then
if [ $LOGIN_TIMEOUT -eq 0 ]; then
echo " Success!"
break
fi
((--LOGIN_TIMEOUT))
else
LOGIN_TIMEOUT=$RESET_LOGIN_TIMEOUT
((--TIMEOUT))
fi
echo -n "."
sleep 0.5
done
fi
VBoxManage guestcontrol "$VM" run --username "$USER" --exe "$EXEC" "$EXEC" "${ARGS[@]}"
exit $?
@echo off & setlocal
REM Toggles a VirtualBox VM on/off
REM Version 1.1 (build 20160904)
REM
REM Copyright (C) 2016 Daniel Rudolf <www.daniel-rudolf.de>
REM
REM This program is free software: you can redistribute it and/or modify
REM it under the terms of the GNU General Public License as published by
REM the Free Software Foundation, version 3 of the License only.
REM
REM This program is distributed in the hope that it will be useful,
REM but WITHOUT ANY WARRANTY; without even the implied warranty of
REM MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
REM GNU General Public License for more details.
REM
REM See <http://www.gnu.org/licenses/> to receive a full-text-copy of
REM the GNU General Public License.
if "%~1" == "" (
set ERROR=You must pass a VirtualBox VM name or ID as first argument
goto :error
)
cd "%~dp0"
set VBOX_FOUND=true
if not exist VirtualBox.exe set VBOX_FOUND=false
if not exist VBoxManage.exe set VBOX_FOUND=false
if not %VBOX_FOUND% == true (
cd "%PROGRAMFILES%\Oracle\VirtualBox\" 2>NUL
if errorlevel 1 cd "%PROGRAMFILES(X86)%\Oracle\VirtualBox\" 2>NUL
if errorlevel 1 cd "%PROGRAMFILES%\VirtualBox\" 2>NUL
if errorlevel 1 cd "%PROGRAMFILES(X86)%\VirtualBox\" 2>NUL
if not exist VirtualBox.exe (
set ERROR=VirtualBox.exe not found
goto :error
)
if not exist VBoxManage.exe (
set ERROR=VBoxManage.exe not found
goto :error
)
)
VBoxManage.exe list vms | findstr /C:"%~1" >NUL
if errorlevel 1 (
set ERROR=Unable to start VirtualBox VM "%~1": Invalid VM name or ID given
goto :error
)
VBoxManage.exe list runningvms | findstr /C:"%~1" >NUL
if not errorlevel 1 (
echo Stopping VM...
VBoxManage.exe controlvm "%~1" savestate
exit /B 0
)
:loop_args
if not "%~1" == "" (
set ARGS=%ARGS% %1
shift
goto :loop_args
)
echo Starting VM...
start VirtualBox.exe --startvm %ARGS%
exit /B 0
:error
echo ERROR: %ERROR%
start cmd /c "echo VirtualBox: %ERROR% && pause"
exit /B 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment