LoginSignup
4
5

More than 5 years have passed since last update.

VirtualBoxのCentOS、VagrantBoxを作成する

Last updated at Posted at 2015-11-23

日頃ありがたく利用させてもらっているVagrantのBaseboxをCentOSのisoファイルから作成します。
VagrantのBasebox作成にあたっては Vagarnt公式のCREATING A BASE BOX を参考にしています。

前提条件

作業環境はMac OS Xで、事前にhomebrewで VagrantVirtual Boxはインストール済みとします。
それぞれの細かいバージョン

  • Mac OS 10.10.5
  • VirtualBox 5.0.8
  • Vagrant 1.7.4

VirtualBoxにCentOS6.7をVirtualBoxでインストールし仮想サーバーを作成する

ISOファイルをダウンロードする

CentOSの CentOS公式サイト からOS ISOファイル(CentOS-6.7-x86_64-minimal.iso)をダウンロードします。

VirtualBoxのデバイス設定

公式ドキュメントを参考に以下の各デバイスをオフにする

  • USBデバイス
  • Audioデバイス

Usb.png

Audio.png

インストールディスクを作成する

  • VirtuaBox起動 -> Oracle VM VirtualBoxマネージャー -> 新規
  • 名前とオペレーティングシステム
    • 名前: centos67_minimal_ja
    • タイプ: Linux
    • バージョン: Linux2.6 / 3.x / 4.x (64-bit)
  • メモリーサイズ 512MB
  • 仮想ハードディスク 8GB、可変のVMDK形式

CentOS6.7をインストールする

ダウンロード済みのCentOS6.7 ISOファイルをVirtualBox
Oracle VM VirtualBoxマネージャー -> 設定 -> ストレージ -> (ストレージツリー)コントローラーからディスクを選択し、追加します。

iso.png

CentOSインストール時は以下の設定

  • 言語設定 Japanese、キーボードjaを選択。
  • 時刻 Asia/Tokyoを選択。
  • rootパスワードは公式ドキュメントにしたがって vagrantを設定。

ブート時にネットワークが起動するように

/etc/sysconfig/network-scripts/ifcfg-eth0の設定を ONBOOG=yesに変更

  • Selinuxをオフにする
[root@localhost sysconfig]# cp -p /etc/sysconfig/selinux /etc/sysconfig/selinux.org
[root@localhost sysconfig]# diff /etc/sysconfig/selinux /etc/sysconfig/selinux.org
7c7
< SELINUX=disabled
---
> SELINUX=enforcing
  • Iptablesをオフにする
[root@localhost sysconfig]# /sbin/chkconfig iptables off
[root@localhost sysconfig]# /sbin/chkconfig ip6tables off

公開鍵認証設定

公開鍵認証の設定とVagrant推奨設定

[root@localhost ~]# vi /etc/ssh/sshd_config

修正点

# diff sshd_config sshd_config.org
48,49c48,49
< PubkeyAuthentication yes
< AuthorizedKeysFile    .ssh/authorized_keys
---
> #PubkeyAuthentication yes
> #AuthorizedKeysFile   .ssh/authorized_keys
66c66
< PasswordAuthentication no
---
> PasswordAuthentication yes
122c122
< UseDNS no
---
> #UseDNS yes

sshdを再起動

[root@localhost ssh]# /etc/init.d/sshd restart
sshd を停止中:                                             [  OK  ]
sshd を起動中:                                             [  OK  ]

vagrantユーザーの作成と設定

  • vagrantユーザーを作成する
[root@localhost ~]# useradd vagrant
  • vagrantユーザーのsudo設定をする

[root@localhost ~]# 
visudo

Defaults requirettyをコメントアウトし以下を追加。
vagrant ALL=(ALL) NOPASSWD: ALL を追加。

vagrantユーザーの公開鍵を追加する

公式ドキュメントで指定された、鍵ペアを利用する。

[root@localhost ~]# su - vagrant
[vagrant@localhost ~]$ mkdir .ssh
[vagrant@localhost ~]$ chmod 700 .ssh
[vagrant@localhost .ssh]$ curl https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub > authorized_keys
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 83013    0 83013    0     0  65489      0 --:--:--  0:00:01 --:--:--  129k
[vagrant@localhost .ssh]$ cat vagrant.pub > authorized_keys
[vagrant@localhost .ssh]$ chmod 600 authorized_keys

Boxを作成する

起動中の仮想サーバーを停止してから、パッケージ化する。
仮想サーバーの停止。

[root@localhost ~]# shutdown -h now

仮想サーバーをbox化。

$ vagrant package --base centos67_minimal_ja
==> centos67_minimal_ja: Clearing any previously set forwarded ports...
==> centos67_minimal_ja: Exporting VM...
==> centos67_minimal_ja: Compressing package to: /Users/xxxxxx/VirtualBox VMs/package.box

boxが作成されたので、vagrant box addする。

$ vagrant box add package.box --name centos67_minimal_ja
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos67_minimal_ja' (v0) for provider:
    box: Unpacking necessary files from: file:///Users/xxxxxxxx/VirtualBox%20VMs/package.box
==> box: Successfully added box 'centos67_minimal_ja' (v0) for 'virtualbox'!
  • vagrant boxが追加されていることを確認する
$ vagrant box list
centos67_minimal_ja (virtualbox, 0)

あとは、追加したVagrant Boxが vagrant init / vagrant up で正常に起動して、ログインできることを確認したら完了。

4
5
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
4
5