do_mounts_initrd.c 3.0 KB

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