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

govc: Add support for providing size while we create a VMFS datastore #3528

Closed
wants to merge 2 commits into from

Conversation

HaruChebrolu
Copy link
Contributor

@HaruChebrolu HaruChebrolu commented Aug 21, 2024

Description

govc: Add support for providing size while we create a VMFS datastore

Closes: #(issue-number)
#3519

Type of change

Please mark options that are relevant:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to
    not work as expected)
  • This change requires a documentation update
  • Build related change

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. If applicable, please also list any relevant
details for your test configuration.

  • Test Description 1
    Tried to create a VMFS datastore with some size in govc cli
    go run main.go datastore.create -type VMFS -name vmfs61 -version 6 -disk eui.b8fa2f975ecc490ea090a46404568540 -size 30 ocsqe-virt07.lab.eng.blr.redhat.com
  • Test Description 2
    Tried to create a VMFS datastore without size argument in govc cli, so that it takes the entire size of disk into consideration(default case)
    go run main.go datastore.create -type VMFS -name vmfs61 -version 6 -disk eui.b8fa2f975ecc490ea090a46404568540 ocsqe-virt07.lab.eng.blr.redhat.com

Checklist:

  • My code follows the CONTRIBUTION guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged

@vmwclabot
Copy link
Member

@HaruChebrolu, you must sign our contributor license agreement before your changes are merged. Click here to sign the agreement. If you are a VMware employee, read this for further instruction.

@HaruChebrolu HaruChebrolu changed the title govc: Add support for providing size while we create a datastore govc: Add support for providing size while we create a VMFS datastore Aug 21, 2024
@vmwclabot
Copy link
Member

@HaruChebrolu, we have received your signed contributor license agreement. The review is usually completed within a week, but may take longer under certain circumstances. Another comment will be added to the pull request to notify you when the merge can proceed.

Copy link
Member

@dougm dougm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @HaruChebrolu
A few general items, please

  • Run make doc to update USAGE.md
  • Code needs to be gofmt'd
  • Amend commit message to include Closes #3519 , this will create a link in generated release notes and auto-close the issue when we merge your change.

One more suggestion for the size flag, to use units.ByteSize. This allows value to be specified as (for example) -size 30M , -size 2T, etc. See patch below.

diff --git a/govc/datastore/create.go b/govc/datastore/create.go
index 0432e46b..fc5e11e8 100644
--- a/govc/datastore/create.go
+++ b/govc/datastore/create.go
@@ -28,13 +28,14 @@ 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)
+	sectorSize  = 512  // Sector size in bytes
+	startSector = 2048 // Start sector (typically fixed)
 )
 
 type create struct {
@@ -55,7 +56,7 @@ type create struct {
 	// Options for VMFS
 	DiskCanonicalName string
 	Version           *int32
-	Size              int64
+	Size              units.ByteSize
 
 	// Options for local
 	Path string
@@ -150,7 +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.Int64Var(&cmd.Size, "size", -1, "Size of new disk")
+	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)")
@@ -296,7 +297,7 @@ 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(cmd.Size)
+			endSector := CalculateSectors(int64(cmd.Size))
 			// set values for Sectors
 			spec.Partition.Partition[0].StartSector = startSector
 			spec.Partition.Partition[0].EndSector = endSector
@@ -314,13 +315,11 @@ func (cmd *create) CreateVmfsDatastore(ctx context.Context, hosts []*object.Host
 }
 
 // CalculateSectors calculates the start and end sectors based on the given size.
-func CalculateSectors(sizeGB int64) (endSector int64) {
-    sizeInBytes := sizeGB * 1024 * 1024 * 1024
+func CalculateSectors(sizeInBytes int64) (endSector int64) {
+	totalSectors := sizeInBytes / sectorSize
+	endSector = startSector + totalSectors - 1
 
-    totalSectors := sizeInBytes / sectorSize
-    endSector = startSector + totalSectors - 1
-
-    return endSector
+	return endSector
 }
 
 func (cmd *create) CreateLocalDatastore(ctx context.Context, hosts []*object.HostSystem) error {

@HaruChebrolu
Copy link
Contributor Author

Thank you @HaruChebrolu A few general items, please

  • Run make doc to update USAGE.md
  • Code needs to be gofmt'd
  • Amend commit message to include Closes #3519 , this will create a link in generated release notes and auto-close the issue when we merge your change.

One more suggestion for the size flag, to use units.ByteSize. This allows value to be specified as (for example) -size 30M , -size 2T, etc. See patch below.

diff --git a/govc/datastore/create.go b/govc/datastore/create.go
index 0432e46b..fc5e11e8 100644
--- a/govc/datastore/create.go
+++ b/govc/datastore/create.go
@@ -28,13 +28,14 @@ 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)
+	sectorSize  = 512  // Sector size in bytes
+	startSector = 2048 // Start sector (typically fixed)
 )
 
 type create struct {
@@ -55,7 +56,7 @@ type create struct {
 	// Options for VMFS
 	DiskCanonicalName string
 	Version           *int32
-	Size              int64
+	Size              units.ByteSize
 
 	// Options for local
 	Path string
@@ -150,7 +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.Int64Var(&cmd.Size, "size", -1, "Size of new disk")
+	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)")
@@ -296,7 +297,7 @@ 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(cmd.Size)
+			endSector := CalculateSectors(int64(cmd.Size))
 			// set values for Sectors
 			spec.Partition.Partition[0].StartSector = startSector
 			spec.Partition.Partition[0].EndSector = endSector
@@ -314,13 +315,11 @@ func (cmd *create) CreateVmfsDatastore(ctx context.Context, hosts []*object.Host
 }
 
 // CalculateSectors calculates the start and end sectors based on the given size.
-func CalculateSectors(sizeGB int64) (endSector int64) {
-    sizeInBytes := sizeGB * 1024 * 1024 * 1024
+func CalculateSectors(sizeInBytes int64) (endSector int64) {
+	totalSectors := sizeInBytes / sectorSize
+	endSector = startSector + totalSectors - 1
 
-    totalSectors := sizeInBytes / sectorSize
-    endSector = startSector + totalSectors - 1
-
-    return endSector
+	return endSector
 }
 
 func (cmd *create) CreateLocalDatastore(ctx context.Context, hosts []*object.HostSystem) error {

Done

Copy link
Member

@dougm dougm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating @HaruChebrolu
But still missing govc/USAGE.md from make doc (you can see the GH Action check failed)
See this doc on the commit message. Still want the govc: subject prefix as you had it before, Closes should be in the body:

govc: Add '-size' flag to datastore.create

Closes #3519

@HaruChebrolu HaruChebrolu closed this by deleting the head repository Aug 29, 2024
@vmwclabot
Copy link
Member

@HaruChebrolu, VMware has approved your signed contributor license agreement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants