minor fixes and improvements
[automated-distro-installer] / fai / config / scripts / GRUB_PC / 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 -riBM /boot
28 # this is also done by FABASE/10-misc by default.
29 fcopy -riBM /root
30
31
32 src=$FAI/distro-install-common/shadow
33 dst=/q/root/shadow
34 if [[ ! -e $dst && -e $src ]]; then
35 # outside of fai context, we skip this
36 mkdir -p $dst
37 mount -o bind $src $dst
38 fi
39
40 $FAI/distro-install-common/end
41
42
43
44 if ifclass VOL_STRETCH_BOOTSTRAP; then
45 fcopy -riM /etc/systemd/system
46 chroot $FAI_ROOT bash <<'EOFOUTER'
47 systemctl enable fai_check.service
48 EOFOUTER
49 exit 0 # avoid unnecessary stuff in bootstrap vol
50 fi
51
52
53 # these get copied in an earlier stage by fai, but leaving it here since
54 # I run this as a single post-fai script to update things that have changed.
55 tmpfile1=$(mktemp)
56 # this can fail if we need an apt update
57 chroot $FAI_ROOT /usr/bin/apt-cache policy >$tmpfile1 ||:
58 fcopy -riBM /etc/apt
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
65 # outside of fai, this seems to regularly lead to
66 # E: Could not get lock /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable)
67 # so add a sleep. 1 sec is probably way more than needed.
68 sleep 1
69 f=$FAI_ROOT/var/cache/apt/pkgcache.bin
70 if [[ ! -r $f ]] || (( $(( $(date +%s) - $(stat -c %Y $f ) )) > 60*60*2 )); then
71 i=0
72 while fuser $FAI_ROOT/var/lib/dpkg/lock &>/dev/null; do
73 sleep 1
74 i=$(( i+1 ))
75 if (( i > 300 )); then
76 echo "error: timed out waiting for /var/lib/dpkg/lock" >&2
77 exit 1
78 fi
79 $ROOTCMD apt-get update
80 done
81 fi
82
83
84
85 #### misc configurations
86 chroot $FAI_ROOT bash <<'EOFOUTER'
87 if getent group systemd-journal >/dev/null; then
88 # makes the journal be saved to disk.
89 mkdir -p /var/log/journal
90 chmod 755 /var/log/journal
91 fi
92 debconf-set-selections <<EOF
93 kexec-tools kexec-tools/load_kexec boolean false
94 EOF
95 apt-get install -y pxe-kexec
96
97
98 #### begin .ssh setup ###
99 set -x
100 set -eE -o pipefail
101 mkdir -p /home/iank/.ssh
102 f=/root/.ssh/authorized_keys
103 if [[ -e $f ]]; then
104 cp $f /home/iank/.ssh
105 fi
106 chown -R 1000:1000 /home/iank/.ssh
107 chmod -R u=Xrw,og= /home/iank/.ssh
108 rm -rf /root/.ssh
109 # remove broken symlinks or the following cp will fail
110 find /home/iank/.ssh -xtype l -exec rm '{}' \;
111 cp -rL /home/iank/.ssh /root
112 chown -R root:root /root/.ssh
113 chmod 700 /root/.ssh
114 # https://ticktockhouse.svbtle.com/my-obligatory-ubuntu-ssh-agent-post
115 # systemctl --user is not available at fai time, so create the link ourselves
116 d=/home/iank/.config/systemd/user/default.target.wants
117 sudo -u iank mkdir -p $d
118 sudo -u iank ln -sf /usr/lib/systemd/user/ssh-agent.service $d
119 #### end .ssh setup ###
120
121
122 ## duplicated in ssh-emacs-setup
123 # done here so its setup earlier for convenience
124 line='AcceptEnv INSIDE_EMACS BRC COLUMNS'
125 f=/etc/ssh/sshd_config
126 grep -xFq "$line" $f || tee -a $f <<<"$line"
127
128
129 # default debian groups (jessie through buster) + adm, sudo, root
130 for g in cdrom floppy audio dip video plugdev netdev adm sudo; do
131 if getent gropu $g >/dev/null; then
132 usermod -aG $g iank
133 fi
134 done
135
136 if getent group systemd-journal >/dev/null; then
137 usermod -aG systemd-journal iank
138 fi
139
140
141 # this is usefull. Only thing reason I see this being disabled by default is
142 # that a normal user can disrupt the system, eg cause a reboot.
143 sed -i '$a kernel.sysrq=1
144 /^kernel.sysrq=/d' /etc/sysctl.conf
145 EOFOUTER
146
147
148 rm -f $target/etc/resolv.conf
149 ln -s ../run/systemd/resolve/stub-resolv.conf $target/etc/resolv.conf
150 # needed for bitfolk image
151 if [[ -e /a/bin/fai/fai-wrapper ]]; then
152 systemctl enable systemd-resolved
153 systemctl start systemd-resolved
154 fi
155
156
157 # bitfolk installer handles the rest
158 case $HOSTNAME in
159 bk|je) exit 0 ;;
160 esac
161
162
163 if [[ $FAI_ACTION != dirinstall ]] && ! ifclass NOCRYPT; then
164 # luks options, see man systemd-cryptsetup-generator
165 # all i know is that with luks.crypttab=no, swap still timed out on boot.
166 # and with rd.luks.crypttab=no, it works.
167 if ifclass LINODE; then
168 speed=19200
169 cmdline="rd.luks.crypttab=no net.ifnames=0 console=ttyS0,${speed}n8"
170 else
171 speed=115200
172 cmdline="rd.luks.crypttab=no net.ifnames=0 console=ttyS0,${speed}n8 console=tty0"
173 case $HOSTNAME in
174 # per rubens suggestion to make a d16 more stable
175 kd|kw) cmdline+=" pci=realloc=off" ;;
176 esac
177 fi
178
179 cat >$FAI_ROOT/etc/grub.d/40_custom <<EOF
180 #!/bin/sh
181 exec tail -n +3 \$0
182 # This file provides an easy way to add custom menu entries. Simply type the
183 # menu entries you want to add after this comment. Be careful not to change
184 # the 'exec tail' line above.
185
186 # https://www.coreboot.org/Serial_console # tty
187 # but removed unneeded stuff
188
189 serial --speed=$speed
190 terminal_input --append serial
191 terminal_output --append serial
192 EOF
193
194
195 chroot $FAI_ROOT bash <<EOF
196 set -eE -o pipefail
197 # https://askubuntu.com/questions/33416/how-do-i-disable-the-boot-splash-screen-and-only-show-kernel-and-boot-text-inst
198 # we remove quiet and splash, and all thats left is what we want
199
200 if grep -qF "$cmdline" /etc/default/grub; then
201 # already set things, exit
202 exit 0
203 fi
204 sed -ri 's/^ *GRUB_CMDLINE_LINUX_DEFAULT=.*/GRUB_CMDLINE_LINUX_DEFAULT="$cmdline"/' /etc/default/grub
205 # on xenial, no grub is displayed at all. fix that.
206 # found just by noticing this in the config file, and a
207 # warning about it in error.log
208 sed -i '/^ *GRUB_HIDDEN_TIMEOUT/d' /etc/default/grub
209
210 update-grub2
211 EOF
212 fi ##### end != dirinstall && != NOCRYPT
213
214
215 # reading through the groups that iank is in but user2 isn't,
216 for g in plugdev audio video cdrom; do
217 $ROOTCMD usermod -a -G $g user2
218 done
219
220
221
222 ###### begin network setup ####
223
224 # use old names. the idea of them changing between boots has never
225 # happened to me and I usually only have 1 wired or other type.
226 # If I ever do need to care about it, I will.
227 # Strangely this didn't work on kw, so I added kernel cmdline parameter.
228 # https://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames/
229 ln -sf /dev/null $target/etc/systemd/network/99-default.link
230
231 # use networkmanager if this host has wireless.
232
233 if type -p iw &>/dev/null && [[ $(iw dev) ]]; then
234 chroot $FAI_ROOT bash <<EOF
235 apt-get -y install network-manager
236 EOF
237
238 # allow networkmanager to manage interfaces
239 #https://bugs.launchpad.net/ubuntu/+source/network-manager/+bug/1638842
240 touch $target/etc/NetworkManager/conf.d/10-globally-managed-devices.conf
241 # in a default desktop install, it looks like netplan creates this file under
242 # run/NetworkManager/conf.d in early boot.
243
244 # By default, dns=default is set in etiona, and dns is just broken.
245 # Maybe with resolvconf it would work, but theres no need for that.
246 # https://wiki.gnome.org/Projects/NetworkManager/DNS
247 cat >$target/etc/NetworkManager/conf.d/99-iank.conf <<'EOF'
248 [main]
249 dns=systemd-resolved
250 EOF
251
252 else
253 cat > $target/etc/network/interfaces <<-EOF
254 # generated by FAI
255 auto lo eth0
256 iface lo inet loopback
257 iface eth0 inet dhcp
258 iface eth0 inet6 auto
259 EOF
260
261 # previously had an else condition after
262 #elif ifclass VM || ifclass LINODE; then
263 # iface $NIC1 inet manual
264 # iface br0 inet dhcp
265 # bridge_ports $NIC1
266 # bridge_stp off
267 # bridge_maxwait 0
268 # however, on t9, on startup, br0, became
269 # rename1 and didn't come up. i dunno why,
270 # but the bridge is for vms that I rarely use,
271 # so not bothering to figure it out.
272
273
274 fi
275
276 if ifclass LINODE; then
277 mkdir -p $target/etc/initramfs-tools/conf.d
278 cat >$target/etc/initramfs-tools/conf.d/mine <<EOF
279 # dhcp in initramfs doesn't work on linode. i dunno why, whatever.
280 # man 5 initramfs.conf
281 # /usr/share/doc/klibc-utils/README.ipconfig.gz
282 # /usr/share/initramfs-tools/scripts/functions
283 IP=$linode_ip::$linode_gw:255.255.255.0::eth0:off
284 EOF
285
286
287 if [[ $HOSTNAME == li ]]; then
288
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 EOF
306 fi
307
308 fi
309
310 # I prefer to stick with ifup/down for now. a. networkd is not in its
311 # own package, so cant use in other init systems. b. it works fine.
312 chroot $FAI_ROOT bash <<EOF
313 systemctl disable systemd-networkd.socket systemd-networkd networkd-dispatcher systemd-networkd-wait-online
314 systemctl mask systemd-networkd.socket systemd-networkd networkd-dispatcher systemd-networkd-wait-online
315 EOF
316
317 ##### end network setup #####