do_mounts_initrd.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. 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. static int __init no_initrd(char *str)
  20. {
  21. mount_initrd = 0;
  22. return 1;
  23. }
  24. __setup("noinitrd", no_initrd);
  25. static int __init early_initrdmem(char *p)
  26. {
  27. phys_addr_t start;
  28. unsigned long size;
  29. char *endp;
  30. start = memparse(p, &endp);
  31. if (*endp == ',') {
  32. size = memparse(endp + 1, NULL);
  33. phys_initrd_start = start;
  34. phys_initrd_size = size;
  35. }
  36. return 0;
  37. }
  38. early_param("initrdmem", early_initrdmem);
  39. static int __init early_initrd(char *p)
  40. {
  41. return early_initrdmem(p);
  42. }
  43. early_param("initrd", early_initrd);
  44. static int __init init_linuxrc(struct subprocess_info *info, struct cred *new)
  45. {
  46. ksys_unshare(CLONE_FS | CLONE_FILES);
  47. console_on_rootfs();
  48. /* move initrd over / and chdir/chroot in initrd root */
  49. init_chdir("/root");
  50. init_mount(".", "/", NULL, MS_MOVE, NULL);
  51. init_chroot(".");
  52. ksys_setsid();
  53. return 0;
  54. }
  55. static void __init handle_initrd(void)
  56. {
  57. struct subprocess_info *info;
  58. static char *argv[] = { "linuxrc", NULL, };
  59. extern char *envp_init[];
  60. int error;
  61. pr_warn("using deprecated initrd support, will be removed in 2021.\n");
  62. real_root_dev = new_encode_dev(ROOT_DEV);
  63. create_dev("/dev/root.old", Root_RAM0);
  64. /* mount initrd on rootfs' /root */
  65. mount_block_root("/dev/root.old", root_mountflags & ~MS_RDONLY);
  66. init_mkdir("/old", 0700);
  67. init_chdir("/old");
  68. /*
  69. * In case that a resume from disk is carried out by linuxrc or one of
  70. * its children, we need to tell the freezer not to wait for us.
  71. */
  72. current->flags |= PF_FREEZER_SKIP;
  73. info = call_usermodehelper_setup("/linuxrc", argv, envp_init,
  74. GFP_KERNEL, init_linuxrc, NULL, NULL);
  75. if (!info)
  76. return;
  77. call_usermodehelper_exec(info, UMH_WAIT_PROC);
  78. current->flags &= ~PF_FREEZER_SKIP;
  79. /* move initrd to rootfs' /old */
  80. init_mount("..", ".", NULL, MS_MOVE, NULL);
  81. /* switch root and cwd back to / of rootfs */
  82. init_chroot("..");
  83. if (new_decode_dev(real_root_dev) == Root_RAM0) {
  84. init_chdir("/old");
  85. return;
  86. }
  87. init_chdir("/");
  88. ROOT_DEV = new_decode_dev(real_root_dev);
  89. mount_root();
  90. printk(KERN_NOTICE "Trying to move old root to /initrd ... ");
  91. error = init_mount("/old", "/root/initrd", NULL, MS_MOVE, NULL);
  92. if (!error)
  93. printk("okay\n");
  94. else {
  95. if (error == -ENOENT)
  96. printk("/initrd does not exist. Ignored.\n");
  97. else
  98. printk("failed\n");
  99. printk(KERN_NOTICE "Unmounting old root\n");
  100. init_umount("/old", MNT_DETACH);
  101. }
  102. }
  103. bool __init initrd_load(void)
  104. {
  105. if (mount_initrd) {
  106. create_dev("/dev/ram", Root_RAM0);
  107. /*
  108. * Load the initrd data into /dev/ram0. Execute it as initrd
  109. * unless /dev/ram0 is supposed to be our actual root device,
  110. * in that case the ram disk is just set up here, and gets
  111. * mounted in the normal path.
  112. */
  113. if (rd_load_image("/initrd.image") && ROOT_DEV != Root_RAM0) {
  114. init_unlink("/initrd.image");
  115. handle_initrd();
  116. return true;
  117. }
  118. }
  119. init_unlink("/initrd.image");
  120. return false;
  121. }