aboutsummaryrefslogtreecommitdiffstats
path: root/on_chroot.sh
blob: 3d52bb8d7ef0da983e86dbd9d669112e473b4a4e (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
#!/bin/bash

# Example:
#
# 1) sudo mount /data/diskimages/rootfs-arm64.img /mnt/tst
# 2) ./on_chroot.sh /mnt/tst "cd /usr/local/projects/zafena/direct_bt ; sh scripts/build.sh"
# 3) sudo ./imagetool.sh -u /mnt/tst
#

# set -x

username=${USER}

BASE_DIR="$(dirname $0)"

export ROOTFS_DIR=$1
shift 1

if [ -z "${ROOTFS_DIR}" ]; then
    echo Usage "$0 <rootfs-dir> commands..."
    exit 1
fi

if ! mount | grep -q "$(realpath "${ROOTFS_DIR}"/proc)"; then
    sudo mount -t proc proc "${ROOTFS_DIR}/proc"
fi

if ! mount | grep -q "$(realpath "${ROOTFS_DIR}"/dev)"; then
    sudo mount --bind /dev "${ROOTFS_DIR}/dev"
fi

if ! mount | grep -q "$(realpath "${ROOTFS_DIR}"/dev/pts)"; then
    sudo mount --bind /dev/pts "${ROOTFS_DIR}/dev/pts"
fi

if ! mount | grep -q "$(realpath "${ROOTFS_DIR}"/sys)"; then
    sudo mount --bind /sys "${ROOTFS_DIR}/sys"
fi

#if ! mount | grep -q "$(realpath "${ROOTFS_DIR}"/usr/local/projects)"; then
#    mkdir -p "${ROOTFS_DIR}/usr/local/projects"
#    sudo mount --bind /usr/local/projects "${ROOTFS_DIR}/usr/local/projects"
#fi

sudo /sbin/capsh --user=$username --drop=cap_setfcap "--chroot=${ROOTFS_DIR}/" -- -c "$*"