linux initramfs启动原理

当linux选择支持initramfs方式启动,并且在initramfs source file中选择了要打包的rootfs路径以后,则会尝试以initramfs方式启动。initramfs方式会对rootfs进行压缩,和linux kernel打包在同一个镜像文件中。然后系统加载的时候uboot会把整个镜像文件都加载到内存中。以该种方式加载的rootfs,是没办法修改flash中rootfs的数据的,掉电即会丢失。具体的kernel 配置类似如下:

linux initramfs启动原理_第1张图片

下面我们分析一下具体的加载过程。

系统初始化的时候,在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.");
}

 

你可能感兴趣的:(initramfs)