fixup initial faiserver bootstrap & small bugs
[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 # Initial setup of a fai server on debian. works on localhost.
19 # Set's the current ip as the tftp server. I vaguely remember
20 # that using a hostname does not work
21 # Requires changing dns to point faiserver and running fai-redep
22
23 set -eE -o pipefail
24 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
25
26 [[ $EUID == 0 ]] || exec sudo "${BASH_SOURCE}" "$@"
27
28 e() { echo "$@"; "$@"; }
29
30 base=${1:-jessie}
31 sed="sed -ri --follow-symlinks"
32
33
34 if [[ $base == jessie ]]; then
35 wget -O - http://fai-project.org/download/074BCDE4.asc | apt-key add -
36 cat >/etc/apt/sources.list.d/fai.list <<'EOF'
37 deb http://fai-project.org/download jessie koeln
38 EOF
39 else
40 rm -f /etc/apt/sources.list.d/fai.list
41 fi
42
43 # for ubuntu:
44 #add-apt-repository -y ppa:fai/ppa
45
46 # for debian:
47
48 apt-get update
49 # all the dependencies except the dhcp server
50 deps="$(apt-cache show fai-quickstart | grep ^Depends: |head -n 1|\
51 sed -r 's/^Depends:|,|\|[^,]+|isc-dhcp-server//g')"
52 to_install=()
53 for pkg in $deps; do
54 dpkg -s $pkg &>/dev/null && continue ||:
55 to_install+=($pkg)
56 # just so we have a record.
57 echo `date` $pkg >>/var/log/fai-manually-installed-packages.log
58 done
59 if [[ $to_install ]]; then
60 apt-get -y install ${to_install[@]}
61 fi
62
63 r=http://http.us.debian.org/debian
64 # like default, but scrap httpredir and add suggested newer pkgs in fai-project.org
65 dd of=/etc/fai/apt/sources.list <<EOF
66 deb $r $base main contrib non-free
67 deb http://security.debian.org/debian-security $base/updates main contrib non-free
68
69 EOF
70
71 if [[ $base == jessie ]]; then
72 tee -a /etc/fai/apt/sources.list <<'EOF'
73 deb http://fai-project.org/download jessie koeln
74 EOF
75 fi
76
77 # tried out a stretch base, doesn't work yet.
78 $sed -f - /etc/fai/nfsroot.conf <<EOF
79 s,^( *FAI_DEBOOTSTRAP=).*,\1"$base $r",
80 EOF
81
82 $sed 's/#LOGUSER/LOGUSER/' /etc/fai/fai.conf
83 # from man fai-make-nfsroot,
84 # figured out after partitioning ignored my crypt partition
85
86
87 if ! grep cryptsetup /etc/fai/NFSROOT &>/dev/null; then
88 $sed '/^PACKAGES install$/a cryptsetup' /etc/fai/NFSROOT
89 fi
90 e fai-setup -vf
91 { head -n 1 /srv/fai/nfsroot/root/.ssh/known_hosts | awk '{print $1}' \
92 | tr '\n' ' '; ssh-keyscan localhost | grep -o "ecdsa-sha2-nistp256.*"; \
93 } >>/srv/fai/nfsroot/root/.ssh/known_hosts
94
95 # this does not alter the config on a new install
96 $sed 's#^([[:space:]]*TFTP_DIRECTORY[[:space:]]*=).*#\1"/srv/tftp"#' \
97 /etc/default/tftpd-hpa
98
99 service tftpd-hpa restart
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 std_arg="-u nfs://faiserver/srv/fai/config"
112 e fai-chboot -Iv $std_arg default # reset so we are idempotent
113 kernel=$(fai-chboot -L '^default$' | awk '{print $3}')
114 type -t host &>/dev/null || apt-get -y install dnsutils
115 # resolve host using gateway address
116 my_ip=$(host faiserver $(route -n | sed -rn 's/^(0\.){3}0\s+(\S+).*/\2/p') | \
117 sed -rn 's/^\S+ has address //p')
118 k_args=$(fai-chboot -L '^default$' | \
119 sed -r "s/^(\S+\s+){3}(.*root=)(.*)/\2$my_ip:\3/")
120 e fai-chboot -k "$k_args" -v -f verbose,sshd,createvt,reboot $std_arg $kernel default
121
122 # make the faiserver also the apt proxy server
123 apt-get -y install apt-cacher-ng
124
125 # background on choosing apt-cacher-ng:
126 # googling around a bit finds 2 main solutions:
127 # http://askubuntu.com/questions/3503/best-way-to-cache-apt-downloads-on-a-lan
128 # apt-cacher-ng doesn't have zeroconf.
129 # It touts having minimal dependencies, but I don't care.
130 # The downside to squid-deb-proxy is that it's config is for specific repos,
131 # you have to add all the repos you use.
132 # That is the main reason I use apt-cacher-ng.
133 # It has a web portal, at http://faiserver:3142/acng-report.html
134
135
136 # random fai note: as far as I can tell, profiles are just for putting
137 # in a selectable boot menu, which I don't want.
138
139 # the logsave prompted because the hostname faiserver was uknown.
140 # Here it was faiserver.lan when running from a faiserver vm.
141 # When running from a normal host with faiserver alias, it was the normal hosts name.
142 $sed 's/(^[^,]+,)\S+/\1faiserver/' /srv/fai/nfsroot/root/.ssh/known_hosts
143 # ditch the logo banner up top which screws with less.
144 touch /srv/fai/nfsroot/.nocolorlogo