Skip to contentSkip to conversation

runqemu: avoid setting "mem=" when using a DTB

PATCH New Jon Mason 1 participant 2 messages Nov 13, 2025

lore
Share & tools
lore

Jon Mason

author
#0 lore raw

PATCH v4 1/2 runqemu: avoid setting "mem=" when using a DTB

Some emulated machines, such as the Raspberry Pi models, fail to boot if a fixed "mem=" value is passed on the kernel command line. When a DTB is used (QB_DTB is set), the memory size is already defined there, so specifying it again is unnecessary and can cause issues.

Skip adding "mem=" to the kernel command line when QB_DTB is present.

---
 scripts/runqemu | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/runqemu +1 -1
index 2be7a0f2869f..370b45578137 100755
@@ -884,7 +884,7 @@ to your build configuration.
         self.set('QB_MEM', qb_mem)
 
         mach = self.get('MACHINE')
-        if not mach.startswith(('qemumips', 'qemux86', 'qemuloongarch64')):
+        if not mach.startswith(('qemumips', 'qemux86', 'qemuloongarch64')) and self.get('QB_DTB') == "":
             self.kernel_cmdline_script += ' mem=%s' % self.get('QB_MEM').replace('-m','').strip() + 'M'
 
         self.qemu_opt_script += ' %s' % self.get('QB_MEM')
Signature · 1 line
2.50.1 (Apple Git-155)
author
#1 lore raw

PATCH v4 2/2 runqemu: resize rootfs image to power of 2 for SD or pflash

QEMU requires that SD and pflash images are sized to be a power of 2 (e.g., 32M, 64M, etc). So, if the image being used is not a power of 2 and it's being used for SD or pflash, increase it to the next power of 2 size via the truncate command.

This might not be an actual spec requirement, and is being investigated in https://gitlab.com/qemu-project/qemu/-/issues/1754

---
 scripts/runqemu | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
scripts/runqemu +21 -0
index 370b45578137..5bce021d8167 100755
@@ -421,6 +421,26 @@ class BaseConfig(object):
         else:
             raise RunQemuError("Unknown path arg %s" % p)
 
+    def rootfs_fixups(self):
+        """Address or workaround any rootfs issues.  Currently, only resize the rootfs image (if needed)"""
+        qb_rootfs_opt = self.get('QB_ROOTFS_OPT')
+
+        # Check if sdcard size is a power of 2, as that is currently a requirement for qemu
+        # See https://gitlab.com/qemu-project/qemu/-/issues/1754
+        rootfs_size = os.path.getsize(self.rootfs)
+        rootfs_size_pwr2 = 1 << (rootfs_size - 1).bit_length()
+        if ("if=sd" in qb_rootfs_opt or "if=pflash" in qb_rootfs_opt) and rootfs_size != rootfs_size_pwr2:
+            logger.info("Using sd-card or pflash and is not power of 2. File size %d, power of 2 size %d" %(rootfs_size, rootfs_size_pwr2))
+            logger.info("Attempting to use truncate to correct this.")
+
+            # Ensure the 'truncate' tool is installed before attempting to make a power of 2.
+            if not shutil.which('truncate'):
+                raise RunQemuError(f"'truncate' is required to make {self.rootfs} a power of 2 in size but was not found in PATH")
+            try:
+                subprocess.check_call(['truncate', '-s', str(rootfs_size_pwr2), self.rootfs])
+            except subprocess.CalledProcessError as e:
+                raise RunQemuError(f"Failed to make {self.rootfs} power of 2 in size: {e}")
+
     def uncompress_rootfs(self):
         """Decompress ZST rootfs image if needed"""
         if not self.rootfs or not self.fstype.endswith('.zst'):
@@ -1784,6 +1804,7 @@ def main():
         config.read_qemuboot()
         config.check_and_set()
         config.uncompress_rootfs()
+        config.rootfs_fixups()
         # Check whether the combos is valid or not
         config.validate_combos()
         config.print_config()
Signature · 1 line
2.50.1 (Apple Git-155)