2 # enable automatic i386/ARM/M68K/MIPS/SPARC/PPC/s390 program execution by the kernel
4 qemu_target_list
="i386 i486 alpha arm sparc32plus ppc ppc64 ppc64le m68k \
5 mips mipsel mipsn32 mipsn32el mips64 mips64el \
6 sh4 sh4eb s390x aarch64"
8 i386_magic
='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x03\x00'
9 i386_mask
='\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
12 i486_magic
='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x06\x00'
13 i486_mask
='\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
16 alpha_magic
='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x26\x90'
17 alpha_mask
='\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
20 arm_magic
='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00'
21 arm_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
24 armeb_magic
='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28'
25 armeb_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
28 sparc_magic
='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x02'
29 sparc_mask
='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
32 sparc32plus_magic
='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x12'
33 sparc32plus_mask
='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
34 sparc32plus_family
=sparc
36 ppc_magic
='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x14'
37 ppc_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
40 ppc64_magic
='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15'
41 ppc64_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
44 ppc64le_magic
='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15\x00'
45 ppc64le_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\x00'
48 m68k_magic
='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x04'
49 m68k_mask
='\xff\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
52 # FIXME: We could use the other endianness on a MIPS host.
54 mips_magic
='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08'
55 mips_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
58 mipsel_magic
='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00'
59 mipsel_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
62 mipsn32_magic
='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08'
63 mipsn32_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
66 mipsn32el_magic
='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00'
67 mipsn32el_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
70 mips64_magic
='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08'
71 mips64_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
74 mips64el_magic
='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00'
75 mips64el_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
78 sh4_magic
='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2a\x00'
79 sh4_mask
='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
82 sh4eb_magic
='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2a'
83 sh4eb_mask
='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
86 s390x_magic
='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x16'
87 s390x_mask
='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
90 aarch64_magic
='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00'
91 aarch64_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
95 cpu
=${HOST_ARCH:-$(uname -m)}
97 amd64|i386|i486|i586|i686|i86pc|BePC|x86_64
)
103 "Power Macintosh"|ppc64|powerpc|ppc
)
109 arm|armel|armhf|arm64|armv
[4-9]*)
123 Usage: qemu-binfmt-conf.sh [--qemu-path PATH][--debian][--systemd CPU]
124 [--help][--credential yes|no][--exportdir PATH]
126 Configure binfmt_misc to use qemu interpreter
128 --help: display this usage
129 --qemu-path: set path to qemu interpreter ($QEMU_PATH)
130 --debian: don't write into /proc,
131 instead generate update-binfmts templates
132 --systemd: don't write into /proc,
133 instead generate file for systemd-binfmt.service
135 --exportdir: define where to write configuration files
136 (default: $SYSTEMDDIR or $DEBIANDIR)
137 --credential: if yes, credential and security tokens are
138 calculated according to the binary to interpret
140 To import templates with update-binfmts, use :
142 sudo update-binfmts --importdir ${EXPORTDIR:-$DEBIANDIR} --import qemu-CPU
144 To remove interpreter, use :
146 sudo update-binfmts --package qemu-CPU --remove qemu-CPU $QEMU_PATH
148 With systemd, binfmt files are loaded by systemd-binfmt.service
150 The environment variable HOST_ARCH allows to override 'uname' to generate
151 configuration files for a different architecture than the current one.
160 qemu_check_access
() {
161 if [ ! -w "$1" ] ; then
162 echo "ERROR: cannot write to $1" 1>&2
167 qemu_check_bintfmt_misc
() {
168 # load the binfmt_misc module
169 if [ ! -d /proc
/sys
/fs
/binfmt_misc
]; then
170 if ! /sbin
/modprobe binfmt_misc
; then
174 if [ ! -f /proc
/sys
/fs
/binfmt_misc
/register
]; then
175 if ! mount binfmt_misc
-t binfmt_misc
/proc
/sys
/fs
/binfmt_misc
; then
180 qemu_check_access
/proc
/sys
/fs
/binfmt_misc
/register
184 dpkg
--status "$1" > /dev
/null
2>&1
187 qemu_check_debian
() {
188 if [ ! -e /etc
/debian_version
] ; then
189 echo "WARNING: your system is not a Debian based distro" 1>&2
190 elif ! installed_dpkg binfmt-support
; then
191 echo "WARNING: package binfmt-support is needed" 1>&2
193 qemu_check_access
"$EXPORTDIR"
196 qemu_check_systemd
() {
197 if ! systemctl
-q is-enabled systemd-binfmt.service
; then
198 echo "WARNING: systemd-binfmt.service is missing or disabled" 1>&2
200 qemu_check_access
"$EXPORTDIR"
203 qemu_generate_register
() {
204 echo ":qemu-$cpu:M::$magic:$mask:$qemu:$FLAGS"
207 qemu_register_interpreter
() {
208 echo "Setting $qemu as binfmt interpreter for $cpu"
209 qemu_generate_register
> /proc
/sys
/fs
/binfmt_misc
/register
212 qemu_generate_systemd
() {
213 echo "Setting $qemu as binfmt interpreter for $cpu for systemd-binfmt.service"
214 qemu_generate_register
> "$EXPORTDIR/qemu-$cpu.conf"
217 qemu_generate_debian
() {
218 cat > "$EXPORTDIR/qemu-$cpu" <<EOF
224 if [ "$FLAGS" = "OC" ] ; then
225 echo "credentials yes" >> "$EXPORTDIR/qemu-$cpu"
231 host_family
=$
(qemu_get_family
)
233 # register the interpreter for each cpu except for the native one
235 for cpu
in ${qemu_target_list} ; do
236 magic
=$
(eval echo \$
${cpu}_magic
)
237 mask
=$
(eval echo \$
${cpu}_mask
)
238 family
=$
(eval echo \$
${cpu}_family
)
240 if [ "$magic" = "" ] ||
[ "$mask" = "" ] ||
[ "$family" = "" ] ; then
241 echo "INTERNAL ERROR: unknown cpu $cpu" 1>&2
245 qemu
="$QEMU_PATH/qemu-$cpu"
246 if [ "$cpu" = "i486" ] ; then
247 qemu
="$QEMU_PATH/qemu-i386"
250 if [ "$host_family" != "$family" ] ; then
256 CHECK
=qemu_check_bintfmt_misc
257 BINFMT_SET
=qemu_register_interpreter
259 SYSTEMDDIR
="/etc/binfmt.d"
260 DEBIANDIR
="/usr/share/binfmts"
262 QEMU_PATH
=/usr
/local
/bin
265 options
=$
(getopt
-o ds
:Q
:e
:hc
: -l debian
,systemd
:,qemu-path
:,exportdir
:,help,credential
: -- "$@")
266 eval set -- "$options"
271 CHECK
=qemu_check_debian
272 BINFMT_SET
=qemu_generate_debian
273 EXPORTDIR
=${EXPORTDIR:-$DEBIANDIR}
276 CHECK
=qemu_check_systemd
277 BINFMT_SET
=qemu_generate_systemd
278 EXPORTDIR
=${EXPORTDIR:-$SYSTEMDDIR}
280 # check given cpu is in the supported CPU list
281 for cpu
in ${qemu_target_list} ; do
282 if [ "$cpu" == "$1" ] ; then
287 if [ "$cpu" == "$1" ] ; then
288 qemu_target_list
="$1"
290 echo "ERROR: unknown CPU \"$1\"" 1>&2
309 if [ "$1" = "yes" ] ; then