Skip to content

Commit 0f2c3d7

Browse files
imranh2cjlapao
andauthored
Extra CD support, supply files via cd_files or cd_content (#119)
* 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 <cjlapao@gmail.com>
1 parent d8d6a1d commit 0f2c3d7

File tree

12 files changed

+221
-0
lines changed

12 files changed

+221
-0
lines changed

.web-docs/components/builder/iso/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,17 @@ In HCL2:
241241
Kickstart or other early initialization tools, which can benefit from labelled floppy disks.
242242
By default, the floppy label will be 'packer'.
243243

244+
- `cd_files` ([]string) - A list of files to place onto a CD that is attached when the VM is
245+
booted. This can include either files or directories; any directories
246+
will be copied onto the CD recursively, preserving directory structure
247+
hierarchy. Symlinks will have the link's target copied into the directory
248+
tree on the CD where the symlink was. File globbing is allowed.
249+
250+
- `cd_content` (map[string]string) - Key/Values to add to the CD. The keys represent the paths, and the values
251+
contents. It can be used alongside `cd_files`, which is useful to add large
252+
files without loading them into memory. If any paths are specified by both,
253+
the contents in `cd_content` will take precedence.
254+
244255
- `guest_os_type` (string) - The guest OS type being installed. By default
245256
this is "other", but you can get _dramatic_ performance improvements by
246257
setting this to the proper value. To view all available values for this run

.web-docs/components/builder/pvm/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ can also be supplied to override the typical auto-generated key:
9393
Kickstart or other early initialization tools, which can benefit from labelled floppy disks.
9494
By default, the floppy label will be 'packer'.
9595

96+
- `cd_files` ([]string) - A list of files to place onto a CD that is attached when the VM is
97+
booted. This can include either files or directories; any directories
98+
will be copied onto the CD recursively, preserving directory structure
99+
hierarchy. Symlinks will have the link's target copied into the directory
100+
tree on the CD where the symlink was. File globbing is allowed.
101+
102+
- `cd_content` (map[string]string) - Key/Values to add to the CD. The keys represent the paths, and the values
103+
contents. It can be used alongside `cd_files`, which is useful to add large
104+
files without loading them into memory. If any paths are specified by both,
105+
the contents in `cd_content` will take precedence.
106+
96107
- `output_directory` (string) - This is the path to the directory where the
97108
resulting virtual machine will be created. This may be relative or absolute.
98109
If relative, the path is relative to the working directory when `packer`

builder/parallels/common/driver_mock.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ func (d *DriverMock) DeviceAddCDROM(name string, image string) (string, error) {
7676
d.DeviceAddCDROMCalled = true
7777
d.DeviceAddCDROMName = name
7878
d.DeviceAddCDROMImage = image
79+
d.DeviceAddCDROMResult = "cdrom0"
80+
d.DeviceAddCDROMErr = nil
7981
return d.DeviceAddCDROMResult, d.DeviceAddCDROMErr
8082
}
8183

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package common
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"log"
10+
11+
"github.com/hashicorp/packer-plugin-sdk/multistep"
12+
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
13+
)
14+
15+
// StepAttachCD is a step that attaches a cd to the virtual machine.
16+
//
17+
// Uses:
18+
//
19+
// driver Driver
20+
// ui packersdk.Ui
21+
// vmName string
22+
//
23+
// Produces:
24+
type StepAttachCD struct {
25+
cdPath string
26+
cdromDevice string
27+
}
28+
29+
// Run adds a virtual CD to the VM and attaches the image.
30+
// If the image is not specified, then this step will be skipped.
31+
func (s *StepAttachCD) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
32+
// Determine if we even have a cd to attach
33+
var cdPath string
34+
if cdPathRaw, ok := state.GetOk("cd_path"); ok {
35+
cdPath = cdPathRaw.(string)
36+
} else {
37+
log.Println("No cd found, not attaching.")
38+
return multistep.ActionContinue
39+
}
40+
41+
driver := state.Get("driver").(Driver)
42+
ui := state.Get("ui").(packersdk.Ui)
43+
vmName := state.Get("vmName").(string)
44+
45+
ui.Say("Attaching cd...")
46+
// Attaching the cd using the driver
47+
cdrom, err := driver.DeviceAddCDROM(vmName, cdPath)
48+
if err != nil {
49+
err = fmt.Errorf("error attaching cd: %s", err)
50+
state.Put("error", err)
51+
ui.Error(err.Error())
52+
return multistep.ActionHalt
53+
}
54+
// Track the device name so that we can can delete later
55+
s.cdromDevice = cdrom
56+
57+
return multistep.ActionContinue
58+
}
59+
60+
// Cleanup removes the virtual FDD device attached to the VM.
61+
func (s *StepAttachCD) Cleanup(state multistep.StateBag) {
62+
driver := state.Get("driver").(Driver)
63+
vmName := state.Get("vmName").(string)
64+
65+
if s.cdPath == "" {
66+
return
67+
}
68+
69+
log.Println("Detaching cd disk...")
70+
command := []string{
71+
"set", vmName,
72+
"--device-del", s.cdromDevice,
73+
}
74+
_ = driver.Prlctl(command...)
75+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package common
5+
6+
import (
7+
"context"
8+
"io/ioutil"
9+
"os"
10+
"testing"
11+
12+
"github.com/hashicorp/packer-plugin-sdk/multistep"
13+
)
14+
15+
func TestStepAttachCD_impl(t *testing.T) {
16+
var _ multistep.Step = new(StepAttachCD)
17+
}
18+
19+
func TestStepAttachCD(t *testing.T) {
20+
state := testState(t)
21+
step := new(StepAttachCD)
22+
23+
// Create a temporary file for our CD
24+
tf, err := ioutil.TempFile("", "packer")
25+
if err != nil {
26+
t.Fatalf("err: %s", err)
27+
}
28+
tf.Close()
29+
defer os.Remove(tf.Name())
30+
31+
state.Put("cd_path", tf.Name())
32+
state.Put("vmName", "foo")
33+
34+
driver := state.Get("driver").(*DriverMock)
35+
36+
// Test the run
37+
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
38+
t.Fatalf("bad action: %#v", action)
39+
}
40+
if _, ok := state.GetOk("error"); ok {
41+
t.Fatal("should NOT have error")
42+
}
43+
44+
if !driver.DeviceAddCDROMCalled {
45+
t.Fatal("DeviceAddCDROM not called")
46+
}
47+
if driver.DeviceAddCDROMName != "foo" {
48+
t.Fatal("DeviceAddCDROM name isn't what we specified (foo)")
49+
}
50+
if driver.DeviceAddCDROMResult != "cdrom0" {
51+
t.Fatal("DeviceAddCDROM didn't return cdrom0")
52+
}
53+
if driver.DeviceAddCDROMErr != nil {
54+
t.Fatal("DeviceAddCDROM returned an error")
55+
}
56+
57+
}
58+
59+
func TestStepAttachCD_noCD(t *testing.T) {
60+
state := testState(t)
61+
step := new(StepAttachCD)
62+
63+
driver := state.Get("driver").(*DriverMock)
64+
65+
// Test the run
66+
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
67+
t.Fatalf("bad action: %#v", action)
68+
}
69+
if _, ok := state.GetOk("error"); ok {
70+
t.Fatal("should NOT have error")
71+
}
72+
73+
if driver.DeviceAddCDROMCalled {
74+
t.Fatal("DeviceAddCDROM has been called")
75+
}
76+
}

builder/parallels/iso/builder.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Config struct {
3636
commonsteps.HTTPConfig `mapstructure:",squash"`
3737
commonsteps.ISOConfig `mapstructure:",squash"`
3838
commonsteps.FloppyConfig `mapstructure:",squash"`
39+
commonsteps.CDConfig `mapstructure:",squash"`
3940
bootcommand.BootConfig `mapstructure:",squash"`
4041
parallelscommon.OutputConfig `mapstructure:",squash"`
4142
parallelscommon.HWConfig `mapstructure:",squash"`
@@ -214,6 +215,10 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
214215
Directories: b.config.FloppyConfig.FloppyDirectories,
215216
Label: b.config.FloppyConfig.FloppyLabel,
216217
},
218+
&commonsteps.StepCreateCD{
219+
Files: b.config.CDConfig.CDFiles,
220+
Content: b.config.CDConfig.CDContent,
221+
},
217222
commonsteps.HTTPServerFromHTTPConfig(&b.config.HTTPConfig),
218223
new(stepCreateVM),
219224
new(stepCreateDisk),
@@ -223,6 +228,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
223228
ParallelsToolsMode: b.config.ParallelsToolsMode,
224229
},
225230
new(parallelscommon.StepAttachFloppy),
231+
new(parallelscommon.StepAttachCD),
226232
&parallelscommon.StepPrlctl{
227233
Commands: b.config.Prlctl,
228234
Ctx: b.config.ctx,

builder/parallels/iso/builder.hcl2spec.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builder/parallels/pvm/builder.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
6767
Directories: b.config.FloppyConfig.FloppyDirectories,
6868
Label: b.config.FloppyConfig.FloppyLabel,
6969
},
70+
&commonsteps.StepCreateCD{
71+
Files: b.config.CDConfig.CDFiles,
72+
Content: b.config.CDConfig.CDContent,
73+
},
7074
&parallelscommon.StepImport{
7175
Name: b.config.VMName,
7276
SourcePath: b.config.SourcePath,
@@ -77,6 +81,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
7781
ParallelsToolsMode: b.config.ParallelsToolsMode,
7882
},
7983
new(parallelscommon.StepAttachFloppy),
84+
new(parallelscommon.StepAttachCD),
8085
&parallelscommon.StepPrlctl{
8186
Commands: b.config.Prlctl,
8287
Ctx: b.config.ctx,

builder/parallels/pvm/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
type Config struct {
2525
common.PackerConfig `mapstructure:",squash"`
2626
commonsteps.FloppyConfig `mapstructure:",squash"`
27+
commonsteps.CDConfig `mapstructure:",squash"`
2728
parallelscommon.OutputConfig `mapstructure:",squash"`
2829
parallelscommon.PrlctlConfig `mapstructure:",squash"`
2930
parallelscommon.PrlctlPostConfig `mapstructure:",squash"`

builder/parallels/pvm/config.hcl2spec.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/builders/iso.mdx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ can also be supplied to override the typical auto-generated key:
129129
Kickstart or other early initialization tools, which can benefit from labelled floppy disks.
130130
By default, the floppy label will be 'packer'.
131131

132+
- `cd_files` ([]string) - A list of files to place onto a CD that is attached when the VM is
133+
booted. This can include either files or directories; any directories
134+
will be copied onto the CD recursively, preserving directory structure
135+
hierarchy. Symlinks will have the link's target copied into the directory
136+
tree on the CD where the symlink was. File globbing is allowed.
137+
138+
- `cd_content` (map[string]string) - Key/Values to add to the CD. The keys represent the paths, and the values
139+
contents. It can be used alongside `cd_files`, which is useful to add large
140+
files without loading them into memory. If any paths are specified by both,
141+
the contents in `cd_content` will take precedence.
142+
132143
- `guest_os_type` (string) - The guest OS type being installed. By default
133144
this is "other", but you can get _dramatic_ performance improvements by
134145
setting this to the proper value. To view all available values for this run

docs/builders/pvm.mdx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ can also be supplied to override the typical auto-generated key:
103103
Kickstart or other early initialization tools, which can benefit from labelled floppy disks.
104104
By default, the floppy label will be 'packer'.
105105

106+
- `cd_files` ([]string) - A list of files to place onto a CD that is attached when the VM is
107+
booted. This can include either files or directories; any directories
108+
will be copied onto the CD recursively, preserving directory structure
109+
hierarchy. Symlinks will have the link's target copied into the directory
110+
tree on the CD where the symlink was. File globbing is allowed.
111+
112+
- `cd_content` (map[string]string) - Key/Values to add to the CD. The keys represent the paths, and the values
113+
contents. It can be used alongside `cd_files`, which is useful to add large
114+
files without loading them into memory. If any paths are specified by both,
115+
the contents in `cd_content` will take precedence.
116+
106117
- `output_directory` (string) - This is the path to the directory where the
107118
resulting virtual machine will be created. This may be relative or absolute.
108119
If relative, the path is relative to the working directory when `packer`

0 commit comments

Comments
 (0)