我有一个python脚本(以root身份运行),需要能够挂载和卸载USB闪存盘的文件系统。我做了一些研究,我找到了这个使用ctypes的答案https://stackoverflow.com/a/29156997。然而。答案只指定如何安装,所以我试图创建一个类似的功能来卸载设备。总而言之,我有这个:
import os
import ctypes
def mount(source, target, fs, options=''):
ret = ctypes.CDLL('libc.so.6', use_errno=True).mount(source, target, fs, 0, options)
if ret < 0:
errno = ctypes.get_errno()
raise RuntimeError("Error mounting {} ({}) on {} with options '{}': {}".
format(source, fs, target, options, os.strerror(errno)))
def unmount(device, options=0):
ret = ctypes.CDLL('libc.so.6', use_errno=True).umount2(device, options)
if ret < 0:
errno = ctypes.get_errno()
raise RuntimeError("Error umounting {} with options '{}': {}".format(device, options, os.strerror(errno)))
但是,尝试使用选项&#34; 0&#34;的卸载命令或&#34; 1&#34;像:
unmount('/dev/sdb', 0)
或
unmount('/dev/sdb', 1)
给出以下错误:
Traceback (most recent call last):
File "./BuildAndInstallXSystem.py", line 265, in <module>
prepare_root_device()
File "./BuildAndInstallXSystem.py", line 159, in prepare_root_device
unmount('/dev/sdb', 0)
File "./BuildAndInstallXSystem.py", line 137, in unmount
raise RuntimeError("Error umounting {} with options '{}': {}".format(device, options, os.strerror(errno)))
RuntimeError: Error umounting /dev/sdb with options '0': Device or resource busy
在运行它时选择2作为选项:
unmount('/dev/sdb', 2)
卸载我的所有文件系统,包括&#39; /&#39;,导致系统崩溃。
即使我用特定分区替换设备号,所有这些仍然适用:
/dev/sdb -> /dev/sdb1
我做错了什么?