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