与Docker卷装载NFS共享有关的权限测试问题 [英] Permissions tests issue with Docker volume mounting NFS share

查看:16
本文介绍了与Docker卷装载NFS共享有关的权限测试问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下Python代码有问题

import os
print("Can I write into the tv folder?",os.access("/tv", os.W_OK))
print("Does the /tv/test file exists?", os.access("/tv/test", os.R_OK))
with open("/tv/test", "w") as f:
    f.write("toto")
print("Does the /tv/test file exists now?", os.access("/tv/test", os.R_OK))
with open("/tv/test", "r") as f:
    print("Content of the /tv/test file:")
    print(f.read())

打印输出

Can I write into the tv folder? False
Does the /tv/test file exists? False
Does the /tv/test file exists now? True
Content of the /tv/test file;
toto

但根据对os.access的第一次调用,我应该无法写入测试文件...

我的问题不是它已经能够创建文件的事实,而是我想要的。但测试应该会产生相关的结果。(我使用的一个软件因此而出现故障)

我在覆盆子pi上的docker容器(Docker版本20.10.7)中运行的是Python3.8.10,而TV文件夹是由docker挂载的远程nfs共享。

root@7f0a44aad8a9:/> ls -la /tv
total 8
drwxrwxrwx 1 abc  abc   3826 Jul 27 14:18  .
drwxr-xr-x 1 root root  4096 Jul 27 14:12  ..
drwxrwxrwx 1 abc  abc    988 May 13 07:30 DARK
... (a lot of other folders)
-rwxrwxrwx 1 abc  abc      4 Jul 27 14:18  test

我的docker撰写文件:

version: "3.4"

services:
  bazarr:
    image: ghcr.io/linuxserver/bazarr
    container_name: bazarr
    volumes:
      - shows:/tv
    ports:
      - 6767:6767
    dns: 1.1.1.1
    restart: unless-stopped

volumes:
  shows:
    driver_opts:
      type: "nfs"
      o: "nfsvers=3,addr=xxx,soft,rw"
      device: ":xxx"

我知道os.access可以给出假阳性,但给出假阴性?
有没有人看过这个?
这是预期的吗?

编辑:问题不是特定于Python

root@6c5dd99ee211:/> if [ -w /tv ]; then echo "WRITABLE"; else echo "NOT WRITABLE"; fi
NOT WRITABLE

但为什么?

推荐答案

这与POSIX文件系统(包括*nix文件系统)的关系要比与Python大得多。

但根据对os.access的第一次调用,我不应该能够写入测试文件...

不,很遗憾,这是错误的(但不可否认,这有点不直观)。

在POSIX文件系统中,您可能没有写入目录的权限(这是您选中的目录),但并不意味着您不能写入该目录中的文件。这意味着您无法将文件写入到该目录。

示例:

(base) mmessersmith@HP-ZBK-0425:~$ mkdir test_dir
(base) mmessersmith@HP-ZBK-0425:~$ touch test_dir/abc.txt
(base) mmessersmith@HP-ZBK-0425:~$ cat test_dir/abc.txt
(base) mmessersmith@HP-ZBK-0425:~$ chmod 777 test_dir/abc.txt
(base) mmessersmith@HP-ZBK-0425:~$ chmod 500 test_dir
(base) mmessersmith@HP-ZBK-0425:~$ touch test_dir/new_file.txt
touch: cannot touch 'test_dir/new_file.txt': Permission denied
(base) mmessersmith@HP-ZBK-0425:~$ echo "123" > test_dir/abc.txt
(base) mmessersmith@HP-ZBK-0425:~$ cat test_dir/abc.txt
123
(base) mmessersmith@HP-ZBK-0425:~$ ls -al test_dir/
total 12
dr-x------  2 mmessersmith mmessersmith 4096 Jul 27 09:32 .
drwxr-xr-x 24 mmessersmith mmessersmith 4096 Jul 27 09:31 ..
-rwxrwxrwx  1 mmessersmith mmessersmith    4 Jul 27 09:32 abc.txt
请注意,我无法将新文件写入目录test_dir,但我仍可以写入test_dir中的abc.txt。在上面的示例中,您需要选中os.access("/tv/test", os.W_OK)。在我对上例的REPL中:

(base) mmessersmith@HP-ZBK-0425:~$ python
Python 3.8.3 (default, May 19 2020, 18:47:26)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.access('test_dir', os.W_OK)
False
>>> os.access('test_dir/abc.txt', os.R_OK)
True
>>> os.access('test_dir/abc.txt', os.W_OK)
True

最后,注意到os.access是对Linux/Unix访问的简单包装:https://manpages.debian.org/buster/manpages-dev/access.2.en.html

所以不是,这不是假阳性或假阴性,只是文件系统是如何工作的。

这篇关于与Docker卷装载NFS共享有关的权限测试问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆