Virtualbox

From My Mnemonic Rhyme
Revision as of 14:43, 24 April 2025 by Weiss (talk | contribs)
Jump to navigation Jump to search

Howto: Virtualbox unter Debian compilieren

addons for debian

sudo apt install linux-headers-$(uname -r) build-essential dkms
sudo /sbin/rcvboxadd quicksetup all

Fix Windows7 screen resolution

  • Make sure that 2D- and 3D-acceleration is disabled.
  • Execute following code on the host while the VM is off:
VBoxManage setextradata global GUI/MaxGuestResolution any 

VM manuell registrieren

VBoxManage registervm Machines/gentoo/gentoo.xml

Starten einer VM ohne GUI (headless)

vboxmanage startvm vmname -type headless

systemd service

[Unit]
Description=VBox Virtual Machine %i Service
Requires=systemd-modules-load.service
After=systemd-modules-load.service vboxdrv.service

[Service]
User=vboxheadless
Group=vboxusers
ExecStart=/usr/bin/VBoxHeadless -s %i
ExecStop=/usr/bin/VBoxManage controlvm %i acpipowerbutton
KillMode=process
KillSignal=SIGWINCH
TimeoutStopSec=40

[Install]
WantedBy=multi-user.target

SysV init script

/etc/init.d/virtualbox-custom

#!/bin/sh
### BEGIN INIT INFO
# Provides:          Curstom vbox VM Handling
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts / Stops VMs
# Description:       Starts / Stops VMs to avoid system hang...
### END INIT INFO

# Author: Tobias Weiß <tobias@tobias-weiss.org>

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="custom vbox vm handling"
NAME=virtualbox-custom
DAEMON=/usr/bin/vboxmanage
SCRIPTNAME=/etc/init.d/$NAME

#VMs to start
VM_LIST="gentoo"

#Start VMs as the following user
VM_USER="homaar"

# Exit if the package is not installed
[ -x "$DAEMON" ] || echo "Daemon not found" || exit 127

# Read configuration variable file if it is present
#[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
    for VM in $VM_LIST 
	            do
			su $VM_USER -c "$DAEMON startvm $VM --type=headless"
		    done
}

#
# Function that stops the daemon/service
#
do_stop()
{
    WAITCOUNT=0
    VM_RUNNING=`vboxmanage list runningvms | cut -d" " -f1 | tr -d '\"'`

    while [ "$VM_RUNNING" != "" ]
	do
	    for VM in $VM_RUNNING
		do
		    if [ $WAITCOUNT -eq 3 ]
		    then
			vboxmanage controlvm $VM poweroff
		    else
		        vboxmanage controlvm $VM acpipowerbutton
		    fi
		done
		sleep 5
		VM_RUNNING=`vboxmanage list runningvms | cut -d" " -f1 | tr -d '\"'`
		WAITCOUNT=` expr $WAITCOUNT + 1`
	done
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
	#
	# If the daemon can reload its configuration without
	# restarting (for example, when it is sent a SIGHUP),
	# then implement that here.
	#
	return 0
}

case "$1" in
  start)
	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
	do_start
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  stop)
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
	do_stop
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  status)
	status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
	;;
  #reload|force-reload)
	#
	# If do_reload() is not implemented then leave this commented out
	# and leave 'force-reload' as an alias for 'restart'.
	#
	#log_daemon_msg "Reloading $DESC" "$NAME"
	#do_reload
	#log_end_msg $?
	#;;
  restart|force-reload)
	#
	# If the "reload" option is implemented then remove the
	# 'force-reload' alias
	#
	log_daemon_msg "Restarting $DESC" "$NAME"
	do_stop
	case "$?" in
	  0|1)
		do_start
		case "$?" in
			0) log_end_msg 0 ;;
			1) log_end_msg 1 ;; # Old process is still running
			*) log_end_msg 1 ;; # Failed to start
		esac
		;;
	  *)
		# Failed to stop
		log_end_msg 1
		;;
	esac
	;;
  *)
	#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
	exit 3
	;;
esac