Skip to content

Instantly share code, notes, and snippets.

@PaulNeumann
Last active May 23, 2021 00:28
Show Gist options
  • Save PaulNeumann/d0877356ef49017e876ee601d0cac457 to your computer and use it in GitHub Desktop.
Save PaulNeumann/d0877356ef49017e876ee601d0cac457 to your computer and use it in GitHub Desktop.
How to install VirtualBox Guest Additions for Linux from the command line

How to install/upgrade/downgrade VirtualBox Guest Additions for Linux from the command line

The basic steps used are from https://www.vagrantup.com/docs/virtualbox/boxes.html.

Run the script below to install, upgrade or downgrade the VirtualBox Guest Additions for Linux. The script will install the latest version by default, but also allows specifying a different version as an argument. Note: The script requires the bzip2 and wget packages.

If the guest already has a shared folder with the host, one can also copy VBoxGuestAdditions.iso from the VirtualBox installation folder (usually C:\Program Files\Oracle\VirtualBox on Windows) instead of downloading the Guest Additions using wget. However, mounting the .iso from a shared folder probably won't work--the file needs to be copied to the guest's filesystem.

#!/usr/bin/env bash
#
# Install, upgrade or downgrade VirtualBox Guest Additions
#
# Installs latest version unless a different version is
# specified as an argument
#
# Ensure bzip2 and wget are installed before running
#
# Optional argument:
# $1 = VirtualBox Guest Additions version
set -Eeuo pipefail
trap cleanup EXIT
# initialize variables
base_url='https://download.virtualbox.org/virtualbox'
dnld_dir=$(mktemp -d)
mount_dir=$(mktemp -d)
ga_module=''
install_version=''
running_version=''
# cleanup on exit
cleanup () {
local iso_mounted
# grep exit status is 1 if no match is found
iso_mounted=$(mount -l | grep "${mount_dir}") || [[ $? -eq 1 ]]
if [[ -n "${iso_mounted}" ]]; then
sudo umount "${mount_dir}"
fi
rm -rf "${dnld_dir}" "${mount_dir}"
unset base_url dnld_dir mount_dir ga_module install_version running_version
}
# determine Guest Additions version to install
if [[ $# -ne 1 ]]
then
wget -P "${dnld_dir}" "${base_url}/LATEST.TXT"
install_version=$(cat "${dnld_dir}/LATEST.TXT")
else
install_version=$1
fi
# determine running Guest Additions version, if any
# grep exit status is 1 if no match is found
ga_module=$(lsmod | grep vboxguest) || [[ $? -eq 1 ]]
if [[ -n "${ga_module}" ]]; then
running_version=$(modinfo vboxguest -F version | awk '{print $1}')
fi
if [[ "${install_version}" != "${running_version}" ]]
then
wget -P "${dnld_dir}" \
"${base_url}/${install_version}/VBoxGuestAdditions_${install_version}.iso"
sudo mount -o loop,ro \
"${dnld_dir}/VBoxGuestAdditions_${install_version}.iso" "${mount_dir}"
# VBoxLinuxAdditions.run exit status is 2 when
# running modules prevent new ones from loading
sudo sh "${mount_dir}/VBoxLinuxAdditions.run" || [[ $? -eq 2 ]]
else
echo "VirtualBox Guest Additions ${install_version} already installed"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment