summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Olbrich <m.olbrich@pengutronix.de>2019-05-11 14:06:22 +0000
committerGitHub <noreply@github.com>2019-05-11 14:06:22 +0000
commitf05d2b52920d8c32121f3a9db676b024fbcd495c (patch)
tree5161973217b54fd45ab70fad7688bc73e037eac2
parent3c58ac895037c047134419e2baa8cf533eb86694 (diff)
parent3af6d35b01343b9adbae51ef179fe9bc6817a7ec (diff)
downloadgenimage-f05d2b52920d8c32121f3a9db676b024fbcd495c.tar.gz
genimage-f05d2b52920d8c32121f3a9db676b024fbcd495c.tar.xz
Merge pull request #62 from michaelolbrich/hd-fill
image-hd: add option to extend the image to the full size
-rw-r--r--genimage.h1
-rw-r--r--image-hd.c11
-rwxr-xr-xtest/basic-images.test2
-rw-r--r--test/hdimage.config1
-rw-r--r--util.c43
5 files changed, 57 insertions, 1 deletions
diff --git a/genimage.h b/genimage.h
index 1ebe1cd..eabcc46 100644
--- a/genimage.h
+++ b/genimage.h
@@ -143,6 +143,7 @@ int pad_file(struct image *image, const char *infile,
size_t size, unsigned char fillpattern, enum pad_mode mode);
int insert_data(struct image *image, const char *data, const char *outfile,
size_t size, long offset);
+int extend_file(struct image *image, size_t size);
int reload_partitions(struct image *image);
unsigned long long cfg_getint_suffix(cfg_t *sec, const char *name);
diff --git a/image-hd.c b/image-hd.c
index 90f550e..204d601 100644
--- a/image-hd.c
+++ b/image-hd.c
@@ -33,6 +33,7 @@ struct hdimage {
uint32_t disksig;
const char *disk_uuid;
cfg_bool_t gpt;
+ cfg_bool_t fill;
};
struct mbr_partition_entry {
@@ -379,6 +380,14 @@ static int hdimage_generate(struct image *image)
}
}
+ if (hd->fill) {
+ ret = extend_file(image, image->size);
+ if (ret) {
+ image_error(image, "failed to fill the image.\n");
+ return ret;
+ }
+ }
+
if (hd->partition_table) {
if (hd->gpt) {
ret = hdimage_insert_gpt(image, &image->partitions);
@@ -414,6 +423,7 @@ static int hdimage_setup(struct image *image, cfg_t *cfg)
hd->extended_partition = cfg_getint(cfg, "extended-partition");
hd->disksig = strtoul(cfg_getstr(cfg, "disk-signature"), NULL, 0);
hd->gpt = cfg_getbool(cfg, "gpt");
+ hd->fill = cfg_getbool(cfg, "fill");
hd->disk_uuid = cfg_getstr(cfg, "disk-uuid");
if (hd->extended_partition > 4) {
@@ -585,6 +595,7 @@ cfg_opt_t hdimage_opts[] = {
CFG_BOOL("partition-table", cfg_true, CFGF_NONE),
CFG_INT("extended-partition", 0, CFGF_NONE),
CFG_BOOL("gpt", cfg_false, CFGF_NONE),
+ CFG_BOOL("fill", cfg_false, CFGF_NONE),
CFG_END()
};
diff --git a/test/basic-images.test b/test/basic-images.test
index cf6d68b..4554b7a 100755
--- a/test/basic-images.test
+++ b/test/basic-images.test
@@ -198,7 +198,7 @@ exec_test_set_prereq sfdisk
test_expect_success fdisk,sfdisk "hdimage" "
setup_test_images &&
run_genimage hdimage.config test.hdimage &&
- check_size images/test.hdimage 9442816 &&
+ check_size images/test.hdimage 10485760 &&
# check the this identifier
fdisk -l images/test.hdimage | grep identifier: > hdimage.fdisk &&
# check partitions; filter output to handle different sfdisk versions
diff --git a/test/hdimage.config b/test/hdimage.config
index 117e69f..09978c4 100644
--- a/test/hdimage.config
+++ b/test/hdimage.config
@@ -1,6 +1,7 @@
image test.hdimage {
hdimage {
align = 1M
+ fill = true
disk-signature = 0x12345678
}
partition part1 {
diff --git a/util.c b/util.c
index bccd3dd..f659548 100644
--- a/util.c
+++ b/util.c
@@ -479,6 +479,49 @@ err_out:
return ret;
}
+int extend_file(struct image *image, size_t size)
+{
+ const char *outfile = imageoutfile(image);
+ char buf = '\0';
+ int f;
+ off_t offset;
+ int ret = 0;
+
+ f = open_file(image, outfile, 0);
+ if (f < 0)
+ return f;
+
+ offset = lseek(f, 0, SEEK_END);
+ if (offset < 0) {
+ ret = -errno;
+ image_error(image, "seek: %s\n", strerror(errno));
+ goto out;
+ }
+ if ((size_t)offset > size) {
+ ret = -EINVAL;
+ image_error(image, "output file is larger than requested size\n");
+ goto out;
+ }
+ if ((size_t)offset == size)
+ goto out;
+
+ if (lseek(f, size - 1, SEEK_SET) < 0) {
+ ret = -errno;
+ image_error(image, "seek %s: %s\n", outfile, strerror(errno));
+ goto out;
+ }
+ ret = write(f, &buf, 1);
+ if (ret < 1) {
+ ret = -errno;
+ image_error(image, "write %s: %s\n", outfile, strerror(errno));
+ goto out;
+ }
+ ret = 0;
+out:
+ close(f);
+ return ret;
+}
+
int uuid_validate(const char *str)
{
int i;