k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/third_party/multiarch/qemu-user-static/register/qemu-binfmt-conf.sh (about)

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