Skip to contentSkip to conversation

Add grub_util_readlink()

PATCH RESEND In review Javier Martinez Canillas 2 participants 3 messages Jan 17, 2020 – Feb 11, 2020 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH RESEND v4 1/2 Add grub_util_readlink()

From: Peter Jones <[email protected]>

Currently grub-editenv and related tools are not able to follow symbolic links when finding their config file. For example the grub-editenv create command will wrongly overwrite a symlink in /boot/grub2/grubenv with a new regular file, instead of creating a file in the path the symlink points to.

A following patch will change that and add support in grub-editenv to follow symbolic links when finding the grub environment variables file.

Add a grub_util_readlink() helper function that is just a wrapper around the platform specific function to read the value of a symbolic link. This helper function will be used by the following patch for grub-editenv.

The helper function is not added for Windows, since this operating system doesn't have a primitive to read the contents of a symbolic link.

---
Resending the patches rebased on top of the latest master branch so they apply cleanly. Sorry for the noise.

Changes in v4:
- Don't add this helper for Windows OS since doesn't support it.

Changes in v3: None
Changes in v2:
- Add a better commit message explaining why the change is needed.
- Add Reviewed-by tag from Daniel Kiper.

 include/grub/osdep/hostfile_aros.h | 6 ++++++
 include/grub/osdep/hostfile_unix.h | 6 ++++++
 2 files changed, 12 insertions(+)
include/grub/osdep/hostfile_aros.h +6 -0
index a059c0fa40a..161fbb7bdfd 100644
@@ -68,6 +68,12 @@ grub_util_rename (const char *from, const char *to)
   return rename (from, to);
 }
 
+static inline ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize)
+{
+  return readlink(name, buf, bufsize);
+}
+
 #define grub_util_mkdir(a) mkdir ((a), 0755)
 
 struct grub_util_fd
include/grub/osdep/hostfile_unix.h +6 -0
index 9ffe46fa3ca..17cd3aa8b30 100644
@@ -71,6 +71,12 @@ grub_util_rename (const char *from, const char *to)
   return rename (from, to);
 }
 
+static inline ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize)
+{
+  return readlink(name, buf, bufsize);
+}
+
 #define grub_util_mkdir(a) mkdir ((a), 0755)
 
 #if defined (__NetBSD__)
Signature · 1 line
2.23.0
#1 lore raw

PATCH RESEND v4 2/2 Make editenv chase symlinks including those across devices

From: Peter Jones <[email protected]>

The grub-editenv create command will wrongly overwrite /boot/grub2/grubenv with a regular file if grubenv is a symbolic link. But instead, it should create a new file in the path the symlink points to.

This lets /boot/grub2/grubenv be a symlink to /boot/efi/EFI/fedora/grubenv even when they're different mount points, which allows grub2-editenv to be the same across platforms (i.e. UEFI vs BIOS).

For example, in Fedora the GRUB EFI builds have prefix set to /EFI/fedora (on the EFI System Partition), but for BIOS machine it'll be /boot/grub2 (which may or may not be its own mountpoint).

With this patch, on EFI machines we can make /boot/grub2/grubenv a symlink to /boot/efi/EFI/fedora/grubenv, and the same copy of grub-set-default will work on both kinds of systems.

Windows doesn't implement a readlink primitive, so the current behaviour is maintained for this operating system.

---

Changes in v4:
- Only attempt following symlinks in operating sytems other than Windows.

Changes in v3:
- A couple of style cleanups.

Changes in v2:
- Address issues pointed out by Daniel Kiper in the previous version.

 Makefile.util.def | 11 ++++++++
 util/editenv.c    | 65 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+)
Makefile.util.def +11 -0
index 94336392b9e..51a254f1a6f 100644
@@ -242,8 +242,19 @@ program = {
 
   common = util/grub-editenv.c;
   common = util/editenv.c;
+  common = util/grub-install-common.c;
   common = grub-core/osdep/init.c;
+  common = grub-core/osdep/compress.c;
+  extra_dist = grub-core/osdep/unix/compress.c;
+  extra_dist = grub-core/osdep/basic/compress.c;
+  common = util/mkimage.c;
+  common = util/grub-mkimage32.c;
+  common = util/grub-mkimage64.c;
+  common = grub-core/osdep/config.c;
+  common = util/config.c;
+  common = util/resolve.c;
 
+  ldadd = '$(LIBLZMA)';
   ldadd = libgrubmods.a;
   ldadd = libgrubgcry.a;
   ldadd = libgrubkern.a;
util/editenv.c +65 -0

84 lines not on this page · raw message

Signature · 1 line
2.23.0
Reviewed-by on v4 2/2
#2 lore raw

Re: [RESEND PATCH v4 2/2] Make editenv chase symlinks including those across devices

Quoted 18 lines · Javier Martinez Canillas

From: Peter Jones <[email protected]>

The grub-editenv create command will wrongly overwrite /boot/grub2/grubenv with a regular file if grubenv is a symbolic link. But instead, it should create a new file in the path the symlink points to.

This lets /boot/grub2/grubenv be a symlink to /boot/efi/EFI/fedora/grubenv even when they're different mount points, which allows grub2-editenv to be the same across platforms (i.e. UEFI vs BIOS).

For example, in Fedora the GRUB EFI builds have prefix set to /EFI/fedora (on the EFI System Partition), but for BIOS machine it'll be /boot/grub2 (which may or may not be its own mountpoint).

With this patch, on EFI machines we can make /boot/grub2/grubenv a symlink to /boot/efi/EFI/fedora/grubenv, and the same copy of grub-set-default will work on both kinds of systems.

Quoted 7 lines

Windows doesn't implement a readlink primitive, so the current behaviour is maintained for this operating system.

Daniel