Make nsinit a proper go pkg and add the main in another dir

Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
Michael Crosby 2014-02-20 18:27:42 -08:00
parent 52fa4de610
commit 5d71533d4e
7 changed files with 20 additions and 12 deletions

View file

@ -72,9 +72,11 @@ rootfs and copy a `container.json` file into the directory with your specified c
To execution `/bin/bash` in the current directory as a container just run: To execution `/bin/bash` in the current directory as a container just run:
```bash ```bash
nsinit exec /bin/bash nsinit -tty exec /bin/bash
``` ```
If you want a proper tty setup inside the new container you must use the `-tty` flag when running nsinit.
If you wish to spawn another process inside the container while your current bash session is If you wish to spawn another process inside the container while your current bash session is
running just run the exact same command again to get another bash shell or change the command. If the original process dies, PID 1, all other processes spawned inside the container will also be killed and the namespace will be removed. running just run the exact same command again to get another bash shell or change the command. If the original process dies, PID 1, all other processes spawned inside the container will also be killed and the namespace will be removed.

View file

@ -1,6 +1,6 @@
// +build linux // +build linux
package main package nsinit
import ( import (
"fmt" "fmt"
@ -16,7 +16,9 @@ import (
"syscall" "syscall"
) )
func execCommand(container *libcontainer.Container, tty bool, args []string) (int, error) { // Exec performes setup outside of a namespace so that a container can be
// executed. Exec is a high level function for working with container namespaces.
func Exec(container *libcontainer.Container, tty bool, args []string) (int, error) {
var ( var (
master *os.File master *os.File
console string console string

View file

@ -1,4 +1,4 @@
package main package nsinit
import ( import (
"fmt" "fmt"
@ -11,7 +11,8 @@ import (
"syscall" "syscall"
) )
func execinCommand(container *libcontainer.Container, nspid int, args []string) (int, error) { // ExecIn uses an existing pid and joins the pid's namespaces with the new command.
func ExecIn(container *libcontainer.Container, nspid int, args []string) (int, error) {
for _, ns := range container.Namespaces { for _, ns := range container.Namespaces {
if err := system.Unshare(namespaceMap[ns]); err != nil { if err := system.Unshare(namespaceMap[ns]); err != nil {
return -1, err return -1, err

View file

@ -1,6 +1,6 @@
// +build linux // +build linux
package main package nsinit
import ( import (
"fmt" "fmt"
@ -15,7 +15,9 @@ import (
"syscall" "syscall"
) )
func initCommand(container *libcontainer.Container, console string, pipe io.ReadCloser, args []string) error { // Init is the init process that first runs inside a new namespace to setup mounts, users, networking,
// and other options required for the new container.
func Init(container *libcontainer.Container, console string, pipe io.ReadCloser, args []string) error {
rootfs, err := resolveRootfs() rootfs, err := resolveRootfs()
if err != nil { if err != nil {
return err return err

View file

@ -1,6 +1,6 @@
// +build linux // +build linux
package main package nsinit
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
package main package nsinit
import ( import (
"github.com/dotcloud/docker/pkg/libcontainer" "github.com/dotcloud/docker/pkg/libcontainer"

View file

@ -5,6 +5,7 @@ import (
"errors" "errors"
"flag" "flag"
"github.com/dotcloud/docker/pkg/libcontainer" "github.com/dotcloud/docker/pkg/libcontainer"
"github.com/dotcloud/docker/pkg/libcontainer/nsinit"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
@ -42,9 +43,9 @@ func main() {
} }
} }
if nspid > 0 { if nspid > 0 {
exitCode, err = execinCommand(container, nspid, flag.Args()[1:]) exitCode, err = nsinit.ExecIn(container, nspid, flag.Args()[1:])
} else { } else {
exitCode, err = execCommand(container, *tty, flag.Args()[1:]) exitCode, err = nsinit.Exec(container, *tty, flag.Args()[1:])
} }
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -54,7 +55,7 @@ func main() {
if flag.NArg() < 2 { if flag.NArg() < 2 {
log.Fatal(ErrWrongArguments) log.Fatal(ErrWrongArguments)
} }
if err := initCommand(container, *console, os.NewFile(uintptr(*pipeFd), "pipe"), flag.Args()[1:]); err != nil { if err := nsinit.Init(container, *console, os.NewFile(uintptr(*pipeFd), "pipe"), flag.Args()[1:]); err != nil {
log.Fatal(err) log.Fatal(err)
} }
default: default: