您的位置:首页 > 运维架构 > Linux

分析无法进入Linux系统的原因

2014-02-20 17:26 459 查看
上文:/article/6526516.html





static int __init kernel_init(void * unused)
{
......
 if (!ramdisk_execute_command)
 ramdisk_execute_command = "/init";
 if (sys_access((const char __user *) ramdisk_execute_command, 0) != 0){
 ramdisk_execute_command = NULL;
 prepare_namespace();
}
/*
* Ok, we have completed the initial bootup, and
* we're essentially up and running. Get rid of the
* initmem segments and start the user-mode stuff..
*/
 init_post();
 return 0;
}


如何在Source Insight中查找sys_access在哪里,直接找是找不到了,不如通过”Ctrl + /”来全局查找,查找:

SYSCALL_DEFINE2(access


这里面是对/init做一下检测,看看是否存在该文件,sys_access用来检测是否有权限访问该文件。

返回值不为0,代表无法访问该文件,就执行prepare_namespace另作准备。

如果这里可以访问/init文件,那么就直接调用该文件来进行初始化设置。

因此,可以推测,prepare_namespace与initrd中的/init的作用类似。

/*
* Prepare the namespace - decide what/where to mount, load ramdisks, etc.
*/
void __init prepare_namespace(void)
{

注释的意思,决定挂载点,加载内存盘。

 if (root_delay){
 printk(KERN_INFO "Waiting %dsec before mounting root device...\n",
root_delay);
 ssleep(root_delay);
}


是用来在挂载根文件系统之前,等待某些比较慢的IO设备准备好根设备。

root_device_name就是我们在grub中通过root=/dev/sda1设置的根文件设备。

static int __init root_dev_setup(char *line)
{
 strlcpy(saved_root_name, line, sizeof(saved_root_name));
 return 1;
}
__setup("root=", root_dev_setup);


 if (saved_root_name[0]){
 root_device_name = saved_root_name;
 if (!strncmp(root_device_name, "mtd", 3) ||
 !strncmp(root_device_name, "ubi", 3)){
 mount_block_root(root_device_name, root_mountflags);
 goto out;
}
 ROOT_DEV = name_to_dev_t(root_device_name);
 if (strncmp(root_device_name, "/dev/", 5) == 0)
 root_device_name += 5;
}

对于root=/dev/sda1,将root_device_name设置成sda1。


ROOT_DEV = name_to_dev_t(root_device_name);


在这之前,已经获取到了ROOT_DEV的描述符。

if (initrd_load())
 goto out;
/* wait for any asynchronous scanning to complete */
 if ((ROOT_DEV == 0) && root_wait){
 printk(KERN_INFO "Waiting for root device %s...\n",
 saved_root_name);
 while (driver_probe_done() != 0 ||
 (ROOT_DEV = name_to_dev_t(saved_root_name)) == 0)
 msleep(100);
 async_synchronize_full();
}
is_floppy = MAJOR(ROOT_DEV) == FLOPPY_MAJOR;
if (is_floppy && rd_doload && rd_load_disk(0))
 ROOT_DEV = Root_RAM0;
mount_root();
devtmpfs_mount("dev");
sys_mount(".", "/", NULL, MS_MOVE, NULL);
sys_chroot((const char __user __force *)".");


如果initrd_load()成功了,就跳过mount_root这一步,这表明initrd_load()如果成功,就已经将root根文件系统给挂载好了。

否则,调用mount_root/mount_block_root,尝试以各种可能的文件系统挂载根文件系统

static char * __initdata root_fs_names;
static int __init fs_names_setup(char *str)
{
 root_fs_names = str;
 return 1;
}
static unsigned int __initdata root_delay;
__setup("rootfstype=", fs_names_setup);


static int __init do_mount_root(char *name, char *fs, int flags, void *data)
{
 int err = sys_mount(name, "/root", fs, flags, data);
 if (err)
 return err;
 sys_chdir((const char __user __force *)"/root");
 ROOT_DEV = current->fs->pwd.mnt->mnt_sb->s_dev;
 printk(KERN_INFO
"VFS: Mounted root (%s filesystem)%s on device %u:%u.\n",
current->fs->pwd.mnt->mnt_sb->s_type->name,
current->fs->pwd.mnt->mnt_sb->s_flags & MS_RDONLY ?
" readonly" : "", MAJOR(ROOT_DEV), MINOR(ROOT_DEV));
 return 0;
}

因此,这里的错误在于,没有正确地执行initrd中的初始化程序,也没有成功地mount根文件系统。

明明是ext2的根文件系统,为什么没有被mount上呢?这是问题的根本原因。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: