3

I am trying to automate a quick and dirty backup of a headless system. (I feel it is relevant to note that I am running Busybox utilities so this is the mount tool I have at my disposal) I have made a udev rule to execute a script when my USB device is plugged in and it does trigger and execute the called script.

Currently the script just mounts the device but it fails to do so. If I run the script manually, there is no error. I added the line exec >/home/user/udev.out 2>&1 to try to figure out why it is failing.

Script Contents:

#!/bin/sh
exec >/home/user/udev.out 2>&1
mount -t ntfs-3g /dev/sdb1 /mnt/backup
exit 0

I know I shouldn't use /dev/sdb1 in the script, and I do generate a symlink in the udev rule, but just for error checking I have hardcoded the device.

I have also tried adding a 3 second sleep before the mount command to be sure the device could be detected by the system before executing the mount command, to no avail.

Results of udev.out:

mount: mounting /dev/sdb1 on /mnt/backup failed: No such device

I know the device is there and it is listed by fdisk. If I call the script from shell, it runs successfully no problem.

Has anyone encountered anything similar before? I am not sure how to proceed.

edit: udev rule:

SUBSYSTEM=="block", ATTRS{serial}=="serialforusbdrive", SYMLINK+="kingston%n"
SUBSYSTEM=="usb", ATTRS{serial}=="serialforusbdrive", RUN+="/usr/local/bin/backup.sh"

edit: Clarification: udev rule triggers and runs my script. The script when run manually from shell (I type /usr/local/bin/backup.sh in shell and the drive is mounted) The script when run by udev rule (RUN+="/usr/local/bin/backup.sh") runs the script but fails to mount the drive.

  • is ntfs-3g already loaded? – mikeserv Apr 11 '14 at 19:50
  • Hmm.. I get ntfs-3g not found on modprobe. ntfs modules loads, though. – user2894937 Apr 11 '14 at 19:55
  • 2
    You got me moving in the right direction and we have liftoff!! I changed my script to run ntfs-3g /dev/sdb1 /mnt/backup instead of mount /dev/sdb1 /mnt/backup and it works like a charm! – user2894937 Apr 11 '14 at 20:11
  • 4
    Good - put that in an answer, please. – mikeserv Apr 11 '14 at 20:21
  • 3
    What is your udev rule? – Gilles Apr 11 '14 at 23:00
2

I eventually resolved this issue.

Problem, I think, had to do with timing of it all.

I changed my udev rules to both be on the same subsystem

SUBSYSTEM=="block", ATTRS{serial}=="serialforusbdrive", SYMLINK+="kingston%n"
SUBSYSTEM=="block", ATTRS{serial}=="serialforusbdrive", RUN+="/usr/local/bin/backup.sh"

And then added the following lines to the beginning of the script.

sleep 5
stat /dev/kingston1

I think the timing was a bit off on the whole thing and it would try to mount before the disk was ready, and the "usb" subsystem was triggering before the "block" subsystem so my symlink was non-existent when the script actually ran.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.