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

how can i use customize configuration when cloneing vm? #616

Closed
freebsdly opened this issue Oct 24, 2016 · 29 comments
Closed

how can i use customize configuration when cloneing vm? #616

freebsdly opened this issue Oct 24, 2016 · 29 comments

Comments

@freebsdly
Copy link
Contributor

hi,
how can i use customize configuration file when cloneing vm? i want to set the hostname and ip use customize configuration file.
use "-customization=" ?

@kasper5
Copy link

kasper5 commented Nov 3, 2016

I'd also like to know how I cloud set the ip address when cloning

@warroyo
Copy link

warroyo commented Nov 15, 2016

it looks like you need to create a customization spec in vcenter and use the name of that created spec for reference. however this does not seem very dynamic, how would you chnage these settings at clone time?

@mgherman
Copy link

When you specify -customization=xxx govc appears to retrieve the customisation from vcenter and injects it into the cloneSpec that is passed to the cmd.VirtualMachine.Clone

I patched the vm.clone command to allow for providing a fixed IP address, netmask and gateway on the command line which modifies the customisation object after it has been retrieved from vcenter, before cmd.VirtualMachine.Clone is called.

Obviously the image you are cloning has to support using the customisation spec, and to be perfectly honest, the code is pretty ugly, has no tests, and is probably not very go-like, but it does what I needed it to do.

Please let me know if you are interested in it.

@warroyo
Copy link

warroyo commented Dec 7, 2016

@mgherman could you please provide this code?

@acbox
Copy link

acbox commented Feb 13, 2017

+1 @mgherman please share :)

@mgherman
Copy link

So, this is the diff. You need to have the named customisation already provisioned within vmware, and then provide any / all of the overrides: custip, custgw and custmask which should then override the values in the customisation.

Im sure there is a better way of doing this, but this was sufficient to get it working as a PoC in our environment.

The cmdline flags i was using was something along the lines of:
-customization="nonprod" -custip "10.10.10.32"

+++ clone.go	2017-02-14 22:51:54.622445394 -0800
@@ -28,6 +28,8 @@
 	"github.com/vmware/govmomi/vim25"
 	"github.com/vmware/govmomi/vim25/mo"
 	"github.com/vmware/govmomi/vim25/types"
+
+	"reflect"
 )
 
 type clone struct {
@@ -48,6 +50,9 @@
 	force         bool
 	template      bool
 	customization string
+	custip        string
+	custmask      string
+	custgw        string
 	waitForIP     bool
 
 	Client         *vim25.Client
@@ -98,6 +103,9 @@
 	f.BoolVar(&cmd.force, "force", false, "Create VM if vmx already exists")
 	f.BoolVar(&cmd.template, "template", false, "Create a Template")
 	f.StringVar(&cmd.customization, "customization", "", "Customization Specification Name")
+	f.StringVar(&cmd.custip, "custip", "", "Customization IPAddress")
+	f.StringVar(&cmd.custgw, "custgw", "", "Customization Gateway")
+	f.StringVar(&cmd.custmask, "custmask", "", "Customization Netmask")
 	f.BoolVar(&cmd.waitForIP, "waitip", false, "Wait for VM to acquire IP address")
 }
 
@@ -400,7 +408,23 @@
 		customSpec := customSpecItem.Spec
 		// set the customization
 		cloneSpec.Customization = &customSpec
+
+		xx := customSpec.NicSettingMap[0].Adapter.Ip
+
+		if len(cmd.custip) > 0 {
+			aa := reflect.ValueOf(xx).Elem()
+			bb := aa.FieldByName("IpAddress")
+			bb.SetString(cmd.custip)
+		}
+
+		if len(cmd.custmask) > 0 {
+			customSpec.NicSettingMap[0].Adapter.SubnetMask = cmd.custmask
+		}
+		if len(cmd.custgw) > 0 {
+			customSpec.NicSettingMap[0].Adapter.Gateway[0] = cmd.custgw
+		}
 	}
+		
 
 	// clone virtualmachine
 	return cmd.VirtualMachine.Clone(ctx, cmd.Folder, cmd.name, *cloneSpec)

@murarisumit
Copy link

@mgherman how did you make the build after making changes,
I used ./build.sh in govmomi/govc directory, after running it gives me error : version mismatch: git=9.0-dirty vs govc=0.14.0

How can I resolve this ?

@dougm can you please help me in building after making changes ??

@dougm
Copy link
Member

dougm commented Mar 22, 2017

build.sh is for creating releases. Just use 'go build ./govc'

@avnish30jn
Copy link

avnish30jn commented Sep 11, 2017

Can we create customSpec using govmomi ?

I tried using the method, CreateCustomizationSpec in below file, but that gives error.

func (cs CustomizationSpecManager) CreateCustomizationSpec(ctx context.Context, item types.CustomizationSpecItem) error {

Error:
Required property identity is missing from data object of type CustomizationSpec

The property identity has some dynamic data. How can we set the dynamic data ?

@warroyo
Copy link

warroyo commented Oct 10, 2017

any chance that @mgherman's code could be added to the cli?

@dougm
Copy link
Member

dougm commented Oct 10, 2017

Yes, can you open a PR @mgherman ?

I think it'd be fine to name those flags with the cust prefix as they wouldn't conflict with any existing flags.

Can also avoid reflection:

		if len(customSpec.NicSettingMap) > 0 {
			nic := customSpec.NicSettingMap[0].Adapter

			if ip, ok := nic.Ip.(*types.CustomizationFixedIp); ok {
				ip.IpAddress = "..."
			}

			nic.SubnetMask = "..."
			nic.Gateway = []string{"..."}
		}

@dougm
Copy link
Member

dougm commented Oct 16, 2017

Maybe we should consider a new 'vm.customize' command rather than putting more into the clone command? Looks like it's possible to do so after cloning with the CustomizeVm_Task method.

govc vm.clone -on=false template-vm new-vm
govc vm.customize -vm new-vm -ip x.x.x.x -dns-server x.x.x.x -dns-suffix example.com customization-name
govc vm.power -on new-vm

@murarisumit
Copy link

It it resolved now ??

@freebsdly
Copy link
Contributor Author

@mgherman did it

@Boran
Copy link

Boran commented Jan 3, 2018

Why was the issue closed? There is no PR yet from gherman's code, this (useful) feature has not been integrated into the govc cli?

@dkirrane
Copy link

+1 I'd also like to customise -ip, netmask, gateway, dns, disk, -net etc..

@xayangjing
Copy link

+1 I'd also like to customise -ip, netmask, gateway, dns, disk, -net etc..

@sfozz
Copy link

sfozz commented Jun 28, 2018

+1 it is a feature that is lacking

@vincentmli
Copy link

+1 for this feature

@mgherman tried your patch to clone.go and build the govc, running govc got error

$./govc vm.clone -vm testvm -customization="test-spec"  -custip="1.1.1.1" new-testvm
./govc: flag provided but not defined: -custip

Am I missing something

@neduma
Copy link

neduma commented Oct 24, 2018

+1 for this feature.

## Something like 
 govc vm.network.add -vm test-vm -net ISERV_188 -net.adapter vmxnet3 -ip x.x.x.x -dns-server x.x.x.x -dns-suffix example.com 

@dumez-k
Copy link

dumez-k commented Nov 29, 2018

+1 for this feature.

@pytimer
Copy link

pytimer commented Nov 30, 2018

+1 for this feature.

I try @mgherman patch to clone.go and build govc success. When i run ./govc vm.clone -customization=govc-cust, vm created but ip address not changed. I don't know where is wrong. Maybe my vsphere customization specification.

Anyone have the same problem?

@angystardust
Copy link
Contributor

@pytimer i haven't tested the patch but if you're trying to deploy a Linux guest please be remember that...

Linux based OSes requires Perl package to be installed for OS customizations.

(For reference: https://docs.ansible.com/ansible/latest/modules/vmware_guest_module.html#vmware-guest-module)

@pytimer
Copy link

pytimer commented Dec 3, 2018

@angystardust Thanks for you reply.

I follow your said and install Perl package, customization spec is OK. Thanks. :-)

@dumez-k
Copy link

dumez-k commented Dec 3, 2018

@pytimer you were able to successfully user the patch? The ip was set?

After applying the patch I'm seeing a panic: reflect: call of reflect.Value.SetString on zero Value error message when attempting to run.

After some debugging it appears that this is because customSpec.NicSettingMap[0].Adapter.Ip is returning a nil object. I figured that this was due to the patch no longer being compatible with the current version of govc (seeing as how the patch is a year old).

If you were able to successfully use the patch though this gives me hope

@pytimer
Copy link

pytimer commented Dec 4, 2018

@dumez-k I update the latest version of govc clone code, you can see this pr: Add customSpec network configuration for govc vm.clone in my fork repository. it works ok for me.

@dnicolaev
Copy link

Hello, also a question about cloning VMs.
Is there a way to not show all the progress output lines during clone ?

Sometimes there are just about 10 lines - that is fine.
But sometimes there are tons of such lines.

[07-12-18 15:04:36] Cloning /Static/vm/HEAD-current to NightlyBuild_Fix-2018...(0%)
[07-12-18 15:04:36] Cloning /Static/vm/HEAD-current to NightlyBuild_Fix-2018...(0%)
[07-12-18 15:04:36] Cloning /Static/vm/HEAD-current to NightlyBuild_Fix-2018...(0%)
[07-12-18 15:04:36] Cloning /Static/vm/HEAD-current to NightlyBuild_Fix-2018...(0%)
[07-12-18 15:04:36] Cloning /Static/vm/HEAD-current to NightlyBuild_Fix-2018...(0%)
[07-12-18 15:04:36] Cloning /Static/vm/HEAD-current to NightlyBuild_Fix-2018...(5%)
[07-12-18 15:04:36] Cloning /Static/vm/HEAD-current to NightlyBuild_Fix-2018...(29%)
[07-12-18 15:04:36] Cloning /Static/vm/HEAD-current to NightlyBuild_Fix-2018...(29%)
[07-12-18 15:04:37] Cloning /Static/vm/HEAD-current to NightlyBuild_Fix-2018...(32%)
[07-12-18 15:04:37] Cloning /Static/vm/HEAD-current to NightlyBuild_Fix-2018...(40%)
[07-12-18 15:04:37] Cloning /Static/vm/HEAD-current to NightlyBuild_Fix-2018...(40%)
[07-12-18 15:04:37] Cloning /Static/vm/HEAD-current to NightlyBuild_Fix-2018...(94%)
[07-12-18 15:04:37] Cloning /Static/vm/HEAD-current to NightlyBuild_Fix-2018...(94%)

@bpottier
Copy link

Has there been any update on this? It would be a huge help to have this feature.

@dougm
Copy link
Member

dougm commented Feb 21, 2019

There is recent discussion here: #984 (comment)

Still no development yet, but hoping to make it happen for the next release in the next few weeks.

dougm pushed a commit to dougm/govmomi that referenced this issue Oct 8, 2019
@dougm dougm closed this as completed in 3185f7b Oct 15, 2019
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

No branches or pull requests