Skip to content

Commit

Permalink
Merge pull request #62 from michaelolbrich/hd-fill
Browse files Browse the repository at this point in the history
image-hd: add option to extend the image to the full size
  • Loading branch information
michaelolbrich authored May 11, 2019
2 parents 3c58ac8 + 3af6d35 commit f05d2b5
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions genimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions image-hd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
};

Expand Down
2 changes: 1 addition & 1 deletion test/basic-images.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/hdimage.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
image test.hdimage {
hdimage {
align = 1M
fill = true
disk-signature = 0x12345678
}
partition part1 {
Expand Down
43 changes: 43 additions & 0 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,49 @@ int insert_data(struct image *image, const char *data, const char *outfile,
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;
Expand Down

0 comments on commit f05d2b5

Please sign in to comment.