* [PATCH v3 0/2] wic: extend empty plugin with options to write zeros to partiton
@ 2023-11-28 13:52 Lukas Funke
2023-11-28 13:52 ` [PATCH v3 1/2] " Lukas Funke
2023-11-28 13:52 ` [PATCH v3 2/2] selftest: wic: add test for zerorize option of empty plugin Lukas Funke
0 siblings, 2 replies; 3+ messages in thread
From: Lukas Funke @ 2023-11-28 13:52 UTC (permalink / raw)
To: openembedded-core; +Cc: Alexandre Belloni, Richard Purdie, Lukas Funke
From: Lukas Funke <lukas.funke@weidmueller.com>
Adds features to explicitly write zeros to the start of the
partition. This is useful to overwrite old content like
filesystem signatures which may be re-recognized otherwise.
The new features can be enabled with
'--soucreparams="[fill|size=<N>[S|s|K|k|M|G]][,][bs=<N>[S|s|K|k|M|G]]"'
Conflicting or missing options throw errors.
The features are:
- fill
Fill the entire partition with zeros. Requires '--fixed-size' option
to be set.
- size=<N>[S|s|K|k|M|G]
Set the first N bytes of the partition to zero. Default unit is 'K'.
- bs=<N>[S|s|K|k|M|G]
Write at most N bytes at a time during source file creation.
Defaults to '1M'. Default unit is 'K'.
Changes in v3:
- Add testcase
- Fix interpretation of --fixed-size parameter
Changed in v2:
- Added SoB
---
Lukas Funke (1):
selftest: wic: add test for zerorize option of empty plugin
Malte Schmidt (1):
wic: extend empty plugin with options to write zeros to partiton
meta/lib/oeqa/selftest/cases/wic.py | 36 ++++++++++++++++
scripts/lib/wic/plugins/source/empty.py | 57 ++++++++++++++++++++++++-
2 files changed, 92 insertions(+), 1 deletion(-)
--
2.30.2
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v3 1/2] wic: extend empty plugin with options to write zeros to partiton
2023-11-28 13:52 [PATCH v3 0/2] wic: extend empty plugin with options to write zeros to partiton Lukas Funke
@ 2023-11-28 13:52 ` Lukas Funke
2023-11-28 13:52 ` [PATCH v3 2/2] selftest: wic: add test for zerorize option of empty plugin Lukas Funke
1 sibling, 0 replies; 3+ messages in thread
From: Lukas Funke @ 2023-11-28 13:52 UTC (permalink / raw)
To: openembedded-core
Cc: Alexandre Belloni, Richard Purdie, Malte Schmidt, Lukas Funke
From: Malte Schmidt <malte.schmidt@weidmueller.com>
Adds features to explicitly write zeros to the start of the
partition. This is useful to overwrite old content like
filesystem signatures which may be re-recognized otherwise.
The new features can be enabled with
'--soucreparams="[fill|size=<N>[S|s|K|k|M|G]][,][bs=<N>[S|s|K|k|M|G]]"'
Conflicting or missing options throw errors.
The features are:
- fill
Fill the entire partition with zeros. Requires '--fixed-size' option
to be set.
- size=<N>[S|s|K|k|M|G]
Set the first N bytes of the partition to zero. Default unit is 'K'.
- bs=<N>[S|s|K|k|M|G]
Write at most N bytes at a time during source file creation.
Defaults to '1M'. Default unit is 'K'.
Signed-off-by: Malte Schmidt <malte.schmidt@weidmueller.com>
Signed-off-by: Lukas Funke <lukas.funke@weidmueller.com>
---
scripts/lib/wic/plugins/source/empty.py | 57 ++++++++++++++++++++++++-
1 file changed, 56 insertions(+), 1 deletion(-)
diff --git a/scripts/lib/wic/plugins/source/empty.py b/scripts/lib/wic/plugins/source/empty.py
index 9c492ca206..0a9f5fa27c 100644
--- a/scripts/lib/wic/plugins/source/empty.py
+++ b/scripts/lib/wic/plugins/source/empty.py
@@ -9,9 +9,19 @@
# To use it you must pass "empty" as argument for the "--source" parameter in
# the wks file. For example:
# part foo --source empty --ondisk sda --size="1024" --align 1024
+#
+# The plugin supports writing zeros to the start of the
+# partition. This is useful to overwrite old content like
+# filesystem signatures which may be re-recognized otherwise.
+# This feature can be enabled with
+# '--soucreparams="[fill|size=<N>[S|s|K|k|M|G]][,][bs=<N>[S|s|K|k|M|G]]"'
+# Conflicting or missing options throw errors.
import logging
+import os
+from wic import WicError
+from wic.ksparser import sizetype
from wic.pluginbase import SourcePlugin
logger = logging.getLogger('wic')
@@ -19,6 +29,16 @@ logger = logging.getLogger('wic')
class EmptyPartitionPlugin(SourcePlugin):
"""
Populate unformatted empty partition.
+
+ The following sourceparams are supported:
+ - fill
+ Fill the entire partition with zeros. Requires '--fixed-size' option
+ to be set.
+ - size=<N>[S|s|K|k|M|G]
+ Set the first N bytes of the partition to zero. Default unit is 'K'.
+ - bs=<N>[S|s|K|k|M|G]
+ Write at most N bytes at a time during source file creation.
+ Defaults to '1M'. Default unit is 'K'.
"""
name = 'empty'
@@ -31,4 +51,39 @@ class EmptyPartitionPlugin(SourcePlugin):
Called to do the actual content population for a partition i.e. it
'prepares' the partition to be incorporated into the image.
"""
- return
+ get_byte_count = sizetype('K', True)
+ size = 0
+
+ if 'fill' in source_params and 'size' in source_params:
+ raise WicError("Conflicting source parameters 'fill' and 'size' specified, exiting.")
+
+ # Set the size of the zeros to be written to the partition
+ if 'fill' in source_params:
+ if part.fixed_size == 0:
+ raise WicError("Source parameter 'fill' only works with the '--fixed-size' option, exiting.")
+ size = get_byte_count(part.fixed_size)
+ elif 'size' in source_params:
+ size = get_byte_count(source_params['size'])
+
+ if size == 0:
+ # Nothing to do, create empty partition
+ return
+
+ if 'bs' in source_params:
+ bs = get_byte_count(source_params['bs'])
+ else:
+ bs = get_byte_count('1M')
+
+ # Create a binary file of the requested size filled with zeros
+ source_file = os.path.join(cr_workdir, 'empty-plugin-zeros%s.bin' % part.lineno)
+ if not os.path.exists(os.path.dirname(source_file)):
+ os.makedirs(os.path.dirname(source_file))
+
+ quotient, remainder = divmod(size, bs)
+ with open(source_file, 'wb') as file:
+ for _ in range(quotient):
+ file.write(bytearray(bs))
+ file.write(bytearray(remainder))
+
+ part.size = (size + 1024 - 1) // 1024 # size in KB rounded up
+ part.source_file = source_file
--
2.30.2
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH v3 2/2] selftest: wic: add test for zerorize option of empty plugin
2023-11-28 13:52 [PATCH v3 0/2] wic: extend empty plugin with options to write zeros to partiton Lukas Funke
2023-11-28 13:52 ` [PATCH v3 1/2] " Lukas Funke
@ 2023-11-28 13:52 ` Lukas Funke
1 sibling, 0 replies; 3+ messages in thread
From: Lukas Funke @ 2023-11-28 13:52 UTC (permalink / raw)
To: openembedded-core; +Cc: Alexandre Belloni, Richard Purdie, Lukas Funke
From: Lukas Funke <lukas.funke@weidmueller.com>
Add test for empty plugin which tests whether the plugin creates
partitions with actual data which is 'zero'.
Signed-off-by: Lukas Funke <lukas.funke@weidmueller.com>
---
meta/lib/oeqa/selftest/cases/wic.py | 36 +++++++++++++++++++++++++++++
1 file changed, 36 insertions(+)
diff --git a/meta/lib/oeqa/selftest/cases/wic.py b/meta/lib/oeqa/selftest/cases/wic.py
index b4866bcb32..9672f3fae4 100644
--- a/meta/lib/oeqa/selftest/cases/wic.py
+++ b/meta/lib/oeqa/selftest/cases/wic.py
@@ -1391,6 +1391,42 @@ class Wic2(WicTestCase):
result = runCmd("%s/usr/sbin/sfdisk --part-label %s 3" % (sysroot, image_path))
self.assertEqual('ext-space', result.output)
+ def test_empty_zeroize_plugin(self):
+ img = 'core-image-minimal'
+ expected_size = [ 1024*1024, # 1M
+ 512*1024, # 512K
+ 2*1024*1024] # 2M
+ # Check combination of sourceparams
+ with NamedTemporaryFile("w", suffix=".wks") as wks:
+ wks.writelines(
+ ['part empty --source empty --sourceparams="fill" --ondisk sda --fixed-size 1M\n',
+ 'part empty --source empty --sourceparams="size=512K" --ondisk sda --size 1M --align 1024\n',
+ 'part empty --source empty --sourceparams="size=2048k,bs=512K" --ondisk sda --size 4M --align 1024\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]
+ wicout = glob(os.path.join(self.resultdir, "%s-*direct" % wksname))
+ # Skip the complete image and just look at the single partitions
+ for idx, value in enumerate(wicout[1:]):
+ self.logger.info(wicout[idx])
+ # Check if partitions are actually zeroized
+ with open(wicout[idx], mode="rb") as fd:
+ ba = bytearray(fd.read())
+ for b in ba:
+ self.assertEqual(b, 0)
+ self.assertEqual(expected_size[idx], os.path.getsize(wicout[idx]))
+
+ # Check inconsistancy check between "fill" and "--size" parameter
+ with NamedTemporaryFile("w", suffix=".wks") as wks:
+ wks.writelines(['part empty --source empty --sourceparams="fill" --ondisk sda --size 1M\n'])
+ wks.flush()
+ cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
+ result = runCmd(cmd, ignore_status=True)
+ self.assertIn("Source parameter 'fill' only works with the '--fixed-size' option, exiting.", result.output)
+ self.assertNotEqual(0, result.status)
+
class ModifyTests(WicTestCase):
def test_wic_ls(self):
"""Test listing image content using 'wic ls'"""
--
2.30.2
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-11-28 13:52 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-28 13:52 [PATCH v3 0/2] wic: extend empty plugin with options to write zeros to partiton Lukas Funke
2023-11-28 13:52 ` [PATCH v3 1/2] " Lukas Funke
2023-11-28 13:52 ` [PATCH v3 2/2] selftest: wic: add test for zerorize option of empty plugin Lukas Funke
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.