2013年7月20日土曜日

VirtualBox 4.2.16

VirtualBox 4.2.16がリリースされていたので、早速アップデートしました。

Changelogを読むと、以前調べたmanifestファイルがないとクラッシュする問題が解決しているようです。

ちなみに、Download VirtualBox for Linux Hostsに書かれていますが、virtualbox.repoを設定しておくと、yum updateでアップデートできます。

私は、repoファイルをenabled=0に設定しているので、こんな感じです。
# yum update --enablerepo=virtualbox
veeweeでboxを作成するとmanifestファイルが作られないので、面倒くさいなと思ってたので助かりました。

2013年7月14日日曜日

Veeweeを使ってVagrant boxを作成する (その2)

次のようなメッセージを出力して、veeweeの「build」コマンドが失敗してしまったので原因を調べます。
$ bundle exec veewee vbox build 'CentOS-6.4-x86_64'
 :
Typing:[1]:  text ks=http://10.0.2.2:7122/ks.cfg
Error: We executed a shell command and the exit status was not 0
- Command :VBoxManage controlvm "CentOS-6.4-x86_64" keyboardputscancode 0f.
- Exitcode :1.
- Output   :
VBoxManage: error: Guest not running

$
メッセージから、VirtualBoxが上手く動いてないことは分かります。
しかし、VirtualBoxをちゃんと使ったことがないので、仮想マシンの起動の仕方が分かりません。

VirtualBox UserManualで調べると、VBoxManage startvmで、仮想マシンをスタートできるようです。
早速、試してみると次のように失敗しました。
$ VBoxManage startvm "CentOS-6.4-x86_64"
Waiting for VM "CentOS-6.4-x86_64" to power on...
VBoxManage: error: The virtual machine 'CentOS-6.4-x86_64' has terminated unexpe
ctedly during startup with exit code 0
VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component Machin
e, interface IMachine
$
ヒントがとれたので、ググるとforums.virtualbox.orgでNS_ERROR_FAILURE (0x80004005)という記事が見つかりました。
読んでみると、どうもGUIを使用しようとして失敗しているような感じです。
確かに、リモートからsshでログインして操作しているので該当しそうです。

--nogui--forceを付けて再度buildコマンドを実行します。
途中まで実行されているため、--forceを付けないとエラーになります。
$ bundle exec veewee vbox build 'CentOS-6.4-x86_64' --force --nogui
Downloading vbox guest additions iso v 4.2.14 - http://download.virtualbox.org/virtualbox/4.2.14/VBoxGuestAdditions_4.2.14.iso
Checking if isofile VBoxGuestAdditions_4.2.14.iso already exists.
Full path: /home/foo/work/veewee/iso/VBoxGuestAdditions_4.2.14.iso

The isofile VBoxGuestAdditions_4.2.14.iso already exists.
Building Box CentOS-6.4-x86_64 with Definition CentOS-6.4-x86_64:
- debug : false
- cwd : /home/foo/work/veewee
- force : true
- nogui : true
- auto : false
- checksum : false
- redirectconsole : false
- postinstall_include : []
- postinstall_exclude : []

The isofile CentOS-6.4-x86_64-minimal.iso already exists.
VBoxManage unregistervm  "CentOS-6.4-x86_64" --delete
Deleting vm CentOS-6.4-x86_64
Creating vm CentOS-6.4-x86_64 : 480M - 1 CPU - RedHat_64
Creating new harddrive of size 10140, format VDI, variant Standard
Attaching disk: /home/foo/VirtualBox VMs/CentOS-6.4-x86_64/CentOS-6.4-x86_641.vdi
Mounting cdrom: /home/foo/work/veewee/iso/CentOS-6.4-x86_64-minimal.iso
Mounting guest additions: /home/foo/work/veewee/iso/VBoxGuestAdditions_4.2.14.iso
Finding unused TCP port in range: 7222 - 7262
Selected TCP port 7222
Finding unused TCP port in range: 7222 - 7262
Selected TCP port 7222
Waiting 10 seconds for the machine to boot
Finding unused TCP port in range: 7122 - 7199
Selected TCP port 7122
Starting a webserver :7122

Typing:[1]:  text ks=http://10.0.2.2:7122/ks.cfg
Done typing.

 :
 :
No Match for argument: bitstream-vera-fonts
No Packages marked for removal
Loaded plugins: fastestmirror
Cleaning repos: base epel extras puppetlabs puppetlabs-dependencies updates
Cleaning up Everything
Cleaning up list of fastest mirrors
Waiting for ssh login on 127.0.0.1 with user veewee to sshd on port => 7222 to work, timeout=10000 sec
.
Executing command: echo 'veewee'|sudo -S sh './zerodisk.sh'
dd: writing `/EMPTY': デバイスに空き領域がありません
7454+0 records in
7453+0 records out
7815069696 bytes (7.8 GB) copied, 12.6561 s, 617 MB/s
The box CentOS-6.4-x86_64 was built successfully!
You can now login to the box with:
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p 7222 -l veewee 127.0.0.1
$
なんかメッセージが出てますが、とりあえず成功したようです。
試しに、最後のメッセージにあるようにsshしたところ、ログインも出来ました。

次は、validateコマンドで作成した仮想マシンの検証をします。
$ bundle exec veewee vbox validate 'CentOS-6.4-x86_64'
Checking user - OK
Checking sudo - OK
Checking ruby - OK
Checking gem - OK
Checking chef - OK
Checking puppet - OK
Checking shared folder - OK
$
問題ないようです。
いよいよ、exportコマンドでboxを作ります。
$ bundle exec veewee vbox export 'CentOS-6.4-x86_64'
Vagrant requires the box to be shutdown, before it can export
Sudo also needs to work for user veewee
Performing a clean shutdown now.
Waiting for ssh login on 127.0.0.1 with user veewee to sshd on port => 7222 to work, timeout=10000 sec
.
Executing command: echo '/sbin/halt -h -p' > /tmp/shutdown.sh
Waiting for ssh login on 127.0.0.1 with user veewee to sshd on port => 7222 to work, timeout=10000 sec
.
Executing command: chmod +x /tmp/shutdown.sh
Waiting for ssh login on 127.0.0.1 with user veewee to sshd on port => 7222 to work, timeout=10000 sec
.
Executing command: echo 'veewee'|sudo -S sh '/tmp/shutdown.sh'
.......
Machine CentOS-6.4-x86_64 is powered off cleanly
Creating a temporary directory for export
Adding additional files
Creating Vagrantfile
Exporting the box
Executing VBoxManage export CentOS-6.4-x86_64 --output /tmp/d20130713-10173-1y2vmzn/box.ovf
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Successfully exported 1 machine(s).
Packaging the box
Cleaning up temporary directory

To import it into vagrant type:
vagrant box add 'CentOS-6.4-x86_64' '/home/foo/work/veewee/CentOS-6.4-x86_64.box'

To use it:
vagrant init 'CentOS-6.4-x86_64'
vagrant up
vagrant ssh
$
$ ls *.box
CentOS-6.4-x86_64.box
$
やっとboxができました。

Veeweeを使ってVagrant boxを作成する (その1)

Vagrant便利ですね。

でもboxが無いとダメだよね。何処にあるの?どうやって作るの?となります。
ググるとVagrantbox.esなどでboxが公開されていますが、作成者がハッキリしないものは使いたくないので作ることにしました。

VAGRANT DOCSを見ると、次の手順になるようです。
  1. VirtualBoxで仮想マシンを作り、OSをインストールしてexportする。
  2. vagrant packageコマンドでパッケージ化する。
仮想マシン上にvagrant用のユーザ設定や共有フォルダ設定もしておくようです。

で、色々やってくれるツールとしてVeeweeがあります。
Veeweeは、VirtualBoxだけでなくKVM、VMware Fusion、Parallelsの仮想マシンイメージを作ることができるようです。すばらしい。

インストールは、Veewee Installationに書かれているとおりで問題なくできました。
gemでもインストールできるようですが、最新のものでは無いようなのでソースからインストールしました。

私の環境では、rbenvを使用しているので、手順は次のようになります。
git cloneしてbundle installするだけです。
$ git clone https://github.com/jedi4ever/veewee.git
Initialized empty Git repository in /home/foo/work/veewee/.git/
remote: Counting objects: 12661, done.
remote: Compressing objects: 100% (4587/4587), done.
remote: Total 12661 (delta 8416), reused 11992 (delta 7873)
Receiving objects: 100% (12661/12661), 3.74 MiB | 1.30 MiB/s, done.
Resolving deltas: 100% (8416/8416), done.

$ cd veewee
$ rbenv versions
  1.8.7-p371
* 1.9.3-p429 (set by /usr/local/rbenv/version)
  2.0.0-p195
$ rbenv local 1.9.3-p429
$ bundle install
Fetching http://github.com/hh/em-winrm.git
remote: Counting objects: 144, done.
remote: Compressing objects: 100% (100/100), done.
remote: Total 144 (delta 68), reused 113 (delta 43)
Receiving objects: 100% (144/144), 23.55 KiB, done.
Resolving deltas: 100% (68/68), done.
Fetching gem metadata from http://rubygems.org/........
Fetching gem metadata from http://rubygems.org/..
Resolving dependencies...
Installing rake (10.1.0)
Installing libxml-ruby (2.6.0)
 :
Installing whichr (0.3.6)
Using veewee (0.3.7) from source at /home/foo/work/veewee
Your bundle is complete!
Gems in the group kvm were not installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
$
$ rbenv rehash
動かしてみます。
$ bundle exec veewee
Commands:
  veewee fusion          # Subcommand for Vmware fusion
  veewee help [COMMAND]  # Describe available commands or one specific command
  veewee kvm             # Subcommand for KVM
  veewee parallels       # Subcommand for Parallels
  veewee vbox            # Subcommand for VirtualBox
  veewee version         # Prints the Veewee version information

$ bundle exec veewee vbox
Commands:
  veewee vbox build [BOX_NAME]                     # Build box
  veewee vbox copy [BOX_NAME] [SRC] [DST]          # Copy a file to the VM
  veewee vbox define [BOX_NAME] [TEMPLATE]         # Define a new basebox sta...
  veewee vbox destroy [BOX_NAME]                   # Destroys the virtualmach...
  veewee vbox export [BOX_NAME]                    # Exports the basebox to t...
  veewee vbox halt [BOX_NAME]                      # Activates a shutdown the...
  veewee vbox help [COMMAND]                       # Describe subcommands or ...
  veewee vbox list                                 # Lists all defined boxes
  veewee vbox ostypes                              # List the available Opera...
  veewee vbox screenshot [BOX_NAME] [PNGFILENAME]  # Takes a screenshot of th...
  veewee vbox sendkeys [BOX_NAME] [SEQUENCE]       # Sends the key sequence (...
  veewee vbox ssh [BOX_NAME] [COMMAND]             # SSH to box
  veewee vbox templates                            # List the currently avail...
  veewee vbox undefine [BOX_NAME]                  # Removes the definition o...
  veewee vbox up [BOX_NAME]                        # Starts a Box
  veewee vbox validate [BOX_NAME]                  # Validates a box against ...
  veewee vbox winrm [BOX_NAME] [COMMAND]           # Execute command via winrm

Options:
          [--debug]           # enable debugging
  -w, --workdir, [--cwd=CWD]  # Change the working directory. (The folder contai
ning the definitions folder).
                              # Default: /home/hide/work/veewee

$
早速、Veewee Basicsに沿って、boxを作ってみます。
まずは、使用するテンプレートを決めます。
CentOSのboxを使用することにして、次のようにしてテンプレートのリストを表示させます。
$ bundle exec veewee vbox templates |grep -i centos
veewee vbox define '' 'CentOS-4.8-i386' --workdir=/home/foo/work/veewee
veewee vbox define '' 'CentOS-5.5-i386-netboot' --workdir=/home/foo/work/veewee
 :
veewee vbox define '' 'CentOS-6.4-i386-minimal' --workdir=/home/foo/work/veewee
veewee vbox define '' 'CentOS-6.4-i386-netboot' --workdir=/home/foo/work/veewee
veewee vbox define '' 'CentOS-6.4-x86_64-minimal' --workdir=/home/foo/work/veewee
veewee vbox define '' 'CentOS-6.4-x86_64-netboot' --workdir=/home/foo/work/veewee
$
たくさん表示されましたが、今回は「CentOS-6.4-x86_64-minimal」を使用することにしました。
次に、「define」コマンドで定義ファイルを出力させます。
$ bundle exec veewee vbox define 'CentOS-6.4-x86_64' 'CentOS-6.4-x86_64-minimal'
The basebox 'CentOS-6.4-x86_64' has been successfully created from the template 'CentOS-6.4-x86_64-minimal'
You can now edit the definition files stored in /home/foo/work/veewee/definitions/CentOS-6.4-x86_64 or build the box with:
veewee vbox build 'CentOS-6.4-x86_64' --workdir=/home/foo/work/veewee
$
「CentOS-6.4-x86_64」という名前で定義ファイルができています。
$ ls definitions/
CentOS-6.4-x86_64
$ ls definitions/CentOS-6.4-x86_64/
base.sh  cleanup.sh     ks.cfg     vagrant.sh     vmfusion.sh
chef.sh  definition.rb  puppet.sh  virtualbox.sh  zerodisk.sh
$
ファイルの説明は、Customizing Definitionsにあります。
definition.rbが主設定ファイル、ks.cfgがkickstartファイル、*.shがインストール後に実行されるスクリプトという感じのようです。
今回は、次のようにISOイメージのダウンロードサイトと、キックスタートでの言語、キーボード、タイムゾーンだけ変更してみました。
$ diff -uw definitions/CentOS-6.4-x86_64/{definition.rb.original,definition.rb}
--- definitions/CentOS-6.4-x86_64/definition.rb.original        2013-07-13 21:35:08.719699339 +0900
+++ definitions/CentOS-6.4-x86_64/definition.rb 2013-07-14 11:27:50.263552526 +0900
@@ -6,7 +6,7 @@
   :hostiocache => 'off',
   :os_type_id => 'RedHat6_64',
   :iso_file => "CentOS-6.4-x86_64-minimal.iso",
-  :iso_src => "http://yum.singlehop.com/CentOS/6.4/isos/x86_64/CentOS-6.4-x86_64-minimal.iso",
+  :iso_src => "http://ftp.riken.jp/Linux/centos/6.4/isos/x86_64/CentOS-6.4-x86_64-minimal.iso",
   :iso_md5 => "4a5fa01c81cc300f4729136e28ebe600",
   :iso_download_timeout => 1000,
   :boot_wait => "10",
$
$ diff -uw definitions/CentOS-6.4-x86_64/{ks.cfg.original,ks.cfg}
--- definitions/CentOS-6.4-x86_64/ks.cfg.original       2013-07-13 21:35:08.719699339 +0900
+++ definitions/CentOS-6.4-x86_64/ks.cfg        2013-07-14 11:28:32.210553491 +0900
@@ -1,13 +1,13 @@
 install
 cdrom
-lang en_US.UTF-8
-keyboard us
+lang ja_JP.UTF-8
+keyboard jp106
 network --bootproto=dhcp
 rootpw --iscrypted $1$damlkd,f$UC/u5pUts5QiU3ow.CSso/
 firewall --enabled --service=ssh
 authconfig --enableshadow --passalgo=sha512
 selinux --disabled
-timezone UTC
+timezone Asia/Tokyo
 bootloader --location=mbr

 text
いよいよ、仮想マシンの作成です。
「build」コマンドを実行します。
$ bundle exec veewee vbox build 'CentOS-6.4-x86_64'
Downloading vbox guest additions iso v 4.2.14 - http://download.virtualbox.org/virtualbox/4.2.14/VBoxGuestAdditions_4.2.14.iso
Checking if isofile VBoxGuestAdditions_4.2.14.iso already exists.
Full path: /home/foo/work/veewee/iso/VBoxGuestAdditions_4.2.14.iso

The isofile VBoxGuestAdditions_4.2.14.iso already exists.
Building Box CentOS-6.4-x86_64 with Definition CentOS-6.4-x86_64:
- debug : false
- cwd : /home/foo/work/veewee
- force : false
- nogui : false
- auto : false
- checksum : false
- redirectconsole : false
- postinstall_include : []
- postinstall_exclude : []

We did not find an isofile here : /home/foo/work/veewee/iso/CentOS-6.4-x86_64-minimal.iso.

The definition provided the following download information:
- Download url: http://ftp.riken.jp/Linux/centos/6.4/isos/x86_64/CentOS-6.4-x86_64-minimal.iso
- Md5 Checksum: 4a5fa01c81cc300f4729136e28ebe600


Download? (Yes/No) Yes
Checking if isofile CentOS-6.4-x86_64-minimal.iso already exists.
Full path: /home/foo/work/veewee/iso/CentOS-6.4-x86_64-minimal.iso
Moving /tmp/open-uri20130713-9671-3i1bbnotoo/home/foo/work/veewee/iso/CentOS-6.4-x86_64-minimal.iso|   343M   6.1MB/s ETA:  0:00:00
Creating vm CentOS-6.4-x86_64 : 480M - 1 CPU - RedHat_64
Creating new harddrive of size 10140, format VDI, variant Standard
Attaching disk: /home/foo/VirtualBox VMs/CentOS-6.4-x86_64/CentOS-6.4-x86_641.vdi
Mounting cdrom: /home/foo/work/veewee/iso/CentOS-6.4-x86_64-minimal.iso
Mounting guest additions: /home/foo/work/veewee/iso/VBoxGuestAdditions_4.2.14.iso
Finding unused TCP port in range: 7222 - 7262
Selected TCP port 7222
Finding unused TCP port in range: 7222 - 7262
Selected TCP port 7222
Waiting 10 seconds for the machine to boot
Finding unused TCP port in range: 7122 - 7199
Selected TCP port 7122
Starting a webserver :7122

Typing:[1]:  text ks=http://10.0.2.2:7122/ks.cfg
Error: We executed a shell command and the exit status was not 0
- Command :VBoxManage controlvm "CentOS-6.4-x86_64" keyboardputscancode 0f.
- Exitcode :1.
- Output   :
VBoxManage: error: Guest not running

$
うーん、失敗したようです。
しばらく待ってもプロンプトが帰ってこないので、CTRL+Cで抜けました。

ちなみに、途中「Download? (Yes/No)」の箇所で正しく「Yes」と入力しないと、手動でダウンロードするように言われます。
Download? (Yes/No) Y
You have selected manual download:
curl -C - -L 'http://ftp.riken.jp/Linux/centos/6.4/isos/x86_64/CentOS-6.4-x86_64-minimal.iso' -o 'iso/CentOS-6.4-x86_64-minimal.iso'
md5 'iso/CentOS-6.4-x86_64-minimal.iso'

$
つづく