do_mounts_initrd.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. /* stdin/stdout/stderr for /linuxrc */
  34. sys_open("/dev/console", O_RDWR, 0);
  35. sys_dup(0);
  36. sys_dup(0);
  37. /* move initrd over / and chdir/chroot in initrd root */
  38. sys_chdir("/root");
  39. sys_mount(".", "/", NULL, MS_MOVE, NULL);
  40. sys_chroot(".");
  41. sys_setsid();
  42. return 0;
  43. }
  44. static void __init handle_initrd(void)
  45. {
  46. static char *argv[] = { "linuxrc", NULL, };
  47. extern char *envp_init[];
  48. int error;
  49. real_root_dev = new_encode_dev(ROOT_DEV);
  50. create_dev("/dev/root.old", Root_RAM0);
  51. /* mount initrd on rootfs' /root */
  52. mount_block_root("/dev/root.old", root_mountflags & ~MS_RDONLY);
  53. sys_mkdir("/old", 0700);
  54. sys_chdir("/old");
  55. /* try loading default modules from initrd */
  56. load_default_modules();
  57. /*
  58. * In case that a resume from disk is carried out by linuxrc or one of
  59. * its children, we need to tell the freezer not to wait for us.
  60. */
  61. current->flags |= PF_FREEZER_SKIP;
  62. call_usermodehelper_fns("/linuxrc", argv, envp_init, UMH_WAIT_PROC,
  63. init_linuxrc, NULL, NULL);
  64. current->flags &= ~PF_FREEZER_SKIP;
  65. /* move initrd to rootfs' /old */
  66. sys_mount("..", ".", NULL, MS_MOVE, NULL);
  67. /* switch root and cwd back to / of rootfs */
  68. sys_chroot("..");
  69. if (new_decode_dev(real_root_dev) == Root_RAM0) {
  70. sys_chdir("/old");
  71. return;
  72. }
  73. sys_chdir("/");
  74. ROOT_DEV = new_decode_dev(real_root_dev);
  75. mount_root();
  76. printk(KERN_NOTICE "Trying to move old root to /initrd ... ");
  77. error = sys_mount("/old", "/root/initrd", NULL, MS_MOVE, NULL);
  78. if (!error)
  79. printk("okay\n");
  80. else {
  81. int fd = sys_open("/dev/root.old", O_RDWR, 0);
  82. if (error == -ENOENT)
  83. printk("/initrd does not exist. Ignored.\n");
  84. else
  85. printk("failed\n");
  86. printk(KERN_NOTICE "Unmounting old root\n");
  87. sys_umount("/old", MNT_DETACH);
  88. printk(KERN_NOTICE "Trying to free ramdisk memory ... ");
  89. if (fd < 0) {
  90. error = fd;
  91. } else {
  92. error = sys_ioctl(fd, BLKFLSBUF, 0);
  93. sys_close(fd);
  94. }
  95. printk(!error ? "okay\n" : "failed\n");
  96. }
  97. }
  98. int __init initrd_load(void)
  99. {
  100. if (mount_initrd) {
  101. create_dev("/dev/ram", Root_RAM0);
  102. /*
  103. * Load the initrd data into /dev/ram0. Execute it as initrd
  104. * unless /dev/ram0 is supposed to be our actual root device,
  105. * in that case the ram disk is just set up here, and gets
  106. * mounted in the normal path.
  107. */
  108. if (rd_load_image("/initrd.image") && ROOT_DEV != Root_RAM0) {
  109. sys_unlink("/initrd.image");
  110. handle_initrd();
  111. return 1;
  112. }
  113. }
  114. sys_unlink("/initrd.image");
  115. return 0;
  116. }