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