#!/bin/bash # # Chroot to an RPI image or device (sdcard) # # usage: rpi_mount image # # Copyright (c) 2012 John Lane # # MIT License # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # # http://www.opensource.org/licenses/mit-license.php if [[ $(id -u ) != 0 ]]; then echo "Must be root" else if [[ -b $1 ]]; then # mount a device [[ ${1:5:6} == mmcblk ]] && partition_prefix="p" boot_partition=${1}${partition_prefix}1 root_partition=${1}${partition_prefix}2 elif [[ -f $1 ]]; then if [[ "$(file -b myimage | awk -F ';' '{print $1}')" == "x86 boot sector" ]]; then # loop mount an image lsmod | grep loop > /dev/null || modprobe loop loop_device=$(losetup -f) losetup -P $loop_device $1 boot_partition=${loop_device}p1 root_partition=${loop_device}p2 fi elif [[ -d $1 ]]; then # directory chroot_dir=$1 else echo "Don't know how to handle $1. Sorry" exit -1 fi if [[ ! -z "${boot_partition}" ]]; then temp_mountpoint=$(mktemp -d) mount $root_partition $temp_mountpoint mkdir -p $temp_mountpoint/boot mount $boot_partition $temp_mountpoint/boot chroot_dir=$temp_mountpoint fi # Allow transparent emulation with QEMU for ARM chroot if [[ (-x /usr/bin/qemu-arm-static) && ($(file $chroot_dir/sbin/init | awk -F ', ' '{print $2}') == ARM) && ($(uname -m) != ARM) ]]; then cp /usr/bin/qemu-arm-static $chroot_dir/usr/bin # mount binfmt_misc proc space if not already mounted mount | grep /proc/sys/fs/binfmt_misc > /dev/null || mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc # register arm format if not already registered [[ -f /proc/sys/fs/binfmt_misc/arm ]] || echo ':arm:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/bin/qemu-arm-static:' > /proc/sys/fs/binfmt_misc/register fi # copy in terminfo ti=$(find /usr/share/terminfo -name $TERM -type f) [[ ! -z $ti ]] && cp $ti $chroot_dir/$ti mkdir -p $chroot_dir/{proc,dev/pts} mount proc -t proc $chroot_dir/proc mount devpts -t devpts $chroot_dir/dev/pts echo "Entering chroot $chroot_dir. Type 'exit' when done..." #chroot $chroot_dir /usr/bin/env TERM="vt100" PS1='FooFoo \u' /bin/bash --noprofile --norc chroot $chroot_dir /usr/bin/env -i TERM="$TERM" /bin/bash --login umount $chroot_dir/dev/pts $chroot_dir/proc if [[ ! -z $temp_mountpoint ]]; then umount $temp_mountpoint/boot $temp_mountpoint rmdir $temp_mountpoint fi fi