better support for systems with no pxe
[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 apt-get update
56 # all the dependencies except the dhcp server
57 deps="$(apt-cache show fai-quickstart | grep ^Depends: |head -n 1|\
58 sed -r 's/^Depends:|,|\|[^,]+|isc-dhcp-server//g')"
59 to_install=()
60 for pkg in $deps; do
61 dpkg -s $pkg &>/dev/null && continue ||:
62 to_install+=($pkg)
63 # just so we have a record.
64 echo `date` $pkg >>/var/log/fai-manually-installed-packages.log
65 done
66 if [[ $to_install ]]; then
67 apt-get -y install ${to_install[@]}
68 fi
69
70 r=http://http.us.debian.org/debian
71 # like default, but scrap httpredir and add suggested newer pkgs in fai-project.org
72 dd of=/etc/fai/apt/sources.list <<EOF
73 deb $r $base main contrib non-free
74 deb http://security.debian.org/debian-security $base/updates main contrib non-free
75 EOF
76
77 if [[ $base == jessie ]]; then
78 tee -a /etc/fai/apt/sources.list <<'EOF'
79 deb http://fai-project.org/download jessie koeln
80 EOF
81 fi
82
83 # tried out a stretch base, doesn't work yet.
84 $sed -f - /etc/fai/nfsroot.conf <<EOF
85 s,^( *FAI_DEBOOTSTRAP=).*,\1"$base $r",
86 EOF
87
88 $sed 's/#LOGUSER/LOGUSER/' /etc/fai/fai.conf
89 # from man fai-make-nfsroot,
90 # figured out after partitioning ignored my crypt partition
91
92
93 if ! grep cryptsetup /etc/fai/NFSROOT &>/dev/null; then
94 $sed '/^PACKAGES install$/a cryptsetup' /etc/fai/NFSROOT
95 fi
96 e fai-setup -vf
97 { head -n 1 /srv/fai/nfsroot/root/.ssh/known_hosts | awk '{print $1}' \
98 | tr '\n' ' '; ssh-keyscan localhost | grep -o "ecdsa-sha2-nistp256.*"; \
99 } >>/srv/fai/nfsroot/root/.ssh/known_hosts
100
101 # initially did the basic fai-chboot -Iv $std_arg default
102 # but found in console that it wanted to mount nfsroot
103 # to be the same as my dhcp server.
104 # Figured out to change the root= parameter from googling,
105 # and seeing fai-chboot -L
106 # using hostname failed.
107 # for -f, combined the 2 defaults so it will reboot and print to screen.
108
109 # Add debug to -f flag for more verbose output.
110
111 # make the faiserver also the apt proxy server
112 apt-get -y install apt-cacher-ng
113
114 # background on choosing apt-cacher-ng:
115 # googling around a bit finds 2 main solutions:
116 # http://askubuntu.com/questions/3503/best-way-to-cache-apt-downloads-on-a-lan
117 # apt-cacher-ng doesn't have zeroconf.
118 # It touts having minimal dependencies, but I don't care.
119 # The downside to squid-deb-proxy is that it's config is for specific repos,
120 # you have to add all the repos you use.
121 # That is the main reason I use apt-cacher-ng.
122 # It has a web portal, at http://faiserver:3142/acng-report.html
123
124
125 # random fai note: as far as I can tell, profiles are just for putting
126 # in a selectable boot menu, which I don't want.
127
128 # the logsave prompted because the hostname faiserver was uknown.
129 # Here it was faiserver.lan when running from a faiserver vm.
130 # When running from a normal host with faiserver alias, it was the normal hosts name.
131 $sed 's/(^[^,]+,)\S+/\1faiserver/' /srv/fai/nfsroot/root/.ssh/known_hosts
132 # ditch the logo banner up top which screws with less.
133 touch /srv/fai/nfsroot/.nocolorlogo