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

(vsphere-iso) add ability to define multiple disks #8787

Merged
merged 2 commits into from
Mar 4, 2020
Merged
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
42 changes: 26 additions & 16 deletions builder/vsphere/driver/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ type NIC struct {
}

type CreateConfig struct {
DiskThinProvisioned bool
DiskEagerlyScrub bool
DiskControllerType string // example: "scsi", "pvscsi"
DiskSize int64
DiskControllerType string // example: "scsi", "pvscsi"

Annotation string
Name string
Expand All @@ -69,6 +66,13 @@ type CreateConfig struct {
USBController bool
Version uint // example: 10
Firmware string // efi or bios
Storage []Disk
jhawk28 marked this conversation as resolved.
Show resolved Hide resolved
}

type Disk struct {
DiskSize int64
DiskEagerlyScrub bool
DiskThinProvisioned bool
}

func (d *Driver) NewVM(ref *types.ManagedObjectReference) *VirtualMachine {
Expand Down Expand Up @@ -488,6 +492,10 @@ func (vm *VirtualMachine) GetDir() (string, error) {
}

func addDisk(_ *Driver, devices object.VirtualDeviceList, config *CreateConfig) (object.VirtualDeviceList, error) {
if len(config.Storage) == 0 {
return nil, errors.New("no storage devices have been defined")
}

device, err := devices.CreateSCSIController(config.DiskControllerType)
if err != nil {
return nil, err
Expand All @@ -498,20 +506,22 @@ func addDisk(_ *Driver, devices object.VirtualDeviceList, config *CreateConfig)
return nil, err
}

disk := &types.VirtualDisk{
VirtualDevice: types.VirtualDevice{
Key: devices.NewKey(),
Backing: &types.VirtualDiskFlatVer2BackingInfo{
DiskMode: string(types.VirtualDiskModePersistent),
ThinProvisioned: types.NewBool(config.DiskThinProvisioned),
EagerlyScrub: types.NewBool(config.DiskEagerlyScrub),
for _, dc := range config.Storage {
disk := &types.VirtualDisk{
VirtualDevice: types.VirtualDevice{
Key: devices.NewKey(),
Backing: &types.VirtualDiskFlatVer2BackingInfo{
DiskMode: string(types.VirtualDiskModePersistent),
ThinProvisioned: types.NewBool(dc.DiskThinProvisioned),
EagerlyScrub: types.NewBool(dc.DiskEagerlyScrub),
},
},
},
CapacityInKB: config.DiskSize * 1024,
}
CapacityInKB: dc.DiskSize * 1024,
}

devices.AssignController(disk, controller)
devices = append(devices, disk)
devices.AssignController(disk, controller)
devices = append(devices, disk)
}

return devices, nil
}
Expand Down
2 changes: 2 additions & 0 deletions builder/vsphere/iso/config.hcl2spec.go

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

60 changes: 43 additions & 17 deletions builder/vsphere/iso/step_create.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:generate struct-markdown
//go:generate mapstructure-to-hcl2 -type NIC,CreateConfig
//go:generate mapstructure-to-hcl2 -type NIC,CreateConfig,DiskConfig

package iso

Expand All @@ -24,6 +24,15 @@ type NIC struct {
Passthrough *bool `mapstructure:"passthrough"`
}

type DiskConfig struct {
// Set the size of the disk
DiskSize int64 `mapstructure:"disk_size" required:"true"`
// Enable VMDK thin provisioning for VM. Defaults to `false`.
DiskThinProvisioned bool `mapstructure:"disk_thin_provisioned"`
// Enable VMDK eager scrubbing for VM. Defaults to `false`.
DiskEagerlyScrub bool `mapstructure:"disk_eagerly_scrub"`
}

type CreateConfig struct {
// Set VM hardware version. Defaults to the most current VM hardware
// version supported by vCenter. See
Expand All @@ -44,6 +53,8 @@ type CreateConfig struct {
DiskThinProvisioned bool `mapstructure:"disk_thin_provisioned"`
// Enable VMDK eager scrubbing for VM. Defaults to `false`.
DiskEagerlyScrub bool `mapstructure:"disk_eagerly_scrub"`
// A collection of one or more disks to be provisioned along with the VM.
Storage []DiskConfig `mapstructure:"storage"`
// Set network VM will be connected to.
Network string `mapstructure:"network"`
// Set VM network card type. Example `vmxnet3`.
Expand Down Expand Up @@ -115,23 +126,38 @@ func (s *StepCreateVM) Run(_ context.Context, state multistep.StateBag) multiste
})
}

// add disk as the first drive for backwards compatibility if the type is defined
var disks []driver.Disk
if s.Config.DiskSize != 0 {
disks = append(disks, driver.Disk{
DiskSize: s.Config.DiskSize,
DiskEagerlyScrub: s.Config.DiskEagerlyScrub,
DiskThinProvisioned: s.Config.DiskThinProvisioned,
})
}
for _, disk := range s.Config.Storage {
disks = append(disks, driver.Disk{
DiskSize: disk.DiskSize,
DiskEagerlyScrub: disk.DiskEagerlyScrub,
DiskThinProvisioned: disk.DiskThinProvisioned,
})
}

vm, err = d.CreateVM(&driver.CreateConfig{
DiskThinProvisioned: s.Config.DiskThinProvisioned,
DiskEagerlyScrub: s.Config.DiskEagerlyScrub,
DiskControllerType: s.Config.DiskControllerType,
DiskSize: s.Config.DiskSize,
Name: s.Location.VMName,
Folder: s.Location.Folder,
Cluster: s.Location.Cluster,
Host: s.Location.Host,
ResourcePool: s.Location.ResourcePool,
Datastore: s.Location.Datastore,
GuestOS: s.Config.GuestOSType,
NICs: networkCards,
USBController: s.Config.USBController,
Version: s.Config.Version,
Firmware: s.Config.Firmware,
Annotation: s.Config.Notes,
DiskControllerType: s.Config.DiskControllerType,
Storage: disks,
Annotation: s.Config.Notes,
Name: s.Location.VMName,
Folder: s.Location.Folder,
Cluster: s.Location.Cluster,
Host: s.Location.Host,
ResourcePool: s.Location.ResourcePool,
Datastore: s.Location.Datastore,
GuestOS: s.Config.GuestOSType,
NICs: networkCards,
USBController: s.Config.USBController,
Version: s.Config.Version,
Firmware: s.Config.Firmware,
})
if err != nil {
state.Put("error", fmt.Errorf("error creating vm: %v", err))
Expand Down
55 changes: 42 additions & 13 deletions builder/vsphere/iso/step_create.hcl2spec.go

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

4 changes: 4 additions & 0 deletions website/source/docs/builders/vsphere-iso.html.md.erb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ necessary for this build to succeed and can be found further down the page.
### Network Adapter Configuration
<%= partial "partials/builder/vsphere/iso/NIC-required" %>

### Storage Configuration
<%= partial "partials/builder/vsphere/iso/DiskConfig-required" %>
<%= partial "partials/builder/vsphere/iso/DiskConfig-not-required" %>

### Floppy Configuration
<%= partial "partials/builder/vsphere/iso/FloppyConfig-not-required" %>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

- `disk_eagerly_scrub` (bool) - Enable VMDK eager scrubbing for VM. Defaults to `false`.

- `storage` ([]DiskConfig) - A collection of one or more disks to be provisioned along with the VM.

- `network` (string) - Set network VM will be connected to.

- `network_card` (string) - Set VM network card type. Example `vmxnet3`.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!-- Code generated from the comments of the DiskConfig struct in builder/vsphere/iso/step_create.go; DO NOT EDIT MANUALLY -->

- `disk_thin_provisioned` (bool) - Enable VMDK thin provisioning for VM. Defaults to `false`.

- `disk_eagerly_scrub` (bool) - Enable VMDK eager scrubbing for VM. Defaults to `false`.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!-- Code generated from the comments of the DiskConfig struct in builder/vsphere/iso/step_create.go; DO NOT EDIT MANUALLY -->

- `disk_size` (int64) - Set the size of the disk