Open Source and Linux Lab

Главная » Uncategorized » Quick way to create Vagrant libvirt box from existing one

Quick way to create Vagrant libvirt box from existing one

Introduction

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/.

Prerequisites

  1. Clone https://github.com/vagrant-libvirt/vagrant-libvirt .
  2. 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.
  3. Do vagrant box add <needed_box_name>.

Creating box

Write Vagrantfile

# -*- 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

Start VM

  1. Call vagrant up —provider=libvirt .
  2. Wait for virtual machine to complete provision.
  3. Copy any files to machine or do any other manual actions which should persist in new box.

Package box

Run:

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/).

Links

  1. https://github.com/vagrant-libvirt/vagrant-libvirt#create-box
  2. https://www.vagrantup.com/
  3. https://libvirt.org/
  4. https://atlas.hashicorp.com/
  5. https://www.packer.io/

Добавить комментарий

Заполните поля или щелкните по значку, чтобы оставить свой комментарий:

Логотип WordPress.com

Для комментария используется ваша учётная запись WordPress.com. Выход /  Изменить )

Google+ photo

Для комментария используется ваша учётная запись Google+. Выход /  Изменить )

Фотография Twitter

Для комментария используется ваша учётная запись Twitter. Выход /  Изменить )

Фотография Facebook

Для комментария используется ваша учётная запись Facebook. Выход /  Изменить )

Connecting to %s

%d такие блоггеры, как: