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.

kickstart file

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

###############################################################
#
# 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

Few parameters are to be changed, maybe the IP address and hostname. Below bash script is used to install a new vm – will ask you 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 with the path to your ISO file, 192.168.122.1:8090 with your web server ip address hosting ks.cfg 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