2 # Copyright (C) 2016 Ian Kelling
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.
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.
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.
19 [[ $EUID == 0 ]] ||
exec sudo
-E "${BASH_SOURCE[0]}" "$@"
21 set -e; .
/usr
/local
/lib
/bash-bear
; set +e
23 this_file
="$(readlink -f -- "${BASH_SOURCE[0]}")"
24 readonly this_file this_dir
="${this_file%/*}"
29 e
() { echo "$*"; "$@"; }
34 # Usage: ${0##*/} [OPTIONS]
35 Setup fai or arch pxe (depending on $0 name)
36 then start a virtual machine to test the config
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).
47 Note, sometimes shutting down the existing demohost vm
48 fails. Just run again if that happens.
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.
59 Note: Uses GNU getopt options parsing style
68 temp
=$
(getopt
-l help dnpcrh
"$@") || usage
1
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 ;;
79 *) echo "$0: Internal error!" ; exit 1 ;;
84 # change this to test different disk counts.
89 rm -f /tmp
/fai-revm-did-pxe
91 if ip l show br0
&>/dev
/null
; then
92 net_arg
="-w bridge=br0,mac=52:54:00:9c:ef:ad"
94 # if this computer has ethernet, we could setup a br0 like so:
96 # fai-rvm error: no bridge detected. add one to interfaces like this:
97 # iface eth0 inet manual
104 # if we only have wifi, cant use eth0
105 net_arg
="-w network=default,mac=52:54:00:9c:ef:ad"
108 if [[ $this_dir == /a
/bin
/* ]]; then
109 # Copy our script elsewhere so we can develop it
110 # and save it at the same time it's running
111 rm -rf /tmp
/faifreeze
112 cp -ar /a
/bin
/fai
/tmp
/faifreeze
113 exec /tmp
/faifreeze
/${BASH_SOURCE##*/} "${orig_args[@]}"
118 [[ ${0##*/} == arch-revm
]]
123 if [[ -e /tmp
/fai-revm-did-pxe
]]; then
124 e .
/pxe-server
$dhcp_arg
130 if is_arch_revm
; then
131 e .
/pxe-server
$dhcp_arg demohost arch
132 touch /tmp
/fai-revm-did-pxe
134 # via osinfo-query os. guessing arch is closest to latest fedora.
138 e .
/pxe-server
$dhcp_arg demohost fai
139 touch /tmp
/fai-revm-did-pxe
142 killall fai-monitor
&>/dev
/null ||
:
144 if [[ ! $BASEFILE_DIR ]]; then
147 isopath
=$BASEFILE_DIR/$iso
148 isosrc
=$BASEFILE_DIR/BOOKWORM64.
tar.zst
149 if [[ ! -e $isopath || $
(stat
-c %Y
$isopath) -lt $
(stat
-c %Y
$isosrc) ]]; then
150 e fai-cd
-g $
(readlink
-f grub.cfg.
${iso%%.*}) -f -A $isopath
152 boot_arg
="--cdrom $isopath"
154 /a
/exe
/cedit
-s /srv
/fai
/nfsroot
/root
/.ssh
/authorized_keys
<~
/.ssh
/demo.pub
155 e myfai-chboot default
157 # I don't think these variants actually make a diff for us, but I
158 # use the appropriate one when trying a new distro just in case.
167 e virsh destroy
$name ||
:
169 e virsh destroy
$name ||
:
170 e virsh undefine
$name ||
:
173 ## begin virtual disk creation ##
175 for ((i
=1; i
<= disk_count
; i
++)); do
176 f
=/var
/lib
/libvirt
/images
/${name}$i
177 disk_arg
+=("--disk path=$f")
178 if $new_disk ||
[[ ! -e $f ]]; then
180 # https://btrfs.wiki.kernel.org/index.php/FAQ
183 e qemu-img create
-o preallocation
=metadata
-f qcow2
$f 50G
186 ## end virtual disk creation ##
188 if [[ $SSH_CLIENT ]]; then
189 console_arg
=--noautoconsole
192 # docker makes forward default to drop, which blocks the vm pxe on flidas. easiest solution:
193 e iptables
-P FORWARD ACCEPT
195 # --cpu host: this causes mkfs.btrfs to fail with a stack trace which began
197 # init_module+0x108/0x1000 [raid6_pq]
199 # uniq is to stop gtk-warning spam
200 # e virt-install --os-variant $variant -n $name --pxe -r 2048 --vcpus 1 \
201 # ${disk_arg[*]} -w bridge=br0,mac=52:54:00:9c:ef:ad $reboot_arg \
202 # --graphics spice,listen=0.0.0.0 $console_arg |& grep -v '^ *$' | uniq &
205 if (( $
(nproc
) > 2 )); then
209 e systemctl start libvirtd
210 e virt-install
--rng /dev
/urandom
--os-variant $variant -n $name $boot_arg -r 2048 --vcpus $cpus \
211 ${disk_arg[*]} $net_arg $reboot_arg \
212 --graphics spice
,listen
=0.0.0.0 $console_arg |
& grep -v '^ *$' |
uniq &
215 if [[ $SSH_CLIENT ]]; then
220 while ! timeout
-s 9 10 ssh -oBatchMode=yes root@
$name true
; do
225 rm -f /tmp
/fai-revm-did-pxe
226 e .
/pxe-server
$dhcp_arg
229 # this tends to remove it too soon
230 #echo | /a/exe/cedit -s /srv/fai/nfsroot/root/.ssh/authorized_keys
232 if is_arch_revm
; then
233 .
/arch-init-remote
$name