do_mounts_initrd.c 3.8 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. static __init int kernel_do_mounts_initrd_sysctls_init(void)
  30. {
  31. register_sysctl_init("kernel", kern_do_mounts_initrd_table);
  32. return 0;
  33. }
  34. late_initcall(kernel_do_mounts_initrd_sysctls_init);
  35. #endif /* CONFIG_SYSCTL */
  36. static int __init no_initrd(char *str)
  37. {
  38. mount_initrd = 0;
  39. return 1;
  40. }
  41. __setup("noinitrd", no_initrd);
  42. static int __init early_initrdmem(char *p)
  43. {
  44. phys_addr_t start;
  45. unsigned long size;
  46. char *endp;
  47. start = memparse(p, &endp);
  48. if (*endp == ',') {
  49. size = memparse(endp + 1, NULL);
  50. phys_initrd_start = start;
  51. phys_initrd_size = size;
  52. }
  53. return 0;
  54. }
  55. early_param("initrdmem", early_initrdmem);
  56. static int __init early_initrd(char *p)
  57. {
  58. return early_initrdmem(p);
  59. }
  60. early_param("initrd", early_initrd);
  61. static int __init init_linuxrc(struct subprocess_info *info, struct cred *new)
  62. {
  63. ksys_unshare(CLONE_FS | CLONE_FILES);
  64. console_on_rootfs();
  65. /* move initrd over / and chdir/chroot in initrd root */
  66. init_chdir("/root");
  67. init_mount(".", "/", NULL, MS_MOVE, NULL);
  68. init_chroot(".");
  69. ksys_setsid();
  70. return 0;
  71. }
  72. static void __init handle_initrd(char *root_device_name)
  73. {
  74. struct subprocess_info *info;
  75. static char *argv[] = { "linuxrc", NULL, };
  76. extern char *envp_init[];
  77. int error;
  78. pr_warn("using deprecated initrd support, will be removed in 2021.\n");
  79. real_root_dev = new_encode_dev(ROOT_DEV);
  80. create_dev("/dev/root.old", Root_RAM0);
  81. /* mount initrd on rootfs' /root */
  82. mount_root_generic("/dev/root.old", root_device_name,
  83. 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(root_device_name);
  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(char *root_device_name)
  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(root_device_name);
  128. return true;
  129. }
  130. }
  131. init_unlink("/initrd.image");
  132. return false;
  133. }