do_mounts_initrd.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/unistd.h>
  3. #include <linux/kernel.h>
  4. #include <linux/fs.h>
  5. #include <linux/minix_fs.h>
  6. #include <linux/romfs_fs.h>
  7. #include <linux/initrd.h>
  8. #include <linux/sched.h>
  9. #include <linux/freezer.h>
  10. #include <linux/kmod.h>
  11. #include <uapi/linux/mount.h>
  12. #include "do_mounts.h"
  13. unsigned long initrd_start, initrd_end;
  14. int initrd_below_start_ok;
  15. static unsigned int real_root_dev; /* do_proc_dointvec cannot handle kdev_t */
  16. static int __initdata mount_initrd = 1;
  17. phys_addr_t phys_initrd_start __initdata;
  18. unsigned long phys_initrd_size __initdata;
  19. #ifdef CONFIG_SYSCTL
  20. static struct ctl_table kern_do_mounts_initrd_table[] = {
  21. {
  22. .procname = "real-root-dev",
  23. .data = &real_root_dev,
  24. .maxlen = sizeof(int),
  25. .mode = 0644,
  26. .proc_handler = proc_dointvec,
  27. },
  28. { }
  29. };
  30. static __init int kernel_do_mounts_initrd_sysctls_init(void)
  31. {
  32. register_sysctl_init("kernel", kern_do_mounts_initrd_table);
  33. return 0;
  34. }
  35. late_initcall(kernel_do_mounts_initrd_sysctls_init);
  36. #endif /* CONFIG_SYSCTL */
  37. static int __init no_initrd(char *str)
  38. {
  39. mount_initrd = 0;
  40. return 1;
  41. }
  42. __setup("noinitrd", no_initrd);
  43. static int __init early_initrdmem(char *p)
  44. {
  45. phys_addr_t start;
  46. unsigned long size;
  47. char *endp;
  48. start = memparse(p, &endp);
  49. if (*endp == ',') {
  50. size = memparse(endp + 1, NULL);
  51. phys_initrd_start = start;
  52. phys_initrd_size = size;
  53. }
  54. return 0;
  55. }
  56. early_param("initrdmem", early_initrdmem);
  57. static int __init early_initrd(char *p)
  58. {
  59. return early_initrdmem(p);
  60. }
  61. early_param("initrd", early_initrd);
  62. static int __init init_linuxrc(struct subprocess_info *info, struct cred *new)
  63. {
  64. ksys_unshare(CLONE_FS | CLONE_FILES);
  65. console_on_rootfs();
  66. /* move initrd over / and chdir/chroot in initrd root */
  67. init_chdir("/root");
  68. init_mount(".", "/", NULL, MS_MOVE, NULL);
  69. init_chroot(".");
  70. ksys_setsid();
  71. return 0;
  72. }
  73. static void __init handle_initrd(void)
  74. {
  75. struct subprocess_info *info;
  76. static char *argv[] = { "linuxrc", NULL, };
  77. extern char *envp_init[];
  78. int error;
  79. pr_warn("using deprecated initrd support, will be removed in 2021.\n");
  80. real_root_dev = new_encode_dev(ROOT_DEV);
  81. create_dev("/dev/root.old", Root_RAM0);
  82. /* mount initrd on rootfs' /root */
  83. mount_block_root("/dev/root.old", root_mountflags & ~MS_RDONLY);
  84. init_mkdir("/old", 0700);
  85. init_chdir("/old");
  86. info = call_usermodehelper_setup("/linuxrc", argv, envp_init,
  87. GFP_KERNEL, init_linuxrc, NULL, NULL);
  88. if (!info)
  89. return;
  90. call_usermodehelper_exec(info, UMH_WAIT_PROC|UMH_FREEZABLE);
  91. /* move initrd to rootfs' /old */
  92. init_mount("..", ".", NULL, MS_MOVE, NULL);
  93. /* switch root and cwd back to / of rootfs */
  94. init_chroot("..");
  95. if (new_decode_dev(real_root_dev) == Root_RAM0) {
  96. init_chdir("/old");
  97. return;
  98. }
  99. init_chdir("/");
  100. ROOT_DEV = new_decode_dev(real_root_dev);
  101. mount_root();
  102. printk(KERN_NOTICE "Trying to move old root to /initrd ... ");
  103. error = init_mount("/old", "/root/initrd", NULL, MS_MOVE, NULL);
  104. if (!error)
  105. printk("okay\n");
  106. else {
  107. if (error == -ENOENT)
  108. printk("/initrd does not exist. Ignored.\n");
  109. else
  110. printk("failed\n");
  111. printk(KERN_NOTICE "Unmounting old root\n");
  112. init_umount("/old", MNT_DETACH);
  113. }
  114. }
  115. bool __init initrd_load(void)
  116. {
  117. if (mount_initrd) {
  118. create_dev("/dev/ram", Root_RAM0);
  119. /*
  120. * Load the initrd data into /dev/ram0. Execute it as initrd
  121. * unless /dev/ram0 is supposed to be our actual root device,
  122. * in that case the ram disk is just set up here, and gets
  123. * mounted in the normal path.
  124. */
  125. if (rd_load_image("/initrd.image") && ROOT_DEV != Root_RAM0) {
  126. init_unlink("/initrd.image");
  127. handle_initrd();
  128. return true;
  129. }
  130. }
  131. init_unlink("/initrd.image");
  132. return false;
  133. }