fix some minor bugs
[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 type -t host &>/dev/null || apt-get -y install dnsutils
35 gateway_if=$(ip route | sed -rn 's/^default via \S+ dev (\S+) .*/\1/p')
36 if [[ ! $gateway_if ]]; then
37 echo "$0: failed to find gateway interface"
38 exit 1
39 fi
40 # assuming ipv4, or else we might need to deal with multiple addresses
41 # in an ipv4 + ipv6 network.
42 network=$(ip -4 -o a show dev $gateway_if | sed -rn '/scope.*global/s/^(\S+\s+){3}(\S+)\s.*/\2/p')
43 if [[ ! $network ]]; then
44 echo "$0: failed to find network"
45 exit 1
46 fi
47 my_ip=${network%/*}
48 if [[ $host == default ]]; then
49 ip=$network
50 elif [[ $host == [0-9]*.[0-9]*.[0-9]*.[0-9]* ]]; then
51 ip=$host
52 else
53 ip=$(host $host | sed -rn 's/^\S+ has address //p;T;q')/32
54 fi
55
56
57 # alternate way of getting my ip
58 #gateway_ip=$(ip route | sed -rn 's/^default via (\S+) .*/\1/p')
59 #my_ip=$(host faiserver $gateway_ip | sed -rn 's/^\S+ has address //p;T;q')
60
61 if modprobe nfsd &>/dev/null; then
62 std_arg="-u nfs://faiserver/srv/fai/config"
63 root_arg="$my_ip:/srv/fai/nfsroot"
64 # fai-setup without -e sets the ip to the local_ip/local_network, eg 192.168.1.3/24
65 # I restrict it to one ip as simple but imperfect access control.
66 sed -ri --follow-symlinks '\%^/srv/fai/%d' /etc/exports
67 cat >>/etc/exports <<EOF
68 /srv/fai/config $ip(async,ro,no_subtree_check)
69 /srv/fai/nfsroot $ip(async,ro,no_subtree_check,no_root_squash)
70 EOF
71 exportfs -ra
72 else
73 std_arg="-u http://faiserver:8080/config.tar.gz"
74 root_arg="live:http://faiserver:8080/squash.img"
75 /a/exe/web-conf -i -p 8080 - apache2 faiserver <<EOF
76 <Location />
77 Deny from all
78 Allow from $ip
79 </Location>
80 EOF
81 fi
82
83 rm -f /srv/tftp/fai/pxelinux.cfg/*
84 if [[ ! $1 ]]; then
85 exit 0
86 fi
87
88
89 # man page doesn't explain this, but this deletes & thus disables
90 # all chboot systems.
91 e fai-chboot -${fai_action_arg}v $std_arg default # set it to default to get a val out of it next
92 kernel=$(fai-chboot -L '^default$' | awk '{print $3}')
93 default_k_args=$(fai-chboot -L '^default$' | \
94 sed -r "s/^(\S+\s+){3}(.*)/\2/")
95 # example of default_k_args
96 # 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
97
98 k_args=()
99 for arg in $default_k_args; do
100 case $arg in
101 # default root arg is /srv/fai/nfsroot
102 root=*) k_args+=(root=$root_arg) ;;
103 *) k_args+=($arg) ;;
104 esac
105 done
106 rm -f /srv/tftp/fai/pxelinux.cfg/*
107 e fai-chboot -k "${k_args[*]}" -v -f verbose,sshd,createvt$fai_reboot_arg $std_arg $kernel "$host"