hakk

software development, devops, and other drivel
Tree lined path

CentOS 9 Automated KVM Install Using Kickstart

A quick demo using virt-install and Kickstart to provision a new CentOS 9 virtual machine. This uses KVM (Kernel Virtual Machine) and libvirt to manage the VM. And uses Kickstart to setup and install the new system within the virtual machine.

Creating kickstart file

I’m going to post my Kickstart file on GitHub as a reference. It installs a headless server with some system admin tools, sets up networking, SELinux, and uses autopart to partition the disk with LVM support. This also assumes that you have setup a Bridge Network that is shared to KVM so the local network can be used instead of the libvirt network.

#version=RHEL9
text
cdrom
eula --agreed

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

# Keyboard layouts
keyboard --xlayouts='us'
# System language
lang en_US.UTF-8

# Use CDROM installation media
cdrom

%packages
@^server-product-environment
@console-internet
@debugging
@performance
@system-tools

%end

# Run the Setup Agent on first boot
firstboot --enable

ignoredisk --only-use=/dev/vda
# System bootloader configuration
bootloader --append="crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M" --location=mbr --boot-drive=vda
autopart --type=lvm
reboot --eject
# Partition clearing information
clearpart --none --initlabel

# System timezone
timezone America/New_York --utc

###########################################################################################
# 
# User Accounts
# Generate encrypted password: python -c 'import crypt; print(crypt.crypt("My Password"))'
# Or  openssl passwd -1 password
#
###########################################################################################

# Root password
rootpw --iscrypted <encrypted password>
# Admin user
user --groups=wheel --name=<default-user> --password=<encrypted password> --iscrypted --gecos="<default-user>"

# network
network --bootproto=dhcp --noipv6 --onboot=on --device=enp1s0

# SELinux
selinux --enforcing

The Kickstart documentation recommends manually installing a system first and then using the file generated from that (located in the root home /root/anaconda-ks.cfg) to create your installation file. You can also find more Kickstart commands and options on the documentation page.

Virt Install

Download the CentOS 9 install disk to your local hard drive, it’s quite large coming in over 9 GB. After it’s downloaded place the kickstart file and the following shell script in the same directory. Update the path to the CentOS disk iso as well.

#!/usr/bin/env bash
 
## Define variables
MEM_SIZE=2048       # Memory setting in MiB
VCPUS=2             # CPU Cores count
BRIDGE_NETWORK=br0  # Name of the Bridge Network
OS_VARIANT="rhel9.0" # virt-install --osinfo list
ISO_FILE="/path/to/downloads/centos/CentOS-Stream-9-latest-x86_64-dvd1.iso" # Path to ISO file
VM_NAME="centos9ks" # The VM name
DISK_SIZE=20 # Disk size in GB
 
virt-install \
     --name ${VM_NAME} \
     --memory=${MEM_SIZE} \
     --vcpus=${VCPUS} \
     --location ${ISO_FILE} \
     --disk size=${DISK_SIZE}  \
     --network bridge=${BRIDGE_NETWORK} \
     --graphics=none \
     --os-variant=${OS_VARIANT} \
     --console pty,target_type=serial \
     --initrd-inject centos9-ks.cfg --extra-args "inst.ks=file:/centos9-ks.cfg console=tty0 console=ttyS0,115200n8"

Run the script using sudo and go get a fresh cup of coffee. If all goes well you’ll come back to a brand new CentOS 9 system running.