LoginSignup
0
1

More than 3 years have passed since last update.

VirtualBox(mac+vagrant)上のCentOS7にVirtualBoxをインストールする

Last updated at Posted at 2020-03-05

VirtualBox(mac+vagrant)上のCentOS7で、kubernetes(minikube)を動かそうとしたときに、CentOS7にVirtualBoxをインストールする必要がありました。

その時の手順を記載しておこうと思います。

yumリポジトリをCentOSに追加

以下の公式サイトのページを参考に、VirtualBoxのyumリポジトリをCentOSに追加します。

※公式サイトのページ:https://www.virtualbox.org/wiki/Linux_Downloads
 「RPM-based Linux distributions」セクションの最下部をご覧ください。

$ sudo wget https://download.virtualbox.org/virtualbox/rpm/el/virtualbox.repo -O /etc/yum.repos.d/virtualbox.repo

改竄有無チェック用の公開鍵をCentOSにインポート

先ほどの公式サイトのページには

Note that importing the key is not necessary for yum users (Oracle Linux/Fedora/RHEL/CentOS) when using one of the virtualbox.repo files from below as yum downloads and imports the public key automatically!

(Google翻訳)yumは公開キーを自動的にダウンロードしてインポートするため、下からvirtualbox.repoファイルのいずれかを使用する場合、yumユーザー(Oracle Linux / Fedora / RHEL / CentOS)にはキーをインポートする必要はありません。

と書かれていますが、実際にはそんなことはありません。

なぜなら、後ほどインストール可能なバージョンを確認するために

yum list |grep Virtual

をする必要があり、事前に改竄有無チェックのための公開鍵がCentOSにインポートされている必要があるからです。なので、この公開鍵のインポートは結局必要となります。
(もちろん、インストール可能なバージョンが事前に分かっている場合はこの限りではありません。)

公式サイトに記載されている通り、公開鍵をインポートします。

wget https://www.virtualbox.org/download/oracle_vbox.asc
sudo rpm --import oracle_vbox.asc

VirtualBoxをインストール

インストール可能なVirtualBoxのバージョンを確認します。

$ yum list |grep Virtual

VirtualBox-4.3.x86_64                       4.3.40_110317_el7-1        virtualbox
VirtualBox-5.0.x86_64                       5.0.40_115130_el7-1        virtualbox
VirtualBox-5.1.x86_64                       5.1.38_122592_el7-1        virtualbox
VirtualBox-5.2.x86_64                       5.2.38_136252_el7-1        virtualbox
VirtualBox-6.0.x86_64                       6.0.18_136238_el7-1        virtualbox
VirtualBox-6.1.x86_64                       6.1.4_136177_el7-1         virtualbox

最新版をインストールします。

sudo yum install VirtualBox-6.1.x86_64

カーネルモジュールをインストール

virtualboxが動くか確認してみましょう。

$ virtualbox

WARNING: The vboxdrv kernel module is not loaded. Either there is no module
         available for the current kernel (3.10.0-957.12.2.el7.x86_64) or it failed to
         load. Please recompile the kernel module and install it by

           sudo /sbin/vboxconfig

         You will not be able to start VMs until this problem is fixed.
Qt FATAL: QXcbConnection: Could not connect to display 
Aborted

エラーとなってしまいました。エラメッセージにsudo /sbin/vboxconfigを実行せよ、と書いてありますので、素直に実行してみます。

$ sudo /sbin/vboxconfig

vboxdrv.sh: Stopping VirtualBox services.
vboxdrv.sh: Starting VirtualBox services.
vboxdrv.sh: Building VirtualBox kernel modules.
This system is currently not set up to build kernel modules.
Please install the gcc make perl packages from your distribution.
Please install the Linux kernel "header" files matching the current kernel
for adding new hardware support to the system.
The distribution packages containing the headers are probably:
    kernel-devel kernel-devel-3.10.0-957.12.2.el7.x86_64

エラーとなりました。エラーメッセージを見てみると、

Please install the gcc make perl packages from your distribution.

とのことなので素直にインストールします。

$ sudo yum install gcc make perl

さらに、エラーメッセージには以下の記載もありました。

Please install the Linux kernel "header" files matching the current kernel for adding new hardware support to the system.
The distribution packages containing the headers are probably:
    kernel-devel kernel-devel-3.10.0-957.12.2.el7.x86_64

kernel-develが無いということなので、こちらも素直にインストールします。

$ sudo yum install kernel-devel

設定を有効にするためにCentOSを再起動して、リトライです。(再起動は不要なのかもしれません)

$ sudo /sbin/vboxconfig

Please install the Linux kernel "header" files matching the current kernel for adding new hardware support to the system.
The distribution packages containing the headers are probably:
    kernel-devel kernel-devel-3.10.0-957.12.2.el7.x86_64

あれ?「kernel-devel」が無い、ということで、同じエラーです。

SELinuxが関係してるのかなと思い、SELinuxをDisabledにしてみたのですが、状況は変わりませんでした。

色々と調べてみると、CentOSのkernelのバージョンに対応するkernel-develがインストールされていないことが原因のようでした。

$ sudo yum install kernel-devel

としてしまうと、最新版のkenel-develがインストールされてしまいます。最新版のkenel-develは、CentOSのkernelのバージョンと整合性が無いので、エラーが起きてしまったのです。

詳しくは以下の解説をご覧ください。大変参考になります。こちらに記載の手順を実施して、CentOSのkernelのバージョンと整合性があるkernel-develをインストールしましょう。
 https://qiita.com/metheglin/items/60261f474ccdfb467574

その後、リトライしてみます。

$ sudo /sbin/vboxconfig

vboxdrv.sh: Stopping VirtualBox services.
vboxdrv.sh: Starting VirtualBox services.
vboxdrv.sh: Building VirtualBox kernel modules.

やっとうまくいきました!

おまけ

今回は、macのVirtualBox上でCentOS7を動かし、そのCentOS上でVirtualBoxを動かそうとしていました。それによって、CentOS上でkubernetes(minikube)を動かそうとしました。

こういったVMの中でさらにVMを動かす、ということを「ネステッドVM」と呼びます。

しかし、このネステッドVMは、macのVirutualBoxでは使えませんでした。AMDのCPUしか対応してないらしく、macのIntel CPUでは使えないとのこと。せっかく本記事のように頑張ったのに、残念でした・・・。

VirtualBox上のCentOS7でkubernetesを動かすのは諦めて、mac上で動かしてみることにしました(おわり)。

0
1
0

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
0
1