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

Advertisements

, , , ,

  1. #1 by vadaptsdata on May 26, 2015 - 9:02 pm

    Thanks this helped lots!
    The only thing I would add the is the following otherwise ifcfg-eth0 interface can fail to come up:

    # Remove old MAC address.
    rm -f /etc/udev/rules.d/70-persistant-net.rules

    Like

    • #2 by vadaptsdata on May 28, 2015 - 10:11 pm

      Whoops! Typo! should be:
      rm -f /etc/udev/rules.d/70-persistent-net.rules

      Like

  2. #3 by gildub on May 29, 2015 - 2:12 am

    Thanks Vadaptsdata for the feedback.

    Regarding the udev rule, what is the OS involved? I’m asking because usually that is needed on RHEL/Centos/6.x but not on more recent versions.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: