From 01b3c28874fed08bd49d4bb3a0a71ebac8762dd2 Mon Sep 17 00:00:00 2001 From: Jonathon Anderson Date: Tue, 11 Feb 2025 09:24:24 -0700 Subject: [PATCH] Update syncuser to read `passwd` and `group` from sysconfdir Signed-off-by: Jonathon Anderson --- internal/app/wwctl/image/build/main.go | 2 +- internal/app/wwctl/image/exec/main.go | 4 ++-- internal/app/wwctl/image/syncuser/main.go | 2 +- internal/pkg/api/image/image.go | 2 +- .../pkg/image/{syncuids.go => syncuser.go} | 23 +++++++++---------- 5 files changed, 16 insertions(+), 17 deletions(-) rename internal/pkg/image/{syncuids.go => syncuser.go} (94%) diff --git a/internal/app/wwctl/image/build/main.go b/internal/app/wwctl/image/build/main.go index 5be4171b..4e5df393 100644 --- a/internal/app/wwctl/image/build/main.go +++ b/internal/app/wwctl/image/build/main.go @@ -12,7 +12,7 @@ import ( func CobraRunE(cmd *cobra.Command, args []string) error { if SyncUser { for _, name := range args { - if err := image.SyncUids(name, true); err != nil { + if err := image.Syncuser(name, true); err != nil { return fmt.Errorf("syncuser error: %w", err) } } diff --git a/internal/app/wwctl/image/exec/main.go b/internal/app/wwctl/image/exec/main.go index d60a671b..586d3cd4 100644 --- a/internal/app/wwctl/image/exec/main.go +++ b/internal/app/wwctl/image/exec/main.go @@ -142,11 +142,11 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } if SyncUser { if userdbChanged { - if err = image.SyncUids(imageName, false); err != nil { + if err = image.Syncuser(imageName, false); err != nil { wwlog.Error("syncuser error: %s", err) } } else { - wwlog.Info("Skipping syncuser (passwd or group not changed)") + wwlog.Info("Skipping syncuser (passwd and group files not changed)") } } diff --git a/internal/app/wwctl/image/syncuser/main.go b/internal/app/wwctl/image/syncuser/main.go index c96ffd4f..7b8f52e0 100644 --- a/internal/app/wwctl/image/syncuser/main.go +++ b/internal/app/wwctl/image/syncuser/main.go @@ -15,7 +15,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { if !image.ValidName(imageName) { return fmt.Errorf("%s is not a valid image", imageName) } - err := image.SyncUids(imageName, write) + err := image.Syncuser(imageName, write) if err != nil { return fmt.Errorf("error in synchronize: %s", err) } diff --git a/internal/pkg/api/image/image.go b/internal/pkg/api/image/image.go index 22de5fda..be9995a0 100644 --- a/internal/pkg/api/image/image.go +++ b/internal/pkg/api/image/image.go @@ -193,7 +193,7 @@ func ImageImport(cip *wwapiv1.ImageImportParameter) (imageName string, err error } if cip.SyncUser { - err = image.SyncUids(cip.Name, true) + err = image.Syncuser(cip.Name, true) if err != nil { err = fmt.Errorf("syncuser error: %w", err) return diff --git a/internal/pkg/image/syncuids.go b/internal/pkg/image/syncuser.go similarity index 94% rename from internal/pkg/image/syncuids.go rename to internal/pkg/image/syncuser.go index 73449a26..67ad6e1d 100644 --- a/internal/pkg/image/syncuids.go +++ b/internal/pkg/image/syncuser.go @@ -12,14 +12,12 @@ import ( "syscall" "github.com/pkg/errors" + "github.com/warewulf/warewulf/internal/pkg/config" "github.com/warewulf/warewulf/internal/pkg/util" "github.com/warewulf/warewulf/internal/pkg/wwlog" ) -const passwdPath = "/etc/passwd" -const groupPath = "/etc/group" - -// SyncUids updates the /etc/passwd and /etc/group files in the +// Syncuser updates the /etc/passwd and /etc/group files in the // image identified by imageName by installing the equivalent // files from the host and appending names only in the // image. Files in the image are updated to match the new @@ -33,14 +31,15 @@ const groupPath = "/etc/group" // A conflict arises if the image has an entry with the same id as // an entry in the host and the host does not have an entry with the // same name. In this case, an error is returned. -func SyncUids(imageName string, write bool) error { - wwlog.Debug("SyncUids(imageName=%v, write=%v)", imageName, write) +func Syncuser(imageName string, write bool) error { + wwlog.Debug("Syncuser(imageName=%v, write=%v)", imageName, write) + conf := config.Get() imagePath := RootFsDir(imageName) - imagePasswdPath := path.Join(imagePath, passwdPath) - imageGroupPath := path.Join(imagePath, groupPath) + imagePasswdPath := path.Join(imagePath, "/etc/passwd") + imageGroupPath := path.Join(imagePath, "/etc/group") passwdSync := make(syncDB) - if err := passwdSync.readFromHost(passwdPath); err != nil { + if err := passwdSync.readFromHost(path.Join(conf.Paths.Sysconfdir, "passwd")); err != nil { return err } if err := passwdSync.readFromimage(imagePasswdPath); err != nil { @@ -51,7 +50,7 @@ func SyncUids(imageName string, write bool) error { } groupSync := make(syncDB) - if err := groupSync.readFromHost(groupPath); err != nil { + if err := groupSync.readFromHost(path.Join(conf.Paths.Sysconfdir, "group")); err != nil { return err } if err := groupSync.readFromimage(imageGroupPath); err != nil { @@ -78,10 +77,10 @@ func SyncUids(imageName string, write bool) error { if err := groupSync.chownGroupFiles(); err != nil { return err } - if err := passwdSync.update(imagePasswdPath, passwdPath); err != nil { + if err := passwdSync.update(imagePasswdPath, "/etc/passwd"); err != nil { return err } - if err := groupSync.update(imageGroupPath, groupPath); err != nil { + if err := groupSync.update(imageGroupPath, "/etc/group"); err != nil { return err } wwlog.Info("uid/gid synced for image %s", imageName)