Skip to content

Commit

Permalink
Merge pull request #3539 from dougm/disk-attach
Browse files Browse the repository at this point in the history
Add support for using storage profiles when creating and attaching VirtualDisks
  • Loading branch information
dougm authored Sep 6, 2024
2 parents 717871d + 93da4a2 commit 988a047
Show file tree
Hide file tree
Showing 18 changed files with 541 additions and 81 deletions.
49 changes: 44 additions & 5 deletions govc/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ but appear via `govc $cmd -h`:
- [device.serial.connect](#deviceserialconnect)
- [device.serial.disconnect](#deviceserialdisconnect)
- [device.usb.add](#deviceusbadd)
- [disk.attach](#diskattach)
- [disk.create](#diskcreate)
- [disk.detach](#diskdetach)
- [disk.ls](#diskls)
- [disk.register](#diskregister)
- [disk.rm](#diskrm)
Expand Down Expand Up @@ -2010,6 +2012,24 @@ Options:
-vm= Virtual machine [GOVC_VM]
```

## disk.attach

```
Usage: govc disk.attach [OPTIONS] ID
Attach disk ID on VM.
See also: govc vm.disk.attach
Examples:
govc disk.attach -vm $vm ID
govc disk.attach -vm $vm -ds $ds ID
Options:
-ds= Datastore [GOVC_DATASTORE]
-vm= Virtual machine [GOVC_VM]
```

## disk.create

```
Expand All @@ -2025,9 +2045,26 @@ Options:
-ds= Datastore [GOVC_DATASTORE]
-keep=<nil> Keep disk after VM is deleted
-pool= Resource pool [GOVC_RESOURCE_POOL]
-profile=[] Storage profile name or ID
-size=10.0GB Size of new disk
```

## disk.detach

```
Usage: govc disk.detach [OPTIONS] ID
Detach disk ID from VM.
See also: govc device.remove
Examples:
govc disk.detach -vm $vm ID
Options:
-vm= Virtual machine [GOVC_VM]
```

## disk.ls

```
Expand Down Expand Up @@ -3604,7 +3641,7 @@ Options:
-m=false Preserve MAC-addresses on network adapters
-ovf=false Clone as OVF (default is VM Template)
-pool= Resource pool [GOVC_RESOURCE_POOL]
-profile= Storage profile
-profile=[] Storage profile name or ID
-vm= Virtual machine [GOVC_VM]
```

Expand Down Expand Up @@ -3669,7 +3706,7 @@ Options:
-host= Host system [GOVC_HOST]
-options= Options spec file path for VM deployment
-pool= Resource pool [GOVC_RESOURCE_POOL]
-profile= Storage profile
-profile=[] Storage profile name or ID
```

## library.evict
Expand Down Expand Up @@ -4436,7 +4473,7 @@ Examples:
Options:
-cluster= Cluster [GOVC_CLUSTER]
-library=[] Content library IDs to associate with the vSphere Namespace.
-storage=[] Storage profile IDs to associate with the vSphere Namespace.
-storage=[] Storage profile name or ID
-vmclass=[] Virtual machine class IDs to associate with the vSphere Namespace.
```

Expand Down Expand Up @@ -4611,7 +4648,7 @@ Examples:
Options:
-library=[] Content library IDs to associate with the vSphere Namespace.
-storage=[] Storage profile IDs to associate with the vSphere Namespace.
-storage=[] Storage profile name or ID
-vmclass=[] Virtual machine class IDs to associate with the vSphere Namespace.
```

Expand Down Expand Up @@ -6451,7 +6488,7 @@ Options:
-net.protocol= Network device protocol. Applicable to vmxnet3vrdma. Default to 'rocev2'
-on=true Power on VM
-pool= Resource pool [GOVC_RESOURCE_POOL]
-profile= Storage profile name or ID
-profile=[] Storage profile name or ID
-version= ESXi hardware version [2|3|4|5.0|5.1|5.5|6.0|6.5|6.7|6.7.2|7.0|7.0.1|7.0.2|8.0|8.0.1|8.0.2]
```

Expand Down Expand Up @@ -6695,6 +6732,7 @@ Options:
-link=true Link specified disk
-mode= Disk mode (persistent|nonpersistent|undoable|independent_persistent|independent_nonpersistent|append)
-persist=true Persist attached disk
-profile=[] Storage profile name or ID
-sharing= Sharing (sharingNone|sharingMultiWriter)
-vm= Virtual machine [GOVC_VM]
```
Expand Down Expand Up @@ -6743,6 +6781,7 @@ Options:
-eager=false Eagerly scrub new disk
-mode=persistent Disk mode (persistent|nonpersistent|undoable|independent_persistent|independent_nonpersistent|append)
-name= Name for new disk
-profile=[] Storage profile name or ID
-sharing= Sharing (sharingNone|sharingMultiWriter)
-size=10.0GB Size of new disk
-thick=false Thick provision new disk
Expand Down
97 changes: 97 additions & 0 deletions govc/disk/attach.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
Copyright (c) 2024-2024 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package disk

import (
"context"
"flag"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/vim25/mo"
)

type attach struct {
*flags.VirtualMachineFlag
*flags.DatastoreFlag
}

func init() {
cli.Register("disk.attach", &attach{})
}

func (cmd *attach) Register(ctx context.Context, f *flag.FlagSet) {
cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
cmd.VirtualMachineFlag.Register(ctx, f)
cmd.DatastoreFlag, ctx = flags.NewDatastoreFlag(ctx)
cmd.DatastoreFlag.Register(ctx, f)
}

func (cmd *attach) Process(ctx context.Context) error {
if err := cmd.VirtualMachineFlag.Process(ctx); err != nil {
return err
}
return cmd.DatastoreFlag.Process(ctx)
}

func (cmd *attach) Usage() string {
return "ID"
}

func (cmd *attach) Description() string {
return `Attach disk ID on VM.
See also: govc vm.disk.attach
Examples:
govc disk.attach -vm $vm ID
govc disk.attach -vm $vm -ds $ds ID`
}

func (cmd *attach) Run(ctx context.Context, f *flag.FlagSet) error {
if f.NArg() != 1 {
return flag.ErrHelp
}

ds, err := cmd.DatastoreIfSpecified()
if err != nil {
return err
}

vm, err := cmd.VirtualMachine()
if err != nil {
return err
}

if ds == nil {
var props mo.VirtualMachine
err = vm.Properties(ctx, vm.Reference(), []string{"datastore"}, &props)
if err != nil {
return err
}
if len(props.Datastore) != 1 {
ds, err = cmd.Datastore() // likely results in MultipleFoundError
if err != nil {
return err
}
}
}

id := f.Arg(0)

return vm.AttachDisk(ctx, id, ds, 0, nil)
}
17 changes: 15 additions & 2 deletions govc/disk/create.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Copyright (c) 2018 VMware, Inc. All Rights Reserved.
Copyright (c) 2018-2024 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -34,6 +34,7 @@ type disk struct {
*flags.DatastoreFlag
*flags.ResourcePoolFlag
*flags.StoragePodFlag
*flags.StorageProfileFlag

size units.ByteSize
keep *bool
Expand All @@ -50,6 +51,9 @@ func (cmd *disk) Register(ctx context.Context, f *flag.FlagSet) {
cmd.StoragePodFlag, ctx = flags.NewStoragePodFlag(ctx)
cmd.StoragePodFlag.Register(ctx, f)

cmd.StorageProfileFlag, ctx = flags.NewStorageProfileFlag(ctx)
cmd.StorageProfileFlag.Register(ctx, f)

cmd.ResourcePoolFlag, ctx = flags.NewResourcePoolFlag(ctx)
cmd.ResourcePoolFlag.Register(ctx, f)

Expand All @@ -65,6 +69,9 @@ func (cmd *disk) Process(ctx context.Context) error {
if err := cmd.StoragePodFlag.Process(ctx); err != nil {
return err
}
if err := cmd.StorageProfileFlag.Process(ctx); err != nil {
return err
}
return cmd.ResourcePoolFlag.Process(ctx)
}

Expand Down Expand Up @@ -108,12 +115,18 @@ func (cmd *disk) Run(ctx context.Context, f *flag.FlagSet) error {
}
}

profile, err := cmd.StorageProfileSpec(ctx)
if err != nil {
return err
}

m := vslm.NewObjectManager(c)

spec := types.VslmCreateSpec{
Name: name,
CapacityInMB: int64(cmd.size) / units.MB,
KeepAfterDeleteVm: cmd.keep,
Profile: profile,
BackingSpec: &types.VslmCreateSpecDiskFileBackingSpec{
VslmCreateSpecBackingSpec: types.VslmCreateSpecBackingSpec{
Datastore: ds.Reference(),
Expand Down
66 changes: 66 additions & 0 deletions govc/disk/detach.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright (c) 2024-2024 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package disk

import (
"context"
"flag"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
)

type detach struct {
*flags.VirtualMachineFlag
}

func init() {
cli.Register("disk.detach", &detach{})
}

func (cmd *detach) Register(ctx context.Context, f *flag.FlagSet) {
cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
cmd.VirtualMachineFlag.Register(ctx, f)
}

func (cmd *detach) Usage() string {
return "ID"
}

func (cmd *detach) Description() string {
return `Detach disk ID from VM.
See also: govc device.remove
Examples:
govc disk.detach -vm $vm ID`
}

func (cmd *detach) Run(ctx context.Context, f *flag.FlagSet) error {
if f.NArg() != 1 {
return flag.ErrHelp
}

vm, err := cmd.VirtualMachine()
if err != nil {
return err
}

id := f.Arg(0)

return vm.DetachDisk(ctx, id)
}
Loading

0 comments on commit 988a047

Please sign in to comment.