Back to home page

LXR

 
 

    


0001 /*
0002  * Many of the syscalls used in this file expect some of the arguments
0003  * to be __user pointers not __kernel pointers.  To limit the sparse
0004  * noise, turn off sparse checking for this file.
0005  */
0006 #ifdef __CHECKER__
0007 #undef __CHECKER__
0008 #warning "Sparse checking disabled for this file"
0009 #endif
0010 
0011 #include <linux/unistd.h>
0012 #include <linux/kernel.h>
0013 #include <linux/fs.h>
0014 #include <linux/minix_fs.h>
0015 #include <linux/romfs_fs.h>
0016 #include <linux/initrd.h>
0017 #include <linux/sched.h>
0018 #include <linux/freezer.h>
0019 #include <linux/kmod.h>
0020 
0021 #include "do_mounts.h"
0022 
0023 unsigned long initrd_start, initrd_end;
0024 int initrd_below_start_ok;
0025 unsigned int real_root_dev; /* do_proc_dointvec cannot handle kdev_t */
0026 static int __initdata mount_initrd = 1;
0027 
0028 static int __init no_initrd(char *str)
0029 {
0030     mount_initrd = 0;
0031     return 1;
0032 }
0033 
0034 __setup("noinitrd", no_initrd);
0035 
0036 static int init_linuxrc(struct subprocess_info *info, struct cred *new)
0037 {
0038     sys_unshare(CLONE_FS | CLONE_FILES);
0039     /* stdin/stdout/stderr for /linuxrc */
0040     sys_open("/dev/console", O_RDWR, 0);
0041     sys_dup(0);
0042     sys_dup(0);
0043     /* move initrd over / and chdir/chroot in initrd root */
0044     sys_chdir("/root");
0045     sys_mount(".", "/", NULL, MS_MOVE, NULL);
0046     sys_chroot(".");
0047     sys_setsid();
0048     return 0;
0049 }
0050 
0051 static void __init handle_initrd(void)
0052 {
0053     struct subprocess_info *info;
0054     static char *argv[] = { "linuxrc", NULL, };
0055     extern char *envp_init[];
0056     int error;
0057 
0058     real_root_dev = new_encode_dev(ROOT_DEV);
0059     create_dev("/dev/root.old", Root_RAM0);
0060     /* mount initrd on rootfs' /root */
0061     mount_block_root("/dev/root.old", root_mountflags & ~MS_RDONLY);
0062     sys_mkdir("/old", 0700);
0063     sys_chdir("/old");
0064 
0065     /* try loading default modules from initrd */
0066     load_default_modules();
0067 
0068     /*
0069      * In case that a resume from disk is carried out by linuxrc or one of
0070      * its children, we need to tell the freezer not to wait for us.
0071      */
0072     current->flags |= PF_FREEZER_SKIP;
0073 
0074     info = call_usermodehelper_setup("/linuxrc", argv, envp_init,
0075                      GFP_KERNEL, init_linuxrc, NULL, NULL);
0076     if (!info)
0077         return;
0078     call_usermodehelper_exec(info, UMH_WAIT_PROC);
0079 
0080     current->flags &= ~PF_FREEZER_SKIP;
0081 
0082     /* move initrd to rootfs' /old */
0083     sys_mount("..", ".", NULL, MS_MOVE, NULL);
0084     /* switch root and cwd back to / of rootfs */
0085     sys_chroot("..");
0086 
0087     if (new_decode_dev(real_root_dev) == Root_RAM0) {
0088         sys_chdir("/old");
0089         return;
0090     }
0091 
0092     sys_chdir("/");
0093     ROOT_DEV = new_decode_dev(real_root_dev);
0094     mount_root();
0095 
0096     printk(KERN_NOTICE "Trying to move old root to /initrd ... ");
0097     error = sys_mount("/old", "/root/initrd", NULL, MS_MOVE, NULL);
0098     if (!error)
0099         printk("okay\n");
0100     else {
0101         int fd = sys_open("/dev/root.old", O_RDWR, 0);
0102         if (error == -ENOENT)
0103             printk("/initrd does not exist. Ignored.\n");
0104         else
0105             printk("failed\n");
0106         printk(KERN_NOTICE "Unmounting old root\n");
0107         sys_umount("/old", MNT_DETACH);
0108         printk(KERN_NOTICE "Trying to free ramdisk memory ... ");
0109         if (fd < 0) {
0110             error = fd;
0111         } else {
0112             error = sys_ioctl(fd, BLKFLSBUF, 0);
0113             sys_close(fd);
0114         }
0115         printk(!error ? "okay\n" : "failed\n");
0116     }
0117 }
0118 
0119 bool __init initrd_load(void)
0120 {
0121     if (mount_initrd) {
0122         create_dev("/dev/ram", Root_RAM0);
0123         /*
0124          * Load the initrd data into /dev/ram0. Execute it as initrd
0125          * unless /dev/ram0 is supposed to be our actual root device,
0126          * in that case the ram disk is just set up here, and gets
0127          * mounted in the normal path.
0128          */
0129         if (rd_load_image("/initrd.image") && ROOT_DEV != Root_RAM0) {
0130             sys_unlink("/initrd.image");
0131             handle_initrd();
0132             return true;
0133         }
0134     }
0135     sys_unlink("/initrd.image");
0136     return false;
0137 }