Open lvps opened 1 day ago
- Count how many elements with parttype != null are there and use that as the number
Could that be a problem with unallocated/borked partitions? Sometimes partition numbers can get skipped in /dev/xxx
Ideally we shouldn't have borked partitions on the golden image that has been just written (if there are any, we have much bigger problems). Unallocated shouldn't be a problem either, parttype is part of GPT or MBR table, it's not read from the filesystem or similar.
Here's a test with an unformatted partition:
dd if=/dev/zero of=test.img bs=4M count=1
cgdisk test.img # Create a some GPT partitions with empty space and random types
sudo losetup -fP test.img
lsblk -J -o PATH,PARTN,PARTTYPE /dev/loop0
{
"blockdevices": [
{
"path": "/dev/loop0",
"partn": null,
"parttype": null
},{
"path": "/dev/loop0p1",
"partn": 1,
"parttype": "0fc63daf-8483-4772-8e79-3d69d8477de4"
},{
"path": "/dev/loop0p2",
"partn": 2,
"parttype": "0fc63daf-8483-4772-8e79-3d69d8477de4"
},{
"path": "/dev/loop0p3",
"partn": 3,
"parttype": "0fc63daf-8483-4772-8e79-3d69d8477de4"
},{
"path": "/dev/loop0p4",
"partn": 4,
"parttype": "0657fd6d-a4ab-43c4-84e5-0933c84b4f4f"
}
]
}
4 partitions with parttype != null, last partition is number 4
Let's try with MBR:
sudo losetup -d /dev/loop0
dd if=/dev/zero of=test.img bs=4M count=1
fdisk test.img # Create some partitions
sudo losetup -fP test.img
lsblk -J -o PATH,PARTN,PARTTYPE /dev/loop0
{
"blockdevices": [
{
"path": "/dev/loop0",
"partn": null,
"parttype": null
},{
"path": "/dev/loop0p1",
"partn": 1,
"parttype": "0x83"
},{
"path": "/dev/loop0p2",
"partn": 2,
"parttype": "0x6"
},{
"path": "/dev/loop0p3",
"partn": 3,
"parttype": "0x8e"
}
]
}
3 partitions with parttype != null, last partition is number 3
There's nothing out of the ordinary in the output. I also tested MBR logical & extended partitions and that requires some special care when handling... but we don't use them, nobody uses them anymore, there's no point in supporting that.
So it should be safe to fall back to counting partitions. If you want to be super mega ultra hyper extra safe, you can add PTTYPE to the output and if it's "dos" (MBR) try to handle logical partitions, but it's not worth the effort.
Restore PARTN that was removed in https://github.com/WEEE-Open/pesto/commit/24f2e0b85a7875fb2ea2d6a25c517257eec68276, then do one or more of the following when PARTN is not available: