Jacob J. Walker's Blog

Scholarly Thoughts, Research, and Journalism for Informal Peer Review

Passing Variables from the %Pre to %Post scripts in Kickstart on Ubuntu

with 8 comments

Picture of a Penguin (Tux) kickingThe development of the VubuntuBox Linux Distribution has required making a custom installation of Lubuntu, and I thought the easiest way to do this would be through the standard / recommended methods for making an Automatic Installation, which was either to use preseeding or to use kickstart.

In the end, it turned out that I needed to use both to automate the installation to a level needed, and that several “tricks” were needed to solve inherent limitations of kickstart (and preseeding).   And now that I’ve figured some of these out, I am writing several blog articles to share what I’ve learned, so others hopefully won’t have to bang their head against the wall as much as I have.  Also, I should note, that I’m not a Linux expert, so there may be more elegant solutions than the ones I’ll be sharing in these articles.

The Problem

One of the first challenges that was faced, was how to ask questions of the user in the beginning of an installation, but act on these at the end of the installation.  This has been important in VubuntuBox because the goal is to have the installation as automatic as possible, and so it is better to have all user interaction happen in the beginning and then to be able to just leave the machine to do the rest.

I never figured out how to do this with preseeding, although it has an early_command and late_command option, these just never seemed to work right for me.  Kickstart has the %pre and %post scripts, and with an ingenious trick from Michael Ludvig, you can make these interactive with the user.  But I could not at first figure out how to get environmental variables created in the %pre section to work in the %post section, and because the default behavior is to have the %post be a chroot of the final installation, I couldn’t just save the variables in the initial file system (rootfs).

The Solution

The solution to this problem was to create a ram disk where I could save my variables by using the following code in my %pre script:

mkfs -q /dev/ram1 8192
mkdir -p /variables
mount /dev/ram1 /variables

I then would read in variables and copy them to the /variables folder, as the following example code demonstrates:

read -p "Enter the full URL of the Virtual Image: " VirtualImageURL
echo $VirtualImageURL > /variables/VirtualImageURL

Also, while I’m not sure if it is necessary, I went ahead and unmounted the ram disk at the end of the %pre section, and then mounted it again in the post section, with similar code, but not using the mkfs again, as the file system is still in /dev/ram1.

To get a variable out, I would use the export command with inline command substitution, as the following example in the %post section shows:

export VirtualImageURL=`cat /variables/VirtualImageURL`

There are probably some more elegant solutions for some of this (like some type of loop to get all the variables from the ram disk), but I can vouch that my method works.

Also, I should mention, that to do this, you need to have mkfs installed in the %pre environment.  And for right now, I’m “cheating” by just copying in the binary and libraries.  But in the future, I hope to be able to load the appropriate packages, as I talked about in a previous post.

Post Revisions:

Written by Jacob Walker

November 30th, 2014 at 5:50 am

Posted in Descartes' Daemon

8 Responses to 'Passing Variables from the %Pre to %Post scripts in Kickstart on Ubuntu'

Subscribe to comments with RSS or TrackBack to 'Passing Variables from the %Pre to %Post scripts in Kickstart on Ubuntu'.

  1. Hi Jacob

    I wonder if you could elaborate on what it is you are coping into the %pre with respect to the “mkfs”.



    10 Mar 15 at 8:47 am

  2. Hi

    I have question on %pre %post. My requirement is to backup whatever all the messages under /var/log -> /opt/. The reason is when we update our Centos, /var/log gets re-created and all messages are lost. Where /opt/ is persisted.
    So I want to do following

    under %pre
    1) create temporary filesystem ( which you showed in the article above it is possible).
    2) copy all the messages under /var/log/messages* into the temporary filesystem.
    under %post
    1) copy all the messages* under temporary file system to /opt/log/

    1) is possible to mount the filesystem where the /var/log/messages* is present. If yes, i want to copy them ?
    2) Is it possible to copy them into temporary filesystem created under %pre ?
    3) Is the temporary file system accessible in %post ?



    12 May 15 at 1:14 am

  3. Hey,

    wouldn’t it be easier to do something like:
    echo “VARIABLE=$VALUE” >> /tmp/variables.txt and then read that file from particular sections ? It prevents installer to eat up memory and you can make a copy of this file on your real filesystem so you can analyze the output later on.


    6 Jul 15 at 6:08 am

  4. Because the system has a chroot later, I don’t believe that the /tmp folder from the original root will be available later, and the amount of memory that is needed to make the ram drive is rather small. But, with that being said, there probably is some way of passing the variable in a more elegant manner than I’m doing it, as I’m not an uber Linux nerd, and so I recognize my solution is rather kludgy.

    Jacob Walker

    6 Jul 15 at 6:44 am

  5. Dan is correct: You can copy your variable-file to /tmp. This directory is a ram disk that anaconda creates at install time, no need to create and mount your own.

    Then in a “%post –nochroot” section of the kickstart file, copy the variable file from /tmp to /mnt/sysimage/root (for example).

    In the “%post” section (after the chroot), the files will be available at /root. I do this in my kickstarts to capture /tmp/partition.info and other anaconda log files for examination after the install.

    Austin Ellis

    2 Sep 15 at 7:32 am

  6. Hmm is anyone else experiencing problems with the images on this blog loading? I’m trying to determine if its a problem on my end or if it’s the blog. Any responses would be greatly appreciated.

    Angelia Lathim

    15 May 17 at 6:24 am

  7. I often will link to graphics from other sites. I probably should have more that are original…. So sometimes old posts don’t have the graphics work any longer. I will work on fixing this in the future.

    Jacob Walker

    15 May 17 at 10:10 am

  8. I attempted your approach for a CENTOS 7 but mkfs is not available at that stage. Any suggestions how I can solve this?
    edititing the initrd.img seems unconvienent to me. Can I put it somewhere that I don’t need to format first?


    11 Dec 17 at 8:28 am

Leave a Reply

%d bloggers like this: