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