Created
December 28, 2017 05:13
-
-
Save Dobbie03/0f1c7843333a37af7285c6cdbd9bcb14 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/usr/bin/env bash | |
| # Daemon tool to mount/unmount drives | |
| # Written by Nathaniel Maia, December 2017 | |
| # List of UUIDs | |
| DRIVES=( | |
| DEBC8E9CBC8E6EB9 | |
| ) | |
| ############################################################# | |
| ######### | |
| # if passed --remove or -r, unmount drives and exit | |
| if [[ "$1" = "-r" ]] || [[ "$1" = "--remove" ]]; then | |
| for d in ${DRIVES[@]}; do | |
| NAME=$(awk '{print $1}' <<< $(grep $d <<< $(lsblk -lno NAME,UUID))) | |
| MNTP=$(awk '{print $2}' <<< $(grep $d <<< $(lsblk -lno UUID,MOUNTPOINT))) | |
| if [[ "$NAME" ]] && [[ "$MNTP" ]]; then | |
| udisksctl unmount -b /dev/$NAME | |
| fi | |
| done | |
| # kill existing mounter instances to stop them re-mounting | |
| PID=$(awk '{print $1}' <<< $(grep 'mounter' <<< $(pgrep -a bash))) | |
| if [[ "$PID" ]]; then | |
| kill $PID | |
| fi | |
| else | |
| # infinite 'daemon' loop looking for drives | |
| while true; do | |
| # improve performance with secondary loop | |
| # while no new devices are detected sleep 30s | |
| OLD=$(wc -l <<< $(grep -i 'usb [0-9].*: new' <<< $(dmesg))) | |
| while [[ "$CUR" ]] && [ $CUR -eq $OLD ]; do | |
| sleep 30 | |
| CUR=$(wc -l <<< $(grep -i 'usb [0-9].*: new' <<< $(dmesg))) | |
| done | |
| # Check for device UUID, if not mounted, mount it | |
| for d in ${DRIVES[@]}; do | |
| NAME=$(awk '{print $1}' <<< $(grep $d <<< $(lsblk -lno NAME,UUID))) | |
| MNTP=$(awk '{print $2}' <<< $(grep $d <<< $(lsblk -lno UUID,MOUNTPOINT))) | |
| if [[ "$NAME" ]] && ! [[ "$MNTP" ]]; then | |
| udisksctl mount -b /dev/$NAME | |
| fi | |
| done | |
| # Update CUR | |
| CUR=$OLD | |
| done | |
| fi | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment