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();
}
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, ...)
{
va_list ap;
@ -2529,9 +2543,11 @@ static int ui_run(struct cfdisk *cf)
if (!fdisk_has_label(cf->cxt) || cf->zero_start) {
rc = ui_create_label(cf);
if (rc < 0)
ui_errx(EXIT_FAILURE,
if (rc < 0) {
errno = -rc;
ui_err(EXIT_FAILURE,
_("failed to create a new disklabel"));
}
if (rc)
return rc;
}

View File

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