python - How to obtain detailed device / partition info from file path on Linux (like UUID, hard drive serial etc.) -


starting absolute file path, want obtain following information:

  1. the mount point of filesystem on file stored (in order compute path relative mount point)
  2. the uuid , label of file system
  3. the type (or vendor name) , serial number of hard drive contains partition

i aware 2 , 3 may undefined in many cases (e.g. loopback, ramfs, encyrpted devices), totally fine. i know how obtain information using shell , system tools df , /sys or /proc filesystem. see this question reference.

however, searching least cumbersone method programmatically python 3.5. means:

  • prefer system calls instead of parsing contents of /proc or /sys (which may subject change or depend on kernel configuration?)
  • avoid calling subprocesses , parsing output (the definition of cumbersome)

so far, using os.stat() on path block device's major , minor number stat_result.st_dev. what's proper way proceed?

there example

  • /proc/mounts
  • /proc/partitions
  • /sys/dev/block/<major>:<minor>

notes: regarding mounted block devices partitions, /proc/mounts , /proc/partitions seem canonical information source (which ok). uuids, labels, serials etc. use udevadm , parse output:

def get_udev_properties(dev_name):             cmd = ["udevadm", "info", "--query=property", "--name", dev_name]     result = subprocess.run(cmd, stdout=subprocess.pipe)     return parse_properties(result.stdout) 

further note: abstracting acutal problem, 1 ask more general:

  • what's canonical identification or representation of block device respect linux system calls , kernel filesystems?
  • what's proper way obtain representation major , minor number?
  • what's proper way obtain detailed information block device?

this script on github came across earlier today uses python fetch information drive make , model (and lots of others).

/proc/partitions holds info on partitions; more detailed information you'll have either run subprocess now, or example gpt do parsing yourself.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -