TransWikia.com

Automatically mount and copy files to USB drive [Raspbian Buster Lite, headless]

Super User Asked on November 29, 2021

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.

One Answer

  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.

Answered by krg on November 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP