Claudio Borges

Technical blog about Linux, BSD, Mac OS X, Games and etc.

Archive for the ‘Kickstart’ tag

Dynamic disk partitioning with RedHat Kickstart

without comments

Hi folks, this time I’ll show how to create a dynamic disk partitioning scheme using RedHat Kickstart.

TL;DR: I’ve added the entire Kickstart file on my GitHub account. feel free to download it if you don’t want to read the entire article.

Overview

Many system administrators would prefer to use an automated installation method to install RedHat/CentOS Linux on their machines. RedHat created the Kickstart method to do that. Using it, you can create a single file containing the answers to all the questions that would normally be asked during a typical installation.

Different than Debian Preseed, a Kickstart file is precise and easy to understand. But both of them are text files containing a list of items, each one identified by a keyword.

Your first Kickstart file

When you finished a RedHat/CentOS installation, if you check the root directory, you will see the file /root/anaconda-ks.cfg. It was created based on the options that you selected during installation. Let’s say that it is your first Kickstart File and you can use it to create a new installation (just by changing a few options).

You have two ways to modify or create a Kickstart file: the first one is to use your preferred text editor (vi/vim) or use a tool like Kickstart Configurator (system-config-kickstart) for example.

While the general principles of Kickstart installations tend to stay the same, the commands and options can change between major releases of RedHat/CentOS. If you are creating a Kickstart from scratch or if you are modifying an old one to use it in a new installation, it is highly recommended to verify its syntax, you can use the Pykickstart (available in the RedHat/CentOS repository) to do that.

Pre-install section

The %pre section defines actions or commands that will be perform before installation (useful to automate things). You can use it to download a file or you can generate a new one. For example, let’s use it to create the file “/tmp/partitioning.txt” with the partitioning scheme that will be used in the installation:

cat >> /tmp/partitioning.txt <<EOF
clearpart --all --initlabel
bootloader --location=mbr --driveorder=sda

part /boot --fstype="xfs"   --size=512 --ondisk=sda
part pv.00 --fstype="lvmpv" --ondisk=sda

volgroup VolGroup00 pv.00

logvol swap --fstype="swap" --size=4096 --name=lv_swap --vgname=VolGroup00
logvol /    --fstype="xfs"  --size=3072 --name=lv_root --vgname=VolGroup00
logvol /tmp --fstype="xfs"  --size=512  --name=lv_tmp  --vgname=VolGroup00
logvol /var --fstype="xfs"  --size=1024 --name=lv_var  --vgname=VolGroup00
EOF
%end

PS: Although you can access the network in the %pre section, the name service has not been configured at this point, in other words, just IP addresses will work.

The –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 /bin/sh, /bin/bash and /usr/bin/python. Append the %pre line at the beginning of the script to be able to use it. The following example changes the pre-installation script behavior.

%pre --interpreter=/usr/bin/python
--- Python script omitted --
%end

Include section

The %include section is used to include the contents of another file in the kickstart file as though the contents were part of the Kickstart file. Generally this option it used to specify a generated or downloaded file by a pre-install script.

To include a file in the Kickstart file, just use the %include section as follows:

%include /path/to/file

Partitioning rules

Like I said, the %pre section will run immediately before the partitioner starts. It will count the number of disks, define the RAID level and the partitioning scheme. It will use the following rule to do that:

1 disk = LVM Physical device
2 disks = RAID 1 + LVM Physical device
3 disks = RAID 5 + LVM Physical device
4 disks or more = RAID 10 + LVM Physical device

Why Sofware RAID?

Perhaps are you thinking why someone would use software instead of using Hardware RAID? The answer is simple: Everything depends on your point of view. Hardware RAID (integrated into the motherboard or a separate controller) is similar in performance to the software RAID. But, it has certain features not available in software RAID and that are never implemented in low-cost controllers, such as caching, hot-swapping, and battery backup. On the other hand, software RAID is implemented by the operating system, it is cheap, easy and a fairly versatile option. However, it has certain limitations, for example, it has no hotswap. In my case, I’m using Software RAID (sometimes) because, I have a few servers with low-cost controllers.

Both of them (Hardware and Software RAID) aren’t perfect. You can still lose the array to a controller failure or operator error.

Swap size and Partitioning scheme

The SWAP size will be calculated based on the amount of RAM and I’ve used the following rule:

if RAM < 2GB then SWAP = 2x physical RAM
if RAM > 2GB or MEM < 8GB then SWAP = Equal to the amount of RAM
if RAM > 8GB then SWAP = At least 4 GB

And the partitioning scheme will be as follows:

/dev/mapper/VolGroup00/lv_root - 3Gb   - /    xfs
/dev/mapper/VolGroup00/lv_tmp  - 512Mb - /tmp xfs
/dev/mapper/VolGroup00/lv_var  - 1Gb   - /var xfs

Kickstart file

The following Kickstart file is used in my production environment. Feel free to use it or change it if you want:

%pre
#!/bin/bash
# Use RAID+LVM or just LVM to partition the disk

# Partitioning scheme:
#
# /dev/md0 - 512Mb - /boot xfs
# /dev/md1 - raid device + LVM VolGroup00 (if you have 2 or more disks)
# /dev/mapper/VolGroup00/lv_swap - the swap is calculated over the amount
# of RAM in the system, ex:
# if RAM < 2GB then SWAP = 2x physical RAM
# if RAM > 2GB or MEM < 8GB then SWAP = Equal to the amount of RAM
# if RAM > 8GB then SWAP = At least 4 GB
# /dev/mapper/VolGroup00/lv_root - 3Gb   - /    xfs
# /dev/mapper/VolGroup00/lv_tmp  - 512Mb - /tmp xfs
# /dev/mapper/VolGroup00/lv_var  - 1Gb   - /var xfs

# Get the disks
COUNT=0
for DISK in $(awk '{if ($NF ~ "^(s|h)d|cciss" && $NF !~ "((s|h)d|c.d.)[a-z][0-9]$") print $4}' /proc/partitions); do
    DEVS[${COUNT}]="${DISK}"
    DISKS[${COUNT}]="${DISK//\/dev\/}"
    let COUNT++
done

# Define the RAID level
if [ ${COUNT} -eq "1" ]; then
    LEVEL=-1
elif [ ${COUNT} -eq "2" ]; then
    LEVEL=1
elif [ ${COUNT} -eq "3" ]; then
    LEVEL=5
elif [ ${COUNT} -ge "4" ]; then
    LEVEL=10
fi

# Calculate the SWAP size over the amount of RAM
MEM=$(($(sed -n 's/^MemTotal: \+\([0-9]*\) kB/\1/p' /proc/meminfo) / 1024))
if [ "${MEM}" -lt "2048" ]; then
    SWAP=$((MEM * 2))
elif [ "${MEM}" -gt "2048" ] || [ "${MEM}" -le "8192" ]; then
    SWAP=${MEM}
elif [ "${MEM}" -ge "8192" ]; then
    SWAP=4096
fi

# 
# Create the RAID + LVM (if the system has two disks or more)
if [ ${LEVEL} -ge "1" ]; then
    x=${#DEVS[@]}
    DEVS=${DEVS[@]:0}
    DISKS=${DISKS[@]:0}
    echo "ignoredisk --only-use=${DISKS// /,}"                 > /tmp/partitioning.txt
    echo "clearpart --all --initlabel --drives=${DISKS// /,}" >> /tmp/partitioning.txt
    for ((i=0; i < ${#DEVS[@]}; i++)); do
        echo "part raid.0${i} --fstype=\"mdmember\" --size=512 --ondisk=${DISK[$i]}" >> /tmp/partitioning.txt
        echo "part raid.0${x} --fstype=\"mdmember\" --grow     --ondisk=${DISK[$i]}" >> /tmp/partitioning.txt
        RAIDPARTS1[$i]="raid.0${i}"
        RAIDPARTS2[$x]="raid.0${x}"
        let x++
    done
    echo "raid /boot --device=0 --fstype=\"xfs\"   --level=raid${LEVEL} ${RAIDPARTS1[@]:0}" >> /tmp/partitioning.txt
    echo "raid pv.00 --device=1 --fstype=\"lvmpv\" --level=raid${LEVEL} ${RAIDPARTS2[@]:0}" >> /tmp/partitioning.txt
# Otherwise, it will use just LVM
else
    echo "part /boot --fstype=\"xfs\"   --size=512"            >> /tmp/partitioning.txt
    echo "part pv.00 --fstype=\"lvmpv\" --ondisk=${DISK[@]:0}" >> /tmp/partitioning.txt
fi

# Define the volume group and logical volumes
cat >> /tmp/partitioning.txt <<EOF
volgroup VolGroup00 pv.00
logvol swap --fstype="swap" --size=${SWAP} --name=lv_swap --vgname=VolGroup00
logvol /    --fstype="xfs"  --size=3072 --name=lv_root --vgname=VolGroup00
logvol /tmp --fstype="xfs"  --size=512 --name=lv_tmp --vgname=VolGroup00
logvol /var --fstype="xfs"  --size=1024 --name=lv_var --vgname=VolGroup00
EOF
%end

Conclusion

As you can see, Kickstart files are pretty easy and powerful. I tried to be generic and cover all kinds of disk devices in my script. For example, in the past, I had around 10 Kickstart files to handle different hardware brands and now I have just one.

So, that is all for now folks. If you have any questions about this article, feel free to ask me.

Written by but3k4

October 6th, 2015 at 8:46 am