do_mounts_initrd.c 3.1 KB

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