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