*init/do_mounts.c中的prepare_namespace函数 -- 确定系统的根文件系统*/
void __init prepare_namespace(void)
{
int is_floppy;
mount_devfs();
md_run_setup();
if (saved_root_name[0]) {
root_device_name = saved_root_name;
ROOT_DEV = name_to_dev_t(root_device_name);
if (strncmp(root_device_name, "/dev/", 5) == 0)
root_device_name += 5;
}
is_floppy = MAJOR(ROOT_DEV) == FLOPPY_MAJOR;
/*[2]尝试处理initrd,见下*/
if (initrd_load())
goto out;
/*initrd_load()返回0,没有处理initrd,存在以下两种可能*/
/*或许已加载initrd但由于根设备是ramdisk而没有进行initrd处理,
则rd_doload==1, 调用rd_load_disk使用/dev/root取代上次rd_load_image中的/dev/root.old,ROOT_DEV=Root_RAM0*/
/*或许是没有initrd以致,则rd_doload==0,ROOT_DEV!=Root_RAM0*/
/*rd_doload由arch/i386/kernel/setup.c中的setup_arch函数根据boot loader的命令行参数来决定*/
if (is_floppy && rd_doload && rd_load_disk(0))
ROOT_DEV = Root_RAM0;
/*根据ROOT_DEV挂载文件系统*/
/*注意橙红色标示的两个mount_root()*/
mount_root();
/*无论有无经过initrd处理,最终为内核作最后chroot处理,确定系统的最终根文件系统*/
out:
umount_devfs("/dev");
sys_mount(".", "/", NULL, MS_MOVE, NULL);
sys_chroot(".");
security_sb_post_mountroot();
mount_devfs_fs ();
}///////////////////////////////////////////////////////////////////
/*init/do_mounts_initrd.c*/
/*根据boot loader的命令行参数来判断是否有initrd,如果没有mount_initrd=0,否则为1*/
static int __initdata mount_initrd = 1;
static int __init no_initrd(char *str)
{
mount_initrd = 0;
return 1;
}
__setup("noinitrd", no_initrd);
...
int __init initrd_load(void)
{
/*[2]如果mount_initrd=0,则跳过initrd机制,直接挂载常规的根文件系统(也就是initrd手册所说的disable two-phase system boot-up)*/
if (mount_initrd) {
create_dev("/dev/ram", Root_RAM0, NULL);
/*用rd_load_image函数把/initrd.image(也就是initrd的实际数据)挂载到/dev/ram0,接着判断根设备是否就是/dev/ram0*/
/*如果是则跳过initrd处理,按正常流程挂载根文件系统,否则用initrd_handle函数进行initrd处理*/
if (rd_load_image("/initrd.image") && ROOT_DEV != Root_RAM0) {
sys_unlink("/initrd.image");
/*[1]initrd处理函数*/
handle_initrd();
return 1;
}
}
sys_unlink("/initrd.image");
return 0;
}...............................................................
/*init/do_mounts_initrd.c*/[/b]
/*执行linuxrc(通常是脚本,由静态链接的sh来执行)*/
/*linuxrc脚本的作用通常有以下这些:*/
/*加载所需模块(比如在虚拟机中从livecd启动就应该要加载scsi模块Buslogic.o)*/
/*改变/proc/sys/kernel/real-root-dev来改变随后的正常根文件系统(见handle_initrd函数)*/
static int __init do_linuxrc(void * shell)
{
static char *argv[] = { "linuxrc", NULL, };
extern char * envp_init[];
sys_close(old_fd);sys_close(root_fd);
sys_close(0);sys_close(1);sys_close(2);
sys_setsid();
(void) sys_open("/dev/console",O_RDWR,0);
(void) sys_dup(0);
(void) sys_dup(0);
return execve(shell, argv, envp_init);
}
/*[1]核心函数handle_initrd--负责initrd的处理*/
static void __init handle_initrd(void)
{
int error;
int i, pid;
real_root_dev = new_encode_dev(ROOT_DEV);
create_dev("/dev/root.old", Root_RAM0, NULL);
/* mount initrd on rootfs' /root */
mount_block_root("/dev/root.old", root_mountflags & ~MS_RDONLY);
sys_mkdir("/old", 0700);
root_fd = sys_open("/", 0, 0);
old_fd = sys_open("/old", 0, 0);
/* move initrd over / and chdir/chroot in initrd root */
sys_chdir("/root");
sys_mount(".", "/", NULL, MS_MOVE, NULL);
sys_chroot(".");
mount_devfs_fs ();
pid = kernel_thread(do_linuxrc, "/linuxrc", SIGCHLD);
if (pid > 0) {
while (pid != sys_wait4(-1, &i, 0, 0))
yield();
}
/* move initrd to rootfs' /old */
sys_fchdir(old_fd);
sys_mount("/", ".", NULL, MS_MOVE, NULL);
/* switch root and cwd back to / of rootfs */
sys_fchdir(root_fd);
sys_chroot(".");
sys_close(old_fd);
sys_close(root_fd);
umount_devfs("/old/dev");
if (new_decode_dev(real_root_dev) == Root_RAM0) {
sys_chdir("/old");
return;
}
ROOT_DEV = new_decode_dev(real_root_dev);
mount_root();
printk(KERN_NOTICE "Trying to move old root to /initrd ... ");
error = sys_mount("/old", "/root/initrd", NULL, MS_MOVE, NULL);
if (!error)
printk("okay\n");
else {
int fd = sys_open("/dev/root.old", O_RDWR, 0);
printk("failed\n");
printk(KERN_NOTICE "Unmounting old root\n");
sys_umount("/old", MNT_DETACH);
printk(KERN_NOTICE "Trying to free ramdisk memory ... ");
if (fd < 0) {
error = fd;
} else {
error = sys_ioctl(fd, BLKFLSBUF, 0);
sys_close(fd);
}
printk(!error ? "okay\n" : "failed\n");
}
}
阅读(2572) | 评论(0) | 转发(0) |