VirtualBox 4.3.14でCentOS7対応Guest Additionsをインストールする

# はじめに結論

2014-07-21編集: 2014-07-15にリリースされたVirtualBox 4.3.14で動作確認がとれたので、それに合わせて編集しました。

[centos7.0のvagrant boxを作るpacker templates.jsonを作った - Qiita](http://qiita.com/eigo_s/items/6d1cf360d670e4ed4775)によると、CentOS 7.0にVirtualBox Guest Additionsをインストールするとエラーになるそうです。

vagrantに非公式のtemporary fixである[vStone/vagrant-centos7_fix](https://github.com/vStone/vagrant-centos7_fix)を適用した上でVirtualBox 4.3.14を使えばGuest Additionsもインストール出来ました。

## Vagrantに改変が必要

暫定対応として上がっている[vStone/vagrant-centos7_fix](https://github.com/vStone/vagrant-centos7_fix)のプラグインをインストールすればOKです。

# 手順

## CentOS7のVagrant boxをビルド

VirtualBox 4.3.14なら[centos7.0のvagrant boxを作るpacker templates.jsonを作った - Qiita](http://qiita.com/eigo_s/items/6d1cf360d670e4ed4775)で紹介されている ```packer.json``` のそのままでboxをビルド出来ます。

```shell-session
% cd
% git clone https://github.com/eigo-s/packer_templates
% cd packer_templates/centos7.0/x86_64 
% packer build packer.json
virtualbox-iso output will be in this color.

==> virtualbox-iso: Downloading or copying Guest additions
    virtualbox-iso: Downloading or copying: file:///Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso
==> virtualbox-iso: Downloading or copying ISO
    virtualbox-iso: Downloading or copying: http://ftp.riken.jp/Linux/centos/7.0.1406/isos/x86_64/CentOS-7.0-1406-x86_64-NetInstall.iso
==> virtualbox-iso: Starting HTTP server on port 8081
==> virtualbox-iso: Creating virtual machine...
==> virtualbox-iso: Creating hard drive...
==> virtualbox-iso: Creating forwarded port mapping for SSH (host port 3213)
==> virtualbox-iso: Executing custom VBoxManage commands...
    virtualbox-iso: Executing: modifyvm packer-virtualbox-iso --memory 1024
    virtualbox-iso: Executing: modifyvm packer-virtualbox-iso --cpus 1
==> virtualbox-iso: Starting the virtual machine...
==> virtualbox-iso: Waiting 10s for boot...
==> virtualbox-iso: Typing the boot command...
==> virtualbox-iso: Waiting for SSH to become available...
==> virtualbox-iso: Connected to SSH!
==> virtualbox-iso: Uploading VirtualBox version info (4.3.14_RC1)
==> virtualbox-iso: Uploading VirtualBox guest additions ISO...
==> virtualbox-iso: Provisioning with shell script: scripts/base.sh
==> virtualbox-iso: Provisioning with shell script: scripts/vagrant.sh
    virtualbox-iso: % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
    virtualbox-iso: Dload  Upload   Total   Spent    Left  Speed
    virtualbox-iso: 100   409  100   409    0     0    793      0 --:--:-- --:--:-- --:--:--   792
==> virtualbox-iso: Provisioning with shell script: scripts/virtualbox.sh
    virtualbox-iso: mount: /dev/loop0 is write-protected, mounting read-only
    virtualbox-iso: Verifying archive integrity... All good.
    virtualbox-iso: Uncompressing VirtualBox 4.3.14_RC1 Guest Additions for Linux............
    virtualbox-iso: VirtualBox Guest Additions installer
    virtualbox-iso: Copying additional installer modules ...
    virtualbox-iso: Installing additional modules ...
    virtualbox-iso: Removing existing VirtualBox non-DKMS k[  OK  ]dules
    virtualbox-iso: Building the VirtualBox Guest Additions kernel modules
    virtualbox-iso: Building the main Guest Additions modul[  OK  ]
    virtualbox-iso: Building the shared folder support modu[  OK  ]
    virtualbox-iso: Building the OpenGL support module     [  OK  ]
    virtualbox-iso: Doing non-kernel setup of the Guest Add[  OK  ]
    virtualbox-iso: Starting the VirtualBox Guest Additions[  OK  ]
    virtualbox-iso: Installing the Window System drivers
    virtualbox-iso: Could not find the X.Org or XFree86 Window System, skipping.
==> virtualbox-iso: Provisioning with shell script: scripts/cleanup.sh
    virtualbox-iso: Loaded plugins: fastestmirror
    virtualbox-iso: Cleaning repos: base extras updates
    virtualbox-iso: Cleaning up everything
    virtualbox-iso: Cleaning up list of fastest mirrors
==> virtualbox-iso: Gracefully halting virtual machine...
==> virtualbox-iso: Preparing to export machine...
    virtualbox-iso: Deleting forwarded port mapping for SSH (host port 3213)
==> virtualbox-iso: Exporting virtual machine...
    virtualbox-iso: Executing: export packer-virtualbox-iso --output output-virtualbox-iso/packer-virtualbox-iso.ovf
==> virtualbox-iso: Unregistering and deleting virtual machine...
==> virtualbox-iso: Running post-processor: vagrant
==> virtualbox-iso (vagrant): Creating Vagrant box for 'virtualbox' provider
    virtualbox-iso (vagrant): Copying from artifact: output-virtualbox-iso/packer-virtualbox-iso-disk1.vmdk
    virtualbox-iso (vagrant): Copying from artifact: output-virtualbox-iso/packer-virtualbox-iso.ovf
    virtualbox-iso (vagrant): Renaming the OVF to box.ovf...
    virtualbox-iso (vagrant): Compressing: Vagrantfile
    virtualbox-iso (vagrant): Compressing: box.ovf
    virtualbox-iso (vagrant): Compressing: metadata.json
    virtualbox-iso (vagrant): Compressing: packer-virtualbox-iso-disk1.vmdk
Build 'virtualbox-iso' finished.

==> Builds finished. The artifacts of successful builds are:
--> virtualbox-iso: 'virtualbox' provider box: ./vagrant-box/centos-7.0-x64_64-virtualbox.box
```

以下のコマンドでboxを追加します。

```shell-session
% vagrant box add centos7 vagrant-box/centos-7.0-x64_64-virtualbox.box
```

## vagrant upでハマった

```rb:Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "centos7"
  config.vm.network :private_network, ip: "192.168.33.36"
  config.vm.provider :virtualbox do |vb|
    vb.customize ["modifyvm", :id, "--memory", "1024"]
  end
  config.vm.synced_folder ".", "/vagrant"
end
```

というVagrantfileを作って試してみましたが、エラーになりました。

```shell-session
% vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos7'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: cent7_default_1405160474845_75801
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> 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: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

ARPCHECK=no /sbin/ifup eth1 2> /dev/null

Stdout from the command:

ERROR    : [/etc/sysconfig/network-scripts/ifup-eth] Device eth1 does not seem to be present, delaying initialization.


Stderr from the command:
```

調べてみると[Issue with Consistent Network Device Naming on CentOS 7 · Issue #4171 · mitchellh/vagrant](https://github.com/mitchellh/vagrant/issues/4171)に一時的な修正版として[vStone/vagrant-centos7_fix](https://github.com/vStone/vagrant-centos7_fix)を作ったというコメントがありました。


```shell-session
% git clone https://github.com/vStone/vagrant-centos7_fix
% cd vagrant-centos7_fix
% vagrant plugin install vagrant-centos7_fix
Installing the 'vagrant-centos7_fix' plugin. This can take a few minutes...
Installed the plugin 'vagrant-centos7_fix (0.0.1)'!
```

Vagrantfileのあるディレクトリに戻ってVMを削除して作りなおしたら、今度は成功しました！

```shell-session
% vagrant destroy -f
==> default: Forcing shutdown of VM...
==> default: Destroying VM and associated drives...
% vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos7'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: cent7_default_1405161101264_28164
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> 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: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => /Users/hnakamur/vagrantfiles/cent7
```

ゲストにログインして、Vagrantファイルで指定したIPアドレス192.168.33.36が設定されていること、/vagrantに共有フォルダがマウントされていることを確認しました。

```shell-session
% vagrant ssh
Last login: Sat Jul 12 19:15:54 2014 from 10.0.2.2
[vagrant@localhost ~]$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:13:49:98 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3
       valid_lft 86334sec preferred_lft 86334sec
    inet6 fe80::a00:27ff:fe13:4998/64 scope link
       valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:b4:c7:f6 brd ff:ff:ff:ff:ff:ff
    inet 192.168.33.36/24 brd 192.168.33.255 scope global enp0s8
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:feb4:c7f6/64 scope link
       valid_lft forever preferred_lft forever
[vagrant@localhost ~]$ ll /vagrant
合計 4
-rw-r--r-- 1 vagrant vagrant 362  7月 12 17:55 Vagrantfile
```