fixes, t11, install in ad-hoc network
[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 [0-9:])
61 hostip=$host
62 ;;
63 *)
64 hostip=$(getent ahostsv4 "$host" | awk '{ print $1 }' | head -n1)
65 ;;
66 esac
67
68
69 # assuming ipv4, or else we might need to deal with multiple addresses
70 # in an ipv4 + ipv6 network.
71 my_ip=$(ip -4 route get $hostip | sed -nr 's,^.*src\s+(\S+).*,\1,p')
72 if [[ ! $my_ip || $my_ip =~ [[:space:]] ]]; then
73 echo "$0: error: failed to get \$my_ip, got: $my_ip"
74 exit 1
75 fi
76
77 if [[ $host == default ]]; then
78 ip='*'
79 elif [[ $host == [0-9]*.[0-9]*.[0-9]*.[0-9]* ]]; then
80 ip=$host/32
81 else
82 type -t host &>/dev/null || apt-get -y install dnsutils
83 ip=$(host $host | sed -rn 's/^\S+ has address //p;T;q' ||:)
84 if [[ ! $ip || $ip =~ [[:space:]] ]]; then
85 echo "$0: error: failed to get \$ip, got: $ip"
86 exit 1
87 fi
88 ip=$ip/32
89 echo "$0: found ip of $host: $ip"
90 fi
91
92 if modprobe nfsd &>/dev/null; then
93 std_arg="-u nfs://faiserver/srv/fai/config"
94 # nfsv4 wont do rw with overlayfs yet
95 # https://lists.uni-koeln.de/pipermail/linux-fai/2017-March/011641.html
96 root_arg="$my_ip:/srv/fai/nfsroot:vers=3"
97 # fai-setup without -e sets the ip to the local_ip/local_network, eg 192.168.1.3/24
98 # I restrict it to one ip as simple but imperfect access control.
99
100 # we may chattr +i /etc/exports if we dun want it modified
101 # for example, if we made these exports more widely available
102 # while doing multiple installs or a recovery.
103 if [[ -w /etc/exports ]]; then
104 sed -ri --follow-symlinks '\%^/srv/fai/%d' /etc/exports
105 cat >>/etc/exports <<EOF
106 /srv/fai/config $ip(async,ro,no_subtree_check,no_root_squash)
107 /srv/fai/nfsroot $ip(async,ro,no_subtree_check,no_root_squash)
108 EOF
109 exportfs -ra
110 fi
111 systemctl start nfs-server # assumes recent os
112 else
113 std_arg="-u http://faiserver:8080/config.tar.gz"
114 root_arg="live:http://faiserver:8080/squash.img"
115 /a/exe/web-conf -i -p 8080 - apache2 faiserver <<EOF
116 <Location />
117 Deny from all
118 Allow from $ip
119 </Location>
120 EOF
121 fi
122
123
124
125 # man page doesn't explain this, but this deletes & thus disables
126 # all chboot systems.
127 m fai-chboot -iv $std_arg default # set it to default to get a val out of it next
128 kernel=$(fai-chboot -L '^default$' | awk '{print $3}')
129 default_k_args=$(fai-chboot -L '^default$' | \
130 sed -r "s/^(\S+\s+){3}(.*)/\2/")
131 # example of default_k_args
132 # 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
133
134 # https://wiki.archlinux.org/index.php/Solid_state_drive#Resolving_NCQ_errors
135 # currently on needed on d16 samsung 870 qvo, but better to have this
136 # and not wait for more conditions where its needed.
137 #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)
138 #k_args=(FAI_ACTION=$fai_action libata.force=noncq ifname:bootnet0:08:60:6e:10:f0:fe biosdevname=0 bootdev=bootnet0)
139 k_args=(FAI_ACTION=$fai_action libata.force=noncq)
140 if $kgped16; then
141 k_args+=(console=tty0 console=ttyS0,115200)
142 fi
143
144 for arg in $default_k_args; do
145 case $arg in
146 # default root arg is /srv/fai/nfsroot
147 root=*) k_args+=(root=$root_arg) ;;
148 # note: this works to only dhcp on one interface: ip=eth0:dhcp
149 ip=*)
150 if $bond; then
151 k_args+=("bond=bond0:eth0,eth1:mode=balance-rr ip=bond0:dhcp")
152 else
153 k_args+=($arg)
154 fi
155 ;;
156 *) k_args+=($arg) ;;
157 esac
158 done
159 rm -f /srv/tftp/fai/pxelinux.cfg/*
160 m fai-chboot -k "${k_args[*]}" -v -f verbose,sshd,createvt$fai_reboot_arg $std_arg $kernel "$host"
161
162 # this is needed for autodiscover iso. i'm not sure, it might override
163 # the fai-chboot method of setting this, i'm not sure.
164 echo FAI_ACTION=$fai_action >> /srv/fai/config/class/LAST.var