-
-
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 |
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.
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,
Work for me. Thanks.
Thanks for your support .
after long struggle finally its working for me.
Thanks.
vagrant ssh -c "sudo yum clean all && sudo rm -rf /var/cache/yum"
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"
@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
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.
Thanks! Just FYI, I had to do a restart my box after updating kernel-devel to make it work.