forked from solo-io/packer-plugin-arm-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
311 lines (260 loc) · 8.42 KB
/
builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package builder
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"runtime"
packer_common "github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
"github.com/solo-io/packer-builder-arm-image/pkg/image"
"github.com/solo-io/packer-builder-arm-image/pkg/image/utils"
)
const BuilderId = "yuval-k.arm-image"
var knownTypes map[utils.KnownImageType][]string
var knownArgs map[utils.KnownImageType][]string
func init() {
knownTypes = make(map[utils.KnownImageType][]string)
knownArgs = make(map[utils.KnownImageType][]string)
knownTypes[utils.RaspberryPi] = []string{"/boot", "/"}
knownTypes[utils.BeagleBone] = []string{"/"}
knownTypes[utils.Kali] = []string{"/root", "/"}
knownArgs[utils.BeagleBone] = []string{"-cpu", "cortex-a8"}
}
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.
// Provide the arm image in the iso_url fields.
packer_common.ISOConfig `mapstructure:",squash"`
// Lets you prefix all builder commands, such as with ssh for a remote build host. Defaults to "".
// Copied from other builders :)
CommandWrapper string `mapstructure:"command_wrapper"`
// Output directory, where the final image will be stored.
OutputDir string `mapstructure:"output_directory"`
// Image type. this is used to deduce other settings like image mounts and qemu args.
// If not provided, we will try to deduce it from the image url. (see autoDetectType())
ImageType utils.KnownImageType `mapstructure:"image_type"`
// Where to mounts the image partitions in the chroot.
// first entry is the mount point of the first partition. etc..
ImageMounts []string `mapstructure:"image_mounts"`
// What directories mount from the host to the chroot.
// leave it empty for reasonable deafults.
// array of triplets: [type, device, mntpoint].
ChrootMounts [][]string `mapstructure:"chroot_mounts"`
// 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"`
// The target size of the final image. The last partiation will be extended to
// 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.
TargetImageSize uint64 `mapstructure:"target_image_size"`
// Qemu binary to use. default is qemu-arm-static
QemuBinary string `mapstructure:"qemu_binary"`
// Arguments to qemu binary. default depends on the image type. see init() function above.
QemuArgs []string `mapstructure:"qemu_args"`
ctx interpolate.Context
}
type Builder struct {
config Config
runner *multistep.BasicRunner
context context.Context
cancel context.CancelFunc
}
func NewBuilder() *Builder {
ctx, cancel := context.WithCancel(context.Background())
return &Builder{
context: ctx,
cancel: cancel,
}
}
func (b *Builder) autoDetectType() utils.KnownImageType {
if len(b.config.ISOUrls) < 1 {
return ""
}
url := b.config.ISOUrls[0]
return utils.GuessImageType(url)
}
func (b *Builder) Prepare(cfgs ...interface{}) ([]string, error) {
err := config.Decode(&b.config, &config.DecodeOpts{
Interpolate: true,
InterpolateFilter: &interpolate.RenderFilter{},
}, cfgs...)
if err != nil {
return nil, err
}
var errs *packer.MultiError
var warnings []string
isoWarnings, isoErrs := b.config.ISOConfig.Prepare(&b.config.ctx)
warnings = append(warnings, isoWarnings...)
errs = packer.MultiErrorAppend(errs, isoErrs...)
if b.config.OutputDir == "" {
b.config.OutputDir = fmt.Sprintf("output-%s", b.config.PackerConfig.PackerBuildName)
}
if b.config.ChrootMounts == nil {
b.config.ChrootMounts = make([][]string, 0)
}
if len(b.config.ChrootMounts) == 0 {
b.config.ChrootMounts = [][]string{
{"proc", "proc", "/proc"},
{"sysfs", "sysfs", "/sys"},
{"bind", "/dev", "/dev"},
{"devpts", "devpts", "/dev/pts"},
{"binfmt_misc", "binfmt_misc", "/proc/sys/fs/binfmt_misc"},
}
}
if b.config.CommandWrapper == "" {
b.config.CommandWrapper = "{{.Command}}"
}
if b.config.ImageType == "" {
// defaults...
b.config.ImageType = b.autoDetectType()
} else {
if _, ok := knownTypes[b.config.ImageType]; !ok {
var validvalues []utils.KnownImageType
for k := range knownTypes {
validvalues = append(validvalues, k)
}
errs = packer.MultiErrorAppend(errs, fmt.Errorf("unknown image_type. must be one of: %v", validvalues))
b.config.ImageType = ""
}
}
if b.config.ImageType != "" {
if len(b.config.ImageMounts) == 0 {
b.config.ImageMounts = knownTypes[b.config.ImageType]
}
if len(b.config.QemuArgs) == 0 {
b.config.QemuArgs = knownArgs[b.config.ImageType]
}
}
if len(b.config.ImageMounts) == 0 {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("no image mounts provided. Please set the image mounts or image type."))
}
if b.config.QemuBinary == "" {
b.config.QemuBinary = "qemu-arm-static"
}
// convert to full path
path, err := exec.LookPath(b.config.QemuBinary)
if err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("qemu binary not found."))
} else {
if !strings.Contains(path, "qemu-") {
warnings = append(warnings, "binary doesn't look like qemu-user")
}
b.config.QemuBinary = path
}
if errs != nil && len(errs.Errors) > 0 {
return warnings, errs
}
return warnings, nil
}
type wrappedCommandTemplate struct {
Command string
}
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
wrappedCommand := func(command string) (string, error) {
ctx := b.config.ctx
ctx.Data = &wrappedCommandTemplate{Command: command}
return interpolate.Render(b.config.CommandWrapper, &ctx)
}
state := new(multistep.BasicStateBag)
state.Put("cache", cache)
state.Put("config", &b.config)
state.Put("debug", b.config.PackerDebug)
state.Put("hook", hook)
state.Put("ui", ui)
state.Put("wrappedCommand", CommandWrapper(wrappedCommand))
steps := []multistep.Step{
&packer_common.StepDownload{
Checksum: b.config.ISOChecksum,
ChecksumType: b.config.ISOChecksumType,
Description: "Image",
ResultKey: "iso_path",
Url: b.config.ISOUrls,
Extension: b.config.TargetExtension,
TargetPath: b.config.TargetPath,
},
&stepCopyImage{FromKey: "iso_path", ResultKey: "imagefile", ImageOpener: image.NewImageOpener(ui)},
}
if b.config.LastPartitionExtraSize > 0 {
steps = append(steps,
&stepResizeLastPart{FromKey: "imagefile"},
)
}
steps = append(steps,
&stepMapImage{ImageKey: "imagefile", ResultKey: "partitions"},
)
if b.config.LastPartitionExtraSize > 0 {
steps = append(steps,
&stepResizeFs{PartitionsKey: "partitions"},
)
}
steps = append(steps,
&stepMountImage{PartitionsKey: "partitions", ResultKey: "mount_path"},
&StepMountExtra{ChrootKey: "mount_path"},
)
native := runtime.GOARCH == "arm" || runtime.GOARCH == "arm64"
if !native {
steps = append(steps,
&stepQemuUserStatic{ChrootKey: "mount_path", PathToQemuInChrootKey: "qemuInChroot", Args: Args{Args: b.config.QemuArgs}},
&stepRegisterBinFmt{QemuPathKey: "qemuInChroot"},
)
}
steps = append(steps,
&StepChrootProvision{ChrootKey: "mount_path"},
)
b.runner = &multistep.BasicRunner{Steps: steps}
done := make(chan struct{})
go func() {
select {
case <-done:
return
case <-b.context.Done():
b.runner.Cancel()
hook.Cancel()
}
}()
// Executes the steps
b.runner.Run(state)
close(done)
if rawErr, ok := state.GetOk("error"); ok {
return nil, rawErr.(error)
}
// check if it is ok
_, canceled := state.GetOk(multistep.StateCancelled)
_, halted := state.GetOk(multistep.StateHalted)
if canceled || halted {
return nil, errors.New("step canceled or halted")
}
return &Artifact{image: state.Get("imagefile").(string)}, nil
}
func (b *Builder) Cancel() {
if b.runner != nil {
b.cancel()
}
}
type Artifact struct {
image string
}
func (a *Artifact) BuilderId() string {
return BuilderId
}
func (a *Artifact) Files() []string {
return []string{a.image}
}
func (a *Artifact) Id() string {
return ""
}
func (a *Artifact) String() string {
return a.image
}
func (a *Artifact) State(name string) interface{} {
return nil
}
func (a *Artifact) Destroy() error {
return os.Remove(a.image)
}