run_archiso.sh 3.54 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/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() {
18
19
    local usagetext
    IFS='' read -r -d '' usagetext <<EOF || true
20
21
22
23
Usage:
    run_archiso [options]

Options:
24
    -b              set boot type to 'BIOS' (default)
25
26
    -h              print help
    -i [image]      image to boot into
27
28
    -s              use Secure Boot (only relevant when using UEFI)
    -u              set boot type to 'UEFI'
29
30
31
32
33

Example:
    Run an image using UEFI:
    $ run_archiso -u -i archiso-2020.05.23-x86_64.iso
EOF
34
    printf '%s' "${usagetext}"
35
36
37
}

cleanup_working_dir() {
38
39
    if [[ -d "${working_dir}" ]]; then
        rm -rf -- "${working_dir}"
40
41
42
43
    fi
}

copy_ovmf_vars() {
44
45
    if [[ ! -f '/usr/share/edk2-ovmf/x64/OVMF_VARS.fd' ]]; then
        printf 'ERROR: %s\n' "OVMF_VARS.fd not found. Install edk2-ovmf."
46
47
        exit 1
    fi
48
    cp -av -- '/usr/share/edk2-ovmf/x64/OVMF_VARS.fd' "${working_dir}/"
49
50
51
}

check_image() {
52
53
    if [[ -z "$image" ]]; then
        printf 'ERROR: %s\n' "Image name can not be empty."
54
55
        exit 1
    fi
56
57
    if [[ ! -f "$image" ]]; then
        printf 'ERROR: %s\n' "Image file (${image}) does not exist."
58
59
60
61
62
        exit 1
    fi
}

run_image() {
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    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
77
78
79

    qemu-system-x86_64 \
        -boot order=d,menu=on,reboot-timeout=5000 \
80
        -m "size=3072,slots=0,maxmem=$((3072*1024*1024))" \
81
82
83
84
85
86
87
88
89
        -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 \
        -machine type=q35,smm=on,accel=kvm \
        -global ICH9-LPC.disable_s3=1 \
        -enable-kvm \
90
        "${qemu_options[@]}" \
91
92
93
94
        -no-reboot
}

set_image() {
95
96
    if [[ -z "$image" ]]; then
        printf 'ERROR: %s\n' "Image name can not be empty."
97
98
        exit 1
    fi
99
100
    if [[ ! -f "$image" ]]; then
        printf 'ERROR: %s\n' "Image (${image}) does not exist."
101
102
103
104
105
        exit 1
    fi
    image="$1"
}

106
107
108
109
110
image=''
boot_type='bios'
secure_boot='off'
qemu_options=()
working_dir="$(mktemp -dt run_archiso.XXXXXXXXXX)"
111
112
trap cleanup_working_dir EXIT

113
if (( ${#@} > 0 )); then
114
    while getopts 'bhi:su' flag; do
115
        case "$flag" in
116
            b)
117
                boot_type='bios'
118
119
120
121
122
123
124
125
126
                ;;
            h)
                print_help
                exit 0
                ;;
            i)
                image="$OPTARG"
                ;;
            u)
127
                boot_type='uefi'
128
129
                ;;
            s)
130
                secure_boot='on'
131
132
                ;;
            *)
133
                printf '%s\n' "Error: Wrong option. Try 'run_archiso -h'."
134
135
136
137
138
139
140
141
142
143
144
                exit 1
                ;;
        esac
    done
else
    print_help
    exit 1
fi

check_image
run_image