scripts

misc scripts and tools
git clone git://git.2f30.org/scripts
Log | Files | Refs

qemu-arm-chroot (1752B)


      1 #!/bin/sh
      2 
      3 debian_arch="armel"
      4 debian_version="jessie"
      5 chroot_shell="bash"
      6 
      7 qemu_user_path="/usr/bin/qemu-arm-static"
      8 binfmt_name="armel"
      9 binfmt_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00'
     10 binfmt_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
     11 
     12 usage() {
     13     echo "Usage: $(basename "$0") [build] chroot_directory"
     14     exit 1
     15 }
     16 
     17 is_mounted() {
     18     mount | grep "$1" > /dev/null 2>&1
     19 }
     20 
     21 [ $# -lt 1 -o $# -gt 2 ] && usage
     22 [ $# -eq 2 -a ! "$1" = "build" ] && usage
     23 
     24 [ -d /proc/sys/fs/binfmt_misc ] || modprobe binfmt_misc
     25 [ -f /proc/sys/fs/binfmt_misc/register ] || mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc
     26 if [ -f "/proc/sys/fs/binfmt_misc/${binfmt_name}" ]; then
     27     qemu_user_path="$(grep interpreter "/proc/sys/fs/binfmt_misc/${binfmt_name}" | awk '{print $2}')"
     28 else
     29     echo ":${binfmt_name}:M::${binfmt_magic}:${binfmt_mask}:${qemu_user_path}:" > /proc/sys/fs/binfmt_misc/register
     30 fi
     31 
     32 if [ $# -eq 2 -a "$1" = "build" ]; then
     33     chroot_path="$2"
     34     mkdir -p "${chroot_path}$(dirname "${qemu_user_path}")" || exit 1
     35     cp "${qemu_user_path}" "${chroot_path}$(dirname "${qemu_user_path}")" || exit 1
     36     debootstrap --arch $debian_arch $debian_version "$chroot_path" || exit 1
     37     exit 0
     38 fi
     39 
     40 chroot_path="$1"
     41 is_mounted "${chroot_path}/proc" || mount -t proc proc "${chroot_path}/proc"
     42 is_mounted "${chroot_path}/sys" || mount -t sysfs sys "${chroot_path}/sys"
     43 is_mounted "${chroot_path}/dev" || mount -t devtmpfs dev "${chroot_path}/dev"
     44 is_mounted "${chroot_path}/dev/pts" || mount -t devpts pts "${chroot_path}/dev/pts"
     45 cp -L /etc/resolv.conf "${chroot_path}/etc/resolv.conf"
     46 
     47 chroot "${chroot_path}" /bin/sh -c ". /etc/profile; cd ~; ${chroot_shell}"