do_mounts_initrd.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "do_mounts.h"
  12. unsigned long initrd_start, initrd_end;
  13. int initrd_below_start_ok;
  14. unsigned int real_root_dev; /* do_proc_dointvec cannot handle kdev_t */
  15. static int __initdata mount_initrd = 1;
  16. static int __init no_initrd(char *str)
  17. {
  18. mount_initrd = 0;
  19. return 1;
  20. }
  21. __setup("noinitrd", no_initrd);
  22. static int init_linuxrc(struct subprocess_info *info, struct cred *new)
  23. {
  24. ksys_unshare(CLONE_FS | CLONE_FILES);
  25. /* stdin/stdout/stderr for /linuxrc */
  26. ksys_open("/dev/console", O_RDWR, 0);
  27. ksys_dup(0);
  28. ksys_dup(0);
  29. /* move initrd over / and chdir/chroot in initrd root */
  30. ksys_chdir("/root");
  31. ksys_mount(".", "/", NULL, MS_MOVE, NULL);
  32. ksys_chroot(".");
  33. ksys_setsid();
  34. return 0;
  35. }
  36. static void __init handle_initrd(void)
  37. {
  38. struct subprocess_info *info;
  39. static char *argv[] = { "linuxrc", NULL, };
  40. extern char *envp_init[];
  41. int error;
  42. real_root_dev = new_encode_dev(ROOT_DEV);
  43. create_dev("/dev/root.old", Root_RAM0);
  44. /* mount initrd on rootfs' /root */
  45. mount_block_root("/dev/root.old", root_mountflags & ~MS_RDONLY);
  46. ksys_mkdir("/old", 0700);
  47. ksys_chdir("/old");
  48. /* try loading default modules from initrd */
  49. load_default_modules();
  50. /*
  51. * In case that a resume from disk is carried out by linuxrc or one of
  52. * its children, we need to tell the freezer not to wait for us.
  53. */
  54. current->flags |= PF_FREEZER_SKIP;
  55. info = call_usermodehelper_setup("/linuxrc", argv, envp_init,
  56. GFP_KERNEL, init_linuxrc, NULL, NULL);
  57. if (!info)
  58. return;
  59. call_usermodehelper_exec(info, UMH_WAIT_PROC);
  60. current->flags &= ~PF_FREEZER_SKIP;
  61. /* move initrd to rootfs' /old */
  62. ksys_mount("..", ".", NULL, MS_MOVE, NULL);
  63. /* switch root and cwd back to / of rootfs */
  64. ksys_chroot("..");
  65. if (new_decode_dev(real_root_dev) == Root_RAM0) {
  66. ksys_chdir("/old");
  67. return;
  68. }
  69. ksys_chdir("/");
  70. ROOT_DEV = new_decode_dev(real_root_dev);
  71. mount_root();
  72. printk(KERN_NOTICE "Trying to move old root to /initrd ... ");
  73. error = ksys_mount("/old", "/root/initrd", NULL, MS_MOVE, NULL);
  74. if (!error)
  75. printk("okay\n");
  76. else {
  77. int fd = ksys_open("/dev/root.old", O_RDWR, 0);
  78. if (error == -ENOENT)
  79. printk("/initrd does not exist. Ignored.\n");
  80. else
  81. printk("failed\n");
  82. printk(KERN_NOTICE "Unmounting old root\n");
  83. ksys_umount("/old", MNT_DETACH);
  84. printk(KERN_NOTICE "Trying to free ramdisk memory ... ");
  85. if (fd < 0) {
  86. error = fd;
  87. } else {
  88. error = ksys_ioctl(fd, BLKFLSBUF, 0);
  89. ksys_close(fd);
  90. }
  91. printk(!error ? "okay\n" : "failed\n");
  92. }
  93. }
  94. bool __init initrd_load(void)
  95. {
  96. if (mount_initrd) {
  97. create_dev("/dev/ram", Root_RAM0);
  98. /*
  99. * Load the initrd data into /dev/ram0. Execute it as initrd
  100. * unless /dev/ram0 is supposed to be our actual root device,
  101. * in that case the ram disk is just set up here, and gets
  102. * mounted in the normal path.
  103. */
  104. if (rd_load_image("/initrd.image") && ROOT_DEV != Root_RAM0) {
  105. ksys_unlink("/initrd.image");
  106. handle_initrd();
  107. return true;
  108. }
  109. }
  110. ksys_unlink("/initrd.image");
  111. return false;
  112. }