From: Julian Stecklina <julian.stecklina@xxxxxxxxxxxxxxxxxxxxx> When the initrd doesn't fit into the RAM disk, we currently just die. This is unfortunate, because users have to manually configure the RAM disk size for no good reason. It also means that the kernel command line needs to be changed for different initrds, which is sometimes cumbersome. Attempt resizing /dev/ram to fit the RAM disk size instead. This makes initrd images work a bit more like initramfs images in that they just work. Of course, this only works, because we know that /dev/ram is a RAM disk and we can resize it freely. I'm not sure whether I've used the blockdev APIs here in a sane way. If not, please advise! Signed-off-by: Julian Stecklina <julian.stecklina@xxxxxxxxxxxxxxxxxxxxx> --- init/do_mounts_rd.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/init/do_mounts_rd.c b/init/do_mounts_rd.c index ac021ae6e6fa78c7b7828a78ab2fa3af3611bef3..5ae3639765199294a07a9b9025b7b43265370896 100644 --- a/init/do_mounts_rd.c +++ b/init/do_mounts_rd.c @@ -183,6 +183,24 @@ static unsigned long nr_blocks(struct file *file) return i_size_read(inode) >> 10; } +static int resize_ramdisk(const char *devname, u64 new_size_blocks) +{ + struct block_device *bdev; + struct file *bdev_file; + + bdev_file = bdev_file_open_by_path(devname, BLK_OPEN_READ, NULL, NULL); + if (IS_ERR(bdev_file)) + goto err; + + bdev = file_bdev(bdev_file); + set_capacity(bdev->bd_disk, (new_size_blocks * BLOCK_SIZE) / SECTOR_SIZE); + + fput(bdev_file); + return 0; +err: + return -1; +} + int __init rd_load_image(char *from) { int res = 0; @@ -219,9 +237,10 @@ int __init rd_load_image(char *from) * the number of kibibytes of data to load into a ramdisk. */ rd_blocks = nr_blocks(out_file); - if (nblocks > rd_blocks) { - printk("RAMDISK: image too big! (%dKiB/%ldKiB)\n", + if (nblocks > rd_blocks && resize_ramdisk("/dev/ram", nblocks)) { + printk("RAMDISK: image too big and couldn't resize! (%dKiB/%ldKiB)\n", nblocks, rd_blocks); + goto done; } --- base-commit: 5fc31936081919a8572a3d644f3fbb258038f337 change-id: 20250320-initrd-autoresize-b1efccf75366 Best regards, -- Julian Stecklina <julian.stecklina@xxxxxxxxxxxxxxxxxxxxx>