わすれっぽいきみえ

みらいのじぶんにやさしくしてやる

17日目: macのvagrant周りのファイルを外付けHDDに移してみた(その1)

年の瀬が近づいてきたので、そろそろwindowsmacもまた中身をキレイキレイしたい、そんな時期。

特にwindowsに関しては先輩から今使ってるやつより容量の大きいSSDをいただいたので、windowsSSDを換装したいなーと思っていて、ついに外付けHDDを買ってきた。こいつに一旦バックアップを取っておきSSDを置き換えてクリーンインストールして、前に困ってたAdobeの管理者権限問題も一気に解決したいと思っている。

だが、よく考えてみたらmacも中身はSSDで、あんまり書き込み激しいとすぐ死にそうだし、こいつも外付けに移したいよなぁと思った。プログラムの本体はSSDの方が早いので移す気はないが、ただのファイルたちは別にHDDに移してもいいよなぁ、いやむしろ移した方がよくね?と。

その中で気になったのはvagrantのboxたちだ。
これの容量がバカにならないので、ここではvagrantのboxたちを外付けに移して、そこから起動する方法を調べた。

まずは単純にVagrantfileがあるフォルダごと移動させる

単純に以下のように移す

$ cp -R $HOME/vagrant $MOUNT_DISK/.

ここで$MOUNT_DISKは自分の外付けHDDまでのパスを意味している。単純にHDDのどっかの場所にコピる。この時まだ消すのが怖いのでmac本体のディレクトリは消さずに$MOUNT_DISKの方に移動する。

$ cd $MOUNT_DISK/vagrant

私はvagrantディレクトリの中で、さらに仮想マシンごとにディレクトリを切っている。
以下のような感じ

$ ls -la
total 20
drwxrwxrwx 1 kimikimi714 staff    0 11 15 13:40 ./
drwxrwxrwx 1 kimikimi714 staff 4096 12 17 21:59 ../
drwxrwxrwx 1 kimikimi714 staff 4096  7 11 01:10 centos65/
drwxrwxrwx 1 kimikimi714 staff    0  6 19 19:16 docker/
drwxrwxrwx 1 kimikimi714 staff 4096  4 30  2014 packer_test/

どれでもいいので今回はcentos65ディレクトリに移動してみて、そこでおもむろにvagrant upコマンドを叩いてみた。

$ vagrant up
/Applications/Vagrant/bin/../embedded/gems/gems/vagrant-1.6.3/lib/vagrant/pre-rubygems.rb:31: warning: Insecure world writable dir /usr in PATH, mode 040777
/Applications/Vagrant/embedded/gems/gems/bundler-1.6.2/lib/bundler/runtime.rb:222: warning: Insecure world writable dir /usr in PATH, mode 040777
Bringing machine 'web_server' up with 'virtualbox' provider...
==> web_server: Checking if box 'chef/centos-6.5' is up to date...
==> web_server: Clearing any previously set forwarded ports...
==> web_server: Clearing any previously set network interfaces...
==> web_server: Preparing network interfaces based on configuration...
    web_server: Adapter 1: nat
    web_server: Adapter 2: hostonly
==> web_server: Forwarding ports...
    web_server: 22 => 2222 (adapter 1)
==> web_server: Booting VM...
==> web_server: Waiting for machine to boot. This may take a few minutes...
    web_server: SSH address: 127.0.0.1:2222
    web_server: SSH username: vagrant
    web_server: SSH auth method: private key
    web_server: Warning: Connection timeout. Retrying...
    web_server: Warning: Remote connection disconnect. Retrying...
==> web_server: Machine booted and ready!
GuestAdditions 4.3.6 running --- OK.
==> web_server: Checking for guest additions in VM...
==> web_server: Configuring and enabling network interfaces...
==> web_server: Mounting shared folders...
    web_server: /vagrant => $MOUNT_DISK/vagrant/centos65
==> web_server: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> web_server: to force provisioning. Provisioners marked to run always will still run.

おお、なんか外付けHDDのパーミッションの問題でセキュアじゃないよって怒られてるけど、一応立ち上げられたっぽい。

つまりVagrantfileがどこにあるかに依存せずに仮想マシンの立ち上げが出来ている。Vagrantfileのあるフォルダの場所が変わり、そこでvagrant upを実行した時に別物だと認識されて新たに仮想マシンが立ち上がると意味ないなと思っていたので、仮想マシンの実体を移さない限りは普通に立ち上げられるとわかって一安心した。

仮想マシンの実体があるディレクトリ

VirtualBox側の設定まで本当はいじらんとあかん気がするけども、vagrantで起動・終了までやってるのでまずはvagrant側の設定をいじらんとあかんだろう。
というので調べたらやっぱり似たようなことを考える人はいるわけで

VirtualBox - vagrantのboxフォルダ変更手順メモ - Qiita

この記事での環境はwindows7 64bitとあるが、まぁ$HOME/.vagrant.dになっているのを$MOUNT_DISK/.vagrant.dにすれば良いっぽいと思ってやってみた。

cp -R ~/.vagrant.d $MOUNT_DISK/.

まず環境変数VAGRANT_HOMEを変えてない状態だとvagrant upで見にいくboxの場所は~/.vagrant.dのままである。
で、一旦ホームディレクトリにある.vagrant.dディレクトリを移動させたあとexport VAGRANT_HOME="${MOUNT_DISK}/.vagrant.d"環境変数を書き換えた。そしてsource ~/.bashrcなりして環境変数をリロードしたあと、Vagrantfileのある場所でvagrant upしたところ以下のようになった。

$ vagrant up
/Applications/Vagrant/bin/../embedded/gems/gems/vagrant-1.6.3/lib/vagrant/pre-rubygems.rb:31: warning: Insecure world writable dir /usr in PATH, mode 040777
/Applications/Vagrant/embedded/gems/gems/bundler-1.6.2/lib/bundler/runtime.rb:222: warning: Insecure world writable dir /usr in PATH, mode 040777
Bringing machine 'web_server' up with 'virtualbox' provider...
==> web_server: Checking if box 'chef/centos-6.5' is up to date...
==> web_server: Clearing any previously set forwarded ports...
==> web_server: Clearing any previously set network interfaces...
==> web_server: Preparing network interfaces based on configuration...
    web_server: Adapter 1: nat
    web_server: Adapter 2: hostonly
==> web_server: Forwarding ports...
    web_server: 22 => 2222 (adapter 1)
==> web_server: Booting VM...
==> web_server: Waiting for machine to boot. This may take a few minutes...
    web_server: SSH address: 127.0.0.1:2222
    web_server: SSH username: vagrant
    web_server: SSH auth method: private key
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.

If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.

If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.

If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.

$ vagrant status
/Applications/Vagrant/bin/../embedded/gems/gems/vagrant-1.6.3/lib/vagrant/pre-rubygems.rb:31: warning: Insecure world writable dir /usr in PATH, mode 040777
/Applications/Vagrant/embedded/gems/gems/bundler-1.6.2/lib/bundler/runtime.rb:222: warning: Insecure world writable dir /usr in PATH, mode 040777
Current machine states:

web_server                running (virtualbox)

$ vagrant ssh web_server
/Applications/Vagrant/bin/../embedded/gems/gems/vagrant-1.6.3/lib/vagrant/pre-rubygems.rb:31: warning: Insecure world writable dir /usr in PATH, mode 040777
/Applications/Vagrant/embedded/gems/gems/bundler-1.6.2/lib/bundler/runtime.rb:222: warning: Insecure world writable dir /usr in PATH, mode 040777
The private key to connect to this box via SSH has invalid permissions
set on it. The permissions of the private key should be set to 0600, otherwise SSH will
ignore the key. Vagrant tried to do this automatically for you but failed. Please set the
permissions on the following file to 0600 and then try running this command again:

$MOUNT_DISK/.vagrant.d/insecure_private_key

うへぇ。秘密鍵パーミッションか。
外付けに移動させた.vagrant.dフォルダの中身を見ると

 ls -la
total 32
total 32
drwxrwxrwx 1 kimikimi714 kimikimi714 4096 12 17 23:29 ./
drwxrwxrwx 1 kimikimi714 kimikimi714 4096 12 17 23:09 ../
drwxrwxrwx 1 kimikimi714 kimikimi714 4096 12 17 23:10 boxes/
drwxrwxrwx 1 kimikimi714 kimikimi714 4096 12 17 23:24 data/
drwxrwxrwx 1 kimikimi714 kimikimi714 4096 12 17 23:09 gems/
-rwxrwxrwx 1 kimikimi714 kimikimi714 1675 12 17 23:09 insecure_private_key*
-rwxrwxrwx 1 kimikimi714 kimikimi714  270 12 17 23:09 plugins.json*
drwxrwxrwx 1 kimikimi714 kimikimi714    0 12 17 23:09 rgloader/
-rwxrwxrwx 1 kimikimi714 kimikimi714    3 12 17 23:10 setup_version*
drwxrwxrwx 1 kimikimi714 kimikimi714    0 12 17 23:09 tmp/

このあと外付けHDDに移したファイルのパーミッションを変えようとしたが変わってくれなかった。


ちょっとこの回ではまだつまづいているので、続きの記事に話は続く。