diff --git a/fdisks/fdisk.c b/fdisks/fdisk.c index 2b0f91a82..997ee946c 100644 --- a/fdisks/fdisk.c +++ b/fdisks/fdisk.c @@ -1515,7 +1515,7 @@ expert_command_prompt(struct fdisk_context *cxt) fix_partition_table_order(); break; case 'g': - create_sgilabel(cxt); + fdisk_create_disklabel(cxt, "sgi"); break; case 'h': user_heads = cxt->geom.heads = read_int(cxt, 1, cxt->geom.heads, 256, 0, @@ -1742,7 +1742,7 @@ static void command_prompt(struct fdisk_context *cxt) new_partition(cxt); break; case 'o': - create_doslabel(cxt); + fdisk_create_disklabel(cxt, "dos"); break; case 'p': list_table(cxt, 0); @@ -1750,7 +1750,7 @@ static void command_prompt(struct fdisk_context *cxt) case 'q': handle_quit(cxt); case 's': - create_sunlabel(cxt); + fdisk_create_disklabel(cxt, "sun"); break; case 't': change_sysid(cxt); @@ -1915,7 +1915,7 @@ int main(int argc, char **argv) if (!fdisk_dev_has_disklabel(cxt)) { fprintf(stderr, _("Device does not contain a recognized partition table\n")); - fdisk_create_default_disklabel(cxt); + fdisk_create_disklabel(cxt, NULL); } command_prompt(cxt); diff --git a/fdisks/fdisk.h b/fdisks/fdisk.h index 7f85020f1..195db190e 100644 --- a/fdisks/fdisk.h +++ b/fdisks/fdisk.h @@ -141,6 +141,8 @@ struct fdisk_label { int (*write)(struct fdisk_context *cxt); /* verify the partition table */ int (*verify)(struct fdisk_context *cxt); + /* create new disk label */ + int (*create)(struct fdisk_context *cxt); /* new partition */ void (*part_add)(struct fdisk_context *cxt, int partnum, int parttype); /* delete partition */ @@ -167,11 +169,11 @@ extern int fdisk_context_force_sector_size(struct fdisk_context *cxt, sector_t s extern int fdisk_context_set_user_geometry(struct fdisk_context *cxt, unsigned int cylinders, unsigned int heads, unsigned int sectors); -extern int fdisk_create_default_disklabel(struct fdisk_context *cxt); extern int fdisk_delete_partition(struct fdisk_context *cxt, int partnum); extern int fdisk_add_partition(struct fdisk_context *cxt, int partnum, int parttype); extern int fdisk_write_disklabel(struct fdisk_context *cxt); extern int fdisk_verify_disklabel(struct fdisk_context *cxt); +extern int fdisk_create_disklabel(struct fdisk_context *cxt, const char *name); /* prototypes for fdisk.c */ extern char *disk_device, *line_ptr; diff --git a/fdisks/fdiskaixlabel.c b/fdisks/fdiskaixlabel.c index c692b0848..884baa6ce 100644 --- a/fdisks/fdiskaixlabel.c +++ b/fdisks/fdiskaixlabel.c @@ -80,6 +80,7 @@ const struct fdisk_label aix_label = .probe = aix_probe_label, .write = NULL, .verify = NULL, + .create = NULL, .part_add = aix_add_partition, .part_delete = NULL, }; diff --git a/fdisks/fdiskbsdlabel.c b/fdisks/fdiskbsdlabel.c index 3492aa83c..f5446a706 100644 --- a/fdisks/fdiskbsdlabel.c +++ b/fdisks/fdiskbsdlabel.c @@ -62,7 +62,6 @@ #include "fdiskbsdlabel.h" static void xbsd_delete_part (struct fdisk_context *cxt, int partnum); -static int xbsd_create_disklabel (struct fdisk_context *cxt); static void xbsd_edit_disklabel (void); static void xbsd_write_bootstrap (struct fdisk_context *cxt); static void xbsd_change_fstype (void); @@ -190,6 +189,37 @@ static void xbsd_add_part (struct fdisk_context *cxt, int partnum, int parttype) xbsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED; } +static int xbsd_create_disklabel (struct fdisk_context *cxt) +{ + char c; + +#if defined (__alpha__) + fprintf (stderr, _("%s contains no disklabel.\n"), cxt->dev_path); +#else + fprintf (stderr, _("%s contains no disklabel.\n"), + partname(cxt->dev_path, xbsd_part_index+1, 0)); +#endif + + while (1) { + c = read_char (_("Do you want to create a disklabel? (y/n) ")); + if (tolower(c) == 'y') { + if (xbsd_initlabel (cxt, +#if defined (__alpha__) || defined (__powerpc__) || defined (__hppa__) || \ + defined (__s390__) || defined (__s390x__) + NULL, &xbsd_dlabel, 0 +#else + xbsd_part, &xbsd_dlabel, xbsd_part_index +#endif + ) == 1) { + xbsd_print_disklabel (cxt, 1); + return 1; + } else + return 0; + } else if (c == 'n') + return 0; + } +} + void bsd_command_prompt (struct fdisk_context *cxt) { @@ -388,37 +418,6 @@ xbsd_print_disklabel (struct fdisk_context *cxt, int show_all) { } } -static int -xbsd_create_disklabel (struct fdisk_context *cxt) { - char c; - -#if defined (__alpha__) - fprintf (stderr, _("%s contains no disklabel.\n"), cxt->dev_path); -#else - fprintf (stderr, _("%s contains no disklabel.\n"), - partname(cxt->dev_path, xbsd_part_index+1, 0)); -#endif - - while (1) { - c = read_char (_("Do you want to create a disklabel? (y/n) ")); - if (tolower(c) == 'y') { - if (xbsd_initlabel (cxt, -#if defined (__alpha__) || defined (__powerpc__) || defined (__hppa__) || \ - defined (__s390__) || defined (__s390x__) - NULL, &xbsd_dlabel, 0 -#else - xbsd_part, &xbsd_dlabel, xbsd_part_index -#endif - ) == 1) { - xbsd_print_disklabel (cxt, 1); - return 1; - } else - return 0; - } else if (c == 'n') - return 0; - } -} - static int edit_int (int def, char *mesg) { @@ -847,6 +846,7 @@ const struct fdisk_label bsd_label = .probe = osf_probe_label, .write = xbsd_write_disklabel, .verify = NULL, + .create = xbsd_create_disklabel, .part_add = xbsd_add_part, .part_delete = xbsd_delete_part, }; diff --git a/fdisks/fdiskdoslabel.c b/fdisks/fdiskdoslabel.c index f81ada53a..06c868e71 100644 --- a/fdisks/fdiskdoslabel.c +++ b/fdisks/fdiskdoslabel.c @@ -285,7 +285,7 @@ void dos_print_mbr_id(struct fdisk_context *cxt) printf(_("Disk identifier: 0x%08x\n"), mbr_get_id(cxt->firstsector)); } -int create_doslabel(struct fdisk_context *cxt) +static int dos_create_disklabel(struct fdisk_context *cxt) { unsigned int id; @@ -822,6 +822,7 @@ const struct fdisk_label dos_label = .probe = dos_probe_label, .write = dos_write_disklabel, .verify = dos_verify_disklabel, + .create = dos_create_disklabel, .part_add = dos_add_partition, .part_delete = dos_delete_partition, }; diff --git a/fdisks/fdiskdoslabel.h b/fdisks/fdiskdoslabel.h index 77009dca0..0754b1714 100644 --- a/fdisks/fdiskdoslabel.h +++ b/fdisks/fdiskdoslabel.h @@ -37,7 +37,6 @@ static inline sector_t get_partition_start(struct pte *pe) return pe->offset + get_start_sect(pe->part_table); } -extern int create_doslabel(struct fdisk_context *cxt); extern void dos_print_mbr_id(struct fdisk_context *cxt); extern void dos_set_mbr_id(struct fdisk_context *cxt); extern int is_dos_partition(int t); diff --git a/fdisks/fdiskmaclabel.c b/fdisks/fdiskmaclabel.c index 28eaa93b2..f3909ec1b 100644 --- a/fdisks/fdiskmaclabel.c +++ b/fdisks/fdiskmaclabel.c @@ -95,6 +95,7 @@ const struct fdisk_label mac_label = .probe = mac_probe_label, .write = NULL, .verify = NULL, + .create = NULL, .part_add = mac_add_partition, .part_delete = NULL, }; diff --git a/fdisks/fdisksgilabel.c b/fdisks/fdisksgilabel.c index 65f6dde64..af0ad149a 100644 --- a/fdisks/fdisksgilabel.c +++ b/fdisks/fdisksgilabel.c @@ -724,8 +724,7 @@ static void sgi_add_partition(struct fdisk_context *cxt, int n, int sys) sgi_set_partition(cxt, n, first, last-first, sys); } -void -create_sgilabel(struct fdisk_context *cxt) +static int sgi_create_disklabel(struct fdisk_context *cxt) { struct hd_geometry geometry; struct { @@ -901,6 +900,7 @@ const struct fdisk_label sgi_label = .probe = sgi_probe_label, .write = sgi_write_disklabel, .verify = sgi_verify_disklabel, + .create = sgi_create_disklabel, .part_add = sgi_add_partition, .part_delete = sgi_delete_partition, }; diff --git a/fdisks/fdisksgilabel.h b/fdisks/fdisksgilabel.h index cf52bdd03..4d511139d 100644 --- a/fdisks/fdisksgilabel.h +++ b/fdisks/fdisksgilabel.h @@ -116,7 +116,6 @@ extern int sgi_change_sysid(struct fdisk_context *cxt, int i, int sys); extern unsigned int sgi_get_start_sector(struct fdisk_context *cxt, int i ); extern unsigned int sgi_get_num_sectors(struct fdisk_context *cxt, int i ); extern int sgi_get_sysid(struct fdisk_context *cxt, int i ); -extern void create_sgilabel( struct fdisk_context *cxt ); extern void create_sgiinfo(struct fdisk_context *cxt); extern void sgi_set_ilfact( void ); extern void sgi_set_rspeed( void ); diff --git a/fdisks/fdisksunlabel.c b/fdisks/fdisksunlabel.c index 31c263ee4..9d17ed22e 100644 --- a/fdisks/fdisksunlabel.c +++ b/fdisks/fdisksunlabel.c @@ -144,7 +144,7 @@ static int sun_probe_label(struct fdisk_context *cxt) return 1; } -int create_sunlabel(struct fdisk_context *cxt) +static int sun_create_disklabel(struct fdisk_context *cxt) { struct hd_geometry geometry; sector_t llsectors, llcyls; @@ -655,6 +655,7 @@ const struct fdisk_label sun_label = .probe = sun_probe_label, .write = sun_write_disklabel, .verify = sun_verify_disklabel, + .create = sun_create_disklabel, .part_add = sun_add_partition, .part_delete = sun_delete_partition, }; diff --git a/fdisks/fdisksunlabel.h b/fdisks/fdisksunlabel.h index 86db5fbc9..12cccb146 100644 --- a/fdisks/fdisksunlabel.h +++ b/fdisks/fdisksunlabel.h @@ -77,7 +77,6 @@ struct sun_disk_label { /* fdisksunlabel.c */ extern struct systypes sun_sys_types[]; -extern int create_sunlabel(struct fdisk_context *cxt); extern int sun_change_sysid(struct fdisk_context *cxt, int i, uint16_t sys); extern void sun_list_table(struct fdisk_context *cxt, int xtra); extern void sun_set_alt_cyl(struct fdisk_context *cxt); diff --git a/fdisks/utils.c b/fdisks/utils.c index 9ed2ca97f..2a06f3aa9 100644 --- a/fdisks/utils.c +++ b/fdisks/utils.c @@ -361,29 +361,45 @@ int fdisk_dev_has_disklabel(struct fdisk_context *cxt) } /** - * fdisk_create_default_disklabel: + * fdisk_create_disklabel: * @cxt: fdisk context + * @name: label name * - * Creates (in memory) disk label which is usual default for the system. For - * example sun label on sparcs, gpt on UEFI machines (TODO), DOS on another - * machines, ...etc. + * Creates a new disk label of type @name. If @name is NULL, then it + * will create a default system label type, either SUN or DOS. * - * Returns: 0 on sucess, < 0 on error. + * Returns 0 on success, otherwise, a corresponding error. */ -int fdisk_create_default_disklabel(struct fdisk_context *cxt) +int fdisk_create_disklabel(struct fdisk_context *cxt, const char *name) { if (!cxt) return -EINVAL; - /* TODO: use fdisk_create_disklabel() */ -#ifdef __sparc__ - cxt->label = &sun_label; - return create_sunlabel(cxt); + cxt->label = NULL; + if (!name) { /* use default label creation */ +#ifdef __sparc__ + cxt->label = &sun_label; #else - cxt->label = &dos_label; - return create_doslabel(cxt); + cxt->label = &dos_label; #endif + } else { + size_t i; + + for (i = 0; i < ARRAY_SIZE(labels); i++) { + if (strcmp(name, labels[i]->name) != 0) + continue; + + cxt->label = labels[i]; + DBG(LABEL, dbgprint("changing to %s label\n", cxt->label->name)); + break; + } + } + + if (!cxt->label) + return -EINVAL; + + return cxt->label->create(cxt); } /**