new stack trace, linode fixes, minor improvements
[automated-distro-installer] / fai / config / scripts / GRUB_PC / 11-iank
1 #!/bin/bash -x
2
3 set -eE -o pipefail
4 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
5
6 if [[ $EUID != 0 ]]; then
7 echo "$0: error: expected to be root."
8 exit 1
9 fi
10
11 if ! type -t fcopy &>/dev/null; then
12 sudo apt-get -y install fai-client
13 fi
14
15 chroot $FAI_ROOT bash <<'EOFOUTER'
16 set -eE -o pipefail
17 if getent group systemd-journal >/dev/null; then
18 # makes the journal be saved to disk.
19 mkdir -p /var/log/journal
20 chmod 755 /var/log/journal
21 fi
22 debconf-set-selections <<EOF
23 kexec-tools kexec-tools/load_kexec boolean false
24 EOF
25 apt-get install -y pxe-kexec
26 EOFOUTER
27
28 # -r = recursive
29 # -i = ignore non-matching class warnings, always exit 0
30 # -B = no backup files
31 fcopy -riBM /boot
32 # this is also done by FABASE/10-misc by default.
33 fcopy -riBM /root
34
35
36 src=$FAI/distro-install-common/shadow
37 dst=/q/root/shadow
38 if [[ ! -e $dst && -e $src ]]; then
39 # outside of fai context, we skip this
40 mkdir -p $dst
41 mount -o bind $src $dst
42 fi
43
44 $FAI/distro-install-common/end
45 if ifclass VOL_STRETCH_BOOTSTRAP; then
46 fcopy -riM /etc/systemd/system
47 chroot $FAI_ROOT bash <<'EOFOUTER'
48 systemctl enable fai_check.service
49 EOFOUTER
50 exit 0 # avoid unnecessary stuff in bootstrap vol
51 fi
52
53
54 # these get copied in an earlier stage by fai, but leaving it here since
55 # I run this as a single post-fai script to update things that have changed.
56 fcopy -riBM /etc/apt
57 # outside of fai, this seems to regularly lead to
58 # E: Could not get lock /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable)
59 # so add a sleep. 1 sec is probably way more than needed.
60 sleep 1
61 f=$FAI_ROOT/var/cache/apt/pkgcache.bin
62 if [[ ! -r $f ]] || (( $(( $(date +%s) - $(stat -c %Y $f ) )) > 60*60*2 )); then
63 i=0
64 while fuser $FAI_ROOT/var/lib/dpkg/lock &>/dev/null; do
65 sleep 1
66 i=$(( i+1 ))
67 if (( i > 300 )); then
68 echo "error: timed out waiting for /var/lib/dpkg/lock" >&2
69 exit 1
70 fi
71 $ROOTCMD apt-get update
72 done
73 fi
74
75
76 chroot $FAI_ROOT bash <<'EOF'
77 #### begin .ssh setup ###
78 set -eE -o pipefail
79 mkdir -p /home/iank/.ssh
80 f=/root/.ssh/authorized_keys
81 if [[ -e $f ]]; then
82 cp $f /home/iank/.ssh
83 fi
84 chown -R 1000:1000 /home/iank/.ssh
85 chmod -R u=Xrw,og= /home/iank/.ssh
86 rm -rf /root/.ssh
87 # remove broken symlinks or the following cp will fail
88 find /home/iank/.ssh -xtype l -exec rm '{}' \;
89 cp -rL /home/iank/.ssh /root
90 chown -R root:root /root/.ssh
91 chmod 700 /root/.ssh
92 #### end .ssh setup ###
93
94 # this is needed to enable resolvconf, making /etc/resolv.conf be a symlink.
95 # why? i dun know, it\'s really dumb.
96 dpkg-reconfigure -fnoninteractive resolvconf
97
98 # default jessie groups + kvm, systemd-journal, adm
99 for g in adm cdrom floppy sudo audio dip video plugdev netdev; do
100 if getent gropu $g >/dev/null; then
101 usermod -aG $g iank
102 fi
103 done
104
105 if getent group systemd-journal >/dev/null; then
106 usermod -aG systemd-journal iank
107 fi
108
109
110 # this is usefull. Only thing reason I see this being disabled by default is
111 # that a normal user can disrupt the system, eg cause a reboot.
112 sed -i '$a kernel.sysrq=1
113 /^kernel.sysrq=/d' /etc/sysctl.conf
114 EOF
115
116
117 if [[ $FAI_ACTION != dirinstall ]]; then
118
119
120 # luks options, see man systemd-cryptsetup-generator
121 # all i know is that with luks.crypttab=no, swap still timed out on boot.
122 # and with rd.luks.crypttab=no, it works.
123 cmdline="rd.luks.crypttab=no console=ttyS0"
124 if ifclass LINODE; then
125 speed=19200
126 cmdline+=",${speed}n8"
127 cmdline="rd.luks.crypttab=no console=ttyS0,${speed}n8"
128 else
129 speed=115200
130 cmdline+=",${speed}n8 console=tty0"
131 fi
132
133 cat >$FAI_ROOT/etc/grub.d/40_custom <<EOF
134 #!/bin/sh
135 exec tail -n +3 \$0
136 # This file provides an easy way to add custom menu entries. Simply type the
137 # menu entries you want to add after this comment. Be careful not to change
138 # the 'exec tail' line above.
139
140 # https://www.coreboot.org/Serial_console # tty
141 # but removed unneeded stuff
142
143 serial --speed=$speed
144 terminal_input --append serial
145 terminal_output --append serial
146 EOF
147
148
149 chroot $FAI_ROOT bash <<EOF
150 set -eE -o pipefail
151 # https://askubuntu.com/questions/33416/how-do-i-disable-the-boot-splash-screen-and-only-show-kernel-and-boot-text-inst
152 # we remove quiet and splash, and all thats left is what we want
153
154 if grep -qF "$cmdline" /etc/default/grub; then
155 # already set things, exit
156 exit 0
157 fi
158 sed -ri 's/^ *GRUB_CMDLINE_LINUX_DEFAULT=.*/GRUB_CMDLINE_LINUX_DEFAULT="$cmdline"/' /etc/default/grub
159 # on xenial, no grub is displayed at all. fix that.
160 # found just by noticing this in the config file, and a
161 # warning about it in error.log
162 sed -i '/^ *GRUB_HIDDEN_TIMEOUT/d' /etc/default/grub
163
164 update-grub2
165 EOF
166
167 if [[ ! $FAI_WRAPPER ]]; then
168 chroot $FAI_ROOT bash <<EOF
169 # Just include all of them for now incase we are creating
170 # an install for a different machine.. in distro-begin, we
171 # slim it down to whats used.
172 find /lib/modules/*/kernel/drivers/net /lib/modules/*/kernel/net -type f -name '*.ko' -printf "%f\n" | sed 's/.ko$//' | sort -u >/etc/initramfs-tools/modules
173 update-initramfs -u -k all
174 EOF
175 fi
176 fi
177
178
179 # reading through the groups that iank is in but user2 isn't,
180 for g in plugdev audio video cdrom; do
181 $ROOTCMD usermod -a -G $g user2
182 done