Script to Install Virtual Guest Additions from CD

Share your own howto's etc. Not for support questions!

Script to Install Virtual Guest Additions from CD

Postby makh » 2017-11-27 09:49

Objective:
To install the virtualbox guest additions in the Debian, as Guest OS, inside Virtualbox, after inserting the guest additions CD.

:idea: Reason to create a script:
It makes using linux faster.


:arrow: Creating the script:
Code: Select all
# nano /root/myvboxguestadditions.sh


Caution:
Code: Select all
It creates and uses the path "/mnt/cdrom", please edit the script if you have something else mounted on directly on /mnt
Theres no responsibility for any damage caused, in any way by this script.


Make it executable:
Code: Select all
# chmod a+x myvboxguestadditions.sh


How to run as root:
Code: Select all
# ./myvboxguestadditions.sh


For other details:
Please refer to the Official Documentation of Virtualbox.


Running OK on other Linux (right now):
Code: Select all
1. Devuan
2. Linux Mint
3. Open suse Tumbleweed
4. Ubuntu


The bash script:
# cat myvboxguestadditions.sh
Code: Select all
#!/bin/bash
#
# Author: Khurram M. <makhmct@hotmail.com>
# Script Version: 1.0
# Script type: Opensource
# Licensed: GPL v3
# Created on: 24 November 2017
# Last edited: 27 November 2017
#
# Tested to work on: VirtualBox 5.2, 64-bit
# Host: Arch Linux, kernel ver 4.13.12-1
# Guests: Debian 9.2, Devuan Ascii, Linux Mint 18.2, OpenSuse Tumbleweed 4.14, 
# Not Tested on Guest OS: MacOS, Android, BSD, others
#

echo 'Checking if run as Root...'
if [[ $EUID -ne 0 ]]
   then
      echo "[ERROR] This script must be run as root."
      echo "Exiting ..."
      exit 1
   else
      echo "[OK] This script is run by the root."
fi

echo

echo 'Checking mount point...'
FILE1=/mnt/cdrom
if [ -f $FILE1 ]
   then
      echo "File $FILE does not exist."
      echo 'Creating the Mount point...'
      mkdir -pv /mnt/cdrom && echo '[OK] Mount point created ...'
   else
      echo '[OK] Mount point exists...'
fi

echo

echo 'Mounting the disk...'
mount -v /dev/sr0 /mnt/cdrom && echo '[OK] Disk Mounted ...'

echo

echo 'Installing the guest addition modules...'

echo

sh /mnt/cdrom/VBoxLinuxAdditions.run && echo '[OK] Guest additions installed ...'

echo

echo 'Umounting disk...'
umount -v /dev/sr0 && echo '[OK] Umount of disk is successful'

echo '[OK] Exiting the script ... Have a nice day! :) ... '

exit 1


Do give a good suggestion, if you have, I may put changes to this script. But I dont intend to add unneccessary complexity(s).

Hope you enjoyed this script.

Thankyou
:)

Edit:
1. Works fine currently, on Debian 10/Buster. Hope there are no issues at your end, too. :)

2. See my 4th post, below for Debian only, where it is added to cross check and add dependencies. :arrow:

3. Tested Ok for Ubuntu also.

4. This does not applies to Desktop Linux: For CLI/TUI Linux (ie you are using no X windows on this machine), edit the code to:
Code: Select all
sh /mnt/cdrom/VBoxLinuxAdditions.run --noX11
Last edited by makh on 2017-12-09 06:20, edited 2 times in total.
HP Probook 440 G2: Arch, Debian Stable
Server: none
Past: Debian, Centos, Ubuntu, Opensuse
GUI: Openbox, Cinnamon
Chroot: Debian, Ubuntu, Fedora
VM: Devuan

Employing the best:
Arabic
Debian
Homeopathic

For new: Try Linux Mint
User avatar
makh
 
Posts: 565
Joined: 2011-10-09 09:16

Re: Script to Install Virtual Guest Additions from CD

Postby Ardouos » 2017-11-27 11:51

makh wrote:Do give a good suggestion, if you have, I may put changes to this script. But I dont intend to add unneccessary complexity(s).

If I wanted to run the script, I would want to run it as soon as I have installed a minimal system. The problem is that virtualbox guest additions requires headers and various other packages.

It'll be great if you could add a line for apt to check and install all the necessary dependencies.
User avatar
Ardouos
 
Posts: 953
Joined: 2013-11-03 00:30
Location: Elicoor II

Re: Script to Install Virtual Guest Additions from CD

Postby Dai_trying » 2017-11-27 12:13

Ardouos wrote:It'll be great if you could add a line for apt to check and install all the necessary dependencies.


I was thinking something along the same lines, and if I recall, the package build-essential brings in all that is usually required but it has been a while since I last installed a VM so there could possibly be other requirements.
Dai_trying
 
Posts: 337
Joined: 2016-01-07 12:25

Re: Script to Install Virtual Guest Additions from CD

Postby Head_on_a_Stick » 2017-11-27 19:20

makh wrote:Do give a good suggestion, if you have

Stop using Virtualbox and switch to KVM (with QEMU) instead:

https://wiki.debian.org/KVM

No need to install any silly "guest additions" with the open-source solution and in my experience it's even quicker than VB.

EDIT: simpler guide here:

https://forums.bunsenlabs.org/viewtopic.php?id=1546
"Only the mediocre are always at their best." — Jean Giraudoux
User avatar
Head_on_a_Stick
 
Posts: 6784
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: Script to Install Virtual Guest Additions from CD

Postby stevepusser » 2017-11-28 01:48

Dai_trying wrote:
Ardouos wrote:It'll be great if you could add a line for apt to check and install all the necessary dependencies.


I was thinking something along the same lines, and if I recall, the package build-essential brings in all that is usually required but it has been a while since I last installed a VM so there could possibly be other requirements.


build-essential only brings in compilers, so the kernel headers are another matter. Perhaps you could take a look at how module-assistant handles the same job with the "prepare" target, as in "m-a prepare".
The MX Linux repositories: Backports galore! If we don't have something, just ask and we'll try--we like challenges. New packages: Krita 3.3.2.1, Pale Moon 27.6.0, Audacity 2.2.0, mpv 0.27.0, Corebird 1.7.1, Firefox 57.0, SMPlayer 17.11.2
User avatar
stevepusser
 
Posts: 8938
Joined: 2009-10-06 05:53

Re: Script to Install Virtual Guest Additions from CD

Postby makh » 2017-11-28 06:43

Hi

@Ardouos: It was just a small fun project.If I am using a few scripts, then that is for a specific purpose. Your point is definitely doable, but then I have to modify for Opensuse and many others, in case I am testing, that would be complicated, as these server managing guys:
https://unix.stackexchange.com/questions/6345/how-can-i-get-distribution-name-and-version-number-in-a-simple-shell-scriptseem to have a hard time.
2. then the complication of non-debian and non-rpm based and others, the packages differ. :?
3. But an option can be added for Debian/Devuan/Mint only... I will try if I get time. :idea:

@Dai_trying: Thankyou. :)

@Head_on_a_Stick: Sir! you are asking a very tough question... it was my first VM experience, as I recall... :wink: One cannot forget his first love... :roll: :lol:
My first DE were KDE of opensuse and Gnome of Ubuntu. I didnt used opensuse for long, as "KDE of opensuse" didnt run on next upgrade on my old computer, back then... so I became the Gnome Guy, until gnome startled me out... today I use either of Openbox, xfce or Cinnamon... :cry:
...
Personally, I am more impressed by XEN. I read somewhere, it allows the use of the drivers of Guest OS to access the hardware, ... it seems par to Virtualbox. But for a user of my level, ... to run a VM is pretty much faster... with a fully featured GUI experience. But then still for some tasks, the CLI based programs are par to GUI
onces. But still some CLI programs arent easy to use. :wink:

@Stevepusser: Thankyou.

Thankyou

Edit: corrections...
HP Probook 440 G2: Arch, Debian Stable
Server: none
Past: Debian, Centos, Ubuntu, Opensuse
GUI: Openbox, Cinnamon
Chroot: Debian, Ubuntu, Fedora
VM: Devuan

Employing the best:
Arabic
Debian
Homeopathic

For new: Try Linux Mint
User avatar
makh
 
Posts: 565
Joined: 2011-10-09 09:16

Re: Script to Install Virtual Guest Additions from CD

Postby Head_on_a_Stick » 2017-11-28 18:07

makh wrote:But for a user of my level, ... to run a VM is pretty much faster... with a fully featured GUI experience. But then still for some tasks, the CLI based programs are par to GUI
onces. But still some CLI programs arent easy to use

My guide is command-line based but only because that it what I prefer.

KVM can also be used with virt-manager — this provides a nice, friendly, point-and-click GUI interface that is quite similar to VirtualBox.

makh wrote:XEN

Xen is a full hypervisor solution and is _much_ more difficult to configure than QEMU/KVM and isn't really intended for simple virtualisation tasks in the same way that VB & virt-manager are.
"Only the mediocre are always at their best." — Jean Giraudoux
User avatar
Head_on_a_Stick
 
Posts: 6784
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: Script to Install Virtual Guest Additions from CD

Postby makh » 2017-11-28 21:01

Head_on_a_Stick wrote:
makh wrote:XEN

Xen is a full hypervisor solution ...

Hi,
Thats the beauty of Xen over others. But, I didnt search for similar projects, maybe exist or not.
... :)
HP Probook 440 G2: Arch, Debian Stable
Server: none
Past: Debian, Centos, Ubuntu, Opensuse
GUI: Openbox, Cinnamon
Chroot: Debian, Ubuntu, Fedora
VM: Devuan

Employing the best:
Arabic
Debian
Homeopathic

For new: Try Linux Mint
User avatar
makh
 
Posts: 565
Joined: 2011-10-09 09:16

Re: Script to Install Virtual Guest Additions from CD

Postby makh » 2017-11-28 21:49

@Ardouos

Hi,
I rechecked it. In the current version of Virtualbox, the guest additions program itself, informs the user for any missing dependencies. I just ran slax to confirm that. So there doesnt seem to be any need to add that part.

Thankyou

Edit 1: Adding for the dependency checking... for Debian Stretch only:

I checked and found that the only dkms was an additional dependency for the Stretch, for the xfce install.

Taking guidance from:
https://stackoverflow.com/questions/1298066/check-if-a-package-is-installed-and-then-install-it-if-its-not

You just simply add the following lines at the start of the script:

if [dpkg-query -W -f='${Status}' dkms 2>/dev/null | grep -c "ok installed"] -eq 0
then
apt update
apt install dkms
fi


It should work for any frest debian stretch/v9 xfce4 installation, I didnt test it on a fresh install. Hope this helps you. Thankyou. :)
Last edited by makh on 2017-11-29 19:49, edited 1 time in total.
HP Probook 440 G2: Arch, Debian Stable
Server: none
Past: Debian, Centos, Ubuntu, Opensuse
GUI: Openbox, Cinnamon
Chroot: Debian, Ubuntu, Fedora
VM: Devuan

Employing the best:
Arabic
Debian
Homeopathic

For new: Try Linux Mint
User avatar
makh
 
Posts: 565
Joined: 2011-10-09 09:16

Re: Script to Install Virtual Guest Additions from CD

Postby Dai_trying » 2017-11-29 10:55

stevepusser wrote:build-essential only brings in compilers, so the kernel headers are another matter. Perhaps you could take a look at how module-assistant handles the same job with the "prepare" target, as in "m-a prepare".


Yes it would likely be a better choice, but after checking I noticed header files were installed during my OS installation (using Xfce Live-cd) so it might be an unnecessary concern, although I don't know if this is the case for all install mediums.
Dai_trying
 
Posts: 337
Joined: 2016-01-07 12:25

Re: Script to Install Virtual Guest Additions from CD

Postby makh » 2017-12-01 12:50

Hi

With repsect to a minimal install, using Debian Stretch Xfce installation CD (while not selecting the xfce and the desktop components), which I just tried, one needs to install these packages:
    gcc
    make
    perl
    header files

For any one new a one liner:
Code: Select all
apt install gcc make perl  linux-headers-$(uname -r|sed 's,[^-]*-[^-]*-,,')



CAUTION: This new code is not yet tested: I will confirm back as I get time to run it.

The above script can be changed to, (for Debian Stretch only):
Code: Select all
#!/bin/bash
#
# Author: Khurram M. <makhmct@hotmail.com>
# Script Version: 1.1
# Script type: Opensource, Debian 9/Stretch
# Licensed: GPL v3
# Created on: 24 November 2017
# Last edited: 01 December 2017
#
# Tested to work on: VirtualBox 5.2, 64-bit
# Host: Arch Linux, kernel ver 4.13.12-1
# Guests: Debian 9.2,
#

echo 'Checking if run as Root...'
if [[ $EUID -ne 0 ]]
   then
      echo "[ERROR] This script must be run as root."
      echo "Exiting ..."
      exit 1
   else
      echo "[OK] This script is run by the root."
fi

echo "Testing the dependencies..."

if [dpkg-query -W -f='${Status}' dkms 2>/dev/null | grep -c "ok installed"] -eq 0; then apt update; apt install dkms; fi
if [dpkg-query -W -f='${Status}' gcc 2>/dev/null | grep -c "ok installed"] -eq 0; then apt update; apt install gcc; fi
if [dpkg-query -W -f='${Status}' make 2>/dev/null | grep -c "ok installed"] -eq 0; then apt update; apt install make; fi
if [dpkg-query -W -f='${Status}' perl 2>/dev/null | grep -c "ok installed"] -eq 0; then apt update; apt install dkms; fi
if [dpkg-query -W -f='${Status}' linux-headers-$(uname -r|sed 's,[^-]*-[^-]*-,,') 2>/dev/null | grep -c "ok installed"] -eq 0; then apt update; apt install linux-headers-$(uname -r|sed 's,[^-]*-[^-]*-,,'); fi

echo "[OK] All the dependencies are met. Good to go."

echo

echo 'Checking mount point...'
FILE1=/mnt/cdrom
if [ -f $FILE1 ]
   then
      echo "File $FILE does not exist."
      echo 'Creating the Mount point...'
      mkdir -pv /mnt/cdrom && echo '[OK] Mount point created ...'
   else
      echo '[OK] Mount point exists...'
fi

echo

echo 'Mounting the disk...'
mount -v /dev/sr0 /mnt/cdrom && echo '[OK] Disk Mounted ...'

echo

echo 'Installing the guest addition modules...'

echo

sh /mnt/cdrom/VBoxLinuxAdditions.run && echo '[OK] Guest additions installed ...'

echo

echo 'Umounting disk...'
umount -v /dev/sr0 && echo '[OK] Umount of disk is successful'

echo '[OK] Exiting the script ... Have a nice day! :) ... '

exit 1


Thankyou
HP Probook 440 G2: Arch, Debian Stable
Server: none
Past: Debian, Centos, Ubuntu, Opensuse
GUI: Openbox, Cinnamon
Chroot: Debian, Ubuntu, Fedora
VM: Devuan

Employing the best:
Arabic
Debian
Homeopathic

For new: Try Linux Mint
User avatar
makh
 
Posts: 565
Joined: 2011-10-09 09:16


Return to Docs, Howtos, Tips & Tricks

Who is online

Users browsing this forum: No registered users and 6 guests

fashionable