Kickstart %pre script examples in RHEL CentOS 7 8

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 before actual installation starts using %pre. So in this article we will learn about Kickstart's %pre script examples in RHEL/CentOS 7/8

 

What is Kickstart %pre script?

  • The %pre scripts are run on the system immediately after the Kickstart file has been loaded, but before it is completely parsed and installation begins
  • All the Kickstart %pre install script must start with %pre and end with %end line
  • Commands related to networking, storage, and file systems are available to use in the %pre script, in addition to most of the utilities in the installation environment /sbin and /bin directories.
  • The %pre script does not run in the chroot environment.

 

What is Kickstart %pre-install script?

  • The commands in the %pre-install script are run after the following tasks are complete:
    • System is partitioned
    • File systems are created and mounted under /mnt/sysimage
    • Network has been configured according to any boot options and kickstart commands
  • Each of the %pre-install sections must start with %pre-install and end with %end.
  • The %pre-install scripts can be used to modify the installation, and to add users and groups with guaranteed IDs before package installation.
  • The %pre-install script does not run in chroot environment.

 

Kickstart %pre script examples

Below are some kickstart %pre script examples

Example 1: Store logs from kickstart %pre script

We can use --log and define the path of a log file (if not found, a new file will be created) to store the logs of %pre script execution. In this kickstart %pre script example I will checking systems stats and storing them in /tmp/kickstart_pre.log

%pre --log=/tmp/kickstart_pre.log

echo "Currently mounted partitions"
df -Th

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

%end

By default any log file you create at kickstart %pre section will not be available after the installation completes. So we must use post scripts to copy these log files once the installation completes.

Assuming that you have copied the log file, verify kickstart_pre.log on the client node, after the kickstart installation completes.

# cat /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   17M  2.0G   1% /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  939M  69% /
tmpfs                tmpfs     2.0G  220K  2.0G   1% /tmp
==============================
Available memory
              total        used        free      shared  buff/cache   available
Mem:           3940         437        2736          69         766        3206
Swap:             0           0           0
==============================
Kickstart pre install script completed at: Sun Apr 26 10:57:19 UTC 2020
==============================

 

Example 2: Execute scripts at Kickstart %pre section using --interpreter

--interpreter= allows you to specify a different scripting language, such as Python. 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 %pre script example I have written some dummy python and bash script and storing the output in separate log files

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

%pre --interpreter=/usr/bin/bash --log=/tmp/kickstart_bash_pre.log
echo "This is a sample bash script called at %pre stage"
%end

Assuming that you have copied the log file, verify kickstart_bash_pre.log and kickstart_python_pre.log on the client node, after the kickstart installation completes.

# cat /var/log/kickstart_python_pre.log
This is a sample python script called at %pre stage

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

 

Example 3: Create pre-install script

In this kickstart %pre-install script example we will get our system environment details

%pre-install --log=/tmp/kickstart_pre_install.log

echo "Currently mounted partitions"
df -Th

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

%end

Assuming that you have copied the log file, verify kickstart_pre_install.log on the client node, after the kickstart installation completes. You can check the difference of partition layout between %pre and %pre-install section.

# cat /var/log/kickstart_pre_install.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   21M  2.0G   2% /tmp
/dev/sda3            ext4       17G   45M   16G   1% /mnt/sysimage
/dev/sda1            ext4      976M  2.6M  907M   1% /mnt/sysimage/boot
tmpfs                tmpfs     2.0G     0  2.0G   0% /mnt/sysimage/dev/shm
==============================
Available memory
              total        used        free      shared  buff/cache   available
Mem:           3940         487        2572         100         879        3121
Swap:          2047           0        2047
==============================
Kickstart pre install script completed at: Sun Apr 26 11:41:45 UTC 2020
==============================

 

Lastly I hope the steps from the article to write kickstart %pre script with examples on 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

Leave a Comment

Please use shortcodes <cmd>your code</cmd> for syntax highlighting when adding code.