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