bug fix
[automated-distro-installer] / fai-revm
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18
19 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
20
21 set -e; . /usr/local/lib/bash-bear; set +e
22
23 this_file="$(readlink -f -- "${BASH_SOURCE[0]}")"
24 readonly this_file this_dir="${this_file%/*}"
25 cd "$this_dir"
26
27 PATH="$PATH:$PWD"
28
29 e() { echo "$*"; "$@"; }
30
31
32 usage() {
33 cat <<EOF
34 # Usage: ${0##*/} [OPTIONS]
35 Setup fai or arch pxe (depending on $0 name)
36 then start a virtual machine to test the config
37
38 todo: make it so this can run this on a network we dont control, the 2
39 ways I know which could work are either running in libvirt's the nated
40 network, and altering the dnsmasq options for the dnsmasq that runs in
41 that, or giving the vm a static ip and for resolving faiserver, and then
42 for resolving "faiserver", either setup some static resolution in the
43 vm, or give it the host machine's ip as a dns server, or in general
44 change references of faiserver to faiserver.b8.nz (I like this idea
45 because it helps in other cases too).
46
47 Note, sometimes shutting down the existing demohost vm
48 fails. Just run again if that happens.
49
50 -d When doing pxe with -p, don't do dhcp setup. Good for when we
51 aren't on Ian's home network.
52 -n Create new qcow2(s) for vm. Good for testing partitioning
53 script, to ensure a blank disk.
54 -p Use pxe instead of autodiscover iso with fai.
55 -c Use normal fai-cd iso is instead of autodiscover iso.
56 -r Do not boot after install is complete
57 -h|--help Print help and exit.
58
59 Note: Uses GNU getopt options parsing style
60 EOF
61 exit $1
62 }
63
64 orig_args=("$@")
65 new_disk=false
66 pxe=false
67 iso=autodiscover.iso
68 temp=$(getopt -l help dnpcrh "$@") || usage 1
69 eval set -- "$temp"
70 while true; do
71 case $1 in
72 -d) dhcp_arg=-d; shift ;;
73 -n) new_disk=true; shift ;;
74 -p) pxe=true; shift ;;
75 -c) iso=netinst.iso; shift ;;
76 -r) reboot_arg=--noreboot; shift ;;
77 -h|--help) usage ;;
78 --) shift; break ;;
79 *) echo "$0: Internal error!" ; exit 1 ;;
80 esac
81 done
82
83
84 # change this to test different disk counts.
85 disk_count=3
86 disk_count=1
87
88
89 rm -f /tmp/fai-revm-did-pxe
90
91 if ip l show br0 &>/dev/null; then
92 net_arg="-w bridge=br0,mac=52:54:00:9c:ef:ad"
93 else
94 # if this computer has ethernet, we could setup a br0 like so:
95 # cat <<'EOF'
96 # fai-rvm error: no bridge detected. add one to interfaces like this:
97 # iface eth0 inet manual
98 # iface br0 inet dhcp
99 # bridge_ports eth0
100 # bridge_stp off
101 # bridge_maxwait 0
102 # EOF
103
104 # if we only have wifi, cant use eth0
105 net_arg="-w network=default,mac=52:54:00:9c:ef:ad"
106 fi
107
108 if [[ $this_dir == /a/bin/* ]]; then
109 # Copy our script elsewhere so we can develop it
110 # and save it at the same time it's running
111 rm -rf /tmp/faifreeze
112 cp -ar /a/bin/fai /tmp/faifreeze
113 exec /tmp/faifreeze/${BASH_SOURCE##*/} "${orig_args[@]}"
114 fi
115
116
117 is_arch_revm() {
118 [[ ${0##*/} == arch-revm ]]
119 }
120
121 err-cleanup() {
122 echo "doing cleanup"
123 if [[ -e /tmp/fai-revm-did-pxe ]]; then
124 e ./pxe-server $dhcp_arg
125 fi
126 ./faiserver-disable
127 }
128
129 boot_arg=--pxe
130 if is_arch_revm; then
131 e ./pxe-server $dhcp_arg demohost arch
132 touch /tmp/fai-revm-did-pxe
133 sleep 2
134 # via osinfo-query os. guessing arch is closest to latest fedora.
135 variant=fedora22
136 else
137 if $pxe; then
138 e ./pxe-server $dhcp_arg demohost fai
139 touch /tmp/fai-revm-did-pxe
140 sleep 2
141 else
142 killall fai-monitor &>/dev/null ||:
143 fai-monitor &
144 if [[ ! $BASEFILE_DIR ]]; then
145 BASEFILE_DIR=/tmp
146 fi
147 isopath=$BASEFILE_DIR/$iso
148 isosrc=$BASEFILE_DIR/BOOKWORM64.tar.zst
149 if [[ ! -e $isopath || $(stat -c %Y $isopath) -lt $(stat -c %Y $isosrc) ]]; then
150 e fai-cd -g $(readlink -f grub.cfg.${iso%%.*}) -f -A $isopath
151 fi
152 boot_arg="--cdrom $isopath"
153 e fai-redep
154 /a/exe/cedit -s /srv/fai/nfsroot/root/.ssh/authorized_keys <~/.ssh/demo.pub
155 e myfai-chboot default
156 fi
157 # I don't think these variants actually make a diff for us, but I
158 # use the appropriate one when trying a new distro just in case.
159 #variant=ubuntu14.04
160 #variant=ubuntu16.04
161 #variant=debian8
162 variant=ubuntu20.04
163 fi
164
165 name=demohost
166
167 e virsh destroy $name ||:
168 sleep 1
169 e virsh destroy $name ||:
170 e virsh undefine $name ||:
171 sleep 1
172
173 ## begin virtual disk creation ##
174 disk_arg=()
175 for ((i=1; i <= disk_count; i++)); do
176 f=/var/lib/libvirt/images/${name}$i
177 disk_arg+=("--disk path=$f")
178 if $new_disk || [[ ! -e $f ]]; then
179 rm -f $f
180 # https://btrfs.wiki.kernel.org/index.php/FAQ
181 touch $f
182 chattr +C $f
183 e qemu-img create -o preallocation=metadata -f qcow2 $f 50G
184 fi
185 done
186 ## end virtual disk creation ##
187
188 if [[ $SSH_CLIENT ]]; then
189 console_arg=--noautoconsole
190 fi
191
192 # docker makes forward default to drop, which blocks the vm pxe on flidas. easiest solution:
193 e iptables -P FORWARD ACCEPT
194
195 # --cpu host: this causes mkfs.btrfs to fail with a stack trace which began
196 # something like:
197 # init_module+0x108/0x1000 [raid6_pq]
198 #
199 # uniq is to stop gtk-warning spam
200 # e virt-install --os-variant $variant -n $name --pxe -r 2048 --vcpus 1 \
201 # ${disk_arg[*]} -w bridge=br0,mac=52:54:00:9c:ef:ad $reboot_arg \
202 # --graphics spice,listen=0.0.0.0 $console_arg |& grep -v '^ *$' | uniq &
203
204 cpus=1
205 if (( $(nproc) > 2 )); then
206 cpus=2
207 fi
208
209 e systemctl start libvirtd
210 e virt-install --rng /dev/urandom --os-variant $variant -n $name $boot_arg -r 2048 --vcpus $cpus \
211 ${disk_arg[*]} $net_arg $reboot_arg \
212 --graphics spice,listen=0.0.0.0 $console_arg |& grep -v '^ *$' | uniq &
213
214
215 if [[ $SSH_CLIENT ]]; then
216 fg
217 fi
218
219 sleep 90
220 while ! timeout -s 9 10 ssh -oBatchMode=yes root@$name true; do
221 e sleep 5
222 done
223 unset -f err-cleanup
224 if $pxe; then
225 rm -f /tmp/fai-revm-did-pxe
226 e ./pxe-server $dhcp_arg
227 fi
228
229 # this tends to remove it too soon
230 #echo | /a/exe/cedit -s /srv/fai/nfsroot/root/.ssh/authorized_keys
231
232 if is_arch_revm; then
233 ./arch-init-remote $name
234 fi