Kickstart post install script examples in RHEL CentOS 7 8


Kickstart

We use Kickstart to perform automated installation with Red Hat/CentOS and Fedora (or any other OS using Red Hat's environment). You have the option of adding commands to run on the system once the installation is complete. So in this article we will learn about Kickstart's post install script examples in RHEL/CentOS 7/8

 

What is Kickstart post install script?

  • The %post script is a post-installation script that is run after the installation is complete, but before the system is rebooted for the first time.
  • You can add multiple %post install scripts in a Kickstart
  • All the Kickstart post install script must start with %post and end with %end line
  • All Kickstart post install scripts by default execute in chroot environment

 

Can we execute systemctl command in Kickstart %post section?

  • Since %post section by default uses chroot environment, most systemctl commands will refuse to perform any action
  • The reason for this is that the systemd process and the user that used the chroot command do not have the same view of the file system
  • The exception to this are unit file commands such as the systemctl enable and systemctl disable commands.
  • These commands do not need a running system and do not affect running processes, but they do affect unit files.
  • Although you can execute systemctl commands in %post install scripts using --nochroot.
  • But instead of using systemctl you can also use kickstart command - services

 

Why do we use Kickstart post install scripts?

  • Here we have the option of adding commands to run on the system once the installation is complete, but before the system is rebooted for the first time.
  • The %post section is useful for functions such as installing additional software or configuring an additional name server.
  • You can also execute your custom script in %post section to perform post installation tasks such as checking system health performance etc
  • You can add your own interpreter such as bash, perl, python etc based on the script you plan to run in %post section

 

Kickstart post install script examples

Let us go though some kickstart post install script examples, you can have multiple post sections in a single kickstart file.

Example 1: Save kickstart %post logs

In this kickstart post install script example I will run some basic commands and store the logs of these commands in a separate log file

%post --log=/var/log/kickstart_post.log

echo "Currently mounted partitions"
df -Th

echo "=============================="
echo "Available memory"
free -m
echo "=============================="
echo "Kickstart post install script completed at: `date`"
echo "=============================="

%end

Verify /var/log/kickstart_post.log on the client node, after the kickstart installation completes.

# cat /var/log/kickstart_post.log
Currently mounted partitions
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/sda3      ext4       17G  2.1G   14G  14% /
/dev/sda1      ext4      976M  121M  789M  14% /boot
devtmpfs       devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs          tmpfs     2.0G     0  2.0G   0% /dev/shm
tmpfs          tmpfs     2.0G   25M  2.0G   2% /run
==============================
Available memory
              total        used        free      shared  buff/cache   available
Mem:           3940         487         449         104        3003        3058
Swap:          2047           8        2039
==============================
Kickstart post install script completed at: Sun Apr 26 16:32:55 IST 2020
==============================

 

Example 2: Execute kickstart post install script with nochroot

--nochroot allows you to specify commands that you would like to run outside of the chroot environment. If you wish to copy logs from your RHEL ISO or the log files created at %pre stage of kickstart, then you must use --nochroot to access these logs

In this kickstart post install script example we will copy a log file we had created at %pre stage and will also check the existing system environment with --nochroot

%post --nochroot --log=/mnt/sysimage/var/log/kickstart_post_nochroot.log

echo "Copying %pre stage log files"
/usr/bin/cp -rv /tmp/kickstart_pre.log /mnt/sysimage/var/log/

echo "=============================="

echo "Currently mounted partitions"
df -Th

%end

Verify /var/log/kickstart_post_nochroot.log on the client node, after the kickstart installation completes.

# cat /var/log/kickstart_post_nochroot.log
Copying %pre stage log files
'/tmp/kickstart_pre.log' -> '/mnt/sysimage/var/log/kickstart_pre.log'
==============================
Currently mounted partitions
Filesystem           Type      Size  Used Avail Use% Mounted on
devtmpfs             devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                tmpfs     2.0G  4.0K  2.0G   1% /dev/shm
tmpfs                tmpfs     2.0G   25M  2.0G   2% /run
tmpfs                tmpfs     2.0G     0  2.0G   0% /sys/fs/cgroup
10.10.10.12:/images/ nfs        22G   15G  6.0G  71% /run/install/repo
/dev/mapper/live-rw  ext4      2.9G  2.0G  936M  69% /
tmpfs                tmpfs     2.0G   19M  2.0G   1% /tmp
/dev/sda3            ext4       17G  2.1G   14G  14% /mnt/sysimage
/dev/sda1            ext4      976M  121M  789M  14% /mnt/sysimage/boot
tmpfs                tmpfs     2.0G     0  2.0G   0% /mnt/sysimage/dev/shm

 

Example 3: Use --interpreter with %post scripts

We can execute our own custom script at %post stage. Any scripting language available on the system can be used; in most cases, these are /usr/bin/sh, /usr/bin/bash, and /usr/libexec/platform-python

In this Kickstart post install script example I am executing python and bash script and storing the logs in a separate log file

%post --interpreter=/usr/libexec/platform-python --log=/var/log/kickstart_python_post.log
print("This is a sample python script called at %post stage")
%end

%post --interpreter=/usr/bin/bash --log=/var/log/kickstart_bash_post.log
echo "This is a sample bash script called at %post stage"
%end

Verify /var/log/kickstart_bash_post.log and /var/log/kickstart_python_post.log files on the client node, after the kickstart installation completes.

# cat /var/log/kickstart_bash_post.log
This is a sample bash script called at %post stage

# cat kickstart_python_post.log
This is a sample python script called at %post stage

 

Lastly I hope the steps from the article to write Kickstart post install script examples in RHEL/CentOS 7/8 Linux was helpful. So, let me know your suggestions and feedback using the comment section.

 

References:
Perform Advanced Installation in RHEL 8 using Kickstart

Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment