just docs
[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 reboot.
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
47 new_disk=false
48 reboot=true
49 temp=$(getopt -l help hnr "$@") || usage 1
50 eval set -- "$temp"
51 while true; do
52 case $1 in
53 -n) new_disk=true; shift ;;
54 -r) reboot=false; shift ;;
55 -h|--help) usage ;;
56 --) shift; break ;;
57 *) echo "$0: Internal error!" ; exit 1 ;;
58 esac
59 done
60
61
62 # change this to test different disk counts. 1 and > 1 should be the only
63 # important things to test.
64 disk_count=1
65
66
67 if [[ $script_dir == /a/bin/* ]]; then
68 # Copy our script elsewhere so we can develop it
69 # and save it at the same time it's running
70 rm -rf /tmp/faifreeze
71 mkdir -p /a/tmp
72 cp -ar /a/bin/fai /tmp/faifreeze
73 exec /tmp/faifreeze/${BASH_SOURCE##*/} "$@"
74 fi
75
76 cd $script_dir
77
78 is_arch_revm() {
79 [[ ${0##*/} == arch-revm ]]
80 }
81
82 cleanup() { ./pxe-server; }
83 if is_arch_revm; then
84 ./pxe-server demohost arch
85 sleep 2
86 # via osinfo-query os. guessing arch is closest to latest fedora.
87 variant=fedora22
88 else
89 ./pxe-server demohost fai
90 sleep 2
91 # I don't think these variants actually make a diff for us, but I
92 # use the appropriate one when trying a new distro just in case.
93 variant=ubuntu14.04
94 #variant=ubuntu16.04
95 #variant=debian8
96 fi
97
98 name=demohost
99
100 e s virshrm $name ||:
101
102
103 disk_arg=()
104 for ((i=1; i <= disk_count; i++)); do
105 f=/var/lib/libvirt/images/${name}$i
106 disk_arg+=("--disk path=$f")
107 if $new_disk || [[ ! -e $f ]]; then
108 s rm -f $f
109 e s qemu-img create -o preallocation=metadata -f qcow2 $f 20G
110 fi
111 done
112
113 if [[ $SSH_CLIENT ]]; then
114 console_arg=--noautoconsole
115 fi
116
117 # --cpu host: this causes mkfs.btrfs to fail with a stack trace which began
118 # something like:
119 # init_module+0x108/0x1000 [raid6_pq]
120 #
121 # uniq is to stop gtk-warning spam
122 e s virt-install --os-variant $variant -n $name --pxe -r 2048 --vcpus 1 \
123 ${disk_arg[*]} -w bridge=br0,mac=52:54:00:9c:ef:ad \
124 --graphics spice,listen=0.0.0.0 $console_arg |& grep -v '^ *$' | uniq &
125
126 if [[ $SSH_CLIENT ]]; then
127 fg
128 fi
129
130 sleep 30
131 while ! timeout -s 9 10 ssh root@$name /bin/true; do
132 e sleep 5
133 done
134 cleanup() { :; }
135 e pxe-server
136 if is_arch_revm; then
137 ./arch-init-remote $name
138 fi