add rescue, fix default pxe, ubuntu locale
[automated-distro-installer] / fai-redep
1 #!/bin/bash -l
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 set -eE -o pipefail
18 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
19
20 x="$(readlink -f "$BASH_SOURCE")"; cd ${x%/*}
21
22 usage() {
23 cat <<EOF
24 usage: ${0##*/} [-h|--help] [HOST]
25 Deploy fai config (the one in nfs) to HOST or default faiserver
26 EOF
27 exit $1
28 }
29 case $1 in
30 -h|--help) usage ;;
31 esac
32
33 host=${1:-faiserver}
34
35
36 # i use faiserver as a dns alias, but ssh key is associated with
37 # a canonical hostname and we will have ssh warning spam unless we
38 # use it, so look it up just to avoid the warning spam.
39 faiserver_host=$(chost $host) || faiserver_host=$host
40
41 rsync -rl --delete --relative --exclude /fai/config/basefiles/ fai/config root@$faiserver_host:/srv
42
43
44 scp -q ~/.ssh/id_rsa.pub \
45 root@$faiserver_host:/srv/fai/config/files/root/.ssh/authorized_keys/GRUB_PC
46 # todo: automatically disable faiserver after a period so
47 # these files are not exposed.
48 s scp -qr /q/root/luks /q/root/shadow \
49 root@$faiserver_host:/srv/fai/config/distro-install-common
50
51 # should tar ssh all the files, but these ones really justified it
52 tar -cz /p/c/machine_specific/*/filesystem/etc/ssh | \
53 ssh root@$faiserver_host tar -xz -C /srv/fai/config/distro-install-common
54
55
56 # built BELENOS basefile with mk-basefile -J BELENOS64. it's stored in
57 # it's own repo which is published alongside this one called
58 # fai-basefiles due to being a large binary file.
59
60 declare -A sums
61 while read -r sum file; do
62 sums[$file]=$sum
63 done < <(cat /a/bin/fai-basefiles/md5sums.txt)
64
65 { timeout 2 curl -s http://fai-project.org/download/basefiles/md5sums.txt ||:; } |
66 while read -r sum file; do
67 if [[ ${sums[$file]} && ${sums[$file]} != $sum ]]; then
68 echo "${0##*/}: WARNING!!!!!!!!! NEW UPSTREAM BASEFILE: $file"
69 fi
70 done
71 rsync -r --delete /a/bin/fai-basefiles/basefiles root@$faiserver_host:/srv/fai/config
72 ssh root@$faiserver_host bash <<'EOF'
73 set -eE -o pipefail
74 set -x
75 # make it the root because pxe-kexec only looks there.
76 # It wouldn't be too hard to change if we needed.
77 # We could also just dump things in /srv/tftp, but fai
78 # has some defaults, which I don't even use, which expect
79 # the other directory, so it's kind of a tossup, whatever.
80 sed -ri 's,^ *(TFTP_DIRECTORY=).*,\1"/srv/tftp/fai",' /etc/default/tftpd-hpa
81 systemctl restart tftpd-hpa
82 chmod 644 /srv/fai/config/files/root/.ssh/authorized_keys/GRUB_PC
83 chmod -R a+rX /srv/fai/config/distro-install-common
84 # this basefile has tar acls bug, so I'm using my own
85 # local one for now.
86 #cd /srv/fai/config/basefiles
87 #u=http://fai-project.org/download/basefiles/XENIAL64.tar.xz
88 #wget -nv -N $u
89
90 changed=false
91 f=/srv/fai/nfsroot/root/.ssh/known_hosts
92 # the known hosts entries that fai already sets up are like
93 # IP,HOSTNAME key_info...
94 # we are skipping the ip, because it doesn't block ssh
95 # with a prompt as long as you have the user supplied hostname,
96 # and i don't want to deal with getting it, it's not adding
97 # any important security in this case.
98 if ! grep -xFq "$line" $f; then
99 changed=true
100 printf "%s\n" "$line" >>$f
101 fi
102
103 if ! modprobe nfsd &>/dev/null; then
104 # no apt-cache on maru debian, because we are low on space already
105 sed -i '/^ *APTPROXY=/d' /srv/fai/config/class/DEBIAN.var
106 # maru debian doesn't have loopback devs created
107 if ! losetup -f; then
108 shopt -s nullglob
109 x=(/dev/loop*)
110 minor=0
111 if (( ${#x[@]} )); then
112 minor=$(( ${x[-1]#/dev/loop} + 1 ))
113 fi
114 mknod -m660 /dev/loop$minor b 7 $minor
115 losetup -f
116 fi
117 # -B boo only iso, no nfsroot, no paritial miorr, no config space.
118 # -f = force, for overwriting
119 # -S = make squash image for http booting
120 # -d config space url, instead of putting it in the squash.img,
121 # this just makes it so that we don't have to regenerate the img
122 # when the config changes.
123 cd /srv/fai/config
124 tar czf /var/www/faiserver/html/config.tar.gz .
125 if $changed || [[ ! -e /var/www/faiserver/html/squash.img ]]; then
126 # note, on maru, selinux needs to be disabled in android before
127 # this will work.
128 mount
129 export debug=true
130 fai-cd -d http://faiserver:8080/config.tar.gz -f -M -S /var/www/faiserver/html/squash.img
131 mount
132 fi
133 fi
134 EOF