OSDN Git Service

[fix] : Use = and Not use ==
[alterlinux/alterlinux.git] / tools / run_archiso.sh
1 #!/usr/bin/env bash
2 #
3 # Copyright (C) 2020 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     cat << EOF
19 Usage:
20     run_archiso [options]
21 Options:
22     -b              set boot type to 'bios' (default)
23     -h              print help
24     -i [image]      image to boot into
25     -s              use secure boot (only relevant when using UEFI)
26     -u              set boot type to 'uefi'
27 Example:
28     Run an image using UEFI:
29     $ run_archiso -u -i archiso-2020.05.23-x86_64.iso
30 EOF
31 }
32
33 cleanup_working_dir() {
34     if [ -d "${working_dir}" ]; then
35         rm -rf "${working_dir}"
36     fi
37 }
38
39 copy_ovmf_vars() {
40     if [ ! -f /usr/share/edk2-ovmf/x64/OVMF_VARS.fd ]; then
41         echo "ERROR: OVMF_VARS.fd not found. Install edk2-ovmf."
42         exit 1
43     fi
44     cp -av /usr/share/edk2-ovmf/x64/OVMF_VARS.fd "${working_dir}"
45 }
46
47 check_image() {
48     if [ -z "$image" ]; then
49         echo "ERROR: Image name can not be empty."
50         exit 1
51     fi
52     if [ ! -f "$image" ]; then
53         echo "ERROR: Image file ($image) does not exist."
54         exit 1
55     fi
56 }
57
58 run_image() {
59     [ "$boot_type" = "bios" ] && run_image_using_bios
60     [ "$boot_type" = "uefi" ] && run_image_using_uefi
61 }
62
63 run_image_using_bios() {
64     qemu-system-x86_64 \
65         -boot order=d,menu=on,reboot-timeout=5000 \
66         -m size=3072,slots=0,maxmem=$((3072*1024*1024)) \
67         -k en \
68         -name archiso,process=archiso_0 \
69         -drive file="${image}",media=cdrom,readonly=on,if=virtio \
70         -display sdl \
71         -vga virtio \
72         -device virtio-net-pci,netdev=net0 -netdev user,id=net0 \
73         -enable-kvm \
74         -no-reboot
75 }
76
77 run_image_using_uefi() {
78     local ovmf_code=/usr/share/edk2-ovmf/x64/OVMF_CODE.fd
79     local secure_boot_state=off
80     copy_ovmf_vars
81     if [ "${secure_boot}" == "yes" ]; then
82         echo "Using Secure Boot"
83         ovmf_code=/usr/share/edk2-ovmf/x64/OVMF_CODE.secboot.fd
84         secure_boot_state=on
85     fi
86     qemu-system-x86_64 \
87         -boot order=d,menu=on,reboot-timeout=5000 \
88         -m size=3072,slots=0,maxmem=$((3072*1024*1024)) \
89         -k en \
90         -name archiso,process=archiso_0 \
91         -drive file="${image}",media=cdrom,readonly=on,if=virtio \
92         -drive if=pflash,format=raw,unit=0,file="${ovmf_code}",readonly \
93         -drive if=pflash,format=raw,unit=1,file="${working_dir}/OVMF_VARS.fd" \
94         -machine type=q35,smm=on,accel=kvm \
95         -global driver=cfi.pflash01,property=secure,value="${secure_boot_state}" \
96         -global ICH9-LPC.disable_s3=1 \
97         -display sdl \
98         -vga virtio \
99         -device virtio-net-pci,netdev=net0 -netdev user,id=net0 \
100         -enable-kvm \
101         -no-reboot
102 }
103
104 set_image() {
105     if [ -z "$image" ]; then
106         echo "ERROR: Image name can not be empty."
107         exit 1
108     fi
109     if [ ! -f "$image" ]; then
110         echo "ERROR: Image ($image) does not exist."
111         exit 1
112     fi
113     image="$1"
114 }
115
116 image=""
117 boot_type="bios"
118 secure_boot="no"
119 working_dir="$(mktemp -d)"
120 trap cleanup_working_dir EXIT
121
122 if [ ${#@} -gt 0 ]; then
123     while getopts 'bhi:su' flag; do
124         case "${flag}" in
125             b)
126                 boot_type=bios
127                 ;;
128             h)
129                 print_help
130                 exit 0
131                 ;;
132             i)
133                 image="$OPTARG"
134                 ;;
135             u)
136                 boot_type=uefi
137                 ;;
138             s)
139                 secure_boot=yes
140                 ;;
141             *)
142                 echo "Error: Wrong option. Try 'run_archiso -h'."
143                 exit 1
144                 ;;
145         esac
146     done
147 else
148     print_help
149     exit 1
150 fi
151
152 check_image
153 run_image