Skip to content

Commit d477392

Browse files
authored
Merge pull request #124 from learmj/idp_dev
IDP: JSON cleanup
2 parents 7738fc4 + b13e1a3 commit d477392

File tree

9 files changed

+67
-115
lines changed

9 files changed

+67
-115
lines changed

bin/image2json

Lines changed: 13 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,11 @@ def parse_genimage_config(config_path):
318318
if piname == simg.get("image"):
319319
partitions[pname]["simage"] = sname
320320

321+
# Remove filesystem attributes from all partitions
322+
for part in partitions.values():
323+
fs_key = str(part.get("type", "")).lower()
324+
part.pop(fs_key, None)
325+
321326
# Read the partition table from the image
322327
img_path = get_artefact_path(img_name, "IGconf_image_outputdir")
323328
try:
@@ -330,10 +335,7 @@ def parse_genimage_config(config_path):
330335
except Exception as e:
331336
raise RuntimeError(f"{e} Failed to read partition table from {img_path}")
332337

333-
# Nodes are don't care
334338
ptable = json.loads(res.stdout)
335-
for p in ptable["partitiontable"]["partitions"]:
336-
p.pop("node", None)
337339

338340
# Populate genimage partition entries with GPT attributes
339341
def insert_gptattr(genimage_partitions, table_json):
@@ -365,89 +367,16 @@ def parse_genimage_config(config_path):
365367

366368
return genimage_partitions
367369

370+
# Assign GPT attributes from the partition table probe to each genimage partition
368371
partitions = insert_gptattr(partitions, ptable)
369372

370-
return (disk_attr, partitions, ptable)
371-
372-
373-
# Read all fstabs and store in a dict using UUID or label if we can,
374-
# or a unique key if we can't.
375-
def parse_fstab(fstab_paths):
376-
fstab_data = {}
377-
fcount = 1
378-
for fstab_path in fstab_paths:
379-
try:
380-
with open(fstab_path, "r") as f:
381-
lcount = 1
382-
for line in f:
383-
line = line.strip()
384-
if line.startswith("#") or line == "":
385-
continue # skip comments or empty
386-
387-
parts = line.split()
388-
if len(parts) == 4:
389-
device, mountpoint, fstype, options = parts[:4]
390-
freq = "0"
391-
passn = "0"
392-
elif len(parts) == 5:
393-
device, mountpoint, fstype, options, freq = parts[:5]
394-
passn = "0"
395-
elif len(parts) == 6:
396-
device, mountpoint, fstype, options, freq, passn = parts[:6]
397-
else:
398-
continue # skip unusable
399-
400-
mount_options = options.split(",")
401-
402-
# Supported fs_spec:
403-
if device.startswith(("UUID=", "LABEL=", "PARTUUID=", "PARTLABEL=")):
404-
deviceid = device.split("=", 1)[1]
405-
elif device.startswith(("/dev/disk/by-label/", "/dev/disk/by-uuid/")):
406-
deviceid = device.rsplit("/", 1)[-1]
407-
else:
408-
deviceid = f"fstab{fcount}_{lcount}"
409-
410-
# This will overwrite previous settings if the device exists in multiple fstabs
411-
# under the same uuid/label.
412-
fstab_data[deviceid] = {"fs_spec": device,
413-
"fs_file": mountpoint,
414-
"fs_vfstype": fstype,
415-
"fs_mntops": mount_options,
416-
"fs_freq": freq,
417-
"fs_passno": passn}
418-
lcount += 1
419-
420-
except FileNotFoundError:
421-
sys.exit('invalid fstab: %s' % fstab_path)
422-
423-
fcount += 1
424-
425-
return fstab_data
426-
427-
428-
# Try to match a genimage partition with an fstab device entry to establish mount options.
429-
# Try static uuid and label first, falling back to genimage mountpoint.
430-
# This lookup can only give guaranteed matching results if there is no duplication of
431-
# uuid, label or mountpoint in each fstab file provided.
432-
# If a match is found, the fstab section of the partition is populated.
433-
def merge_configs(genimage_partitions, fstab_data):
434-
for pname, pdata in genimage_partitions.items():
435-
label = pdata.get("fs_label")
436-
uuid = pdata.get("fs_uuid")
437-
438-
if uuid and uuid in fstab_data:
439-
pdata["fstab"] = fstab_data[uuid]
440-
elif label and label in fstab_data:
441-
pdata["fstab"] = fstab_data[label]
442-
else:
443-
pmnt = pdata.get("mountpoint")
444-
if pmnt:
445-
for name, contents in fstab_data.items():
446-
if pmnt == contents.get("fs_file"):
447-
pdata["fstab"] = fstab_data[name]
448-
449-
return genimage_partitions
373+
# Prune non-essential partition table info
374+
pt = ptable.get("partitiontable")
375+
if isinstance(pt, dict):
376+
for key in ("partitions", "device", "unit", "firstlba", "lastlba", "sectorsize"):
377+
pt.pop(key, None)
450378

379+
return (disk_attr, partitions, ptable)
451380

452381

453382
def get_env_vars(prefix=None):
@@ -480,11 +409,6 @@ if __name__ == '__main__':
480409
help="Path to genimage config file",
481410
required=True)
482411

483-
parser.add_argument("-f", "--fstab",
484-
help="Paths to one or more fstab files",
485-
nargs="*",
486-
required=False)
487-
488412
parser.add_argument("-m", "--provisionmap",
489413
help="Path to the JSON provisioning map",
490414
type=argparse.FileType('r'),
@@ -495,14 +419,7 @@ if __name__ == '__main__':
495419

496420
# Base info
497421
attributes, genimage_partitions, ptable = parse_genimage_config(genimage_file)
498-
499-
# fstab is optional
500-
if args.fstab:
501-
fstab_files = args.fstab
502-
fstab_data = parse_fstab(fstab_files)
503-
partition_json = merge_configs(genimage_partitions, fstab_data)
504-
else:
505-
partition_json = genimage_partitions
422+
partition_json = genimage_partitions
506423

507424
top_template["IGmeta"] = get_image_meta()
508425
top_template["attributes"]["image-name"] = os.path.basename(attributes.get("image-name"))

image/gpt/ab_userdata/device/provisionmap-clear.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
[
22
{
33
"attributes": {
4-
"PMAPversion": "1.2.0",
4+
"PMAPversion": "1.3.0",
55
"system_type": "slotted"
66
}
77
},
88
{
99
"partitions": [
1010
{
11+
"comment": "boot configuration",
1112
"image": "config"
1213
}
1314
]
@@ -17,6 +18,7 @@
1718
"A": {
1819
"partitions": [
1920
{
21+
"comment": "kernel + device tree + initramfs slot A",
2022
"image": "boot_a",
2123
"static": {
2224
"uuid": "<BOOT_UUID>",
@@ -32,6 +34,7 @@
3234
"B": {
3335
"partitions": [
3436
{
37+
"comment": "kernel + device tree + initramfs slot B",
3538
"image": "boot_b",
3639
"static": {
3740
"uuid": "<BOOT_UUID>",
@@ -47,6 +50,7 @@
4750
"A": {
4851
"partitions": [
4952
{
53+
"comment": "root filesystem slot A",
5054
"image": "system_a",
5155
"static": {
5256
"uuid": "<SYSTEM_UUID>",
@@ -62,6 +66,7 @@
6266
"B": {
6367
"partitions": [
6468
{
69+
"comment": "root filesystem slot B",
6570
"image": "system_b",
6671
"static": {
6772
"uuid": "<SYSTEM_UUID>",
@@ -75,6 +80,7 @@
7580
{
7681
"partitions": [
7782
{
83+
"comment": "user data - shared between slots",
7884
"image": "persistent"
7985
}
8086
]

image/gpt/ab_userdata/device/provisionmap-crypt.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
[
22
{
33
"attributes": {
4-
"PMAPversion": "1.3.1",
4+
"PMAPversion": "1.4.1",
55
"system_type": "slotted"
66
}
77
},
88
{
99
"partitions": [
1010
{
11+
"comment": "boot configuration",
1112
"image": "config"
1213
}
1314
]
@@ -17,6 +18,7 @@
1718
"A": {
1819
"partitions": [
1920
{
21+
"comment": "kernel + device tree + initramfs slot A",
2022
"image": "boot_a",
2123
"static": {
2224
"uuid": "<BOOT_UUID>",
@@ -32,6 +34,7 @@
3234
"B": {
3335
"partitions": [
3436
{
37+
"comment": "kernel + device tree + initramfs slot B",
3538
"image": "boot_b",
3639
"static": {
3740
"uuid": "<BOOT_UUID>",
@@ -48,15 +51,16 @@
4851
"key_size": 512,
4952
"cipher": "aes-xts-plain64",
5053
"hash": "sha256",
51-
"label": "root",
54+
"label": "OSDATA",
5255
"uuid": "<CRYPT_UUID>",
53-
"mname": "cryptroot",
56+
"mname": "osdata_crypt",
5457
"etype": "partitioned"
5558
},
5659
"slots": {
5760
"A": {
5861
"partitions": [
5962
{
63+
"comment": "Encrypted root filesystem slot A",
6064
"image": "system_a",
6165
"static": {
6266
"uuid": "<SYSTEM_UUID>",
@@ -68,6 +72,7 @@
6872
"B": {
6973
"partitions": [
7074
{
75+
"comment": "Encrypted root filesystem slot B",
7176
"image": "system_b",
7277
"static": {
7378
"uuid": "<SYSTEM_UUID>",
@@ -79,6 +84,7 @@
7984
},
8085
"partitions": [
8186
{
87+
"comment": "Encrypted user data - shared between slots",
8288
"image": "persistent"
8389
}
8490
]

image/gpt/ab_userdata/device/provisionmap-cryptdata.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
[
22
{
33
"attributes": {
4-
"PMAPversion": "1.0.0",
4+
"PMAPversion": "1.1.0",
55
"system_type": "slotted"
66
}
77
},
88
{
99
"partitions": [
1010
{
11+
"comment": "boot configuration",
1112
"image": "config"
1213
}
1314
]
@@ -17,6 +18,7 @@
1718
"A": {
1819
"partitions": [
1920
{
21+
"comment": "kernel + device tree + initramfs slot A",
2022
"image": "boot_a",
2123
"static": {
2224
"uuid": "<BOOT_UUID>",
@@ -32,6 +34,7 @@
3234
"B": {
3335
"partitions": [
3436
{
37+
"comment": "kernel + device tree + initramfs slot B",
3538
"image": "boot_b",
3639
"static": {
3740
"uuid": "<BOOT_UUID>",
@@ -47,6 +50,7 @@
4750
"A": {
4851
"partitions": [
4952
{
53+
"comment": "root filesystem slot A",
5054
"image": "system_a",
5155
"static": {
5256
"uuid": "<SYSTEM_UUID>",
@@ -62,6 +66,7 @@
6266
"B": {
6367
"partitions": [
6468
{
69+
"comment": "root filesystem slot B",
6570
"image": "system_b",
6671
"static": {
6772
"uuid": "<SYSTEM_UUID>",
@@ -78,13 +83,14 @@
7883
"key_size": 512,
7984
"cipher": "aes-xts-plain64",
8085
"hash": "sha256",
81-
"label": "root",
86+
"label": "USERDATA",
8287
"uuid": "<CRYPT_UUID>",
83-
"mname": "cryptroot",
88+
"mname": "userdata_crypt",
8489
"etype": "partitioned"
8590
},
8691
"partitions": [
8792
{
93+
"comment": "Encrypted user data - shared between slots",
8894
"image": "persistent"
8995
}
9096
]

0 commit comments

Comments
 (0)