Skip to content

Commit ea326f0

Browse files
committed
generate: Respect runtime.GOOS when generating default template
Don't fill in a bunch of Linux stuff if runtime.GOOS isn't Linux ;). We don't have sensible defaults for other OSes yet, so error out in those cases. Signed-off-by: W. Trevor King <wking@tremily.us>
1 parent e3df0d5 commit ea326f0

2 files changed

Lines changed: 55 additions & 39 deletions

File tree

cmd/oci-runtime-tool/generate.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,23 @@ var generateCommand = cli.Command{
9292
Before: before,
9393
Action: func(context *cli.Context) error {
9494
// Start from the default template.
95-
specgen := generate.New()
95+
specgen, err := generate.New(nil)
96+
if err != nil {
97+
return err
98+
}
9699

97100
var template string
98101
if context.IsSet("template") {
99102
template = context.String("template")
100103
}
101104
if template != "" {
102-
var err error
103105
specgen, err = generate.NewFromFile(template)
104106
if err != nil {
105107
return err
106108
}
107109
}
108110

109-
err := setupSpec(&specgen, context)
111+
err = setupSpec(&specgen, context)
110112
if err != nil {
111113
return err
112114
}

generate/generate.go

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@ type ExportOptions struct {
3030
Seccomp bool // seccomp toggles if only seccomp should be exported
3131
}
3232

33-
// New creates a spec Generator with the default spec.
34-
func New() Generator {
33+
// New creates a spec Generator with the default spec for the target
34+
// OS (which defaults to runtime.GOOS).
35+
func New(os *string) (generator Generator, err error) {
36+
var goos string
37+
goos = runtime.GOOS
38+
if os == nil {
39+
os = &goos
40+
}
3541
spec := rspec.Spec{
3642
Version: rspec.Version,
3743
Platform: rspec.Platform{
@@ -44,41 +50,45 @@ func New() Generator {
4450
},
4551
Process: rspec.Process{
4652
Terminal: false,
47-
User: rspec.User{},
4853
Args: []string{
4954
"sh",
5055
},
51-
Env: []string{
52-
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
53-
"TERM=xterm",
54-
},
55-
Cwd: "/",
56-
Capabilities: []string{
57-
"CAP_CHOWN",
58-
"CAP_DAC_OVERRIDE",
59-
"CAP_FSETID",
60-
"CAP_FOWNER",
61-
"CAP_MKNOD",
62-
"CAP_NET_RAW",
63-
"CAP_SETGID",
64-
"CAP_SETUID",
65-
"CAP_SETFCAP",
66-
"CAP_SETPCAP",
67-
"CAP_NET_BIND_SERVICE",
68-
"CAP_SYS_CHROOT",
69-
"CAP_KILL",
70-
"CAP_AUDIT_WRITE",
71-
},
72-
Rlimits: []rspec.Rlimit{
73-
{
74-
Type: "RLIMIT_NOFILE",
75-
Hard: uint64(1024),
76-
Soft: uint64(1024),
77-
},
78-
},
7956
},
8057
Hostname: "mrsdalloway",
81-
Mounts: []rspec.Mount{
58+
}
59+
60+
if *os == "linux" {
61+
spec.Process.User = rspec.User{}
62+
spec.Process.Env = []string{
63+
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
64+
"TERM=xterm",
65+
}
66+
spec.Process.Cwd = "/"
67+
spec.Process.Capabilities = []string{
68+
"CAP_CHOWN",
69+
"CAP_DAC_OVERRIDE",
70+
"CAP_FSETID",
71+
"CAP_FOWNER",
72+
"CAP_MKNOD",
73+
"CAP_NET_RAW",
74+
"CAP_SETGID",
75+
"CAP_SETUID",
76+
"CAP_SETFCAP",
77+
"CAP_SETPCAP",
78+
"CAP_NET_BIND_SERVICE",
79+
"CAP_SYS_CHROOT",
80+
"CAP_KILL",
81+
"CAP_AUDIT_WRITE",
82+
}
83+
spec.Process.Rlimits = []rspec.Rlimit{
84+
{
85+
Type: "RLIMIT_NOFILE",
86+
Hard: uint64(1024),
87+
Soft: uint64(1024),
88+
},
89+
}
90+
91+
spec.Mounts = []rspec.Mount{
8292
{
8393
Destination: "/proc",
8494
Type: "proc",
@@ -115,8 +125,9 @@ func New() Generator {
115125
Source: "sysfs",
116126
Options: []string{"nosuid", "noexec", "nodev", "ro"},
117127
},
118-
},
119-
Linux: &rspec.Linux{
128+
}
129+
130+
spec.Linux = &rspec.Linux{
120131
Resources: &rspec.Resources{
121132
Devices: []rspec.DeviceCgroup{
122133
{
@@ -143,12 +154,15 @@ func New() Generator {
143154
},
144155
},
145156
Devices: []rspec.Device{},
146-
},
157+
}
158+
} else {
159+
return generator, fmt.Errorf("no defaults configured for %s", *os)
147160
}
148161
spec.Linux.Seccomp = seccomp.DefaultProfile(&spec)
149-
return Generator{
162+
generator = Generator{
150163
spec: &spec,
151164
}
165+
return generator, nil
152166
}
153167

154168
// NewFromSpec creates a spec Generator from a given spec.

0 commit comments

Comments
 (0)