Fri, March 30, 2018 ยท 1 min read

Creating VM on KVM hosts with kickstart file

Creating VM on KVM hosts with kickstart file

virt-install can be used to provision kickstart while installing, this provides an answer file to all the settings required to install OS. The kickstart file will have the information of partition layout, username, password, etc.

Sample kickstart file

#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 --ipv6=auto --no-activate
network --hostname=localhost.localdomain

# Root password
rootpw --iscrypted $6$pufROlQv24BRI6uK$...
# System timezone
timezone Asia/Kolkata --isUtc
user --groups=wheel --name=cipher --password=...
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=vda
autopart --type=thinp
# Partition clearing information
clearpart --all --initlabel --drives=vda

Creating a VM with kickstart

$ virt-install \
  --name linux2 \
  --ram 512 \
  --disk path=/dev/store/linux2 \
  --vcpus 2 \
  --os-type linux \
  --os-variant rhel7 \
  --network network=default \
  --graphics none \
  --console pty,target_type=serial \
  --location '/home/cipher/CentOS-7-x86_64-Minimal-1708.iso' \
  --initrd-inject '/var/www/html/pub/ks.cfg' \
  --extra-args 'console=ttyS0,115200n8 serial' \
  --extra-args 'ks=file:/ks.cfg' \
  --accelerate

The kickstart file is injected via --initrd-inject and referenced with ks=file:/ks.cfg in the kernel command line, allowing fully automated installation.