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