Kickstart CentOS 7 installation

Recently I was playing around with kickstart files. Kickstart files contains specification on how a CentOS (or RHEL) OS should be installed. For example, it may be used to specify the keyboard layout, the system language, the time zone, the disk partition, the network, etc. Basically it can set anything that you could set with the graphical installation interface. If a kickstart file is referenced during OS installation, then the OS can be installed automatically without any human intervention. This can be really helpful if you are deploying OS onto a lot of servers. Let’s walk through the steps required to utilize kickstart file for USB drive installation. Please note that you need a CentOS environment to do the following steps.

Generate a valid Kickstart configuration file

There are two easy ways to generate a kickstart file. You could install the OS manually once. And you will find the file containing the steps you specified in the /root folder. You could also use the GUI tool provided by CentOS. Let’s go with the second option. First, install the Kickstart configuration GUI in Application Installer. It can be started from the GNOME menu, or alternatively from the command line

$ system-config-kickstart

Modify the settings as you want, and save it to a file in the end. Notice that the Package Selection panel may be disabled. Therefore, you have to manually add that section in the kickstart file if you want to install any extra packages (Core and Base groups are selected by default)

%packages
@^Compute Node
@Development Tools
%end

In the above example, the Compute Node base environment is used and the Development Tools group is installed. Notice that base environment is specified with @^ and groups are specified with @. To make sure the file’s syntax is valid, use the ksvalidator tool which is pre-installed on CentOS

$ ksvalidator ks.cfg

If you are looking for an example kickstart file, I have one on GitHub.

Modify the CentOS 7 ISO

After creating a kickstart file, we need to create a custom bootable ISO containing that file.

First, create a working directory

$ mkdir -p ~/kickstart/isolinux/{images,ks,LiveOS,Packages}

Then we copy some content from an original ISO

# mount ISO
$ mkdir -p /mnt/iso
$ mount -o loop /path/to/your/ISO /mnt/iso

# copy some content
$ cp /mnt/iso/.discinfo ~/kickstart/isolinux/
$ cp /mnt/iso/isolinux/* ~/kickstart/isolinux/
$ rsync -av /mnt/iso/images/ ~/kickstart/isolinux/images/
$ cp /mnt/iso/LiveOS/* ~/kickstart/isolinux/LiveOS/

# find the name for comps.xml.gz
$ ll /mnt/iso/repodata/ | grep -i comps

# copy over the comps.xml file
$ cp /mnt/iso/repodata/<whatever-name>-comps.xml.gz ~/kickstart/
$ cd ~/kickstart
$ gunzip <whatever-name>-comps.xml.gz
$ mv <whatever-name>-comps.xml comps.xml

# copy over all .rpm packages
# alternatively, copy only the rpms you need
$ rsync -av /mnt/iso/Packages/ ~/kickstart/isolinux/Packages/

Next we need to create repodata for the packages copied over so that it could be used for package installation during OS installation

$ yum install -y createrepo
$ cd ~/kickstart/isolinux
$ createrepo -g ~/kickstart/comps.xml .

Copy over your kickstart file

$ cp /path/to/your/kickstart/file ~/kickstart/isolinux/ks/

We need to modify isolinux.cfg so that our kickstart file will be used by default, make sure the boot label linux looks like the following (notice the addition of inst.ks and menu default)

label linux
  menu label ^Install CentOS 7
  menu default
  kernel vmlinuz
  append initrd=initrd.img inst.ks=hd:LABEL=CentOS\x207\x20x86_64:/ks/ks.cfg inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 quiet

and remove the line menu default for label check.

You could also reduce the time before the default choice is chosen (50 means 5 seconds)

timeout 50

Finally, we generate a new ISO

$ yum install -y genisoimage syslinux
$ cd ~/kickstart
$ mkisofs -o centos-7-custom.iso -b isolinux.bin -c boot.cat -no-emul-boot -V 'CentOS 7 x86_64' -boot-load-size 4 -boot-info-table -R -J -v -T isolinux/
$ isohybrid centos-7-custom.iso # make it bootable from USB

The last line is very important as your custom ISO will only boot from CD-ROMs if you don’t do that. I will talk about the difference it makes in a future blog post.

Installing with the custom ISO

Let’s create a bootable USB drive from the ISO file we just generated. Suppose we want to write the image to /dev/sdb1

$ umount /dev/sdb1
$ mkfs.vfat /dev/sdb1
$ dd if=/path/to/your/custom/iso of=/dev/sdb1

Now that you have a bootable USB drive, insert it into a computer and boot from it. The installation process will begin automatically. After the installation has finished, the system will be rebooted. Remember to remove the bootable device (or select not to boot from it) after the system reboots so that you can boot into the newly installed CentOS 7. Otherwise, you will install CentOS again!

Conclusion

This tutorial walks you through installing CentOS automatically with a bootable thumb drive or CD alone. It is also possible to host the kickstart file remotely and tell the installation media to look for your remotely hosted kickstart file instead of getting the file from the installation media itself. This requires more complicated set up and I haven’t tried it personally. However, you can find some tutorials for that kind of set up with Google.

References

 
comments powered by Disqus