Automatically install KVM virtual machine using kickstart file

Posted: June 26, 2015 in Linux

In this article we’ll automatically install virtual machine (CENTOS 7),using “answer” file,in this process,root password will be set,additional user (user1) will be created,and web,mail and FTP packages will be installed

But,first,we need to create storage pool and storage volume for our VM

A storage pool is a  storage for virtual machines.Storage pools are divided into storage volumes

Storage volumes as virtual machine’s hard disk

In terminal,type virsh to enter in virsh prompt

[root@localhost centos]# virsh
Welcome to virsh, the virtualization interactive terminal.

Type:  'help' for help with commands
       'quit' to quit

Let’s create storage pool

virsh # pool-define-as skladishte dir - - - - "/disk/skladishte/"
Pool skladishte defined

Pool named skladishte is created in folder /disk

We now need to build pool:

virsh # pool-build skladishte
Pool skladishte built

List pools:

virsh # pool-list --all
Name State Autostart
default active yes
Desktop active yes
dirpool active yes
disk active yes
Downloads inactive yes
Downloads-1 active yes
KVM active yes
skladishte inactive no
USB inactive yes

start pool

virsh # pool-start skladishte
Pool skladishte started

Set pool to autostart

virsh # pool-autostart skladishte
Pool skladishte marked as autostarted

Now,create a volume for pool

vol-create-as skladishte volume1 12G

For pool skladishte,volume volume1 with 12GB was created

virsh # pool-info skladishte
Name: skladishte
UUID: 04483aca-512e-4d3a-8910-3091632567ef
State: running
Persistent: yes
Autostart: yes
Capacity: 72.71 GiB
Allocation: 56.08 GiB
Available: 16.63 GiB

virsh # vol-list skladishte
Name Path
volume1 /disk/skladishte/volume1

Now,install FTP server,create folder where install files and kickstart file will be created and set permissions:

[root@localhost pub]#yum install vsftpd -y

[root@localhost pub]#mkdir /var/ftp/pub/centos

[root@localhost pub]#chmod 755 -R /var/ftp/pub/centos/

Mount previously downloaded iso and copy all files to ftp folder

mkdir /tmp/iso

[root@localhost pub]#mount /home/godon/Downloads/CentOS-7-x86_64-Minimal-1503-01.iso /tmp/iso/
[root@localhost pub]#mount: /dev/loop0 is write-protected, mounting read-only

[root@localhost pub]# cp -R * /tmp/iso/ /var/ftp/pub/centos/

As template for out kickstar file we will use anaconda-ks.cfg located in root directory:

[root@localhost pub]# cp /root/anaconda-ks.cfg /var/ftp/pub/centos/ks.cfg

Edit ks.cfg:

# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
# Use graphical install
eula --agreed
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8
# Root password
rootpw --iscrypted $6$HqS6tEQZKMzZXRqT$T1dmqcISnQQtVR.u/Sh56jKr75bS2JwZe20PddB/Ab1kT3391igLp8AA5VX7vlhbl9WJHtMKyMlrxeHB7ghjK1
# System timezone
timezone Europe/Belgrade --isUtc
# Run the Setup Agent on first boot
firstboot --disable
ignoredisk --only-use=vda
clearpart --all --initlabel --drives=vda

# System bootloader configuration
#Partition clearing information
part /boot --fstype="ext4" --ondisk=vda --size=500 --asprimary
part pv.16 --ondisk=vda --grow --size=9739 --asprimary
volgroup vg00 --pesize=4096 pv.16
logvol swap --fstype="swap" --size=500 --name=swap --vgname=vg00
logvol / --fstype="ext4" --size=8230 --name=root --vgname=vg00
logvol /home --fstype="ext4" --size=300 --name=home --vgname=vg00
bootloader --location=mbr --boot-drive=vda

user --groups=wheel --name=admin1 --password=$6$HqS6tEQZKMzZXRqT$T1dmqcISnQQtVR.u/Sh56jKr75bS2JwZe20PddB/Ab1kT3391igLp8AA5VX7vlhbl9WJHtMKyMlrxeHB7ghjK1 --iscrypted --gecos="admin1"

@FTP Server
@DNS Name Server
@Web Server

As you can see,we set root password (copied from /etc/shadow),time zone,partioned hard disk,

another admin account (admin1),and defined packages which will be installed

Validate ks.cfg file

[root@localhost pub]# ksvalidator /var/ftp/pub/centos/ks.cfg

Set SELinux context,for SELinux to allow access to ks.cfg file

[root@localhost pub]# tscon -t  public_content_t /var/ftp/pub/centos/ks.cfg

Start vsftpd service,and open firewall ports:

[root@localhost pub]# systemctl start vsftpd
[root@localhost pub]# firewall-cmd --add-service=ftp --permanent
[root@localhost pub]# firewall-cmd --reload

Test ftp access


Now we can install virtual machine:

[root@localhost disk]# virt-install --ram=2048 --vcpus=2 --autostart --os-type=linux --extra-args='ks= ksdevice=ens3 ip= netmask= gateway= dns=' --disk vol=skladishte/volume1,bus=virtio --location= --network bridge=virbr0

Starting install...
Retrieving file vmlinuz... | 9.6 MB 00:00 !!!
Retrieving file initrd.img... | 68 MB 00:00 !!!
Creating domain... | 0 B 00:00

VM named with 2 GB of RAM,2 CPU’s,IP will be installed.

As hard disk,this VM will used previously created volume1 from skladishte pool

I had to specify VM network settings here instead in ks.cfg file to get this working



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s