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