add more stack traces, other minor changes
[automated-distro-installer] / myfai-chboot-local
1 #!/bin/bash
2
3 x="$(readlink -f "$BASH_SOURCE")"; source "${x%/*}/bash-trace"
4
5 fai_action_arg=I
6 fai_reboot_arg=,reboot
7 case $1 in
8 -h|--help)
9 echo "see help from myfai-chboot"
10 exit 0
11 ;;
12 -S)
13 fai_action_arg=S
14 fai_reboot_arg=
15 shift
16 ;;
17 esac
18
19 [[ $EUID == 0 ]] || exec sudo "${BASH_SOURCE}" "$@"
20
21 e() { echo "$@"; "$@"; }
22
23 host=$1
24
25 type -t host &>/dev/null || apt-get -y install dnsutils
26 gateway_if=$(ip route | sed -rn 's/^default via \S+ dev (\S+) .*/\1/p')
27 if [[ ! $gateway_if ]]; then
28 echo "$0: failed to find gateway interface"
29 exit 1
30 fi
31 # assuming ipv4, or else we might need to deal with multiple addresses
32 # in an ipv4 + ipv6 network.
33 network=$(ip -4 -o a show dev $gateway_if | sed -rn '/scope.*global/s/^(\S+\s+){3}(\S+)\s.*/\2/p')
34 if [[ ! $network ]]; then
35 echo "$0: failed to find network"
36 exit 1
37 fi
38 my_ip=${network%/*}
39 if [[ $host == default ]]; then
40 ip=$network
41 else
42 ip=$(host $host | sed -rn 's/^\S+ has address //p;T;q')/32
43 fi
44
45
46 # alternate way of getting my ip
47 #gateway_ip=$(ip route | sed -rn 's/^default via (\S+) .*/\1/p')
48 #my_ip=$(host faiserver $gateway_ip | sed -rn 's/^\S+ has address //p;T;q')
49
50 if modprobe nfsd &>/dev/null; then
51 std_arg="-u nfs://faiserver/srv/fai/config"
52 root_arg="$my_ip:/srv/fai/nfsroot"
53 # fai-setup without -e sets the ip to the local_ip/local_network, eg 192.168.1.3/24
54 # I restrict it to one ip as simple but imperfect access control.
55 sed -ri --follow-symlinks '\%^/srv/fai/%d' /etc/exports
56 cat >>/etc/exports <<EOF
57 /srv/fai/config $ip(async,ro,no_subtree_check)
58 /srv/fai/nfsroot $ip(async,ro,no_subtree_check,no_root_squash)
59 EOF
60 exportfs -ra
61 else
62 std_arg="-u http://faiserver:8080/config.tar.gz"
63 root_arg="live:http://faiserver:8080/squash.img"
64 /a/exe/web-conf -i -p 8080 - apache2 faiserver <<EOF
65 <Location />
66 Deny from all
67 Allow from $ip
68 </Location>
69 EOF
70 fi
71
72 rm -f /srv/tftp/fai/pxelinux.cfg/*
73 if [[ ! $1 ]]; then
74 exit 0
75 fi
76
77
78 # man page doesn't explain this, but this deletes & thus disables
79 # all chboot systems.
80 e fai-chboot -${fai_action_arg}v $std_arg default # set it to default to get a val out of it next
81 kernel=$(fai-chboot -L '^default$' | awk '{print $3}')
82 default_k_args=$(fai-chboot -L '^default$' | \
83 sed -r "s/^(\S+\s+){3}(.*)/\2/")
84 # example of default_k_args
85 # 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
86
87 k_args=()
88 for arg in $default_k_args; do
89 case $arg in
90 # default root arg is /srv/fai/nfsroot
91 root=*) k_args+=(root=$root_arg) ;;
92 *) k_args+=($arg) ;;
93 esac
94 done
95 rm -f /srv/tftp/fai/pxelinux.cfg/*
96 e fai-chboot -k "${k_args[*]}" -v -f verbose,sshd,createvt$fai_reboot_arg $std_arg $kernel "$host"