LoginSignup
20
20

More than 5 years have passed since last update.

vagrant-libvirtで高速検証環境の構築

Posted at

chefやansibleのテストの際にvagrant+VirtualBoxを使用していたが如何せんvagrant upが遅い、遅すぎる。ストレイト・クーガーに合わせる顔がない。というわけで、vagrant-libvirtで高速検証環境の構築を行った際のメモ。historyからの掘り起こし、なので所々動かない所があるかもしれないけどあくまでメモ。

想定環境

  • Centos7 x86_64
  • libvirt 1.2.17
  • vagrant 1.8.3

必要なパッケージのインストール

libvirt関連

sudo yum install -y qemu-kvm libvirt libvirt-devel

vagrantを使う際に必要になるもの

sudo yum install -y nfs-utils

vagrant

sudo yum install -y https://releases.hashicorp.com/vagrant/1.8.3/vagrant_1.8.3_x86_64.rpm

vagrant plugins

vagrant plugin install vagrant-libvirt vagrant-mutate sahara 

libvirtdの設定

/etc/libvirt/libvirtd.conf で下記4行をコメントイン

/etc/libvirt/libvirtd.conf
unix_sock_group = "libvirt"
unix_sock_ro_perms = "0777"
auth_unix_ro = "none"
auth_unix_rw = "none"

libvirt groupを作成して自分を追加する

sudo groupadd libvirt
sudo gpasswd -a shunkou libvirt

sysctlの設定

sudo echo 'net.ipv4.ip_forward = 1' > /etc/sysctl.d/00-libvirt.conf
sudo sysctl -p

起動する

sudo systemctl enable libvirtd.service
sudo systemctl start libvirtd.service

管理用ネットワークの作成

/tmp/vagrant_libvirt.xml
<network>
  <name>vagrant_libvirt</name>
  <forward mode='nat'/>
  <bridge name='virbr0' stp='on' delay='0'/>
  <ip address='10.114.51.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='10.114.51.4' end='10.114.51.254'/>
    </dhcp>
  </ip>
</network>
sudo virsh
virsh# net-define /tmp/vagrant_libvirt.xml
virsh# net-autostart vagrant_libvirt
virsh# net-start vagrant_libvirt

vagrant関連の設定

host-guest間のファイル共有のためにnfs-serverを起動

sudo systemctl enable nfs-server
sudo systemctl start nfs-server

実行準備

適当にboxをdownload or 作ったboxを入れる
こことかから取ってくる

vagrant box add https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box --name ubuntu14.04
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'ubuntu14.04' (v0) for provider:
    box: Downloading: https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box
==> box: Successfully added box 'ubuntu14.04' (v0) for 'virtualbox'!
vagrant box list
ubuntu14.04     (virtualbox, 0)

libvirtで使えるように対応providerを変換する

vagrant mutate ubuntu14.04 --input-provider virtualbox libvirt
Converting ubuntu14.04 from virtualbox to libvirt.
    (100.00/100%)
The box ubuntu14.04 (libvirt) is now ready to use.
vagrant box list
ubuntu14.04     (libvirt, 0)
ubuntu14.04     (virtualbox, 0)

不要なboxを削除する

vagrant box remove ubuntu14.04 --provider virtualbox

VMを起動する

Vagrantfileの作成

vagrant init ubuntu14.04

Vagrantfileの編集

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu14.04"
  config.vm.hostname = "anchovy"

  # 管理用N/Wの設定
  config.vm.provider :libvirt do |lv|
    lv.management_network_name = "vagrant_libvirt"
    lv.management_network_address = "10.114.51.0/24"
  end
end

VMの起動 

vagrant up
Bringing machine 'default' up with 'libvirt' provider...
==> default: Creating image (snapshot of base box volume).
==> default: Creating domain with the following settings...
...
==> default: Exporting NFS shared folders...
==> default: Preparing to edit /etc/exports. Administrator privileges will be required...
...

速い

引っかかったこと・メモ

iptable/firewalld

vagrantがnatする際にfirewalldを使うため、停止しているとvagrant up時にssh出来ずに刺さる

管理用ネットワーク

独自のboxを使っているとデフォルトでiptable等の制限がキツイものがる。そこで管理用ネットワークからのアクセスが許可されていないとvagrant up時にssh出来ずに刺さる

sahara

使うとimageのsnapshotが取れてrollbackが出来て便利
vagrant-libvirtにも対応している模様

そして何よりも

速さが足りない

20
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
20