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