読者です 読者をやめる 読者になる 読者になる

Still life

web系エンジニャー @yui_tang の備忘録等々

Packerを使ってCentOS7のboxを作りましょう。

はい、やってまいりました。

今回はCentOS7のvagrantのboxを作ってみましょう。

今回の材料はこちらです。

各々インストールを済ませておきましょう。 Packerについてはこちら等々参考にどうぞ。

Packerを使ってVagrantのBoxを作る方法を一つずつ説明する/index - ヽ(´・肉・`)ノログ

手順

1. 設定ファイルを用意する

こちらのリポジトリからpacker用のtemplateファイルを用意したので落としてきましょう。

YuiSakamoto/my_packer_templates · GitHub

参考:以下のリポジトリは、CentOS/Ubuntu/ScientificLinuxの新バージョンが出る毎に、 packer用の設定ファイルを公開してくれています。助かります。

packer-templates/centos-7.0 at develop · shiguredo/packer-templates · GitHub

今回はこちらを参考に(というかそのまま使用)させていただきました。

2. Packerを使ってbox作成

$ cd my_packer_templates/centos-7.0
$ packer build -only=virtualbox-iso template.json
virtualbox-iso output will be in this color.

==> virtualbox-iso: Downloading or copying Guest additions
    virtualbox-iso: Downloading or copying: file:///Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso
==> virtualbox-iso: Downloading or copying ISO
    virtualbox-iso: Downloading or copying: http://ftp.iij.ad.jp/pub/linux/centos/7.0.1406/isos/x86_64/CentOS-7.0-1406-x86_64-DVD.iso
    virtualbox-iso: Download progress: 0%
    virtualbox-iso: Download progress: 0%
    virtualbox-iso: Download progress: 1%
    virtualbox-iso: Download progress: 1%
    virtualbox-iso: Download progress: 1%
    virtualbox-iso: Download progress: 2%
    virtualbox-iso: Download progress: 2%
    virtualbox-iso: Download progress: 3%
    virtualbox-iso: Download progress: 3%
〜中略〜
    virtualbox-iso: Download progress: 98%
    virtualbox-iso: Download progress: 98%
    virtualbox-iso: Download progress: 98%
    virtualbox-iso: Download progress: 99%
    virtualbox-iso: Download progress: 99%
    virtualbox-iso: Download progress: 100%
    virtualbox-iso: Download progress: 100%
    virtualbox-iso: Download progress: 100%
    virtualbox-iso: Download progress: 100%
==> virtualbox-iso: Starting HTTP server on port 8081
==> virtualbox-iso: Creating virtual machine...
==> virtualbox-iso: Creating hard drive...
==> virtualbox-iso: Creating forwarded port mapping for SSH (host port 3213)
==> virtualbox-iso: Executing custom VBoxManage commands...
    virtualbox-iso: Executing: modifyvm packer-virtualbox-iso --memory 512
    virtualbox-iso: Executing: modifyvm packer-virtualbox-iso --cpus 1
==> virtualbox-iso: Starting the virtual machine...
==> virtualbox-iso: Waiting 10s for boot...
==> virtualbox-iso: Typing the boot command...
==> virtualbox-iso: Error sending boot command: VBoxManage error: VBoxManage: error: Guest not running
==> virtualbox-iso: Unregistering and deleting virtual machine...
==> virtualbox-iso: Deleting output directory...
Build 'virtualbox-iso' errored: Error sending boot command: VBoxManage error: VBoxManage: error: Guest not running

==> Some builds didn't complete successfully and had errors:
--> virtualbox-iso: Error sending boot command: VBoxManage error: VBoxManage: error: Guest not running

==> Builds finished but no artifacts were created.

3. エラー発生

上記にもある通りVBoxでゲストOSが走らなかったとのこと。 ↓GUIのエラーも出てます。

f:id:yuitang:20140811175517p:plain

ぐぐってみると以下の様な情報が。

MacのVirtualboxでkernel driver not installed のエラーで仮想サーバーが起動できなくなったときにしたこと - Vermee81の日録

チッ、材料が腐ってやがった

新鮮な具材を取り揃えましょうね。 uninstall toolsを使ってアンインストールを行い、最新版(4.3.13)を再度インストール。

4. 再度packer buildを実行

$ packer build -only=virtualbox-iso template.json
virtualbox-iso output will be in this color.

==> virtualbox-iso: Downloading or copying Guest additions
    virtualbox-iso: Downloading or copying: file:///Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso
==> virtualbox-iso: Downloading or copying ISO
    virtualbox-iso: Downloading or copying: http://ftp.iij.ad.jp/pub/linux/centos/7.0.1406/isos/x86_64/CentOS-7.0-1406-x86_64-DVD.iso
==> virtualbox-iso: Starting HTTP server on port 8081
==> virtualbox-iso: Creating virtual machine...
==> virtualbox-iso: Creating hard drive...
==> virtualbox-iso: Creating forwarded port mapping for SSH (host port 3213)
==> virtualbox-iso: Executing custom VBoxManage commands...
    virtualbox-iso: Executing: modifyvm packer-virtualbox-iso --memory 512
    virtualbox-iso: Executing: modifyvm packer-virtualbox-iso --cpus 1
==> virtualbox-iso: Starting the virtual machine...
==> virtualbox-iso: Waiting 10s for boot...
==> virtualbox-iso: Typing the boot command...
==> virtualbox-iso: Waiting for SSH to become available...

ここで結構時間が掛かる。(VirtualBoxのウィンドウで諸々インストールされているのがわかります。)

==> virtualbox-iso: Connected to SSH!
==> virtualbox-iso: Uploading VirtualBox version info (4.3.14)
==> virtualbox-iso: Uploading VirtualBox guest additions ISO...
==> virtualbox-iso: Provisioning with shell script: scripts/base.sh
    virtualbox-iso: Loaded plugins: fastestmirror
    virtualbox-iso: base             | 3.6 kB     00:00
    virtualbox-iso: extras           | 3.3 kB     00:00
    virtualbox-iso: updates          | 3.4 kB     00:00
    virtualbox-iso: Loading mirror speeds from cached hostfile
    virtualbox-iso: * base: ftp.riken.jp
    virtualbox-iso: * extras: ftp.riken.jp
    virtualbox-iso: * updates: ftp.riken.jp
    virtualbox-iso: Package 1:make-3.82-21.el7.x86_64 already installed and latest version
    virtualbox-iso: Package kernel-devel-3.10.0-123.el7.x86_64 already installed and latest version
    virtualbox-iso: Package 4:perl-5.16.3-283.el7.x86_64 already installed and latest version
    virtualbox-iso: Resolving Dependencies
    virtualbox-iso: --> Running transaction check
    virtualbox-iso: ---> Package gcc.x86_64 0:4.8.2-16.el7 will be installed
    virtualbox-iso: --> Processing Dependency: cpp = 4.8.2-16.el7 for package: gcc-4.8.2-16.el7.x86_64
    virtualbox-iso: --> Processing Dependency: glibc-devel >= 2.2.90-12 for package: gcc-4.8.2-16.el7.x86_64
    virtualbox-iso: --> Processing Dependency: libmpfr.so.4()(64bit) for package: gcc-4.8.2-16.el7.x86_64
    virtualbox-iso: --> Processing Dependency: libmpc.so.3()(64bit) for package: gcc-4.8.2-16.el7.x86_64
    virtualbox-iso: ---> Package gcc-c++.x86_64 0:4.8.2-16.el7 will be installed
    virtualbox-iso: --> Processing Dependency: libstdc++-devel = 4.8.2-16.el7 for package: gcc-c++-4.8.2-16.el7.x86_64
    virtualbox-iso: --> Running transaction check
    virtualbox-iso: ---> Package cpp.x86_64 0:4.8.2-16.el7 will be installed
    virtualbox-iso: ---> Package glibc-devel.x86_64 0:2.17-55.el7 will be installed
    virtualbox-iso: --> Processing Dependency: glibc-headers = 2.17-55.el7 for package: glibc-devel-2.17-55.el7.x86_64
    virtualbox-iso: --> Processing Dependency: glibc-headers for package: glibc-devel-2.17-55.el7.x86_64
    virtualbox-iso: ---> Package libmpc.x86_64 0:1.0.1-3.el7 will be installed
    virtualbox-iso: ---> Package libstdc++-devel.x86_64 0:4.8.2-16.el7 will be installed
    virtualbox-iso: ---> Package mpfr.x86_64 0:3.1.1-4.el7 will be installed
    virtualbox-iso: --> Running transaction check
    virtualbox-iso: ---> Package glibc-headers.x86_64 0:2.17-55.el7 will be installed
    virtualbox-iso: --> Finished Dependency Resolution
    virtualbox-iso:
    virtualbox-iso: Dependencies Resolved
    virtualbox-iso:
    virtualbox-iso: ========================================
    virtualbox-iso: Package Arch   Version      Repository
    virtualbox-iso: Size
    virtualbox-iso: ========================================
    virtualbox-iso: Installing:
    virtualbox-iso: gcc     x86_64 4.8.2-16.el7 base  16 M
    virtualbox-iso: gcc-c++ x86_64 4.8.2-16.el7 base 7.2 M
    virtualbox-iso: Installing for dependencies:
    virtualbox-iso: cpp     x86_64 4.8.2-16.el7 base 5.9 M
    virtualbox-iso: glibc-devel
    virtualbox-iso: x86_64 2.17-55.el7  base 1.0 M
    virtualbox-iso: glibc-headers
    virtualbox-iso: x86_64 2.17-55.el7  base 650 k
    virtualbox-iso: libmpc  x86_64 1.0.1-3.el7  base  51 k
    virtualbox-iso: libstdc++-devel
    virtualbox-iso: x86_64 4.8.2-16.el7 base 1.5 M
    virtualbox-iso: mpfr    x86_64 3.1.1-4.el7  base 203 k
    virtualbox-iso:
    virtualbox-iso: Transaction Summary
    virtualbox-iso: ========================================
    virtualbox-iso: Install  2 Packages (+6 Dependent packages)
    virtualbox-iso:
    virtualbox-iso: Total download size: 33 M
    virtualbox-iso: Installed size: 80 M
    virtualbox-iso: Downloading packages:
    virtualbox-iso: warning: /var/cache/yum/x86_64/7/base/packages/glibc-devel-2.17-55.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
    virtualbox-iso: Public key for glibc-devel-2.17-55.el7.x86_64.rpm is not installed
    virtualbox-iso: (1/8): glibc-devel | 1.0 MB   00:06
    virtualbox-iso: (2/8): gcc-c++-4.8 | 7.2 MB   00:09
    virtualbox-iso: (3/8): libmpc-1.0. |  51 kB   00:00
    virtualbox-iso: (4/8): glibc-heade | 650 kB   00:00
    virtualbox-iso: (5/8): cpp-4.8.2-1 | 5.9 MB   00:09
    virtualbox-iso: (6/8): mpfr-3.1.1- | 203 kB   00:00
    virtualbox-iso: (7/8): libstdc++-d | 1.5 MB   00:00
    virtualbox-iso: (8/8): gcc-4.8.2-1 |  16 MB   00:13
    virtualbox-iso: ----------------------------------------
    virtualbox-iso: Total      2.4 MB/s |  33 MB  00:13
    virtualbox-iso: Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    virtualbox-iso: Importing GPG key 0xF4A80EB5:
    virtualbox-iso: Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) "
    virtualbox-iso: Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
    virtualbox-iso: Package    : centos-release-7-0.1406.el7.centos.2.3.x86_64 (@anaconda)
    virtualbox-iso: From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    virtualbox-iso: Running transaction check
    virtualbox-iso: Running transaction test
    virtualbox-iso: Transaction test succeeded
    virtualbox-iso: Running transaction
    virtualbox-iso:   Installing : mpfr-3.1.1-4.el7.x   1/8
    virtualbox-iso:   Installing : libmpc-1.0.1-3.el7   2/8
    virtualbox-iso:   Installing : cpp-4.8.2-16.el7.x   3/8
    virtualbox-iso:   Installing : glibc-headers-2.17   4/8
    virtualbox-iso:   Installing : glibc-devel-2.17-5   5/8
    virtualbox-iso:   Installing : gcc-4.8.2-16.el7.x   6/8
    virtualbox-iso:   Installing : libstdc++-devel-4.   7/8
    virtualbox-iso:   Installing : gcc-c++-4.8.2-16.e   8/8
    virtualbox-iso: Verifying  : libstdc++-devel-4.   1/8
    virtualbox-iso: Verifying  : mpfr-3.1.1-4.el7.x   2/8
    virtualbox-iso: Verifying  : glibc-headers-2.17   3/8
    virtualbox-iso: Verifying  : glibc-devel-2.17-5   4/8
    virtualbox-iso: Verifying  : libmpc-1.0.1-3.el7   5/8
    virtualbox-iso: Verifying  : gcc-c++-4.8.2-16.e   6/8
    virtualbox-iso: Verifying  : gcc-4.8.2-16.el7.x   7/8
    virtualbox-iso: Verifying  : cpp-4.8.2-16.el7.x   8/8
    virtualbox-iso:
    virtualbox-iso: Installed:
    virtualbox-iso: gcc.x86_64 0:4.8.2-16.el7
    virtualbox-iso: gcc-c++.x86_64 0:4.8.2-16.el7
    virtualbox-iso:
    virtualbox-iso: Dependency Installed:
    virtualbox-iso: cpp.x86_64 0:4.8.2-16.el7
    virtualbox-iso: glibc-devel.x86_64 0:2.17-55.el7
    virtualbox-iso: glibc-headers.x86_64 0:2.17-55.el7
    virtualbox-iso: libmpc.x86_64 0:1.0.1-3.el7
    virtualbox-iso: libstdc++-devel.x86_64 0:4.8.2-16.el7
    virtualbox-iso: mpfr.x86_64 0:3.1.1-4.el7
    virtualbox-iso:
    virtualbox-iso: Complete!
==> virtualbox-iso: Provisioning with shell script: scripts/vagrant.sh
    virtualbox-iso: % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
    virtualbox-iso: Dload  Upload   Total   Spent    Left  Speed
    virtualbox-iso: 100   409  100   409    0     0     77      0  0:00:05  0:00:05 --:--:--    83
==> virtualbox-iso: Provisioning with shell script: scripts/virtualbox.sh
    virtualbox-iso: mount: /dev/loop0 is write-protected, mounting read-only
    virtualbox-iso: Verifying archive integrity... All good.
    virtualbox-iso: Uncompressing VirtualBox 4.3.14 Guest Additions for Linux............
    virtualbox-iso: VirtualBox Guest Additions installer
    virtualbox-iso: Copying additional installer modules ...
    virtualbox-iso: Installing additional modules ...
    virtualbox-iso: Removing existing VirtualBox non-DKMS k[  OK  ]dules
    virtualbox-iso: Building the VirtualBox Guest Additions kernel modules
    virtualbox-iso: Building the main Guest Additions modul[  OK  ]
    virtualbox-iso: Building the shared folder support modu[  OK  ]
    virtualbox-iso: Building the OpenGL support module     [  OK  ]
    virtualbox-iso: Doing non-kernel setup of the Guest Add[  OK  ]
    virtualbox-iso: Starting the VirtualBox Guest Additions[  OK  ]
    virtualbox-iso: Installing the Window System drivers
    virtualbox-iso: Could not find the X.Org or XFree86 Window System, skipping.
==> virtualbox-iso: Provisioning with shell script: scripts/cleanup.sh
    virtualbox-iso: Loaded plugins: fastestmirror
    virtualbox-iso: No Match for argument: gtk2
    virtualbox-iso: No Match for argument: libX11
    virtualbox-iso: No Match for argument: hicolor-icon-theme
    virtualbox-iso: No Match for argument: bitstream-vera-fonts
    virtualbox-iso: Resolving Dependencies
    virtualbox-iso: --> Running transaction check
    virtualbox-iso: ---> Package avahi.x86_64 0:0.6.31-13.el7 will be erased
    virtualbox-iso: --> Processing Dependency: avahi = 0.6.31-13.el7 for package: avahi-libs-0.6.31-13.el7.x86_64
    virtualbox-iso: ---> Package freetype.x86_64 0:2.4.11-9.el7 will be erased
==> virtualbox-iso: Gracefully halting virtual machine...
==> virtualbox-iso: Preparing to export machine...
    virtualbox-iso: Deleting forwarded port mapping for SSH (host port 3213)
==> virtualbox-iso: Exporting virtual machine...
    virtualbox-iso: Executing: export packer-virtualbox-iso --output output-virtualbox-iso/packer-virtualbox-iso.ovf
==> virtualbox-iso: Unregistering and deleting virtual machine...
==> virtualbox-iso: Running post-processor: vagrant
==> virtualbox-iso (vagrant): Creating Vagrant box for 'virtualbox' provider
    virtualbox-iso (vagrant): Copying from artifact: output-virtualbox-iso/packer-virtualbox-iso-disk1.vmdk
    virtualbox-iso (vagrant): Copying from artifact: output-virtualbox-iso/packer-virtualbox-iso.ovf
    virtualbox-iso (vagrant): Renaming the OVF to box.ovf...
    virtualbox-iso (vagrant): Compressing: Vagrantfile
    virtualbox-iso (vagrant): Compressing: box.ovf
    virtualbox-iso (vagrant): Compressing: metadata.json
    virtualbox-iso (vagrant): Compressing: packer-virtualbox-iso-disk1.vmdk
Build 'virtualbox-iso' finished.

==> Builds finished. The artifacts of successful builds are:
--> virtualbox-iso: 'virtualbox' provider box: centos-7-0-x64-virtualbox.box

無事boxファイルが作成されました。

$ ls -la                                 (git)-[master] ? [ ~/my_packer_templates/centos-7.0]
total 1058288
drwxr-xr-x  7 y-sakamoto  staff        238  8 11 18:09 .
drwxr-xr-x  8 y-sakamoto  staff        272  8 11 18:06 ..
-rw-r--r--  1 y-sakamoto  staff  541837956  8 11 18:09 centos-7-0-x64-virtualbox.box
drwxr-xr-x  3 y-sakamoto  staff        102  8 11 16:49 http
drwxr-xr-x  3 y-sakamoto  staff        102  8 11 16:57 packer_cache
drwxr-xr-x  8 y-sakamoto  staff        272  8 11 16:49 scripts
-rw-r--r--  1 y-sakamoto  staff       2713  8 11 16:49 template.json

boxを追加する

$ vagrant box add centos-7.0 centos-7-0-x64-virtualbox.box
==> box: Adding box 'centos-7.0' (v0) for provider:
    box: Downloading: file:///Users/y-sakamoto/my_packer_templates/centos-7.0/centos-7-0-x64-virtualbox.box
==> box: Successfully added box 'centos-7.0' (v0) for 'virtualbox'!
$ vagrant box list                                   (git)-[master]  [ ~/my_packer_templates]
centos-7.0 (virtualbox, 0)

無事に追加されたので、vagrant init なり vagrant upなりして 美味しく召し上がりましょう。