Last active
March 10, 2021 13:03
-
-
Save euikook/5bed603d4963dad2949df8136f41447e to your computer and use it in GitHub Desktop.
auto-mount-removable-drives-to-specific-mounting-point
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
ACTION=="add", SUBSYSTEM=="block", ENV{ID_FS_UUID}=="ef6e4eba-7ad9-4a4c-be75-3b85ba2d14a3", ENV{UDISKS_IGNORE}="1", RUN+="/usr/bin/systemctl start usb.mount@$env{ID_FS_UUID}.service" | |
ACTION=="remove", SUBSYSTEM=="block", ENV{ID_FS_UUID}=="ef6e4eba-7ad9-4a4c-be75-3b85ba2d14a3", ENV{UDISKS_IGNORE}="1", RUN+="/usr/bin/systemctl stop usb.mount@$env{ID_FS_UUID}.service" |
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
blkid /dev/sda2 | |
/dev/sda2: UUID="ef6e4eba-7ad9-4a4c-be75-3b85ba2d14a3" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="39b52ccd-5972-4bb7-ba9b-279441d990df" |
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
blkid |
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
sudo chmod +x /usr/local/bin/usb-mount` |
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
sudo systemctl daemon-reload |
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
# Disk from USB | |
UUID=ef6e4eba-7ad9-4a4c-be75-3b85ba2d14a3 /home/data ext4 noauto,rw,relatime 0 2 |
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
sudo udevadm control --reload-rules && sudo udevadm trigger |
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
sudo umount /dev/sda2 |
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
#!/bin/bash | |
UUID=$2 | |
DEVF=$(blkid --uuid ${UUID}) | |
[[ -n ${DEVF} ]] || { | |
echo " No such disk partition from ${UUID}" | |
exit -1 | |
} | |
# Check partition already mounted | |
MOUNTED=$(/bin/mount | /bin/grep ${DEVF} | /usr/bin/awk '{ print $3 }') | |
start() { | |
[[ -n ${MOUNTED} ]] || { | |
/bin/mount -U ${UUID} | |
} | |
} | |
stop() | |
{ | |
[[ -n ${MOUNTED} ]] && { | |
/bin/umount -l ${DEVF} | |
} || exit 0 | |
} | |
status() { | |
/bin/mount | /bin/grep ${DEVF} || exit 0 | |
} | |
case "${1:-status}" in | |
start) | |
start | |
;; | |
stop) | |
stop | |
;; | |
restart) | |
stop | |
start | |
;; | |
status) | |
status | |
;; | |
*) | |
echo "Usage: $0 {start|stop|restart|status}" >&2 | |
exit 1 | |
;; | |
esac |
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
[Unit] | |
Description=Mount USB Disk %i | |
[Service] | |
Type=oneshot | |
RemainAfterExit=true | |
ExecStart=/usr/local/bin/usb.mount start %i | |
ExecStop=/usr/local/bin/usb.mount stop %i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment