File: step_create_disk.go

package info (click to toggle)
packer 1.6.6%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 32,016 kB
  • sloc: sh: 1,154; python: 619; makefile: 251; ruby: 205; xml: 97
file content (91 lines) | stat: -rw-r--r-- 2,593 bytes parent folder | download | duplicates (2)
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
package qemu

import (
	"context"
	"fmt"
	"log"
	"path/filepath"

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

// This step creates the virtual disk that will be used as the
// hard drive for the virtual machine.
type stepCreateDisk struct {
	AdditionalDiskSize []string
	DiskImage          bool
	DiskSize           string
	Format             string
	OutputDir          string
	UseBackingFile     bool
	VMName             string
	QemuImgArgs        QemuImgArgs
}

func (s *stepCreateDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
	driver := state.Get("driver").(Driver)
	ui := state.Get("ui").(packersdk.Ui)
	name := s.VMName

	if len(s.AdditionalDiskSize) > 0 || s.UseBackingFile {
		ui.Say("Creating required virtual machine disks")
	}

	// The 'main' or 'default' disk
	diskFullPaths := []string{filepath.Join(s.OutputDir, name)}
	diskSizes := []string{s.DiskSize}

	// Additional disks
	if len(s.AdditionalDiskSize) > 0 {
		for i, diskSize := range s.AdditionalDiskSize {
			path := filepath.Join(s.OutputDir, fmt.Sprintf("%s-%d", name, i+1))
			diskFullPaths = append(diskFullPaths, path)
			diskSizes = append(diskSizes, diskSize)
		}
	}

	// Create all required disks
	for i, diskFullPath := range diskFullPaths {
		if s.DiskImage && !s.UseBackingFile && i == 0 {
			// Let the copy disk step (step_copy_disk.go) create the 'main' or
			// 'default' disk.
			continue
		}
		log.Printf("[INFO] Creating disk with Path: %s and Size: %s", diskFullPath, diskSizes[i])

		command := s.buildCreateCommand(diskFullPath, diskSizes[i], i, state)

		if err := driver.QemuImg(command...); err != nil {
			err := fmt.Errorf("Error creating hard drive: %s", err)
			state.Put("error", err)
			ui.Error(err.Error())
			return multistep.ActionHalt
		}
	}

	// Stash the disk paths so we can retrieve later
	state.Put("qemu_disk_paths", diskFullPaths)

	return multistep.ActionContinue
}

func (s *stepCreateDisk) buildCreateCommand(path string, size string, i int, state multistep.StateBag) []string {
	command := []string{"create", "-f", s.Format}

	if s.DiskImage && s.UseBackingFile && i == 0 {
		// Use a backing file for the 'main' or 'default' disk
		isoPath := state.Get("iso_path").(string)
		command = append(command, "-b", isoPath)
	}

	// add user-provided convert args
	command = append(command, s.QemuImgArgs.Create...)

	// add target path and size.
	command = append(command, path, size)

	return command
}

func (s *stepCreateDisk) Cleanup(state multistep.StateBag) {}