do_mounts_initrd.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 <linux/kmod.h>
  19. #include "do_mounts.h"
  20. unsigned long initrd_start, initrd_end;
  21. int initrd_below_start_ok;
  22. unsigned int real_root_dev; /* do_proc_dointvec cannot handle kdev_t */
  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_linuxrc(struct subprocess_info *info, struct cred *new)
  31. {
  32. sys_unshare(CLONE_FS | CLONE_FILES);
  33. /* move initrd over / and chdir/chroot in initrd root */
  34. sys_chdir("/root");
  35. sys_mount(".", "/", NULL, MS_MOVE, NULL);
  36. sys_chroot(".");
  37. sys_setsid();
  38. return 0;
  39. }
  40. static void __init handle_initrd(void)
  41. {
  42. static char *argv[] = { "linuxrc", NULL, };
  43. extern char *envp_init[];
  44. int error;
  45. real_root_dev = new_encode_dev(ROOT_DEV);
  46. create_dev("/dev/root.old", Root_RAM0);
  47. /* mount initrd on rootfs' /root */
  48. mount_block_root("/dev/root.old", root_mountflags & ~MS_RDONLY);
  49. sys_mkdir("/old", 0700);
  50. sys_chdir("/old");
  51. /*
  52. * In case that a resume from disk is carried out by linuxrc or one of
  53. * its children, we need to tell the freezer not to wait for us.
  54. */
  55. current->flags |= PF_FREEZER_SKIP;
  56. call_usermodehelper_fns("/linuxrc", argv, envp_init, UMH_WAIT_PROC,
  57. init_linuxrc, NULL, NULL);
  58. current->flags &= ~PF_FREEZER_SKIP;
  59. /* move initrd to rootfs' /old */
  60. sys_mount("..", ".", NULL, MS_MOVE, NULL);
  61. /* switch root and cwd back to / of rootfs */
  62. sys_chroot("..");
  63. if (new_decode_dev(real_root_dev) == Root_RAM0) {
  64. sys_chdir("/old");
  65. return;
  66. }
  67. sys_chdir("/");
  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. }