Skip to content

Commit

Permalink
Implement to set a domain name
Browse files Browse the repository at this point in the history
Signed-off-by: utam0k <k0ma@utam0k.jp>
  • Loading branch information
utam0k committed Sep 12, 2022
1 parent 3b550db commit bfbc652
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion libcontainer/configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ type Config struct {
// Hostname optionally sets the container's hostname if provided
Hostname string `json:"hostname"`

// Hostname optionally sets the container's domainname if provided
// 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
Expand Down
9 changes: 2 additions & 7 deletions libcontainer/configs/validate/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ func Validate(config *configs.Config) error {
cgroupsCheck,
rootfs,
network,
hostname,
domainname,
uts,
security,
namespaces,
sysctl,
Expand Down Expand Up @@ -76,14 +75,10 @@ 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")
}
return nil
}

func domainname(config *configs.Config) error {
if config.Domainname != "" && !config.Namespaces.Contains(configs.NEWUTS) {
return errors.New("unable to set domainname without a private UTS namespace")
}
Expand Down
11 changes: 5 additions & 6 deletions libcontainer/configs/validate/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ func TestValidateHostname(t *testing.T) {
}
}

func TestValidateDomainname(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},
Expand All @@ -99,7 +100,7 @@ func TestValidateDomainname(t *testing.T) {
}
}

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

func TestValidateDomainnameWithoutUTSNamespace(t *testing.T) {
config := &configs.Config{
config = &configs.Config{
Rootfs: "/var",
Domainname: "runc",
}

err := Validate(config)
err = Validate(config)
if err == nil {
t.Error("Expected error to occur but it was nil")
}
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

0 comments on commit bfbc652

Please sign in to comment.