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