[meta-oe][PATCH v2 1/2] image_types_sparse: Pad source image to block size


Sean Anderson <sean.anderson@...>
 

If the source image's size is not aligned to the sparse image's block
size, then conversion will fail with

img2simg: libsparse/sparse.cpp:133: int write_all_blocks(sparse_file*, output_file*): Assertion `pad >= 0' failed.

This is a bug in img2simg, but an easy way to work around it is to pad
the source image ourselves. The default block size of 4096 matches
img2simg's default block size.

Signed-off-by: Sean Anderson <sean.anderson@...>
---

Changes in v2:
- New

meta-oe/classes/image_types_sparse.bbclass | 17 +++++++++++++----
1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/meta-oe/classes/image_types_sparse.bbclass b/meta-oe/classes/image_types_sparse.bbclass
index 4263593a8d..58221888ed 100644
--- a/meta-oe/classes/image_types_sparse.bbclass
+++ b/meta-oe/classes/image_types_sparse.bbclass
@@ -1,8 +1,17 @@
inherit image_types

+# This sets the granularity of the sparse image conversion. Chunk sizes will be
+# specified in units of this value. Setting this value smaller than the
+# underlying image's block size will not result in any further space saving.
+# However, there is no loss in correctness if this value is larger or smaller
+# than optimal. This value should be a power of two.
+SPARSE_BLOCK_SIZE ??= "4096"
+
+convert_sparse() {
+ truncate --no-create --size=%${SPARSE_BLOCK_SIZE} "$1"
+ img2simg "$1" "$1.sparse" ${SPARSE_BLOCK_SIZE}
+}
+
CONVERSIONTYPES += "sparse"
-CONVERSION_CMD:sparse = " \
- img2simg "${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.${type}" \
- "${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.${type}.sparse" \
-"
+CONVERSION_CMD:sparse = "convert_sparse '${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.${type}'"
CONVERSION_DEPENDS_sparse = "android-tools-native"
--
2.35.1.1320.gc452695387.dirty


Sean Anderson <sean.anderson@...>
 

On 5/16/22 12:52 PM, Sean Anderson wrote:
If the source image's size is not aligned to the sparse image's block
size, then conversion will fail with

img2simg: libsparse/sparse.cpp:133: int write_all_blocks(sparse_file*, output_file*): Assertion `pad >= 0' failed.

This is a bug in img2simg, but an easy way to work around it is to pad
the source image ourselves. The default block size of 4096 matches
img2simg's default block size.

Signed-off-by: Sean Anderson <sean.anderson@...>
---

Changes in v2:
- New

meta-oe/classes/image_types_sparse.bbclass | 17 +++++++++++++----
1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/meta-oe/classes/image_types_sparse.bbclass b/meta-oe/classes/image_types_sparse.bbclass
index 4263593a8d..58221888ed 100644
--- a/meta-oe/classes/image_types_sparse.bbclass
+++ b/meta-oe/classes/image_types_sparse.bbclass
@@ -1,8 +1,17 @@
inherit image_types

+# This sets the granularity of the sparse image conversion. Chunk sizes will be
+# specified in units of this value. Setting this value smaller than the
+# underlying image's block size will not result in any further space saving.
+# However, there is no loss in correctness if this value is larger or smaller
+# than optimal. This value should be a power of two.
+SPARSE_BLOCK_SIZE ??= "4096"
+
+convert_sparse() {
It looks like this could be specified directly like

CONVERSION_CMD:sparse() {
...
}

This will be fixed in v3.

--Sean

+ truncate --no-create --size=%${SPARSE_BLOCK_SIZE} "$1"
+ img2simg "$1" "$1.sparse" ${SPARSE_BLOCK_SIZE}
+}
+
CONVERSIONTYPES += "sparse"
-CONVERSION_CMD:sparse = " \
- img2simg "${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.${type}" \
- "${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.${type}.sparse" \
-"
+CONVERSION_CMD:sparse = "convert_sparse '${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.${type}'"
CONVERSION_DEPENDS_sparse = "android-tools-native"


Sean Anderson <sean.anderson@...>
 

On 5/16/22 1:06 PM, Sean Anderson wrote:


On 5/16/22 12:52 PM, Sean Anderson wrote:
If the source image's size is not aligned to the sparse image's block
size, then conversion will fail with

img2simg: libsparse/sparse.cpp:133: int write_all_blocks(sparse_file*, output_file*): Assertion `pad >= 0' failed.

This is a bug in img2simg, but an easy way to work around it is to pad
the source image ourselves. The default block size of 4096 matches
img2simg's default block size.

Signed-off-by: Sean Anderson <sean.anderson@...>
---

Changes in v2:
- New

meta-oe/classes/image_types_sparse.bbclass | 17 +++++++++++++----
1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/meta-oe/classes/image_types_sparse.bbclass b/meta-oe/classes/image_types_sparse.bbclass
index 4263593a8d..58221888ed 100644
--- a/meta-oe/classes/image_types_sparse.bbclass
+++ b/meta-oe/classes/image_types_sparse.bbclass
@@ -1,8 +1,17 @@
inherit image_types

+# This sets the granularity of the sparse image conversion. Chunk sizes will be
+# specified in units of this value. Setting this value smaller than the
+# underlying image's block size will not result in any further space saving.
+# However, there is no loss in correctness if this value is larger or smaller
+# than optimal. This value should be a power of two.
+SPARSE_BLOCK_SIZE ??= "4096"
+
+convert_sparse() {
It looks like this could be specified directly like

CONVERSION_CMD:sparse() {
...
}

This will be fixed in v3.

--Sean
Actually, it seems like this only works with underscore override syntax.
Something like CONVERSION_CMD_sparse() { ... } works, but the above
example fails with

`CONVERSION_CMD:sparse': not a valid identifier

so this revision of the patch is OK as-is.

--Sean


Sean Anderson <sean.anderson@...>
 

On 5/16/22 12:52 PM, Sean Anderson wrote:
If the source image's size is not aligned to the sparse image's block
size, then conversion will fail with

img2simg: libsparse/sparse.cpp:133: int write_all_blocks(sparse_file*, output_file*): Assertion `pad >= 0' failed.

This is a bug in img2simg, but an easy way to work around it is to pad
the source image ourselves. The default block size of 4096 matches
img2simg's default block size.

Signed-off-by: Sean Anderson <sean.anderson@...>
---

Changes in v2:
- New

meta-oe/classes/image_types_sparse.bbclass | 17 +++++++++++++----
1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/meta-oe/classes/image_types_sparse.bbclass b/meta-oe/classes/image_types_sparse.bbclass
index 4263593a8d..58221888ed 100644
--- a/meta-oe/classes/image_types_sparse.bbclass
+++ b/meta-oe/classes/image_types_sparse.bbclass
@@ -1,8 +1,17 @@
inherit image_types

+# This sets the granularity of the sparse image conversion. Chunk sizes will be
+# specified in units of this value. Setting this value smaller than the
+# underlying image's block size will not result in any further space saving.
+# However, there is no loss in correctness if this value is larger or smaller
+# than optimal. This value should be a power of two.
+SPARSE_BLOCK_SIZE ??= "4096"
+
+convert_sparse() {
+ truncate --no-create --size=%${SPARSE_BLOCK_SIZE} "$1"
+ img2simg "$1" "$1.sparse" ${SPARSE_BLOCK_SIZE}
+}
+
CONVERSIONTYPES += "sparse"
-CONVERSION_CMD:sparse = " \
- img2simg "${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.${type}" \
- "${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.${type}.sparse" \
-"
+CONVERSION_CMD:sparse = "convert_sparse '${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.${type}'"
CONVERSION_DEPENDS_sparse = "android-tools-native"
ping?


Khem Raj
 

On Fri, Aug 5, 2022 at 2:50 PM Sean Anderson <sean.anderson@...> wrote:

On 5/16/22 12:52 PM, Sean Anderson wrote:
If the source image's size is not aligned to the sparse image's block
size, then conversion will fail with

img2simg: libsparse/sparse.cpp:133: int write_all_blocks(sparse_file*, output_file*): Assertion `pad >= 0' failed.

This is a bug in img2simg, but an easy way to work around it is to pad
the source image ourselves. The default block size of 4096 matches
img2simg's default block size.

Signed-off-by: Sean Anderson <sean.anderson@...>
---

Changes in v2:
- New

meta-oe/classes/image_types_sparse.bbclass | 17 +++++++++++++----
1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/meta-oe/classes/image_types_sparse.bbclass b/meta-oe/classes/image_types_sparse.bbclass
index 4263593a8d..58221888ed 100644
--- a/meta-oe/classes/image_types_sparse.bbclass
+++ b/meta-oe/classes/image_types_sparse.bbclass
@@ -1,8 +1,17 @@
inherit image_types

+# This sets the granularity of the sparse image conversion. Chunk sizes will be
+# specified in units of this value. Setting this value smaller than the
+# underlying image's block size will not result in any further space saving.
+# However, there is no loss in correctness if this value is larger or smaller
+# than optimal. This value should be a power of two.
+SPARSE_BLOCK_SIZE ??= "4096"
+
+convert_sparse() {
+ truncate --no-create --size=%${SPARSE_BLOCK_SIZE} "$1"
+ img2simg "$1" "$1.sparse" ${SPARSE_BLOCK_SIZE}
+}
+
CONVERSIONTYPES += "sparse"
-CONVERSION_CMD:sparse = " \
- img2simg "${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.${type}" \
- "${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.${type}.sparse" \
-"
+CONVERSION_CMD:sparse = "convert_sparse '${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.${type}'"
CONVERSION_DEPENDS_sparse = "android-tools-native"
ping?
Thanks for reminder, Can you rebase it on latest master-next and resend please ?