ログってなんぼ

日々のメモです

VirtualBox用に自作したVMからVagrantのBase Boxを作る

まずVMを作る

CentOS6.5_x86のVMを作りました。

f:id:Okisanjp:20160715122940p:plain

イザ作業開始

Creating a Base Box - Vagrant Documentation はてなブックマーク - Creating a Base Box - Vagrant Documentation

こちらを参考にしながら作業。

vagrant ユーザー

公式ドキュメントによると

  • vagrant ユーザーを作る
  • パスワードは vagrant
  • 公開鍵認証を行うためのkey
  • パスワード無しでsudo出来るように

という感じのガイドが書いてるのでとりあえずその通りにしてみます。

# visudo
## Same thing without a password
%wheel  ALL=(ALL) NOPASSWD: ALL

# vim /etc/ssh/sshd_config
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile  .ssh/authorized_keys

# /etc/init.d/sshd reload

# useradd vagrant
# passwd vagrant
# usermod -G wheel vagrant

公開鍵を登録

vagrant/keys at master · mitchellh/vagrant · GitHub はてなブックマーク - vagrant/keys at master · mitchellh/vagrant · GitHub

ここにあるらしい。

# mkdir /home/vagrant/.ssh
# chmod 700 /home/vagrant
# curl 'https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub' > /home/vagrant/.ssh/authorized_keys
# chmod 600 /home/vagrant/.ssh/authorized_keys
# chown -R vagrant:vagrant /home/vagrant/.ssh

host PCに戻ってパッケージ作成

$ vagrant package --base centos6.x86
[centos6.x86] Attempting graceful shutdown of VM...
[centos6.x86] Forcing shutdown of VM...
[centos6.x86] Clearing any previously set forwarded ports...
[centos6.x86] Creating temporary directory for export...
[centos6.x86] Exporting VM...
[centos6.x86] Compressing package to: /Users/okisanjp/package.box

でけた。

確認してみる

$ ls -lh /Users/okisanjp/package.box
-rw-r--r--  1 okisanjp  staff   493M  1 25 15:08 /Users/okisanjp/package.box

出来てますね。名前変えときましょう。

$ mv /Users/okisanjp/package.box /Users/okisanjp/vagrantbox_centos6.x86.box
/Users/okisanjp/package.box -> /Users/okisanjp/vagrantbox_centos6.x86.box
$ ls -lh /Users/okisanjp/vagrantbox_centos6.x86.box
-rw-r--r--  1 okisanjp  staff   493M  1 25 15:08 /Users/okisanjp/vagrantbox_centos6.x86.box

このファイルを配布する感じになりますね。

起動してみる

$ vagrant box add centos6 /Users/okisanjp/vagrantbox_centos6.x86.box
Downloading or copying the box...
Extracting box...te: 145M/s, Estimated time remaining: --:--:--)
Successfully added box 'centos6' with provider 'virtualbox'!

$ vagrant box list
centos6 (virtualbox)

$ mkdir centos6_new
$ cd centos6_new/

$ vagrant init centos6
A <code>Vagrantfile</code> has been placed in this directory. You are now
ready to <code>vagrant up</code> your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
<code>vagrantup.com</code> for more information on using Vagrant.
$ ls -l
total 16
-rw-r--r--  1 okisanjp  staff  4612  1 25 15:19 Vagrantfile

Vagrantfileが出来たので立ち上げてみます

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'centos6'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for machine to boot. This may take a few minutes...
[default] Machine booted and ready!
[default] No guest additions were detected on the base box for this VM! Guest
additions are required for forwarded ports, shared folders, host only
networking, and more. If SSH fails on this machine, please install
the guest additions and repackage the box to continue.

This is not an error message; everything may continue to work properly,
in which case you may ignore this message.
[default] Mounting shared folders...
[default] -- /vagrant

sshできるかな

$ vagrant ssh
[vagrant@localhost ~]$ sudo -s
[root@localhost /home/vagrant]#

OKです。