Skip to content

Commit

Permalink
Extra CD support, supply files via cd_files or cd_content (#119)
Browse files Browse the repository at this point in the history
* Extra CD support, supply files via cd_files or cd_content

* Fix highlighted go linter errors

* Add suggested tweak

* Use driver for adding CDROM and add tests

---------

Co-authored-by: Carlos Lapao <[email protected]>
  • Loading branch information
imranh2 and cjlapao authored Oct 18, 2024
1 parent d8d6a1d commit 0f2c3d7
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .web-docs/components/builder/iso/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,17 @@ In HCL2:
Kickstart or other early initialization tools, which can benefit from labelled floppy disks.
By default, the floppy label will be 'packer'.

- `cd_files` ([]string) - A list of files to place onto a CD that is attached when the VM is
booted. This can include either files or directories; any directories
will be copied onto the CD recursively, preserving directory structure
hierarchy. Symlinks will have the link's target copied into the directory
tree on the CD where the symlink was. File globbing is allowed.

- `cd_content` (map[string]string) - Key/Values to add to the CD. The keys represent the paths, and the values
contents. It can be used alongside `cd_files`, which is useful to add large
files without loading them into memory. If any paths are specified by both,
the contents in `cd_content` will take precedence.

- `guest_os_type` (string) - The guest OS type being installed. By default
this is "other", but you can get _dramatic_ performance improvements by
setting this to the proper value. To view all available values for this run
Expand Down
11 changes: 11 additions & 0 deletions .web-docs/components/builder/pvm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ can also be supplied to override the typical auto-generated key:
Kickstart or other early initialization tools, which can benefit from labelled floppy disks.
By default, the floppy label will be 'packer'.

- `cd_files` ([]string) - A list of files to place onto a CD that is attached when the VM is
booted. This can include either files or directories; any directories
will be copied onto the CD recursively, preserving directory structure
hierarchy. Symlinks will have the link's target copied into the directory
tree on the CD where the symlink was. File globbing is allowed.

- `cd_content` (map[string]string) - Key/Values to add to the CD. The keys represent the paths, and the values
contents. It can be used alongside `cd_files`, which is useful to add large
files without loading them into memory. If any paths are specified by both,
the contents in `cd_content` will take precedence.

- `output_directory` (string) - This is the path to the directory where the
resulting virtual machine will be created. This may be relative or absolute.
If relative, the path is relative to the working directory when `packer`
Expand Down
2 changes: 2 additions & 0 deletions builder/parallels/common/driver_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ func (d *DriverMock) DeviceAddCDROM(name string, image string) (string, error) {
d.DeviceAddCDROMCalled = true
d.DeviceAddCDROMName = name
d.DeviceAddCDROMImage = image
d.DeviceAddCDROMResult = "cdrom0"
d.DeviceAddCDROMErr = nil
return d.DeviceAddCDROMResult, d.DeviceAddCDROMErr
}

Expand Down
75 changes: 75 additions & 0 deletions builder/parallels/common/step_attach_cd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package common

import (
"context"
"fmt"
"log"

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

// StepAttachCD is a step that attaches a cd to the virtual machine.
//
// Uses:
//
// driver Driver
// ui packersdk.Ui
// vmName string
//
// Produces:
type StepAttachCD struct {
cdPath string
cdromDevice string
}

// Run adds a virtual CD to the VM and attaches the image.
// If the image is not specified, then this step will be skipped.
func (s *StepAttachCD) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
// Determine if we even have a cd to attach
var cdPath string
if cdPathRaw, ok := state.GetOk("cd_path"); ok {
cdPath = cdPathRaw.(string)
} else {
log.Println("No cd found, not attaching.")
return multistep.ActionContinue
}

driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packersdk.Ui)
vmName := state.Get("vmName").(string)

ui.Say("Attaching cd...")
// Attaching the cd using the driver
cdrom, err := driver.DeviceAddCDROM(vmName, cdPath)
if err != nil {
err = fmt.Errorf("error attaching cd: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
// Track the device name so that we can can delete later
s.cdromDevice = cdrom

return multistep.ActionContinue
}

// Cleanup removes the virtual FDD device attached to the VM.
func (s *StepAttachCD) Cleanup(state multistep.StateBag) {
driver := state.Get("driver").(Driver)
vmName := state.Get("vmName").(string)

if s.cdPath == "" {
return
}

log.Println("Detaching cd disk...")
command := []string{
"set", vmName,
"--device-del", s.cdromDevice,
}
_ = driver.Prlctl(command...)
}
76 changes: 76 additions & 0 deletions builder/parallels/common/step_attach_cd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package common

import (
"context"
"io/ioutil"
"os"
"testing"

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

func TestStepAttachCD_impl(t *testing.T) {
var _ multistep.Step = new(StepAttachCD)
}

func TestStepAttachCD(t *testing.T) {
state := testState(t)
step := new(StepAttachCD)

// Create a temporary file for our CD
tf, err := ioutil.TempFile("", "packer")
if err != nil {
t.Fatalf("err: %s", err)
}
tf.Close()
defer os.Remove(tf.Name())

state.Put("cd_path", tf.Name())
state.Put("vmName", "foo")

driver := state.Get("driver").(*DriverMock)

// Test the run
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
t.Fatalf("bad action: %#v", action)
}
if _, ok := state.GetOk("error"); ok {
t.Fatal("should NOT have error")
}

if !driver.DeviceAddCDROMCalled {
t.Fatal("DeviceAddCDROM not called")
}
if driver.DeviceAddCDROMName != "foo" {
t.Fatal("DeviceAddCDROM name isn't what we specified (foo)")
}
if driver.DeviceAddCDROMResult != "cdrom0" {
t.Fatal("DeviceAddCDROM didn't return cdrom0")
}
if driver.DeviceAddCDROMErr != nil {
t.Fatal("DeviceAddCDROM returned an error")
}

}

func TestStepAttachCD_noCD(t *testing.T) {
state := testState(t)
step := new(StepAttachCD)

driver := state.Get("driver").(*DriverMock)

// Test the run
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
t.Fatalf("bad action: %#v", action)
}
if _, ok := state.GetOk("error"); ok {
t.Fatal("should NOT have error")
}

if driver.DeviceAddCDROMCalled {
t.Fatal("DeviceAddCDROM has been called")
}
}
6 changes: 6 additions & 0 deletions builder/parallels/iso/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Config struct {
commonsteps.HTTPConfig `mapstructure:",squash"`
commonsteps.ISOConfig `mapstructure:",squash"`
commonsteps.FloppyConfig `mapstructure:",squash"`
commonsteps.CDConfig `mapstructure:",squash"`
bootcommand.BootConfig `mapstructure:",squash"`
parallelscommon.OutputConfig `mapstructure:",squash"`
parallelscommon.HWConfig `mapstructure:",squash"`
Expand Down Expand Up @@ -214,6 +215,10 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
Directories: b.config.FloppyConfig.FloppyDirectories,
Label: b.config.FloppyConfig.FloppyLabel,
},
&commonsteps.StepCreateCD{
Files: b.config.CDConfig.CDFiles,
Content: b.config.CDConfig.CDContent,
},
commonsteps.HTTPServerFromHTTPConfig(&b.config.HTTPConfig),
new(stepCreateVM),
new(stepCreateDisk),
Expand All @@ -223,6 +228,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
ParallelsToolsMode: b.config.ParallelsToolsMode,
},
new(parallelscommon.StepAttachFloppy),
new(parallelscommon.StepAttachCD),
&parallelscommon.StepPrlctl{
Commands: b.config.Prlctl,
Ctx: b.config.ctx,
Expand Down
6 changes: 6 additions & 0 deletions builder/parallels/iso/builder.hcl2spec.go

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

5 changes: 5 additions & 0 deletions builder/parallels/pvm/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
Directories: b.config.FloppyConfig.FloppyDirectories,
Label: b.config.FloppyConfig.FloppyLabel,
},
&commonsteps.StepCreateCD{
Files: b.config.CDConfig.CDFiles,
Content: b.config.CDConfig.CDContent,
},
&parallelscommon.StepImport{
Name: b.config.VMName,
SourcePath: b.config.SourcePath,
Expand All @@ -77,6 +81,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
ParallelsToolsMode: b.config.ParallelsToolsMode,
},
new(parallelscommon.StepAttachFloppy),
new(parallelscommon.StepAttachCD),
&parallelscommon.StepPrlctl{
Commands: b.config.Prlctl,
Ctx: b.config.ctx,
Expand Down
1 change: 1 addition & 0 deletions builder/parallels/pvm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
type Config struct {
common.PackerConfig `mapstructure:",squash"`
commonsteps.FloppyConfig `mapstructure:",squash"`
commonsteps.CDConfig `mapstructure:",squash"`
parallelscommon.OutputConfig `mapstructure:",squash"`
parallelscommon.PrlctlConfig `mapstructure:",squash"`
parallelscommon.PrlctlPostConfig `mapstructure:",squash"`
Expand Down
6 changes: 6 additions & 0 deletions builder/parallels/pvm/config.hcl2spec.go

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

11 changes: 11 additions & 0 deletions docs/builders/iso.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ can also be supplied to override the typical auto-generated key:
Kickstart or other early initialization tools, which can benefit from labelled floppy disks.
By default, the floppy label will be 'packer'.

- `cd_files` ([]string) - A list of files to place onto a CD that is attached when the VM is
booted. This can include either files or directories; any directories
will be copied onto the CD recursively, preserving directory structure
hierarchy. Symlinks will have the link's target copied into the directory
tree on the CD where the symlink was. File globbing is allowed.

- `cd_content` (map[string]string) - Key/Values to add to the CD. The keys represent the paths, and the values
contents. It can be used alongside `cd_files`, which is useful to add large
files without loading them into memory. If any paths are specified by both,
the contents in `cd_content` will take precedence.

- `guest_os_type` (string) - The guest OS type being installed. By default
this is "other", but you can get _dramatic_ performance improvements by
setting this to the proper value. To view all available values for this run
Expand Down
11 changes: 11 additions & 0 deletions docs/builders/pvm.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ can also be supplied to override the typical auto-generated key:
Kickstart or other early initialization tools, which can benefit from labelled floppy disks.
By default, the floppy label will be 'packer'.

- `cd_files` ([]string) - A list of files to place onto a CD that is attached when the VM is
booted. This can include either files or directories; any directories
will be copied onto the CD recursively, preserving directory structure
hierarchy. Symlinks will have the link's target copied into the directory
tree on the CD where the symlink was. File globbing is allowed.

- `cd_content` (map[string]string) - Key/Values to add to the CD. The keys represent the paths, and the values
contents. It can be used alongside `cd_files`, which is useful to add large
files without loading them into memory. If any paths are specified by both,
the contents in `cd_content` will take precedence.

- `output_directory` (string) - This is the path to the directory where the
resulting virtual machine will be created. This may be relative or absolute.
If relative, the path is relative to the working directory when `packer`
Expand Down

0 comments on commit 0f2c3d7

Please sign in to comment.