Advertisement
DeaD_EyE

lsblk without lsblk?

Jul 28th, 2021
1,292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. """
  2. dnutc [do not use this code]
  3.  
  4. Trying to get the same information without lsblk
  5.  
  6. got the idea from https://github.com/archlinux/archinstall/blob/master/archinstall/lib/disk.py#L583
  7. """
  8.  
  9. def all_disks():
  10.     for block in Path("/sys/class/block").glob("*"):
  11.         data = dict.fromkeys("path size type mountpoint label name model".split())
  12.         for line in (block / "uevent").read_text().splitlines():
  13.             if not (line := line.strip()):
  14.                 continue
  15.             key, value = line.split("=", maxsplit=1)
  16.             key = key.lower().removeprefix("dev")
  17.             if key in ("major", "minor", "partn"):
  18.                 continue
  19.             data[key] = value
  20.         data["size"] = int((block / "size").read_bytes()) * 512
  21.         model_file = block / "device/model"
  22.         if model_file.exists():
  23.             data["model"] = model_file.read_text().strip()
  24.         data["path"] = str(Path("/dev", data["name"]))
  25.        
  26.         with open("/proc/mounts") as fd:
  27.             for line in fd:
  28.                 dev, mountpoint, fstype, *_ = line.split()
  29.                 if dev == data["path"]:
  30.                     _, data["mountpoint"], data["fstype"], *_ = line.split()
  31.                     # what if more than on mount?
  32.                     continue
  33.         yield data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement