Skip to content

Instantly share code, notes, and snippets.

@jonasschultzmblox
Last active May 12, 2023 07:59
Show Gist options
  • Save jonasschultzmblox/f15fe3c10769d5f269635a54394c84d4 to your computer and use it in GitHub Desktop.
Save jonasschultzmblox/f15fe3c10769d5f269635a54394c84d4 to your computer and use it in GitHub Desktop.
#!/bin/bash
#Heavily inspired by clivewalkden/centos-7-package.sh
# ( https://gist.github.com/clivewalkden/b4df0074fc3a84f5bc0a39dc4b344c57 )
#However, this one was tested... 2017-JAN-09
vagrant init centos/7
vagrant up
vagrant ssh -c "sudo yum -y update"
vagrant ssh -c "sudo yum -y install wget nano kernel-devel gcc"
vagrant ssh -c "sudo cd /opt && sudo wget http://download.virtualbox.org/virtualbox/5.1.12/VBoxGuestAdditions_5.1.12.iso -O /opt/VBGAdd.iso"
vagrant ssh -c "sudo mount /opt/VBGAdd.iso -o loop /mnt"
vagrant ssh -c "sudo sh /mnt/VBoxLinuxAdditions.run --nox11"
vagrant ssh -c "sudo umount /mnt"
vagrant ssh -c "sudo rm /opt/VBGAdd.iso"
#Check that we can halt and boot
vagrant halt
vagrant up
#Halt again and package
vagrant halt
vagrant package
#And finally, clean up
mv package.box centos7vb.box
rm Vagrantfile
@somedrunkbum
Copy link

Thanks! Just FYI, I had to do a restart my box after updating kernel-devel to make it work.

@lbriais
Copy link

lbriais commented Mar 10, 2017

The problem is that if kernel version has been changed between the base image and now, if you just yum install kernel-devel you will have a discrepancy between the kernel and the tools, headers etc... and therefore running VBoxLinuxAdditions.run will fail.
To avoid that you have two solutions:

Either you yum -y update as you did. It will bump the kernel version to the latest one and then when installing kernel-devel you will have the same aligned version. But to compile VBox additions you will need to reboot first to boot on the newly installed kernel image! Which is painful if you want to automate the process...

The second solution is not as straightforward but has the strong benefit not to require any reboot:

ARCH=`uname -r |cut -f7 -d.`
KVER=`uname -r |cut -f1-6 -d.`
yum -y install kernel-devel-${KVER}.${ARCH}

By doing that you will install the kernel devel counterpart of the kernel image currently installed (of course then you should not update!). And thus the build of the VBox additions works as expected...

Even if we could think updating is good, I think in this case that keeping the kernel at the level intended by the image is not a bad pattern as when the new version of the image is released, you could automate the rebuild while not breaking the contract of the image. Let me know if you think I'm wrong.

@eRadical
Copy link

eRadical commented Mar 29, 2017

Hi,

Can you please turn this into a repo... so I can fork it & do proper pull requests?
We can do autodetection of latest VBoxGuestAdditions.

Regards,

@trialforce
Copy link

Work for me. Thanks.

@Himavanth123
Copy link

Thanks for your support .
after long struggle finally its working for me.

@bibigon812
Copy link

Thanks.

vagrant ssh -c "sudo yum clean all && sudo rm -rf /var/cache/yum"

@ros-financial-com
Copy link

Very nice.
You could add a tiny bit of automation into it like so:
#!/bin/bash
# Get current installed version of VirtualBox VERSION=$(rpm -q --queryformat %{VERSION} VirtualBox-6.0 | cut -d '_' -f 1)
and replacing your line
vagrant ssh -c "sudo cd /opt && sudo wget http://download.virtualbox.org/virtualbox/5.1.12/VBoxGuestAdditions_5.1.12.iso -O /opt/VBGAdd.iso"
by
vagrant ssh -c "sudo cd /opt && sudo wget http://download.virtualbox.org/virtualbox/${VERSION}/VBoxGuestAdditions_${VERSION}.iso -O /opt/VBGAdd.iso"

@boktai1000
Copy link

boktai1000 commented Mar 27, 2019

@lbriais thank you for your comment and working out a solution to get a matching header/etc. That works great and I've implemented it successfully in my own script.

@ros-financial-com I added automation to my script by using a variable in the beginning and setting it equal to $1 so that way whatever parameter I append after the script execution, it will go grab that version and install it. I do this because some systems may be on an older version and you will want to match it against your installed version IMO, as well you may not have Guest Additions installed yet on the machine so you may not be able to query an installed version.

Example: Running ./vbox.sh 6.0.4

VBOX_VERSION=$1
curl -O http://download.virtualbox.org/virtualbox/$VBOX_VERSION/VBoxGuestAdditions_$VBOX_VERSION.iso

Would grab and download

http://download.virtualbox.org/virtualbox/6.0.4/VBoxGuestAdditions_6.0.4.iso

@Zeebrow
Copy link

Zeebrow commented Jun 9, 2019

FYI - if you need the latest version of the guest additions is available in the repo:

sudo yum -y install wget
cd /opt/
sudo wget http://download.virtualbox.org/virtualbox/LATEST.TXT
sudo wget -c http://download.virtualbox.org/virtualbox/$(cat LATEST.TXT)/VBoxGuestAdditions_$(cat LATEST.TXT).iso

Above might not be the best syntax, I dunno. Anyways, IMO, it feels a bit cleaner to grab the version info from the same repo that we're wget-ing from.There's also a LATEST-STABLE.TXT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment