various fixes and improvements
[automated-distro-installer] / myfai-chboot-local
1 #!/bin/bash
2
3 # note, this script gets piped to bash, so cant cd to current dir
4 set -eE -o pipefail
5 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
6
7
8 fai_action_arg=I
9 fai_reboot_arg=,reboot
10 case $1 in
11 -h|--help)
12 echo "see help from myfai-chboot"
13 exit 0
14 ;;
15 -S)
16 fai_action_arg=S
17 fai_reboot_arg=
18 shift
19 ;;
20 esac
21
22 [[ $EUID == 0 ]] || exec sudo "${BASH_SOURCE}" "$@"
23
24 e() {
25 echo "$*"
26 if ! "$@"; then
27 echo "$0: error: exit code $? from: $*"
28 exit 1
29 fi
30 }
31
32 host=$1
33
34 # assuming ipv4, or else we might need to deal with multiple addresses
35 # in an ipv4 + ipv6 network.
36 my_ip=$(ip -4 route get 8.8.8.8 | sed -nr 's,^.*src\s+(\S+).*,\1,p')
37 if [[ ! $my_ip || $my_ip =~ [[:space:]] ]]; then
38 echo "$0: error: failed to get \$my_ip, got: $my_ip"
39 exit 1
40 fi
41
42 if [[ $host == default ]]; then
43 ip='*'
44 elif [[ $host == [0-9]*.[0-9]*.[0-9]*.[0-9]* ]]; then
45 ip=$host/32
46 else
47 type -t host &>/dev/null || apt-get -y install dnsutils
48 ip=$(host $host | sed -rn 's/^\S+ has address //p;T;q')/32
49 if [[ ! $ip || $ip =~ [[:space:]] ]]; then
50 echo "$0: error: failed to get \$my_ip, got: $my_ip"
51 exit 1
52 fi
53
54 fi
55
56 if modprobe nfsd &>/dev/null; then
57 std_arg="-u nfs://faiserver/srv/fai/config"
58 # nfsv4 wont do rw with overlayfs yet
59 # https://lists.uni-koeln.de/pipermail/linux-fai/2017-March/011641.html
60 root_arg="$my_ip:/srv/fai/nfsroot:vers=3"
61 # fai-setup without -e sets the ip to the local_ip/local_network, eg 192.168.1.3/24
62 # I restrict it to one ip as simple but imperfect access control.
63 sed -ri --follow-symlinks '\%^/srv/fai/%d' /etc/exports
64 cat >>/etc/exports <<EOF
65 /srv/fai/config $ip(async,ro,no_subtree_check)
66 /srv/fai/nfsroot $ip(async,ro,no_subtree_check,no_root_squash)
67 EOF
68 exportfs -ra
69 systemctl start nfs-server # assumes recent os
70 else
71 std_arg="-u http://faiserver:8080/config.tar.gz"
72 root_arg="live:http://faiserver:8080/squash.img"
73 /a/exe/web-conf -i -p 8080 - apache2 faiserver <<EOF
74 <Location />
75 Deny from all
76 Allow from $ip
77 </Location>
78 EOF
79 fi
80
81 rm -f /srv/tftp/fai/pxelinux.cfg/*
82 if [[ ! $1 ]]; then
83 exit 0
84 fi
85
86
87 # man page doesn't explain this, but this deletes & thus disables
88 # all chboot systems.
89 e fai-chboot -${fai_action_arg}v $std_arg default # set it to default to get a val out of it next
90 kernel=$(fai-chboot -L '^default$' | awk '{print $3}')
91 default_k_args=$(fai-chboot -L '^default$' | \
92 sed -r "s/^(\S+\s+){3}(.*)/\2/")
93 # example of default_k_args
94 # 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
95
96 k_args=()
97 for arg in $default_k_args; do
98 case $arg in
99 # default root arg is /srv/fai/nfsroot
100 root=*) k_args+=(root=$root_arg) ;;
101 *) k_args+=($arg) ;;
102 esac
103 done
104 rm -f /srv/tftp/fai/pxelinux.cfg/*
105 e fai-chboot -k "${k_args[*]}" -v -f verbose,sshd,createvt$fai_reboot_arg $std_arg $kernel "$host"