LoginSignup
15
17

More than 5 years have passed since last update.

CentOSのオフィシャルvagrant boxにVBoxGuestAdditionsを追加する

Posted at

Ubuntuは公式イメージあるけどCentOSは公式イメージ無いんだなーと思いながら"chef/centos-7.0"とかを使っていたら、chefのboxがなんか全部bento/配下に移動しちゃっったりしてて、何が起こったんだって調べてるうちにCentOS公式イメージが出てるのを発見。せっかくならこれからは公式を使っていきたい。

でもこいつにはVirtualBoxのGuestAdditionsがインストールされてない状態で配布されているようで、そのままvagrantで使おうとすると以下のようなメッセージが出て、Shared Folderとかポートフォワーディングとかができない。

vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'centos/7' is up to date...
(中略)
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default:
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
*

CentOSにGuestAdditionsをインストールする方法について調べると、GUIを前提にしてるページだったり、微妙に必要なパッケージが漏れていたり、カーネルのバージョンについての注意点が抜けてたりしたのでメモ

ゲストマシンOptical Diskを追加

VirtualBox Gueset AdditionsはVMにCD-ROMをインサートしてインストールするので、まず光学ドライブがVMにセットされてないといけない

  1. Guest VMを終了させる
  2. VBoxのGUIから、[対象のVMを選択] - [Settings] - [Storage] - [+アイコン] - [Optical Disk] - [Leave Empty]

必要パッケージのインストール

# yum install bzip2 kernel-devel

ここで現在のカーネルとインストールされたkernel-develのバージョンを見てみると、

# yum list installed | grep kernel
kernel.x86_64                    3.10.0-229.14.1.el7            @koji-override-1
kernel-devel.x86_64              3.10.0-229.20.1.el7            @updates
kernel-headers.x86_64            3.10.0-229.20.1.el7            @updates
kernel-tools.x86_64              3.10.0-229.14.1.el7            @koji-override-1
kernel-tools-libs.x86_64         3.10.0-229.14.1.el7            @koji-override-1

この状態だと、インストールされているカーネルのバージョンに対応する kernel-devel, が無いため、この状況でGuest Additionsのインストールしようとしても失敗する

(実行不要)
# sh VBoxLinuxAdditions.run
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.0.10 Guest Additions for Linux............
VirtualBox Guest Additions installer
Removing installed version 5.0.10 of VirtualBox Guest Additions...
Removing existing VirtualBox non-DKMS kernel modules[  OK  ]
Copying additional installer modules ...
Installing additional modules ...
Removing existing VirtualBox non-DKMS kernel modules[  OK  ]
Building the VirtualBox Guest Additions kernel modules
The headers for the current running kernel were not found. If the following
module compilation fails then this could be the reason.
The missing package can be probably installed with
yum install kernel-devel-3.10.0-229.14.1.el7.x86_64

Building the main Guest Additions module[FAILED]
(Look at /var/log/vboxadd-install.log to find out what went wrong)
Doing non-kernel setup of the Guest Additions[  OK  ]
Installing the Window System drivers
Could not find the X.Org or XFree86 Window System, skipping.

カーネルをアップデート

kernel-develに合わせてkernelもアップデート
(kernelに合わせてkernel-devel-3.10.0-229.14.1.el7.x86_64をインストールするのでもいいんだとおもう)

アップデート前のバージョン

# uname -r
3.10.0-229.14.1.el7.x86_64

アップデート

# yum install kernel kernel-tools
...

# yum list installed | grep kernel
kernel.x86_64                    3.10.0-229.14.1.el7            @koji-override-1
kernel.x86_64                    3.10.0-229.20.1.el7            @updates
kernel-devel.x86_64              3.10.0-229.20.1.el7            @updates
kernel-headers.x86_64            3.10.0-229.20.1.el7            @updates
kernel-tools.x86_64              3.10.0-229.20.1.el7            @updates
kernel-tools-libs.x86_64         3.10.0-229.20.1.el7            @updates

再起動すると

# uname -r
3.10.0-229.20.1.el7.x86_64

CD-ROMをインサートしてマウント

  1. vagrant up ではなく、Virtual BoxのGUIからVMを立ち上げる
  2. ログイン
  3. [Device] - [Insert Guest Additions CD Image...]
  4. rootユーザになり、CDをファイルシステムにマウント
$ sudo su -
# mkdir /media/cdrom
# mount /dev/cdrom /media/cdrom
mount: /dev/sr0 is write-protected, mounting read-only
# ls /media/cdrom
... ... VBoxLinuxGuestAdditions.run ... ...

VBoxLinuxGuestAdditions.run 実行

# cd /media/cdrom

# sh VBoxLinuxAdditions.run
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.0.10 Guest Additions for Linux............
VirtualBox Guest Additions installer
Removing installed version 5.0.10 of VirtualBox Guest Additions...
Removing existing VirtualBox non-DKMS kernel modules[  OK  ]
Copying additional installer modules ...
Installing additional modules ...
Removing existing VirtualBox non-DKMS kernel modules[  OK  ]
Building the VirtualBox Guest Additions kernel modules
Building the main Guest Additions module[  OK  ]
Building the shared folder support module[  OK  ]
Building the OpenGL support module[  OK  ]
Doing non-kernel setup of the Guest Additions[  OK  ]
Starting the VirtualBox Guest Additions [  OK  ]
Installing the Window System drivers
Could not find the X.Org or XFree86 Window System, skipping.

GUIはインストールしていないので、X.Org関連はスキップされていてOK

以上インストール完了。

Ubuntuみたいに最初からGuest Additionsインストールされた状態で配布してくれたらいいのになー

15
17
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
17