【Mac】Vagrant + VirtualBoxのデータ領域を外付けHDDに変更する

普段開発作業は社内の仮想環境に自宅からVPN経由でアクセスし行っているのですが、先日から社内へのVPN接続が不安定で・・・思い切って週末を利用し、ローカルにVagrantの環境を構築し直すことにしました。
(VagrantはこれまでMacbook Pro Retina(2014)のSSD容量を気にして、以前に使っていたMacbook Pro(2011)上で触っていました)

今回はその時の作業を備忘録としてメモに残します。

VirtualBoxをインストール

こちらは公式の手順に従ってインストールを行ってください。
Chapter 2. Installation details

VirtualBoxのデータ領域保存フォルダを作成

ここでいうデータ領域とは、VirtualBox仮想マシンフォルダを指しています。
HDDの部分は外付けHDDのドライブ名が入ります。

# mkdir /Voluems/HDD/VirtualBox

デフォルトの仮想マシンフォルダーを変更

VirtualBoxを起動し、[環境設定]→[一般]の順にメニューを表示します。
その中の[デフォルトの仮想マシンフォルダー]を、先ほど作成した外付けHDDのパスに変更します。

Vagrantをインストール

こちらも公式の手順に従ってインストールを行ってください。
Installing Vagrant - Vagrant Documentation

VAGRANT_HOMEを変更

Vagrantはデフォルトでユーザーのホームディレクトリ以下に .vagrant.d というフォルダが掘られ、そちらにboxファイルなどが保存されていきます。
今回はこちらのフォルダを変更してみましょう。

# mkdir /Voluems/HDD/Vagrant
# cp -r ~/.vagrant.d /Voluems/HDD/Vagrant
# vim ~/.bash_profile
export VAGRANT_HOME=/Volumes/HDD/Vagrant/.vagrant.d
export PATH=$VAGRANT_HOME:$PATH
# source ~/.bash_profile

boxファイルをダウンロード

ここまでの手順でVirtualBoxVagrantのデータ領域を外付けHDDに変更できました。
早速設定が正しく行われていることを確認するために、boxファイルのダウンロードを行ってみます。

# vagrant box add CentOS-7.1.1503-x86_64 https://github.com/holms/vagrant-centos7-box/releases/download/7.1.1503.001/CentOS-7.1.1503-x86_64-netboot.box
/opt/vagrant/bin/../embedded/gems/gems/vagrant-1.7.2/lib/vagrant/pre-rubygems.rb:31: warning: Insecure world writable dir /Volumes/HDD/Vagrant/.vagrant.d in PATH, mode 040777
/opt/vagrant/embedded/gems/gems/bundler-1.7.11/lib/bundler/runtime.rb:222: warning: Insecure world writable dir /Volumes/HDD/Vagrant/.vagrant.d in PATH, mode 040777
==> box: Adding box 'CentOS-7.1.1503-x86_64' (v0) for provider: 
    box: Downloading: https://github.com/holms/vagrant-centos7-box/releases/download/7.1.1503.001/CentOS-7.1.1503-x86_64-netboot.box
==> box: Box download is resuming from prior download progress
==> box: Successfully added box 'CentOS-7.1.1503-x86_64' (v0) for 'virtualbox'!

なにやら警告が表示されていますが、データ領域を確認してみましょう。

# ls -al /Volumes/HDD/Vagrant/.vagrant.d/boxes/
total 64
drwxrwxrwx  1 demiglacesource  staff  32768  6 28 23:39 CentOS-7.1.1503-x86_64

無事に外付けHDDにダウンロードされているようです。
当然、元のディレクトリにはなにも保存されていません。

# ls -al ~/.vagrant.d/boxes/
total 0
drwxr-xr-x  2 demiglacesource  staff   68  6 28 23:26 .
drwxr-xr-x  9 demiglacesource  staff  306  6 28 23:26 ..

先ほど表示されていた警告は、外付けHDDの権限に問題があるため表示されているようです。
他人の権限で書き込み権限が付いていると、Rubyが警告を表示しているとか。(VagrantRubyで出来ています)

vagrant init

続いて仮想マシンを起動する準備のため、vagrant initを行います。

# mkdir centos7
# cd centos7
# vagrant init CentOS-7.1.1503-x86_64
/opt/vagrant/bin/../embedded/gems/gems/vagrant-1.7.2/lib/vagrant/pre-rubygems.rb:31: warning: Insecure world writable dir /Volumes/ELEMENTS2/Vagrant/.vagrant.d in PATH, mode 040777
/opt/vagrant/embedded/gems/gems/bundler-1.7.11/lib/bundler/runtime.rb:222: warning: Insecure world writable dir /Volumes/ELEMENTS2/Vagrant/.vagrant.d in PATH, mode 040777
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

ここでも先ほどの警告が表示されましたが、それ以外の問題は無さそうです。

vagrant up

では仮想マシンを起動します。

# vagrant up 
/opt/vagrant/bin/../embedded/gems/gems/vagrant-1.7.2/lib/vagrant/pre-rubygems.rb:31: warning: Insecure world writable dir /Volumes/HDD/Vagrant/.vagrant.d in PATH, mode 040777
/opt/vagrant/embedded/gems/gems/bundler-1.7.11/lib/bundler/runtime.rb:222: warning: Insecure world writable dir /Volumes/HDD/Vagrant/.vagrant.d in PATH, mode 040777
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'CentOS-7.1.1503-x86_64'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: centos7_default_1435503088775_59076
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
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:

/Volumes/HDD/Vagrant/.vagrant.d/insecure_private_key

最後に赤字でエラーが表示されてしまいました。

これは先ほどから出ているvagrantの警告とも同じ原因によるものなどですが、どうやら私の環境で外付けHDDに.vagrant.dフォルダをコピーした際に、 パーミッションが全て777になってしまっていることが原因のようです。

この状態ではsshコマンドによるログインは行えますが、vagrant sshによるログインが行えません。

insecure_private_keyのエラー回避

しばらく奮闘したのですが、外付けHDDの方のパーミッションを変更することができませんでした。
(sudoしてchmodでもダメ。フォーマットの問題?)

今回はいったん諦めて、ローカルに残っていたinsecure_private_keyのシンボリックリンクを外付けHDDに作成することにしました。

# rm /Volumes/HDD/Vagrant/.vagrant.d/insecure_private_key 
# ln -s ~/.vagrant.d/insecure_private_key /Volumes/HDD/Vagrant/.vagrant.d/

vagrant up (2回目)

あがれーーーー!!!

# vagrant up
/opt/vagrant/bin/../embedded/gems/gems/vagrant-1.7.2/lib/vagrant/pre-rubygems.rb:31: warning: Insecure world writable dir /Volumes/HDD/Vagrant/.vagrant.d in PATH, mode 040777
/opt/vagrant/embedded/gems/gems/bundler-1.7.11/lib/bundler/runtime.rb:222: warning: Insecure world writable dir /Volumes/HDD/Vagrant/.vagrant.d in PATH, mode 040777
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'CentOS-7.1.1503-x86_64'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: centos7_default_1435504676791_55032
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if its present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /Users/demiglacesource/Vagrant/centos7

キタコレ

最後にVirtulBoxの仮想マシンフォルダも確認してみましょう。

# ls -l /Volumes/HDD/VirtualBox
total 64
drwxrwxrwx  1 demiglacesource  staff  32768  6 29 00:17 centos7_default_1435504676791_55032

問題無さそうですね!

まとめ

これでローカルのSSDを食い潰すことなく、快適な仮想環境ライフをエンジョイできそうです。
あとはVagrantの警告だけ、なんとかできないかなぁー・・・