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