qemu-binfmt-conf.sh 10.0 KB

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