various fixes
[automated-distro-installer] / myfai-chboot-local
1 #!/bin/bash
2 # note, this script gets piped to bash, so cant cd to current dir
3
4 [[ $EUID == 0 ]] || exec sudo "${BASH_SOURCE}" "$@"
5
6 set -eE -o pipefail
7 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
8
9 kgped16=false
10 bond=false
11 fai_action=install
12 fai_reboot_arg=,reboot
13 while [[ $1 == -* ]]; do
14 case $1 in
15 -h|--help)
16 echo "see help from myfai-chboot"
17 exit 0
18 ;;
19 -S)
20 fai_action=sysinfo
21 fai_reboot_arg=
22 shift
23 ;;
24 -i) #inventory
25 fai_action=inventory
26 fai_reboot_arg=
27 shift
28 ;;
29 -k)
30 kgped16=true
31 shift
32 ;;
33 -b)
34 bond=true
35 shift
36 ;;
37 --no-r)
38 fai_reboot_arg=
39 shift
40 ;;
41 esac
42 done
43
44 pre="${0##*/}:"
45 m() { printf "$pre %s\n" "$*"; "$@"; }
46 e() { printf "$pre %s\n" "$*"; }
47 err() { echo "[$(date +'%Y-%m-%d %H:%M:%S%z')]: $pre: $*" >&2; }
48
49 host=$1
50
51
52 rm -f /srv/tftp/fai/pxelinux.cfg/*
53 if [[ ! $1 ]]; then
54 echo "$0: clearing pxe config and exiting"
55 exit 0
56 fi
57
58 # somewhat duplicated in brc hostip()
59 case $host in
60 default) : ;;
61 [0-9:])
62 hostip=$host
63 ;;
64 *)
65 hostip=$(getent ahostsv4 "$host" | awk '{ print $1 }' | head -n1)
66 ;;
67 esac
68
69 if [[ $hostip ]]; then
70
71 # assuming ipv4, or else we might need to deal with multiple addresses
72 # in an ipv4 + ipv6 network.
73 my_ip=$(ip -4 route get $hostip | sed -nr 's,^.*src\s+(\S+).*,\1,p')
74 if [[ ! $my_ip || $my_ip =~ [[:space:]] ]]; then
75 echo "$0: error: failed to get \$my_ip, got: $my_ip"
76 exit 1
77 fi
78 else
79 my_ip=$(ip r show default | sed -r 's/.*via ([^ ]*).*/\1/' | head -n1)
80 fi
81
82 if [[ $host == default ]]; then
83 ip='*'
84 elif [[ $host == [0-9]*.[0-9]*.[0-9]*.[0-9]* ]]; then
85 ip=$host/32
86 else
87 type -t host &>/dev/null || apt-get -y install dnsutils
88 ip=$(host $host | sed -rn 's/^\S+ has address //p;T;q' ||:)
89 if [[ ! $ip || $ip =~ [[:space:]] ]]; then
90 echo "$0: error: failed to get \$ip, got: $ip"
91 exit 1
92 fi
93 ip=$ip/32
94 echo "$0: found ip of $host: $ip"
95 fi
96
97 if modprobe nfsd &>/dev/null; then
98 std_arg="-u nfs://faiserver/srv/fai/config"
99 # nfsv4 wont do rw with overlayfs yet
100 # https://lists.uni-koeln.de/pipermail/linux-fai/2017-March/011641.html
101 root_arg="$my_ip:/srv/fai/nfsroot:vers=3"
102 # fai-setup without -e sets the ip to the local_ip/local_network, eg 192.168.1.3/24
103 # I restrict it to one ip as simple but imperfect access control.
104
105 # we may chattr +i /etc/exports if we dun want it modified
106 # for example, if we made these exports more widely available
107 # while doing multiple installs or a recovery.
108 if [[ -w /etc/exports ]]; then
109 sed -ri --follow-symlinks '\%^/srv/fai/%d' /etc/exports
110 cat >>/etc/exports <<EOF
111 /srv/fai/config $ip(async,ro,no_subtree_check,no_root_squash)
112 /srv/fai/nfsroot $ip(async,ro,no_subtree_check,no_root_squash)
113 EOF
114 exportfs -ra
115 fi
116 systemctl start nfs-server # assumes recent os
117 else
118 std_arg="-u http://faiserver:8080/config.tar.gz"
119 root_arg="live:http://faiserver:8080/squash.img"
120 /a/exe/web-conf -i -p 8080 - apache2 faiserver <<EOF
121 <Location />
122 Deny from all
123 Allow from $ip
124 </Location>
125 EOF
126 fi
127
128
129
130 # man page doesn't explain this, but this deletes & thus disables
131 # all chboot systems.
132 m fai-chboot -iv $std_arg default # set it to default to get a val out of it next
133 kernel=$(fai-chboot -L '^default$' | awk '{print $3}')
134 default_k_args=$(fai-chboot -L '^default$' | \
135 sed -r "s/^(\S+\s+){3}(.*)/\2/")
136 # example of default_k_args
137 # initrd=initrd.img-3.16.0-4-amd64 ip=dhcp root=192.168.1.3:/srv/fai/nfsroot FAI_CONFIG_SRC=nfs://faiserver/srv/fai/config FAI_ACTION=install
138
139 # https://wiki.archlinux.org/index.php/Solid_state_drive#Resolving_NCQ_errors
140 # currently on needed on d16 samsung 870 qvo, but better to have this
141 # and not wait for more conditions where its needed.
142 #k_args=(FAI_ACTION=$fai_action libata.force=noncq ifname:bootnet0:08:60:6e:10:f0:fe ifname:bootnet1:08:60:6e:10:f0:98 bond=bond0:bootnet0,bootnet1:mode=balance-rr)
143 #k_args=(FAI_ACTION=$fai_action libata.force=noncq ifname:bootnet0:08:60:6e:10:f0:fe biosdevname=0 bootdev=bootnet0)
144 k_args=(FAI_ACTION=$fai_action libata.force=noncq)
145 if $kgped16; then
146 k_args+=(console=tty0 console=ttyS0,115200)
147 fi
148
149 for arg in $default_k_args; do
150 case $arg in
151 # default root arg is /srv/fai/nfsroot
152 root=*) k_args+=(root=$root_arg) ;;
153 # note: this works to only dhcp on one interface: ip=eth0:dhcp
154 ip=*)
155 if $bond; then
156 k_args+=("bond=bond0:eth0,eth1:mode=balance-rr ip=bond0:dhcp")
157 else
158 k_args+=($arg)
159 fi
160 ;;
161 *) k_args+=($arg) ;;
162 esac
163 done
164 rm -f /srv/tftp/fai/pxelinux.cfg/*
165 m fai-chboot -k "${k_args[*]}" -v -f verbose,sshd,createvt$fai_reboot_arg $std_arg $kernel "$host"
166
167 # this is needed for autodiscover iso. i'm not sure, it might override
168 # the fai-chboot method of setting this, i'm not sure.
169 echo FAI_ACTION=$fai_action >> /srv/fai/config/class/LAST.var