From a71601af2769cc256341b85414065e13377671d7 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 23 Jul 2012 10:56:06 +0200 Subject: [PATCH] fdisk: get_boot() has to die (step I.) - move generic stuff around "create disklabel" operation to API Signed-off-by: Karel Zak --- fdisks/fdisk.c | 15 ++++++--------- fdisks/fdisk.h | 2 ++ fdisks/fdiskdoslabel.c | 3 ++- fdisks/fdiskdoslabel.h | 2 +- fdisks/fdisksunlabel.c | 4 +++- fdisks/fdisksunlabel.h | 2 +- fdisks/utils.c | 35 +++++++++++++++++++++++++++++++++++ 7 files changed, 50 insertions(+), 13 deletions(-) diff --git a/fdisks/fdisk.c b/fdisks/fdisk.c index a7787c6a2a..e24dd3bf45 100644 --- a/fdisks/fdisk.c +++ b/fdisks/fdisk.c @@ -523,14 +523,6 @@ static int get_boot(struct fdisk_context *cxt, int try_only) { if (disklabel == ANY_LABEL) { if (try_only) return -1; - - fprintf(stderr, - _("Device does not contain a recognized partition table\n")); -#ifdef __sparc__ - create_sunlabel(cxt); -#else - create_doslabel(cxt); -#endif } return 0; } @@ -2072,7 +2064,12 @@ int main(int argc, char **argv) cxt->sector_size, DEFAULT_SECTOR_SIZE); gpt_warning(cxt->dev_path); - get_boot(cxt, 0); + + if (!fdisk_dev_has_disklabel(cxt)) { + fprintf(stderr, + _("Device does not contain a recognized partition table\n")); + fdisk_create_default_disklabel(cxt); + } command_prompt(cxt); diff --git a/fdisks/fdisk.h b/fdisks/fdisk.h index 9eead15f2e..3783f82732 100644 --- a/fdisks/fdisk.h +++ b/fdisks/fdisk.h @@ -146,6 +146,7 @@ extern const struct fdisk_label sgi_label; extern struct fdisk_context *fdisk_new_context_from_filename(const char *fname, int readonly); extern int fdisk_dev_has_topology(struct fdisk_context *cxt); +extern int fdisk_dev_has_disklabel(struct fdisk_context *cxt); extern int fdisk_dev_sectsz_is_default(struct fdisk_context *cxt); extern void fdisk_free_context(struct fdisk_context *cxt); extern void fdisk_mbr_zeroize(struct fdisk_context *cxt); @@ -153,6 +154,7 @@ 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); /* prototypes for fdisk.c */ extern char *disk_device, *line_ptr; diff --git a/fdisks/fdiskdoslabel.c b/fdisks/fdiskdoslabel.c index a115e66157..b181fb3530 100644 --- a/fdisks/fdiskdoslabel.c +++ b/fdisks/fdiskdoslabel.c @@ -219,7 +219,7 @@ void dos_print_mbr_id(struct fdisk_context *cxt) printf(_("Disk identifier: 0x%08x\n"), dos_read_mbr_id(cxt->mbr)); } -void create_doslabel(struct fdisk_context *cxt) +int create_doslabel(struct fdisk_context *cxt) { unsigned int id; @@ -238,6 +238,7 @@ void create_doslabel(struct fdisk_context *cxt) /* Put MBR signature */ write_part_table_flag(cxt->mbr); + return 0; } void dos_set_mbr_id(struct fdisk_context *cxt) diff --git a/fdisks/fdiskdoslabel.h b/fdisks/fdiskdoslabel.h index 608e3f7003..a8fab52894 100644 --- a/fdisks/fdiskdoslabel.h +++ b/fdisks/fdiskdoslabel.h @@ -43,7 +43,7 @@ static inline sector_t get_partition_start(struct pte *pe) return pe->offset + get_start_sect(pe->part_table); } -extern void create_doslabel(struct fdisk_context *cxt); +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 void dos_delete_partition(int i); diff --git a/fdisks/fdisksunlabel.c b/fdisks/fdisksunlabel.c index 909f159e4c..6a0c31b15a 100644 --- a/fdisks/fdisksunlabel.c +++ b/fdisks/fdisksunlabel.c @@ -144,7 +144,7 @@ static int sun_probe_label(struct fdisk_context *cxt) return 1; } -void create_sunlabel(struct fdisk_context *cxt) +int create_sunlabel(struct fdisk_context *cxt) { struct hd_geometry geometry; sector_t llsectors, llcyls; @@ -234,6 +234,8 @@ void create_sunlabel(struct fdisk_context *cxt) set_all_unchanged(); set_changed(0); + + return 0; } void toggle_sunflags(struct fdisk_context *cxt, int i, uint16_t mask) diff --git a/fdisks/fdisksunlabel.h b/fdisks/fdisksunlabel.h index 9779e22f33..fa12a1d2a7 100644 --- a/fdisks/fdisksunlabel.h +++ b/fdisks/fdisksunlabel.h @@ -77,7 +77,7 @@ struct sun_disk_label { /* fdisksunlabel.c */ extern struct systypes sun_sys_types[]; -extern void create_sunlabel(struct fdisk_context *cxt); +extern int create_sunlabel(struct fdisk_context *cxt); extern void sun_delete_partition(struct fdisk_context *cxt, int i); 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); diff --git a/fdisks/utils.c b/fdisks/utils.c index f1a9f3da1b..07348d3803 100644 --- a/fdisks/utils.c +++ b/fdisks/utils.c @@ -29,6 +29,9 @@ #include "common.h" #include "fdisk.h" +#include "fdiskdoslabel.h" +#include "fdisksunlabel.h" + int fdisk_debug_mask; /* @@ -274,6 +277,38 @@ int fdisk_dev_has_topology(struct fdisk_context *cxt) return 0; } +/** + * fdisk_dev_has_disklabel: + * @cxt: fdisk context + * + * Returns: return 1 if there is label on the device. + */ +int fdisk_dev_has_disklabel(struct fdisk_context *cxt) +{ + return cxt && disklabel != ANY_LABEL; +} + +/** + * fdisk_create_default_disklabel: + * @cxt: fdisk context + * + * 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. + * + * Returns: 0 on sucess, < 0 on error. + */ +int fdisk_create_default_disklabel(struct fdisk_context *cxt) +{ + if (!cxt) + return -EINVAL; +#ifdef __sparc__ + return create_sunlabel(cxt); +#else + return create_doslabel(cxt); +#endif +} + /** * fdisk_init_debug: * @mask: debug mask (0xffff to enable full debuging) -- 2.47.3