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