summaryrefslogtreecommitdiffstats
path: root/disk/rpi_chroot
blob: aa0109ef7fc43391a1ad1d3c33b9d726151950a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/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