Skip to content

Commit

Permalink
Merge pull request #59 from michaelolbrich/autoresize
Browse files Browse the repository at this point in the history
hdimge: allow the last partition to fill the rest of the image
  • Loading branch information
michaelolbrich authored May 11, 2019
2 parents 28152c2 + 1ff8c61 commit e685816
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 31 deletions.
12 changes: 8 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,16 @@ Partition options:

:offset: The offset of this partition as a total offset to the beginning
of the device.
:size: The size of this partition in bytes. The last partition may have
size 0 to make this partition use the rest of the available space
on the device.
:size: The size of this partition in bytes. If the size and
autoresize are both not set then the size of the partition
image is used.
:partition-type: Used by dos partition tables to specify the partition type.
:image: The image file this partition shall be filled with
:autoresize: used by ubi (FIXME: do we need this? Isn't size = 0 enough)
:autoresize: Boolean specifying that the partition should be resized
automatically. For UBI volumes this means that the
``autoresize`` flag is set. Only one volume can have this flag.
For hd images this can be used for the last partition. If set
the partition will fill the remaining space of the image.
:bootable: Boolean specifying whether to set the bootable flag.
:in-partition-table: Boolean specifying whether to include this partition in
the partition table.
Expand Down
73 changes: 47 additions & 26 deletions image-hd.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ static unsigned long long roundup(unsigned long long value, unsigned long long a
static int hdimage_setup(struct image *image, cfg_t *cfg)
{
struct partition *part;
int has_extended;
int has_extended, autoresize = 0;
unsigned int partition_table_entries = 0;
unsigned long long now = 0;
struct hdimage *hd = xzalloc(sizeof(*hd));
Expand Down Expand Up @@ -448,33 +448,15 @@ static int hdimage_setup(struct image *image, cfg_t *cfg)

partition_table_entries = 0;
list_for_each_entry(part, &image->partitions, list) {
if (part->image) {
struct image *child = image_get(part->image);
if (!child) {
image_error(image, "could not find %s\n",
part->image);
return -EINVAL;
}
if (!part->size) {
if (part->in_partition_table)
part->size = roundup(child->size, hd->align);
else
part->size = child->size;
} else if (child->size > part->size) {
image_error(image, "part %s size (%lld) too small for %s (%lld)\n",
part->name, part->size, child->file, child->size);
return -EINVAL;
}
}
if (!part->size) {
image_error(image, "part %s size must not be zero\n",
part->name);
if (autoresize) {
image_error(image, "'autoresize' is only supported "
"for the last partition\n");
return -EINVAL;
}
if (part->in_partition_table && (part->size % 512)) {
image_error(image, "part %s size (%lld) must be a "
"multiple of 1 sector (512 bytes)\n",
part->name, part->size);
autoresize = part->autoresize;
if (autoresize && image->size == 0) {
image_error(image, "the images size must be specified "
"when using a 'autoresize' partition\n");
return -EINVAL;
}
if (hd->gpt) {
Expand Down Expand Up @@ -538,6 +520,45 @@ static int hdimage_setup(struct image *image, cfg_t *cfg)
}
part->offset = roundup(now, hd->align);
}
if (autoresize) {
long long partsize = image->size - now;
if (hd->gpt)
partsize -= GPT_SECTORS * 512;
if (partsize < 0) {
image_error(image, "partitions exceed device size\n");
return -EINVAL;
}
part->size = partsize;
}
if (part->image) {
struct image *child = image_get(part->image);
if (!child) {
image_error(image, "could not find %s\n",
part->image);
return -EINVAL;
}
if (!part->size) {
if (part->in_partition_table)
part->size = roundup(child->size, hd->align);
else
part->size = child->size;
} else if (child->size > part->size) {
image_error(image, "part %s size (%lld) too small for %s (%lld)\n",
part->name, part->size, child->file, child->size);
return -EINVAL;
}
}
if (!part->size) {
image_error(image, "part %s size must not be zero\n",
part->name);
return -EINVAL;
}
if (part->in_partition_table && (part->size % 512)) {
image_error(image, "part %s size (%lld) must be a "
"multiple of 1 sector (512 bytes)\n",
part->name, part->size);
return -EINVAL;
}
now = part->offset + part->size;
}

Expand Down
3 changes: 2 additions & 1 deletion test/hdimage.config
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ image test.hdimage-2 {
}
partition part6 {
image = "part2.img"
size = 1M
autoresize = true
partition-type = 0x83
}
size = 12M
}

0 comments on commit e685816

Please sign in to comment.