Plamo Linux 5.1 で vagrant は vagrant-kvm の方が楽だった
(2014-01-15 追記) Vagrant 1.4 系だとまだ vagrant-kvm はちゃんと動かないようです.ご参考
さっき『Plamo Linux 5.1 で vagrant を Libvirt Provider 経由で使う - TenForwardの日記』って書いたばっかりなのに,次は vagrant-kvm っての試してみようかと思って試してみたら,こっちの方が断然楽だったので,Plamo でやるときはこっちでしょう!! 名前からてっきり libvirt 使わないのかと思ったらしっかり使います
- netcat の置き換え不要
- Plamo の標準的な使い方だと何もせずに VM 起動
- VirtualBox 用に作られた box が使える
悪い点.
VM起動時に共有フォルダを設定しようとするけど Plamo じゃ期待する動作をしないので NFS 共有なしで起動する- なぜか ~/.vagrant.d/tmp/storage-pool がないとか言ってエラーになる (mkdir すれば解決(?))
- libvirt がデフォルトで作成する 192.168.122.0/24 のネットワークを使わない (
使う方法あるのかな?)
さて,手順ですが,
- (Optional) netcat の置き換えは不要なので,さっきのエントリに騙されて置き換えちゃった人は戻しておきましょう.netcat 置き換えてない人は不要.
# removepkg netcat_openbsd libbsd
# installpkg netcat-0.7.1-x86_64-P1.tgz - (Optional) vagrant-libvirt 入れちゃった人は vagrant-kvm と同居できないので削除しましょう.(vagrant-libvirtの次バージョンで解消らしい)
$ vagrant plugin uninstall vagrant-libvirt
- VM 起動時に sudo で NFS 共有関連の処理をやるので,vagrant 実行ユーザで sudo を実行可能に設定しましょう.
- vagrant-kvm 入れましょう
$ vagrant plugin install vagrant-kvm
Installing the 'vagrant-kvm' plugin. This can take a few minutes...
Installed the plugin 'vagrant-kvm (0.1.2)'! - 適当な box ファイルを add します. (http://www.vagrantbox.es/ 辺りから) 例えば...
$ vagrant box add precise64 http://files.vagrantup.com/precise64.box
- この Box ファイルを kvm 用に変換.virtualbox というディレクトリを kvm に変えて,中にある metadata.json 内の provider という定義を virtualbox -> kvm に書き換えます.
$ cd ~/.vagrant.d/boxes/precise64 ; mv virtualbox kvm
$ cd kvm ; sed -i 's/virtualbox/kvm/' metadata.json - Vagrantfile の作成
$ mkdir /path/to/vagrant ; cd /path/to/vagrant
vi Vagrantfile - なぜか ~/.vagrant.d/tmp/storage-pool がないとか言ってエラーが出るので
$ mkdir ~/.vagrant.d/tmp/storage-pool
- 起動 (2013/09/10 追記: vagrant 1.3.x 以降では以下のようなエラーは出ません.詳細は後で説明)
$ vagrant up --provider=kvm
Bringing machine 'default' up with 'kvm' provider...
[default] Preparing network interfaces based on configuration...
[default] Creating shared folders metadata...
[default] Booting VM...
[default] Exporting NFS shared folders...
Preparing to edit /etc/exports. Administrator privileges will be required...
パスワード:
exportfs: extra arguments are not permitted with -a or -r
[default] Mounting NFS shared folders...
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!
mount -o udp,vers=3 10.0.0.1:'/home/karma/work/vagrant/precise64' /vagrant
Stdout from the command:
Stderr from the command:
stdin: is not a tty
mount.nfs: access denied by server while mounting 10.0.0.1:/home/karma/work/vagrant/precise64 - ssh ログイン
$ vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64)
* Documentation: https://help.ubuntu.com/
Welcome to your Vagrant-built virtual machine.
Last login: Thu Aug 22 09:28:14 2013 from 10.0.0.1
vagrant@precise64:~$
と,まあエラーは出ますが,(vagrant 1.3.x 以降だとエラー出ません.1.3.1 まではバグでエラーでますが.以下参照)ちゃんと ssh アクセスは可能です.この時の Vagrantfile は
$ less Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "precise64" config.vm.network :private_network, ip: "10.0.0.100" end
この時,ネットワークは libvirt がデフォルトで使っている 192.168.122.0/24 以外のを指定する必要があります.
起動時エラーを消す
(2013/09/10 追記)
vagrant 1.3.1 が出ていたので試してみました.以下の修正は不要になっていますが,バグっていて exportfs -r がエラーになって NFS マウントがうまくいきません.以下を参照して修正すれば Plamo でも問題なく動きます.
- Typo in /etc/exports · Issue #2169 · mitchellh/vagrant · GitHub
- Fixed options to NFS: no_subtree and check became no_subtree_check · mitchellh/vagrant@693b813 · GitHub
(2013/08/29 追記:)
以下の問題は次のバージョンでおそらく無くなるはずです.Plamo 独自のコードが入るかも? と思いましたが,exportfs コマンドで NFS の export の設定をうまく動的に追加・削除するようですね.(今までは /etc/exports を書き換えて NFS サーバ再起動とかやっていた.)
関連:
- Use exportfs cli for NFS exports by p0deje · Pull Request #1695 · mitchellh/vagrant · GitHub
- Improvement of Linux supports and NFS handling by miurahr · Pull Request #2077 · mitchellh/vagrant · GitHub
(追記ここまで)
起動時のエラーはホスト側で Vagrantfile を置いたディレクトリを VM に対して NFS 共有可能なように設定し (/etc/exports 編集),ホスト上で NFS サーバの再起動を行なっているのですが,著名ディストリビューションならきちんと NFS サーバの再起動を行なってくれる(?)のが,Plamo の場合「その他 Linux」みたいな扱いになり,"/etc/init.d/nfs-kernel-server restart" というコマンドを実行しようとします.Plamo の場合,NFS サーバが起動しているので(でしたよね?),ここは "exportfs -a" すれば良いはずです (他のディストリビューションでもそうじゃないの?).
というわけで,ここを設定で逃げられれば良いのですが,無理そうなので配布ファイルを書き換えてしまいます.対象となるファイルは /opt/vagrant/embedded/gems/gems/vagrant-1.2.7/plugins/hosts/linux/host.rb
ファイルです.
--- embedded/gems/gems/vagrant-1.2.7/plugins/hosts/linux/host.rb.original 2013-08-22 18:25:24.116716705 +0900 +++ embedded/gems/gems/vagrant-1.2.7/plugins/hosts/linux/host.rb 2013-08-22 18:27:31.742816829 +0900 @@ -24,7 +24,7 @@ super @logger = Log4r::Logger.new("vagrant::hosts::linux") - @nfs_server_binary = "/etc/init.d/nfs-kernel-server" + @nfs_server_binary = "/usr/sbin/exportfs -a" end def nfs? @@ -53,7 +53,7 @@ # We run restart here instead of "update" just in case nfsd # is not starting - system("sudo #{@nfs_server_binary} restart") + system("sudo #{@nfs_server_binary}") end def nfs_prune(valid_ids)
あとは,ホスト側の /etc/hosts.allow, deny の設定を確認し,vagrant up すれば問題なく起動して共有されます.