Skip to contentSkip to conversation

wic: add option to specify the diskid

PATCH In review Steffen Greber 3 participants 4 messages Oct 27, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 wic: add option to specify the diskid

This adds a feature to specify the disk ID when creating a disk with the wic tool. This is useful when using the DOS partition scheme and booting with root=PARTUUID=<partuuid>. In DOS partitions, the partition ID is <diskid>-<partition-number>, so it makes sense to let the user define the disk ID.

You can specify it in the kickstart file using the --diskid argument to the bootloader command. The value can be given in decimal or hexadecimal format (e.g. 3735928559 or 0xdeadbeef). If omitted, the previous behaviour does not change.

Add WIC tests and support setting GPT diskid

Add unit tests for wic.py to cover previous patch review.
Also extend implementation to allow defining the diskid for GPT partitions.

---
changes in v2:
- add diskid support for gpt tabels
- add wic tests as requested by the reviewer

---
 meta/lib/oeqa/selftest/cases/wic.py      | 36 ++++++++++++++++++++++++
 scripts/lib/wic/ksparser.py              | 20 +++++++++++++
 scripts/lib/wic/plugins/imager/direct.py | 20 +++++++------
 3 files changed, 68 insertions(+), 8 deletions(-)
meta/lib/oeqa/selftest/cases/wic.py +36 -0
index bb4ac23ebf..d7a9b14658 100644
@@ -1905,6 +1905,42 @@ INITRAMFS_IMAGE = "core-image-initramfs-boot"
             self.assertIn("Source parameter 'fill' only works with the '--fixed-size' option, exiting.", result.output)
             self.assertNotEqual(0, result.status)
 
+    def test_diskid_on_msdos_partition(self):
+        """Test diksid on msdos partions"""
+        img = 'core-image-minimal'
+        diskid = "0xdeadbbef"
+        with NamedTemporaryFile("w", suffix=".wks") as wks:
+            wks.writelines(['bootloader --ptable msdos --diskid %s\n' % diskid,
+                            'part /boot --size=100M --active --fstype=ext4 --label boot\n'
+                            'part /     --source rootfs      --fstype=ext4 --label root\n'])
+            wks.flush()
+            cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
+            runCmd(cmd)
+            wksname = os.path.splitext(os.path.basename(wks.name))[0]
+            out = glob(os.path.join(self.resultdir, "%s-*direct" % wksname))
+            self.assertEqual(1, len(out))
+            sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
+            result = runCmd("%s/usr/sbin/sfdisk -l %s | grep 'Disk identifier:'" % (sysroot, out[0]))
+            self.assertEqual("Disk identifier: %s" % diskid.lower(), result.output)
+
+    def test_diskid_on_gpt_partition(self):
+        """Test diksid on gpt partions"""
+        img = 'core-image-minimal'
+        diskid = "deadbeef-cafe-babe-f00d-cec2ea4eface"
+        with NamedTemporaryFile("w", suffix=".wks") as wks:
+            wks.writelines(['bootloader --ptable gpt --diskid %s\n' % diskid,
+                            'part /boot --size=100M --active --fstype=ext4 --label boot\n'
+                            'part /     --source rootfs      --fstype=ext4 --label root\n'])
+            wks.flush()
+            cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
+            runCmd(cmd)
+            wksname = os.path.splitext(os.path.basename(wks.name))[0]
+            out = glob(os.path.join(self.resultdir, "%s-*direct" % wksname))
+            self.assertEqual(1, len(out))
+            sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
+            result = runCmd("%s/usr/sbin/sfdisk -l %s | grep 'Disk identifier:'" % (sysroot, out[0]))
+            self.assertEqual("Disk identifier: %s" % diskid.upper(), result.output)
+
 class ModifyTests(WicTestCase):
     def test_wic_ls(self):
         """Test listing image content using 'wic ls'"""
scripts/lib/wic/ksparser.py +20 -0
index a762d3b6cf..4ccd70dc55 100644
@@ -16,6 +16,7 @@ import os
 import shlex
 import logging
 import re
+import uuid
 
 from argparse import ArgumentParser, ArgumentError, ArgumentTypeError
 
@@ -196,6 +197,7 @@ class KickStart():
         bootloader.add_argument('--configfile')
         bootloader.add_argument('--ptable', choices=('msdos', 'gpt', 'gpt-hybrid'),
                                 default='msdos')
+        bootloader.add_argument('--diskid')
         bootloader.add_argument('--timeout', type=int)
         bootloader.add_argument('--source')
 
@@ -296,6 +298,24 @@ class KickStart():
                             if append_var:
                                 self.bootloader.append = ' '.join(filter(None, \
                                                          (self.bootloader.append, append_var)))
+                            if parsed.diskid:
+                                if parsed.ptable == "msdos":
+                                    try:
+                                        self.bootloader.diskid = int(parsed.diskid, 0)
+                                    except ValueError:
+                                        err = "with --ptbale msdos only 32bit integers " \
+                                              "are allowed for --diskid. %s could not " \
+                                              "be parsed" % self.ptable
+                                        raise KickStartError(err)
+                                else:
+                                    try:
+                                        self.bootloader.diskid = uuid.UUID(parsed.diskid)
+                                    except ValueError:
+                                        err = "with --ptable %s only valid uuids are " \
+                                              "allowed for --diskid. %s could not be " \
+                                              "parsed" % (parsed.ptable, parsed.diskid)
+                                        raise KickStartError(err)
+
                         else:
                             err = "%s:%d: more than one bootloader specified" \
                                       % (confpath, lineno)
scripts/lib/wic/plugins/imager/direct.py +12 -8
index 6e1f1c8cba..ad922cfbf1 100644
@@ -76,7 +76,7 @@ class DirectPlugin(ImagerPlugin):
                 break
 
         image_path = self._full_path(self.workdir, self.parts[0].disk, "direct")
-        self._image = PartitionedImage(image_path, self.ptable_format,
+        self._image = PartitionedImage(image_path, self.ptable_format, self.ks.bootloader.diskid,
                                        self.parts, self.native_sysroot,
                                        options.extra_space)
 
@@ -302,7 +302,7 @@ class PartitionedImage():
     Partitioned image in a file.
     """
 
-    def __init__(self, path, ptable_format, partitions, native_sysroot=None, extra_space=0):
+    def __init__(self, path, ptable_format, disk_id, partitions, native_sysroot=None, extra_space=0):
         self.path = path  # Path to the image file
         self.numpart = 0  # Number of allocated partitions
         self.realpart = 0 # Number of partitions in the partition table
@@ -315,7 +315,16 @@ class PartitionedImage():
                           # all partitions (in bytes)
         self.ptable_format = ptable_format  # Partition table format
         # Disk system identifier
-        if os.getenv('SOURCE_DATE_EPOCH'):
+        if disk_id and ptable_format in ('gpt', 'gpt-hybrid'):
+            self.disk_guid = disk_id
+        elif os.getenv('SOURCE_DATE_EPOCH'):
+            self.disk_guid = uuid.UUID(int=int(os.getenv('SOURCE_DATE_EPOCH')))
+        else:
+            self.disk_guid = uuid.uuid4()
+
+        if disk_id and ptable_format == 'msdos':
+            self.identifier = disk_id
+        elif os.getenv('SOURCE_DATE_EPOCH'):
             self.identifier = random.Random(int(os.getenv('SOURCE_DATE_EPOCH'))).randint(1, 0xffffffff)
         else:
             self.identifier = random.SystemRandom().randint(1, 0xffffffff)
@@ -543,11 +552,6 @@ class PartitionedImage():
 
     def _write_disk_guid(self):
         if self.ptable_format in ('gpt', 'gpt-hybrid'):
-            if os.getenv('SOURCE_DATE_EPOCH'):
-                self.disk_guid = uuid.UUID(int=int(os.getenv('SOURCE_DATE_EPOCH')))
-            else:
-                self.disk_guid = uuid.uuid4()
-
             logger.debug("Set disk guid %s", self.disk_guid)
             sfdisk_cmd = "sfdisk --sector-size %s --disk-id %s %s" % \
                         (self.sector_size, self.path, self.disk_guid)
#1 lore raw
Quoted 13 lines · Steffen Greber

This adds a feature to specify the disk ID when creating a disk with the wic tool. This is useful when using the DOS partition scheme and booting with root=PARTUUID=<partuuid>. In DOS partitions, the partition ID is <diskid>-<partition-number>, so it makes sense to let the user define the disk ID.

You can specify it in the kickstart file using the --diskid argument to the bootloader command. The value can be given in decimal or hexadecimal format (e.g. 3735928559 or 0xdeadbeef). If omitted, the previous behaviour does not change.

Quoted 8 lines

Add WIC tests and support setting GPT diskid

Add unit tests for wic.py to cover previous patch review.
Also extend implementation to allow defining the diskid for GPT partitions.

---

Hi Steffen,

Thanks for the v2, but it looks like the v1 was merged. Can you rebase on master and make a separate patch for tests and GPT partitions handling?

Thanks,
Mathieu

Signature · 3 lines
Mathieu Dubois-Briand, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#2 lore raw

Sure, I can do. Btw I am asking myself if I should also add some documentation? Or will this be done by someone else?

regards

Steffen

#3 lore raw
Quoted 1 line · Steffen Greber via lists.openembedded.org

Sure, I can do. Btw I am asking myself if I should also add some documentation? Or will this be done by someone else?

It's better if you can send a patch for the docs as well.

Alex