2
0

qemu-binfmt-conf.sh 13 KB

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