Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 35 additions & 11 deletions cli/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/hyperhq/runv/hypervisor"
Expand Down Expand Up @@ -95,20 +97,11 @@ func cmdCreateContainer(context *cli.Context, attach bool) error {
} else {
for _, ns := range spec.Linux.Namespaces {
if ns.Path != "" {
if strings.Contains(ns.Path, "/") {
return fmt.Errorf("Runv doesn't support path to namespace file, it supports containers name as shared namespaces only")
}
if ns.Type == "mount" {
return fmt.Errorf("Runv doesn't support containers with shared mount namespace, use `runv exec` instead")
}
sharedContainer = ns.Path
_, err = os.Stat(filepath.Join(root, sharedContainer, stateJSON))
if err != nil {
return fmt.Errorf("The container %q is not existing or not ready", sharedContainer)
}
_, err = os.Stat(filepath.Join(root, sharedContainer, "namespace"))
if err != nil {
return fmt.Errorf("The container %q is not ready", sharedContainer)
if sharedContainer, err = findSharedContainer(context.GlobalString("root"), ns.Path); err != nil {
return fmt.Errorf("failed to find shared container: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you keep

-				_, err = os.Stat(filepath.Join(root, sharedContainer, stateJSON))
 -				if err != nil {
 -					return fmt.Errorf("The container %q is not existing or not ready", sharedContainer)
 -				}
 -				_, err = os.Stat(filepath.Join(root, sharedContainer, "namespace"))
 -				if err != nil {
 -					return fmt.Errorf("The container %q is not ready", sharedContainer)

This piece of code is used for validation, I think it's still useful.

Copy link
Contributor Author

@ZhaoXuqiang ZhaoXuqiang Aug 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's not useful.

 	if sharedContainer != "" {
 		scState, err = loadStateFile(root, sharedContainer)
 		if err != nil {
 			return err
 		}
 		vm, lockFile, err = getSandbox(filepath.Join(context.GlobalString("root"), sharedContainer, "sandbox"))
 		if err != nil {
 			return err
 		}
 	}
func loadStateFile(root, container string) (*State, error) {
	stateFile := filepath.Join(root, container, stateJSON)
	file, err := os.Open(stateFile)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	var state State
	if err = json.NewDecoder(file).Decode(&state); err != nil {
		return nil, fmt.Errorf("Decode state file %s error: %s", stateFile, err.Error())
	}
	return &state, nil
}

loadStateFile will confirm the state file exist.
namespace file is not used.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it's OK then.

ping @laijs

}
}
}
Expand Down Expand Up @@ -159,3 +152,34 @@ func checkConsole(context *cli.Context, p *specs.Process, attach bool) error {
}
return nil
}

func findSharedContainer(root, nsPath string) (container string, err error) {
absRoot, err := filepath.Abs(root)
if err != nil {
return "", err
}
list, err := ioutil.ReadDir(absRoot)
if err != nil {
return "", err
}

if strings.Contains(nsPath, "/") {
pidexp := regexp.MustCompile(`/proc/(\d+)/ns/*`)
matches := pidexp.FindStringSubmatch(nsPath)
if len(matches) != 2 {
return "", fmt.Errorf("malformed ns path: %s", nsPath)
}
pid := matches[1]

for _, item := range list {
if state, err := loadStateFile(absRoot, item.Name()); err == nil {
spid := fmt.Sprintf("%d", state.Pid)
if spid == pid {
return item.Name(), nil
}
}
}
return "", fmt.Errorf("can't find container with shim pid %s", pid)
}
return nsPath, nil
}