Skip to content

Commit

Permalink
dont use resolveconf on systemd (#70)
Browse files Browse the repository at this point in the history
* dont use resolveconf on system

* build

* copy resolv conf

* custom handling of resolve conf

* generated code
  • Loading branch information
yuval-k authored May 26, 2020
1 parent bf52fd3 commit 536e21e
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 4 deletions.
37 changes: 33 additions & 4 deletions pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,30 @@ 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"},
}
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.
Expand Down Expand Up @@ -88,6 +102,9 @@ type Config struct {
// for example: `["bind", "/run/systemd", "/run/systemd"]`
AdditionalChrootMounts [][]string `mapstructure:"additional_chroot_mounts"`

// 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
LastPartitionExtraSize uint64 `mapstructure:"last_partition_extra_size"`
Expand Down Expand Up @@ -157,13 +174,20 @@ 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 {
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}}"
}
Expand Down Expand Up @@ -233,7 +257,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)
Expand Down Expand Up @@ -271,6 +295,11 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
&StepMountExtra{ChrootKey: "mount_path"},
)

if b.config.ResolvConf == CopyHost || b.config.ResolvConf == Delete {
steps = append(steps,
&stepHandleResolvConf{ChrootKey: "mount_path", Delete: b.config.ResolvConf == Delete})
}

native := runtime.GOARCH == "arm" || runtime.GOARCH == "arm64"
if !native {
steps = append(steps,
Expand Down
2 changes: 2 additions & 0 deletions pkg/builder/builder.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions pkg/builder/step_handle_resolv_conf.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions pkg/image/utils/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
RaspberryPi KnownImageType = "raspberrypi"
BeagleBone KnownImageType = "beaglebone"
Kali KnownImageType = "kali"
Unknown KnownImageType = ""
)

func GuessImageType(url string) KnownImageType {
Expand Down

0 comments on commit 536e21e

Please sign in to comment.