I’m a fan of KVM (for various reasons, one of which being the fact that it’s there in the kernel for free) and like to automate stuff as much as I can. Every so often I find myself needing to provision VMs to test or to deploy all sorts of setups and when that happens I can either go for the traditional approach and use virt-manager to install the os from an iso OR (my personal favorite) kickstart the installation using virt-install (in case you haven’t figured it out, we’re talking about unattended installation, which usually saves time).
Problem with kickstarting an os install is that kickstart is supported by a few distros (usually rpm and deb based) so this article does not work for the Gentoo or Slackware fans but does apply to sys admins that are supposed to work with rpm and deb based distros (for the fans, I’ll prep something special in a different article)
For this particular case, to be able to kickstart CentOS using virt-install, one needs to have virt-install package installed on the KVM host, a CentOS iso (or in my case, a cobbler install with the CentOS iso imported and a profile for it) and a kickstart file.
CentOS7
here’s how my kickstart file looks like:
text
cdrom
auth --enableshadow --passalgo=sha512
keyboard --vckeymap=uk --xlayouts='uk'
lang en_US.UTF-8
eula --agreed
reboot
network --bootproto=static --ip=192.168.123.11 --gateway=192.168.123.1 --netmask=255.255.255.0 --noipv6 --device=eth0 --nameserver=8.8.8.8,8.8.4.4 --activate
network --hostname=centos7test
timezone Europe/London --isUtc
ignoredisk --only-use=vda
bootloader --location=mbr --boot-drive=vda
zerombr
clearpart --all --initlabel
part swap --asprimary --fstype="swap" --size=4096
part /boot --fstype xfs --size=1024
part / --fstype=xfs --grow --size=1
rootpw password
selinux --enforcing
%packages --nobase --ignoremissing
@core
@base
vim
bash-completion
%end
in my case, file is /tmp/centos7test.ks but can be anywhere
To kickstart the OS install simply run this command:
virt-install -n centos7test -r 2048 --vcpus=1 --os-variant=rhel7.7 -v -w bridge:virbr0 --disk /virt/centos7test.qcow2,size=20 --location http://192.168.123.222/cblr/ks_mirror/CentOS7-x86_64/ --accelerate --nographics --initrd-inject /tmp/centos7test.ks --extra-args="ks=file:/centos7test.ks console=tty0 console=ttyS0,115200n8"
the location parameter in the command above can be changed to point to an iso file on your local machine if you don’t want to use cobbler or some third party url
CentOS8
here’s how my kickstart file looks like:
text
cdrom
auth --enableshadow --passalgo=sha512
keyboard --vckeymap=uk --xlayouts='uk'
lang en_US.UTF-8
eula --agreed
reboot
network --bootproto=static --ip=192.168.123.10 --gateway=192.168.123.1 --netmask=255.255.255.0 --noipv6 --device=eth0 --nameserver=8.8.8.8,8.8.4.4 --activate
network --hostname=centos8test
timezone Europe/London --isUtc
ignoredisk --only-use=vda
bootloader --location=mbr --boot-drive=vda
zerombr
clearpart --all --initlabel
part swap --asprimary --fstype="swap" --size=4096
part /boot --fstype xfs --size=1024
part / --fstype=xfs --grow --size=1
rootpw password
selinux --enforcing
%packages --ignoremissing
@core
@base
vim
bash-completion
%end
in my case, file is /tmp/centos8test.ks but can be anywhere
To kickstart the OS install simply run this command:
virt-install -n centos8test -r 2048 --vcpus=1 --os-variant=rhel8.0 -v -w bridge:virbr0 --disk /virt-002/centos8test.qcow2,size=20 --location http://192.168.123.222/cobbler/ks_mirror/CentOS8-x86_64/ --accelerate --nographics --initrd-inject /tmp/centos8test.ks --extra-args="ks=file:/centos8test.ks SERVERNAME=centos8test net.ifnames=0 biosdevname=0 console=tty0 console=ttyS0,115200n8"
same as before, the location parameter in the command above can be changed to point to an iso file on your local machine if you don’t want to use cobbler or some third party url
also notice how the –extra-args param has a slightly different value – that’s because when I tried to kickstart CentOS8 it was messing with the device names and had to stop it from doing that
The sample kickstart files above are not for production use so don’t just copy/paste them and expect the os install to magically work … adjust them first with respect to your environment and needs.
Older distros like CentOS6 can be kickstarted as well in a similar way but before thinking about doing something like that one should consider moving forward in time not backwads.
There are other ways to provision VMs (some prefer to use cloud images which in essence are pre-installed os images) but if you want to be 100% sure that you are running the official image then you will either go with the way I described above to provision a VM or create the cloud image(s) you need yourself (I’ll cover this aspect at a later time)