Skip to content

Instantly share code, notes, and snippets.

@nijave
Last active July 11, 2021 16:26
Show Gist options
  • Save nijave/3d8fdf44557242070616a9cda939a416 to your computer and use it in GitHub Desktop.
Save nijave/3d8fdf44557242070616a9cda939a416 to your computer and use it in GitHub Desktop.
FreeBSD ZFS Device Info
#!/usr/bin/env python3
import json
import re
import subprocess
import libzfs
zfs = libzfs.ZFS()
#pools = [
# line.split("\t")[0]
# for line in subprocess.check_output(["zpool", "list", "-H"], text=True).splitlines()
#]
pools = [p.name for p in zfs.pools]
# print(pools)
disk_info = {
line.split()[0]: {
"partition": line.split()[2],
"disk": line.split()[2].split('p')[0],
}
for line in subprocess.check_output(["geom", "label", "status", "-s"], text=True).splitlines()
}
# print(disk_info)
pool_devices = [
{
# "pool": pool,
# "device_label": line.split()[0],
# "partition": disk_info[line.split()[0]]["partition"],
# "disk": disk_info[line.split()[0]]["disk"],
"pool": pool.name,
"device_label": disk,
"partition": disk_info[disk]["partition"],
"disk": disk_info[disk]["disk"],
"present": 1,
}
for pool in zfs.pools
for disk in (d[5:] for d in pool.disks)
if disk.startswith("gptid/")
# for pool in pools
# for line in subprocess.check_output(["zpool", "iostat", "-v", pool], text=True).splitlines()
# if re.match("\s{4}[^\s]", line)
]
# print(pool_devices)
for d in pool_devices:
d["serial"] = {
l.split(": ")[0].strip(): l.split(": ")[1].strip()
for l in subprocess.check_output(["geom", "disk", "list", d["disk"]], text=True).splitlines()
if l.startswith(" ")
}.get("ident", None)
print(
json.dumps(pool_devices, indent=2)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment