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