zowのプログラムな日々

日々のプログラミングとか

VagrantのBase Boxを作成する - ubuntu14.04 Server編(随時更新)

Vagrant関連の検索で訪れる人が多いのでまとめておく。

今後、VagrantのBox作成関連はこのページを随時更新する形にしたいと思う。

作業の流れ

  1. Box作成前の確認事項
  2. VirtualBoxubuntuインストール
  3. VirtualBoxssh接続
  4. root設定
  5. vagrantユーザ設定
  6. システム設定
  7. 共有ディレクトリ設定
  8. vagrantパッケージ作成
  9. box登録
  10. 確認

Box作成前の確認事項

32bitか64bitか

ゲストOSに割り当てるメモリ量が4GB以下であれば32bit、それより大きければ64bitというのが目安

基本的にスクリプト言語の開発環境と言うことで考えるのであれば、言語側でその差異を吸収してくれているので、作成した物はどちらの環境で作ったものでも双方の環境で動く。

複数環境起動したりする事を考えるのであれば32bit版で動かしてメモリを節約したほうがいいと思う。

※ もちろん、必要なパッケージが64bit版しか提供されていなかったりする場合はこの限りでは無い。

仮想ディスクについて

仮想ディスクはvdiで作成。初期サイズは4GBにして、足りなければ拡張する

以下は4GBで作成した場合のディスクサイズになる。

base32_4gb_ubuntu1404 13:55:31 zow$ ls -l
total 4463072
drwx------  4 zow  staff         136  1 31 06:53 Logs
-rw-------  1 zow  staff        8167  1 31 13:54 base32_4gb_ubuntu1404.vbox
-rw-------  1 zow  staff        8047  1 31 08:05 base32_4gb_ubuntu1404.vbox-prev
-rw-------  1 zow  staff  1778384896  1 31 07:57 base32_4gb_ubuntu1404.vdi
-rw-r--r--  1 zow  staff   506689293  1 31 08:07 package.box
base32_4gb_ubuntu1404 13:55:35 zow$ 

システム内部からみるとこんな感じになる。

vagrant@base32-4gb-ubuntu1404:~$ df -m
Filesystem     1M-blocks  Used Available Use% Mounted on
/dev/sda1           3463  1131      2137  35% /
none                   1     0         1   0% /sys/fs/cgroup
udev                 239     1       239   1% /dev
tmpfs                 50     1        50   1% /run
none                   5     0         5   0% /run/lock
none                 248     0       248   0% /run/shm
none                 100     0       100   0% /run/user
vagrant           114538 40051     74488  35% /vagrant
vagrant@base32-4gb-ubuntu1404:~$ 

4GBのうち、500MBぐらいはスワップ領域で使用しているため、実際に使用できる領域は約3.5GB。

現在はほぼシステムのみをインストールした状態で、使用しているのは1GB強。空き領域は2GB強になる。

これだけの領域が余っていれば、開発環境として充分だと思う。

「/vagrant」はホストOS側の領域になるので、最悪、ここを利用すれば問題無いと思う。

もし、それ以上の空き領域が必要なのであれば、vdiで作成しているので、簡単に領域を増やすことが出来る。

使用するインストールOSイメージ

余程の事情が無ければインストール時に使用するOSイメージはminimalを使用した方が良い

Installation/MinimalCD - Community Help Wiki

普通のフルOSイメージを使うと、インストール後にアップデートする必要が出てくる。その際、古いパッケージが新しくなったりする過程でいらないパッケージや、古いカーネルイメージ等が残ってしまう。仮想ディスクのサイズを減らすために、これらの不必要なパッケージを削除する事になるのだけど、そんな手間を掛けるぐらいなら、最初から最新の物をインストールしてしまえば良い。

minimalのイメージは小さい為、インストールするパッケージがイメージ内に無い。インストールするパッケージはネットから最新の物をダウンロードしてくる。なので、インストール直後はアップデートの必要が無い最新の環境が出来上がる。ネットからのインストールになる為多少時間はかかるが、初期サイズが小さいに越したことは無いので、VagrantのBoxを作る場合はminimalを使用した方がいいと思う。

VirtualBoxubuntuインストール

以下の設定値でディスクを作成する。

設定項目 設定値
名前 base32_4gb_ubuntu1404
タイプ Linux
バージョン Ubuntu(32 bit)
メモリ 512MB
仮想ハードドライブ 作成する
ファイルタイプ VDI
ストレージ 可変
ファイルの名前 base32_4gb_ubuntu1404
ファイルのサイズ 4GB
オーディオ 無効
USB 無効

OS設定項目

項目 設定値
ホスト名 base32-4gb-ubuntu1404
ユーザ名 vagrant
パスワード vagrant
Timezone Asia/Tokyo
パーティション ガイド-ディスク全体を使う
アップグレード 自動的にアップデートしない
インストールするソフトウェア Basic Ubuntu server, OpenSSH server

VirtualBoxssh接続

まず、ゲストOSのIPアドレスを調べる。

vagrant@base32-4gb-ubuntu1404:~$ ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:ba:fa:2f  
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:feba:fa2f/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:164 errors:0 dropped:0 overruns:0 frame:0
          TX packets:126 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:20330 (20.3 KB)  TX bytes:21447 (21.4 KB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

vagrant@base32-4gb-ubuntu1404:~$ 

ここでVirtualBoxで、[ネットワーク]-[アダプター1]-[高度な設定]-[ポートフォワーディング]を開き、VMの22番ポートをホスト側の任意のポートに割り当ててやる。

f:id:zow3:20150131065955p:plain

設定したら、ホストOSからssh接続出来るようになる。ポート指定で接続する。

~ 06:57:33 zow$ ssh vagrant@127.0.0.1 -p 2222
The authenticity of host '[127.0.0.1]:2222 ([127.0.0.1]:2222)' can't be established.
RSA key fingerprint is 8e:bc:3c:1a:4c:0c:dc:7a:bc:fa:87:64:40:fa:00:5d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[127.0.0.1]:2222' (RSA) to the list of known hosts.
vagrant@127.0.0.1's password: 
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-44-generic i686)

 * Documentation:  https://help.ubuntu.com/

  System information as of Sat Jan 31 06:56:52 JST 2015

  System load:  0.03              Processes:           69
  Usage of /:   30.6% of 3.38GB   Users logged in:     1
  Memory usage: 6%                IP address for eth0: 10.0.2.15
  Swap usage:   0%

  Graph this data and manage this system at:
    https://landscape.canonical.com/

0 packages can be updated.
0 updates are security updates.

Last login: Sat Jan 31 06:56:52 2015 from 10.0.2.2
vagrant@base32-4gb-ubuntu1404:~$ 

root設定

rootのパスワードを「vagrant」にする。

vagrant@base32-4gb-ubuntu1404:~$ sudo passwd root
[sudo] password for vagrant: 
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
vagrant@base32-4gb-ubuntu1404:~$ 

変更したら、設定したパスワードでrootになれる事を確認する。

vagrant@base32-4gb-ubuntu1404:~$ su - 
Password: 
root@base32-4gb-ubuntu1404:~# 

vagrantユーザ設定

sudoの設定

パスワード無しでsudo出来るようにする。

まず、vagrantユーザの所属グループを確認する。

vagrant@base32-4gb-ubuntu1404:~$ id vagrant
uid=1000(vagrant) gid=1000(vagrant) groups=1000(vagrant),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),110(lpadmin),111(sambashare)
vagrant@base32-4gb-ubuntu1404:~$ 

「sudo」グループに所属している。このグループに所属しているユーザはsudoが可能になっている。このグループに対してパスワード無しでsudoを利用出来るように設定してやる。

vagrant@base32-4gb-ubuntu1404:~$ sudo visudo

「/etc/sudoer」の以下の行を変更する。

%sudo    ALL=(ALL:ALL) ALL
    ↓
%sudo   ALL=(ALL) NOPASSWD:ALL

変更後はこうなる。

vagrant@base32-4gb-ubuntu1404:~$ sudo cat /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults    env_reset
Defaults    mail_badpass
Defaults    secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
#%sudo  ALL=(ALL:ALL) ALL
%sudo   ALL=(ALL) NOPASSWD:ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d
vagrant@base32-4gb-ubuntu1404:~$ 

変更後、一度ログアウトし、再接続後にパスワード無しでsudoできる事を確認する。

~ 07:12:34 zow$ ssh vagrant@127.0.0.1 -p 2222
vagrant@127.0.0.1's password: 
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-44-generic i686)

 * Documentation:  https://help.ubuntu.com/

  System information as of Sat Jan 31 06:58:36 JST 2015

  System load:  0.0               Processes:           65
  Usage of /:   30.6% of 3.38GB   Users logged in:     1
  Memory usage: 6%                IP address for eth0: 10.0.2.15
  Swap usage:   0%

  Graph this data and manage this system at:
    https://landscape.canonical.com/

0 packages can be updated.
0 updates are security updates.

Last login: Sat Jan 31 06:58:36 2015 from 10.0.2.2
vagrant@base32-4gb-ubuntu1404:~$ sudo -s
root@base32-4gb-ubuntu1404:~# 

ssh設定

vagrantユーザ用の鍵が用意されているので、それを設定しておく。

鍵はこちら

鍵格納用のディレクトリを作成する。

vagrant@base32-4gb-ubuntu1404:~$ mkdir ~/.ssh
vagrant@base32-4gb-ubuntu1404:~$ ls -la ~/ |grep .ssh
drwxrwxr-x 2 vagrant vagrant 4096  1月 31 07:16 .ssh
vagrant@base32-4gb-ubuntu1404:~$ chmod 0700 ~/.ssh
vagrant@base32-4gb-ubuntu1404:~$ ls -la ~/ |grep .ssh
drwx------ 2 vagrant vagrant 4096  1月 31 07:16 .ssh
vagrant@base32-4gb-ubuntu1404:~$ 

次に鍵格納用のファイルを作成する。

vagrantユーザ用の公開鍵をDLし、リネームして使う。

公開鍵は以下URLからDL出来る。

https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub
vagrant@base32-4gb-ubuntu1404:~$ cd ~/.ssh
vagrant@base32-4gb-ubuntu1404:~/.ssh$ ls -la
合計 8
drwx------ 2 vagrant vagrant 4096  1月 31 07:16 .
drwxr-xr-x 4 vagrant vagrant 4096  1月 31 07:16 ..
vagrant@base32-4gb-ubuntu1404:~/.ssh$ wget https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub
--2015-01-31 07:18:52--  https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub
raw.github.com (raw.github.com) をDNSに問いあわせています... 103.245.222.133
raw.github.com (raw.github.com)|103.245.222.133|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 301 Moved Permanently
場所: https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub [続く]
--2015-01-31 07:18:55--  https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub
raw.githubusercontent.com (raw.githubusercontent.com) をDNSに問いあわせています... 103.245.222.133
raw.githubusercontent.com (raw.githubusercontent.com)|103.245.222.133|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 409 [text/plain]
`vagrant.pub' に保存中

100%[======================================>] 409         --.-K/s   時間 0s    

2015-01-31 07:18:55 (8.24 MB/s) - `vagrant.pub' へ保存完了 [409/409]

vagrant@base32-4gb-ubuntu1404:~/.ssh$ ls -la
合計 12
drwx------ 2 vagrant vagrant 4096  1月 31 07:18 .
drwxr-xr-x 4 vagrant vagrant 4096  1月 31 07:16 ..
-rw-rw-r-- 1 vagrant vagrant  409  1月 31 07:18 vagrant.pub
vagrant@base32-4gb-ubuntu1404:~/.ssh$ mv vagrant.pub authorized_keys
vagrant@base32-4gb-ubuntu1404:~/.ssh$ ls -la
合計 12
drwx------ 2 vagrant vagrant 4096  1月 31 07:21 .
drwxr-xr-x 4 vagrant vagrant 4096  1月 31 07:16 ..
-rw-rw-r-- 1 vagrant vagrant  409  1月 31 07:18 authorized_keys
vagrant@base32-4gb-ubuntu1404:~/.ssh$ chmod 0600 authorized_keys 
vagrant@base32-4gb-ubuntu1404:~/.ssh$ ls -la
合計 12
drwx------ 2 vagrant vagrant 4096  1月 31 07:21 .
drwxr-xr-x 4 vagrant vagrant 4096  1月 31 07:16 ..
-rw------- 1 vagrant vagrant  409  1月 31 07:18 authorized_keys
vagrant@base32-4gb-ubuntu1404:~/.ssh$ 

システム設定

sshd設定

ssh接続時にdns参照させるのを防ぐ為、以下設定を「/etc/ssh/sshd_config」に追記する。

UseDNS no

設定後はこうなる。

vagrant@base32-4gb-ubuntu1404:~$ cat /etc/ssh/sshd_config 
# Package generated configuration file
# See the sshd_config(5) manpage for details

# What ports, IPs and protocols we listen for
Port 22
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes

# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 1024

# Logging
SyslogFacility AUTH
LogLevel INFO

# Authentication:
LoginGraceTime 120
PermitRootLogin without-password
StrictModes yes

RSAAuthentication yes
PubkeyAuthentication yes
#AuthorizedKeysFile %h/.ssh/authorized_keys

# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes

# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no

# Change to no to disable tunnelled clear text passwords
#PasswordAuthentication yes

# Kerberos options
#KerberosAuthentication no
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes

X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no

#MaxStartups 10:30:60
#Banner /etc/issue.net

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

Subsystem sftp /usr/lib/openssh/sftp-server

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes
UseDNS no
vagrant@base32-4gb-ubuntu1404:~$ 

設定変更後にsshdを再起動しておく。

vagrant@base32-4gb-ubuntu1404:~$ sudo service ssh restart
ssh stop/waiting
ssh start/running, process 1353
vagrant@base32-4gb-ubuntu1404:~$ 

ビルドツールインストール

コンパイラ等のビルドツールをインストールしておく。

vagrant@base32-4gb-ubuntu1404:~$ sudo apt-get install linux-headers-generic build-essential dkms
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています                
状態情報を読み取っています... 完了
linux-headers-generic は既に最新バージョンです。
以下の特別パッケージがインストールされます:
  binutils cpp cpp-4.8 dpkg-dev fakeroot g++ g++-4.8 gcc gcc-4.8
  libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl
  libasan0 libatomic1 libc-dev-bin libc6-dev libcloog-isl4 libdpkg-perl
  libfakeroot libfile-fcntllock-perl libgcc-4.8-dev libgmp10 libgomp1 libisl10
  libitm1 libmpc3 libmpfr4 libquadmath0 libstdc++-4.8-dev linux-libc-dev make
  manpages-dev
提案パッケージ:
  binutils-doc cpp-doc gcc-4.8-locales debhelper debian-keyring g++-multilib
  g++-4.8-multilib gcc-4.8-doc libstdc++6-4.8-dbg gcc-multilib autoconf
  automake1.9 libtool flex bison gdb gcc-doc gcc-4.8-multilib libgcc1-dbg
  libgomp1-dbg libitm1-dbg libatomic1-dbg libasan0-dbg libtsan0-dbg
  libbacktrace1-dbg libquadmath0-dbg binutils-gold glibc-doc libstdc++-4.8-doc
  make-doc
以下のパッケージが新たにインストールされます:
  binutils build-essential cpp cpp-4.8 dkms dpkg-dev fakeroot g++ g++-4.8 gcc
  gcc-4.8 libalgorithm-diff-perl libalgorithm-diff-xs-perl
  libalgorithm-merge-perl libasan0 libatomic1 libc-dev-bin libc6-dev
  libcloog-isl4 libdpkg-perl libfakeroot libfile-fcntllock-perl libgcc-4.8-dev
  libgmp10 libgomp1 libisl10 libitm1 libmpc3 libmpfr4 libquadmath0
  libstdc++-4.8-dev linux-libc-dev make manpages-dev
アップグレード: 0 個、新規インストール: 34 個、削除: 0 個、保留: 0 個。
28.0 MB のアーカイブを取得する必要があります。
この操作後に追加で 97.3 MB のディスク容量が消費されます。
…

共有ディレクトリ設定

共有ディレクトリを使用出来るようにする為、「VBoxGuestAdditions」を公式からDLして「VBoxLinuxAdditions」をインストールする。

http://download.virtualbox.org/virtualbox/

使用しているVirtualboxのバージョンに合わせた物をゲストOS内からwgetでDLし(今回は4.3.20)、マウントする。

vagrant@base32-4gb-ubuntu1404:~$ cd /tmp
vagrant@base32-4gb-ubuntu1404:/tmp$ wget http://download.virtualbox.org/virtualbox/4.3.20/VBoxGuestAdditions_4.3.20.iso
--2015-01-31 07:34:04--  http://download.virtualbox.org/virtualbox/4.3.20/VBoxGuestAdditions_4.3.20.iso
download.virtualbox.org (download.virtualbox.org) をDNSに問いあわせています... 137.254.120.26
download.virtualbox.org (download.virtualbox.org)|137.254.120.26|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 302 Moved Temporarily
場所: http://dlc-cdn.sun.com/virtualbox/4.3.20/VBoxGuestAdditions_4.3.20.iso [続く]
--2015-01-31 07:34:06--  http://dlc-cdn.sun.com/virtualbox/4.3.20/VBoxGuestAdditions_4.3.20.iso
dlc-cdn.sun.com (dlc-cdn.sun.com) をDNSに問いあわせています... 72.246.190.66, 72.246.190.67
dlc-cdn.sun.com (dlc-cdn.sun.com)|72.246.190.66|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 58343424 (56M) [application/octet-stream]
`VBoxGuestAdditions_4.3.20.iso' に保存中

100%[======================================>] 58,343,424  3.26MB/s   時間 16s  

2015-01-31 07:34:22 (3.52 MB/s) - `VBoxGuestAdditions_4.3.20.iso' へ保存完了 [58343424/58343424]

vagrant@base32-4gb-ubuntu1404:/tmp$ ls -l
合計 56976
-rw-rw-r-- 1 vagrant vagrant 58343424 11月 21 23:01 VBoxGuestAdditions_4.3.20.iso
vagrant@base32-4gb-ubuntu1404:/tmp$ df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda1        3545824 1141960   2204032  35% /
none                   4       0         4   0% /sys/fs/cgroup
udev              243932       4    243928   1% /dev
tmpfs              50720     368     50352   1% /run
none                5120       0      5120   0% /run/lock
none              253600       0    253600   0% /run/shm
none              102400       0    102400   0% /run/user
vagrant@base32-4gb-ubuntu1404:/tmp$ 
vagrant@base32-4gb-ubuntu1404:/tmp$ sudo mount -t iso9660 /tmp/VBoxGuestAdditions_4.3.20.iso /mnt
mount: ブロックデバイス /tmp/VBoxGuestAdditions_4.3.20.iso は書き込み禁止です、読込み専用でマウントします
vagrant@base32-4gb-ubuntu1404:/tmp$ df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda1        3545824 1141960   2204032  35% /
none                   4       0         4   0% /sys/fs/cgroup
udev              243932       4    243928   1% /dev
tmpfs              50720     368     50352   1% /run
none                5120       0      5120   0% /run/lock
none              253600       0    253600   0% /run/shm
none              102400       0    102400   0% /run/user
/dev/loop1         56976   56976         0 100% /mnt
vagrant@base32-4gb-ubuntu1404:/tmp$ 

/mntにマウントされたので、そこにある「VBoxLinuxAdditions.run」を実行する。

vagrant@base32-4gb-ubuntu1404:/tmp$ cd /mnt/
vagrant@base32-4gb-ubuntu1404:/mnt$ ls -l
合計 50808
dr-xr-xr-x 2 root root     2048 11月 21 23:01 32Bit
dr-xr-xr-x 2 root root     2048 11月 21 23:01 64Bit
-r-xr-xr-x 1 root root      647  9月 12 21:10 AUTORUN.INF
dr-xr-xr-x 2 root root     2048 11月 21 23:01 OS2
-r-xr-xr-x 1 root root  7146957 11月 21 23:58 VBoxLinuxAdditions.run
-r-xr-xr-x 1 root root 16434176 11月 21 23:59 VBoxSolarisAdditions.pkg
-r-xr-xr-x 1 root root 17356976 11月 22 00:00 VBoxWindowsAdditions-amd64.exe
-r-xr-xr-x 1 root root 10754048 11月 21 23:57 VBoxWindowsAdditions-x86.exe
-r-xr-xr-x 1 root root   312384 11月 21 23:56 VBoxWindowsAdditions.exe
-r-xr-xr-x 1 root root     6966 11月 21 23:57 autorun.sh
dr-xr-xr-x 2 root root     2048 11月 21 23:01 cert
-r-xr-xr-x 1 root root     5523 11月 21 23:57 runasroot.sh
vagrant@base32-4gb-ubuntu1404:/mnt$ sudo ./VBoxLinuxAdditions.run 
Verifying archive integrity... All good.
Uncompressing VirtualBox 4.3.20 Guest Additions for Linux............
VirtualBox Guest Additions installer
Removing installed version 4.3.20 of VirtualBox Guest Additions...
Copying additional installer modules ...
Installing additional modules ...
Removing existing VirtualBox DKMS kernel modules ...done.
Removing existing VirtualBox non-DKMS kernel modules ...done.
Building the VirtualBox Guest Additions kernel modules ...done.
Doing non-kernel setup of the Guest Additions ...done.
Starting the VirtualBox Guest Additions ...done.
Installing the Window System drivers
Could not find the X.Org or XFree86 Window System, skipping.
vagrant@base32-4gb-ubuntu1404:/mnt$ 

実行後、使用したCDイメージをアンマウントしてから削除する。

vagrant@base32-4gb-ubuntu1404:/mnt$ cd /tmp
vagrant@base32-4gb-ubuntu1404:/tmp$ df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda1        3545824 1290600   2055392  39% /
none                   4       0         4   0% /sys/fs/cgroup
udev              243932       4    243928   1% /dev
tmpfs              50720     384     50336   1% /run
none                5120       0      5120   0% /run/lock
none              253600       0    253600   0% /run/shm
none              102400       0    102400   0% /run/user
/dev/loop1         56976   56976         0 100% /mnt
vagrant@base32-4gb-ubuntu1404:/tmp$ sudo umount /mnt
vagrant@base32-4gb-ubuntu1404:/tmp$ df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda1        3545824 1290608   2055384  39% /
none                   4       0         4   0% /sys/fs/cgroup
udev              243932       4    243928   1% /dev
tmpfs              50720     384     50336   1% /run
none                5120       0      5120   0% /run/lock
none              253600       0    253600   0% /run/shm
none              102400       0    102400   0% /run/user
vagrant@base32-4gb-ubuntu1404:/tmp$ ls -l
合計 56980
-rw-rw-r-- 1 vagrant vagrant 58343424 11月 21 23:01 VBoxGuestAdditions_4.3.20.iso
vagrant@base32-4gb-ubuntu1404:/tmp$ rm -f VBoxGuestAdditions_4.3.20.iso 
vagrant@base32-4gb-ubuntu1404:/tmp$ ls -l
合計 0
vagrant@base32-4gb-ubuntu1404:/tmp$ 

vboxaddのセットアップを実行し、共有ディレクトリを設定する。

vagrant@base32-4gb-ubuntu1404:/tmp$ sudo /etc/init.d/vboxadd setup
Removing existing VirtualBox DKMS kernel modules ...done.
Removing existing VirtualBox non-DKMS kernel modules ...done.
Building the VirtualBox Guest Additions kernel modules ...done.
Doing non-kernel setup of the Guest Additions ...done.
You should restart your guest to make sure the new modules are actually used

vagrant@base32-4gb-ubuntu1404:/tmp$ 

Vagrantパッケージ作成

掃除

ゲストOSのセットアップは終わったので、綺麗にしておく。

vagrant@base32-4gb-ubuntu1404:~$ sudo apt-get autoremove 
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています                
状態情報を読み取っています... 完了
アップグレード: 0 個、新規インストール: 0 個、削除: 0 個、保留: 0 個。
vagrant@base32-4gb-ubuntu1404:~$ sudo apt-get clean
vagrant@base32-4gb-ubuntu1404:~$ 

最後にフラグメントを解消してやる

vagrant@base32-4gb-ubuntu1404:~$ cd /tmp
vagrant@base32-4gb-ubuntu1404:/tmp$ ls -l
合計 0
vagrant@base32-4gb-ubuntu1404:/tmp$ dd if=/dev/zero of=/tmp/ZERO bs=1M
dd: `/tmp/ZERO' の書き込みエラー: デバイスに空き領域がありません
2129+0 レコード入力
2128+0 レコード出力
2232172544 バイト (2.2 GB) コピーされました、 6.59424 秒、 339 MB/秒
vagrant@base32-4gb-ubuntu1404:/tmp$ ls -l
合計 2179864
-rw-rw-r-- 1 vagrant vagrant 2232172544  1月 31 07:55 ZERO
vagrant@base32-4gb-ubuntu1404:/tmp$ rm /tmp/ZERO 
vagrant@base32-4gb-ubuntu1404:/tmp$ ls -l
合計 0
vagrant@base32-4gb-ubuntu1404:/tmp$ 

シャットダウンする。

vagrant@base32-4gb-ubuntu1404:/tmp$ sudo shutdown -h now
vagrant@base32-4gb-ubuntu1404:/tmp$ 
vagrant@base32-4gb-ubuntu1404 からのブロードキャスト・メッセージ
    (/dev/pts/0) at 7:57 ...

The system is going down for halt NOW!
Connection to 127.0.0.1 closed by remote host.
Connection to 127.0.0.1 closed.
~ 07:57:26 zow$ 

ホストOSでbase boxを作成する。

base32_4gb_ubuntu1404 08:04:47 zow$ ls -l
total 3473440
drwx------  4 zow  staff         136  1 31 06:53 Logs
-rw-------  1 zow  staff        8167  1 31 07:57 base32_4gb_ubuntu1404.vbox
-rw-------  1 zow  staff        7402  1 31 07:57 base32_4gb_ubuntu1404.vbox-prev
-rw-------  1 zow  staff  1778384896  1 31 07:57 base32_4gb_ubuntu1404.vdi
base32_4gb_ubuntu1404 08:04:49 zow$ vagrant package --base base32_4gb_ubuntu1404 
==> base32_4gb_ubuntu1404: Clearing any previously set forwarded ports...
==> base32_4gb_ubuntu1404: Exporting VM...
==> base32_4gb_ubuntu1404: Compressing package to: /Users/zow/VirtualBox VMs/base32_4gb_ubuntu1404/package.box
base32_4gb_ubuntu1404 08:07:05 zow$ ls -l
total 4463072
drwx------  4 zow  staff         136  1 31 06:53 Logs
-rw-------  1 zow  staff        8047  1 31 08:05 base32_4gb_ubuntu1404.vbox
-rw-------  1 zow  staff        8167  1 31 07:57 base32_4gb_ubuntu1404.vbox-prev
-rw-------  1 zow  staff  1778384896  1 31 07:57 base32_4gb_ubuntu1404.vdi
-rw-r--r--  1 zow  staff   506689293  1 31 08:07 package.box
base32_4gb_ubuntu1404 08:07:34 zow$ 

Box登録

作成したパッケージを登録する。

base32_4gb_ubuntu1404 08:10:58 zow$ vagrant box add base32_4gb_ubuntu1404 package.box 
==> box: Adding box 'base32_4gb_ubuntu1404' (v0) for provider: 
    box: Downloading: file:///Users/zow/VirtualBox%20VMs/base32_4gb_ubuntu1404/package.box
==> box: Successfully added box 'base32_4gb_ubuntu1404' (v0) for 'virtualbox'!
base32_4gb_ubuntu1404 08:12:01 zow$

登録されていることを確認。

virtualbox 08:14:42 zow$ vagrant box list
base32_4gb_ubuntu1404 (virtualbox, 0)
virtualbox 08:14:49 zow$ 

確認

仮想環境を作ってみる。

~ 08:20:54 zow$ mkdir -p ~/dev/vagrant/vagrant_test
~ 08:21:00 zow$ cd ~/dev/vagrant/vagrant_test/
vagrant_test 08:21:10 zow$ vagrant init base32_4gb_ubuntu1404
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
vagrant_test 08:21:53 zow$ ls -l 
total 8
-rw-r--r--  1 zow  staff  3033  1 31 08:21 Vagrantfile
vagrant_test 08:22:21 zow$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'base32_4gb_ubuntu1404'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vagrant_test_default_1422660173796_41835
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> 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: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if its present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /Users/zow/dev/vagrant/vagrant_test
vagrant_test 08:23:14 zow$ 

正常に起動した。

ssh接続する。

vagrant_test 08:24:07 zow$ vagrant ssh
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-44-generic i686)

 * Documentation:  https://help.ubuntu.com/

  System information as of Sat Jan 31 08:23:11 JST 2015

  System load:  0.22              Processes:           76
  Usage of /:   32.6% of 3.38GB   Users logged in:     0
  Memory usage: 6%                IP address for eth0: 10.0.2.15
  Swap usage:   0%

  Graph this data and manage this system at:
    https://landscape.canonical.com/

Last login: Sat Jan 31 07:13:31 2015 from 10.0.2.2
vagrant@base32-4gb-ubuntu1404:~$ cd /vagrant/
vagrant@base32-4gb-ubuntu1404:/vagrant$ ls -l
合計 4
-rw-r--r-- 1 vagrant vagrant 3033  1月 31 08:21 Vagrantfile
vagrant@base32-4gb-ubuntu1404:/vagrant$ 

共有ディレクトリも問題なく使える。