tools/image-info: try simplifying device handling

When iterating over partitions, only record the ones that have a file-
system and save them in a filesystem to device map. Then use that for
mounting. This also prepares the way for LVM and LUKS where there is
not a 1:1 mapping between partition and filesystem.
This commit is contained in:
Christian Kellner 2022-02-28 09:31:01 +01:00
parent 9e5b265a58
commit 932a8a0333

View file

@ -14,10 +14,13 @@ import platform
import re import re
import subprocess import subprocess
import sys import sys
import time
import tempfile import tempfile
import xml.etree.ElementTree import xml.etree.ElementTree
import yaml import yaml
from typing import Dict
from osbuild import loop from osbuild import loop
@ -2374,33 +2377,33 @@ def append_filesystem(report, tree, *, is_ostree=False):
print("EFI partition", file=sys.stderr) print("EFI partition", file=sys.stderr)
def partition_is_lvm(part: Dict) -> bool:
return part["type"].upper() in ["E6D6D379-F507-44C2-A23C-238F2A3DF928", "8E"]
def append_partitions(report, device, loctl): def append_partitions(report, device, loctl):
partitions = report["partitions"] partitions = report["partitions"]
lvm = False lvm = False
with contextlib.ExitStack() as cm: with contextlib.ExitStack() as cm:
# open each partition as a loop device # open each partition as a loop device
devices = {} filesystems = {}
device_idx_by_part_uuid = {} for part in partitions:
for n, part in enumerate(partitions):
start, size = part["start"], part["size"] start, size = part["start"], part["size"]
dev = cm.enter_context(loop_open(loctl, device, offset=start, size=size)) dev = cm.enter_context(loop_open(loctl, device, offset=start, size=size))
devices[n] = dev
read_partition(dev, part) read_partition(dev, part)
if part["uuid"]: if partition_is_lvm(part):
device_idx_by_part_uuid[part["uuid"].upper()] = n
if part["type"] == "E6D6D379-F507-44C2-A23C-238F2A3DF928":
lvm = True lvm = True
elif part["uuid"] and part["fstype"]:
filesystems[part["uuid"].upper()] = dev
if lvm: if lvm:
return return
# find partition with fstab and read it # find partition with fstab and read it
fstab = [] fstab = []
for n, part in enumerate(partitions): for dev in filesystems.values():
if not part["fstype"]: with mount(dev) as tree:
continue
with mount(devices[n]) as tree:
if os.path.exists(f"{tree}/etc/fstab"): if os.path.exists(f"{tree}/etc/fstab"):
fstab.extend(read_fstab(tree)) fstab.extend(read_fstab(tree))
break break
@ -2411,7 +2414,7 @@ def append_partitions(report, device, loctl):
root_tree = "" root_tree = ""
for n, fstab_entry in enumerate(fstab): for n, fstab_entry in enumerate(fstab):
part_uuid = fstab_entry[0].split("=")[1].upper() part_uuid = fstab_entry[0].split("=")[1].upper()
part_device = devices[device_idx_by_part_uuid[part_uuid]] part_device = filesystems[part_uuid]
part_mountpoint = fstab_entry[1] part_mountpoint = fstab_entry[1]
part_fstype = fstab_entry[2] part_fstype = fstab_entry[2]
part_options = fstab_entry[3].split(",") part_options = fstab_entry[3].split(",")