【问题标题】:Is it possible to atomically change mount device in Linux?是否可以在 Linux 中自动更改挂载设备?
【发布时间】:2021-10-10 14:22:50
【问题描述】:

是否可以原子地更改挂载到挂载点的设备,因此在任何时候都不能观察到挂载点为空?这是我想要做的:

$ mkdir /point
$ mkdir /device1 /device2
$ echo 1 > /device1/text
$ echo 2 > /device2/text
$ mount --bind /device1 /point
$ # do some kind of magic to get /device2 mounted at /point and /device1 unmounted

从技术上讲,我可以忽略以前的挂载并在顶部挂载新设备,但内核现在会记录这两者,所以感觉就像“挂载泄漏”。

【问题讨论】:

    标签: linux mount mount-point


    【解决方案1】:

    我已经成功运行 [ubuntu "20.04.3 LTS (Focal Fossa)"]:

    /tmp# cat test.sh
    #!/bin/bash
    
    set -e
    mkdir -p /tmp/{point,device1,device2}
    echo 1 > /tmp/device1/text
    echo 2 > /tmp/device2/text
    mount --bind /tmp/device1 /tmp/point
    cat /tmp/point/text
    mount --bind /tmp/device2 /tmp/point
    cat /tmp/point/text
    umount /tmp/device1
    echo "Finished successfully"
    
    /tmp# bash test.sh
    1
    2
    Finished successfully
    /tmp#
    

    【讨论】:

    • umount: /tmp/device1/: not mounted.。 /device1 不是挂载点。
    • @KAction 查看我的更新
    • ```+; doas bash test.sh 1 2 umount: /tmp/device1: 未安装。成功完成+; doas bash test.sh +;其中umount /bin/umount +; umount --version umount 从 util-linux 2.37.2 (libmount 2.37.2: btrfs, namespaces, assert, debug)
    • 由于你的脚本没有使用“set -e”,我也不能确定你的机器上卸载是否成功。
    • @KAction 添加了set -e。同样的成功。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    • 1970-01-01
    • 2012-01-15
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多