Howto create a Vagrant image box for libvirt (kvm)

In previous post [1] we’ve seen how Vagrant is readily available on Fedora 20. Now let’s see how we can easily create boxes for libvirt provider [2]

In this example we are using a Fedora 20 image, but any qcow2 image can be used, just make sure to replace the “fedora 20” name accordingly.

A Vagrant box is in fact a compress TAR archive of following 3 files:

  • A QCOW2 image file
  • A Metadata file describing the QCOW2 image
  • A Vagrantfile

Image File

With a Fedora 20 image:

mkdir Fedora20
cd Fedora20
wget download.fedoraproject.org/pub/fedora/linux/updates/20/Images/x86_64/Fedora-x86_64-20-20140407-sda.qcow2

Preparation Script

The following script prepares an image for Vagrant, basically adding Vagrant user (like cloud-init) and sets-up the NICs

Launch the image in libvirt and execute following script:

# Vagrant user
useradd vagrant
mkdir ~vagrant/.ssh
chmod 700 ~vagrant/.ssh
echo "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key" > ~vagrant/.ssh/authorized_keys
chmod 600 ~vagrant/.ssh/authorized_keys
chown -R vagrant.vagrant ~vagrant/.ssh

# Sudoers
sed -i -r 's/(.* requiretty)/#\1/' /etc/sudoers

cat > /etc/sudoers.d/90-vagrant-users <<EOF
# User rules for vagrant
vagrant ALL=(ALL) NOPASSWD:ALL
EOF

# Network
# Leave only eth0 config - the rest will be managed by vagrant
rm -f /etc/sysconfig/network-scripts/ifcfg-eth{0,1,2,3}
cat > /etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF
DEVICE="eth0"
TYPE="Ethernet"
ONBOOT="yes"
NM_CONTROLLED=no
BOOTPROTO="dhcp"
EOF

# SSH Server
# No UseDNS - Optional, to speed up ssh connections
sed -i -r 's/.*UseNS.*/UseDNS no/' /etc/ssh/sshd_config

# Yum deps and update
yum -y install rsync
yum -y update
yum clean all
# Misc - For instance add a ssh key, etc

Metadata file

Let’s inspect the image file, to determine size and confirm format with file command:

file ./Fedora-x86_64-20-20140407-sda.qcow2
./Fedora-x86_64-20-20140407-sda.qcow2: QEMU QCOW Image (v2), 2147483648 bytes

The size is 2 GB (2147483648 bytes / 1024 / 1024 / 1024) and format is effectively qcow2. We can now create the metadata.json file:

cat > metadata.json <<EOF
{
  "provider"     : "libvirt",
  "format"       : "qcow2",
  "virtual_size" : 2
}
EOF

Vagrantfile

Vagrant.configure('2') do |config|
  config.vm.box = "gildub/fedora20"

  config.vm.define :test_vm do |test_vm|
    test_vm.vm.network :bridged, :adapter => 1
    test_vm.vm.network :bridged, :adapter => 2
  end
 
  config.vm.provider :libvirt do |libvirt|
    libvirt.driver = 'kvm'
    # libvirt.host = 'localhost'
    libvirt.connect_via_ssh = false
    libvirt.username = 'root'
    libvirt.storage_pool_name = 'default'
    # libvirt.default_prefix = ''
  end
end

Wrapping the box

cp Fedora-x86_64-20-20140407-sda.qcow2 box.img
tar cvzf Fedora20.box ./metadata.json ./Vagrantfile ./box.img

Ready to go

vagrant box add ./Fedora20.box

Notes

Links

[1] https://gilmatdub.wordpress.com/2014/08/08/howto_vagrant_on_fedora_using_libvirt/

[2] https://github.com/pradels/vagrant-libvirt/tree/master/example_box

, , , ,

3 Comments

Howto Vagrant on Fedora using Libvirt (kvm)

Install

The following is assuming an updated Fedora 20 system.

Sudo (or Root)

sudo yum -y install libvirt-devel libxslt-devel libxml2-devel
sudo yum -y install https://dl.bintray.com/mitchellh/vagrant/vagrant_1.6.5_x86_64.rpm

Libvirt Plugins

From our favourite user (see the ‘Notes’ section about User & Space):

vagrant plugin install vagrant-mutate # Optional, only for migrating boxes
vagrant plugin install vagrant-libvirt --plugin-version=0.0.20

Get Started

Environment

To avoid using ‘–provider=libvirt’ option all the time when running vagrant commands, add the following to .bash_profile:

export VAGRANT_DEFAULT_PROVIDER=libvirt

Adding an image a box

Let’s use a Fedora 20 guest for libvirt provider (available here [1]):

vagrant box add https://vagrantcloud.com/gildub/fedora20

Alternatively, a box can be added via either:

  • Convert other providers boxes (virtualbox, etc) using vagrant-mutate [2]
  • Create your own vagrant box [3]

Init & Vagrantfile

mkdir test1
cd test1

Create a Vagrantfile (or ack your own using ‘vagrant init’ to generate one):

cat > Vagrantfile <<EOF
Vagrant.configure("2") do |config|
  config.vm.define :vm1 do |vm|
    vm.vm.box = "fedora20"
    vm.vm.network :private_network, :ip => '192.168.200.40'
    vm.vm.network :private_network, :ip => '192.168.201.40'
  end
end
EOF

Run

vagrant up

Notes

User & Space

Any user will do but it’s preferable not to use ‘root’. 

Keep in mind that boxes, among other things, are stored in ‘~/.vagrant’.  And also ‘/var/lib/libvirt/’ can grow quickly, so keep it provisioned.

Libvirt Plugin Version

Don’t forget to specify the version wen installing vagrant-libvirt plugin otherwise you might end up using an older version with issues.

Private networks

Private networks are NATed therefore only visible from the host and have internet access accordingly.

Libvirt & SELinux

To avoid using ‘root’ for testing, the following provides full credentials to the specified user. Thanks to purpleidea [4].

sudo sh -c "cat >> /etc/polkit-1/localauthority/50-local.d/vagrant.pkla <<EOF
[Allow $USERNAME libvirt management permissions]
Identity=unix-user:$USERNAME
Action=org.libvirt.unix.manage
ResultAny=yes
ResultInactive=yes
ResultActive=yes
EOF"

Fedora 20 Guests

There is an issue with Fedora 20 guests during the network setup. A fix is available here [5] and should make it to the next release.

Links

[1] https://vagrantcloud.com/gildub/fedora20
[2] https://github.com/sciurus/vagrant-mutate
[3] https://github.com/pradels/vagrant-libvirt/tree/master/example_box
[4] https://ttboj.wordpress.com/2013/12/09/vagrant-on-fedora-with-libvirt/
[5] https://github.com/mitchellh/vagrant/commit/bd4d4284c473860b800906e4db889b5a04552d70

, , , , ,

Leave a comment