Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add macOS support #155

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ builds:
- '-s -w -X {{ .ModulePath }}/version.Version={{.Version}} -X {{ .ModulePath }}/version.VersionPrerelease= '
goos:
- linux
- darwin
goarch:
- amd64
binary: '{{ .ProjectName }}_v{{ .Version }}_{{ .Env.API_VERSION }}_{{ .Os }}_{{ .Arch }}'
Expand All @@ -37,6 +38,7 @@ builds:
- '-s -w -X {{ .ModulePath }}/version.Version={{.Version}} -X {{ .ModulePath }}/version.VersionPrerelease= '
goos:
- linux
- darwin
goarch:
- amd64
- '386'
Expand All @@ -56,6 +58,7 @@ builds:
- '-s -w -X {{ .ModulePath }}/version.Version={{.Version}} -X {{ .ModulePath }}/version.VersionPrerelease= '
goos:
- linux
- darwin
goarch:
- amd64
- '386'
Expand All @@ -71,6 +74,8 @@ checksum:
name_template: '{{ .ProjectName }}_v{{ .Version }}_SHA256SUMS'
algorithm: sha256



release:
# If you want to manually examine the release before its live, uncomment this line:
# draft: true
Expand Down
9 changes: 8 additions & 1 deletion docs/builders/arm-image.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ a [communicator](/docs/templates/legacy_json_templates/communicator) can be conf
If not provided, we will try to deduce it from the image url. (see autoDetectType())
For list of valid values, see: pkg/image/utils/images.go

- `image_arch` (arch.KnownArchType) - Image's target CPU architecture.
This is used to determine if qemu is necessary and which flavor to use.
Defaults to "arm". For list of valid values, see: pkg/image/arch/arch.go

- `image_mounts` ([]string) - Where to mounts the image partitions in the chroot.
first entry is the mount point of the first partition. etc..

Expand All @@ -111,14 +115,17 @@ a [communicator](/docs/templates/legacy_json_templates/communicator) can be conf
fill up this much room. I.e. if the generated image is 256MB and TargetImageSize
is set to 384MB the last partition will be extended with an additional 128MB.

- `qemu_binary` (string) - Qemu binary to use. default is qemu-arm-static
- `qemu_binary` (string) - Qemu binary to use. default is determined based on `image_arch`.
If this is an absolute path, it will be used. Otherwise, we will look for one in your PATH
and finally, try to auto fetch one from https://github.com/multiarch/qemu-user-static/

- `disable_embedded` (bool) - Do not use embedded qemu.

- `qemu_args` ([]string) - Arguments to qemu binary. default depends on the image type. see init() function above.

- `qemu_required` (bool) - Use qemu even when the build machine's CPU architecture matches the image's CPU architecture.
Defaults to true if non-default `qemu_binary` or `qemu_args` are supplied.

<!-- End of code generated from the comments of the Config struct in pkg/builder/config.go; -->


Expand Down
98 changes: 98 additions & 0 deletions pkg/builder/step_map_image_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//go:build darwin
// +build darwin

package builder

import (
"context"
"fmt"
"os/exec"
"regexp"
"sort"
"strings"

"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
)

type stepMapImage struct {
ImageKey string
ResultKey string
}

var (
whitespaceRe = regexp.MustCompile(`\s+`)
diskRe = regexp.MustCompile(`(?m)^(/dev/disk[0-9]+)[a-z][0-9]+$`)
)

func (s *stepMapImage) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
// Read our value and assert that it is the type we want
image := state.Get(s.ImageKey).(string)
ui := state.Get("ui").(packer.Ui)

ui.Message(fmt.Sprintf("mapping %s", image))

// Attach disk image
// -nomount same as -mount suppressed
// -imagekey diskimage-class=CRawDiskImage
// Output example:
// /dev/disk2 FDisk_partition_scheme
// /dev/disk2s1 Windows_FAT_32
// /dev/disk2s2 Linux
out, err := exec.Command("hdiutil", "attach",
"-imagekey", "diskimage-class=CRawDiskImage",
"-nomount", image).CombinedOutput()
cmd := fmt.Sprintf("hdiutil attach -imagekey diskimage-class=CRawDiskImage -nomount %s", image)
ui.Say(cmd)
if err != nil {
ui.Error(fmt.Sprintf("error %s %v: %s", cmd, err, string(out)))
s.Cleanup(state)
return multistep.ActionHalt
}

// Look for all partitions of created loopback
var partitions []string
lines := strings.Split(string(out), "\n")

// make sure /dev/disk2 is always the first line,
// also to make sure disks match the partition map.
sort.Strings(lines)
for _, l := range lines {
split := whitespaceRe.Split(strings.TrimSpace(l), -1)
if len(split) != 2 {
continue
}
partition := split[0]
if !diskRe.MatchString(partition) {
continue
}
partitions = append(partitions, partition)
}

state.Put(s.ResultKey, partitions)

return multistep.ActionContinue
}

func (s *stepMapImage) Cleanup(state multistep.StateBag) {
switch partitions := state.Get(s.ResultKey).(type) {
case nil:
return
case []string:
if len(partitions) > 0 {
// Convert /dev/disk2s1 into /dev/disk2
matches := diskRe.FindAllStringSubmatch(partitions[0], -1)
if len(matches) == 0 {
// must not happen
return
}
if len(matches[0]) == 0 {
// must not happen
return
}
disk := matches[0][1]
run(context.TODO(), state, fmt.Sprintf(
"hdiutil eject -force %s", disk))
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build linux
// +build linux

package builder

import (
Expand Down