7c5ce08ceee532c7e038698667f8719b4c299401
[automated-distro-installer] / fai-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 # for ubuntu:
11 #add-apt-repository -y ppa:fai/ppa
12
13 # for debian:
14 wget -O - http://fai-project.org/download/074BCDE4.asc | apt-key add -
15 cat >/etc/apt/sources.list.d/fai.list <<'EOF'
16 deb http://fai-project.org/download jessie koeln
17 EOF
18
19 apt-get update
20 # all the dependencies except the dhcp server
21 apt-get -y install $(apt-cache show fai-quickstart | grep ^Depends: |head -n 1|\
22 sed -r 's/^Depends:|,|\|[^,]+|isc-dhcp-server//g')
23 sed -i 's/^#deb/deb/' /etc/fai/apt/sources.list
24 sed -i 's/#LOGUSER/LOGUSER/' /etc/fai/fai.conf
25 fai-setup -v
26 { head -n 1 /srv/fai/nfsroot/root/.ssh/known_hosts | awk '{print $1}' \
27 | tr '\n' ' '; ssh-keyscan localhost | grep -o "ecdsa-sha2-nistp256.*"; \
28 } >>/srv/fai/nfsroot/root/.ssh/known_hosts
29
30 # from config machine. todo: clean this up.
31 sed -ri 's#^([[:space:]]*TFTP_DIRECTORY[[:space:]]*=).*#\1"/srv/tftp"#' \
32 /etc/default/tftpd-hpa
33
34 service tftpd-hpa restart
35
36 # initially did the basic fai-chboot -Iv $std_arg default
37 # but found in console that it wanted to mount nfsroot
38 # to be the same as my dhcp server.
39 # Figured out to change the root= parameter from googling,
40 # and seeing fai-chboot -L
41 # using hostname failed.
42 # for -f, combined the 2 defaults so it will reboot and print to screen.
43
44 # Add debug to -f flag for more verbose output.
45
46 std_arg="-u nfs://faiserver/srv/fai/config"
47 fai-chboot -Iv $std_arg default
48 kernel=$(fai-chboot -L '^default$' | awk '{print $3}')
49 my_ip=$(getent hosts faiserver | awk '{ print $1 }')
50 k_args=$(fai-chboot -L '^default$' | \
51 sed -r "s/^(\S+\s+){3}(.*root=)(.*)/\2$my_ip:\3/")
52 fai-chboot -k "$k_args" -v -f verbose,sshd,createvt,reboot $std_arg $kernel default
53
54 # make the faiserver also the apt proxy server
55 apt-get install apt-cacher-ng
56
57 # background on choosing apt-cacher-ng:
58 # googling around a bit finds 2 main solutions:
59 # http://askubuntu.com/questions/3503/best-way-to-cache-apt-downloads-on-a-lan
60 # apt-cacher-ng doesn't have zeroconf.
61 # so I'm not sure how smart it will be if the server goes down.
62 # It touts having minimal dependencies, but I don't care.
63 # The downside to squid-deb-proxy is that it's config is for specific repos,
64 # you have to add all the repos you use.
65 # That is the main reason I use apt-cacher-ng.
66 # It has a web portal, at http://faiserver:3142/acng-report.html
67
68
69 # random fai note: as far as I can tell, profiles are just for putting
70 # in a selectable boot menu, which I don't want.
71
72 if [[ ! -e ~/.ssh/id_rsa.pub ]]; then
73 ssh-keygen -t rsa -N ''
74 fi
75 x=$(mktemp); ssh -F /dev/null -oUserKnownHostsFile=$x localhost :
76 cat x | tee -a /srv/fai/nfsroot/root/.ssh/known_hosts