[PATCH-RFC] init: simplify initrd code (was Re: [PATCH RESEND 00/62] initrd: remove classic initrd support).

nschichan
2025-09-25T13:12:29Z

From: Nicolas Schichan <nschichan@freebox.fr>

- drop prompt_ramdisk and ramdisk_start kernel parameters
- drop compression support
- drop image autodetection, the whole /initrd.image content is now
copied into /dev/ram0
- remove rd_load_disk() which doesn't seem to be used anywhere.

There is now no more limitation on the type of initrd filesystem that
can be loaded since the code trying to guess the initrd filesystem
size is gone (the whole /initrd.image file is used).

A few global variables in do_mounts_rd.c are now put as local
variables in rd_load_image() since they do not need to be visible
outside this function.
---

Hello,

Hopefully my email config is now better and reaches gmail users
correctly.

The patch below could probably split in a few patches, but I think
this simplify the code greatly without removing the functionality we
depend on (and this allows now to use EROFS initrd images).

Coupled with keeping the function populate_initrd_image() in
init/initramfs.c, this will keep what we need from the initrd code.

This removes support of loading bzip/gz/xz/... compressed images as
well, not sure if many user depend on this feature anymore.

No signoff because I'm only seeking comments about those changes right
now.

init/do_mounts.h | 2 -
init/do_mounts_rd.c | 243 +-------------------------------------------
2 files changed, 4 insertions(+), 241 deletions(-)

diff --git a/init/do_mounts.h b/init/do_mounts.h
index 6069ea3eb80d..c0028ee3cff6 100644
--- a/init/do_mounts.h
+++ b/init/do_mounts.h
@@ -24,12 +24,10 @@ static inline __init int create_dev(char *name, dev_t dev)

#ifdef CONFIG_BLK_DEV_RAM

-int __init rd_load_disk(int n);
int __init rd_load_image(char *from);

#else

-static inline int rd_load_disk(int n) { return 0; }
static inline int rd_load_image(char *from) { return 0; }

#endif
diff --git a/init/do_mounts_rd.c b/init/do_mounts_rd.c
index ac021ae6e6fa..5a69ff43f5ee 100644
--- a/init/do_mounts_rd.c
+++ b/init/do_mounts_rd.c
@@ -14,173 +14,9 @@

#include <linux/decompress/generic.h>

-static struct file *in_file, *out_file;
-static loff_t in_pos, out_pos;
-
-static int __init prompt_ramdisk(char *str)
-{
- pr_warn("ignoring the deprecated prompt_ramdisk= option\n");
- return 1;
-}
-__setup("prompt_ramdisk=", prompt_ramdisk);
-
-int __initdata rd_image_start; /* starting block # of image */
-
-static int __init ramdisk_start_setup(char *str)
-{
- rd_image_start = simple_strtol(str,NULL,0);
- return 1;
-}
-__setup("ramdisk_start=", ramdisk_start_setup);
-
-static int __init crd_load(decompress_fn deco);
-
-/*
- * This routine tries to find a RAM disk image to load, and returns the
- * number of blocks to read for a non-compressed image, 0 if the image
- * is a compressed image, and -1 if an image with the right magic
- * numbers could not be found.
- *
- * We currently check for the following magic numbers:
- * minix
- * ext2
- * romfs
- * cramfs
- * squashfs
- * gzip
- * bzip2
- * lzma
- * xz
- * lzo
- * lz4
- */
-static int __init
-identify_ramdisk_image(struct file *file, loff_t pos,
- decompress_fn *decompressor)
-{
- const int size = 512;
- struct minix_super_block *minixsb;
- struct romfs_super_block *romfsb;
- struct cramfs_super *cramfsb;
- struct squashfs_super_block *squashfsb;
- int nblocks = -1;
- unsigned char *buf;
- const char *compress_name;
- unsigned long n;
- int start_block = rd_image_start;
-
- buf = kmalloc(size, GFP_KERNEL);
- if (!buf)
- return -ENOMEM;
-
- minixsb = (struct minix_super_block *) buf;
- romfsb = (struct romfs_super_block *) buf;
- cramfsb = (struct cramfs_super *) buf;
- squashfsb = (struct squashfs_super_block *) buf;
- memset(buf, 0xe5, size);
-
- /*
- * Read block 0 to test for compressed kernel
- */
- pos = start_block * BLOCK_SIZE;
- kernel_read(file, buf, size, &pos);
-
- *decompressor = decompress_method(buf, size, &compress_name);
- if (compress_name) {
- printk(KERN_NOTICE "RAMDISK: %s image found at block %d\n",
- compress_name, start_block);
- if (!*decompressor)
- printk(KERN_EMERG
- "RAMDISK: %s decompressor not configured!\n",
- compress_name);
- nblocks = 0;
- goto done;
- }
-
- /* romfs is at block zero too */
- if (romfsb->word0 == ROMSB_WORD0 &&
- romfsb->word1 == ROMSB_WORD1) {
- printk(KERN_NOTICE
- "RAMDISK: romfs filesystem found at block %d\n",
- start_block);
- nblocks = (ntohl(romfsb->size)+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS;
- goto done;
- }
-
- if (cramfsb->magic == CRAMFS_MAGIC) {
- printk(KERN_NOTICE
- "RAMDISK: cramfs filesystem found at block %d\n",
- start_block);
- nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
- goto done;
- }
-
- /* squashfs is at block zero too */
- if (le32_to_cpu(squashfsb->s_magic) == SQUASHFS_MAGIC) {
- printk(KERN_NOTICE
- "RAMDISK: squashfs filesystem found at block %d\n",
- start_block);
- nblocks = (le64_to_cpu(squashfsb->bytes_used) + BLOCK_SIZE - 1)
- >> BLOCK_SIZE_BITS;
- goto done;
- }
-
- /*
- * Read 512 bytes further to check if cramfs is padded
- */
- pos = start_block * BLOCK_SIZE + 0x200;
- kernel_read(file, buf, size, &pos);
-
- if (cramfsb->magic == CRAMFS_MAGIC) {
- printk(KERN_NOTICE
- "RAMDISK: cramfs filesystem found at block %d\n",
- start_block);
- nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
- goto done;
- }
-
- /*
- * Read block 1 to test for minix and ext2 superblock
- */
- pos = (start_block + 1) * BLOCK_SIZE;
- kernel_read(file, buf, size, &pos);
-
- /* Try minix */
- if (minixsb->s_magic == MINIX_SUPER_MAGIC ||
- minixsb->s_magic == MINIX_SUPER_MAGIC2) {
- printk(KERN_NOTICE
- "RAMDISK: Minix filesystem found at block %d\n",
- start_block);
- nblocks = minixsb->s_nzones << minixsb->s_log_zone_size;
- goto done;
- }
-
- /* Try ext2 */
- n = ext2_image_size(buf);
- if (n) {
- printk(KERN_NOTICE
- "RAMDISK: ext2 filesystem found at block %d\n",
- start_block);
- nblocks = n;
- goto done;
- }
-
- printk(KERN_NOTICE
- "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
- start_block);
-
-done:
- kfree(buf);
- return nblocks;
-}
-
static unsigned long nr_blocks(struct file *file)
{
- struct inode *inode = file->f_mapping->host;
-
- if (!S_ISBLK(inode->i_mode))
- return 0;
- return i_size_read(inode) >> 10;
+ return i_size_read(file->f_mapping->host) >> 10;
}

int __init rd_load_image(char *from)
@@ -190,10 +26,11 @@ int __init rd_load_image(char *from)
int nblocks, i;
char *buf = NULL;
unsigned short rotate = 0;
- decompress_fn decompressor = NULL;
#if !defined(CONFIG_S390)
char rotator[4] = { '|' , '/' , '-' , '\\' };
#endif
+ struct file *in_file, *out_file;
+ loff_t in_pos = 0, out_pos = 0;

out_file = filp_open("/dev/ram", O_RDWR, 0);
if (IS_ERR(out_file))
@@ -203,17 +40,6 @@ int __init rd_load_image(char *from)
if (IS_ERR(in_file))
goto noclose_input;

- in_pos = rd_image_start * BLOCK_SIZE;
- nblocks = identify_ramdisk_image(in_file, in_pos, &decompressor);
- if (nblocks < 0)
- goto done;
-
- if (nblocks == 0) {
- if (crd_load(decompressor) == 0)
- goto successful_load;
- goto done;
- }
-
/*
* NOTE NOTE: nblocks is not actually blocks but
* the number of kibibytes of data to load into a ramdisk.
@@ -228,10 +54,7 @@ int __init rd_load_image(char *from)
/*
* OK, time to copy in the data
*/
- if (strcmp(from, "/initrd.image") == 0)
- devblocks = nblocks;
- else
- devblocks = nr_blocks(in_file);
+ nblocks = devblocks = nr_blocks(in_file);

if (devblocks == 0) {
printk(KERN_ERR "RAMDISK: could not determine device size\n");
@@ -264,7 +87,6 @@ int __init rd_load_image(char *from)
}
pr_cont("done.\n");

-successful_load:
res = 1;
done:
fput(in_file);
@@ -275,60 +97,3 @@ int __init rd_load_image(char *from)
init_unlink("/dev/ram");
return res;
}
-
-int __init rd_load_disk(int n)
-{
- create_dev("/dev/root", ROOT_DEV);
- create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, n));
- return rd_load_image("/dev/root");
-}
-
-static int exit_code;
-static int decompress_error;
-
-static long __init compr_fill(void *buf, unsigned long len)
-{
- long r = kernel_read(in_file, buf, len, &in_pos);
- if (r < 0)
- printk(KERN_ERR "RAMDISK: error while reading compressed data");
- else if (r == 0)
- printk(KERN_ERR "RAMDISK: EOF while reading compressed data");
- return r;
-}
-
-static long __init compr_flush(void *window, unsigned long outcnt)
-{
- long written = kernel_write(out_file, window, outcnt, &out_pos);
- if (written != outcnt) {
- if (decompress_error == 0)
- printk(KERN_ERR
- "RAMDISK: incomplete write (%ld != %ld)\n",
- written, outcnt);
- decompress_error = 1;
- return -1;
- }
- return outcnt;
-}
-
-static void __init error(char *x)
-{
- printk(KERN_ERR "%s\n", x);
- exit_code = 1;
- decompress_error = 1;
-}
-
-static int __init crd_load(decompress_fn deco)
-{
- int result;
-
- if (!deco) {
- pr_emerg("Invalid ramdisk decompression routine. "
- "Select appropriate config option.\n");
- panic("Could not decompress initial ramdisk image.");
- }
-
- result = deco(NULL, 0, compr_fill, compr_flush, NULL, NULL, error);
- if (decompress_error)
- result = 1;
- return result;
-}
--
2.34.1