do_mounts_initrd.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Many of the syscalls used in this file expect some of the arguments
  3. * to be __user pointers not __kernel pointers. To limit the sparse
  4. * noise, turn off sparse checking for this file.
  5. */
  6. #ifdef __CHECKER__
  7. #undef __CHECKER__
  8. #warning "Sparse checking disabled for this file"
  9. #endif
  10. #include <linux/unistd.h>
  11. #include <linux/kernel.h>
  12. #include <linux/fs.h>
  13. #include <linux/minix_fs.h>
  14. #include <linux/romfs_fs.h>
  15. #include <linux/initrd.h>
  16. #include <linux/sched.h>
  17. #include <linux/freezer.h>
  18. #include "do_mounts.h"
  19. unsigned long initrd_start, initrd_end;
  20. int initrd_below_start_ok;
  21. unsigned int real_root_dev; /* do_proc_dointvec cannot handle kdev_t */
  22. static int __initdata old_fd, root_fd;
  23. static int __initdata mount_initrd = 1;
  24. static int __init no_initrd(char *str)
  25. {
  26. mount_initrd = 0;
  27. return 1;
  28. }
  29. __setup("noinitrd", no_initrd);
  30. static int __init do_linuxrc(void *_shell)
  31. {
  32. static const char *argv[] = { "linuxrc", NULL, };
  33. extern const char *envp_init[];
  34. const char *shell = _shell;
  35. sys_close(old_fd);sys_close(root_fd);
  36. sys_setsid();
  37. return kernel_execve(shell, argv, envp_init);
  38. }
  39. static void __init handle_initrd(void)
  40. {
  41. int error;
  42. int pid;
  43. real_root_dev = new_encode_dev(ROOT_DEV);
  44. create_dev("/dev/root.old", Root_RAM0);
  45. /* mount initrd on rootfs' /root */
  46. mount_block_root("/dev/root.old", root_mountflags & ~MS_RDONLY);
  47. sys_mkdir("/old", 0700);
  48. root_fd = sys_open("/", 0, 0);
  49. old_fd = sys_open("/old", 0, 0);
  50. /* move initrd over / and chdir/chroot in initrd root */
  51. sys_chdir("/root");
  52. sys_mount(".", "/", NULL, MS_MOVE, NULL);
  53. sys_chroot(".");
  54. /*
  55. * In case that a resume from disk is carried out by linuxrc or one of
  56. * its children, we need to tell the freezer not to wait for us.
  57. */
  58. current->flags |= PF_FREEZER_SKIP;
  59. pid = kernel_thread(do_linuxrc, "/linuxrc", SIGCHLD);
  60. if (pid > 0)
  61. while (pid != sys_wait4(-1, NULL, 0, NULL))
  62. yield();
  63. current->flags &= ~PF_FREEZER_SKIP;
  64. /* move initrd to rootfs' /old */
  65. sys_fchdir(old_fd);
  66. sys_mount("/", ".", NULL, MS_MOVE, NULL);
  67. /* switch root and cwd back to / of rootfs */
  68. sys_fchdir(root_fd);
  69. sys_chroot(".");
  70. sys_close(old_fd);
  71. sys_close(root_fd);
  72. if (new_decode_dev(real_root_dev) == Root_RAM0) {
  73. sys_chdir("/old");
  74. return;
  75. }
  76. ROOT_DEV = new_decode_dev(real_root_dev);
  77. mount_root();
  78. printk(KERN_NOTICE "Trying to move old root to /initrd ... ");
  79. error = sys_mount("/old", "/root/initrd", NULL, MS_MOVE, NULL);
  80. if (!error)
  81. printk("okay\n");
  82. else {
  83. int fd = sys_open("/dev/root.old", O_RDWR, 0);
  84. if (error == -ENOENT)
  85. printk("/initrd does not exist. Ignored.\n");
  86. else
  87. printk("failed\n");
  88. printk(KERN_NOTICE "Unmounting old root\n");
  89. sys_umount("/old", MNT_DETACH);
  90. printk(KERN_NOTICE "Trying to free ramdisk memory ... ");
  91. if (fd < 0) {
  92. error = fd;
  93. } else {
  94. error = sys_ioctl(fd, BLKFLSBUF, 0);
  95. sys_close(fd);
  96. }
  97. printk(!error ? "okay\n" : "failed\n");
  98. }
  99. }
  100. int __init initrd_load(void)
  101. {
  102. if (mount_initrd) {
  103. create_dev("/dev/ram", Root_RAM0);
  104. /*
  105. * Load the initrd data into /dev/ram0. Execute it as initrd
  106. * unless /dev/ram0 is supposed to be our actual root device,
  107. * in that case the ram disk is just set up here, and gets
  108. * mounted in the normal path.
  109. */
  110. if (rd_load_image("/initrd.image") && ROOT_DEV != Root_RAM0) {
  111. sys_unlink("/initrd.image");
  112. handle_initrd();
  113. return 1;
  114. }
  115. }
  116. sys_unlink("/initrd.image");
  117. return 0;
  118. }