runv start container 流程分析

一、runv/start.gojson

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

該函數所作的工做很簡單,首先構建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。數據結構

 

二、runv/containerd/api/grpc/server/server.go函數

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

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

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

(3)、最後調用apiP := supervisorProcess2ApiProcess(p), apiC := supervisorContainer2ApiContainer(c), addApiProcess2ApiContainer(apiC, apiP)完成api的轉換,而且將進程apiP添加到apiC中blog

 

三、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
}

  

四、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

 

五、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


六、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

 

七、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設置爲容器的退出碼

 

八、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的建立

相關文章
相關標籤/搜索