5

Is it possible to embed an initramfs image into existing kernel without recompiling the whole thing? I don't have resources to do that.

  • I don't think so. Why do you need the initramfs inside your kernel? I imagine that you have already seen the option in menuconfig to specify a source directory that would include the initramfs into the kernel during the compilation yeah? When you say "embed" I assume you mean have the initramfs and kernel in a single file right? Maybe it's 'possible' but I'd say it's not feasible. You'd be hacking kernel binary and you'd still have to build the initramfs first, I doubt it'd work. – Jacob Margason May 28 '15 at 12:31
  • @JacobMargason I want to boot EFIstub and have my initramfs signed too. –  May 28 '15 at 13:20
  • 1
    (1) An initramfs is not the same filesystem as an *"initial ramdisk"* or initrd. Those are two different schemes to implement the same functionality, but they are not 100% interchangeable. (2) The initramfs has to be linked with the kernel, and exists as a single image. The initrd is typically a separate image from the kernel, and has to be loaded with the kernel for booting. So which one, initramfs or initrd, are you actually using? See https://www.kernel.org/doc/Documentation/filesystems/ramfs-rootfs-initramfs.txt – sawdust May 28 '15 at 20:15
  • @sawdust corrected –  May 29 '15 at 06:03
  • The initramfs has to be linked with the kernel. But it can be augmented with an "external initramfs image" (a cpio.gz archive loaded just like an initrd), so long as the kernel has been built with initramfs support. So does your kernel already have initramfs support enabled? – sawdust May 29 '15 at 07:12
  • @sawdust It does, I load initramfs using grub currently. –  May 29 '15 at 07:27
  • *"I load initramfs using grub currently."* -- Then you must be using the "external initramfs image" capability. Read the kernel doc (I provided the link above) about "external initramfs image". If you want to replace the existing initramfs in your kernel, then you would have to re-link the kernel. That's not a full build, but I've only done it within a kernel build environment. – sawdust May 29 '15 at 08:14
  • @sawdust I really appreciate your help but I haven't found abything about re-linking the kernel. Also I suppose I have to do a full build in the first place to be able to replace the image in the build environment. –  May 29 '15 at 09:19

2 Answers2

0

initramfs can be embedded in the kernel at build time (of course this is allowed from menuconfig, selecting rootfs tree). And can be useful for some specific circumstances. Only note that once embedded, any propertary rootfs tool in the final binary may break the GPL. But until your initramfs just contains some busybox stuff, there should not be any issue.

-1

normally nobody would integrate an initramfs image into a kernel, as far as i know that isn't even possible (without rewriting the code against the concept) at least it is not intended.

the initrd is always a separated data stream (file) that is loaded together with the kernel to help with booting, it is optional but almost always included in modern gnu/linux distributions

so you don't have to ask yourself the question of how to embed it but how to change it, so have a look at your bootloader config, normally you should identify 3 things (also mostly in this order)

  • kernel image (something that guides your bootolader to the kernel image (most times a file) to load it into ram
  • command line (a string that is also written to ram for the kernel to access and use it (like giving parameters within a OS, but outside its just writing and calling (assembler code))) (this thing most times has some root=/dev/xy ro whatever option
  • an initrd image (optional image of initial ramdisk that is loaded after the kernel into ram, the kernel then uses it as a virtual disk that is used to mostly (but there are live systems that run completely within it for example) assist booting before root fs is found and mounted (like disks are slow and fluid (order of disk, scsi stuff, booting from network, filesystem encryption, recovery emergency shell if fsck fails,... etc)

so to answer your question, you dont, if your boot-loader is incapable of using initrd directly and you cant replace it just load another bootloader with it (like very archaic stuff can still load grub as a kernel for example), otherwise just update the path to the separate initrd file in your bootloader config.

BTW, if you using grub2, dont edit config directly, its generated by update-grub using data within /etc/default/grub and /etc/default/grub.d/, on grub legacy you still edit the menu.lst directly on the boot partition, but grub2 is much to complex for this, you can still edit stuff directly but it will most possible be overwritten afterwards, so thats not a good idea...

Tilo
  • 1
  • 1
  • 1
  • Sure it’s possible and intended. It’s a regular kernel feature. OP also laid out the reasons in the question’s comments. – Daniel B Aug 16 '21 at 19:31