2017-02-07 21:33:23 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
2017-04-11 15:10:46 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/specconv"
|
2017-02-07 21:33:23 +00:00
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
var specCommand = cli.Command{
|
|
|
|
Name: "spec",
|
|
|
|
Usage: "create a new specification file",
|
|
|
|
ArgsUsage: "",
|
|
|
|
Description: `The spec command creates the new specification file named "` + specConfig + `" for
|
|
|
|
the bundle.
|
|
|
|
|
|
|
|
The spec generated is just a starter file. Editing of the spec is required to
|
|
|
|
achieve desired results. For example, the newly generated spec includes an args
|
|
|
|
parameter that is initially set to call the "sh" command when the container is
|
|
|
|
started. Calling "sh" may work for an ubuntu container or busybox, but will not
|
|
|
|
work for containers that do not include the "sh" program.
|
|
|
|
|
|
|
|
EXAMPLE:
|
|
|
|
To run docker's hello-world container one needs to set the args parameter
|
|
|
|
in the spec to call hello. This can be done using the sed command or a text
|
|
|
|
editor. The following commands create a bundle for hello-world, change the
|
|
|
|
default args parameter in the spec from "sh" to "/hello", then run the hello
|
|
|
|
command in a new hello-world container named container1:
|
|
|
|
|
|
|
|
mkdir hello
|
|
|
|
cd hello
|
|
|
|
docker pull hello-world
|
|
|
|
docker export $(docker create hello-world) > hello-world.tar
|
|
|
|
mkdir rootfs
|
|
|
|
tar -C rootfs -xf hello-world.tar
|
|
|
|
runc spec
|
|
|
|
sed -i 's;"sh";"/hello";' ` + specConfig + `
|
|
|
|
runc run container1
|
|
|
|
|
|
|
|
In the run command above, "container1" is the 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.
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
An alternative for generating a customized spec config is to use "oci-runtime-tool", the
|
|
|
|
sub-command "oci-runtime-tool generate" has lots of options that can be used to do any
|
|
|
|
customizations as you want, see [runtime-tools](https://github.com/opencontainers/runtime-tools)
|
2017-02-07 21:33:23 +00:00
|
|
|
to get more information.
|
|
|
|
|
|
|
|
When starting a container through runc, runc needs root privilege. If not
|
|
|
|
already running as root, you can use sudo to give runc root privilege. For
|
|
|
|
example: "sudo runc start container1" will give runc root privilege to start the
|
|
|
|
container on your host.`,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "bundle, b",
|
|
|
|
Value: "",
|
|
|
|
Usage: "path to the root of the bundle directory",
|
|
|
|
},
|
2017-04-11 15:10:46 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "rootless",
|
|
|
|
Usage: "generate a configuration for a rootless container",
|
|
|
|
},
|
2017-02-07 21:33:23 +00:00
|
|
|
},
|
|
|
|
Action: func(context *cli.Context) error {
|
2017-04-11 15:10:46 +00:00
|
|
|
if err := checkArgs(context, 0, exactArgs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
spec := specconv.Example()
|
|
|
|
|
|
|
|
rootless := context.Bool("rootless")
|
|
|
|
if rootless {
|
|
|
|
specconv.ToRootless(spec)
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
checkNoFile := func(name string) error {
|
|
|
|
_, err := os.Stat(name)
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("File %s exists. Remove it first", name)
|
|
|
|
}
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
bundle := context.String("bundle")
|
|
|
|
if bundle != "" {
|
|
|
|
if err := os.Chdir(bundle); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := checkNoFile(specConfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-11 15:10:46 +00:00
|
|
|
data, err := json.MarshalIndent(spec, "", "\t")
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(specConfig, data, 0666); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
func sPtr(s string) *string { return &s }
|
2017-02-07 21:33:23 +00:00
|
|
|
|
|
|
|
// loadSpec loads the specification from the provided path.
|
|
|
|
func loadSpec(cPath string) (spec *specs.Spec, err error) {
|
|
|
|
cf, err := os.Open(cPath)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, fmt.Errorf("JSON specification file %s not found", cPath)
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer cf.Close()
|
|
|
|
|
|
|
|
if err = json.NewDecoder(cf).Decode(&spec); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-11 15:10:46 +00:00
|
|
|
if err = validatePlatform(&spec.Platform); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-07 21:33:23 +00:00
|
|
|
return spec, validateProcessSpec(&spec.Process)
|
|
|
|
}
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
func createLibContainerRlimit(rlimit specs.LinuxRlimit) (configs.Rlimit, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
rl, err := strToRlimit(rlimit.Type)
|
|
|
|
if err != nil {
|
|
|
|
return configs.Rlimit{}, err
|
|
|
|
}
|
|
|
|
return configs.Rlimit{
|
|
|
|
Type: rl,
|
2017-04-11 15:10:46 +00:00
|
|
|
Hard: rlimit.Hard,
|
|
|
|
Soft: rlimit.Soft,
|
2017-02-07 21:33:23 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2017-04-11 15:10:46 +00:00
|
|
|
|
|
|
|
func validatePlatform(platform *specs.Platform) error {
|
|
|
|
if platform.OS != runtime.GOOS {
|
|
|
|
return fmt.Errorf("target os %s mismatch with current os %s", platform.OS, runtime.GOOS)
|
|
|
|
}
|
|
|
|
if platform.Arch != runtime.GOARCH {
|
|
|
|
return fmt.Errorf("target arch %s mismatch with current arch %s", platform.Arch, runtime.GOARCH)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|