libfdisk: check for collisions when create new label

We need to be sure that when create a new disklabel than the old label
will be removed.

Addresses: https://github.com/karelzak/util-linux/issues/410
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2017-02-14 13:07:54 +01:00
parent b192dd6943
commit 37204dc60c
4 changed files with 57 additions and 50 deletions

View File

@ -500,55 +500,6 @@ static void reset_context(struct fdisk_context *cxt)
fdisk_free_wipe_areas(cxt);
}
/*
* This function prints a warning if the device is not wiped (e.g. wipefs(8).
* Please don't call this function if there is already a PT.
*
* Returns: 0 if nothing found, < 0 on error, 1 if found a signature
*/
static int check_collisions(struct fdisk_context *cxt)
{
#ifdef HAVE_LIBBLKID
int rc = 0;
blkid_probe pr;
assert(cxt);
assert(cxt->dev_fd >= 0);
DBG(CXT, ul_debugobj(cxt, "wipe check: initialize libblkid prober"));
pr = blkid_new_probe();
if (!pr)
return -ENOMEM;
rc = blkid_probe_set_device(pr, cxt->dev_fd, 0, 0);
if (rc)
return rc;
blkid_probe_enable_superblocks(pr, 1);
blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_TYPE);
blkid_probe_enable_partitions(pr, 1);
/* we care about the first found FS/raid, so don't call blkid_do_probe()
* in loop or don't use blkid_do_fullprobe() ... */
rc = blkid_do_probe(pr);
if (rc == 0) {
const char *name = NULL;
if (blkid_probe_lookup_value(pr, "TYPE", &name, 0) == 0 ||
blkid_probe_lookup_value(pr, "PTTYPE", &name, 0) == 0) {
cxt->collision = strdup(name);
if (!cxt->collision)
rc = -ENOMEM;
}
}
blkid_free_probe(pr);
return rc;
#else
return 0;
#endif
}
/**
* fdisk_assign_device:
* @cxt: context
@ -621,7 +572,8 @@ int fdisk_assign_device(struct fdisk_context *cxt,
/* warn about obsolete stuff on the device if we aren't in
* list-only mode and there is not PT yet */
if (!fdisk_is_listonly(cxt) && !fdisk_has_label(cxt) && check_collisions(cxt) < 0)
if (!fdisk_is_listonly(cxt) && !fdisk_has_label(cxt)
&& fdisk_check_collisions(cxt) < 0)
goto fail;
DBG(CXT, ul_debugobj(cxt, "initialized for %s [%s]",

View File

@ -477,5 +477,6 @@ void fdisk_free_wipe_areas(struct fdisk_context *cxt);
int fdisk_set_wipe_area(struct fdisk_context *cxt, uint64_t start, uint64_t size, int enable);
int fdisk_do_wipe(struct fdisk_context *cxt);
int fdisk_has_wipe_area(struct fdisk_context *cxt, uint64_t start, uint64_t size);
int fdisk_check_collisions(struct fdisk_context *cxt);
#endif /* _LIBFDISK_PRIVATE_H */

View File

@ -367,6 +367,10 @@ int fdisk_create_disklabel(struct fdisk_context *cxt, const char *name)
lb = fdisk_get_label(cxt, name);
if (!lb || lb->disabled)
return -EINVAL;
if (!haslabel || (lb && cxt->label != lb))
fdisk_check_collisions(cxt);
if (!lb->op->create)
return -ENOSYS;

View File

@ -143,3 +143,53 @@ int fdisk_do_wipe(struct fdisk_context *cxt)
#endif
return 0;
}
/*
* Please don't call this function if there is already a PT.
*
* Returns: 0 if nothing found, < 0 on error, 1 if found a signature
*/
int fdisk_check_collisions(struct fdisk_context *cxt)
{
#ifdef HAVE_LIBBLKID
int rc = 0;
blkid_probe pr;
assert(cxt);
assert(cxt->dev_fd >= 0);
DBG(CXT, ul_debugobj(cxt, "wipe check: initialize libblkid prober"));
pr = blkid_new_probe();
if (!pr)
return -ENOMEM;
rc = blkid_probe_set_device(pr, cxt->dev_fd, 0, 0);
if (rc)
return rc;
blkid_probe_enable_superblocks(pr, 1);
blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_TYPE);
blkid_probe_enable_partitions(pr, 1);
/* we care about the first found FS/raid, so don't call blkid_do_probe()
* in loop or don't use blkid_do_fullprobe() ... */
rc = blkid_do_probe(pr);
if (rc == 0) {
const char *name = NULL;
if (blkid_probe_lookup_value(pr, "TYPE", &name, 0) == 0 ||
blkid_probe_lookup_value(pr, "PTTYPE", &name, 0) == 0) {
cxt->collision = strdup(name);
if (!cxt->collision)
rc = -ENOMEM;
}
}
blkid_free_probe(pr);
return rc;
#else
return 0;
#endif
}