merge upstream, untested
[automated-distro-installer] / myfai-chboot-local
1 #!/bin/bash
2 # note, this script gets piped to bash, so cant cd to current dir
3 set -eE -o pipefail
4 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
5
6 kgped16=false
7 fai_action=install
8 fai_reboot_arg=,reboot
9 while [[ $1 == -* ]]; do
10 case $1 in
11 -h|--help)
12 echo "see help from myfai-chboot"
13 exit 0
14 ;;
15 -S)
16 fai_action=sysinfo
17 fai_reboot_arg=
18 shift
19 ;;
20 -i) #inventory
21 fai_action=inventory
22 fai_reboot_arg=
23 shift
24 ;;
25 -k)
26 kgped16=true;
27 shift
28 ;;
29 esac
30 done
31
32 [[ $EUID == 0 ]] || exec sudo "${BASH_SOURCE}" "$@"
33
34 e() {
35 echo "$*"
36 if ! "$@"; then
37 echo "$0: error: exit code $? from: $*"
38 exit 1
39 fi
40 }
41
42 host=$1
43
44 rm -f /srv/tftp/fai/pxelinux.cfg/*
45 if [[ ! $1 ]]; then
46 echo "$0: clearing pxe config and exiting"
47 exit 0
48 fi
49
50 # assuming ipv4, or else we might need to deal with multiple addresses
51 # in an ipv4 + ipv6 network.
52 my_ip=$(ip -4 route get 8.8.8.8 | sed -nr 's,^.*src\s+(\S+).*,\1,p')
53 if [[ ! $my_ip || $my_ip =~ [[:space:]] ]]; then
54 echo "$0: error: failed to get \$my_ip, got: $my_ip"
55 exit 1
56 fi
57
58 if [[ $host == default ]]; then
59 ip='*'
60 elif [[ $host == [0-9]*.[0-9]*.[0-9]*.[0-9]* ]]; then
61 ip=$host/32
62 else
63 type -t host &>/dev/null || apt-get -y install dnsutils
64 ip=$(host $host | sed -rn 's/^\S+ has address //p;T;q' ||:)
65 if [[ ! $ip || $ip =~ [[:space:]] ]]; then
66 echo "$0: error: failed to get \$ip, got: $ip"
67 exit 1
68 fi
69 ip=$ip/32
70 fi
71
72 if modprobe nfsd &>/dev/null; then
73 std_arg="-u nfs://faiserver/srv/fai/config"
74 # nfsv4 wont do rw with overlayfs yet
75 # https://lists.uni-koeln.de/pipermail/linux-fai/2017-March/011641.html
76 root_arg="$my_ip:/srv/fai/nfsroot:vers=3"
77 # fai-setup without -e sets the ip to the local_ip/local_network, eg 192.168.1.3/24
78 # I restrict it to one ip as simple but imperfect access control.
79
80 # we may chattr +i /etc/exports if we dun want it modified
81 # for example, if we made these exports more widely available
82 # while doing multiple installs or a recovery.
83 if [[ -w /etc/exports ]]; then
84 sed -ri --follow-symlinks '\%^/srv/fai/%d' /etc/exports
85 cat >>/etc/exports <<EOF
86 /srv/fai/config $ip(async,ro,no_subtree_check)
87 /srv/fai/nfsroot $ip(async,ro,no_subtree_check,no_root_squash)
88 EOF
89 exportfs -ra
90 fi
91 systemctl start nfs-server # assumes recent os
92 else
93 std_arg="-u http://faiserver:8080/config.tar.gz"
94 root_arg="live:http://faiserver:8080/squash.img"
95 /a/exe/web-conf -i -p 8080 - apache2 faiserver <<EOF
96 <Location />
97 Deny from all
98 Allow from $ip
99 </Location>
100 EOF
101 fi
102
103
104
105 # man page doesn't explain this, but this deletes & thus disables
106 # all chboot systems.
107 e fai-chboot -iv $std_arg default # set it to default to get a val out of it next
108 kernel=$(fai-chboot -L '^default$' | awk '{print $3}')
109 default_k_args=$(fai-chboot -L '^default$' | \
110 sed -r "s/^(\S+\s+){3}(.*)/\2/")
111 # example of default_k_args
112 # 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
113
114 k_args=(FAI_ACTION=$fai_action)
115 if $kgped16; then
116 k_args+=(console=tty0 console=ttyS0,115200)
117 fi
118
119 for arg in $default_k_args; do
120 case $arg in
121 # default root arg is /srv/fai/nfsroot
122 root=*) k_args+=(root=$root_arg) ;;
123 *) k_args+=($arg) ;;
124 esac
125 done
126 rm -f /srv/tftp/fai/pxelinux.cfg/*
127 e fai-chboot -k "${k_args[*]}" -v -f verbose,sshd,createvt$fai_reboot_arg $std_arg $kernel "$host"
128
129 # this is needed for autodiscover iso. i'm not sure, it might override
130 # the fai-chboot method of setting this, i'm not sure.
131 echo FAI_ACTION=$fai_action >> /srv/fai/config/class/LAST.var