【问题标题】:Moving a mount's origin directory移动挂载的原始目录
【发布时间】:2021-05-23 17:08:48
【问题描述】:

我在我们的生产环境中遇到了奇怪的行为。

我们有一个 NFS 和一个运行我们的应用程序的 linux 服务器。

在 linux 服务器上有一个 NFS 挂载,
来自:/configuration/data(在 NFS 上)
收件人:/software(在 linux 服务器上)。
应用程序定期修改那里的文件。

前段时间有人不小心将“data”文件夹移动到:/configuration/other/data

应用程序继续运行,没有任何副作用,
并定期修改文件,
即使挂载(/configuration/data)指向任何内容,/configuration/other/data 中的文件也发生了变化。

我猜想在文件夹重定位时修改了挂载源的快捷方式,但这只是一个猜测。

我想知道为什么和如何会出现这种行为,以及它在内部是如何运作的。

【问题讨论】:

    标签: linux directory mount nfs


    【解决方案1】:

    以及它在内部是如何工作的。

    文件描述符是指一个文件。您可以移动文件,也可以删除文件——文件描述符指的是同一个“实体”。所以在shell中你可以例如:

    # open fd 10 to refer to /tmp/10
    $ exec 10>/tmp/10
    

    # Just write something so that it works
    $ echo abc >&10
    $ cat /tmp/10
    abc
    

    # Move the file to some dir
    $ mkdir /tmp/dir
    $ mv /tmp/10 /tmp/dir/10
    # now it still writes to the same "file", even when moved
    $ echo def >&10
    $ cat /tmp/dir/10 
    abc
    def
    

    # You can remove the file and still access it
    # The file still "exists"
    $ exec 11</tmp/dir/10
    $ rm /tmp/dir/10
    $ echo 123 >&10
    $ cat <&11
    abc
    def
    123
    

    为文件创建文件描述符然后删除文件通常在 C 程序中:

    char *filename = mkstemp(...);
    int fd = open(filename);
    unlink(filename); 
    // use fd
    

    当没有指向它的链接时,文件实际上是“删除”的——当最后一个文件描述符关闭时。研究 posix 文件描述符并查看例如 man 2 unlink 和其他解释什么是文件描述符的资源。

    很可能您的应用程序不断打开文件描述符到/configuration/data 中的文件,因此在移动文件后,数据在新位置可用,但应用程序仍使用相同的文件描述符。

    【讨论】:

      猜你喜欢
      • 2011-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-20
      • 1970-01-01
      • 2010-09-23
      相关资源
      最近更新 更多