WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Automounting, card readers, CD drives, and udev  (Read 4353 times)

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11679
Automounting, card readers, CD drives, and udev
« on: November 23, 2019, 01:22:05 AM »
The subject of automounting has come up numerous times in the past, so I decided to work on my own implementation. The script is
heavily commented to explain its operation, including the arbitration logic which ensures all running copies of the script execute in an
orderly fashion. Here's a sample of the scripts execution when simultaneously plugging in 2 thumb drives with 4 partitions on each:
Code: [Select]
12:16:11.728014826 sdg3 Start Action=add ID_FS_TYPE=vfat
12:16:11.733783046 sdg2 Start Action=add ID_FS_TYPE=ext4
12:16:11.734910904 sdg4 Start Action=add ID_FS_TYPE=vfat
12:16:11.736757228 sdg1 Start Action=add ID_FS_TYPE=ntfs
12:16:11.741884957 sdg3 Arbitration complete.
12:16:12.642256457 sdh3 Start Action=add ID_FS_TYPE=ext4
12:16:12.647476239 sdh5 Start Action=add ID_FS_TYPE=ntfs
12:16:12.649693723 sdh2 Start Action=add ID_FS_TYPE=vfat
12:16:12.653829231 sdh1 Start Action=add ID_FS_TYPE=vfat
12:16:15.194433818 sdg3 Rebuildfstab
12:16:15.204650971 sdg3 Mount succeeded.
12:16:15.210529823 sdg3 End.
12:16:15.241720415 sdg4 Arbitration complete.
12:16:15.269454517 sdg4 Mount succeeded.
12:16:15.274411476 sdg4 End.
12:16:15.350481248 sdg2 Arbitration complete.
12:16:15.365231122 sdg2 Mount succeeded.
12:16:15.371101648 sdg2 End.
12:16:15.471143422 sdg1 Arbitration complete.
12:16:15.509303554 sdg1 Mount succeeded.
12:16:15.514125518 sdg1 End.
12:16:15.521592647 sdh5 Arbitration complete.
12:16:15.576720442 sdh5 Mount succeeded.
12:16:15.581481619 sdh5 End.
12:16:15.643502356 sdh3 Arbitration complete.
12:16:15.660354467 sdh3 Mount succeeded.
12:16:15.665355678 sdh3 End.
12:16:15.746717722 sdh2 Arbitration complete.
12:16:15.760382074 sdh2 Mount succeeded.
12:16:15.765185883 sdh2 End.
12:16:15.843873444 sdh1 Arbitration complete.
12:16:15.856722142 sdh1 Mount succeeded.
12:16:17.261456513 sdh1 Refreshed MountTool
12:16:17.264661501 sdh1 End.
It shows 8 copies of the script get queued up. As each script completes, it removes itself from the queue allowing the next script
to run. The partitions of  sdh  and  sdg  get mounted in the order they were entered into the queue. There are no race conditions.
After unmounting the partitions, here's what happens when you pull both thumb drives at the same time:
Code: [Select]
12:35:10.610996685 sdg4 Start Action=remove ID_FS_TYPE=vfat
12:35:10.615818654 sdg3 Start Action=remove ID_FS_TYPE=vfat
12:35:10.621543619 sdg2 Start Action=remove ID_FS_TYPE=ext4
12:35:10.622881169 sdg1 Start Action=remove ID_FS_TYPE=ntfs
12:35:10.628809615 sdg3 Arbitration complete.
12:35:10.641434926 sdg3 Removed.
12:35:10.646388256 sdg3 End.
12:35:10.752067627 sdg4 Arbitration complete.
12:35:10.793385203 sdh5 Start Action=remove ID_FS_TYPE=ntfs
12:35:10.801529633 sdg4 Removed.
12:35:10.803482879 sdh3 Start Action=remove ID_FS_TYPE=ext4
12:35:10.809238686 sdh1 Start Action=remove ID_FS_TYPE=vfat
12:35:10.813418339 sdh2 Start Action=remove ID_FS_TYPE=vfat
12:35:10.816413574 sdg4 End.
12:35:10.866580289 sdg2 Arbitration complete.
12:35:10.876581917 sdg2 Removed.
12:35:10.881357925 sdg2 End.
12:35:10.978851590 sdg1 Arbitration complete.
12:35:10.987204029 sdg1 Removed.
12:35:10.991936071 sdg1 End.
12:35:11.018089751 sdh5 Arbitration complete.
12:35:11.026619772 sdh5 Removed.
12:35:11.036442636 sdh5 End.
12:35:11.138708670 sdh3 Arbitration complete.
12:35:11.149585623 sdh3 Removed.
12:35:11.154353658 sdh3 End.
12:35:11.243734743 sdh1 Arbitration complete.
12:35:11.253554659 sdh1 Removed.
12:35:11.258268741 sdh1 End.
12:35:11.351155065 sdh2 Arbitration complete.
12:35:11.359571180 sdh2 Removed.
12:35:13.031303070 sdh2 Rebuildfstab.
12:35:13.034646793 sdh2 End.

Notes:
1. If the  MountTool  is running, the number of buttons and the mount state will be updated as devices are added/removed.
2. The script will abort if parted, gdisk, mkfs, or fsck are running so it doesn't interfere with them.
3. Mount points get added/removed from  /mnt  as devices are added/removed (except CD/DVD, they're permanent).
4. Diagnostic data is logged if syslog is running.
5. Enabling  DEBUGLOG  sends execution data as shown above to  /home/tc/automount.log.
6. If your card reader or CD/DVD won't automount, enable polling (see notes in script).
7. If you have a Teac All-In-One Multi-Card Reader and it won't recognize anything larger than 2GB, update the firmware to 4.08.
8. ntfs-3g.tcz is required for automounting NTFS devices.
9. This script requires  udev-extra.tcz  to function.

More info from the script:
Code: [Select]
#!/bin/sh
# Script to automount CDROMs and USB devices written by Richard A. Rost Sept. 27, 2019.
# We specify busybox commands wherever possible.
# ntfs-3g is required for mounting NTFS formatted devices.
#
# If your CD drive or your card reader devices won't automount, try this:
# echo 2000 | sudo tee /sys/module/block/parameters/events_dfl_poll_msecs
#
# or to enable automount for an individual device (sde for example):
# echo 2000 | sudo tee /sys/block/sde/events_poll_msecs
#
# Card reader info found here:
# https://unix.stackexchange.com/questions/38582/udev-triggers-are-not-firing-on-insert-of-cf-card-into-usb-card-reader-anymore
# http://ignorantguru.github.io/spacefm/spacefm-manual-en.html#devices-kernpoll
#
# Save this script to  /usr/local/sbin/automount.sh  and add it to your backup.
#
# Modify the  add/remove  lines in  /etc/udev/rules.d/98-tc.rules  as follows:
# ACTION=="add", SUBSYSTEM=="block", ENV{ID_FS_TYPE}=="?*" RUN+="/bin/sh -c '/usr/local/sbin/automount.sh %k'"
# ACTION=="remove", SUBSYSTEM=="block", ENV{ID_FS_TYPE}=="?*" RUN+="/bin/sh -c '/usr/local/sbin/automount.sh %k'"
# and add this for CDROMs:
# ACTION=="change", SUBSYSTEM=="block", ENV{ID_CDROM}=="1" RUN+="/bin/sh -c '/usr/local/sbin/automount.sh %k'"
« Last Edit: March 26, 2023, 12:45:23 AM by Rich »

Offline gadget42

  • Hero Member
  • *****
  • Posts: 817
Re: Automounting, card readers, CD drives, and udev
« Reply #1 on: January 23, 2022, 05:54:21 AM »
adding a reference link regarding the topic of "automounting"
(sorry for the necro-posting but better late than never)

regarding "automounting" please review the discussion at:
http://forum.tinycorelinux.net/index.php/topic,10602.0.html

also, at this time a forum search for "automounting" returns less than one page of results
(so fairly easy to get an idea of past thoughts on the subject)

sharing is caring
The fluctuation theorem has long been known for a sudden switch of the Hamiltonian of a classical system Z54 . For a quantum system with a Hamiltonian changing from... https://forum.tinycorelinux.net/index.php/topic,25972.msg166580.html#msg166580

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11679
Re: Automounting, card readers, CD drives, and udev
« Reply #2 on: March 26, 2023, 01:10:37 AM »
Update: Added note  "9. This script requires  udev-extra.tcz  to function."  to original post.

After adding  automount.sh  to a TC13 install on a laptop I found it was not getting
called because udev was not generating  ENV{ID_FS_TYPE}.

Turns out  60-persistent-storage.rules  which is part of  udev-extra.tcz  runs  blkid -o udev -p
which generates  ENV{ID_FS_TYPE}.