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