Building a custom CentOS 5 kickstart disc, part 4

Note: this series of articles applies to CentOS 5; for CentOS 6, see this series. Let’s make things really interesting with a postinstall script to do some custom configuration.

First things first

Unfortunately, CentOS 5 has a bug in anaconda where the install media is unmounted before the postinstall runs. This makes it impossible for your postinstall script to reference files from the install disc (if you’re doing any substantial postinstall work, you’ll need files from the disc — scripts, additional RPMs, configuration files, etc.) The fix isn’t too bad. As root, do the following: Using vi, swap the following two lines: so that they look like: (if you don’t know vi well, type /methodcomplete to jump to the methodcomplete line; type dd and then p — this deletes the line and puts it back into the file one line lower) Now save the file (in vi, type :w! to save the file, then type :q to quit). Execute this command: You now have stage2.img.new, a new version of stage2.img which fixes the bug in anaconda. Replace ~/kickstart_build/isolinux/images/stage2.img with this new version. Now you’re ready to do some serious postinstall work.

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 CD is available at /tmp/cdrom in a non-chrooted environment. In other words, when you are chrooted to /mnt/sysimage, you can’t see the CD’s contents. 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. 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. 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 (the DAG repository is a good place to find CentOS 5 compatible RPMs)
  • 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 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. 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

Leave a Reply

Your email address will not be published. Required fields are marked *