use latest btrfs-progs to match linux-libre
[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 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
20
21
22 readonly this_file="$(readlink -f -- "${BASH_SOURCE[0]}")"
23 script_dir="${this_file%/*}"
24 # shellcheck source=./bash-trace
25 source "${script_dir}/bash-trace"
26 cd $script_dir
27 PATH="$PATH:$PWD"
28
29 e() { echo "$*"; "$@"; }
30
31
32 usage() {
33 cat <<EOF
34 # Usage: ${0##*/} [OPTIONS]
35 Setup fai or arch pxe (depending on $0 name)
36 then start a virtual machine to test the config
37
38 todo: make it so this can run this on a network we dont control, the 2
39 ways I know which could work are either running in libvirt's the nated
40 network, and altering the dnsmasq options for the dnsmasq that runs in
41 that, or giving the vm a static ip and for resolving faiserver, and then
42 for resolving "faiserver", either setup some static resolution in the
43 vm, or give it the host machine's ip as a dns server, or in general
44 change references of faiserver to faiserver.b8.nz (I like this idea
45 because it helps in other cases too).
46
47 Note, sometimes shutting down the existing demohost vm
48 fails. Just run again if that happens.
49
50 -d When doing pxe with -p, don't do dhcp setup. Good for when we
51 aren't on Ian's home network.
52 -n Create new qcow2(s) for vm. Good for testing partitioning
53 script, to ensure a blank disk.
54 -p Use pxe instead of autodiscover iso with fai.
55 -c Use normal fai-cd iso is instead of autodiscover iso.
56 -r Do not boot after install is complete
57 -h|--help Print help and exit.
58
59 Note: Uses GNU getopt options parsing style
60 EOF
61 exit $1
62 }
63
64 orig_args=("$@")
65 new_disk=false
66 pxe=false
67 iso=autodiscover.iso
68 temp=$(getopt -l help dnpcrh "$@") || usage 1
69 eval set -- "$temp"
70 while true; do
71 case $1 in
72 -d) dhcp_arg=-d; shift ;;
73 -n) new_disk=true; shift ;;
74 -p) pxe=true; shift ;;
75 -c) iso=netinst.iso; shift ;;
76 -r) reboot_arg=--noreboot; shift ;;
77 -h|--help) usage ;;
78 --) shift; break ;;
79 *) echo "$0: Internal error!" ; exit 1 ;;
80 esac
81 done
82
83
84 # change this to test different disk counts.
85 disk_count=3
86 disk_count=1
87
88
89 rm -f /tmp/fai-revm-did-pxe
90
91 if ! ip l show br0 &>/dev/null; then
92 cat <<'EOF'
93 fai-rvm error: no bridge detected. add one to interfaces like this:
94 iface eth0 inet manual
95 iface br0 inet dhcp
96 bridge_ports eth0
97 bridge_stp off
98 bridge_maxwait 0
99 EOF
100 exit 1
101 fi
102
103 if [[ $script_dir == /a/bin/* ]]; then
104 # Copy our script elsewhere so we can develop it
105 # and save it at the same time it's running
106 rm -rf /tmp/faifreeze
107 cp -ar /a/bin/fai /tmp/faifreeze
108 exec /tmp/faifreeze/${BASH_SOURCE##*/} "${orig_args[@]}"
109 fi
110
111
112 is_arch_revm() {
113 [[ ${0##*/} == arch-revm ]]
114 }
115
116 err-cleanup() {
117 echo "doing cleanup"
118 if [[ -e /tmp/fai-revm-did-pxe ]]; then
119 e ./pxe-server $dhcp_arg
120 fi
121 ./faiserver-disable
122 }
123
124 boot_arg=--pxe
125 if is_arch_revm; then
126 e ./pxe-server $dhcp_arg demohost arch
127 touch /tmp/fai-revm-did-pxe
128 sleep 2
129 # via osinfo-query os. guessing arch is closest to latest fedora.
130 variant=fedora22
131 else
132 if $pxe; then
133 e ./pxe-server $dhcp_arg demohost fai
134 touch /tmp/fai-revm-did-pxe
135 sleep 2
136 else
137 killall fai-monitor &>/dev/null ||:
138 fai-monitor &
139 if [[ ! $BASEFILE_DIR ]]; then
140 BASEFILE_DIR=/tmp
141 fi
142 isopath=$BASEFILE_DIR/$iso
143 isosrc=$BASEFILE_DIR/BULLSEYE64.tar.gz
144 if [[ ! -e $isopath || $(stat -c %Y $isopath) -lt $(stat -c %Y $isosrc) ]]; then
145 e fai-cd -g $(readlink -f grub.cfg.${iso%%.*}) -f -A $isopath
146 fi
147 boot_arg="--cdrom $isopath"
148 e fai-redep
149 cat ~/.ssh/demo.pub | /a/exe/cedit -s /srv/fai/nfsroot/root/.ssh/authorized_keys
150 e myfai-chboot default
151 fi
152 # I don't think these variants actually make a diff for us, but I
153 # use the appropriate one when trying a new distro just in case.
154 #variant=ubuntu14.04
155 #variant=ubuntu16.04
156 #variant=debian8
157 variant=ubuntu20.04
158 fi
159
160 name=demohost
161
162 e virsh destroy $name ||:
163 sleep 1
164 e virsh destroy $name ||:
165 e virsh undefine $name ||:
166 sleep 1
167
168
169 disk_arg=()
170 for ((i=1; i <= disk_count; i++)); do
171 f=/var/lib/libvirt/images/${name}$i
172 disk_arg+=("--disk path=$f")
173 if $new_disk || [[ ! -e $f ]]; then
174 rm -f $f
175 # https://btrfs.wiki.kernel.org/index.php/FAQ
176 touch $f
177 chattr +C $f
178 e qemu-img create -o preallocation=metadata -f qcow2 $f 50G
179 fi
180 done
181
182 if [[ $SSH_CLIENT ]]; then
183 console_arg=--noautoconsole
184 fi
185
186 # docker makes forward default to drop, which blocks the vm pxe on flidas. easiest solution:
187 e iptables -P FORWARD ACCEPT
188
189 # --cpu host: this causes mkfs.btrfs to fail with a stack trace which began
190 # something like:
191 # init_module+0x108/0x1000 [raid6_pq]
192 #
193 # uniq is to stop gtk-warning spam
194 # e virt-install --os-variant $variant -n $name --pxe -r 2048 --vcpus 1 \
195 # ${disk_arg[*]} -w bridge=br0,mac=52:54:00:9c:ef:ad $reboot_arg \
196 # --graphics spice,listen=0.0.0.0 $console_arg |& grep -v '^ *$' | uniq &
197
198 cpus=1
199 if (( $(nproc) > 2 )); then
200 cpus=2
201 fi
202
203 e systemctl start libvirtd
204 e virt-install --rng /dev/urandom --os-variant $variant -n $name $boot_arg -r 2048 --vcpus $cpus \
205 ${disk_arg[*]} -w bridge=br0,mac=52:54:00:9c:ef:ad $reboot_arg \
206 --graphics spice,listen=0.0.0.0 $console_arg |& grep -v '^ *$' | uniq &
207
208
209 if [[ $SSH_CLIENT ]]; then
210 fg
211 fi
212
213 sleep 90
214 while ! timeout -s 9 10 ssh -oBatchMode=yes root@$name true; do
215 e sleep 5
216 done
217 unset -f err-cleanup
218 if $pxe; then
219 rm -f /tmp/fai-revm-did-pxe
220 e ./pxe-server $dhcp_arg
221 fi
222
223 # this tends to remove it too soon
224 #echo | /a/exe/cedit -s /srv/fai/nfsroot/root/.ssh/authorized_keys
225
226 if is_arch_revm; then
227 ./arch-init-remote $name
228 fi