Blame view

init/do_mounts_initrd.c 3.39 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
  #include <linux/unistd.h>
  #include <linux/kernel.h>
  #include <linux/fs.h>
  #include <linux/minix_fs.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
6
7
8
  #include <linux/romfs_fs.h>
  #include <linux/initrd.h>
  #include <linux/sched.h>
7dfb71030   Nigel Cunningham   [PATCH] Add inclu...
9
  #include <linux/freezer.h>
ba4df2808   Al Viro   don't bother with...
10
  #include <linux/kmod.h>
e262e32d6   David Howells   vfs: Suppress MS_...
11
  #include <uapi/linux/mount.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
12
13
14
15
16
17
  
  #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 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
  static int __initdata mount_initrd = 1;
b1ab95c63   Florian Fainelli   arch: Make phys_i...
19
20
  phys_addr_t phys_initrd_start __initdata;
  unsigned long phys_initrd_size __initdata;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
22
23
24
25
26
27
  static int __init no_initrd(char *str)
  {
  	mount_initrd = 0;
  	return 1;
  }
  
  __setup("noinitrd", no_initrd);
694cfd87b   Ronald G. Minnich   x86/setup: Add an...
28
  static int __init early_initrdmem(char *p)
229c55ccb   Florian Fainelli   arch: Move initrd...
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  {
  	phys_addr_t start;
  	unsigned long size;
  	char *endp;
  
  	start = memparse(p, &endp);
  	if (*endp == ',') {
  		size = memparse(endp + 1, NULL);
  
  		phys_initrd_start = start;
  		phys_initrd_size = size;
  	}
  	return 0;
  }
694cfd87b   Ronald G. Minnich   x86/setup: Add an...
43
  early_param("initrdmem", early_initrdmem);
694cfd87b   Ronald G. Minnich   x86/setup: Add an...
44
45
46
47
  static int __init early_initrd(char *p)
  {
  	return early_initrdmem(p);
  }
229c55ccb   Florian Fainelli   arch: Move initrd...
48
  early_param("initrd", early_initrd);
f0ea68f13   Christoph Hellwig   initrd: mark init...
49
  static int __init init_linuxrc(struct subprocess_info *info, struct cred *new)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
50
  {
9b32105ec   Dominik Brodowski   kernel: add ksys_...
51
  	ksys_unshare(CLONE_FS | CLONE_FILES);
b49a733d6   Dominik Brodowski   init: unify openi...
52
  	console_on_rootfs();
ba4df2808   Al Viro   don't bother with...
53
  	/* move initrd over / and chdir/chroot in initrd root */
db63f1e31   Christoph Hellwig   init: add an init...
54
  	init_chdir("/root");
c60166f04   Christoph Hellwig   init: add an init...
55
  	init_mount(".", "/", NULL, MS_MOVE, NULL);
4b7ca5014   Christoph Hellwig   init: add an init...
56
  	init_chroot(".");
e2aaa9f42   Dominik Brodowski   kernel: add ksys_...
57
  	ksys_setsid();
ba4df2808   Al Viro   don't bother with...
58
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
60
61
62
  }
  
  static void __init handle_initrd(void)
  {
907ed1328   Lucas De Marchi   usermodehelper: s...
63
  	struct subprocess_info *info;
ba4df2808   Al Viro   don't bother with...
64
65
  	static char *argv[] = { "linuxrc", NULL, };
  	extern char *envp_init[];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
66
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
67

9acc17baf   Christoph Hellwig   initrd: mark init...
68
69
  	pr_warn("using deprecated initrd support, will be removed in 2021.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
70
  	real_root_dev = new_encode_dev(ROOT_DEV);
bdaf85293   Greg Kroah-Hartman   [PATCH] devfs: Re...
71
  	create_dev("/dev/root.old", Root_RAM0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
72
73
  	/* mount initrd on rootfs' /root */
  	mount_block_root("/dev/root.old", root_mountflags & ~MS_RDONLY);
83ff98c3e   Christoph Hellwig   init: add an init...
74
  	init_mkdir("/old", 0700);
db63f1e31   Christoph Hellwig   init: add an init...
75
  	init_chdir("/old");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
76

8baabde66   Rafael J. Wysocki   Freezer: Fix s2di...
77
78
79
80
81
  	/*
  	 * 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;
907ed1328   Lucas De Marchi   usermodehelper: s...
82
83
84
85
86
  	info = call_usermodehelper_setup("/linuxrc", argv, envp_init,
  					 GFP_KERNEL, init_linuxrc, NULL, NULL);
  	if (!info)
  		return;
  	call_usermodehelper_exec(info, UMH_WAIT_PROC);
8baabde66   Rafael J. Wysocki   Freezer: Fix s2di...
87
88
  
  	current->flags &= ~PF_FREEZER_SKIP;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
89
90
  
  	/* move initrd to rootfs' /old */
c60166f04   Christoph Hellwig   init: add an init...
91
  	init_mount("..", ".", NULL, MS_MOVE, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
  	/* switch root and cwd back to / of rootfs */
4b7ca5014   Christoph Hellwig   init: add an init...
93
  	init_chroot("..");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
94
95
  
  	if (new_decode_dev(real_root_dev) == Root_RAM0) {
db63f1e31   Christoph Hellwig   init: add an init...
96
  		init_chdir("/old");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
97
98
  		return;
  	}
db63f1e31   Christoph Hellwig   init: add an init...
99
  	init_chdir("/");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
100
101
102
103
  	ROOT_DEV = new_decode_dev(real_root_dev);
  	mount_root();
  
  	printk(KERN_NOTICE "Trying to move old root to /initrd ... ");
c60166f04   Christoph Hellwig   init: add an init...
104
  	error = init_mount("/old", "/root/initrd", NULL, MS_MOVE, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
105
106
107
108
  	if (!error)
  		printk("okay
  ");
  	else {
f220ab2a5   Jay Lan   [PATCH] Improper ...
109
110
111
112
113
114
  		if (error == -ENOENT)
  			printk("/initrd does not exist. Ignored.
  ");
  		else
  			printk("failed
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
115
116
  		printk(KERN_NOTICE "Unmounting old root
  ");
09267defa   Christoph Hellwig   init: add an init...
117
  		init_umount("/old", MNT_DETACH);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
118
119
  	}
  }
f057f3b22   Yaowei Bai   init/do_mounts: i...
120
  bool __init initrd_load(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
121
122
  {
  	if (mount_initrd) {
bdaf85293   Greg Kroah-Hartman   [PATCH] devfs: Re...
123
  		create_dev("/dev/ram", Root_RAM0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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) {
8fb9f73e5   Christoph Hellwig   init: add an init...
131
  			init_unlink("/initrd.image");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
132
  			handle_initrd();
f057f3b22   Yaowei Bai   init/do_mounts: i...
133
  			return true;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
134
135
  		}
  	}
8fb9f73e5   Christoph Hellwig   init: add an init...
136
  	init_unlink("/initrd.image");
f057f3b22   Yaowei Bai   init/do_mounts: i...
137
  	return false;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
138
  }