do_mounts_initrd.c 3.0 KB

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