Automate RHEL and CentOS Installation on KVM using Kickstart

Posted on 143 views

Hello all. This is my first tutorial on KVM Virtualization prepared as a reference and to help you automate boring GUI-based installation questions. We’re using Kickstart on KVM to deploy VMs. If you have a good hands-on knowledge of Linux and Virtualization, my bet is you have already worked with KVM and Kickstart.

In case this is the first time you are giving KVM a shot, worry less since you don’t need a deep understanding of KVM virtualization internals to use this guide. Just know that Virtualization enables rapid deployment by isolating the application in a known controlled environment and adds a layer of abstraction between the virtual machine and underlying physical hardware. The easiest way to get many VMs running on KVM is by using templates and Kickstart on KVM deployment model.

Major Advantages of Virtualization

  • Service Isolation : Application isolation and elimination of compatibility issues
  • Improved system security and reliability : Abstraction between the virtual machine and the underlying physical hardware
  • Faster server provisioning : Use of snapshots and templates.
  • Quicker disaster recovery : Keeping up-to-date snapshots of virtual machines – easy to redeploy
  • Dynamic load balancing : Live migration of overload to underutilized servers

What is Kickstart?

A tool created by Red Hat for system administrators to help them automate installation of Red Hat Linux family of Distributions. A single file containing the answers to all the questions that would normally be asked during a typical installation has to be created. Kickstart files can be kept on a single server system and read by individual computers during the installation, ready for Kickstart on KVM.

How to Perform Kickstart on KVM Installation?

Kickstart installations can be performed using a local CD-ROM, a local hard drive, or via NFS, FTP, or HTTP.

To use kickstart, you must:

  • Create a kickstart file.
  • Create a boot media with the kickstart file or make the kickstart file available on the network.
  • Make the installation tree available.
  • Start the kickstart installation.

In this post, I’ll provide you with basic kickstart file for quickstart; you can modify and advance it to suite your use case and environment setup. Used virt-install commands are provided as well. To save you quite some time, i wrote a simple bash script that makes the process of spinning new virtual machine easy and less tiresome.

Creating kickstart file

This is a kickstart file to help you get started. With penchant goal, edit and save it as ks.cfg under your home user directory.

cd ~/
vim ks.cfg

It has below contents – modify to suit your use case

###############################################################
#
# Environment setup
#
###############################################################

# url --url="http://192.168.122.1:8090/cent7"
text
cdrom
auth --enableshadow --passalgo=sha512
keyboard --vckeymap=us --xlayouts='us'
lang en_US.UTF-8
eula --agreed
reboot

###############################################################
#
# network configuration
#
###############################################################

network --bootproto=dhcp --noipv6 --onboot=on --device=eth0
timezone UTC

#network --bootproto=static --ip=192.168.122.100 --gateway=192.168.122.1  --netmask=255.255.255.0  --noipv6 --device=eth0 --nameserver=192.168.122.1,8.8.8.8 --activate
#network  --hostname=server1.example.com
#timezone Africa/Nairobi --isUtc

###############################################################
#
# partitioning
#
###############################################################
bootloader --timeout=1 --location=mbr --append="net.ifnames=0 biosdevname=0"
text
skipx
zerombr
clearpart --all --initlabel
autopart --type=lvm
reboot --eject

# part swap --asprimary --fstype="swap" --size=1024
# part /boot --fstype xfs --size=200
# part pv.01 --size=1 --grow
# volgroup rootvg01 pv.01
# logvol / --fstype xfs --name=lv01 --vgname=rootvg01 --size=1 --grow


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

rootpw StrongRootPassword 
# user --groups=wheel --name=computingforgeeks --password=StrongUserPassword --gecos="Computingforgeeks User"

###############################################################
#
# SELinux and Firewalld
#
###############################################################

selinux --enforcing
#selinux --permissive
#selinux --disabled
 
# firewall --enabled --ssh 
# firewall --disabled

 
###############################################################
#
# Software Packages
#
###############################################################

%packages --ignoremissing --excludedocs --instLangs=en_US.utf8
@core
@base
openssh-clients
bash-completion
sudo
selinux-policy-devel
nfs-utils
net-tools
tar
bzip2
drpm
rsync
yum-utils
elfutils-libelf-devel
network-scripts
%end

Few parameters are to be changed, maybe the IP address and hostname, timezoneif set.

Download OS installation ISO file

In this guide I’ll be working with CentOS Stream 8 DVD ISO file

cd ~/
wget http://centos.mirror.liquidtelecom.com/8-stream/isos/x86_64/CentOS-Stream-8-x86_64-latest-dvd1.iso
sudo mv CentOS-Stream-8-x86_64-latest-dvd1.iso /var/lib/libvirt/images

Create VM creation script

Let’s create a Virtual Machine creation script called create_vm.sh:

vim create_vm.sh

Below bash script is used to install a new vm – will ask you few questions

#!/usr/bin/env bash
 
## Define variables
MEM_SIZE=2048       # Memory setting in MiB
VCPUS=2             # CPU Cores count
OS_VARIANT="rhl8.0" # List with osinfo-query  os
ISO_FILE="/var/lib/libvirt/images/CentOS-Stream-8-x86_64-latest-dvd1.iso" # Path to ISO file

echo -en "Enter vm name: "
read VM_NAME
OS_TYPE="linux"
echo -en "Enter virtual disk size : "
read DISK_SIZE
 
sudo virt-install \
     --name $VM_NAME \
     --memory=$MEM_SIZE \
     --vcpus=$VCPUS \
     --os-type $OS_TYPE \
     --location $ISO_FILE \
     --disk size=$DISK_SIZE  \
     --network bridge=virbr0 \
     --graphics=none \
     --os-variant=$OS_VARIANT \
     --console pty,target_type=serial \
     --initrd-inject ks.cfg --extra-args "inst.ks=file:/ks.cfg console=tty0 console=ttyS0,115200n8"
     #--extra-args="ks=http://192.168.122.1/ks.cfg console=tty0 console=ttyS0,115200n8"

Replace $HOME/iso/CentOS-7-x86_64-Everything-1611.iso with the path to your ISO file.

You can modify other parameters like VCPU,RAM and Bridge to fit your use.

For virtual disk size, just enter number, e.g 10 for 10 GB

Save the script to a file called create_vm.sh. Make it executable and run it

chmod +x create_vm.sh
./create_vm.sh

After providing VM name and disk size automated installation of the OS with Kickstart begins

Enter vm name: test
Enter virtual disk size : 10

Starting install...
Retrieving file vmlinuz...                                                                                                                 | 9.7 MB  00:00:00
Retrieving file initrd.img...                                                                                                              |  72 MB  00:00:00
Allocating 'test.qcow2'                                                                                                                    |  10 GB  00:00:00
Connected to domain 'test'
Escape character is ^] (Ctrl + ])
OVR (bus 0 bus_irq 9 global_irq 9 high level)
[    0.000000] ACPI: INT_SRC_OVR (bus 0 bus_irq 10 global_irq 10 high level)
[    0.000000] ACPI: INT_SRC_OVR (bus 0 bus_irq 11 global_irq 11 high level)
[    0.000000] Using ACPI (MADT) for SMP configuration information
[    0.000000] smpboot: Allowing 1 CPUs, 0 hotplug CPUs
[    0.000000] PM: Registered nosave memory: [mem 0x00000000-0x00000fff]
[    0.000000] PM: Registered nosave memory: [mem 0x0009f000-0x0009ffff]
[    0.000000] PM: Registered nosave memory: [mem 0x000a0000-0x000effff]
[    0.000000] PM: Registered nosave memory: [mem 0x000f0000-0x000fffff]
[    0.000000] [mem 0x80000000-0xfeffbfff] available for PCI devices
[    0.000000] Booting paravirtualized kernel on KVM
[    0.000000] clocksource: refined-jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 1910969940391419 ns
[    0.000000] setup_percpu: NR_CPUS:8192 nr_cpumask_bits:1 nr_cpu_ids:1 nr_node_ids:1
[    0.000000] percpu: Embedded 54 pages/cpu s184320 r8192 d28672 u2097152
[    0.000000] kvm-guest: stealtime: cpu 0, msr 7942c080
[    0.000000] kvm-guest: PV spinlocks disabled, single CPU
[    0.000000] Built 1 zonelists, mobility grouping on.  Total pages: 515945
[    0.000000] Policy zone: DMA32
....

After complete installation login with created username and password:

....
[  OK  ] Started GSSAPI Proxy Daemon.
[  OK  ] Reached target NFS client services.
[  OK  ] Reached target Remote File Systems (Pre).
[  OK  ] Reached target Remote File Systems.
         Starting Permit User Sessions...
[  OK  ] Started OpenSSH server daemon.
[  OK  ] Started Permit User Sessions.
         Starting Terminate Plymouth Boot Screen...
[  OK  ] Started Job spooling tools.
[  OK  ] Started Command Scheduler.
         Starting Hold until boot process finishes up...
[  OK  ] Started Terminate Plymouth Boot Screen.
[  OK  ] Started Hold until boot process finishes up.
[   10.188420] IPv6: ADDRCONF(NETDEV_UP): eth0: link is not ready
[   10.201749] e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
[   10.207768] IPv6: ADDRCONF(NETDEV_UP): eth0: link is not ready
[   10.208903] IPv6: ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready

CentOS Stream 8
Kernel 4.18.0-326.el8.x86_64 on an x86_64

Activate the web console with: systemctl enable --now cockpit.socket

localhost login: root
Password:

Reset root password:

# passwd root
Changing password for user root.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

 

coffee

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.