runv containerd 流程分析
当runv需要启动一个容器的时候,首先需要启动containrd,作为该容器的daemon。因此,启动containerd的相关代码也是从runv/start.go开始。最终,启动containerd的命令行参数如下所示:
runv --kernel /var/lib/hyper/kernel --initrd /var/lib/hyper/hyper-initrd.img --default_cpus 1 --default_memory 128 containerd
--solo-namespaced --containerd-dir /run/runv-namespace-457712864 --state-dir /run/runv --listen /run/runv-namespace-457712864/namespaced.sock
1、runv/containerd/containerd.go
Action: func(context *cli.Context)
(1)、对各个flag进行加载,包括driver, kernel, initrd,template,stateDir等等,上面的命令行都描述得很清晰了
(2)、调用hypervisor.HDriver, err = driverloader.Probe(driver)加载hypervisor的驱动,一般为qemu
(3)、调用f = factory.NewFromConfigs(kernel, initrd, nil)获得一个工厂实例
(4)、调用sv, err := supervisor.New(stateDir, containerdDir, f, context.GlobalInt("default_cpus"), context.GlobalInt("default_memory")),获得一个supervisor实例
(5)、如果指定了solo-namespaced参数,则调用go namespaceShare(sv, containerdDir, stateDir)
(6)、创建daemon,调用daemon(sv, context.String("listen"))
(7)、如果指定了solo-namespaced,则进行清除工作,os.RemoveAll(containerdDir)
2、runv/driverloader_linux.go
func Probe(driver string) (hypervisor.HypervisorDriver, error)
该函数针对不同的driver做初始化操作,这里我们只对driver为qemu的情况进行讨论,因此直接调用qd := qemu.InitDriver(),并且return qd。而qemu的InitDriver的操作很简单,仅仅是找到qemu的绝对路径,然后return &QemuDriver{executable: cmd.,}。
3、runv/factory/factory.go
func NewFromConfigs(kernel, initrd string, configs []FactoryConfig) Factory
因为在初始化的过程中configs为nil,因此仅仅只是调用return single.New(direct.New(1000000, 1000000, kernel, initrd))。其中direct.New返回了一个base.Factory,首先构建一个hypervisor.BootConfig的实例,b := hypervisor.BootConfig{CPU: cpu, Memory: mem, HotAddCpuMem: true, Kernel: kernel, Initrd: initrd}, 在包装一下b,return &directFactory{config: b}。而single.New(b base.Factory)仅仅只是返回 Factory{Factory: b}
Supervisor结构如下所示:
type Supervisor struct {
StateDir string
Factory factory.Factory
defaultCpus int
defaultMemory int
Events SvEvents
sync.RWMutex
containers map[string]*Container
}
4、runv/supervisor/supervisor.go
func New(stateDir, eventLogDir string, f factory.Factory, defaultCpus int , defaultMemory int) (*Supervisor, error)
首先创建stateDir目录和eventLogDir目录,接着填充数据结构Supervisor, sv := &Supervisor{...}。创建sv.Events.subscribers = make(map[chan Event]struct{})。接着go sv.reaper(),最后,return sv, sv.Events.setupEventLog(eventLogDir) // eventLogDir其实就是containerdDir。例如,/run/runv-namespace-457712864
5、runv/containerd/containerd.go
func namaspaceShare(sv *supervisor.Supervisor, namespace, state string)
该函数主要通过从events := sv.Events.Events(time.Time{})接收来的事件来对容器进行计数
(1)、当接收到的事件e的Type为EventContainerStart时,调用os.Symlink(namespace, filepath.Join(state, e.Id, "namespace")),链接两个容器的namespace,最后,containerCount++
(2)、当接收到的事件e的Type为EventExit并且e.Pid为init时,containerCount--,当containerCount为0时,调用syscall.Kill(0, syscall.SIGQUIT)(注:kill的pid为0,表示向进程组所在的所有进程发送信号)。最后,containerd的reaper接收到syscall.SIGQUIT时,退出。
6、runv/containerd/containerd.go
func daemon(sv *supervisor.Supervisor, address)
首先创建一个reaper, s := make(chan os.Signal, 2048),signal.Notify(s, syscall.SIGCHLD, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)。之后,调用server, err := startServer(address, sv)启动server。最后,处理来自s的信号,若信号为syscall.SIGCHLD只调用osutils.Reap()进行简单的处理,其他信号则直接调用server.Stop()关闭containerd
7、runv/containerd/containerd.go
func startServer(address string, sv *supervisor.Supervisor) (*grpc.Server, error)
该函数主要用于创建grpc server
(1)、l, err := net.Listen(defautlListenType, address)
(2)、types.RegisterAPIServer(s, server.NewServer(sv))
(3)、go func() { s.server(l) }()
-------------------------------------------------------------------------- containerd 对 event的处理---------------------------------------------------------
1、runv/supervisor/supervisor.go
func (sv *Supervisor) reaper()
首先调用events := sv.Events.Events(time.Time{})获得一个channel,用于获取container最新的状态。之后,调用一个for循环,从events中得到事件e,当e.Type 为EventExit时,调用go sv.reap(e.ID, e.PID)
2、runv/supervisor/supervisor.go
func (sv *Supervisor) reap(container, processId string)
根据container和processId,调用go p.reap(),删除c.ownerPod.Processes中和c.Processes对应的processId, 如果c中的Process为0了,则调用go c.reap(),再依次删除c.ownerPod.Containers和sv.Containers中的container。最后,若c.ownerPod.Containers为0了,则调用go c.ownerPod.reap()
3、runv/supervisor/supervisor.go
func (se *SvEvents) setupEventLog(logDir string) error
首先调用se.readEventLog(logDir),将events.log中的内容都读取到se.eventLog中,接着调用events := se.Events(time.Time{})获取events的channel,最后将channel中读取的event写入events.log中并且也添加到se.eventLog中。其实这个函数所做的工作,就是将events.log中原有的log添加到se.eventLog中,并且将新的event添加到se.eventLog和events.log中。
4、runv/supervisor/supervisor.go
// notifySubscribers will send the provided event to the external subscriber of the events channel,就是向se.subscriber中注册的各个channel发送event
func (se *SvEvents) notifySubscribers(e Event)
仅仅只是一个简单的for循环,遍历subscribers:
for sub := range se.subscribers{
select{
// do a non-blocking send for the channel (non-blocking是指,如果sub<-e阻塞,就直接default?)
case sub <- e:
default:
glog.Infof("containerd: event not sent to the subscriber")
}
}
-------------------------------------------------------------------- process, container, pod的reap 操作 --------------------------------------------------------------------------------------
1、runv/supervisor/process.go
func (p *Process) reap()
该函数只是简单的调用p.closeStdin而已
2、runv/supervisor/process.go
func (p *Process) closeStdin() error
当p.stdinCloser不为空时,调用p.stdinCloser.Close(),最后将p.stdinCloser置为nil即可
3、runv/supervisor/container.go
func (c *Container) reap()
首先,containerShareDir := filepath.Join(hypervisor.BaseDir, c.ownerPod.vm.Id, hypervisor.ShareDirTag, c.Id),然后将containerShareDir中的rootfs umount,最后,删除containerShareDir和filepath.Join(c.ownerPod.sv.StateDir, c.Id)
4、runv/supervisor/hyperpod.go
func (hp *HyperPod) reap()
首先调用Response := hp.vm.StopPod(hp.podStatus),接着调用hp.stopNsListener(),最后删除目录filepath.Join(hypervisor.BaseDir, hp.vm.Id)
runv containerd 流程分析的更多相关文章
- runv kill 流程分析
1.runv/kill.go Action: func(context *cli.Context) 该函数做的工作很简单,就是通过grpc客户端,发送一个grpc请求而已,如下: c.Signal(n ...
- runv start container 流程分析
1.runv/start.go func startContainer(context *cli.Context, container, address string, config *spec.Sp ...
- 8、Struts2 运行流程分析
1.流程分析: 请求发送给 StrutsPrepareAndExecuteFilter StrutsPrepareAndExecuteFilter 询问 ActionMapper: 该请求是否是一个 ...
- freeswitch呼叫流程分析
今天翻文档时发现之前整理的关于freeswitch呼叫相关的内容,写成博文分享出来也方便我以后查阅. 整体结构图 FreeswitchCore 模块加载过程 freeswitch主程序初始化时会从mo ...
- u-boot 流程分析
u-boot 介绍: 对于计算机来说 , 从一开始上机通电是无法直接启动操作系统的 , 这中间需要一个引导过程 , 嵌入式Linux系统同样离不开引导程序 , 这个启动程序就叫启动加载程序(Boot ...
- thttpd和cgilua安装与运行流程分析
安装 参考如下博文安装thttpd软件 http://blog.csdn.net/21aspnet/article/details/7045845 http://blog.csdn.net/drago ...
- 【转】Hostapd工作流程分析
[转]Hostapd工作流程分析 转自:http://blog.chinaunix.net/uid-30081165-id-5290531.html Hostapd是一个运行在用户态的守护进程,可以通 ...
- u-boot中nandflash初始化流程分析(转)
u-boot中nandflash初始化流程分析(转) 原文地址http://zhuairlunjj.blog.163.com/blog/static/80050945201092011249136/ ...
- Android7.0 Phone应用源码分析(二) phone来电流程分析
接上篇博文:Android7.0 Phone应用源码分析(一) phone拨号流程分析 今天我们再来分析下Android7.0 的phone的来电流程 1.1TelephonyFramework 当有 ...
随机推荐
- csharp:正则表达式采集网页数据
https://msdn.microsoft.com/zh-cn/library/system.text.regularexpressions.regex(v=vs.110).aspx https:/ ...
- 用Perl编写Apache模块续 - SVNAuth
目标:以整合各类项目管理系统中的用户体系及权限体系为目标,实现SVN的集成式动态鉴权 支持平台:usvn.禅道等 - 开发中,本文仅对前期的探索工作做点整理 开发环境: Windows.XAMPP 1 ...
- PHP学习笔记:万能随机字符串生成函数(已经封装好)
做验证码用到的,然后就把这个函数封装起来,使用时候要设置2个参数: $str设置里要被采集的字符串,比如: $str='efasfgzsrhftjxjxjhsrth'; 则在函数里面生成的字符串就回从 ...
- [小北De编程手记] : Lesson 06 - Selenium For C# 之 流程控制
无论你是用哪一种自动化测试的驱动框架,当我们构建一个复杂应用程序的自动化测试的时候.都希望构建一个测试流程稳定,维护成本较低的自动化测试.但是,现实往往没有理想丰满.而这一篇,我会为大家讲解我们在使用 ...
- 硅谷新闻3--使用Android系统自带的API解析json数据
NewsCenterPagerBean2 bean2 = new NewsCenterPagerBean2(); try { JSONObject object = new JSONObject(js ...
- 【GOF23设计模式】外观模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_外观模式.公司注册流程.迪米特法则 package com.test.facade; public interface 工 ...
- 从网络中获取图片显示到Image控件并保存到磁盘
一.从网络中获取图片信息: /// <summary> /// 获取图片 /// </summary> /// <param name="url"&g ...
- js得到屏幕宽高、页面宽高 (window.screen.availHeight)等--笔记
window.screen.availWidth 返回当前屏幕宽度(空白空间) window.screen.availHeight 返回当前屏幕高度(空白空间) window.screen.width ...
- CSS的一些小事
1.什么时候能将零散的图片整合成一张大图,达到减少请求数的作用? 答:整合进大图的图片是被设置no-repeat用的,如果是repeat-x.repeat-y就不可以. 2.E + F 选择紧贴在E元 ...
- java多线程系列4-线程池
在之前的文章中,学习了通过实现java.lang.Runnable来定义类,以及像下面这样创建一个线程来运行任务: Runnable task = new TaskClass(task); new T ...