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-10-29 10:52:03 +01:00
|
|
|
-a set accessibility support using brltty
|
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-12-29 20:59:13 +01:00
|
|
|
-v use VNC display (instead of default SDL)
|
2020-12-30 04:02:34 +01:00
|
|
|
-c [image] attach an additional optical disc image (e.g. for cloud-init)
|
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+=(
|
2021-05-06 01:40:17 +02:00
|
|
|
'-drive' "if=pflash,format=raw,unit=0,file=${ovmf_code},read-only=on"
|
2020-08-17 19:26:26 +02:00
|
|
|
'-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
|
|
|
|
2020-10-29 10:52:03 +01:00
|
|
|
if [[ "${accessibility}" == 'on' ]]; then
|
|
|
|
qemu_options+=(
|
|
|
|
'-chardev' 'braille,id=brltty'
|
|
|
|
'-device' 'usb-braille,id=usbbrl,chardev=brltty'
|
|
|
|
)
|
|
|
|
fi
|
|
|
|
|
2020-12-30 04:02:34 +01:00
|
|
|
if [[ -n "${oddimage}" ]]; then
|
|
|
|
qemu_options+=(
|
|
|
|
'-device' 'scsi-cd,bus=scsi0.0,drive=cdrom1'
|
2021-05-06 01:40:17 +02:00
|
|
|
'-drive' "id=cdrom1,if=none,format=raw,media=cdrom,read-only=on,file=${oddimage}"
|
2020-12-30 04:02:34 +01:00
|
|
|
)
|
|
|
|
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-12-29 20:59:13 +01:00
|
|
|
-k en-us \
|
2020-07-02 10:11:49 +02:00
|
|
|
-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" \
|
2021-05-06 01:40:17 +02:00
|
|
|
-drive "id=${mediatype}0,if=none,format=raw,media=${mediatype/hd/disk},read-only=on,file=${image}" \
|
2020-12-29 20:59:13 +01:00
|
|
|
-display "${display}" \
|
2020-07-02 10:11:49 +02:00
|
|
|
-vga virtio \
|
2020-10-07 17:11:13 +02:00
|
|
|
-audiodev pa,id=snd0 \
|
|
|
|
-device ich9-intel-hda \
|
|
|
|
-device hda-output,audiodev=snd0 \
|
2020-12-29 23:27:30 +01:00
|
|
|
-device virtio-net-pci,romfile=,netdev=net0 -netdev user,id=net0,hostfwd=tcp::60022-:22 \
|
2020-10-24 23:11:22 +02:00
|
|
|
-machine type=q35,smm=on,accel=kvm,usb=on,pcspk-audiodev=snd0 \
|
2020-07-02 10:11:49 +02:00
|
|
|
-global ICH9-LPC.disable_s3=1 \
|
|
|
|
-enable-kvm \
|
2020-08-17 19:26:26 +02:00
|
|
|
"${qemu_options[@]}" \
|
2020-10-29 10:52:03 +01:00
|
|
|
-serial stdio \
|
|
|
|
-no-reboot
|
2020-07-02 10:11:49 +02:00
|
|
|
}
|
|
|
|
|
2020-08-17 19:26:26 +02:00
|
|
|
image=''
|
2020-12-30 04:02:34 +01:00
|
|
|
oddimage=''
|
2020-10-29 10:52:03 +01:00
|
|
|
accessibility=''
|
2020-08-17 19:26:26 +02:00
|
|
|
boot_type='bios'
|
2020-08-17 19:33:50 +02:00
|
|
|
mediatype='cdrom'
|
2020-08-17 19:26:26 +02:00
|
|
|
secure_boot='off'
|
2020-12-29 20:59:13 +01:00
|
|
|
display='sdl'
|
2020-08-17 19:26:26 +02:00
|
|
|
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-12-30 04:02:34 +01:00
|
|
|
while getopts 'abc:dhi:suv' flag; do
|
2020-08-17 19:26:26 +02:00
|
|
|
case "$flag" in
|
2020-10-29 10:52:03 +01:00
|
|
|
a)
|
|
|
|
accessibility='on'
|
|
|
|
;;
|
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-12-30 04:02:34 +01:00
|
|
|
c)
|
|
|
|
oddimage="$OPTARG"
|
|
|
|
;;
|
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-12-29 20:59:13 +01:00
|
|
|
v)
|
|
|
|
display='none'
|
|
|
|
qemu_options+=(-vnc 'vnc=0.0.0.0:0,vnc=[::]:0')
|
|
|
|
;;
|
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
|