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