Note: this series of articles applies to CentOS 7; for CentOS 6, see this series.
Let’s make things really interesting with a postinstall script to do some custom configuration.
The %post section
In your kickstart configuration file, you can have sections of commands that are
designated to run after the anaconda installer has done its work. These sections are
denoted with the
Typically, you follow this directive with the bash shebang line and the contents of
a custom shell script. One thing to note is the filesystem organization at this point
during the installation. Your new system’s disk is mounted at
/ the way it will be once the system is up and running
By default, the commands in the
section are run in a chroot environment, where
/mnt/sysimage appears as the
/ directory. This lets you use “normal” paths to
configuration files like
/etc instead of
/mnt/sysimage/etc (if you didn’t chroot like
this, you wouldn’t be able to do things like install RPMs). The primary disadvantage is
that your install media is not visible in a chrooted environment.
We solve this problem by building our postinstall in two stages. In the first stage, we tell anaconda not to chroot us; we then copy files from the CD to the hard drive.
set -x -v
exec 1>/mnt/sysimage/root/kickstart-stage1.log 2>&1
echo "==> copying files from media to install drive..."
cp -r /run/install/repo/postinstall /mnt/sysimage/root
In our case, we’ve put all our postinstallation files into the
directory under the
~/kickstart_build/isolinux directory. Note that the
isolinux directory in our build environment
becomes the root of the install disc that we create, and the install disc is mounted
is available at
We copy postinstall directory on the install disc to
/root/postinstall on the new system’s hard drive.
We’re now ready to run stage 2 of the postinstall, where we actually use the postinstallation files.
set -x -v
exec 1>/root/kickstart-stage2.log 2>&1
ls -l /root/postinstall
Note that in both the stage1 and stage2 postinstall scripts, I redirect stdout to a log file in root’s home directory. This is very helpful for diagnosing problems during the kickstart postinstall. This comes in handy when you have many hundreds of lines of postinstall that need to be tested and debugged.
The sky is the limit for what you can do in the postinstallation:
- add users or groups
- install non-CentOS applications from RPMs (see below for some good repos)
- install non-CentOS applications from tarballs (I prefer RPMs where available, but sometimes you don’t have them handy)
- set the runlevels for various system services
- configure servers like apache, samba, sshd, and MySQL
- configure the default behavior of the bash shell
and anything else you could imagine. In my ideal world, my machines are ready to perform their designated tasks from the very first second I boot them up. I don’t want to have a series of manual steps to complete the configuration.
Organizing the postinstall files
If I can offer any suggestions in terms of how you organize your
postinstall files, I would suggest breaking the files up
into directories like this:
Put your non-CentOS application RPMs and tarballs into
apps (with a subdirectory for each application), put
application configuration files and scripts into
appconfig (again with a subdirectory for each
application), put OS configuration files (like network config files) into
sysconfig, and put general-purpose libraries (those not
specifically required by any applications you’re installing) into
Of these strategies, the organization of
apps is by far the most important. When you install
applications that are not part of the CentOS distro, you’ll likely have to install
additional libraries or utilities to satisfy dependencies in those packages. When you
need to refresh your kickstart image, it is helpful to have each app and its
dependencies contained in a single directory. If you throw them all into a big
directory, you’ll never remember, for example, that mhash is in there because aide
I have found the following repositories to be reliable sources of packages that aren’t included in the CentOS distro:
- EPEL – Extra Packages for Enterprise Linux
- repoforge – formerly RPMforge; DAG repository now redirects to this
- ATrpms – good place to get ffmpeg rpms
- ELrepo – source for hardware drivers
Good luck building your custom installation disc. I welcome any comments or suggestions you might have for this guide!