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