fix pxe server leaving background procs
[automated-distro-installer] / fai-revm
1 #!/bin/bash -l
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 set -x
18
19 # Note, sometimes shutting down the existing demohost vm
20 # fails. Just run again if that happens.
21
22 # Deploy fai configuration to faiserver,
23 # then start a virtual machine to test the config.
24
25 cleanup() { :; }
26 set -eE -o pipefail
27 trap 'cleanup; echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
28 script_dir=$(dirname $(readlink -f "$BASH_SOURCE"))
29
30 e() { echo "$*"; "$@"; }
31
32 # I had this set false as default before, can't remember why. oh well.
33 redeploy=true
34 if [[ $1 == -r ]]; then
35 redeploy=false
36 fi
37
38 disk_count=2
39
40
41 if [[ $script_dir == /a/bin/* ]]; then
42 # Copy our script elsewhere so we can develop it
43 # and save it at the same time it's running
44 rm -rf /tmp/faifreeze
45 mkdir -p /a/tmp
46 cp -ar /a/bin/fai /tmp/faifreeze
47 exec /tmp/faifreeze/${BASH_SOURCE##*/} "$@"
48 fi
49
50 cd $script_dir
51
52 is_arch_revm() {
53 [[ ${0##*/} == arch-revm ]]
54 }
55
56 new_disk=false
57 [[ ! $1 ]] || new_disk=true
58
59 cleanup() { ./pxe-server :; }
60 if is_arch_revm; then
61 ./pxe-server arch
62 sleep 2
63 # via osinfo-query os. guessing arch is closest to latest fedora.
64 variant=fedora22
65 else
66 ./pxe-server fai
67 sleep 2
68 if $redeploy; then
69 ./fai-redep
70 fi
71 variant=debian8
72 fi
73
74 name=demohost
75
76 e s virshrm $name ||:
77
78
79 disk_arg=()
80 for ((i=1; i <= disk_count; i++)); do
81 f=/var/lib/libvirt/images/${name}$i
82 disk_arg+=("--disk path=$f")
83 if $new_disk || [[ ! -e $f ]]; then
84 s rm -f $f
85 e s qemu-img create -o preallocation=metadata -f qcow2 $f 20G
86 fi
87 done
88
89 if [[ $SSH_CLIENT ]]; then
90 console_arg=--noautoconsole
91 fi
92
93 # --cpu host: this causes mkfs.btrfs to fail with a stack trace which began
94 # something like:
95 # init_module+0x108/0x1000 [raid6_pq]
96 #
97 # uniq is to stop gtk-warning spam
98 e s virt-install --os-variant $variant -n $name --pxe -r 2048 --vcpus 1 \
99 ${disk_arg[*]} -w bridge=br0,mac=52:54:00:9c:ef:ad \
100 --graphics spice,listen=0.0.0.0 $console_arg |& grep -v '^ *$' | uniq &
101
102 if [[ $SSH_CLIENT ]]; then
103 fg
104 fi
105
106 sleep 30
107 while ! timeout -s 9 10 ssh root@$name /bin/true; do sleep 5; done
108 cleanup() { :; }
109 pxe-server :
110 if is_arch_revm; then
111 ./arch-init-remote $name
112 fi