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