linux initramfs啓動原理

當linux選擇支持initramfs方式啓動,並且在initramfs source file中選擇了要打包的rootfs路徑以後,則會嘗試以initramfs方式啓動。initramfs方式會對rootfs進行壓縮,和linux kernel打包在同一個鏡像文件中。然後系統加載的時候uboot會把整個鏡像文件都加載到內存中。以該種方式加載的rootfs,是沒辦法修改flash中rootfs的數據的,掉電即會丟失。具體的kernel 配置類似如下:

下面我們分析一下具體的加載過程。

系統初始化的時候,在init進程中又如下調用:

kernel_init

      ---------->kernel_init_freeable

            --------------->do_basic_setup

                    ----------------->do_initcalls

在do_initcalls中主要完成linux各個驅動模塊的初始化。其中有個函數populate_rootfs,他是如此定義的:

rootfs_initcall(populate_rootfs);
#define fs_initcall(fn)			__define_initcall(fn, 5)
#define fs_initcall_sync(fn)		__define_initcall(fn, 5s)
#define rootfs_initcall(fn)		__define_initcall(fn, rootfs)
#define device_initcall(fn)		__define_initcall(fn, 6)

該函數最終也會被do_initcalls調用到。看一下populate_rootfs的具體定義:

static int __init populate_rootfs(void)
{
	char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
	if (err)
		panic(err);	/* Failed to decompress INTERNAL initramfs */
	if (initrd_start) { //initrd_start爲0,所以下面這支不會走到
#ifdef CONFIG_BLK_DEV_RAM
		int fd;
		printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
		err = unpack_to_rootfs((char *)initrd_start,
			initrd_end - initrd_start);
		if (!err) {
			free_initrd();
			goto done;
		} else {
			clean_rootfs();
			unpack_to_rootfs(__initramfs_start, __initramfs_size);
		}
		printk(KERN_INFO "rootfs image is not initramfs (%s)"
				"; looks like an initrd\n", err);
		fd = sys_open("/initrd.image",
			      O_WRONLY|O_CREAT, 0700);
		if (fd >= 0) {
			sys_write(fd, (char *)initrd_start,
					initrd_end - initrd_start);
			sys_close(fd);
			free_initrd();
		}
	done:
#else
		printk(KERN_INFO "Unpacking initramfs...\n");
		err = unpack_to_rootfs((char *)initrd_start,
			initrd_end - initrd_start);
		if (err)
			printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
		free_initrd();
#endif
		/*
		 * Try loading default modules from initramfs.  This gives
		 * us a chance to load before device_initcalls.
		 */
		load_default_modules();
	}
	return 0;
}

__initramfs_start是壓縮的rootfs的起始地址,__initramfs_size是rootfs的大小。unpack_to_rootfs函數負責把rootfs中的文件一個個解壓出來。系統初始化的時候,在調用init函數之前,就已經把根文件系統初始化好了,他是個基於ramfs的文件系統。所以這邊解壓這個壓縮的rootfs的時候,就是把rootfs中的文件,挨個寫到之前系統初始化好的根文件系統中。ramfs文件系統的讀寫原理可以參考這篇文章:

https://blog.csdn.net/oqqYuJi12345678/article/details/103192290

下面來大概看一下unpack_to_rootfs這個函數:

static char * __init unpack_to_rootfs(char *buf, unsigned len)
{
	int written, res;
	decompress_fn decompress;
	const char *compress_name;
	static __initdata char msg_buf[64];

	header_buf = kmalloc(110, GFP_KERNEL);
	symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
	name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);

	if (!header_buf || !symlink_buf || !name_buf)
		panic("can't allocate buffers");

	state = Start;
	this_header = 0;
	message = NULL;
	while (!message && len) {
		loff_t saved_offset = this_header;
		if (*buf == '0' && !(this_header & 3)) {
			state = Start;
			written = write_buffer(buf, len);
			buf += written;
			len -= written;
			continue;
		}
		if (!*buf) {
			buf++;
			len--;
			this_header++;
			continue;
		}
		this_header = 0;
		decompress = decompress_method(buf, len, &compress_name);//根據獲取的文件頭中的信息,判斷這個文件的壓縮方式,獲取解壓函數集
		if (decompress) {
//解壓
			res = decompress(buf, len, NULL, flush_buffer, NULL,
				   &my_inptr, error);
			if (res)
				error("decompressor failed");
		} else if (compress_name) {
			if (!message) {
				snprintf(msg_buf, sizeof msg_buf,
					 "compression method %s not configured",
					 compress_name);
				message = msg_buf;
			}
		} else
			error("junk in compressed archive");
		if (state != Reset)
			error("junk in compressed archive");
		this_header = saved_offset + my_inptr;
		buf += my_inptr;
		len -= my_inptr;
	}
	dir_utime();
	kfree(name_buf);
	kfree(symlink_buf);
	kfree(header_buf);
	return message;
}

上面函數最終會在ramfs 根文件系統中創建rootfs壓縮包中的每個文件。

這個時候我們的rootfs就已經準備好了。

在kernel_init_freeable函數中:

static noinline void __init kernel_init_freeable(void)
{
	/*
	 * Wait until kthreadd is all set-up.
	 */
	wait_for_completion(&kthreadd_done);

	/* Now the scheduler is fully set up and can do blocking allocations */
	gfp_allowed_mask = __GFP_BITS_MASK;

	/*
	 * init can allocate pages on any node
	 */
	set_mems_allowed(node_states[N_MEMORY]);
	/*
	 * init can run on any cpu.
	 */
	set_cpus_allowed_ptr(current, cpu_all_mask);

	cad_pid = task_pid(current);

	smp_prepare_cpus(setup_max_cpus);

	do_pre_smp_initcalls();
	lockup_detector_init();

	smp_init();
	sched_init_smp();

	do_basic_setup();

	/* Open the /dev/console on the rootfs, this should never fail */
	if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
		pr_err("Warning: unable to open an initial console.\n");

	(void) sys_dup(0);
	(void) sys_dup(0);
	/*
	 * check if there is an early userspace init.  If yes, let it do all
	 * the work
	 */

	if (!ramdisk_execute_command)//沒有設置ramdisk_execute_command,就設置ramdisk_execute_command爲"/init"
		ramdisk_execute_command = "/init";
//判斷/init是否存在,不存在ramdisk_execute_command被置爲空
	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..
	 */

	/* rootfs is available now, try loading default modules */
	load_default_modules();
}

該函數中對ramdisk_execute_command初始化。在initramfs方式啓動時,需要在rootfs壓縮包中包含/init文件。經過上面的過程,ramdisk_execute_command 最終被設置爲/init。一般init文件是個鏈接:

lrwxrwxrwx  1 lu lu      11 Jun 29 05:59 init -> bin/busybox*

最後一切準備完畢以後,會調用/init完成最後的初始化工作:

static int __ref kernel_init(void *unused)
{
	printk(KERN_INFO "  kernel_init.\n");
	kernel_init_freeable();
	/* need to finish all async __init code before freeing the memory */
	async_synchronize_full();
	free_initmem();
	mark_rodata_ro();
	system_state = SYSTEM_RUNNING;
	numa_default_policy();

	flush_delayed_fput();
	
	if (ramdisk_execute_command) {//在這邊執行init程序,kernel_init程序一去不復返了
		if (!run_init_process(ramdisk_execute_command))
			return 0;
		pr_err("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) {
		if (!run_init_process(execute_command))
			return 0;
		pr_err("Failed to execute %s.  Attempting defaults...\n",
			execute_command);
	}
	if (!run_init_process("/sbin/init") ||
	    !run_init_process("/etc/init") ||
	    !run_init_process("/bin/init") ||
	    !run_init_process("/bin/sh"))
		return 0;
	panic("No init found.  Try passing init= option to kernel. "
	      "See Linux Documentation/init.txt for guidance.");
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章