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