Note: this series of articles applies to CentOS 6; for CentOS 5, see this series.
Let's make things really interesting with a postinstall script to do some custom configuration.
The good news
In CentOS 5, you had to do some fairly ugly modifications to the boot images to allow your postinstall code to access files from the install media, and then your postinstall code had to remount the install media. You don't have to do that anymore! You can just access your files under /mnt/source.
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 %post directive.
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 /mnt/sysimage, not at / the way it will be once the system is up and running after installation.
By default, the commands in the %post 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.
%post --nochroot
#!/bin/sh
set -x -v
exec 1>/mnt/sysimage/root/kickstart-stage1.log 2>&1
echo "==> copying files..."
cp -r /mnt/source/postinstall /mnt/sysimage/root/postinstall
In our case, we've put all our postinstallation files into the postinstall 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. So the contents of the postinstall directory on the install disc are copied 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.
%post
#!/bin/sh
set -x -v
exec 1>/root/kickstart-stage2.log 2>&1
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:
~/kickstart_build/isolinux/postinstall
+-- apps
+-- appconfig
+-- sysconfig
+-- libs
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 libs.
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 requires it.
External repositories
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!
Part 1 • Part 2 • Part 3 • Part 4





Comments