scripts/run_archiso.sh: don't duplicate qemu commands for each boot mode

Various bash script improvements.
This commit is contained in:
nl6720 2020-08-17 20:26:26 +03:00
parent e9f209efbf
commit 1e0ffd0297
No known key found for this signature in database
GPG Key ID: 5CE88535E188D369

View File

@ -15,117 +15,106 @@
set -eu set -eu
print_help() { print_help() {
cat << EOF local usagetext
IFS='' read -r -d '' usagetext <<EOF || true
Usage: Usage:
run_archiso [options] run_archiso [options]
Options: Options:
-b set boot type to 'bios' (default) -b set boot type to 'BIOS' (default)
-h print help -h print help
-i [image] image to boot into -i [image] image to boot into
-s use secure boot (only relevant when using UEFI) -s use Secure Boot (only relevant when using UEFI)
-u set boot type to 'uefi' -u set boot type to 'UEFI'
Example: Example:
Run an image using UEFI: Run an image using UEFI:
$ run_archiso -u -i archiso-2020.05.23-x86_64.iso $ run_archiso -u -i archiso-2020.05.23-x86_64.iso
EOF EOF
printf '%s' "${usagetext}"
} }
cleanup_working_dir() { cleanup_working_dir() {
if [ -d "${working_dir}" ]; then if [[ -d "${working_dir}" ]]; then
rm -rf "${working_dir}" rm -rf -- "${working_dir}"
fi fi
} }
copy_ovmf_vars() { copy_ovmf_vars() {
if [ ! -f /usr/share/edk2-ovmf/x64/OVMF_VARS.fd ]; then if [[ ! -f '/usr/share/edk2-ovmf/x64/OVMF_VARS.fd' ]]; then
echo "ERROR: OVMF_VARS.fd not found. Install edk2-ovmf." printf 'ERROR: %s\n' "OVMF_VARS.fd not found. Install edk2-ovmf."
exit 1 exit 1
fi fi
cp -av /usr/share/edk2-ovmf/x64/OVMF_VARS.fd "${working_dir}" cp -av -- '/usr/share/edk2-ovmf/x64/OVMF_VARS.fd' "${working_dir}/"
} }
check_image() { check_image() {
if [ -z "$image" ]; then if [[ -z "$image" ]]; then
echo "ERROR: Image name can not be empty." printf 'ERROR: %s\n' "Image name can not be empty."
exit 1 exit 1
fi fi
if [ ! -f "$image" ]; then if [[ ! -f "$image" ]]; then
echo "ERROR: Image file ($image) does not exist." printf 'ERROR: %s\n' "Image file (${image}) does not exist."
exit 1 exit 1
fi fi
} }
run_image() { run_image() {
[ "$boot_type" == "bios" ] && run_image_using_bios if [[ "$boot_type" == 'uefi' ]]; then
[ "$boot_type" == "uefi" ] && run_image_using_uefi
}
run_image_using_bios() {
qemu-system-x86_64 \
-boot order=d,menu=on,reboot-timeout=5000 \
-m size=3072,slots=0,maxmem=$((3072*1024*1024)) \
-k en \
-name archiso,process=archiso_0 \
-drive file="${image}",media=cdrom,readonly=on,if=virtio \
-display sdl \
-vga virtio \
-device virtio-net-pci,netdev=net0 -netdev user,id=net0 \
-enable-kvm \
-no-reboot
}
run_image_using_uefi() {
local ovmf_code=/usr/share/edk2-ovmf/x64/OVMF_CODE.fd
local secure_boot_state=off
copy_ovmf_vars copy_ovmf_vars
if [ "${secure_boot}" == "yes" ]; then if [[ "${secure_boot}" == 'on' ]]; then
echo "Using Secure Boot" printf '%s\n' 'Using Secure Boot'
ovmf_code=/usr/share/edk2-ovmf/x64/OVMF_CODE.secboot.fd local ovmf_code='/usr/share/edk2-ovmf/x64/OVMF_CODE.secboot.fd'
secure_boot_state=on else
local ovmf_code='/usr/share/edk2-ovmf/x64/OVMF_CODE.fd'
fi fi
qemu_options+=(
'-drive' "if=pflash,format=raw,unit=0,file=${ovmf_code},readonly"
'-drive' "if=pflash,format=raw,unit=1,file=${working_dir}/OVMF_VARS.fd"
'-global' "driver=cfi.pflash01,property=secure,value=${secure_boot}"
)
fi
qemu-system-x86_64 \ qemu-system-x86_64 \
-boot order=d,menu=on,reboot-timeout=5000 \ -boot order=d,menu=on,reboot-timeout=5000 \
-m size=3072,slots=0,maxmem=$((3072*1024*1024)) \ -m "size=3072,slots=0,maxmem=$((3072*1024*1024))" \
-k en \ -k en \
-name archiso,process=archiso_0 \ -name archiso,process=archiso_0 \
-drive file="${image}",media=cdrom,readonly=on,if=virtio \ -drive file="${image}",media=cdrom,readonly=on,if=virtio \
-drive if=pflash,format=raw,unit=0,file="${ovmf_code}",readonly \
-drive if=pflash,format=raw,unit=1,file="${working_dir}/OVMF_VARS.fd" \
-machine type=q35,smm=on,accel=kvm \
-global driver=cfi.pflash01,property=secure,value="${secure_boot_state}" \
-global ICH9-LPC.disable_s3=1 \
-display sdl \ -display sdl \
-vga virtio \ -vga virtio \
-device virtio-net-pci,netdev=net0 -netdev user,id=net0 \ -device virtio-net-pci,netdev=net0 -netdev user,id=net0 \
-machine type=q35,smm=on,accel=kvm \
-global ICH9-LPC.disable_s3=1 \
-enable-kvm \ -enable-kvm \
"${qemu_options[@]}" \
-no-reboot -no-reboot
} }
set_image() { set_image() {
if [ -z "$image" ]; then if [[ -z "$image" ]]; then
echo "ERROR: Image name can not be empty." printf 'ERROR: %s\n' "Image name can not be empty."
exit 1 exit 1
fi fi
if [ ! -f "$image" ]; then if [[ ! -f "$image" ]]; then
echo "ERROR: Image ($image) does not exist." printf 'ERROR: %s\n' "Image (${image}) does not exist."
exit 1 exit 1
fi fi
image="$1" image="$1"
} }
image="" image=''
boot_type="bios" boot_type='bios'
secure_boot="no" secure_boot='off'
working_dir="$(mktemp -d)" qemu_options=()
working_dir="$(mktemp -dt run_archiso.XXXXXXXXXX)"
trap cleanup_working_dir EXIT trap cleanup_working_dir EXIT
if [ ${#@} -gt 0 ]; then if (( ${#@} > 0 )); then
while getopts 'bhi:su' flag; do while getopts 'bhi:su' flag; do
case "${flag}" in case "$flag" in
b) b)
boot_type=bios boot_type='bios'
;; ;;
h) h)
print_help print_help
@ -135,13 +124,13 @@ if [ ${#@} -gt 0 ]; then
image="$OPTARG" image="$OPTARG"
;; ;;
u) u)
boot_type=uefi boot_type='uefi'
;; ;;
s) s)
secure_boot=yes secure_boot='on'
;; ;;
*) *)
echo "Error: Wrong option. Try 'run_archiso -h'." printf '%s\n' "Error: Wrong option. Try 'run_archiso -h'."
exit 1 exit 1
;; ;;
esac esac