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