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