mostly fixes and host updates
[automated-distro-installer] / fai / config / scripts / IANK / 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 if [[ $FAI_ROOT != / ]]; then
23 bprogs_pre=/srv
24 chroot="chroot $FAI_ROOT"
25 fi
26
27 # -r = recursive
28 # -i = ignore non-matching class warnings, always exit 0
29 # -B = no backup files
30 fcopy -riB /boot
31 # this is also done by FAIBASE/10-misc by default (without B)
32 fcopy -riB /usr/local/bin
33
34 fcopy -riB /etc/apt/logind.conf.d
35
36 # this gets done by fai, but just happens too often that
37 # I add sources due to new distros, whatever.
38 fcopy -riB /etc/apt/preferences.d
39 fcopy -riB /etc/apt/sources.list.d
40
41
42 src=$FAI/distro-install-common/shadow
43 dst=/q/root/shadow
44 if [[ ! -e $dst && -e $src ]]; then
45 # outside of fai context, we skip this
46 mkdir -p $dst
47 mount -o bind $src $dst
48 fi
49
50 $FAI/distro-install-common/end
51
52
53
54 ### begin sources install + updates
55 # these get copied in an earlier stage by fai, but leaving it here since
56 # I run this as a single post-fai script to update things that have changed.
57 tmpfile1=$(mktemp)
58 # this can fail if we need an apt update
59 $chroot /usr/bin/apt-cache policy >$tmpfile1 ||:
60 fcopy -riB /etc/apt
61
62 tmpfile2=$(mktemp)
63 $chroot /usr/bin/apt-cache policy >$tmpfile2
64 if ! diff -q $tmpfile1 $tmpfile2; then
65 $chroot /usr/bin/apt update
66 fi
67 # outside of fai, this seems to regularly lead to
68 # E: Could not get lock /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable)
69 # so add a sleep. 1 sec is probably way more than needed.
70 sleep 1
71 f=$FAI_ROOT/var/cache/apt/pkgcache.bin
72 if [[ ! -r $f ]] || (( $(( $(date +%s) - $(stat -c %Y $f ) )) > 60*60*2 )); then
73 i=0
74 while fuser $FAI_ROOT/var/lib/dpkg/lock &>/dev/null; do
75 sleep 1
76 i=$(( i+1 ))
77 if (( i > 300 )); then
78 echo "error: timed out waiting for /var/lib/dpkg/lock" >&2
79 exit 1
80 fi
81 $ROOTCMD apt-get update
82 done
83 fi
84 ### end sources install + updates
85
86
87 #### misc configurations
88
89
90 if [[ $FAI_ACTION != dirinstall ]] && ! ifclass NOCRYPT; then
91 if ifclass LINODE; then
92 speed=19200
93 cmdline="rd.luks.crypttab=no net.ifnames=0 console=ttyS0,${speed}n8"
94 else
95 speed=115200
96 cmdline="rd.luks.crypttab=no net.ifnames=0 console=ttyS0,${speed}n8 console=tty0"
97 case $HOSTNAME in
98 kd)
99 fcopy -v /usr/bin/myncq
100
101 cat >$target/etc/systemd/system/myncq.service <<'EOF'
102 [Unit]
103 Description=fix ncq errors
104
105 [Service]
106 Type=oneshot
107 ExecStart=/usr/bin/myncq
108 TimeoutStartSec=20
109
110 [Install]
111 # https://www.enricozini.org/blog/2017/debian/systemd-07-devices/
112 WantedBy=dev-disk-by\x2did-ata\x2dSamsung_SSD_870_QVO_8TB_S5VUNG0N900656V.device
113 EOF
114
115 $chroot bash <<'EOFOUTER'
116 systemctl enable myncq.service
117 /usr/bin/myncq no-upgrub
118 EOFOUTER
119
120 ;;
121 # per rubens suggestion to make a d16 more stable
122 kd|kw) cmdline+=" pci=realloc=off" ;;
123 esac
124 fi
125
126 fi ##### end != dirinstall && != NOCRYPT
127
128
129 ###### begin network setup ####
130
131 # use old names. the idea of them changing between boots has never
132 # happened to me and I usually only have 1 wired or other type.
133 # If I ever do need to care about it, I will.
134 # Strangely this didn't work on kw, so I added kernel cmdline parameter.
135 # https://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames/
136 ln -sf /dev/null $target/etc/systemd/network/99-default.link
137
138
139 # bitfolk installer handles the rest
140 case $HOSTNAME in
141 bk|je) exit 0 ;;
142 esac
143
144
145 # bug fix, somewhere between t9's xorg 1.19.6
146 # and 1.20.1-3ubuntu2
147 # xserver-xorg-video-nouveau 1:1.0.15-3
148 # xorg stopped load nouveau
149 # https://www.linuxquestions.org/questions/slackware-14/kernel-modules-conflicting-with-nouveau-driver-4175623867/
150 # https://nouveau.freedesktop.org/InstallNouveau.html
151 # And now in t11, things got worse with a newer card also not loading
152 # nouveau when it did in t10.
153 if lspci|grep -q 'VGA compatible controller: NVIDIA'; then
154 mkdir -p $target/etc/X11/xorg.conf.d/
155 cat >$target/etc/X11/xorg.conf.d/10-nouveau.conf <<'EOF'
156 Section "Device"
157 Identifier "Device0"
158 Driver "nouveau"
159 EndSection
160 EOF
161 fi
162
163 # use networkmanager if this host has wireless.
164 if [[ $HOSTNAME == bo ]] || type -p iw &>/dev/null && [[ $(iw dev) ]]; then
165 $chroot bash <<EOF
166 apt-get -y install network-manager
167 EOF
168
169 # allow networkmanager to manage interfaces
170 #https://bugs.launchpad.net/ubuntu/+source/network-manager/+bug/1638842
171 touch $target/etc/NetworkManager/conf.d/10-globally-managed-devices.conf
172 # in a default desktop install, it looks like netplan creates this file under
173 # run/NetworkManager/conf.d in early boot.
174
175 # By default, dns=default is set in etiona, and dns is just broken.
176 # Maybe with resolvconf it would work, but theres no need for that.
177 # https://wiki.gnome.org/Projects/NetworkManager/DNS
178 cat >$target/etc/NetworkManager/conf.d/99-iank.conf <<'EOF'
179 [main]
180 dns=systemd-resolved
181 EOF
182 else
183 cat > $target/etc/network/interfaces <<-EOF
184 # generated by FAI
185 auto lo eth0
186 iface lo inet loopback
187 iface eth0 inet dhcp
188 iface eth0 inet6 auto
189
190 source-directory /etc/network/interfaces.d
191 EOF
192
193 # previously had an else condition after
194 #elif ifclass VM || ifclass LINODE; then
195 # iface $NIC1 inet manual
196 # iface br0 inet dhcp
197 # bridge_ports $NIC1
198 # bridge_stp off
199 # bridge_maxwait 0
200 # however, on t9, on startup, br0, became
201 # rename1 and didn't come up. i dunno why,
202 # but the bridge is for vms that I rarely use,
203 # so not bothering to figure it out.
204
205
206 fi
207
208 # for btrfs bug reporting, install latest kernel and btrfs progs, as of
209 # 2023-07-21
210 case $HOSTNAME in
211 sy)
212 mkdir -p $FAI_ROOT/tmp/kernel-debs
213 cd $FAI_ROOT/tmp/kernel-debs
214 va=6.4.3
215 vb=$va-060403
216 vc=${vb}.202307110536
217 pre="https://kernel.ubuntu.com/~kernel-ppa/mainline/v$va/amd64/linux-"
218 if ! dpkg -s -- linux-headers-${vb} 2>&1 | grep -Fx "Status: install ok installed" &>/dev/null; then
219 urls=(
220 ${pre}headers-${vb}_${vc}_all.deb
221 ${pre}{headers,image-unsigned,modules}-${vb}-generic_${vc}_amd64.deb
222 )
223 wget "${urls[@]}"
224 $chroot bash <<EOF
225 cd /tmp/kernel-debs
226 dpkg -i *.deb
227 EOF
228 fi
229 ;;
230 *)
231 $chroot apt-get -y install linux-libre
232 ;;
233 esac
234
235 cp -ra $bprogs_pre/a/opt/btrfs-progs-release/btrfs-progs-v6.3.2 $FAI_ROOT/tmp/btrfs-progs
236 $chroot bash <<EOF
237 cd /tmp/btrfs-progs
238 make install
239 EOF
240
241
242 if ifclass LINODE; then
243 mkdir -p $target/etc/initramfs-tools/conf.d
244 cat >$target/etc/initramfs-tools/conf.d/mine <<EOF
245 # dhcp in initramfs doesn't work on linode. i dunno why, whatever.
246 # man 5 initramfs.conf
247 # /usr/share/doc/klibc-utils/README.ipconfig.gz
248 # /usr/share/initramfs-tools/scripts/functions
249 IP=$linode_ip::$linode_gw:255.255.255.0::eth0:off
250 EOF
251
252
253 if [[ $HOSTNAME == li ]]; then
254 cat > $target/etc/network/interfaces <<-EOF
255 # generated by FAI
256 auto lo eth0
257 iface lo inet loopback
258 iface eth0 inet dhcp
259 # for the standard network config, uncomment this and comment the lines after it.
260 #iface eth0 inet6 auto
261
262 iface eth0 inet6 static
263 # this is really a /128. it seems like we need to assign it for ipv6 to work.
264 address 2600:3c00::f03c:91ff:fe6d:baf8/64
265 gateway fe80::1
266
267 iface eth0 inet6 static
268 # from a requested /64 pool
269 address 2600:3c00:e000:280::2/64
270
271 source-directory /etc/network/interfaces.d
272 EOF
273 fi
274 fi
275
276
277 ##### end network setup #####
278
279
280 if ifclass VOL_BULLSEYE_BOOTSTRAP; then
281 fcopy /etc/systemd/system/faicheck.service
282 $chroot bash <<'EOFOUTER'
283 systemctl enable faicheck.service
284 EOFOUTER
285 exit 0 # avoid unnecessary stuff in bootstrap vol
286 fi
287
288
289 ## misc settings
290 $chroot bash <<'EOFOUTER'
291 #### begin .ssh setup ###
292 set -x
293 set -eE -o pipefail
294 if ! [[ -s /home/iank/.ssh/authorized_keys ]]; then
295 mkdir -p /home/iank/.ssh
296 f=/root/.ssh/authorized_keys
297 if [[ -e $f ]]; then
298 cp $f /home/iank/.ssh
299 fi
300 chown -R 1000:1000 /home/iank/.ssh
301 chmod -R u=Xrw,og= /home/iank/.ssh
302 rm -rf /root/.ssh
303 # remove broken symlinks or the following cp will fail
304 find /home/iank/.ssh -xtype l -exec rm '{}' \;
305 cp -rL /home/iank/.ssh /root
306 chown -R root:root /root/.ssh
307 chmod 700 /root/.ssh
308 fi
309
310 # old link from
311 # # https://ticktockhouse.svbtle.com/my-obligatory-ubuntu-ssh-agent-post
312 # but that made a service that started too soon and didn't pick up our
313 # x env vars. instead, copy from the root ssh-agent just the
314 # appropriate things into a new service.
315 rm -f /home/iank/.config/systemd/user/default.target.wants/ssh-agent.service
316
317 rm -f /home/iank/.local/share/systemd/user/sshaiank.service \
318 /home/iank/.config/systemd/user/default.target.wants/sshaiank.service
319
320 #### end .ssh setup ###
321
322 ## duplicated in ssh-emacs-setup
323 # done here so its setup earlier for convenience
324 line='AcceptEnv INSIDE_EMACS BRC COLUMNS'
325 f=/etc/ssh/sshd_config
326 grep -xFq "$line" $f || tee -a $f <<<"$line"
327
328
329 # default debian groups (jessie through buster) + adm, root, admin
330 for g in cdrom floppy audio dip video plugdev netdev adm sudo admin; do
331 if getent group $g >/dev/null; then
332 usermod -aG $g iank
333 fi
334 done
335
336 if getent group systemd-journal >/dev/null; then
337 usermod -aG systemd-journal iank
338 fi
339 EOFOUTER
340
341 rm -f $target/etc/resolv.conf
342 ln -s ../run/systemd/resolve/stub-resolv.conf $target/etc/resolv.conf
343 # needed for bitfolk image
344 if [[ -e /a/bin/fai/fai-wrapper ]]; then
345 systemctl enable systemd-resolved
346 systemctl start systemd-resolved
347 fi
348
349
350
351 # reading through the groups that iank is in but user2 isn't,
352 for g in plugdev audio video cdrom; do
353 $ROOTCMD usermod -a -G $g user2
354 done