lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250320-initrd-autoresize-v1-1-a9a5930205f8@cyberus-technology.de>
Date: Thu, 20 Mar 2025 20:46:14 +0100
From: Julian Stecklina via B4 Relay <devnull+julian.stecklina.cyberus-technology.de@...nel.org>
To: Christoph Hellwig <hch@....de>, Al Viro <viro@...iv.linux.org.uk>, 
 Linus Torvalds <torvalds@...ux-foundation.org>
Cc: Greg Kroah-Hartman <gregkh@...uxfoundation.org>, 
 "Rafael J. Wysocki" <rafael@...nel.org>, linux-fsdevel@...r.kernel.org, 
 linux-kernel@...r.kernel.org, 
 Julian Stecklina <julian.stecklina@...erus-technology.de>
Subject: [PATCH RFC] initrd: resize /dev/ram as needed

From: Julian Stecklina <julian.stecklina@...erus-technology.de>

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@...erus-technology.de>
---
 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@...erus-technology.de>



Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ