-
Notifications
You must be signed in to change notification settings - Fork 7
/
autoconfig.functions
executable file
·1556 lines (1361 loc) · 46.5 KB
/
autoconfig.functions
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/zsh
# Filename: autoconfig.functions
# Purpose: basic system configuration and hardware setup for grml system
# Authors: grml-team (grml.org), (c) Michael Prokop <[email protected]>
# Bug-Reports: see http://grml.org/bugs/
# License: This file is licensed under the GPL v2.
################################################################################
# {{{ path, variables, signals, umask, zsh
export PATH="/bin:/sbin:/usr/bin:/usr/sbin"
DEBUG="/dev/null"
KERNEL="$(uname -r)"
umask 022
[ -d /run/live/medium ] && export LIVECD_PATH='/run/live/medium'
# Ignore these signals in non-interactive mode: INT, TERM, SEGV
[ -z "$PS1" ] && trap "" 2 3 11
service_wrapper() {
if [ "$#" -lt 2 ] ; then
echo "Usage: service_wrapper <service> <action>" >&2
return 1
fi
local service="$1"
local action="$2"
systemctl "$action" "$service"
}
# zsh stuff
iszsh(){
if [ -n "$ZSH_VERSION" ] ; then
return 0
else
return 1
fi
}
# avoid 'no matches found: ...'
iszsh && setopt no_nomatch # || echo "Warning: not running under zsh!"
# }}}
# {{{ Read in boot parameters
if [ -z "$CMDLINE" ]; then
# if CMDLINE was set from the outside, we're debugging.
# otherwise, take CMDLINE from Kernel and config files.
CMDLINE="$(< /proc/cmdline)"
[ -d ${LIVECD_PATH}/bootparams/ ] && CMDLINE="$CMDLINE $(cat ${LIVECD_PATH}/bootparams/* | tr '\n' ' ')"
TAG="grml-parameters"
if grep -q "$TAG" /sys/bus/virtio/devices/*/mount_tag 2>/dev/null ; then
MOUNTDIR="$(mktemp -d)"
mount -t 9p -o trans=virtio,ro "$TAG" "$MOUNTDIR"
CMDLINE="$CMDLINE $(cat "$MOUNTDIR"/* 2>/dev/null | tr '\n' ' ')"
umount "$MOUNTDIR"
rmdir "$MOUNTDIR"
fi
fi
# }}}
### {{{ Utility Functions
# Get a bootoption's parameter: read boot command line and either
# echo last parameter's argument or return false.
getbootparam(){
local line
local ws
ws=' '
line=" $CMDLINE "
case "$line" in
*[${ws}]"$1="*)
result="${line##*[$ws]$1=}"
result="${result%%[$ws]*}"
echo "$result"
return 0 ;;
*) # no match?
return 1 ;;
esac
}
# Check boot commandline for specified option
checkbootparam(){
[ -n "$1" ] || ( echo "Error: missing argument to checkbootparam()" ; return 1 )
local line
local ws
ws=' '
line=" $CMDLINE "
case "$line" in
*[${ws}]"$1"=*|*[${ws}]"$1"[${ws}]*)
return 0 ;;
*)
return 1 ;;
esac
}
# Check if currently using a framebuffer
hasfb() {
[ -e /dev/fb0 ] && return 0 || return 1
}
# Check wheter a configuration variable (like $CONFIG_TOHD) is
# enabled or not
checkvalue(){
case "$1" in
[yY][eE][sS]) return 0 ;; # it's set to 'yes'
[tT][rR][uU][eE]) return 0 ;; # it's set to 'true'
*) return 1 ;; # default
esac
}
# Are we using grml-small?
checkgrmlsmall(){
grep -q small /etc/grml_version 2>>$DEBUG && return 0 || return 1
}
# if no password is set return a random password
set_passwd() {
[ -n "$PASSWD" ] && return 0
if [ -x /usr/bin/xkcdpass ] ; then
PASSWD="$(xkcdpass)"
elif [ -x /usr/bin/diceware ] ; then
PASSWD="$(diceware)"
else
PASSWD="$(tr -dc '[:alnum:]' < /dev/urandom | head -c 28)"
fi
}
### }}}
# {{{ Check if we are running in live mode or from HD
INSTALLED=""
[ -e /etc/grml_cd ] || INSTALLED="yes"
# }}}
# {{{ provide information about virtual environments
VIRTUAL=false # assume physical system by default
KVM=false
VIRTUALBOX=false
VMWARE=false
_detect_virtual() {
local virt_what_result
virt_what_result=$(virt-what 2>/dev/null)
if [[ $virt_what_result == *vmware* ]]; then
VIRTUAL=true; VMWARE=true; VIRTUAL_ENV='VMware'
fi
if [[ $virt_what_result == *kvm* ]]; then
VIRTUAL=true; KVM=true; VIRTUAL_ENV='KVM'
fi
if [[ $virt_what_result == *virtualbox* ]]; then
VIRTUAL=true; VIRTUALBOX=true; VIRTUAL_ENV='VirtualBox'
fi
if [ -z "$VIRTUAL" ]; then
# imvirt is slow. do not use it, if virt-what already found the hypervisor.
local imvirt_result
imvirt_result=$(imvirt 2>/dev/null)
if [[ $imvirt_result == *VMware* ]]; then
VIRTUAL=true; VMWARE=true; VIRTUAL_ENV='VMware'
fi
if [[ $imvirt_result == "KVM" ]]; then
VIRTUAL=true; KVM=true; VIRTUAL_ENV='KVM'
fi
if [[ $imvirt_result == "VirtualBox" ]]; then
VIRTUAL=true; VIRTUALBOX=true; VIRTUAL_ENV='VirtualBox'
fi
fi
}
_detect_virtual
# }}}
# {{{ source lsb-functions , color handling
if checkbootparam 'nocolor'; then
. /etc/grml/lsb-functions
einfo "Disabling colors in bootsequence as requested on commandline." ; eend 0
else
. /etc/grml/lsb-functions
fi
# }}}
# {{{ log
config_log(){
if checkbootparam 'log' || checkbootparam 'debug' ; then
export DEBUG="/tmp/grml.log.`date +%Y%m%d`"
touch "${DEBUG}"
einfo "Bootparameter log found, debug log file from grml-autoconfig available at '${DEBUG}'"
else
DEBUG="/dev/null"
fi
}
# }}}
### {{{ language configuration / localization
config_language(){
einfo "Activating language settings:"
eindent
# people can specify $LANGUAGE in a config file
[ -r /etc/grml/autoconfig ] && . /etc/grml/autoconfig
# check for bootoption which overrides config from /etc/grml/autoconfig
BOOT_LANGUAGE="$(getbootparam 'lang' 2>>$DEBUG)"
[ -n "$BOOT_LANGUAGE" ] && LANGUAGE="$BOOT_LANGUAGE"
# set default to 'en' in live-cd mode iff $LANGUAGE is not set yet
if [ -z "$INSTALLED" ] ; then
[ -n "$LANGUAGE" ] || LANGUAGE='en'
fi
if [ -x /usr/sbin/grml-setlang ] ; then
# if bootoption lang is used update /etc/default/locale accordingly
if [ -n "$BOOT_LANGUAGE" ] ; then
/usr/sbin/grml-setlang "$LANGUAGE"
# otherwise default to lang=en
else
/usr/sbin/grml-setlang "en"
fi
fi
# export it now, so error messages get translated, too
[ -r /etc/default/locale ] && . /etc/default/locale
export LANG LANGUAGE
local KEYBOARD
KEYBOARD="$(getbootparam 'keyboard' 2>>$DEBUG)"
[ -n "$KEYBOARD" ] || KEYBOARD="$LANGUAGE"
# "symbols/en" doesn't exist, so rewrite to "us"
[[ "$KEYBOARD" == 'en' ]] && KEYBOARD="us"
if [ -r /etc/default/keyboard ] ; then
sed -i "s/^XKBLAYOUT=.*/XKBLAYOUT=\"$KEYBOARD\"/" /etc/default/keyboard
case "$KEYBOARD" in
de|at)
sed -i "s/^XKBVARIANT=.*/XKBVARIANT=\"nodeadkeys\"/" /etc/default/keyboard
;;
esac
fi
eoutdent
}
# }}}
# {{{ Set hostname
config_hostname(){
if ! checkbootparam 'hostname' ; then
return 0
fi
HOSTNAME="$(getbootparam 'hostname' 2>>$DEBUG)"
if [ -z "$HOSTNAME" ] && [ -x /usr/bin/random-hostname ] ; then
einfo "Generating random hostname as no hostname was specified."
HOSTNAME="$(/usr/bin/random-hostname)"
eend $?
fi
einfo "Setting hostname to $HOSTNAME as requested."
grml-hostname $HOSTNAME >>$DEBUG
eend $?
}
# }}}
# fstabuser (needed when running from harddisk with username != grml {{{
config_userfstab(){
# force load of build-in and local config
[ -r /etc/grml/autoconfig ] && . /etc/grml/autoconfig
[ -r /etc/grml/autoconfig ] && . /etc/grml/autoconfig.local
# 1st. try configured fstab user
if [ -n "$CONFIG_FSTAB_USER" ] ; then
fstabuser=$(getent passwd $CONFIG_FSTAB_USER | cut -d: -f1)
fi
# 2nd. use standard user id
[ -n "$fstabuser" ] || fstabuser=$(getent passwd 1000 | cut -d: -f1)
# 3rd. use standard user name
[ -n "$fstabuser" ] || fstabuser=$(getent passwd grml | cut -d: -f1)
# if not yet set fall back to 'root' user, avoid bad /etc/fstab
[ -n "$fstabuser" ] || fstabuser='root'
}
# }}}
# local_user (needed when running with username != grml {{{
config_userlocal() {
# force load of build-in and local config
[ -r /etc/grml/autoconfig ] && . /etc/grml/autoconfig
[ -r /etc/grml/autoconfig ] && . /etc/grml/autoconfig.local
# 1st. try id of primary user
localuser=$(getent passwd 1000 | cut -d: -f1)
# 2nd. use name standard user
[ -n "$localuser" ] || localuser=$(getent passwd grml | cut -d: -f1)
}
# }}}
# {{{ Set clock (Local time is more often used than GMT, so it is default)
config_time(){
# don't touch the files if running from harddisk:
if [ -z "$INSTALLED" ]; then
# The default hardware clock timezone is stated as representing local time.
UTC="--localtime"
if [ -f /etc/default/rcS ] ; then
grep -q "^UTC=" /etc/default/rcS || echo "UTC=no" >> /etc/default/rcS
checkbootparam 'utc' >>$DEBUG 2>&1 && sed -i "s|^UTC=.*$|UTC=yes|" /etc/default/rcS
checkbootparam 'gmt' >>$DEBUG 2>&1 && sed -i "s|^UTC=.*$|UTC=yes|" /etc/default/rcS
checkbootparam 'localtime' >>$DEBUG 2>&1 && sed -i "s|^UTC=.*$|UTC=no|" /etc/default/rcS
grep -q -i "^UTC=yes" /etc/default/rcS && UTC="-u"
# recent initscripts package versions don't ship /etc/default/rcS anymore, instead rely on /etc/adjtime
elif [ -f /etc/adjtime ] ; then
checkbootparam 'utc' >>$DEBUG 2>&1 && sed -i "s/^LOCAL/UTC/" /etc/adjtime
checkbootparam 'gmt' >>$DEBUG 2>&1 && sed -i "s/^LOCAL/UTC/" /etc/adjtime
checkbootparam 'localtime' >>$DEBUG 2>&1 && sed -i "s/^UTC$/LOCAL/" /etc/adjtime
grep -q "^UTC$" /etc/adjtime && UTC="-u"
fi
# hwclock uses the TZ variable
KTZ="$(getbootparam 'tz' 2>>$DEBUG)"
[ -z "$KTZ" ] && [ -r /etc/timezone ] && KTZ=$(< /etc/timezone)
if [ ! -f "/usr/share/zoneinfo/$KTZ" ] ; then
ewarn "Warning: unknown timezone $KTZ" ; eend 1
KTZ="UTC"
ewarn "Falling back to timezone $KTZ" ; eend 0
fi
if ! [ -r /dev/rtc ] ; then
ewarn "Warning: realtime clock not available, trying to execute hwclock anyway." ; eend 0
fi
ERROR=$(TZ="$KTZ" hwclock $UTC -s 2>&1 | head -1) ; RC=$?
if [ -n "$ERROR" ] ; then
eindent
ERROR=$(TZ="$KTZ" hwclock $UTC -s --directisa 2>&1 | head -1)
if [ -n "$ERROR" ] ; then
eerror "Problem running hwclock: $ERROR" ; eend 1
fi
eoutdent
fi
fi
}
# }}}
# {{{ print kernel info
config_kernel(){
if $VIRTUAL && [ -n "$VIRTUAL_ENV" ] ; then
einfo "Running Linux Kernel $KERNEL inside $VIRTUAL_ENV" ; eend 0
else
einfo "Running Linux Kernel $KERNEL" ; eend 0
fi
if [ -r /proc/cpuinfo ] ; then
if egrep -q '^flags.*(vmx|svm)' /proc/cpuinfo ; then
eindent
einfo 'CPU(s) featuring virtualization technology detected' ; eend 0
eoutdent
fi
fi
if [ -d /proc/xen ] ; then
eindent
einfo 'Running kernel featuring support for Xen detected' ; eend 0
eoutdent
fi
}
# }}}
# {{{ secure boot
# helper function to check whether we're running under (enabled) Secure Boot
running_under_secureboot() {
if [[ -x "$(command -v mokutil)" ]] ; then
if mokutil --sb-state 2>/dev/null | grep -q 'SecureBoot enabled' ; then
return 0
else
return 1
fi
else
if od -An -t u1 /sys/firmware/efi/vars/SecureBoot-*/data 2>/dev/null | grep -q 1 ; then
return 0
else
return 1
fi
fi
}
config_secureboot(){
if running_under_secureboot ; then
einfo "SecureBoot is enabled" ; eend 0
else
einfo "SecureBoot not detected" ; eend 0
fi
}
# }}}
# {{{ timezone
config_timezone(){
# don't touch the files if running from harddisk:
if [ -z "$INSTALLED" ]; then
KTZ="$(getbootparam 'tz' 2>>$DEBUG)"
if [ -n "$KTZ" ] ; then
if [ ! -f "/usr/share/zoneinfo/$KTZ" ]
then
ewarn "Warning: unknown timezone $KTZ"; eend 0
else
einfo "Setting timezone."
# update debconf
area=$(echo $KTZ | cut -d '/' -f1)
zone=$(echo $KTZ | cut -d '/' -f2)
echo "tzdata tzdata/Areas select $area" | debconf-set-selections
echo "tzdata tzdata/Zones/$area select $zone" | debconf-set-selections
# update files
echo $KTZ > /etc/timezone
rm -f /etc/localtime
cp "/usr/share/zoneinfo/$KTZ" /etc/localtime ; eend $?
fi
fi
fi
}
# }}}
# {{{ CD Checker
config_testcd(){
if checkbootparam 'testcd' ; then
einfo "Checking CD data integrity as requested by '${WHITE}testcd${NORMAL}' boot option."
eindent
local ERROR=true
local FOUND_FILE=false
local logfile='/tmp/md5sum.log'
rm -f "$logfile"
for md5 in $(find "${LIVECD_PATH}" -name md5sums) ; do
einfo "Checking files against $md5, this may take a while..."
FOUND_FILE=true
OLD_PWD=$(pwd)
cd $(dirname "$md5")
md5sum -c $(basename "$md5") |& tee -a "${logfile}"
if [ $pipestatus[1] -eq 0 ] ; then
ERROR=false
fi
cd "${OLD_PWD}"
done
if ! $FOUND_FILE ; then
eerror 'Error: Could not find md5sum file' ; eend 1
return
fi
if ! $ERROR ; then
einfo "Everything looks OK" ; eend 0
else
eerror 'Checksum failed for theses files:' ; eend 1
egrep -v '(^md5sum:|OK$)' "${logfile}"
eerror 'Data on the medium is possibly incomplete/damaged or RAM of your system is broken.' ; eend 1
einfon "Hit return to continue, or press the power button to shut down system."
read a
fi
eoutdent
fi
}
# }}}
# {{{ blacklist specific module [ used in /etc/init.d/udev ]
config_blacklist(){
if checkbootparam 'blacklist' ; then
if [ -z "$INSTALLED" ]; then
einfo "Bootoption blacklist found."
BLACK="$(getbootparam 'blacklist' 2>>$DEBUG)"
BLACKLIST_FILE='/etc/modprobe.d/grml.conf'
if [ -n "$BLACK" ] ; then
for module in $(echo ${BLACK//,/ }) ; do
einfo "Blacklisting module ${module} via ${BLACKLIST_FILE}."
echo "# begin entry generated by config_blacklist of grml-autoconfig" >> "$BLACKLIST_FILE"
echo "blacklist $module" >> "$BLACKLIST_FILE"
echo "alias $module off" >> "$BLACKLIST_FILE"
echo "# end entry generated by config_blacklist of grml-autoconfig" >> "$BLACKLIST_FILE" ; eend $?
done
else
eerror "No given module for blacklist found. Blacklisting will not work therefore."
fi
else
ewarn "Backlisting via bootoption is not intended for use on harddisk installations." ; eend 1
eindent
einfo "Please blacklist the module(s) manually using the 'blacklist' script."
eoutdent
fi
fi
}
# }}}
# {{{ Start brltty
config_brltty() {
if checkbootparam 'brltty' ; then
einfo "Starting brltty service as requested on boot commandline."
service_wrapper brltty start ; eend $?
fi
}
# }}}
# {{{ Start creating /etc/fstab with HD partitions and USB SCSI devices now
config_fstab(){
NOSWAP="yes" # we do not use swap by default!
if checkbootparam 'swap' || checkbootparam 'anyswap' ; then
NOSWAP=''
checkbootparam 'anyswap' && export ANYSWAP='yes' || export ANYSWAP=""
fi
# Scan for swap, config, homedir - but only in live-mode
if [ -z "$INSTALLED" ] ; then
[ -z "$NOSWAP" ] && einfo "Searching for swap partition(s) as requested."
GRML_IMG=""
GRML_SWP=""
HOMEDIR="$(getbootparam 'home')"
if [ -n "$partitions" ]; then
while read p m f relax; do
case "$p" in *fd0*|*proc*|*sys*|*\#*) continue;; esac
partoptions="users,exec"
fnew=""
# it's a swap partition?
case "$f" in swap)
eindent
if [ -n "$NOSWAP" ]; then
ewarn "Ignoring swap partition ${WHITE}$p${NORMAL}. (Force usage via boot option 'swap', or execute grml-swapon)"
eend 0
else
case "$(dd if=$p bs=1 count=6 skip=4086 2>/dev/null)" in
S1SUSP|S2SUSP|pmdisk|[zZ]*)
if [ -n "$ANYSWAP" ] ; then
einfo "Using swap partition ${WHITE}${p}${NORMAL} [bootoption anyswap found]."
swapon $p 2>>$DEBUG ; eend $?
else
ewarn "Suspend signature on ${WHITE}${p}${NORMAL} found, not using as swap. (Force usage via boot option: anyswap)"
fi
;;
*)
if [[ "$p" == LABEL* ]] ; then
p=$(blkid -t $p | awk -F: '{print $1}')
fi
if grep -q $p /proc/swaps ; then
ewarn "Not using swap partition ${WHITE}${p}${NORMAL} as it is already in use." ; eend 0
else
if [ -b "$p" ] ; then
einfo "Using swap partition ${WHITE}${p}${NORMAL}."
swapon $p 2>>$DEBUG ; eend $?
else
ewarn "$p is not a valid block device - not using it therefore." ; eend 0
fi
fi
;;
esac # dd-check
fi # -n "$NOSWAP
eoutdent
continue
;;
esac # it's a swap partition?
# mount read-only
MOUNTOPTS="ro"
case "$f" in
vfat|msdos|ntfs) MOUNTOPTS="$MOUNTOPTS,uid=${fstabuser},gid=${fstabuser}" ;;
ext2|ext3|reiserfs|jfs|reiser4|xfs) MOUNTOPTS="$MOUNTOPTS,noatime" ;;
*) continue ;;
# *) NONEFOUND='1'; continue ;;
esac
# use a swapfile
if [ -z "$NOSWAP" ] ; then
mount -o "$MOUNTOPTS" -t $f $p $m 2>>$DEBUG && MOUNTED=1 || continue
# Activate swapfile, if exists
SWAPFILE="$(/bin/ls -1d $m/[Gg][Rr][Mm][Ll].[Ss][Ww][Pp] 2>/dev/null)"
fi
if [ -z "$NOSWAP" -a -n "$SWAPFILE" -a -f "$SWAPFILE" ]; then
mount -o remount,rw $m && MOUNTED=1
if swapon "$SWAPFILE" 2>>$DEBUG ; then
eindent
einfo "Using GRML swapfile ${WHITE}${SWAPFILE}${NORMAL}."
eoutdent
fnew="$SWAPFILE swap swap defaults 0 0"
grep -q "$fnew" "/etc/fstab" || echo "$fnew" >> /etc/fstab
GRML_SWP="$GRML_SWP $SWAPFILE"
eend 0
fi
mount -o remount,ro $m 2>>$DEBUG && MOUNTED=1
fi
# use a image as home
IMAGE="$(/bin/ls -1d $m/[Gg][Rr][Mm][Ll].[Ii][Mm][Gg] 2>/dev/null)"
if [ -z "$GRML_IMG" -a -n "$IMAGE" -a -f "$IMAGE" ]; then
if [ -n "$HOMEDIR" ]; then
if [ "$HOMEDIR" != "scan" -a "$HOMEDIR" != "$IMAGE" -a "$HOMEDIR" != "${IMAGE%/*.*}" ]; then
continue
fi
fi
if type -a grml-image >/dev/null 2>&1 && grml-image "$IMAGE" </dev/console >/dev/console 2>&1; then
GRML_IMG="$IMAGE"
mount -o remount,ro $m 2>>$DEBUG && MOUNTED=1
fi
fi
eend 0
# Umount, if not in use
[ -n "$MOUNTED" ] && umount -r $m 2>/dev/null
done <<EOT
$(cat /etc/fstab)
EOT
fi # -n $partitions
fi # -z $INSTALLED
}
# }}}
# {{{ CPU-detection
config_cpu(){
if checkbootparam 'nocpu'; then
ewarn "Skipping CPU detection as requested on boot commandline." ; eend 0
return 0
fi
if ! [ -x "$(which lscpu)" ] ; then
ewarn "Skipping CPU detection due to lack of lscpu."; eend 0
return 0
fi
local cpu_info num_cpus
cpu_info="$(lscpu | sed -n '/^Model name:/s/[^:]*:\s*//p')"
num_cpus=$(grep -c processor /proc/cpuinfo)
einfo "Found ${num_cpus} CPU(s): ${cpu_info}"
eend 0
}
# }}}
# {{{ autostart of ssh
config_ssh(){
if checkbootparam 'ssh' ; then
local PASSWD
PASSWD="$(getbootparam 'ssh' 2>>$DEBUG)"
config_userlocal
einfo "Bootoption ssh found, trying to set password for root and user $localuser"
[ -z "$localuser" ] && eend 1
eindent
if [ -z "$PASSWD" ] ; then
set_passwd && ewarn "No given password for found. Using random password: $PASSWD" && eend 0
fi
eoutdent
if [ -n "$PASSWD" ] ; then
chpass_options=""
if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
chpass_options="-m"
fi
echo "$localuser:$PASSWD" | chpasswd $chpass_options
echo "root:$PASSWD" | chpasswd $chpass_options
eindent
ewarn "Warning: please change the password for root and user $localuser as soon as possible!"
eoutdent
fi
einfo "Starting secure shell server in background for root and user $localuser"
service_wrapper haveged start >>$DEBUG 2>>$DEBUG
service_wrapper rmnologin start >>$DEBUG 2>>$DEBUG
service_wrapper ssh start >>$DEBUG 2>>$DEBUG
eend $?
fi
}
# }}}
# {{{ display hostkeys of SSH server
config_display_ssh_fingerprints() {
if ! ls /etc/ssh/ssh_host_\*_key >/dev/null 2>&1 ; then
return 0 # no SSH host keys present
fi
einfo "SSH key fingerprints:"
for file in /etc/ssh/ssh_host_*_key ; do
einfon
ssh-keygen -l -f $file
done | column -t
eend $?
}
# }}}
# {{{ autostart of x11vnc
config_vnc(){
if checkbootparam 'vnc' ; then
config_userlocal
VNC_PASSWD=''
VNC_PASSWD="$(getbootparam 'vnc' 2>>$DEBUG)"
einfo "Bootoption vnc found, trying to set password for user $localuser."
eindent
if [ -z "$VNC_PASSWD" ] ; then
if [ -x /usr/bin/apg ] ; then
VNC_PASSWD="$(apg -M NL -a 0 -m 8 -x 12 -n 1)"
elif [ -x /usr/bin/gpw ] ; then
VNC_PASSWD="$(gpw 1)"
elif [ -x /usr/bin/pwgen ] ; then
VNC_PASSWD="$(pwgen -1 8)"
elif [ -x /usr/bin/hexdump ] ; then
VNC_PASSWD="$(dd if=/dev/urandom bs=14 count=1 2>/dev/null | hexdump | awk '{print $3 $4}')"
elif [ -n "$RANDOM" ] ; then
VNC_PASSWD="${localuser}${RANDOM}"
else
VNC_PASSWD=''
eerror "Empty passphrase and neither pwgen nor hexdump nor \$RANDOM found. Skipping."
eend 1
fi
if [ -n "$VNC_PASSWD" ] ; then
ewarn "No given password for vnc found. Using random password: $VNC_PASSWD" ; eend 0
fi
fi
eoutdent
# finally check if we have a password we can use:
if [ -n "$VNC_PASSWD" ] ; then
VNCDIR="/home/${localuser}/.vnc"
[ -d "$VNCDIR" ] || mkdir "$VNCDIR"
if [ ! -x /usr/bin/x11vnc ] ; then
eerror "Error: x11vnc not found - can not set up vnc. Please make sure to install the x11vnc package."
eend 1
else
/usr/bin/x11vnc -storepasswd "$VNC_PASSWD" "$VNCDIR"/passwd ; eend $?
/bin/chown -R "$localuser": "$VNCDIR"
fi
fi
if checkbootparam 'vnc_connect' ; then
VNC_CONNECT=''
VNC_CONNECT="$(getbootparam 'vnc_connect' 2>>$DEBUG)"
einfo "Bootoption vnc_connect found, will start vnc with connect to $VNC_CONNECT."
#store the options in a file
VNCDIR="/home/${localuser}/.vnc"
[ -d "$VNCDIR" ] || mkdir "$VNCDIR"
echo " --connect $VNC_CONNECT " >> $VNCDIR/options
fi
fi
}
# }}}
# {{{ set password for root and default user
config_passwd(){
if checkbootparam 'passwd' >>$DEBUG 2>&1; then
local PASSWD
PASSWD="$(getbootparam 'passwd' 2>>$DEBUG)"
config_userlocal
einfo "Bootoption passwd found, trying to set password for root and user $localuser"
[ -z "$localuser" ] && eend 1
eindent
if [ -z "$PASSWD" ] ; then
set_passwd && ewarn "No given password for found. Using random password: $PASSWD" && eend 0
fi
eoutdent
if [ -n "$PASSWD" ] ; then
chpass_options=""
if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
chpass_options="-m"
fi
echo "$localuser:$PASSWD" | chpasswd $chpass_options
echo "root:$PASSWD" | chpasswd $chpass_options
eindent
ewarn "Warning: please change the password for root and user $localuser as soon as possible!"
eoutdent
fi
fi
if checkbootparam 'encpasswd' >>$DEBUG 2>&1; then
local PASSWD
PASSWD="$(getbootparam 'encpasswd' 2>>$DEBUG)"
if [ -z "$PASSWD" ] ; then
eerror "No hashed password found, can not set password."
eend 1
return
fi
config_userlocal
einfo "Bootoption encpasswd found, trying to set hashed password for root and user $localuser"
[ -z "$localuser" ] && eend 1
if [ -n "$PASSWD" ] ; then
chpass_options="-e"
echo "$localuser:$PASSWD" | chpasswd $chpass_options
echo "root:$PASSWD" | chpasswd $chpass_options
eindent
ewarn "Warning: please change the password for root and user $localuser as soon as possible!"
eoutdent
fi
fi
}
# }}}
# {{{ Sound
config_mixer () {
if ! [ -x /usr/bin/amixer ] ; then
logger -t grml-autoconfig "amixer binary not available"
return
fi
if ! [ -r /proc/asound/cards ] ; then
ewarn "No soundcard present, skipping mixer settings therefore."
eend 0
return
fi
for card in $(cat /proc/asound/cards| grep -e '^\s*[0-9]' | awk '{print $1}') ; do
einfo "Configuring soundcard \"$(awk -F\[ '/^ *'$card' \[/{ FS=" "; $0=$2; print $1}' < /proc/asound/cards)\""
eindent
if checkbootparam 'vol' ; then
VOL="$(getbootparam 'vol' 2>>$DEBUG)"
if [ -z "$VOL" ] ; then
eerror "Bootoption vol found but no volume level/parameter given. Using defaults (75%)."
VOL='75'
eend 1
fi
else
VOL='75'
fi
if checkbootparam 'nosound' ; then
einfo "Muting sound devices on request."
ERROR=$(amixer -q set Master mute)
RC=$?
if [ -n "$ERROR" ] ; then
eindent
eerror "Problem muting sound devices: $ERROR"
eoutdent
fi
eend $RC
elif [ -z "$INSTALLED" ] ; then
einfo "Setting mixer volumes to level ${WHITE}${VOL}${NORMAL}."
if checkbootparam 'micvol' ; then
MICVOL="$(getbootparam 'micvol' 2>>$DEBUG)"
einfo "Setting microphone to ${WHITE}${MICVOL}${NORMAL}."
else
MICVOL=0
fi
CONTROLS=$(amixer -c $card scontrols | awk -F"Simple mixer control " '{print $2}')
IFSOLD=${IFS:-}
IFS=$'\n'
for CONTROL in ${=CONTROLS} ; do
# such devices can not be controlled with amixer ... unmute
[[ "$CONTROL" == *Console* ]] && continue
if ! echo "${CONTROL}" | grep -q -i "mic" ; then
if amixer -c $card sget "${CONTROL}" | grep -q 'Capabilities:.*pswitch' ; then
amixer -c $card -q set "${CONTROL}" unmute
fi
if amixer -c $card sget "${CONTROL}" | grep -q -P 'Capabilities:.*(pvolume| volume)' ; then
amixer -c $card -q set "${CONTROL}" "${VOL}"%
fi
fi
if [ ${MICVOL} -ne 0 ] ; then
if amixer -c $card sget "${CONTROL}" | grep -q 'Capabilities:.*cswitch' ; then
amixer -c $card -q set "${CONTROL}" unmute
fi
if amixer -c $card sget "${CONTROL}" | grep -q 'Capabilities:.*cvolume' ; then
amixer -c $card -q set "${CONTROL}" $MICVOL%
fi
eend $?
fi
done
IFS=$IFSOLD
fi # checkbootparam 'nosound'
eoutdent
done
}
# }}}
# {{{ gpm
config_gpm(){
if checkbootparam 'nogpm'; then
ewarn "Not starting GPM as requested on boot commandline." ; eend 0
else
if ! [ -r /dev/input/mice ] ; then
eerror "No mouse found - not starting GPM." ; eend 1
else
einfo "Starting gpm in background."
service_wrapper gpm start >>$DEBUG
eend 0
fi
fi
}
# }}}
# {{{ services
config_services(){
if checkbootparam 'services' ; then
SERVICE="$(getbootparam 'services' 2>>$DEBUG)"
SERVICELIST=$(echo "$SERVICE" | sed 's/,/\\n/g')
SERVICENL=$(echo "$SERVICE" | sed 's/,/ /g')
for service in $(echo -e $SERVICELIST) ; do
einfo "Starting service ${service}."
service_wrapper "${service}" start >>$DEBUG
done
eend $?
fi
}
# }}}
# {{{ remote files
get_remote_file() {
[ "$#" -eq 2 ] || ( echo "Error: wrong parameter for get_remote_file()" ; return 1 )
SOURCE=$(eval echo "$1")
TARGET="$2"
getconfig() {
wget --timeout=10 --dns-timeout=10 --connect-timeout=10 --tries=1 \
--read-timeout=10 ${SOURCE} -O ${TARGET} && return 0 || return 1
}
einfo "Trying to get ${WHITE}${TARGET}${NORMAL}"
if checkbootparam 'getfile.retries' ; then
local counter="$(getbootparam 'getfile.retries' 2>>$DEBUG)"
else
local counter=10
fi
while ! getconfig && [[ "$counter" != 0 ]] ; do
echo -n "Sleeping for 1 second and trying to get config again... "
counter=$(( counter-1 ))
echo "$counter tries left" ; sleep 1
done
if [ -s "$TARGET" ] ; then
einfo "Downloading was successfull." ; eend 0
einfo "md5sum of ${WHITE}${TARGET}${NORMAL}: "
md5sum ${TARGET} ; eend 0
return 0;
else
einfo "Sorry, could not fetch ${SOURCE}" ; eend 1
return 1;
fi
}
# }}}
# {{{ config files
config_netconfig(){
if checkbootparam 'netconfig' ; then
# Provide ARCH here as it is documented to work, but skip calling uname when not necessary.
ARCH="$(uname -m)"
CONFIG="$(getbootparam 'netconfig' 2>>$DEBUG)"
CONFIGFILE='/tmp/netconfig.grml'
if get_remote_file ${CONFIG} ${CONFIGFILE} ; then
cd / && einfo "Unpacking ${WHITE}${CONFIGFILE}${NORMAL}:" && /usr/bin/unp $CONFIGFILE $EXTRACTOPTIONS ; eend $?
fi
fi
}
# }}}
# {{{ remote scripts
config_netscript() {
if checkbootparam 'netscript' ; then
CONFIG="$(getbootparam 'netscript' 2>>$DEBUG)"
SCRIPTFILE='/tmp/netscript.grml'
if get_remote_file ${CONFIG} ${SCRIPTFILE} ; then
chmod +x ${SCRIPTFILE}
einfo "Running ${WHITE}${SCRIPTFILE}${NORMAL}:" && NETSCRIPT=${CONFIG} ${SCRIPTFILE} ; eend $?
fi
fi
}
# }}}
# {{{ start X window system via grml-x
config_x_startup(){
# make sure we start X only if startx is used *before* a nostartx option