<repo_name>TFDuesing/packer-templates<file_sep>/README.markdown
packer-templates
================

This is a collection of Packer templates, kickstart files, and shell scripts that can be used to build Vagrant boxes for VirtualBox and VMware providers, starting from canonical Linux distribution ISOs.


Prerequisites
-------------

* [Packer](http://www.packer.io/)
* [VirtualBox](https://www.virtualbox.org) and/or [VMWare Fusion](http://www.vmware.com/products/fusion)
* [Vagrant](http://www.vagrantup.com)


Usage
-----

	$ git clone https://github.com/TFDuesing/packer-templates.git
	$ cd packer-templates/<desired template directory>
	$ packer build template.json

Once the build is complete, Packer will output one or more Vagrant .box files in the current working directory.  You can add a box to your Vagrant installation with `vagrant box add --name <name-of-box> </path/to/.box file>`.


Helpful Documentation
---------------------

* [`packer build`](http://www.packer.io/docs/command-line/build.html)
* [`vagrant box`](http://docs.vagrantup.com/v2/boxes.html)
<file_sep>/Fedora-20/scripts/30virtualbox.sh
#!/usr/bin/env bash

# Check which Packer builder type is being used
if [ $PACKER_BUILDER_TYPE = "virtualbox-iso" ]; then
  # Required for the VirtualBox Guest Additions for Linux
  # http://www.virtualbox.org/manual/ch04.html
  yum install --assumeyes bzip2 dkms gcc kernel-devel make
  
  # Required to prevent VBoxLinuxAdditions.run from throwing (harmless) errors
  yum install --assumeyes which
  
  # Install the VirtualBox Guest Additions
  mkdir /mnt/cdrom
  mount /root/VBoxGuestAdditions.iso /mnt/cdrom
  sh /mnt/cdrom/VBoxLinuxAdditions.run
  
  # Cleanup
  umount /mnt/cdrom
  rmdir /mnt/cdrom
  rm --force /root/VBoxGuestAdditions.iso
      
  # Fix permissions and SELinux context for Packer’s virtualbox_version_file
  chmod 0644 /etc/virtualbox-version
  chcon system_u:object_r:etc_t:s0 /etc/virtualbox-version
fi
<file_sep>/Fedora-20/scripts/50cleanup.sh
#!/usr/bin/env bash

# Remove unnecessary packages (and their dependencies)
# which can’t be removed until after the installation process
yum --assumeyes autoremove authconfig firewalld linux-firmware

# Clean up old yum repo data & logs
yum clean all
yum history new
rm --recursive --force /var/lib/yum/yumdb/*
rm --recursive --force /var/lib/yum/history/*
truncate --no-create --size=0 /var/log/yum.log

# Remove random-seed, so it’s not the same in every image
rm --force /var/lib/random-seed

# Change any incorrect SELinux context labels
fixfiles -R -a restore

# Force the filesystem to reclaim space from deleted files
dd if=/dev/zero of=/var/tmp/zeros bs=1M
rm --force /var/tmp/zeros
<file_sep>/Fedora-23/scripts/30cleanup.sh
#!/usr/bin/env sh

# Remove UUID from non-loopback interface configuration files
sed --in-place '/^UUID/d' /etc/sysconfig/network-scripts/ifcfg-eth*

# Remove random-seed, so it’s not the same in every image
rm --force /var/lib/systemd/random-seed

# Truncate machine-id, for the same reasons random-seed is removed
# Note: this image will not boot if /etc/machine-id is not present, but systemd
# will generate a new machine ID if /etc/machine-id is present but empty
truncate --size=0 /etc/machine-id

# Remove SSH host keys
rm --force /etc/ssh/ssh_host_*

# Clean up dnf repo data, keys & logs
dnf clean all
rpm -e gpg-pubkey
rm --recursive --force /var/lib/dnf/history/*
rm --recursive --force /var/lib/dnf/yumdb/*
truncate --no-create --size=0 /var/log/dnf.*

# Force the filesystem to reclaim space from deleted files
dd if=/dev/zero of=/var/tmp/zeros bs=1M
rm --force /var/tmp/zeros
<file_sep>/Fedora-23/README.markdown
Fedora-23
=========

This Packer template can be used to build minimal Fedora 23 base boxes for Vagrant’s VirtualBox provider, starting from Fedora 23 Server’s netinstall ISO for x86_64 architectures.

Vagrant base boxes built from this template can be downloaded from [Atlas][1] directly, or by running `vagrant init TFDuesing/Fedora-23`.

[1]:https://atlas.hashicorp.com/TFDuesing/boxes/Fedora-23
<file_sep>/Fedora-20/README.markdown
Fedora-20
=========

This Packer template can be used to build minimal Fedora 20 base boxes for Vagrant’s VirtualBox and VMware providers, starting from Fedora’s “Install DVD” ISO for x86_64 architectures.

Vagrant base boxes built from this template can be downloaded from [Atlas][1] directly, or by running `vagrant init TFDuesing/Fedora-20`.

[1]:https://atlas.hashicorp.com/TFDuesing/boxes/Fedora-20
<file_sep>/Fedora-21/scripts/30cleanup.sh
#!/usr/bin/env sh

# Remove UUID from non-loopback interface configuration files
sed --in-place '/^UUID/d' /etc/sysconfig/network-scripts/ifcfg-eth*

# Remove random-seed, so it’s not the same in every image
rm --force /var/lib/systemd/random-seed

# Truncate machine-id, for the same reasons random-seed is removed
# Note: this image will not boot if /etc/machine-id is not present, but systemd
# will generate a new machine ID if /etc/machine-id is present but empty
truncate --size=0 /etc/machine-id

# Clean up old yum repo data & logs
yum clean all
yum history new
rm --recursive --force /var/lib/yum/yumdb/*
rm --recursive --force /var/lib/yum/history/*
truncate --no-create --size=0 /var/log/yum.log

# Force the filesystem to reclaim space from deleted files
dd if=/dev/zero of=/var/tmp/zeros bs=1M
rm --force /var/tmp/zeros
<file_sep>/Fedora-20/scripts/10extlinux.sh
#!/usr/bin/env bash

# Set the timeout for the EXTLINUX bootloader to 1/10 second
sed --in-place 's/^timeout 50$/timeout 1/' /boot/extlinux/extlinux.conf
<file_sep>/Fedora-23/scripts/10vmware.sh
#!/usr/bin/env sh

# Check which Packer builder type is being used
if [ $PACKER_BUILDER_TYPE = "vmware-iso" ]; then
  # Required for VMware Tools for Linux Guests
  # http://kb.vmware.com/kb/1018414
  # http://pubs.vmware.com/fusion-7/topic/com.vmware.fusion.help.doc/GUID-08BB9465-D40A-4E16-9E15-8C016CC8166F.html
  dnf install --assumeyes tar net-tools gcc kernel-devel

  # Install VMware Tools
  mkdir /mnt/cdrom
  mount /root/linux.iso /mnt/cdrom
  tar -zxpf /mnt/cdrom/VMwareTools-*.tar.gz -C /tmp
  /tmp/vmware-tools-distrib/vmware-install.pl --default

  # Disable ThinPrint service
  systemctl disable vmware-tools-thinprint.service

  # Cleanup
  dnf remove --assumeyes tar net-tools gcc kernel-devel
  umount /mnt/cdrom
  rmdir /mnt/cdrom
  rm --force /root/linux.iso
  rm --force --recursive /tmp/vmware*

  # Prevent dnf from updating the kernel
  # in order to preserve the VMware kernel extensions
  echo "exclude=kernel-core" >> /etc/dnf/dnf.conf
fi
<file_sep>/Fedora-21/README.markdown
Fedora-21
=========

This Packer template can be used to build minimal Fedora 21 base boxes for Vagrant’s VirtualBox and VMware providers, starting from Fedora 21 Server’s netinstall ISO for x86_64 architectures.

Vagrant base boxes built from this template can be downloaded from [Atlas][1] directly, or by running `vagrant init TFDuesing/Fedora-21`.

[1]:https://atlas.hashicorp.com/TFDuesing/boxes/Fedora-21
<file_sep>/Fedora-22/scripts/10virtualbox.sh
#!/usr/bin/env sh

# Check which Packer builder type is being used
if [ $PACKER_BUILDER_TYPE = "virtualbox-iso" ]; then
  # Required for the VirtualBox Guest Additions for Linux
  # http://www.virtualbox.org/manual/ch04.html
  dnf install --assumeyes bzip2 dkms kernel-devel

  # Install the VirtualBox Guest Additions
  mkdir /mnt/cdrom
  mount /root/VBoxGuestAdditions.iso /mnt/cdrom
  sh /mnt/cdrom/VBoxLinuxAdditions.run

  # Disable VirtualBox X11 Guest Additions
  systemctl disable vboxadd-x11.service

  # Cleanup
  dnf remove --assumeyes bzip2 dkms kernel-devel
  umount /mnt/cdrom
  rmdir /mnt/cdrom
  rm --force /root/VBoxGuestAdditions.iso

  # Prevent dnf from updating the kernel
  # in order to preserve the VirtualBox kernel extensions
  echo "exclude=kernel-core" >> /etc/dnf/dnf.conf

  # Fix permissions and SELinux context for Packer’s virtualbox_version_file
  chmod 0644 /root/virtualbox-version
  chcon system_u:object_r:admin_home_t:s0 /root/virtualbox-version
fi
<file_sep>/Fedora-22/README.markdown
Fedora-22
=========

This Packer template can be used to build minimal Fedora 22 base boxes for Vagrant’s VirtualBox and VMware providers, starting from Fedora 22 Server’s netinstall ISO for x86_64 architectures.

Vagrant base boxes built from this template can be downloaded from [Atlas][1] directly, or by running `vagrant init TFDuesing/Fedora-22`.

[1]:https://atlas.hashicorp.com/TFDuesing/boxes/Fedora-22
<file_sep>/Fedora-20/scripts/20network.sh
#!/usr/bin/env bash

# Remove MAC address and UUID from non-loopback interface configuration files
sed --in-place '/^HWADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth*
sed --in-place '/^UUID/d' /etc/sysconfig/network-scripts/ifcfg-eth*

# Tell udev to disable the assignment of fixed network interface names
# http://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames/
ln --symbolic /dev/null /etc/udev/rules.d/80-net-name-slot.rules
<file_sep>/Fedora-20/scripts/40vagrant.sh
#!/usr/bin/env bash

# Document box build time
echo 'Built by Packer at' $(date '+%H:%M %Z on %B %-d, %Y') \
    > /etc/vagrant-box-build-time
chmod 0644 /etc/vagrant-box-build-time
chcon system_u:object_r:etc_t:s0 /etc/vagrant-box-build-time

# Create vagrant user, required by Vagrant box specs
# http://docs.vagrantup.com/v2/virtualbox/boxes.html
useradd --user-group vagrant
echo 'vagrant' | /usr/bin/passwd --stdin vagrant

# Give vagrant user permission to sudo
echo 'Defaults:vagrant !requiretty' > /etc/sudoers.d/vagrant
echo '%vagrant ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers.d/vagrant
chmod 0440 /etc/sudoers.d/vagrant
chcon system_u:object_r:etc_t:s0 /etc/sudoers.d/vagrant

# Install Vagrant’s “insecure” public key
mkdir --parents /home/vagrant/.ssh/
curl --location --remote-name \
    https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub
mv vagrant.pub /home/vagrant/.ssh/authorized_keys
chown --recursive vagrant:vagrant /home/vagrant/.ssh/
chmod 0700 /home/vagrant/.ssh/
chmod 0600 /home/vagrant/.ssh/authorized_keys
chcon --recursive unconfined_u:object_r:ssh_home_t:s0 /home/vagrant/.ssh/

# Delete and lock root password
passwd --delete root
passwd --lock root
<file_sep>/Fedora-20/scripts/30vmware.sh
#!/usr/bin/env bash

# Check which Packer builder type is being used
if [ $PACKER_BUILDER_TYPE = "vmware-iso" ]; then
  # Required for VMware Tools for Linux Guests
  # http://kb.vmware.com/kb/1018414
  # http://pubs.vmware.com/fusion-6/topic/com.vmware.fusion.help.doc/GUID-08BB9465-D40A-4E16-9E15-8C016CC8166F.html
  yum install --assumeyes tar perl net-tools make gcc kernel-devel
  
  # Install the VirtualBox Guest Additions
  mkdir /mnt/cdrom
  mount /root/linux.iso /mnt/cdrom
  tar -zxf /mnt/cdrom/VMwareTools-*.tar.gz -C /tmp
  /tmp/vmware-tools-distrib/vmware-install.pl --default
  
  # Cleanup
  umount /mnt/cdrom
  rmdir /mnt/cdrom
  rm --force /root/linux.iso
  rm --force --recursive /tmp/vmware*
fi
