Skip to content

Commit

Permalink
incusd/instance/lxc: Add support for template ownership/mode
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Graber <[email protected]>
  • Loading branch information
stgraber committed Feb 6, 2024
1 parent 16440f8 commit 62fdb1f
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions internal/server/instance/drivers/driver_lxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -6623,6 +6624,43 @@ func (d *lxc) templateApplyNow(trigger instance.TemplateTrigger) error {
return fmt.Errorf("Failed to create template file: %w", err)
}
} else {
// UID and GID
fileUID := int64(0)
fileGID := int64(0)

if tpl.UID != "" {
id, err := strconv.ParseInt(tpl.UID, 10, 64)
if err != nil {
return fmt.Errorf("Bad file UID %q for %q: %w", tpl.UID, tplPath, err)
}

fileUID = id
}

if tpl.GID != "" {
id, err := strconv.ParseInt(tpl.GID, 10, 64)
if err != nil {
return fmt.Errorf("Bad file GID %q for %q: %w", tpl.GID, tplPath, err)
}

fileGID = id
}

if idmapset != nil {
fileUID, fileGID = idmapset.ShiftIntoNS(fileUID, fileGID)
}

// Mode
fileMode := fs.FileMode(0644)
if tpl.Mode != "" {
mode, err := strconv.ParseInt(tpl.Mode, 0, 0)
if err != nil {
return fmt.Errorf("Bad mode %q for %q: %w", tpl.Mode, tplPath, err)
}

fileMode = os.FileMode(mode) & os.ModePerm
}

// Create the directories leading to the file
err = internalUtil.MkdirAllOwner(path.Dir(fullpath), 0755, int(rootUID), int(rootGID))
if err != nil {
Expand All @@ -6636,12 +6674,12 @@ func (d *lxc) templateApplyNow(trigger instance.TemplateTrigger) error {
}

// Fix ownership and mode
err = w.Chown(int(rootUID), int(rootGID))
err = w.Chown(int(fileUID), int(fileGID))
if err != nil {
return err
}

err = w.Chmod(0644)
err = w.Chmod(fileMode)
if err != nil {
return err
}
Expand Down

0 comments on commit 62fdb1f

Please sign in to comment.