Introduction
So far we have been able to deploy a VM using KVM but we need to reply to all the questions the installer will ask us.As our final objective is to automate the VM deployment, a possible solution is to proceed with a manual installation, selecting the option we would like to replicate in our cluster deployment, and copy the automatically generated kickstart file.
In this post we will show you the steps to automate the tasks and in particular how to manipulate the kickstart file to allow the customization of the partitions.
Let's get started !
Kickstart file
We need to create a kickstart configuration file. The Red Hat documentation provide a good description about kickstart: link.First of all you should remember the followings:
1) There are 4 important sections:
- command sections: this section must be the first and it contains something like the "expected question/answer" to all the installation questions.
- %packages sections: this section should be the second and it includes the list of packages to be installed.
- %pre %post script sections: these sections contain script to be executed before and after the installation. These sections can be omitted (not required).
2) If you omit some item, the installation may interrupt in a "wait for customer input" mode ... it may be tricky initially to find the right setup but it will be rewarding at the end!
3) Like for Bash scripts, lines which start with a # (pound sign) are treated like comments.
We can start by manipulating the kickstart file that anaconda created when we installed the host OS (se Part1). The file should be located under the root home directory: /root/anaconda-ks.cfg
This is my anaconda-ks.cfg file (depurated of the password hashes !!):
# cat /root/anaconda-ks.cfg
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Use text mode install
text
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=vda
# Keyboard layouts
keyboard --vckeymap=us --xlayouts=''
# System language
lang en_US.UTF-8
# Network information
network --bootproto=dhcp --device=eth0 --onboot=off --ipv6=auto --no-activate
network --hostname=localhost.localdomain
# Root password
rootpw --iscrypted HASHED-PASSWORD
# System services
services --enabled="chronyd"
# Do not configure the X Window System
skipx
# System timezone
timezone Europe/London --isUtc
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=vda
autopart --type=lvm
# Partition clearing information
clearpart --all --initlabel --drives=vda
%packages
@core
chrony
kexec-tools
%end
%addon com_redhat_kdump --enable --reserve-mb='auto'
%end
%anaconda
pwpolicy root --minlen=6 --minquality=50 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=50 --notstrict --nochanges --notempty
pwpolicy luks --minlen=6 --minquality=50 --notstrict --nochanges --notempty
%end
As you can see the file is pretty easy and self explanatory. When it comes to using the kickstart file to create/install a VM guest, you need to have particular care for the network and the disks/partitions configuration. Then you have the package selection: the difficult part is to satisfy the dependencies.
Let's modify the partition entries ... we want to create our "standard" set of partitions. The boot partition cannot be created on an LVM partition. We will create a swap and a root partition. I like to suggest to create other partitions as well, such as the home and the var partitions as they may grow significantly and you may eventually need to increase the size or move out to a network file system, but this is not the scope of this article.
So this is my partition command list:
# Partition clearing information
clearpart --all --initlabel --drives=vda
####################################
part /boot --fstype ext4 --size=500
part swap --size=1024
part pv.01 --size=1000 --grow --ondisk=sda
volgroup vg00 pv.01
logvol / --vgname=vg00 --fstype=xfs --size=10240 --name=lv_root
We start by clearing any existing partitions (clearpart --all --initlabel --drives=vda) on the virtual drive we will use (vda) which we have created with this command (see Part 1 for details):
# qemu-img create -f qcow2 /opt/VM/CentOS-server1/guest.qcow2 32768M
Formatting '/opt/VM/CentOS-server1/guest.qcow2', fmt=qcow2 size=34359738368 encryption=off cluster_size=65536 lazy_refcounts=off
Then we provide a list of the partition, indicating the size and type of the filesystem. I didn't use all the space available on the virtual drive (32GB) as I would like to be able to dinamically increase the space of the existing partitions later or create new partitions.
About the %post section, the following is an example of a command you may find useful to modify an entry in a script file.
%post
sed -i 's/ONBOOT=no/ONBOOT=yes/g' /etc/sysconfig/network-scripts/ifcfg-eth0
%end
At the end of the installation you should reboot the guest VM.
This is the complete kickstart file I have been using:
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Use text mode install
text
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=vda
# Keyboard layouts
keyboard --vckeymap=us --xlayouts=''
# System language
lang en_US.UTF-8
# Network information
network --bootproto=dhcp --device=eth0 --onboot=off --ipv6=auto --no-activate
network --hostname=localhost.localdomain
# Root password
rootpw --iscrypted HASHED-PASSWORD
# System services
services --enabled="chronyd"
# Do not configure the X Window System
skipx
# System timezone
timezone Europe/London --isUtc
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=vda
# Partition clearing information
clearpart --all --initlabel --drives=vda
####################################
part /boot --fstype ext4 --size=500
part swap --size=1024
part pv.01 --size=1000 --grow --ondisk=sda
volgroup vg00 pv.01
logvol / --vgname=vg00 --fstype=xfs --size=10240 --name=lv_root
%post
sed -i 's/ONBOOT=no/ONBOOT=yes/g' /etc/sysconfig/network-scripts/ifcfg-eth0
%end
%packages
@core
chrony
kexec-tools
%end
%addon com_redhat_kdump --enable --reserve-mb='auto'
%end
%anaconda
pwpolicy root --minlen=6 --minquality=50 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=50 --notstrict --nochanges --notempty
pwpolicy luks --minlen=6 --minquality=50 --notstrict --nochanges --notempty
%end
Verify the Kickstart file
You can make sure your Kickstart file is valid by using “ksvalidator”.Install ksvalidator:
# yum install pykickstart
Run ksvalidator on your Kickstart file:
# ksvalidator /path/to/anaconda-ks.cfg
Deploy the VM
As explained along part one, to create and deploy a VM we will use the command virt-install. The additional option for the automation with kickstart file are:--initrd-inject=/opt/VM/ks.cfg
--extra-args "ks=file:/ks.cfg"
We use the initrd-inject, to tell anaconda to use the kickstart file in the ramdisk. The extra args help anaconda later to "reply" the questions automatically.
This is the command I have tested so far:
# virt-install --name CentOS-LDAP-server --ram 1024 --disk path=./ldap-guest.qcow2,size=32 --vcpus 1 --os-type linux --os-variant centos7.0 --network bridge=virbr0 --nographics --location /opt/VM/CentOS-7-x86_64-DVD-1611.iso --extra-args "console=ttyS0" --initrd-inject=/opt/VM/ks.cfg --extra-args "ks=file:/ks.cfg"
We got our VM created and deployed automatically without any user interation. In the next posts I will explore a basic setup of Ansible in a docker and following, how to deploy VM using Ansible.