revert chost, broke for localhost
[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 case $1 in
7 -h|--help)
8 echo "see help from myfai-chboot"
9 exit 0
10 ;;
11 esac
12
13 [[ $EUID == 0 ]] || exec sudo "${BASH_SOURCE}" "$@"
14
15 e() { echo "$@"; "$@"; }
16
17
18 rm -f /srv/tftp/fai/pxelinux.cfg/*
19 if [[ ! $1 ]]; then
20 exit 0
21 fi
22
23 host=$1
24 ip=$(getent hosts $host | awk '{print $1}')
25
26 std_arg="-u nfs://faiserver/srv/fai/config"
27 e fai-chboot -Iv $std_arg default # set it to default to get a val out of it next
28 kernel=$(fai-chboot -L '^default$' | awk '{print $3}')
29 # man page doesn't explain this, but this deletes & thus disables
30 # all chboot systems.
31 type -t host &>/dev/null || apt-get -y install dnsutils
32 gateway_ip=$(route -n | sed -rn 's/^0\.0\.0\.0\s+(\S+).*/\1/p')
33 my_ip=$(host faiserver $gateway_ip | sed -rn 's/^\S+ has address //p')
34 k_args=$(fai-chboot -L '^default$' | \
35 sed -r "s/^(\S+\s+){3}(.*root=)(.*)/\2$my_ip:\3/")
36 rm -f /srv/tftp/fai/pxelinux.cfg/*
37 e fai-chboot -k "$k_args" -v -f verbose,sshd,createvt,reboot $std_arg $kernel "$host"
38
39 # fai-setup without -e sets the ip to the local_ip/local_network, eg 192.168.1.3/24
40 # I restrict it to one ip as simple but imperfect access control.
41 sed -ri --follow-symlinks '\%^/srv/fai/%d' /etc/exports
42 cat >>/etc/exports <<EOF
43 /srv/fai/config $ip/32(async,ro,no_subtree_check)
44 /srv/fai/nfsroot $ip/32(async,ro,no_subtree_check,no_root_squash)
45 EOF
46 exportfs -ra
47
48
49 # todo, remove the nopxe script. adjust fai-check to reboot if it fails on the kexec.