Skip to content

Commit

Permalink
Merge pull request #3600 from utam0k/domainname
Browse files Browse the repository at this point in the history
Implement to set a domainname
  • Loading branch information
kolyshkin authored Apr 19, 2023
2 parents 1198389 + d923060 commit fdc2515
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 5 deletions.
3 changes: 3 additions & 0 deletions libcontainer/configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ type Config struct {
// Hostname optionally sets the container's hostname if provided
Hostname string `json:"hostname"`

// Domainname optionally sets the container's domainname if provided
Domainname string `json:"domainname"`

// Namespaces specifies the container's namespaces that it should setup when cloning the init process
// If a namespace is not provided that namespace is shared from the container's parent process
Namespaces Namespaces `json:"namespaces"`
Expand Down
7 changes: 5 additions & 2 deletions libcontainer/configs/validate/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Validate(config *configs.Config) error {
cgroupsCheck,
rootfs,
network,
hostname,
uts,
security,
namespaces,
sysctl,
Expand Down Expand Up @@ -75,10 +75,13 @@ func network(config *configs.Config) error {
return nil
}

func hostname(config *configs.Config) error {
func uts(config *configs.Config) error {
if config.Hostname != "" && !config.Namespaces.Contains(configs.NEWUTS) {
return errors.New("unable to set hostname without a private UTS namespace")
}
if config.Domainname != "" && !config.Namespaces.Contains(configs.NEWUTS) {
return errors.New("unable to set domainname without a private UTS namespace")
}
return nil
}

Expand Down
30 changes: 29 additions & 1 deletion libcontainer/configs/validate/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,25 @@ func TestValidateHostname(t *testing.T) {
}
}

func TestValidateHostnameWithoutUTSNamespace(t *testing.T) {
func TestValidateUTS(t *testing.T) {
config := &configs.Config{
Rootfs: "/var",
Domainname: "runc",
Hostname: "runc",
Namespaces: configs.Namespaces(
[]configs.Namespace{
{Type: configs.NEWUTS},
},
),
}

err := Validate(config)
if err != nil {
t.Errorf("Expected error to not occur: %+v", err)
}
}

func TestValidateUTSWithoutUTSNamespace(t *testing.T) {
config := &configs.Config{
Rootfs: "/var",
Hostname: "runc",
Expand All @@ -92,6 +110,16 @@ func TestValidateHostnameWithoutUTSNamespace(t *testing.T) {
if err == nil {
t.Error("Expected error to occur but it was nil")
}

config = &configs.Config{
Rootfs: "/var",
Domainname: "runc",
}

err = Validate(config)
if err == nil {
t.Error("Expected error to occur but it was nil")
}
}

func TestValidateSecurityWithMaskPaths(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions libcontainer/integration/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ func newTemplateConfig(t *testing.T, p *tParam) *configs.Config {
ReadonlyPaths: []string{
"/proc/sys", "/proc/sysrq-trigger", "/proc/irq", "/proc/bus",
},
Devices: specconv.AllowedDevices,
Hostname: "integration",
Devices: specconv.AllowedDevices,
Hostname: "integration",
Domainname: "integration",
Mounts: []*configs.Mount{
{
Source: "proc",
Expand Down
1 change: 1 addition & 0 deletions libcontainer/specconv/spec_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ func CreateLibcontainerConfig(opts *CreateOpts) (*configs.Config, error) {
NoPivotRoot: opts.NoPivotRoot,
Readonlyfs: spec.Root.Readonly,
Hostname: spec.Hostname,
Domainname: spec.Domainname,
Labels: append(labels, "bundle="+cwd),
NoNewKeyring: opts.NoNewKeyring,
RootlessEUID: opts.RootlessEUID,
Expand Down
5 changes: 5 additions & 0 deletions libcontainer/standard_init_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ func (l *linuxStandardInit) Init() error {
return &os.SyscallError{Syscall: "sethostname", Err: err}
}
}
if domainname := l.config.Config.Domainname; domainname != "" {
if err := unix.Setdomainname([]byte(domainname)); err != nil {
return &os.SyscallError{Syscall: "setdomainname", Err: err}
}
}
if err := apparmor.ApplyProfile(l.config.AppArmorProfile); err != nil {
return fmt.Errorf("unable to apply apparmor profile: %w", err)
}
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,22 @@ function teardown() {
runc state test_run_keep
[ "$status" -ne 0 ]
}

@test "runc run [hostname domainname]" {
update_config ' .process.args |= ["sh"]
| .hostname = "myhostname"
| .domainname= "mydomainname"'

runc run -d --console-socket "$CONSOLE_SOCKET" test_utc
[ "$status" -eq 0 ]

# test hostname
runc exec test_utc hostname
[ "$status" -eq 0 ]
[[ "${lines[0]}" == *'myhostname'* ]]

# test domainname
runc exec test_utc cat /proc/sys/kernel/domainname
[ "$status" -eq 0 ]
[[ "${lines[0]}" == *'mydomainname'* ]]
}

0 comments on commit fdc2515

Please sign in to comment.