just docs
[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 # Usage: faiserver-setup
19 #
20 # Initial setup of a fai server on debian. works on localhost.
21 # Set's the current ip as the tftp server. I vaguely remember
22 # that using a hostname does not work.
23 # Separate from running this, faiserver needs to be setup in dns
24 # to point to whatever host this is run on.
25
26 set -eE -o pipefail
27 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
28
29 [[ $EUID == 0 ]] || exec sudo "${BASH_SOURCE}" "$@"
30
31 e() { echo "$@"; "$@"; }
32
33 # tried with stretch at one point, but jessie works for everything,
34 # so no point in changing it. Leaving some code to deal with
35 # stretch hanging around as it will eventually become stable.
36 base=jessie
37 sed="sed -ri --follow-symlinks"
38
39
40 if [[ $base == jessie ]]; then
41 wget -O - http://fai-project.org/download/074BCDE4.asc | apt-key add -
42 cat >/etc/apt/sources.list.d/fai.list <<'EOF'
43 deb http://fai-project.org/download jessie koeln
44 EOF
45 else
46 # if we use stretch, no need for fai-project repo.
47 rm -f /etc/apt/sources.list.d/fai.list
48 fi
49
50 # for ubuntu:
51 #add-apt-repository -y ppa:fai/ppa
52
53 # for debian:
54
55
56 apt-get update
57 # all the dependencies except the dhcp server
58 deps="$(apt-cache show fai-quickstart | grep ^Depends: |head -n 1|\
59 sed -r 's/^Depends:|,|\|[^,]+|isc-dhcp-server//g')"
60 to_install=(tar)
61 for pkg in $deps; do
62 dpkg -s $pkg &>/dev/null && continue ||:
63 to_install+=($pkg)
64 # just so we have a record.
65 echo `date` $pkg >>/var/log/fai-manually-installed-packages.log
66 done
67 if [[ $to_install ]]; then
68 apt-get -y install ${to_install[@]}
69 fi
70
71 r=http://http.us.debian.org/debian
72 # like default, but scrap httpredir, and nonfree.
73 # All my systems should be able to get along without nonfree
74 # for a base working system afaik.
75 dd of=/etc/fai/apt/sources.list <<EOF
76 deb $r $base main contrib
77 deb http://security.debian.org/debian-security $base/updates main contrib
78 EOF
79
80 if [[ $base == jessie ]]; then
81 tee -a /etc/fai/apt/sources.list <<'EOF'
82 # uncommenting this from the defaults. it's got bug fixes.
83 # repository that may contain newer fai packages for jessie
84 deb http://fai-project.org/download jessie koeln
85 # fix tar https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=819978
86 deb http://ftp.debian.org/debian jessie-backports main
87 EOF
88
89 # note, fai doesn't look at /etc/fai/apt/preferences.d
90 cat >/etc/fai/apt/preferences <<'EOF'
91 Package: tar
92 Pin: release a=jessie-backports
93 Pin-Priority: 500
94 EOF
95 fi
96
97
98 # tried out a stretch base, doesn't work yet.
99 $sed -f - /etc/fai/nfsroot.conf <<EOF
100 s,^( *FAI_DEBOOTSTRAP=).*,\1"$base $r",
101 EOF
102
103 $sed 's/#LOGUSER/LOGUSER/' /etc/fai/fai.conf
104 # from man fai-make-nfsroot,
105 # figured out after partitioning ignored my crypt partition
106
107
108 if ! grep cryptsetup /etc/fai/NFSROOT &>/dev/null; then
109 $sed '/^PACKAGES install$/a cryptsetup' /etc/fai/NFSROOT
110 fi
111 e fai-setup -vf
112 { head -n 1 /srv/fai/nfsroot/root/.ssh/known_hosts | awk '{print $1}' \
113 | tr '\n' ' '; ssh-keyscan localhost | grep -o "ecdsa-sha2-nistp256.*"; \
114 } >>/srv/fai/nfsroot/root/.ssh/known_hosts
115
116 # initially did the basic fai-chboot -Iv $std_arg default
117 # but found in console that it wanted to mount nfsroot
118 # to be the same as my dhcp server.
119 # Figured out to change the root= parameter from googling,
120 # and seeing fai-chboot -L
121 # using hostname failed.
122 # for -f, combined the 2 defaults so it will reboot and print to screen.
123
124 # Add debug to -f flag for more verbose output.
125
126 # make the faiserver also the apt proxy server
127 apt-get -y install apt-cacher-ng
128
129 # background on choosing apt-cacher-ng:
130 # googling around a bit finds 2 main solutions:
131 # http://askubuntu.com/questions/3503/best-way-to-cache-apt-downloads-on-a-lan
132 # apt-cacher-ng doesn't have zeroconf.
133 # It touts having minimal dependencies, but I don't care.
134 # The downside to squid-deb-proxy is that it's config is for specific repos,
135 # you have to add all the repos you use.
136 # That is the main reason I use apt-cacher-ng.
137 # It has a web portal, at http://faiserver:3142/acng-report.html
138
139
140 # random fai note: as far as I can tell, profiles are just for putting
141 # in a selectable boot menu, which I don't want.
142
143 # the logsave prompted because the hostname faiserver was uknown.
144 # Here it was faiserver.lan when running from a faiserver vm.
145 # When running from a normal host with faiserver alias, it was the normal hosts name.
146 $sed 's/(^[^,]+,)\S+/\1faiserver/' /srv/fai/nfsroot/root/.ssh/known_hosts
147 # ditch the logo banner up top which screws with less.
148 touch /srv/fai/nfsroot/.nocolorlogo