ubuntu-serverをvmware fusionに入れる(packerでbox作成)

# 自分用メモ

## vmwareのpackerを作成する

- 基本的にはググった結果をくっつける。
- 下記は参考程度に見てください。

```
{
  "provisioners": [
    {
      "type": "shell",
      "execute_command": "echo 'vagrant'|sudo -S sh '{{.Path}}'",
      "override": {
        "vmware-iso": {
          "scripts": [
            "scripts/base.sh",
            "scripts/vagrant.sh",
            "scripts/vmware.sh",
            "scripts/cleanup.sh",
            "scripts/zerodisk.sh"
          ]
        }
      }
    }
  ],
  "post-processors": [
    {
      "type": "vagrant",
      "override": {
        "vmware": {
          "output": "ubuntu_server_14.04.4_LTS.box"
        }
      }
    }
  ],
  "builders": [
    {
      "type": "vmware-iso",
      "boot_command": [
        "<esc><wait>",
        "<esc><wait>",
        "<enter><wait>",
        "/install/vmlinuz<wait>",
        " auto<wait>",
        " console-setup/ask_detect=false<wait>",
        " console-setup/layoutcode=us<wait>",
        " console-setup/modelcode=pc105<wait>",
        " debian-installer=en_US<wait>",
        " fb=false<wait>",
        " initrd=/install/initrd.gz<wait>",
        " kbd-chooser/method=us<wait>",
        " keyboard-configuration/layout=USA<wait>",
        " keyboard-configuration/variant=USA<wait>",
        " locale=en_US<wait>",
        " netcfg/get_hostname=ubuntu-1404<wait>",
        " netcfg/get_domain=vagrantup.com<wait>",
        " noapic<wait>",
        " preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg<wait>",
        " -- <wait>",
        "<enter><wait>"
      ],
      "boot_wait": "10s",
      "disk_size": 40960,
      "guest_os_type": "ubuntu-64",
      "http_directory": "http",
      "iso_checksum": "2ac1f3e0de626e54d05065d6f549fa3a",
      "iso_checksum_type": "md5",
      "iso_url": "http://www.ftp.ne.jp/Linux/packages/ubuntu/releases-cd/trusty/ubuntu-14.04.4-server-amd64.iso",
      "ssh_username": "vagrant",
      "ssh_password": "vagrant",
      "ssh_port": 22,
      "ssh_wait_timeout": "10000s",
      "shutdown_command": "echo 'shutdown -P now' > /tmp/shutdown.sh; echo 'vagrant'|sudo -S sh '/tmp/shutdown.sh'",
      "vmx_data": {
        "memsize": "512",
        "numvcpus": "1",
        "cpuid.coresPerSocket": "1"
      }
    }
  ]
}
```

- 上記でpackerのtemplateと呼ばれるものは作成完了なので、shellとかを揃える。
- httpディレクトリの下に"preseed.cfg"というファイルを作成する

```
debconf debconf/frontend select Noninteractive
choose-mirror-bin mirror/http/proxy string
d-i base-installer/kernel/override-image string linux-server
d-i clock-setup/utc boolean true
d-i clock-setup/utc-auto boolean true
d-i finish-install/reboot_in_progress note
d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean true
d-i partman-auto-lvm/guided_size string max
d-i partman-auto/choose_recipe select atomic
d-i partman-auto/method string lvm
d-i partman-lvm/confirm boolean true
d-i partman-lvm/confirm_nooverwrite boolean true
d-i partman-lvm/device_remove_lvm boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true
d-i partman/confirm_write_new_label boolean true

# Default user
d-i passwd/user-fullname string vagrant
d-i passwd/username string vagrant
d-i passwd/user-password password vagrant
d-i passwd/user-password-again password vagrant
d-i passwd/username string vagrant

# Minimum packages (see postinstall.sh)
d-i pkgsel/include string openssh-server
d-i pkgsel/install-language-support boolean true
d-i pkgsel/update-policy select none
d-i pkgsel/upgrade select none

d-i time/zone string Asia/Tokyo
d-i user-setup/allow-password-weak boolean true
d-i user-setup/encrypt-home boolean false
tasksel tasksel/first multiselect standard, server
```
- scriptsの下に各種作成
    - base.sh
    - cleanup.sh
    - vagrant.sh
    - virtualbox.sh
    - vmware.sh
    - zerodisk.sh

```
# base.sh
apt-get update
apt-get -y upgrade
apt-get -y install linux-headers-$(uname -r)

sed -i -e '/Defaults\s\+env_reset/a Defaults\texempt_group=sudo' /etc/sudoers
sed -i -e 's/%sudo  ALL=(ALL:ALL) ALL/%sudo  ALL=NOPASSWD:ALL/g' /etc/sudoers

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

# cleanup.sh
apt-get -y autoremove
apt-get -y clean

echo "cleaning up guest additions"
rm -rf VBoxGuestAdditions_*.iso VBoxGuestAdditions_*.iso.?

echo "cleaning up dhcp leases"
rm /var/lib/dhcp/*

echo "cleaning up udev rules"
rm /etc/udev/rules.d/70-persistent-net.rules
mkdir /etc/udev/rules.d/70-persistent-net.rules
rm -rf /dev/.udev/
rm /lib/udev/rules.d/75-persistent-net-generator.rules

# vagrant.sh
date > /etc/vagrant_box_build_time

mkdir /home/vagrant/.ssh
wget --no-check-certificate \
    'https://github.com/mitchellh/vagrant/raw/master/keys/vagrant.pub' \
    -O /home/vagrant/.ssh/authorized_keys
chown -R vagrant /home/vagrant/.ssh
chmod -R go-rwsx /home/vagrant/.ssh

# virtualbox.sh
apt-get -y install virtualbox-guest-utils

# vmware.sh
apt-get -y install open-vm-tools
echo -n ".host:/ /mnt/hgfs vmhgfs rw,ttl=1,uid=my_uid,gid=my_gid,nobootwait 0 0" >> /etc/fstab

# zerodisk.sh
dd if=/dev/zero of=/EMPTY bs=1M
rm -f /EMPTY
```

- 上記で作成したものが揃ったディレクトリ内で下記コマンドを実行

```packer build テンプレート名.json```

## vagrant作成

- 下準備は省略
- ubuntu-serverをvagrant upするとエラーが出る(VMWareだけ？)
- [ここ](http://stackoverflow.com/questions/31142607/how-can-i-resolve-the-error-the-hgfs-kernel-module-was-not-found-after-upgrading)と[ここ](https://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=2078905)参照でvmware-toolを入れる

```
# vagrant sshする
# --追記--
sudo apt-get update
sudo apt-get upgrade
# --追記ここまで--
sudo apt-get install -y build-essential linux-headers-$(uname -r)
sudo mkdir /mnt/cdrom
sudo mount /dev/sr0 /mnt/cdrom
ls /mnt/cdrom
tar xzvf /mnt/cdrom/VMwareTools-x.x.x-xxxx.tar.gz -C /tmp/
注： x.x.x-xxxx x.x.x-xxxx は、前の手順で検出されたバージョンです。
cd /tmp/vmware-tools-distrib/
# 手動でyesかEnterを押下
sudo ./vmware-install.pl
```

## 無事仮想環境にubuntu-serverを入れられました。