I am upgrading our project RPM. The problem is when I upgrade from projectname-1.0-0 to projectname-1.0-1, it first installs the new project and uninstalls the old project, which, in overall view, removes my project entirely. I have used "vv" option while upgrading and the output showed the uninstallation is done after installation.
Somebody please help with this problem. Is there anything I should change specifically in the RPM spec or rpmbuild options?
Yes, when an RPM upgrade occurs, RPM first installs the new version of the package and then uninstalls the old version of the package. Only the files of the old package are removed. But your scripts (i.e. %pre, %post, %preun, %postun) need to know whether they are handling an upgrade or just a plain install or uninstall.
The rpm command will pass one argument to your scripts, that is, $1, which is a count of the number of versions of the package that are installed. The table below (from the RedHat RPM Guide by Eric Foster-Johnston) provides a sample of possible values.
Install the first time: 1
Upgrade: 2 or higher
(depending on the number of versions installed)
Remove last version of package: 0
So, in your %preun, you probably want to check if "$1 = 0" before removing any services.
For more information (and a better table) see: http://docs.fedoraproject.org/en-US/Fedora_Draft_Documentation/0.1/html/RPM_Guide/ch09s04s05.html
Yes. During the rpm install, the %install and %post scripts will be called. After successful installation, the %preun and %postun scripts will be called to uninstall the previous version of rpm. If not handled properly, these %preun and %postun scripts may manipulate the changes brought in by %install and %post scripts.
The rpm sets $1 argument with appropriate values to distinguish the number of rpm versions installed. During fresh installation of projectname-1.0-0, %install and %post scripts will be called with $1 set to 1 indicating that this is the only active version. When upgraded to projectname-1.0-1, %install and %post scripts will be called with $1 set to 2. After which, the %preun and %postun scripts will be called with $1 set to 1 so as to clean up stuffs of projectname-1.0-0. Thus by writing the spec file based on $1 value, we can handle the upgrades effectively.
When you upgrade a RPM package, scripts are executed in following order:
1. %Pre of new package
2. %Post of new package
3. %Preun of old package
4. %Postun of old package
Whether the installation is fresh or upgrade, there is one argument passed to each script which represents number of RPMs installed of the same package with different versions.
For pre & post scripts it will be 1 in case of first install.
For preun & postun scripts it will be 0 for last uninstall.
What might be happening in your case is that preun or postun scripts might be deleting files that are useful for new package. You don't need to worry about manual file deletion in postun scripts, it will be handled intelligently be RPM itself.
ref: Upgrading & uninstalling
it is important to understand how uninstall sections work on upgrade
We have a variable $1 which can be checked at pre, post, unpre, unpost (its value differs based on install, upgrade, uninstall)
Depending on its value we can say whether it is install or upgrade from within rpm
E.g:
In Pre/post
if $1 == 1 initial installation
if $1 == 2 upgrade
In preun/postun
if $1 == 0 uninstall
if $1 == 1 upgrade
consider upgrading abc-1 from version 1 to version 2 (abc-2)
Run %pre from "abc-2".
Run %post from "abc-2".
Run %preun from "abc-1".
Run %postun from "abc-1".