2

Say I chrooted into ~/myroot with its /mnt being bind-mounted from the host's /mnt. Now (from within the chroot environment) I want to e.g. mount /dev/something /mnt/something such that not only the host's ~/myroot/mnt/something but also its /mnt/something contains this mount. How can this be achieved?

Unfortunately, the mount command of busybox doesn't seem to implement --make-shared mentioned here, which should make mount --make-shared --bind /mnt ~/myroot/mnt run on the host make this work (untested though!), so how can this be achieved with busybox?

  • If I understand your question correctly, you can't do this: you can't mount something to a directory outside the chroot from within the chroot. – Gilles 'SO- stop being evil' Nov 12 '13 at 23:41
  • @Gilles Shouldn't a host's mount --make-shared --bind /mnt ~/myroot/mnt work like this? Anyway, it is pretty certainly possible, though my ugly prototype is probably not the best solution... edit I think I see what you meant, I'll improve my question – Tobias Kienzler Nov 13 '13 at 8:45
  • 1
    --make-shared affects mounts done under another mount. It can allow mounts done in a chroot to leak outside the chroot, but only if the area outside the chroot had first been shared with an area within the chroot. – Gilles 'SO- stop being evil' Nov 13 '13 at 9:21
  • @Gilles So that's precisely what I want to achieve. Unfortunately busybox siltently ignores --make-shared and -o shared – Tobias Kienzler Nov 13 '13 at 10:19
1

I think the busybox option you need is -o shared.

| improve this answer | |
  • I'm afraid this has the same effect, namely mount silently ignoring this :( – Tobias Kienzler Nov 13 '13 at 8:33
1

Prototype!

A way to do this would be the following script running on the host:

#!/bin/bash
# host: bind-mount
PREFIX="~/myroot"
MNT="/mnt"

MNT=${MNT#/}
CMD=$PREFIX/$MNT/.mounts.cmd
OUT=$PREFIX/$MNT/.mounts.out
ERR=$OUT
#ERR=$PREFIX/$MNT/.mounts.err


echo "Using prefix $PREFIX"
if ! [ -d $PREFIX/$MNT ]; then
    mkdir -p $PREFIX/$MNT
    echo "Created $PREFIX/mnt"
fi
for i in $CMD $OUT $ERR; do
    if ! [ -e $i ]; then
        mkfifo $i
        echo "Created $i"
    fi
done

trap "exit 0" SIGINT

while true; do
(   # subshell for better output redirection
    line=$(cat $CMD)
    # FIXME there's a problem if this script doesn't react fast
    # enough such that .mounts.cmd contains more than one line...

    # This is a VERY primitive parser of arguments that will fail
    # in many situations, hence PROTOTYPE
    if [ "$line" == "QUIT" ]; then
        exit 254;
    fi
    isopttype=false
    for para in $line; do
        if $isopttype; then
            switches="$switches$para "
            isopttype=false
        else
            case $para in
                -a)
                    echo "mount -a not supported!" >&2
                    exit 253
                    ;;
                -o|-O|-t)
                    isopttype=true
                    switches="$switches$para "
                    ;;
                -*)
                    switches="$switches$para "
                    ;;
                *)
                    if [ -z "$src" ]; then
                        src=$para
                    elif [ -z "$dest" ]; then
                        dest=$para
                    else
                        echo "Confused by $para after src=$src and dest=$dest" >&2
                        exit 252
                    fi
                    ;;
            esac
        fi
    done

    if [ -z "$src" ] || [ -z "$dest" ]; then
        echo "Please provide both mount source and destination!" >&2
        exit 251
    fi
    mount $PREFIX/${src#/} $dest $switches && mount --bind $dest $PREFIX/${dest#/}
) >>$OUT 2>>$ERR
[[ $? == 254 ]] && exit 0
done

for i in $CMD $OUT $ERR; do
    rm $i
done

It listens to a named pipe (~/myroot/mnt/.mounts.cmd), which the chrooted mount will write to instead of doing an actual mount, and after mounting on the host bind-mount the new mount to the chroot-environment.

The chrooted mount is replaced by this:

#!/bin/bash
# chroot: talk to hosts mount-listener
MNT="/mnt"

CMD=$MNT/.mounts.cmd
OUT=$MNT/.mounts.out
ERR=$OUT
#ERR=$MNT/.mounts.err

echo "$@" > $CMD
cat < $OUT  # TODO output $ERR to stderr
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.