USBフラッシュドライブのファイルシステムをマウントおよびアンマウントできる必要があるpythonスクリプト(ルートとして実行)があります。私はいくつかの調査を行い、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)))
ただし、次のようにオプション「0」または「1」を指定してマウント解除コマンドを試行します。
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)
「/」を含むすべてのファイルシステムをアンマウントすると、システムがクラッシュします。
デバイス番号を特定のパーティションに置き換えても、これはすべて適用されます。
/dev/sdb -> /dev/sdb1
私は何を間違っていますか?