使用udisks二进制文件显然可以获得大量有关附加磁盘的信息:
udisks --show-info /dev/sda1
udisks显然只是枚举了udev可用的数据。
是否可以使用python获取此信息?比方说,如果我只想检索设备序列,挂载点和大小。
答案 0 :(得分:13)
您可以直接在python中通过dbus使用Udisks。
import dbus
bus = dbus.SystemBus()
ud_manager_obj = bus.get_object("org.freedesktop.UDisks", "/org/freedesktop/UDisks")
ud_manager = dbus.Interface(ud_manager_obj, 'org.freedesktop.UDisks')
for dev in ud_manager.EnumerateDevices():
device_obj = bus.get_object("org.freedesktop.UDisks", dev)
device_props = dbus.Interface(device_obj, dbus.PROPERTIES_IFACE)
print device_props.Get('org.freedesktop.UDisks.Device', "DriveVendor")
print device_props.Get('org.freedesktop.UDisks.Device', "DeviceMountPaths")
print device_props.Get('org.freedesktop.UDisks.Device', "DriveSerial")
print device_props.Get('org.freedesktop.UDisks.Device', "PartitionSize")
可用属性的完整列表位于http://hal.freedesktop.org/docs/udisks/Device.html
答案 1 :(得分:1)
如果所有其他方法都失败了,您可以解析udisks
的输出。这是Python3.2中的示例脚本:
from subprocess import check_output as qx
from configparser import ConfigParser
def parse(text):
parser = ConfigParser()
parser.read_string("[DEFAULT]\n"+text)
return parser['DEFAULT']
def udisks_info(device):
# get udisks output
out = qx(['udisks', '--show-info', device]).decode()
# strip header & footer
out = out[out.index('\n')+1:]
i = out.find('=====')
if i != -1: out = out[:i]
return parse(out)
info = udisks_info('/dev/sda1')
print("size = {:.2f} GiB".format(info.getint('size')/2**30))
print("""mount point = {mount paths}
uuid = {uuid}""".format_map(info))
# complex values could be parsed too
info = udisks_info('/dev/sda')
drive_data = info['drive'].replace('ports:\n', 'ports:\n ')
print('serial =', parse(drive_data)['serial'])
size = 57.15 GiB
mount point = /
uuid = b1812c6f-3ad6-40d5-94a6-1575b8ff02f0
serial = N31FNPH8