Adding linked clones (#17)
* Simple linked cloning, just crashing if no snapshots
This commit is contained in:
parent
ac139471c8
commit
66705e4a26
|
@ -27,6 +27,7 @@ type Config struct {
|
||||||
Host string `mapstructure:"host"`
|
Host string `mapstructure:"host"`
|
||||||
ResourcePool string `mapstructure:"resource_pool"`
|
ResourcePool string `mapstructure:"resource_pool"`
|
||||||
Datastore string `mapstructure:"datastore"`
|
Datastore string `mapstructure:"datastore"`
|
||||||
|
LinkedClone bool `mapstructure:"linked_clone"`
|
||||||
|
|
||||||
// Hardware
|
// Hardware
|
||||||
Cpus string `mapstructure:"cpus"`
|
Cpus string `mapstructure:"cpus"`
|
||||||
|
|
|
@ -10,6 +10,8 @@ import (
|
||||||
"github.com/vmware/govmomi/find"
|
"github.com/vmware/govmomi/find"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"github.com/vmware/govmomi/vim25/mo"
|
||||||
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CloneParameters struct {
|
type CloneParameters struct {
|
||||||
|
@ -20,6 +22,7 @@ type CloneParameters struct {
|
||||||
vmSrc *object.VirtualMachine
|
vmSrc *object.VirtualMachine
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
vmName string
|
vmName string
|
||||||
|
linkedClone bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type StepCloneVM struct{
|
type StepCloneVM struct{
|
||||||
|
@ -86,6 +89,7 @@ func (s *StepCloneVM) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
vmSrc: vmSrc,
|
vmSrc: vmSrc,
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
vmName: s.config.VMName,
|
vmName: s.config.VMName,
|
||||||
|
linkedClone: s.config.LinkedClone,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
|
@ -139,11 +143,27 @@ func cloneVM(params *CloneParameters) (vm *object.VirtualMachine, err error) {
|
||||||
datastoreRef := params.datastore.Reference()
|
datastoreRef := params.datastore.Reference()
|
||||||
relocateSpec.Datastore = &datastoreRef
|
relocateSpec.Datastore = &datastoreRef
|
||||||
}
|
}
|
||||||
|
if params.linkedClone == true {
|
||||||
|
relocateSpec.DiskMoveType = "createNewChildDiskBacking"
|
||||||
|
}
|
||||||
|
|
||||||
cloneSpec := types.VirtualMachineCloneSpec{
|
cloneSpec := types.VirtualMachineCloneSpec{
|
||||||
Location: relocateSpec,
|
Location: relocateSpec,
|
||||||
PowerOn: false,
|
PowerOn: false,
|
||||||
}
|
}
|
||||||
|
if params.linkedClone == true {
|
||||||
|
var vmImage mo.VirtualMachine
|
||||||
|
err = params.vmSrc.Properties(params.ctx, params.vmSrc.Reference(), []string{"snapshot"}, &vmImage)
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("Error reading base VM properties: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if vmImage.Snapshot == nil {
|
||||||
|
err = errors.New("`linked_clone=true`, but image VM has no snapshots")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cloneSpec.Snapshot = vmImage.Snapshot.CurrentSnapshot
|
||||||
|
}
|
||||||
|
|
||||||
// Cloning itself
|
// Cloning itself
|
||||||
task, err := params.vmSrc.Clone(params.ctx, params.folder, params.vmName, cloneSpec)
|
task, err := params.vmSrc.Clone(params.ctx, params.folder, params.vmName, cloneSpec)
|
||||||
|
|
Loading…
Reference in New Issue