Minimal Linux with Packer and Vagrant

As an IT professional, it’s important to take the time to sharpen your tools. And as a systems programmer and project leader in research computing, one of my most valuable tools is a lean, mean operating system install. Prior to the deployment of Wharton’s High Performance Computing Cluster (HPCC), the goal was to reacquaint myself with a minimal Linux from scratch. Enter Packer – a tool for building machine images.

Here I am using Packer to build an image for VirtualBox containing a minimal CentOS 7 install:

Getting started…

Next let’s take a look at our packer file: centos7.json

{
    "variables": {
    },
    "builders": [{
        "type": "virtualbox-iso",
        "guest_os_type": "RedHat_64",
        "iso_url": "CentOS-7.0-1406-x86_64-Minimal.iso",
        "iso_checksum": "e3afe3f1121d69c40cc23f0bafa05e5d",
        "iso_checksum_type": "md5",
        "ssh_username": "vagrant",
        "ssh_password": "vagrant",
        "ssh_wait_timeout": "600s",
        "vm_name": "centos70",
        "http_directory": "./",
        "boot_wait": "10s",
        "boot_command": [
            "<esc><wait>",
            "linux ks=http://{{.HTTPIP}}:{{.HTTPPort}}/ks.cfg",
            "<enter><wait>"
        ],
        "shutdown_command": "echo 'vagrant' |sudo -S shutdown -P now",
        "vboxmanage": [
            ["modifyvm", "{{.Name}}", "--memory", "512"],
            ["modifyvm", "{{.Name}}", "--usb", "off"],
            ["modifyvm", "{{.Name}}", "--audio", "none"]
        ]
    }],
    "provisioners": [{
        "type": "shell",
        "inline": [
            "sudo yum -y install http://download.fedoraproject.org/pub/epel/beta/7/x86_64/epel-release-7-0.2.noarch.rpm",
            "sudo yum -y install gcc make bzip2 kernel-headers kernel-devel dkms",
            "sudo yum clean all",
            "sudo mount -o loop /home/vagrant/VBoxGuestAdditions.iso /mnt",
            "sudo sh /mnt/VBoxLinuxAdditions.run",
            "sudo umount /mnt",
            "sudo /etc/rc.d/init.d/vboxadd setup",
            "sudo rm -f /home/vagrant/VBoxGuestAdditions.iso",
            "sudo dd if=/dev/zero of=/boot/zero bs=1M",
            "sudo rm -f /boot/zero",
            "sudo dd if=/dev/zero of=/zero bs=1M",
            "sudo rm -f /zero"
        ]
    }],
    "post-processors": [{
        "type": "vagrant",
        "output": "./centos-70-x64-{{.Provider}}-minimal.box",
        "vagrantfile_template": "./Vagrantfile"
    }]
}

Make sure to update the above json for the exact ISO filename and checksum. The anaconda installer in CentOS also needs to know what to do via a kickstart file: ks.cfg

install
text
cdrom
skipx
lang en_US.UTF-8
keyboard us
timezone UTC
rootpw vagrant
user --name=vagrant --password=vagrant
auth --enableshadow --passalgo=sha512 --kickstart
firewall --disabled
selinux --permissive
bootloader --location=mbr
zerombr
clearpart --all --initlabel
autopart
firstboot --disable
reboot

%packages --instLangs=en_US.utf8 --nobase --ignoremissing --excludedocs
openssh-clients
sudo
kernel-headers
kernel-devel
gcc
make
perl
curl
wget
nfs-utils
net-tools
vim-minimal
bzip2
-fprintd-pam
-intltool
-mariadb-libs
-postfix
-linux-firmware
-aic94xx-firmware
-atmel-firmware
-b43-openfwwf
-bfa-firmware
-ipw2100-firmware
-ipw2200-firmware
-ivtv-firmware
-iwl100-firmware
-iwl105-firmware
-iwl135-firmware
-iwl1000-firmware
-iwl2030-firmware
-iwl2000-firmware
-iwl3060-firmware
-iwl3160-firmware
-iwl3945-firmware
-iwl4965-firmware
-iwl5000-firmware
-iwl5150-firmware
-iwl6000-firmware
-iwl6000g2a-firmware
-iwl6000g2b-firmware
-iwl6050-firmware
-iwl7260-firmware
-libertas-sd8686-firmware
-libertas-sd8787-firmware
-libertas-usb8388-firmware
-ql2100-firmware
-ql2200-firmware
-ql23xx-firmware
-ql2400-firmware
-ql2500-firmware
-rt61pci-firmware
-rt73usb-firmware
-xorg-x11-drv-ati-firmware
-zd1211-firmware
%end

%post --log=/root/ks.log
echo "vagrant ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/vagrant
echo "Defaults:vagrant !requiretty" >> /etc/sudoers.d/vagrant
chmod 0440 /etc/sudoers.d/vagrant
mkdir -pm 700 /home/vagrant/.ssh
#curl -o /home/vagrant/.ssh/authorized_keys https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub
cat <<EOK >/home/vagrant/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8Y\
Vr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdO\
KLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7Pt\
ixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmC\
P3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcW\
yLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key
EOK
chmod 0600 /home/vagrant/.ssh/authorized_keys
chown -R vagrant.vagrant /home/vagrant/.ssh
yum -y update
yum -y remove linux-firmware
%end

Now that we have a Packer and a kickstart file, we can issue the Packer build command:

packer build centos7.json

It is pretty neat to watch packer do its thing. Packer will now boot a virtual machine from the ISO, using the kickstart file to configure it. If all goes according to plan, we’ll have a box file that we can open via Vagrant.

On that note, let’s take a look at a minimal Vagrant config file: Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
	config.vm.box = "bug/centos70"
	config.vm.box_url = "centos-70-x64-virtualbox-minimal.box"
	config.vm.provider :virtualbox do |p|
		p.customize ["modifyvm", :id, "--memory", 1024]
		p.customize ["modifyvm", :id, "--cpus", 2]
		p.customize ["modifyvm", :id, "--cpuexecutioncap", 50]
	end
	config.vm.hostname = "centos70"
	config.vm.network "forwarded_port", guest: 80, host: 8000
	#config.vm.provision :shell, path: "bootstrap.sh"
	#config.vm.provision "ansible" do |ansible|
	#	ansible.playbook = "provisioning/site.yml"
	#	ansible.host_key_checking = false
	#	ansible.raw_ssh_args = '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PasswordAuthentication=no -o IdentitiesOnly=yes'
	#end
end

We can now take the minimal OS for a test drive with these commands:

vagrant up
vagrant ssh

All of this marked the beginning of Red Hat Enterprise Linux 7 support for the Wharton HPCC system (CentOS being binary compatible with RHEL).

As a specialist in Linux and high-performance computing, Burris enjoys enabling faculty within The Wharton School of the University of Pennsylvania by providing effective research computing resources. Burris has been involved in research computing since 2001. Current projects find Burris working with HPC, big data, cloud computing and grid technologies. His favorite languages are Python and BASH. In his free time, he enjoys bad cinema, video editing, synthesizers and bicycling.