various fixes for bullseye and new ssh key changes
[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 if [[ -e /a/bin/fai/fai-wrapper ]]; then
16 chroot() {
17 shift
18 "$@"
19 }
20 fi
21
22
23
24 # -r = recursive
25 # -i = ignore non-matching class warnings, always exit 0
26 # -B = no backup files
27 fcopy -riB /boot
28 # this is also done by FABASE/10-misc by default (without B)
29 fcopy -riB /root
30 fcopy -riB /usr/local/bin
31
32
33 src=$FAI/distro-install-common/shadow
34 dst=/q/root/shadow
35 if [[ ! -e $dst && -e $src ]]; then
36 # outside of fai context, we skip this
37 mkdir -p $dst
38 mount -o bind $src $dst
39 fi
40
41 $FAI/distro-install-common/end
42
43
44
45 ### begin sources install + updates
46 # these get copied in an earlier stage by fai, but leaving it here since
47 # I run this as a single post-fai script to update things that have changed.
48 tmpfile1=$(mktemp)
49 # this can fail if we need an apt update
50 chroot $FAI_ROOT /usr/bin/apt-cache policy >$tmpfile1 ||:
51 fcopy -riBM /etc/apt
52 tmpfile2=$(mktemp)
53 chroot $FAI_ROOT /usr/bin/apt-cache policy >$tmpfile2
54 if ! diff -q $tmpfile1 $tmpfile2; then
55 chroot $FAI_ROOT /usr/bin/apt update
56 fi
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 ### end sources install + updates
75
76
77 #### misc configurations
78 chroot $FAI_ROOT bash <<'EOFOUTER'
79 if getent group systemd-journal >/dev/null; then
80 # makes the journal be saved to disk.
81 mkdir -p /var/log/journal
82 chmod 755 /var/log/journal
83 fi
84 debconf-set-selections <<EOF
85 kexec-tools kexec-tools/load_kexec boolean false
86 EOF
87 apt-get install -y pxe-kexec
88
89 # this is usefull. Only thing reason I see this being disabled by default is
90 # that a normal user can disrupt the system, eg cause a reboot.
91 sed -i '$a kernel.sysrq=1
92 /^kernel.sysrq=/d' /etc/sysctl.conf
93
94 EOFOUTER
95
96
97 if [[ $FAI_ACTION != dirinstall ]] && ! ifclass NOCRYPT; then
98 # luks options, see man systemd-cryptsetup-generator
99 # all i know is that with luks.crypttab=no, swap still timed out on boot.
100 # and with rd.luks.crypttab=no, it works.
101 if ifclass LINODE; then
102 speed=19200
103 cmdline="rd.luks.crypttab=no net.ifnames=0 console=ttyS0,${speed}n8"
104 else
105 speed=115200
106 cmdline="rd.luks.crypttab=no net.ifnames=0 console=ttyS0,${speed}n8 console=tty0"
107 case $HOSTNAME in
108 kd)
109 fcopy -v /usr/bin/myncq
110
111 cat >$target/etc/systemd/system/myncq.service <<'EOF'
112 [Unit]
113 Description=fix ncq errors
114
115 [Service]
116 Type=oneshot
117 ExecStart=/usr/bin/myncq
118 TimeoutStartSec=20
119
120 [Install]
121 # https://www.enricozini.org/blog/2017/debian/systemd-07-devices/
122 WantedBy=dev-disk-by\x2did-ata\x2dSamsung_SSD_870_QVO_8TB_S5VUNG0N900656V.device
123 EOF
124
125 chroot $FAI_ROOT bash <<'EOFOUTER'
126 systemctl enable myncq.service
127 /usr/bin/myncq no-upgrub
128 EOFOUTER
129
130 ;;
131 # per rubens suggestion to make a d16 more stable
132 kd|kw) cmdline+=" pci=realloc=off" ;;
133 esac
134 fi
135
136 cat >$FAI_ROOT/etc/grub.d/40_custom <<EOF
137 #!/bin/sh
138 exec tail -n +3 \$0
139 # This file provides an easy way to add custom menu entries. Simply type the
140 # menu entries you want to add after this comment. Be careful not to change
141 # the 'exec tail' line above.
142
143 # https://www.coreboot.org/Serial_console # tty
144 # but removed unneeded stuff
145
146 serial --speed=$speed
147 terminal_input --append serial
148 terminal_output --append serial
149 EOF
150
151
152 chroot $FAI_ROOT bash <<EOF
153 set -eE -o pipefail
154 # https://askubuntu.com/questions/33416/how-do-i-disable-the-boot-splash-screen-and-only-show-kernel-and-boot-text-inst
155
156 sed -ri 's/(^GRUB_CMDLINE_LINUX_DEFAULT=")quiet/\1/;s/^(GRUB_CMDLINE_LINUX_DEFAULT=".*) quiet([ "])/\1\2/' /etc/default/grub
157 sed -ri 's/(^GRUB_CMDLINE_LINUX_DEFAULT=")splash/\1/;s/^(GRUB_CMDLINE_LINUX_DEFAULT=".*) splash([ "])/\1\2/' /etc/default/grub
158
159 for arg in $cmdline; do
160 if ! grep "^GRUB_CMDLINE_LINUX_DEFAULT=.*[\" ]${arg//./\\.}[\" ]" /etc/default/grub; then
161 sed -ri "s/^GRUB_CMDLINE_LINUX_DEFAULT=\"(.*)/GRUB_CMDLINE_LINUX_DEFAULT=\"$arg \1/" /etc/default/grub
162 fi
163 done
164
165 if grep -qF "$cmdline" /etc/default/grub; then
166 # already set things, exit
167 exit 0
168 fi
169 sed -ri 's/^ *GRUB_CMDLINE_LINUX_DEFAULT=.*/GRUB_CMDLINE_LINUX_DEFAULT="$cmdline"/' /etc/default/grub
170 # on xenial, no grub is displayed at all. fix that.
171 # found just by noticing this in the config file, and a
172 # warning about it in error.log
173 sed -i '/^ *GRUB_HIDDEN_TIMEOUT/d' /etc/default/grub
174
175 if type -P update-grub2 &>/dev/null; then
176 update-grub2
177 else
178 update-grub
179 fi
180
181 EOF
182 fi ##### end != dirinstall && != NOCRYPT
183
184
185 ###### begin network setup ####
186
187 # use old names. the idea of them changing between boots has never
188 # happened to me and I usually only have 1 wired or other type.
189 # If I ever do need to care about it, I will.
190 # Strangely this didn't work on kw, so I added kernel cmdline parameter.
191 # https://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames/
192 ln -sf /dev/null $target/etc/systemd/network/99-default.link
193
194
195 # bitfolk installer handles the rest
196 case $HOSTNAME in
197 bk|je) exit 0 ;;
198 esac
199
200
201 # bug fix, somewhere between t9's xorg 1.19.6
202 # and 1.20.1-3ubuntu2
203 # xserver-xorg-video-nouveau 1:1.0.15-3
204 # xorg stopped load nouveau
205 # https://www.linuxquestions.org/questions/slackware-14/kernel-modules-conflicting-with-nouveau-driver-4175623867/
206 # https://nouveau.freedesktop.org/InstallNouveau.html
207 if lspci|grep -q 'GeForce GTX 6[0-9][0-9]\]'; then
208 mkdir -p $target/etc/X11/xorg.conf.d/
209 cat >$target/etc/X11/xorg.conf.d/10-nouveau.conf <<'EOF'
210 Section "Device"
211 Identifier "Device0"
212 Driver "nouveau"
213 EndSection
214 EOF
215 fi
216
217 # use networkmanager if this host has wireless.
218 if type -p iw &>/dev/null && [[ $(iw dev) ]]; then
219 chroot $FAI_ROOT bash <<EOF
220 apt-get -y install network-manager
221 EOF
222
223 # allow networkmanager to manage interfaces
224 #https://bugs.launchpad.net/ubuntu/+source/network-manager/+bug/1638842
225 touch $target/etc/NetworkManager/conf.d/10-globally-managed-devices.conf
226 # in a default desktop install, it looks like netplan creates this file under
227 # run/NetworkManager/conf.d in early boot.
228
229 # By default, dns=default is set in etiona, and dns is just broken.
230 # Maybe with resolvconf it would work, but theres no need for that.
231 # https://wiki.gnome.org/Projects/NetworkManager/DNS
232 cat >$target/etc/NetworkManager/conf.d/99-iank.conf <<'EOF'
233 [main]
234 dns=systemd-resolved
235 EOF
236 if [[ $HOSTNAME == frodo ]]; then
237 cat > $target/etc/network/interfaces <<-EOF
238 # generated by FAI
239 auto lo eth0
240 iface lo inet loopback
241 iface eth0 inet static
242 address 10.3.0.2/16
243 EOF
244 fi
245
246 else
247 cat > $target/etc/network/interfaces <<-EOF
248 # generated by FAI
249 auto lo eth0
250 iface lo inet loopback
251 iface eth0 inet dhcp
252 iface eth0 inet6 auto
253 EOF
254
255 # previously had an else condition after
256 #elif ifclass VM || ifclass LINODE; then
257 # iface $NIC1 inet manual
258 # iface br0 inet dhcp
259 # bridge_ports $NIC1
260 # bridge_stp off
261 # bridge_maxwait 0
262 # however, on t9, on startup, br0, became
263 # rename1 and didn't come up. i dunno why,
264 # but the bridge is for vms that I rarely use,
265 # so not bothering to figure it out.
266
267
268 fi
269
270 if ifclass LINODE; then
271 mkdir -p $target/etc/initramfs-tools/conf.d
272 cat >$target/etc/initramfs-tools/conf.d/mine <<EOF
273 # dhcp in initramfs doesn't work on linode. i dunno why, whatever.
274 # man 5 initramfs.conf
275 # /usr/share/doc/klibc-utils/README.ipconfig.gz
276 # /usr/share/initramfs-tools/scripts/functions
277 IP=$linode_ip::$linode_gw:255.255.255.0::eth0:off
278 EOF
279
280
281 if [[ $HOSTNAME == li ]]; then
282
283 cat > $target/etc/network/interfaces <<-EOF
284 # generated by FAI
285 auto lo eth0
286 iface lo inet loopback
287 iface eth0 inet dhcp
288 # for the standard network config, uncomment this and comment the lines after it.
289 #iface eth0 inet6 auto
290
291 iface eth0 inet6 static
292 # this is really a /128. it seems like we need to assign it for ipv6 to work.
293 address 2600:3c00::f03c:91ff:fe6d:baf8/64
294 gateway fe80::1
295
296 iface eth0 inet6 static
297 # from a requested /64 pool
298 address 2600:3c00:e000:280::2/64
299 EOF
300 fi
301 fi
302
303 # I prefer to stick with ifup/down for now. a. networkd is not in its
304 # own package, so cant use in other init systems. b. it works fine.
305 chroot $FAI_ROOT bash <<EOF
306 systemctl disable systemd-networkd.socket systemd-networkd networkd-dispatcher systemd-networkd-wait-online
307 systemctl mask systemd-networkd.socket systemd-networkd networkd-dispatcher systemd-networkd-wait-online
308 EOF
309
310 ##### end network setup #####
311
312
313 if ifclass VOL_BULLSEYE_BOOTSTRAP; then
314 fcopy /etc/systemd/system/faicheck.service
315 chroot $FAI_ROOT bash <<'EOFOUTER'
316 systemctl enable faicheck.service
317 EOFOUTER
318 exit 0 # avoid unnecessary stuff in bootstrap vol
319 fi
320
321
322 ## misc settings
323 chroot $FAI_ROOT bash <<'EOFOUTER'
324 #### begin .ssh setup ###
325 set -x
326 set -eE -o pipefail
327 mkdir -p /home/iank/.ssh
328 f=/root/.ssh/authorized_keys
329 if [[ -e $f ]]; then
330 cp $f /home/iank/.ssh
331 fi
332 chown -R 1000:1000 /home/iank/.ssh
333 chmod -R u=Xrw,og= /home/iank/.ssh
334 rm -rf /root/.ssh
335 # remove broken symlinks or the following cp will fail
336 find /home/iank/.ssh -xtype l -exec rm '{}' \;
337 cp -rL /home/iank/.ssh /root
338 chown -R root:root /root/.ssh
339 chmod 700 /root/.ssh
340 # https://ticktockhouse.svbtle.com/my-obligatory-ubuntu-ssh-agent-post
341 # systemctl --user is not available at fai time, so create the link ourselves
342 d=/home/iank/.config/systemd/user/default.target.wants
343 sudo -u iank mkdir -p $d
344 sudo -u iank ln -sf /usr/lib/systemd/user/ssh-agent.service $d
345 #### end .ssh setup ###
346
347 ## duplicated in ssh-emacs-setup
348 # done here so its setup earlier for convenience
349 line='AcceptEnv INSIDE_EMACS BRC COLUMNS'
350 f=/etc/ssh/sshd_config
351 grep -xFq "$line" $f || tee -a $f <<<"$line"
352
353
354 # default debian groups (jessie through buster) + adm, sudo, root, admin
355 for g in cdrom floppy audio dip video plugdev netdev adm sudo admin; do
356 if getent group $g >/dev/null; then
357 usermod -aG $g iank
358 fi
359 done
360
361 if getent group systemd-journal >/dev/null; then
362 usermod -aG systemd-journal iank
363 fi
364 EOFOUTER
365
366 rm -f $target/etc/resolv.conf
367 ln -s ../run/systemd/resolve/stub-resolv.conf $target/etc/resolv.conf
368 # needed for bitfolk image
369 if [[ -e /a/bin/fai/fai-wrapper ]]; then
370 systemctl enable systemd-resolved
371 systemctl start systemd-resolved
372 fi
373
374
375
376 # reading through the groups that iank is in but user2 isn't,
377 for g in plugdev audio video cdrom; do
378 $ROOTCMD usermod -a -G $g user2
379 done