a bunch of fixes and improvements
[automated-distro-installer] / myfai-chboot-local
1 #!/bin/bash
2 # note, this script gets piped to bash, so cant cd to current dir
3
4 [[ $EUID == 0 ]] || exec sudo "${BASH_SOURCE}" "$@"
5
6 set -eE -o pipefail
7 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
8
9 kgped16=false
10 fai_action=install
11 fai_reboot_arg=,reboot
12 while [[ $1 == -* ]]; do
13 case $1 in
14 -h|--help)
15 echo "see help from myfai-chboot"
16 exit 0
17 ;;
18 -S)
19 fai_action=sysinfo
20 fai_reboot_arg=
21 shift
22 ;;
23 -i) #inventory
24 fai_action=inventory
25 fai_reboot_arg=
26 shift
27 ;;
28 -k)
29 kgped16=true;
30 shift
31 ;;
32 esac
33 done
34
35 pre="${0##*/}:"
36 m() { printf "$pre %s\n" "$*"; "$@"; }
37 e() { printf "$pre %s\n" "$*"; }
38 err() { echo "[$(date +'%Y-%m-%d %H:%M:%S%z')]: $pre: $*" >&2; }
39
40 host=$1
41
42 rm -f /srv/tftp/fai/pxelinux.cfg/*
43 if [[ ! $1 ]]; then
44 echo "$0: clearing pxe config and exiting"
45 exit 0
46 fi
47
48 # assuming ipv4, or else we might need to deal with multiple addresses
49 # in an ipv4 + ipv6 network.
50 my_ip=$(ip -4 route get 8.8.8.8 | sed -nr 's,^.*src\s+(\S+).*,\1,p')
51 if [[ ! $my_ip || $my_ip =~ [[:space:]] ]]; then
52 echo "$0: error: failed to get \$my_ip, got: $my_ip"
53 exit 1
54 fi
55
56 if [[ $host == default ]]; then
57 ip='*'
58 elif [[ $host == [0-9]*.[0-9]*.[0-9]*.[0-9]* ]]; then
59 ip=$host/32
60 else
61 type -t host &>/dev/null || apt-get -y install dnsutils
62 ip=$(host $host | sed -rn 's/^\S+ has address //p;T;q' ||:)
63 if [[ ! $ip || $ip =~ [[:space:]] ]]; then
64 echo "$0: error: failed to get \$ip, got: $ip"
65 exit 1
66 fi
67 ip=$ip/32
68 echo "$0: found ip of $host: $ip"
69 fi
70
71 if modprobe nfsd &>/dev/null; then
72 std_arg="-u nfs://faiserver/srv/fai/config"
73 # nfsv4 wont do rw with overlayfs yet
74 # https://lists.uni-koeln.de/pipermail/linux-fai/2017-March/011641.html
75 root_arg="$my_ip:/srv/fai/nfsroot:vers=3"
76 # fai-setup without -e sets the ip to the local_ip/local_network, eg 192.168.1.3/24
77 # I restrict it to one ip as simple but imperfect access control.
78
79 # we may chattr +i /etc/exports if we dun want it modified
80 # for example, if we made these exports more widely available
81 # while doing multiple installs or a recovery.
82 if [[ -w /etc/exports ]]; then
83 sed -ri --follow-symlinks '\%^/srv/fai/%d' /etc/exports
84 cat >>/etc/exports <<EOF
85 /srv/fai/config $ip(async,ro,no_subtree_check,no_root_squash)
86 /srv/fai/nfsroot $ip(async,ro,no_subtree_check,no_root_squash)
87 EOF
88 exportfs -ra
89 fi
90 systemctl start nfs-server # assumes recent os
91 else
92 std_arg="-u http://faiserver:8080/config.tar.gz"
93 root_arg="live:http://faiserver:8080/squash.img"
94 /a/exe/web-conf -i -p 8080 - apache2 faiserver <<EOF
95 <Location />
96 Deny from all
97 Allow from $ip
98 </Location>
99 EOF
100 fi
101
102
103
104 # man page doesn't explain this, but this deletes & thus disables
105 # all chboot systems.
106 m fai-chboot -iv $std_arg default # set it to default to get a val out of it next
107 kernel=$(fai-chboot -L '^default$' | awk '{print $3}')
108 default_k_args=$(fai-chboot -L '^default$' | \
109 sed -r "s/^(\S+\s+){3}(.*)/\2/")
110 # example of default_k_args
111 # initrd=initrd.img-3.16.0-4-amd64 ip=dhcp root=192.168.1.3:/srv/fai/nfsroot aufs FAI_CONFIG_SRC=nfs://faiserver/srv/fai/config FAI_ACTION=install
112
113 k_args=(FAI_ACTION=$fai_action)
114 if $kgped16; then
115 k_args+=(console=tty0 console=ttyS0,115200)
116 fi
117
118 for arg in $default_k_args; do
119 case $arg in
120 # default root arg is /srv/fai/nfsroot
121 root=*) k_args+=(root=$root_arg) ;;
122 *) k_args+=($arg) ;;
123 esac
124 done
125 rm -f /srv/tftp/fai/pxelinux.cfg/*
126 m fai-chboot -k "${k_args[*]}" -v -f verbose,sshd,createvt$fai_reboot_arg $std_arg $kernel "$host"
127
128 # this is needed for autodiscover iso. i'm not sure, it might override
129 # the fai-chboot method of setting this, i'm not sure.
130 echo FAI_ACTION=$fai_action >> /srv/fai/config/class/LAST.var