misc 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 chroot $FAI_ROOT /usr/bin/apt-cache policy >$tmpfile1
57 fcopy -riBM /etc/apt
58 tmpfile2=$(mktemp)
59 chroot $FAI_ROOT /usr/bin/apt-cache policy >$tmpfile2
60 if ! diff -q $tmpfile1 $tmpfile2; then
61 chroot $FAI_ROOT /usr/bin/apt update
62 fi
63
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
82
83
84 #### misc configurations
85 chroot $FAI_ROOT bash <<'EOFOUTER'
86 if getent group systemd-journal >/dev/null; then
87 # makes the journal be saved to disk.
88 mkdir -p /var/log/journal
89 chmod 755 /var/log/journal
90 fi
91 debconf-set-selections <<EOF
92 kexec-tools kexec-tools/load_kexec boolean false
93 EOF
94 apt-get install -y pxe-kexec
95
96
97 #### begin .ssh setup ###
98 set -x
99 set -eE -o pipefail
100 mkdir -p /home/iank/.ssh
101 f=/root/.ssh/authorized_keys
102 if [[ -e $f ]]; then
103 cp $f /home/iank/.ssh
104 fi
105 chown -R 1000:1000 /home/iank/.ssh
106 chmod -R u=Xrw,og= /home/iank/.ssh
107 rm -rf /root/.ssh
108 # remove broken symlinks or the following cp will fail
109 find /home/iank/.ssh -xtype l -exec rm '{}' \;
110 cp -rL /home/iank/.ssh /root
111 chown -R root:root /root/.ssh
112 chmod 700 /root/.ssh
113 # https://ticktockhouse.svbtle.com/my-obligatory-ubuntu-ssh-agent-post
114 # systemctl --user is not available at fai time, so create the link ourselves
115 d=/home/iank/.config/systemd/user/default.target.wants
116 sudo -u iank mkdir -p $d
117 sudo -u iank ln -sf /usr/lib/systemd/user/ssh-agent.service $d
118 #### end .ssh setup ###
119
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 # bitfolk installer handles the rest
148 case $HOSTNAME in
149 bk) exit 0 ;;
150 esac
151
152
153 if [[ $FAI_ACTION != dirinstall ]] && ! ifclass NOCRYPT; then
154 # luks options, see man systemd-cryptsetup-generator
155 # all i know is that with luks.crypttab=no, swap still timed out on boot.
156 # and with rd.luks.crypttab=no, it works.
157 if ifclass LINODE; then
158 speed=19200
159 cmdline="rd.luks.crypttab=no net.ifnames=0 console=ttyS0,${speed}n8"
160 else
161 speed=115200
162 cmdline="rd.luks.crypttab=no net.ifnames=0 console=ttyS0,${speed}n8 console=tty0"
163 case $HOSTNAME in
164 # per rubens suggestion to make a d16 more stable
165 kd|kw) cmdline+=" pci=realloc=off" ;;
166 esac
167 fi
168
169 cat >$FAI_ROOT/etc/grub.d/40_custom <<EOF
170 #!/bin/sh
171 exec tail -n +3 \$0
172 # This file provides an easy way to add custom menu entries. Simply type the
173 # menu entries you want to add after this comment. Be careful not to change
174 # the 'exec tail' line above.
175
176 # https://www.coreboot.org/Serial_console # tty
177 # but removed unneeded stuff
178
179 serial --speed=$speed
180 terminal_input --append serial
181 terminal_output --append serial
182 EOF
183
184
185 chroot $FAI_ROOT bash <<EOF
186 set -eE -o pipefail
187 # https://askubuntu.com/questions/33416/how-do-i-disable-the-boot-splash-screen-and-only-show-kernel-and-boot-text-inst
188 # we remove quiet and splash, and all thats left is what we want
189
190 if grep -qF "$cmdline" /etc/default/grub; then
191 # already set things, exit
192 exit 0
193 fi
194 sed -ri 's/^ *GRUB_CMDLINE_LINUX_DEFAULT=.*/GRUB_CMDLINE_LINUX_DEFAULT="$cmdline"/' /etc/default/grub
195 # on xenial, no grub is displayed at all. fix that.
196 # found just by noticing this in the config file, and a
197 # warning about it in error.log
198 sed -i '/^ *GRUB_HIDDEN_TIMEOUT/d' /etc/default/grub
199
200 update-grub2
201 EOF
202 fi ##### end != dirinstall && != NOCRYPT
203
204
205 # reading through the groups that iank is in but user2 isn't,
206 for g in plugdev audio video cdrom; do
207 $ROOTCMD usermod -a -G $g user2
208 done
209
210
211
212 ###### begin network setup ####
213
214 # use old names. the idea of them changing between boots has never
215 # happened to me and I usually only have 1 wired or other type.
216 # If I ever do need to care about it, I will.
217 # Strangely this didn't work on kw, so I added kernel cmdline parameter.
218 # https://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames/
219 ln -sf /dev/null $target/etc/systemd/network/99-default.link
220
221 # use networkmanager if this host has wireless.
222
223 if type -p iw &>/dev/null && [[ $(iw dev) ]]; then
224 chroot $FAI_ROOT bash <<EOF
225 apt-get -y install network-manager
226 EOF
227
228 # allow networkmanager to manage interfaces
229 #https://bugs.launchpad.net/ubuntu/+source/network-manager/+bug/1638842
230 touch $target/etc/NetworkManager/conf.d/10-globally-managed-devices.conf
231 # in a default desktop install, it looks like netplan creates this file under
232 # run/NetworkManager/conf.d in early boot.
233
234 # By default, dns=default is set in etiona, and dns is just broken.
235 # Maybe with resolvconf it would work, but theres no need for that.
236 # https://wiki.gnome.org/Projects/NetworkManager/DNS
237 cat >$target/etc/NetworkManager/conf.d/99-iank.conf <<'EOF'
238 [main]
239 dns=systemd-resolved
240 EOF
241
242 else
243 cat > $target/etc/network/interfaces <<-EOF
244 # generated by FAI
245 auto lo eth0
246 iface lo inet loopback
247 iface eth0 inet dhcp
248 iface eth0 inet6 auto
249 EOF
250
251 # previously had an else condition after
252 #elif ifclass VM || ifclass LINODE; then
253 # iface $NIC1 inet manual
254 # iface br0 inet dhcp
255 # bridge_ports $NIC1
256 # bridge_stp off
257 # bridge_maxwait 0
258 # however, on t9, on startup, br0, became
259 # rename1 and didn't come up. i dunno why,
260 # but the bridge is for vms that I rarely use,
261 # so not bothering to figure it out.
262
263
264 fi
265
266 rm -f $target/etc/resolv.conf
267 ln -s ../run/systemd/resolve/stub-resolv.conf $target/etc/resolv.conf
268 # needed for bitfolk image
269 if [[ -e /a/bin/fai/fai-wrapper ]]; then
270 systemctl enable systemd-resolved
271 systemctl start systemd-resolved
272 fi
273
274 if ifclass LINODE; then
275 mkdir -p $target/etc/initramfs-tools/conf.d
276 cat >$target/etc/initramfs-tools/conf.d/mine <<EOF
277 # dhcp in initramfs doesn't work on linode. i dunno why, whatever.
278 # man 5 initramfs.conf
279 # /usr/share/doc/klibc-utils/README.ipconfig.gz
280 # /usr/share/initramfs-tools/scripts/functions
281 IP=$linode_ip::$linode_gw:255.255.255.0::eth0:off
282 EOF
283 fi
284
285 # I prefer to stick with ifup/down for now. a. networkd is not in its
286 # own package, so cant use in other init systems. b. it works fine.
287 chroot $FAI_ROOT bash <<EOF
288 systemctl disable systemd-networkd.socket systemd-networkd networkd-dispatcher systemd-networkd-wait-online
289 systemctl mask systemd-networkd.socket systemd-networkd networkd-dispatcher systemd-networkd-wait-online
290 EOF
291
292 ##### end network setup #####