OSDN Git Service

Merge branch 'dev' into dev-stable
[alterlinux/alterlinux.git] / tools / run_archiso.sh
1 #!/usr/bin/env bash
2 #
3 # Copyright (C) 2021 David Runge <dvzrv@archlinux.org>
4 #
5 # SPDX-License-Identifier: GPL-3.0-or-later
6 #
7 # A simple script to run an archiso image using qemu. The image can be booted
8 # using BIOS or UEFI.
9 #
10 # Requirements:
11 # - qemu
12 # - edk2-ovmf (when UEFI booting)
13
14
15 set -eu
16
17 print_help() {
18     local usagetext
19     IFS='' read -r -d '' usagetext <<EOF || true
20 Usage:
21     run_archiso [options]
22
23 Options:
24     -b              set boot type to 'BIOS' (default)
25     -d              set image type to hard disk instead of optical disc
26     -h              print help
27     -i [image]      image to boot into
28     -s              use Secure Boot (only relevant when using UEFI)
29     -u              set boot type to 'UEFI'
30
31 Example:
32     Run an image using UEFI:
33     $ run_archiso -u -i archiso-2021.05.23-x86_64.iso
34 EOF
35     printf '%s' "${usagetext}"
36 }
37
38 cleanup_working_dir() {
39     if [[ -d "${working_dir}" ]]; then
40         rm -rf -- "${working_dir}"
41     fi
42 }
43
44 copy_ovmf_vars() {
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."
47         exit 1
48     fi
49     cp -av -- '/usr/share/edk2-ovmf/x64/OVMF_VARS.fd' "${working_dir}/"
50 }
51
52 check_image() {
53     if [[ -z "$image" ]]; then
54         printf 'ERROR: %s\n' "Image name can not be empty."
55         exit 1
56     fi
57     if [[ ! -f "$image" ]]; then
58         printf 'ERROR: %s\n' "Image file (${image}) does not exist."
59         exit 1
60     fi
61 }
62
63 run_image() {
64     if [[ "$boot_type" == 'uefi' ]]; then
65         copy_ovmf_vars
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'
69         else
70             local ovmf_code='/usr/share/edk2-ovmf/x64/OVMF_CODE.fd'
71         fi
72         qemu_options+=(
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}"
76         )
77     fi
78
79     qemu-system-x86_64 \
80         -boot order=d,menu=on,reboot-timeout=5000 \
81         -m "size=3072,slots=0,maxmem=$((3072*1024*1024))" \
82         -k en \
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}" \
87         -display sdl \
88         -vga virtio \
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 \
92         -enable-kvm \
93         "${qemu_options[@]}" \
94         -no-reboot
95 }
96
97 set_image() {
98     if [[ -z "$image" ]]; then
99         printf 'ERROR: %s\n' "Image name can not be empty."
100         exit 1
101     fi
102     if [[ ! -f "$image" ]]; then
103         printf 'ERROR: %s\n' "Image (${image}) does not exist."
104         exit 1
105     fi
106     image="$1"
107 }
108
109 image=''
110 boot_type='bios'
111 mediatype='cdrom'
112 secure_boot='off'
113 qemu_options=()
114 working_dir="$(mktemp -dt run_archiso.XXXXXXXXXX)"
115 trap cleanup_working_dir EXIT
116
117 if (( ${#@} > 0 )); then
118     while getopts 'bdhi:su' flag; do
119         case "$flag" in
120             b)
121                 boot_type='bios'
122                 ;;
123             d)
124                 mediatype='hd'
125                 ;;
126             h)
127                 print_help
128                 exit 0
129                 ;;
130             i)
131                 image="$OPTARG"
132                 ;;
133             u)
134                 boot_type='uefi'
135                 ;;
136             s)
137                 secure_boot='on'
138                 ;;
139             *)
140                 printf '%s\n' "Error: Wrong option. Try 'run_archiso -h'."
141                 exit 1
142                 ;;
143         esac
144     done
145 else
146     print_help
147     exit 1
148 fi
149
150 check_image
151 run_image