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