Skip to content

Instantly share code, notes, and snippets.

@jonchampagne
Created April 19, 2020 02:15
Show Gist options
  • Save jonchampagne/28ee498916d610e4ba4b5c5ee8af2753 to your computer and use it in GitHub Desktop.
Save jonchampagne/28ee498916d610e4ba4b5c5ee8af2753 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Loops through all the zfs datasets in a system, and dumps their properties into JSON
import subprocess
import json
def get_zfs_property(prop, pool):
process = subprocess.Popen(['zfs', 'get', '-H', '-o', 'value', '-p', prop, pool],
stdout=subprocess.PIPE, stdin=subprocess.PIPE, universal_newlines=True)
stdout, stderr = process.communicate()
return stdout.strip()
def get_zfs_pools():
process = subprocess.Popen(['zpool', 'list', '-H', '-o', 'name', '-p'],
stdout=subprocess.PIPE, stdin=subprocess.PIPE, universal_newlines=True)
stdout, stderr = process.communicate()
return stdout.splitlines()
def get_zfs_datasets(pool):
process = subprocess.Popen(['zfs', 'list', pool, '-r', '-H', '-o', 'name'],
stdout=subprocess.PIPE, stdin=subprocess.PIPE, universal_newlines=True)
stdout, stderr = process.communicate()
return stdout.splitlines()
def get_zfs_properties(pool):
process = subprocess.Popen(['zfs', 'get', '-H', '-p','-o', 'property,value', 'all', pool],
stdout=subprocess.PIPE, stdin=subprocess.PIPE, universal_newlines=True)
stdout, stderr = process.communicate()
props = {}
for prop in stdout.splitlines():
s = prop.split('\t')
name = s[0]
value = s[1]
try:
props[name] = int(value)
except ValueError:
try:
props[name] = float(value)
except ValueError:
if(value == "on" or value == "yes"):
props[name] = True
elif(value == "off" or value == "no"):
props[name] = False
else:
props[name] = value
return props
pools = {}
pools = {}
for pool in get_zfs_pools():
pool_info = {}
for dataset in get_zfs_datasets(pool):
dataset_info = get_zfs_properties(dataset)
pool_info[dataset] = dataset_info
pools[pool] = pool_info
print(json.dumps(pools))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment