VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxManage/VBoxManageSnapshot.cpp@ 25624

Last change on this file since 25624 was 25019, checked in by vboxsync, 15 years ago

VBoxManage: -Wshadow warnings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.9 KB
Line 
1/* $Id: VBoxManageSnapshot.cpp 25019 2009-11-26 15:38:39Z vboxsync $ */
2/** @file
3 * VBoxManage - The 'snapshot' command.
4 */
5
6/*
7 * Copyright (C) 2006-2009 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25#include <VBox/com/com.h>
26#include <VBox/com/string.h>
27#include <VBox/com/array.h>
28#include <VBox/com/ErrorInfo.h>
29#include <VBox/com/errorprint.h>
30
31#include <VBox/com/VirtualBox.h>
32
33#include <iprt/stream.h>
34#include <iprt/getopt.h>
35
36#include "VBoxManage.h"
37using namespace com;
38
39/**
40 * Helper function used with "VBoxManage snapshot ... dump". Gets called to find the
41 * snapshot in the machine's snapshot tree that uses a particular diff image child of
42 * a medium.
43 * Horribly inefficient since we keep re-querying the snapshots tree for each image,
44 * but this is for quick debugging only.
45 * @param pMedium
46 * @param pThisSnapshot
47 * @param pCurrentSnapshot
48 * @param uMediumLevel
49 * @param uSnapshotLevel
50 * @return
51 */
52bool FindAndPrintSnapshotUsingMedium(ComPtr<IMedium> &pMedium,
53 ComPtr<ISnapshot> &pThisSnapshot,
54 ComPtr<ISnapshot> &pCurrentSnapshot,
55 uint32_t uMediumLevel,
56 uint32_t uSnapshotLevel)
57{
58 HRESULT rc;
59
60 do
61 {
62 // get snapshot machine so we can figure out which diff image this created
63 ComPtr<IMachine> pSnapshotMachine;
64 CHECK_ERROR_BREAK(pThisSnapshot, COMGETTER(Machine)(pSnapshotMachine.asOutParam()));
65
66 // get media attachments
67 SafeIfaceArray<IMediumAttachment> aAttachments;
68 CHECK_ERROR_BREAK(pSnapshotMachine, COMGETTER(MediumAttachments)(ComSafeArrayAsOutParam(aAttachments)));
69
70 for (uint32_t i = 0;
71 i < aAttachments.size();
72 ++i)
73 {
74 ComPtr<IMediumAttachment> pAttach(aAttachments[i]);
75 DeviceType_T type;
76 CHECK_ERROR_BREAK(pAttach, COMGETTER(Type)(&type));
77 if (type == DeviceType_HardDisk)
78 {
79 ComPtr<IMedium> pMediumInSnapshot;
80 CHECK_ERROR_BREAK(pAttach, COMGETTER(Medium)(pMediumInSnapshot.asOutParam()));
81
82 if (pMediumInSnapshot == pMedium)
83 {
84 // get snapshot name
85 Bstr bstrSnapshotName;
86 CHECK_ERROR_BREAK(pThisSnapshot, COMGETTER(Name)(bstrSnapshotName.asOutParam()));
87
88 RTPrintf("%*s \"%ls\"%s\n",
89 50 + uSnapshotLevel * 2, "", // indent
90 bstrSnapshotName.raw(),
91 (pThisSnapshot == pCurrentSnapshot) ? " (CURSNAP)" : "");
92 return true; // found
93 }
94 }
95 }
96
97 // not found: then recurse into child snapshots
98 SafeIfaceArray<ISnapshot> aSnapshots;
99 CHECK_ERROR_BREAK(pThisSnapshot, COMGETTER(Children)(ComSafeArrayAsOutParam(aSnapshots)));
100
101 for (uint32_t i = 0;
102 i < aSnapshots.size();
103 ++i)
104 {
105 ComPtr<ISnapshot> pChild(aSnapshots[i]);
106 if (FindAndPrintSnapshotUsingMedium(pMedium,
107 pChild,
108 pCurrentSnapshot,
109 uMediumLevel,
110 uSnapshotLevel + 1))
111 // found:
112 break;
113 }
114 } while (0);
115
116 return false;
117}
118
119/**
120 * Helper function used with "VBoxManage snapshot ... dump". Called from DumpSnapshot()
121 * for each hard disk attachment found in a virtual machine. This then writes out the
122 * root (base) medium for that hard disk attachment and recurses into the children
123 * tree of that medium, correlating it with the snapshots of the machine.
124 * @param pCurrentStateMedium constant, the medium listed in the current machine data (latest diff image).
125 * @param pMedium variant, initially the base medium, then a child of the base medium when recursing.
126 * @param pRootSnapshot constant, the root snapshot of the machine, if any; this then looks into the child snapshots.
127 * @param pCurrentSnapshot constant, the machine's current snapshot (so we can mark it in the output).
128 * @param uLevel variant, the recursion level for output indentation.
129 */
130void DumpMediumWithChildren(ComPtr<IMedium> &pCurrentStateMedium,
131 ComPtr<IMedium> &pMedium,
132 ComPtr<ISnapshot> &pRootSnapshot,
133 ComPtr<ISnapshot> &pCurrentSnapshot,
134 uint32_t uLevel)
135{
136 HRESULT rc;
137 do
138 {
139 // print this medium
140 Bstr bstrMediumName;
141 CHECK_ERROR_BREAK(pMedium, COMGETTER(Name)(bstrMediumName.asOutParam()));
142 RTPrintf("%*s \"%ls\"%s\n",
143 uLevel * 2, "", // indent
144 bstrMediumName.raw(),
145 (pCurrentStateMedium == pMedium) ? " (CURSTATE)" : "");
146
147 // find and print the snapshot that uses this particular medium (diff image)
148 FindAndPrintSnapshotUsingMedium(pMedium, pRootSnapshot, pCurrentSnapshot, uLevel, 0);
149
150 // recurse into children
151 SafeIfaceArray<IMedium> aChildren;
152 CHECK_ERROR_BREAK(pMedium, COMGETTER(Children)(ComSafeArrayAsOutParam(aChildren)));
153 for (uint32_t i = 0;
154 i < aChildren.size();
155 ++i)
156 {
157 ComPtr<IMedium> pChild(aChildren[i]);
158 DumpMediumWithChildren(pCurrentStateMedium, pChild, pRootSnapshot, pCurrentSnapshot, uLevel + 1);
159 }
160 } while (0);
161}
162
163/**
164 * Implementation for "VBoxManage snapshot ... dump". This goes thru the machine's
165 * medium attachments and calls DumpMediumWithChildren() for each hard disk medium found,
166 * which then dumps the parent/child tree of that medium together with the corresponding
167 * snapshots.
168 * @param pMachine Machine to dump snapshots for.
169 */
170void DumpSnapshot(ComPtr<IMachine> &pMachine)
171{
172 HRESULT rc;
173
174 do
175 {
176 // get root snapshot
177 ComPtr<ISnapshot> pSnapshot;
178 CHECK_ERROR_BREAK(pMachine, GetSnapshot(Bstr(""), pSnapshot.asOutParam()));
179
180 // get current snapshot
181 ComPtr<ISnapshot> pCurrentSnapshot;
182 CHECK_ERROR_BREAK(pMachine, COMGETTER(CurrentSnapshot)(pCurrentSnapshot.asOutParam()));
183
184 // get media attachments
185 SafeIfaceArray<IMediumAttachment> aAttachments;
186 CHECK_ERROR_BREAK(pMachine, COMGETTER(MediumAttachments)(ComSafeArrayAsOutParam(aAttachments)));
187 for (uint32_t i = 0;
188 i < aAttachments.size();
189 ++i)
190 {
191 ComPtr<IMediumAttachment> pAttach(aAttachments[i]);
192 DeviceType_T type;
193 CHECK_ERROR_BREAK(pAttach, COMGETTER(Type)(&type));
194 if (type == DeviceType_HardDisk)
195 {
196 ComPtr<IMedium> pCurrentStateMedium;
197 CHECK_ERROR_BREAK(pAttach, COMGETTER(Medium)(pCurrentStateMedium.asOutParam()));
198
199 ComPtr<IMedium> pBaseMedium;
200 CHECK_ERROR_BREAK(pCurrentStateMedium, COMGETTER(Base)(pBaseMedium.asOutParam()));
201
202 Bstr bstrBaseMediumName;
203 CHECK_ERROR_BREAK(pBaseMedium, COMGETTER(Name)(bstrBaseMediumName.asOutParam()));
204
205 RTPrintf("[%RI32] Images and snapshots for medium \"%ls\"\n", i, bstrBaseMediumName.raw());
206
207 DumpMediumWithChildren(pCurrentStateMedium,
208 pBaseMedium,
209 pSnapshot,
210 pCurrentSnapshot,
211 0);
212 }
213 }
214 } while (0);
215}
216
217/**
218 * Implementation for all VBoxManage snapshot ... subcommands.
219 * @param a
220 * @return
221 */
222int handleSnapshot(HandlerArg *a)
223{
224 HRESULT rc;
225
226 /* we need at least a VM and a command */
227 if (a->argc < 2)
228 return errorSyntax(USAGE_SNAPSHOT, "Not enough parameters");
229
230 /* the first argument must be the VM */
231 Bstr bstrMachine(a->argv[0]);
232 ComPtr<IMachine> pMachine;
233 /* assume it's a UUID */
234 rc = a->virtualBox->GetMachine(bstrMachine, pMachine.asOutParam());
235 if (FAILED(rc) || !pMachine)
236 {
237 /* must be a name */
238 CHECK_ERROR(a->virtualBox, FindMachine(bstrMachine, pMachine.asOutParam()));
239 }
240 if (!pMachine)
241 return 1;
242 Bstr guidMachine;
243 pMachine->COMGETTER(Id)(guidMachine.asOutParam());
244
245 do
246 {
247 /* we have to open a session for this task. First try an existing session */
248 rc = a->virtualBox->OpenExistingSession(a->session, guidMachine);
249 if (FAILED(rc))
250 CHECK_ERROR_BREAK(a->virtualBox, OpenSession(a->session, guidMachine));
251 ComPtr<IConsole> console;
252 CHECK_ERROR_BREAK(a->session, COMGETTER(Console)(console.asOutParam()));
253
254 /* switch based on the command */
255 bool fDelete = false, fRestore = false;
256 if (!strcmp(a->argv[1], "take"))
257 {
258 /* there must be a name */
259 if (a->argc < 3)
260 {
261 errorSyntax(USAGE_SNAPSHOT, "Missing snapshot name");
262 rc = E_FAIL;
263 break;
264 }
265 Bstr name(a->argv[2]);
266
267 /* parse the optional arguments */
268 Bstr desc;
269 bool fPause = false;
270 static const RTGETOPTDEF s_aTakeOptions[] =
271 {
272 { "--description", 'd', RTGETOPT_REQ_STRING },
273 { "-description", 'd', RTGETOPT_REQ_STRING },
274 { "-desc", 'd', RTGETOPT_REQ_STRING },
275 { "--pause", 'p', RTGETOPT_REQ_NOTHING }
276 };
277 RTGETOPTSTATE GetOptState;
278 RTGetOptInit(&GetOptState, a->argc, a->argv, s_aTakeOptions, RT_ELEMENTS(s_aTakeOptions), 3, 0 /*fFlags*/);
279 int ch;
280 RTGETOPTUNION Value;
281 while ( SUCCEEDED(rc)
282 && (ch = RTGetOpt(&GetOptState, &Value)))
283 {
284 switch (ch)
285 {
286 case 'p':
287 fPause = true;
288 break;
289
290 case 'd':
291 desc = Value.psz;
292 break;
293
294 default:
295 errorGetOpt(USAGE_SNAPSHOT, ch, &Value);
296 rc = E_FAIL;
297 break;
298 }
299 }
300 if (FAILED(rc))
301 break;
302
303 if (fPause)
304 {
305 MachineState_T machineState;
306 CHECK_ERROR_BREAK(console, COMGETTER(State)(&machineState));
307 if (machineState == MachineState_Running)
308 CHECK_ERROR_BREAK(console, Pause());
309 else
310 fPause = false;
311 }
312
313 ComPtr<IProgress> progress;
314 CHECK_ERROR_BREAK(console, TakeSnapshot(name, desc, progress.asOutParam()));
315
316 rc = showProgress(progress);
317 if (FAILED(rc))
318 {
319 com::ProgressErrorInfo info(progress);
320 if (info.isBasicAvailable())
321 RTPrintf("Error: failed to take snapshot. Error message: %lS\n", info.getText().raw());
322 else
323 RTPrintf("Error: failed to take snapshot. No error message available!\n");
324 }
325
326 if (fPause)
327 {
328 MachineState_T machineState;
329 CHECK_ERROR_BREAK(console, COMGETTER(State)(&machineState));
330 if (machineState == MachineState_Paused)
331 {
332 if (SUCCEEDED(rc))
333 CHECK_ERROR_BREAK(console, Resume());
334 else
335 console->Resume();
336 }
337 }
338 }
339 else if ( (fDelete = !strcmp(a->argv[1], "delete"))
340 || (fRestore = !strcmp(a->argv[1], "restore"))
341 )
342 {
343 /* exactly one parameter: snapshot name */
344 if (a->argc != 3)
345 {
346 errorSyntax(USAGE_SNAPSHOT, "Expecting snapshot name only");
347 rc = E_FAIL;
348 break;
349 }
350
351 ComPtr<ISnapshot> pSnapshot;
352
353 /* assume it's a UUID */
354 Bstr guidSnap(a->argv[2]);
355 if (!guidSnap.isEmpty())
356 {
357 CHECK_ERROR_BREAK(pMachine, GetSnapshot(guidSnap, pSnapshot.asOutParam()));
358 }
359 else
360 {
361 /* then it must be a name */
362 CHECK_ERROR_BREAK(pMachine, FindSnapshot(Bstr(a->argv[2]), pSnapshot.asOutParam()));
363 pSnapshot->COMGETTER(Id)(guidSnap.asOutParam());
364 }
365
366 ComPtr<IProgress> pProgress;
367 if (fDelete)
368 {
369 CHECK_ERROR_BREAK(console, DeleteSnapshot(guidSnap, pProgress.asOutParam()));
370 }
371 else
372 {
373 // must be restore
374 CHECK_ERROR_BREAK(console, RestoreSnapshot(pSnapshot, pProgress.asOutParam()));
375 }
376
377 rc = showProgress(pProgress);
378 if (FAILED(rc))
379 {
380 com::ProgressErrorInfo info(pProgress);
381 if (info.isBasicAvailable())
382 RTPrintf("Error: snapshot operation failed. Error message: %lS\n", info.getText().raw());
383 else
384 RTPrintf("Error: snapshot operation failed. No error message available!\n");
385 }
386 }
387 else if (!strcmp(a->argv[1], "edit"))
388 {
389 if (a->argc < 3)
390 {
391 errorSyntax(USAGE_SNAPSHOT, "Missing snapshot name");
392 rc = E_FAIL;
393 break;
394 }
395
396 ComPtr<ISnapshot> snapshot;
397
398 if ( !strcmp(a->argv[2], "--current")
399 || !strcmp(a->argv[2], "-current"))
400 {
401 CHECK_ERROR_BREAK(pMachine, COMGETTER(CurrentSnapshot)(snapshot.asOutParam()));
402 }
403 else
404 {
405 /* assume it's a UUID */
406 Bstr guidSnap(a->argv[2]);
407 if (!guidSnap.isEmpty())
408 {
409 CHECK_ERROR_BREAK(pMachine, GetSnapshot(guidSnap, snapshot.asOutParam()));
410 }
411 else
412 {
413 /* then it must be a name */
414 CHECK_ERROR_BREAK(pMachine, FindSnapshot(Bstr(a->argv[2]), snapshot.asOutParam()));
415 }
416 }
417
418 /* parse options */
419 for (int i = 3; i < a->argc; i++)
420 {
421 if ( !strcmp(a->argv[i], "--name")
422 || !strcmp(a->argv[i], "-name")
423 || !strcmp(a->argv[i], "-newname"))
424 {
425 if (a->argc <= i + 1)
426 {
427 errorArgument("Missing argument to '%s'", a->argv[i]);
428 rc = E_FAIL;
429 break;
430 }
431 i++;
432 snapshot->COMSETTER(Name)(Bstr(a->argv[i]));
433 }
434 else if ( !strcmp(a->argv[i], "--description")
435 || !strcmp(a->argv[i], "-description")
436 || !strcmp(a->argv[i], "-newdesc"))
437 {
438 if (a->argc <= i + 1)
439 {
440 errorArgument("Missing argument to '%s'", a->argv[i]);
441 rc = E_FAIL;
442 break;
443 }
444 i++;
445 snapshot->COMSETTER(Description)(Bstr(a->argv[i]));
446 }
447 else
448 {
449 errorSyntax(USAGE_SNAPSHOT, "Invalid parameter '%s'", Utf8Str(a->argv[i]).raw());
450 rc = E_FAIL;
451 break;
452 }
453 }
454
455 }
456 else if (!strcmp(a->argv[1], "showvminfo"))
457 {
458 /* exactly one parameter: snapshot name */
459 if (a->argc != 3)
460 {
461 errorSyntax(USAGE_SNAPSHOT, "Expecting snapshot name only");
462 rc = E_FAIL;
463 break;
464 }
465
466 ComPtr<ISnapshot> snapshot;
467
468 /* assume it's a UUID */
469 Bstr guidSnap(a->argv[2]);
470 if (!guidSnap.isEmpty())
471 {
472 CHECK_ERROR_BREAK(pMachine, GetSnapshot(guidSnap, snapshot.asOutParam()));
473 }
474 else
475 {
476 /* then it must be a name */
477 CHECK_ERROR_BREAK(pMachine, FindSnapshot(Bstr(a->argv[2]), snapshot.asOutParam()));
478 }
479
480 /* get the machine of the given snapshot */
481 ComPtr<IMachine> pMachine2;
482 snapshot->COMGETTER(Machine)(pMachine2.asOutParam());
483 showVMInfo(a->virtualBox, pMachine2, VMINFO_NONE, console);
484 }
485 else if (!strcmp(a->argv[1], "dump")) // undocumented parameter to debug snapshot info
486 {
487 DumpSnapshot(pMachine);
488 }
489 else
490 {
491 errorSyntax(USAGE_SNAPSHOT, "Invalid parameter '%s'", Utf8Str(a->argv[1]).raw());
492 rc = E_FAIL;
493 }
494 } while (0);
495
496 a->session->Close();
497
498 return SUCCEEDED(rc) ? 0 : 1;
499}
500
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette