This article is aimed to describe very fast approach of creating Vagrant Libvirt boxes using existing ones as a base. The described solution is applicable for following use-cases:
- you need box with slightly different software dependencies (different packages/versions);
- you need different configuration of existing software;
- you need add some files to box.
Due to described process simplicity it has one big limitation — ssh password of vagrant use is hard-coded. This means that every box user will need to set it explicitly in Vagrantfile. Such solution may be inflexible for a number of use-cases and obviously contain security vulnerability.
Described approach target is only to modify existing box quickly and it does not cover deep libvirt configuration issues. For more complex boxes it is better to use https://www.packer.io/.
- Clone https://github.com/vagrant-libvirt/vagrant-libvirt .
- Ensure that you have enough free space on the host machine, you may need atleast three or a four times more space that <needed_box_name> file takes.
- Do vagrant box add <needed_box_name>.
# -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure(2) do |config| config.vm.box = "<needed_box_name>" config.vm.synced_folder "./", "/vagrant", type: "rsync", rsync__auto: true config.vm.provider "libvirt" do |libvirt| libvirt.driver = "qemu" end config.vm.provision "shell", inline: <<-EOC # Important part - hardcoding vagrant password which will allow us to connect to new box without messing with keys echo "vagrant\nvagrant"| passwd vagrant # Do needed environment settings sudo aptitude install ruby gcc make EOC end
- Call vagrant up —provider=libvirt .
- Wait for virtual machine to complete provision.
- Copy any files to machine or do any other manual actions which should persist in new box.
cd <vagrant_libvirt_dir> sudo tools/create_box.sh /var/lib/libvirt/images/<needed_box_name>.img
(If you have troubles finding .img file, corresponding to running VM use virsh -q vol-list —pool default command to list all libvirt images. )
After successful ending of the last command the box file will appear at ./<needed_box_name>.box. This file can be added localy to vagrant by running:
vagrant box add ./needed_box_name.box --name <name_of_new_box>
or can be uploaded to Atlas( https://atlas.hashicorp.com/).