Update syncuser to read passwd and group from sysconfdir

Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
Jonathon Anderson
2025-02-11 09:24:24 -07:00
parent b0b4e8117f
commit 01b3c28874
5 changed files with 16 additions and 17 deletions

View File

@@ -12,7 +12,7 @@ import (
func CobraRunE(cmd *cobra.Command, args []string) error { func CobraRunE(cmd *cobra.Command, args []string) error {
if SyncUser { if SyncUser {
for _, name := range args { 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) return fmt.Errorf("syncuser error: %w", err)
} }
} }

View File

@@ -142,11 +142,11 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
} }
if SyncUser { if SyncUser {
if userdbChanged { if userdbChanged {
if err = image.SyncUids(imageName, false); err != nil { if err = image.Syncuser(imageName, false); err != nil {
wwlog.Error("syncuser error: %s", err) wwlog.Error("syncuser error: %s", err)
} }
} else { } else {
wwlog.Info("Skipping syncuser (passwd or group not changed)") wwlog.Info("Skipping syncuser (passwd and group files not changed)")
} }
} }

View File

@@ -15,7 +15,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
if !image.ValidName(imageName) { if !image.ValidName(imageName) {
return fmt.Errorf("%s is not a valid image", imageName) return fmt.Errorf("%s is not a valid image", imageName)
} }
err := image.SyncUids(imageName, write) err := image.Syncuser(imageName, write)
if err != nil { if err != nil {
return fmt.Errorf("error in synchronize: %s", err) return fmt.Errorf("error in synchronize: %s", err)
} }

View File

@@ -193,7 +193,7 @@ func ImageImport(cip *wwapiv1.ImageImportParameter) (imageName string, err error
} }
if cip.SyncUser { if cip.SyncUser {
err = image.SyncUids(cip.Name, true) err = image.Syncuser(cip.Name, true)
if err != nil { if err != nil {
err = fmt.Errorf("syncuser error: %w", err) err = fmt.Errorf("syncuser error: %w", err)
return return

View File

@@ -12,14 +12,12 @@ import (
"syscall" "syscall"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/warewulf/warewulf/internal/pkg/config"
"github.com/warewulf/warewulf/internal/pkg/util" "github.com/warewulf/warewulf/internal/pkg/util"
"github.com/warewulf/warewulf/internal/pkg/wwlog" "github.com/warewulf/warewulf/internal/pkg/wwlog"
) )
const passwdPath = "/etc/passwd" // Syncuser updates the /etc/passwd and /etc/group files in the
const groupPath = "/etc/group"
// SyncUids updates the /etc/passwd and /etc/group files in the
// image identified by imageName by installing the equivalent // image identified by imageName by installing the equivalent
// files from the host and appending names only in the // files from the host and appending names only in the
// image. Files in the image are updated to match the new // 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 // 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 // an entry in the host and the host does not have an entry with the
// same name. In this case, an error is returned. // same name. In this case, an error is returned.
func SyncUids(imageName string, write bool) error { func Syncuser(imageName string, write bool) error {
wwlog.Debug("SyncUids(imageName=%v, write=%v)", imageName, write) wwlog.Debug("Syncuser(imageName=%v, write=%v)", imageName, write)
conf := config.Get()
imagePath := RootFsDir(imageName) imagePath := RootFsDir(imageName)
imagePasswdPath := path.Join(imagePath, passwdPath) imagePasswdPath := path.Join(imagePath, "/etc/passwd")
imageGroupPath := path.Join(imagePath, groupPath) imageGroupPath := path.Join(imagePath, "/etc/group")
passwdSync := make(syncDB) passwdSync := make(syncDB)
if err := passwdSync.readFromHost(passwdPath); err != nil { if err := passwdSync.readFromHost(path.Join(conf.Paths.Sysconfdir, "passwd")); err != nil {
return err return err
} }
if err := passwdSync.readFromimage(imagePasswdPath); err != nil { if err := passwdSync.readFromimage(imagePasswdPath); err != nil {
@@ -51,7 +50,7 @@ func SyncUids(imageName string, write bool) error {
} }
groupSync := make(syncDB) groupSync := make(syncDB)
if err := groupSync.readFromHost(groupPath); err != nil { if err := groupSync.readFromHost(path.Join(conf.Paths.Sysconfdir, "group")); err != nil {
return err return err
} }
if err := groupSync.readFromimage(imageGroupPath); err != nil { if err := groupSync.readFromimage(imageGroupPath); err != nil {
@@ -78,10 +77,10 @@ func SyncUids(imageName string, write bool) error {
if err := groupSync.chownGroupFiles(); err != nil { if err := groupSync.chownGroupFiles(); err != nil {
return err return err
} }
if err := passwdSync.update(imagePasswdPath, passwdPath); err != nil { if err := passwdSync.update(imagePasswdPath, "/etc/passwd"); err != nil {
return err return err
} }
if err := groupSync.update(imageGroupPath, groupPath); err != nil { if err := groupSync.update(imageGroupPath, "/etc/group"); err != nil {
return err return err
} }
wwlog.Info("uid/gid synced for image %s", imageName) wwlog.Info("uid/gid synced for image %s", imageName)