From: Robert Pengelly Date: Sat, 22 Mar 2025 11:00:08 +0000 (+0000) Subject: Explain why a disk image needs to be way bigger than it should be X-Git-Url: https://git.candlhat.org/?a=commitdiff_plain;h=d3c50eea6da9e3419ed4746337da4f3a1daff8e8;p=parted.git Explain why a disk image needs to be way bigger than it should be --- diff --git a/parted.c b/parted.c index 2c3bf27..a369b38 100644 --- a/parted.c +++ b/parted.c @@ -24,6 +24,7 @@ static long cylinders = 0; static long heads_per_cylinder = 0; static long sectors_per_track = 0; +static long sectors_per_cylinder = 0; struct parted_state *state = 0; const char *program_name = 0; @@ -146,12 +147,32 @@ static void calculate_geometry (void) { cylinders++; - state->image_size = ((cylinders * heads_per_cylinder) * sectors_per_track); - state->image_size += ((heads_per_cylinder - 1) * sectors_per_track); +#if 0 + /* This works for PCem and 86box but not for qemu. */ + state->image_size = cylinders * heads_per_cylinder; + state->image_size = (state->image_size * sectors_per_track) * 512; + +#else + + /** + * Ideally we'd have a new size of 41,954,304 for the image as Pcem and + * 86box reports 81940 sectors and times by 512 equals 41,953,280 add 512 + * bytes for the mbr and 512 for the VHD footer however qemu seems to report + * different disk geometry compared to PCem and 86box resuling to 81900 + * sectors being reported instead so we need to make the image way bigger + * than it needs to be to accommodate qemu. + */ + state->image_size = (cylinders * heads_per_cylinder) * sectors_per_track; + + state->image_size = state->image_size + ((heads_per_cylinder - 1) * sectors_per_track); state->image_size = (state->image_size + (sectors_per_track - 1)) * 512; + +#endif } + + sectors_per_cylinder = heads_per_cylinder * sectors_per_track; } @@ -251,7 +272,7 @@ int main (int argc, char **argv) { struct mbr mbr; long p, sector = 1; - long sectors_per_cylinder, start, end; + long start, end; if (argc && *argv) { @@ -321,7 +342,6 @@ int main (int argc, char **argv) { write741_to_byte_array (mbr.signature, generate_signature (), 1); calculate_geometry (); - sectors_per_cylinder = heads_per_cylinder * sectors_per_track; for (p = 0; p < state->nb_parts; ++p) {