Skip to content
This repository has been archived by the owner on Feb 8, 2021. It is now read-only.

Commit

Permalink
Merge pull request #572 from ZhaoXuqiang/enable-pod-support
Browse files Browse the repository at this point in the history
Enable pod support
  • Loading branch information
laijs authored Aug 27, 2017
2 parents 076c3af + 44ec27e commit 8ad7e33
Showing 1 changed file with 35 additions and 11 deletions.
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)
}
}
}
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
}

0 comments on commit 8ad7e33

Please sign in to comment.