1 | /* $Id: VBoxManageSnapshot.cpp 58410 2015-10-26 10:05:42Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxManage - The 'snapshot' command.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #include <VBox/com/com.h>
|
---|
23 | #include <VBox/com/string.h>
|
---|
24 | #include <VBox/com/array.h>
|
---|
25 | #include <VBox/com/ErrorInfo.h>
|
---|
26 | #include <VBox/com/errorprint.h>
|
---|
27 |
|
---|
28 | #include <VBox/com/VirtualBox.h>
|
---|
29 |
|
---|
30 | #include <iprt/stream.h>
|
---|
31 | #include <iprt/getopt.h>
|
---|
32 | #include <iprt/time.h>
|
---|
33 |
|
---|
34 | #include "VBoxManage.h"
|
---|
35 | using namespace com;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Helper function used with "VBoxManage snapshot ... dump". Gets called to find the
|
---|
39 | * snapshot in the machine's snapshot tree that uses a particular diff image child of
|
---|
40 | * a medium.
|
---|
41 | * Horribly inefficient since we keep re-querying the snapshots tree for each image,
|
---|
42 | * but this is for quick debugging only.
|
---|
43 | * @param pMedium
|
---|
44 | * @param pThisSnapshot
|
---|
45 | * @param pCurrentSnapshot
|
---|
46 | * @param uMediumLevel
|
---|
47 | * @param uSnapshotLevel
|
---|
48 | * @return
|
---|
49 | */
|
---|
50 | bool FindAndPrintSnapshotUsingMedium(ComPtr<IMedium> &pMedium,
|
---|
51 | ComPtr<ISnapshot> &pThisSnapshot,
|
---|
52 | ComPtr<ISnapshot> &pCurrentSnapshot,
|
---|
53 | uint32_t uMediumLevel,
|
---|
54 | uint32_t uSnapshotLevel)
|
---|
55 | {
|
---|
56 | HRESULT rc;
|
---|
57 |
|
---|
58 | do
|
---|
59 | {
|
---|
60 | // get snapshot machine so we can figure out which diff image this created
|
---|
61 | ComPtr<IMachine> pSnapshotMachine;
|
---|
62 | CHECK_ERROR_BREAK(pThisSnapshot, COMGETTER(Machine)(pSnapshotMachine.asOutParam()));
|
---|
63 |
|
---|
64 | // get media attachments
|
---|
65 | SafeIfaceArray<IMediumAttachment> aAttachments;
|
---|
66 | CHECK_ERROR_BREAK(pSnapshotMachine, COMGETTER(MediumAttachments)(ComSafeArrayAsOutParam(aAttachments)));
|
---|
67 |
|
---|
68 | for (uint32_t i = 0;
|
---|
69 | i < aAttachments.size();
|
---|
70 | ++i)
|
---|
71 | {
|
---|
72 | ComPtr<IMediumAttachment> pAttach(aAttachments[i]);
|
---|
73 | DeviceType_T type;
|
---|
74 | CHECK_ERROR_BREAK(pAttach, COMGETTER(Type)(&type));
|
---|
75 | if (type == DeviceType_HardDisk)
|
---|
76 | {
|
---|
77 | ComPtr<IMedium> pMediumInSnapshot;
|
---|
78 | CHECK_ERROR_BREAK(pAttach, COMGETTER(Medium)(pMediumInSnapshot.asOutParam()));
|
---|
79 |
|
---|
80 | if (pMediumInSnapshot == pMedium)
|
---|
81 | {
|
---|
82 | // get snapshot name
|
---|
83 | Bstr bstrSnapshotName;
|
---|
84 | CHECK_ERROR_BREAK(pThisSnapshot, COMGETTER(Name)(bstrSnapshotName.asOutParam()));
|
---|
85 |
|
---|
86 | RTPrintf("%*s \"%ls\"%s\n",
|
---|
87 | 50 + uSnapshotLevel * 2, "", // indent
|
---|
88 | bstrSnapshotName.raw(),
|
---|
89 | (pThisSnapshot == pCurrentSnapshot) ? " (CURSNAP)" : "");
|
---|
90 | return true; // found
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | // not found: then recurse into child snapshots
|
---|
96 | SafeIfaceArray<ISnapshot> aSnapshots;
|
---|
97 | CHECK_ERROR_BREAK(pThisSnapshot, COMGETTER(Children)(ComSafeArrayAsOutParam(aSnapshots)));
|
---|
98 |
|
---|
99 | for (uint32_t i = 0;
|
---|
100 | i < aSnapshots.size();
|
---|
101 | ++i)
|
---|
102 | {
|
---|
103 | ComPtr<ISnapshot> pChild(aSnapshots[i]);
|
---|
104 | if (FindAndPrintSnapshotUsingMedium(pMedium,
|
---|
105 | pChild,
|
---|
106 | pCurrentSnapshot,
|
---|
107 | uMediumLevel,
|
---|
108 | uSnapshotLevel + 1))
|
---|
109 | // found:
|
---|
110 | break;
|
---|
111 | }
|
---|
112 | } while (0);
|
---|
113 |
|
---|
114 | return false;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Helper function used with "VBoxManage snapshot ... dump". Called from DumpSnapshot()
|
---|
119 | * for each hard disk attachment found in a virtual machine. This then writes out the
|
---|
120 | * root (base) medium for that hard disk attachment and recurses into the children
|
---|
121 | * tree of that medium, correlating it with the snapshots of the machine.
|
---|
122 | * @param pCurrentStateMedium constant, the medium listed in the current machine data (latest diff image).
|
---|
123 | * @param pMedium variant, initially the base medium, then a child of the base medium when recursing.
|
---|
124 | * @param pRootSnapshot constant, the root snapshot of the machine, if any; this then looks into the child snapshots.
|
---|
125 | * @param pCurrentSnapshot constant, the machine's current snapshot (so we can mark it in the output).
|
---|
126 | * @param uLevel variant, the recursion level for output indentation.
|
---|
127 | */
|
---|
128 | void DumpMediumWithChildren(ComPtr<IMedium> &pCurrentStateMedium,
|
---|
129 | ComPtr<IMedium> &pMedium,
|
---|
130 | ComPtr<ISnapshot> &pRootSnapshot,
|
---|
131 | ComPtr<ISnapshot> &pCurrentSnapshot,
|
---|
132 | uint32_t uLevel)
|
---|
133 | {
|
---|
134 | HRESULT rc;
|
---|
135 | do
|
---|
136 | {
|
---|
137 | // print this medium
|
---|
138 | Bstr bstrMediumName;
|
---|
139 | CHECK_ERROR_BREAK(pMedium, COMGETTER(Name)(bstrMediumName.asOutParam()));
|
---|
140 | RTPrintf("%*s \"%ls\"%s\n",
|
---|
141 | uLevel * 2, "", // indent
|
---|
142 | bstrMediumName.raw(),
|
---|
143 | (pCurrentStateMedium == pMedium) ? " (CURSTATE)" : "");
|
---|
144 |
|
---|
145 | // find and print the snapshot that uses this particular medium (diff image)
|
---|
146 | FindAndPrintSnapshotUsingMedium(pMedium, pRootSnapshot, pCurrentSnapshot, uLevel, 0);
|
---|
147 |
|
---|
148 | // recurse into children
|
---|
149 | SafeIfaceArray<IMedium> aChildren;
|
---|
150 | CHECK_ERROR_BREAK(pMedium, COMGETTER(Children)(ComSafeArrayAsOutParam(aChildren)));
|
---|
151 | for (uint32_t i = 0;
|
---|
152 | i < aChildren.size();
|
---|
153 | ++i)
|
---|
154 | {
|
---|
155 | ComPtr<IMedium> pChild(aChildren[i]);
|
---|
156 | DumpMediumWithChildren(pCurrentStateMedium, pChild, pRootSnapshot, pCurrentSnapshot, uLevel + 1);
|
---|
157 | }
|
---|
158 | } while (0);
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Handles the 'snapshot myvm list' sub-command.
|
---|
164 | * @returns Exit code.
|
---|
165 | * @param pArgs The handler argument package.
|
---|
166 | * @param pMachine Reference to the VM (locked) we're operating on.
|
---|
167 | */
|
---|
168 | static RTEXITCODE handleSnapshotList(HandlerArg *pArgs, ComPtr<IMachine> &pMachine)
|
---|
169 | {
|
---|
170 | static const RTGETOPTDEF g_aOptions[] =
|
---|
171 | {
|
---|
172 | { "--details", 'D', RTGETOPT_REQ_NOTHING },
|
---|
173 | { "--machinereadable", 'M', RTGETOPT_REQ_NOTHING },
|
---|
174 | };
|
---|
175 |
|
---|
176 | VMINFO_DETAILS enmDetails = VMINFO_STANDARD;
|
---|
177 |
|
---|
178 | int c;
|
---|
179 | RTGETOPTUNION ValueUnion;
|
---|
180 | RTGETOPTSTATE GetState;
|
---|
181 | RTGetOptInit(&GetState, pArgs->argc, pArgs->argv, g_aOptions, RT_ELEMENTS(g_aOptions), 2 /*iArg*/, 0 /*fFlags*/);
|
---|
182 | while ((c = RTGetOpt(&GetState, &ValueUnion)))
|
---|
183 | {
|
---|
184 | switch (c)
|
---|
185 | {
|
---|
186 | case 'D': enmDetails = VMINFO_FULL; break;
|
---|
187 | case 'M': enmDetails = VMINFO_MACHINEREADABLE; break;
|
---|
188 | default: return errorGetOpt(USAGE_SNAPSHOT, c, &ValueUnion);
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | ComPtr<ISnapshot> pSnapshot;
|
---|
193 | HRESULT hrc = pMachine->FindSnapshot(Bstr().raw(), pSnapshot.asOutParam());
|
---|
194 | if (FAILED(hrc))
|
---|
195 | {
|
---|
196 | RTPrintf("This machine does not have any snapshots\n");
|
---|
197 | return RTEXITCODE_FAILURE;
|
---|
198 | }
|
---|
199 | if (pSnapshot)
|
---|
200 | {
|
---|
201 | ComPtr<ISnapshot> pCurrentSnapshot;
|
---|
202 | CHECK_ERROR2I_RET(pMachine, COMGETTER(CurrentSnapshot)(pCurrentSnapshot.asOutParam()), RTEXITCODE_FAILURE);
|
---|
203 | hrc = showSnapshots(pSnapshot, pCurrentSnapshot, enmDetails);
|
---|
204 | if (FAILED(hrc))
|
---|
205 | return RTEXITCODE_FAILURE;
|
---|
206 | }
|
---|
207 | return RTEXITCODE_SUCCESS;
|
---|
208 | }
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * Implementation for "VBoxManage snapshot ... dump". This goes thru the machine's
|
---|
212 | * medium attachments and calls DumpMediumWithChildren() for each hard disk medium found,
|
---|
213 | * which then dumps the parent/child tree of that medium together with the corresponding
|
---|
214 | * snapshots.
|
---|
215 | * @param pMachine Machine to dump snapshots for.
|
---|
216 | */
|
---|
217 | void DumpSnapshot(ComPtr<IMachine> &pMachine)
|
---|
218 | {
|
---|
219 | HRESULT rc;
|
---|
220 |
|
---|
221 | do
|
---|
222 | {
|
---|
223 | // get root snapshot
|
---|
224 | ComPtr<ISnapshot> pSnapshot;
|
---|
225 | CHECK_ERROR_BREAK(pMachine, FindSnapshot(Bstr("").raw(), pSnapshot.asOutParam()));
|
---|
226 |
|
---|
227 | // get current snapshot
|
---|
228 | ComPtr<ISnapshot> pCurrentSnapshot;
|
---|
229 | CHECK_ERROR_BREAK(pMachine, COMGETTER(CurrentSnapshot)(pCurrentSnapshot.asOutParam()));
|
---|
230 |
|
---|
231 | // get media attachments
|
---|
232 | SafeIfaceArray<IMediumAttachment> aAttachments;
|
---|
233 | CHECK_ERROR_BREAK(pMachine, COMGETTER(MediumAttachments)(ComSafeArrayAsOutParam(aAttachments)));
|
---|
234 | for (uint32_t i = 0;
|
---|
235 | i < aAttachments.size();
|
---|
236 | ++i)
|
---|
237 | {
|
---|
238 | ComPtr<IMediumAttachment> pAttach(aAttachments[i]);
|
---|
239 | DeviceType_T type;
|
---|
240 | CHECK_ERROR_BREAK(pAttach, COMGETTER(Type)(&type));
|
---|
241 | if (type == DeviceType_HardDisk)
|
---|
242 | {
|
---|
243 | ComPtr<IMedium> pCurrentStateMedium;
|
---|
244 | CHECK_ERROR_BREAK(pAttach, COMGETTER(Medium)(pCurrentStateMedium.asOutParam()));
|
---|
245 |
|
---|
246 | ComPtr<IMedium> pBaseMedium;
|
---|
247 | CHECK_ERROR_BREAK(pCurrentStateMedium, COMGETTER(Base)(pBaseMedium.asOutParam()));
|
---|
248 |
|
---|
249 | Bstr bstrBaseMediumName;
|
---|
250 | CHECK_ERROR_BREAK(pBaseMedium, COMGETTER(Name)(bstrBaseMediumName.asOutParam()));
|
---|
251 |
|
---|
252 | RTPrintf("[%RI32] Images and snapshots for medium \"%ls\"\n", i, bstrBaseMediumName.raw());
|
---|
253 |
|
---|
254 | DumpMediumWithChildren(pCurrentStateMedium,
|
---|
255 | pBaseMedium,
|
---|
256 | pSnapshot,
|
---|
257 | pCurrentSnapshot,
|
---|
258 | 0);
|
---|
259 | }
|
---|
260 | }
|
---|
261 | } while (0);
|
---|
262 | }
|
---|
263 |
|
---|
264 | typedef enum SnapshotUniqueFlags
|
---|
265 | {
|
---|
266 | SnapshotUniqueFlags_Null = 0,
|
---|
267 | SnapshotUniqueFlags_Number = RT_BIT(1),
|
---|
268 | SnapshotUniqueFlags_Timestamp = RT_BIT(2),
|
---|
269 | SnapshotUniqueFlags_Space = RT_BIT(16),
|
---|
270 | SnapshotUniqueFlags_Force = RT_BIT(30)
|
---|
271 | } SnapshotUniqueFlags;
|
---|
272 |
|
---|
273 | static int parseSnapshotUniqueFlags(const char *psz, SnapshotUniqueFlags *pUnique)
|
---|
274 | {
|
---|
275 | int rc = VINF_SUCCESS;
|
---|
276 | unsigned uUnique = 0;
|
---|
277 | while (psz && *psz && RT_SUCCESS(rc))
|
---|
278 | {
|
---|
279 | size_t len;
|
---|
280 | const char *pszComma = strchr(psz, ',');
|
---|
281 | if (pszComma)
|
---|
282 | len = pszComma - psz;
|
---|
283 | else
|
---|
284 | len = strlen(psz);
|
---|
285 | if (len > 0)
|
---|
286 | {
|
---|
287 | if (!RTStrNICmp(psz, "number", len))
|
---|
288 | uUnique |= SnapshotUniqueFlags_Number;
|
---|
289 | else if (!RTStrNICmp(psz, "timestamp", len))
|
---|
290 | uUnique |= SnapshotUniqueFlags_Timestamp;
|
---|
291 | else if (!RTStrNICmp(psz, "space", len))
|
---|
292 | uUnique |= SnapshotUniqueFlags_Space;
|
---|
293 | else if (!RTStrNICmp(psz, "force", len))
|
---|
294 | uUnique |= SnapshotUniqueFlags_Force;
|
---|
295 | else
|
---|
296 | rc = VERR_PARSE_ERROR;
|
---|
297 | }
|
---|
298 | if (pszComma)
|
---|
299 | psz += len + 1;
|
---|
300 | else
|
---|
301 | psz += len;
|
---|
302 | }
|
---|
303 |
|
---|
304 | if (RT_SUCCESS(rc))
|
---|
305 | *pUnique = (SnapshotUniqueFlags)uUnique;
|
---|
306 | return rc;
|
---|
307 | }
|
---|
308 |
|
---|
309 | /**
|
---|
310 | * Implementation for all VBoxManage snapshot ... subcommands.
|
---|
311 | * @param a
|
---|
312 | * @return
|
---|
313 | */
|
---|
314 | RTEXITCODE handleSnapshot(HandlerArg *a)
|
---|
315 | {
|
---|
316 | HRESULT rc;
|
---|
317 |
|
---|
318 | /* we need at least a VM and a command */
|
---|
319 | if (a->argc < 2)
|
---|
320 | return errorSyntax(USAGE_SNAPSHOT, "Not enough parameters");
|
---|
321 |
|
---|
322 | /* the first argument must be the VM */
|
---|
323 | Bstr bstrMachine(a->argv[0]);
|
---|
324 | ComPtr<IMachine> pMachine;
|
---|
325 | CHECK_ERROR(a->virtualBox, FindMachine(bstrMachine.raw(),
|
---|
326 | pMachine.asOutParam()));
|
---|
327 | if (!pMachine)
|
---|
328 | return RTEXITCODE_FAILURE;
|
---|
329 |
|
---|
330 | /* we have to open a session for this task (new or shared) */
|
---|
331 | CHECK_ERROR_RET(pMachine, LockMachine(a->session, LockType_Shared), RTEXITCODE_FAILURE);
|
---|
332 | do
|
---|
333 | {
|
---|
334 | /* replace the (read-only) IMachine object by a writable one */
|
---|
335 | ComPtr<IMachine> sessionMachine;
|
---|
336 | CHECK_ERROR_BREAK(a->session, COMGETTER(Machine)(sessionMachine.asOutParam()));
|
---|
337 |
|
---|
338 | /* switch based on the command */
|
---|
339 | bool fDelete = false,
|
---|
340 | fRestore = false,
|
---|
341 | fRestoreCurrent = false;
|
---|
342 |
|
---|
343 | if (!strcmp(a->argv[1], "take"))
|
---|
344 | {
|
---|
345 | /* there must be a name */
|
---|
346 | if (a->argc < 3)
|
---|
347 | {
|
---|
348 | errorSyntax(USAGE_SNAPSHOT, "Missing snapshot name");
|
---|
349 | rc = E_FAIL;
|
---|
350 | break;
|
---|
351 | }
|
---|
352 | Bstr name(a->argv[2]);
|
---|
353 |
|
---|
354 | /* parse the optional arguments */
|
---|
355 | Bstr desc;
|
---|
356 | bool fPause = true; /* default is NO live snapshot */
|
---|
357 | SnapshotUniqueFlags enmUnique = SnapshotUniqueFlags_Null;
|
---|
358 | static const RTGETOPTDEF s_aTakeOptions[] =
|
---|
359 | {
|
---|
360 | { "--description", 'd', RTGETOPT_REQ_STRING },
|
---|
361 | { "-description", 'd', RTGETOPT_REQ_STRING },
|
---|
362 | { "-desc", 'd', RTGETOPT_REQ_STRING },
|
---|
363 | { "--pause", 'p', RTGETOPT_REQ_NOTHING },
|
---|
364 | { "--live", 'l', RTGETOPT_REQ_NOTHING },
|
---|
365 | { "--uniquename", 'u', RTGETOPT_REQ_STRING }
|
---|
366 | };
|
---|
367 | RTGETOPTSTATE GetOptState;
|
---|
368 | RTGetOptInit(&GetOptState, a->argc, a->argv, s_aTakeOptions, RT_ELEMENTS(s_aTakeOptions),
|
---|
369 | 3, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
|
---|
370 | int ch;
|
---|
371 | RTGETOPTUNION Value;
|
---|
372 | int vrc;
|
---|
373 | while ( SUCCEEDED(rc)
|
---|
374 | && (ch = RTGetOpt(&GetOptState, &Value)))
|
---|
375 | {
|
---|
376 | switch (ch)
|
---|
377 | {
|
---|
378 | case 'p':
|
---|
379 | fPause = true;
|
---|
380 | break;
|
---|
381 |
|
---|
382 | case 'l':
|
---|
383 | fPause = false;
|
---|
384 | break;
|
---|
385 |
|
---|
386 | case 'd':
|
---|
387 | desc = Value.psz;
|
---|
388 | break;
|
---|
389 |
|
---|
390 | case 'u':
|
---|
391 | vrc = parseSnapshotUniqueFlags(Value.psz, &enmUnique);
|
---|
392 | if (RT_FAILURE(vrc))
|
---|
393 | return errorArgument("Invalid unique name description '%s'", Value.psz);
|
---|
394 | break;
|
---|
395 |
|
---|
396 | default:
|
---|
397 | errorGetOpt(USAGE_SNAPSHOT, ch, &Value);
|
---|
398 | rc = E_FAIL;
|
---|
399 | break;
|
---|
400 | }
|
---|
401 | }
|
---|
402 | if (FAILED(rc))
|
---|
403 | break;
|
---|
404 |
|
---|
405 | if (enmUnique & (SnapshotUniqueFlags_Number | SnapshotUniqueFlags_Timestamp))
|
---|
406 | {
|
---|
407 | ComPtr<ISnapshot> pSnapshot;
|
---|
408 | rc = sessionMachine->FindSnapshot(name.raw(),
|
---|
409 | pSnapshot.asOutParam());
|
---|
410 | if (SUCCEEDED(rc) || (enmUnique & SnapshotUniqueFlags_Force))
|
---|
411 | {
|
---|
412 | /* there is a duplicate, need to create a unique name */
|
---|
413 | uint32_t count = 0;
|
---|
414 | RTTIMESPEC now;
|
---|
415 |
|
---|
416 | if (enmUnique & SnapshotUniqueFlags_Number)
|
---|
417 | {
|
---|
418 | if (enmUnique & SnapshotUniqueFlags_Force)
|
---|
419 | count = 1;
|
---|
420 | else
|
---|
421 | count = 2;
|
---|
422 | }
|
---|
423 | else
|
---|
424 | RTTimeNow(&now);
|
---|
425 |
|
---|
426 | while (count < 500)
|
---|
427 | {
|
---|
428 | Utf8Str suffix;
|
---|
429 | if (enmUnique & SnapshotUniqueFlags_Number)
|
---|
430 | suffix = Utf8StrFmt("%u", count);
|
---|
431 | else
|
---|
432 | {
|
---|
433 | RTTIMESPEC nowplus = now;
|
---|
434 | RTTimeSpecAddSeconds(&nowplus, count);
|
---|
435 | RTTIME stamp;
|
---|
436 | RTTimeExplode(&stamp, &nowplus);
|
---|
437 | suffix = Utf8StrFmt("%04u-%02u-%02uT%02u:%02u:%02uZ", stamp.i32Year, stamp.u8Month, stamp.u8MonthDay, stamp.u8Hour, stamp.u8Minute, stamp.u8Second);
|
---|
438 | }
|
---|
439 | Bstr tryName = name;
|
---|
440 | if (enmUnique & SnapshotUniqueFlags_Space)
|
---|
441 | tryName = BstrFmt("%ls %s", name.raw(), suffix.c_str());
|
---|
442 | else
|
---|
443 | tryName = BstrFmt("%ls%s", name.raw(), suffix.c_str());
|
---|
444 | count++;
|
---|
445 | rc = sessionMachine->FindSnapshot(tryName.raw(),
|
---|
446 | pSnapshot.asOutParam());
|
---|
447 | if (FAILED(rc))
|
---|
448 | {
|
---|
449 | name = tryName;
|
---|
450 | break;
|
---|
451 | }
|
---|
452 | }
|
---|
453 | if (SUCCEEDED(rc))
|
---|
454 | {
|
---|
455 | errorArgument("Failed to generate a unique snapshot name");
|
---|
456 | rc = E_FAIL;
|
---|
457 | break;
|
---|
458 | }
|
---|
459 | }
|
---|
460 | rc = S_OK;
|
---|
461 | }
|
---|
462 |
|
---|
463 | ComPtr<IProgress> progress;
|
---|
464 | Bstr snapId;
|
---|
465 | CHECK_ERROR_BREAK(sessionMachine, TakeSnapshot(name.raw(), desc.raw(),
|
---|
466 | fPause, snapId.asOutParam(),
|
---|
467 | progress.asOutParam()));
|
---|
468 |
|
---|
469 | rc = showProgress(progress);
|
---|
470 | if (SUCCEEDED(rc))
|
---|
471 | RTPrintf("Snapshot taken. UUID: %ls\n", snapId.raw());
|
---|
472 | else
|
---|
473 | CHECK_PROGRESS_ERROR(progress, ("Failed to take snapshot"));
|
---|
474 | }
|
---|
475 | else if ( (fDelete = !strcmp(a->argv[1], "delete"))
|
---|
476 | || (fRestore = !strcmp(a->argv[1], "restore"))
|
---|
477 | || (fRestoreCurrent = !strcmp(a->argv[1], "restorecurrent"))
|
---|
478 | )
|
---|
479 | {
|
---|
480 | if (fRestoreCurrent)
|
---|
481 | {
|
---|
482 | if (a->argc > 2)
|
---|
483 | {
|
---|
484 | errorSyntax(USAGE_SNAPSHOT, "Too many arguments");
|
---|
485 | rc = E_FAIL;
|
---|
486 | break;
|
---|
487 | }
|
---|
488 | }
|
---|
489 | /* exactly one parameter: snapshot name */
|
---|
490 | else if (a->argc != 3)
|
---|
491 | {
|
---|
492 | errorSyntax(USAGE_SNAPSHOT, "Expecting snapshot name only");
|
---|
493 | rc = E_FAIL;
|
---|
494 | break;
|
---|
495 | }
|
---|
496 |
|
---|
497 | ComPtr<ISnapshot> pSnapshot;
|
---|
498 | ComPtr<IProgress> pProgress;
|
---|
499 | Bstr bstrSnapGuid;
|
---|
500 |
|
---|
501 | if (fRestoreCurrent)
|
---|
502 | {
|
---|
503 | CHECK_ERROR_BREAK(sessionMachine, COMGETTER(CurrentSnapshot)(pSnapshot.asOutParam()));
|
---|
504 | if (pSnapshot.isNull())
|
---|
505 | {
|
---|
506 | RTPrintf("This machine does not have any snapshots\n");
|
---|
507 | return RTEXITCODE_FAILURE;
|
---|
508 | }
|
---|
509 | }
|
---|
510 | else
|
---|
511 | {
|
---|
512 | // restore or delete snapshot: then resolve cmd line argument to snapshot instance
|
---|
513 | CHECK_ERROR_BREAK(sessionMachine, FindSnapshot(Bstr(a->argv[2]).raw(),
|
---|
514 | pSnapshot.asOutParam()));
|
---|
515 | }
|
---|
516 |
|
---|
517 | CHECK_ERROR_BREAK(pSnapshot, COMGETTER(Id)(bstrSnapGuid.asOutParam()));
|
---|
518 |
|
---|
519 | if (fDelete)
|
---|
520 | {
|
---|
521 | CHECK_ERROR_BREAK(sessionMachine, DeleteSnapshot(bstrSnapGuid.raw(),
|
---|
522 | pProgress.asOutParam()));
|
---|
523 | }
|
---|
524 | else
|
---|
525 | {
|
---|
526 | // restore or restore current
|
---|
527 | RTPrintf("Restoring snapshot %ls\n", bstrSnapGuid.raw());
|
---|
528 | CHECK_ERROR_BREAK(sessionMachine, RestoreSnapshot(pSnapshot, pProgress.asOutParam()));
|
---|
529 | }
|
---|
530 |
|
---|
531 | rc = showProgress(pProgress);
|
---|
532 | CHECK_PROGRESS_ERROR(pProgress, ("Snapshot operation failed"));
|
---|
533 | }
|
---|
534 | else if (!strcmp(a->argv[1], "edit"))
|
---|
535 | {
|
---|
536 | if (a->argc < 3)
|
---|
537 | {
|
---|
538 | errorSyntax(USAGE_SNAPSHOT, "Missing snapshot name");
|
---|
539 | rc = E_FAIL;
|
---|
540 | break;
|
---|
541 | }
|
---|
542 |
|
---|
543 | ComPtr<ISnapshot> pSnapshot;
|
---|
544 |
|
---|
545 | if ( !strcmp(a->argv[2], "--current")
|
---|
546 | || !strcmp(a->argv[2], "-current"))
|
---|
547 | {
|
---|
548 | CHECK_ERROR_BREAK(sessionMachine, COMGETTER(CurrentSnapshot)(pSnapshot.asOutParam()));
|
---|
549 | if (pSnapshot.isNull())
|
---|
550 | {
|
---|
551 | RTPrintf("This machine does not have any snapshots\n");
|
---|
552 | return RTEXITCODE_FAILURE;
|
---|
553 | }
|
---|
554 | }
|
---|
555 | else
|
---|
556 | {
|
---|
557 | CHECK_ERROR_BREAK(sessionMachine, FindSnapshot(Bstr(a->argv[2]).raw(),
|
---|
558 | pSnapshot.asOutParam()));
|
---|
559 | }
|
---|
560 |
|
---|
561 | /* parse options */
|
---|
562 | for (int i = 3; i < a->argc; i++)
|
---|
563 | {
|
---|
564 | if ( !strcmp(a->argv[i], "--name")
|
---|
565 | || !strcmp(a->argv[i], "-name")
|
---|
566 | || !strcmp(a->argv[i], "-newname"))
|
---|
567 | {
|
---|
568 | if (a->argc <= i + 1)
|
---|
569 | {
|
---|
570 | errorArgument("Missing argument to '%s'", a->argv[i]);
|
---|
571 | rc = E_FAIL;
|
---|
572 | break;
|
---|
573 | }
|
---|
574 | i++;
|
---|
575 | pSnapshot->COMSETTER(Name)(Bstr(a->argv[i]).raw());
|
---|
576 | }
|
---|
577 | else if ( !strcmp(a->argv[i], "--description")
|
---|
578 | || !strcmp(a->argv[i], "-description")
|
---|
579 | || !strcmp(a->argv[i], "-newdesc"))
|
---|
580 | {
|
---|
581 | if (a->argc <= i + 1)
|
---|
582 | {
|
---|
583 | errorArgument("Missing argument to '%s'", a->argv[i]);
|
---|
584 | rc = E_FAIL;
|
---|
585 | break;
|
---|
586 | }
|
---|
587 | i++;
|
---|
588 | pSnapshot->COMSETTER(Description)(Bstr(a->argv[i]).raw());
|
---|
589 | }
|
---|
590 | else
|
---|
591 | {
|
---|
592 | errorSyntax(USAGE_SNAPSHOT, "Invalid parameter '%s'", Utf8Str(a->argv[i]).c_str());
|
---|
593 | rc = E_FAIL;
|
---|
594 | break;
|
---|
595 | }
|
---|
596 | }
|
---|
597 |
|
---|
598 | }
|
---|
599 | else if (!strcmp(a->argv[1], "showvminfo"))
|
---|
600 | {
|
---|
601 | /* exactly one parameter: snapshot name */
|
---|
602 | if (a->argc != 3)
|
---|
603 | {
|
---|
604 | errorSyntax(USAGE_SNAPSHOT, "Expecting snapshot name only");
|
---|
605 | rc = E_FAIL;
|
---|
606 | break;
|
---|
607 | }
|
---|
608 |
|
---|
609 | ComPtr<ISnapshot> pSnapshot;
|
---|
610 |
|
---|
611 | CHECK_ERROR_BREAK(sessionMachine, FindSnapshot(Bstr(a->argv[2]).raw(),
|
---|
612 | pSnapshot.asOutParam()));
|
---|
613 |
|
---|
614 | /* get the machine of the given snapshot */
|
---|
615 | ComPtr<IMachine> pMachine2;
|
---|
616 | pSnapshot->COMGETTER(Machine)(pMachine2.asOutParam());
|
---|
617 | showVMInfo(a->virtualBox, pMachine2, NULL, VMINFO_NONE);
|
---|
618 | }
|
---|
619 | else if (!strcmp(a->argv[1], "list"))
|
---|
620 | rc = handleSnapshotList(a, sessionMachine) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
|
---|
621 | else if (!strcmp(a->argv[1], "dump")) // undocumented parameter to debug snapshot info
|
---|
622 | DumpSnapshot(sessionMachine);
|
---|
623 | else
|
---|
624 | {
|
---|
625 | errorSyntax(USAGE_SNAPSHOT, "Invalid parameter '%s'", Utf8Str(a->argv[1]).c_str());
|
---|
626 | rc = E_FAIL;
|
---|
627 | }
|
---|
628 | } while (0);
|
---|
629 |
|
---|
630 | a->session->UnlockMachine();
|
---|
631 |
|
---|
632 | return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
633 | }
|
---|
634 |
|
---|