Skip to content

Commit 30d4540

Browse files
committed
Implement to set a domainname
opencontainers/runtime-spec#1156 Signed-off-by: utam0k <[email protected]> Implement to set a domain name Signed-off-by: utam0k <[email protected]>
1 parent 2e8b7a1 commit 30d4540

File tree

10 files changed

+60
-9
lines changed

10 files changed

+60
-9
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/godbus/dbus/v5 v5.1.0
1313
github.com/moby/sys/mountinfo v0.6.2
1414
github.com/mrunalp/fileutils v0.5.0
15-
github.com/opencontainers/runtime-spec v1.0.3-0.20220718201635-a8106e99982b
15+
github.com/opencontainers/runtime-spec v1.0.3-0.20220909204839-494a5a6aca78
1616
github.com/opencontainers/selinux v1.10.1
1717
github.com/seccomp/libseccomp-golang v0.10.0
1818
github.com/sirupsen/logrus v1.9.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ github.com/moby/sys/mountinfo v0.6.2 h1:BzJjoreD5BMFNmD9Rus6gdd1pLuecOFPt8wC+Vyg
3131
github.com/moby/sys/mountinfo v0.6.2/go.mod h1:IJb6JQeOklcdMU9F5xQ8ZALD+CUr5VlGpwtX+VE0rpI=
3232
github.com/mrunalp/fileutils v0.5.0 h1:NKzVxiH7eSk+OQ4M+ZYW1K6h27RUV3MI6NUTsHhU6Z4=
3333
github.com/mrunalp/fileutils v0.5.0/go.mod h1:M1WthSahJixYnrXQl/DFQuteStB1weuxD2QJNHXfbSQ=
34-
github.com/opencontainers/runtime-spec v1.0.3-0.20220718201635-a8106e99982b h1:udwtfS44rxYE/ViMLchHQBjfE60GZSB1arY7BFbyxLs=
35-
github.com/opencontainers/runtime-spec v1.0.3-0.20220718201635-a8106e99982b/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
34+
github.com/opencontainers/runtime-spec v1.0.3-0.20220909204839-494a5a6aca78 h1:R5M2qXZiK/mWPMT4VldCOiSL9HIAMuxQZWdG0CSM5+4=
35+
github.com/opencontainers/runtime-spec v1.0.3-0.20220909204839-494a5a6aca78/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
3636
github.com/opencontainers/selinux v1.10.1 h1:09LIPVRP3uuZGQvgR+SgMSNBd1Eb3vlRbGqQpoHsF8w=
3737
github.com/opencontainers/selinux v1.10.1/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI=
3838
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

libcontainer/configs/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ type Config struct {
119119
// Hostname optionally sets the container's hostname if provided
120120
Hostname string `json:"hostname"`
121121

122+
// Domainname optionally sets the container's domainname if provided
123+
Domainname string `json:"domainname"`
124+
122125
// Namespaces specifies the container's namespaces that it should setup when cloning the init process
123126
// If a namespace is not provided that namespace is shared from the container's parent process
124127
Namespaces Namespaces `json:"namespaces"`

libcontainer/configs/validate/validator.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func Validate(config *configs.Config) error {
2323
cgroupsCheck,
2424
rootfs,
2525
network,
26-
hostname,
26+
uts,
2727
security,
2828
namespaces,
2929
sysctl,
@@ -75,10 +75,13 @@ func network(config *configs.Config) error {
7575
return nil
7676
}
7777

78-
func hostname(config *configs.Config) error {
78+
func uts(config *configs.Config) error {
7979
if config.Hostname != "" && !config.Namespaces.Contains(configs.NEWUTS) {
8080
return errors.New("unable to set hostname without a private UTS namespace")
8181
}
82+
if config.Domainname != "" && !config.Namespaces.Contains(configs.NEWUTS) {
83+
return errors.New("unable to set domainname without a private UTS namespace")
84+
}
8285
return nil
8386
}
8487

libcontainer/configs/validate/validator_test.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,25 @@ func TestValidateHostname(t *testing.T) {
8282
}
8383
}
8484

85-
func TestValidateHostnameWithoutUTSNamespace(t *testing.T) {
85+
func TestValidateUTS(t *testing.T) {
86+
config := &configs.Config{
87+
Rootfs: "/var",
88+
Domainname: "runc",
89+
Hostname: "runc",
90+
Namespaces: configs.Namespaces(
91+
[]configs.Namespace{
92+
{Type: configs.NEWUTS},
93+
},
94+
),
95+
}
96+
97+
err := Validate(config)
98+
if err != nil {
99+
t.Errorf("Expected error to not occur: %+v", err)
100+
}
101+
}
102+
103+
func TestValidateUTSWithoutUTSNamespace(t *testing.T) {
86104
config := &configs.Config{
87105
Rootfs: "/var",
88106
Hostname: "runc",
@@ -92,6 +110,16 @@ func TestValidateHostnameWithoutUTSNamespace(t *testing.T) {
92110
if err == nil {
93111
t.Error("Expected error to occur but it was nil")
94112
}
113+
114+
config = &configs.Config{
115+
Rootfs: "/var",
116+
Domainname: "runc",
117+
}
118+
119+
err = Validate(config)
120+
if err == nil {
121+
t.Error("Expected error to occur but it was nil")
122+
}
95123
}
96124

97125
func TestValidateSecurityWithMaskPaths(t *testing.T) {

libcontainer/integration/template_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ func newTemplateConfig(t *testing.T, p *tParam) *configs.Config {
129129
ReadonlyPaths: []string{
130130
"/proc/sys", "/proc/sysrq-trigger", "/proc/irq", "/proc/bus",
131131
},
132-
Devices: specconv.AllowedDevices,
133-
Hostname: "integration",
132+
Devices: specconv.AllowedDevices,
133+
Hostname: "integration",
134+
Domainname: "integration",
134135
Mounts: []*configs.Mount{
135136
{
136137
Source: "proc",

libcontainer/specconv/spec_linux.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ func CreateLibcontainerConfig(opts *CreateOpts) (*configs.Config, error) {
354354
NoPivotRoot: opts.NoPivotRoot,
355355
Readonlyfs: spec.Root.Readonly,
356356
Hostname: spec.Hostname,
357+
Domainname: spec.Domainname,
357358
Labels: append(labels, "bundle="+cwd),
358359
NoNewKeyring: opts.NoNewKeyring,
359360
RootlessEUID: opts.RootlessEUID,

libcontainer/standard_init_linux.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ func (l *linuxStandardInit) Init() error {
126126
return &os.SyscallError{Syscall: "sethostname", Err: err}
127127
}
128128
}
129+
if domainname := l.config.Config.Domainname; domainname != "" {
130+
if err := unix.Setdomainname([]byte(domainname)); err != nil {
131+
return &os.SyscallError{Syscall: "setdomainname", Err: err}
132+
}
133+
}
129134
if err := apparmor.ApplyProfile(l.config.AppArmorProfile); err != nil {
130135
return fmt.Errorf("unable to apply apparmor profile: %w", err)
131136
}

vendor/github.com/opencontainers/runtime-spec/specs-go/config.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ github.com/moby/sys/mountinfo
3636
# github.com/mrunalp/fileutils v0.5.0
3737
## explicit; go 1.13
3838
github.com/mrunalp/fileutils
39-
# github.com/opencontainers/runtime-spec v1.0.3-0.20220718201635-a8106e99982b
39+
# github.com/opencontainers/runtime-spec v1.0.3-0.20220909204839-494a5a6aca78
4040
## explicit
4141
github.com/opencontainers/runtime-spec/specs-go
4242
# github.com/opencontainers/selinux v1.10.1

0 commit comments

Comments
 (0)