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
    Jul 19 '20 at 21:09
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.

4
  • 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
    Jul 20 '20 at 23:10
  • My bad, check updated answer.
    – krg
    Jul 22 '20 at 19:00
  • thanks! However, it doesn't seem like USB drives are always put onto sda1. Sometimes its sdb, sdc, etc.
    – rfii
    Jul 25 '20 at 20:29
  • 1
    Weird. Added a bash script to get you starting.
    – krg
    Jul 26 '20 at 20:37

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.