Skip to content

Instantly share code, notes, and snippets.

@sergeycherepanov
Forked from yuanying/vagrant-kvm.md
Created September 21, 2016 15:30
Show Gist options
  • Save sergeycherepanov/773c4994aca1ec1ff4265d066b4f8700 to your computer and use it in GitHub Desktop.
Save sergeycherepanov/773c4994aca1ec1ff4265d066b4f8700 to your computer and use it in GitHub Desktop.
How to use vagrant-kvm

Install Vagrant

sudo su
apt-get update && apt-get install -y libvirt-dev ruby-all-dev apparmor-utils
curl -O -L https://dl.bintray.com/mitchellh/vagrant/vagrant_1.6.5_x86_64.deb
dpkg -i vagrant_1.6.5_x86_64.deb 
aa-complain /usr/lib/libvirt/virt-aa-helper # workaround
exit

Install vagrant-kvm as user

vagrant plugin install vagrant-kvm 

Config kvm

sudo vim /etc/modprobe.d/kvm.conf
cat /etc/modprobe.d/kvm.conf
    options kvm-intel nested=1
    options kvm-amd   nested=1

Add ubuntu trusty image

vagrant box add trusty64 https://vagrant-kvm-boxes-si.s3.amazonaws.com/trusty64-kvm-20140418.box
vagrant box list

Create Vagrantfile

mkdir -p ~/vagrant/test
cd ~/vagrant/test
vagrant init trusty64

Config Vagrantfile

Vagrant.configure('2') do |config|
  config.vm.box = "trusty64"

  config.vm.network :public_network, ip: "192.168.11.197"
  config.vm.network :private_network, ip: "192.168.123.10"
  config.vm.provider :kvm do |kvm, override|
    kvm.memory_size     = '2048m'
  end
end

Vagrant up

vagrant up --provider=kvm

Login to VM

vagrant ssh

Multi-node Vagrantfile example

Vagrant.configure('2') do |config|
  config.vm.box = "trusty64"
  
  config.vm.define :control do |control|
    control.vm.hostname = "controller"
    control.vm.network :public_network
    control.vm.network :private_network, ip: "192.168.123.10"
    control.vm.provider :kvm do |kvm, override|
      kvm.memory_size     = '2048m'
    end
  end

  config.vm.define :network do |network|
    network.vm.hostname = "network"
    network.vm.network :public_network
    network.vm.network :private_network, ip: "192.168.123.12"
    network.vm.provider :kvm do |kvm, override|
      kvm.memory_size     = '2048m'
    end
  end

end

Access to node

vagrant ssh control
vagrant ssh network

Install vagrant-libvirt

sudo apt-get install libxslt-dev libxml2-dev libvirt-dev qemu-utils
vagrant plugin install vagrant-libvirt
vagrant plugin install vagrant-mutate

convert kvm box to libvirt box

vagrant mutate trusty64 libvirt

Config Vagrantfile

Vagrant.configure('2') do |config|
  config.vm.box = "trusty64"

  config.vm.define :control do |control|
    control.vm.hostname = "controller"
    control.vm.network :public_network
    control.vm.network :private_network, ip: "192.168.123.10"
    control.vm.provider :kvm do |kvm, override|
      kvm.memory_size     = '2048m'
    end
    control.vm.provider :libvirt do |libvirt, override|
      libvirt.memory = 2048
      libvirt.nested = true
    end
  end

  config.vm.define :network do |network|
    network.vm.hostname = "network"
    network.vm.network :public_network
    network.vm.network :private_network, ip: "192.168.123.12"
    network.vm.provider :kvm do |kvm, override|
      kvm.memory_size     = '2048m'
    end
    network.vm.provider :libvirt do |libvirt, override|
      libvirt.memory = 2048
      libvirt.nested = true
    end
  end

end

Vagrant up and access

 vagrant up --provider=libvirt
 vagrant ssh control
 vagrant ssh network

Install virtualbox

sudo su
echo "deb http://download.virtualbox.org/virtualbox/debian trusty contrib" >> /etc/apt/sources.list
wget -q https://www.virtualbox.org/download/oracle_vbox.asc -O- | apt-key add -
apt-get update && apt-get install -y virtualbox-4.3 dkms

Config Vagrantfile

mkdir ~/vagrant/vb
cd ~/vagrant/vb
vim Vagrantfile

Sample Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box     = "ubuntu/trusty64"

  config.vm.define :control do |controller|
    controller.vm.hostname = "controller"
    controller.vm.provider "virtualbox" do |vb|
      vb.customize ["modifyvm", :id, "--memory", "2048"]
    end
    controller.vm.network :private_network, ip: '192.0.2.24', virtualbox__intnet: "intnet"
    controller.vm.network :private_network, ip: '192.168.99.24', virtualbox__intnet: "extnet"

  end
  config.vm.define :network do |network|
    network.vm.hostname = "network"
    network.vm.provider "virtualbox" do |vb|
      vb.customize ["modifyvm", :id,
                    "--memory", "1024",
                    "--nicpromisc1", "allow-all",
                    "--nicpromisc2", "allow-all",
                    "--nicpromisc3", "allow-all",
                  ]
    end
    network.vm.network :private_network, ip: '192.0.2.25', virtualbox__intnet: "intnet"
    network.vm.network :private_network, ip: '192.168.99.25', virtualbox__intnet: "extnet"
    network.vm.network :public_network
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment