qemu-binfmt-conf.sh 10 KB

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