]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | #include <linux/unistd.h> |
3 | #include <linux/kernel.h> | |
4 | #include <linux/fs.h> | |
5 | #include <linux/minix_fs.h> | |
1da177e4 LT |
6 | #include <linux/romfs_fs.h> |
7 | #include <linux/initrd.h> | |
8 | #include <linux/sched.h> | |
7dfb7103 | 9 | #include <linux/freezer.h> |
ba4df280 | 10 | #include <linux/kmod.h> |
1da177e4 LT |
11 | |
12 | #include "do_mounts.h" | |
13 | ||
14 | unsigned long initrd_start, initrd_end; | |
15 | int initrd_below_start_ok; | |
16 | unsigned int real_root_dev; /* do_proc_dointvec cannot handle kdev_t */ | |
1da177e4 LT |
17 | static int __initdata mount_initrd = 1; |
18 | ||
b1ab95c6 FF |
19 | phys_addr_t phys_initrd_start __initdata; |
20 | unsigned long phys_initrd_size __initdata; | |
21 | ||
1da177e4 LT |
22 | static int __init no_initrd(char *str) |
23 | { | |
24 | mount_initrd = 0; | |
25 | return 1; | |
26 | } | |
27 | ||
28 | __setup("noinitrd", no_initrd); | |
29 | ||
ba4df280 | 30 | static int init_linuxrc(struct subprocess_info *info, struct cred *new) |
1da177e4 | 31 | { |
9b32105e | 32 | ksys_unshare(CLONE_FS | CLONE_FILES); |
43b16820 | 33 | /* stdin/stdout/stderr for /linuxrc */ |
bae217ea | 34 | ksys_open("/dev/console", O_RDWR, 0); |
c7248321 DB |
35 | ksys_dup(0); |
36 | ksys_dup(0); | |
ba4df280 | 37 | /* move initrd over / and chdir/chroot in initrd root */ |
447016e9 | 38 | ksys_chdir("/root"); |
312db1aa | 39 | ksys_mount(".", "/", NULL, MS_MOVE, NULL); |
a16fe33a | 40 | ksys_chroot("."); |
e2aaa9f4 | 41 | ksys_setsid(); |
ba4df280 | 42 | return 0; |
1da177e4 LT |
43 | } |
44 | ||
45 | static void __init handle_initrd(void) | |
46 | { | |
907ed132 | 47 | struct subprocess_info *info; |
ba4df280 AV |
48 | static char *argv[] = { "linuxrc", NULL, }; |
49 | extern char *envp_init[]; | |
1da177e4 | 50 | int error; |
1da177e4 LT |
51 | |
52 | real_root_dev = new_encode_dev(ROOT_DEV); | |
bdaf8529 | 53 | create_dev("/dev/root.old", Root_RAM0); |
1da177e4 LT |
54 | /* mount initrd on rootfs' /root */ |
55 | mount_block_root("/dev/root.old", root_mountflags & ~MS_RDONLY); | |
0101db7a | 56 | ksys_mkdir("/old", 0700); |
447016e9 | 57 | ksys_chdir("/old"); |
1da177e4 | 58 | |
bb813f4c TH |
59 | /* try loading default modules from initrd */ |
60 | load_default_modules(); | |
61 | ||
8baabde6 RW |
62 | /* |
63 | * In case that a resume from disk is carried out by linuxrc or one of | |
64 | * its children, we need to tell the freezer not to wait for us. | |
65 | */ | |
66 | current->flags |= PF_FREEZER_SKIP; | |
67 | ||
907ed132 LDM |
68 | info = call_usermodehelper_setup("/linuxrc", argv, envp_init, |
69 | GFP_KERNEL, init_linuxrc, NULL, NULL); | |
70 | if (!info) | |
71 | return; | |
72 | call_usermodehelper_exec(info, UMH_WAIT_PROC); | |
8baabde6 RW |
73 | |
74 | current->flags &= ~PF_FREEZER_SKIP; | |
1da177e4 LT |
75 | |
76 | /* move initrd to rootfs' /old */ | |
312db1aa | 77 | ksys_mount("..", ".", NULL, MS_MOVE, NULL); |
1da177e4 | 78 | /* switch root and cwd back to / of rootfs */ |
a16fe33a | 79 | ksys_chroot(".."); |
1da177e4 LT |
80 | |
81 | if (new_decode_dev(real_root_dev) == Root_RAM0) { | |
447016e9 | 82 | ksys_chdir("/old"); |
1da177e4 LT |
83 | return; |
84 | } | |
85 | ||
447016e9 | 86 | ksys_chdir("/"); |
1da177e4 LT |
87 | ROOT_DEV = new_decode_dev(real_root_dev); |
88 | mount_root(); | |
89 | ||
90 | printk(KERN_NOTICE "Trying to move old root to /initrd ... "); | |
312db1aa | 91 | error = ksys_mount("/old", "/root/initrd", NULL, MS_MOVE, NULL); |
1da177e4 LT |
92 | if (!error) |
93 | printk("okay\n"); | |
94 | else { | |
bae217ea | 95 | int fd = ksys_open("/dev/root.old", O_RDWR, 0); |
f220ab2a JL |
96 | if (error == -ENOENT) |
97 | printk("/initrd does not exist. Ignored.\n"); | |
98 | else | |
99 | printk("failed\n"); | |
1da177e4 | 100 | printk(KERN_NOTICE "Unmounting old root\n"); |
3a18ef5c | 101 | ksys_umount("/old", MNT_DETACH); |
1da177e4 LT |
102 | printk(KERN_NOTICE "Trying to free ramdisk memory ... "); |
103 | if (fd < 0) { | |
104 | error = fd; | |
105 | } else { | |
cbb60b92 | 106 | error = ksys_ioctl(fd, BLKFLSBUF, 0); |
2ca2a09d | 107 | ksys_close(fd); |
1da177e4 LT |
108 | } |
109 | printk(!error ? "okay\n" : "failed\n"); | |
110 | } | |
111 | } | |
112 | ||
f057f3b2 | 113 | bool __init initrd_load(void) |
1da177e4 LT |
114 | { |
115 | if (mount_initrd) { | |
bdaf8529 | 116 | create_dev("/dev/ram", Root_RAM0); |
1da177e4 LT |
117 | /* |
118 | * Load the initrd data into /dev/ram0. Execute it as initrd | |
119 | * unless /dev/ram0 is supposed to be our actual root device, | |
120 | * in that case the ram disk is just set up here, and gets | |
121 | * mounted in the normal path. | |
122 | */ | |
123 | if (rd_load_image("/initrd.image") && ROOT_DEV != Root_RAM0) { | |
0f32ab8c | 124 | ksys_unlink("/initrd.image"); |
1da177e4 | 125 | handle_initrd(); |
f057f3b2 | 126 | return true; |
1da177e4 LT |
127 | } |
128 | } | |
0f32ab8c | 129 | ksys_unlink("/initrd.image"); |
f057f3b2 | 130 | return false; |
1da177e4 | 131 | } |