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

Some tweaks to image-info #9

Merged
merged 3 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
37 changes: 23 additions & 14 deletions tools/image-info
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def read_partition_table(device):
for p in ptable["partitions"]:
partitions.append({
"type": p["type"],
"bootable": p["bootable"],
"bootable": p.get("bootable", False),
"start": p["start"] * 512,
"size": p["size"] * 512
})
Expand All @@ -65,7 +65,7 @@ def read_bootloader_type(device):
if b"GRUB" in f.read(512):
return "grub"
else:
raise RuntimeError("unkown bootloader")
return "unknown"


def read_os_release(tree):
Expand All @@ -87,23 +87,32 @@ with nbd_connect(image) as device:
report["bootloader"] = read_bootloader_type(device)
report["partition_table"], report["partitions"] = read_partition_table(device)

# only one partition containing the root filesystem supported for now
assert len(report["partitions"]) == 1
n_partitions = len(report["partitions"])

with mount(device + "p1") as tree:
report["packages"] = sorted(subprocess_check_output(["rpm", "--root", tree, "-qa"], str.split))
report["os_release"] = read_os_release(tree)

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("#")])
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/passwd") as f:
report["passwd"] = sorted(f.read().strip().split("\n"))
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("#")])

with open(f"{tree}/etc/group") as f:
report["groups"] = sorted(f.read().strip().split("\n"))
with open(f"{tree}/etc/passwd") as f:
report["passwd"] = sorted(f.read().strip().split("\n"))

report["bootmenu"] = [read_bls_conf(f) for f in glob.glob(f"{tree}/boot/loader/entries/*.conf")]
with open(f"{tree}/etc/group") as f:
report["groups"] = sorted(f.read().strip().split("\n"))

if os.path.exists(f"{tree}/boot") and len(os.listdir(f"{tree}/boot")) > 0:
assert "bootmenu" not in report
report["bootmenu"] = [read_bls_conf(f) for f in glob.glob(f"{tree}/boot/loader/entries/*.conf")]

elif len(glob.glob(f"{tree}/vmlinuz-*")) > 0:
assert "bootmenu" not in report
report["bootmenu"] = [read_bls_conf(f) for f in glob.glob(f"{tree}/loader/entries/*.conf")]


json.dump(report, sys.stdout, sort_keys=True, indent=2)