From e44e0df0d61f652d13528c6a9419e49d0da5e985 Mon Sep 17 00:00:00 2001 From: EnumDev Date: Sun, 9 Mar 2025 21:54:21 +0200 Subject: [PATCH] Add setHostname function to enit --- cmd/enit/main.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/cmd/enit/main.go b/cmd/enit/main.go index ba107e0..5cdd46f 100644 --- a/cmd/enit/main.go +++ b/cmd/enit/main.go @@ -9,6 +9,7 @@ import ( "os/exec" "os/signal" "path" + "strings" "syscall" "time" ) @@ -41,6 +42,8 @@ func main() { mountVirtualFilesystems() // Mount filesystems in fstab mountFilesystems() + // Set hostname + setHostname() // Start service manager startServiceManager() @@ -168,6 +171,25 @@ func stopServiceManager() { fmt.Println("Done.") } +func setHostname() { + fmt.Print("Setting hostname... ") + + bytes, err := os.ReadFile("/etc/hostname") + if err != nil { + log.Println("Could not set hostname!") + return + } + + hostname := strings.TrimSpace(string(bytes)) + + if err := syscall.Sethostname([]byte(hostname)); err != nil { + log.Println("Could not set hostname!") + return + } + + fmt.Println("Done.") +} + func waitZombieProcesses() { for { if wpid, _ := syscall.Wait4(-1, nil, syscall.WNOHANG, nil); wpid <= 0 {