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