0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

根文件系統(tǒng)的含義和相關(guān)重要概念以及加載代碼分析

電子工程師 ? 來源:互聯(lián)網(wǎng) ? 作者:佚名 ? 2018-01-23 08:54 ? 次閱讀

簡單的來說,根文件系統(tǒng)包括虛擬根文件系統(tǒng)和真實根文件系統(tǒng)。在Kernel啟動的初始階段,首先去創(chuàng)建虛擬的根文件系統(tǒng),接下來再去調(diào)用do_mount來加載真正的文件系統(tǒng),并將根文件系統(tǒng)切換到真正的文件系統(tǒng),也即真實的文件系統(tǒng)。

一.什么是根文件系統(tǒng)

在傳統(tǒng)的Windows機器上目錄結(jié)構(gòu)中,可能會包括C:或者D:盤,而他們一般就稱之為特定邏輯磁盤的根目錄。從文件系統(tǒng)的層面來說,每一個分區(qū)都包含了一個根目錄區(qū),也即系統(tǒng)中存在多個根目錄。

但是,在Linux系統(tǒng)中,目錄結(jié)構(gòu)與Windows上有較大的不同。系統(tǒng)中只有一個根目錄,路徑是“/”,而其它的分區(qū)只是掛載在根目錄中的一個文件夾,如“/proc”和“system”等,這里的“/”就是Linux中的根目錄。

對應(yīng)根目錄也就存在一個根目錄文件系統(tǒng)的概念,我們可以將某一個分區(qū)掛載為根目錄文件系統(tǒng),如6410公版中就將mtdblk2掛載為根目錄文件系統(tǒng)。程序中可以通過U-Boot給Kernel指定參數(shù)或者編譯選項來指定,如目前的開發(fā)板中就通過如下的編譯選項來制定根目錄文件系統(tǒng):

CONFIG_CMDLINE="console=ttyS0,115200 mem=108M rdinit=/linuxrc root=/dev/mtdblock2"

簡單的來說,根目錄文件系統(tǒng)就是一種目錄結(jié)構(gòu),包括了Linux啟動的時候所必須的一些目錄結(jié)構(gòu)和重要文件。

根文件系統(tǒng)有兩種,一種是虛擬根文件系統(tǒng),另外一種是真實的根文件系統(tǒng)。一般情況下,會首先在虛擬的根文件系統(tǒng)中做一部分工作,然后切換到真實的根文件系統(tǒng)下面。

籠統(tǒng)的來說,虛擬的根文件系統(tǒng)包括三種類型,即Initramfs、cpio-initrd和image-initrd。

二.相關(guān)重要概念

1. Initrd

Initrd是在Linux中普遍采用的一種技術(shù),就是由Bootloader加載的內(nèi)存盤。在系統(tǒng)啟動的過程中,首先會執(zhí)行Initrd中的“某一個文件”來完成驅(qū)動模塊加載的任務(wù),第二階段才會執(zhí)行真正的根文件系統(tǒng)中的/sbin/init。這里提到的第一階段是為第二階段服務(wù)的,主要是用來加載根文件系統(tǒng)以及根文件系統(tǒng)存儲介質(zhì)的驅(qū)動程序。

注:之所以稱之為“某一個文件”,是因為這里文件的名字因為操作系統(tǒng)的版本不同而不同

資料中提到,存在多種類型的Initrd,實際應(yīng)用中包括無Initrd、Linux Kernel和Initrd打包、Linux Kernel和Initrd分離以及RAMDisk Initrd。

目前,手中項目采用的就是第四種策略。在系統(tǒng)啟動的時候,U-Boot會將Linux Kernel和Rootfs加載到內(nèi)存,并跳轉(zhuǎn)到Linux Kernel的入口地址執(zhí)行程序。這篇文章將側(cè)重對該種情況進行分析。

三.根文件系統(tǒng)加載代碼分析

1. VFS的注冊

首先不得不從老掉牙的Linux系統(tǒng)的函數(shù)start_kernel()說起。函數(shù)start_kernel()中會去調(diào)用vfs_caches_init()來初始化VFS。

下面看一下函數(shù)vfs_caches_init ()的代碼:

void __init vfs_caches_init(unsigned long mempages)

{

unsigned long reserve;

/* Base hash sizes on available memory, with a reserve equal to

150% of current kernel size */

reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);

mempages -= reserve;

names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,

SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);

dcache_init();

inode_init();

files_init(mempages);

[1] mnt_init();

bdev_cache_init();

chrdev_init();

}

代碼【1】:vfs_caches_init()中最重要的函數(shù)。函數(shù)mnt_init()會創(chuàng)建一個rootfs,這是個虛擬的rootfs,即內(nèi)存文件系統(tǒng),后面還會指向真實的文件系統(tǒng)。

接下來看一下函數(shù)mnt_init():

Void __init mnt_init(void)

{

unsigned u;

int err;

init_rwsem(&namespace_sem);

mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),

0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);

mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);

if (!mount_hashtable)

panic("Failed to allocate mount hash table/n");

printk("Mount-cache hash table entries: %lu/n", HASH_SIZE);

for (u = 0; u < HASH_SIZE; u++)

INIT_LIST_HEAD(&mount_hashtable[u]);

err = sysfs_init();

if (err)

printk(KERN_WARNING "%s: sysfs_init error: %d/n",

__func__, err);

fs_kobj = kobject_create_and_add("fs", NULL);

if (!fs_kobj)

printk(KERN_WARNING "%s: kobj create error/n", __func__);

[1] init_rootfs();

[2] init_mount_tree();

}

代碼[1]:創(chuàng)建虛擬根文件系統(tǒng);

代碼[2]:注冊根文件系統(tǒng)。

接下來看一下函數(shù)init_mount_tree()的代碼:

static void __init init_mount_tree(void)

{

struct vfsmount *mnt;

struct mnt_namespace *ns;

struct path root;

[1] mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);

if (IS_ERR(mnt))

panic("Can't create rootfs");

ns = kmalloc(sizeof(*ns), GFP_KERNEL);

if (!ns)

panic("Can't allocate initial namespace");

atomic_set(&ns->count, 1);

INIT_LIST_HEAD(&ns->list);

init_waitqueue_head(&ns->poll);

ns->event = 0;

list_add(&mnt->mnt_list, &ns->list);

ns->root = mnt;

mnt->mnt_ns = ns;

init_task.nsproxy->mnt_ns = ns;

get_mnt_ns(ns);

root.mnt = ns->root;

root.dentry = ns->root->mnt_root;

set_fs_pwd(current->fs, &root);

[2] set_fs_root(current->fs, &root);

}

代碼[1]:創(chuàng)建虛擬文件系統(tǒng);

代碼[2]:將當(dāng)前的文件系統(tǒng)配置為根文件系統(tǒng)。

可能有人會問,為什么不直接把真實的文件系統(tǒng)配置為根文件系統(tǒng)?

答案很簡單,內(nèi)核中沒有根文件系統(tǒng)的設(shè)備驅(qū)動,如USB等存放根文件系統(tǒng)的設(shè)備驅(qū)動,而且即便你將根文件系統(tǒng)的設(shè)備驅(qū)動編譯到內(nèi)核中,此時它們還尚未加載,其實所有的Driver是由在后面的Kernel_Init線程進行加載。所以需要CPIO Initrd、Initrd和RAMDisk Initrd。另外,我們的Root設(shè)備都是以設(shè)備文件的方式指定的,如果沒有根文件系統(tǒng),設(shè)備文件怎么可能存在呢?

2. VFS的掛載

接下來,Kernel_Start會去調(diào)用rest_init()并會去創(chuàng)建系統(tǒng)中的第一個進程Kernel_Init,并由其調(diào)用所有模塊的初始化函數(shù),其中ROOTFS的初始化函數(shù)也在這個期間被調(diào)用。

函數(shù)rest_init代碼如下:

/*

* We need to finalize in a non-__init function or else race conditions

* between the root thread and the init thread may cause start_kernel to

* be reaped by free_initmem before the root thread has proceeded to

* cpu_idle.

*

* gcc-3.4 accidentally inlines this function, so use noinline.

*/

static noinline void __init_refok rest_init(void)

__releases(kernel_lock)

{

int pid;

kernel_thread(kernel_init, NULL, CLONE_FS | CLONE_SIGHAND);

numa_default_policy();

pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);

kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);

unlock_kernel();

/*

* The boot idle thread must execute schedule()

* at least once to get things moving:

*/

init_idle_bootup_task(current);

rcu_scheduler_starting();

preempt_enable_no_resched();

schedule();

preempt_disable();

/* Call into cpu_idle with preempt disabled */

cpu_idle();

}

函數(shù)Kernel_Init代碼如下:

static int __init kernel_init(void * unused)

{

lock_kernel();

/*

* init can run on any cpu.

*/

set_cpus_allowed_ptr(current, CPU_MASK_ALL_PTR);

/*

* Tell the world that we're going to be the grim

* reaper of innocent orphaned children.

*

* We don't want people to have to make incorrect

* assumptions about where in the task array this

* can be found.

*/

init_pid_ns.child_reaper = current;

cad_pid = task_pid(current);

smp_prepare_cpus(setup_max_cpus);

do_pre_smp_initcalls();

start_boot_trace();

smp_init();

sched_init_smp();

cpuset_init_smp();

[1] do_basic_setup();

/*

* check if there is an early userspace init. If yes, let it do all

* the work

*/

[2] if (!ramdisk_execute_command)

ramdisk_execute_command = "/init";

[3] 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;

}

代碼[1]:函數(shù)do_basic_setup()調(diào)用所有模塊的初始化函數(shù),包括initramfs的初始化函數(shù)populate_rootfs。這部分代碼在init/initramfs.c下面,函數(shù)populate_rootfs通過如下方式導(dǎo)出:

rootfs_initcall(populate_rootfs);

代碼[2]:ramdisk_execute_command值通過“rdinit=”指定,如果未指定,則采用默認的值/init。

代碼[3]:檢查根文件系統(tǒng)中是否存在文件ramdisk_execute_command,如果存在的話則執(zhí)行init_post(),否則執(zhí)行prepare_namespace()掛載根文件系統(tǒng)。

需要特別指出的是initramfs.c模塊的入口函數(shù)populate_rootfs()是否執(zhí)行取決于Kernel的編譯選項。參照linux/init目錄下的makefile文件,如下:

#

# Makefile for the linux kernel.

#

obj-y := main.o version.o mounts.o

ifneq ($(CONFIG_BLK_DEV_INITRD),y)

obj-y += noinitramfs.o

else

obj-$(CONFIG_BLK_DEV_INITRD) += initramfs.o

endif

obj-$(CONFIG_GENERIC_CALIBRATE_DELAY) += calibrate.o

mounts-y := do_mounts.o

mounts-$(CONFIG_BLK_DEV_RAM) += do_mounts_rd.o

mounts-$(CONFIG_BLK_DEV_INITRD) += do_mounts_initrd.o

mounts-$(CONFIG_BLK_DEV_MD) += do_mounts_md.o

主要完成Initrd的檢測工作,檢查出是CPIO Initrd還是Initramfs還是Image-Initrd還是需要在編譯的時候做如下的配置(General setupàInitramfs/initrd support):

該函數(shù)的代碼如下:

static int __init populate_rootfs(void)

{

[1] char *err = unpack_to_rootfs(__initramfs_start,

__initramfs_end - __initramfs_start, 0);

if (err)

panic(err);

[2] if (initrd_start) {

#ifdef CONFIG_BLK_DEV_RAM

int fd;

printk(KERN_INFO "checking if image is initramfs...");

[3] err = unpack_to_rootfs((char *)initrd_start,

initrd_end - initrd_start, 1);

if (!err) {

printk(" it is/n");

unpack_to_rootfs((char *)initrd_start,

initrd_end - initrd_start, 0);

free_initrd();

return 0;

}

printk("it isn't (%s); looks like an initrd/n", err);

[4] fd = sys_open("/initrd.image", O_WRONLY|O_CREAT, 0700);

if (fd >= 0) {

[5] sys_write(fd, (char *)initrd_start,

initrd_end - initrd_start);

sys_close(fd);

[6] free_initrd();

}

#else

printk(KERN_INFO "Unpacking initramfs...");

[7] err = unpack_to_rootfs((char *)initrd_start,

initrd_end - initrd_start, 0);

if (err)

panic(err);

printk(" done/n");

free_initrd();

#endif

}

return 0;

}

代碼[1]:unpack_to_rootfs顧名思義,就是解壓包到rootfs,其具有兩個功能,一個是檢測是否是屬于cpio包,另外一個就是解壓cpio包,通過最后一個參數(shù)進行控制。1:檢測,0:解壓。其實,Initramfs也是壓縮過后的CPIO文件。

資料中提到,Linux2.5中開始引入initramfs,在Linux2.6中一定存在,而且編譯的時候通過連接腳本arch/arm/kernel/vmlinux.lds將其編譯到__initramfs_start~__initramfs_end,執(zhí)行完unpack_to_rootfs后將被拷貝到根目錄。

代碼[2]:判斷是否加載了Initrd,無論對于那種格式的Initrd,即無論是CPIO-Initrd還是Image-Initrd,U-Boot都會將其拷貝到initrd_start。當(dāng)然了,如果是initramfs的情況下,該值肯定為空了。

代碼[3]:判斷加載的是不是CPIO-Initrd。

通過在這里主要用于檢測,如果是編譯到Linux Kernel的CPIO Initrd,__initramfs_end - __initramfs_start應(yīng)該是大于零的,否則為零,其實也就是通過這里來判斷是否為CPIO Initrd。

代碼[4]:如果不是CPIO-Initrd,則就是Image-Initrd,將其內(nèi)容保存到文件/initrd.image中。在根文件系統(tǒng)中創(chuàng)建文件/initrd.image。

代碼[5]:這里是對Image-Initrd提供支持的,將內(nèi)存中的initrd賦值到initrd.image中,以釋放內(nèi)存空間。

代碼[6]:釋放Initrd所占用的內(nèi)存空間。

另外,如果要支持Image-Initrd的話,必須要配置CONFIG_BLK_DEV_RAM,配置的方法上面已經(jīng)講過。

下面接著來分析函數(shù)kernel_init

static int __init kernel_init(void * unused)

{

do_basic_setup();

/*

* check if there is an early userspace init. If yes, let it do all

* the work

*/

if (!ramdisk_execute_command)

ramdisk_execute_command = "/init";

[1] 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;

}

代碼[1]:前面在對函數(shù)populate_rootfs進行分析的時候已經(jīng)知道,對于initramfs和cpio-initrd的情況,都會將文件系統(tǒng)(其實是一個VFS)解壓到根文件系統(tǒng)。如果虛擬文件系統(tǒng)中存在ramdisk_execute_command指定的文件則直接轉(zhuǎn)向init_post()來執(zhí)行,否則執(zhí)行函數(shù)prepare_namespace()。

3. 根文件系統(tǒng)的掛載

從上面的代碼分析中知道,對于Image-Initrd或者VFS(即InitRamfs或者CPIO-Initrd)中不存在文件ramdisk_execute_command的情況,則執(zhí)行prepare_namespace()。

接下來看一下函數(shù)prepare_namespace()的代碼:

/*

* Prepare the namespace - decide what/where to mount, load ramdisks, etc.

*/

void __init prepare_namespace(void)

{

int is_floppy;

[1] if (root_delay) {

printk(KERN_INFO "Waiting %dsec before mounting root device.../n",

root_delay);

ssleep(root_delay);

}

/*

* wait for the known devices to complete their probing

*

* Note: this is a potential source of long boot delays.

* For example, it is not atypical to wait 5 seconds here

* for the touchpad of a laptop to initialize.

*/

[2] wait_for_device_probe();

md_run_setup();

[3] if (saved_root_name[0]) {

root_device_name = saved_root_name;

if (!strncmp(root_device_name, "mtd", 3) ||

!strncmp(root_device_name, "ubi", 3)) {

[4] mount_block_root(root_device_name, root_mountflags);

goto out;

}

[5] ROOT_DEV = name_to_dev_t(root_device_name);

if (strncmp(root_device_name, "/dev/", 5) == 0)

root_device_name += 5;

}

[6] if (initrd_load())

goto out;

[7] /* 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();

out:

[9] sys_mount(".", "/", NULL, MS_MOVE, NULL);

[10] sys_chroot(".");

}

代碼[1]:資料中提到,對于將根文件系統(tǒng)存放到USB或者SCSI設(shè)備上的情況,Kernel需要等待這些耗費時間比較久的設(shè)備驅(qū)動加載完畢,所以這里存在一個Delay。

代碼[2]:從字面的意思來看,這里也是來等待根文件系統(tǒng)所在的設(shè)備探測函數(shù)的完成。

代碼[3]:參數(shù)saved_root_name存放的是Kernel參數(shù)root=所指定的設(shè)備文件,這點不再贅述,可以參照代碼。

代碼[4]:按照資料中的解釋,這里相當(dāng)于將saved_root_nam指定的設(shè)備進行加載。如下面?zhèn)鬟f給內(nèi)核的command line:

CONFIG_CMDLINE="console=ttyS0,115200 mem=108M rdinit=/linuxrc root=/dev/mtdblock2"

實際上就是加載/dev/mtdblock2。

代碼[5]:參數(shù)ROOT_DEV存放設(shè)備節(jié)點號。

代碼[6]:掛載initrd,這里進行的操作相當(dāng)?shù)膹?fù)雜,可以參照后續(xù)關(guān)于該函數(shù)的詳細解釋。

代碼[7]:如果指定mount_initrd為true,即沒有指定在函數(shù)initrd_load中mount的話,則在這里重新realfs的mount操作。

代碼[9]:將掛載點從當(dāng)前目錄(實際當(dāng)前的目錄在mount_root中或者在mount_block_root中指定)移到根目錄。對于上面的command line的話,當(dāng)前的目錄就是/dev/mtdblock2。

代碼[10]:將當(dāng)前目錄當(dāng)作系統(tǒng)的根目錄,至此虛擬系統(tǒng)根目錄文件系統(tǒng)切換到了實際的根目錄文件系統(tǒng)。

接下來看一下函數(shù)initrd_load()的代碼:

int __init initrd_load(void)

{

[1] if (mount_initrd) {

[2] create_dev("/dev/ram", Root_RAM0);

/*

* Load the initrd data into /dev/ram0. Execute it as initrd

* unless /dev/ram0 is supposed to be our actual root device,

* in that case the ram disk is just set up here, and gets

* mounted in the normal path.

*/

[3] if (rd_load_image("/initrd.image") && ROOT_DEV != Root_RAM0) {

sys_unlink("/initrd.image");

[4] handle_initrd();

return 1;

}

}

sys_unlink("/initrd.image");

return 0;

}

代碼[1]:可以通過Kernel的參數(shù)“noinitrd“來配置mount_initrd的值,默認為1,很少看到有項目區(qū)配置該值,所以一般情況下,mount_initrd的值應(yīng)該為1;

代碼[2]:創(chuàng)建一個Root_RAM0的設(shè)備節(jié)點/dev/ram;

代碼[3]:如果根文件設(shè)備號不是Root_RAM0則程序就會執(zhí)行代碼[4],換句話說,就是給內(nèi)核指定的參數(shù)不是/dev/ram,例如上面指定的/dev/mtdblock2設(shè)備節(jié)點肯定就不是Root_RAM0。

另外這行代碼還將文件initrd.image釋放到節(jié)點/dev/ram0,也就是對應(yīng)image-initrd的操作。

代碼[4]:函數(shù)handle_initrd主要功能是執(zhí)行Initrd中的linuxrc文件,并且將realfs的根目錄設(shè)置為當(dāng)前目錄。其實前面也已經(jīng)提到了,這些操作只對image-cpio的情況下才會去執(zhí)行。

函數(shù)handle_initrd的代碼如下:

static void __init handle_initrd(void)

{

int error;

int pid;

[1] real_root_dev = new_encode_dev(ROOT_DEV);

[2] create_dev("/dev/root.old", Root_RAM0);

/* mount initrd on rootfs' /root */

mount_block_root("/dev/root.old", root_mountflags & ~MS_RDONLY);

[3] 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 */

[4] sys_chdir("/root");

sys_mount(".", "/", NULL, MS_MOVE, NULL);

sys_chroot(".");

/*

* In case that a resume from disk is carried out by linuxrc or one of

* its children, we need to tell the freezer not to wait for us.

*/

current->flags |= PF_FREEZER_SKIP;

[5] pid = kernel_thread(do_linuxrc, "/linuxrc", SIGCHLD);

if (pid > 0)

while (pid != sys_wait4(-1, NULL, 0, NULL))

yield();

current->flags &= ~PF_FREEZER_SKIP;

/* move initrd to rootfs' /old */

sys_fchdir(old_fd);

sys_mount("/", ".", NULL, MS_MOVE, NULL);

/* switch root and cwd back to / of rootfs */

[6] sys_fchdir(root_fd);

sys_chroot(".");

sys_close(old_fd);

sys_close(root_fd);

[7] if (new_decode_dev(real_root_dev) == Root_RAM0) {

sys_chdir("/old");

return;

}

[8] ROOT_DEV = new_decode_dev(real_root_dev);

mount_root();

[9] 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);

if (error == -ENOENT)

printk("/initrd does not exist. Ignored./n");

else

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");

}

}

代碼[1]:real_root_dev為一個全局變量,用來保存realfs的設(shè)備號。

代碼[2]:調(diào)用mount_block_root將realfs加載到VFS的/root下。

代碼[3]:提取rootfs的根文件描述符并將其保存到root_fd,資料中提及其用處就是在后續(xù)調(diào)用sys_chroot到initrd的文件系統(tǒng)后,處理完init請求后,還能夠再次切回到rootfs,這一點在一份IBM官方有關(guān)cpio-initrd和image-initrd的執(zhí)行流程圖中可以看到,如下:

代碼[4]:sys_chroot到initrd文件系統(tǒng),前面已經(jīng)掛載initrd到VFS的root目錄下;

代碼[5]:執(zhí)行initrd中的linuxrc,并等待執(zhí)行結(jié)束;

代碼[6]:initrd執(zhí)行結(jié)束后,切回到rootfs,不知道為什么直接用節(jié)點切呢?

代碼[7]:如果real_root_dev直接配置為Root_RAM0,也即直接使用直接使用initrd作為realfs,改變當(dāng)前目錄到initrd中,并直接返回。

代碼[8]:執(zhí)行完Linuxrc后,realfs已經(jīng)確定,則調(diào)用mount_root將realfs掛載到VFS的/root目錄下,并將當(dāng)前的目錄配置為VFS的/root。

代碼[9]:收尾的工作,例如釋放內(nèi)存等。

4. 真實根文件系統(tǒng)掛載后的操作

下面回過頭來再看上面提到的init_post,該函數(shù)實際上是在Kernel_init中最后執(zhí)行的函數(shù)。其代碼如下:

/* This is a non __init function. Force it to be noinline otherwise gcc

* makes it inline to init() and it becomes part of init.text section

*/

static noinline int init_post(void)

{

/* need to finish all async __init code before freeing the memory */

async_synchronize_full();

free_initmem();

unlock_kernel();

mark_rodata_ro();

system_state = SYSTEM_RUNNING;

numa_default_policy();

if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)

printk(KERN_WARNING "Warning: unable to open an initial console./n");

(void) sys_dup(0);

(void) sys_dup(0);

current->signal->flags |= SIGNAL_UNKILLABLE;

if (ramdisk_execute_command) {

run_init_process(ramdisk_execute_command);

printk(KERN_WARNING "Failed to execute %s/n",

ramdisk_execute_command);

}

/*

* We try each of these until one succeeds.

*

* The Bourne shell can be used instead of init if we are

* trying to recover a really broken machine.

*/

if (execute_command) {

run_init_process(execute_command);

printk(KERN_WARNING "Failed to execute %s. Attempting "

"defaults.../n", execute_command);

}

run_init_process("/sbin/init");

run_init_process("/etc/init");

run_init_process("/bin/init");

run_init_process("/bin/sh");

panic("No init found. Try passing init= option to kernel.");

}

可以看到,在該函數(shù)的最后,以此會去搜索文件并執(zhí)行ramdisk_execute_command、execute_command、/sbin/init、/etc/init、/bin/init和/bin/sh,如果發(fā)現(xiàn)這些文件均不存在的話,則通過panic輸出錯誤命令,并將當(dāng)前的系統(tǒng)Halt在那里。

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • Linux
    +關(guān)注

    關(guān)注

    87

    文章

    11182

    瀏覽量

    208524
  • 代碼
    +關(guān)注

    關(guān)注

    30

    文章

    4700

    瀏覽量

    68114
  • 根文件系統(tǒng)
    +關(guān)注

    關(guān)注

    0

    文章

    25

    瀏覽量

    11954
  • vfs
    vfs
    +關(guān)注

    關(guān)注

    0

    文章

    13

    瀏覽量

    5241

原文標(biāo)題:為什么不直接把真實的文件系統(tǒng)配置為根文件系統(tǒng)?Linux根文件系統(tǒng)的掛載過程詳解

文章出處:【微信號:gh_c472c2199c88,微信公眾號:嵌入式微處理器】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。

收藏 人收藏

    評論

    相關(guān)推薦

    基于Buildroot的Linux系統(tǒng)構(gòu)建之文件系統(tǒng)

    基本的Linux文件系統(tǒng)是Unix文件夾目錄層次結(jié)構(gòu),skeleton是構(gòu)建文件系統(tǒng)的基礎(chǔ)。   skeleton配置入口:   S
    發(fā)表于 04-01 10:46 ?2546次閱讀

    roofs文件系統(tǒng)簡介制作(下)

    文件系統(tǒng)首先是內(nèi)核啟動時所mount的第一個文件系統(tǒng),內(nèi)核代碼映像文件保存在
    的頭像 發(fā)表于 09-18 09:26 ?1774次閱讀
    roofs<b class='flag-5'>根</b><b class='flag-5'>文件系統(tǒng)</b>簡介制作(下)

    Linux文件系統(tǒng)的組成及構(gòu)建方案

    Linux“三巨頭”已經(jīng)完成了 2 個了,就剩最后一個 rootfs(文件系統(tǒng))了,本章就來學(xué)習(xí)一下文件系統(tǒng)的組成以及如何構(gòu)建
    發(fā)表于 10-13 14:21 ?991次閱讀

    文件系統(tǒng)的制作

    在嵌入式Linux操作系統(tǒng)越中文件系統(tǒng)作為操作系統(tǒng)重要組成部分,用于控制對數(shù)據(jù)文件及設(shè)備的存取,提供對
    發(fā)表于 04-29 21:32

    文件系統(tǒng)掛載錯誤

    老師您好!1.通過配置u-boot 參數(shù),實現(xiàn):如下環(huán)境:1》 通過TFTP 加載內(nèi)核,并啟動內(nèi)核,已經(jīng)是OK的。2》 修改u-boot 參數(shù) 。內(nèi)核加載 nfs 文件系統(tǒng)情況描述:
    發(fā)表于 03-11 04:32

    Linux文件系統(tǒng)簡介

    Linux文件系統(tǒng)簡介 什么是文件   文件系統(tǒng)首先是一種
    發(fā)表于 04-21 17:01 ?5092次閱讀

    基于Cramfs的文件系統(tǒng)配置

    文件系統(tǒng)是構(gòu)建一個Linux 嵌入式系統(tǒng)重要組成部分,目前嵌入式系統(tǒng)可以選擇的
    發(fā)表于 05-25 17:07 ?23次下載

    CramFS文件系統(tǒng)的移植解析

    文件系統(tǒng)是構(gòu)建一個uCLinux嵌入式系統(tǒng)重要組成部分。目前嵌入式系統(tǒng)可以選擇的
    發(fā)表于 11-06 11:21 ?0次下載

    Xilinx Zynq制作修改文件系統(tǒng)的方法

    proc文件系統(tǒng)是用來提供內(nèi)核和進程信息的虛擬文件系統(tǒng),使用內(nèi)核自動生成的文件,加載完成一定要能查到很多信息才對。
    發(fā)表于 07-13 08:31 ?3492次閱讀

    Linux中文件系統(tǒng)分類方法

    Linux一個重要的哲學(xué)是:一切皆文件。而文件文件系統(tǒng)是密切相關(guān)的,本篇筆記我們來一起學(xué)習(xí)、理清那些令我們眼花繚亂的
    的頭像 發(fā)表于 02-17 09:30 ?2096次閱讀

    嵌入式Linux開發(fā)-busybox文件系統(tǒng)制作

    文件系統(tǒng)是內(nèi)核啟動時所掛載mount的第一個文件系統(tǒng),系統(tǒng)引導(dǎo)啟動程序會在文件系統(tǒng)掛載之后從
    的頭像 發(fā)表于 08-14 08:51 ?1709次閱讀

    rootfs文件系統(tǒng)制作

    本文記錄了文件系統(tǒng)的一些知識點,Busybox 工具的使用和 最小文件系統(tǒng)的制作。
    發(fā)表于 10-31 11:56 ?2次下載

    使用BusyBox構(gòu)建文件系統(tǒng)

    文件系統(tǒng)的構(gòu)建,是 Linux移植三大組成部分的最后一步,文件系統(tǒng)構(gòu)建好后,就構(gòu)成了一個基礎(chǔ)的、可以運行的嵌入式 Linux最小系統(tǒng)
    的頭像 發(fā)表于 04-19 11:20 ?1160次閱讀
    使用BusyBox構(gòu)建<b class='flag-5'>根</b><b class='flag-5'>文件系統(tǒng)</b>

    如何構(gòu)建Linux文件系統(tǒng)

    構(gòu)建Linux文件系統(tǒng)是一個涉及多個步驟和概念的過程,它對于Linux系統(tǒng)的啟動和運行至關(guān)重要。
    的頭像 發(fā)表于 10-05 16:47 ?161次閱讀

    Linux文件系統(tǒng)的掛載過程

    Linux文件系統(tǒng)(rootfs)是Linux系統(tǒng)中所有其他文件系統(tǒng)和目錄的起點,它是內(nèi)核啟動時掛載的第一個文件系統(tǒng)。
    的頭像 發(fā)表于 10-05 16:50 ?162次閱讀