-
Notifications
You must be signed in to change notification settings - Fork 7
/
build-efi-image
executable file
·188 lines (153 loc) · 4.21 KB
/
build-efi-image
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/bash
set -e
name="$(basename ${0})"
: ${TOP_DIR:="$( cd "$( dirname "${BASH_SOURCE[0]}" )/" && pwd )"}
usage () {
echo "${name} - Build Petitboot UEFI disk image." >&2
echo "Usage: ${name} [flags]" >&2
echo "Option flags:" >&2
echo " -d --dry-run - Do not run commands." >&2
echo " -f --fresh - Build from scratch." >&2
echo " -g --debug - Debug mode." >&2
echo " -h --help - Show this help and exit." >&2
echo " -v --verbose - Verbose mode." >&2
}
short_opts="dfghv"
long_opts="dry-run,fresh,debug,help,verbose"
opts=$(getopt --options ${short_opts} --long ${long_opts} -n "${name}" -- "$@")
if [[ ! $? ]]; then
echo "${name}: ERROR: Internal error: getopt." >&2
exit 1
fi
eval set -- "${opts}"
while true ; do
case "${1}" in
-f | --fresh)
fresh=1
shift
;;
-d | --dry-run)
dry_run=1
shift
;;
-g | --debug)
set -x
verbose=1
debug=1
shift
;;
-h | --help)
help=1
shift
;;
-v | --verbose)
verbose=1
shift
;;
:)
[[ -z ${debug} ]] || echo "GOT ':': 1=@${1}@ 2=@${2}@"
echo "${name}: ERROR: Got ':'." >&2
exit 1
;;
--)
[[ -z ${debug} ]] || echo "GOT '--': 1=@${1}@ 2=@${2}@"
shift
while [[ -n ${1} ]]; do
[[ -z ${debug} ]] || echo "ADD: 1=@${1}@ 2=@${2}@"
args="${args} ${1}"
shift
done
if [[ -n "${args}" ]]; then
echo "${name}: ERROR: Got extra args '${args}'." >&2
exit 1
fi
break
;;
*)
echo "${name}: ERROR: Internal error: args: '${@}'." >&2
exit 1
;;
esac
done
if [[ -n ${help} ]]; then
usage
exit 0
fi
run_cmd () {
local cmd="${*}"
if [[ -n ${verbose} || -n "${dry_run}" ]]; then
echo "==> ${cmd}"
fi
if [[ -n "${dry_run}" ]]; then
true
else
eval "${cmd}"
fi
}
br_image="/home/geoff/projects/builds/buildroot/arm64/images/Image"
url="https://alpha.release.core-os.net/arm64-usr/current"
efi_code="coreos_production_qemu_uefi_efi_code.fd"
efi_vars="coreos_production_qemu_uefi_efi_vars.fd"
arm64_kernel="coreos_production_pxe.vmlinuz"
arm64_initrd="coreos_production_pxe_image.cpio.gz"
down_load () {
[[ -e ${efi_code} ]] || run_cmd "wget ${url}/${efi_code}"
[[ -e ${efi_vars} ]] || run_cmd "wget ${url}/${efi_vars}"
[[ -e ${arm64_kernel} ]] || run_cmd "wget ${url}/${arm64_kernel}"
[[ -e ${arm64_initrd} ]] || run_cmd "wget ${url}/${arm64_initrd}"
}
work="$(pwd)"
mount="${work}/cow-mount"
disk_image="${work}/pb.qcow2"
kboot_conf="
default=linux-http
linux-http=${url}/${arm64_kernel} initrd=${url}/${arm64_initrd} coreos.autologin
linux-hd=/boot/${arm64_kernel} initrd=/boot/${arm64_initrd} coreos.autologin
"
down_load
[[ -z ${fresh} ]] || run_cmd "rm -f ${disk_image}"
if [[ ! -e ${disk_image} ]]; then
fresh=1
run_cmd "qemu-img create -f qcow2 ${disk_image} 4G"
fi
cleanup () {
echo "unmounting..." >&2
sudo umount ${mount}
sudo qemu-nbd --disconnect /dev/nbd0
}
trap cleanup EXIT
sudo modprobe nbd max_part=1
sudo qemu-nbd --connect=/dev/nbd0 ${disk_image}
[[ -z ${fresh} ]] || run_cmd "sudo mkfs.vfat /dev/nbd0"
mkdir -p ${mount}
sudo mount /dev/nbd0 ${mount} -o rw,uid=$(id -u),gid=$(id -g)
run_cmd "mkdir -p ${mount}/EFI/boot/"
run_cmd "cp ${br_image} ${mount}/EFI/boot/bootaa64.efi"
run_cmd "mkdir -p ${mount}/debug/"
#run_cmd "cp -v /home/geoff/projects/builds/buildroot/arm64/build/petitboot-master/discover/pb-discover ${mount}/debug/"
if [[ -n ${fresh} ]]; then
run_cmd "mkdir -p ${mount}/boot/"
run_cmd "cp -v ${arm64_kernel} ${mount}/boot/"
run_cmd "cp -v ${arm64_initrd} ${mount}/boot/"
run_cmd "echo \"${kboot_conf}\" > ${mount}/boot/kboot.conf"
fi
echo "----------" >&2
echo "Files:" >&2
find ${mount} -type f >&2
echo "----------" >&2
trap - EXIT
cleanup
echo "" >&2
echo "Examples:" >&2
echo "[1] qemu-system-aarch64 -machine virt -cpu cortex-a57 -machine type=virt -m 4096 -nographic \
-drive if=pflash,file=${efi_code},format=raw,readonly \
-drive if=pflash,file=${efi_vars},format=raw \
-netdev user,id=eth0,hostfwd=tcp::2222-:22,hostname=pb_tester \
-device virtio-net-device,netdev=eth0 \
-hda ${disk_image}" >&2
echo "[2] qemu-system-aarch64 -machine virt -cpu cortex-a57 -machine type=virt -m 4096 -nographic \
-netdev user,id=eth0,hostfwd=tcp::2222-:22,hostname=pb_tester \
-device virtio-net-device,netdev=eth0 \
-kernel ${br_image} \
-hda ${disk_image}" >&2
echo "" >&2