Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <graf@amazon.com>
Tested-by: Bagas Sanjaya <bagasdotme@gmail.com>
Link: https://lore.kernel.org/r/20231207235654.16622-1-graf@amazon.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Alexander Graf and committed by
Greg Kroah-Hartman
2678fd2f 5133bee6

+28 -3
+8
Documentation/ABI/testing/sysfs-firmware-initrd
··· 1 + What: /sys/firmware/initrd 2 + Date: December 2023 3 + Contact: Alexander Graf <graf@amazon.com> 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.
+3 -2
Documentation/admin-guide/kernel-parameters.txt
··· 2438 2438 between unregistering the boot console and initializing 2439 2439 the real console. 2440 2440 2441 - keepinitrd [HW,ARM] 2441 + keepinitrd [HW,ARM] See retain_initrd. 2442 2442 2443 2443 kernelcore= [KNL,X86,IA-64,PPC] 2444 2444 Format: nn[KMGTPE] | nn% | "mirror" ··· 5580 5580 Useful for devices that are detected asynchronously 5581 5581 (e.g. USB and MMC devices). 5582 5582 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. 5584 5585 5585 5586 retbleed= [X86] Control mitigation of RETBleed (Arbitrary 5586 5587 Speculative Code Execution with Return Instructions)
+17 -1
init/initramfs.c
··· 574 574 #include <linux/initrd.h> 575 575 #include <linux/kexec.h> 576 576 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 + 577 587 void __init reserve_initrd_mem(void) 578 588 { 579 589 phys_addr_t start; ··· 725 715 * If the initrd region is overlapped with crashkernel reserved region, 726 716 * free only memory that is not part of crashkernel region. 727 717 */ 728 - if (!do_retain_initrd && initrd_start && !kexec_free_initrd()) 718 + if (!do_retain_initrd && initrd_start && !kexec_free_initrd()) { 729 719 free_initrd_mem(initrd_start, initrd_end); 720 + } else if (do_retain_initrd && initrd_start) { 721 + bin_attr_initrd.size = initrd_end - initrd_start; 722 + bin_attr_initrd.private = (void *)initrd_start; 723 + if (sysfs_create_bin_file(firmware_kobj, &bin_attr_initrd)) 724 + pr_err("Failed to create initrd sysfs file"); 725 + } 730 726 initrd_start = 0; 731 727 initrd_end = 0; 732 728