Fdisk and grep command not working in udev trigger

Hi Guys,

Can someone take a look at my scripts what missing, plugin usb drive the script is running can log all my echo but cannot execute command. Is there any configuration in linux or to my scripts need to add?.

What i want to achieve is every time I plugin the usbdisk automatic mount to /media/usb/test

cat /etc/udev/rules.d/100-mount-usb.rules

ACTION=="add", SUBSYSTEMS=="usb", ATTR{idVendor}=="8564", ATTR{idProduct}=="1000", RUN+="/bin/bash -c /usr/local/bin/test.sh"
cat /usr/local/bin/test.sh

#!/bin/bash

LOG_FILE=/opt/weekly_backup.log

DEVNAME=$(fdisk -l |grep FAT16 |awk '{print $1}')

echo "New usb device detected at $DEVNAME" >> $LOG_FILE

echo "mount $DEVNAME /media/usb/test" >> $LOG_FILE

mount $DEVNAME /media/usb/test &>> $LOG_FILE

Reload all rules in udev:

udevadm control --reload-rules

Trigger all udev rules:

udevadm trigger

Here the log ouput:

cat /opt/weekly_backup.log

New usb device detected at
mount  /media/usb/test
mount: can't find /media/usb/test in /etc/fstab or /etc/mtab

Which distribution of Linux??

Just a shot in the dark: did you consider supplying the full pathnames?

Yes sir I did tried /bin/fdisk, /bin/grep

------ Post updated at 05:47 PM ------

CentOS 6.8 Sir

Looking at the output( mount /media/usb/test ), DEVNAME is not set. It looks like the previous fdisk command is not returning anything. What is fdisk -l |grep FAT16 |awk '{print $1}' returning if you run it manually?

If DEVNAME isn't set, the mount command won't know which device to mount, and will look in the fstab / mtab for /media/usb/test to figure out what to do.

Supposedly it will return /dev/sdb1 in my case, already tried manually like this DEVNAME="/dev/sdb1" and put echo $DEVNAME and i see the output, but is not mounting

"Supposedly" doesn't really instil confidence :slight_smile: You tried setting DEVNAME manually, and that also didn't work?

Is all of the code in the same script.. e.g. did you export DEVNAME if it's not?

If you set DEVNAME manually, after (or in place of) the fdisk command, there's no reason that the output would be

mount /media/usb/test

instead of

mount /dev/sdb1 /media/usb/test

If the variable DEVNAME is not created as read-only (e.g. gives an error why you try to set it), and the script is not being run in a shell jail (chroot environment), that is.

Yes Sir you right the output

mount /dev/sdb1 /media/usb/test

but mounting is not working

Do you have permissions to mount filesystems?

I dont know where can i add permission, the trigger of my script when the usbdisk plugi it in. Do you have any idea?

Generally speaking, to mount a filesystem, you need root privileges. If you're running your script as a non-root user, you can get root privileges for the purpose of mounting the FS, using sudo. If you administer the system you can easily do this by adding something to the "sudoers" file (with the visudo command, not vi), for example, from the sudoers file in CentOS 6:

## Allows members of the users group to mount and unmount the
## cdrom as root
# %users  ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom
%users

is replace my user as

root

sir? In my visudo put this

root ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom, /bin/fdisk /bin/grep

is this right sir?

OK, it's late and I need to go. Let's speed this up.

Are you root? If you are, what error do you get when you try to mount the filesystem manually? A media error, an unsupported type error, an unknown / unsupported FS type error, or what?

Please try to be more forthcoming and communicative with information so we don't end up with a War and Peace-sized thread for what is probably a simple problem.

If you're not root, then the sudo rule you need to add should be for the user you want to allow to be superuser for the purposes of running the command - which won't be root, as root doesn't need sudo to run anything - the su in sudo means super user.. root is already a superuser.

If manually mount the

/dev/sdb1 /media/usb/test

no error sir, my script works run manually in terminal,but is not working from udev trigger when plugin the usbdisk.

Thanks for your time

Ah, bajeezus.. I somehow forgot this was about udev! Are there any errors in the system logs (dmesg, etc.)?

Here what happened when issued run

udevadm trigger

manually.

cat weekly_backup.log

New usb device detected at
mount  /media/usb/test
mount: can't find /media/usb/test in /etc/fstab or /etc/mtab

and here the logs also when plugin my usb to the server, which is I get sameoutput in my log.

cat weekly_backup.log

New usb device detected at
mount  /media/usb/test
mount: can't find /media/usb/test in /etc/fstab or /etc/mtab

Im running in Centos 6.9

Thanks

------ Post updated at 08:20 PM ------

no error in dmesg sir

OK, well apologies for the slight tangent I sent us off on, but again it comes back to this "DEVNAME" thing.

Before, or after, when you call fdisk..

DEVNAME=$(fdisk -l |grep FAT16 |awk '{print $1}')

can you add some debug to the log file?

fdisk -l |grep FAT16 |awk '{print $1}' 2>&1 >> $LOG_FILE

It seems that's not doing what you think it should be. Again, as previously suggested, you should fully qualify the commands (/usr/bin/grep, etc.) - shouldn't make any assumptions about the environment udev is using when it does stuff.

If you change all of the ">> $LOG_FILE" to "&>> $LOG_FILE", you'll also log all the errors, if there are any.

cat  /usr/local/bin/test.sh

#!/bin/bash

LOG_FILE=/opt/weekly_backup.log

DEVNAME=$(/sbin/fdisk -l |/bin/grep FAT16 |awk '{print $1}') 2>&1 >> $LOG_FILE

#fdisk -l |grep FAT16 |awk '{print $1}' 2>&1 >> $LOG_FILE

sleep 2

echo "New usb device detected at $DEVNAME" &>> $LOG_FILE

echo "mount $DEVNAME /media/usb/test" &>> $LOG_FILE

sudo mount $DEVNAME /media/usb/test &>> $LOG_FILE

running this command

udevadm trigger

is working here the output of log

cat /opt/weekly_backup.log

New usb device detected at /dev/sdb1
mount /dev/sdb1 /media/usb/test

pluging the usb here the output:

New usb device detected at
mount  /media/usb/test
mount: can't find /media/usb/test in /etc/fstab or /etc/mtab

but if I put manually the

DEVNAME=/dev/sdb1

and plugin the usb is working

I'll agree with Scott. It looks like something is not being set, or is not mounted to set. Maybe if you post the output of "mount" and "fdisk -l" we can see what's missing.

Here's the output

fdisk -l

Disk /dev/sdb: 8019 MB, 8019509248 bytes
255 heads, 63 sectors/track, 974 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x78a9e380

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *           1         975     7830528    c  FAT16
Partition 1 has different physical/logical endings:
     phys=(973, 254, 63) logical=(974, 250, 44)