add copyright, fix arch
[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 # for ubuntu:
30 #add-apt-repository -y ppa:fai/ppa
31
32 # for debian:
33 wget -O - http://fai-project.org/download/074BCDE4.asc | apt-key add -
34 cat >/etc/apt/sources.list.d/fai.list <<'EOF'
35 deb http://fai-project.org/download jessie koeln
36 EOF
37
38 apt-get update
39 # all the dependencies except the dhcp server
40 deps="$(apt-cache show fai-quickstart | grep ^Depends: |head -n 1|\
41 sed -r 's/^Depends:|,|\|[^,]+|isc-dhcp-server//g')"
42 to_install=()
43 for pkg in $deps; do
44 dpkg -s $pkg &>/dev/null && continue ||:
45 to_install+=($pkg)
46 echo $pkg >>/etc/fai/fai-manually-installed-packages
47 done
48 if [[ $to_install ]]; then
49 apt-get -y install ${to_install[@]}
50 fi
51
52 sed="sed -ri --follow-symlinks"
53 $sed 's/^#deb/deb/' /etc/fai/apt/sources.list
54 $sed 's/#LOGUSER/LOGUSER/' /etc/fai/fai.conf
55 # from man fai-make-nfsroot,
56 # figured out after partitioning ignored my crypt partition
57 if ! grep cryptsetup /etc/fai/NFSROOT &>/dev/null; then
58 $sed '/^PACKAGES install$/a cryptsetup' /etc/fai/NFSROOT
59 fi
60 fai-setup -vf
61 { head -n 1 /srv/fai/nfsroot/root/.ssh/known_hosts | awk '{print $1}' \
62 | tr '\n' ' '; ssh-keyscan localhost | grep -o "ecdsa-sha2-nistp256.*"; \
63 } >>/srv/fai/nfsroot/root/.ssh/known_hosts
64
65 # this does not alter the config on a new install
66 $sed 's#^([[:space:]]*TFTP_DIRECTORY[[:space:]]*=).*#\1"/srv/tftp"#' \
67 /etc/default/tftpd-hpa
68
69 service tftpd-hpa restart
70
71 # initially did the basic fai-chboot -Iv $std_arg default
72 # but found in console that it wanted to mount nfsroot
73 # to be the same as my dhcp server.
74 # Figured out to change the root= parameter from googling,
75 # and seeing fai-chboot -L
76 # using hostname failed.
77 # for -f, combined the 2 defaults so it will reboot and print to screen.
78
79 # Add debug to -f flag for more verbose output.
80
81 std_arg="-u nfs://faiserver/srv/fai/config"
82 e fai-chboot -Iv $std_arg default # reset so we are idempotent
83 kernel=$(fai-chboot -L '^default$' | awk '{print $3}')
84 type -t host &>/dev/null || apt-get -y install dnsutils
85 # resolve host using gateway address
86 my_ip=$(host faiserver $(route -n | sed -rn 's/^(0\.){3}0\s+(\S+).*/\2/p') | \
87 sed -rn 's/^\S+ has address //p')
88 k_args=$(fai-chboot -L '^default$' | \
89 sed -r "s/^(\S+\s+){3}(.*root=)(.*)/\2$my_ip:\3/")
90 e fai-chboot -k "$k_args" -v -f verbose,sshd,createvt,reboot $std_arg $kernel default
91
92 # make the faiserver also the apt proxy server
93 apt-get -y install apt-cacher-ng
94
95 # background on choosing apt-cacher-ng:
96 # googling around a bit finds 2 main solutions:
97 # http://askubuntu.com/questions/3503/best-way-to-cache-apt-downloads-on-a-lan
98 # apt-cacher-ng doesn't have zeroconf.
99 # It touts having minimal dependencies, but I don't care.
100 # The downside to squid-deb-proxy is that it's config is for specific repos,
101 # you have to add all the repos you use.
102 # That is the main reason I use apt-cacher-ng.
103 # It has a web portal, at http://faiserver:3142/acng-report.html
104
105
106 # random fai note: as far as I can tell, profiles are just for putting
107 # in a selectable boot menu, which I don't want.
108
109 # the logsave prompted because the hostname faiserver was uknown.
110 # Here it was faiserver.lan when running from a faiserver vm.
111 # When running from a normal host with faiserver alias, it was the normal hosts name.
112 $sed 's/(^[^,]+,)\S+/\1faiserver/' /srv/fai/nfsroot/root/.ssh/known_hosts
113 # ditch the logo banner up top which screws with less.
114 touch /srv/fai/nfsroot/.nocolorlogo