5245b35ae7a210dd1bf4733f61a86d4f2dfe417f
[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 if lspci|grep -q 'GeForce GTX 6[0-9][0-9]\]'; then
147 mkdir -p $target/etc/X11/xorg.conf.d/
148 cat >$target/etc/X11/xorg.conf.d/10-nouveau.conf <<'EOF'
149 Section "Device"
150 Identifier "Device0"
151 Driver "nouveau"
152 EndSection
153 EOF
154 fi
155
156 # use networkmanager if this host has wireless.
157 if [[ $HOSTNAME == bo ]] || type -p iw &>/dev/null && [[ $(iw dev) ]]; then
158 chroot $FAI_ROOT bash <<EOF
159 apt-get -y install network-manager
160 EOF
161
162 # allow networkmanager to manage interfaces
163 #https://bugs.launchpad.net/ubuntu/+source/network-manager/+bug/1638842
164 touch $target/etc/NetworkManager/conf.d/10-globally-managed-devices.conf
165 # in a default desktop install, it looks like netplan creates this file under
166 # run/NetworkManager/conf.d in early boot.
167
168 # By default, dns=default is set in etiona, and dns is just broken.
169 # Maybe with resolvconf it would work, but theres no need for that.
170 # https://wiki.gnome.org/Projects/NetworkManager/DNS
171 cat >$target/etc/NetworkManager/conf.d/99-iank.conf <<'EOF'
172 [main]
173 dns=systemd-resolved
174 EOF
175 if [[ $HOSTNAME == frodo ]]; then
176 cat > $target/etc/network/interfaces <<-EOF
177 # generated by FAI
178 auto lo eth0
179 iface lo inet loopback
180 iface eth0 inet static
181 address 10.3.0.2/16
182
183 source-directory /etc/network/interfaces.d
184 EOF
185 fi
186
187 else
188 cat > $target/etc/network/interfaces <<-EOF
189 # generated by FAI
190 auto lo eth0
191 iface lo inet loopback
192 iface eth0 inet dhcp
193 iface eth0 inet6 auto
194
195 source-directory /etc/network/interfaces.d
196 EOF
197
198 # previously had an else condition after
199 #elif ifclass VM || ifclass LINODE; then
200 # iface $NIC1 inet manual
201 # iface br0 inet dhcp
202 # bridge_ports $NIC1
203 # bridge_stp off
204 # bridge_maxwait 0
205 # however, on t9, on startup, br0, became
206 # rename1 and didn't come up. i dunno why,
207 # but the bridge is for vms that I rarely use,
208 # so not bothering to figure it out.
209
210
211 fi
212
213 if ifclass LINODE; then
214 mkdir -p $target/etc/initramfs-tools/conf.d
215 cat >$target/etc/initramfs-tools/conf.d/mine <<EOF
216 # dhcp in initramfs doesn't work on linode. i dunno why, whatever.
217 # man 5 initramfs.conf
218 # /usr/share/doc/klibc-utils/README.ipconfig.gz
219 # /usr/share/initramfs-tools/scripts/functions
220 IP=$linode_ip::$linode_gw:255.255.255.0::eth0:off
221 EOF
222
223
224 if [[ $HOSTNAME == li ]]; then
225 cat > $target/etc/network/interfaces <<-EOF
226 # generated by FAI
227 auto lo eth0
228 iface lo inet loopback
229 iface eth0 inet dhcp
230 # for the standard network config, uncomment this and comment the lines after it.
231 #iface eth0 inet6 auto
232
233 iface eth0 inet6 static
234 # this is really a /128. it seems like we need to assign it for ipv6 to work.
235 address 2600:3c00::f03c:91ff:fe6d:baf8/64
236 gateway fe80::1
237
238 iface eth0 inet6 static
239 # from a requested /64 pool
240 address 2600:3c00:e000:280::2/64
241
242 source-directory /etc/network/interfaces.d
243 EOF
244 fi
245 fi
246
247
248 ##### end network setup #####
249
250
251 if ifclass VOL_BULLSEYE_BOOTSTRAP; then
252 fcopy /etc/systemd/system/faicheck.service
253 chroot $FAI_ROOT bash <<'EOFOUTER'
254 systemctl enable faicheck.service
255 EOFOUTER
256 exit 0 # avoid unnecessary stuff in bootstrap vol
257 fi
258
259
260 ## misc settings
261 chroot $FAI_ROOT bash <<'EOFOUTER'
262 #### begin .ssh setup ###
263 set -x
264 set -eE -o pipefail
265 if ! [[ -s /home/iank/.ssh/authorized_keys ]]; then
266 mkdir -p /home/iank/.ssh
267 f=/root/.ssh/authorized_keys
268 if [[ -e $f ]]; then
269 cp $f /home/iank/.ssh
270 fi
271 chown -R 1000:1000 /home/iank/.ssh
272 chmod -R u=Xrw,og= /home/iank/.ssh
273 rm -rf /root/.ssh
274 # remove broken symlinks or the following cp will fail
275 find /home/iank/.ssh -xtype l -exec rm '{}' \;
276 cp -rL /home/iank/.ssh /root
277 chown -R root:root /root/.ssh
278 chmod 700 /root/.ssh
279 fi
280
281 # old link from
282 # # https://ticktockhouse.svbtle.com/my-obligatory-ubuntu-ssh-agent-post
283 # but that made a service that started too soon and didn't pick up our
284 # x env vars. instead, copy from the root ssh-agent just the
285 # appropriate things into a new service.
286 rm -f /home/iank/.config/systemd/user/default.target.wants/ssh-agent.service
287
288 rm -f /home/iank/.local/share/systemd/user/sshaiank.service \
289 /home/iank/.config/systemd/user/default.target.wants/sshaiank.service
290
291 #### end .ssh setup ###
292
293 ## duplicated in ssh-emacs-setup
294 # done here so its setup earlier for convenience
295 line='AcceptEnv INSIDE_EMACS BRC COLUMNS'
296 f=/etc/ssh/sshd_config
297 grep -xFq "$line" $f || tee -a $f <<<"$line"
298
299
300 # default debian groups (jessie through buster) + adm, root, admin
301 for g in cdrom floppy audio dip video plugdev netdev adm sudo admin; do
302 if getent group $g >/dev/null; then
303 usermod -aG $g iank
304 fi
305 done
306
307 if getent group systemd-journal >/dev/null; then
308 usermod -aG systemd-journal iank
309 fi
310 EOFOUTER
311
312 rm -f $target/etc/resolv.conf
313 ln -s ../run/systemd/resolve/stub-resolv.conf $target/etc/resolv.conf
314 # needed for bitfolk image
315 if [[ -e /a/bin/fai/fai-wrapper ]]; then
316 systemctl enable systemd-resolved
317 systemctl start systemd-resolved
318 fi
319
320
321
322 # reading through the groups that iank is in but user2 isn't,
323 for g in plugdev audio video cdrom; do
324 $ROOTCMD usermod -a -G $g user2
325 done