CLOVER🍀

That was when it all began.

vagrant-libvirtをUbuntu Linux 18.04 LTSにインストールして、Ubuntu Linuxの仮想マシンを起動するまで

以前、セキュアブートが有効なUbuntu Linux 18.04 LTSに、VirtualBoxをインストールするのに失敗しました。

Ubuntu Linux 18.04 LTSに、VirtualBox 6.0をaptでインストールする - CLOVER🍀

ただ、Vagrantが使いたいなぁとは思っていて、VirtualBox以外のProviderをちょっと探してみました。

Vagrantは2.2.5、vagrant-libvirtは0.0.45を使用します。

自分の手元では、libvirt向けのものが良さそうだったので、こちらにチャレンジ。

GitHub - vagrant-libvirt/vagrant-libvirt: Vagrant provider for libvirt.

ちなみに、KVM用のProviderもあったみたいですが、こちらはメンテナンスが終了し、libvirtの方を使うことを勧めています。

GitHub - adrahon/vagrant-kvm: This project is dead, please use vagrant-libvirt instead! A Vagrant 1.4+ plugin that adds a KVM provider to Vagrant, allowing Vagrant to control and provision KVM/QEMU VM.

というわけで、今回の環境はこちら。

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.3 LTS
Release:    18.04
Codename:   bionic


$ uname -a
Linux xxxxx 4.15.0-65-generic #74-Ubuntu SMP Tue Sep 17 17:06:04 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

インストール方法に従って、vagrant-libvirtをインストールしてみます。

Installation

で、いきなり失敗。

$ sudo apt build-dep vagrant ruby-libvirt
パッケージリストを読み込んでいます... 完了
E: sources.list に 'ソース' URI を指定する必要があります

調べてみると、/etc/apt/sources.listファイルで、「deb-src」がコメントアウトされているのがいけないみたいです。ソースが入手
できません、と。

# deb cdrom:[Ubuntu 18.04.1 LTS _Bionic Beaver_ - Release amd64 (20180725)]/ bionic main restricted

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://jp.archive.ubuntu.com/ubuntu/ bionic main restricted
# deb-src http://jp.archive.ubuntu.com/ubuntu/ bionic main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://jp.archive.ubuntu.com/ubuntu/ bionic-updates main restricted
# deb-src http://jp.archive.ubuntu.com/ubuntu/ bionic-updates main restricted

〜省略〜

というわけで、コメントアウト解除。

$ sudo perl -wpi -e 's!# deb-src!deb-src!' /etc/apt/sources.list

リポジトリの情報を更新。

$ sudo apt update

今度は、うまくインストールできます。

$ sudo apt install build-dep ruby-libvirt
$ sudo apt install qemu libvirt-bin ebtables dnsmasq-base
$ sudo apt install libxslt-dev libxml2-dev libvirt-dev zlib1g-dev ruby-dev

あと、libvirtを使う準備も。

$ sudo usermod -a -G libvirt $(whoami)

Vagrantのインストール。

$ wget https://releases.hashicorp.com/vagrant/2.2.5/vagrant_2.2.5_x86_64.deb
$ sudo dpkg -i vagrant_2.2.5_x86_64.deb

vagrant-libvirtプラグインのインストール。

$ vagrant plugin install vagrant-libvirt

少し時間がかかりますが、インストールできました。

Installing the 'vagrant-libvirt' plugin. This can take a few minutes...
Fetching: formatador-0.2.5.gem (100%)
Fetching: excon-0.67.0.gem (100%)
Fetching: fog-core-1.43.0.gem (100%)
Fetching: mini_portile2-2.4.0.gem (100%)
Fetching: nokogiri-1.10.4.gem (100%)
Building native extensions.  This could take a while...
Fetching: ruby-libvirt-0.7.1.gem (100%)
Building native extensions.  This could take a while...
Fetching: fog-xml-0.1.3.gem (100%)
Fetching: fog-json-1.2.0.gem (100%)
Fetching: fog-libvirt-0.6.0.gem (100%)
Fetching: vagrant-libvirt-0.0.45.gem (100%)
Installed the plugin 'vagrant-libvirt (0.0.45)'!

では、vagrant-libvirtを使って、仮想マシンを起動したいと思います。Ubuntu Linux仮想マシンがいいのですが、Ubuntuオフィシャルの
ものではなくて、こちらであればlibvirt用のBoxがあるので、こちらを利用。

Vagrant box generic/ubuntu1804 - Vagrant Cloud

VagrantのProvider間で、Boxを変換するvagrant-mutateのメンテナンスも終了しているようですし。

GitHub - sciurus/vagrant-mutate: Convert vagrant boxes to work with different providers

vagrant init」

$ vagrant init generic/ubuntu1804

「--provider」で、libvirtを指定して起動。

$ vagrant up --provider=libvirt

ドキュメントを見ると、環境変数でデフォルトのProviderを指定できるようです。

Start VM

$ export VAGRANT_DEFAULT_PROVIDER=libvirt

SSHで接続。

$ vagrant ssh

「apt update」だけ確認。

$ sudo apt update
Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:2 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [523 kB]
Get:3 http://security.ubuntu.com/ubuntu bionic-security/main i386 Packages [378 kB]
Hit:4 http://us.archive.ubuntu.com/ubuntu bionic InRelease             
Get:5 http://us.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB] 

〜省略〜

これで、libvirtをProviderとしてVagrantが利用できますね。

メモリなどの設定は、こちらを参照。

Domain Specific Options

オマケ

「apt update」などで、タイムアウトしたりして不安定な場合。

$ sudo apt update -y
Err:1 http://us.archive.ubuntu.com/ubuntu bionic InRelease                  
  Temporary failure resolving 'us.archive.ubuntu.com'
Err:2 http://security.ubuntu.com/ubuntu bionic-security InRelease           
  Temporary failure resolving 'security.ubuntu.com'
Err:3 http://us.archive.ubuntu.com/ubuntu bionic-updates InRelease
  Temporary failure resolving 'us.archive.ubuntu.com'

〜省略〜

以下のようにするとよいでしょう。

$ sudo perl -wpi -e 's!nameserver .*!nameserver 8.8.8.8!' /etc/resolv.conf