Bala's Blog

CentOS image builds using Packer

December 6 2016
188 words, ~1 min. read
packer,  centos,  images 

Packer is a tool for creating machine and container images for multiple platforms from a single source configuration.

Steps

  1. Install packer (and ensure packer binary is in PATH)
  2. Create a directory structure to hold configuration files
  3. Execute packer packer build centos-6.8.json

Reference

Directory structure

├── centos-6.8.json
├── http
    └── ks.cfg

centos-6.8.json

{
  "variables": {
    "ip": "bala",
    "iso": "CentOS-6.8-x86_64-minimal.iso",
    "os_type": "centos-64",
    "os_name": "centos-6.8",
    "os_memory_size_mb": "4096",
    "os_disk_size_mb": "81920"
  },

  "builders": [
    {
      "type": "vmware-iso",
      "boot_command": [
        "<tab> text ks=http://{{user `ip`}}:{{ .HTTPPort }}/ks.cfg<enter><wait>"
      ],
      "boot_wait": "10s",
      "iso_urls": [
        "iso/{{user `iso`}}",
        "http://mirrors.kernel.org/centos/6.8/isos/x86_64/{{user `iso`}}"
      ],
      "iso_checksum_type": "sha256",
      "iso_checksum": "ec49c297d484b9da0787e5944edc38f7c70f21c0f6a60178d8e9a8926d1949f4",
      "http_directory": "http",
      "headless": false,
      "guest_os_type": "{{user `os_type`}}",
      "vm_name": "{{user `os_name`}}",
      "vmx_data": {
        "cpuid.coresPerSocket": "1",
        "memsize": "{{user `os_memory_size_mb`}}",
        "numvcpus": "1"
      },
      "disk_size": "{{user `os_disk_size_mb`}}",
      "ssh_username": "root",
      "ssh_password": "secret",
      "ssh_port": 22,
      "ssh_wait_timeout": "6000s",
      "shutdown_command": "/sbin/halt -p"
    }
  ]
}

ks.cfg

install
cdrom
lang en_US.UTF-8
keyboard us
network --bootproto=dhcp --device=eth0 --onboot=on
rootpw --iscrypted $1$Myjc9rc2$KqbQgxaXXq0VXrHxxk1D3/
firewall --disabled
authconfig --enableshadow --passalgo=sha512
selinux --enforcing
timezone UTC
bootloader --location=mbr
text
skipx
zerombr
clearpart --all --initlabel
autopart
auth --useshadow --enablemd5
firstboot --disabled
reboot

%packages --nobase
openssh-clients
%end

TIP: To create encrypted root password for use in ks.cfg, use openssl passwd -1 <password>