Skip to content

Commit 59582c4

Browse files
authored
Merge pull request #2691 from prydin/prydin-exportsnap
Added vm::ExportSnapshot and snapshot flag to export.ovf
2 parents e1df929 + cd577f4 commit 59582c4

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

govc/USAGE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,6 +1843,7 @@ Options:
18431843
-name= Specifies target name (defaults to source name)
18441844
-prefix=true Prepend target name to image filenames if missing
18451845
-sha=0 Generate manifest using SHA 1, 256, 512 or 0 to skip
1846+
-snapshot= Specifies a snapshot to export from (supports running VMs)
18461847
-vm= Virtual machine [GOVC_VM]
18471848
```
18481849

govc/export/ovf.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import (
3030
"path/filepath"
3131
"strings"
3232

33+
"github.com/vmware/govmomi/object"
34+
3335
"github.com/vmware/govmomi/govc/cli"
3436
"github.com/vmware/govmomi/govc/flags"
3537
"github.com/vmware/govmomi/nfc"
@@ -41,12 +43,13 @@ import (
4143
type ovfx struct {
4244
*flags.VirtualMachineFlag
4345

44-
dest string
45-
name string
46-
force bool
47-
images bool
48-
prefix bool
49-
sha int
46+
dest string
47+
name string
48+
snapshot string
49+
force bool
50+
images bool
51+
prefix bool
52+
sha int
5053

5154
mf bytes.Buffer
5255
}
@@ -66,6 +69,7 @@ func (cmd *ovfx) Register(ctx context.Context, f *flag.FlagSet) {
6669
cmd.VirtualMachineFlag.Register(ctx, f)
6770

6871
f.StringVar(&cmd.name, "name", "", "Specifies target name (defaults to source name)")
72+
f.StringVar(&cmd.snapshot, "snapshot", "", "Specifies a snapshot to export from (supports running VMs)")
6973
f.BoolVar(&cmd.force, "f", false, "Overwrite existing")
7074
f.BoolVar(&cmd.images, "i", false, "Include image files (*.{iso,img})")
7175
f.BoolVar(&cmd.prefix, "prefix", true, "Prepend target name to image filenames if missing")
@@ -129,7 +133,7 @@ func (cmd *ovfx) Run(ctx context.Context, f *flag.FlagSet) error {
129133
return err
130134
}
131135

132-
lease, err := vm.Export(ctx)
136+
lease, err := cmd.requestExport(ctx, vm)
133137
if err != nil {
134138
return err
135139
}
@@ -213,6 +217,17 @@ func (cmd *ovfx) Run(ctx context.Context, f *flag.FlagSet) error {
213217
return file.Close()
214218
}
215219

220+
func (cmd *ovfx) requestExport(ctx context.Context, vm *object.VirtualMachine) (*nfc.Lease, error) {
221+
if cmd.snapshot != "" {
222+
snapRef, err := vm.FindSnapshot(ctx, cmd.snapshot)
223+
if err != nil {
224+
return nil, err
225+
}
226+
return vm.ExportSnapshot(ctx, snapRef)
227+
}
228+
return vm.Export(ctx)
229+
}
230+
216231
func (cmd *ovfx) include(item *nfc.FileItem) bool {
217232
if cmd.images {
218233
return true

object/virtual_machine.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,3 +1063,16 @@ func (v VirtualMachine) QueryChangedDiskAreas(ctx context.Context, baseSnapshot,
10631063

10641064
return res.Returnval, nil
10651065
}
1066+
1067+
// ExportSnapshot exports all VMDK-files up to (but not including) a specified snapshot. This
1068+
// is useful when exporting a running VM.
1069+
func (v *VirtualMachine) ExportSnapshot(ctx context.Context, snapshot *types.ManagedObjectReference) (*nfc.Lease, error) {
1070+
req := types.ExportSnapshot{
1071+
This: *snapshot,
1072+
}
1073+
resp, err := methods.ExportSnapshot(ctx, v.Client(), &req)
1074+
if err != nil {
1075+
return nil, err
1076+
}
1077+
return nfc.NewLease(v.c, resp.Returnval), nil
1078+
}

0 commit comments

Comments
 (0)