do_mounts_initrd.c 3.49 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8 9 10 11
/*
 * Many of the syscalls used in this file expect some of the arguments
 * to be __user pointers not __kernel pointers.  To limit the sparse
 * noise, turn off sparse checking for this file.
 */
#ifdef __CHECKER__
#undef __CHECKER__
#warning "Sparse checking disabled for this file"
#endif

Linus Torvalds's avatar
Linus Torvalds committed
12 13 14 15 16 17 18
#include <linux/unistd.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/minix_fs.h>
#include <linux/romfs_fs.h>
#include <linux/initrd.h>
#include <linux/sched.h>
19
#include <linux/freezer.h>
20
#include <linux/kmod.h>
Linus Torvalds's avatar
Linus Torvalds committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

#include "do_mounts.h"

unsigned long initrd_start, initrd_end;
int initrd_below_start_ok;
unsigned int real_root_dev;	/* do_proc_dointvec cannot handle kdev_t */
static int __initdata mount_initrd = 1;

static int __init no_initrd(char *str)
{
	mount_initrd = 0;
	return 1;
}

__setup("noinitrd", no_initrd);

37
static int init_linuxrc(struct subprocess_info *info, struct cred *new)
Linus Torvalds's avatar
Linus Torvalds committed
38
{
39
	ksys_unshare(CLONE_FS | CLONE_FILES);
40
	/* stdin/stdout/stderr for /linuxrc */
41
	ksys_open("/dev/console", O_RDWR, 0);
42 43
	ksys_dup(0);
	ksys_dup(0);
44
	/* move initrd over / and chdir/chroot in initrd root */
45
	ksys_chdir("/root");
46
	ksys_mount(".", "/", NULL, MS_MOVE, NULL);
47
	ksys_chroot(".");
48
	ksys_setsid();
49
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
50 51 52 53
}

static void __init handle_initrd(void)
{
54
	struct subprocess_info *info;
55 56
	static char *argv[] = { "linuxrc", NULL, };
	extern char *envp_init[];
Linus Torvalds's avatar
Linus Torvalds committed
57 58 59
	int error;

	real_root_dev = new_encode_dev(ROOT_DEV);
60
	create_dev("/dev/root.old", Root_RAM0);
Linus Torvalds's avatar
Linus Torvalds committed
61 62
	/* mount initrd on rootfs' /root */
	mount_block_root("/dev/root.old", root_mountflags & ~MS_RDONLY);
63
	ksys_mkdir("/old", 0700);
64
	ksys_chdir("/old");
Linus Torvalds's avatar
Linus Torvalds committed
65

66 67 68
	/* try loading default modules from initrd */
	load_default_modules();

69 70 71 72 73 74
	/*
	 * In case that a resume from disk is carried out by linuxrc or one of
	 * its children, we need to tell the freezer not to wait for us.
	 */
	current->flags |= PF_FREEZER_SKIP;

75 76 77 78 79
	info = call_usermodehelper_setup("/linuxrc", argv, envp_init,
					 GFP_KERNEL, init_linuxrc, NULL, NULL);
	if (!info)
		return;
	call_usermodehelper_exec(info, UMH_WAIT_PROC);
80 81

	current->flags &= ~PF_FREEZER_SKIP;
Linus Torvalds's avatar
Linus Torvalds committed
82 83

	/* move initrd to rootfs' /old */
84
	ksys_mount("..", ".", NULL, MS_MOVE, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
85
	/* switch root and cwd back to / of rootfs */
86
	ksys_chroot("..");
Linus Torvalds's avatar
Linus Torvalds committed
87 88

	if (new_decode_dev(real_root_dev) == Root_RAM0) {
89
		ksys_chdir("/old");
Linus Torvalds's avatar
Linus Torvalds committed
90 91 92
		return;
	}

93
	ksys_chdir("/");
Linus Torvalds's avatar
Linus Torvalds committed
94 95 96 97
	ROOT_DEV = new_decode_dev(real_root_dev);
	mount_root();

	printk(KERN_NOTICE "Trying to move old root to /initrd ... ");
98
	error = ksys_mount("/old", "/root/initrd", NULL, MS_MOVE, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
99 100 101
	if (!error)
		printk("okay\n");
	else {
102
		int fd = ksys_open("/dev/root.old", O_RDWR, 0);
103 104 105 106
		if (error == -ENOENT)
			printk("/initrd does not exist. Ignored.\n");
		else
			printk("failed\n");
Linus Torvalds's avatar
Linus Torvalds committed
107
		printk(KERN_NOTICE "Unmounting old root\n");
108
		ksys_umount("/old", MNT_DETACH);
Linus Torvalds's avatar
Linus Torvalds committed
109 110 111 112
		printk(KERN_NOTICE "Trying to free ramdisk memory ... ");
		if (fd < 0) {
			error = fd;
		} else {
113
			error = ksys_ioctl(fd, BLKFLSBUF, 0);
114
			ksys_close(fd);
Linus Torvalds's avatar
Linus Torvalds committed
115 116 117 118 119
		}
		printk(!error ? "okay\n" : "failed\n");
	}
}

120
bool __init initrd_load(void)
Linus Torvalds's avatar
Linus Torvalds committed
121 122
{
	if (mount_initrd) {
123
		create_dev("/dev/ram", Root_RAM0);
Linus Torvalds's avatar
Linus Torvalds committed
124 125 126 127 128 129 130
		/*
		 * Load the initrd data into /dev/ram0. Execute it as initrd
		 * unless /dev/ram0 is supposed to be our actual root device,
		 * in that case the ram disk is just set up here, and gets
		 * mounted in the normal path.
		 */
		if (rd_load_image("/initrd.image") && ROOT_DEV != Root_RAM0) {
131
			ksys_unlink("/initrd.image");
Linus Torvalds's avatar
Linus Torvalds committed
132
			handle_initrd();
133
			return true;
Linus Torvalds's avatar
Linus Torvalds committed
134 135
		}
	}
136
	ksys_unlink("/initrd.image");
137
	return false;
Linus Torvalds's avatar
Linus Torvalds committed
138
}