Vagrant add local updated ubuntu version for libvirt provider
#!/bin/bash
# Make sure libguestfs-tools package is installed (libguestfs-tools-c on EL)
# Make sure current user is in kvm group: sudo usermod $USER -g kvm OR sudo chmod 0666 /dev/kvm
# Based on https://scotch.io/tutorials/how-to-create-a-vagrant-base-box-from-an-existing-one
box=ubuntu1804
provider=libvirt
if [[ $EUID > 0 ]]; then
echo "Please run as root/sudo"
exit 1
else
mkdir -p $box"_box"
cd $box"_box"
touch Vagrantfile
cat << EOF > Vagrantfile
Vagrant.configure("2") do |config|
config.ssh.insert_key = false
config.vm.box = "generic/ubuntu1804"
config.vm.box_check_update = true
config.vm.provider "libvirt" do |libvirt|
libvirt.memory = "2048"
libvirt.cpus = 2
end
config.vm.provision "shell", inline: <<-SHELL
sudo DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" dist-upgrade
DEBIAN_FRONTEND=noninteractive sudo apt install ansible -y
DEBIAN_FRONTEND=noninteractive sudo apt autoremove
sudo dd if=/dev/zero of=/EMPTY bs=1M
sudo rm -f /EMPTY
cat /dev/null > ~/.bash_history && history -c
SHELL
end
EOF
vagrant up --provider=$provider
vagrant halt
sudo chmod a+r "/var/lib/libvirt/images/"$box"_box_default.img"
vagrant package --output $box".box"
vagrant box add $box $box".box" --force
vagrant destroy --force
ls -lh $box".box"
cd ..
rm -rf $box"_box"
fi