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