forked from jacklul/asuswrt-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swap.sh
174 lines (147 loc) · 5.13 KB
/
swap.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
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
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/sh
# Made by Jack'lul <jacklul.github.io>
#
# Setup and enable swap file on startup
#
# Based on:
# https://github.com/decoderman/amtm/blob/master/amtm_modules/swap.mod
#
#jacklul-asuswrt-scripts-update
#shellcheck disable=SC2155
readonly SCRIPT_PATH="$(readlink -f "$0")"
readonly SCRIPT_NAME="$(basename "$SCRIPT_PATH" .sh)"
readonly SCRIPT_DIR="$(dirname "$SCRIPT_PATH")"
readonly SCRIPT_CONFIG="$SCRIPT_DIR/$SCRIPT_NAME.conf"
readonly SCRIPT_TAG="$(basename "$SCRIPT_PATH")"
SWAP_FILE="" # swap file path, like /tmp/mnt/USBDEVICE/swap.img, leave empty to search for it in /tmp/mnt/*/swap.img
SWAP_SIZE=128000 # swap file size, changing after swap is created requires it to be manually removed, 128000 = 128MB
if [ -f "$SCRIPT_CONFIG" ]; then
#shellcheck disable=SC1090
. "$SCRIPT_CONFIG"
fi
if [ -z "$SWAP_FILE" ]; then
for DIR in /tmp/mnt/*; do
if [ -d "$DIR" ] && [ -f "$DIR/swap.img" ]; then
SWAP_FILE="$DIR/swap.img"
break
fi
done
fi
lockfile() { #LOCKFILE_START#
_LOCKFILE="/var/lock/script-$SCRIPT_NAME.lock"
_PIDFILE="/var/run/script-$SCRIPT_NAME.pid"
_FD=100
_FD_MAX=200
if [ -n "$2" ]; then
_LOCKFILE="/var/lock/script-$SCRIPT_NAME-$2.lock"
_PIDFILE="/var/run/script-$SCRIPT_NAME-$2.lock"
fi
[ -n "$3" ] && [ "$3" -eq "$3" ] && _FD="$3" && _FD_MAX="$3"
[ -n "$4" ] && [ "$4" -eq "$4" ] && _FD_MAX="$4"
[ ! -d /var/lock ] && mkdir -p /var/lock
[ ! -d /var/run ] && mkdir -p /var/run
_LOCKPID=
[ -f "$_PIDFILE" ] && _LOCKPID="$(cat "$_PIDFILE")"
case "$1" in
"lockwait"|"lockfail"|"lockexit")
while [ -f "/proc/$$/fd/$_FD" ]; do
echo "File descriptor $_FD is already in use ($(readlink -f "/proc/$$/fd/$_FD"))"
_FD=$((_FD+1))
[ "$_FD" -gt "$_FD_MAX" ] && exit 1
done
eval exec "$_FD>$_LOCKFILE"
case "$1" in
"lockwait")
flock -x "$_FD"
;;
"lockfail")
flock -nx "$_FD" || return 1
;;
"lockexit")
flock -nx "$_FD" || exit 1
;;
esac
echo $$ > "$_PIDFILE"
trap 'flock -u $_FD; rm -f "$_LOCKFILE" "$_PIDFILE"; exit $?' INT TERM EXIT
;;
"unlock")
flock -u "$_FD"
rm -f "$_LOCKFILE" "$_PIDFILE"
trap - INT TERM EXIT
;;
"check")
[ -n "$_LOCKPID" ] && [ -f "/proc/$_LOCKPID/stat" ] && return 0
return 1
;;
"kill")
[ -n "$_LOCKPID" ] && [ -f "/proc/$_LOCKPID/stat" ] && kill -9 "$_LOCKPID" && return 0
return 1
;;
esac
} #LOCKFILE_END#
case "$1" in
"run")
lockfile lockexit
if [ "$(nvram get usb_idle_enable)" != "0" ]; then
logger -st "$SCRIPT_TAG" "Unable to enable swap - USB Idle timeout is set"
[ -x "$SCRIPT_DIR/cron-queue.sh" ] && sh "$SCRIPT_DIR/cron-queue.sh" remove "$SCRIPT_NAME"
cru d "$SCRIPT_NAME"
else
if ! grep -q "file" /proc/swaps; then
if [ -d "$(dirname "$SWAP_FILE")" ] && [ ! -f "$SWAP_FILE" ]; then
sh "$SCRIPT_PATH" create
fi
if [ -f "$SWAP_FILE" ]; then
if swapon "$SWAP_FILE" ; then
#shellcheck disable=SC2012
logger -st "$SCRIPT_TAG" "Enabled swap on $SWAP_FILE ($(ls -hs "$SWAP_FILE" | awk '{print $1}'))"
else
logger -st "$SCRIPT_TAG" "Failed to enable swap on $SWAP_FILE"
fi
fi
fi
fi
lockfile unlock
;;
"create")
[ -n "$2" ] && SWAP_FILE="$2"
[ -n "$3" ] && SWAP_SIZE="$3"
[ -z "$SWAP_FILE" ] && { logger -st "$SCRIPT_TAG" "Swap file is not set"; exit 1; }
[ -z "$SWAP_SIZE" ] && { logger -st "$SCRIPT_TAG" "Swap size is not set"; exit 1; }
set -e
echo "Creating swap file..."
touch "$SWAP_FILE"
dd if=/dev/zero of="$SWAP_FILE" bs=1k count="$SWAP_SIZE"
mkswap "$SWAP_FILE"
set +e
;;
"start")
if [ -x "$SCRIPT_DIR/cron-queue.sh" ]; then
sh "$SCRIPT_DIR/cron-queue.sh" add "$SCRIPT_NAME" "$SCRIPT_PATH run"
else
cru a "$SCRIPT_NAME" "*/1 * * * * $SCRIPT_PATH run"
fi
sh "$SCRIPT_PATH" run
;;
"stop")
[ -x "$SCRIPT_DIR/cron-queue.sh" ] && sh "$SCRIPT_DIR/cron-queue.sh" remove "$SCRIPT_NAME"
cru d "$SCRIPT_NAME"
if [ -n "$SWAP_FILE" ]; then
sync
echo 3 > /proc/sys/vm/drop_caches
if swapoff "$SWAP_FILE" ; then
logger -st "$SCRIPT_TAG" "Disabled swap on $SWAP_FILE"
else
logger -st "$SCRIPT_TAG" "Failed to disable swap on $SWAP_FILE"
fi
fi
;;
"restart")
sh "$SCRIPT_PATH" stop
sh "$SCRIPT_PATH" start
;;
*)
echo "Usage: $0 run|start|stop|restart|create"
exit 1
;;
esac