如何与其主机共享 chroot 系统挂载?

如何与其主机共享 chroot 系统挂载?

假设我chroot~/myroot/mnt从主机的/mnt.现在(从之内环境chroot)我想要mount /dev/something /mnt/something例如不仅是主机~/myroot/mnt/something,而且它是 /mnt/something包含此坐骑。如何才能实现这一目标?

不幸的是,mount命令busybox似乎没有实现--make-shared提到的这里,这应该使mount --make-shared --bind /mnt ~/myroot/mnt在主机上运行可以使这项工作正常进行(尽管未经测试!),那么如何使用 来实现这一点呢busybox

答案1

我认为您需要的 busybox 选项是-o shared.

答案2

原型!

一种方法是在主机上运行以下脚本:

#!/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

它侦听命名管道 ( ~/myroot/mnt/.mounts.cmd),chrootedmount将写入该管道而不是进行实际安装,并在安装到主机上后将新安装绑定到环境中chroot

edchrootmount替换为:

#!/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

相关内容