3

I have created a *.deb package which saves some files into the required locations which works fine. During the postinst I generate a new file (merging 2 files together using my own script "mergeconfig"), the file is created fine.

However, on the next line of the postinst, I try to create a symlink to this new file, this doesn't work as expected.

Instead of getting a link, I seem to get a hard copy/a new file:

$ ls -la /etc/app/conf-enabled/
-rw-r--r-- 1 root root 2600 Dec  1 17:00 enabledconfig

I tried pulling the code out into it's own file and executing it from the postinst file (after giving it executable privileges) and got the same result. Though if I manually run the file I get a valid symlink created:

$ /../../updateEnabledConf
 -- Merge this package's server config into app config
Complete
 -- Update conf-enabled symlink

$ ls -la /etc/app/conf-enabled/
lrwxrwxrwx 1 root root   42 Dec  1 17:06 enabledconfig -> /etc/app/conf-available/server-app

The code in updateEnableConf is:

#!/bin/bash
SERVERNAME="server"

APPNAME="app"
CONFIGNAME="enabledconfig"
ETCAPPDIR="/etc/app"
CONFAVAILABLEDIR="$ETCAPPDIR/conf-available"
CONFENABLEDDIR="$ETCAPPDIR/conf-enabled"
CONFAPPDIR="$ETCAPPDIR/conf-app"
CONFSERVERDIR="$ETCAPPDIR/conf-server"

echo " -- Merge this package's server config into app config"
sudo mergeconfig $CONFAPPDIR/$APPNAME $CONFSERVERDIR/$SERVERNAME $CONFAVAILABLEDIR/$SERVERNAME-$APPMNAME

echo " -- Update conf-enabled symlink"
sudo ln -s -f $CONFAVAILABLEDIR/$SERVERNAME-$APPNAME $CONFENABLEDDIR/$CONFIGNAME

exit 0

I'm not sure what I'm doing wrong, in that the symlink isn't being created correctly during the install of the deb package, but does when I run the same file manually from putty. Both users have sudo access, I'm using gdebi to install the package and I have other packages that create symlinks in the same way without a problem. Any help would be great

UPDATE

After more investigating, turns out the symlinks were being created correctly by the postinst scripts, but there was another file being called directly after with a sed command to the path of the symlink:

ENABLEDCONF="/etc/app/conf-enabled/enabledconfig"
sed -i 's#<search>#<replace>#g' $ENABLEDCONF

This seems to take the data form the file the symlink points too, but overwrites the symlink with the new file. I have updated the sed line to use a readlink command, this has fixed the problem:

sed -i 's#<search>#<replace>#g' $(readlink $ENABLEDCONF)

Thanks for the help

  • What happens if you `set -x` and run it? You can set it back by `set +x` – jmunsch Dec 01 '14 at 18:26
  • Hey @jmunsch, not seen set -x and set +x before, a quick google seems to be debugging flags? Am I right in thinking you mean to add it like: `echo " -- Update conf-enabled symlink"` `set -x` `sudo ln -s -f $CONFAVAILABLEDIR/$SERVERNAME-$APPNAME $CONFENABLEDDIR/$CONFIGNAME` `set +x` `exit 0` – Martyn Gough Dec 01 '14 at 18:52
  • 1
    Why are you using sudo? postinst (et al) already run as root. – Jonathan Hall Dec 02 '14 at 13:10
  • 1
    @MartynGough you can write the solution also as an answer, not only as update to the question itself... – Marki555 Apr 23 '15 at 08:20

1 Answers1

0

After more investigating, turns out the symlinks were being created correctly by the postinst scripts, but there was another file being called directly after with a sed command to the path of the symlink:

ENABLEDCONF="/etc/app/conf-enabled/enabledconfig"
sed -i 's#<search>#<replace>#g' $ENABLEDCONF

This seems to take the data form the file the symlink points too, but overwrites the symlink with the new file. I have updated the sed line to use a readlink command, this has fixed the problem:

sed -i 's#<search>#<replace>#g' $(readlink $ENABLEDCONF)

– Martyn Gough

Armali
  • 18,255
  • 14
  • 57
  • 171