Centos 6 Virtual Machine custom partition layout in text mode install

I have been trying to create a simple bash script for copying/configuring KVM guests using templates.
This way I don’t need to do the whole installation over and over again.
The script would use an already KVM image file copy it to a new image file, mount it change the hostname and network settings. It would also copy the xml file remove the uuid, change its name and define it with virsh command.

I did not have any issue with CentOS 5.x image files nor with Ubuntu ones since I was able to install the initial templates using a partition layout which the main partition was either ext3 or ext4. When I tried to use my script on a CentOS 6.x template I could not mount the main partition which was LVM and so my script was failing. I noticed that when installing CentOS 6.x in text mode you can not use a custom partition layout and you end up with the default one. This is only available on graphical installation. To install my KVM guests I was using the following command for CentOS 6.x

# virt-install -n centos6 -r 2048  \
--vcpus=1 --os-type linux --os-variant=rhel6 \ 
--network bridge=br0 --nographics \
--location='http://ftp.ntua.gr/pub/linux/centos/6/os/x86_64/' \
--extra-args='console=tty0 console=ttyS0,115200n8 serial' \
--disk /var/lib/libvirt/images/centos6.img,size=15 --hvm

After searching around I found the solution. I could use kickstart files to automate the installation of my centos templates and do custom partition layout.

We still use virt-install but we only have to change the network settings that will be used and then execute.
We can execute using a local kickstart file or a kickstart file that we are going to “load” from an http server. The way we execute virt-install depends whether we use a local kickstarrt file or one pulled from an http server.

So if you are using a local kickstart file you execute

 # virt-install -n centos6 -r 2048  \
--vcpus=1 --os-type linux --os-variant=rhel6 \ 
--network bridge=br0 --nographics 
--location='http://ftp.ntua.gr/pub/linux/centos/6/os/x86_64/' \
--initrd-inject=/root/centos6x_kvm.ks 
--extra-args='ks=file:/centos6x_kvm.ks text console=tty0 console=ttyS0,115200n8 serial' \
--disk path=/var/lib/libvirt/images/centos6.img,size=12 --hvm 

What you need here is to use –initrd-inject=/root/centos6x_kvm.ks and also ks=file:/centos6x_kvm.ks
On ks=file:/centos6x_kvm.ks you just define the file name not the absolute path this is defined in –initrd-inject
One more thing you need to have in mind is that you don’t need to define network settings on extra-args otherwise execution will fail.

If you are pulling the kickstart file from an http server then you need to execute

 # virt-install -n centos6 -r 2048  \
--vcpus=1 --os-type linux --os-variant=rhel6 \
--network bridge=br0 --nographics \ 
--location='http://ftp.ntua.gr/pub/linux/centos/6/os/x86_64/' 
--extra-args='ks=http://MYSERVER.MYORG/centos6x_kvm.ks ksdevice=eth0 ip=192.168.1.2 netmask=255.255.255.0 dns=8.8.8.8 gateway=192.168.1.254 text console=tty0 console=ttyS0,115200n8 serial' \
--disk path=/var/lib/libvirt/images/centos6.img,size=12 --hvm

In this method you need to define inside extra-args the following ks=//MYSERVER.MYORG/centos6x_kvm.ks ksdevice=eth0 ip=192.168.1.2 netmask=255.255.255.0 dns=8.8.8.8 gateway=192.168.1.254

The kickstart file should look like this

# text mode (no graphical mode)
text

# do not configure X
skipx

# install
install

# installation url
url --url=http://ftp.ntua.gr/pub/linux/centos/6/os/x86_64/

# Language support
lang en_US

# Keyboard
keyboard us

# Network
network --device eth0 --bootproto static --ip 38.130.222.31 --netmask 255.255.255.0 --gateway 38.130.222.1 --nameserver 8.8.8.8 --noipv6 --hostname centos7

# auth config
auth --useshadow --enablemd5

# root password
rootpw --iscrypted CHANGEME
# SElinux
selinux --disabled

# timezone
timezone  America/New_York
# clear the MBR (Master Boot Record)
zerombr

# the Setup Agent is not started the first time the system boots
firstboot --disable

# Reboot after installation
reboot

# Logging lever
logging --level=info

# Remove all partitions
clearpart --all --initlabel

# create partitions on the system
part / --asprimary --fstype="ext4" --grow --size=1
part swap --recommended

# Packages installation
%packages
@core
wget
net-tools
--nobase
%end

%post
/usr/bin/yum update -y
mkdir -p /root/.ssh
echo "MY_PUBLIC_SSH_KEY" > /root/.ssh/authorized_keys
%end

# bootloader
bootloader --location=mbr

In order to change the line rootpw –iscrypted CHANGEME you need to execute

# openssl passwd -1

You can find the script that I use to copy/configure KVM in my github
https://github.com/mrdimka/kvm-scripts

Links
https://github.com/fubralimited/CentOS-KVM-Image-Tools
http://blog.gluster.org/2013/05/kvm-clusters-on-the-fly-virt-install-kickstart-with-static-ips-2/
http://www.cyberciti.biz/faq/kvm-install-centos-redhat-using-kickstart-ks-cfg/
https://www.dark.ca/2009/08/03/complex-partitioning-in-kickstart/
http://mylinuxlife.com/creating-virtual-machines-with-a-local-kickstart-image-and-file/
http://fedoraproject.org/wiki/Anaconda/Kickstart

Advertisements

One thought on “Centos 6 Virtual Machine custom partition layout in text mode install

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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