grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

loadenv.c - How to check if a file is writable or not?


From: Aaron Rainbolt
Subject: loadenv.c - How to check if a file is writable or not?
Date: Fri, 15 Mar 2024 15:21:19 -0500
User-agent: Mozilla Thunderbird

Good morning/evening, and thanks for your time.

I'm currently attempting to implement a patch in the GRUB that will allow the `load_env` command to set an environment variable in the event the grubenv file can be read from but cannot be written to (for instance, if the file is on a BTRFS filesystem). This would allow the GRUB configuration script to react in different ways depending on whether or not the file is writable or not (disabling features that require the ability to write to the environment block if the block is not writable, or enabling features that are needed if the block is not writable). For this purpose, I'm attempting to figure out how to test if a file can be opened for writing... and that is proving to be a bit difficult.

AFAICT, there is no easy way to "open a file for writing" under GRUB. The way the save_env command in loadenv.c does it is by opening the environment block file with mode GRUB_FILE_TYPE_SAVEENV, then it does some tricky work with blocklists. This makes sense since GRUB in general isn't meant to write anything during the boot process except the environment block file, but it is presenting me with a challenge - how do I determine if a file is writable or not?

So far, from inspecting the code, my guess is that if a file cannot be written to, opening it as GRUB_FILE_TYPE_SAVEENV will fail and grub_file_open will return a null file handle. The GRUB manual notes, "For safety reasons, this storage (the environment block) is only available when installed on a plain disk (no LVM or RAID), using a non-checksumming filesystem (no ZFS), and using BIOS or EFI functions (no ATA, USB or IEEE1275)." Therefore it seems like trying to open a file as GRUB_FILE_TYPE_SAVEENV should fail if any of those conditions are met. However, despite about an hour of searching, I cannot find the code that would cause opening a file as GRUB_FILE_TYPE_SAVEENV to fail if the file can't be written to. All I see is that grub_file_open sends the grub file type parameter to one or more filters, and so far those filters either are for compression or verification. The compression ones are obviously not what I'm looking for (I would expect to see a filesystem driver or some related code say "can't open this file like that", not a compression filter), and the verification filters don't appear to have anything about whether a filesystem can be written to or not.

Could someone verify if I'm at least moving in the right direction with my idea of checking for read-write support using GRUB_FILE_TYPE_SAVEENV, and possibly explain where in the code the check for write-ability is?

Thanks for your help!

Aaron




reply via email to

[Prev in Thread] Current Thread [Next in Thread]