如何使用Python挂载网络目录?

8 浏览
0 Comments

如何使用Python挂载网络目录?

我需要在Linux机器上使用Python将目录“dir”挂载到网络机器“data”上。

我知道可以通过命令行发送命令:

mkdir ~/mnt/data_dir
mount -t data:/dir/ ~/mnt/data_dir

但我该如何从Python脚本中发送这些命令?

admin 更改状态以发布 2023年5月23日
0
0 Comments

我在一个没有安装proc挂载的chroot中尝试了这个。

/ # python
Python 2.7.1 (r271:86832, Feb 26 2011, 00:09:03) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> from ctypes import *
>>> libc = cdll.LoadLibrary("libc.so.0")
>>> os.listdir("/proc")
[]
>>> libc.mount(None, "/proc", "proc", 0, None)
0
>>> os.listdir("/proc")
['vmnet', 'asound', 'sysrq-trigger', 'partitions', 'diskstats', 'crypto', 'key-users', 'version_signature', 'kpageflags', 'kpagecount', 'kmsg', 'kcore', 'softirqs', 'version', 'uptime', 'stat', 'meminfo', 'loadavg', 'interrupts', 'devices', 'cpuinfo', 'cmdline', 'locks', 'filesystems', 'slabinfo', 'swaps', 'vmallocinfo', 'zoneinfo', 'vmstat', 'pagetypeinfo', 'buddyinfo', 'latency_stats', 'kallsyms', 'modules', 'dma', 'timer_stats', 'timer_list', 'iomem', 'ioports', 'execdomains', 'schedstat', 'sched_debug', 'mdstat', 'scsi', 'misc', 'acpi', 'fb', 'mtrr', 'irq', 'cgroups', 'sys', 'bus', 'tty', 'driver', 'fs', 'sysvipc', 'net', 'mounts', 'self', '1', '2', '3', '4', '5', '6', '7', '8' ..........

你应该能够把设备文件从“None”更改为mount()函数期望的网络共享格式。 我相信它与mount命令“host:/path/to/dir”相同。

0
0 Comments

我建议您使用subprocess.checkcall

from subprocess import *
#most simply
check_call( 'mkdir ~/mnt/data_dir', shell=True )
check_call( 'mount -t whatever data:/dir/ ~/mnt/data_dir', shell=True )
#more securely
from os.path import expanduser
check_call( [ 'mkdir', expanduser( '~/mnt/data_dir' ) ] )
check_call( [ 'mount', '-t', 'whatever', 'data:/dir/', expanduser( '~/mnt/data_dir' ) ] )

0