2022-06-11 16:26:40 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
import pyudev
|
|
|
|
import psutil
|
|
|
|
import time
|
2022-06-11 16:01:39 +00:00
|
|
|
import os
|
2022-06-11 16:26:40 +02:00
|
|
|
|
|
|
|
context = pyudev.Context()
|
|
|
|
monitor = pyudev.Monitor.from_netlink(context)
|
2022-06-11 21:06:59 +02:00
|
|
|
monitor.filter_by("block")
|
|
|
|
|
2022-06-11 21:50:01 +02:00
|
|
|
AUTO_MOUNT = True
|
2022-06-11 16:26:40 +02:00
|
|
|
|
2022-06-11 16:01:39 +00:00
|
|
|
|
2022-06-11 20:06:30 +02:00
|
|
|
def printDeviceInfo(dev):
|
2022-06-11 21:06:59 +02:00
|
|
|
print("")
|
|
|
|
print("<BLOCK INFORMATION>")
|
|
|
|
print("Device name: %s" % dev.get("DEVNAME"))
|
|
|
|
print("Device type: %s" % dev.get("DEVTYPE"))
|
|
|
|
print("Bus system: %s" % dev.get("ID_BUS"))
|
|
|
|
print("Partition label: %s" % dev.get("ID_FS_LABEL"))
|
|
|
|
print("FS: %s" % dev.get("ID_FS_SYSTEM_ID"))
|
|
|
|
print("FS type: %s" % dev.get("ID_FS_TYPE"))
|
|
|
|
print("Device usage: %s" % dev.get("ID_FS_USAGE"))
|
|
|
|
print("Device model: %s" % dev.get("ID_MODEL"))
|
|
|
|
print("Partition type: %s" % dev.get("ID_PART_TABLE_TYPE"))
|
|
|
|
print("USB driver: %s" % dev.get("ID_USB_DRIVER"))
|
|
|
|
print("Path id: %s" % dev.get("ID_PATH"))
|
2022-06-11 21:50:01 +02:00
|
|
|
print('Usage: %s' % dev.get("ID_FS_USAGE"))
|
2022-06-12 00:26:34 +02:00
|
|
|
print('Serial short: %s' % dev.get("ID_SERIAL_SHORT"))
|
|
|
|
print('Serial: %s' % dev.get("ID_SERIAL"))
|
|
|
|
print('Model: %s' % dev.get("ID_MODEL_ID"))
|
|
|
|
print(os.stat(dev.get("DEVNAME")))
|
2022-06-11 21:06:59 +02:00
|
|
|
print("</BLOCK INFORMATION>")
|
2022-06-11 21:50:01 +02:00
|
|
|
print("")
|
2022-06-11 21:06:59 +02:00
|
|
|
|
2022-06-11 16:01:39 +00:00
|
|
|
|
2022-06-11 16:26:40 +02:00
|
|
|
# enumerate at device connection
|
|
|
|
for device in iter(monitor.poll, None):
|
2022-06-11 21:50:01 +02:00
|
|
|
if device.get("ID_FS_USAGE") == "filesystem" and device.device_node[5:7] == "sd":
|
2022-06-11 21:06:59 +02:00
|
|
|
if device.action == "add":
|
2022-06-11 21:50:01 +02:00
|
|
|
printDeviceInfo(device)
|
|
|
|
print("New device {}".format(device.device_node))
|
|
|
|
# loop until device is mounted
|
|
|
|
if AUTO_MOUNT:
|
|
|
|
found = False
|
|
|
|
loop = 0
|
|
|
|
while (not found) and (loop < 10):
|
|
|
|
# need to sleep before devide is mounted
|
|
|
|
time.sleep(1)
|
|
|
|
for partition in psutil.disk_partitions():
|
|
|
|
if partition.device == device.device_node:
|
|
|
|
print("Mounted at {}".format(partition.mountpoint))
|
2023-02-25 18:59:41 +01:00
|
|
|
statvfs = os.statvfs(partition.mountpoint)
|
2023-02-25 19:03:34 +01:00
|
|
|
print("size %4.1fGB" %
|
2023-02-25 19:02:42 +01:00
|
|
|
(statvfs.f_frsize * statvfs.f_blocks // 1024 // 1024 / 1024))
|
2023-02-25 19:03:34 +01:00
|
|
|
print("used %4.1fGB" %
|
2023-02-25 19:02:42 +01:00
|
|
|
(statvfs.f_frsize * (statvfs.f_blocks - statvfs.f_bfree) // 1024 // 1024 / 1024))
|
2022-06-11 21:50:01 +02:00
|
|
|
found = True
|
|
|
|
loop += 1
|
|
|
|
else:
|
|
|
|
print("mount device to /media/box")
|
|
|
|
res = os.system("pmount " + device.device_node + " box")
|
|
|
|
print("Return type: ", res)
|
2022-06-11 21:06:59 +02:00
|
|
|
|
|
|
|
if device.action == "remove":
|
2022-06-11 21:50:01 +02:00
|
|
|
print("Device removed")
|
|
|
|
if not AUTO_MOUNT:
|
|
|
|
print("unmount device /media/box")
|
|
|
|
res = os.system("pumount /media/box")
|
|
|
|
print("Return type: ", res)
|