A little bit more automation: with each new kernel version that comes out, I need to update my base Vagrant image, and as a result, update the VirtualBox Guest Additions as well.
Here is my Vagrantfile to control the process. The VM is started three times, of which only two are really necessary (the first two). These are the steps:
vagrant up --provision-with newkernel
SYNC_DISABLED=true vagrant reload --provision-with newadditions
vagrant reload
A description of the steps:
- Step #1: the first 'vagrant up' step requires a synced folder to obtain the new kernel version package and installs it
- Step #2: the 'vagrant reload' step will restart the vm, but since the GuestAdditions won't work with the new kernel, a synced_folder can't be defined, so needs to be disabled during the process of rebuilding the GuestAdditions
- Step #3: the second 'vagrant reload' restarts to allow one to inspect and confirm proper operation of the vm
The file itself (which is used to upgrade a Debian Stretch version with 4.7 kernel to kernel 4.8.6 with VirtualBox 5.1.8):
# -*- mode: ruby -*-
# vi: set ft=ruby :
# To run this file for upgrade images and GuestAdditions:
#
# vagrant up --provision-with newkernel
# SYNC_DISABLED=true vagrant reload --provision-with newadditions
# vagrant reload --provision-with fixkey
# # perform packaging step, then..
# vagrant destroy
# this is a three step process
# #1 the 'vagrant up' step requires a synced folder to obtain the new kernel version package
# and installs it
# #2 the 'vagrant reload' step will restart the vm, but since the GuestAdditions won't
# work with the new kernel, a synced_folder can't be defined, so needs to be disabled
# during the process of rebuilding the GuestAdditions
# #3 the final 'vagrant reload' restarts into the vm to enable manual configuration
# of updated kernel and GuestAdditions, confirm synced folders available,
# and resets key back to default
Vagrant.configure("2") do |config|
config.vm.box = "stretch"
config.vm.box_url = "file://~/vagrant/boxes/stretch.box"
config.vm.hostname = "stretch486"
sync=ENV['SYNC_DISABLED']
if "" == sync then
sync = "false"
end
config.vm.synced_folder ".", "/vagrant", disabled: sync
config.vm.synced_folder "../packages", "/vagrant_packages", disabled: sync
config.vm.provision "newkernel", type: "shell" do |s|
s.name = "newkernel"
s.inline = <<-SHELL
dpkg -i /vagrant_packages/linux-image-4.8.6-rpb_1.0_amd64.deb
dpkg -i /vagrant_packages/linux-headers-4.8.6-rpb_1.0_amd64.deb
echo "done."
SHELL
end
config.vm.provision "newadditions", type: "shell" do |s|
s.name = "newadditions"
s.inline = <<-SHELL
echo "updating package list ..."
apt-get update
echo "installing packages ..."
PACKAGES=$(apt-get -y install build-essential | awk \
'BEGIN { flag=0; list="" } \
/^[^ ]/ { flag=0 } \
/NEW packages will be installed/ { flag=1 } \
/ / { if (1 == flag) list=list$0 } \
END { print list }' \
)
echo "Installed packages: ${PACKAGES}"
echo "obtaining guest additions iso ..."
VBOXVER="5.1.8"
wget -q\
http://download.virtualbox.org/virtualbox/${VBOXVER}/VBoxGuestAdditions_${VBOXVER}.iso
mkdir /media/VBoxGuestAdditions
mount -o loop,ro VBoxGuestAdditions_${VBOXVER}.iso /media/VBoxGuestAdditions
echo "Building additions ..."
KERN_DIR=/usr/src/linux-headers-$(uname -r)/ \
sh /media/VBoxGuestAdditions/VBoxLinuxAdditions.run
umount /media/VBoxGuestAdditions
rmdir /media/VBoxGuestAdditions
rm VBoxGuestAdditions_${VBOXVER}.iso
echo "Removing packages..."
apt-get -y remove ${PACKAGES}
apt-get -y remove linux-headers-4.8.6
apt-get -y remove linux-image-4.7.0-1
echo "cleaning up package residue..."
apt-get autoremove
apt-get autoclean
apt-get purge
apt-get clean
rm /var/lib/apt/lists/*
echo "Zeroing out dead space ..."
dd if=/dev/zero of=/EMPTY bs=1M
rm -f /EMPTY
echo "results..."
ls -alt /usr/src
df -h
echo "done."
SHELL
end
config.vm.provision "fixkey", type: "shell" do |s|
s.name = "fixkey"
s.inline = <<-SHELL
echo "Installing default key ...."
pushd /home/vagrant/.ssh
rm authorized_keys
wget -q --no-check-certificate \
'https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub' \
-O authorized_keys
chmod 600 authorized_keys
chown -R vagrant.vagrant /home/vagrant/.ssh
popd
SHELL
end
config.vm.provider "virtualbox" do |v|
v.name = "stretch486"
#v.cpus = 3
end
end