revert chost, broke for localhost
[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
18
19
20 cleanup() { :; }
21 set -eE -o pipefail
22 trap 'cleanup; echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
23 script_dir=$(dirname $(readlink -f "$BASH_SOURCE"))
24
25 e() { echo "$*"; "$@"; }
26
27
28 usage() {
29 cat <<EOF
30 # Usage: ${0##*/} [OPTIONS]
31 Setup fai or arch pxe (depending on $0 name)
32 then start a virtual machine to test the config
33
34 Note, sometimes shutting down the existing demohost vm
35 fails. Just run again if that happens.
36
37 -r Do not boot after install is complete
38 -n Create new qcow2(s) for vm
39 -h|--help Print help and exit.
40
41 Note: Uses GNU getopt options parsing style
42 EOF
43 exit $1
44 }
45
46 new_disk=false
47 temp=$(getopt -l help hnr "$@") || usage 1
48 eval set -- "$temp"
49 while true; do
50 case $1 in
51 -n) new_disk=true; shift ;;
52 -r) reboot_arg=--noreboot; shift ;;
53 -h|--help) usage ;;
54 --) shift; break ;;
55 *) echo "$0: Internal error!" ; exit 1 ;;
56 esac
57 done
58
59
60 # change this to test different disk counts. 1 and > 1 should be the only
61 # important things to test.
62 disk_count=1
63
64
65 if [[ $script_dir == /a/bin/* ]]; then
66 # Copy our script elsewhere so we can develop it
67 # and save it at the same time it's running
68 rm -rf /tmp/faifreeze
69 mkdir -p /a/tmp
70 cp -ar /a/bin/fai /tmp/faifreeze
71 exec /tmp/faifreeze/${BASH_SOURCE##*/} "$@"
72 fi
73
74 cd $script_dir
75
76 is_arch_revm() {
77 [[ ${0##*/} == arch-revm ]]
78 }
79
80 cleanup() {
81 ./pxe-server
82 ./faiserver-disable
83 }
84 if is_arch_revm; then
85 ./pxe-server demohost arch
86 sleep 2
87 # via osinfo-query os. guessing arch is closest to latest fedora.
88 variant=fedora22
89 else
90 ./pxe-server demohost fai
91 sleep 2
92 # I don't think these variants actually make a diff for us, but I
93 # use the appropriate one when trying a new distro just in case.
94 variant=ubuntu14.04
95 #variant=ubuntu16.04
96 #variant=debian8
97 fi
98
99 name=demohost
100
101 e s virshrm $name ||:
102
103
104 disk_arg=()
105 for ((i=1; i <= disk_count; i++)); do
106 f=/var/lib/libvirt/images/${name}$i
107 disk_arg+=("--disk path=$f")
108 if $new_disk || [[ ! -e $f ]]; then
109 s rm -f $f
110 e s qemu-img create -o preallocation=metadata -f qcow2 $f 20G
111 fi
112 done
113
114 if [[ $SSH_CLIENT ]]; then
115 console_arg=--noautoconsole
116 fi
117
118 # --cpu host: this causes mkfs.btrfs to fail with a stack trace which began
119 # something like:
120 # init_module+0x108/0x1000 [raid6_pq]
121 #
122 # uniq is to stop gtk-warning spam
123 e s virt-install --os-variant $variant -n $name --pxe -r 2048 --vcpus 1 \
124 ${disk_arg[*]} -w bridge=br0,mac=52:54:00:9c:ef:ad $reboot_arg \
125 --graphics spice,listen=0.0.0.0 $console_arg |& grep -v '^ *$' | uniq &
126
127 if [[ $SSH_CLIENT ]]; then
128 fg
129 fi
130
131 sleep 30
132 while ! timeout -s 9 10 ssh -oBatchMode=yes root@$name /bin/true; do
133 e sleep 5
134 done
135 cleanup() { :; }
136 e pxe-server
137 if is_arch_revm; then
138 ./arch-init-remote $name
139 fi