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