summaryrefslogtreecommitdiff
path: root/scripts/poky-chroot-run
blob: f1f4dec6a4087cafcbd7313f47ed23046c765dec (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
#!/bin/bash
#
# Runs a command within a Poky chroot
#

XEPHYR=`which Xephyr`
if [ ! -n "$XEPHYR" -o ! -x "$XEPHYR" ]; then
    echo "You need to install Xephyr to use $0"
    exit 1
fi

CHROOTUID=`which chrootuid`
if [ ! -n "$CHROOTUID" -o ! -x "$CHROOTUID" ]; then
    echo "You need to install Xephyr to use $0"
    exit 1
fi


case $# in
    0)
    echo "Invalid arguments."
    echo "$ $0 <target> [command]"
    exit 1
    ;;
    1)
    ROOTFS=$1
    shift
    # Set $1 to be the boot script
    set -- /usr/bin/poky-chroot-launch
    ;;
    *)
    ROOTFS=$1
    shift
    # Now $1 onwards are the command and arguments to run
    ;;
esac

test -f "$ROOTFS/.pokychroot" || { echo "$ROOTFS is not setup for use as a Poky chroot." ; exit 1 ;}

set -e

# chrootuid doesn't handle relative paths, so ensure that the rootfs path is
# absolute
if test ${ROOTFS:0:1} != /; then
    ROOTFS="$(pwd)/$ROOTFS"
fi

safe_mount() {
    if ! mountpoint -q "$ROOTFS/$1"; then
        sudo mount --bind $1 "$ROOTFS/$1"
    fi
}
safe_umount() {
    if mountpoint -q "$ROOTFS/$1"; then
        sudo umount "$ROOTFS/$1"
    fi
}

# Mount the directories we need
for m in /dev /dev/pts /dev/shm /proc /sys /tmp; do
    safe_mount $m
done

# Set up the environment
export PATH=/bin:/usr/bin:/sbin:/usr/sbin
export HOME=/home/$USER

if [ ! -f "$ROOTFS/.pokychroot.init" ]; then
    sudo $CHROOTUID -i "$ROOTFS" $USER /bin/sh -c "/usr/bin/poky-chroot-init"
    touch "$ROOTFS/.pokychroot.init"
fi

$XEPHYR :1 -ac -screen 640x480x16 &

# Go go go!
sudo $CHROOTUID -i "$ROOTFS" $USER "$@" || /bin/true

# Trap term signals so we don't kill ourselves
trap true TERM
# send term signal to the process group
kill -- -$$

# Unmount TODO: only umount if there are no other sessions active, somehow.
for m in /tmp /sys /proc /dev/shm /dev/pts /dev; do
    safe_umount $m
done