2020-07-02 10:11:49 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
|
|
|
# Copyright (C) 2020 David Runge <dvzrv@archlinux.org>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
#
|
|
|
|
# A simple script to run an archiso image using qemu. The image can be booted
|
|
|
|
# using BIOS or UEFI.
|
|
|
|
#
|
|
|
|
# Requirements:
|
|
|
|
# - qemu
|
|
|
|
# - edk2-ovmf (when UEFI booting)
|
|
|
|
|
|
|
|
|
|
|
|
set -eu
|
|
|
|
|
|
|
|
print_help() {
|
2020-08-17 19:26:26 +02:00
|
|
|
local usagetext
|
|
|
|
IFS='' read -r -d '' usagetext <<EOF || true
|
2020-07-02 10:11:49 +02:00
|
|
|
Usage:
|
|
|
|
run_archiso [options]
|
|
|
|
|
|
|
|
Options:
|
2020-08-17 19:26:26 +02:00
|
|
|
-b set boot type to 'BIOS' (default)
|
2020-08-17 19:33:50 +02:00
|
|
|
-d set image type to hard disk instead of optical disc
|
2020-07-02 10:11:49 +02:00
|
|
|
-h print help
|
|
|
|
-i [image] image to boot into
|
2020-08-17 19:26:26 +02:00
|
|
|
-s use Secure Boot (only relevant when using UEFI)
|
|
|
|
-u set boot type to 'UEFI'
|
2020-07-02 10:11:49 +02:00
|
|
|
|
|
|
|
Example:
|
|
|
|
Run an image using UEFI:
|
|
|
|
$ run_archiso -u -i archiso-2020.05.23-x86_64.iso
|
|
|
|
EOF
|
2020-08-17 19:26:26 +02:00
|
|
|
printf '%s' "${usagetext}"
|
2020-07-02 10:11:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup_working_dir() {
|
2020-08-17 19:26:26 +02:00
|
|
|
if [[ -d "${working_dir}" ]]; then
|
|
|
|
rm -rf -- "${working_dir}"
|
2020-07-02 10:11:49 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
copy_ovmf_vars() {
|
2020-08-17 19:26:26 +02:00
|
|
|
if [[ ! -f '/usr/share/edk2-ovmf/x64/OVMF_VARS.fd' ]]; then
|
|
|
|
printf 'ERROR: %s\n' "OVMF_VARS.fd not found. Install edk2-ovmf."
|
2020-07-02 10:11:49 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
2020-08-17 19:26:26 +02:00
|
|
|
cp -av -- '/usr/share/edk2-ovmf/x64/OVMF_VARS.fd' "${working_dir}/"
|
2020-07-02 10:11:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
check_image() {
|
2020-08-17 19:26:26 +02:00
|
|
|
if [[ -z "$image" ]]; then
|
|
|
|
printf 'ERROR: %s\n' "Image name can not be empty."
|
2020-07-02 10:11:49 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
2020-08-17 19:26:26 +02:00
|
|
|
if [[ ! -f "$image" ]]; then
|
|
|
|
printf 'ERROR: %s\n' "Image file (${image}) does not exist."
|
2020-07-02 10:11:49 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
run_image() {
|
2020-08-17 19:26:26 +02:00
|
|
|
if [[ "$boot_type" == 'uefi' ]]; then
|
|
|
|
copy_ovmf_vars
|
|
|
|
if [[ "${secure_boot}" == 'on' ]]; then
|
|
|
|
printf '%s\n' 'Using Secure Boot'
|
|
|
|
local ovmf_code='/usr/share/edk2-ovmf/x64/OVMF_CODE.secboot.fd'
|
|
|
|
else
|
|
|
|
local ovmf_code='/usr/share/edk2-ovmf/x64/OVMF_CODE.fd'
|
|
|
|
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
|
2020-07-02 10:11:49 +02:00
|
|
|
|
|
|
|
qemu-system-x86_64 \
|
|
|
|
-boot order=d,menu=on,reboot-timeout=5000 \
|
2020-08-17 19:26:26 +02:00
|
|
|
-m "size=3072,slots=0,maxmem=$((3072*1024*1024))" \
|
2020-07-02 10:11:49 +02:00
|
|
|
-k en \
|
|
|
|
-name archiso,process=archiso_0 \
|
2020-08-17 19:33:50 +02:00
|
|
|
-device virtio-scsi-pci,id=scsi0 \
|
|
|
|
-device "scsi-${mediatype%rom},bus=scsi0.0,drive=${mediatype}0" \
|
|
|
|
-drive "id=${mediatype}0,if=none,format=raw,media=${mediatype/hd/disk},readonly=on,file=${image}" \
|
2020-07-02 10:11:49 +02:00
|
|
|
-display sdl \
|
|
|
|
-vga virtio \
|
2020-08-17 19:31:54 +02:00
|
|
|
-device virtio-net-pci,romfile=,netdev=net0 -netdev user,id=net0 \
|
2020-07-02 10:11:49 +02:00
|
|
|
-machine type=q35,smm=on,accel=kvm \
|
|
|
|
-global ICH9-LPC.disable_s3=1 \
|
|
|
|
-enable-kvm \
|
2020-08-17 19:26:26 +02:00
|
|
|
"${qemu_options[@]}" \
|
2020-07-02 10:11:49 +02:00
|
|
|
-no-reboot
|
|
|
|
}
|
|
|
|
|
|
|
|
set_image() {
|
2020-08-17 19:26:26 +02:00
|
|
|
if [[ -z "$image" ]]; then
|
|
|
|
printf 'ERROR: %s\n' "Image name can not be empty."
|
2020-07-02 10:11:49 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
2020-08-17 19:26:26 +02:00
|
|
|
if [[ ! -f "$image" ]]; then
|
|
|
|
printf 'ERROR: %s\n' "Image (${image}) does not exist."
|
2020-07-02 10:11:49 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
image="$1"
|
|
|
|
}
|
|
|
|
|
2020-08-17 19:26:26 +02:00
|
|
|
image=''
|
|
|
|
boot_type='bios'
|
2020-08-17 19:33:50 +02:00
|
|
|
mediatype='cdrom'
|
2020-08-17 19:26:26 +02:00
|
|
|
secure_boot='off'
|
|
|
|
qemu_options=()
|
|
|
|
working_dir="$(mktemp -dt run_archiso.XXXXXXXXXX)"
|
2020-07-02 10:11:49 +02:00
|
|
|
trap cleanup_working_dir EXIT
|
|
|
|
|
2020-08-17 19:26:26 +02:00
|
|
|
if (( ${#@} > 0 )); then
|
2020-08-17 19:33:50 +02:00
|
|
|
while getopts 'bdhi:su' flag; do
|
2020-08-17 19:26:26 +02:00
|
|
|
case "$flag" in
|
2020-07-02 10:11:49 +02:00
|
|
|
b)
|
2020-08-17 19:26:26 +02:00
|
|
|
boot_type='bios'
|
2020-07-02 10:11:49 +02:00
|
|
|
;;
|
2020-08-17 19:33:50 +02:00
|
|
|
d)
|
|
|
|
mediatype='hd'
|
|
|
|
;;
|
2020-07-02 10:11:49 +02:00
|
|
|
h)
|
|
|
|
print_help
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
i)
|
|
|
|
image="$OPTARG"
|
|
|
|
;;
|
|
|
|
u)
|
2020-08-17 19:26:26 +02:00
|
|
|
boot_type='uefi'
|
2020-07-02 10:11:49 +02:00
|
|
|
;;
|
|
|
|
s)
|
2020-08-17 19:26:26 +02:00
|
|
|
secure_boot='on'
|
2020-07-02 10:11:49 +02:00
|
|
|
;;
|
|
|
|
*)
|
2020-08-17 19:26:26 +02:00
|
|
|
printf '%s\n' "Error: Wrong option. Try 'run_archiso -h'."
|
2020-07-02 10:11:49 +02:00
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
else
|
|
|
|
print_help
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
check_image
|
|
|
|
run_image
|