host config changes
[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 ## duplicated in ssh-emacs-setup
122 # done here so its setup earlier for convenience
123 line='AcceptEnv INSIDE_EMACS BRC COLUMNS'
124 f=/etc/ssh/sshd_config
125 grep -xFq "$line" $f || tee -a $f <<<"$line"
126
127
128 # default debian groups (jessie through buster) + adm, sudo, root
129 for g in cdrom floppy audio dip video plugdev netdev adm sudo; do
130 if getent gropu $g >/dev/null; then
131 usermod -aG $g iank
132 fi
133 done
134
135 if getent group systemd-journal >/dev/null; then
136 usermod -aG systemd-journal iank
137 fi
138
139
140 # this is usefull. Only thing reason I see this being disabled by default is
141 # that a normal user can disrupt the system, eg cause a reboot.
142 sed -i '$a kernel.sysrq=1
143 /^kernel.sysrq=/d' /etc/sysctl.conf
144 EOFOUTER
145
146
147 rm -f $target/etc/resolv.conf
148 ln -s ../run/systemd/resolve/stub-resolv.conf $target/etc/resolv.conf
149 # needed for bitfolk image
150 if [[ -e /a/bin/fai/fai-wrapper ]]; then
151 systemctl enable systemd-resolved
152 systemctl start systemd-resolved
153 fi
154
155
156 # bitfolk installer handles the rest
157 case $HOSTNAME in
158 bk|je) exit 0 ;;
159 esac
160
161
162 if [[ $FAI_ACTION != dirinstall ]] && ! ifclass NOCRYPT; then
163 # luks options, see man systemd-cryptsetup-generator
164 # all i know is that with luks.crypttab=no, swap still timed out on boot.
165 # and with rd.luks.crypttab=no, it works.
166 if ifclass LINODE; then
167 speed=19200
168 cmdline="rd.luks.crypttab=no net.ifnames=0 console=ttyS0,${speed}n8"
169 else
170 speed=115200
171 cmdline="rd.luks.crypttab=no net.ifnames=0 console=ttyS0,${speed}n8 console=tty0"
172 case $HOSTNAME in
173 # per rubens suggestion to make a d16 more stable
174 kd|kw) cmdline+=" pci=realloc=off" ;;
175 esac
176 fi
177
178 cat >$FAI_ROOT/etc/grub.d/40_custom <<EOF
179 #!/bin/sh
180 exec tail -n +3 \$0
181 # This file provides an easy way to add custom menu entries. Simply type the
182 # menu entries you want to add after this comment. Be careful not to change
183 # the 'exec tail' line above.
184
185 # https://www.coreboot.org/Serial_console # tty
186 # but removed unneeded stuff
187
188 serial --speed=$speed
189 terminal_input --append serial
190 terminal_output --append serial
191 EOF
192
193
194 chroot $FAI_ROOT bash <<EOF
195 set -eE -o pipefail
196 # https://askubuntu.com/questions/33416/how-do-i-disable-the-boot-splash-screen-and-only-show-kernel-and-boot-text-inst
197 # we remove quiet and splash, and all thats left is what we want
198
199 if grep -qF "$cmdline" /etc/default/grub; then
200 # already set things, exit
201 exit 0
202 fi
203 sed -ri 's/^ *GRUB_CMDLINE_LINUX_DEFAULT=.*/GRUB_CMDLINE_LINUX_DEFAULT="$cmdline"/' /etc/default/grub
204 # on xenial, no grub is displayed at all. fix that.
205 # found just by noticing this in the config file, and a
206 # warning about it in error.log
207 sed -i '/^ *GRUB_HIDDEN_TIMEOUT/d' /etc/default/grub
208
209 update-grub2
210 EOF
211 fi ##### end != dirinstall && != NOCRYPT
212
213
214 # reading through the groups that iank is in but user2 isn't,
215 for g in plugdev audio video cdrom; do
216 $ROOTCMD usermod -a -G $g user2
217 done
218
219
220
221 ###### begin network setup ####
222
223 # use old names. the idea of them changing between boots has never
224 # happened to me and I usually only have 1 wired or other type.
225 # If I ever do need to care about it, I will.
226 # Strangely this didn't work on kw, so I added kernel cmdline parameter.
227 # https://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames/
228 ln -sf /dev/null $target/etc/systemd/network/99-default.link
229
230 # use networkmanager if this host has wireless.
231
232 if type -p iw &>/dev/null && [[ $(iw dev) ]]; then
233 chroot $FAI_ROOT bash <<EOF
234 apt-get -y install network-manager
235 EOF
236
237 # allow networkmanager to manage interfaces
238 #https://bugs.launchpad.net/ubuntu/+source/network-manager/+bug/1638842
239 touch $target/etc/NetworkManager/conf.d/10-globally-managed-devices.conf
240 # in a default desktop install, it looks like netplan creates this file under
241 # run/NetworkManager/conf.d in early boot.
242
243 # By default, dns=default is set in etiona, and dns is just broken.
244 # Maybe with resolvconf it would work, but theres no need for that.
245 # https://wiki.gnome.org/Projects/NetworkManager/DNS
246 cat >$target/etc/NetworkManager/conf.d/99-iank.conf <<'EOF'
247 [main]
248 dns=systemd-resolved
249 EOF
250 if [[ $HOSTNAME == frodo ]]; then
251 cat > $target/etc/network/interfaces <<-EOF
252 # generated by FAI
253 auto lo eth0
254 iface lo inet loopback
255 iface eth0 inet static
256 address 10.3.0.2/16
257 EOF
258 fi
259
260 else
261 cat > $target/etc/network/interfaces <<-EOF
262 # generated by FAI
263 auto lo eth0
264 iface lo inet loopback
265 iface eth0 inet dhcp
266 iface eth0 inet6 auto
267 EOF
268
269 # previously had an else condition after
270 #elif ifclass VM || ifclass LINODE; then
271 # iface $NIC1 inet manual
272 # iface br0 inet dhcp
273 # bridge_ports $NIC1
274 # bridge_stp off
275 # bridge_maxwait 0
276 # however, on t9, on startup, br0, became
277 # rename1 and didn't come up. i dunno why,
278 # but the bridge is for vms that I rarely use,
279 # so not bothering to figure it out.
280
281
282 fi
283
284 if ifclass LINODE; then
285 mkdir -p $target/etc/initramfs-tools/conf.d
286 cat >$target/etc/initramfs-tools/conf.d/mine <<EOF
287 # dhcp in initramfs doesn't work on linode. i dunno why, whatever.
288 # man 5 initramfs.conf
289 # /usr/share/doc/klibc-utils/README.ipconfig.gz
290 # /usr/share/initramfs-tools/scripts/functions
291 IP=$linode_ip::$linode_gw:255.255.255.0::eth0:off
292 EOF
293
294
295 if [[ $HOSTNAME == li ]]; then
296
297 cat > $target/etc/network/interfaces <<-EOF
298 # generated by FAI
299 auto lo eth0
300 iface lo inet loopback
301 iface eth0 inet dhcp
302 # for the standard network config, uncomment this and comment the lines after it.
303 #iface eth0 inet6 auto
304
305 iface eth0 inet6 static
306 # this is really a /128. it seems like we need to assign it for ipv6 to work.
307 address 2600:3c00::f03c:91ff:fe6d:baf8/64
308 gateway fe80::1
309
310 iface eth0 inet6 static
311 # from a requested /64 pool
312 address 2600:3c00:e000:280::2/64
313 EOF
314 fi
315
316 fi
317
318 # I prefer to stick with ifup/down for now. a. networkd is not in its
319 # own package, so cant use in other init systems. b. it works fine.
320 chroot $FAI_ROOT bash <<EOF
321 systemctl disable systemd-networkd.socket systemd-networkd networkd-dispatcher systemd-networkd-wait-online
322 systemctl mask systemd-networkd.socket systemd-networkd networkd-dispatcher systemd-networkd-wait-online
323 EOF
324
325 ##### end network setup #####