SOLVED: USB Hot Plug / Hot Swapping

Is this possible? For 3d printers running klipper, having the ability to load files / update system files with USB would be excellent.

Requests

  • Auto mount on insert, unmount on remove
  • File system FAT32 (second choice NTFS)
  • No permissions needed to read / write to the disk
  • Must not corrupt when hot swapped
  • Auto updates through USB

You can use udev and a script to accomplish this. Mount the partition read-only and it won’t corrupt it. You have to do your own updating based on the script.

FAT and NTFS are inherently prone to corruption if hot-plugged as they are not copy-on-write filesystems. If it is not mounted read-only, it will be corrupted if pulled out during a write.

BTRFS is our preferred because it can do duplicate data, metadata at the expense of storage and it countered by compression. It is COW as well so risk of full disk corruption is lower. No other FS comes close.

Made a little progress

before

after

$ sudo mount /dev/sda1 /media
$ ln -s /media /home/pi/gcode_files/usb

not successful in creating udev rule to auto mount to /media yet but it works when mounted and can remove files from the UI.

I’ve tried to use the below rule but seems not to work. I’ve also tried some other scripts that auto mount but don’t show any files.

SUBSYSTEM==“block”, RUN+="/bin/mount -a"

Another rule I tried
https://www.axllent.org/docs/auto-mounting-usb-storage/

A script I tried

See udev - ArchWiki

This will automount USB drives/ SD card readers when added and unmount when removed.

64gb USB stick formatted to NTFS
image

USB SD card reader with FAT 32 formatted 8gb card
image

both drives in
image

https://wiki.archlinux.org/title/Udev#Writing_udev_rules

###from root###
sudo apt install git
git clone https://github.com/Ferk/udev-media-automount.git
cd udev-media-automount
make
ln -s /media /home/pi/gcode_files/ #### symlink to target folder, this one is setup for Klipper

Create the file, 11-usb-sdcard-automount.rules to /etc/udev/rules.d

sudo nano /etc/udev/rules.d/11-usb-sdcard-automount.rules

###add below only and make sure it is all on one line###

ACTION==“add”, SUBSYSTEMS==“usb”, SUBSYSTEM==“block”, ENV{ID_FS_USAGE}==“filesystem”, RUN{program}+="/usr/bin/systemd-mount --no-block --automount=yes --collect $devnode /media"

sudo reboot

The drive will show as whatever name it has . file system type. In my example, USB.ntfs. Multiple drives will show up as well as USB SD card readers. Now just to write some scripts to push updates over USB for Klipper!!

1 Like

You should also check out udisksctl.