-
Notifications
You must be signed in to change notification settings - Fork 23
/
create-autorun.sh
executable file
·480 lines (346 loc) · 11.2 KB
/
create-autorun.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#!/usr/bin/env bash
####################################################################################
# create-autorun.sh
#
# Copyright (C) 2017-2023 OneCD - [email protected]
#
# Create an autorun environment suited to this model QNAP NAS
#
# For more info: https://forum.qnap.com/viewtopic.php?f=45&t=130345
#
# Project source: https://github.com/OneCDOnly/create-autorun
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see http://www.gnu.org/licenses/
####################################################################################
readonly USER_ARGS_RAW=$*
Init()
{
local -r SCRIPT_FILE=create-autorun.sh
local -r SCRIPT_VERSION=230527
exitcode=0
# include QNAP functions
if [[ -e /etc/init.d/functions ]]; then
. /etc/init.d/functions
else
ShowAsError 'QNAP OS functions missing (is this a QNAP NAS?): aborting ...'
exitcode=1
return
fi
if [[ $EUID -ne 0 ]]; then
ShowAsError 'this script must be run with superuser privileges. Try again as:'
echo 'curl -skL https://git.io/create-autorun | sudo bash'
exitcode=2
return
fi
FindDefVol
readonly NAS_ARC=$(</etc/default_config/BOOT.conf)
readonly NAS_DEV_NODE=$(/sbin/getcfg 'CONFIG STORAGE' DEVICE_NODE -f /etc/platform.conf)
readonly NAS_AUTORUN_PART=$(/sbin/getcfg 'CONFIG STORAGE' FS_ACTIVE_PARTITION -f /etc/platform.conf)
readonly NAS_AUTORUN_FS=$(/sbin/getcfg 'CONFIG STORAGE' FS_TYPE -f /etc/platform.conf)
readonly NAS_SYSTEM_DEV=$(/sbin/getcfg System 'System Device' -f /etc/config/uLinux.conf)
readonly AUTORUN_FILE=autorun.sh
readonly AUTORUN_PATH=$DEF_VOLMP/.system/autorun
readonly SCRIPT_STORE_PATH=$AUTORUN_PATH/scripts
readonly MOUNT_BASE_PATH=/tmp/${SCRIPT_FILE%.*}
readonly AUTORUN_PATHFILE=$AUTORUN_PATH/$AUTORUN_FILE
autorun_partition=''
partition_mounted=false
script_store_created=false
echo "$(ColourTextBrightWhite "$SCRIPT_FILE") $SCRIPT_VERSION"
echo
ShowAsInfo "NAS model: $(get_display_name)"
ShowAsInfo "$(GetQnapOS) version: $(/sbin/getcfg System Version) #$(/sbin/getcfg System 'Build Number')"
ShowAsInfo "default volume: $DEF_VOLMP"
}
DetermineAutorunPartitionLocation()
{
[[ $exitcode -eq 0 ]] || return
if [[ -n $NAS_DEV_NODE ]]; then
autorun_partition=${NAS_DEV_NODE}${NAS_AUTORUN_PART}
else
if [[ -e /sbin/hal_app ]]; then
if /bin/grep -q zfs /proc/filesystems; then # NAS is running QuTS
autorun_partition=$NAS_SYSTEM_DEV
else
autorun_partition=$(/sbin/hal_app --get_boot_pd port_id=0)
fi
case $(/sbin/getcfg System Model) in
TS-X16|TS-X28A|TS-XA28A|TS-X33|TS-X35EU)
autorun_partition+=5
;;
*)
autorun_partition+=6
esac
elif [[ $NAS_ARC = TS-NASARM ]]; then
autorun_partition=/dev/mtdblock5
else
autorun_partition=/dev/sdx6
fi
fi
if [[ -n $autorun_partition ]]; then
ShowAsInfo "autorun partition should be: $autorun_partition"
return
fi
ShowAsError 'unable to determine the autorun partition location'
exitcode=3
}
CreateMountPoint()
{
[[ $exitcode -eq 0 ]] || return
mount_point=$(/bin/mktemp -d $MOUNT_BASE_PATH.XXXXXX 2>/dev/null)
[[ $? -eq 0 ]] && return
ShowAsError "unable to create a temporary mount-point ($MOUNT_BASE_PATH.XXXXXX)"
exitcode=4
}
MountAutorunPartition()
{
[[ $exitcode -eq 0 ]] || return
local mount_dev=$autorun_partition
local result_msg=''
if [[ $NAS_AUTORUN_FS = ubifs ]]; then
result_msg=$(/sbin/ubiattach -m "$NAS_AUTORUN_PART" -d 2 2>/dev/null)
if [[ $? -eq 0 ]]; then
ShowAsDone "ubiattached partition: $NAS_AUTORUN_PART"
mount_type=ubifs
mount_dev=ubi2:config
else
ShowAsSkip "unable to ubiattach"
mount_type=ext4
mount_dev=/dev/mmcblk0p7
ShowAsInfo "will try as: $mount_type instead"
fi
else
mount_type=ext2
fi
result_msg=$(/bin/mount -t $mount_type $mount_dev "$mount_point" 2>&1)
if [[ $? -eq 0 ]]; then
ShowAsDone "mounted: $mount_type device: $mount_dev -> $mount_point"
partition_mounted=true
return
fi
ShowAsError "unable to mount: $mount_type device: $mount_dev '$result_msg'"
partition_mounted=false
exitcode=5
}
ConfirmAutorunPartition()
{
[[ $exitcode -eq 0 ]] || return
# Look for a known file to confirm this is the autorun partition.
# Include an alternative file to confirm autorun partition on QuTS. https://github.com/OneCDOnly/create-autorun/issues/10
local tag_file=''
for tag_file in uLinux.conf .sys_update_time; do
if [[ -e $mount_point/$tag_file ]]; then
ShowAsInfo "confirmed partition tag-file exists: $tag_file $(ColourTextBrightGreen "(we're in the right place)")"
return 0
fi
done
ShowAsInfo 'partition tag-file not found'
return 0
}
CreateProcessor()
{
[[ $exitcode -eq 0 ]] || return
[[ ! -d $AUTORUN_PATH ]] && mkdir -p "$AUTORUN_PATH"
if [[ -e $AUTORUN_PATHFILE ]]; then
ShowAsSkip "$AUTORUN_FILE already exists: $AUTORUN_PATHFILE"
return
fi
# write a new scripts directory processor to disk
cat > "$AUTORUN_PATHFILE" << EOF
#!/usr/bin/env bash
# source: https://github.com/OneCDOnly/create-autorun
readonly LOGFILE=/var/log/autorun.log
f=''
echo "\$(date) -- begin processing --" >> "\$LOGFILE"
for f in $SCRIPT_STORE_PATH/*; do
if [[ -x \$f ]]; then
echo -n "\$(date)" >> "\$LOGFILE"
echo " executing \$f ..." >> "\$LOGFILE"
\$f >> "\$LOGFILE" 2>&1
fi
done
echo "\$(date) -- end processing --" >> "\$LOGFILE"
EOF
if [[ -e $AUTORUN_PATHFILE ]]; then
ShowAsDone "created autorun script processor: $AUTORUN_PATHFILE"
chmod +x "$AUTORUN_PATHFILE"
CreateScriptStore
return
fi
ShowAsError "unable to create autorun script processor $AUTORUN_PATHFILE"
exitcode=7
}
CreateScriptStore()
{
[[ $exitcode -eq 0 ]] || return
if mkdir -p "$SCRIPT_STORE_PATH"; then
ShowAsDone "created script store: $SCRIPT_STORE_PATH"
script_store_created=true
return 0
fi
ShowAsError "unable to create script store $SCRIPT_STORE_PATH"
exitcode=8
}
AddLinkFromAutorunPartition()
{
[[ $exitcode -eq 0 ]] || return
if [[ -L "$mount_point/$AUTORUN_FILE" && $USER_ARGS_RAW != force ]]; then
ShowAsSkip "symlink from partition already exists and points to: $(/usr/bin/readlink "$mount_point/$AUTORUN_FILE")"
return
fi
if ln -sf "$AUTORUN_PATHFILE" "$mount_point/$AUTORUN_FILE"; then
ShowAsDone "created symlink from partition to $AUTORUN_FILE"
return
fi
ShowAsError 'unable to create symlink'
exitcode=9
}
EnableAutorun()
{
[[ $exitcode -eq 0 ]] || return
local fwvers=$(/sbin/getcfg System Version)
if [[ ${fwvers//.} -ge 430 ]]; then
if [[ $(/sbin/getcfg Misc Autorun) != TRUE ]]; then
/sbin/setcfg Misc Autorun TRUE
ShowAsDone 'enabled autorun.sh in OS'
else
ShowAsSkip 'autorun.sh is already enabled in OS'
fi
fi
}
UnmountAutorunPartition()
{
[[ $partition_mounted = true ]] || return
if /bin/umount "$mount_point"; then
ShowAsDone "unmounted $mount_type autorun partition" "$mount_point"
partition_mounted=false
else
ShowAsError "unable to unmount $mount_type autorun partition"
exitcode=10
fi
[[ $NAS_AUTORUN_FS = ubifs ]] && /sbin/ubidetach -m "$NAS_AUTORUN_PART" 2>/dev/null
}
RemoveMountPoint()
{
[[ $partition_mounted = false ]] || return
[[ -e $mount_point ]] || return
rmdir "$mount_point" && return
ShowAsError "unable to remove temporary mount-point: $mount_point"
exitcode=11
}
ShowResult()
{
[[ $exitcode -eq 0 ]] || return
[[ $script_store_created = true ]] && ShowAsInfo "please place your startup scripts into: $SCRIPT_STORE_PATH"
[[ -e $AUTORUN_PATHFILE ]] && ShowAsInfo "your autorun.sh file is located at: $AUTORUN_PATHFILE"
}
ShowAsInfo()
{
WriteToDisplay.New "$(ColourTextBrightYellow info)" "${1:-}"
}
ShowAsSkip()
{
WriteToDisplay.New "$(ColourTextBrightOrange skip)" "${1:-}"
}
ShowAsDone()
{
WriteToDisplay.New "$(ColourTextBrightGreen 'done')" "${1:-}"
}
ShowAsError()
{
local buffer="${1:-}"
WriteToDisplay.New "$(ColourTextBrightRed fail)" "$(tr 'a-z' 'A-Z' <<< "${buffer:0:1}")${buffer:1}"
}
WriteToDisplay.Wait()
{
# Write a new message without newline
# input:
# $1 = pass/fail
# $2 = message
previous_msg=$(printf "%-10s: %s" "${1:-}" "${2:-}")
echo -n "$previous_msg"
return 0
}
WriteToDisplay.New()
{
# Update the previous message
# input:
# $1 = pass/fail
# $2 = message
# output:
# stdout = overwrites previous message with updated message
# $previous_length
# $appended_length
local new_message=''
local strbuffer=''
local new_length=0
new_message=$(printf "%-10s: %s" "$1" "$2")
if [[ $new_message != "$previous_msg" ]]; then
previous_length=$((${#previous_msg}+1))
new_length=$((${#new_message}+1))
# jump to start of line, print new msg
strbuffer=$(echo -en "\r$new_message ")
# if new msg is shorter then add spaces to end to cover previous msg
if [[ $new_length -lt $previous_length ]]; then
appended_length=$((new_length-previous_length))
strbuffer+=$(printf "%${appended_length}s")
fi
echo "$strbuffer"
fi
return 0
}
GetQnapOS()
{
if /bin/grep -q zfs /proc/filesystems; then
echo 'QuTS hero'
else
echo QTS
fi
}
ColourTextBrightGreen()
{
echo -en '\033[1;32m'"$(ColourReset "${1:-}")"
}
ColourTextBrightYellow()
{
echo -en '\033[1;33m'"$(ColourReset "${1:-}")"
}
ColourTextBrightOrange()
{
echo -en '\033[1;38;5;214m'"$(ColourReset "${1:-}")"
}
ColourTextBrightRed()
{
echo -en '\033[1;31m'"$(ColourReset "${1:-}")"
}
ColourTextBrightWhite()
{
echo -en '\033[1;97m'"$(ColourReset "${1:-}")"
}
ColourReset()
{
echo -en "${1:-}"'\033[0m'
}
Init
DetermineAutorunPartitionLocation
CreateMountPoint
MountAutorunPartition
ConfirmAutorunPartition
CreateProcessor
AddLinkFromAutorunPartition
EnableAutorun
UnmountAutorunPartition
RemoveMountPoint
ShowResult
exit "$exitcode"