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