一、startCommand
startCommand的执行步骤如下:
1、从context中获取id,再获取指定的容器
2、启动处于Created状态的容器
var startCommand = cli.Command{
Name: "start",
Usage: "executes the user defined process in a created container",
ArgsUsage: `<container-id>
Where "<container-id>" is your name for the instance of the container that you
are starting. The name you provide for the container instance must be unique on
your host.`,
Description: `The start command executes the user defined process in a created container.`,
Action: func(context *cli.Context) error {
//取指定的容器
container, err := getContainer(context)
if err != nil {
return err
}
status, err := container.Status()
if err != nil {
return err
}
switch status {
//仅仅会启动处于Created状态的容器
case libcontainer.Created:
return container.Exec()
case libcontainer.Stopped:
return fmt.Errorf("cannot start a container that has run and stopped")
case libcontainer.Running:
return fmt.Errorf("cannot start an already running container")
default:
return fmt.Errorf("cannot start a container in the %s state", status)
}
},
}
二、获取指定container
//从容器文件夹中载入该容器的配置,生成libcontainer.Container对象
func getContainer(context *cli.Context) (libcontainer.Container, error) {
id := context.Args().First()
if id == "" {
return nil, errEmptyID
}
factory, err := loadFactory(context)
if err != nil {
return nil, err
}
return factory.Load(id)
}
三、container.Exec()函数
以“只读”的方式打开FIFO管道,读取内容。
这同时也恢复之前处于阻塞状态的runc Init
进程,Init进程会执行最后调用用户期待的cmd部分。
func (c *linuxContainer) Exec() error {
c.m.Lock()
defer c.m.Unlock()
return c.exec()
}
func (c *linuxContainer) exec() error {
path := filepath.Join(c.root, execFifoFilename)
//以“只读”的方式打开FIFO管道,读取内容。
f, err := os.OpenFile(path, os.O_RDONLY, 0)
if err != nil {
return newSystemErrorWithCause(err, "open exec fifo for reading")
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
return err
}
if len(data) > 0 {
//如果读取到的data长度大于0,则读取到Create流程中最后写入的“0”,则删除FIFO管道文件
os.Remove(path)
return nil
}
return fmt.Errorf("cannot start an already running container")
}