カメニッキ

カメとインコと釣りの人です

Packerを試してみた

Packerインストール

Install Packer - Packer by HashiCorp

$ brew tap homebrew/binary
$ brew install packer
$ packer version

使ったファイル

momijiame/packer-vanilla-box

ファイル見てみる

構成

[PMAC226S 6]$ pwd;find . | sort | sed '1d;s/^\.//;s/\/\([^/]*\)$/|--\1/;s/\/[^/|]*/|  /g'
/Users/PMAC226S/src/github.com/momijiame/packer-vanilla-box/centos/6
|--.gitignore
|--artifacts
|  |--今回はここに出力される
|--centos6.json
|--http
|  |--ks.cfg
|--packer_cache
|  |--インストール用のisoが落ちてくる
|--scripts
|  |--cleanup.sh
|  |--netpersist.sh
|  |--publickey.sh
|  |--sshd.sh
|  |--sudoers.sh
|  |--vbox.sh
|  |--vfusion.sh
|  |--zerofill.sh

./centos6.json

{
  # 仮想マシン作成のために必要なビルダーを定義
  "builders": [
    # VirtualBox向けの記述(一つのjsonでVirtualBox用、VMware用を生成できる
    {
      # デフォルトのvm名(Vagrantfileで上書き可能)
      "vm_name": "centos6",
      # 何に対しインスタンス作成するか
      "type": "virtualbox-iso",
      # OSのタイプ。「RedHat6.4」ではなく「RedHat 64bitを意味する
      "guest_os_type": "RedHat_64",
      # isoのダウンロード元。サポートしているのは「http://」「file://」「/local/path/isofile」
      "iso_url": "http://ftp.iij.ad.jp/pub/linux/centos/6.6/isos/x86_64/CentOS-6.6-x86_64-minimal.iso",
      # iso_urlから落としたやつのチェックサム
      "iso_checksum": "4ed6c56d365bd3ab12cd88b8a480f4a62e7c66d2",
      # チェックサムの形式
      "iso_checksum_type": "sha1",
      # ユーザ・パスワード
      "ssh_username": "vagrant",
      "ssh_password": "vagrant",
      # 起動後SSH接続までの待つ時間
      "ssh_wait_timeout": "1h",
      # hddの容量
      "disk_size": "32768",
      # シャットダウン時のコマンド
      "shutdown_command": "echo 'vagrant' | sudo -S shutdown -P now",
      # guest_additionsのパス
      "guest_additions_path": "VBoxGuestAdditions.iso",
      # インストールする際にKickstartを配信するためのローカルフォルダ
      "http_directory": "./http",
      # ブート時にKickstartを指定する
      "boot_command": [
        "<tab> text ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/ks.cfg<enter><wait>"
      ],
      # virtualboxのCPU・メモリの設定
      "vboxmanage": [
        ["modifyvm", "{{.Name}}", "--memory", "512"],
        ["modifyvm", "{{.Name}}", "--cpus", "1"]
      ]
    },
    # VMware向けの記述
    {
      "vm_name": "centos6",
      "type": "vmware-iso",
      "guest_os_type": "centos-64",
      "iso_url": "http://ftp.iij.ad.jp/pub/linux/centos/6.6/isos/x86_64/CentOS-6.6-x86_64-minimal.iso",
      "iso_checksum": "4ed6c56d365bd3ab12cd88b8a480f4a62e7c66d2",
      "iso_checksum_type": "sha1",
      "ssh_username": "vagrant",
      "ssh_password": "vagrant",
      "ssh_wait_timeout": "1h",
      "disk_size": "32768",
      "shutdown_command": "echo 'vagrant' | sudo -S shutdown -P now",
      "tools_upload_flavor": "linux",
      "http_directory": "./http",
      "boot_command": [
        "<tab> text ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/ks.cfg<enter><wait>"
      ],
      "vmx_data": {
        "memsize": "512",
        "numvcpus": "1",
        "vhv.enable": "TRUE"
      }
    }
  ],
  # プロビジョニング
  "provisioners": [
    {
      # Shellを使用する
      "type": "shell",
      # 実行する際のコマンドsudoを使用している
      "execute_command": "echo 'vagrant' | {{.Vars}} sudo -E -S bash '{{.Path}}'",
      "scripts": [
        # 外部に保存したShellを使用
        "./scripts/netpersist.sh",
        "./scripts/publickey.sh",
        "./scripts/sudoers.sh",
        "./scripts/sshd.sh",
        "./scripts/vbox.sh",
        "./scripts/vfusion.sh",
        "./scripts/cleanup.sh",
        "./scripts/zerofill.sh"
      ]
    }
  ],
  # インストール完了後のコマンド
  "post-processors": [
    {
      # vagrantのboxを作る
      "type": "vagrant",
      # box生成中のファイルを保存しない
      "keep_input_artifact": false,
      # 作成先のパス
      "output": "./artifacts/centos6-{{.Provider}}.box"
    }
  ]
}

./scripts/netpersist.sh

udevがインターフェイスとUUIDを固定化して悪さするのを防ぐ

[PMAC226S 6]$ cat scripts/netpersist.sh
#!/bin/sh
ln -f -s /dev/null /etc/udev/rules.d/70-persistent-net.rules
sed -i "/^HWADDR/d" /etc/sysconfig/network-scripts/ifcfg-eth0
sed -i "/^UUID/d" /etc/sysconfig/network-scripts/ifcfg-eth0

./scripts/publickey.sh

sshの認証情報を配置

[PMAC226S 6]$ cat scripts/publickey.sh
#!/bin/sh
mkdir -p /home/vagrant/.ssh
chmod 0700 /home/vagrant/.ssh
curl -L -o /home/vagrant/.ssh/authorized_keys https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub
chmod 0600 /home/vagrant/.ssh/authorized_keys
chown -R vagrant:vagrant /home/vagrant/.ssh

./scripts/sudoers.sh

sudo時にTTY要求させない

[PMAC226S 6]$ cat scripts/sudoers.sh
#!/bin/sh

sed -i -e "s:^.*requiretty:#Defaults requiretty:" /etc/sudoers
sed -i "s/^\(.*env_keep = \"\)/\1PATH /" /etc/sudoers

./scripts/sshd.sh

[PMAC226S 6]$ cat scripts/sshd.sh
#!/bin/sh

echo "UseDNS no" >> /etc/ssh/sshd_config
echo "GSSAPIAuthentication no" >> /etc/ssh/sshd_config

./scripts/vbox.sh

VirtualBoxの時はGuestAdditionsを入れる

[PMAC226S 6]$ cat scripts/vbox.sh
#!/bin/sh

if [[ $PACKER_BUILDER_TYPE =~ virtualbox ]]; then
  mount -t iso9660 -o loop /home/vagrant/VBoxGuestAdditions.iso /mnt
  sh /mnt/VBoxLinuxAdditions.run
  umount /mnt
  rm -rf /home/vagrant/VBoxGuestAdditions.iso
fi

./scripts/vfusion.sh

VMwareの時はvmware-toolsを入れる

[PMAC226S 6]$ cat scripts/vfusion.sh
#!/bin/sh

if [[ $PACKER_BUILDER_TYPE =~ vmware ]]; then
  yum -y install fuse-libs
  mount -t iso9660 -o loop /home/vagrant/linux.iso /mnt
  tar zxf /mnt/VMwareTools-*.tar.gz -C /tmp/
  umount /mnt
  rm /home/vagrant/linux.iso
  /tmp/vmware-tools-distrib/vmware-install.pl -d
fi

./scripts/cleanup.sh

@harasouさんにも教えてもらった、boxを小さくするテクニック。ゴミ掃除

[PMAC226S 6]$ cat scripts/cleanup.sh
#!/bin/sh

package-cleanup --oldkernels --count=1 -y

yum -y clean all

rm -rf /tmp/*
rm -rf /var/tmp/*

./scripts/zerofill.sh

同上にサイズを小さくする

[PMAC226S 6]$ cat scripts/zerofill.sh
#!/bin/sh

dd if=/dev/zero of=zerofill bs=1M
rm -rf zerofill

ビルドする

[PMAC226S 6]$ packer build -only=virtualbox-iso centos6.json

勝手にVirtualBoxが立ち上がって処理が走り、boxが作られた!!!