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