-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebian-installer.sh
executable file
·358 lines (307 loc) · 9.87 KB
/
debian-installer.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
#!/bin/bash -e
########################################################################
#** Version: 0.1
#* This script checks a mirror for files and checksums..
#
# note: the frame for this script was auto-created with
# *https://github.com/inofix/admin-toolbox/blob/master/makebashscript.sh*
########################################################################
# author/copyright: <mic@inofix.ch>
# license: gladly sharing with the universe and any lifeform,
# be it naturally born or constructed, alien or kin..
# USE AT YOUR OWN RISK.
########################################################################
[ "$1" == "debug" ] && shift && set -x
## variables ##
### you may copy the following variables into this file for having your own
### local config ...
#conffile=.debian-installer-checksums.sh
### {{{
# do not change anything (=0)
dryrun=1
# do not ignore gpg errors (=1)
do_force_gpg=1
# do not stop and ask overwriting local files
do_force_overwrite=1
# no need to be `root` here.. (=1)
needsroot=1
debian_mirror="https://ftp.uni-stuttgart.de/debian/dists/"
debian_version="stable"
debian_arch="amd64"
### }}}
# clean up temporary working dir
keep_tmp_dir=1
# Unsetting this helper variable
_pre=""
# The system tools we gladly use. Thank you!
declare -A sys_tools
sys_tools=( ["_awk"]="/usr/bin/awk"
["_cat"]="/bin/cat"
["_cp"]="/bin/cp"
["_gpg"]="/usr/bin/gpg"
["_grep"]="/bin/grep"
["_id"]="/usr/bin/id"
["_ls"]="/bin/ls"
["_mkdir"]="/bin/mkdir"
["_mktemp"]="/bin/mktemp"
["_mv"]="/bin/mv"
["_pwd"]="/bin/pwd"
["_rm"]="/bin/rm"
["_rmdir"]="/bin/rmdir"
["_sed"]="/bin/sed"
["_sed_forced"]="/bin/sed"
["_sha256sum"]="/usr/bin/sha256sum"
["_tr"]="/usr/bin/tr"
["_wget"]="/usr/bin/wget" )
# this tools get disabled in dry-run and sudo-ed for needsroot
danger_tools=( "_cp" "_cat" "_dd" "_mkdir" "_sed" "_rm" "_rmdir" )
# special case sudo (not mandatory)
_sudo="/usr/bin/sudo"
## functions ##
print_usage()
{
printf "\e[1;39musage: $0 [options] action\e[0;39m\n"
}
print_help()
{
print_usage
$_grep "^#\* " $0 | $_sed_forced 's;^#\*;;'
}
print_version()
{
printf "\e[1;39m"
$_grep "^#\*\* " $0 | $_sed 's;^#\*\* ;;'
printf "\e[0;39m"
}
warn()
{
printf "\e[1;33mWarning: $@\e[0;39m\n"
}
error()
{
printf "\e[1;31mError: $@\e[0;39m\n"
exit 1
}
error_help()
{
printf "\e[1;31mError: $@\e[0;39m\n"
print_help
exit 1
}
## logic ##
## first set the system tools
for t in ${!sys_tools[@]} ; do
if [ -x "${sys_tools[$t]##* }" ] ; then
export ${t}="${sys_tools[$t]}"
else
error "Missing system tool: ${sys_tools[$t]##* } must be installed."
fi
done
[ ! -f "/etc/$conffile" ] || . "/etc/$conffile"
[ ! -f "/usr/etc/$conffile" ] || . "/usr/etc/$conffile"
[ ! -f "/usr/local/etc/$conffile" ] || . "/usr/local/etc/$conffile"
[ ! -f ~/"$conffile" ] || . ~/"$conffile"
[ ! -f "$conffile" ] || . "$conffile"
#* options:
while true ; do
case "$1" in
#* -a |--arch architecture provide the target arch (default: 'amd64')
-a|--arch)
shift
debian_arch=$1
;;
#* -c |--config conffile alternative config file
-c|--config)
shift
if [ -r "$1" ] ; then
. $1
else
error "config file '$1' does not exist."
fi
;;
#* -f |--force-gpg do not care about GPG errors..
-f|--force-gpg)
do_force_gpg=0
;;
#* -F|--force-overwrite do not care to overwrite files locally (e.g. SHA256SUM)
-F|--force-overwrite)
do_force_overwrite=0
;;
#* -h |--help print this help
-h|--help)
print_help
exit 0
;;
#* -m |--mirror mirror set an alternative mirror
-m|--mirror)
shift
debian_mirror="$1"
;;
#* -n |--dry-run do not change anything
-n|--dry-run)
dryrun=0
;;
#* -T |--keep-tmp-dir do not clean up temporary working dir
-T|--keep-tmp-dir)
keep_tmp_dir=0
;;
#* -v |--version
-v|--version)
print_version
exit
;;
#* -V |--debian-version version version to search for (default: 'stable')
-V|--debian-version)
shift
debian_version=$1
;;
-*|--*)
error_help "option '$1' not supported."
;;
*)
break
;;
esac
shift
done
#* actions:
action=$1
if [ $dryrun -eq 0 ] ; then
_pre="echo "
fi
if [ $needsroot -eq 0 ] ; then
iam=$($_id -u)
if [ $iam -ne 0 ] ; then
if [ -x "$_sudo" ] ; then
_pre="$_pre $_sudo"
else
error "Priviledges missing: use ${_sudo}."
fi
fi
fi
for t in ${danger_tools[@]} ; do
export ${t}="$_pre ${sys_tools[$t]}"
done
get_checksums() {
[ -z "$debian_version" ] && error "There was no Debian version provided. See action: 'versions'"
echo "Trying to get the checksums for $debian_version/$debian_arch from $debian_mirror"
tempdir=$($_mktemp -d)
cd $tempdir
$_wget "$debian_mirror/$debian_version/Release.gpg"
$_wget "$debian_mirror/$debian_version/Release"
$_mv Release.gpg Release.sig
set +e
$_gpg -v Release.sig ; retval=$?
set -e
if [ $do_force -ne 0 ] ; then
if [ $retval -ne 0 ] ; then
clean_tmp $tempdir
error "The Release file could not be verified with Release.gpg! (use '--force' to ignore)"
fi
else
if [ $retval -ne 0 ] ; then
warn "The Release file could not be verified with Release.gpg!"
fi
fi
$_awk 'BEGIN{
mode="false"
}
/^SHA256:/{
mode="true"
}
/main\/installer-'${debian_arch}'\/current\/images\/SHA256SUMS/{
if (mode == "true"){
print $1" SHA256SUMS"
}}' Release > Release.sha256sums
$_wget "$debian_mirror/$debian_version/main/installer-${debian_arch}/current/images/SHA256SUMS"
$_sha256sum -c Release.sha256sums
case "$1" in
persist*)
if [ $do_force_overwrite -ne 0 ] && [ -e $cwd/SHA256SUMS ] ; then
warn "There already is a file called '$cwd/SHA256SUMS'."
echo "To save it please rename it prior to go on by hitting <Enter>."
read
fi
;;&
persist)
$_grep "./netboot/debian-installer/${debian_arch}/initrd.gz$" SHA256SUMS | $_sed 's; .*/\(initrd.gz\); ./\1;' > $cwd/SHA256SUMS
$_grep "./netboot/debian-installer/${debian_arch}/linux$" SHA256SUMS | $_sed 's; .*/\(linux\).*; ./\1;' >> $cwd/SHA256SUMS
;;
persist_iso)
$_grep "./netboot/mini.iso$" SHA256SUMS | $_sed 's; .*/\(mini.iso\); ./\1;' > $cwd/SHA256SUMS
;;
*)
$_grep "./MANIFEST$" SHA256SUMS
$_grep "./MANIFEST.udebs$" SHA256SUMS
$_grep "./netboot/debian-installer/${debian_arch}/initrd.gz$" SHA256SUMS
$_grep "./netboot/debian-installer/${debian_arch}/linux$" SHA256SUMS
;;
esac
clean_tmp $tempdir
}
clean_tmp() {
if [ -d "$1" ] ; then
t=$1
else
error "Error: $1 is not a temporary working directory"
fi
if [ $keep_tmp_dir -eq 0 ] ; then
warn "The temporary working dir is not going to be cleaned up, please do so manually: $tempdir"
else
$_rm $t/*
$_rmdir $t
fi
}
get_files() {
[ -z "$debian_version" ] && error "There was no Debian version provided. See action: 'versions'"
echo "Trying to get the kernel and the initrd for $debian_version/$debian_arch from $debian_mirror installing it here $cwd"
get_checksums persist
cd $cwd
$_wget "$debian_mirror/$debian_version/main/installer-${debian_arch}/current/images/netboot/debian-installer/${debian_arch}/initrd.gz"
$_wget "$debian_mirror/$debian_version/main/installer-${debian_arch}/current/images/netboot/debian-installer/${debian_arch}/linux"
$_sha256sum -c SHA256SUMS
}
get_iso() {
[ -z "$debian_version" ] && error "There was no Debian version provided. See action: 'versions'"
echo "Trying to get the kernel and the initrd for $debian_version/$debian_arch from $debian_mirror installing it here $cwd"
get_checksums persist_iso
cd $cwd
$_wget "$debian_mirror/$debian_version/main/installer-${debian_arch}/current/images/netboot/mini.iso"
$_sha256sum -c SHA256SUMS
}
get_versions() {
$_wget -O - $debian_mirror 2>/dev/null | $_grep "Debian[0-9]\+\.[0-9]\+" | $_sed 's;.*\(Debian[0-9]\+\.[0-9]\+\).*;\1;' | $_sed 's;/;;'
}
get_architectures() {
$_wget -O - ${debian_mirror}/${debian_version}/main/ 2>/dev/null | $_grep "installer-" | $_sed 's;.*installer-\([a-z]*[0-9]*\).*;\1;' | $_sed 's;/;;'
}
cwd=$($_pwd)
case $action in
#* architectures Show what architectures are available on the mirror.
arch*)
get_architectures
;;
#* checksums Verify the signature and print the checksum for central files.
checksums|sum)
get_checksums
;;
#* files Get the files needed for a minimal installation.
files)
get_files
;;
#* mini.iso Get the whole netboot mini.iso just like that...
iso|mini*)
get_iso
;;
#* show_mirror Display the configured mirror.
show_mirror|mirror)
echo $debian_mirror
;;
#* versions Show what versions are available on the mirror.
ver*)
get_versions
;;
*)
error_help "action not supported."
;;
esac