This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 218
/
chroot.sh
executable file
·67 lines (59 loc) · 2.1 KB
/
chroot.sh
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
#!/bin/bash
IMG_FILE=$1
RUN_SCRIPT=$2
SRC_FILES=$(cd $(dirname $0) ; pwd -P)
ROOTFS_DIR=${ROOTFS_DIR:-rootfs}
CHROOT_CMD=/bin/bash
if [ "$RUN_SCRIPT" != "" ]; then
if [ ! -f "$SRC_FILES/$RUN_SCRIPT" ]; then
echo "** ERROR: Can't find script file '$SRC_FILES/$RUN_SCRIPT' **"
exit 1
fi
CHROOT_CMD+=" -c ./tmp/${RUN_SCRIPT}"
fi
if [ ! -f "$IMG_FILE" ]; then
echo "** ERROR: No musicbox image found **"
exit 1
fi
sudo echo "Info: Checking have root access to mount the disk images."
echo "Mounting $IMG_FILE and preparing arm chroot..."
LOOP_DEV=$(sudo losetup -fP --show $IMG_FILE)
mkdir -p ${ROOTFS_DIR}
sudo mount ${LOOP_DEV}p2 ${ROOTFS_DIR}
sudo mount ${LOOP_DEV}p1 ${ROOTFS_DIR}/boot
CHROOT_MOUNTS="dev proc sys dev/pts"
for x in $CHROOT_MOUNTS
do sudo mount --bind /$x ${ROOTFS_DIR}/$x/
done
if [ -d "$SRC_FILES" ]; then
sudo mount --bind $SRC_FILES $ROOTFS_DIR/tmp
fi
# Disable preloaded shared library to get everything including networking to work on x86
sudo mv ${ROOTFS_DIR}/etc/ld.so.preload ${ROOTFS_DIR}/etc/ld.so.preload.bak
sudo cp $(which qemu-arm-static) ${ROOTFS_DIR}/usr/bin/
if [ -n "$APT_PROXY" ]; then
echo "Using APT proxy $APT_PROXY"
echo "Acquire::http { Proxy \"http://$APT_PROXY\"; };" | \
sudo tee ${ROOTFS_DIR}/etc/apt/apt.conf.d/01proxy
fi
# Prevent upgraded services from trying to start inside chroot.
echo "exit 101" | sudo tee ${ROOTFS_DIR}/usr/sbin/policy-rc.d
sudo chmod +x ${ROOTFS_DIR}/usr/sbin/policy-rc.d
function finish {
echo "Cleaning up chroot and unmounting..."
sudo mv ${ROOTFS_DIR}/etc/ld.so.preload.bak ${ROOTFS_DIR}/etc/ld.so.preload
sudo rm ${ROOTFS_DIR}/usr/bin/qemu-arm-static
sudo rm -f ${ROOTFS_DIR}/etc/apt/apt.conf.d/01proxy
sudo rm -f ${ROOTFS_DIR}/usr/sbin/policy-rc.d
CHROOT_MOUNTS=$(mount | grep "${ROOTFS_DIR}" | cut -f 3 -d ' ' | sort -r)
for m in $CHROOT_MOUNTS
do
echo "Unmounting $m"
sudo umount $m
done
sudo losetup -D $IMG_FILE
rm -rf ${ROOTFS_DIR}
}
trap finish EXIT
echo "Executing 'chroot ${ROOTFS_DIR} ${CHROOT_CMD}'"
sudo chroot ${ROOTFS_DIR} ${CHROOT_CMD}