-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathswap.sh
More file actions
162 lines (132 loc) · 4.82 KB
/
Copy pathswap.sh
File metadata and controls
162 lines (132 loc) · 4.82 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/bin/sh
# Made by Jack'lul <jacklul.github.io>
#
# Enable swap file on startup
#
# Based on:
# https://github.com/decoderman/amtm/blob/master/amtm_modules/swap.mod
#
#jas-update=swap.sh
#shellcheck shell=ash
#shellcheck disable=SC2155
#shellcheck source=./common.sh
readonly common_script="$(dirname "$0")/common.sh"
if [ -f "$common_script" ]; then . "$common_script"; else { echo "$common_script not found" >&2; exit 1; } fi
SWAP_FILE="" # swap file path, like /tmp/mnt/USBDEVICE/swap.img, leave empty to search for it in /tmp/mnt/*/swap.img
SWAP_SIZE=1048576 # swap file size, changing after swap is created requires it to be manually removed, 1048576 = 1GB
SWAPPINESS= # change the value of vm.swappiness (/proc/sys/vm/swappiness), if left empty it will not be changed
load_script_config
find_swap_file() {
local _dir
for _dir in /tmp/mnt/*; do
if [ -d "$_dir" ] && [ -f "$_dir/swap.img" ]; then
SWAP_FILE="$_dir/swap.img"
return
fi
done
}
create_swap() {
[ -z "$SWAP_FILE" ] && { logecho "Error: SWAP_FILE is not set" error; exit 1; }
[ -z "$SWAP_SIZE" ] && { logecho "Error: SWAP_SIZE is not set" error; exit 1; }
{ [ -f "$SWAP_FILE" ] && grep -Fq "$(readlink -f "$SWAP_FILE")" /proc/swaps ; } && { logecho "Error: Swap file is mounted" error; exit 1; }
set -e
logecho "Creating swap file... ($SWAP_SIZE KB)"
touch "$SWAP_FILE"
dd if=/dev/zero of="$SWAP_FILE" bs=1k count="$SWAP_SIZE"
mkswap "$SWAP_FILE"
chmod 640 "$SWAP_FILE"
set +e
}
enable_swap() {
lockfile lockexit
if ! grep -Fq "file" /proc/swaps; then
[ -z "$SWAP_FILE" ] && find_swap_file
if [ -n "$SWAP_FILE" ] && [ -d "$(dirname "$SWAP_FILE")" ] && [ ! -f "$SWAP_FILE" ]; then
create_swap
fi
if [ -f "$SWAP_FILE" ]; then
if swapon "$SWAP_FILE" ; then
#shellcheck disable=SC2012
logecho "Enabled swap file '$SWAP_FILE' ($(/bin/ls -lh "$SWAP_FILE" | awk '{print $5}'))" alert
if [ -n "$SWAPPINESS" ]; then
echo "$SWAPPINESS" > /proc/sys/vm/swappiness
logecho "Set swappiness to: $SWAPPINESS" alert
fi
else
logecho "Failed to enable swap on '$SWAP_FILE'" error
fi
fi
fi
# No matter what the result here is, unset the cron entry if hotplug-event script is available
execute_script_basename "hotplug-event.sh" check && crontab_entry delete
lockfile unlock
}
disable_swap() {
[ -z "$1" ] && return
local _swap_file="$1"
sync
echo 3 > /proc/sys/vm/drop_caches
if swapoff "$_swap_file" ; then
logecho "Disabled swap on '$_swap_file'" alert
else
logecho "Failed to disable swap on '$_swap_file'" error
fi
}
case "$1" in
"run")
enable_swap
;;
"hotplug")
if [ "$SUBSYSTEM" = "block" ] && [ -n "$DEVICENAME" ]; then
case "$ACTION" in
"add")
grep -Fq "file" /proc/swaps && exit 0 # do nothing if swap is already enabled
timeout=60
while ! df | grep -Fq "/dev/$DEVICENAME" && [ "$timeout" -ge 0 ]; do
[ "$timeout" -lt 60 ] && { echo "Device is not yet mounted, waiting 5 seconds..."; sleep 5; }
timeout=$((timeout-5))
done
if df | grep -Fq "/dev/$DEVICENAME"; then
enable_swap
exit
fi
[ "$timeout" -le 0 ] && logecho "Device '/dev/$DEVICENAME' did not mount within 60 seconds" error
;;
"remove")
# officially multiple swap files are not supported but try to handle it...
swap_files=$(grep -F 'file' /proc/swaps | awk '{print $1}')
for swap_file in $swap_files; do
if [ ! -e "$swap_file" ]; then
disable_swap "$swap_file"
fi
done
;;
esac
fi
;;
"create")
[ -n "$2" ] && SWAP_FILE="$2"
[ -n "$3" ] && SWAP_SIZE="$3"
create_swap
;;
"start")
[ "$(nvram get usb_idle_enable)" != "0" ] && { logecho "Error: USB idle timeout is set" error; exit 1; }
crontab_entry add "*/1 * * * * $script_path run"
enable_swap
;;
"stop")
crontab_entry delete
[ -z "$SWAP_FILE" ] && find_swap_file
if [ -n "$SWAP_FILE" ] && grep -Fq "$SWAP_FILE" /proc/swaps; then
disable_swap "$SWAP_FILE"
fi
;;
"restart")
sh "$script_path" stop
sh "$script_path" start
;;
*)
echo "Usage: $0 run|start|stop|restart|create"
exit 1
;;
esac