runc create container 流程分析
1、// runc/create.go
Action: func(context *cli.Context) error
首先调用spec, err := setupSpec(context)加载配置文件config.json的内容。之后调用status, err := startcontainer(context, spec, true)进行容器的创建工作,其中最后一个布尔型的参数为true,表示进行容器的创建。
2、// runc/utils_linux.go
func startContainer(context *cli.Context, spec *specs.Spec, create bool) (int ,error)
首先调用container, err := createContainer(context, id, spec)创建容器, 之后填充runner结构r,如下所示:
r := &runner{
enableSubreaper: !context.Bool("no-subreaper"),
shouldDestroy: true,
container: container,
listenFDs: listenFDs, // if os.Getenv("LISTEN_FDS") != "" { listenFDs = activation.Files(false) }
console: context.String("console")
detach: detach,
pidFile: context.String("pid-file")
create: create,
}
最后调用r.run(&spec.Process)。
--------------------------------------------------------------------开始创建容器对象---------------------------------------------------------------------
3、// runc/utils_linux.go
func createContainer(context *cli.Context, id string, spec *specs.Spec) (libcontainer.Container, error)
首先调用config, err := specconv.CreateLibcontainerConfig(&specconv.CreateOpts{...})将配置转换为符合libcontainer要求的配置格式。之后调用factory, err := loadFactory(context)加载factory对象。最后,调用factory.Create(id, config)创建一个libcontainer的container对象。
4、// runc/utils_linux.go
// loadFactory returns the configured factory instance for execing containers
func loadFactory(context *cli.Context) (libcontainer.Factory, error)
首先获取root dir的绝对路径abs(默认为/run/runc,用于存储容器的状态)以及cgroupManager,最后调用libcontainer.New(abs, cgroupManager, libcontainer.CriuPath(context.GlobalString("criu")))来生产一个Factory实例。
5、// runc/libcontainer/factory_linux.go
// New returns a linux based container factory based in the root directory and configures the factory with the provided option funcs
func New(root string, options ...func(*LinuxFactory) error) (Factory, error)
首先填充一个LinuxFactory对象,如下所示:
l := &LinuxFactory{
Root: root,
InitArgs: []string{"/proc/self/exec", "init"},
Validator: validate.New(),
CriuPath: "criu",
}
接着调用Cgroupfs(l)将l的NewCgroupsManager设置为利用本地的cgroups filesystem implementation来创建和管理cgroups(相对应的,也可以利用systemd来)。再遍历options,来对l进行选择性配置。从这个函数的调用处可知,主要配置了cgroups和criupath。最后,返回l。
6、// runc/libcontainer/factory_linux.go
func (l *LinuxFactory) Create(id string, config *configs.Config) (Container)
该函数首先对id和config进行验证,接着获取uid, gid, containerRoot并且创建containerRoot(默认为/run/runc/container-id)。之后再创建一个FIFO文件,填充一个linuxContainer对象,如下所示:
c := &linuxContainer{
id: id,
root: containerRoot,
config: config,
initArgs: l.InitArgs,
criuPath: l.CriuPath,
cgroupManager: l.NewCgroupsManager(config.Cgroups, nil)
}
c.state = &stoppedState{c: c}
最后,return c
--------------------------------------------------------------------创建容器对象结束---------------------------------------------------------------------
7、// runc/utils_linux.go
func (r *runner) run(config *specs.Process) (int ,error)
(1)、调用process, err := newProcess(*config),根据config配置一个libcontainer.Process对象。如果r.listenFDs的数目不为0的话,扩展process.Env和process.ExtraFiles,然后获取rootuid和rootgid。
(2)、接着调用tty, err := setupIO(process, rootuid, rootgid, r.console, config.Terminal, r.detach || r.create)设置和process的IO。
(3)、调用handler := newSignalHandler(tty, r.enableSubreaper)以及之后的status, err := handler.forward(process)对来自process的SIGNAL进行处理(如果没有detach并且不是create容器的话)。
(4)、调用startFn(process),如果是create容器的话,那么startFn := r.container.Start,否则startFn := r.container.Run。因为我们是对create 容器进行分析 ,因此startFn为r.container.Start。
(5)、调用tty.ClosePostStart()关闭已经传递给容器的fds。
8、// runc/libcontainer/container_linux.go
func (c *linuxContainer) Start(process *Process)
这个函数很简单,先调用status, err := c.currentStatus()获取容器的当前状态,最后调用c.start(process, status == Stopped),在create时,此时容器的状态确实为Stopped
9、// runc/libcontainer/container_linux.go
func (c *linuxContainer) start(process *Process, isInit bool) error
首先调用parent, err := c.newParentProcess(process, isInit)获取一个parentProcess的接口类型。然后再调用parent.start()启动进程。最后,如果isInit为true,即create容器时,c.state = &createdState{c: c}, 假如c.config.Hooks != nil的话,执行一些c.config.Hooks.Poststart。(在基本的runc spec生成的config.json中hooks为空)
10、// runc/libcontainer/container_linux.go
func (c *linuxContainer) newParentProcess(p *Process, doInit bool) (parentProcess, error)
调用parentPipe, childPipe, err := newPipe()生成一个管道,接着打开rootDir, err = os.Open(c.root),即打开/run/runc/container-id。然后调用cmd, err := c.commandTemplate(p, childPipe, rootDir)配置启动命令。commandTemplate中最重要的内容为
(1)、添加了两个ExtraFiles:cmd.ExtraFiles = append(p.ExtraFiles, childPipe, rootDir)
(2)、cmd.Env = append(cmd.Env, fmt.Sprintf("_LIBCONTAINER_INITPIPE=%d", stdioFdCount+len(cmd.ExtraFiles)-2),
fmt.Sprintf("_LIBCONTAINER_STATEDIR=%d", stdioFdCount+len(cmd.ExtraFiles)-1))
最后,若为create container,则调用c.newInitProcess(p, cmd, parentPipe, childPipe, rootDir),否则调用c.newSetnsProcess(p, cmd, parentPipe, childPipe, rootDir)
11、// runc/libcontainer/container_linux.go
func (c *linuxContainer) newInitProcess(p *Process, cmd *exec.Cmd, parentPipe, childPipe, rootDir *os.File) (*initProcess, error)
(1)、首先扩展cmd.Env = append(cmd.Env, "_LIBCONTAINER_INITTYPE="+string(initStandard)) (initStandard是一个值为"standard"的string)。
(2)、接着获取config.Namespace的nsMaps,类型为map[configs.NamespaceType]string。再调用_, sharePidns := nsMaps[configs.NEWPID]
(3)、调用data, err := c.bootstrapData(c.config.Namespaces.CloneFlags(), nsMaps, "")
(4)、最后返回: return &initProcess {
cmd: cmd,
childPipe: childPipe,
parentPipe: parentPipe,
manager: c.cgroupManager,
config: c.newInitConfig(p), // 根据c和p填充结构initConfig
container: c,
process: p,
bootstrapData: data,
sharePidns: sharePidns,
rootDir: rootDir,
}
12、// runc/libcontainer/container_linux.go
// bootstrapData encodes the necessary data in netlink binary format as a io.Reader
// Consumer can write the data to a bootstrap program such as one that uses nsenter package to bootsrap the container's init process correctly
// with correct namespaces, uid/gid, mapping etc
func (c *linuxContainer) bootstrapData(cloneFlags uintptr, nsMaps map[configs.NamespaceType]string, consolePath) (io.Reader, error)
先创建一个利用r := nl.NewlinkRequest(int(InitMsg), 0)创建一个netlink message。之后,将cloneFlags,console path, namespace path等信息写入。最后,return bytes.NewReader(r.Serialize())
13、// runc/libcontainer/process_linux.go
(1)、调用p.cmd.Start启动runc init
(2)、io.Copy(p.parentPipe, p.bootstrapData) // 将bootstrapData的数据传入runc init
(3)、p.execSetns() // 获取容器init进程的pid,process = os.FindProcess(pid.Pid), p.cmd.Process = process, p.process.ops = p
(4)、fds, err := getPipeFds(p.pid()) // save the standard descriptor names before the container can potentially move them
(5)、p.setExternalDescriptors(fds)
(6)、p.manager.Apply(p.pid()) // do this before syncing with child so that no children can escape the cgroup
(7)、p.createNetworkInterfaces() // creating network interfaces
(8)、p.sendConfig() // sending config to init process, utils.WriteJSON(p.parentPipe, p.config)
(9)、进入loop,从p.parenPipe中获取同步状态procSync
loop :
for {
dec.Decode(&procSync)
switch procSync.Type {
case procReady:
... // set oom_score_adj, set rlimits, call prestart hooks
utils.WriteJSON(p.parentPipe, syncT{procRun}) // reading syncT run type
sentRun = true
case procHooks:
...
case procError:
...
}
}
....
syscall.Shutdown(int(p.parentPipe.Fd()), syscall.SHUT_WR)
-------------------------------------------------------------------------runc init 执行流程----------------------------------------------------------
1、// main_unix.go
Action: func(context *cli.Context) error
factory, _ := libcontainer.New("")
factory.StartInitialization()
2、// runc/libcontainer/factory_linux.go
// StartInitialization loads a container by opening fd from the parent to read the configuration and state
func (l *LinuxFactory) StartInitialization() (err error)
这个函数很简单,从"_LIBCONTAINER_INITPIPE"等环境变量中获取pipefd, rootfd(/run/runc/container-id), it(初始化类型,create中为standard),接着调用i, err = newContainerInit(it, pipe, rootfd),最后调用i.Init()
3、// runc/libcontainer/init_linux.go
func newContainerInit(t initType, pipe *os.File, stateDirFD int) (inter, error)
该函数很简单,先调用json.NewDecoder(pipe).Decode(&config)中从管道中获取配置信息,再调用populateProcessEnvironment(config.Env)将config中的环境变量加载到当前进程中。最后,根据initType的不同,当t为initSetns时,返回&linuxSetnsInit{config: config}。在create container时,则返回&linuxStandardInit{pipe: pipe, parentPid: syscall.Getppid(), config: config, stateDirFD: stateDirFD}
4、// runc/libcontainer/standard_init_linux.go
func (l *linuxStandardInit) Init() error
(1)、该函数先处理l.config.Config.NoNewKeyring,l.config.Console, setupNetwork, setupRoute, label.Init()
(2)、if l.config.Config.Namespaces.Contains(configs.NEWNS) -> setupRootfs(l.config.Config, console, l.pipe)
(3)、设置hostname, apparmor.ApplyProfile(...), label.SetProcessLabel(...),l.config.Config.Sysctl
(4)、调用remountReadonly(path)重新挂载ReadonlyPaths,在配置文件中为/proc/asound,/proc/bus, /proc/fs等等
(5)、调用maskPath(path)设置maskedPaths,pdeath := system.GetParentDeathSignal(), 处理l.config.NoNewPrivileges
(6)、调用syncParentReady(l.pipe) // 告诉父进程容器可以执行Execv了, 从父进程来看,create已经完成了
(7)、处理l.config.Config.Seccomp 和 l.config.NoNewPrivileges, finalizeNamespace(l.config),pdeath.Restore(), 判断syscall.Getppid()和l.parentPid是否相等,找到name, err := exec.Lookpath(l.config.Args[0]),最后l.pipe.Close(),init完成。此时create 在子进程中也完成了。
(8)、fd, err := syscall.Openat(l.stateDirFD, execFifoFilename, os.O_WRONLY|syscall.O_CLOEXEC, 0) ---> wait for the fifo to be opened on the other side before exec'ing the user process,其实此处就是在等待start命令。之后,再往fd中写一个字节,用于同步:syscall.Write(fd, []byte("0"))
(9)、调用syscall.Exec(name, l.config.Args[0:], os.Environ())执行容器命令
runc create container 流程分析的更多相关文章
- runc start container流程分析
1.runc/start.go Action: func(context *cli.Context) error 该函数首先调用container, err := getContainer(conte ...
- runv start container 流程分析
1.runv/start.go func startContainer(context *cli.Context, container, address string, config *spec.Sp ...
- docker启动报错解决及分析(Cannot create container for service *******: cannot mount volume over existing file, file exists /var/lib/docker/overlay2/)
现象: Cannot create container for service *******: cannot mount volume over existing file, file exists ...
- u-boot 流程分析
u-boot 介绍: 对于计算机来说 , 从一开始上机通电是无法直接启动操作系统的 , 这中间需要一个引导过程 , 嵌入式Linux系统同样离不开引导程序 , 这个启动程序就叫启动加载程序(Boot ...
- ofbiz进击 。 ofbiz 退货流程(包含获取可退货项流程分析 以及 取消退货项的过程分析)
根据订单获取可退货项流程分析 退货的时候,调用 services_return.xml 中的获取可进行退货的退货项 getReturnableItems ,该服务调用了Java类 org.ofbi ...
- Android7.0 Phone应用源码分析(一) phone拨号流程分析
1.1 dialer拨号 拨号盘点击拨号DialpadFragment的onClick方法会被调用 public void onClick(View view) { int resId = view. ...
- Android 4.4KitKat AudioRecord 流程分析
Android是架构分为三层: 底层 Linux Kernel 中间层 主要由C++实现 (Android 60%源码都是C++实现) 应用层 主要由JAVA开发的应用程序 应用程序执行 ...
- HDFS2.x之RPC流程分析
HDFS2.x之RPC流程分析 1 概述 Hadoop提供了一个统一的RPC机制来处理client-namenode, namenode-dataname,client-dataname之间的通信.R ...
- hadoop运行流程分析源代码级
前言: 最近一直在分析hadoop的运行流程,我们查阅了大量的资料,虽然从感性上对这个流程有了一个认识但是我总是感觉对mapreduce的运行还是没有一个全面的认识,所以决定从源代码级别对mapred ...
随机推荐
- SqlServer根据时段统计数据
create table ST_PPTN_R_Real ( TID int primary key identity(1,1), STCD varchar(100) not null, TM date ...
- csharp: WebBrowser read baidumap
setpoint.html: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Typ ...
- phpwind的rewrite重写原理
没有深入过pw,被人问到这方面的问题,搜索了一下,发现了一篇博文,但原博客已打不开. http://www.phpsoho.com/html/document/200608/1154750694.ht ...
- NYOJ:题目529 flip
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=529 由于此题槽点太多,所以没忍住...吐槽Time: 看到这题通过率出奇的高然后愉快的进 ...
- B/S和C/S的区别(转)
一.什么是CS和BS结构? 1.C/S又称Client/Server或客户/服务器模式.服务器通常采用高性能的PC.工作站或小型机,并采用大型数据库系统,如Oracle.Sybase.Informix ...
- PowerShell与CMD在路径解析上的一点不同
对于路径含有空格的文件夹,在加入PATH环境变量时,前后往往会加上引号.这种情况,CMD可以正确识别:但是Powershell却不能加上引号,否则无法定位路径. 例如,在PS中,$env:path查看 ...
- WCF概念
WCF 概念 WCF是.NET Framework 上灵活通讯技术.在.NET 3.0推出之前,一个企业解决方案需要几种通讯方式.对于独立于平台的通讯,使用ASP.NET Web服务.对于比较高级的 ...
- 转:jQuery 常见操作实现方式
http://www.cnblogs.com/guomingfeng/articles/2038707.html 一个优秀的 JavaScript 框架,一篇 jQuery 常用方法及函数的文章留存备 ...
- 同步推是如何给未越狱的IOS设备安装任意IPA的?
工作准备: 1. 准备一台MAC 2. 拥有一份299企业证书, 然后按照下面步骤操作: 1. 把xxxx.ipa改成xxx.zip, 解压缩得到Payload文件夹 2. 替换Payload里的em ...
- Static Cell-静态TableView
使用静态TableView有两个前提,1.要在Storyboard上 2.需要使用TableViewController PS:如果需要设置不同的cell的高度不同的话,还是需要使用tableVie ...