zowのプログラムな日々

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

VagrantのBoxを作成する(ubuntu 14.04 Server編)

追記:2015/02/06

※新しく記事を書き直しました。

今後は以下の新しい記事の方を随時更新していきます。

VagrantのBase Boxを作成する - ubuntu14.04 Server編(随時更新) - zowのプログラムな日々

追記ここまで

やはり他人の作ったOSイメージで作業してると、問題があった時にそこを疑ってしまうので、自分でインストールしたOSイメージを用意しておきたいと思う。今回はubuntu編。

必要なもの

作業の流れ

基本的にここに書かれている内容を行う

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

VirtualBoxubuntuインストール

以下の構成で作成

名前 base_ubuntu1404
タイプ Linux
バージョン Ubuntu(64 bit)
メモリ 512MB
仮想ハードドライブ 作成する
ファイルタイプ VMDK
ストレージ 可変
ファイルの名前 base_ubuntu1404
ファイルのサイズ 8GB
オーディオ 無効
USB 無効

上記で作成してから仮想CDにインストールメディアを設定してインストールする。

以下はOS設定項目

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

インストールが終わったらVirtualBoxコンソールからvagrantユーザでログインしてipを確認

vagrant@base-ubuntu1404:~$ ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:98:73:81  
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe98:7381/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:110 errors:0 dropped:0 overruns:0 frame:0
          TX packets:81 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:11535 (11.5 KB)  TX bytes:11497 (11.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@base-ubuntu1404:~$

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

f:id:zow3:20150117044952p:plain

設定したらVMを再開し、ホスト側からsshフォワードしたポートに接続する。

$ 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 de:9a:c4:a6:f2:67:74:1f:3b:31:2e:01:74:eb:8e:c6.
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-32-generic x86_64)

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

  System information as of Sat Jan 17 04:43:09 JST 2015

  System load:  0.2               Processes:           73
  Usage of /:   15.1% of 7.26GB   Users logged in:     0
  Memory usage: 9%                IP address for eth0: 10.0.2.15
  Swap usage:   0%

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

97 packages can be updated.
56 updates are security updates.

Last login: Sat Jan 17 04:43:09 2015
vagrant@base-ubuntu1404:~$

これでホスト側ターミナルから作業が出来るようになる。

root設定

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

vagrant@base-ubuntu1404:~$ sudo passwd root
[sudo] password for vagrant: 
新しい UNIX パスワードを入力してください: 
新しい UNIX パスワードを再入力してください: 
passwd: password updated successfully
vagrant@base-ubuntu1404:~$ 
vagrant@base-ubuntu1404:~$ su - 
パスワード: 
root@base-ubuntu1404:~# 

vagrantユーザ設定

sudoの設定

sudoするのにパスワード入力無しにする。

まずvagrantユーザを確認する

vagrant@base-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@base-ubuntu1404:~$ 

次に/etc/sudoersを確認する

vagrant@base-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

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

#includedir /etc/sudoers.d
vagrant@base-ubuntu1404:~$ 

vagrantユーザも入っているsudoグループに対して権限が与えられているのでこの設定をパスワードなしに変更する。

vagrant@base-ubuntu1404:~$ sudo visudo 

#以下の行を変更
%sudo   ALL=(ALL:ALL) ALL
    ↓
%sudo   ALL=(ALL) NOPASSWD:ALL

変更後はこんな感じ

vagrant@base-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@base-ubuntu1404:~$ 

変更後、一度ログアウト後、再度ssh接続してからsudoがパスワード無しになっていることを確認する

ssh設定

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

鍵はこちら

鍵格納用のディレクトリ作成

vagrant@base-ubuntu1404:~$ mkdir ~/.ssh
vagrant@base-ubuntu1404:~$ ls -la ~/
合計 36
drwxr-xr-x 4 vagrant vagrant 4096  1月 17 05:57 .
drwxr-xr-x 3 root    root    4096  1月 17 04:32 ..
-rw------- 1 vagrant vagrant  183  1月 17 05:03 .bash_history
-rw-r--r-- 1 vagrant vagrant  220  1月 17 04:32 .bash_logout
-rw-r--r-- 1 vagrant vagrant 3637  1月 17 04:32 .bashrc
drwx------ 2 vagrant vagrant 4096  1月 17 04:32 .cache
-rw-r--r-- 1 vagrant vagrant  675  1月 17 04:32 .profile
drwxrwxr-x 2 vagrant vagrant 4096  1月 17 05:57 .ssh
-rw------- 1 root    root    1016  1月 17 05:01 .viminfo
vagrant@base-ubuntu1404:~$ chmod 0700 .ssh
vagrant@base-ubuntu1404:~$ ls -la
合計 36
drwxr-xr-x 4 vagrant vagrant 4096  1月 17 05:57 .
drwxr-xr-x 3 root    root    4096  1月 17 04:32 ..
-rw------- 1 vagrant vagrant  183  1月 17 05:03 .bash_history
-rw-r--r-- 1 vagrant vagrant  220  1月 17 04:32 .bash_logout
-rw-r--r-- 1 vagrant vagrant 3637  1月 17 04:32 .bashrc
drwx------ 2 vagrant vagrant 4096  1月 17 04:32 .cache
-rw-r--r-- 1 vagrant vagrant  675  1月 17 04:32 .profile
drwx------ 2 vagrant vagrant 4096  1月 17 05:57 .ssh
-rw------- 1 root    root    1016  1月 17 05:01 .viminfo
vagrant@base-ubuntu1404:~$ 

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

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

vagrant@base-ubuntu1404:~$ cd ~/.ssh/
vagrant@base-ubuntu1404:~/.ssh$ ls -l
合計 0
vagrant@base-ubuntu1404:~/.ssh$ wget https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub
--2015-01-17 06:04:57--  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-17 06:04:58--  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-17 06:04:58 (7.21 MB/s) - `vagrant.pub' へ保存完了 [409/409]

vagrant@base-ubuntu1404:~/.ssh$ ls -l
合計 4
-rw-rw-r-- 1 vagrant vagrant 409  1月 17 06:04 vagrant.pub
vagrant@base-ubuntu1404:~/.ssh$ mv vagrant.pub authorized_keys
vagrant@base-ubuntu1404:~/.ssh$ ls -l
合計 4
-rw-rw-r-- 1 vagrant vagrant 409  1月 17 06:04 authorized_keys
vagrant@base-ubuntu1404:~/.ssh$ chmod 600 authorized_keys 
vagrant@base-ubuntu1404:~/.ssh$ ls -l
合計 4
-rw------- 1 vagrant vagrant 409  1月 17 06:04 authorized_keys
vagrant@base-ubuntu1404:~/.ssh$ 

システム設定

sshd設定

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

UseDNS no

設定後はこんな感じ

root@base-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
root@base-ubuntu1404:~# 

設定変更後にssh再起動

vagrant@base-ubuntu1404:~$ sudo service ssh restart
ssh stop/waiting
ssh start/running, process 1560
vagrant@base-ubuntu1404:~$ 

パッケージ更新

ここまで行った所で一度パッケージを更新する

vagrant@base-ubuntu1404:~$ sudo apt-get update
vagrant@base-ubuntu1404:~$ sudo apt-get upgrade -y

パッケージインストール

共有ディレクトリを使えるようにする為にビルドツール等をインストール

vagrant@base-ubuntu1404:~$ sudo apt-get install linux-headers-generic build-essential dkms

共有ディレクトリ設定

こちらの記事に書いた内容と同じ

Mac(Yosemite)でVagrant環境を構築する - zowのプログラムな日々

上記記事内の作業を実施し、vboxadd追加が終わったらマウント先の「/vagrant」を作っておく

vagrant@base-ubuntu1404:~$ sudo mkdir /vagrant
vagrant@base-ubuntu1404:~$ sudo chown vagrant.vagrant /vagrant
vagrant@base-ubuntu1404:~$ ls -l / |grep vagrant
drwxr-xr-x  2 vagrant vagrant  4096  1月 17 06:47 vagrant
vagrant@base-ubuntu1404:~$ 

追記 ここで以下を実行しておく

$ sudo /etc/init.d/vboxadd setup

これをやらないと共有ディレクトリがマウントできないエラーが発生する。

不要ファイル削除

vboxaddをビルドする為にダウンロードしたISOイメージは削除しておく

vagrant@base-ubuntu1404:~$ cd /tmp
vagrant@base-ubuntu1404:/tmp$ ls -l
合計 56976
-rw-rw-r-- 1 vagrant vagrant 58343424 11月 21 23:01 VBoxGuestAdditions_4.3.20.iso
vagrant@base-ubuntu1404:/tmp$ rm VBoxGuestAdditions_4.3.20.iso 
vagrant@base-ubuntu1404:/tmp$ ls -l
合計 0
vagrant@base-ubuntu1404:/tmp$ 

不要パッケージ削除

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

不要カーネルの削除(追記)

パッケージを上げた事でカーネルのバージョンも上がっている。

古いカーネルが残っているのでこれを削除する。

現在のカーネルバージョンを調べる

vagrant@base-ubuntu1404:~$ uname -r
3.13.0-44-generic
vagrant@base-ubuntu1404:~$ 

次に不要なカーネルのバージョンを調べる

vagrant@base-ubuntu1404:/boot$ ls -l /boot |grep vmlinuz|grep -v `uname -r`
-rw------- 1 root root  5798112  7月 15  2014 vmlinuz-3.13.0-32-generic
vagrant@base-ubuntu1404:/boot$ 

上記で見る通り、3.13.0-32-genericが不要カーネルになっている。これを削除する

vagrant@base-ubuntu1404:/boot$ sudo apt-get purge linux-image-3.13.0-32-generic 
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています                
状態情報を読み取っています... 完了
以下のパッケージは「削除」されます:
  linux-image-3.13.0-32-generic* linux-image-extra-3.13.0-32-generic*
アップグレード: 0 個、新規インストール: 0 個、削除: 2 個、保留: 0 個。
この操作後に 194 MB のディスク容量が解放されます。
続行しますか? [Y/n] y
(データベースを読み込んでいます ... 現在 90493 個のファイルとディレクトリがインストールされています。)
Removing linux-image-extra-3.13.0-32-generic (3.13.0-32.57) ...
Examining /etc/kernel/postrm.d .
run-parts: executing /etc/kernel/postrm.d/initramfs-tools 3.13.0-32-generic /boot/vmlinuz-3.13.0-32-generic
update-initramfs: Deleting /boot/initrd.img-3.13.0-32-generic
run-parts: executing /etc/kernel/postrm.d/zz-update-grub 3.13.0-32-generic /boot/vmlinuz-3.13.0-32-generic
Generating grub configuration file ...
Linux イメージを見つけました: /boot/vmlinuz-3.13.0-44-generic
Found initrd image: /boot/initrd.img-3.13.0-44-generic
Linux イメージを見つけました: /boot/vmlinuz-3.13.0-32-generic
Found memtest86+ image: /boot/memtest86+.elf
Found memtest86+ image: /boot/memtest86+.bin
完了
The link /initrd.img.old is a damaged link
Removing symbolic link initrd.img.old 
 you may need to re-run your boot loader[grub]
Purging configuration files for linux-image-extra-3.13.0-32-generic (3.13.0-32.57) ...
Examining /etc/kernel/postrm.d .
run-parts: executing /etc/kernel/postrm.d/initramfs-tools 3.13.0-32-generic /boot/vmlinuz-3.13.0-32-generic
run-parts: executing /etc/kernel/postrm.d/zz-update-grub 3.13.0-32-generic /boot/vmlinuz-3.13.0-32-generic
Removing linux-image-3.13.0-32-generic (3.13.0-32.57) ...
Examining /etc/kernel/prerm.d.
run-parts: executing /etc/kernel/prerm.d/dkms 3.13.0-32-generic /boot/vmlinuz-3.13.0-32-generic
dkms: removing: vboxguest 4.3.20 (3.13.0-32-generic) (x86_64)

-------- Uninstall Beginning --------
Module:  vboxguest
Version: 4.3.20
Kernel:  3.13.0-32-generic (x86_64)
-------------------------------------

Status: Before uninstall, this module version was ACTIVE on this kernel.

vboxguest.ko:
 - Uninstallation
   - Deleting from: /lib/modules/3.13.0-32-generic/updates/dkms/
 - Original module
   - No original module was found for this module on this kernel.
   - Use the dkms install command to reinstall any previous module version.


vboxsf.ko:
 - Uninstallation
   - Deleting from: /lib/modules/3.13.0-32-generic/updates/dkms/
 - Original module
   - No original module was found for this module on this kernel.
   - Use the dkms install command to reinstall any previous module version.


vboxvideo.ko:
 - Uninstallation
   - Deleting from: /lib/modules/3.13.0-32-generic/updates/dkms/
 - Original module
   - No original module was found for this module on this kernel.
   - Use the dkms install command to reinstall any previous module version.

depmod....

DKMS: uninstall completed.

------------------------------
Deleting module version: 4.3.20
completely from the DKMS tree.
------------------------------
Done.
Examining /etc/kernel/postrm.d .
run-parts: executing /etc/kernel/postrm.d/initramfs-tools 3.13.0-32-generic /boot/vmlinuz-3.13.0-32-generic
update-initramfs: Deleting /boot/initrd.img-3.13.0-32-generic
run-parts: executing /etc/kernel/postrm.d/zz-update-grub 3.13.0-32-generic /boot/vmlinuz-3.13.0-32-generic
Generating grub configuration file ...
Linux イメージを見つけました: /boot/vmlinuz-3.13.0-44-generic
Found initrd image: /boot/initrd.img-3.13.0-44-generic
Found memtest86+ image: /boot/memtest86+.elf
Found memtest86+ image: /boot/memtest86+.bin
完了
The link /vmlinuz.old is a damaged link
Removing symbolic link vmlinuz.old 
 you may need to re-run your boot loader[grub]
Purging configuration files for linux-image-3.13.0-32-generic (3.13.0-32.57) ...
Examining /etc/kernel/postrm.d .
run-parts: executing /etc/kernel/postrm.d/initramfs-tools 3.13.0-32-generic /boot/vmlinuz-3.13.0-32-generic
run-parts: executing /etc/kernel/postrm.d/zz-update-grub 3.13.0-32-generic /boot/vmlinuz-3.13.0-32-generic
vagrant@base-ubuntu1404:/boot$ 

最後にaptのキャッシュを削除

vagrant@base-ubuntu1404:~$ sudo apt-get clean
vagrant@base-ubuntu1404:~$ 

ここまで終わったら再起動

フラグメント解消

Box化する前にディスクのフラグメントを解消してやる

vagrant@base-ubuntu1404:/$ cd tmp
vagrant@base-ubuntu1404:/tmp$ ls -l
合計 0
vagrant@base-ubuntu1404:/tmp$ dd if=/dev/zero of=/tmp/ZERO bs=1M
dd: `/tmp/ZERO' の書き込みエラー: デバイスに空き領域がありません
5451+0 レコード入力
5450+0 レコード出力
5715054592 バイト (5.7 GB) コピーされました、 11.0699 秒、 516 MB/秒
vagrant@base-ubuntu1404:/tmp$ ls -l
合計 5581112
-rw-rw-r-- 1 vagrant vagrant 5715054592  1月 17 07:18 ZERO
vagrant@base-ubuntu1404:/tmp$ rm ZERO 
vagrant@base-ubuntu1404:/tmp$ ls -l
合計 0
vagrant@base-ubuntu1404:/tmp$ 

終わったらシャットダウンしておく。

vagrantパッケージ作成

ホストOSでパッケージ化実行

$ vagrant package --base base_ubuntu1404
==> base_ubuntu1404: Clearing any previously set forwarded ports...
==> base_ubuntu1404: Exporting VM...
==> base_ubuntu1404: Compressing package to: /Users/zow/VirtualBox VMs/base_ubuntu1404/package.box
$ ls -l
total 6281864
drwx------  6 zow  staff         204  1 17 07:10 Logs
drwx------  2 zow  staff          68  1 17 04:43 Snapshots
-rw-------  1 zow  staff        7500  1 17 07:31 base_ubuntu1404.vbox
-rw-------  1 zow  staff        7617  1 17 07:28 base_ubuntu1404.vbox-prev
-rw-------  1 zow  staff  2555445248  1 17 07:28 base_ubuntu1404.vmdk
-rw-r--r--  1 zow  staff   660849298  1 17 07:34 package.box
$

660MBか・・・。なんでこんなにでかいんだろ? 何か消し忘れがあるかもしれない。

不要カーネル削除時のパッケージ(追記)

不要カーネルを削除したので、再度パッケージ化する。

削除前のパッケージを「old-package.box」として残しておくのでサイズ比較してみる。

base_ubuntu1404 21:22:12 zow$ vagrant package --base base_ubuntu1404
==> base_ubuntu1404: Clearing any previously set forwarded ports...
==> base_ubuntu1404: Exporting VM...
==> base_ubuntu1404: Compressing package to: /Users/zow/VirtualBox VMs/base_ubuntu1404/package.box
base_ubuntu1404 21:25:17 zow$ ls -l
total 7379024
drwx------  6 zow  staff         204  1 17 21:04 Logs
drwx------  2 zow  staff          68  1 17 04:43 Snapshots
-rw-------  1 zow  staff        7500  1 17 21:22 base_ubuntu1404.vbox
-rw-------  1 zow  staff        7620  1 17 21:21 base_ubuntu1404.vbox-prev
-rw-------  1 zow  staff  2564030464  1 17 21:21 base_ubuntu1404.vmdk
-rw-r--r--  1 zow  staff   660849298  1 17 07:34 old-package.box
-rw-r--r--  1 zow  staff   553159009  1 17 21:25 package.box
base_ubuntu1404 21:25:21 zow$ 

110MB弱しか変わらん・・・・orz

パッケージ削除時に194MB開放されるって書いてあって、パッケージ化の時は110MB弱減ってるから、2分の1ぐらいに圧縮してるのかな。

もうちょっと減らしたいけど、出来る限り素のubuntu環境を残して実環境に近づけたいから、いらないパッケージの削除とかは控えたい感じ。

今はこれぐらいで妥協しますか

Box登録

とりあえず出来上がったパッケージを登録する

$ vagrant box add base_ubuntu1404 package.box 
==> box: Adding box 'base_ubuntu1404' (v0) for provider: 
    box: Downloading: file:///Users/zow/VirtualBox%20VMs/base_ubuntu1404/package.box
==> box: Successfully added box 'base_ubuntu1404' (v0) for 'virtualbox'!
$ vagrant box list
base_ubuntu1404             (virtualbox, 0)
ubuntu-14.04-server         (virtualbox, 0)
ubuntu-14.04-server-rebuild (virtualbox, 0)
$

確認

追加したboxから仮想環境を作成してみる。

$ mkdir -p ~/dev/vagrant/base_ubuntu1404
$ cd ~/dev/vagrant/base_ubuntu1404/
$ vagrant init base_ubuntu1404
$ ls -l
total 8
-rw-r--r--  1 zow  staff  3027  1 17 07:40 Vagrantfile
$

出来た。実行してみる。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'base_ubuntu1404'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: base_ubuntu1404_default_1421448116078_53357
==> 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: 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.
==> default: Mounting shared folders...
    default: /vagrant => /Users/zow/dev/vagrant/base_ubuntu1404

Failed to mount folders in Linux guest. This is usually because
the "vboxsf" file system is not available. Please verify that
the guest additions are properly installed in the guest and
can work properly. The command attempted was:

mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` vagrant /vagrant
mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` vagrant /vagrant

The error output from the last command was:

stdin: is not a tty
/sbin/mount.vboxsf: mounting failed with the error: No such device

$ vagrant status
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.
$ vagrant ssh
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-44-generic x86_64)

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

  System information as of Sat Jan 17 07:42:13 JST 2015

  System load:  0.15              Processes:           76
  Usage of /:   21.2% of 7.26GB   Users logged in:     0
  Memory usage: 9%                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 17 07:27:47 2015 from 10.0.2.2
vagrant@base-ubuntu1404:~$ 

エラーが出てるけど立ち上がったしsshで接続できた。

エラーは2つあって、ひとつはssh絡み。もう一つは共有ディレクトリの方だ。

共有ディレクトリの方はこの記事の内容で解消できそうなので、まずはこちらからやってみる。

追記 /vagrantディレクトリを作り終わった所でvboxadd setupを実行すると共有ディレクトリのエラーは発生しなくなる。

vagrantでmountエラーの解決方法 - Qiita

vagrant@base-ubuntu1404:/vagrant$ 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.
Starting the VirtualBox Guest Additions ...done.
vagrant@base-ubuntu1404:/vagrant$ sudo shutdown -r now

次にホストOS側でvagrantの再起動

$ vagrant halt
==> default: Attempting graceful shutdown of VM...
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> 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: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /Users/zow/dev/vagrant/base_ubuntu1404
$

あれ・・・、sshのエラーも消えてしまった。どうも正常に立ち上がったみたいだ。

$ vagrant ssh
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-44-generic x86_64)

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

  System information as of Sat Jan 17 07:53:09 JST 2015

  System load:  0.24              Processes:           77
  Usage of /:   21.2% of 7.26GB   Users logged in:     0
  Memory usage: 10%               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 17 07:44:37 2015 from 10.0.2.2
vagrant@base-ubuntu1404:~$ ls /vagrant/
Vagrantfile
vagrant@base-ubuntu1404:~$ 

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

てことで、最初のsshのエラーを調べてみた。

どうも、gitで配られてる鍵はセキュアじゃないから、初回接続時に新しい鍵に置き換えるらしい。その為に出たWarningっぽい。

よく読んでみたらちゃんと書いてある。

    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.

エラーじゃないのね。一安心だ。