From e5b740e22322e0e455c2b1f9584c64cc252bedb6 Mon Sep 17 00:00:00 2001 From: Omer Katz Date: Sun, 29 Jul 2018 13:16:41 +0300 Subject: [PATCH 1/2] Determine lxc root according to the running user. --- builder/lxc/step_lxc_create.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/builder/lxc/step_lxc_create.go b/builder/lxc/step_lxc_create.go index 0c89ce190..1526ed715 100644 --- a/builder/lxc/step_lxc_create.go +++ b/builder/lxc/step_lxc_create.go @@ -3,6 +3,8 @@ package lxc import ( "context" "fmt" + "log" + "os/user" "path/filepath" "github.com/hashicorp/packer/helper/multistep" @@ -19,6 +21,13 @@ func (s *stepLxcCreate) Run(_ context.Context, state multistep.StateBag) multist // TODO: read from env lxc_dir := "/var/lib/lxc" + user, err := user.Current() + if err != nil { + log.Print("Cannot find current user. Falling back to /var/lib/lxc...") + } + if user.Uid != "0" && user.HomeDir != "" { + lxc_dir = filepath.Join(user.HomeDir, ".local", "share", "lxc") + } rootfs := filepath.Join(lxc_dir, name, "rootfs") if config.PackerForce { From 26dd6441e039d694b4c15936e0c253632adf1739 Mon Sep 17 00:00:00 2001 From: Omer Katz Date: Sun, 29 Jul 2018 16:14:17 +0300 Subject: [PATCH 2/2] Locate lxc root directory when exporting as well. --- builder/lxc/step_export.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/builder/lxc/step_export.go b/builder/lxc/step_export.go index ddfa85048..b6f5aca5f 100644 --- a/builder/lxc/step_export.go +++ b/builder/lxc/step_export.go @@ -4,7 +4,9 @@ import ( "context" "fmt" "io" + "log" "os" + "os/user" "path/filepath" "github.com/hashicorp/packer/helper/multistep" @@ -19,7 +21,16 @@ func (s *stepExport) Run(_ context.Context, state multistep.StateBag) multistep. name := config.ContainerName - containerDir := fmt.Sprintf("/var/lib/lxc/%s", name) + lxc_dir := "/var/lib/lxc" + user, err := user.Current() + if err != nil { + log.Print("Cannot find current user. Falling back to /var/lib/lxc...") + } + if user.Uid != "0" && user.HomeDir != "" { + lxc_dir = filepath.Join(user.HomeDir, ".local", "share", "lxc") + } + + containerDir := filepath.Join(lxc_dir, name) outputPath := filepath.Join(config.OutputDir, "rootfs.tar.gz") configFilePath := filepath.Join(config.OutputDir, "lxc-config")