changes for bullseye
[automated-distro-installer] / faiserver-setup
1 #!/bin/bash
2 # Copyright (C) 2018 Ian Kelling
3
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 x="$(readlink -f "$BASH_SOURCE")"; source "${x%/*}/bash-trace"
19
20 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
21
22 LC_USEBASHRC=t; . ~/.bashrc
23
24 usage() {
25 cat <<EOF
26 usage: ${0##*/} [-h|--help] [BASE_CODENAME] [ARCH]
27 install fai-server on the current machine
28
29 Initial setup of a fai server. works on localhost. Set's the current ip
30 as the tftp server. I vaguely remember that using a hostname does not
31 work. Separate from running this, faiserver needs to be setup in dns to
32 point to whatever host this is run on.
33
34 Default BASE_CODENAME is buster. Default ARCH is 64. The script expects corresponding
35 $BASEFILE_DIR/${UPCASED_BASE_CODENAME}${ARCH}.tar.(gz|xz) to exist, and it must have been
36 generated around the same time as the nfsroot, at least so it has the
37 same kernel version.
38
39 EOF
40 exit $1
41 }
42 case $1 in
43 -h|--help) usage ;;
44 esac
45
46
47 e() { echo "+ $@"; "$@"; }
48
49
50 base=${1:-buster}
51 arch=${2:-64}
52
53 if [[ $base == [[:upper:]] ]]; then
54 echo $0: error: use lowercase base
55 exit 1
56 fi
57
58 basefile=($BASEFILE_DIR/${base^^}${arch^^}.tar.gz)
59 sed="sed -ri --follow-symlinks"
60
61 if [[ ! -e $basefile ]]; then
62 printf "%s\n" "$0: error basefile=$basefile does not exist" >&2
63 exit 1
64 fi
65
66 if [[ ! -d $BASEFILE_DIR ]]; then
67 printf "%s\n" "$0: error BASEFILE_DIR=$BASEFILE_DIR does not exist" >&2
68 exit 1
69 fi
70
71
72 if ! type -p wget &>/dev/null; then
73 apt-get install -y wget
74 fi
75
76 armhf() {
77 [[ $(dpkg --print-architecture) == armhf ]]
78 }
79
80 if grep -xFq 'VERSION="8 (jessie)"' /etc/os-release; then
81 gpg -a --recv-keys 2BF8D9FE074BCDE4; gpg -a --export 2BF8D9FE074BCDE4 | apt-key add -
82 cat >/etc/apt/sources.list.d/fai.list <<'EOF'
83 deb https://fai-project.org/download jessie koeln
84 EOF
85 elif grep -iE 'VERSION=.*(stretch|flidas|xenail|buster|bullseye|etiona)' /etc/os-release; then
86 # fai on ubuntu only has official support using the universe repo, but newer
87 # tends to have less bugs.
88 wget -O - https://fai-project.org/download/2BF8D9FE074BCDE4.asc | apt-key add -
89
90 case $base in
91 stretch|buster|bullseye)
92 cat >/etc/apt/sources.list.d/fai.list <<EOF
93 deb https://fai-project.org/download $base koeln
94 EOF
95 ;;
96 *)
97 echo "$0: error: script needs updating for new base" >&2
98 exit 1
99 ;;
100 esac
101 else
102 rm -f /etc/apt/sources.list.d/fai.list
103 fi
104
105 apt-get update
106
107 # Relevant packages from fai-quickstart depends and fai-server recommends.
108 # I especially do not wait isc-dhcp-server or an inetd. Also excludes
109 # nfs-kernel-server. On an android chroot, we don\'t have nfs in the
110 # kernel, or the ability to install it.
111 # xorriso is for running fai-cd -a, not strictly need for fai-server
112 # perl-tk is for fai-monitor-gui
113 pkgs=(fai-doc tftpd-hpa tar reprepro squashfs-tools binutils xorriso)
114 if modprobe nfsd &>/dev/null; then
115 pkgs+=(nfs-kernel-server)
116 else
117 pkgs+=(apache2)
118 fi
119
120
121 e apt-get install -y ${pkgs[@]}
122 # confnew since we edit /etc/fai/NFSROOT in an automated way
123 # fai-client is already a fai-server dependency, but make sure it gets upgraded
124 e apt-get install --no-install-recommends -y -o Dpkg::Options::=--no-force-confdef -o Dpkg::Options::=--force-confnew fai-server fai-client
125
126 r=http://http.us.debian.org/debian
127 # like default, but scrap httpredir, and nonfree.
128 # All my systems should be able to get along without nonfree
129 # for a base working system afaik.
130
131 cat >/etc/fai/apt/sources.list <<EOF
132 deb $r $base main contrib
133 EOF
134 case $base in
135 jessie|stretch|buster)
136 cat >>/etc/fai/apt/sources.list <<EOF
137 deb http://security.debian.org/debian-security $base/updates main contrib
138 EOF
139 ;;
140 *)
141 # new naming convention
142 cat >>/etc/fai/apt/sources.list <<EOF
143 deb http://security.debian.org/debian-security $base-security main contrib
144 EOF
145 esac
146
147
148 cat >>/etc/fai/apt/sources.list <<EOF
149 # use fai repo. it's commented in the defaults. it's got bug fixes.
150 # and may contain newer packages.
151 deb http://fai-project.org/download $base koeln
152 EOF
153
154 if [[ $base == jessie ]]; then
155 cat >>/etc/fai/apt/sources.list <<'EOF'
156 # fix tar https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=819978
157 deb http://ftp.debian.org/debian jessie-backports main
158 EOF
159 # note, fai doesn\'t look at /etc/fai/apt/preferences.d
160 cat >/etc/fai/apt/preferences <<'EOF'
161 Package: tar
162 Pin: release a=jessie-backports
163 Pin-Priority: 500
164 EOF
165 fi
166
167
168 $sed -f - /etc/fai/nfsroot.conf <<EOF
169 $ a FAI_ROOTPW='$(</q/root/shadow/standard)'
170 /^\s*FAI_ROOTPW/d
171 $ a SSH_IDENTITY=/root/.ssh/home.pub
172 /^\s*SSH_IDENTITY/d
173 s,^( *FAI_DEBOOTSTRAP=).*,\1"$base $r",
174 # add --arch amd64. this is needed on arm system which is
175 # used to install amd64 clients. On amd64 servers, it's redundant.
176 # disabled for now, since creating fai nfsroot on my arm machine
177 # is not working
178 #/--arch amd64/!s/^(\s*FAI_DEBOOTSTRAP_OPTS=")/\1--arch amd64 /
179 EOF
180
181 $sed 's/#LOGUSER/LOGUSER/' /etc/fai/fai.conf
182 $sed -i '/^LOGUSER=/d' /etc/fai/fai.conf
183 $sed -i '/^FAI_FLAGS=/d' /etc/fai/fai.conf
184 echo "FAI_FLAGS=verbose" >>/etc/fai/fai.conf
185 # note if this isnt set, the user isnt created
186 echo "LOGUSER=fai" >>/etc/fai/fai.conf
187
188 # from man fai-make-nfsroot,
189 # figured out after partitioning ignored my crypt partition
190
191
192 cedit /etc/fai/NFSROOT <<'EOF' || [[ $? == 1 ]]
193 # inserted by faserver-setup
194 PACKAGES install
195 cryptsetup
196 # default one is linux-image-server, doesnt exist anymore
197 linux-image-amd64
198 # default is btrfs-tools which doesnt exist anymore
199 btrfs-progs
200 iw
201 # got an error in error.log about not having gpg.
202 # system seemed to still install ok, so i havent tested if this fixes it.
203 gpg
204 EOF
205
206 if armhf; then
207 cd /srv/fai
208 e rm -rf nfsroot
209 e tar Jxf $basefile
210 # background: Can't build the nfsroot on my arm system now. First,
211 # fai-make-nfsroot won't work out of the box. One idea to make it work
212 # is by installing qemu-user-static, then copying qemu-x86_64-static
213 # into the nfsroot, and prepending it to chroot commands in
214 # fai-make-nfsroot, but that fails in odd ways. ls has permissions
215 # problems on reading directories, various programs segfault
216 # immediately, cat can't open a file, etc.
217
218 NFSROOT=/srv/fai/nfsroot
219 TFTPROOT=/srv/tftp/fai
220
221 # test if our copy of setup_tftp has changed in fai-make-nfsroot,
222 # and if not, run it.
223 setup_tftp(){
224
225 # tftp environment
226 local pxebin
227
228 # wheezy path
229 if [ -f $NFSROOT/usr/lib/PXELINUX/pxelinux.0 ]; then
230 pxebin=$NFSROOT/usr/lib/PXELINUX/pxelinux.0
231 else
232 # jessie+ path
233 pxebin=$NFSROOT/usr/lib/syslinux/pxelinux.0
234 fi
235
236 rm -f $NFSROOT/boot/*.bak
237 mkdir -p $TFTPROOT/pxelinux.cfg
238 if ! chmod a+r $NFSROOT/boot/initrd.img-*; then
239 echo "$0: error: No initrd was created. Check the package name of the linux-image package in /etc/fai/NFSROOT."
240 exit 1
241 fi
242 cp -p $v $NFSROOT/boot/vmlinu?-* $NFSROOT/boot/initrd.img-* $TFTPROOT
243 cp -u $pxebin $TFTPROOT
244 if [ -f $NFSROOT/usr/lib/syslinux/modules/bios/ldlinux.c32 ]; then
245 cp -u $NFSROOT/usr/lib/syslinux/modules/bios/ldlinux.c32 $TFTPROOT
246 fi
247 if [ X$verbose = X1 ]; then
248 echo "TFTP environment prepared. Enable DHCP and start the TFTP daemon on root $TFTPROOT."
249 fi
250 }
251 diff -u <(type setup_tftp) <(cat <(sed -n '/^setup_tftp(){/,/^}/p' $(which fai-make-nfsroot) ) - <<'EOF' |bash
252 type setup_tftp
253 EOF
254 )
255 e setup_tftp
256
257 # -g causes skipping set_root_pw() in fai-make-nfsroot, -ag
258 # is the only way to make it run without chrooting. the options
259 # seem contradictory, but it works.
260 e fai-setup -evag
261
262 else # not armhf
263 # note, this copies the -B arg to
264 # /srv/fai/nfsroot/var/tmp/base.tar.xz
265 e fai-setup -evf -B $basefile
266 # fai-setup expert mode avoids writing to /var/log/fai/variables
267 # at least config_src is needed for autodiscover
268 $sed '/^FAI_CONFIGDIR|^FAI_CONFIG_SRC|^LOGUSER/d' /var/log/fai/variables
269 tee -a /var/log/fai/variables <<'EOF'
270 LOGUSER=fai
271 FAI_CONFIGDIR=/srv/fai/config
272 FAI_CONFIG_SRC=nfs://faiserver/srv/fai/config
273 EOF
274 # make the faiserver also the apt proxy server
275 apt-get -y install apt-cacher-ng
276 fi
277
278 rm -f /srv/fai/nfsroot/root/.ssh/known_hosts
279 key=$(ssh-keyscan localhost |& grep -o "ecdsa-sha2-nistp256.*")
280 for ip in faiserver $(ip addr show up| grep -w '^ *inet' | awk '{print $2}'| cut -d / -f 1 | grep -vF 127.0.0.1); do
281 echo "$ip $key" >>/srv/fai/nfsroot/root/.ssh/known_hosts
282 done
283
284
285 # serial console
286 # mainly from
287 # https://wiki.archlinux.org/index.php/working_with_the_serial_console
288 # at runtime, running this from ssh worked:
289 # https://unix.stackexchange.com/questions/242778/what-is-the-easiest-way-to-configure-serial-port-on-linux
290 # stty -F /dev/ttyS0 115200 cs8 -cstopb -parenb
291 # /sbin/agetty 115200 ttyS0 linux
292 # dated info, but validation that this might work on debian:
293 # https://help.ubuntu.com/community/SerialConsoleHowto
294 # note in the nfsroot, systemd is not running.
295 echo "c0:2345:respawn:/sbin/agetty 115200 ttyS0 linux" >>/srv/fai/nfsroot/etc/inittab
296
297 # initially did the basic fai-chboot -Iv $std_arg default
298 # but found in console that it wanted to mount nfsroot
299 # to be the same as my dhcp server.
300 # Figured out to change the root= parameter from googling,
301 # and seeing fai-chboot -L
302 # using hostname failed.
303 # for -f, combined the 2 defaults so it will reboot and print to screen.
304
305 # Add debug to -f flag for more verbose output.
306
307
308 # background on choosing apt-cacher-ng:
309 # googling around a bit finds 2 main solutions:
310 # http://askubuntu.com/questions/3503/best-way-to-cache-apt-downloads-on-a-lan
311 # apt-cacher-ng doesn\'t have zeroconf.
312 # It touts having minimal dependencies, but I don\'t care.
313 # The downside to squid-deb-proxy is that it\'s config is for specific repos,
314 # you have to add all the repos you use.
315 # That is the main reason I use apt-cacher-ng.
316 # It has a web portal, at http://faiserver:3142/acng-report.html
317
318
319 # random fai note: as far as I can tell, profiles are just for putting
320 # in a selectable boot menu, which I don\'t want.
321
322 # the logsave prompted because the hostname faiserver was uknown.
323 # Here it was faiserver.lan when running from a faiserver vm.
324 # When running from a normal host with faiserver alias, it was the normal hosts name.
325 $sed 's/(^[^,]+,)\S+/\1faiserver/' /srv/fai/nfsroot/root/.ssh/known_hosts
326 # ditch the logo banner up top which screws with less.
327 touch /srv/fai/nfsroot/.nocolorlogo