示例#1
0
func init() {
	execdriver.RegisterInitFunc(DriverName, func(args *execdriver.InitArgs) error {
		var (
			container *libcontainer.Container
			ns        = nsinit.NewNsInit(&nsinit.DefaultCommandFactory{}, &nsinit.DefaultStateWriter{args.Root}, createLogger(""))
		)
		f, err := os.Open(filepath.Join(args.Root, "container.json"))
		if err != nil {
			return err
		}
		if err := json.NewDecoder(f).Decode(&container); err != nil {
			f.Close()
			return err
		}
		f.Close()

		cwd, err := os.Getwd()
		if err != nil {
			return err
		}
		syncPipe, err := nsinit.NewSyncPipeFromFd(0, uintptr(args.Pipe))
		if err != nil {
			return err
		}
		if err := ns.Init(container, cwd, args.Console, syncPipe, args.Args); err != nil {
			return err
		}
		return nil
	})
}
示例#2
0
文件: driver.go 项目: ranthama/docker
func init() {
	execdriver.RegisterInitFunc(DriverName, func(args *execdriver.InitArgs) error {
		var container *libcontainer.Container
		f, err := os.Open(filepath.Join(args.Root, "container.json"))
		if err != nil {
			return err
		}
		if err := json.NewDecoder(f).Decode(&container); err != nil {
			f.Close()
			return err
		}
		f.Close()

		rootfs, err := os.Getwd()
		if err != nil {
			return err
		}
		syncPipe, err := nsinit.NewSyncPipeFromFd(0, uintptr(args.Pipe))
		if err != nil {
			return err
		}
		if err := nsinit.Init(container, rootfs, args.Console, syncPipe, args.Args); err != nil {
			return err
		}
		return nil
	})
}
示例#3
0
文件: main.go 项目: ChaosCloud/docker
func main() {
	registerFlags()

	if flag.NArg() < 1 {
		log.Fatalf("wrong number of argments %d", flag.NArg())
	}
	container, err := loadContainer()
	if err != nil {
		log.Fatalf("Unable to load container: %s", err)
	}
	l, err := getLogger("[exec] ")
	if err != nil {
		log.Fatal(err)
	}

	ns, err := newNsInit(l)
	if err != nil {
		log.Fatalf("Unable to initialize nsinit: %s", err)
	}

	switch flag.Arg(0) {
	case "exec": // this is executed outside of the namespace in the cwd
		var exitCode int
		nspid, err := readPid()
		if err != nil {
			if !os.IsNotExist(err) {
				l.Fatalf("Unable to read pid: %s", err)
			}
		}
		if nspid > 0 {
			exitCode, err = ns.ExecIn(container, nspid, flag.Args()[1:])
		} else {
			term := nsinit.NewTerminal(os.Stdin, os.Stdout, os.Stderr, container.Tty)
			exitCode, err = ns.Exec(container, term, flag.Args()[1:])
		}
		if err != nil {
			l.Fatalf("Failed to exec: %s", err)
		}
		os.Exit(exitCode)
	case "init": // this is executed inside of the namespace to setup the container
		cwd, err := os.Getwd()
		if err != nil {
			l.Fatal(err)
		}
		if flag.NArg() < 2 {
			l.Fatalf("wrong number of argments %d", flag.NArg())
		}
		syncPipe, err := nsinit.NewSyncPipeFromFd(0, uintptr(pipeFd))
		if err != nil {
			l.Fatalf("Unable to create sync pipe: %s", err)
		}
		if err := ns.Init(container, cwd, console, syncPipe, flag.Args()[1:]); err != nil {
			l.Fatalf("Unable to initialize for container: %s", err)
		}
	default:
		l.Fatalf("command not supported for nsinit %s", flag.Arg(0))
	}
}
示例#4
0
文件: main.go 项目: newgoliath/docker
func main() {
	if len(os.Args) < 2 {
		log.Fatalf("invalid number of arguments %d", len(os.Args))
	}

	container, err := loadContainer()
	if err != nil {
		log.Fatalf("unable to load container: %s", err)
	}

	switch os.Args[1] {
	case "exec": // this is executed outside of the namespace in the cwd
		var nspid, exitCode int
		if nspid, err = readPid(); err != nil && !os.IsNotExist(err) {
			log.Fatalf("unable to read pid: %s", err)
		}

		if nspid > 0 {
			exitCode, err = nsinit.ExecIn(container, nspid, os.Args[2:])
		} else {
			term := nsinit.NewTerminal(os.Stdin, os.Stdout, os.Stderr, container.Tty)
			exitCode, err = nsinit.Exec(container, term, "", dataPath, os.Args[2:], nsinit.DefaultCreateCommand, nil)
		}

		if err != nil {
			log.Fatalf("failed to exec: %s", err)
		}
		os.Exit(exitCode)
	case "init": // this is executed inside of the namespace to setup the container
		// by default our current dir is always our rootfs
		rootfs, err := os.Getwd()
		if err != nil {
			log.Fatal(err)
		}

		pipeFd, err := strconv.Atoi(rawPipeFd)
		if err != nil {
			log.Fatal(err)
		}
		syncPipe, err := nsinit.NewSyncPipeFromFd(0, uintptr(pipeFd))
		if err != nil {
			log.Fatalf("unable to create sync pipe: %s", err)
		}

		if err := nsinit.Init(container, rootfs, console, syncPipe, os.Args[2:]); err != nil {
			log.Fatalf("unable to initialize for container: %s", err)
		}
	default:
		log.Fatalf("command not supported for nsinit %s", os.Args[0])
	}
}