image-squashfs: handle image->size appropriately

I was trying to actually make use of genimage's ability to split a
root filesystem into a read-only part stored in a squashfs, and a
writable /home partition in an ext4 image. Unfortunately, the squashfs
didn't end up included in the sd image:

INFO: hdimage(...): adding partition 'rootfs-A' (in MBR) from 'rootfs.squashfs' ...
INFO: hdimage(...): adding partition 'rootfs-B' (in MBR) from 'rootfs.squashfs' ...

INFO: hdimage(...): adding partition 'home' (in MBR) from 'home.ext4' ...
DEBUG: hdimage(...): copying 524288000 bytes from .../genimage-tmp/home.ext4 at offset 1577058304

I.e., there's no "copying xxx bytes from" lines following the "adding
partition rootfs" lines. That's due to

		if (child->size == 0)
			continue;

around line 457 in image-hd.c.

For a squashfs, we don't know the final size of the image, and we
should not be forced to decide upfront how large it can be. So
interpret a squashfs image node's size, when given, as a maximum
size. So if a size has been set and the generated image turns out to
be larger, make it an error.

Otherwise, that is, when the size is given and >= the generated
image, or when the size is not given explicitly, set image->size to
the size of the generated file.

There is one small problem: Consumers, e.g. image-hd.c, may have done
a sanity check (part size v child image size) during the setup phase,
but we don't (and can't) set the final size until the generate
phase. The next patch will fix that by repeating the sanity check
during generate.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
This commit is contained in:
Rasmus Villemoes 2021-11-26 14:25:27 +01:00
parent ec44ae086c
commit 7cc8078c41

View File

@ -19,6 +19,9 @@
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "genimage.h"
@ -28,6 +31,9 @@ static int squash_generate(struct image *image)
char compression[128];
char *comp_setup = cfg_getstr(image->imagesec, "compression");
unsigned block_size = cfg_getint_suffix(image->imagesec, "block-size");
unsigned long long file_size;
struct stat sb;
int ret;
/*
* 'mksquashfs' currently defaults to 'gzip' compression. Provide a shortcut
@ -40,11 +46,31 @@ static int squash_generate(struct image *image)
else
snprintf(compression, sizeof(compression), "-comp %s", comp_setup);
return systemp(image, "%s '%s' '%s' -b %u -noappend %s %s",
get_opt("mksquashfs"),
mountpath(image), /* source dir */
imageoutfile(image), /* destination file */
block_size, compression, extraargs);
ret = systemp(image, "%s '%s' '%s' -b %u -noappend %s %s",
get_opt("mksquashfs"),
mountpath(image), /* source dir */
imageoutfile(image), /* destination file */
block_size, compression, extraargs);
if (ret)
return ret;
ret = stat(imageoutfile(image), &sb);
if (ret) {
ret = -errno;
image_error(image, "stat(%s) failed: %s\n", imageoutfile(image), strerror(errno));
return ret;
}
file_size = sb.st_size;
if (image->size && file_size > image->size) {
image_error(image, "generated image %s is larger than given image size (%llu v %llu)\n",
imageoutfile(image), file_size, image->size);
return -E2BIG;
}
image_debug(image, "setting image size to %llu bytes\n", file_size);
image->size = file_size;
return 0;
}
/**