forked from wslutilities/wslu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwslact.sh
110 lines (94 loc) · 3.18 KB
/
wslact.sh
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
98
99
100
101
102
103
104
105
106
107
108
109
110
# shellcheck shell=bash
help_short="wslact COMMAND ..."
function time_reset {
local help_short="wslact time-reset [-h]"
while [ "$1" != "" ]; do
case "$1" in
-h|--help) help "wslact" "$help_short"; exit;;
*) shift;;
esac
done
if [ "$EUID" -ne 0 ]; then
error_echo "\`wslact time-sync\` requires you to run as root. Aborted." 1
fi
echo "${info} Before Sync: $(date +"%d %b %Y %T %Z")"
if date -s "$(winps_exec "Get-Date -UFormat \"%m/%d/%Y %T %Z\"" | tr -d "\r")" >/dev/null; then
echo "${info} After Sync: $(date +"%d %b %Y %T %Z")"
echo "${info} Manual Time Reset Complete."
else
error_echo "Time Sync failed." 1
fi
}
function auto_mount {
local help_short="wslact auto-mount [-mh]"
mntpt_prefix="$(interop_prefix)"
sysdrv_prefix="$(sysdrive_prefix)"
mount_opt=""
while [ "$1" != "" ]; do
case "$1" in
-m|--mount-options) shift; mount_opt="$1"; shift;;
-h|--help) help "wslact" "$help_short"; exit;;
*) shift;;
esac
done
if [ "$EUID" -ne 0 ]; then
error_echo "\`wslact auto-mount\` requires you to run as root. Aborted." 1
fi
#shellcheck disable=SC1003
drive_list="$("$mntpt_prefix$sysdrv_prefix"/WINDOWS/system32/fsutil.exe fsinfo drives | tail -1 | tr '[:upper:]' '[:lower:]' | tr -d ':\\' | sed -e 's/drives //g' -e "s|$sysdrv_prefix ||g" -e 's|\r||g' -e 's| $||g' -e 's| |\n|g')"
if [ -n "$mount_opt" ]; then
echo "${info} Custom mount option detected: $mount_opt"
elif [ -f /etc/wsl.conf ]; then
tmp="$(grep ^options /etc/wsl.conf | sed -r -e 's|^options[ ]+=[ ]+||g' -e 's|^"||g' -e 's|"$||g')"
if [ "$tmp" != "" ]; then
echo "${info} Custom mount option detected: $tmp"
mount_opt="$tmp"
unset tmp
fi
fi
mount_s=0
mount_f=0
mount_j=0
for drive in $drive_list; do
[[ -d "$mntpt_prefix$drive" ]] || mkdir -p "$mntpt_prefix$drive"
if [[ -n $(find "$mntpt_prefix$drive" -maxdepth 0 -type d -empty 2>/dev/null) ]]; then
echo "${info} Mounting Drive ${drive^} to $mntpt_prefix$drive..."
if mount -t drvfs "${drive}:" "$mntpt_prefix$drive" -o "$mount_opt" 2>/dev/null; then
echo "${info} Mounted Drive ${drive^} to $mntpt_prefix$drive."
mount_s=$((mount_s + 1))
else
echo "${error} Failed to mount Drive ${drive^}. Skipped."
mount_f=$((mount_f + 1))
fi
else
echo "${warn} Already mounted Drive ${drive^} at $mntpt_prefix$drive. Skipped."
mount_j=$((mount_j + 1))
fi
done
echo "${info} Auto mounting completed. $mount_s drive(s) succeed. $mount_f drive(s) failed. $mount_j drive(s) skipped."
}
function memory_reclaim {
local help_short="wslact memory-reclaim [-h]"
while [ "$1" != "" ]; do
case "$1" in
-h|--help) help "wslact" "$help_short"; exit;;
*) shift;;
esac
done
if [ "$EUID" -ne 0 ]; then
error_echo "\`wslact memory-reclaim\` requires you to run as root. Aborted." 1
fi
sync
echo 1 > /proc/sys/vm/drop_caches
echo "${info} Memory Reclaimed."
}
while [ "$1" != "" ]; do
case "$1" in
ts|time-sync|tr|time-reset) time_reset "$@"; exit;;
am|auto-mount|sm|smart-mount) auto_mount "$@"; exit;;
mr|memory-reclaim|mem-reclaim) memory_reclaim "$@"; exit;;
-h|--help) help "$0" "$help_short"; exit;;
-v|--version) version; exit;;
*) error_echo "Invalid Input. Aborted." 22;;
esac
done