Installing software for the first time is the simplest case of deployment. The target system lacks your package's daemons, binaries, configuration files, and/or data files, so a first installation need not stop work in progress or back up, restore, and possibly merge files.
However, if the target system has a prior or current version of your software, or if your software is intertwined with other code and services, your RPM must take special care to maintain a workable configuration. You may need to halt processes, swap or preserve files, and restart applications after your new code has been copied to the system. Of course, it isn't always possible to maintain backward compatibility. In those instances, your RPM should make the fewest changes possible to keep the system functional and secure. In some cases, that may mean elaborate scripts to interpret old settings and transform them into new ones.
RPM provides four hooks for injecting commands into the installation and uninstallation sequences: two for installation and two for uninstallation. All hooks run on the target system and are generally sufficient for most housekeeping chores. These four hooks are:
- All commands listed in the
%prehook run before your package is installed. - Commands in the
%posthook run after your package has been installed. - The
%preunhook runs before your package is removed from the system. - Commands in the
%postunhook run after your package is removed from the system.
Hence, the order of operations during an upgrade is:
- Run the
%presection of the RPM being installed. - Install the files that the RPM provides.
- Run the
%postsection of the RPM. - Run the
%preunof the old package. - Delete any old files not overwritten by the newer version. (This step deletes files that the new package does not require.)
- Run the
%postunhook of the old package.
Steps 4 and 6 may seem a bit suspect, and for good reason: If you are upgrading a package, running the older version's uninstallation hooks could undo portions or all of steps 1 through 3. In fact, without conditions, the uninstallation hooks of the older version could destroy the newer version. To prevent unintentional clobbering, RPM passes each hook one argument, a flag. The value of the flag indicates which operation is being performed:
- If the first argument to
%preis1, the RPM operation is an initial installation. If the argument to%preis2, the operation is an upgrade from an existing version to a new one. - Similarly, the arguments to a
%postare1and2for a new installation and upgrade, respectively. (Again,%preand%postaren't executed during an uninstallation.) - If the first argument to
%preunand%postunis1, the action is an upgrade. - If the first argument to
%preunand%postunis0, the action is uninstallation.
Wrap each of your hooks with logic to test the value of the argument and execute the correct code. By default, each hook is interpreted as a Bourne shell (typically, /bin/sh) script, unless you name another script interpreter, such as Perl.
0 comments:
Post a Comment