fix some bugs and mix other things
[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 -n Create new qcow2(s) for vm. Good for testing partitioning
38 script, to ensure a blank disk.
39 -p Use pxe instead of autodiscover iso with fai.
40 -r Do not boot after install is complete
41 -h|--help Print help and exit.
42
43 Note: Uses GNU getopt options parsing style
44 EOF
45 exit $1
46 }
47
48 orig_args=("$@")
49 new_disk=false
50 pxe=false
51 temp=$(getopt -l help dnprh "$@") || usage 1
52 eval set -- "$temp"
53 while true; do
54 case $1 in
55 -d) dhcp_arg=-d; shift ;;
56 -n) new_disk=true; shift ;;
57 -p) pxe=true; shift ;;
58 -r) reboot_arg=--noreboot; shift ;;
59 -h|--help) usage ;;
60 --) shift; break ;;
61 *) echo "$0: Internal error!" ; exit 1 ;;
62 esac
63 done
64
65
66 # change this to test different disk counts. 1 and > 1 should be the only
67 # important things to test.
68 disk_count=1
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 cp -ar /a/bin/fai /tmp/faifreeze
76 exec /tmp/faifreeze/${BASH_SOURCE##*/} "${orig_args[@]}"
77 fi
78
79 cd $script_dir
80
81 is_arch_revm() {
82 [[ ${0##*/} == arch-revm ]]
83 }
84
85 cleanup() {
86 echo "doing cleanup"
87 e ./pxe-server $dhcp_arg
88 ./faiserver-disable
89 }
90 _errcatch_cleanup=cleanup
91
92 boot_arg=--pxe
93 if is_arch_revm; then
94 e ./pxe-server $dhcp_arg demohost arch
95 sleep 2
96 # via osinfo-query os. guessing arch is closest to latest fedora.
97 variant=fedora22
98 else
99 if $pxe; then
100 e ./pxe-server $dhcp_arg demohost fai
101 sleep 2
102 else
103 killall fai-monitor &>/dev/null ||:
104 fai-monitor &
105 if [[ ! $BASEFILE_DIR ]]; then
106 BASEFILE_DIR=/tmp
107 fi
108 a=$BASEFILE_DIR/autodiscover.iso
109 b=$BASEFILE_DIR/STRETCH64.tar.gz
110 if [[ ! -e $a || $(stat -c %Y $a) -lt $(stat -c %Y $b) ]]; then
111 e s fai-cd -g $(readlink -f grub.cfg.autodiscover) -f -A $BASEFILE_DIR/autodiscover.iso
112 fi
113 boot_arg="--cdrom $BASEFILE_DIR/autodiscover.iso"
114 e fai-redep
115 e myfai-chboot default
116 fi
117 # I don't think these variants actually make a diff for us, but I
118 # use the appropriate one when trying a new distro just in case.
119 variant=ubuntu14.04
120 #variant=ubuntu16.04
121 #variant=debian8
122 fi
123
124 name=demohost
125
126 e s virshrm $name ||:
127
128
129 disk_arg=()
130 for ((i=1; i <= disk_count; i++)); do
131 f=/var/lib/libvirt/images/${name}$i
132 disk_arg+=("--disk path=$f")
133 if $new_disk || [[ ! -e $f ]]; then
134 s rm -f $f
135 e s qemu-img create -o preallocation=metadata -f qcow2 $f 50G
136 fi
137 done
138
139 if [[ $SSH_CLIENT ]]; then
140 console_arg=--noautoconsole
141 fi
142
143 # docker makes forward default to drop, which blocks the vm pxe on flidas. easiest solution:
144 s iptables -P FORWARD ACCEPT
145
146 # --cpu host: this causes mkfs.btrfs to fail with a stack trace which began
147 # something like:
148 # init_module+0x108/0x1000 [raid6_pq]
149 #
150 # uniq is to stop gtk-warning spam
151 # e s virt-install --os-variant $variant -n $name --pxe -r 2048 --vcpus 1 \
152 # ${disk_arg[*]} -w bridge=br0,mac=52:54:00:9c:ef:ad $reboot_arg \
153 # --graphics spice,listen=0.0.0.0 $console_arg |& grep -v '^ *$' | uniq &
154
155
156 e s virt-install --os-variant $variant -n $name $boot_arg -r 2048 --vcpus 1 \
157 ${disk_arg[*]} -w bridge=br0,mac=52:54:00:9c:ef:ad $reboot_arg \
158 --graphics spice,listen=0.0.0.0 $console_arg |& grep -v '^ *$' | uniq &
159
160
161 if [[ $SSH_CLIENT ]]; then
162 fg
163 fi
164
165 sleep 30
166 while ! timeout -s 9 10 ssh -oBatchMode=yes root@$name /bin/true; do
167 e sleep 5
168 done
169 unset _errcatch_cleanup
170 e ./pxe-server $dhcp_arg
171 if is_arch_revm; then
172 ./arch-init-remote $name
173 fi