1

I am new to Linux and programming. My question is similar to this one but I am running a Raspberry Pi 4 headless with no desktop [buster lite] which does seem to automount the usb drives. I haven't found a non-janky way to automatically mount USB new drives so that their path will be predictable. This RPi4 has a sensor and I want it to automatically save the sensor data onto any thumbdrive that is inserted into it. I can just periodically unplug the drive and put in a new one, then go back home. I need to be able to use any fresh USB drive, not just reusing the same few. The rest of the work I am doing is on python but this function could be carried out separately without python. This solution needs to survive reboots of the pi.

Appreciate everyone's help. As I am quite new, I'd especially appreciate explanations of what you're recommending.

1
  • I solved part of the problem my using the package usbmount; however, the mounting points it creates (/media/usb0 ... /media/usb7) exist whether or not any USB drive is plugged in. Furthermore, one can actually write to these locations even when no USB is plugged. If I could fix that, then it would be a solution.
    – rfii
    Commented Jul 19, 2020 at 21:09

1 Answer 1

1
  1. Write a script to populate /dev/sdX and mount the usb's (or do other checks)
  2. Modify udevd to run your script when a USB drive is attached.

The script can start like this:

    for i in $(ls /dev/sd* | awk -F '/'  '{ print $3 }')
    do
            echo "Do stuff like: mkdir /mnt/$i"
    done

You can config udev like this. With proper values you will see devices at /dev/usb/NAMEYOUCHOOSE

BUS=="usb", ACTION=="add", KERNEL=="sd[a-z][0-9]*", NAME="%k", RUN+="/usr/local/sbin/YOURSCRIPT"

Helpful commands

udevadm monitor

Plug your devices. Then check their attributes like

udevadm info --path /devices/pci0000:00/0000:00:1d.7/usb1/1-4/usb_device/usbdev1.49 --attribute-walk

(You have to use different values)

After you write your rules, force udevd re-read them.

udevadm control --reload-rules

Recconnect your devices. Modify /etc/fstab as you wish.

But I prefer something simpler. Maybe you have /dev/disk/by-label/ So label your media accordingly and modify your fstab.

6
  • Thank you! I am trying to do this w/o having to precondition the USB devices. I need to be able to let someone go buy a USB drive from any store and plug it in right away. Am I interpreting your answer correctly that it doesn't require a pre-known disk label or UUID?
    – rfii
    Commented Jul 20, 2020 at 23:10
  • 1
    My bad, check updated answer.
    – krg
    Commented Jul 22, 2020 at 19:00
  • thanks! However, it doesn't seem like USB drives are always put onto sda1. Sometimes its sdb, sdc, etc.
    – rfii
    Commented Jul 25, 2020 at 20:29
  • 1
    Weird. Added a bash script to get you starting.
    – krg
    Commented Jul 26, 2020 at 20:37
  • 1
    Hmmm... it's been two years! For a start, you should check if udev reacts to your USB insertion with udevadm monitor. Run the command before the USB insertion. Insert the USB and check for reporting.
    – krg
    Commented Apr 19, 2022 at 14:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .