1、runv/start.go

func startContainer(context *cli.Context, container, address string, config *spec.Spec)

该函数所做的工作很简单,首先构建CreateContainerRequest的grpc请求,如下所示:

r  := &types.CreateContainerRequest {

  Id:       container,
  BundlePath: context.String("bundle"),
  Stdin:     fmt.Sprintf("/proc/%d/fd/0", pid),
  Stdout:    fmt.Sprintf("/proc/%d/fd/1", pid),
  Stderr:    fmt.Sprintf("/proc/%d/fd/2", pid),
}

  

再通过调用c := getClient(address)获取grpc client,再通过调用c.CreateContainer(netcontext.Background(), r)将创建容器的请求发送给grpc server。

2、runv/containerd/api/grpc/server/server.go

func (s *apiServer) CreateContainer(ctx context.Context, r *types.CreateContainerRequest) (*types.CreateContainerResponse, error)

(1)、调用ocfData, err := ioutil.ReadFile(filepath.Join(r.BundlePath, "config.json"))和json.Unmarshal(ocfData, &spec)加载spec

(2)、调用c, p, err := s.sv.CreateContainer(r.Id, r.BundlePath, r.Stdin, r.Stdout, r.Stderr, &spec)

(3)、最后调用apiP := supervisorProcess2ApiProcess(p), apiC := supervisorContainer2ApiContainer(c), addApiProcess2ApiContainer(apiC, apiP)完成api的转换,并且将进程apiP添加到apiC中

3、runv/supervisor/supervisor.go

func (sv *Supervisor) CreateContainer(container, bundlePath, stdin, stdout, stderr string, spec *specs.Spec) (*Container, *Process, error)

(1)、调用hp, err := sv.getHyperPod(container, spec),获取一个hyper pod 实例

(2)、c, err := hp.createContainer(container, bundlePath, stdin, stdout, stderr, spec) 创建一个hyper container实例

(3)、sv.Containers[container] = c, 再 return c, c.Processes["init"], nil

HyperPod的结构如下所示:

type HyperPod struct {

  Containers  map[string]*Container
  Processes  map[string]*Process
  userPod    *pod.UserPod
  podStatus  *hypervisor.PodStatus
  vm     *hypervisor.Vm
  sv      *supervisor
  nslistener  *nsListener
}

  

4、runv/supervisor/supervisor.go

// find shared pod or create a new one

func (sv *Supervisor) getHyperPod(container string, spec *specs.Spec) (hp *HyperPod, err error)

(1)、当spec.Linux.Namespaces不为空时,先进行相关的处理,这里暂时不讨论。

(2)、当hp为nil时,先调用hp, err = createHyperPod(sv.Factory, spec, sv.defaultCpus, sv.defaultMemory),再调用hp.sv = sv将hypervisor和supervisor串联起来

(3)、return hp, nil

5、runv/supervisor/hyperpod.go

func createHyperPod(f factory, spec *specs.Spec, defaultCpus int, defaultMemory int) (*HyperPod, error)

(1)、先获取一个podId,再调用userPod := pod.ConvertOCF2PureUserPod(spec)(并没有container的信息)和podStatus := hypervisor.NewPod(podId, userPod, nil)

(2)、获取cpu, mem, kernel, initrd的信息,再启动虚拟机,虽然根据获得的kernel和initrd情况略有不同,最终调用都是调用hypervisor.GetVm("", boot, bool, bool),启动虚拟机

(3)、调用Response := vm.StartPod(podStatus, userPod, nil, nil)

(4)、填充HyperPod, hp := &HyperPod {

              userPod:  userPod,

              podStatus: podStatus,

              vm:    vm,

              Containers: make(map[string]*Container),

              Processes: make(map[string]*Process)

            }

(5)、调用hp.startNsListener(), 返回 return hp, nil

6、runv/supervisor/hyperpod.go

func (hp *HyperPod) createContainer(container, bundlePath, stdin, stdout, stderr string, spec *specs.Spec) (*Container, error)

(1)、inerProcessId := container + "-init",并且填充数据结构获得 c := &Container{...},p := &Process{...}

(2)、调用p.setupIO()

(3)、对各个数据结构进行关联,c.Processes["init"] = p, c.ownerPod.Processes[inerProcessId] = p, c.ownerPod.Containers[container] = c

(4)、c.run(p),return c, nil

7、runv/supervisor/container.go

func (c *Container) run(p *Process)

(1)、该函数仅仅启动一个goroutine,首先调用c.start(p)启动容器

(2)、创建e := Event{ID: c.Id, Type: EventContainerStart, Timestamp: time.Now(),},调用c.ownerPod.sv.Events.notifySubscribers(e)将event发出

(3)、调用c.wait(p)等待容器退出

(4)、创建e := Event{ID: c.Id, EventExit, Timestamp: time.Now(), PID: p.Id, Status: -1},若正常退出,则将Status设置为容器的退出码

8、runv/supervisor/container.go

func (c *Container) start(p *Process) error

(1)、创建目录/run/runv/container-id,再在其中生成文件state.go,将state := &specs.State{Version: c.Spec.Version, ID: c.Id, Pid: c.ownerPod.getNsPid(), BundlePath: c.BundlePath}写入

(2)、先调用u := pod.ConvertOCF2UserContainer(c.Spec)将spec转换为UserPod,镜像目录为u.Image,镜像的挂载目录为vmRootfs := filepath.Join(hypervisor.BaseDir, c.ownerPod.vm.Id, hypervisor.ShareDirTag, c.Id, "rootfs")(和hyperd兼容),再调用utils.Mount(u.Image, vmRootfs, c.Spec.Root.Readonly)将镜像挂载到vmRootfs。如果c.Spec.Mounts不为空的话,调用mountToRootfs(&m, vmRootfs, ""),挂载相应的目录或文件

(3)、调用info := &hypervisor.ContainerInfo{Id: c.Id, Rootfs: "rootfs", Image: pod.userVolume{Source: c.Id}, Fstype: "dir", Cmd: u.Command, Envs: envs,}

(4)、调用c.ownerPod.vm.Attach(p.stdio, c.Id, nil)连接vm, 再调用execPrestartHooks(c.Spec, state),再调用c.ownerPod.initPodNetwork(c)初始化网络

(5)、调用c.ownerPod.podStatus.AddContainer(c.Id, c.ownerPod.podStatus.Id, "", []string{}, types.S_POD_CREATED) ---> 仅仅只是将ContainerStatus添加到podStatus.Containers中

(6)、return c.ownerPod.vm.NewContainer(u, info) ---> 真正在虚拟机中完成container的创建

runv start container 流程分析的更多相关文章

  1. runc start container流程分析

    1.runc/start.go Action: func(context *cli.Context) error 该函数首先调用container, err := getContainer(conte ...

  2. runc create container 流程分析

    1.// runc/create.go Action: func(context *cli.Context) error 首先调用spec, err := setupSpec(context)加载配置 ...

  3. runv containerd 流程分析

    当runv需要启动一个容器的时候,首先需要启动containrd,作为该容器的daemon.因此,启动containerd的相关代码也是从runv/start.go开始.最终,启动containerd ...

  4. spark 启动job的流程分析

    从WordCount開始分析 编写一个样例程序 编写一个从HDFS中读取并计算wordcount的样例程序: packageorg.apache.spark.examples importorg.ap ...

  5. 8、Struts2 运行流程分析

    1.流程分析: 请求发送给 StrutsPrepareAndExecuteFilter StrutsPrepareAndExecuteFilter 询问 ActionMapper: 该请求是否是一个 ...

  6. freeswitch呼叫流程分析

    今天翻文档时发现之前整理的关于freeswitch呼叫相关的内容,写成博文分享出来也方便我以后查阅. 整体结构图 FreeswitchCore 模块加载过程 freeswitch主程序初始化时会从mo ...

  7. u-boot 流程分析

    u-boot 介绍: 对于计算机来说 , 从一开始上机通电是无法直接启动操作系统的 , 这中间需要一个引导过程 , 嵌入式Linux系统同样离不开引导程序 ,  这个启动程序就叫启动加载程序(Boot ...

  8. thttpd和cgilua安装与运行流程分析

    安装 参考如下博文安装thttpd软件 http://blog.csdn.net/21aspnet/article/details/7045845 http://blog.csdn.net/drago ...

  9. 【转】Hostapd工作流程分析

    [转]Hostapd工作流程分析 转自:http://blog.chinaunix.net/uid-30081165-id-5290531.html Hostapd是一个运行在用户态的守护进程,可以通 ...

随机推荐

  1. 译 PrestaShop开发者指南 第四篇 深入PrestaShop核心开发

    ## 访问数据库 ### 数据库结构 PrestaShop的数据库表默认带有ps_的前缀,前缀在安装时可以自定义. 所有表名都是小写,以下划线分割.当一个表表示要在两个实体间建立连接时,表名中两个实体 ...

  2. Follow me to learn what is repository pattern

    Introduction Creating a generic repository pattern in an mvc application with entity framework is th ...

  3. C++ 面向对象的三个特点--继承与封装(二)

    顺着上一篇的内容,我们继续来了解继承的基本知识. 派生类的构造函数和析构函数 派生类继承了基类的成员,但是不能继承基类的构造函数和析构函数,首先,我们了解构造函数和析构函数的执行顺序是当我们创建一个派 ...

  4. 移除NDK方法

    以下内容由:于伟建 提供 删除.project中的以下字段基本都是有cdt关键字的位置 删除.cproject然后重启eclipse,clean,重新编译我这里还有错误,就手动删了gen 删除包含cd ...

  5. SQL数据库基础(五)

    字符串函数: 时间日期函数: SELECT @@DATEFIRST AS '1st Day', DATEPART(dw, GETDATE()) AS 'Today' SELECT GETDATE() ...

  6. jquery 监控文本框键盘事件(回车事件),附常用keycode值。

    $(function(){ $(".search").keydown(function(event) { ) { //执行操作 } }) ); 完整的 key press 过程分为 ...

  7. Crash日志符号化

    1.符号化crash日志需要3样东西: 1).crash日志本身(如:example.crash),从Xcode的organizer导出或者来自Itunes Connect. 2).crash日志所对 ...

  8. CRM Look Up 解决方案

    CRM 前瑞开发中关于lookup的开发工作肯定会遇到,例如选中一个客户或者联系人后自动把相关的信息映射到相关记录上,这样可以减少用户的输入工作.我们在CRM 的映射关系中可以配置相关字段的映射可以解 ...

  9. Arcengine实现创建网络数据集札记(二)

    四 ArcEngine实现创建网络数据集 ArcEngine创建网络数据集的过程,与ArcMap设置的过程类似,主要通过六个步骤即可以实现. 1 定义网络数据集对象,并设置基本属性,包括网络数据集名称 ...

  10. oracle断电重启之ORA-01033和ORA-01172

    参考文献: ORA-01033:解决方法 数据库掉电后 ORA-01172 磁盘坏块解决方法 --尝试连接数据库prjdb C:\Documents and Settings\Administrato ...