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

Support .vhd file #124

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
79 changes: 66 additions & 13 deletions builder/virtualbox/common/step_attach_isos.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ package common
import (
"context"
"fmt"
"github.com/hashicorp/packer-plugin-sdk/tmp"
"io"
"log"
"os"
"path/filepath"

"github.com/hashicorp/packer-plugin-sdk/multistep"
Expand Down Expand Up @@ -74,7 +77,7 @@ func (s *StepAttachISOs) Run(ctx context.Context, state multistep.StateBag) mult

// We have three different potential isos we can attach, so let's
// assign each one its own spot so they don't conflict.
var controllerName string
var controllerName, diskType string
var device, port int
switch diskCategory {
case "boot_iso":
Expand All @@ -91,11 +94,26 @@ func (s *StepAttachISOs) Run(ctx context.Context, state multistep.StateBag) mult
port = 13
device = 0
}
// If iso is a vhd, copy it to a temp dir and attach it as a hdd, else attach it as a dvddrive
if filepath.Ext(isoPath) == ".vhd" {
ui.Say("Copying boot VHD...")
isoPath, err = s.CopyVHD(isoPath)
if err != nil {
err := fmt.Errorf("error copying VHD file: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
diskType = "hdd"
} else {
diskType = "dvddrive"
}
ui.Message("Mounting boot ISO...")
case "guest_additions":
controllerName = "IDE Controller"
port = 1
device = 0
diskType = "dvddrive"
if s.GuestAdditionsInterface == "sata" {
controllerName = "SATA Controller"
port = 14
Expand All @@ -110,6 +128,7 @@ func (s *StepAttachISOs) Run(ctx context.Context, state multistep.StateBag) mult
controllerName = "IDE Controller"
port = 1
device = 1
diskType = "dvddrive"
if s.ISOInterface == "sata" {
controllerName = "SATA Controller"
port = 15
Expand All @@ -128,7 +147,7 @@ func (s *StepAttachISOs) Run(ctx context.Context, state multistep.StateBag) mult
"--storagectl", controllerName,
"--port", strconv.Itoa(port),
"--device", strconv.Itoa(device),
"--type", "dvddrive",
"--type", diskType,
"--medium", isoPath,
}
if err := driver.VBoxManage(command...); err != nil {
Expand All @@ -138,18 +157,20 @@ func (s *StepAttachISOs) Run(ctx context.Context, state multistep.StateBag) mult
return multistep.ActionHalt
}

// Track the disks we've mounted so we can remove them without having
// to re-derive what was mounted where
unmountCommand := []string{
"storageattach", vmName,
"--storagectl", controllerName,
"--port", strconv.Itoa(port),
"--device", strconv.Itoa(device),
"--type", "dvddrive",
"--medium", "none",
}
if diskType != "hdd" {
// Track the disks we've mounted so we can remove them without having
// to re-derive what was mounted where
unmountCommand := []string{
"storageattach", vmName,
"--storagectl", controllerName,
"--port", strconv.Itoa(port),
"--device", strconv.Itoa(device),
"--type", diskType,
"--medium", "none",
}

s.diskUnmountCommands[diskCategory] = unmountCommand
s.diskUnmountCommands[diskCategory] = unmountCommand
}
}

state.Put("disk_unmount_commands", s.diskUnmountCommands)
Expand All @@ -174,3 +195,35 @@ func (s *StepAttachISOs) Cleanup(state multistep.StateBag) {
}
}
}

func (s *StepAttachISOs) CopyVHD(path string) (string, error) {
tempDir, err := tmp.Dir("virtualbox")
if err != nil {
return "", err
}

sourceFile, err := os.Open(path)
if err != nil {
return "", err
}
defer sourceFile.Close()

destinationFile, err := os.Create(tempDir + "/" + filepath.Base(path))
if err != nil {
return "", err
}
defer destinationFile.Close()

_, err = io.Copy(destinationFile, sourceFile)
if err != nil {
return "", err
}

err = destinationFile.Chmod(0666)
if err != nil {
return "", err
}
newVHDPath := destinationFile.Name()

return newVHDPath, nil
}
86 changes: 45 additions & 41 deletions builder/virtualbox/iso/step_create_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ package iso
import (
"context"
"fmt"

"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
vboxcommon "github.com/hashicorp/packer-plugin-virtualbox/builder/virtualbox/common"

"path/filepath"
"strconv"
"strings"
Expand All @@ -25,45 +23,6 @@ func (s *stepCreateDisk) Run(ctx context.Context, state multistep.StateBag) mult
driver := state.Get("driver").(vboxcommon.Driver)
ui := state.Get("ui").(packersdk.Ui)
vmName := state.Get("vmName").(string)
format := "VDI"

// The main disk and additional disks
diskFullPaths := []string{}
diskSizes := []uint{config.DiskSize}
if len(config.AdditionalDiskSize) == 0 {
// If there are no additional disks, use disk naming as before
diskFullPaths = append(diskFullPaths, filepath.Join(config.OutputDir, fmt.Sprintf("%s.%s", config.VMName, strings.ToLower(format))))
} else {
// If there are additional disks, use consistent naming with numbers
diskFullPaths = append(diskFullPaths, filepath.Join(config.OutputDir, fmt.Sprintf("%s-0.%s", config.VMName, strings.ToLower(format))))

for i, diskSize := range config.AdditionalDiskSize {
path := filepath.Join(config.OutputDir, fmt.Sprintf("%s-%d.%s", config.VMName, i+1, strings.ToLower(format)))
diskFullPaths = append(diskFullPaths, path)
diskSizes = append(diskSizes, diskSize)
}
}

// Create all required disks
for i := range diskFullPaths {
ui.Say(fmt.Sprintf("Creating hard drive %s with size %d MiB...", diskFullPaths[i], diskSizes[i]))

command := []string{
"createhd",
"--filename", diskFullPaths[i],
"--size", strconv.FormatUint(uint64(diskSizes[i]), 10),
"--format", format,
"--variant", "Standard",
}

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

// Add the IDE controller so we can later attach the disk.
// When the hard disk controller is not IDE, this device is still used
Expand Down Expand Up @@ -116,6 +75,51 @@ func (s *stepCreateDisk) Run(ctx context.Context, state multistep.StateBag) mult
}
}

format := "VDI"

// The main disk and additional disks
diskFullPaths := []string{}
diskSizes := []uint{config.DiskSize}
if len(config.AdditionalDiskSize) == 0 {
// If there are no additional disks, use disk naming as before
diskFullPaths = append(diskFullPaths, filepath.Join(config.OutputDir, fmt.Sprintf("%s.%s", config.VMName, strings.ToLower(format))))
} else {
// If there are additional disks, use consistent naming with numbers
diskFullPaths = append(diskFullPaths, filepath.Join(config.OutputDir, fmt.Sprintf("%s-0.%s", config.VMName, strings.ToLower(format))))

for i, diskSize := range config.AdditionalDiskSize {
diskPath := filepath.Join(config.OutputDir, fmt.Sprintf("%s-%d.%s", config.VMName, i+1, strings.ToLower(format)))
diskFullPaths = append(diskFullPaths, diskPath)
diskSizes = append(diskSizes, diskSize)
}
}

// Don't create disks if the iso is a vhd
if filepath.Ext(state.Get("iso_path").(string)) == ".vhd" {
return multistep.ActionContinue
}

// Else, create all required disks
for i := range diskFullPaths {
ui.Say(fmt.Sprintf("Creating hard drive %s with size %d MiB...", diskFullPaths[i], diskSizes[i]))

command := []string{
"createhd",
"--filename", diskFullPaths[i],
"--size", strconv.FormatUint(uint64(diskSizes[i]), 10),
"--format", format,
"--variant", "Standard",
}

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

// Attach the disk to the controller
controllerName := "IDE Controller"
if config.HardDriveInterface == "sata" {
Expand Down