do_mounts_initrd.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Many of the syscalls used in this file expect some of the arguments
  4. * to be __user pointers not __kernel pointers. To limit the sparse
  5. * noise, turn off sparse checking for this file.
  6. */
  7. #ifdef __CHECKER__
  8. #undef __CHECKER__
  9. #warning "Sparse checking disabled for this file"
  10. #endif
  11. #include <linux/unistd.h>
  12. #include <linux/kernel.h>
  13. #include <linux/fs.h>
  14. #include <linux/minix_fs.h>
  15. #include <linux/romfs_fs.h>
  16. #include <linux/initrd.h>
  17. #include <linux/sched.h>
  18. #include <linux/freezer.h>
  19. #include <linux/kmod.h>
  20. #include "do_mounts.h"
  21. unsigned long initrd_start, initrd_end;
  22. int initrd_below_start_ok;
  23. unsigned int real_root_dev; /* do_proc_dointvec cannot handle kdev_t */
  24. static int __initdata mount_initrd = 1;
  25. static int __init no_initrd(char *str)
  26. {
  27. mount_initrd = 0;
  28. return 1;
  29. }
  30. __setup("noinitrd", no_initrd);
  31. static int init_linuxrc(struct subprocess_info *info, struct cred *new)
  32. {
  33. sys_unshare(CLONE_FS | CLONE_FILES);
  34. /* stdin/stdout/stderr for /linuxrc */
  35. sys_open("/dev/console", O_RDWR, 0);
  36. sys_dup(0);
  37. sys_dup(0);
  38. /* move initrd over / and chdir/chroot in initrd root */
  39. sys_chdir("/root");
  40. sys_mount(".", "/", NULL, MS_MOVE, NULL);
  41. sys_chroot(".");
  42. sys_setsid();
  43. return 0;
  44. }
  45. static void __init handle_initrd(void)
  46. {
  47. struct subprocess_info *info;
  48. static char *argv[] = { "linuxrc", NULL, };
  49. extern char *envp_init[];
  50. int error;
  51. real_root_dev = new_encode_dev(ROOT_DEV);
  52. create_dev("/dev/root.old", Root_RAM0);
  53. /* mount initrd on rootfs' /root */
  54. mount_block_root("/dev/root.old", root_mountflags & ~MS_RDONLY);
  55. sys_mkdir("/old", 0700);
  56. sys_chdir("/old");
  57. /* try loading default modules from initrd */
  58. load_default_modules();
  59. /*
  60. * In case that a resume from disk is carried out by linuxrc or one of
  61. * its children, we need to tell the freezer not to wait for us.
  62. */
  63. current->flags |= PF_FREEZER_SKIP;
  64. info = call_usermodehelper_setup("/linuxrc", argv, envp_init,
  65. GFP_KERNEL, init_linuxrc, NULL, NULL);
  66. if (!info)
  67. return;
  68. call_usermodehelper_exec(info, UMH_WAIT_PROC);
  69. current->flags &= ~PF_FREEZER_SKIP;
  70. /* move initrd to rootfs' /old */
  71. sys_mount("..", ".", NULL, MS_MOVE, NULL);
  72. /* switch root and cwd back to / of rootfs */
  73. sys_chroot("..");
  74. if (new_decode_dev(real_root_dev) == Root_RAM0) {
  75. sys_chdir("/old");
  76. return;
  77. }
  78. sys_chdir("/");
  79. ROOT_DEV = new_decode_dev(real_root_dev);
  80. mount_root();
  81. printk(KERN_NOTICE "Trying to move old root to /initrd ... ");
  82. error = sys_mount("/old", "/root/initrd", NULL, MS_MOVE, NULL);
  83. if (!error)
  84. printk("okay\n");
  85. else {
  86. int fd = sys_open("/dev/root.old", O_RDWR, 0);
  87. if (error == -ENOENT)
  88. printk("/initrd does not exist. Ignored.\n");
  89. else
  90. printk("failed\n");
  91. printk(KERN_NOTICE "Unmounting old root\n");
  92. sys_umount("/old", MNT_DETACH);
  93. printk(KERN_NOTICE "Trying to free ramdisk memory ... ");
  94. if (fd < 0) {
  95. error = fd;
  96. } else {
  97. error = sys_ioctl(fd, BLKFLSBUF, 0);
  98. sys_close(fd);
  99. }
  100. printk(!error ? "okay\n" : "failed\n");
  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. sys_unlink("/initrd.image");
  115. handle_initrd();
  116. return true;
  117. }
  118. }
  119. sys_unlink("/initrd.image");
  120. return false;
  121. }