Unattended Linux Installation on KVM with virsh and kickstart

For system administrators looking to automate deployments, using Kickstart files for automated installations can be a highly efficient approach. This lesson will show you how to install Rocky Linux 9 on a KVM virtual machine using a Kickstart file and the virsh command-line tool.

Before starting, ensure you have the following:

  1. A host system with KVM and libvirt installed and configured.
  2. A Kickstart file (kickstart.cfg) configured for your Rocky Linux 9 installation requirements.
  3. The Rocky Linux 9 ISO image downloaded and reachable.
  4. Basic familiarity with the command line and virsh commands.

Prepare the Kickstart file

To prepare your Kickstart file (kickstart.cfg) open a new file in terminal. A simple Kickstart file might look like this:

# Perform a text based installation
text

# Set the installation language
lang en_US.UTF-8

# Set the keyboard layout
keyboard us

# Enable the firewall
firewall --enabled

# Set the local timezone
timezone Europe/Budapest

# Network configuration via DHCP
network --bootproto dhcp

# Create a new user with wheel group access
user --name=tmolnar --password=titkos12 --groups=wheel --plaintext

# Set the root user password
rootpw --plaintext titkos12

# Clear the existing storage
zerombr
clearpart --all --initlabel

# Automatically create the default storage layout
autopart

# Included packages
%packages
@^minimal-environment
%end

# Reboot the node
reboot

Ensure this file is accessible on the host system.

Install the Rocky Linux

Run the following command to start the installation using the Kickstart file:

virt-install \
--name ks-rocky \
--ram 2048 \
--vcpus 2 \
--disk path=/home/tmolnar/Documents/kvm-vms/ks-rocky.qcow2,format=qcow2,size=20 \
--os-variant rocky9 \
--network network=default \
--graphics none \
--location /data/datahdd/Lab\ Installers/Rocky-9.4-x86_64-minimal.iso \
--initrd-inject=/home/tmolnar/kickstart.cfg \
--extra-args="inst.ks=file:/kickstart.cfg console=tty0 console=ttyS0,115200n8"

Explanation of the options:

  • --name: The name of the virtual machine.
  • --ram: The amount of memory allocated to the VM.
  • --vcpus: The number of virtual CPUs.
  • --disk: The path and format of the virtual disk.
  • --os-variant: Specify the operating system variant.
  • --network: The network configuration.
  • --graphics none: Disables graphical output (console mode).
  • --location: The path to the Rocky Linux 9 ISO.
  • --initrd-inject: Injects the Kickstart file into the VM.
  • --extra-args: Additional arguments passed to the installer, including the Kickstart file location and console configuration.

Post-installation steps

Once the installation is complete, you may need to perform additional configuration depending on your use case. This might include setting up users, configuring services, and applying security settings.

Connect to the console with virsh:

virsh console ks-rocky

You can make the necessary changes or set up Ansible to do it for you automatically.

dnf update
hostnamectl set-hostname ks-rocky.tomsitcafe.com
dnf install qemu-guest-agent

Automating the installation of Rocky Linux 9 on a KVM virtual machine using a Kickstart file and virsh automates the deployment process, saving time and reducing the potential for manual errors. By following the steps outlined in this lesson, you can efficiently set up Rocky Linux environments configured for your specific requirements.

If you want to discuss the topic with other technology-minded people, join my Discord: https://discord.gg/YbSYGsQYES

Now we have an IRC channel as well: irc.libera.chat / #tomsitcafe

Leave a comment