From 54da548e021b4682d99d3b66e3c3e386af5816cf Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Fri, 25 Jun 2021 12:58:51 +0200 Subject: [PATCH] cfdisk: optimize mountpoint detection for PARTUUID Don't check fstab (and udev symplinks) for new UUIDs. Fixes: https://github.com/karelzak/util-linux/issues/1331 Signed-off-by: Karel Zak --- disk-utils/cfdisk.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/disk-utils/cfdisk.c b/disk-utils/cfdisk.c index d7a9a4e3d..c1b28889f 100644 --- a/disk-utils/cfdisk.c +++ b/disk-utils/cfdisk.c @@ -1331,6 +1331,25 @@ static inline int iszero(const char *str) return !p || *p == '\0'; } +static int has_uuid(struct fdisk_table *tb, const char *uuid) +{ + struct fdisk_partition *pa; + struct fdisk_iter *itr; + int rc = 0; + + if (!tb || !uuid || fdisk_table_is_empty(tb)) + return 0; + + itr = fdisk_new_iter(FDISK_ITER_FORWARD); + while (rc == 0 && fdisk_table_next_partition(tb, itr, &pa) == 0) { + const char *x = fdisk_partition_get_uuid(pa); + if (x) + rc = strcmp(x, uuid) == 0; + } + fdisk_free_iter(itr); + return rc; +} + static void extra_prepare_data(struct cfdisk *cf) { struct fdisk_partition *pa = get_current_partition(cf); @@ -1350,7 +1369,14 @@ static void extra_prepare_data(struct cfdisk *cf) if (!fdisk_partition_to_string(pa, cf->cxt, FDISK_FIELD_UUID, &data) && data) { extra_insert_pair(l, _("Partition UUID:"), data); - if (!mountpoint) + + /* Search for mountpoint by PARTUUID= means that we need to + * check fstab and convert PARTUUID to the device name. This is + * unnecessary and overkill for newly created partitions. Let's + * check if the UUID already exist in the old layout, otherwise + * ignore it. + */ + if (!mountpoint && has_uuid(cf->original_layout, data)) mountpoint = get_mountpoint(cf, "PARTUUID", data); free(data); }