OSDN Git Service

fixed scr saver
[alterlinux/LFBS.git] / 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         -device virtio-net-pci,netdev=net0 -netdev user,id=net0 \
71         -enable-kvm \
72         -no-reboot
73 }
74
75 run_image_using_uefi() {
76     local ovmf_code=/usr/share/edk2-ovmf/x64/OVMF_CODE.fd
77     local secure_boot_state=off
78     copy_ovmf_vars
79     if [ "${secure_boot}" == "yes" ]; then
80         echo "Using Secure Boot"
81         ovmf_code=/usr/share/edk2-ovmf/x64/OVMF_CODE.secboot.fd
82         secure_boot_state=on
83     fi
84     qemu-system-x86_64 \
85         -boot order=d,menu=on,reboot-timeout=5000 \
86         -m size=3072,slots=0,maxmem=$((3072*1024*1024)) \
87         -k en \
88         -name archiso,process=archiso_0 \
89         -drive file="${image}",media=cdrom,readonly=on,if=virtio \
90         -drive if=pflash,format=raw,unit=0,file="${ovmf_code}",readonly \
91         -drive if=pflash,format=raw,unit=1,file="${working_dir}/OVMF_VARS.fd" \
92         -machine type=q35,smm=on,accel=kvm \
93         -global driver=cfi.pflash01,property=secure,value="${secure_boot_state}" \
94         -global ICH9-LPC.disable_s3=1  \
95         -device virtio-net-pci,netdev=net0 -netdev user,id=net0 \
96         -enable-kvm \
97         -no-reboot
98 }
99
100 set_image() {
101     if [ -z "$image" ]; then
102         echo "ERROR: Image name can not be empty."
103         exit 1
104     fi
105     if [ ! -f "$image" ]; then
106         echo "ERROR: Image ($image) does not exist."
107         exit 1
108     fi
109     image="$1"
110 }
111
112 image=""
113 boot_type="bios"
114 secure_boot="no"
115 working_dir="$(mktemp -d)"
116 trap cleanup_working_dir EXIT
117
118 if [ ${#@} -gt 0 ]; then
119     while getopts 'bhi:su' flag; do
120         case "${flag}" in
121             b)
122                 boot_type=bios
123                 ;;
124             h)
125                 print_help
126                 exit 0
127                 ;;
128             i)
129                 image="$OPTARG"
130                 ;;
131             u)
132                 boot_type=uefi
133                 ;;
134             s)
135                 secure_boot=yes
136                 ;;
137             *)
138                 echo "Error: Wrong option. Try 'run_archiso -h'."
139                 exit 1
140                 ;;
141         esac
142     done
143 else
144     print_help
145     exit 1
146 fi
147
148 check_image
149 run_image