-
Notifications
You must be signed in to change notification settings - Fork 11
/
netboot-download.sh
94 lines (79 loc) · 2.86 KB
/
netboot-download.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
#!/bin/sh
# Made by Jack'lul <jacklul.github.io>
#
# Download files from netboot.xyz to specified directory
#
# Based on:
# https://github.com/RMerl/asuswrt-merlin.ng/wiki/Enable-PXE-booting-into-netboot.xyz
#
#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")"
FILES="netboot.xyz.efi netboot.xyz.kpxe" # what files to download, space separated
DIRECTORY="/tmp/netboot.xyz" # where to save the files
BASE_URL="https://boot.netboot.xyz/ipxe/" # base download URL, with ending slash
if [ -f "$SCRIPT_CONFIG" ]; then
#shellcheck disable=SC1090
. "$SCRIPT_CONFIG"
fi
CURL_BINARY="curl"
[ -f /opt/bin/curl ] && CURL_BINARY="/opt/bin/curl"
is_started_by_system() { #ISSTARTEDBYSYSTEM_START#
_PPID=$PPID
while true; do
[ -z "$_PPID" ] && break
_PPID=$(< "/proc/$_PPID/stat" awk '{print $4}')
grep -q "cron" "/proc/$_PPID/comm" && return 0
grep -q "hotplug" "/proc/$_PPID/comm" && return 0
[ "$_PPID" -gt "1" ] || break
done
return 1
} #ISSTARTEDBYSYSTEM_END#
case "$1" in
"run")
{ [ "$(nvram get wan0_state_t)" != "2" ] && [ "$(nvram get wan1_state_t)" != "2" ]; } && { echo "WAN network is not connected"; exit; }
[ -z "$($CURL_BINARY -fs "https://boot.netboot.xyz")" ] && { echo "Cannot reach boot.netboot.xyz"; exit 1; }
[ ! -d "$DIRECTORY" ] && mkdir -p "$DIRECTORY"
DOWNLOADED=""
FAILED=""
for FILE in $FILES; do
[ -f "$DIRECTORY/$FILE" ] && continue
if $CURL_BINARY -fsSL "$BASE_URL$FILE" -o "$DIRECTORY/$FILE" && [ -f "$DIRECTORY/$FILE" ]; then
DOWNLOADED="$DOWNLOADED $FILE"
else
FAILED="$FAILED $FILE"
fi
done
[ -n "$DOWNLOADED" ] && logger -st "$SCRIPT_TAG" "Downloaded files from netboot.xyz:$DOWNLOADED"
[ -n "$FAILED" ] && logger -st "$SCRIPT_TAG" "Failed to downloaded files from netboot.xyz:$FAILED"
[ -z "$FAILED" ] && sh "$SCRIPT_PATH" stop
;;
"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
if is_started_by_system; then
{
sh "$SCRIPT_PATH" run
} &
fi
;;
"stop")
[ -x "$SCRIPT_DIR/cron-queue.sh" ] && sh "$SCRIPT_DIR/cron-queue.sh" remove "$SCRIPT_NAME"
cru d "$SCRIPT_NAME"
;;
"restart")
sh "$SCRIPT_PATH" stop
sh "$SCRIPT_PATH" start
;;
*)
echo "Usage: $0 run|start|stop|restart"
exit 1
;;
esac