b0in.xyz

Notes - rockylinux 9 with packer, qemu, and kickstart

2023-10-09 11:26:00 -0700

Working on something longer about sort of “from scratch” infra and had to wrangle packer qemu and a set of instructions from around the web. Not meant as a guide for setting this up but a PURPOSEFULLY INCOMPLETE writeup

Add the plugin to the packer file

packer {
  required_plugins {
    qemu = {
      version = "~> 1"
      source  = "github.com/hashicorp/qemu"
    }
  }
}

Add our qemu rockylinux setup

source "qemu" "rocky9" {
  # You'll need the iso_url and checksum
  iso_url          = "..."
  iso_checksum     = "sha256:..."
  output_directory = "out"
  shutdown_command = "echo 'changemerocky' | sudo -S shutdown -P now"

  # this size is awfully big but the 'qcow2' format is "sparse". that means
  # that it won't take up that much space unless
  disk_size        = "100000M"
  format           = "qcow2"

  # without kvm and qemuargs -cpu host i kept getting kernel panics!
  # But you have to have kvm setup
  accelerator      = "kvm"
  qemuargs = [[
    "-cpu", "host"
  ]]
    
  # without expanding the memory, the ramdisk kept filling up
  memory         = "1024"

  # user and pass are created by the kickstart files
  ssh_username   = "rocky"
  ssh_password   = "changemerocky"
  ssh_timeout    = "10m"

  vm_name        = "rocky9-example"
  boot_wait      = "5s"

  # the kickstart file references /dev/vda and it comes from this.
  disk_interface = "virtio"

  # The opearting system on the iso file can reach out to the network.
  http_directory = "kickstart/"
  boot_command = [
    # tab to swap to a mode that lets you type in stuff into the default linux kernel args
    "<tab>", 
    # delete the phrase 'quiet' so we can get more text.
    "<bs><bs><bs><bs><bs>",
    # inst.text puts the installer in text mode as we may not have a monitor.
    #   lots of sources just use 'text' and 'ks' but those do not work 
    #   in newer versions of rocky
    "inst.text",
    # inst.ks is how we get the booted system to find the rest of the installation
    # instructions. we fetch it from the network! [1]
    "inst.ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/rocky9-kvm-ks.cfg",
    # enter causes us to boot and well, wait does what it says
    "<enter><wait>"
  ]
}

Note1:

One of the implications of kickstart is that you could build your own CDROM or thumbdrive that just points to a file on a public or private server. The file would then be run on any compatible machine you put the cdrom or thumbnail drive.

Exercise for the reader: think of the implications, good and bad, of this setup.

The (incomplete) kickstart file:

Most of this came from here but was tweaked for the software i’m running.

# A bunch of sources use the term 'install' but that did not seem to work
cdrom
lang en_US.UTF-8
keyboard us
network --bootproto=dhcp --hostname=rocky9-example
user --name=example --groups=wheel --plaintext --password=changemerocky
rootpw changemerocky
timezone UTC
# bootloaders are an article itself.
bootloader --append=' crashkernel=auto' --location=mbr --boot-drive=vda
text
skipx
ignoredisk --only-use=vda

zerombr

clearpart --none --initlabel

part pv.305 --fstype='lvmpv' --ondisk=vda --size=98000
part /boot --fstype='ext4' --ondisk=vda --size=1024 --label=BOOT
volgroup VGsystem --pesize=4096 pv.305
logvol /opt  --fstype='ext4' --size=5120 --name=LVopt --vgname=VGsystem
logvol /usr  --fstype='ext4' --size=10240 --name=LVusr --vgname=VGsystem
logvol /var  --fstype='ext4' --size=10240 --name=LVvar --vgname=VGsystem
logvol swap  --fstype='swap' --size=4096 --name=LVswap --vgname=VGsystem
logvol /  --fstype='ext4' --size=10240 --label='ROOT' --name=LVroot --vgname=VGsystem
logvol /tmp  --fstype='ext4' --size=5120 --name=LVtmp --vgname=VGsystem
logvol /var/log  --fstype='ext4' --size=10240 --name=LVvarlog --vgname=VGsystem
logvol /home  --fstype='ext4' --size=5120 --name=LVhome --vgname=VGsystem
 
firstboot --disabled
eula --agreed
services --disabled='chronyd' --enabled='sshd'
 
reboot
 
%packages --ignoremissing --excludedocs
@core
%end