2ecf45b69fe0b4d813d7520aaa04704047e3097f
[automated-distro-installer] / faiserver-setup
1 #!/bin/bash
2 # Copyright (C) 2016 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 # Usage: faiserver-setup
19 #
20 # Initial setup of a fai server on debian. works on localhost.
21 # Set's the current ip as the tftp server. I vaguely remember
22 # that using a hostname does not work.
23 # Separate from running this, faiserver needs to be setup in dns
24 # to point to whatever host this is run on.
25
26 set -eE -o pipefail
27 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
28
29 [[ $EUID == 0 ]] || exec sudo "${BASH_SOURCE}" "$@"
30
31 e() { echo "$@"; "$@"; }
32
33 # tried with stretch at one point, but jessie works for everything,
34 # so no point in changing it. Leaving some code to deal with
35 # stretch hanging around as it will eventually become stable.
36 base=jessie
37 sed="sed -ri --follow-symlinks"
38
39
40 if [[ $base == jessie ]]; then
41 wget -O - http://fai-project.org/download/074BCDE4.asc | apt-key add -
42 cat >/etc/apt/sources.list.d/fai.list <<'EOF'
43 deb http://fai-project.org/download jessie koeln
44 EOF
45 else
46 # if we use stretch, no need for fai-project repo.
47 rm -f /etc/apt/sources.list.d/fai.list
48 fi
49
50 # for ubuntu:
51 #add-apt-repository -y ppa:fai/ppa
52
53 # for debian:
54
55
56 apt-get update
57 # all the dependencies except the dhcp server
58 deps="$(apt-cache show fai-quickstart | grep ^Depends: |head -n 1|\
59 sed -r 's/^Depends:|,|\|[^,]+|isc-dhcp-server//g')"
60
61 # Relevant packages from fai-quickstart depends and fai-server recommends.
62 # I especially do not wait isc-dhcp-server or an inetd
63 apt-get install -y fai-doc nfs-kernel-server tftpd-hpa tar reprepro squashfs-tools binutils
64 apt-get install -y --no-recommends fai-server
65
66 r=http://http.us.debian.org/debian
67 # like default, but scrap httpredir, and nonfree.
68 # All my systems should be able to get along without nonfree
69 # for a base working system afaik.
70 dd of=/etc/fai/apt/sources.list <<EOF
71 deb $r $base main contrib
72 deb http://security.debian.org/debian-security $base/updates main contrib
73 EOF
74
75 if [[ $base == jessie ]]; then
76 tee -a /etc/fai/apt/sources.list <<'EOF'
77 # uncommenting this from the defaults. it's got bug fixes.
78 # repository that may contain newer fai packages for jessie
79 deb http://fai-project.org/download jessie koeln
80 # fix tar https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=819978
81 deb http://ftp.debian.org/debian jessie-backports main
82 EOF
83
84 # note, fai doesn't look at /etc/fai/apt/preferences.d
85 cat >/etc/fai/apt/preferences <<'EOF'
86 Package: tar
87 Pin: release a=jessie-backports
88 Pin-Priority: 500
89 EOF
90 fi
91
92
93 # tried out a stretch base, doesn't work yet.
94 $sed -f - /etc/fai/nfsroot.conf <<EOF
95 s,^( *FAI_DEBOOTSTRAP=).*,\1"$base $r",
96 EOF
97
98 $sed 's/#LOGUSER/LOGUSER/' /etc/fai/fai.conf
99 # from man fai-make-nfsroot,
100 # figured out after partitioning ignored my crypt partition
101
102
103 if ! grep cryptsetup /etc/fai/NFSROOT &>/dev/null; then
104 $sed '/^PACKAGES install$/a cryptsetup' /etc/fai/NFSROOT
105 fi
106 e fai-setup -vf
107 { head -n 1 /srv/fai/nfsroot/root/.ssh/known_hosts | awk '{print $1}' \
108 | tr '\n' ' '; ssh-keyscan localhost | grep -o "ecdsa-sha2-nistp256.*"; \
109 } >>/srv/fai/nfsroot/root/.ssh/known_hosts
110
111 # initially did the basic fai-chboot -Iv $std_arg default
112 # but found in console that it wanted to mount nfsroot
113 # to be the same as my dhcp server.
114 # Figured out to change the root= parameter from googling,
115 # and seeing fai-chboot -L
116 # using hostname failed.
117 # for -f, combined the 2 defaults so it will reboot and print to screen.
118
119 # Add debug to -f flag for more verbose output.
120
121 # make the faiserver also the apt proxy server
122 apt-get -y install apt-cacher-ng
123
124 # background on choosing apt-cacher-ng:
125 # googling around a bit finds 2 main solutions:
126 # http://askubuntu.com/questions/3503/best-way-to-cache-apt-downloads-on-a-lan
127 # apt-cacher-ng doesn't have zeroconf.
128 # It touts having minimal dependencies, but I don't care.
129 # The downside to squid-deb-proxy is that it's config is for specific repos,
130 # you have to add all the repos you use.
131 # That is the main reason I use apt-cacher-ng.
132 # It has a web portal, at http://faiserver:3142/acng-report.html
133
134
135 # random fai note: as far as I can tell, profiles are just for putting
136 # in a selectable boot menu, which I don't want.
137
138 # the logsave prompted because the hostname faiserver was uknown.
139 # Here it was faiserver.lan when running from a faiserver vm.
140 # When running from a normal host with faiserver alias, it was the normal hosts name.
141 $sed 's/(^[^,]+,)\S+/\1faiserver/' /srv/fai/nfsroot/root/.ssh/known_hosts
142 # ditch the logo banner up top which screws with less.
143 touch /srv/fai/nfsroot/.nocolorlogo