From a208584ad0509d4b06c0a2a2e3f3a4c181add202 Mon Sep 17 00:00:00 2001 From: Yuval Kohavi Date: Mon, 18 May 2020 07:45:08 -0400 Subject: [PATCH 1/5] dont use resolveconf on system --- pkg/builder/builder.go | 16 +++++++++++++--- pkg/image/utils/images.go | 1 + 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/pkg/builder/builder.go b/pkg/builder/builder.go index ca61660f..b869c3e0 100644 --- a/pkg/builder/builder.go +++ b/pkg/builder/builder.go @@ -36,13 +36,20 @@ var ( knownArgs = map[utils.KnownImageType][]string{ utils.BeagleBone: {"-cpu", "cortex-a8"}, } - defaultChrootTypes = [][]string{ + + defaultBase = [][]string{ {"proc", "proc", "/proc"}, {"sysfs", "sysfs", "/sys"}, {"bind", "/dev", "/dev"}, {"devpts", "devpts", "/dev/pts"}, {"binfmt_misc", "binfmt_misc", "/proc/sys/fs/binfmt_misc"}, - {"bind", "/etc/resolv.conf", "/etc/resolv.conf"}, + } + + defaultChrootTypes = map[string][][]string{ + utils.RaspberryPi: defaultBase, + // for non systemd ones, we want to mount resolv.conf as well. + // this may change to not be a default. + utils.Unknown: append(defaultBase, [][]string{"bind", "/etc/resolv.conf", "/etc/resolv.conf"}), } ) @@ -157,7 +164,10 @@ func (b *Builder) Prepare(cfgs ...interface{}) ([]string, []string, error) { } if len(b.config.ChrootMounts) == 0 { - b.config.ChrootMounts = defaultChrootTypes + b.config.ChrootMounts = defaultChrootTypes[utils.Unknown] + if imageDefaults, ok := defaultChrootTypes[b.config.ImageType]; ok { + b.config.ChrootMounts = imageDefaults + } } if len(b.config.AdditionalChrootMounts) > 0 { diff --git a/pkg/image/utils/images.go b/pkg/image/utils/images.go index cada016a..3a7dd602 100644 --- a/pkg/image/utils/images.go +++ b/pkg/image/utils/images.go @@ -12,6 +12,7 @@ const ( RaspberryPi KnownImageType = "raspberrypi" BeagleBone KnownImageType = "beaglebone" Kali KnownImageType = "kali" + Unknown KnownImageType = "" ) func GuessImageType(url string) KnownImageType { From 92700adfa65cd63b45d7cda1e7248d2b1f39297f Mon Sep 17 00:00:00 2001 From: Yuval Kohavi Date: Mon, 18 May 2020 07:53:27 -0400 Subject: [PATCH 2/5] build --- pkg/builder/builder.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/builder/builder.go b/pkg/builder/builder.go index b869c3e0..8a1e840f 100644 --- a/pkg/builder/builder.go +++ b/pkg/builder/builder.go @@ -45,11 +45,11 @@ var ( {"binfmt_misc", "binfmt_misc", "/proc/sys/fs/binfmt_misc"}, } - defaultChrootTypes = map[string][][]string{ + defaultChrootTypes = map[utils.KnownImageType][][]string{ utils.RaspberryPi: defaultBase, // for non systemd ones, we want to mount resolv.conf as well. // this may change to not be a default. - utils.Unknown: append(defaultBase, [][]string{"bind", "/etc/resolv.conf", "/etc/resolv.conf"}), + utils.Unknown: append(defaultBase, []string{"bind", "/etc/resolv.conf", "/etc/resolv.conf"}), } ) From dd37343087044c728a95f6ca58db677e7d0d5770 Mon Sep 17 00:00:00 2001 From: Yuval Kohavi Date: Wed, 20 May 2020 08:01:10 -0400 Subject: [PATCH 3/5] copy resolv conf --- pkg/builder/builder.go | 15 +++++--- pkg/builder/step_copy_resolv_conf.go | 51 ++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 pkg/builder/step_copy_resolv_conf.go diff --git a/pkg/builder/builder.go b/pkg/builder/builder.go index 8a1e840f..6c0a890f 100644 --- a/pkg/builder/builder.go +++ b/pkg/builder/builder.go @@ -46,10 +46,7 @@ var ( } defaultChrootTypes = map[utils.KnownImageType][][]string{ - utils.RaspberryPi: defaultBase, - // for non systemd ones, we want to mount resolv.conf as well. - // this may change to not be a default. - utils.Unknown: append(defaultBase, []string{"bind", "/etc/resolv.conf", "/etc/resolv.conf"}), + utils.Unknown: defaultBase, } ) @@ -95,6 +92,9 @@ type Config struct { // for example: `["bind", "/run/systemd", "/run/systemd"]` AdditionalChrootMounts [][]string `mapstructure:"additional_chroot_mounts"` + // Should we copy over /etc/resolv.conf from the host? + CopyResolvConf bool + // Should the last partition be extended? this only works for the last partition in the // dos partition table, and ext filesystem LastPartitionExtraSize uint64 `mapstructure:"last_partition_extra_size"` @@ -243,7 +243,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack state.Put("ui", ui) state.Put("wrappedCommand", packer_common.CommandWrapper(wrappedCommand)) - // HACK: go-getter automatically decompreses, which hurts caching. + // HACK: go-getter automatically decompress, which hurts caching. // additionally, we use native binaries to decompress which is faster anyway. // disable decompressors: getter.Decompressors = make(map[string]getter.Decompressor) @@ -281,6 +281,11 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack &StepMountExtra{ChrootKey: "mount_path"}, ) + if b.config.CopyResolvConf{ + steps = append(steps, + &stepCopyResolvConf{ChrootKey: "mount_path"}, + } + native := runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" if !native { steps = append(steps, diff --git a/pkg/builder/step_copy_resolv_conf.go b/pkg/builder/step_copy_resolv_conf.go new file mode 100644 index 00000000..dc34d6e8 --- /dev/null +++ b/pkg/builder/step_copy_resolv_conf.go @@ -0,0 +1,51 @@ +package builder + +// taken from here: https://github.com/hashicorp/packer/blob/81522dced0b25084a824e79efda02483b12dc7cd/builder/amazon/chroot/step_chroot_provision.go + +import ( + "context" + "io" + "os" + "path/filepath" + + "github.com/hashicorp/packer/helper/multistep" + "github.com/hashicorp/packer/packer" +) + +// stepCopyResolvConf provisions the instance within a chroot. +type stepCopyResolvConf struct { + ChrootKey string +} + +func (s *stepCopyResolvConf) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { + mountPath := state.Get(s.ChrootKey).(string) + ui := state.Get("ui").(packer.Ui) + + origResolvConf = "/etc/resolv.conf" + destResolvConf = filepath.Join(mountPath, origResolvConf) + + // copy file over: + err = copyFile(destResolvConf, origResolvConf) + if err != nil { + ui.Error(err.Error()) + return multistep.ActionHalt + } + + return multistep.ActionContinue +} + +func (s *stepCopyResolvConf) Cleanup(state multistep.StateBag) {} + +func copyFile(dst, src string) error { + in, err := os.Open(src) + if err != nil { + return + } + defer in.Close() + out, err := os.Create(dst) + if err != nil { + return + } + defer out.Close() + return io.Copy(out, in) +} From 794e60939bd0a5815c5877ee8569936d06263de7 Mon Sep 17 00:00:00 2001 From: Yuval Kohavi Date: Sun, 24 May 2020 12:27:03 -0400 Subject: [PATCH 4/5] custom handling of resolve conf --- pkg/builder/builder.go | 22 ++++++++-- pkg/builder/step_copy_resolv_conf.go | 51 --------------------- pkg/builder/step_handle_resolv_conf.go | 61 ++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 55 deletions(-) delete mode 100644 pkg/builder/step_copy_resolv_conf.go create mode 100644 pkg/builder/step_handle_resolv_conf.go diff --git a/pkg/builder/builder.go b/pkg/builder/builder.go index 6c0a890f..249a70ba 100644 --- a/pkg/builder/builder.go +++ b/pkg/builder/builder.go @@ -44,12 +44,22 @@ var ( {"devpts", "devpts", "/dev/pts"}, {"binfmt_misc", "binfmt_misc", "/proc/sys/fs/binfmt_misc"}, } + resolvConfBindMount = []string{"bind", "/etc/resolv.conf", "/etc/resolv.conf"} defaultChrootTypes = map[utils.KnownImageType][][]string{ utils.Unknown: defaultBase, } ) +type ResolvConfBehavior string + +const ( + Off ResolvConfBehavior = "off" + CopyHost ResolvConfBehavior = "copy-host" + BindHost ResolvConfBehavior = "bind-host" + Delete ResolvConfBehavior = "delete" +) + type Config struct { packer_common.PackerConfig `mapstructure:",squash"` // While arm image are not ISOs, we resuse the ISO logic as it basically has no ISO specific code. @@ -92,8 +102,8 @@ type Config struct { // for example: `["bind", "/run/systemd", "/run/systemd"]` AdditionalChrootMounts [][]string `mapstructure:"additional_chroot_mounts"` - // Should we copy over /etc/resolv.conf from the host? - CopyResolvConf bool + // Can be one of: off, copy-host, bind-host, delete. Defaults to off + ResolvConf ResolvConfBehavior `mapstructure:"resolv-conf"` // Should the last partition be extended? this only works for the last partition in the // dos partition table, and ext filesystem @@ -174,6 +184,10 @@ func (b *Builder) Prepare(cfgs ...interface{}) ([]string, []string, error) { b.config.ChrootMounts = append(b.config.ChrootMounts, b.config.AdditionalChrootMounts...) } + if b.config.ResolvConf == BindHost { + b.config.ChrootMounts = append(b.config.ChrootMounts, resolvConfBindMount) + } + if b.config.CommandWrapper == "" { b.config.CommandWrapper = "{{.Command}}" } @@ -281,9 +295,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack &StepMountExtra{ChrootKey: "mount_path"}, ) - if b.config.CopyResolvConf{ + if b.config.ResolvConf == CopyHost || b.config.ResolvConf == Delete { steps = append(steps, - &stepCopyResolvConf{ChrootKey: "mount_path"}, + &stepHandleResolvConf{ChrootKey: "mount_path", Delete: b.config.ResolvConf == Delete}) } native := runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" diff --git a/pkg/builder/step_copy_resolv_conf.go b/pkg/builder/step_copy_resolv_conf.go deleted file mode 100644 index dc34d6e8..00000000 --- a/pkg/builder/step_copy_resolv_conf.go +++ /dev/null @@ -1,51 +0,0 @@ -package builder - -// taken from here: https://github.com/hashicorp/packer/blob/81522dced0b25084a824e79efda02483b12dc7cd/builder/amazon/chroot/step_chroot_provision.go - -import ( - "context" - "io" - "os" - "path/filepath" - - "github.com/hashicorp/packer/helper/multistep" - "github.com/hashicorp/packer/packer" -) - -// stepCopyResolvConf provisions the instance within a chroot. -type stepCopyResolvConf struct { - ChrootKey string -} - -func (s *stepCopyResolvConf) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { - mountPath := state.Get(s.ChrootKey).(string) - ui := state.Get("ui").(packer.Ui) - - origResolvConf = "/etc/resolv.conf" - destResolvConf = filepath.Join(mountPath, origResolvConf) - - // copy file over: - err = copyFile(destResolvConf, origResolvConf) - if err != nil { - ui.Error(err.Error()) - return multistep.ActionHalt - } - - return multistep.ActionContinue -} - -func (s *stepCopyResolvConf) Cleanup(state multistep.StateBag) {} - -func copyFile(dst, src string) error { - in, err := os.Open(src) - if err != nil { - return - } - defer in.Close() - out, err := os.Create(dst) - if err != nil { - return - } - defer out.Close() - return io.Copy(out, in) -} diff --git a/pkg/builder/step_handle_resolv_conf.go b/pkg/builder/step_handle_resolv_conf.go new file mode 100644 index 00000000..74adfa51 --- /dev/null +++ b/pkg/builder/step_handle_resolv_conf.go @@ -0,0 +1,61 @@ +package builder + +// taken from here: https://github.com/hashicorp/packer/blob/81522dced0b25084a824e79efda02483b12dc7cd/builder/amazon/chroot/step_chroot_provision.go + +import ( + "context" + "io" + "os" + "path/filepath" + + "github.com/hashicorp/packer/helper/multistep" + "github.com/hashicorp/packer/packer" +) + +// stepHandleResolvConf provisions the instance within a chroot. +type stepHandleResolvConf struct { + ChrootKey string + Delete bool +} + +func (s *stepHandleResolvConf) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { + mountPath := state.Get(s.ChrootKey).(string) + ui := state.Get("ui").(packer.Ui) + + const origResolvConf = "/etc/resolv.conf" + destResolvConf := filepath.Join(mountPath, origResolvConf) + + if s.Delete { + err := os.Remove(destResolvConf) + if err != nil { + ui.Error(err.Error()) + return multistep.ActionHalt + } + } else { + // copy file over: + err := copyFile(destResolvConf, origResolvConf) + if err != nil { + ui.Error(err.Error()) + return multistep.ActionHalt + } + } + + return multistep.ActionContinue +} + +func (s *stepHandleResolvConf) Cleanup(state multistep.StateBag) {} + +func copyFile(dst, src string) error { + in, err := os.Open(src) + if err != nil { + return err + } + defer in.Close() + out, err := os.Create(dst) + if err != nil { + return err + } + defer out.Close() + _, err = io.Copy(out, in) + return err +} From d2bc25028bc0b161630a27139f1b14274b6f1a89 Mon Sep 17 00:00:00 2001 From: Yuval Kohavi Date: Sun, 24 May 2020 12:29:02 -0400 Subject: [PATCH 5/5] generated code --- pkg/builder/builder.hcl2spec.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/builder/builder.hcl2spec.go b/pkg/builder/builder.hcl2spec.go index 1b679c02..0c563245 100644 --- a/pkg/builder/builder.hcl2spec.go +++ b/pkg/builder/builder.hcl2spec.go @@ -32,6 +32,7 @@ type FlatConfig struct { MountPath *string `mapstructure:"mount_path" cty:"mount_path"` ChrootMounts [][]string `mapstructure:"chroot_mounts" cty:"chroot_mounts"` AdditionalChrootMounts [][]string `mapstructure:"additional_chroot_mounts" cty:"additional_chroot_mounts"` + ResolvConf *ResolvConfBehavior `mapstructure:"resolv-conf" cty:"resolv-conf"` LastPartitionExtraSize *uint64 `mapstructure:"last_partition_extra_size" cty:"last_partition_extra_size"` TargetImageSize *uint64 `mapstructure:"target_image_size" cty:"target_image_size"` QemuBinary *string `mapstructure:"qemu_binary" cty:"qemu_binary"` @@ -72,6 +73,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "mount_path": &hcldec.AttrSpec{Name: "mount_path", Type: cty.String, Required: false}, "chroot_mounts": &hcldec.BlockListSpec{TypeName: "chroot_mounts", Nested: &hcldec.AttrSpec{Name: "chroot_mounts", Type: cty.List(cty.String), Required: false}}, "additional_chroot_mounts": &hcldec.BlockListSpec{TypeName: "additional_chroot_mounts", Nested: &hcldec.AttrSpec{Name: "additional_chroot_mounts", Type: cty.List(cty.String), Required: false}}, + "resolv-conf": &hcldec.AttrSpec{Name: "resolv-conf", Type: cty.String, Required: false}, "last_partition_extra_size": &hcldec.AttrSpec{Name: "last_partition_extra_size", Type: cty.Number, Required: false}, "target_image_size": &hcldec.AttrSpec{Name: "target_image_size", Type: cty.Number, Required: false}, "qemu_binary": &hcldec.AttrSpec{Name: "qemu_binary", Type: cty.String, Required: false},