Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

image-info: include partition label and uuid #11

Merged
merged 2 commits into from
Sep 30, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions tools/image-info
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ def mount(device):
subprocess.run(["umount", "--lazy", mountpoint], check=True)


def parse_environment_vars(s):
r = {}
for line in s.split("\n"):
line = line.strip()
if not line:
continue
key, value = line.split("=")
r[key] = value.strip('"')
return r


def subprocess_check_output(argv, parse_fn=None):
output = subprocess.check_output(argv, encoding="utf-8")
return parse_fn(output) if parse_fn else output
Expand All @@ -55,8 +66,12 @@ def read_partition_table(device):

partitions = []
for p in ptable["partitions"]:
blkid = subprocess_check_output(["blkid", "--output", "export", p["node"]], parse_environment_vars)
partitions.append({
"label": blkid.get("LABEL"), # doesn't exist for mbr
"type": p["type"],
"uuid": blkid["UUID"],
"fstype": blkid["TYPE"],
"bootable": p.get("bootable", False),
"start": p["start"] * 512,
"size": p["size"] * 512
Expand All @@ -73,15 +88,6 @@ def read_bootloader_type(device):
return "unknown"


def read_os_release(tree):
r = {}
with open(f"{tree}/etc/os-release") as f:
for line in f:
key, value = line.strip().split("=")
r[key] = value.strip('"')
return r


def read_bls_conf(filename):
with open(filename) as f:
return dict(line.strip().split(" ", 1) for line in f)
Expand All @@ -91,17 +97,16 @@ report = {}
with nbd_connect(image) as device:
report["image-format"] = read_image_format(image)
report["bootloader"] = read_bootloader_type(device)
report["partition_table"], report["partitions"] = read_partition_table(device)
report["partition-table"], report["partitions"] = read_partition_table(device)

n_partitions = len(report["partitions"])


for n in range(1, n_partitions + 1):
with mount(device + f"p{n}") as tree:
# subprocess.run(["ls", "-l", tree])
if os.path.exists(f"{tree}/etc/os-release"):
report["packages"] = sorted(subprocess_check_output(["rpm", "--root", tree, "-qa"], str.split))
report["os_release"] = read_os_release(tree)

with open(f"{tree}/etc/os-release") as f:
report["os-release"] = parse_environment_vars(f.read())

with open(f"{tree}/etc/fstab") as f:
report["fstab"] = sorted([line.split() for line in f.read().split("\n") if line and not line.startswith("#")])
Expand Down