Use Kickstart to automate RHEL and CentOS installation on KVM

You can download this article in PDF format via the link below to support us.Download the guide in PDF formatClose

Hello everyone. This is the first tutorial I wrote about KVM virtualization. It is for reference only and can help you automate boring GUI-based installation problems. We are using Kickstart to deploy VMs on KVM. If you have good practical knowledge of Linux and virtualization, then I bet you are already working on KVM and Kickstart.

If this is your first time trying KVM, don’t worry, because you don’t need to have an in-depth knowledge of the internals of KVM virtualization to use this guide. You only need to know virtualization to achieve rapid deployment by isolating applications in a known controlled environment and adding an abstraction layer between the virtual machine and the underlying physical hardware. The easiest way to make many VMs run on KVM is to use templates and Kickstart on the KVM deployment model.

The main advantages of virtualization

  • Service isolation: application isolation and elimination of compatibility issues
  • Improved system security and reliability: abstraction between virtual machines and underlying physical hardware
  • Faster server configuration: use snapshots and templates.
  • Faster disaster recovery: Keep up-to-date snapshots of virtual machines – easy to redeploy
  • Dynamic load balancing: real-time migration of overloads to underutilized servers

What is Kickstart?

Red Hat creates tools for system administrators to help them automatically install the Red Hat Linux distribution series. You must create a file containing all the answers to all the questions that are usually asked during a typical installation. Kickstart files can be saved on a single server system and can be read by a single computer during the installation process to prepare for Kickstart on KVM.

How to execute Kickstart on KVM installation?

You can use a local CD-ROM, a local hard drive, or perform a Kickstart installation via NFS, FTP or HTTP.

To use kickstart, you must:

  • Create a kickstart file.
  • Use kickstart files to create boot media, or make kickstart files available on the network.
  • Make the installation tree available.
  • Start the kickstart installation.

In this article, I will provide you with a basic kickstart file for a quick start. You can modify and improve it to suit your use case and environment settings.used virt-install Commands are also provided. To save you a lot of time, I wrote a simple bash script that makes the process of spinning a new virtual machine easy and labor-saving.

kickstart file

This is a kickstart file to help you get started.With a goal, edit it and save it as ks.cfg In the root directory of your web server.

###############################################################
#
# 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=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  --bootproto=dhcp --device=eth0 --ipv6=auto --activate
network  --hostname=server1.example.com
timezone Africa/Nairobi --isUtc




###############################################################
#
# partitioning
#
###############################################################

ignoredisk --only-use=vda
bootloader --location=mbr --boot-drive=vda
zerombr
clearpart --none --initlabel
autopart --type=lvm

# 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 password 
# user --groups=wheel --name=josepy --password=password --gecos="Mutai Josphat"

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

 selinux --enforcing
 #selinux --permissive
 #selinux --disabled
 
# firewall --enabled --http --ssh --ftp --port=https:tcp --port=ipp:tcp
# firewall --disabled

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

%packages --nobase --ignoremissing
@core
@base
vim 
bash-completion

%end

Parameters that are rarely changed, maybe IP address with hostname.The following bash script is used to install a new virtual machine-will ask you a few questions

#!/usr/bin/env bash

# Update kickstart file
echo -en "Enter Hostname: "
read HOST_NAME
echo -en "Enter IP Address: "
read IP_ADDRESS
sudo sed -i 's/server1/'$HOST_NAME'/g' /srv/http/ks.cfg
sudo sed -i 's/192.168.122.100/'$IP_ADDRESS'/g' /srv/http/ks.cfg
 
## Pre-defined variables
echo ""
MEM_SIZE=1024
VCPUS=1
OS_VARIANT="rhel7"
ISO_FILE="$HOME/iso/CentOS-7-x86_64-Everything-1611.iso"

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 --network bridge=docker0 
     --graphics=none 
     --os-variant=${OS_VARIANT} 
     --console pty,target_type=serial 
     -x 'console=ttyS0,115200n8 serial' 
     -x "ks=http://192.168.122.1:8090/ks.cfg" 

replace $HOME/iso/CentOS-7-x86_64-Everything-1611.iso Plus the path of your ISO file, 192.168.122.1:8090 Hosted with your web server IP address ks.cfg file.

You can modify other parameters, such as VCPU,RAM and Bridge Suitable for your use.

For the virtual disk size, just enter a number, for example 10 for 10 GB

Save the script to the name create_vm.sh.Make it executable and run

$ chmod +x create_vm.sh
$ ./create_vm.sh

Also check:

How to configure VM on KVM using Terraform

Configure virt-manager as a non-root user on Linux

Further reading:

Master KVM virtualization

Virtualization basics, second edition

You can download this article in PDF format via the link below to support us.Download the guide in PDF formatClose

Sidebar