-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautomount
More file actions
executable file
·97 lines (92 loc) · 3.29 KB
/
automount
File metadata and controls
executable file
·97 lines (92 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#! /usr/bin/env bash
# cspell:ignore jmor automount jmort
readonly AUTO_BINDS_PATH="/home/jmor/reposLocal/custom-shell-scripts/automount_binds.tsv"
readonly WINDOWS_PATH='/mnt/win10'
readonly WINDOWS_PARTITION='/dev/sda3'
readonly DEFAULT_BIND_OPTIONS='defaults,nofail,x-systemd.device-timeout=2,windows_names,noatime'
readonly LOG_FILE='/home/jmor/Desktop/autoLog.txt'
##region########################################### SHOW HELP & VERSION ###########################################
show_help() {
cat << __EOF__
Automatically mounts the Windows partition ($WINDOWS_PARTITION) to '$WINDOWS_PATH', and then binds the contents of '$AUTO_BINDS_PATH'.
Usage: $0
or: $0 --help
or: $0 --version
Options:
-h, --help: Shows this help text.
-v, --version: Shows version info.
__EOF__
}
show_version() {
cat << __EOF__
$0 3.0.0
Copyright (C) 2025 Justin Morris
__EOF__
}
##endregion######################################## SHOW HELP & VERSION ###########################################
{ [ "$1" = '--help' ] || [ "$1" = '-h' ]; } && { show_help; exit; }
{ [ "$1" = '--version' ] || [ "$1" = '-v' ]; } && { show_version; exit; }
date +%c > "$LOG_FILE"
declare -i ec=0
if cd '/mnt/win10/Users/' &>"/dev/null"; then
if [ "$1" = '-y' ] || [ "$1" = '-b' ] || [ "$1" = '--bind' ]; then
echo 'Already mounted. Proceeding with binds...'
else
read -r -N 1 -p 'Already mounted. Proceed with binds? (y/n)'
if [ "$REPLY" = 'y' ] || [ "$REPLY" = 'Y' ]; then
echo $'\nProceeding...'
else
exit
fi
fi
else
sudo mount -t ntfs-3g -o defaults,uid=jmor,nofail,x-systemd.device-timeout=2,windows_names,noatime "$WINDOWS_PARTITION" "$WINDOWS_PATH" || exit $?
fi
readonly ORIG_IFS="$IFS"
declare -a BIND_SOURCE=(); declare -a BIND_DEST=(); declare -a BIND_OPTS=()
do_read() {
local b_source=''; local b_destination=''; local b_opts=''
# Get TSV column labels out of the way
read -sr b_source b_destination b_opts
while read -s -t 0; do
read -sr b_source b_destination b_opts
[[ "$b_destination" =~ \t$ ]] && echo "Trailing tab removed manually" && b_destination="${b_destination:0:-1}"
if (( ${#b_source} <= 0 || ${#b_destination} <= 0 )); then break; fi
[[ "$b_source" =~ /$ ]] && b_source="${b_source:0:-1}"
[[ "$b_destination" =~ /$ ]] && b_destination="${b_destination:0:-1}"
BIND_SOURCE+=("$b_source")
BIND_DEST+=("$b_destination")
if ((${#b_opts} <= 0)); then
BIND_OPTS+=("$DEFAULT_BIND_OPTIONS")
elif [ "$b_opts" = '--' ]; then
BIND_OPTS+=('')
else
BIND_OPTS+=("$b_opts")
fi
done
# echo $?
return
}
IFS=$'\t'
do_read < "$AUTO_BINDS_PATH"
IFS="$ORIG_IFS"
readonly BIND_SOURCE BIND_DEST
shopt -s extglob globasciiranges patsub_replacement
readonly SPACE_LITERAL=' '
src=''; dest=''; opts=''
for ((i = 0, ec = 0; ec == 0 && i < ${#BIND_SOURCE[@]}; ec=$?, i += ec == 0 ? 1 : 0)); do
src="${BIND_SOURCE[$i]}"
dest="${BIND_DEST[$i]}"
opts="${BIND_OPTS[$i]}"
if [[ "$(findmnt -r --target "$dest" -o TARGET,SOURCE)" =~ ^$'TARGET SOURCE\n'(.+)$ ]] && [[ "${BASH_REMATCH[1]}" =~ (^|$'\n')$dest$SPACE_LITERAL([^$'\n']*) ]]; then
echo "Skipping $dest (already mounted to ${BASH_REMATCH[2]})"
continue
fi
if ((${#opts} <= 0)); then
sudo mount --bind "$src" "$dest"
else
sudo mount --bind -o "$opts" "$src" "$dest"
fi
done
if (( i < ${#BIND_SOURCE[@]} )); then echo "Failed at binding $src to $dest ($ec)"; fi
exit $ec