LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Debian (https://www.linuxquestions.org/questions/debian-26/)
-   -   script called from udev-rule can not mount properly (https://www.linuxquestions.org/questions/debian-26/script-called-from-udev-rule-can-not-mount-properly-4175592728/)

huitzilopochtli 11-02-2016 09:05 AM

script called from udev-rule can not mount properly
 
an old suse 11.2 is to be transfered to a newer system, debian was chosen.

3 external usb-devices are used to backup some files, they are mounted using udev rules. The actual mounting is done in a separate script. This script works fine when called directly by root, but can not mount properly when called by the rule.
when it failed, ls -lah /export looks like this:
-----------------------------
drwxr-xr-x 8 root root 4,0K Okt 21 15:50 .
drwxr-xr-x 23 root root 4,0K Feb 26 2016 ..
drwxr-xr-x 7 root root 4,0K Okt 19 15:24 data
drwxrwxrwx 15 root root 4,0K Jun 22 13:28 local
drwxr-xr-x 2 root root 4,0K Okt 19 15:21 usb
drwxr-xr-x 2 root root 4,0K Okt 21 15:44 usb1
drwxr-xr-x 2 root root 4,0K Okt 21 15:50 usb2
d????????? ? ? ? ? ? usb3
------------------------------
last line, usb3, the directory is not accessible.

this is the rule (/etc/udev/rules.d/98-usbmount.rules):
---------------------------------
#
# rules zum automatischen mounten von USB-Devices
#
#hier sind die Regeln angepasst auf debian - dort werden andere Keys sichtbar als unter suse 11.2, deswegen musste ich
# das anpassen lgkf 28.10.16
#

KERNEL=="sd?1", ACTION=="add", ENV{ID_MODEL}=="WDC_WD30EZRX-00SPEB0", ENV{ID_FS_UUID}=="389CE0F676DA7740", SYMLINK+="usb1", RUN+="/etc/udev/scripts/usbmount add 1 389CE0F676DA7740"
KERNEL=="sd?1", ACTION=="add", ENV{ID_MODEL}=="WDC_WD30EZRX-00SPEB0", ENV{ID_FS_UUID}=="4EB9757B135E8163", SYMLINK+="usb2", RUN+="/etc/udev/scripts/usbmount add 2 4EB9757B135E8163"
KERNEL=="sd??", ACTION=="add", ENV{ID_MODEL}=="TOSHIBA_DT01ACA300", ENV{ID_FS_UUID}=="40983A39983A2E32", SYMLINK+="usb3", RUN+="/etc/udev/scripts/usbmount add 3 40983A39983A2E32"

# umount usb1-usbX
KERNEL=="sd?1", ACTION=="remove", ENV{ID_MODEL}=="WDC_WD30EZRX-00SPEB0", ENV{ID_FS_UUID}=="389CE0F676DA7740", RUN+="/etc/udev/scripts/usbmount remove 1"
KERNEL=="sd?1", ACTION=="remove", ENV{ID_MODEL}=="WDC_WD30EZRX-00SPEB0", ENV{ID_FS_UUID}=="4EB9757B135E8163", RUN+="/etc/udev/scripts/usbmount remove 2"
KERNEL=="sd??", ACTION=="remove", ENV{ID_MODEL}=="TOSHIBA_DT01ACA300", ENV{ID_FS_UUID}=="40983A39983A2E32", RUN+="/etc/udev/scripts/usbmount remove 3"
-------------------------------------
and this is the script used to mount (/etc/udev/scripts/usbmount):
-------------------------------------
#!/bin/bash
#
# usbmount - automount-Skript zum automatischen mounten von USB-Devices
# ueber udev
#
BASENAME=`basename $0`
TMPLOGGERFILE=/tmp/$BASENAME.$$
MOUNTTO=/export
DIRPREFIX=usb

EXPORTS=/etc/exports
EXPORTS_LINE="$MOUNTTO/${DIRPREFIX}$2 *(rw,no_root_squash,sync,no_subtree_check)"
EXPORT_SEARCH_PATTERN="^$MOUNTTO/${DIRPREFIX}$2"
#NFSSERVER=/etc/init.d/nfsserver
NFSSERVER='service nfs-kernel-server'

touch $TMPLOGGERFILE



log()
{
logger -i -t $BASENAME -f $TMPLOGGERFILE
rm $TMPLOGGERFILE > /dev/null 2>&1
}

usage()
{
echo "Usage: $BASENAME add|remove <devnr>" >> $TMPLOGGERFILE
log
exit
}

if [ $# -gt 3 ] ; then
usage
fi

echo Username is >> $TMPLOGGERFILE
whoami >> $TMPLOGGERFILE
echo $USER >> $TMPLOGGERFILE
echo $LOGNAME >> $TMPLOGGERFILE


if [ "$1" == "add" ] ; then
echo mount /dev/disk/by-uuid/$3 $MOUNTTO/$DIRPREFIX$2 >> $TMPLOGGERFILE
# mkdir -p $MOUNTTO/$DIRPREFIX$2 >> $TMPLOGGERFILE 2>&1
sleep 2
mount /dev/disk/by-uuid/$3 $MOUNTTO/$DIRPREFIX$2 >> $TMPLOGGERFILE 2>&1

# Falls der Mountpoint noch nicht exportiert, exportieren und
# den nfsserver reloaden
if [ `grep $EXPORT_SEARCH_PATTERN $EXPORTS | wc -l` -eq 0 ] >> $TMPLOGGERFILE 2>&1 ; then
echo Exporting $MOUNTTO/${DIRPREFIX}$2 ... >> $TMPLOGGERFILE 2>&1
cp -a /etc/exports /etc/exports.old >> $TMPLOGGERFILE 2>&1
echo "$EXPORTS_LINE" >> $EXPORTS
echo Reloading nfsserver ... >> $TMPLOGGERFILE 2>&1
$NFSSERVER reload >> $TMPLOGGERFILE 2>&1
fi
else
if [ "$1" == "remove" ] ; then
# Falls der Mountpoint exportiert, nicht mehr exportieren und
# den nfsserver reloaden
if [ `grep $EXPORT_SEARCH_PATTERN $EXPORTS | wc -l` -gt 0 ] ; then
echo Removing export $MOUNTTO/${DIRPREFIX}$2 ... >> $TMPLOGGERFILE 2>&1
grep -v $EXPORT_SEARCH_PATTERN $EXPORTS >> /tmp/$BASENAME.exports
cp -a /etc/exports /etc/exports.old >> $TMPLOGGERFILE 2>&1
mv /tmp/$BASENAME.exports /etc/exports >> $TMPLOGGERFILE 2>&1
echo Reloading nfsserver ... >> $TMPLOGGERFILE 2>&1
$NFSSERVER reload >> $TMPLOGGERFILE 2>&1
fi

echo umount $MOUNTTO/$DIRPREFIX$2 >> $TMPLOGGERFILE
umount $MOUNTTO/$DIRPREFIX$2 >> $TMPLOGGERFILE 2>&1
else
usage
fi
fi

log
------------------------------

the skcipt executes, this is output in /var/log/messages:
-----------------------------
Nov 2 11:15:49 nas1 kernel: [ 5470.990909] scsi 11:0:0:0: Direct-Access ASMT 2105 0 PQ: 0 ANSI: 6
Nov 2 11:15:49 nas1 kernel: [ 5470.991646] sd 11:0:0:0: Attached scsi generic sg7 type 0
Nov 2 11:15:49 nas1 kernel: [ 5471.002752] sd 11:0:0:0: [sdc] Spinning up disk...
Nov 2 11:15:58 nas1 kernel: [ 5472.004039] .........ready
Nov 2 11:15:58 nas1 kernel: [ 5480.070753] sd 11:0:0:0: [sdc] 732566646 4096-byte logical blocks: (3.00 TB/2.72 TiB)
Nov 2 11:15:58 nas1 kernel: [ 5480.072751] sd 11:0:0:0: [sdc] Write Protect is off
Nov 2 11:15:58 nas1 kernel: [ 5480.074733] sd 11:0:0:0: [sdc] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
Nov 2 11:15:58 nas1 kernel: [ 5480.128554] sdc: sdc1 sdc2
Nov 2 11:15:58 nas1 kernel: [ 5480.134789] sd 11:0:0:0: [sdc] Attached SCSI disk
Nov 2 11:16:04 nas1 usbmount[2431]: Username is
Nov 2 11:16:04 nas1 usbmount[2431]: root
Nov 2 11:16:04 nas1 usbmount[2431]:
Nov 2 11:16:04 nas1 usbmount[2431]:
Nov 2 11:16:04 nas1 usbmount[2431]: mount /dev/disk/by-uuid/40983A39983A2E32 /export/usb3
Nov 2 11:16:04 nas1 usbmount[2431]: Exporting /export/usb3 ...
Nov 2 11:16:04 nas1 usbmount[2431]: Reloading nfsserver ...
-------------------------------------
it works fine when the script is started by root, this shows up in
/var/log/messages:
-------------------------------------
Nov 2 12:20:39 nas1 usbmount[2640]: Username is
Nov 2 12:20:39 nas1 usbmount[2640]: root
Nov 2 12:20:39 nas1 usbmount[2640]: root
Nov 2 12:20:39 nas1 usbmount[2640]: root
Nov 2 12:20:39 nas1 usbmount[2640]: mount /dev/disk/by-uuid/40983A39983A2E32 /export/usb3
Nov 2 12:20:39 nas1 usbmount[2640]: Exporting /export/usb3 ...
Nov 2 12:20:39 nas1 usbmount[2640]: Reloading nfsserver ...
--------------------------------------
mountpoint looks good and can be accessed.

the lines follwing 'Username is...'
all show 'root' now, this is different when called by the udev-rule.
anyway, whoami says 'root'
so i think the script is run by root - but can not mount properly.

any ideas?

by the way: the symlink-entries seem not to work, there is no device /dev/usb3

thanks in advance

this is a crosslink to:
https://www.heise.de/forum/heise-onl...29439215/show/

dijetlo 11-03-2016 08:48 AM

Quote:

echo Username is >> $TMPLOGGERFILE
whoami >> $TMPLOGGERFILE
echo $USER >> $TMPLOGGERFILE
echo $LOGNAME >> $TMPLOGGERFILE #<-- this didn't appear in your output
This produces two different outputs based on execution permissions.
As root, it produces:
Quote:

Nov 2 12:20:39 nas1 usbmount[2640]: Username is
Nov 2 12:20:39 nas1 usbmount[2640]: root
Nov 2 12:20:39 nas1 usbmount[2640]: root
Nov 2 12:20:39 nas1 usbmount[2640]: root
Script running at mount however, produces this:
Quote:

Nov 2 11:16:04 nas1 usbmount[2431]: Username is
Nov 2 11:16:04 nas1 usbmount[2431]: root
Nov 2 11:16:04 nas1 usbmount[2431]:
Nov 2 11:16:04 nas1 usbmount[2431]:
So what's the difference between "whoami" and "$USER"?
whoami returns the effective users id (euid) while $USER returns the environmental variable.

Which is a long way of saying, log in as root and run a 'set' command. Look at the environmental variables. The source of your issue could be something as simple as a discrepancy between roots $PATH and system $PATH, for example

huitzilopochtli 11-13-2016 06:54 AM

Thank you for your answer.
Quote:

Originally Posted by dijetlo (Post 5626471)
(...)
whoami returns the effective users id (euid) while $USER returns the environmental variable.

Which is a long way of saying, log in as root and run a 'set' command. Look at the environmental variables. The source of your issue could be something as simple as a discrepancy between roots $PATH and system $PATH, for example

putting in a 'set >> (logfile)' reveals a difference in paths, but i do not consider these differences being relevant.
as user root i get :
Quote:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
inside the script, order is switched at some positions:
Quote:

PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:.
also tried absolute paths, no change, and tried explicit /sbin/mount.ntfs and /sbin/mount.ntfs-3g

problem depends on the version of debian and on filesystem used on the device:
only ntfs shows this problem, vfat and ext3 work fine.

I tried on a debian wheezy 7.11 and it works fine.

At the time being I do not have access to the system where i saw the problem first time, but i tried on a debian jessie 8.6 and it shows the same problem.

symlink+= .. entries work fine with filesystem vfat or ext3 but do not with ntfs -
however, the symlink seems to be present in the environment of the script - it shows up in the output produced by putting in 'set >> /var/log/messages' into the script:
Quote:

DEVLINKS='/dev/disk/by-id/usb-Kingston_DataTraveler_2.0_000AEBFFB4C85B9103080017-0:0-part1 /dev/disk/by-path/pci-0000:00:1d.0-usb-0:1.4:1.0-scsi-0:0:0:0-part1 /dev/disk/by-uuid/7F01570947C7C66F /dev/usbNTFS8'
so, i do not see a /dev/NTFS8 after i plugged in the stick - but I see a /dev/KGS4 when using a Stick with a vfat filesystem on the very same machine.

so some error in the execution of the script called by the udev-rule prevents the symlink to show up properly.

currently trying to get udev to produce more output.


All times are GMT -5. The time now is 05:25 PM.