Table of Content

download centos7 iso

move iso to /var/lib/libvirt/boot ## prepare centos 7 kickstart file

insert below line to standard kickstart file, to setup console mode for kickstart,

%post --logfile=/root/grubby.log
/sbin/grubby --update-kernel=ALL --args="console=ttyS0"
%end

create vm from iso + ks

virt-install --name testvm-ks \ 
--nographics \ 
--location /var/lib/libvirt/boot/CentOS-7-x86_64-Minimal-1708.iso \ 
--initrd-inject /root/centos7-ks.cfg \ 
--extra-args ks=file:/centos7-ks console=ttyS0 \ --memory=1024 --vcpus=1 \ 
--disk size=20 

verify vm running

virsh list --all 
Id    Name                           State
40    testvm-ks                      running

vm instance location

virsh dumpxml testvm-ks |grep qcow2  

verify inside vm

 
virsh console testvm-ks 
[root@localhost ~]# ip a
2: eth0:  mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 52:54:00:b7:32:e5 brd ff:ff:ff:ff:ff:ff inet 192.168.122.223/24 brd 192.168.122.255 scope global dynamic eth0 valid_lft 2570sec preferred_lft 2570sec inet6 fe80::5054:ff:feb7:32e5/64 scope link valid_lft forever preferred_lft forever 

[root@localhost ~]# ping google.ca 
PING google.ca (172.217.6.131) 56(84) bytes of data. 64 bytes 
from dfw25s16-in-f131.1e100.net (172.217.6.131): icmp_seq=1 ttl=127 time=43.7 ms 64 bytes 
from dfw25s16-in-f131.1e100.net (172.217.6.131): icmp_seq=2 ttl=127 time=45.0 ms 

doing same using script

If you get boring to repeat so many manually steps to build kvm vm from iso, you can try below tiny script.

 
cat kvm_vm_ks.sh 
#!/usr/bin/bash 
# kvm_vm_ks.sh 
# create kvm vm from iso plus kickstart 
# Robert Wang @github.com/robertluwang 
# Dec 22, 2018 
dckvm=<code>dirname &quot;$0&quot;</code>
source $dckvm/kvmrc 
virt-install --name $VM \ 
--nographics \ 
--location /var/lib/libvirt/boot/CentOS-7-x86_64-Minimal-1708.iso \ 
--initrd-inject $dckvm/$KS \ 
--extra-args ks=file:/$KS console=ttyS0 \ 
--memory=$RAM --vcpus=$VCPU \ 
--disk size=$DISKSIZE 

conclusion

We did demo how to build up a kvm vm from iso plus kickstart in cli and bash script.