Debian Codegraph @

dev-utils/fai-kvm

raw
1 #! /bin/bash
2
3 # fai-kvm, start kvm host for testing FAI software
4 #
5 # Author: Thomas Lange, Uni Koeln, 2011-2026
6 # License: GPL v2 or any later version
7
8 fix="-k en-us -smp 2 -cpu host -global isa-fdc.fdtypeA=none " # if loading cirrusfb (via pcimodules and modprobe cirrusfb) causes errors in kvm
9 # without it centos initrd is not created properly and results in kernel panic
10
11 vga=
12 user=1
13 size=10G # default size of the disk image
14 ram=2G
15 disks=1
16 cdimage=/files/scratch/fai-cd.iso # default name for CD image
17 diskdir=/tmp # directory where the disk images will be created, a RAM disk is recommended
18 portbase=5900
19 usernet=0
20 newdisk=0
21 daemonize=0
22 efi=0
23 order=cd
24 usenvme=0
25 spice=0
26 secboot=0
27
28 # not all mac addresses work in kvm (or the bridge), be carefull when changing the first two bytes
29 # If you are generating your own MAC addresses you should use a value that contains 2,6,A or E as the second number as this defines a locally administered MAC address.
30 # x2:xx:xx:xx:xx:xx
31 # x6:xx:xx:xx:xx:xx
32 # xA:xx:xx:xx:xx:xx
33 # xE:xx:xx:xx:xx:xx
34
35 macprefix=52:54:00:11:23
36 # - - - - - - - - - - - - - - - - - - - - - - - - -
37 boot_disk() {
38
39 # boot from disk
40 local ldisk
41 local f
42
43 [ -n "$1" ] && disk=$1
44 shift
45 case "$disk" in
46 *.raw) f=",format=raw" ;;
47 esac
48
49 if [ $usenvme -eq 1 ]; then
50 ldisk="-drive file=$disk,if=none$f,id=nvme1 -device nvme,serial=SN123450001,drive=nvme1"
51 else
52 ldisk="-drive file=$disk,if=virtio$f "
53 fi
54 set -x
55 kvm $gopt -boot order=c $net $ldisk $*
56 }
57 # - - - - - - - - - - - - - - - - - - - - - - - - -
58 boot_pxe() {
59
60 # PXE boot
61 set -x
62 kvm $gopt -boot order=nc $net $disk $*
63 }
64 # - - - - - - - - - - - - - - - - - - - - - - - - -
65 boot_cd() {
66
67 [ -n "$1" ] && cdimage=$1
68 shift
69 # boot fai-cd
70 set -x
71 kvm $gopt -boot order=$order $net $disk -cdrom $cdimage $*
72 }
73 # - - - - - - - - - - - - - - - - - - - - - - - - -
74 boot_usb() {
75
76 [ -n "$1" ] && iso=$1
77 shift
78 # boot an USB stick
79 set -x
80 kvm $gopt -boot order=$order $net $disk -drive if=none,id=stick,format=raw,file=$iso -device nec-usb-xhci,id=xhci -device usb-storage,bus=xhci.0,drive=stick,bootindex=1 $*
81 }
82 # - - - - - - - - - - - - - - - - - - - - - - - - -
83 die() {
84 echo "$2" 1>&2
85 exit $1
86 }
87
88 # - - - - - - - - - - - - - - - - - - - - - - - - -
89 usage() {
90
91 cat <<EOF
92
93 fai-kvm [options] pxe # boot VM using PXE from NIC
94 fai-kvm [options] disk [diskimage] # boot VM from first disk
95 fai-kvm [options] cd [imagename] # boot VM from a CD ISO image
96 fai-kvm [options] usb [imagename] # boot VM from an USB ISO image
97
98 fai-kvm [options] pxe|cd|disk [imagename] -- [more options for kvm]
99
100 -b run the VM daemonized in the background, no video output, activates VNC unless -S was added
101 -S activate the spice remote protocol on port 5900+<user number>, add -b for running in the background
102 -E Boot the VM with UEFI instead of legacy BIOS
103 -n create a new empty disk image, which is used as a local disk
104 -N recreate a new empty disk image, even if one already exists
105 -s <size> size of the local disk (default is ${size}), a suffix like M,G,T can be used.
106 -d <num> number of local disks (default is 1)
107 -D <path> directory, where the disk image files are created (default: $diskdir)
108 -M Create NVMe drives instead of SATA disks. You may want to add -E.
109 -m <mem> RAM size (defaults is ${ram}), a suffix like M,G,T can be used.
110 -u <num> user number. This will become part of the MAC address of the interface
111 of the VM and is the number of the tap device number. It is also used
112 for the file name of the disk files. Each VM must have a unique number.
113 If you have created 9 tap devies you can use the numbers 1 to 9.
114 -U Use kvm user networking instead of tap devices
115 -O Swap boot order when booting an ISO. Default order is local disk, then ISO.
116 -t Set title of the Qemu window
117
118 You can add more options for kvm after --
119 The MAC prefix is set inside the script to $macprefix:XX
120 EOF
121 exit 0
122 }
123 # - - - - - - - - - - - - - - - - - - - - - - - - -
124
125 while getopts "BEbUhnNu:s:SMm:d:D:t:O" opt ; do
126 case "$opt" in
127 O) order=dc ;;
128 B) secboot=1 ;;
129 b) daemonize=1 ;;
130 E) efi=1 ;;
131 n) newdisk=1 ;;
132 N) newdisk=2 ;;
133 M) usenvme=1 ;;
134 U) usernet=1 ;;
135 u) user=$OPTARG ;;
136 m) ram=$OPTARG
137 if [ -z "${ram//[0-9]}" ]; then
138 ram="${ram}G"
139 fi
140 ;;
141 s) size=$OPTARG
142 if [ -z "${size//[0-9]}" ]; then
143 size="${size}G"
144 fi
145 ;;
146 S) spice=1 ;;
147 d) disks=$OPTARG ;;
148 D) diskdir=$OPTARG ;;
149 t) title=$OPTARG ;;
150 h) usage;;
151 ?) die 1 "Unknown option";;
152 esac
153 done
154 shift $((OPTIND - 1))
155
156 if [ -z "$1" ]; then
157 echo Missing argument. pxe, cd or disk
158 usage
159 exit 1
160 fi
161
162 : ${title:=FAI-kvm-$user}
163 if ! [[ $user =~ ^[0-9]+$ ]] ; then
164 die 2 "Error: Option -u needs a number"
165 fi
166 hex=$(printf %02X $user)
167 diskfile=$diskdir/faitest-disk-$user
168
169 port=$(($portbase + $user))
170 if [ $spice -eq 1 ]; then
171 vga="-vga qxl -spice port=$port,disable-ticketing=on"
172 fi
173 if [ $daemonize -eq 1 ]; then
174 if [ $spice -eq 1 ]; then
175 vga+=" -daemonize -display none"
176 else
177 vga="-daemonize -display none -vnc :$user"
178 fi
179 fi
180
181 mac=$macprefix:$hex
182
183 if [ $usernet = 1 ]; then
184 net="-device virtio-net-pci,netdev=net0 -netdev user,id=net0"
185 else
186 net="-device virtio-net-pci,netdev=net0,mac=$mac -netdev tap,ifname=tap$user,id=net0,script=no,downscript=no
187 "
188 fi
189
190 if [ $secboot = 1 ]; then
191 pf="ms.fd"
192 gopt="-machine q35,smm=on -object rng-random,filename=/dev/urandom,id=rng0 \
193 -device virtio-rng-pci,rng=rng0 \
194 -global driver=cfi.pflash01,property=secure,value=on \
195 "
196 else
197 pf="fd"
198
199 fi
200
201 # create new disk images
202 if [ X$newdisk != X0 ]; then
203 for i in $(seq 1 $disks) ; do
204 if [ X$newdisk = X1 -a -f $diskfile-$i.qcow2 ]; then
205 echo "Will not overwrite disk image. Please use -N."
206 exit 3
207 fi
208 rm -f $diskfile-$i.qcow2
209 qemu-img create -f qcow2 -o preallocation=metadata $diskfile-$i.qcow2 ${size}
210 done
211 fi
212
213 disk=""
214 for i in $(seq 1 $disks) ; do
215 if [ ! -f $diskfile-$i.qcow2 ]; then
216 echo "WARN: $diskfile-$i.qcow2 not found. Ignoring."
217 continue
218 fi
219 if [ $usenvme -eq 1 ]; then
220 disk="$disk -drive file=$diskfile-$i.qcow2,if=none,id=nvme$i -device nvme,serial=SN12345000$i,drive=nvme$i"
221 else
222 disk="$disk -drive file=$diskfile-$i.qcow2,if=virtio,index=$i"
223 fi
224 if [ $i -eq 1 ] ; then
225 disk="$disk"
226 fi
227 done
228 gopt="$fix $vga -m $ram -name $title $gopt"
229 if [ $efi = 1 ]; then
230 if [ ! -f /usr/share/OVMF/OVMF_CODE_4M.$pf ]; then
231 die 99 "UEFI firmware not found. Please install package ovmf."
232 fi
233 if [ "$newdisk" -eq 2 ]; then # always overwrite
234 cp -v /usr/share/OVMF/OVMF_VARS_4M.$pf $diskdir/faitest-uefi-$user-OVMF_VARS_4M.$pf
235 fi
236 if [ "$newdisk" -eq 1 ] && [ ! -f $diskdir/faitest-uefi-$user-OVMF_VARS_4M.fd ]; then
237 cp -v /usr/share/OVMF/OVMF_VARS_4M.$pf $diskdir/faitest-uefi-$user-OVMF_VARS_4M.$pf
238 fi
239 gopt="-drive if=pflash,format=raw,unit=0,readonly=on,file=/usr/share/OVMF/OVMF_CODE_4M.$pf \
240 -drive if=pflash,format=raw,unit=1,file=$diskdir/faitest-uefi-$user-OVMF_VARS_4M.$pf $gopt"
241
242 fi
243
244
245 # Loop through parameters until '--' to pass the remainder to kvm.
246 # The preceding parameters are stored in $param1 & $param2
247 # param1 = cd|pxe|pxe ; param2 = diskimage | imagename
248 param1=""
249 param2=""
250 while [[ $# -gt 0 ]]; do
251 if [ "$1" = "--" ]; then
252 shift
253 break
254 fi
255 param1="$param2"
256 param2="$1"
257 shift
258 done
259
260 # if we only have one parameter before '--'
261 if [ -z "$param1" ]; then
262 param1="$param2"
263 param2=""
264 fi
265
266 case "$param1" in
267 pxe) boot_pxe $* ;;
268 cd) boot_cd $param2 $* ;;
269 usb) boot_usb $param2 $* ;;
270 disk) boot_disk $param2 $* ;;
271 *)
272 echo "Wrong argument. Use one of pxe, cd, disk." >&2
273 usage
274 ;;
275 esac