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