small fixes
[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 [[ $x =~ [[: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=$network
44 elif [[ $host == [0-9]*.[0-9]*.[0-9]*.[0-9]* ]]; then
45 ip=$host
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 fi
50
51 if modprobe nfsd &>/dev/null; then
52 std_arg="-u nfs://faiserver/srv/fai/config"
53 # nfsv4 wont do rw with overlayfs yet
54 # https://lists.uni-koeln.de/pipermail/linux-fai/2017-March/011641.html
55 root_arg="$my_ip:/srv/fai/nfsroot:vers=3"
56 # fai-setup without -e sets the ip to the local_ip/local_network, eg 192.168.1.3/24
57 # I restrict it to one ip as simple but imperfect access control.
58 sed -ri --follow-symlinks '\%^/srv/fai/%d' /etc/exports
59 cat >>/etc/exports <<EOF
60 /srv/fai/config $ip(async,ro,no_subtree_check)
61 /srv/fai/nfsroot $ip(async,ro,no_subtree_check,no_root_squash)
62 EOF
63 exportfs -ra
64 systemctl start nfs-server # assumes recent os
65 else
66 std_arg="-u http://faiserver:8080/config.tar.gz"
67 root_arg="live:http://faiserver:8080/squash.img"
68 /a/exe/web-conf -i -p 8080 - apache2 faiserver <<EOF
69 <Location />
70 Deny from all
71 Allow from $ip
72 </Location>
73 EOF
74 fi
75
76 rm -f /srv/tftp/fai/pxelinux.cfg/*
77 if [[ ! $1 ]]; then
78 exit 0
79 fi
80
81
82 # man page doesn't explain this, but this deletes & thus disables
83 # all chboot systems.
84 e fai-chboot -${fai_action_arg}v $std_arg default # set it to default to get a val out of it next
85 kernel=$(fai-chboot -L '^default$' | awk '{print $3}')
86 default_k_args=$(fai-chboot -L '^default$' | \
87 sed -r "s/^(\S+\s+){3}(.*)/\2/")
88 # example of default_k_args
89 # 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
90
91 k_args=()
92 for arg in $default_k_args; do
93 case $arg in
94 # default root arg is /srv/fai/nfsroot
95 root=*) k_args+=(root=$root_arg) ;;
96 *) k_args+=($arg) ;;
97 esac
98 done
99 rm -f /srv/tftp/fai/pxelinux.cfg/*
100 e fai-chboot -k "${k_args[*]}" -v -f verbose,sshd,createvt$fai_reboot_arg $std_arg $kernel "$host"