如何使用 mountpoint 使用变量 shell/py 验证多个目录

如何使用 mountpoint 使用变量 shell/py 验证多个目录

如果尝试自动化此检查,py 函数将如何与 shell 一起工作

def success():
    print("###    ####end")
    print("###end")

# It can be called when the script fails and output the failed signal.
def failure():
    print("### ####end")
    print("###end")

# It can be called when the script needs to Export results.Separate multiple export results with \n.
# if you want to output multi line messages, you can call the function multiple times or add newline characters, for example, output("aaaaa\nbbbbb\nccccc\nddddd")
# Note: 1. If you create a job by calling an interface, the script can obtain the script output from the output field only by calling the output function.
# Note: 2. Only output messages are exported by the Export Result button on the page.
def output(msg):
    print("### ####end")
    print(msg)
    print("###end")

# Special output function for inspection work, output inspection results.Multiple inspection results are separated by %.
# example: inspect_output("$item_name1%$item_name2%$item_name3")
# Note: The inspection task definition script must use the inspect_output function.
def inspect_output(msg):
    print("### ####end")
    print("inspect_output:{msg} end".format(msg=msg))
    print("###end")
 
# Note:script content in the main function. Ensure that you call function success or failure.Otherwise, the system cannot capture the script output information.
def main():
    # Note: if output logs are needed, direct print(xxx) can be used.
    pass

if __name__ == "__main__":
    main()

答案1

如果您想检查一个目录是否“只是”一个目录,或者它是否用作挂载点,可以使用软件包mountpoint中的工具util-linux

使用示例

  • 简单目录
    ~$ mkdir testdir
    ~$ mountpoint testdir
    testdir is not a mountpoint
    
  • 将文件系统挂载到目录
    ~$ sudo mount /my/external/filesystem testdir
    ~$ mountpoint testdir
    testdir is a mountpoint
    

如果你想在shell脚本中使用它进行自动化测试,还有一个静默模式可用:

if mountpoint -q testdir
then
    # perform operations on the mounted filesystem
else
    echo "Error, nothing mounted on testdir!"
fi

答案2

如果我理解正确的话,您需要确保在将数据写入这些目录之前所有安装都已正确安装。如果这是正确的,我建议在安装目录之前在目录上设置一个不可变的标志。这是一个例子:

[user@host ~]$ mkdir ./1 && sudo chattr +i ./1
[user@host ~]$ sudo touch 1/test.txt
touch: cannot touch ‘1/test.txt’: Permission denied
[user@host ~]$ sudo mount -t tmpfs none 1
[user@host ~]$ touch 1/test.txt && ls -l 1/test.txt
-rw-r--r-- 1 user group 0 Aug 18 10:49 1/test.txt

相关内容