Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
deb11:virtualboxvm [2022/09/24 11:28] – created Bernard Condraudeb11:virtualboxvm [2025/03/20 14:09] (current) – [Reboot (dead) vm] Bernard Condrau
Line 1: Line 1:
-====== VirtualBox ====== +====== VirtualBox VM ======
-===== Host ===== +
-  * I recommend to install VirtualBox from the Oracle repository, as you get the latest version of VirtualBox +
-  * Add "virtualbox.list" to /etc/apt/sources.list.d<code>sudo deb http://download.virtualbox.org/virtualbox/debian stretch contrib</code> +
-  * Add Oracle VirtualBox public key:<code>wget https://www.virtualbox.org/download/oracle_vbox_2016.asc +
-sudo apt-key add oracle_vbox_2016.asc +
-wget https://www.virtualbox.org/download/oracle_vbox.asc +
-sudo apt-key add oracle_vbox.asc</code> +
-  * Install Virtualbox:<code>sudo apt-get update +
-sudo apt-get install virtualbox-6.0</code> +
-  * Download the [[https://www.virtualbox.org/wiki/Downloads|VirtualBox Extension Pack]]. Double click the file from within the GUI to install. +
-  * [[https://www.virtualbox.org/manual/ch09.html#diskencryption|VirtualBox: Encryption of Disk Images]] +
-  * [[https://pgaskin.net/linux-tips/configuring-virtualbox-autostart/|Configuring VirtualBox autostart on Linux]] +
- +
-===== Guest =====+
 ===== Guest Additions ===== ===== Guest Additions =====
-  * To upgrade the Guest Additions after upgrading the host version, download the guest additions pack, close all virtual machines, and start virtualbox as root<code>sudo virtualbox</code> +  * To upgrade the Guest Additions after upgrading the VirtualBox version, download the guest additions pack, shut down all virtual machines then start VirtualBox as root<code>sudo virtualbox</code> 
-  * After installing the Guest Additions, close virtualbox and restart it as user +  * After installing the Guest Additions, close VirtualBox and restart it under your user account
- +
-===== Guest Additions for a Debian 9 guest on a Debian 7 host ===== +
-  * Add the following line to /etc/apt/sources.list.d:<code>deb http://ftp.debian.org/debian stretch-backports main contrib</code> +
-  * Install virtualbox-guest-dkms, kernel headers, and, optionally, virtualbox-guest-x11 (for the graphical guest utilities):<code>sudo apt update +
-sudo apt install virtualbox-guest-dkms virtualbox-guest-x11 linux-headers-$(uname -r)</code> +
-  * Reboot the guest system. +
-  * More info: [[https://unix.stackexchange.com/questions/286934/how-to-install-virtualbox-guest-additions-in-a-debian-virtual-machine|How to install VirtualBox Guest Additions in a Debian virtual machine]] +
- +
-===== Autostart and Autostop ===== +
-There are several options to autostart (and autostop) virtual machines. For a server environment with a Debian host the 1. option through rc.local is preferrable. +
-  - through rc.local +
-  - through X desktop +
-  - through VirtualBox command line +
- +
-==== through rc.local ==== +
-=== startup === +
-  - install [[deb10:rclocal|rc.local]] as a systemd service on your debian server. This emulates the //SysVinit// behavior of rc.local as Debian changed the default init system to //systemd// with Debian Jessie 8 +
-  - mount encrypted file systems through rc.local as explained in [[deb9:encrypted|Encrypted partitions/folders with auto-mount]] +
-  - start vms through rc.local headless. Since rc.local runs as root you need to use runuser to start the vm as the user who created the vm. You may also chose to put the startvm code into a script and add execution of the script to rc.local:<code>runuser -l <user> -c "vboxmanage startvm <vm-name> --type headless"</code> +
-=== shutdown === +
-  - put a symlink to the following script into /lib/systemd/system-shutdown:<code>runuser -l <user> -c "ssh <vm-name> 'sudo systemctl poweroff </dev/null &>/dev/null &'" +
-while [ "`runuser -l <user> -c 'vboxmanage list runningvms'`" != ""+
-do +
-    echo waiting for VMs to shutdown +
-    sleep 5 +
-done +
-</code> +
-  - allow <user> to execute command 'systemctl poweroff' in the vm (do not ssh into the VM as root - security!). Note that 'systemctl poweroff' is the systemd command, do not use //poweroff// or //shutdown// commands:<code>sudo visudo +
-<user> ALL=(root) NOPASSWD: /sbin/systemctl poweroff</code> +
-  - the scripts must be run as root so they can be executed through rc.local +
-  - also, the server must be shut down with //systemctl// rather than //reboot// or //shutdown// for this to work +
- +
-=== links === +
-  * Debian [[deb10:services|Services]] +
-==== through XFCE Desktop ==== +
-=== startup === +
-  * On the host, open **Settings -> Session and Startup**, then add a new task under **Application Autostart** with the following command:<code>VBoxManage startvm <VM name></code> +
-=== shutdown === +
-  * On the client, open **Settings -> Power Manager**, then set the desired action under "When power button is pressed" +
- +
-==== through VirtualBox command line ==== +
-  - Configuration<code>sudo vim /etc/default/virtualbox +
-VBOXAUTOSTART_DB=/etc/vbox +
-VBOXAUTOSTART_CONFIG=/etc/vbox/autostart.cfg +
- +
-sudo vim /etc/vbox/autostart.cfg +
-default_policy = deny +
-# Create an entry for each user allowed to use autostart +
-username = { +
-  allow = true +
-  startup_delay = 10 +
-+
- +
-sudo chgrp vboxusers /etc/vbox +
-sudo chmod 1775 /etc/vbox +
-sudo usermod -aG vboxusers username</code> +
-  - Choose VMs to automatically start and stop<code>VBoxManage setproperty autostartdbpath /etc/vbox # first time a user configures autostart only +
-VBoxManage modifyvm <vmname> --autostart-enabled <on|off> +
-VBoxManage modifyvm <vmname> --autostop-type <disabled|savestate|poweroff|acpishutdown></code> +
-  - Restart VirtualBox autostart service<code>sudo service vboxautostart-service restart</code> +
- +
-==== Start VM ==== +
-  vboxmanage startvm MachineName +
-==== Stop VM ==== +
-  vboxmanage controlvm MachineName acpipowerbutton +
-In xfce desktop, make sure the action on Power Button is set in Settings --> Power Manager +
- +
-==== Stop VM before powering down host through NUT ==== +
-Set different timeouts to the shutdown command used by NUT. For example, set +0 for the clients, and +1 for the host. +
- +
-==== Check running VMs ==== +
-One of the following commands: +
-  VBoxManage list runningvms +
-  vboxmanage list vms --long | grep -e "Name:" -e "State:" +
- +
-==== Links ==== +
-  * [[https://superuser.com/questions/622024/how-can-i-tell-if-a-virtual-machine-is-up-from-the-command-line|How can I tell if a virtual machine is up from the command line?]] +
-  * [[https://forums.virtualbox.org/viewtopic.php?f=7&t=61429|autostop-type]] +
-  * [[https://pgaskin.net/linux-tips/configuring-virtualbox-autostart/|Configuring VirtualBox autostart on Linux]] +
-  * [[http://techgenix.com/start-virtualbox-service/|How to start VirtualBox machines automatically as a service on Windows]] +
- +
-===== Update VirtualBox ===== +
-  * Make sure to update the same way you installed the previous version, either from the Oracle repository, or from the Debian backports +
-  * If you followed my guide, check the VirtualBox version.revision.build (e.g. 6.1.16) +
-  * Updating the build is done by running "apt upgrade"<code>audo apt update +
-sudo apt upgrade</code> +
-  * Updating to a new version or revision is done by running "apt install virtualbox-<version>.<revision>", e.g.<code>sudo apt update +
-sudo apt install virtualbox-6.1</code> +
- +
-==== Links ==== +
-  * [[https://www.linuxbabe.com/debian/install-virtualbox-debian-9-stretch|2 Ways to Install VirtualBox on Debian 9 Stretch]] +
-===== Manage VirtualBox through CLI ===== +
-==== Extension Pack ==== +
-  * Remove old extension pack<code>sudo vboxmanage extpack uninstall "Oracle VM VirtualBox Extension Pack"</code> +
-  * Download current extension pack from [[https://www.virtualbox.org/wiki/Downloads|Download VirtualBox]] and install<code>wget https://download.virtualbox.org/virtualbox/6.1.30/Oracle_VM_VirtualBox_Extension_Pack-6.1.30.vbox-extpack +
-sudo vboxmanage extpack install Oracle_VM_VirtualBox_Extension_Pack-6.1.30.vbox-extpack</code> +
- +
-==== Run VM ==== +
-  * <code>vboxmanage startvm "<vm_name>" --type headless</code>+
  
-==== Hold back upgrade ==== +===== Install Guest Additions on a Debian 11 guest operating system ===== 
-  <code>sudo apt-mark hold virtualbox-6.1</code> +  - The following steps need to be taken inside the VM after the operating system has been installed 
-  <code>sudo apt-mark unhold virtualbox-6.1</code> +  - Install the kernel headers, DKMS framework, and build tools<code>sudo apt update -y && sudo apt upgrade 
-==== Links ==== +sudo apt install dkms linux-headers-$(uname -r) build-essential</code> 
-  * [[http://underpop.online.fr/v/virtualbox/vboxmanage-extpack-virtualbox.html.gz|VBoxManage extpack]] +  - Attach the guest addition ISO to the VM and mount it<code>sudo mount /dev/cdrom /mnt/cd</code> 
-  * [[https://docs.oracle.com/en/virtualization/virtualbox/6.0/user/vboxmanage-extpack.html|Oracle VirtualBox Extension Pack]] +  - Install the VirtualBox guest additions<code>sudo sh /mnt/cd/VBoxLinuxAdditions.run</code> 
-  * [[https://linoxide.com/exclude-specific-package-apt-get-upgrade/|How to Exclude Specific Package from apt-get Upgrade]]+  Restart your virtual machine in order to reload the kernel modules<code>systemctl reboot -i</code> 
 +  - If you want the VM to automatically switch to full screen upon boot or login, toggle the **View option --> Auto-resize Guest Display** 
 +  - Uninstall the guest additions<code>sudo sh /mnt/VBoxLinuxAdditions.run uninstall</code> 
 +  Repeat steps 3-6 if the kernel or the VirtualBox version changes 
 +  * [[https://kifarunix.com/install-virtualbox-guest-additions-on-debian-11/|Install VirtualBox Guest Additions on Debian 11]]
  
 +===== Reboot (dead) vm =====
 +  - ''vboxmanage list runningvms''
 +  - ''vboxmanage controlvm <machine> poweroff''
 +  - ''vboxmanage startvm <machine> --type=headless''