summarylogtreecommitdiffstats
path: root/enter-chroot.sh
blob: 9edbbffee7f62e70e5b85e59487098c515329f21 (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
#!/bin/sh
if [ $USER != 'root' ]; then
  echo "This script needs root access"
  exit 1
fi

# Mount resources
MOUNT_POINT=/usr/share/alpine-minirootfs

if ! grep -qs "$MOUNT_POINT/sys" /proc/mounts
then mount --rbind /sys $MOUNT_POINT/sys   || exit 1
fi

if ! grep -qs "$MOUNT_POINT/dev" /proc/mounts
then
    mount --rbind /dev $MOUNT_POINT/dev   || exit 1
    mount --make-rslave $MOUNT_POINT/dev  || exit 1
fi

if ! grep -qs "$MOUNT_POINT/proc" /proc/mounts
then mount -t proc /proc $MOUNT_POINT/proc || exit 1
fi

# chroot exit signal pipe
if [ ! -e $MOUNT_POINT/exit.fifo ]
then mkfifo $MOUNT_POINT/exit.fifo
fi

# Function to monitor the named pipe for the exit signal from the internal script
monitor_chroot_exit() {
    while read -r line
    do
        if [ "$line" == "exit" ]
        then break
        fi
    done < $MOUNT_POINT/exit.fifo

    if grep -qs "$MOUNT_POINT/sys" /proc/mounts
    then umount -l $MOUNT_POINT/sys
    fi

    if grep -qs "$MOUNT_POINT/dev" /proc/mounts
    then umount -l $MOUNT_POINT/dev
    fi

    if grep -qs "$MOUNT_POINT/proc" /proc/mounts
    then umount -l $MOUNT_POINT/proc
    fi

    # Send a SIGTERM signal to the parent process (which is running the chroot command)
    kill -s TERM $$
}

# Start the monitoring function in the background
monitor_chroot_exit &

# Enter the chroot environment in the foreground
export PATH=$PATH:/sbin:/usr/sbin:/bin:/usr/bin
chroot $MOUNT_POINT /bin/ash