Skip to content

Commit acf96da

Browse files
author
naterfute
committed
feat: add machine-id support for hytale servers
1 parent 67b40fa commit acf96da

File tree

4 files changed

+65
-7
lines changed

4 files changed

+65
-7
lines changed

src/cmd/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,9 @@ func rootCmdRun(cmd *cobra.Command, _ []string) {
218218
for _, serv := range manager.All() {
219219
s := serv
220220

221-
// For each server we encounter make sure the root data directory exists.
221+
// For each server ensure the minimal environment is confiugured for the server.
222222
if err := s.EnsureDataDirectoryExists(); err != nil {
223-
s.Log().Error("could not create root data directory for server: not loading server...")
223+
s.Log().Error("could not create base environment for server: not loading server...")
224224
continue
225225
}
226226

src/config/config.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ type SystemConfiguration struct {
182182
// files into containers.
183183
Enable bool `yaml:"enabled" default:"false"`
184184

185-
// Directory is the directory on disk where the generated files will be stored.
185+
// Directory is the directory on disk where the generated passwd files will be stored.
186186
// This directory may be temporary as it will be re-created whenever Elytra is started.
187187
//
188188
// This path **WILL** be both written to by Elytra and mounted into containers created by
@@ -193,6 +193,26 @@ type SystemConfiguration struct {
193193
Directory string `yaml:"directory" default:"/run/elytra/etc"`
194194
} `yaml:"passwd"`
195195

196+
// MachineID controls the mounting of a generated `/etc/machine-id` file into containers started by Wings.
197+
MachineID struct {
198+
// Enable controls whether a generated machine-id file should be mounted
199+
// into containers.
200+
//
201+
// By default this option is enabled and Wings will mount an additional
202+
// machine-id file into containers.
203+
Enable bool `yaml:"enabled" default:"true"`
204+
205+
// Directory is the directory on disk where the generated machine-id files will be stored.
206+
// This directory may be temporary as it will be re-created whenever Wings is started.
207+
//
208+
// This path **WILL** be both written to by Wings and mounted into containers created by
209+
// Wings. If you are running Wings itself in a container, this path will need to be mounted
210+
// into the Wings container as the exact path on the host, which should match the value
211+
// specified here. If you are using SELinux, you will need to make sure this file has the
212+
// correct SELinux context in order for containers to use it.
213+
Directory string `yaml:"directory" default:"/run/elytra/machine-id"`
214+
} `yaml:"machine_id"`
215+
196216
// The amount of time in seconds that can elapse before a server's disk space calculation is
197217
// considered stale and a re-check should occur. DANGER: setting this value too low can seriously
198218
// impact system performance and cause massive I/O bottlenecks and high CPU usage for the Elytra
@@ -744,6 +764,11 @@ func ConfigureDirectories() error {
744764
return err
745765
}
746766

767+
log.WithField("path", _config.System.TmpDirectory).Debug("ensuring temporary data directory exists")
768+
if err := os.MkdirAll(_config.System.TmpDirectory, 0o700); err != nil {
769+
return err
770+
}
771+
747772
log.WithField("path", _config.System.ArchiveDirectory).Debug("ensuring archive data directory exists")
748773
if err := os.MkdirAll(_config.System.ArchiveDirectory, 0o700); err != nil {
749774
return err
@@ -761,6 +786,13 @@ func ConfigureDirectories() error {
761786
}
762787
}
763788

789+
if _config.System.MachineID.Enable {
790+
log.WithField("path", _config.System.MachineID.Directory).Debug("ensuring machine-id directory exists")
791+
if err := os.MkdirAll(_config.System.MachineID.Directory, 0o755); err != nil {
792+
return err
793+
}
794+
}
795+
764796
return nil
765797
}
766798

src/server/mounts.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,34 @@ func (s *Server) Mounts() []environment.Mount {
2828
ReadOnly: false,
2929
},
3030
}
31+
cfg := config.Get()
3132

3233
// Handle mounting a generated `/etc/passwd` if the feature is enabled.
33-
if passwd := config.Get().System.Passwd; passwd.Enable {
34-
s.Log().WithFields(log.Fields{"source_path": passwd.Directory}).Info("mouting generated /etc/{group,passwd} to workaround UID/GID issues")
34+
if cfg.System.Passwd.Enable {
35+
s.Log().WithFields(log.Fields{"source_path": cfg.System.Passwd.Directory}).Info("mouting generated /etc/{group,passwd} to workaround UID/GID issues")
3536
m = append(m, environment.Mount{
36-
Source: filepath.Join(passwd.Directory, "group"),
37+
Source: filepath.Join(cfg.System.Passwd.Directory, "group"),
3738
Target: "/etc/group",
3839
ReadOnly: true,
3940
})
4041
m = append(m, environment.Mount{
41-
Source: filepath.Join(passwd.Directory, "passwd"),
42+
Source: filepath.Join(cfg.System.Passwd.Directory, "passwd"),
4243
Target: "/etc/passwd",
4344
ReadOnly: true,
4445
})
4546
}
4647

48+
if cfg.System.MachineID.Enable {
49+
// Hytale wants a machine-id in order to encrypt tokens for the server.
50+
// So add a mount to `/etc/machine-id` to a source that contains the
51+
// server's UUID without any dashes.
52+
m = append(m, environment.Mount{
53+
Source: filepath.Join(cfg.System.MachineID.Directory, s.ID()),
54+
Target: "/etc/machine-id",
55+
ReadOnly: true,
56+
})
57+
}
58+
4759
// Also include any of this server's custom mounts when returning them.
4860
return append(m, s.customMounts()...)
4961
}

src/server/server.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package server
22

33
import (
4+
"bytes"
45
"context"
56
"encoding/json"
67
"fmt"
78
"net/http"
89
"os"
10+
"path/filepath"
911
"strings"
1012
"sync"
1113

@@ -259,6 +261,18 @@ func (s *Server) CreateEnvironment() error {
259261
return err
260262
}
261263

264+
cfg := config.Get()
265+
if cfg.System.MachineID.Enable {
266+
// Hytale wants a machine-id in order to encrypt tokens for the server. So
267+
// write a machine-id file for the server that contains the server's UUID
268+
// without any dashes.
269+
p := filepath.Join(cfg.System.MachineID.Directory, s.ID())
270+
machineID := append(bytes.ReplaceAll([]byte(s.ID()), []byte{'-'}, []byte{}), '\n')
271+
if err := os.WriteFile(p, machineID, 0o644); err != nil {
272+
return fmt.Errorf("failed to write machine-id (at '%s') for server '%s': %w", p, s.ID(), err)
273+
}
274+
}
275+
262276
return s.Environment.Create()
263277
}
264278

0 commit comments

Comments
 (0)