Last active
January 20, 2022 04:49
-
-
Save omrdk/c25911b7cc7d922d985c30c44cd1f00c to your computer and use it in GitHub Desktop.
Hotplugging usb for linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# HOTPLUGGING W/ UDEV | |
[Udev rule] | |
// Create udev rule file | |
sudo touch /etc/udev/rules.d/66-hotplug.rules | |
// Fill the file with: | |
KERNEL=="sd[a-z][0-9]*", SUBSYSTEM=="block", ACTION=="add", RUN+="/usr/local/bin/mount.sh &" | |
KERNEL=="sd[a-z][0-9]*", SUBSYSTEM=="block", ACTION=="remove", RUN+="/usr/local/bin/umount.sh &" | |
// Scripts will be called whenever you plug/unplug(add/remove) usb. | |
[Mount/Umount scripts] | |
// create mount.sh & umount.sh: | |
sudo touch /usr/local/bin/mount.sh /usr/local/bin/umount.sh | |
// Fill mount.sh with: | |
#!/bin/sh | |
mount /dev/sda1 /mnt/tmp | |
// Fill umount.sh with: | |
#!/bin/sh | |
umount /dev/sda1 /mnt/tmp | |
// Give executable permission to these files: | |
sudo chmod +x /usr/local/bin/mount.sh /usr/local/bin/umount.sh | |
The original udev command has been replaced by systemd-udevd (see its man page). One of the differences is that it creates its own filesystem namespace, so your mount is done, but it is not visible in the principal namespace. Check with: | |
// Get pid of service | |
systemctl status systemd-udevd | |
// See if it is detected | |
cat /proc/<pid-of-systemd-udevd>/mounts | grep sda | |
// Edit the service file: | |
sudo systemctl edit systemd-udevd | |
// insert to it: | |
[Service] | |
MountFlags=shared (on Ubuntu 20.04, replace MountFlags=shared with PrivateMounts=no.) | |
// Reload systemd manager configuration | |
sudo systemctl daemon-reload | |
// Restart service | |
sudo service systemd-udevd --full-restart | |
check with df cmd | |
Tested both 18.04 & 20.04 Ubuntu | |
[For more] | |
https://unix.stackexchange.com/questions/330094/udev-rule-to-mount-disk-does-not-work |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment