set sudo group earlier to be safe
[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
244 source-directory /etc/network/interfaces.d
245 EOF
246 fi
247
248 else
249 cat > $target/etc/network/interfaces <<-EOF
250 # generated by FAI
251 auto lo eth0
252 iface lo inet loopback
253 iface eth0 inet dhcp
254 iface eth0 inet6 auto
255
256 source-directory /etc/network/interfaces.d
257 EOF
258
259 # previously had an else condition after
260 #elif ifclass VM || ifclass LINODE; then
261 # iface $NIC1 inet manual
262 # iface br0 inet dhcp
263 # bridge_ports $NIC1
264 # bridge_stp off
265 # bridge_maxwait 0
266 # however, on t9, on startup, br0, became
267 # rename1 and didn't come up. i dunno why,
268 # but the bridge is for vms that I rarely use,
269 # so not bothering to figure it out.
270
271
272 fi
273
274 if ifclass LINODE; then
275 mkdir -p $target/etc/initramfs-tools/conf.d
276 cat >$target/etc/initramfs-tools/conf.d/mine <<EOF
277 # dhcp in initramfs doesn't work on linode. i dunno why, whatever.
278 # man 5 initramfs.conf
279 # /usr/share/doc/klibc-utils/README.ipconfig.gz
280 # /usr/share/initramfs-tools/scripts/functions
281 IP=$linode_ip::$linode_gw:255.255.255.0::eth0:off
282 EOF
283
284
285 if [[ $HOSTNAME == li ]]; then
286
287 cat > $target/etc/network/interfaces <<-EOF
288 # generated by FAI
289 auto lo eth0
290 iface lo inet loopback
291 iface eth0 inet dhcp
292 # for the standard network config, uncomment this and comment the lines after it.
293 #iface eth0 inet6 auto
294
295 iface eth0 inet6 static
296 # this is really a /128. it seems like we need to assign it for ipv6 to work.
297 address 2600:3c00::f03c:91ff:fe6d:baf8/64
298 gateway fe80::1
299
300 iface eth0 inet6 static
301 # from a requested /64 pool
302 address 2600:3c00:e000:280::2/64
303
304 source-directory /etc/network/interfaces.d
305 EOF
306 fi
307 fi
308
309 # I prefer to stick with ifup/down for now. a. networkd is not in its
310 # own package, so cant use in other init systems. b. it works fine.
311 chroot $FAI_ROOT bash <<EOF
312 systemctl disable systemd-networkd.socket systemd-networkd networkd-dispatcher systemd-networkd-wait-online
313 systemctl mask systemd-networkd.socket systemd-networkd networkd-dispatcher systemd-networkd-wait-online
314 EOF
315
316 ##### end network setup #####
317
318
319 if ifclass VOL_BULLSEYE_BOOTSTRAP; then
320 fcopy /etc/systemd/system/faicheck.service
321 chroot $FAI_ROOT bash <<'EOFOUTER'
322 systemctl enable faicheck.service
323 EOFOUTER
324 exit 0 # avoid unnecessary stuff in bootstrap vol
325 fi
326
327
328 ## misc settings
329 chroot $FAI_ROOT bash <<'EOFOUTER'
330 #### begin .ssh setup ###
331 set -x
332 set -eE -o pipefail
333 if ! [[ -s /home/iank/.ssh/authorized_keys ]]; then
334 mkdir -p /home/iank/.ssh
335 f=/root/.ssh/authorized_keys
336 if [[ -e $f ]]; then
337 cp $f /home/iank/.ssh
338 fi
339 chown -R 1000:1000 /home/iank/.ssh
340 chmod -R u=Xrw,og= /home/iank/.ssh
341 rm -rf /root/.ssh
342 # remove broken symlinks or the following cp will fail
343 find /home/iank/.ssh -xtype l -exec rm '{}' \;
344 cp -rL /home/iank/.ssh /root
345 chown -R root:root /root/.ssh
346 chmod 700 /root/.ssh
347 fi
348
349 # old link from
350 # # https://ticktockhouse.svbtle.com/my-obligatory-ubuntu-ssh-agent-post
351 # but that made a service that started too soon and didn't pick up our
352 # x env vars. instead, copy from the root ssh-agent just the
353 # appropriate things into a new service.
354 rm -f /home/iank/.config/systemd/user/default.target.wants/ssh-agent.service
355
356 mkdir -p /home/iank/.local/share/systemd/user
357 cat >/home/iank/.local/share/systemd/user/sshaiank.service <<'EOF'
358 [Unit]
359 Description=OpenSSH User Agent
360 Documentation=man:ssh-agent(1)
361 [Service]
362 ExecStart=/usr/lib/openssh/agent-launch start
363 ExecStopPost=/usr/lib/openssh/agent-launch stop
364 [Install]
365 WantedBy=default.target
366 EOF
367 # enable it
368 # systemctl --user is not available at fai time, so create the link ourselves
369 dir=/home/iank/.config/systemd/user/default.target.wants
370 mkdir -p $dir
371 ln -sf /home/iank/.local/share/systemd/user/sshaiank.service $dir
372
373 #### end .ssh setup ###
374
375 ## duplicated in ssh-emacs-setup
376 # done here so its setup earlier for convenience
377 line='AcceptEnv INSIDE_EMACS BRC COLUMNS'
378 f=/etc/ssh/sshd_config
379 grep -xFq "$line" $f || tee -a $f <<<"$line"
380
381
382 # default debian groups (jessie through buster) + adm, root, admin
383 for g in cdrom floppy audio dip video plugdev netdev adm sudo admin; do
384 if getent group $g >/dev/null; then
385 usermod -aG $g iank
386 fi
387 done
388
389 if getent group systemd-journal >/dev/null; then
390 usermod -aG systemd-journal iank
391 fi
392 EOFOUTER
393
394 rm -f $target/etc/resolv.conf
395 ln -s ../run/systemd/resolve/stub-resolv.conf $target/etc/resolv.conf
396 # needed for bitfolk image
397 if [[ -e /a/bin/fai/fai-wrapper ]]; then
398 systemctl enable systemd-resolved
399 systemctl start systemd-resolved
400 fi
401
402
403
404 # reading through the groups that iank is in but user2 isn't,
405 for g in plugdev audio video cdrom; do
406 $ROOTCMD usermod -a -G $g user2
407 done