Skip to content

Instantly share code, notes, and snippets.

@euikook
Last active March 10, 2021 13:03
Show Gist options
  • Save euikook/5bed603d4963dad2949df8136f41447e to your computer and use it in GitHub Desktop.
Save euikook/5bed603d4963dad2949df8136f41447e to your computer and use it in GitHub Desktop.
auto-mount-removable-drives-to-specific-mounting-point
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"
blkid /dev/sda2
/dev/sda2: UUID="ef6e4eba-7ad9-4a4c-be75-3b85ba2d14a3" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="39b52ccd-5972-4bb7-ba9b-279441d990df"
sudo chmod +x /usr/local/bin/usb-mount`
sudo systemctl daemon-reload
# Disk from USB
UUID=ef6e4eba-7ad9-4a4c-be75-3b85ba2d14a3 /home/data ext4 noauto,rw,relatime 0 2
sudo udevadm control --reload-rules && sudo udevadm trigger
sudo umount /dev/sda2
#!/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
[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