Skip to content

Commit

Permalink
govc: Add '-size' flag to datastore.create
Browse files Browse the repository at this point in the history
Closes: #3519
Signed-off-by: Chebrolu <hchebrol@redhat.com>
  • Loading branch information
HaruChebrolu authored and dougm committed Aug 29, 2024
1 parent 9090fda commit 2a1cf41
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion govc/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,7 @@ Create datastore on HOST.
Examples:
govc datastore.create -type nfs -name nfsDatastore -remote-host 10.143.2.232 -remote-path /share cluster1
govc datastore.create -type vmfs -name vmfsDatastore -disk=mpx.vmhba0:C0:T0:L0 cluster1
govc datastore.create -type vmfs -name vmfsDatastore -disk=mpx.vmhba0:C0:T0:L0 -size 20G cluster1
govc datastore.create -type local -name localDatastore -path /var/datastore host1
Options:
Expand All @@ -1254,6 +1254,7 @@ Options:
-path= Local directory path for the datastore (local only)
-remote-host= Remote hostname of the NAS datastore
-remote-path= Remote path of the NFS mount point
-size=0B Size of new disk
-type= Datastore type (NFS|NFS41|CIFS|VMFS|local)
-username= Username to use when connecting (CIFS only)
-version=<nil> VMFS major version
Expand Down
24 changes: 23 additions & 1 deletion govc/datastore/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ import (
"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/units"
"github.com/vmware/govmomi/vim25/soap"
"github.com/vmware/govmomi/vim25/types"
)

const (
sectorSize = 512 // Sector size in bytes
startSector = 2048 // Start sector (typically fixed)
)

type create struct {
*flags.HostSystemFlag

Expand All @@ -50,6 +56,7 @@ type create struct {
// Options for VMFS
DiskCanonicalName string
Version *int32
Size units.ByteSize

// Options for local
Path string
Expand Down Expand Up @@ -144,6 +151,7 @@ func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
// Options for VMFS
f.StringVar(&cmd.DiskCanonicalName, "disk", "", "Canonical name of disk (VMFS only)")
f.Var(flags.NewOptionalInt32(&cmd.Version), "version", "VMFS major version")
f.Var(&cmd.Size, "size", "Size of new disk")

// Options for Local
f.StringVar(&cmd.Path, "path", "", "Local directory path for the datastore (local only)")
Expand All @@ -165,7 +173,7 @@ func (cmd *create) Description() string {
Examples:
govc datastore.create -type nfs -name nfsDatastore -remote-host 10.143.2.232 -remote-path /share cluster1
govc datastore.create -type vmfs -name vmfsDatastore -disk=mpx.vmhba0:C0:T0:L0 cluster1
govc datastore.create -type vmfs -name vmfsDatastore -disk=mpx.vmhba0:C0:T0:L0 -size 20G cluster1
govc datastore.create -type local -name localDatastore -path /var/datastore host1`
}

Expand Down Expand Up @@ -289,6 +297,12 @@ func (cmd *create) CreateVmfsDatastore(ctx context.Context, hosts []*object.Host

spec := *option.Spec.(*types.VmfsDatastoreCreateSpec)
spec.Vmfs.VolumeName = cmd.Name
if cmd.Size > 0 {
endSector := CalculateSectors(int64(cmd.Size))
// set values for Sectors
spec.Partition.Partition[0].StartSector = startSector
spec.Partition.Partition[0].EndSector = endSector
}
if cmd.Version != nil {
spec.Vmfs.MajorVersion = *cmd.Version
}
Expand All @@ -301,6 +315,14 @@ func (cmd *create) CreateVmfsDatastore(ctx context.Context, hosts []*object.Host
return nil
}

// CalculateSectors calculates the start and end sectors based on the given size.
func CalculateSectors(sizeInBytes int64) (endSector int64) {
totalSectors := sizeInBytes / sectorSize
endSector = startSector + totalSectors - 1

return endSector
}

func (cmd *create) CreateLocalDatastore(ctx context.Context, hosts []*object.HostSystem) error {
for _, host := range hosts {
ds, err := host.ConfigManager().DatastoreSystem(ctx)
Expand Down

0 comments on commit 2a1cf41

Please sign in to comment.