Tuesday, April 19, 2022

[SOLVED] How do i delete a file that is installed from post installation of a rpm and is not needed in the upgrade of it?

Issue

Following is the first version of rpm spec

    %post
    if [ "$1" = "1" ];
    then
        touch /usr/bin/item1.txt
        touch /usr/bin/item2.txt
        echo "i am in and this line is written from rpm 1-1">>/usr/bin/item1.txt
        echo "i am in and this line is written from rpm 1-1">>/usr/bin/item2.txt
    fi          
    %preun    
    if [ "$1" = "0" ];
    then
        sed -i "/i am in and this line is written from rpm 1-1 /d" /usr/bin/item1.txt
        sed -i "/i am in and this line is written from rpm 1-1 /d" /usr/bin/item2.txt
        rm -rf /usr/bin/item1.txt
        rm -rf /usr/bin/item2.txt
    fi
    if [ "$1" = "1" ];
    then
     # what should be here ?
    fi 

The seacond version of rpm spec is as follows

%post
if [ "$1" = "1" ];
then
    touch /usr/bin/item1.txt
    echo "xyz1" >> /usr/bin/item1.txt
    touch /usr/bin/item3.txt
    echo "xyz3" >> /usr/bin/item3.txt
fi

if [ "$1" = "2" ];
then
    # what should be here if i want to remove item2.txt file and add item3.txt
fi
%preun
if [ "$1" = "0" ];
then
# will i have to remove all the files item1 and item 2 along with item3.txt here 
fi

if [ "$1" = "1" ];
then
    ##
fi

i want to simply remove the file item2 which is already intalled in post install script of base rpm and install item3.txt file as part of upgrade.


Solution

It seems you have a wrong idea of how rpm packaging works. Hereis a sample workflow that would create package-1.0.0-0.spec and package-2.0.0-0.rpmfile:

create the two files that you want to package (these command happen outside the spec file, just like when you write code):

echo "i am in and this line is written from rpm 1" > item1.txt
echo "i am in and this line is written from rpm 1" > item2.txt

now create a spec file just beside with these parts:

Version: 1.0.0
Release: 0

%install
install -d -m 0755 "${RPM_BUILD_ROOT}/usr/bin/"
cp item1.txt ${RPM_BUILD_ROOT}/usr/bin/
cp item2.txt ${RPM_BUILD_ROOT}/usr/bin/

%files
/usr/bin/item1.txt
/usr/bin/item2.txt

this will create package-1.0.0-0.rpm containing item1.txt and item2.txt now suppose we want to create the next version; then:

echo "xyz1" >> item1.txt
echo "xyz3" >> item3.txt

the spec file should now contain:

Version: 2.0.0
Release: 0

%install
install -d -m 0755 "${RPM_BUILD_ROOT}/usr/bin/"
cp item1.txt ${RPM_BUILD_ROOT}/usr/bin/
cp item3.txt ${RPM_BUILD_ROOT}/usr/bin/

%files
/usr/bin/item1.txt
/usr/bin/item3.txt

which will create package-2.0.0-0.rpm. Upon upgrade, rpm will now remove /usr/bin/item2.txt and install the new versions of item1.txt and item3.txt



Answered By - Chris Maes
Answer Checked By - Mary Flores (WPSolving Volunteer)