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