Skip to content

Commit

Permalink
initramfs: factor out a helper to populate the initrd image
Browse files Browse the repository at this point in the history
This will allow for cleaner code sharing in the caller.

Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Christoph Hellwig <[email protected]>
Acked-by: Mike Rapoport <[email protected]>
Cc: Catalin Marinas <[email protected]>	[arm64]
Cc: Geert Uytterhoeven <[email protected]>	[m68k]
Cc: Steven Price <[email protected]>
Cc: Alexander Viro <[email protected]>
Cc: Guan Xuetao <[email protected]>
Cc: Russell King <[email protected]>
Cc: Will Deacon <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Christoph Hellwig authored and torvalds committed May 14, 2019
1 parent 23091e2 commit 7c184ec
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions init/initramfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,28 @@ static void __init clean_rootfs(void)
}
#endif

#ifdef CONFIG_BLK_DEV_RAM
static void populate_initrd_image(char *err)
{
ssize_t written;
int fd;

unpack_to_rootfs(__initramfs_start, __initramfs_size);

printk(KERN_INFO "rootfs image is not initramfs (%s); looks like an initrd\n",
err);
fd = ksys_open("/initrd.image", O_WRONLY | O_CREAT, 0700);
if (fd < 0)
return;

written = xwrite(fd, (char *)initrd_start, initrd_end - initrd_start);
if (written != initrd_end - initrd_start)
pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
written, initrd_end - initrd_start);
ksys_close(fd);
}
#endif /* CONFIG_BLK_DEV_RAM */

static int __init populate_rootfs(void)
{
/* Load the built in initramfs */
Expand All @@ -606,30 +628,14 @@ static int __init populate_rootfs(void)
/* If available load the bootloader supplied initrd */
if (initrd_start && !IS_ENABLED(CONFIG_INITRAMFS_FORCE)) {
#ifdef CONFIG_BLK_DEV_RAM
int fd;
printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
err = unpack_to_rootfs((char *)initrd_start,
initrd_end - initrd_start);
if (!err)
goto done;

clean_rootfs();
unpack_to_rootfs(__initramfs_start, __initramfs_size);

printk(KERN_INFO "rootfs image is not initramfs (%s)"
"; looks like an initrd\n", err);
fd = ksys_open("/initrd.image",
O_WRONLY|O_CREAT, 0700);
if (fd >= 0) {
ssize_t written = xwrite(fd, (char *)initrd_start,
initrd_end - initrd_start);

if (written != initrd_end - initrd_start)
pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
written, initrd_end - initrd_start);

ksys_close(fd);
}
populate_initrd_image(err);
done:
/* empty statement */;
#else
Expand Down

0 comments on commit 7c184ec

Please sign in to comment.