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