From 24c0078a846c0e8dd7cc65f0a4a90ba05864434b Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sat, 29 Jul 2023 20:00:55 +0800 Subject: [PATCH 1/2] fstab-util: add fstab_has_node --- src/shared/fstab-util.c | 19 ++++++++++--------- src/shared/fstab-util.h | 3 +++ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/shared/fstab-util.c b/src/shared/fstab-util.c index 4ffec25c75..67e718b6ea 100644 --- a/src/shared/fstab-util.c +++ b/src/shared/fstab-util.c @@ -63,7 +63,7 @@ bool fstab_is_extrinsic(const char *mount, const char *opts) { return false; } -static int fstab_is_mount_point_of(const char *what_fstab, const char *path) { +static int fstab_is_same_node(const char *what_fstab, const char *path) { _cleanup_free_ char *node = NULL; assert(what_fstab); @@ -86,7 +86,7 @@ int fstab_is_mount_point_full(const char *where, const char *path) { _cleanup_endmntent_ FILE *f = NULL; int r; - assert(where); + assert(where || path); f = setmntent(fstab_path(), "re"); if (!f) @@ -100,14 +100,15 @@ int fstab_is_mount_point_full(const char *where, const char *path) { if (!me) return errno != 0 ? -errno : false; - if (path_equal(where, me->mnt_dir)) { - if (!path) - return true; + if (where && !path_equal(where, me->mnt_dir)) + continue; - r = fstab_is_mount_point_of(me->mnt_fsname, path); - if (r > 0 || (r < 0 && !ERRNO_IS_DEVICE_ABSENT(r))) - return r; - } + if (!path) + return true; + + r = fstab_is_same_node(me->mnt_fsname, path); + if (r > 0 || (r < 0 && !ERRNO_IS_DEVICE_ABSENT(r))) + return r; } return false; diff --git a/src/shared/fstab-util.h b/src/shared/fstab-util.h index cb3686bee3..a1c079fd41 100644 --- a/src/shared/fstab-util.h +++ b/src/shared/fstab-util.h @@ -13,6 +13,9 @@ int fstab_is_mount_point_full(const char *where, const char *path); static inline int fstab_is_mount_point(const char *where) { return fstab_is_mount_point_full(where, NULL); } +static inline int fstab_has_node(const char *path) { + return fstab_is_mount_point_full(NULL, path); +} int fstab_filter_options( const char *opts, From 7893a547ebcee007dbc6119373d3e5d809f221fe Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sat, 29 Jul 2023 20:05:58 +0800 Subject: [PATCH 2/2] gpt-auto: don't mount ESP if there's an fstab entry for it Follow-up for #28511 Fixes #28550 --- src/gpt-auto-generator/gpt-auto-generator.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/gpt-auto-generator/gpt-auto-generator.c b/src/gpt-auto-generator/gpt-auto-generator.c index 4465038198..585aaae2ed 100644 --- a/src/gpt-auto-generator/gpt-auto-generator.c +++ b/src/gpt-auto-generator/gpt-auto-generator.c @@ -574,10 +574,17 @@ static int add_partition_esp(DissectedPartition *p, bool has_xbootldr) { return 0; } + /* Check if there's an existing fstab entry for ESP. If so, we just skip the gpt-auto logic. */ + r = fstab_has_node(p->node); + if (r < 0) + return log_error_errno(r, + "Failed to check if fstab entry for device '%s' exists: %m", p->node); + if (r > 0) + return 0; + /* If /boot/ is present, unused, and empty, we'll take that. * Otherwise, if /efi/ is unused and empty (or missing), we'll take that. - * Otherwise, we do nothing. - */ + * Otherwise, we do nothing. */ if (!has_xbootldr && slash_boot_exists()) { r = slash_boot_in_fstab(); if (r < 0) @@ -590,16 +597,6 @@ static int add_partition_esp(DissectedPartition *p, bool has_xbootldr) { esp_path = "/boot"; id = "boot"; } - } else { - /* Check if the fstab entry for /boot/ is already the ESP. If so, we don't need to - * check /efi/ or duplicate the mount there. */ - r = fstab_is_mount_point_full("/boot", p->node); - if (r < 0) - return log_error_errno(r, - "Failed to check if fstab entry for /boot uses the same device as '%s': %m", - p->node); - if (r > 0) - return 0; } }