Skip to content

Commit 2678fd2

Browse files
agrafgregkh
authored andcommitted
initramfs: Expose retained initrd as sysfs file
When the kernel command line option "retain_initrd" is set, we do not free the initrd memory. However, we also don't expose it to anyone for consumption. That leaves us in a weird situation where the only user of this feature is ppc64 and arm64 specific kexec tooling. To make it more generally useful, this patch adds a kobject to the firmware object that contains the initrd context when "retain_initrd" is set. That way, we can access the initrd any time after boot from user space and for example hand it into kexec as --initrd parameter if we want to reboot the same initrd. Or inspect it directly locally. With this patch applied, there is a new /sys/firmware/initrd file when the kernel was booted with an initrd and "retain_initrd" command line option is set. Signed-off-by: Alexander Graf <[email protected]> Tested-by: Bagas Sanjaya <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 5133bee commit 2678fd2

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
What: /sys/firmware/initrd
2+
Date: December 2023
3+
Contact: Alexander Graf <[email protected]>
4+
Description:
5+
When the kernel was booted with an initrd and the
6+
"retain_initrd" option is set on the kernel command
7+
line, /sys/firmware/initrd contains the contents of the
8+
initrd that the kernel was booted with.

Documentation/admin-guide/kernel-parameters.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2438,7 +2438,7 @@
24382438
between unregistering the boot console and initializing
24392439
the real console.
24402440

2441-
keepinitrd [HW,ARM]
2441+
keepinitrd [HW,ARM] See retain_initrd.
24422442

24432443
kernelcore= [KNL,X86,IA-64,PPC]
24442444
Format: nn[KMGTPE] | nn% | "mirror"
@@ -5580,7 +5580,8 @@
55805580
Useful for devices that are detected asynchronously
55815581
(e.g. USB and MMC devices).
55825582

5583-
retain_initrd [RAM] Keep initrd memory after extraction
5583+
retain_initrd [RAM] Keep initrd memory after extraction. After boot, it will
5584+
be accessible via /sys/firmware/initrd.
55845585

55855586
retbleed= [X86] Control mitigation of RETBleed (Arbitrary
55865587
Speculative Code Execution with Return Instructions)

init/initramfs.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,16 @@ extern unsigned long __initramfs_size;
574574
#include <linux/initrd.h>
575575
#include <linux/kexec.h>
576576

577+
static ssize_t raw_read(struct file *file, struct kobject *kobj,
578+
struct bin_attribute *attr, char *buf,
579+
loff_t pos, size_t count)
580+
{
581+
memcpy(buf, attr->private + pos, count);
582+
return count;
583+
}
584+
585+
static BIN_ATTR(initrd, 0440, raw_read, NULL, 0);
586+
577587
void __init reserve_initrd_mem(void)
578588
{
579589
phys_addr_t start;
@@ -715,8 +725,14 @@ static void __init do_populate_rootfs(void *unused, async_cookie_t cookie)
715725
* If the initrd region is overlapped with crashkernel reserved region,
716726
* free only memory that is not part of crashkernel region.
717727
*/
718-
if (!do_retain_initrd && initrd_start && !kexec_free_initrd())
728+
if (!do_retain_initrd && initrd_start && !kexec_free_initrd()) {
719729
free_initrd_mem(initrd_start, initrd_end);
730+
} else if (do_retain_initrd && initrd_start) {
731+
bin_attr_initrd.size = initrd_end - initrd_start;
732+
bin_attr_initrd.private = (void *)initrd_start;
733+
if (sysfs_create_bin_file(firmware_kobj, &bin_attr_initrd))
734+
pr_err("Failed to create initrd sysfs file");
735+
}
720736
initrd_start = 0;
721737
initrd_end = 0;
722738

0 commit comments

Comments
 (0)