3 # Copyright (C) 2021 David Runge <dvzrv@archlinux.org>
5 # SPDX-License-Identifier: GPL-3.0-or-later
7 # A simple script to run an archiso image using qemu. The image can be booted
12 # - edk2-ovmf (when UEFI booting)
19 IFS='' read -r -d '' usagetext <<EOF || true
24 -b set boot type to 'BIOS' (default)
25 -d set image type to hard disk instead of optical disc
27 -i [image] image to boot into
28 -s use Secure Boot (only relevant when using UEFI)
29 -u set boot type to 'UEFI'
32 Run an image using UEFI:
33 $ run_archiso -u -i archiso-2021.05.23-x86_64.iso
35 printf '%s' "${usagetext}"
38 cleanup_working_dir() {
39 if [[ -d "${working_dir}" ]]; then
40 rm -rf -- "${working_dir}"
45 if [[ ! -f '/usr/share/edk2-ovmf/x64/OVMF_VARS.fd' ]]; then
46 printf 'ERROR: %s\n' "OVMF_VARS.fd not found. Install edk2-ovmf."
49 cp -av -- '/usr/share/edk2-ovmf/x64/OVMF_VARS.fd' "${working_dir}/"
53 if [[ -z "$image" ]]; then
54 printf 'ERROR: %s\n' "Image name can not be empty."
57 if [[ ! -f "$image" ]]; then
58 printf 'ERROR: %s\n' "Image file (${image}) does not exist."
64 if [[ "$boot_type" == 'uefi' ]]; then
66 if [[ "${secure_boot}" == 'on' ]]; then
67 printf '%s\n' 'Using Secure Boot'
68 local ovmf_code='/usr/share/edk2-ovmf/x64/OVMF_CODE.secboot.fd'
70 local ovmf_code='/usr/share/edk2-ovmf/x64/OVMF_CODE.fd'
73 '-drive' "if=pflash,format=raw,unit=0,file=${ovmf_code},readonly"
74 '-drive' "if=pflash,format=raw,unit=1,file=${working_dir}/OVMF_VARS.fd"
75 '-global' "driver=cfi.pflash01,property=secure,value=${secure_boot}"
80 -boot order=d,menu=on,reboot-timeout=5000 \
81 -m "size=3072,slots=0,maxmem=$((3072*1024*1024))" \
83 -name archiso,process=archiso_0 \
84 -device virtio-scsi-pci,id=scsi0 \
85 -device "scsi-${mediatype%rom},bus=scsi0.0,drive=${mediatype}0" \
86 -drive "id=${mediatype}0,if=none,format=raw,media=${mediatype/hd/disk},readonly=on,file=${image}" \
89 -device virtio-net-pci,romfile=,netdev=net0 -netdev user,id=net0 \
90 -machine type=q35,smm=on,accel=kvm \
91 -global ICH9-LPC.disable_s3=1 \
93 "${qemu_options[@]}" \
98 if [[ -z "$image" ]]; then
99 printf 'ERROR: %s\n' "Image name can not be empty."
102 if [[ ! -f "$image" ]]; then
103 printf 'ERROR: %s\n' "Image (${image}) does not exist."
114 working_dir="$(mktemp -dt run_archiso.XXXXXXXXXX)"
115 trap cleanup_working_dir EXIT
117 if (( ${#@} > 0 )); then
118 while getopts 'bdhi:su' flag; do
140 printf '%s\n' "Error: Wrong option. Try 'run_archiso -h'."