fdisk: always report fdisk_create_disklabel() errors

This is fdisk, cfdisk and sfdisk change to inform user about fdisk_create_disklabel()
issues.

Addresses: https://github.com/karelzak/util-linux/issues/1147
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2020-09-30 11:49:05 +02:00
parent ffac9652c7
commit 134b6296e3
3 changed files with 37 additions and 12 deletions

View File

@ -747,6 +747,20 @@ static void ui_clean_warn(void)
clrtoeol(); clrtoeol();
} }
static int __attribute__((__noreturn__)) ui_err(int rc, const char *fmt, ...)
{
va_list ap;
ui_end();
va_start(ap, fmt);
fprintf(stderr, "%s: ", program_invocation_short_name);
vfprintf(stderr, fmt, ap);
fprintf(stderr, ": %s\n", strerror(errno));
va_end(ap);
exit(rc);
}
static int __attribute__((__noreturn__)) ui_errx(int rc, const char *fmt, ...) static int __attribute__((__noreturn__)) ui_errx(int rc, const char *fmt, ...)
{ {
va_list ap; va_list ap;
@ -2529,9 +2543,11 @@ static int ui_run(struct cfdisk *cf)
if (!fdisk_has_label(cf->cxt) || cf->zero_start) { if (!fdisk_has_label(cf->cxt) || cf->zero_start) {
rc = ui_create_label(cf); rc = ui_create_label(cf);
if (rc < 0) if (rc < 0) {
ui_errx(EXIT_FAILURE, errno = -rc;
ui_err(EXIT_FAILURE,
_("failed to create a new disklabel")); _("failed to create a new disklabel"));
}
if (rc) if (rc)
return rc; return rc;
} }

View File

@ -487,7 +487,7 @@ static int script_read(struct fdisk_context *cxt)
rc = fdisk_reassign_device(cxt); rc = fdisk_reassign_device(cxt);
if (rc == 0 && !fdisk_has_label(cxt)) { if (rc == 0 && !fdisk_has_label(cxt)) {
fdisk_info(cxt, _("Device does not contain a recognized partition table.")); fdisk_info(cxt, _("Device does not contain a recognized partition table."));
fdisk_create_disklabel(cxt, NULL); rc = fdisk_create_disklabel(cxt, NULL);
} }
} else } else
fdisk_info(cxt, _("Script successfully applied.")); fdisk_info(cxt, _("Script successfully applied."));
@ -1065,6 +1065,7 @@ static int createlabel_menu_cb(struct fdisk_context **cxt0,
const struct menu_entry *ent) const struct menu_entry *ent)
{ {
struct fdisk_context *cxt = *cxt0; struct fdisk_context *cxt = *cxt0;
const char *wanted = NULL;
int rc = -EINVAL; int rc = -EINVAL;
DBG(MENU, ul_debug("enter Create label menu")); DBG(MENU, ul_debug("enter Create label menu"));
@ -1077,26 +1078,33 @@ static int createlabel_menu_cb(struct fdisk_context **cxt0,
case 'g': case 'g':
/* Deprecated, use 'G' in main menu, just for backward /* Deprecated, use 'G' in main menu, just for backward
* compatibility only. */ * compatibility only. */
rc = fdisk_create_disklabel(cxt, "sgi"); wanted = "sgi";
break; break;
} }
} else { } else {
switch (ent->key) { switch (ent->key) {
case 'g': case 'g':
rc = fdisk_create_disklabel(cxt, "gpt"); wanted = "gpt";
break; break;
case 'G': case 'G':
rc = fdisk_create_disklabel(cxt, "sgi"); wanted = "sgi";
break; break;
case 'o': case 'o':
rc = fdisk_create_disklabel(cxt, "dos"); wanted = "dos";
break; break;
case 's': case 's':
rc = fdisk_create_disklabel(cxt, "sun"); wanted = "sun";
break; break;
} }
} }
if (wanted) {
rc = fdisk_create_disklabel(cxt, wanted);
if (rc) {
errno = -rc;
fdisk_warn(cxt, _("Failed to create '%s' disk label"), wanted);
}
}
if (rc == 0 && fdisk_get_collision(cxt)) if (rc == 0 && fdisk_get_collision(cxt))
follow_wipe_mode(cxt); follow_wipe_mode(cxt);

View File

@ -1890,10 +1890,11 @@ static int command_fdisk(struct sfdisk *sf, int argc, char **argv)
if (!created) { /* create a new disklabel */ if (!created) { /* create a new disklabel */
rc = fdisk_apply_script_headers(sf->cxt, dp); rc = fdisk_apply_script_headers(sf->cxt, dp);
created = !rc; created = !rc;
if (rc) if (rc) {
fdisk_warnx(sf->cxt, _( errno = -rc;
"Failed to apply script headers, " fdisk_warn(sf->cxt, _(
"disk label not created.")); "Failed to apply script headers, disk label not created"));
}
if (rc == 0 && fdisk_get_collision(sf->cxt)) if (rc == 0 && fdisk_get_collision(sf->cxt))
follow_wipe_mode(sf); follow_wipe_mode(sf);