]>
Commit | Line | Data |
---|---|---|
08785f48 | 1 | #!/bin/sh |
e4d966cc LV |
2 | # enable automatic i386/ARM/M68K/MIPS/SPARC/PPC/s390/HPPA |
3 | # program execution by the kernel | |
08785f48 | 4 | |
3bef0451 LV |
5 | qemu_target_list="i386 i486 alpha arm sparc32plus ppc ppc64 ppc64le m68k \ |
6 | mips mipsel mipsn32 mipsn32el mips64 mips64el \ | |
f772f212 | 7 | sh4 sh4eb s390x aarch64 aarch64_be hppa" |
3bef0451 LV |
8 | |
9 | i386_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x03\x00' | |
10 | i386_mask='\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff' | |
11 | i386_family=i386 | |
12 | ||
13 | i486_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x06\x00' | |
14 | i486_mask='\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff' | |
15 | i486_family=i386 | |
16 | ||
17 | alpha_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x26\x90' | |
18 | alpha_mask='\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff' | |
19 | alpha_family=alpha | |
20 | ||
21 | arm_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00' | |
22 | arm_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff' | |
23 | arm_family=arm | |
24 | ||
25 | armeb_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28' | |
26 | armeb_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff' | |
27 | armeb_family=arm | |
28 | ||
29 | sparc_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x02' | |
30 | sparc_mask='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff' | |
31 | sparc_family=sparc | |
32 | ||
33 | sparc32plus_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x12' | |
34 | sparc32plus_mask='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff' | |
35 | sparc32plus_family=sparc | |
36 | ||
37 | ppc_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x14' | |
38 | ppc_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff' | |
39 | ppc_family=ppc | |
40 | ||
41 | ppc64_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15' | |
42 | ppc64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff' | |
43 | ppc64_family=ppc | |
44 | ||
45 | ppc64le_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15\x00' | |
46 | ppc64le_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\x00' | |
47 | ppc64le_family=ppcle | |
48 | ||
49 | m68k_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x04' | |
50 | m68k_mask='\xff\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff' | |
51 | m68k_family=m68k | |
52 | ||
53 | # FIXME: We could use the other endianness on a MIPS host. | |
54 | ||
55 | mips_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08' | |
56 | mips_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff' | |
57 | mips_family=mips | |
58 | ||
59 | mipsel_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00' | |
60 | mipsel_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff' | |
61 | mipsel_family=mips | |
62 | ||
63 | mipsn32_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08' | |
64 | mipsn32_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff' | |
65 | mipsn32_family=mips | |
66 | ||
67 | mipsn32el_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00' | |
68 | mipsn32el_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff' | |
69 | mipsn32el_family=mips | |
70 | ||
71 | mips64_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08' | |
72 | mips64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff' | |
73 | mips64_family=mips | |
74 | ||
75 | mips64el_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00' | |
76 | mips64el_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff' | |
77 | mips64el_family=mips | |
78 | ||
79 | sh4_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2a\x00' | |
80 | sh4_mask='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff' | |
81 | sh4_family=sh4 | |
82 | ||
83 | sh4eb_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2a' | |
84 | sh4eb_mask='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff' | |
85 | sh4eb_family=sh4 | |
86 | ||
87 | s390x_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x16' | |
88 | s390x_mask='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff' | |
89 | s390x_family=s390x | |
90 | ||
91 | aarch64_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00' | |
92 | aarch64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff' | |
93 | aarch64_family=arm | |
94 | ||
f772f212 MW |
95 | aarch64_be_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7' |
96 | aarch64_be_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff' | |
97 | aarch64_be_family=arm | |
98 | ||
e4d966cc LV |
99 | hppa_magic='\x7f\x45\x4c\x46\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x0f' |
100 | hppa_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff' | |
101 | hppa_family=hppa | |
102 | ||
3bef0451 LV |
103 | qemu_get_family() { |
104 | cpu=${HOST_ARCH:-$(uname -m)} | |
105 | case "$cpu" in | |
106 | amd64|i386|i486|i586|i686|i86pc|BePC|x86_64) | |
107 | echo "i386" | |
108 | ;; | |
109 | mips*) | |
110 | echo "mips" | |
111 | ;; | |
112 | "Power Macintosh"|ppc64|powerpc|ppc) | |
113 | echo "ppc" | |
114 | ;; | |
115 | ppc64el|ppc64le) | |
116 | echo "ppcle" | |
117 | ;; | |
118 | arm|armel|armhf|arm64|armv[4-9]*) | |
119 | echo "arm" | |
120 | ;; | |
121 | sparc*) | |
122 | echo "sparc" | |
123 | ;; | |
124 | *) | |
125 | echo "$cpu" | |
126 | ;; | |
127 | esac | |
128 | } | |
129 | ||
130 | usage() { | |
131 | cat <<EOF | |
132 | Usage: qemu-binfmt-conf.sh [--qemu-path PATH][--debian][--systemd CPU] | |
133 | [--help][--credential yes|no][--exportdir PATH] | |
134 | ||
135 | Configure binfmt_misc to use qemu interpreter | |
136 | ||
137 | --help: display this usage | |
138 | --qemu-path: set path to qemu interpreter ($QEMU_PATH) | |
139 | --debian: don't write into /proc, | |
140 | instead generate update-binfmts templates | |
141 | --systemd: don't write into /proc, | |
142 | instead generate file for systemd-binfmt.service | |
143 | for the given CPU | |
144 | --exportdir: define where to write configuration files | |
145 | (default: $SYSTEMDDIR or $DEBIANDIR) | |
146 | --credential: if yes, credential and security tokens are | |
147 | calculated according to the binary to interpret | |
148 | ||
149 | To import templates with update-binfmts, use : | |
150 | ||
151 | sudo update-binfmts --importdir ${EXPORTDIR:-$DEBIANDIR} --import qemu-CPU | |
152 | ||
153 | To remove interpreter, use : | |
154 | ||
155 | sudo update-binfmts --package qemu-CPU --remove qemu-CPU $QEMU_PATH | |
156 | ||
157 | With systemd, binfmt files are loaded by systemd-binfmt.service | |
158 | ||
159 | The environment variable HOST_ARCH allows to override 'uname' to generate | |
160 | configuration files for a different architecture than the current one. | |
161 | ||
162 | where CPU is one of: | |
163 | ||
164 | $qemu_target_list | |
165 | ||
166 | EOF | |
167 | } | |
168 | ||
169 | qemu_check_access() { | |
170 | if [ ! -w "$1" ] ; then | |
171 | echo "ERROR: cannot write to $1" 1>&2 | |
172 | exit 1 | |
173 | fi | |
174 | } | |
175 | ||
176 | qemu_check_bintfmt_misc() { | |
177 | # load the binfmt_misc module | |
178 | if [ ! -d /proc/sys/fs/binfmt_misc ]; then | |
179 | if ! /sbin/modprobe binfmt_misc ; then | |
180 | exit 1 | |
181 | fi | |
182 | fi | |
183 | if [ ! -f /proc/sys/fs/binfmt_misc/register ]; then | |
184 | if ! mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc ; then | |
185 | exit 1 | |
186 | fi | |
187 | fi | |
188 | ||
189 | qemu_check_access /proc/sys/fs/binfmt_misc/register | |
190 | } | |
191 | ||
192 | installed_dpkg() { | |
193 | dpkg --status "$1" > /dev/null 2>&1 | |
194 | } | |
195 | ||
196 | qemu_check_debian() { | |
197 | if [ ! -e /etc/debian_version ] ; then | |
198 | echo "WARNING: your system is not a Debian based distro" 1>&2 | |
199 | elif ! installed_dpkg binfmt-support ; then | |
200 | echo "WARNING: package binfmt-support is needed" 1>&2 | |
201 | fi | |
202 | qemu_check_access "$EXPORTDIR" | |
203 | } | |
204 | ||
205 | qemu_check_systemd() { | |
206 | if ! systemctl -q is-enabled systemd-binfmt.service ; then | |
207 | echo "WARNING: systemd-binfmt.service is missing or disabled" 1>&2 | |
208 | fi | |
209 | qemu_check_access "$EXPORTDIR" | |
210 | } | |
211 | ||
212 | qemu_generate_register() { | |
213 | echo ":qemu-$cpu:M::$magic:$mask:$qemu:$FLAGS" | |
214 | } | |
215 | ||
216 | qemu_register_interpreter() { | |
217 | echo "Setting $qemu as binfmt interpreter for $cpu" | |
218 | qemu_generate_register > /proc/sys/fs/binfmt_misc/register | |
219 | } | |
220 | ||
221 | qemu_generate_systemd() { | |
222 | echo "Setting $qemu as binfmt interpreter for $cpu for systemd-binfmt.service" | |
223 | qemu_generate_register > "$EXPORTDIR/qemu-$cpu.conf" | |
224 | } | |
225 | ||
226 | qemu_generate_debian() { | |
227 | cat > "$EXPORTDIR/qemu-$cpu" <<EOF | |
228 | package qemu-$cpu | |
229 | interpreter $qemu | |
230 | magic $magic | |
231 | mask $mask | |
232 | EOF | |
233 | if [ "$FLAGS" = "OC" ] ; then | |
234 | echo "credentials yes" >> "$EXPORTDIR/qemu-$cpu" | |
235 | fi | |
236 | } | |
237 | ||
238 | qemu_set_binfmts() { | |
239 | # probe cpu type | |
240 | host_family=$(qemu_get_family) | |
241 | ||
242 | # register the interpreter for each cpu except for the native one | |
243 | ||
244 | for cpu in ${qemu_target_list} ; do | |
245 | magic=$(eval echo \$${cpu}_magic) | |
246 | mask=$(eval echo \$${cpu}_mask) | |
247 | family=$(eval echo \$${cpu}_family) | |
248 | ||
249 | if [ "$magic" = "" ] || [ "$mask" = "" ] || [ "$family" = "" ] ; then | |
250 | echo "INTERNAL ERROR: unknown cpu $cpu" 1>&2 | |
251 | continue | |
252 | fi | |
253 | ||
254 | qemu="$QEMU_PATH/qemu-$cpu" | |
255 | if [ "$cpu" = "i486" ] ; then | |
256 | qemu="$QEMU_PATH/qemu-i386" | |
257 | fi | |
258 | ||
259 | if [ "$host_family" != "$family" ] ; then | |
260 | $BINFMT_SET | |
261 | fi | |
262 | done | |
263 | } | |
264 | ||
265 | CHECK=qemu_check_bintfmt_misc | |
266 | BINFMT_SET=qemu_register_interpreter | |
267 | ||
268 | SYSTEMDDIR="/etc/binfmt.d" | |
269 | DEBIANDIR="/usr/share/binfmts" | |
270 | ||
271 | QEMU_PATH=/usr/local/bin | |
272 | FLAGS="" | |
273 | ||
274 | options=$(getopt -o ds:Q:e:hc: -l debian,systemd:,qemu-path:,exportdir:,help,credential: -- "$@") | |
275 | eval set -- "$options" | |
276 | ||
277 | while true ; do | |
278 | case "$1" in | |
279 | -d|--debian) | |
280 | CHECK=qemu_check_debian | |
281 | BINFMT_SET=qemu_generate_debian | |
282 | EXPORTDIR=${EXPORTDIR:-$DEBIANDIR} | |
283 | ;; | |
284 | -s|--systemd) | |
285 | CHECK=qemu_check_systemd | |
286 | BINFMT_SET=qemu_generate_systemd | |
287 | EXPORTDIR=${EXPORTDIR:-$SYSTEMDDIR} | |
288 | shift | |
289 | # check given cpu is in the supported CPU list | |
290 | for cpu in ${qemu_target_list} ; do | |
6f75023a | 291 | if [ "$cpu" = "$1" ] ; then |
3bef0451 LV |
292 | break |
293 | fi | |
294 | done | |
295 | ||
6f75023a | 296 | if [ "$cpu" = "$1" ] ; then |
3bef0451 LV |
297 | qemu_target_list="$1" |
298 | else | |
299 | echo "ERROR: unknown CPU \"$1\"" 1>&2 | |
300 | usage | |
301 | exit 1 | |
302 | fi | |
303 | ;; | |
304 | -Q|--qemu-path) | |
305 | shift | |
306 | QEMU_PATH="$1" | |
307 | ;; | |
308 | -e|--exportdir) | |
309 | shift | |
310 | EXPORTDIR="$1" | |
311 | ;; | |
312 | -h|--help) | |
313 | usage | |
314 | exit 1 | |
315 | ;; | |
316 | -c|--credential) | |
317 | shift | |
318 | if [ "$1" = "yes" ] ; then | |
319 | FLAGS="OC" | |
320 | else | |
321 | FLAGS="" | |
322 | fi | |
323 | ;; | |
324 | *) | |
325 | break | |
326 | ;; | |
327 | esac | |
328 | shift | |
329 | done | |
330 | ||
331 | $CHECK | |
332 | qemu_set_binfmts |