Project

Profile

Help

How to create Vagrant boxes

VirtualBox

Configuring boxes

Create a new Virtual Machine with the following settings:

Name: BOX_NAME
Type: Linux
Version: Ubuntu64
Memory Size: 512MB (to taste)
New Virtual Disk: [Type: VMDK, Size: 40 GB]

Modify the hardware settings of the virtual machine for performance and because SSH needs port-forwarding enabled for the vagrant user:

Disable audio
Disable USB
Ensure Network Adapter 1 is set to NAT
Add this port-forwarding rule: [Name: SSH, Protocol: TCP, Host IP: blank, Host Port: 2222, Guest IP: blank, Guest Port: 22]

Install the OS

When prompted to setup a user, set the user to vagrant and the password to vagrant.

Set root password

sudo passwd root

Setup the super user

su -

Adding vagrant user to sudoers
sudo visudo -f /etc/sudoers.d/vagrant

Add to opened file
vagrant ALL=(ALL) NOPASSWD:ALL

Apply system updates

sudo apt-get update -y
sudo apt-get upgrade -y
sudo shutdown -r now

Install vagrant key

mkdir -p /home/vagrant/.ssh
chmod 0700 /home/vagrant/.ssh
wget --no-check-certificate \
    https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub \
    -O /home/vagrant/.ssh/authorized_keys
chmod 0600 /home/vagrant/.ssh/authorized_keys
chown -R vagrant /home/vagrant/.ssh

Install and configure openSSH server

Install openSHH server

sudo nano /etc/ssh/sshd_config

Uncomment next line
AuthorizedKeysFile %h/.ssh/authorized_keys

sudo service ssh restart

Install guest tools

sudo apt-get install -y gcc build-essential linux-headers-server

In VirtualBox browse to the Devices menu at the top, then in the drop-down list at the bottom, click on Insert Guest Additions CD Image.
sudo mount /dev/cdrom /mnt 
cd /mnt
sudo ./VBoxLinuxAdditions.run

Packageing the box

sudo dd if=/dev/zero of=/EMPTY bs=1M
sudo rm -f /EMPTY
mkdir ~/PATH_TO_VAGRANT_BOXES
cd ~/PATH_TOV_AGRANT_BOXES
vagrant package --base BOX_NAME

Libvirt boxes

Converting from VirtualBox box

Tested only for clean virtual box image without anything installed on it

Prepare

apt-get install qemu-utils libvirt-dev
vagrant plugin install vagrant-mutate

Convert

vagrant mutate BOX_NAME_OR_URL NEW_PROVIDER_NAME

Export box

vagrant box repackage BOX_NAME_OR_URL PROVIDER_NAME

Building from scratch

Tried but does not work, investigation: http://stackoverflow.com/questions/35516234/how-to-build-libvirt-box-from-scratch

Create and start libvirt machine and install OS

Create .xml libvirt configuration file and run

virsh create MACHINE_NAME.xml

Install OS

After installation go through steps for configuring VirtualBox machine until guest tools installation.

Find image file for installed machine

virsh create MACHINE_NAME
virsh domblklist MACHINE_NAME

You will get info like
Target     Source
------------------------------------------------
vda        /home/galiaf95/Development/Projects/OSLL/mdbci_main/Virsh/test.img
hdc        /home/galiaf95/Downloads/ubuntu-14.04.3-desktop-amd64.iso

Aside vda or hda title you will find image path
Copy image file to place where you going to package vagrant box
Convert image file to qcow2 format and rename to box.img
sudo qemu-img convert -f raw -O qcow2  MACHINE_NAME.img  MACHINE_NAME.qcow2
mv MACHINE_NAME.qcow2 box.img 

Create metadata.json and Vagrantfile files

In vagrant file add something like this

Vagrant.configure("2") do |config|
         config.vm.provider :libvirt do |libvirt|
         libvirt.driver = "kvm" 
         libvirt.host = 'localhost'
         libvirt.uri = 'qemu:///system'
         end
config.vm.define "new" do |custombox|
         custombox.vm.box = "custombox"       
         custombox.vm.provider :libvirt do |test|
         test.memory = 1024
         test.cpus = 1
         end
         end
end

In metadata.json add something like this
{
  "provider"     : "libvirt",
  "format"       : "qcow2",
  "virtual_size" : 40
}

Packaging Libvirt box

tar cvzf NEW_BOX_NAME.box ./metadata.json ./Vagrantfile ./box.img 

Links

https://www.skoblenick.com/vagrant/creating-a-custom-box-from-scratch/#t11
http://unix.stackexchange.com/questions/222427/how-to-create-custom-vagrant-box-from-libvirt-kvm-instance