Copy the previous (if existing) bootsector for dos mbr after fdisk_init_firstsector_buffer zeroed it. Signed-off-by: Jean-Loup 'clippix' Bogalho <clippix@xxxxxxxxxxxx> --- libfdisk/src/dos.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/libfdisk/src/dos.c b/libfdisk/src/dos.c index dce9829..91471cc 100644 --- a/libfdisk/src/dos.c +++ b/libfdisk/src/dos.c @@ -18,6 +18,7 @@ #define MAXIMUM_PARTS 60 #define ACTIVE_FLAG 0x80 +#define BOOTSECTOR_SIZE 446 /** * SECTION: dos @@ -636,6 +637,7 @@ static int dos_create_disklabel(struct fdisk_context *cxt) unsigned int id = 0; int rc, has_id = 0; struct fdisk_dos_label *l; + unsigned char *bootsector; assert(cxt); assert(cxt->label); @@ -659,9 +661,19 @@ static int dos_create_disklabel(struct fdisk_context *cxt) if (!has_id) random_get_bytes(&id, sizeof(id)); - rc = fdisk_init_firstsector_buffer(cxt); - if (rc) - return rc; + bootsector = malloc(sizeof(char) * BOOTSECTOR_SIZE); + if (cxt->firstsector && cxt->firstsector_bufsz == cxt->sector_size && + bootsector) { + memcpy(bootsector, cxt->firstsector, BOOTSECTOR_SIZE); + rc = fdisk_init_firstsector_buffer(cxt); + memcpy(cxt->firstsector, bootsector, BOOTSECTOR_SIZE); + free(bootsector); + } + else { + rc = fdisk_init_firstsector_buffer(cxt); + if (rc) + return rc; + } dos_init(cxt); l = self_label(cxt); -- Jean-Loup 'clippix' Bogalho LSE 2016 -- To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html