hyperstart 容器创建流程分析
hyperstart中运行的pod的核心数据结构如下所示:
struct hyper_pod {
struct hyper_interface *iface;
struct hyper_route *rt;
struct portmapping_white_list *portmap_white_lists;
char **dns;
struct list_head containers;
struct list_head exec_head;
char *hostname;
char *share_tag;
int init_pid;
uint32_t i_num;
uint32_t r_num;
uint32_t d_num;
/* how many containers are running */
uint32_t remains;
int req_destroy;
int efd;
};
1、static int hyper_start_pod(char *json, int length):
该函数首先调用hyper_parse_pod(pod, json, length),将从runv传入的json数据解析用来填充pod,pod为指向全局变量global_pod的指针。之后,再调用hyper_setup_pod(pod)进行容器创建之前的准备工作,最后调用hyper_start_containers(pod)进行容器的创建和启动的工作。
2、static int hyper_setup_pod(struct hyper_pod *pod):
该函数首先创建了一个目录"/tmp/hyper/proc",之后再依次调用hyper_setup_network(pod),hyper_setup_dns(pod),hyper_setup_shared(pod),hyper_setup_portmapping(pod),hyper_setup_pod_init(pod)进行pod的初始化工作。其中我们先主要关注hyper_setup_shared和hyper_setup_pod_init的工作。
3、static int hyper_setup_shared(struct hyper_pod *pod):
该函数的作用就是将容器的镜像挂载到SHARE_DIR(/tmp/hyper/shared)。当hypervisor为QEMU时,该函数的操作简化的来看,就是将宿主机的/var/run/hyper/vm-ID/share_dir目录挂载到SHARE_DIR下。此时,rootfs在SHARE_DIR/container-ID/之下。
4、static int hyper_setup_pod_init(struct hyper_pod *pod):
该函数的主要作用是调用clone创建一个pod进程,其中的主要逻辑如下:
int flag = CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWIPC | CLONE_NEWUTS
struct hyper_pod_arg arg = {.pod = NULL, .ctl_pipe = {-1, -1}, .pod = pod}
pod->init_pid = clone(hyper_pod_init, stack + stacksize , flags, &arg)
5、static int hyper_pod_init(void *data):
该函数的工作主要用于为SIGCHLD设置信号处理函数,重新挂载/proc目录,最后重新设置pod的hostname,最后无限等待用于处理SIGCHLD。
hyperstart中container的核心数据结构如下:
struct hyper_container {
struct list_head list;
struct hyper_exec exec;
int ns;
uint32_t code;
// configs
char *id;
char *rootfs;
char *image;
char *scsiaddr;
char *fstype;
struct volume *vols;
struct fsmap *maps;
struct sysctl *sys;
struct port *ports;
int vols_num;
int maps_num;
int sys_num;
int ports_num;
int initialize;
};
6、static int hyper_start_containers(struct hyper_pod *pod):
该函数的作用就是根据pod内container的配置信息创建container,并且在其中运行process。主要的内容为遍历pod中的container,执行hyper_setup_container(c, pod)和hyper_run_process(&c->exec)。
7、int hyper_setup_container(struct hyper_container *container, struct hyper_pod *pod):
该函数首先通过hyper_setup_container_portmapping(container, pod)和hyper_setup_pty(container),为container设置port mapping和pty,之后再调用pid = clone(hyper_setup_container_rootfs, stack + stacksize, flags, &arg) (注:flag = CLONE_NEWNS | SIGCHLD)创建容器进程。最后获取容器的mount namespace fd:
sprintf(path, "/proc/%s/ns/mnt", pid);
container->ns = open(path, O_RDONLY | O_CLOEXEC);
8、static int hyper_setup_container_rootfs(void *data):
(1)、该函数首先等待父进程打开容器进程的container->ns,之后调用hyper_enter_sandbox(arg->pod, -1),利用setns进入pod->init_pid的PID,UTS,IPC namespace,之后再fork一个子进程。(对pod->init_pid的操作不应该是在pod的PID namespace之外么?但现在是在pod 的 pid namespace中,并且已经重新挂载了/proc)
(2)、之后,两个mount命令,将根目录的模式设置为MS_PRIVATE和MS_SLAVE。接着设置root目录为"/tmp/hyper/container-id/root/",然后将SHARE_DIR挂载到root目录,此时容器的根文件系统挂载完成。之后,将当前目录切换到容器的根目录
(3)、调用函数container_setup_init_layer(container, setup_dns),对/etc, /etc/resolv.conf,/etc/hosts,/etc/hostname进行重新创建,并且将/proc/mounts重新链接到/etc/mtab。相当于创建了docker中的init-layer。之后再调用container_setup_modules,container_setup_volumes,container_setup_dns进行rootfs的初始化工作
(4)、调用chroot(".")和chdir("/")
(5)、调用container_setup_sysctl(container)和container_setup_workdir(container),根据配置文件队/proc/sys/进行配置,然后进入容器的workdir
hyperstart中exec的核心数据结构如下所示:
struct hyper_exec {
struct list_head list;
struct hyper_pod *pod;
struct hyper_event stdinev;
struct hyper_event stdoutev;
struct hyper_event stderrev;
int pid;
int ptyno;
int init;
int ptyfd;
uint8_t close_stdin_request;
uint8_t code;
uint8_t exit;
uint8_t ref;
char *container_id;
char *user;
char *group;
char **additional_groups;
int nr_additional_groups;
struct env *envs;
int envs_num;
char **argv;
int argc;
int tty;
uint64_t seq;
uint64_t errseq;
char *workdir;
}
9、int hyper_run_process(struct hyper_exec *exec)
...
struct stdio_config io = {-1, -1,-1, -1,-1, -1}
...
hyper_setup_stdio(exec, &io)
pipe2(pipe, O_CLOEXEC)
pid = fork()
if (pid == 0) {
hyper_do_exec_cmd(exec, pipe[1], &io)
}
hyper_get_type(pipe[0], &type)
hyper_setup_stdio_events(exec, &io) // 和容器建立IO
exec->pid = type
list_add_tail(&exec->list, &exec->pod->exec_head)
exec->ref++
....
10、static int hyper_do_exec_cmd(struct hyper_exec *exec, int pipe, struct stdio_config *io)
hyper_enter_sandbox(exec->pod, pipe) -> enter pidns of pod init
c = hyper_find_container(exec->pod, exec->container_id)
setns(c->ns, CLONE_NEWNS)
chdir("/")
hyper_setup_env(c->exec.envs, c->exec.envs_num)
setenv("HOME", "/root", 1)
setenv("HOSTNAME", exec->pod->hostname, 1)
if (exec->tty) {
setenv("TERM", "xterm", 1)
} else {
unsetenv("TERM")
}
hyper_exec_process(exec, io)
11、static void hyper_exec_process(struct hyper_exec *exec, struct stdio_config *io)
sigprocmask(SIG_SETMASK, &orig_mask, NULL)
exec->workdir && chdir(exec->workdir)
hyper_setup_exec_user(exec)
hyper_setup_exec_env(exec->envs, exec->envs)
setsid()
hyper_install_process_stdio(exec, io)
execvp(exec->argv[0], exec->argv)
hyperstart 容器创建流程分析的更多相关文章
- openstack之虚拟机创建流程分析
这篇博文静静的呆在草稿箱大半年了.假设不是由于某些原因被问到,以及由于忽略它而导致的损失,否则我也不知道什么时候会将它完毕.感谢这段时间经历的挫折,让我知道不足.希望你能给我更大的决心! 本文试图具体 ...
- Spring源码解析02:Spring IOC容器之XmlBeanFactory启动流程分析和源码解析
一. 前言 Spring容器主要分为两类BeanFactory和ApplicationContext,后者是基于前者的功能扩展,也就是一个基础容器和一个高级容器的区别.本篇就以BeanFactory基 ...
- Spring源码解析 | 第二篇:Spring IOC容器之XmlBeanFactory启动流程分析和源码解析
一. 前言 Spring容器主要分为两类BeanFactory和ApplicationContext,后者是基于前者的功能扩展,也就是一个基础容器和一个高级容器的区别.本篇就以BeanFactory基 ...
- 上层应用与wpa_supplicant,wpa_supplicant与kernel 相关socket创建交互分析
单独拿出来,分析以下上层应用与wpa_supplicant wpa_supplicant与kernel 的socket交互. 关联上层应用与wpa_supplicant的socket的创建.连接流 ...
- Spring IOC 容器源码分析 - 创建原始 bean 对象
1. 简介 本篇文章是上一篇文章(创建单例 bean 的过程)的延续.在上一篇文章中,我们从战略层面上领略了doCreateBean方法的全过程.本篇文章,我们就从战术的层面上,详细分析doCreat ...
- Spring IOC 容器源码分析 - 创建单例 bean 的过程
1. 简介 在上一篇文章中,我比较详细的分析了获取 bean 的方法,也就是getBean(String)的实现逻辑.对于已实例化好的单例 bean,getBean(String) 方法并不会再一次去 ...
- SpringBoot启动流程分析(六):IoC容器依赖注入
SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...
- SpringBoot启动流程分析(四):IoC容器的初始化过程
SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...
- SpringBoot框架——从SpringBoot看IoC容器初始化流程之方法分析
目录 一.概观Spring Boot 二.Spring Boot应用初始化 2.1 初始化入口 2.2 SpringApplication的run方法 2.3 方法分析 三.容器创建与初始化 3.1 ...
随机推荐
- 重新想象 Windows 8.1 Store Apps (84) - 图像处理的新特性, Share Contract 的新特性
[源码下载] 重新想象 Windows 8.1 Store Apps (84) - 图像处理的新特性, Share Contract 的新特性 作者:webabcd 介绍重新想象 Windows 8. ...
- fibonacci高精度加法
A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first t ...
- 【iOS】Quartz2D截屏
一.简单说明 在程序开发中,有时候需要截取屏幕上的某一块内容,比如捕鱼达人游戏.如图: 完成截屏功能的核心代码:- (void)renderInContext:(CGContextRef)ctx;调用 ...
- 【iOS】Quartz2D信纸条纹
一.前导程序 新建一个项目,在主控制器文件中实现以下几行代码,就能轻松的完成图片在视图中的平铺. - (void)viewDidLoad { [super viewDidLoad]; UIImage ...
- Error writing file‘frm‘(Errcode: 28)
Error writing file‘frm‘(Errcode: 28) mysql出现这个错误,表示磁盘已经满了,该增加容量了.
- PHP调用SQL Server存储过程
一.安装SQL Server Driver for PHP 在微软官网上发现了这个东西,他提供了一套PHP对MS2005/2008操作的全新函数库,并且支持UTF8,作为PHP的扩展运行.看来 ...
- c# Json Dictionary序列化和反序列化
说明:Dictionary对象本身不支持序列化和反序列化,需要定义一个继承自Dictionary, IXmlSerializable类的自定义类来实现该功能.感觉完全可以把这样的类封装到C#库中,很具 ...
- LIST-PROCESSING用法 ABAP任意时刻进行List输出_SAP
如何在SAP的Screen中编写List报表 1.相关命令LEAVE TO LIST-PROCESSING [AND RETURN TO SCREEN <nnnn>].LEAVE LIST ...
- andriod 用户名和密码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- Nunit在VS2010加载不了程序集的解决办法
本机环境: Win7 64位 旗舰版 VS2010 Nunit2.6.3 故障重现步骤: 1.在启动外部应用程序中增加C:\Program Files (x86)\NUnit 2.6.3\bin\nu ...