1 | /* $Id: SnapshotImpl.cpp 47401 2013-07-25 19:12:24Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * COM class implementation for Snapshot and SnapshotMachine in VBoxSVC.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2013 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include "Logging.h"
|
---|
20 | #include "SnapshotImpl.h"
|
---|
21 |
|
---|
22 | #include "MachineImpl.h"
|
---|
23 | #include "MediumImpl.h"
|
---|
24 | #include "MediumFormatImpl.h"
|
---|
25 | #include "Global.h"
|
---|
26 | #include "ProgressImpl.h"
|
---|
27 |
|
---|
28 | // @todo these three includes are required for about one or two lines, try
|
---|
29 | // to remove them and put that code in shared code in MachineImplcpp
|
---|
30 | #include "SharedFolderImpl.h"
|
---|
31 | #include "USBControllerImpl.h"
|
---|
32 | #include "USBDeviceFiltersImpl.h"
|
---|
33 | #include "VirtualBoxImpl.h"
|
---|
34 |
|
---|
35 | #include "AutoCaller.h"
|
---|
36 |
|
---|
37 | #include <iprt/path.h>
|
---|
38 | #include <iprt/cpp/utils.h>
|
---|
39 |
|
---|
40 | #include <VBox/param.h>
|
---|
41 | #include <VBox/err.h>
|
---|
42 |
|
---|
43 | #include <VBox/settings.h>
|
---|
44 |
|
---|
45 |
|
---|
46 | ////////////////////////////////////////////////////////////////////////////////
|
---|
47 | //
|
---|
48 | // Snapshot private data definition
|
---|
49 | //
|
---|
50 | ////////////////////////////////////////////////////////////////////////////////
|
---|
51 |
|
---|
52 | typedef std::list< ComObjPtr<Snapshot> > SnapshotsList;
|
---|
53 |
|
---|
54 | struct Snapshot::Data
|
---|
55 | {
|
---|
56 | Data()
|
---|
57 | : pVirtualBox(NULL)
|
---|
58 | {
|
---|
59 | RTTimeSpecSetMilli(&timeStamp, 0);
|
---|
60 | };
|
---|
61 |
|
---|
62 | ~Data()
|
---|
63 | {}
|
---|
64 |
|
---|
65 | const Guid uuid;
|
---|
66 | Utf8Str strName;
|
---|
67 | Utf8Str strDescription;
|
---|
68 | RTTIMESPEC timeStamp;
|
---|
69 | ComObjPtr<SnapshotMachine> pMachine;
|
---|
70 |
|
---|
71 | /** weak VirtualBox parent */
|
---|
72 | VirtualBox * const pVirtualBox;
|
---|
73 |
|
---|
74 | // pParent and llChildren are protected by the machine lock
|
---|
75 | ComObjPtr<Snapshot> pParent;
|
---|
76 | SnapshotsList llChildren;
|
---|
77 | };
|
---|
78 |
|
---|
79 | ////////////////////////////////////////////////////////////////////////////////
|
---|
80 | //
|
---|
81 | // Constructor / destructor
|
---|
82 | //
|
---|
83 | ////////////////////////////////////////////////////////////////////////////////
|
---|
84 |
|
---|
85 | HRESULT Snapshot::FinalConstruct()
|
---|
86 | {
|
---|
87 | LogFlowThisFunc(("\n"));
|
---|
88 | return BaseFinalConstruct();
|
---|
89 | }
|
---|
90 |
|
---|
91 | void Snapshot::FinalRelease()
|
---|
92 | {
|
---|
93 | LogFlowThisFunc(("\n"));
|
---|
94 | uninit();
|
---|
95 | BaseFinalRelease();
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Initializes the instance
|
---|
100 | *
|
---|
101 | * @param aId id of the snapshot
|
---|
102 | * @param aName name of the snapshot
|
---|
103 | * @param aDescription name of the snapshot (NULL if no description)
|
---|
104 | * @param aTimeStamp timestamp of the snapshot, in ms since 1970-01-01 UTC
|
---|
105 | * @param aMachine machine associated with this snapshot
|
---|
106 | * @param aParent parent snapshot (NULL if no parent)
|
---|
107 | */
|
---|
108 | HRESULT Snapshot::init(VirtualBox *aVirtualBox,
|
---|
109 | const Guid &aId,
|
---|
110 | const Utf8Str &aName,
|
---|
111 | const Utf8Str &aDescription,
|
---|
112 | const RTTIMESPEC &aTimeStamp,
|
---|
113 | SnapshotMachine *aMachine,
|
---|
114 | Snapshot *aParent)
|
---|
115 | {
|
---|
116 | LogFlowThisFunc(("uuid=%s aParent->uuid=%s\n", aId.toString().c_str(), (aParent) ? aParent->m->uuid.toString().c_str() : ""));
|
---|
117 |
|
---|
118 | ComAssertRet(!aId.isZero() && aId.isValid() && !aName.isEmpty() && aMachine, E_INVALIDARG);
|
---|
119 |
|
---|
120 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
121 | AutoInitSpan autoInitSpan(this);
|
---|
122 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
123 |
|
---|
124 | m = new Data;
|
---|
125 |
|
---|
126 | /* share parent weakly */
|
---|
127 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
128 |
|
---|
129 | m->pParent = aParent;
|
---|
130 |
|
---|
131 | unconst(m->uuid) = aId;
|
---|
132 | m->strName = aName;
|
---|
133 | m->strDescription = aDescription;
|
---|
134 | m->timeStamp = aTimeStamp;
|
---|
135 | m->pMachine = aMachine;
|
---|
136 |
|
---|
137 | if (aParent)
|
---|
138 | aParent->m->llChildren.push_back(this);
|
---|
139 |
|
---|
140 | /* Confirm a successful initialization when it's the case */
|
---|
141 | autoInitSpan.setSucceeded();
|
---|
142 |
|
---|
143 | return S_OK;
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Uninitializes the instance and sets the ready flag to FALSE.
|
---|
148 | * Called either from FinalRelease(), by the parent when it gets destroyed,
|
---|
149 | * or by a third party when it decides this object is no more valid.
|
---|
150 | *
|
---|
151 | * Since this manipulates the snapshots tree, the caller must hold the
|
---|
152 | * machine lock in write mode (which protects the snapshots tree)!
|
---|
153 | */
|
---|
154 | void Snapshot::uninit()
|
---|
155 | {
|
---|
156 | LogFlowThisFunc(("\n"));
|
---|
157 |
|
---|
158 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
159 | AutoUninitSpan autoUninitSpan(this);
|
---|
160 | if (autoUninitSpan.uninitDone())
|
---|
161 | return;
|
---|
162 |
|
---|
163 | Assert(m->pMachine->isWriteLockOnCurrentThread());
|
---|
164 |
|
---|
165 | // uninit all children
|
---|
166 | SnapshotsList::iterator it;
|
---|
167 | for (it = m->llChildren.begin();
|
---|
168 | it != m->llChildren.end();
|
---|
169 | ++it)
|
---|
170 | {
|
---|
171 | Snapshot *pChild = *it;
|
---|
172 | pChild->m->pParent.setNull();
|
---|
173 | pChild->uninit();
|
---|
174 | }
|
---|
175 | m->llChildren.clear(); // this unsets all the ComPtrs and probably calls delete
|
---|
176 |
|
---|
177 | if (m->pParent)
|
---|
178 | deparent();
|
---|
179 |
|
---|
180 | if (m->pMachine)
|
---|
181 | {
|
---|
182 | m->pMachine->uninit();
|
---|
183 | m->pMachine.setNull();
|
---|
184 | }
|
---|
185 |
|
---|
186 | delete m;
|
---|
187 | m = NULL;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Delete the current snapshot by removing it from the tree of snapshots
|
---|
192 | * and reparenting its children.
|
---|
193 | *
|
---|
194 | * After this, the caller must call uninit() on the snapshot. We can't call
|
---|
195 | * that from here because if we do, the AutoUninitSpan waits forever for
|
---|
196 | * the number of callers to become 0 (it is 1 because of the AutoCaller in here).
|
---|
197 | *
|
---|
198 | * NOTE: this does NOT lock the snapshot, it is assumed that the machine state
|
---|
199 | * (and the snapshots tree) is protected by the caller having requested the machine
|
---|
200 | * lock in write mode AND the machine state must be DeletingSnapshot.
|
---|
201 | */
|
---|
202 | void Snapshot::beginSnapshotDelete()
|
---|
203 | {
|
---|
204 | AutoCaller autoCaller(this);
|
---|
205 | if (FAILED(autoCaller.rc()))
|
---|
206 | return;
|
---|
207 |
|
---|
208 | // caller must have acquired the machine's write lock
|
---|
209 | Assert( m->pMachine->mData->mMachineState == MachineState_DeletingSnapshot
|
---|
210 | || m->pMachine->mData->mMachineState == MachineState_DeletingSnapshotOnline
|
---|
211 | || m->pMachine->mData->mMachineState == MachineState_DeletingSnapshotPaused);
|
---|
212 | Assert(m->pMachine->isWriteLockOnCurrentThread());
|
---|
213 |
|
---|
214 | // the snapshot must have only one child when being deleted or no children at all
|
---|
215 | AssertReturnVoid(m->llChildren.size() <= 1);
|
---|
216 |
|
---|
217 | ComObjPtr<Snapshot> parentSnapshot = m->pParent;
|
---|
218 |
|
---|
219 | /// @todo (dmik):
|
---|
220 | // when we introduce clones later, deleting the snapshot will affect
|
---|
221 | // the current and first snapshots of clones, if they are direct children
|
---|
222 | // of this snapshot. So we will need to lock machines associated with
|
---|
223 | // child snapshots as well and update mCurrentSnapshot and/or
|
---|
224 | // mFirstSnapshot fields.
|
---|
225 |
|
---|
226 | if (this == m->pMachine->mData->mCurrentSnapshot)
|
---|
227 | {
|
---|
228 | m->pMachine->mData->mCurrentSnapshot = parentSnapshot;
|
---|
229 |
|
---|
230 | /* we've changed the base of the current state so mark it as
|
---|
231 | * modified as it no longer guaranteed to be its copy */
|
---|
232 | m->pMachine->mData->mCurrentStateModified = TRUE;
|
---|
233 | }
|
---|
234 |
|
---|
235 | if (this == m->pMachine->mData->mFirstSnapshot)
|
---|
236 | {
|
---|
237 | if (m->llChildren.size() == 1)
|
---|
238 | {
|
---|
239 | ComObjPtr<Snapshot> childSnapshot = m->llChildren.front();
|
---|
240 | m->pMachine->mData->mFirstSnapshot = childSnapshot;
|
---|
241 | }
|
---|
242 | else
|
---|
243 | m->pMachine->mData->mFirstSnapshot.setNull();
|
---|
244 | }
|
---|
245 |
|
---|
246 | // reparent our children
|
---|
247 | for (SnapshotsList::const_iterator it = m->llChildren.begin();
|
---|
248 | it != m->llChildren.end();
|
---|
249 | ++it)
|
---|
250 | {
|
---|
251 | ComObjPtr<Snapshot> child = *it;
|
---|
252 | // no need to lock, snapshots tree is protected by machine lock
|
---|
253 | child->m->pParent = m->pParent;
|
---|
254 | if (m->pParent)
|
---|
255 | m->pParent->m->llChildren.push_back(child);
|
---|
256 | }
|
---|
257 |
|
---|
258 | // clear our own children list (since we reparented the children)
|
---|
259 | m->llChildren.clear();
|
---|
260 | }
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Internal helper that removes "this" from the list of children of its
|
---|
264 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
265 | *
|
---|
266 | * The caller must hold the machine lock in write mode (which protects the snapshots tree)!
|
---|
267 | */
|
---|
268 | void Snapshot::deparent()
|
---|
269 | {
|
---|
270 | Assert(m->pMachine->isWriteLockOnCurrentThread());
|
---|
271 |
|
---|
272 | SnapshotsList &llParent = m->pParent->m->llChildren;
|
---|
273 | for (SnapshotsList::iterator it = llParent.begin();
|
---|
274 | it != llParent.end();
|
---|
275 | ++it)
|
---|
276 | {
|
---|
277 | Snapshot *pParentsChild = *it;
|
---|
278 | if (this == pParentsChild)
|
---|
279 | {
|
---|
280 | llParent.erase(it);
|
---|
281 | break;
|
---|
282 | }
|
---|
283 | }
|
---|
284 |
|
---|
285 | m->pParent.setNull();
|
---|
286 | }
|
---|
287 |
|
---|
288 | ////////////////////////////////////////////////////////////////////////////////
|
---|
289 | //
|
---|
290 | // ISnapshot public methods
|
---|
291 | //
|
---|
292 | ////////////////////////////////////////////////////////////////////////////////
|
---|
293 |
|
---|
294 | STDMETHODIMP Snapshot::COMGETTER(Id)(BSTR *aId)
|
---|
295 | {
|
---|
296 | CheckComArgOutPointerValid(aId);
|
---|
297 |
|
---|
298 | AutoCaller autoCaller(this);
|
---|
299 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
300 |
|
---|
301 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
302 |
|
---|
303 | m->uuid.toUtf16().cloneTo(aId);
|
---|
304 | return S_OK;
|
---|
305 | }
|
---|
306 |
|
---|
307 | STDMETHODIMP Snapshot::COMGETTER(Name)(BSTR *aName)
|
---|
308 | {
|
---|
309 | CheckComArgOutPointerValid(aName);
|
---|
310 |
|
---|
311 | AutoCaller autoCaller(this);
|
---|
312 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
313 |
|
---|
314 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
315 |
|
---|
316 | m->strName.cloneTo(aName);
|
---|
317 | return S_OK;
|
---|
318 | }
|
---|
319 |
|
---|
320 | /**
|
---|
321 | * @note Locks this object for writing, then calls Machine::onSnapshotChange()
|
---|
322 | * (see its lock requirements).
|
---|
323 | */
|
---|
324 | STDMETHODIMP Snapshot::COMSETTER(Name)(IN_BSTR aName)
|
---|
325 | {
|
---|
326 | HRESULT rc = S_OK;
|
---|
327 | CheckComArgStrNotEmptyOrNull(aName);
|
---|
328 |
|
---|
329 | // prohibit setting a UUID only as the machine name, or else it can
|
---|
330 | // never be found by findMachine()
|
---|
331 | Guid test(aName);
|
---|
332 |
|
---|
333 | if (!test.isZero() && test.isValid())
|
---|
334 | return setError(E_INVALIDARG, tr("A machine cannot have a UUID as its name"));
|
---|
335 |
|
---|
336 | AutoCaller autoCaller(this);
|
---|
337 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
338 |
|
---|
339 | Utf8Str strName(aName);
|
---|
340 |
|
---|
341 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
342 |
|
---|
343 | if (m->strName != strName)
|
---|
344 | {
|
---|
345 | m->strName = strName;
|
---|
346 | alock.release(); /* Important! (child->parent locks are forbidden) */
|
---|
347 | rc = m->pMachine->onSnapshotChange(this);
|
---|
348 | }
|
---|
349 |
|
---|
350 | return rc;
|
---|
351 | }
|
---|
352 |
|
---|
353 | STDMETHODIMP Snapshot::COMGETTER(Description)(BSTR *aDescription)
|
---|
354 | {
|
---|
355 | CheckComArgOutPointerValid(aDescription);
|
---|
356 |
|
---|
357 | AutoCaller autoCaller(this);
|
---|
358 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
359 |
|
---|
360 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
361 |
|
---|
362 | m->strDescription.cloneTo(aDescription);
|
---|
363 | return S_OK;
|
---|
364 | }
|
---|
365 |
|
---|
366 | STDMETHODIMP Snapshot::COMSETTER(Description)(IN_BSTR aDescription)
|
---|
367 | {
|
---|
368 | HRESULT rc = S_OK;
|
---|
369 | AutoCaller autoCaller(this);
|
---|
370 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
371 |
|
---|
372 | Utf8Str strDescription(aDescription);
|
---|
373 |
|
---|
374 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
375 |
|
---|
376 | if (m->strDescription != strDescription)
|
---|
377 | {
|
---|
378 | m->strDescription = strDescription;
|
---|
379 | alock.release(); /* Important! (child->parent locks are forbidden) */
|
---|
380 | rc = m->pMachine->onSnapshotChange(this);
|
---|
381 | }
|
---|
382 |
|
---|
383 | return rc;
|
---|
384 | }
|
---|
385 |
|
---|
386 | STDMETHODIMP Snapshot::COMGETTER(TimeStamp)(LONG64 *aTimeStamp)
|
---|
387 | {
|
---|
388 | CheckComArgOutPointerValid(aTimeStamp);
|
---|
389 |
|
---|
390 | AutoCaller autoCaller(this);
|
---|
391 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
392 |
|
---|
393 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
394 |
|
---|
395 | *aTimeStamp = RTTimeSpecGetMilli(&m->timeStamp);
|
---|
396 | return S_OK;
|
---|
397 | }
|
---|
398 |
|
---|
399 | STDMETHODIMP Snapshot::COMGETTER(Online)(BOOL *aOnline)
|
---|
400 | {
|
---|
401 | CheckComArgOutPointerValid(aOnline);
|
---|
402 |
|
---|
403 | AutoCaller autoCaller(this);
|
---|
404 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
405 |
|
---|
406 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
407 |
|
---|
408 | *aOnline = getStateFilePath().isNotEmpty();
|
---|
409 | return S_OK;
|
---|
410 | }
|
---|
411 |
|
---|
412 | STDMETHODIMP Snapshot::COMGETTER(Machine)(IMachine **aMachine)
|
---|
413 | {
|
---|
414 | CheckComArgOutPointerValid(aMachine);
|
---|
415 |
|
---|
416 | AutoCaller autoCaller(this);
|
---|
417 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
418 |
|
---|
419 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
420 |
|
---|
421 | m->pMachine.queryInterfaceTo(aMachine);
|
---|
422 | return S_OK;
|
---|
423 | }
|
---|
424 |
|
---|
425 | STDMETHODIMP Snapshot::COMGETTER(Parent)(ISnapshot **aParent)
|
---|
426 | {
|
---|
427 | CheckComArgOutPointerValid(aParent);
|
---|
428 |
|
---|
429 | AutoCaller autoCaller(this);
|
---|
430 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
431 |
|
---|
432 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
433 |
|
---|
434 | m->pParent.queryInterfaceTo(aParent);
|
---|
435 | return S_OK;
|
---|
436 | }
|
---|
437 |
|
---|
438 | STDMETHODIMP Snapshot::COMGETTER(Children)(ComSafeArrayOut(ISnapshot *, aChildren))
|
---|
439 | {
|
---|
440 | CheckComArgOutSafeArrayPointerValid(aChildren);
|
---|
441 |
|
---|
442 | AutoCaller autoCaller(this);
|
---|
443 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
444 |
|
---|
445 | // snapshots tree is protected by machine lock
|
---|
446 | AutoReadLock alock(m->pMachine COMMA_LOCKVAL_SRC_POS);
|
---|
447 |
|
---|
448 | SafeIfaceArray<ISnapshot> collection(m->llChildren);
|
---|
449 | collection.detachTo(ComSafeArrayOutArg(aChildren));
|
---|
450 |
|
---|
451 | return S_OK;
|
---|
452 | }
|
---|
453 |
|
---|
454 | STDMETHODIMP Snapshot::GetChildrenCount(ULONG* count)
|
---|
455 | {
|
---|
456 | CheckComArgOutPointerValid(count);
|
---|
457 |
|
---|
458 | *count = getChildrenCount();
|
---|
459 |
|
---|
460 | return S_OK;
|
---|
461 | }
|
---|
462 |
|
---|
463 | ////////////////////////////////////////////////////////////////////////////////
|
---|
464 | //
|
---|
465 | // Snapshot public internal methods
|
---|
466 | //
|
---|
467 | ////////////////////////////////////////////////////////////////////////////////
|
---|
468 |
|
---|
469 | /**
|
---|
470 | * Returns the parent snapshot or NULL if there's none. Must have caller + locking!
|
---|
471 | * @return
|
---|
472 | */
|
---|
473 | const ComObjPtr<Snapshot>& Snapshot::getParent() const
|
---|
474 | {
|
---|
475 | return m->pParent;
|
---|
476 | }
|
---|
477 |
|
---|
478 | /**
|
---|
479 | * Returns the first child snapshot or NULL if there's none. Must have caller + locking!
|
---|
480 | * @return
|
---|
481 | */
|
---|
482 | const ComObjPtr<Snapshot> Snapshot::getFirstChild() const
|
---|
483 | {
|
---|
484 | if (!m->llChildren.size())
|
---|
485 | return NULL;
|
---|
486 | return m->llChildren.front();
|
---|
487 | }
|
---|
488 |
|
---|
489 | /**
|
---|
490 | * @note
|
---|
491 | * Must be called from under the object's lock!
|
---|
492 | */
|
---|
493 | const Utf8Str& Snapshot::getStateFilePath() const
|
---|
494 | {
|
---|
495 | return m->pMachine->mSSData->strStateFilePath;
|
---|
496 | }
|
---|
497 |
|
---|
498 | /**
|
---|
499 | * Returns the depth in the snapshot tree for this snapshot.
|
---|
500 | *
|
---|
501 | * @note takes the snapshot tree lock
|
---|
502 | */
|
---|
503 |
|
---|
504 | uint32_t Snapshot::getDepth()
|
---|
505 | {
|
---|
506 | AutoCaller autoCaller(this);
|
---|
507 | AssertComRC(autoCaller.rc());
|
---|
508 |
|
---|
509 | // snapshots tree is protected by machine lock
|
---|
510 | AutoReadLock alock(m->pMachine COMMA_LOCKVAL_SRC_POS);
|
---|
511 |
|
---|
512 | uint32_t cDepth = 0;
|
---|
513 | ComObjPtr<Snapshot> pSnap(this);
|
---|
514 | while (!pSnap.isNull())
|
---|
515 | {
|
---|
516 | pSnap = pSnap->m->pParent;
|
---|
517 | cDepth++;
|
---|
518 | }
|
---|
519 |
|
---|
520 | return cDepth;
|
---|
521 | }
|
---|
522 |
|
---|
523 | /**
|
---|
524 | * Returns the number of direct child snapshots, without grandchildren.
|
---|
525 | * Does not recurse.
|
---|
526 | * @return
|
---|
527 | */
|
---|
528 | ULONG Snapshot::getChildrenCount()
|
---|
529 | {
|
---|
530 | AutoCaller autoCaller(this);
|
---|
531 | AssertComRC(autoCaller.rc());
|
---|
532 |
|
---|
533 | // snapshots tree is protected by machine lock
|
---|
534 | AutoReadLock alock(m->pMachine COMMA_LOCKVAL_SRC_POS);
|
---|
535 |
|
---|
536 | return (ULONG)m->llChildren.size();
|
---|
537 | }
|
---|
538 |
|
---|
539 | /**
|
---|
540 | * Implementation method for getAllChildrenCount() so we request the
|
---|
541 | * tree lock only once before recursing. Don't call directly.
|
---|
542 | * @return
|
---|
543 | */
|
---|
544 | ULONG Snapshot::getAllChildrenCountImpl()
|
---|
545 | {
|
---|
546 | AutoCaller autoCaller(this);
|
---|
547 | AssertComRC(autoCaller.rc());
|
---|
548 |
|
---|
549 | ULONG count = (ULONG)m->llChildren.size();
|
---|
550 | for (SnapshotsList::const_iterator it = m->llChildren.begin();
|
---|
551 | it != m->llChildren.end();
|
---|
552 | ++it)
|
---|
553 | {
|
---|
554 | count += (*it)->getAllChildrenCountImpl();
|
---|
555 | }
|
---|
556 |
|
---|
557 | return count;
|
---|
558 | }
|
---|
559 |
|
---|
560 | /**
|
---|
561 | * Returns the number of child snapshots including all grandchildren.
|
---|
562 | * Recurses into the snapshots tree.
|
---|
563 | * @return
|
---|
564 | */
|
---|
565 | ULONG Snapshot::getAllChildrenCount()
|
---|
566 | {
|
---|
567 | AutoCaller autoCaller(this);
|
---|
568 | AssertComRC(autoCaller.rc());
|
---|
569 |
|
---|
570 | // snapshots tree is protected by machine lock
|
---|
571 | AutoReadLock alock(m->pMachine COMMA_LOCKVAL_SRC_POS);
|
---|
572 |
|
---|
573 | return getAllChildrenCountImpl();
|
---|
574 | }
|
---|
575 |
|
---|
576 | /**
|
---|
577 | * Returns the SnapshotMachine that this snapshot belongs to.
|
---|
578 | * Caller must hold the snapshot's object lock!
|
---|
579 | * @return
|
---|
580 | */
|
---|
581 | const ComObjPtr<SnapshotMachine>& Snapshot::getSnapshotMachine() const
|
---|
582 | {
|
---|
583 | return m->pMachine;
|
---|
584 | }
|
---|
585 |
|
---|
586 | /**
|
---|
587 | * Returns the UUID of this snapshot.
|
---|
588 | * Caller must hold the snapshot's object lock!
|
---|
589 | * @return
|
---|
590 | */
|
---|
591 | Guid Snapshot::getId() const
|
---|
592 | {
|
---|
593 | return m->uuid;
|
---|
594 | }
|
---|
595 |
|
---|
596 | /**
|
---|
597 | * Returns the name of this snapshot.
|
---|
598 | * Caller must hold the snapshot's object lock!
|
---|
599 | * @return
|
---|
600 | */
|
---|
601 | const Utf8Str& Snapshot::getName() const
|
---|
602 | {
|
---|
603 | return m->strName;
|
---|
604 | }
|
---|
605 |
|
---|
606 | /**
|
---|
607 | * Returns the time stamp of this snapshot.
|
---|
608 | * Caller must hold the snapshot's object lock!
|
---|
609 | * @return
|
---|
610 | */
|
---|
611 | RTTIMESPEC Snapshot::getTimeStamp() const
|
---|
612 | {
|
---|
613 | return m->timeStamp;
|
---|
614 | }
|
---|
615 |
|
---|
616 | /**
|
---|
617 | * Searches for a snapshot with the given ID among children, grand-children,
|
---|
618 | * etc. of this snapshot. This snapshot itself is also included in the search.
|
---|
619 | *
|
---|
620 | * Caller must hold the machine lock (which protects the snapshots tree!)
|
---|
621 | */
|
---|
622 | ComObjPtr<Snapshot> Snapshot::findChildOrSelf(IN_GUID aId)
|
---|
623 | {
|
---|
624 | ComObjPtr<Snapshot> child;
|
---|
625 |
|
---|
626 | AutoCaller autoCaller(this);
|
---|
627 | AssertComRC(autoCaller.rc());
|
---|
628 |
|
---|
629 | // no need to lock, uuid is const
|
---|
630 | if (m->uuid == aId)
|
---|
631 | child = this;
|
---|
632 | else
|
---|
633 | {
|
---|
634 | for (SnapshotsList::const_iterator it = m->llChildren.begin();
|
---|
635 | it != m->llChildren.end();
|
---|
636 | ++it)
|
---|
637 | {
|
---|
638 | if ((child = (*it)->findChildOrSelf(aId)))
|
---|
639 | break;
|
---|
640 | }
|
---|
641 | }
|
---|
642 |
|
---|
643 | return child;
|
---|
644 | }
|
---|
645 |
|
---|
646 | /**
|
---|
647 | * Searches for a first snapshot with the given name among children,
|
---|
648 | * grand-children, etc. of this snapshot. This snapshot itself is also included
|
---|
649 | * in the search.
|
---|
650 | *
|
---|
651 | * Caller must hold the machine lock (which protects the snapshots tree!)
|
---|
652 | */
|
---|
653 | ComObjPtr<Snapshot> Snapshot::findChildOrSelf(const Utf8Str &aName)
|
---|
654 | {
|
---|
655 | ComObjPtr<Snapshot> child;
|
---|
656 | AssertReturn(!aName.isEmpty(), child);
|
---|
657 |
|
---|
658 | AutoCaller autoCaller(this);
|
---|
659 | AssertComRC(autoCaller.rc());
|
---|
660 |
|
---|
661 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
662 |
|
---|
663 | if (m->strName == aName)
|
---|
664 | child = this;
|
---|
665 | else
|
---|
666 | {
|
---|
667 | alock.release();
|
---|
668 | for (SnapshotsList::const_iterator it = m->llChildren.begin();
|
---|
669 | it != m->llChildren.end();
|
---|
670 | ++it)
|
---|
671 | {
|
---|
672 | if ((child = (*it)->findChildOrSelf(aName)))
|
---|
673 | break;
|
---|
674 | }
|
---|
675 | }
|
---|
676 |
|
---|
677 | return child;
|
---|
678 | }
|
---|
679 |
|
---|
680 | /**
|
---|
681 | * Internal implementation for Snapshot::updateSavedStatePaths (below).
|
---|
682 | * @param aOldPath
|
---|
683 | * @param aNewPath
|
---|
684 | */
|
---|
685 | void Snapshot::updateSavedStatePathsImpl(const Utf8Str &strOldPath,
|
---|
686 | const Utf8Str &strNewPath)
|
---|
687 | {
|
---|
688 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
689 |
|
---|
690 | const Utf8Str &path = m->pMachine->mSSData->strStateFilePath;
|
---|
691 | LogFlowThisFunc(("Snap[%s].statePath={%s}\n", m->strName.c_str(), path.c_str()));
|
---|
692 |
|
---|
693 | /* state file may be NULL (for offline snapshots) */
|
---|
694 | if ( path.length()
|
---|
695 | && RTPathStartsWith(path.c_str(), strOldPath.c_str())
|
---|
696 | )
|
---|
697 | {
|
---|
698 | m->pMachine->mSSData->strStateFilePath = Utf8StrFmt("%s%s",
|
---|
699 | strNewPath.c_str(),
|
---|
700 | path.c_str() + strOldPath.length());
|
---|
701 | LogFlowThisFunc(("-> updated: {%s}\n", path.c_str()));
|
---|
702 | }
|
---|
703 |
|
---|
704 | for (SnapshotsList::const_iterator it = m->llChildren.begin();
|
---|
705 | it != m->llChildren.end();
|
---|
706 | ++it)
|
---|
707 | {
|
---|
708 | Snapshot *pChild = *it;
|
---|
709 | pChild->updateSavedStatePathsImpl(strOldPath, strNewPath);
|
---|
710 | }
|
---|
711 | }
|
---|
712 |
|
---|
713 | /**
|
---|
714 | * Returns true if this snapshot or one of its children uses the given file,
|
---|
715 | * whose path must be fully qualified, as its saved state. When invoked on a
|
---|
716 | * machine's first snapshot, this can be used to check if a saved state file
|
---|
717 | * is shared with any snapshots.
|
---|
718 | *
|
---|
719 | * Caller must hold the machine lock, which protects the snapshots tree.
|
---|
720 | *
|
---|
721 | * @param strPath
|
---|
722 | * @param pSnapshotToIgnore If != NULL, this snapshot is ignored during the checks.
|
---|
723 | * @return
|
---|
724 | */
|
---|
725 | bool Snapshot::sharesSavedStateFile(const Utf8Str &strPath,
|
---|
726 | Snapshot *pSnapshotToIgnore)
|
---|
727 | {
|
---|
728 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
729 | const Utf8Str &path = m->pMachine->mSSData->strStateFilePath;
|
---|
730 |
|
---|
731 | if (!pSnapshotToIgnore || pSnapshotToIgnore != this)
|
---|
732 | if (path.isNotEmpty())
|
---|
733 | if (path == strPath)
|
---|
734 | return true; // no need to recurse then
|
---|
735 |
|
---|
736 | // but otherwise we must check children
|
---|
737 | for (SnapshotsList::const_iterator it = m->llChildren.begin();
|
---|
738 | it != m->llChildren.end();
|
---|
739 | ++it)
|
---|
740 | {
|
---|
741 | Snapshot *pChild = *it;
|
---|
742 | if (!pSnapshotToIgnore || pSnapshotToIgnore != pChild)
|
---|
743 | if (pChild->sharesSavedStateFile(strPath, pSnapshotToIgnore))
|
---|
744 | return true;
|
---|
745 | }
|
---|
746 |
|
---|
747 | return false;
|
---|
748 | }
|
---|
749 |
|
---|
750 |
|
---|
751 | /**
|
---|
752 | * Checks if the specified path change affects the saved state file path of
|
---|
753 | * this snapshot or any of its (grand-)children and updates it accordingly.
|
---|
754 | *
|
---|
755 | * Intended to be called by Machine::openConfigLoader() only.
|
---|
756 | *
|
---|
757 | * @param aOldPath old path (full)
|
---|
758 | * @param aNewPath new path (full)
|
---|
759 | *
|
---|
760 | * @note Locks the machine (for the snapshots tree) + this object + children for writing.
|
---|
761 | */
|
---|
762 | void Snapshot::updateSavedStatePaths(const Utf8Str &strOldPath,
|
---|
763 | const Utf8Str &strNewPath)
|
---|
764 | {
|
---|
765 | LogFlowThisFunc(("aOldPath={%s} aNewPath={%s}\n", strOldPath.c_str(), strNewPath.c_str()));
|
---|
766 |
|
---|
767 | AutoCaller autoCaller(this);
|
---|
768 | AssertComRC(autoCaller.rc());
|
---|
769 |
|
---|
770 | // snapshots tree is protected by machine lock
|
---|
771 | AutoWriteLock alock(m->pMachine COMMA_LOCKVAL_SRC_POS);
|
---|
772 |
|
---|
773 | // call the implementation under the tree lock
|
---|
774 | updateSavedStatePathsImpl(strOldPath, strNewPath);
|
---|
775 | }
|
---|
776 |
|
---|
777 | /**
|
---|
778 | * Internal implementation for Snapshot::saveSnapshot (below). Caller has
|
---|
779 | * requested the snapshots tree (machine) lock.
|
---|
780 | *
|
---|
781 | * @param aNode
|
---|
782 | * @param aAttrsOnly
|
---|
783 | * @return
|
---|
784 | */
|
---|
785 | HRESULT Snapshot::saveSnapshotImpl(settings::Snapshot &data, bool aAttrsOnly)
|
---|
786 | {
|
---|
787 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
788 |
|
---|
789 | data.uuid = m->uuid;
|
---|
790 | data.strName = m->strName;
|
---|
791 | data.timestamp = m->timeStamp;
|
---|
792 | data.strDescription = m->strDescription;
|
---|
793 |
|
---|
794 | if (aAttrsOnly)
|
---|
795 | return S_OK;
|
---|
796 |
|
---|
797 | // state file (only if this snapshot is online)
|
---|
798 | if (getStateFilePath().isNotEmpty())
|
---|
799 | m->pMachine->copyPathRelativeToMachine(getStateFilePath(), data.strStateFile);
|
---|
800 | else
|
---|
801 | data.strStateFile.setNull();
|
---|
802 |
|
---|
803 | HRESULT rc = m->pMachine->saveHardware(data.hardware, &data.debugging, &data.autostart);
|
---|
804 | if (FAILED(rc)) return rc;
|
---|
805 |
|
---|
806 | rc = m->pMachine->saveStorageControllers(data.storage);
|
---|
807 | if (FAILED(rc)) return rc;
|
---|
808 |
|
---|
809 | alock.release();
|
---|
810 |
|
---|
811 | data.llChildSnapshots.clear();
|
---|
812 |
|
---|
813 | if (m->llChildren.size())
|
---|
814 | {
|
---|
815 | for (SnapshotsList::const_iterator it = m->llChildren.begin();
|
---|
816 | it != m->llChildren.end();
|
---|
817 | ++it)
|
---|
818 | {
|
---|
819 | // Use the heap to reduce the stack footprint. Each recursion needs
|
---|
820 | // over 1K, and there can be VMs with deeply nested snapshots. The
|
---|
821 | // stack can be quite small, especially with XPCOM.
|
---|
822 |
|
---|
823 | settings::Snapshot *snap = new settings::Snapshot();
|
---|
824 | rc = (*it)->saveSnapshotImpl(*snap, aAttrsOnly);
|
---|
825 | if (FAILED(rc))
|
---|
826 | {
|
---|
827 | delete snap;
|
---|
828 | return rc;
|
---|
829 | }
|
---|
830 | data.llChildSnapshots.push_back(*snap);
|
---|
831 | delete snap;
|
---|
832 | }
|
---|
833 | }
|
---|
834 |
|
---|
835 | return S_OK;
|
---|
836 | }
|
---|
837 |
|
---|
838 | /**
|
---|
839 | * Saves the given snapshot and all its children (unless \a aAttrsOnly is true).
|
---|
840 | * It is assumed that the given node is empty (unless \a aAttrsOnly is true).
|
---|
841 | *
|
---|
842 | * @param aNode <Snapshot> node to save the snapshot to.
|
---|
843 | * @param aSnapshot Snapshot to save.
|
---|
844 | * @param aAttrsOnly If true, only update user-changeable attrs.
|
---|
845 | */
|
---|
846 | HRESULT Snapshot::saveSnapshot(settings::Snapshot &data, bool aAttrsOnly)
|
---|
847 | {
|
---|
848 | // snapshots tree is protected by machine lock
|
---|
849 | AutoReadLock alock(m->pMachine COMMA_LOCKVAL_SRC_POS);
|
---|
850 |
|
---|
851 | return saveSnapshotImpl(data, aAttrsOnly);
|
---|
852 | }
|
---|
853 |
|
---|
854 | /**
|
---|
855 | * Part of the cleanup engine of Machine::Unregister().
|
---|
856 | *
|
---|
857 | * This recursively removes all medium attachments from the snapshot's machine
|
---|
858 | * and returns the snapshot's saved state file name, if any, and then calls
|
---|
859 | * uninit() on "this" itself.
|
---|
860 | *
|
---|
861 | * This recurses into children first, so the given MediaList receives child
|
---|
862 | * media first before their parents. If the caller wants to close all media,
|
---|
863 | * they should go thru the list from the beginning to the end because media
|
---|
864 | * cannot be closed if they have children.
|
---|
865 | *
|
---|
866 | * This calls uninit() on itself, so the snapshots tree (beginning with a machine's pFirstSnapshot) becomes invalid after this.
|
---|
867 | * It does not alter the main machine's snapshot pointers (pFirstSnapshot, pCurrentSnapshot).
|
---|
868 | *
|
---|
869 | * Caller must hold the machine write lock (which protects the snapshots tree!)
|
---|
870 | *
|
---|
871 | * @param writeLock Machine write lock, which can get released temporarily here.
|
---|
872 | * @param cleanupMode Cleanup mode; see Machine::detachAllMedia().
|
---|
873 | * @param llMedia List of media returned to caller, depending on cleanupMode.
|
---|
874 | * @param llFilenames
|
---|
875 | * @return
|
---|
876 | */
|
---|
877 | HRESULT Snapshot::uninitRecursively(AutoWriteLock &writeLock,
|
---|
878 | CleanupMode_T cleanupMode,
|
---|
879 | MediaList &llMedia,
|
---|
880 | std::list<Utf8Str> &llFilenames)
|
---|
881 | {
|
---|
882 | Assert(m->pMachine->isWriteLockOnCurrentThread());
|
---|
883 |
|
---|
884 | HRESULT rc = S_OK;
|
---|
885 |
|
---|
886 | // make a copy of the Guid for logging before we uninit ourselves
|
---|
887 | #ifdef LOG_ENABLED
|
---|
888 | Guid uuid = getId();
|
---|
889 | Utf8Str name = getName();
|
---|
890 | LogFlowThisFunc(("Entering for snapshot '%s' {%RTuuid}\n", name.c_str(), uuid.raw()));
|
---|
891 | #endif
|
---|
892 |
|
---|
893 | // recurse into children first so that the child media appear on
|
---|
894 | // the list first; this way caller can close the media from the
|
---|
895 | // beginning to the end because parent media can't be closed if
|
---|
896 | // they have children
|
---|
897 |
|
---|
898 | // make a copy of the children list since uninit() modifies it
|
---|
899 | SnapshotsList llChildrenCopy(m->llChildren);
|
---|
900 | for (SnapshotsList::iterator it = llChildrenCopy.begin();
|
---|
901 | it != llChildrenCopy.end();
|
---|
902 | ++it)
|
---|
903 | {
|
---|
904 | Snapshot *pChild = *it;
|
---|
905 | rc = pChild->uninitRecursively(writeLock, cleanupMode, llMedia, llFilenames);
|
---|
906 | if (FAILED(rc))
|
---|
907 | return rc;
|
---|
908 | }
|
---|
909 |
|
---|
910 | // now call detachAllMedia on the snapshot machine
|
---|
911 | rc = m->pMachine->detachAllMedia(writeLock,
|
---|
912 | this /* pSnapshot */,
|
---|
913 | cleanupMode,
|
---|
914 | llMedia);
|
---|
915 | if (FAILED(rc))
|
---|
916 | return rc;
|
---|
917 |
|
---|
918 | // report the saved state file if it's not on the list yet
|
---|
919 | if (!m->pMachine->mSSData->strStateFilePath.isEmpty())
|
---|
920 | {
|
---|
921 | bool fFound = false;
|
---|
922 | for (std::list<Utf8Str>::const_iterator it = llFilenames.begin();
|
---|
923 | it != llFilenames.end();
|
---|
924 | ++it)
|
---|
925 | {
|
---|
926 | const Utf8Str &str = *it;
|
---|
927 | if (str == m->pMachine->mSSData->strStateFilePath)
|
---|
928 | {
|
---|
929 | fFound = true;
|
---|
930 | break;
|
---|
931 | }
|
---|
932 | }
|
---|
933 | if (!fFound)
|
---|
934 | llFilenames.push_back(m->pMachine->mSSData->strStateFilePath);
|
---|
935 | }
|
---|
936 |
|
---|
937 | this->beginSnapshotDelete();
|
---|
938 | this->uninit();
|
---|
939 |
|
---|
940 | #ifdef LOG_ENABLED
|
---|
941 | LogFlowThisFunc(("Leaving for snapshot '%s' {%RTuuid}\n", name.c_str(), uuid.raw()));
|
---|
942 | #endif
|
---|
943 |
|
---|
944 | return S_OK;
|
---|
945 | }
|
---|
946 |
|
---|
947 | ////////////////////////////////////////////////////////////////////////////////
|
---|
948 | //
|
---|
949 | // SnapshotMachine implementation
|
---|
950 | //
|
---|
951 | ////////////////////////////////////////////////////////////////////////////////
|
---|
952 |
|
---|
953 | SnapshotMachine::SnapshotMachine()
|
---|
954 | : mMachine(NULL)
|
---|
955 | {}
|
---|
956 |
|
---|
957 | SnapshotMachine::~SnapshotMachine()
|
---|
958 | {}
|
---|
959 |
|
---|
960 | HRESULT SnapshotMachine::FinalConstruct()
|
---|
961 | {
|
---|
962 | LogFlowThisFunc(("\n"));
|
---|
963 |
|
---|
964 | return BaseFinalConstruct();
|
---|
965 | }
|
---|
966 |
|
---|
967 | void SnapshotMachine::FinalRelease()
|
---|
968 | {
|
---|
969 | LogFlowThisFunc(("\n"));
|
---|
970 |
|
---|
971 | uninit();
|
---|
972 |
|
---|
973 | BaseFinalRelease();
|
---|
974 | }
|
---|
975 |
|
---|
976 | /**
|
---|
977 | * Initializes the SnapshotMachine object when taking a snapshot.
|
---|
978 | *
|
---|
979 | * @param aSessionMachine machine to take a snapshot from
|
---|
980 | * @param aSnapshotId snapshot ID of this snapshot machine
|
---|
981 | * @param aStateFilePath file where the execution state will be later saved
|
---|
982 | * (or NULL for the offline snapshot)
|
---|
983 | *
|
---|
984 | * @note The aSessionMachine must be locked for writing.
|
---|
985 | */
|
---|
986 | HRESULT SnapshotMachine::init(SessionMachine *aSessionMachine,
|
---|
987 | IN_GUID aSnapshotId,
|
---|
988 | const Utf8Str &aStateFilePath)
|
---|
989 | {
|
---|
990 | LogFlowThisFuncEnter();
|
---|
991 | LogFlowThisFunc(("mName={%s}\n", aSessionMachine->mUserData->s.strName.c_str()));
|
---|
992 |
|
---|
993 | Guid l_guid(aSnapshotId);
|
---|
994 | AssertReturn(aSessionMachine && (!l_guid.isZero() && l_guid.isValid()), E_INVALIDARG);
|
---|
995 |
|
---|
996 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
997 | AutoInitSpan autoInitSpan(this);
|
---|
998 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
999 |
|
---|
1000 | AssertReturn(aSessionMachine->isWriteLockOnCurrentThread(), E_FAIL);
|
---|
1001 |
|
---|
1002 | mSnapshotId = aSnapshotId;
|
---|
1003 | ComObjPtr<Machine> pMachine = aSessionMachine->mPeer;
|
---|
1004 |
|
---|
1005 | /* mPeer stays NULL */
|
---|
1006 | /* memorize the primary Machine instance (i.e. not SessionMachine!) */
|
---|
1007 | unconst(mMachine) = pMachine;
|
---|
1008 | /* share the parent pointer */
|
---|
1009 | unconst(mParent) = pMachine->mParent;
|
---|
1010 |
|
---|
1011 | /* take the pointer to Data to share */
|
---|
1012 | mData.share(pMachine->mData);
|
---|
1013 |
|
---|
1014 | /* take the pointer to UserData to share (our UserData must always be the
|
---|
1015 | * same as Machine's data) */
|
---|
1016 | mUserData.share(pMachine->mUserData);
|
---|
1017 | /* make a private copy of all other data (recent changes from SessionMachine) */
|
---|
1018 | mHWData.attachCopy(aSessionMachine->mHWData);
|
---|
1019 | mMediaData.attachCopy(aSessionMachine->mMediaData);
|
---|
1020 |
|
---|
1021 | /* SSData is always unique for SnapshotMachine */
|
---|
1022 | mSSData.allocate();
|
---|
1023 | mSSData->strStateFilePath = aStateFilePath;
|
---|
1024 |
|
---|
1025 | HRESULT rc = S_OK;
|
---|
1026 |
|
---|
1027 | /* create copies of all shared folders (mHWData after attaching a copy
|
---|
1028 | * contains just references to original objects) */
|
---|
1029 | for (HWData::SharedFolderList::iterator it = mHWData->mSharedFolders.begin();
|
---|
1030 | it != mHWData->mSharedFolders.end();
|
---|
1031 | ++it)
|
---|
1032 | {
|
---|
1033 | ComObjPtr<SharedFolder> folder;
|
---|
1034 | folder.createObject();
|
---|
1035 | rc = folder->initCopy(this, *it);
|
---|
1036 | if (FAILED(rc)) return rc;
|
---|
1037 | *it = folder;
|
---|
1038 | }
|
---|
1039 |
|
---|
1040 | /* associate hard disks with the snapshot
|
---|
1041 | * (Machine::uninitDataAndChildObjects() will deassociate at destruction) */
|
---|
1042 | for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
|
---|
1043 | it != mMediaData->mAttachments.end();
|
---|
1044 | ++it)
|
---|
1045 | {
|
---|
1046 | MediumAttachment *pAtt = *it;
|
---|
1047 | Medium *pMedium = pAtt->getMedium();
|
---|
1048 | if (pMedium) // can be NULL for non-harddisk
|
---|
1049 | {
|
---|
1050 | rc = pMedium->addBackReference(mData->mUuid, mSnapshotId);
|
---|
1051 | AssertComRC(rc);
|
---|
1052 | }
|
---|
1053 | }
|
---|
1054 |
|
---|
1055 | /* create copies of all storage controllers (mStorageControllerData
|
---|
1056 | * after attaching a copy contains just references to original objects) */
|
---|
1057 | mStorageControllers.allocate();
|
---|
1058 | for (StorageControllerList::const_iterator
|
---|
1059 | it = aSessionMachine->mStorageControllers->begin();
|
---|
1060 | it != aSessionMachine->mStorageControllers->end();
|
---|
1061 | ++it)
|
---|
1062 | {
|
---|
1063 | ComObjPtr<StorageController> ctrl;
|
---|
1064 | ctrl.createObject();
|
---|
1065 | ctrl->initCopy(this, *it);
|
---|
1066 | mStorageControllers->push_back(ctrl);
|
---|
1067 | }
|
---|
1068 |
|
---|
1069 | /* create all other child objects that will be immutable private copies */
|
---|
1070 |
|
---|
1071 | unconst(mBIOSSettings).createObject();
|
---|
1072 | mBIOSSettings->initCopy(this, pMachine->mBIOSSettings);
|
---|
1073 |
|
---|
1074 | unconst(mVRDEServer).createObject();
|
---|
1075 | mVRDEServer->initCopy(this, pMachine->mVRDEServer);
|
---|
1076 |
|
---|
1077 | unconst(mAudioAdapter).createObject();
|
---|
1078 | mAudioAdapter->initCopy(this, pMachine->mAudioAdapter);
|
---|
1079 |
|
---|
1080 | /* create copies of all USB controllers (mUSBControllerData
|
---|
1081 | * after attaching a copy contains just references to original objects) */
|
---|
1082 | mUSBControllers.allocate();
|
---|
1083 | for (USBControllerList::const_iterator
|
---|
1084 | it = aSessionMachine->mUSBControllers->begin();
|
---|
1085 | it != aSessionMachine->mUSBControllers->end();
|
---|
1086 | ++it)
|
---|
1087 | {
|
---|
1088 | ComObjPtr<USBController> ctrl;
|
---|
1089 | ctrl.createObject();
|
---|
1090 | ctrl->initCopy(this, *it);
|
---|
1091 | mUSBControllers->push_back(ctrl);
|
---|
1092 | }
|
---|
1093 |
|
---|
1094 | unconst(mUSBDeviceFilters).createObject();
|
---|
1095 | mUSBDeviceFilters->initCopy(this, pMachine->mUSBDeviceFilters);
|
---|
1096 |
|
---|
1097 | mNetworkAdapters.resize(pMachine->mNetworkAdapters.size());
|
---|
1098 | for (ULONG slot = 0; slot < mNetworkAdapters.size(); slot++)
|
---|
1099 | {
|
---|
1100 | unconst(mNetworkAdapters[slot]).createObject();
|
---|
1101 | mNetworkAdapters[slot]->initCopy(this, pMachine->mNetworkAdapters[slot]);
|
---|
1102 | }
|
---|
1103 |
|
---|
1104 | for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); slot++)
|
---|
1105 | {
|
---|
1106 | unconst(mSerialPorts[slot]).createObject();
|
---|
1107 | mSerialPorts[slot]->initCopy(this, pMachine->mSerialPorts[slot]);
|
---|
1108 | }
|
---|
1109 |
|
---|
1110 | for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); slot++)
|
---|
1111 | {
|
---|
1112 | unconst(mParallelPorts[slot]).createObject();
|
---|
1113 | mParallelPorts[slot]->initCopy(this, pMachine->mParallelPorts[slot]);
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | unconst(mBandwidthControl).createObject();
|
---|
1117 | mBandwidthControl->initCopy(this, pMachine->mBandwidthControl);
|
---|
1118 |
|
---|
1119 | /* Confirm a successful initialization when it's the case */
|
---|
1120 | autoInitSpan.setSucceeded();
|
---|
1121 |
|
---|
1122 | LogFlowThisFuncLeave();
|
---|
1123 | return S_OK;
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 | /**
|
---|
1127 | * Initializes the SnapshotMachine object when loading from the settings file.
|
---|
1128 | *
|
---|
1129 | * @param aMachine machine the snapshot belongs to
|
---|
1130 | * @param aHWNode <Hardware> node
|
---|
1131 | * @param aHDAsNode <HardDiskAttachments> node
|
---|
1132 | * @param aSnapshotId snapshot ID of this snapshot machine
|
---|
1133 | * @param aStateFilePath file where the execution state is saved
|
---|
1134 | * (or NULL for the offline snapshot)
|
---|
1135 | *
|
---|
1136 | * @note Doesn't lock anything.
|
---|
1137 | */
|
---|
1138 | HRESULT SnapshotMachine::initFromSettings(Machine *aMachine,
|
---|
1139 | const settings::Hardware &hardware,
|
---|
1140 | const settings::Debugging *pDbg,
|
---|
1141 | const settings::Autostart *pAutostart,
|
---|
1142 | const settings::Storage &storage,
|
---|
1143 | IN_GUID aSnapshotId,
|
---|
1144 | const Utf8Str &aStateFilePath)
|
---|
1145 | {
|
---|
1146 | LogFlowThisFuncEnter();
|
---|
1147 | LogFlowThisFunc(("mName={%s}\n", aMachine->mUserData->s.strName.c_str()));
|
---|
1148 |
|
---|
1149 | Guid l_guid(aSnapshotId);
|
---|
1150 | AssertReturn(aMachine && (!l_guid.isZero() && l_guid.isValid()), E_INVALIDARG);
|
---|
1151 |
|
---|
1152 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1153 | AutoInitSpan autoInitSpan(this);
|
---|
1154 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1155 |
|
---|
1156 | /* Don't need to lock aMachine when VirtualBox is starting up */
|
---|
1157 |
|
---|
1158 | mSnapshotId = aSnapshotId;
|
---|
1159 |
|
---|
1160 | /* mPeer stays NULL */
|
---|
1161 | /* memorize the primary Machine instance (i.e. not SessionMachine!) */
|
---|
1162 | unconst(mMachine) = aMachine;
|
---|
1163 | /* share the parent pointer */
|
---|
1164 | unconst(mParent) = aMachine->mParent;
|
---|
1165 |
|
---|
1166 | /* take the pointer to Data to share */
|
---|
1167 | mData.share(aMachine->mData);
|
---|
1168 | /*
|
---|
1169 | * take the pointer to UserData to share
|
---|
1170 | * (our UserData must always be the same as Machine's data)
|
---|
1171 | */
|
---|
1172 | mUserData.share(aMachine->mUserData);
|
---|
1173 | /* allocate private copies of all other data (will be loaded from settings) */
|
---|
1174 | mHWData.allocate();
|
---|
1175 | mMediaData.allocate();
|
---|
1176 | mStorageControllers.allocate();
|
---|
1177 | mUSBControllers.allocate();
|
---|
1178 |
|
---|
1179 | /* SSData is always unique for SnapshotMachine */
|
---|
1180 | mSSData.allocate();
|
---|
1181 | mSSData->strStateFilePath = aStateFilePath;
|
---|
1182 |
|
---|
1183 | /* create all other child objects that will be immutable private copies */
|
---|
1184 |
|
---|
1185 | unconst(mBIOSSettings).createObject();
|
---|
1186 | mBIOSSettings->init(this);
|
---|
1187 |
|
---|
1188 | unconst(mVRDEServer).createObject();
|
---|
1189 | mVRDEServer->init(this);
|
---|
1190 |
|
---|
1191 | unconst(mAudioAdapter).createObject();
|
---|
1192 | mAudioAdapter->init(this);
|
---|
1193 |
|
---|
1194 | unconst(mUSBDeviceFilters).createObject();
|
---|
1195 | mUSBDeviceFilters->init(this);
|
---|
1196 |
|
---|
1197 | mNetworkAdapters.resize(Global::getMaxNetworkAdapters(mHWData->mChipsetType));
|
---|
1198 | for (ULONG slot = 0; slot < mNetworkAdapters.size(); slot++)
|
---|
1199 | {
|
---|
1200 | unconst(mNetworkAdapters[slot]).createObject();
|
---|
1201 | mNetworkAdapters[slot]->init(this, slot);
|
---|
1202 | }
|
---|
1203 |
|
---|
1204 | for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); slot++)
|
---|
1205 | {
|
---|
1206 | unconst(mSerialPorts[slot]).createObject();
|
---|
1207 | mSerialPorts[slot]->init(this, slot);
|
---|
1208 | }
|
---|
1209 |
|
---|
1210 | for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); slot++)
|
---|
1211 | {
|
---|
1212 | unconst(mParallelPorts[slot]).createObject();
|
---|
1213 | mParallelPorts[slot]->init(this, slot);
|
---|
1214 | }
|
---|
1215 |
|
---|
1216 | unconst(mBandwidthControl).createObject();
|
---|
1217 | mBandwidthControl->init(this);
|
---|
1218 |
|
---|
1219 | /* load hardware and harddisk settings */
|
---|
1220 |
|
---|
1221 | HRESULT rc = loadHardware(hardware, pDbg, pAutostart);
|
---|
1222 | if (SUCCEEDED(rc))
|
---|
1223 | rc = loadStorageControllers(storage,
|
---|
1224 | NULL, /* puuidRegistry */
|
---|
1225 | &mSnapshotId);
|
---|
1226 |
|
---|
1227 | if (SUCCEEDED(rc))
|
---|
1228 | /* commit all changes made during the initialization */
|
---|
1229 | commit(); /// @todo r=dj why do we need a commit in init?!? this is very expensive
|
---|
1230 | /// @todo r=klaus for some reason the settings loading logic backs up
|
---|
1231 | // the settings, and therefore a commit is needed. Should probably be changed.
|
---|
1232 |
|
---|
1233 | /* Confirm a successful initialization when it's the case */
|
---|
1234 | if (SUCCEEDED(rc))
|
---|
1235 | autoInitSpan.setSucceeded();
|
---|
1236 |
|
---|
1237 | LogFlowThisFuncLeave();
|
---|
1238 | return rc;
|
---|
1239 | }
|
---|
1240 |
|
---|
1241 | /**
|
---|
1242 | * Uninitializes this SnapshotMachine object.
|
---|
1243 | */
|
---|
1244 | void SnapshotMachine::uninit()
|
---|
1245 | {
|
---|
1246 | LogFlowThisFuncEnter();
|
---|
1247 |
|
---|
1248 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
1249 | AutoUninitSpan autoUninitSpan(this);
|
---|
1250 | if (autoUninitSpan.uninitDone())
|
---|
1251 | return;
|
---|
1252 |
|
---|
1253 | uninitDataAndChildObjects();
|
---|
1254 |
|
---|
1255 | /* free the essential data structure last */
|
---|
1256 | mData.free();
|
---|
1257 |
|
---|
1258 | unconst(mMachine) = NULL;
|
---|
1259 | unconst(mParent) = NULL;
|
---|
1260 | unconst(mPeer) = NULL;
|
---|
1261 |
|
---|
1262 | LogFlowThisFuncLeave();
|
---|
1263 | }
|
---|
1264 |
|
---|
1265 | /**
|
---|
1266 | * Overrides VirtualBoxBase::lockHandle() in order to share the lock handle
|
---|
1267 | * with the primary Machine instance (mMachine) if it exists.
|
---|
1268 | */
|
---|
1269 | RWLockHandle *SnapshotMachine::lockHandle() const
|
---|
1270 | {
|
---|
1271 | AssertReturn(mMachine != NULL, NULL);
|
---|
1272 | return mMachine->lockHandle();
|
---|
1273 | }
|
---|
1274 |
|
---|
1275 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1276 | //
|
---|
1277 | // SnapshotMachine public internal methods
|
---|
1278 | //
|
---|
1279 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1280 |
|
---|
1281 | /**
|
---|
1282 | * Called by the snapshot object associated with this SnapshotMachine when
|
---|
1283 | * snapshot data such as name or description is changed.
|
---|
1284 | *
|
---|
1285 | * @warning Caller must hold no locks when calling this.
|
---|
1286 | */
|
---|
1287 | HRESULT SnapshotMachine::onSnapshotChange(Snapshot *aSnapshot)
|
---|
1288 | {
|
---|
1289 | AutoMultiWriteLock2 mlock(this, aSnapshot COMMA_LOCKVAL_SRC_POS);
|
---|
1290 | Guid uuidMachine(mData->mUuid),
|
---|
1291 | uuidSnapshot(aSnapshot->getId());
|
---|
1292 | bool fNeedsGlobalSaveSettings = false;
|
---|
1293 |
|
---|
1294 | /* Flag the machine as dirty or change won't get saved. We disable the
|
---|
1295 | * modification of the current state flag, cause this snapshot data isn't
|
---|
1296 | * related to the current state. */
|
---|
1297 | mMachine->setModified(Machine::IsModified_Snapshots, false /* fAllowStateModification */);
|
---|
1298 | HRESULT rc = mMachine->saveSettings(&fNeedsGlobalSaveSettings,
|
---|
1299 | SaveS_Force); // we know we need saving, no need to check
|
---|
1300 | mlock.release();
|
---|
1301 |
|
---|
1302 | if (SUCCEEDED(rc) && fNeedsGlobalSaveSettings)
|
---|
1303 | {
|
---|
1304 | // save the global settings
|
---|
1305 | AutoWriteLock vboxlock(mParent COMMA_LOCKVAL_SRC_POS);
|
---|
1306 | rc = mParent->saveSettings();
|
---|
1307 | }
|
---|
1308 |
|
---|
1309 | /* inform callbacks */
|
---|
1310 | mParent->onSnapshotChange(uuidMachine, uuidSnapshot);
|
---|
1311 |
|
---|
1312 | return rc;
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1316 | //
|
---|
1317 | // SessionMachine task records
|
---|
1318 | //
|
---|
1319 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1320 |
|
---|
1321 | /**
|
---|
1322 | * Abstract base class for SessionMachine::RestoreSnapshotTask and
|
---|
1323 | * SessionMachine::DeleteSnapshotTask. This is necessary since
|
---|
1324 | * RTThreadCreate cannot call a method as its thread function, so
|
---|
1325 | * instead we have it call the static SessionMachine::taskHandler,
|
---|
1326 | * which can then call the handler() method in here (implemented
|
---|
1327 | * by the children).
|
---|
1328 | */
|
---|
1329 | struct SessionMachine::SnapshotTask
|
---|
1330 | {
|
---|
1331 | SnapshotTask(SessionMachine *m,
|
---|
1332 | Progress *p,
|
---|
1333 | Snapshot *s)
|
---|
1334 | : pMachine(m),
|
---|
1335 | pProgress(p),
|
---|
1336 | machineStateBackup(m->mData->mMachineState), // save the current machine state
|
---|
1337 | pSnapshot(s)
|
---|
1338 | {}
|
---|
1339 |
|
---|
1340 | void modifyBackedUpState(MachineState_T s)
|
---|
1341 | {
|
---|
1342 | *const_cast<MachineState_T*>(&machineStateBackup) = s;
|
---|
1343 | }
|
---|
1344 |
|
---|
1345 | virtual void handler() = 0;
|
---|
1346 |
|
---|
1347 | ComObjPtr<SessionMachine> pMachine;
|
---|
1348 | ComObjPtr<Progress> pProgress;
|
---|
1349 | const MachineState_T machineStateBackup;
|
---|
1350 | ComObjPtr<Snapshot> pSnapshot;
|
---|
1351 | };
|
---|
1352 |
|
---|
1353 | /** Restore snapshot state task */
|
---|
1354 | struct SessionMachine::RestoreSnapshotTask
|
---|
1355 | : public SessionMachine::SnapshotTask
|
---|
1356 | {
|
---|
1357 | RestoreSnapshotTask(SessionMachine *m,
|
---|
1358 | Progress *p,
|
---|
1359 | Snapshot *s)
|
---|
1360 | : SnapshotTask(m, p, s)
|
---|
1361 | {}
|
---|
1362 |
|
---|
1363 | void handler()
|
---|
1364 | {
|
---|
1365 | pMachine->restoreSnapshotHandler(*this);
|
---|
1366 | }
|
---|
1367 | };
|
---|
1368 |
|
---|
1369 | /** Delete snapshot task */
|
---|
1370 | struct SessionMachine::DeleteSnapshotTask
|
---|
1371 | : public SessionMachine::SnapshotTask
|
---|
1372 | {
|
---|
1373 | DeleteSnapshotTask(SessionMachine *m,
|
---|
1374 | Progress *p,
|
---|
1375 | bool fDeleteOnline,
|
---|
1376 | Snapshot *s)
|
---|
1377 | : SnapshotTask(m, p, s),
|
---|
1378 | m_fDeleteOnline(fDeleteOnline)
|
---|
1379 | {}
|
---|
1380 |
|
---|
1381 | void handler()
|
---|
1382 | {
|
---|
1383 | pMachine->deleteSnapshotHandler(*this);
|
---|
1384 | }
|
---|
1385 |
|
---|
1386 | bool m_fDeleteOnline;
|
---|
1387 | };
|
---|
1388 |
|
---|
1389 | /**
|
---|
1390 | * Static SessionMachine method that can get passed to RTThreadCreate to
|
---|
1391 | * have a thread started for a SnapshotTask. See SnapshotTask above.
|
---|
1392 | *
|
---|
1393 | * This calls either RestoreSnapshotTask::handler() or DeleteSnapshotTask::handler().
|
---|
1394 | */
|
---|
1395 |
|
---|
1396 | /* static */ DECLCALLBACK(int) SessionMachine::taskHandler(RTTHREAD /* thread */, void *pvUser)
|
---|
1397 | {
|
---|
1398 | AssertReturn(pvUser, VERR_INVALID_POINTER);
|
---|
1399 |
|
---|
1400 | SnapshotTask *task = static_cast<SnapshotTask*>(pvUser);
|
---|
1401 | task->handler();
|
---|
1402 |
|
---|
1403 | // it's our responsibility to delete the task
|
---|
1404 | delete task;
|
---|
1405 |
|
---|
1406 | return 0;
|
---|
1407 | }
|
---|
1408 |
|
---|
1409 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1410 | //
|
---|
1411 | // TakeSnapshot methods (SessionMachine and related tasks)
|
---|
1412 | //
|
---|
1413 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1414 |
|
---|
1415 | /**
|
---|
1416 | * Implementation for IInternalMachineControl::beginTakingSnapshot().
|
---|
1417 | *
|
---|
1418 | * Gets called indirectly from Console::TakeSnapshot, which creates a
|
---|
1419 | * progress object in the client and then starts a thread
|
---|
1420 | * (Console::fntTakeSnapshotWorker) which then calls this.
|
---|
1421 | *
|
---|
1422 | * In other words, the asynchronous work for taking snapshots takes place
|
---|
1423 | * on the _client_ (in the Console). This is different from restoring
|
---|
1424 | * or deleting snapshots, which start threads on the server.
|
---|
1425 | *
|
---|
1426 | * This does the server-side work of taking a snapshot: it creates differencing
|
---|
1427 | * images for all hard disks attached to the machine and then creates a
|
---|
1428 | * Snapshot object with a corresponding SnapshotMachine to save the VM settings.
|
---|
1429 | *
|
---|
1430 | * The client's fntTakeSnapshotWorker() blocks while this takes place.
|
---|
1431 | * After this returns successfully, fntTakeSnapshotWorker() will begin
|
---|
1432 | * saving the machine state to the snapshot object and reconfigure the
|
---|
1433 | * hard disks.
|
---|
1434 | *
|
---|
1435 | * When the console is done, it calls SessionMachine::EndTakingSnapshot().
|
---|
1436 | *
|
---|
1437 | * @note Locks mParent + this object for writing.
|
---|
1438 | *
|
---|
1439 | * @param aInitiator in: The console on which Console::TakeSnapshot was called.
|
---|
1440 | * @param aName in: The name for the new snapshot.
|
---|
1441 | * @param aDescription in: A description for the new snapshot.
|
---|
1442 | * @param aConsoleProgress in: The console's (client's) progress object.
|
---|
1443 | * @param fTakingSnapshotOnline in: True if an online snapshot is being taken (i.e. machine is running).
|
---|
1444 | * @param aStateFilePath out: name of file in snapshots folder to which the console should write the VM state.
|
---|
1445 | * @return
|
---|
1446 | */
|
---|
1447 | STDMETHODIMP SessionMachine::BeginTakingSnapshot(IConsole *aInitiator,
|
---|
1448 | IN_BSTR aName,
|
---|
1449 | IN_BSTR aDescription,
|
---|
1450 | IProgress *aConsoleProgress,
|
---|
1451 | BOOL fTakingSnapshotOnline,
|
---|
1452 | BSTR *aStateFilePath)
|
---|
1453 | {
|
---|
1454 | LogFlowThisFuncEnter();
|
---|
1455 |
|
---|
1456 | AssertReturn(aInitiator && aName, E_INVALIDARG);
|
---|
1457 | AssertReturn(aStateFilePath, E_POINTER);
|
---|
1458 |
|
---|
1459 | LogFlowThisFunc(("aName='%ls' fTakingSnapshotOnline=%RTbool\n", aName, fTakingSnapshotOnline));
|
---|
1460 |
|
---|
1461 | AutoCaller autoCaller(this);
|
---|
1462 | AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
|
---|
1463 |
|
---|
1464 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1465 |
|
---|
1466 | AssertReturn( !Global::IsOnlineOrTransient(mData->mMachineState)
|
---|
1467 | || mData->mMachineState == MachineState_Running
|
---|
1468 | || mData->mMachineState == MachineState_Paused, E_FAIL);
|
---|
1469 | AssertReturn(mConsoleTaskData.mLastState == MachineState_Null, E_FAIL);
|
---|
1470 | AssertReturn(mConsoleTaskData.mSnapshot.isNull(), E_FAIL);
|
---|
1471 |
|
---|
1472 | if ( mData->mCurrentSnapshot
|
---|
1473 | && mData->mCurrentSnapshot->getDepth() >= SETTINGS_SNAPSHOT_DEPTH_MAX)
|
---|
1474 | {
|
---|
1475 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1476 | tr("Cannot take another snapshot for machine '%s', because it exceeds the maximum snapshot depth limit. Please delete some earlier snapshot which you no longer need"),
|
---|
1477 | mUserData->s.strName.c_str());
|
---|
1478 | }
|
---|
1479 |
|
---|
1480 | if ( !fTakingSnapshotOnline
|
---|
1481 | && mData->mMachineState != MachineState_Saved
|
---|
1482 | )
|
---|
1483 | {
|
---|
1484 | /* save all current settings to ensure current changes are committed and
|
---|
1485 | * hard disks are fixed up */
|
---|
1486 | HRESULT rc = saveSettings(NULL);
|
---|
1487 | // no need to check for whether VirtualBox.xml needs changing since
|
---|
1488 | // we can't have a machine XML rename pending at this point
|
---|
1489 | if (FAILED(rc)) return rc;
|
---|
1490 | }
|
---|
1491 |
|
---|
1492 | /* create an ID for the snapshot */
|
---|
1493 | Guid snapshotId;
|
---|
1494 | snapshotId.create();
|
---|
1495 |
|
---|
1496 | Utf8Str strStateFilePath;
|
---|
1497 | /* stateFilePath is null when the machine is not online nor saved */
|
---|
1498 | if (fTakingSnapshotOnline)
|
---|
1499 | {
|
---|
1500 | Bstr value;
|
---|
1501 | HRESULT rc = GetExtraData(Bstr("VBoxInternal2/ForceTakeSnapshotWithoutState").raw(),
|
---|
1502 | value.asOutParam());
|
---|
1503 | if (FAILED(rc) || value != "1")
|
---|
1504 | {
|
---|
1505 | // creating a new online snapshot: we need a fresh saved state file
|
---|
1506 | composeSavedStateFilename(strStateFilePath);
|
---|
1507 | }
|
---|
1508 | }
|
---|
1509 | else if (mData->mMachineState == MachineState_Saved)
|
---|
1510 | // taking an online snapshot from machine in "saved" state: then use existing state file
|
---|
1511 | strStateFilePath = mSSData->strStateFilePath;
|
---|
1512 |
|
---|
1513 | if (strStateFilePath.isNotEmpty())
|
---|
1514 | {
|
---|
1515 | // ensure the directory for the saved state file exists
|
---|
1516 | HRESULT rc = VirtualBox::ensureFilePathExists(strStateFilePath, true /* fCreate */);
|
---|
1517 | if (FAILED(rc)) return rc;
|
---|
1518 | }
|
---|
1519 |
|
---|
1520 | /* create a snapshot machine object */
|
---|
1521 | ComObjPtr<SnapshotMachine> snapshotMachine;
|
---|
1522 | snapshotMachine.createObject();
|
---|
1523 | HRESULT rc = snapshotMachine->init(this, snapshotId.ref(), strStateFilePath);
|
---|
1524 | AssertComRCReturn(rc, rc);
|
---|
1525 |
|
---|
1526 | /* create a snapshot object */
|
---|
1527 | RTTIMESPEC time;
|
---|
1528 | ComObjPtr<Snapshot> pSnapshot;
|
---|
1529 | pSnapshot.createObject();
|
---|
1530 | rc = pSnapshot->init(mParent,
|
---|
1531 | snapshotId,
|
---|
1532 | aName,
|
---|
1533 | aDescription,
|
---|
1534 | *RTTimeNow(&time),
|
---|
1535 | snapshotMachine,
|
---|
1536 | mData->mCurrentSnapshot);
|
---|
1537 | AssertComRCReturnRC(rc);
|
---|
1538 |
|
---|
1539 | /* fill in the snapshot data */
|
---|
1540 | mConsoleTaskData.mLastState = mData->mMachineState;
|
---|
1541 | mConsoleTaskData.mSnapshot = pSnapshot;
|
---|
1542 | /// @todo in the long run the progress object should be moved to
|
---|
1543 | // VBoxSVC to avoid trouble with monitoring the progress object state
|
---|
1544 | // when the process where it lives is terminating shortly after the
|
---|
1545 | // operation completed.
|
---|
1546 |
|
---|
1547 | try
|
---|
1548 | {
|
---|
1549 | LogFlowThisFunc(("Creating differencing hard disks (online=%d)...\n",
|
---|
1550 | fTakingSnapshotOnline));
|
---|
1551 |
|
---|
1552 | // backup the media data so we can recover if things goes wrong along the day;
|
---|
1553 | // the matching commit() is in fixupMedia() during endSnapshot()
|
---|
1554 | setModified(IsModified_Storage);
|
---|
1555 | mMediaData.backup();
|
---|
1556 |
|
---|
1557 | /* Console::fntTakeSnapshotWorker and friends expects this. */
|
---|
1558 | if (mConsoleTaskData.mLastState == MachineState_Running)
|
---|
1559 | setMachineState(MachineState_LiveSnapshotting);
|
---|
1560 | else
|
---|
1561 | setMachineState(MachineState_Saving); /** @todo Confusing! Saving is used for both online and offline snapshots. */
|
---|
1562 |
|
---|
1563 | alock.release();
|
---|
1564 | /* create new differencing hard disks and attach them to this machine */
|
---|
1565 | rc = createImplicitDiffs(aConsoleProgress,
|
---|
1566 | 1, // operation weight; must be the same as in Console::TakeSnapshot()
|
---|
1567 | !!fTakingSnapshotOnline);
|
---|
1568 | if (FAILED(rc))
|
---|
1569 | throw rc;
|
---|
1570 |
|
---|
1571 | // MUST NOT save the settings or the media registry here, because
|
---|
1572 | // this causes trouble with rolling back settings if the user cancels
|
---|
1573 | // taking the snapshot after the diff images have been created.
|
---|
1574 | }
|
---|
1575 | catch (HRESULT hrc)
|
---|
1576 | {
|
---|
1577 | LogThisFunc(("Caught %Rhrc [%s]\n", hrc, Global::stringifyMachineState(mData->mMachineState) ));
|
---|
1578 | if ( mConsoleTaskData.mLastState != mData->mMachineState
|
---|
1579 | && ( mConsoleTaskData.mLastState == MachineState_Running
|
---|
1580 | ? mData->mMachineState == MachineState_LiveSnapshotting
|
---|
1581 | : mData->mMachineState == MachineState_Saving)
|
---|
1582 | )
|
---|
1583 | setMachineState(mConsoleTaskData.mLastState);
|
---|
1584 |
|
---|
1585 | pSnapshot->uninit();
|
---|
1586 | pSnapshot.setNull();
|
---|
1587 | mConsoleTaskData.mLastState = MachineState_Null;
|
---|
1588 | mConsoleTaskData.mSnapshot.setNull();
|
---|
1589 |
|
---|
1590 | rc = hrc;
|
---|
1591 |
|
---|
1592 | // @todo r=dj what with the implicit diff that we created above? this is never cleaned up
|
---|
1593 | }
|
---|
1594 |
|
---|
1595 | if (fTakingSnapshotOnline && SUCCEEDED(rc))
|
---|
1596 | strStateFilePath.cloneTo(aStateFilePath);
|
---|
1597 | else
|
---|
1598 | *aStateFilePath = NULL;
|
---|
1599 |
|
---|
1600 | LogFlowThisFunc(("LEAVE - %Rhrc [%s]\n", rc, Global::stringifyMachineState(mData->mMachineState) ));
|
---|
1601 | return rc;
|
---|
1602 | }
|
---|
1603 |
|
---|
1604 | /**
|
---|
1605 | * Implementation for IInternalMachineControl::endTakingSnapshot().
|
---|
1606 | *
|
---|
1607 | * Called by the Console when it's done saving the VM state into the snapshot
|
---|
1608 | * (if online) and reconfiguring the hard disks. See BeginTakingSnapshot() above.
|
---|
1609 | *
|
---|
1610 | * This also gets called if the console part of snapshotting failed after the
|
---|
1611 | * BeginTakingSnapshot() call, to clean up the server side.
|
---|
1612 | *
|
---|
1613 | * @note Locks VirtualBox and this object for writing.
|
---|
1614 | *
|
---|
1615 | * @param aSuccess Whether Console was successful with the client-side snapshot things.
|
---|
1616 | * @return
|
---|
1617 | */
|
---|
1618 | STDMETHODIMP SessionMachine::EndTakingSnapshot(BOOL aSuccess)
|
---|
1619 | {
|
---|
1620 | LogFlowThisFunc(("\n"));
|
---|
1621 |
|
---|
1622 | AutoCaller autoCaller(this);
|
---|
1623 | AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
|
---|
1624 |
|
---|
1625 | AutoWriteLock machineLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1626 |
|
---|
1627 | AssertReturn( !aSuccess
|
---|
1628 | || ( ( mData->mMachineState == MachineState_Saving
|
---|
1629 | || mData->mMachineState == MachineState_LiveSnapshotting)
|
---|
1630 | && mConsoleTaskData.mLastState != MachineState_Null
|
---|
1631 | && !mConsoleTaskData.mSnapshot.isNull()
|
---|
1632 | )
|
---|
1633 | , E_FAIL);
|
---|
1634 |
|
---|
1635 | /*
|
---|
1636 | * Restore the state we had when BeginTakingSnapshot() was called,
|
---|
1637 | * Console::fntTakeSnapshotWorker restores its local copy when we return.
|
---|
1638 | * If the state was Running, then let Console::fntTakeSnapshotWorker do it
|
---|
1639 | * all to avoid races.
|
---|
1640 | */
|
---|
1641 | if ( mData->mMachineState != mConsoleTaskData.mLastState
|
---|
1642 | && mConsoleTaskData.mLastState != MachineState_Running
|
---|
1643 | )
|
---|
1644 | setMachineState(mConsoleTaskData.mLastState);
|
---|
1645 |
|
---|
1646 | ComObjPtr<Snapshot> pOldFirstSnap = mData->mFirstSnapshot;
|
---|
1647 | ComObjPtr<Snapshot> pOldCurrentSnap = mData->mCurrentSnapshot;
|
---|
1648 |
|
---|
1649 | bool fOnline = Global::IsOnline(mConsoleTaskData.mLastState);
|
---|
1650 |
|
---|
1651 | HRESULT rc = S_OK;
|
---|
1652 |
|
---|
1653 | if (aSuccess)
|
---|
1654 | {
|
---|
1655 | // new snapshot becomes the current one
|
---|
1656 | mData->mCurrentSnapshot = mConsoleTaskData.mSnapshot;
|
---|
1657 |
|
---|
1658 | /* memorize the first snapshot if necessary */
|
---|
1659 | if (!mData->mFirstSnapshot)
|
---|
1660 | mData->mFirstSnapshot = mData->mCurrentSnapshot;
|
---|
1661 |
|
---|
1662 | int flSaveSettings = SaveS_Force; // do not do a deep compare in machine settings,
|
---|
1663 | // snapshots change, so we know we need to save
|
---|
1664 | if (!fOnline)
|
---|
1665 | /* the machine was powered off or saved when taking a snapshot, so
|
---|
1666 | * reset the mCurrentStateModified flag */
|
---|
1667 | flSaveSettings |= SaveS_ResetCurStateModified;
|
---|
1668 |
|
---|
1669 | rc = saveSettings(NULL, flSaveSettings);
|
---|
1670 | }
|
---|
1671 |
|
---|
1672 | if (aSuccess && SUCCEEDED(rc))
|
---|
1673 | {
|
---|
1674 | /* associate old hard disks with the snapshot and do locking/unlocking*/
|
---|
1675 | commitMedia(fOnline);
|
---|
1676 |
|
---|
1677 | /* inform callbacks */
|
---|
1678 | mParent->onSnapshotTaken(mData->mUuid,
|
---|
1679 | mConsoleTaskData.mSnapshot->getId());
|
---|
1680 | machineLock.release();
|
---|
1681 | }
|
---|
1682 | else
|
---|
1683 | {
|
---|
1684 | /* delete all differencing hard disks created (this will also attach
|
---|
1685 | * their parents back by rolling back mMediaData) */
|
---|
1686 | machineLock.release();
|
---|
1687 |
|
---|
1688 | rollbackMedia();
|
---|
1689 |
|
---|
1690 | mData->mFirstSnapshot = pOldFirstSnap; // might have been changed above
|
---|
1691 | mData->mCurrentSnapshot = pOldCurrentSnap; // might have been changed above
|
---|
1692 |
|
---|
1693 | // delete the saved state file (it might have been already created)
|
---|
1694 | if (fOnline)
|
---|
1695 | // no need to test for whether the saved state file is shared: an online
|
---|
1696 | // snapshot means that a new saved state file was created, which we must
|
---|
1697 | // clean up now
|
---|
1698 | RTFileDelete(mConsoleTaskData.mSnapshot->getStateFilePath().c_str());
|
---|
1699 | machineLock.acquire();
|
---|
1700 |
|
---|
1701 |
|
---|
1702 | mConsoleTaskData.mSnapshot->uninit();
|
---|
1703 | machineLock.release();
|
---|
1704 |
|
---|
1705 | }
|
---|
1706 |
|
---|
1707 | /* clear out the snapshot data */
|
---|
1708 | mConsoleTaskData.mLastState = MachineState_Null;
|
---|
1709 | mConsoleTaskData.mSnapshot.setNull();
|
---|
1710 |
|
---|
1711 | /* machineLock has been released already */
|
---|
1712 |
|
---|
1713 | mParent->saveModifiedRegistries();
|
---|
1714 |
|
---|
1715 | return rc;
|
---|
1716 | }
|
---|
1717 |
|
---|
1718 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1719 | //
|
---|
1720 | // RestoreSnapshot methods (SessionMachine and related tasks)
|
---|
1721 | //
|
---|
1722 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1723 |
|
---|
1724 | /**
|
---|
1725 | * Implementation for IInternalMachineControl::restoreSnapshot().
|
---|
1726 | *
|
---|
1727 | * Gets called from Console::RestoreSnapshot(), and that's basically the
|
---|
1728 | * only thing Console does. Restoring a snapshot happens entirely on the
|
---|
1729 | * server side since the machine cannot be running.
|
---|
1730 | *
|
---|
1731 | * This creates a new thread that does the work and returns a progress
|
---|
1732 | * object to the client which is then returned to the caller of
|
---|
1733 | * Console::RestoreSnapshot().
|
---|
1734 | *
|
---|
1735 | * Actual work then takes place in RestoreSnapshotTask::handler().
|
---|
1736 | *
|
---|
1737 | * @note Locks this + children objects for writing!
|
---|
1738 | *
|
---|
1739 | * @param aInitiator in: rhe console on which Console::RestoreSnapshot was called.
|
---|
1740 | * @param aSnapshot in: the snapshot to restore.
|
---|
1741 | * @param aMachineState in: client-side machine state.
|
---|
1742 | * @param aProgress out: progress object to monitor restore thread.
|
---|
1743 | * @return
|
---|
1744 | */
|
---|
1745 | STDMETHODIMP SessionMachine::RestoreSnapshot(IConsole *aInitiator,
|
---|
1746 | ISnapshot *aSnapshot,
|
---|
1747 | MachineState_T *aMachineState,
|
---|
1748 | IProgress **aProgress)
|
---|
1749 | {
|
---|
1750 | LogFlowThisFuncEnter();
|
---|
1751 |
|
---|
1752 | AssertReturn(aInitiator, E_INVALIDARG);
|
---|
1753 | AssertReturn(aSnapshot && aMachineState && aProgress, E_POINTER);
|
---|
1754 |
|
---|
1755 | AutoCaller autoCaller(this);
|
---|
1756 | AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
|
---|
1757 |
|
---|
1758 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1759 |
|
---|
1760 | // machine must not be running
|
---|
1761 | ComAssertRet(!Global::IsOnlineOrTransient(mData->mMachineState),
|
---|
1762 | E_FAIL);
|
---|
1763 |
|
---|
1764 | ComObjPtr<Snapshot> pSnapshot(static_cast<Snapshot*>(aSnapshot));
|
---|
1765 | ComObjPtr<SnapshotMachine> pSnapMachine = pSnapshot->getSnapshotMachine();
|
---|
1766 |
|
---|
1767 | // create a progress object. The number of operations is:
|
---|
1768 | // 1 (preparing) + # of hard disks + 1 (if we need to copy the saved state file) */
|
---|
1769 | LogFlowThisFunc(("Going thru snapshot machine attachments to determine progress setup\n"));
|
---|
1770 |
|
---|
1771 | ULONG ulOpCount = 1; // one for preparations
|
---|
1772 | ULONG ulTotalWeight = 1; // one for preparations
|
---|
1773 | for (MediaData::AttachmentList::iterator it = pSnapMachine->mMediaData->mAttachments.begin();
|
---|
1774 | it != pSnapMachine->mMediaData->mAttachments.end();
|
---|
1775 | ++it)
|
---|
1776 | {
|
---|
1777 | ComObjPtr<MediumAttachment> &pAttach = *it;
|
---|
1778 | AutoReadLock attachLock(pAttach COMMA_LOCKVAL_SRC_POS);
|
---|
1779 | if (pAttach->getType() == DeviceType_HardDisk)
|
---|
1780 | {
|
---|
1781 | ++ulOpCount;
|
---|
1782 | ++ulTotalWeight; // assume one MB weight for each differencing hard disk to manage
|
---|
1783 | Assert(pAttach->getMedium());
|
---|
1784 | LogFlowThisFunc(("op %d: considering hard disk attachment %s\n", ulOpCount, pAttach->getMedium()->getName().c_str()));
|
---|
1785 | }
|
---|
1786 | }
|
---|
1787 |
|
---|
1788 | ComObjPtr<Progress> pProgress;
|
---|
1789 | pProgress.createObject();
|
---|
1790 | pProgress->init(mParent, aInitiator,
|
---|
1791 | BstrFmt(tr("Restoring snapshot '%s'"), pSnapshot->getName().c_str()).raw(),
|
---|
1792 | FALSE /* aCancelable */,
|
---|
1793 | ulOpCount,
|
---|
1794 | ulTotalWeight,
|
---|
1795 | Bstr(tr("Restoring machine settings")).raw(),
|
---|
1796 | 1);
|
---|
1797 |
|
---|
1798 | /* create and start the task on a separate thread (note that it will not
|
---|
1799 | * start working until we release alock) */
|
---|
1800 | RestoreSnapshotTask *task = new RestoreSnapshotTask(this,
|
---|
1801 | pProgress,
|
---|
1802 | pSnapshot);
|
---|
1803 | int vrc = RTThreadCreate(NULL,
|
---|
1804 | taskHandler,
|
---|
1805 | (void*)task,
|
---|
1806 | 0,
|
---|
1807 | RTTHREADTYPE_MAIN_WORKER,
|
---|
1808 | 0,
|
---|
1809 | "RestoreSnap");
|
---|
1810 | if (RT_FAILURE(vrc))
|
---|
1811 | {
|
---|
1812 | delete task;
|
---|
1813 | ComAssertRCRet(vrc, E_FAIL);
|
---|
1814 | }
|
---|
1815 |
|
---|
1816 | /* set the proper machine state (note: after creating a Task instance) */
|
---|
1817 | setMachineState(MachineState_RestoringSnapshot);
|
---|
1818 |
|
---|
1819 | /* return the progress to the caller */
|
---|
1820 | pProgress.queryInterfaceTo(aProgress);
|
---|
1821 |
|
---|
1822 | /* return the new state to the caller */
|
---|
1823 | *aMachineState = mData->mMachineState;
|
---|
1824 |
|
---|
1825 | LogFlowThisFuncLeave();
|
---|
1826 |
|
---|
1827 | return S_OK;
|
---|
1828 | }
|
---|
1829 |
|
---|
1830 | /**
|
---|
1831 | * Worker method for the restore snapshot thread created by SessionMachine::RestoreSnapshot().
|
---|
1832 | * This method gets called indirectly through SessionMachine::taskHandler() which then
|
---|
1833 | * calls RestoreSnapshotTask::handler().
|
---|
1834 | *
|
---|
1835 | * The RestoreSnapshotTask contains the progress object returned to the console by
|
---|
1836 | * SessionMachine::RestoreSnapshot, through which progress and results are reported.
|
---|
1837 | *
|
---|
1838 | * @note Locks mParent + this object for writing.
|
---|
1839 | *
|
---|
1840 | * @param aTask Task data.
|
---|
1841 | */
|
---|
1842 | void SessionMachine::restoreSnapshotHandler(RestoreSnapshotTask &aTask)
|
---|
1843 | {
|
---|
1844 | LogFlowThisFuncEnter();
|
---|
1845 |
|
---|
1846 | AutoCaller autoCaller(this);
|
---|
1847 |
|
---|
1848 | LogFlowThisFunc(("state=%d\n", autoCaller.state()));
|
---|
1849 | if (!autoCaller.isOk())
|
---|
1850 | {
|
---|
1851 | /* we might have been uninitialized because the session was accidentally
|
---|
1852 | * closed by the client, so don't assert */
|
---|
1853 | aTask.pProgress->notifyComplete(E_FAIL,
|
---|
1854 | COM_IIDOF(IMachine),
|
---|
1855 | getComponentName(),
|
---|
1856 | tr("The session has been accidentally closed"));
|
---|
1857 |
|
---|
1858 | LogFlowThisFuncLeave();
|
---|
1859 | return;
|
---|
1860 | }
|
---|
1861 |
|
---|
1862 | HRESULT rc = S_OK;
|
---|
1863 |
|
---|
1864 | bool stateRestored = false;
|
---|
1865 |
|
---|
1866 | try
|
---|
1867 | {
|
---|
1868 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1869 |
|
---|
1870 | /* Discard all current changes to mUserData (name, OSType etc.).
|
---|
1871 | * Note that the machine is powered off, so there is no need to inform
|
---|
1872 | * the direct session. */
|
---|
1873 | if (mData->flModifications)
|
---|
1874 | rollback(false /* aNotify */);
|
---|
1875 |
|
---|
1876 | /* Delete the saved state file if the machine was Saved prior to this
|
---|
1877 | * operation */
|
---|
1878 | if (aTask.machineStateBackup == MachineState_Saved)
|
---|
1879 | {
|
---|
1880 | Assert(!mSSData->strStateFilePath.isEmpty());
|
---|
1881 |
|
---|
1882 | // release the saved state file AFTER unsetting the member variable
|
---|
1883 | // so that releaseSavedStateFile() won't think it's still in use
|
---|
1884 | Utf8Str strStateFile(mSSData->strStateFilePath);
|
---|
1885 | mSSData->strStateFilePath.setNull();
|
---|
1886 | releaseSavedStateFile(strStateFile, NULL /* pSnapshotToIgnore */ );
|
---|
1887 |
|
---|
1888 | aTask.modifyBackedUpState(MachineState_PoweredOff);
|
---|
1889 |
|
---|
1890 | rc = saveStateSettings(SaveSTS_StateFilePath);
|
---|
1891 | if (FAILED(rc))
|
---|
1892 | throw rc;
|
---|
1893 | }
|
---|
1894 |
|
---|
1895 | RTTIMESPEC snapshotTimeStamp;
|
---|
1896 | RTTimeSpecSetMilli(&snapshotTimeStamp, 0);
|
---|
1897 |
|
---|
1898 | {
|
---|
1899 | AutoReadLock snapshotLock(aTask.pSnapshot COMMA_LOCKVAL_SRC_POS);
|
---|
1900 |
|
---|
1901 | /* remember the timestamp of the snapshot we're restoring from */
|
---|
1902 | snapshotTimeStamp = aTask.pSnapshot->getTimeStamp();
|
---|
1903 |
|
---|
1904 | ComPtr<SnapshotMachine> pSnapshotMachine(aTask.pSnapshot->getSnapshotMachine());
|
---|
1905 |
|
---|
1906 | /* copy all hardware data from the snapshot */
|
---|
1907 | copyFrom(pSnapshotMachine);
|
---|
1908 |
|
---|
1909 | LogFlowThisFunc(("Restoring hard disks from the snapshot...\n"));
|
---|
1910 |
|
---|
1911 | // restore the attachments from the snapshot
|
---|
1912 | setModified(IsModified_Storage);
|
---|
1913 | mMediaData.backup();
|
---|
1914 | mMediaData->mAttachments.clear();
|
---|
1915 | for (MediaData::AttachmentList::const_iterator it = pSnapshotMachine->mMediaData->mAttachments.begin();
|
---|
1916 | it != pSnapshotMachine->mMediaData->mAttachments.end();
|
---|
1917 | ++it)
|
---|
1918 | {
|
---|
1919 | ComObjPtr<MediumAttachment> pAttach;
|
---|
1920 | pAttach.createObject();
|
---|
1921 | pAttach->initCopy(this, *it);
|
---|
1922 | mMediaData->mAttachments.push_back(pAttach);
|
---|
1923 | }
|
---|
1924 |
|
---|
1925 | /* release the locks before the potentially lengthy operation */
|
---|
1926 | snapshotLock.release();
|
---|
1927 | alock.release();
|
---|
1928 |
|
---|
1929 | rc = createImplicitDiffs(aTask.pProgress,
|
---|
1930 | 1,
|
---|
1931 | false /* aOnline */);
|
---|
1932 | if (FAILED(rc))
|
---|
1933 | throw rc;
|
---|
1934 |
|
---|
1935 | alock.acquire();
|
---|
1936 | snapshotLock.acquire();
|
---|
1937 |
|
---|
1938 | /* Note: on success, current (old) hard disks will be
|
---|
1939 | * deassociated/deleted on #commit() called from #saveSettings() at
|
---|
1940 | * the end. On failure, newly created implicit diffs will be
|
---|
1941 | * deleted by #rollback() at the end. */
|
---|
1942 |
|
---|
1943 | /* should not have a saved state file associated at this point */
|
---|
1944 | Assert(mSSData->strStateFilePath.isEmpty());
|
---|
1945 |
|
---|
1946 | const Utf8Str &strSnapshotStateFile = aTask.pSnapshot->getStateFilePath();
|
---|
1947 |
|
---|
1948 | if (strSnapshotStateFile.isNotEmpty())
|
---|
1949 | // online snapshot: then share the state file
|
---|
1950 | mSSData->strStateFilePath = strSnapshotStateFile;
|
---|
1951 |
|
---|
1952 | LogFlowThisFunc(("Setting new current snapshot {%RTuuid}\n", aTask.pSnapshot->getId().raw()));
|
---|
1953 | /* make the snapshot we restored from the current snapshot */
|
---|
1954 | mData->mCurrentSnapshot = aTask.pSnapshot;
|
---|
1955 | }
|
---|
1956 |
|
---|
1957 | /* grab differencing hard disks from the old attachments that will
|
---|
1958 | * become unused and need to be auto-deleted */
|
---|
1959 | std::list< ComObjPtr<MediumAttachment> > llDiffAttachmentsToDelete;
|
---|
1960 |
|
---|
1961 | for (MediaData::AttachmentList::const_iterator it = mMediaData.backedUpData()->mAttachments.begin();
|
---|
1962 | it != mMediaData.backedUpData()->mAttachments.end();
|
---|
1963 | ++it)
|
---|
1964 | {
|
---|
1965 | ComObjPtr<MediumAttachment> pAttach = *it;
|
---|
1966 | ComObjPtr<Medium> pMedium = pAttach->getMedium();
|
---|
1967 |
|
---|
1968 | /* while the hard disk is attached, the number of children or the
|
---|
1969 | * parent cannot change, so no lock */
|
---|
1970 | if ( !pMedium.isNull()
|
---|
1971 | && pAttach->getType() == DeviceType_HardDisk
|
---|
1972 | && !pMedium->getParent().isNull()
|
---|
1973 | && pMedium->getChildren().size() == 0
|
---|
1974 | )
|
---|
1975 | {
|
---|
1976 | LogFlowThisFunc(("Picked differencing image '%s' for deletion\n", pMedium->getName().c_str()));
|
---|
1977 |
|
---|
1978 | llDiffAttachmentsToDelete.push_back(pAttach);
|
---|
1979 | }
|
---|
1980 | }
|
---|
1981 |
|
---|
1982 | /* we have already deleted the current state, so set the execution
|
---|
1983 | * state accordingly no matter of the delete snapshot result */
|
---|
1984 | if (mSSData->strStateFilePath.isNotEmpty())
|
---|
1985 | setMachineState(MachineState_Saved);
|
---|
1986 | else
|
---|
1987 | setMachineState(MachineState_PoweredOff);
|
---|
1988 |
|
---|
1989 | updateMachineStateOnClient();
|
---|
1990 | stateRestored = true;
|
---|
1991 |
|
---|
1992 | /* Paranoia: no one must have saved the settings in the mean time. If
|
---|
1993 | * it happens nevertheless we'll close our eyes and continue below. */
|
---|
1994 | Assert(mMediaData.isBackedUp());
|
---|
1995 |
|
---|
1996 | /* assign the timestamp from the snapshot */
|
---|
1997 | Assert(RTTimeSpecGetMilli(&snapshotTimeStamp) != 0);
|
---|
1998 | mData->mLastStateChange = snapshotTimeStamp;
|
---|
1999 |
|
---|
2000 | // detach the current-state diffs that we detected above and build a list of
|
---|
2001 | // image files to delete _after_ saveSettings()
|
---|
2002 |
|
---|
2003 | MediaList llDiffsToDelete;
|
---|
2004 |
|
---|
2005 | for (std::list< ComObjPtr<MediumAttachment> >::iterator it = llDiffAttachmentsToDelete.begin();
|
---|
2006 | it != llDiffAttachmentsToDelete.end();
|
---|
2007 | ++it)
|
---|
2008 | {
|
---|
2009 | ComObjPtr<MediumAttachment> pAttach = *it; // guaranteed to have only attachments where medium != NULL
|
---|
2010 | ComObjPtr<Medium> pMedium = pAttach->getMedium();
|
---|
2011 |
|
---|
2012 | AutoWriteLock mlock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
2013 |
|
---|
2014 | LogFlowThisFunc(("Detaching old current state in differencing image '%s'\n", pMedium->getName().c_str()));
|
---|
2015 |
|
---|
2016 | // Normally we "detach" the medium by removing the attachment object
|
---|
2017 | // from the current machine data; saveSettings() below would then
|
---|
2018 | // compare the current machine data with the one in the backup
|
---|
2019 | // and actually call Medium::removeBackReference(). But that works only half
|
---|
2020 | // the time in our case so instead we force a detachment here:
|
---|
2021 | // remove from machine data
|
---|
2022 | mMediaData->mAttachments.remove(pAttach);
|
---|
2023 | // Remove it from the backup or else saveSettings will try to detach
|
---|
2024 | // it again and assert. The paranoia check avoids crashes (see
|
---|
2025 | // assert above) if this code is buggy and saves settings in the
|
---|
2026 | // wrong place.
|
---|
2027 | if (mMediaData.isBackedUp())
|
---|
2028 | mMediaData.backedUpData()->mAttachments.remove(pAttach);
|
---|
2029 | // then clean up backrefs
|
---|
2030 | pMedium->removeBackReference(mData->mUuid);
|
---|
2031 |
|
---|
2032 | llDiffsToDelete.push_back(pMedium);
|
---|
2033 | }
|
---|
2034 |
|
---|
2035 | // save machine settings, reset the modified flag and commit;
|
---|
2036 | bool fNeedsGlobalSaveSettings = false;
|
---|
2037 | rc = saveSettings(&fNeedsGlobalSaveSettings,
|
---|
2038 | SaveS_ResetCurStateModified);
|
---|
2039 | if (FAILED(rc))
|
---|
2040 | throw rc;
|
---|
2041 | // unconditionally add the parent registry. We do similar in SessionMachine::EndTakingSnapshot
|
---|
2042 | // (mParent->saveSettings())
|
---|
2043 |
|
---|
2044 | // release the locks before updating registry and deleting image files
|
---|
2045 | alock.release();
|
---|
2046 |
|
---|
2047 | mParent->markRegistryModified(mParent->getGlobalRegistryId());
|
---|
2048 |
|
---|
2049 | // from here on we cannot roll back on failure any more
|
---|
2050 |
|
---|
2051 | for (MediaList::iterator it = llDiffsToDelete.begin();
|
---|
2052 | it != llDiffsToDelete.end();
|
---|
2053 | ++it)
|
---|
2054 | {
|
---|
2055 | ComObjPtr<Medium> &pMedium = *it;
|
---|
2056 | LogFlowThisFunc(("Deleting old current state in differencing image '%s'\n", pMedium->getName().c_str()));
|
---|
2057 |
|
---|
2058 | HRESULT rc2 = pMedium->deleteStorage(NULL /* aProgress */,
|
---|
2059 | true /* aWait */);
|
---|
2060 | // ignore errors here because we cannot roll back after saveSettings() above
|
---|
2061 | if (SUCCEEDED(rc2))
|
---|
2062 | pMedium->uninit();
|
---|
2063 | }
|
---|
2064 | }
|
---|
2065 | catch (HRESULT aRC)
|
---|
2066 | {
|
---|
2067 | rc = aRC;
|
---|
2068 | }
|
---|
2069 |
|
---|
2070 | if (FAILED(rc))
|
---|
2071 | {
|
---|
2072 | /* preserve existing error info */
|
---|
2073 | ErrorInfoKeeper eik;
|
---|
2074 |
|
---|
2075 | /* undo all changes on failure */
|
---|
2076 | rollback(false /* aNotify */);
|
---|
2077 |
|
---|
2078 | if (!stateRestored)
|
---|
2079 | {
|
---|
2080 | /* restore the machine state */
|
---|
2081 | setMachineState(aTask.machineStateBackup);
|
---|
2082 | updateMachineStateOnClient();
|
---|
2083 | }
|
---|
2084 | }
|
---|
2085 |
|
---|
2086 | mParent->saveModifiedRegistries();
|
---|
2087 |
|
---|
2088 | /* set the result (this will try to fetch current error info on failure) */
|
---|
2089 | aTask.pProgress->notifyComplete(rc);
|
---|
2090 |
|
---|
2091 | if (SUCCEEDED(rc))
|
---|
2092 | mParent->onSnapshotDeleted(mData->mUuid, Guid());
|
---|
2093 |
|
---|
2094 | LogFlowThisFunc(("Done restoring snapshot (rc=%08X)\n", rc));
|
---|
2095 |
|
---|
2096 | LogFlowThisFuncLeave();
|
---|
2097 | }
|
---|
2098 |
|
---|
2099 | ////////////////////////////////////////////////////////////////////////////////
|
---|
2100 | //
|
---|
2101 | // DeleteSnapshot methods (SessionMachine and related tasks)
|
---|
2102 | //
|
---|
2103 | ////////////////////////////////////////////////////////////////////////////////
|
---|
2104 |
|
---|
2105 | /**
|
---|
2106 | * Implementation for IInternalMachineControl::deleteSnapshot().
|
---|
2107 | *
|
---|
2108 | * Gets called from Console::DeleteSnapshot(), and that's basically the
|
---|
2109 | * only thing Console does initially. Deleting a snapshot happens entirely on
|
---|
2110 | * the server side if the machine is not running, and if it is running then
|
---|
2111 | * the individual merges are done via internal session callbacks.
|
---|
2112 | *
|
---|
2113 | * This creates a new thread that does the work and returns a progress
|
---|
2114 | * object to the client which is then returned to the caller of
|
---|
2115 | * Console::DeleteSnapshot().
|
---|
2116 | *
|
---|
2117 | * Actual work then takes place in DeleteSnapshotTask::handler().
|
---|
2118 | *
|
---|
2119 | * @note Locks mParent + this + children objects for writing!
|
---|
2120 | */
|
---|
2121 | STDMETHODIMP SessionMachine::DeleteSnapshot(IConsole *aInitiator,
|
---|
2122 | IN_BSTR aStartId,
|
---|
2123 | IN_BSTR aEndId,
|
---|
2124 | BOOL fDeleteAllChildren,
|
---|
2125 | MachineState_T *aMachineState,
|
---|
2126 | IProgress **aProgress)
|
---|
2127 | {
|
---|
2128 | LogFlowThisFuncEnter();
|
---|
2129 |
|
---|
2130 | Guid startId(aStartId);
|
---|
2131 | Guid endId(aEndId);
|
---|
2132 |
|
---|
2133 | AssertReturn(aInitiator && !startId.isZero() && !endId.isZero() && startId.isValid() && endId.isValid(), E_INVALIDARG);
|
---|
2134 |
|
---|
2135 | AssertReturn(aMachineState && aProgress, E_POINTER);
|
---|
2136 |
|
---|
2137 | /** @todo implement the "and all children" and "range" variants */
|
---|
2138 | if (fDeleteAllChildren || startId != endId)
|
---|
2139 | ReturnComNotImplemented();
|
---|
2140 |
|
---|
2141 | AutoCaller autoCaller(this);
|
---|
2142 | AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
|
---|
2143 |
|
---|
2144 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2145 |
|
---|
2146 | // be very picky about machine states
|
---|
2147 | if ( Global::IsOnlineOrTransient(mData->mMachineState)
|
---|
2148 | && mData->mMachineState != MachineState_PoweredOff
|
---|
2149 | && mData->mMachineState != MachineState_Saved
|
---|
2150 | && mData->mMachineState != MachineState_Teleported
|
---|
2151 | && mData->mMachineState != MachineState_Aborted
|
---|
2152 | && mData->mMachineState != MachineState_Running
|
---|
2153 | && mData->mMachineState != MachineState_Paused)
|
---|
2154 | return setError(VBOX_E_INVALID_VM_STATE,
|
---|
2155 | tr("Invalid machine state: %s"),
|
---|
2156 | Global::stringifyMachineState(mData->mMachineState));
|
---|
2157 |
|
---|
2158 | ComObjPtr<Snapshot> pSnapshot;
|
---|
2159 | HRESULT rc = findSnapshotById(startId, pSnapshot, true /* aSetError */);
|
---|
2160 | if (FAILED(rc)) return rc;
|
---|
2161 |
|
---|
2162 | AutoWriteLock snapshotLock(pSnapshot COMMA_LOCKVAL_SRC_POS);
|
---|
2163 |
|
---|
2164 | size_t childrenCount = pSnapshot->getChildrenCount();
|
---|
2165 | if (childrenCount > 1)
|
---|
2166 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2167 | tr("Snapshot '%s' of the machine '%s' cannot be deleted, because it has %d child snapshots, which is more than the one snapshot allowed for deletion"),
|
---|
2168 | pSnapshot->getName().c_str(),
|
---|
2169 | mUserData->s.strName.c_str(),
|
---|
2170 | childrenCount);
|
---|
2171 |
|
---|
2172 | /* If the snapshot being deleted is the current one, ensure current
|
---|
2173 | * settings are committed and saved.
|
---|
2174 | */
|
---|
2175 | if (pSnapshot == mData->mCurrentSnapshot)
|
---|
2176 | {
|
---|
2177 | if (mData->flModifications)
|
---|
2178 | {
|
---|
2179 | rc = saveSettings(NULL);
|
---|
2180 | // no need to change for whether VirtualBox.xml needs saving since
|
---|
2181 | // we can't have a machine XML rename pending at this point
|
---|
2182 | if (FAILED(rc)) return rc;
|
---|
2183 | }
|
---|
2184 | }
|
---|
2185 |
|
---|
2186 | ComObjPtr<SnapshotMachine> pSnapMachine = pSnapshot->getSnapshotMachine();
|
---|
2187 |
|
---|
2188 | /* create a progress object. The number of operations is:
|
---|
2189 | * 1 (preparing) + 1 if the snapshot is online + # of normal hard disks
|
---|
2190 | */
|
---|
2191 | LogFlowThisFunc(("Going thru snapshot machine attachments to determine progress setup\n"));
|
---|
2192 |
|
---|
2193 | ULONG ulOpCount = 1; // one for preparations
|
---|
2194 | ULONG ulTotalWeight = 1; // one for preparations
|
---|
2195 |
|
---|
2196 | if (pSnapshot->getStateFilePath().length())
|
---|
2197 | {
|
---|
2198 | ++ulOpCount;
|
---|
2199 | ++ulTotalWeight; // assume 1 MB for deleting the state file
|
---|
2200 | }
|
---|
2201 |
|
---|
2202 | // count normal hard disks and add their sizes to the weight
|
---|
2203 | for (MediaData::AttachmentList::iterator it = pSnapMachine->mMediaData->mAttachments.begin();
|
---|
2204 | it != pSnapMachine->mMediaData->mAttachments.end();
|
---|
2205 | ++it)
|
---|
2206 | {
|
---|
2207 | ComObjPtr<MediumAttachment> &pAttach = *it;
|
---|
2208 | AutoReadLock attachLock(pAttach COMMA_LOCKVAL_SRC_POS);
|
---|
2209 | if (pAttach->getType() == DeviceType_HardDisk)
|
---|
2210 | {
|
---|
2211 | ComObjPtr<Medium> pHD = pAttach->getMedium();
|
---|
2212 | Assert(pHD);
|
---|
2213 | AutoReadLock mlock(pHD COMMA_LOCKVAL_SRC_POS);
|
---|
2214 |
|
---|
2215 | MediumType_T type = pHD->getType();
|
---|
2216 | // writethrough and shareable images are unaffected by snapshots,
|
---|
2217 | // so do nothing for them
|
---|
2218 | if ( type != MediumType_Writethrough
|
---|
2219 | && type != MediumType_Shareable
|
---|
2220 | && type != MediumType_Readonly)
|
---|
2221 | {
|
---|
2222 | // normal or immutable media need attention
|
---|
2223 | ++ulOpCount;
|
---|
2224 | ulTotalWeight += (ULONG)(pHD->getSize() / _1M);
|
---|
2225 | }
|
---|
2226 | LogFlowThisFunc(("op %d: considering hard disk attachment %s\n", ulOpCount, pHD->getName().c_str()));
|
---|
2227 | }
|
---|
2228 | }
|
---|
2229 |
|
---|
2230 | ComObjPtr<Progress> pProgress;
|
---|
2231 | pProgress.createObject();
|
---|
2232 | pProgress->init(mParent, aInitiator,
|
---|
2233 | BstrFmt(tr("Deleting snapshot '%s'"), pSnapshot->getName().c_str()).raw(),
|
---|
2234 | FALSE /* aCancelable */,
|
---|
2235 | ulOpCount,
|
---|
2236 | ulTotalWeight,
|
---|
2237 | Bstr(tr("Setting up")).raw(),
|
---|
2238 | 1);
|
---|
2239 |
|
---|
2240 | bool fDeleteOnline = ( (mData->mMachineState == MachineState_Running)
|
---|
2241 | || (mData->mMachineState == MachineState_Paused));
|
---|
2242 |
|
---|
2243 | /* create and start the task on a separate thread */
|
---|
2244 | DeleteSnapshotTask *task = new DeleteSnapshotTask(this, pProgress,
|
---|
2245 | fDeleteOnline, pSnapshot);
|
---|
2246 | int vrc = RTThreadCreate(NULL,
|
---|
2247 | taskHandler,
|
---|
2248 | (void*)task,
|
---|
2249 | 0,
|
---|
2250 | RTTHREADTYPE_MAIN_WORKER,
|
---|
2251 | 0,
|
---|
2252 | "DeleteSnapshot");
|
---|
2253 | if (RT_FAILURE(vrc))
|
---|
2254 | {
|
---|
2255 | delete task;
|
---|
2256 | return E_FAIL;
|
---|
2257 | }
|
---|
2258 |
|
---|
2259 | // the task might start running but will block on acquiring the machine's write lock
|
---|
2260 | // which we acquired above; once this function leaves, the task will be unblocked;
|
---|
2261 | // set the proper machine state here now (note: after creating a Task instance)
|
---|
2262 | if (mData->mMachineState == MachineState_Running)
|
---|
2263 | setMachineState(MachineState_DeletingSnapshotOnline);
|
---|
2264 | else if (mData->mMachineState == MachineState_Paused)
|
---|
2265 | setMachineState(MachineState_DeletingSnapshotPaused);
|
---|
2266 | else
|
---|
2267 | setMachineState(MachineState_DeletingSnapshot);
|
---|
2268 |
|
---|
2269 | /* return the progress to the caller */
|
---|
2270 | pProgress.queryInterfaceTo(aProgress);
|
---|
2271 |
|
---|
2272 | /* return the new state to the caller */
|
---|
2273 | *aMachineState = mData->mMachineState;
|
---|
2274 |
|
---|
2275 | LogFlowThisFuncLeave();
|
---|
2276 |
|
---|
2277 | return S_OK;
|
---|
2278 | }
|
---|
2279 |
|
---|
2280 | /**
|
---|
2281 | * Helper struct for SessionMachine::deleteSnapshotHandler().
|
---|
2282 | */
|
---|
2283 | struct MediumDeleteRec
|
---|
2284 | {
|
---|
2285 | MediumDeleteRec()
|
---|
2286 | : mfNeedsOnlineMerge(false),
|
---|
2287 | mpMediumLockList(NULL)
|
---|
2288 | {}
|
---|
2289 |
|
---|
2290 | MediumDeleteRec(const ComObjPtr<Medium> &aHd,
|
---|
2291 | const ComObjPtr<Medium> &aSource,
|
---|
2292 | const ComObjPtr<Medium> &aTarget,
|
---|
2293 | const ComObjPtr<MediumAttachment> &aOnlineMediumAttachment,
|
---|
2294 | bool fMergeForward,
|
---|
2295 | const ComObjPtr<Medium> &aParentForTarget,
|
---|
2296 | const MediaList &aChildrenToReparent,
|
---|
2297 | bool fNeedsOnlineMerge,
|
---|
2298 | MediumLockList *aMediumLockList)
|
---|
2299 | : mpHD(aHd),
|
---|
2300 | mpSource(aSource),
|
---|
2301 | mpTarget(aTarget),
|
---|
2302 | mpOnlineMediumAttachment(aOnlineMediumAttachment),
|
---|
2303 | mfMergeForward(fMergeForward),
|
---|
2304 | mpParentForTarget(aParentForTarget),
|
---|
2305 | mChildrenToReparent(aChildrenToReparent),
|
---|
2306 | mfNeedsOnlineMerge(fNeedsOnlineMerge),
|
---|
2307 | mpMediumLockList(aMediumLockList)
|
---|
2308 | {}
|
---|
2309 |
|
---|
2310 | MediumDeleteRec(const ComObjPtr<Medium> &aHd,
|
---|
2311 | const ComObjPtr<Medium> &aSource,
|
---|
2312 | const ComObjPtr<Medium> &aTarget,
|
---|
2313 | const ComObjPtr<MediumAttachment> &aOnlineMediumAttachment,
|
---|
2314 | bool fMergeForward,
|
---|
2315 | const ComObjPtr<Medium> &aParentForTarget,
|
---|
2316 | const MediaList &aChildrenToReparent,
|
---|
2317 | bool fNeedsOnlineMerge,
|
---|
2318 | MediumLockList *aMediumLockList,
|
---|
2319 | const Guid &aMachineId,
|
---|
2320 | const Guid &aSnapshotId)
|
---|
2321 | : mpHD(aHd),
|
---|
2322 | mpSource(aSource),
|
---|
2323 | mpTarget(aTarget),
|
---|
2324 | mpOnlineMediumAttachment(aOnlineMediumAttachment),
|
---|
2325 | mfMergeForward(fMergeForward),
|
---|
2326 | mpParentForTarget(aParentForTarget),
|
---|
2327 | mChildrenToReparent(aChildrenToReparent),
|
---|
2328 | mfNeedsOnlineMerge(fNeedsOnlineMerge),
|
---|
2329 | mpMediumLockList(aMediumLockList),
|
---|
2330 | mMachineId(aMachineId),
|
---|
2331 | mSnapshotId(aSnapshotId)
|
---|
2332 | {}
|
---|
2333 |
|
---|
2334 | ComObjPtr<Medium> mpHD;
|
---|
2335 | ComObjPtr<Medium> mpSource;
|
---|
2336 | ComObjPtr<Medium> mpTarget;
|
---|
2337 | ComObjPtr<MediumAttachment> mpOnlineMediumAttachment;
|
---|
2338 | bool mfMergeForward;
|
---|
2339 | ComObjPtr<Medium> mpParentForTarget;
|
---|
2340 | MediaList mChildrenToReparent;
|
---|
2341 | bool mfNeedsOnlineMerge;
|
---|
2342 | MediumLockList *mpMediumLockList;
|
---|
2343 | /* these are for reattaching the hard disk in case of a failure: */
|
---|
2344 | Guid mMachineId;
|
---|
2345 | Guid mSnapshotId;
|
---|
2346 | };
|
---|
2347 |
|
---|
2348 | typedef std::list<MediumDeleteRec> MediumDeleteRecList;
|
---|
2349 |
|
---|
2350 | /**
|
---|
2351 | * Worker method for the delete snapshot thread created by
|
---|
2352 | * SessionMachine::DeleteSnapshot(). This method gets called indirectly
|
---|
2353 | * through SessionMachine::taskHandler() which then calls
|
---|
2354 | * DeleteSnapshotTask::handler().
|
---|
2355 | *
|
---|
2356 | * The DeleteSnapshotTask contains the progress object returned to the console
|
---|
2357 | * by SessionMachine::DeleteSnapshot, through which progress and results are
|
---|
2358 | * reported.
|
---|
2359 | *
|
---|
2360 | * SessionMachine::DeleteSnapshot() has set the machine state to
|
---|
2361 | * MachineState_DeletingSnapshot right after creating this task. Since we block
|
---|
2362 | * on the machine write lock at the beginning, once that has been acquired, we
|
---|
2363 | * can assume that the machine state is indeed that.
|
---|
2364 | *
|
---|
2365 | * @note Locks the machine + the snapshot + the media tree for writing!
|
---|
2366 | *
|
---|
2367 | * @param aTask Task data.
|
---|
2368 | */
|
---|
2369 |
|
---|
2370 | void SessionMachine::deleteSnapshotHandler(DeleteSnapshotTask &aTask)
|
---|
2371 | {
|
---|
2372 | LogFlowThisFuncEnter();
|
---|
2373 |
|
---|
2374 | AutoCaller autoCaller(this);
|
---|
2375 |
|
---|
2376 | LogFlowThisFunc(("state=%d\n", autoCaller.state()));
|
---|
2377 | if (!autoCaller.isOk())
|
---|
2378 | {
|
---|
2379 | /* we might have been uninitialized because the session was accidentally
|
---|
2380 | * closed by the client, so don't assert */
|
---|
2381 | aTask.pProgress->notifyComplete(E_FAIL,
|
---|
2382 | COM_IIDOF(IMachine),
|
---|
2383 | getComponentName(),
|
---|
2384 | tr("The session has been accidentally closed"));
|
---|
2385 | LogFlowThisFuncLeave();
|
---|
2386 | return;
|
---|
2387 | }
|
---|
2388 |
|
---|
2389 | HRESULT rc = S_OK;
|
---|
2390 | MediumDeleteRecList toDelete;
|
---|
2391 | Guid snapshotId;
|
---|
2392 |
|
---|
2393 | try
|
---|
2394 | {
|
---|
2395 | /* Locking order: */
|
---|
2396 | AutoMultiWriteLock2 multiLock(this->lockHandle(), // machine
|
---|
2397 | aTask.pSnapshot->lockHandle() // snapshot
|
---|
2398 | COMMA_LOCKVAL_SRC_POS);
|
---|
2399 | // once we have this lock, we know that SessionMachine::DeleteSnapshot()
|
---|
2400 | // has exited after setting the machine state to MachineState_DeletingSnapshot
|
---|
2401 |
|
---|
2402 | AutoWriteLock treeLock(mParent->getMediaTreeLockHandle()
|
---|
2403 | COMMA_LOCKVAL_SRC_POS);
|
---|
2404 |
|
---|
2405 | ComObjPtr<SnapshotMachine> pSnapMachine = aTask.pSnapshot->getSnapshotMachine();
|
---|
2406 | // no need to lock the snapshot machine since it is const by definition
|
---|
2407 | Guid machineId = pSnapMachine->getId();
|
---|
2408 |
|
---|
2409 | // save the snapshot ID (for callbacks)
|
---|
2410 | snapshotId = aTask.pSnapshot->getId();
|
---|
2411 |
|
---|
2412 | // first pass:
|
---|
2413 | LogFlowThisFunc(("1: Checking hard disk merge prerequisites...\n"));
|
---|
2414 |
|
---|
2415 | // Go thru the attachments of the snapshot machine (the media in here
|
---|
2416 | // point to the disk states _before_ the snapshot was taken, i.e. the
|
---|
2417 | // state we're restoring to; for each such medium, we will need to
|
---|
2418 | // merge it with its one and only child (the diff image holding the
|
---|
2419 | // changes written after the snapshot was taken).
|
---|
2420 | for (MediaData::AttachmentList::iterator it = pSnapMachine->mMediaData->mAttachments.begin();
|
---|
2421 | it != pSnapMachine->mMediaData->mAttachments.end();
|
---|
2422 | ++it)
|
---|
2423 | {
|
---|
2424 | ComObjPtr<MediumAttachment> &pAttach = *it;
|
---|
2425 | AutoReadLock attachLock(pAttach COMMA_LOCKVAL_SRC_POS);
|
---|
2426 | if (pAttach->getType() != DeviceType_HardDisk)
|
---|
2427 | continue;
|
---|
2428 |
|
---|
2429 | ComObjPtr<Medium> pHD = pAttach->getMedium();
|
---|
2430 | Assert(!pHD.isNull());
|
---|
2431 |
|
---|
2432 | {
|
---|
2433 | // writethrough, shareable and readonly images are
|
---|
2434 | // unaffected by snapshots, skip them
|
---|
2435 | AutoReadLock medlock(pHD COMMA_LOCKVAL_SRC_POS);
|
---|
2436 | MediumType_T type = pHD->getType();
|
---|
2437 | if ( type == MediumType_Writethrough
|
---|
2438 | || type == MediumType_Shareable
|
---|
2439 | || type == MediumType_Readonly)
|
---|
2440 | continue;
|
---|
2441 | }
|
---|
2442 |
|
---|
2443 | #ifdef DEBUG
|
---|
2444 | pHD->dumpBackRefs();
|
---|
2445 | #endif
|
---|
2446 |
|
---|
2447 | // needs to be merged with child or deleted, check prerequisites
|
---|
2448 | ComObjPtr<Medium> pTarget;
|
---|
2449 | ComObjPtr<Medium> pSource;
|
---|
2450 | bool fMergeForward = false;
|
---|
2451 | ComObjPtr<Medium> pParentForTarget;
|
---|
2452 | MediaList childrenToReparent;
|
---|
2453 | bool fNeedsOnlineMerge = false;
|
---|
2454 | bool fOnlineMergePossible = aTask.m_fDeleteOnline;
|
---|
2455 | MediumLockList *pMediumLockList = NULL;
|
---|
2456 | MediumLockList *pVMMALockList = NULL;
|
---|
2457 | ComObjPtr<MediumAttachment> pOnlineMediumAttachment;
|
---|
2458 | if (fOnlineMergePossible)
|
---|
2459 | {
|
---|
2460 | // Look up the corresponding medium attachment in the currently
|
---|
2461 | // running VM. Any failure prevents a live merge. Could be made
|
---|
2462 | // a tad smarter by trying a few candidates, so that e.g. disks
|
---|
2463 | // which are simply moved to a different controller slot do not
|
---|
2464 | // prevent online merging in general.
|
---|
2465 | pOnlineMediumAttachment =
|
---|
2466 | findAttachment(mMediaData->mAttachments,
|
---|
2467 | pAttach->getControllerName().raw(),
|
---|
2468 | pAttach->getPort(),
|
---|
2469 | pAttach->getDevice());
|
---|
2470 | if (pOnlineMediumAttachment)
|
---|
2471 | {
|
---|
2472 | rc = mData->mSession.mLockedMedia.Get(pOnlineMediumAttachment,
|
---|
2473 | pVMMALockList);
|
---|
2474 | if (FAILED(rc))
|
---|
2475 | fOnlineMergePossible = false;
|
---|
2476 | }
|
---|
2477 | else
|
---|
2478 | fOnlineMergePossible = false;
|
---|
2479 | }
|
---|
2480 |
|
---|
2481 | // no need to hold the lock any longer
|
---|
2482 | attachLock.release();
|
---|
2483 |
|
---|
2484 | treeLock.release();
|
---|
2485 | rc = prepareDeleteSnapshotMedium(pHD, machineId, snapshotId,
|
---|
2486 | fOnlineMergePossible,
|
---|
2487 | pVMMALockList, pSource, pTarget,
|
---|
2488 | fMergeForward, pParentForTarget,
|
---|
2489 | childrenToReparent,
|
---|
2490 | fNeedsOnlineMerge,
|
---|
2491 | pMediumLockList);
|
---|
2492 | treeLock.acquire();
|
---|
2493 | if (FAILED(rc))
|
---|
2494 | throw rc;
|
---|
2495 |
|
---|
2496 | // For simplicity, prepareDeleteSnapshotMedium selects the merge
|
---|
2497 | // direction in the following way: we merge pHD onto its child
|
---|
2498 | // (forward merge), not the other way round, because that saves us
|
---|
2499 | // from unnecessarily shuffling around the attachments for the
|
---|
2500 | // machine that follows the snapshot (next snapshot or current
|
---|
2501 | // state), unless it's a base image. Backwards merges of the first
|
---|
2502 | // snapshot into the base image is essential, as it ensures that
|
---|
2503 | // when all snapshots are deleted the only remaining image is a
|
---|
2504 | // base image. Important e.g. for medium formats which do not have
|
---|
2505 | // a file representation such as iSCSI.
|
---|
2506 |
|
---|
2507 | // a couple paranoia checks for backward merges
|
---|
2508 | if (pMediumLockList != NULL && !fMergeForward)
|
---|
2509 | {
|
---|
2510 | // parent is null -> this disk is a base hard disk: we will
|
---|
2511 | // then do a backward merge, i.e. merge its only child onto the
|
---|
2512 | // base disk. Here we need then to update the attachment that
|
---|
2513 | // refers to the child and have it point to the parent instead
|
---|
2514 | Assert(pHD->getChildren().size() == 1);
|
---|
2515 |
|
---|
2516 | ComObjPtr<Medium> pReplaceHD = pHD->getChildren().front();
|
---|
2517 |
|
---|
2518 | ComAssertThrow(pReplaceHD == pSource, E_FAIL);
|
---|
2519 | }
|
---|
2520 |
|
---|
2521 | Guid replaceMachineId;
|
---|
2522 | Guid replaceSnapshotId;
|
---|
2523 |
|
---|
2524 | const Guid *pReplaceMachineId = pSource->getFirstMachineBackrefId();
|
---|
2525 | // minimal sanity checking
|
---|
2526 | Assert(!pReplaceMachineId || *pReplaceMachineId == mData->mUuid);
|
---|
2527 | if (pReplaceMachineId)
|
---|
2528 | replaceMachineId = *pReplaceMachineId;
|
---|
2529 |
|
---|
2530 | const Guid *pSnapshotId = pSource->getFirstMachineBackrefSnapshotId();
|
---|
2531 | if (pSnapshotId)
|
---|
2532 | replaceSnapshotId = *pSnapshotId;
|
---|
2533 |
|
---|
2534 | if (replaceMachineId.isValid() && !replaceMachineId.isZero())
|
---|
2535 | {
|
---|
2536 | // Adjust the backreferences, otherwise merging will assert.
|
---|
2537 | // Note that the medium attachment object stays associated
|
---|
2538 | // with the snapshot until the merge was successful.
|
---|
2539 | HRESULT rc2 = S_OK;
|
---|
2540 | rc2 = pSource->removeBackReference(replaceMachineId, replaceSnapshotId);
|
---|
2541 | AssertComRC(rc2);
|
---|
2542 |
|
---|
2543 | toDelete.push_back(MediumDeleteRec(pHD, pSource, pTarget,
|
---|
2544 | pOnlineMediumAttachment,
|
---|
2545 | fMergeForward,
|
---|
2546 | pParentForTarget,
|
---|
2547 | childrenToReparent,
|
---|
2548 | fNeedsOnlineMerge,
|
---|
2549 | pMediumLockList,
|
---|
2550 | replaceMachineId,
|
---|
2551 | replaceSnapshotId));
|
---|
2552 | }
|
---|
2553 | else
|
---|
2554 | toDelete.push_back(MediumDeleteRec(pHD, pSource, pTarget,
|
---|
2555 | pOnlineMediumAttachment,
|
---|
2556 | fMergeForward,
|
---|
2557 | pParentForTarget,
|
---|
2558 | childrenToReparent,
|
---|
2559 | fNeedsOnlineMerge,
|
---|
2560 | pMediumLockList));
|
---|
2561 | }
|
---|
2562 |
|
---|
2563 | {
|
---|
2564 | /*check available place on the storage*/
|
---|
2565 | RTFOFF pcbTotal = 0;
|
---|
2566 | RTFOFF pcbFree = 0;
|
---|
2567 | uint32_t pcbBlock = 0;
|
---|
2568 | uint32_t pcbSector = 0;
|
---|
2569 | std::multimap<uint32_t,uint64_t> neededStorageFreeSpace;
|
---|
2570 | std::map<uint32_t,const char*> serialMapToStoragePath;
|
---|
2571 |
|
---|
2572 | MediumDeleteRecList::const_iterator it_md = toDelete.begin();
|
---|
2573 |
|
---|
2574 | while (it_md != toDelete.end())
|
---|
2575 | {
|
---|
2576 | uint64_t diskSize = 0;
|
---|
2577 | uint32_t pu32Serial = 0;
|
---|
2578 | ComObjPtr<Medium> pSource_local = it_md->mpSource;
|
---|
2579 | ComObjPtr<Medium> pTarget_local = it_md->mpTarget;
|
---|
2580 | ComPtr<IMediumFormat> pTargetFormat;
|
---|
2581 |
|
---|
2582 | {
|
---|
2583 | if ( pSource_local.isNull()
|
---|
2584 | || pSource_local == pTarget_local)
|
---|
2585 | {
|
---|
2586 | ++it_md;
|
---|
2587 | continue;
|
---|
2588 | }
|
---|
2589 | }
|
---|
2590 |
|
---|
2591 | rc = pTarget_local->COMGETTER(MediumFormat)(pTargetFormat.asOutParam());
|
---|
2592 | if (FAILED(rc))
|
---|
2593 | throw rc;
|
---|
2594 |
|
---|
2595 | if(pTarget_local->isMediumFormatFile())
|
---|
2596 | {
|
---|
2597 | int vrc = RTFsQuerySerial(pTarget_local->getLocationFull().c_str(), &pu32Serial);
|
---|
2598 | if (RT_FAILURE(vrc))
|
---|
2599 | {
|
---|
2600 | rc = setError(E_FAIL,
|
---|
2601 | tr(" Unable to merge storage '%s'. Can't get storage UID "),
|
---|
2602 | pTarget_local->getLocationFull().c_str());
|
---|
2603 | throw rc;
|
---|
2604 | }
|
---|
2605 |
|
---|
2606 | pSource_local->COMGETTER(Size)((LONG64*)&diskSize);
|
---|
2607 |
|
---|
2608 | /* store needed free space in multimap */
|
---|
2609 | neededStorageFreeSpace.insert(std::make_pair(pu32Serial,diskSize));
|
---|
2610 | /* linking storage UID with snapshot path, it is a helper container (just for easy finding needed path) */
|
---|
2611 | serialMapToStoragePath.insert(std::make_pair(pu32Serial,pTarget_local->getLocationFull().c_str()));
|
---|
2612 | }
|
---|
2613 |
|
---|
2614 | ++it_md;
|
---|
2615 | }
|
---|
2616 |
|
---|
2617 | while (!neededStorageFreeSpace.empty())
|
---|
2618 | {
|
---|
2619 | std::pair<std::multimap<uint32_t,uint64_t>::iterator,std::multimap<uint32_t,uint64_t>::iterator> ret;
|
---|
2620 | uint64_t commonSourceStoragesSize = 0;
|
---|
2621 |
|
---|
2622 | /* find all records in multimap with identical storage UID*/
|
---|
2623 | ret = neededStorageFreeSpace.equal_range(neededStorageFreeSpace.begin()->first);
|
---|
2624 | std::multimap<uint32_t,uint64_t>::const_iterator it_ns = ret.first;
|
---|
2625 |
|
---|
2626 | for (; it_ns != ret.second ; ++it_ns)
|
---|
2627 | {
|
---|
2628 | commonSourceStoragesSize += it_ns->second;
|
---|
2629 | }
|
---|
2630 |
|
---|
2631 | /* find appropriate path by storage UID*/
|
---|
2632 | std::map<uint32_t,const char*>::const_iterator it_sm = serialMapToStoragePath.find(ret.first->first);
|
---|
2633 | /* get info about a storage */
|
---|
2634 | if (it_sm == serialMapToStoragePath.end())
|
---|
2635 | {
|
---|
2636 | LogFlowThisFunc((" Path to the storage wasn't found...\n "));
|
---|
2637 |
|
---|
2638 | rc = setError(E_INVALIDARG,
|
---|
2639 | tr(" Unable to merge storage '%s'. Path to the storage wasn't found. "),
|
---|
2640 | it_sm->second);
|
---|
2641 | throw rc;
|
---|
2642 | }
|
---|
2643 |
|
---|
2644 | int vrc = RTFsQuerySizes(it_sm->second, &pcbTotal, &pcbFree,&pcbBlock, &pcbSector);
|
---|
2645 | if (RT_FAILURE(vrc))
|
---|
2646 | {
|
---|
2647 | rc = setError(E_FAIL,
|
---|
2648 | tr(" Unable to merge storage '%s'. Can't get the storage size. "),
|
---|
2649 | it_sm->second);
|
---|
2650 | throw rc;
|
---|
2651 | }
|
---|
2652 |
|
---|
2653 | if (commonSourceStoragesSize > (uint64_t)pcbFree)
|
---|
2654 | {
|
---|
2655 | LogFlowThisFunc((" Not enough free space to merge...\n "));
|
---|
2656 |
|
---|
2657 | rc = setError(E_OUTOFMEMORY,
|
---|
2658 | tr(" Unable to merge storage '%s' - not enough free storage space. "),
|
---|
2659 | it_sm->second);
|
---|
2660 | throw rc;
|
---|
2661 | }
|
---|
2662 |
|
---|
2663 | neededStorageFreeSpace.erase(ret.first, ret.second);
|
---|
2664 | }
|
---|
2665 |
|
---|
2666 | serialMapToStoragePath.clear();
|
---|
2667 | }
|
---|
2668 |
|
---|
2669 | // we can release the locks now since the machine state is MachineState_DeletingSnapshot
|
---|
2670 | treeLock.release();
|
---|
2671 | multiLock.release();
|
---|
2672 |
|
---|
2673 | /* Now we checked that we can successfully merge all normal hard disks
|
---|
2674 | * (unless a runtime error like end-of-disc happens). Now get rid of
|
---|
2675 | * the saved state (if present), as that will free some disk space.
|
---|
2676 | * The snapshot itself will be deleted as late as possible, so that
|
---|
2677 | * the user can repeat the delete operation if he runs out of disk
|
---|
2678 | * space or cancels the delete operation. */
|
---|
2679 |
|
---|
2680 | /* second pass: */
|
---|
2681 | LogFlowThisFunc(("2: Deleting saved state...\n"));
|
---|
2682 |
|
---|
2683 | {
|
---|
2684 | // saveAllSnapshots() needs a machine lock, and the snapshots
|
---|
2685 | // tree is protected by the machine lock as well
|
---|
2686 | AutoWriteLock machineLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2687 |
|
---|
2688 | Utf8Str stateFilePath = aTask.pSnapshot->getStateFilePath();
|
---|
2689 | if (!stateFilePath.isEmpty())
|
---|
2690 | {
|
---|
2691 | aTask.pProgress->SetNextOperation(Bstr(tr("Deleting the execution state")).raw(),
|
---|
2692 | 1); // weight
|
---|
2693 |
|
---|
2694 | releaseSavedStateFile(stateFilePath, aTask.pSnapshot /* pSnapshotToIgnore */);
|
---|
2695 |
|
---|
2696 | // machine will need saving now
|
---|
2697 | machineLock.release();
|
---|
2698 | mParent->markRegistryModified(getId());
|
---|
2699 | }
|
---|
2700 | }
|
---|
2701 |
|
---|
2702 | /* third pass: */
|
---|
2703 | LogFlowThisFunc(("3: Performing actual hard disk merging...\n"));
|
---|
2704 |
|
---|
2705 | /// @todo NEWMEDIA turn the following errors into warnings because the
|
---|
2706 | /// snapshot itself has been already deleted (and interpret these
|
---|
2707 | /// warnings properly on the GUI side)
|
---|
2708 | for (MediumDeleteRecList::iterator it = toDelete.begin();
|
---|
2709 | it != toDelete.end();)
|
---|
2710 | {
|
---|
2711 | const ComObjPtr<Medium> &pMedium(it->mpHD);
|
---|
2712 | ULONG ulWeight;
|
---|
2713 |
|
---|
2714 | {
|
---|
2715 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
2716 | ulWeight = (ULONG)(pMedium->getSize() / _1M);
|
---|
2717 | }
|
---|
2718 |
|
---|
2719 | aTask.pProgress->SetNextOperation(BstrFmt(tr("Merging differencing image '%s'"),
|
---|
2720 | pMedium->getName().c_str()).raw(),
|
---|
2721 | ulWeight);
|
---|
2722 |
|
---|
2723 | bool fNeedSourceUninit = false;
|
---|
2724 | bool fReparentTarget = false;
|
---|
2725 | if (it->mpMediumLockList == NULL)
|
---|
2726 | {
|
---|
2727 | /* no real merge needed, just updating state and delete
|
---|
2728 | * diff files if necessary */
|
---|
2729 | AutoMultiWriteLock2 mLock(&mParent->getMediaTreeLockHandle(), pMedium->lockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
2730 |
|
---|
2731 | Assert( !it->mfMergeForward
|
---|
2732 | || pMedium->getChildren().size() == 0);
|
---|
2733 |
|
---|
2734 | /* Delete the differencing hard disk (has no children). Two
|
---|
2735 | * exceptions: if it's the last medium in the chain or if it's
|
---|
2736 | * a backward merge we don't want to handle due to complexity.
|
---|
2737 | * In both cases leave the image in place. If it's the first
|
---|
2738 | * exception the user can delete it later if he wants. */
|
---|
2739 | if (!pMedium->getParent().isNull())
|
---|
2740 | {
|
---|
2741 | Assert(pMedium->getState() == MediumState_Deleting);
|
---|
2742 | /* No need to hold the lock any longer. */
|
---|
2743 | mLock.release();
|
---|
2744 | rc = pMedium->deleteStorage(&aTask.pProgress,
|
---|
2745 | true /* aWait */);
|
---|
2746 | if (FAILED(rc))
|
---|
2747 | throw rc;
|
---|
2748 |
|
---|
2749 | // need to uninit the deleted medium
|
---|
2750 | fNeedSourceUninit = true;
|
---|
2751 | }
|
---|
2752 | }
|
---|
2753 | else
|
---|
2754 | {
|
---|
2755 | bool fNeedsSave = false;
|
---|
2756 | if (it->mfNeedsOnlineMerge)
|
---|
2757 | {
|
---|
2758 | // online medium merge, in the direction decided earlier
|
---|
2759 | rc = onlineMergeMedium(it->mpOnlineMediumAttachment,
|
---|
2760 | it->mpSource,
|
---|
2761 | it->mpTarget,
|
---|
2762 | it->mfMergeForward,
|
---|
2763 | it->mpParentForTarget,
|
---|
2764 | it->mChildrenToReparent,
|
---|
2765 | it->mpMediumLockList,
|
---|
2766 | aTask.pProgress,
|
---|
2767 | &fNeedsSave);
|
---|
2768 | }
|
---|
2769 | else
|
---|
2770 | {
|
---|
2771 | // normal medium merge, in the direction decided earlier
|
---|
2772 | rc = it->mpSource->mergeTo(it->mpTarget,
|
---|
2773 | it->mfMergeForward,
|
---|
2774 | it->mpParentForTarget,
|
---|
2775 | it->mChildrenToReparent,
|
---|
2776 | it->mpMediumLockList,
|
---|
2777 | &aTask.pProgress,
|
---|
2778 | true /* aWait */);
|
---|
2779 | }
|
---|
2780 |
|
---|
2781 | // If the merge failed, we need to do our best to have a usable
|
---|
2782 | // VM configuration afterwards. The return code doesn't tell
|
---|
2783 | // whether the merge completed and so we have to check if the
|
---|
2784 | // source medium (diff images are always file based at the
|
---|
2785 | // moment) is still there or not. Be careful not to lose the
|
---|
2786 | // error code below, before the "Delayed failure exit".
|
---|
2787 | if (FAILED(rc))
|
---|
2788 | {
|
---|
2789 | AutoReadLock mlock(it->mpSource COMMA_LOCKVAL_SRC_POS);
|
---|
2790 | if (!it->mpSource->isMediumFormatFile())
|
---|
2791 | // Diff medium not backed by a file - cannot get status so
|
---|
2792 | // be pessimistic.
|
---|
2793 | throw rc;
|
---|
2794 | const Utf8Str &loc = it->mpSource->getLocationFull();
|
---|
2795 | // Source medium is still there, so merge failed early.
|
---|
2796 | if (RTFileExists(loc.c_str()))
|
---|
2797 | throw rc;
|
---|
2798 |
|
---|
2799 | // Source medium is gone. Assume the merge succeeded and
|
---|
2800 | // thus it's safe to remove the attachment. We use the
|
---|
2801 | // "Delayed failure exit" below.
|
---|
2802 | }
|
---|
2803 |
|
---|
2804 | // need to change the medium attachment for backward merges
|
---|
2805 | fReparentTarget = !it->mfMergeForward;
|
---|
2806 |
|
---|
2807 | if (!it->mfNeedsOnlineMerge)
|
---|
2808 | {
|
---|
2809 | // need to uninit the medium deleted by the merge
|
---|
2810 | fNeedSourceUninit = true;
|
---|
2811 |
|
---|
2812 | // delete the no longer needed medium lock list, which
|
---|
2813 | // implicitly handled the unlocking
|
---|
2814 | delete it->mpMediumLockList;
|
---|
2815 | it->mpMediumLockList = NULL;
|
---|
2816 | }
|
---|
2817 | }
|
---|
2818 |
|
---|
2819 | // Now that the medium is successfully merged/deleted/whatever,
|
---|
2820 | // remove the medium attachment from the snapshot. For a backwards
|
---|
2821 | // merge the target attachment needs to be removed from the
|
---|
2822 | // snapshot, as the VM will take it over. For forward merges the
|
---|
2823 | // source medium attachment needs to be removed.
|
---|
2824 | ComObjPtr<MediumAttachment> pAtt;
|
---|
2825 | if (fReparentTarget)
|
---|
2826 | {
|
---|
2827 | pAtt = findAttachment(pSnapMachine->mMediaData->mAttachments,
|
---|
2828 | it->mpTarget);
|
---|
2829 | it->mpTarget->removeBackReference(machineId, snapshotId);
|
---|
2830 | }
|
---|
2831 | else
|
---|
2832 | pAtt = findAttachment(pSnapMachine->mMediaData->mAttachments,
|
---|
2833 | it->mpSource);
|
---|
2834 | pSnapMachine->mMediaData->mAttachments.remove(pAtt);
|
---|
2835 |
|
---|
2836 | if (fReparentTarget)
|
---|
2837 | {
|
---|
2838 | // Search for old source attachment and replace with target.
|
---|
2839 | // There can be only one child snapshot in this case.
|
---|
2840 | ComObjPtr<Machine> pMachine = this;
|
---|
2841 | Guid childSnapshotId;
|
---|
2842 | ComObjPtr<Snapshot> pChildSnapshot = aTask.pSnapshot->getFirstChild();
|
---|
2843 | if (pChildSnapshot)
|
---|
2844 | {
|
---|
2845 | pMachine = pChildSnapshot->getSnapshotMachine();
|
---|
2846 | childSnapshotId = pChildSnapshot->getId();
|
---|
2847 | }
|
---|
2848 | pAtt = findAttachment(pMachine->mMediaData->mAttachments, it->mpSource);
|
---|
2849 | if (pAtt)
|
---|
2850 | {
|
---|
2851 | AutoWriteLock attLock(pAtt COMMA_LOCKVAL_SRC_POS);
|
---|
2852 | pAtt->updateMedium(it->mpTarget);
|
---|
2853 | it->mpTarget->addBackReference(pMachine->mData->mUuid, childSnapshotId);
|
---|
2854 | }
|
---|
2855 | else
|
---|
2856 | {
|
---|
2857 | // If no attachment is found do not change anything. Maybe
|
---|
2858 | // the source medium was not attached to the snapshot.
|
---|
2859 | // If this is an online deletion the attachment was updated
|
---|
2860 | // already to allow the VM continue execution immediately.
|
---|
2861 | // Needs a bit of special treatment due to this difference.
|
---|
2862 | if (it->mfNeedsOnlineMerge)
|
---|
2863 | it->mpTarget->addBackReference(pMachine->mData->mUuid, childSnapshotId);
|
---|
2864 | }
|
---|
2865 | }
|
---|
2866 |
|
---|
2867 | if (fNeedSourceUninit)
|
---|
2868 | it->mpSource->uninit();
|
---|
2869 |
|
---|
2870 | // One attachment is merged, must save the settings
|
---|
2871 | mParent->markRegistryModified(getId());
|
---|
2872 |
|
---|
2873 | // prevent calling cancelDeleteSnapshotMedium() for this attachment
|
---|
2874 | it = toDelete.erase(it);
|
---|
2875 |
|
---|
2876 | // Delayed failure exit when the merge cleanup failed but the
|
---|
2877 | // merge actually succeeded.
|
---|
2878 | if (FAILED(rc))
|
---|
2879 | throw rc;
|
---|
2880 | }
|
---|
2881 |
|
---|
2882 | {
|
---|
2883 | // beginSnapshotDelete() needs the machine lock, and the snapshots
|
---|
2884 | // tree is protected by the machine lock as well
|
---|
2885 | AutoWriteLock machineLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2886 |
|
---|
2887 | aTask.pSnapshot->beginSnapshotDelete();
|
---|
2888 | aTask.pSnapshot->uninit();
|
---|
2889 |
|
---|
2890 | machineLock.release();
|
---|
2891 | mParent->markRegistryModified(getId());
|
---|
2892 | }
|
---|
2893 | }
|
---|
2894 | catch (HRESULT aRC) {
|
---|
2895 | rc = aRC;
|
---|
2896 | }
|
---|
2897 |
|
---|
2898 | if (FAILED(rc))
|
---|
2899 | {
|
---|
2900 | // preserve existing error info so that the result can
|
---|
2901 | // be properly reported to the progress object below
|
---|
2902 | ErrorInfoKeeper eik;
|
---|
2903 |
|
---|
2904 | AutoMultiWriteLock2 multiLock(this->lockHandle(), // machine
|
---|
2905 | &mParent->getMediaTreeLockHandle() // media tree
|
---|
2906 | COMMA_LOCKVAL_SRC_POS);
|
---|
2907 |
|
---|
2908 | // un-prepare the remaining hard disks
|
---|
2909 | for (MediumDeleteRecList::const_iterator it = toDelete.begin();
|
---|
2910 | it != toDelete.end();
|
---|
2911 | ++it)
|
---|
2912 | {
|
---|
2913 | cancelDeleteSnapshotMedium(it->mpHD, it->mpSource,
|
---|
2914 | it->mChildrenToReparent,
|
---|
2915 | it->mfNeedsOnlineMerge,
|
---|
2916 | it->mpMediumLockList, it->mMachineId,
|
---|
2917 | it->mSnapshotId);
|
---|
2918 | }
|
---|
2919 | }
|
---|
2920 |
|
---|
2921 | // whether we were successful or not, we need to set the machine
|
---|
2922 | // state and save the machine settings;
|
---|
2923 | {
|
---|
2924 | // preserve existing error info so that the result can
|
---|
2925 | // be properly reported to the progress object below
|
---|
2926 | ErrorInfoKeeper eik;
|
---|
2927 |
|
---|
2928 | // restore the machine state that was saved when the
|
---|
2929 | // task was started
|
---|
2930 | setMachineState(aTask.machineStateBackup);
|
---|
2931 | updateMachineStateOnClient();
|
---|
2932 |
|
---|
2933 | mParent->saveModifiedRegistries();
|
---|
2934 | }
|
---|
2935 |
|
---|
2936 | // report the result (this will try to fetch current error info on failure)
|
---|
2937 | aTask.pProgress->notifyComplete(rc);
|
---|
2938 |
|
---|
2939 | if (SUCCEEDED(rc))
|
---|
2940 | mParent->onSnapshotDeleted(mData->mUuid, snapshotId);
|
---|
2941 |
|
---|
2942 | LogFlowThisFunc(("Done deleting snapshot (rc=%08X)\n", rc));
|
---|
2943 | LogFlowThisFuncLeave();
|
---|
2944 | }
|
---|
2945 |
|
---|
2946 | /**
|
---|
2947 | * Checks that this hard disk (part of a snapshot) may be deleted/merged and
|
---|
2948 | * performs necessary state changes. Must not be called for writethrough disks
|
---|
2949 | * because there is nothing to delete/merge then.
|
---|
2950 | *
|
---|
2951 | * This method is to be called prior to calling #deleteSnapshotMedium().
|
---|
2952 | * If #deleteSnapshotMedium() is not called or fails, the state modifications
|
---|
2953 | * performed by this method must be undone by #cancelDeleteSnapshotMedium().
|
---|
2954 | *
|
---|
2955 | * @return COM status code
|
---|
2956 | * @param aHD Hard disk which is connected to the snapshot.
|
---|
2957 | * @param aMachineId UUID of machine this hard disk is attached to.
|
---|
2958 | * @param aSnapshotId UUID of snapshot this hard disk is attached to. May
|
---|
2959 | * be a zero UUID if no snapshot is applicable.
|
---|
2960 | * @param fOnlineMergePossible Flag whether an online merge is possible.
|
---|
2961 | * @param aVMMALockList Medium lock list for the medium attachment of this VM.
|
---|
2962 | * Only used if @a fOnlineMergePossible is @c true, and
|
---|
2963 | * must be non-NULL in this case.
|
---|
2964 | * @param aSource Source hard disk for merge (out).
|
---|
2965 | * @param aTarget Target hard disk for merge (out).
|
---|
2966 | * @param aMergeForward Merge direction decision (out).
|
---|
2967 | * @param aParentForTarget New parent if target needs to be reparented (out).
|
---|
2968 | * @param aChildrenToReparent Children which have to be reparented to the
|
---|
2969 | * target (out).
|
---|
2970 | * @param fNeedsOnlineMerge Whether this merge needs to be done online (out).
|
---|
2971 | * If this is set to @a true then the @a aVMMALockList
|
---|
2972 | * parameter has been modified and is returned as
|
---|
2973 | * @a aMediumLockList.
|
---|
2974 | * @param aMediumLockList Where to store the created medium lock list (may
|
---|
2975 | * return NULL if no real merge is necessary).
|
---|
2976 | *
|
---|
2977 | * @note Caller must hold media tree lock for writing. This locks this object
|
---|
2978 | * and every medium object on the merge chain for writing.
|
---|
2979 | */
|
---|
2980 | HRESULT SessionMachine::prepareDeleteSnapshotMedium(const ComObjPtr<Medium> &aHD,
|
---|
2981 | const Guid &aMachineId,
|
---|
2982 | const Guid &aSnapshotId,
|
---|
2983 | bool fOnlineMergePossible,
|
---|
2984 | MediumLockList *aVMMALockList,
|
---|
2985 | ComObjPtr<Medium> &aSource,
|
---|
2986 | ComObjPtr<Medium> &aTarget,
|
---|
2987 | bool &aMergeForward,
|
---|
2988 | ComObjPtr<Medium> &aParentForTarget,
|
---|
2989 | MediaList &aChildrenToReparent,
|
---|
2990 | bool &fNeedsOnlineMerge,
|
---|
2991 | MediumLockList * &aMediumLockList)
|
---|
2992 | {
|
---|
2993 | Assert(!mParent->getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2994 | Assert(!fOnlineMergePossible || VALID_PTR(aVMMALockList));
|
---|
2995 |
|
---|
2996 | AutoWriteLock alock(aHD COMMA_LOCKVAL_SRC_POS);
|
---|
2997 |
|
---|
2998 | // Medium must not be writethrough/shareable/readonly at this point
|
---|
2999 | MediumType_T type = aHD->getType();
|
---|
3000 | AssertReturn( type != MediumType_Writethrough
|
---|
3001 | && type != MediumType_Shareable
|
---|
3002 | && type != MediumType_Readonly, E_FAIL);
|
---|
3003 |
|
---|
3004 | aMediumLockList = NULL;
|
---|
3005 | fNeedsOnlineMerge = false;
|
---|
3006 |
|
---|
3007 | if (aHD->getChildren().size() == 0)
|
---|
3008 | {
|
---|
3009 | /* This technically is no merge, set those values nevertheless.
|
---|
3010 | * Helps with updating the medium attachments. */
|
---|
3011 | aSource = aHD;
|
---|
3012 | aTarget = aHD;
|
---|
3013 |
|
---|
3014 | /* special treatment of the last hard disk in the chain: */
|
---|
3015 | if (aHD->getParent().isNull())
|
---|
3016 | {
|
---|
3017 | /* lock only, to prevent any usage until the snapshot deletion
|
---|
3018 | * is completed */
|
---|
3019 | alock.release();
|
---|
3020 | return aHD->LockWrite(NULL);
|
---|
3021 | }
|
---|
3022 |
|
---|
3023 | /* the differencing hard disk w/o children will be deleted, protect it
|
---|
3024 | * from attaching to other VMs (this is why Deleting) */
|
---|
3025 | return aHD->markForDeletion();
|
---|
3026 | }
|
---|
3027 |
|
---|
3028 | /* not going multi-merge as it's too expensive */
|
---|
3029 | if (aHD->getChildren().size() > 1)
|
---|
3030 | return setError(E_FAIL,
|
---|
3031 | tr("Hard disk '%s' has more than one child hard disk (%d)"),
|
---|
3032 | aHD->getLocationFull().c_str(),
|
---|
3033 | aHD->getChildren().size());
|
---|
3034 |
|
---|
3035 | ComObjPtr<Medium> pChild = aHD->getChildren().front();
|
---|
3036 |
|
---|
3037 | AutoWriteLock childLock(pChild COMMA_LOCKVAL_SRC_POS);
|
---|
3038 |
|
---|
3039 | /* the rest is a normal merge setup */
|
---|
3040 | if (aHD->getParent().isNull())
|
---|
3041 | {
|
---|
3042 | /* base hard disk, backward merge */
|
---|
3043 | const Guid *pMachineId1 = pChild->getFirstMachineBackrefId();
|
---|
3044 | const Guid *pMachineId2 = aHD->getFirstMachineBackrefId();
|
---|
3045 | if (pMachineId1 && pMachineId2 && *pMachineId1 != *pMachineId2)
|
---|
3046 | {
|
---|
3047 | /* backward merge is too tricky, we'll just detach on snapshot
|
---|
3048 | * deletion, so lock only, to prevent any usage */
|
---|
3049 | childLock.release();
|
---|
3050 | alock.release();
|
---|
3051 | return aHD->LockWrite(NULL);
|
---|
3052 | }
|
---|
3053 |
|
---|
3054 | aSource = pChild;
|
---|
3055 | aTarget = aHD;
|
---|
3056 | }
|
---|
3057 | else
|
---|
3058 | {
|
---|
3059 | /* Determine best merge direction. */
|
---|
3060 | bool fMergeForward = true;
|
---|
3061 |
|
---|
3062 | childLock.release();
|
---|
3063 | alock.release();
|
---|
3064 | HRESULT rc = aHD->queryPreferredMergeDirection(pChild, fMergeForward);
|
---|
3065 | alock.acquire();
|
---|
3066 | childLock.acquire();
|
---|
3067 |
|
---|
3068 | if (FAILED(rc) && rc != E_FAIL)
|
---|
3069 | return rc;
|
---|
3070 |
|
---|
3071 | if (fMergeForward)
|
---|
3072 | {
|
---|
3073 | aSource = aHD;
|
---|
3074 | aTarget = pChild;
|
---|
3075 | LogFlowFunc(("Forward merging selected\n"));
|
---|
3076 | }
|
---|
3077 | else
|
---|
3078 | {
|
---|
3079 | aSource = pChild;
|
---|
3080 | aTarget = aHD;
|
---|
3081 | LogFlowFunc(("Backward merging selected\n"));
|
---|
3082 | }
|
---|
3083 | }
|
---|
3084 |
|
---|
3085 | HRESULT rc;
|
---|
3086 | childLock.release();
|
---|
3087 | alock.release();
|
---|
3088 | rc = aSource->prepareMergeTo(aTarget, &aMachineId, &aSnapshotId,
|
---|
3089 | !fOnlineMergePossible /* fLockMedia */,
|
---|
3090 | aMergeForward, aParentForTarget,
|
---|
3091 | aChildrenToReparent, aMediumLockList);
|
---|
3092 | alock.acquire();
|
---|
3093 | childLock.acquire();
|
---|
3094 | if (SUCCEEDED(rc) && fOnlineMergePossible)
|
---|
3095 | {
|
---|
3096 | /* Try to lock the newly constructed medium lock list. If it succeeds
|
---|
3097 | * this can be handled as an offline merge, i.e. without the need of
|
---|
3098 | * asking the VM to do the merging. Only continue with the online
|
---|
3099 | * merging preparation if applicable. */
|
---|
3100 | childLock.release();
|
---|
3101 | alock.release();
|
---|
3102 | rc = aMediumLockList->Lock();
|
---|
3103 | alock.acquire();
|
---|
3104 | childLock.acquire();
|
---|
3105 | if (FAILED(rc) && fOnlineMergePossible)
|
---|
3106 | {
|
---|
3107 | /* Locking failed, this cannot be done as an offline merge. Try to
|
---|
3108 | * combine the locking information into the lock list of the medium
|
---|
3109 | * attachment in the running VM. If that fails or locking the
|
---|
3110 | * resulting lock list fails then the merge cannot be done online.
|
---|
3111 | * It can be repeated by the user when the VM is shut down. */
|
---|
3112 | MediumLockList::Base::iterator lockListVMMABegin =
|
---|
3113 | aVMMALockList->GetBegin();
|
---|
3114 | MediumLockList::Base::iterator lockListVMMAEnd =
|
---|
3115 | aVMMALockList->GetEnd();
|
---|
3116 | MediumLockList::Base::iterator lockListBegin =
|
---|
3117 | aMediumLockList->GetBegin();
|
---|
3118 | MediumLockList::Base::iterator lockListEnd =
|
---|
3119 | aMediumLockList->GetEnd();
|
---|
3120 | for (MediumLockList::Base::iterator it = lockListVMMABegin,
|
---|
3121 | it2 = lockListBegin;
|
---|
3122 | it2 != lockListEnd;
|
---|
3123 | ++it, ++it2)
|
---|
3124 | {
|
---|
3125 | if ( it == lockListVMMAEnd
|
---|
3126 | || it->GetMedium() != it2->GetMedium())
|
---|
3127 | {
|
---|
3128 | fOnlineMergePossible = false;
|
---|
3129 | break;
|
---|
3130 | }
|
---|
3131 | bool fLockReq = (it2->GetLockRequest() || it->GetLockRequest());
|
---|
3132 | childLock.release();
|
---|
3133 | alock.release();
|
---|
3134 | rc = it->UpdateLock(fLockReq);
|
---|
3135 | alock.acquire();
|
---|
3136 | childLock.acquire();
|
---|
3137 | if (FAILED(rc))
|
---|
3138 | {
|
---|
3139 | // could not update the lock, trigger cleanup below
|
---|
3140 | fOnlineMergePossible = false;
|
---|
3141 | break;
|
---|
3142 | }
|
---|
3143 | }
|
---|
3144 |
|
---|
3145 | if (fOnlineMergePossible)
|
---|
3146 | {
|
---|
3147 | /* we will lock the children of the source for reparenting */
|
---|
3148 | for (MediaList::const_iterator it = aChildrenToReparent.begin();
|
---|
3149 | it != aChildrenToReparent.end();
|
---|
3150 | ++it)
|
---|
3151 | {
|
---|
3152 | ComObjPtr<Medium> pMedium = *it;
|
---|
3153 | AutoReadLock mediumLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
3154 | if (pMedium->getState() == MediumState_Created)
|
---|
3155 | {
|
---|
3156 | mediumLock.release();
|
---|
3157 | childLock.release();
|
---|
3158 | alock.release();
|
---|
3159 | rc = pMedium->LockWrite(NULL);
|
---|
3160 | alock.acquire();
|
---|
3161 | childLock.acquire();
|
---|
3162 | mediumLock.acquire();
|
---|
3163 | if (FAILED(rc))
|
---|
3164 | throw rc;
|
---|
3165 | }
|
---|
3166 | else
|
---|
3167 | {
|
---|
3168 | mediumLock.release();
|
---|
3169 | childLock.release();
|
---|
3170 | alock.release();
|
---|
3171 | rc = aVMMALockList->Update(pMedium, true);
|
---|
3172 | alock.acquire();
|
---|
3173 | childLock.acquire();
|
---|
3174 | mediumLock.acquire();
|
---|
3175 | if (FAILED(rc))
|
---|
3176 | {
|
---|
3177 | mediumLock.release();
|
---|
3178 | childLock.release();
|
---|
3179 | alock.release();
|
---|
3180 | rc = pMedium->LockWrite(NULL);
|
---|
3181 | alock.acquire();
|
---|
3182 | childLock.acquire();
|
---|
3183 | mediumLock.acquire();
|
---|
3184 | if (FAILED(rc))
|
---|
3185 | throw rc;
|
---|
3186 | }
|
---|
3187 | }
|
---|
3188 | }
|
---|
3189 | }
|
---|
3190 |
|
---|
3191 | if (fOnlineMergePossible)
|
---|
3192 | {
|
---|
3193 | childLock.release();
|
---|
3194 | alock.release();
|
---|
3195 | rc = aVMMALockList->Lock();
|
---|
3196 | alock.acquire();
|
---|
3197 | childLock.acquire();
|
---|
3198 | if (FAILED(rc))
|
---|
3199 | {
|
---|
3200 | aSource->cancelMergeTo(aChildrenToReparent, aMediumLockList);
|
---|
3201 | rc = setError(rc,
|
---|
3202 | tr("Cannot lock hard disk '%s' for a live merge"),
|
---|
3203 | aHD->getLocationFull().c_str());
|
---|
3204 | }
|
---|
3205 | else
|
---|
3206 | {
|
---|
3207 | delete aMediumLockList;
|
---|
3208 | aMediumLockList = aVMMALockList;
|
---|
3209 | fNeedsOnlineMerge = true;
|
---|
3210 | }
|
---|
3211 | }
|
---|
3212 | else
|
---|
3213 | {
|
---|
3214 | aSource->cancelMergeTo(aChildrenToReparent, aMediumLockList);
|
---|
3215 | rc = setError(rc,
|
---|
3216 | tr("Failed to construct lock list for a live merge of hard disk '%s'"),
|
---|
3217 | aHD->getLocationFull().c_str());
|
---|
3218 | }
|
---|
3219 |
|
---|
3220 | // fix the VM's lock list if anything failed
|
---|
3221 | if (FAILED(rc))
|
---|
3222 | {
|
---|
3223 | lockListVMMABegin = aVMMALockList->GetBegin();
|
---|
3224 | lockListVMMAEnd = aVMMALockList->GetEnd();
|
---|
3225 | MediumLockList::Base::iterator lockListLast = lockListVMMAEnd;
|
---|
3226 | lockListLast--;
|
---|
3227 | for (MediumLockList::Base::iterator it = lockListVMMABegin;
|
---|
3228 | it != lockListVMMAEnd;
|
---|
3229 | ++it)
|
---|
3230 | {
|
---|
3231 | childLock.release();
|
---|
3232 | alock.release();
|
---|
3233 | it->UpdateLock(it == lockListLast);
|
---|
3234 | alock.acquire();
|
---|
3235 | childLock.acquire();
|
---|
3236 | ComObjPtr<Medium> pMedium = it->GetMedium();
|
---|
3237 | AutoWriteLock mediumLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
3238 | // blindly apply this, only needed for medium objects which
|
---|
3239 | // would be deleted as part of the merge
|
---|
3240 | pMedium->unmarkLockedForDeletion();
|
---|
3241 | }
|
---|
3242 | }
|
---|
3243 |
|
---|
3244 | }
|
---|
3245 | else
|
---|
3246 | {
|
---|
3247 | aSource->cancelMergeTo(aChildrenToReparent, aMediumLockList);
|
---|
3248 | rc = setError(rc,
|
---|
3249 | tr("Cannot lock hard disk '%s' for an offline merge"),
|
---|
3250 | aHD->getLocationFull().c_str());
|
---|
3251 | }
|
---|
3252 | }
|
---|
3253 |
|
---|
3254 | return rc;
|
---|
3255 | }
|
---|
3256 |
|
---|
3257 | /**
|
---|
3258 | * Cancels the deletion/merging of this hard disk (part of a snapshot). Undoes
|
---|
3259 | * what #prepareDeleteSnapshotMedium() did. Must be called if
|
---|
3260 | * #deleteSnapshotMedium() is not called or fails.
|
---|
3261 | *
|
---|
3262 | * @param aHD Hard disk which is connected to the snapshot.
|
---|
3263 | * @param aSource Source hard disk for merge.
|
---|
3264 | * @param aChildrenToReparent Children to unlock.
|
---|
3265 | * @param fNeedsOnlineMerge Whether this merge needs to be done online.
|
---|
3266 | * @param aMediumLockList Medium locks to cancel.
|
---|
3267 | * @param aMachineId Machine id to attach the medium to.
|
---|
3268 | * @param aSnapshotId Snapshot id to attach the medium to.
|
---|
3269 | *
|
---|
3270 | * @note Locks the medium tree and the hard disks in the chain for writing.
|
---|
3271 | */
|
---|
3272 | void SessionMachine::cancelDeleteSnapshotMedium(const ComObjPtr<Medium> &aHD,
|
---|
3273 | const ComObjPtr<Medium> &aSource,
|
---|
3274 | const MediaList &aChildrenToReparent,
|
---|
3275 | bool fNeedsOnlineMerge,
|
---|
3276 | MediumLockList *aMediumLockList,
|
---|
3277 | const Guid &aMachineId,
|
---|
3278 | const Guid &aSnapshotId)
|
---|
3279 | {
|
---|
3280 | if (aMediumLockList == NULL)
|
---|
3281 | {
|
---|
3282 | AutoMultiWriteLock2 mLock(&mParent->getMediaTreeLockHandle(), aHD->lockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3283 |
|
---|
3284 | Assert(aHD->getChildren().size() == 0);
|
---|
3285 |
|
---|
3286 | if (aHD->getParent().isNull())
|
---|
3287 | {
|
---|
3288 | HRESULT rc = aHD->UnlockWrite(NULL);
|
---|
3289 | AssertComRC(rc);
|
---|
3290 | }
|
---|
3291 | else
|
---|
3292 | {
|
---|
3293 | HRESULT rc = aHD->unmarkForDeletion();
|
---|
3294 | AssertComRC(rc);
|
---|
3295 | }
|
---|
3296 | }
|
---|
3297 | else
|
---|
3298 | {
|
---|
3299 | if (fNeedsOnlineMerge)
|
---|
3300 | {
|
---|
3301 | // Online merge uses the medium lock list of the VM, so give
|
---|
3302 | // an empty list to cancelMergeTo so that it works as designed.
|
---|
3303 | aSource->cancelMergeTo(aChildrenToReparent, new MediumLockList());
|
---|
3304 |
|
---|
3305 | // clean up the VM medium lock list ourselves
|
---|
3306 | MediumLockList::Base::iterator lockListBegin =
|
---|
3307 | aMediumLockList->GetBegin();
|
---|
3308 | MediumLockList::Base::iterator lockListEnd =
|
---|
3309 | aMediumLockList->GetEnd();
|
---|
3310 | MediumLockList::Base::iterator lockListLast = lockListEnd;
|
---|
3311 | lockListLast--;
|
---|
3312 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
3313 | it != lockListEnd;
|
---|
3314 | ++it)
|
---|
3315 | {
|
---|
3316 | ComObjPtr<Medium> pMedium = it->GetMedium();
|
---|
3317 | AutoWriteLock mediumLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
3318 | if (pMedium->getState() == MediumState_Deleting)
|
---|
3319 | pMedium->unmarkForDeletion();
|
---|
3320 | else
|
---|
3321 | {
|
---|
3322 | // blindly apply this, only needed for medium objects which
|
---|
3323 | // would be deleted as part of the merge
|
---|
3324 | pMedium->unmarkLockedForDeletion();
|
---|
3325 | }
|
---|
3326 | mediumLock.release();
|
---|
3327 | it->UpdateLock(it == lockListLast);
|
---|
3328 | mediumLock.acquire();
|
---|
3329 | }
|
---|
3330 | }
|
---|
3331 | else
|
---|
3332 | {
|
---|
3333 | aSource->cancelMergeTo(aChildrenToReparent, aMediumLockList);
|
---|
3334 | }
|
---|
3335 | }
|
---|
3336 |
|
---|
3337 | if (aMachineId.isValid() && !aMachineId.isZero())
|
---|
3338 | {
|
---|
3339 | // reattach the source media to the snapshot
|
---|
3340 | HRESULT rc = aSource->addBackReference(aMachineId, aSnapshotId);
|
---|
3341 | AssertComRC(rc);
|
---|
3342 | }
|
---|
3343 | }
|
---|
3344 |
|
---|
3345 | /**
|
---|
3346 | * Perform an online merge of a hard disk, i.e. the equivalent of
|
---|
3347 | * Medium::mergeTo(), just for running VMs. If this fails you need to call
|
---|
3348 | * #cancelDeleteSnapshotMedium().
|
---|
3349 | *
|
---|
3350 | * @return COM status code
|
---|
3351 | * @param aMediumAttachment Identify where the disk is attached in the VM.
|
---|
3352 | * @param aSource Source hard disk for merge.
|
---|
3353 | * @param aTarget Target hard disk for merge.
|
---|
3354 | * @param aMergeForward Merge direction.
|
---|
3355 | * @param aParentForTarget New parent if target needs to be reparented.
|
---|
3356 | * @param aChildrenToReparent Children which have to be reparented to the
|
---|
3357 | * target.
|
---|
3358 | * @param aMediumLockList Where to store the created medium lock list (may
|
---|
3359 | * return NULL if no real merge is necessary).
|
---|
3360 | * @param aProgress Progress indicator.
|
---|
3361 | * @param pfNeedsMachineSaveSettings Whether the VM settings need to be saved (out).
|
---|
3362 | */
|
---|
3363 | HRESULT SessionMachine::onlineMergeMedium(const ComObjPtr<MediumAttachment> &aMediumAttachment,
|
---|
3364 | const ComObjPtr<Medium> &aSource,
|
---|
3365 | const ComObjPtr<Medium> &aTarget,
|
---|
3366 | bool fMergeForward,
|
---|
3367 | const ComObjPtr<Medium> &aParentForTarget,
|
---|
3368 | const MediaList &aChildrenToReparent,
|
---|
3369 | MediumLockList *aMediumLockList,
|
---|
3370 | ComObjPtr<Progress> &aProgress,
|
---|
3371 | bool *pfNeedsMachineSaveSettings)
|
---|
3372 | {
|
---|
3373 | AssertReturn(aSource != NULL, E_FAIL);
|
---|
3374 | AssertReturn(aTarget != NULL, E_FAIL);
|
---|
3375 | AssertReturn(aSource != aTarget, E_FAIL);
|
---|
3376 | AssertReturn(aMediumLockList != NULL, E_FAIL);
|
---|
3377 |
|
---|
3378 | HRESULT rc = S_OK;
|
---|
3379 |
|
---|
3380 | try
|
---|
3381 | {
|
---|
3382 | // Similar code appears in Medium::taskMergeHandle, so
|
---|
3383 | // if you make any changes below check whether they are applicable
|
---|
3384 | // in that context as well.
|
---|
3385 |
|
---|
3386 | unsigned uTargetIdx = (unsigned)-1;
|
---|
3387 | unsigned uSourceIdx = (unsigned)-1;
|
---|
3388 | /* Sanity check all hard disks in the chain. */
|
---|
3389 | MediumLockList::Base::iterator lockListBegin =
|
---|
3390 | aMediumLockList->GetBegin();
|
---|
3391 | MediumLockList::Base::iterator lockListEnd =
|
---|
3392 | aMediumLockList->GetEnd();
|
---|
3393 | unsigned i = 0;
|
---|
3394 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
3395 | it != lockListEnd;
|
---|
3396 | ++it)
|
---|
3397 | {
|
---|
3398 | MediumLock &mediumLock = *it;
|
---|
3399 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
3400 |
|
---|
3401 | if (pMedium == aSource)
|
---|
3402 | uSourceIdx = i;
|
---|
3403 | else if (pMedium == aTarget)
|
---|
3404 | uTargetIdx = i;
|
---|
3405 |
|
---|
3406 | // In Medium::taskMergeHandler there is lots of consistency
|
---|
3407 | // checking which we cannot do here, as the state details are
|
---|
3408 | // impossible to get outside the Medium class. The locking should
|
---|
3409 | // have done the checks already.
|
---|
3410 |
|
---|
3411 | i++;
|
---|
3412 | }
|
---|
3413 |
|
---|
3414 | ComAssertThrow( uSourceIdx != (unsigned)-1
|
---|
3415 | && uTargetIdx != (unsigned)-1, E_FAIL);
|
---|
3416 |
|
---|
3417 | // For forward merges, tell the VM what images need to have their
|
---|
3418 | // parent UUID updated. This cannot be done in VBoxSVC, as opening
|
---|
3419 | // the required parent images is not safe while the VM is running.
|
---|
3420 | // For backward merges this will be simply an array of size 0.
|
---|
3421 | com::SafeIfaceArray<IMedium> childrenToReparent(aChildrenToReparent);
|
---|
3422 |
|
---|
3423 | ComPtr<IInternalSessionControl> directControl;
|
---|
3424 | {
|
---|
3425 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3426 |
|
---|
3427 | if (mData->mSession.mState != SessionState_Locked)
|
---|
3428 | throw setError(VBOX_E_INVALID_VM_STATE,
|
---|
3429 | tr("Machine is not locked by a session (session state: %s)"),
|
---|
3430 | Global::stringifySessionState(mData->mSession.mState));
|
---|
3431 | directControl = mData->mSession.mDirectControl;
|
---|
3432 | }
|
---|
3433 |
|
---|
3434 | // Must not hold any locks here, as this will call back to finish
|
---|
3435 | // updating the medium attachment, chain linking and state.
|
---|
3436 | rc = directControl->OnlineMergeMedium(aMediumAttachment,
|
---|
3437 | uSourceIdx, uTargetIdx,
|
---|
3438 | aSource, aTarget,
|
---|
3439 | fMergeForward, aParentForTarget,
|
---|
3440 | ComSafeArrayAsInParam(childrenToReparent),
|
---|
3441 | aProgress);
|
---|
3442 | if (FAILED(rc))
|
---|
3443 | throw rc;
|
---|
3444 | }
|
---|
3445 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3446 |
|
---|
3447 | // The callback mentioned above takes care of update the medium state
|
---|
3448 |
|
---|
3449 | if (pfNeedsMachineSaveSettings)
|
---|
3450 | *pfNeedsMachineSaveSettings = true;
|
---|
3451 |
|
---|
3452 | return rc;
|
---|
3453 | }
|
---|
3454 |
|
---|
3455 | /**
|
---|
3456 | * Implementation for IInternalMachineControl::finishOnlineMergeMedium().
|
---|
3457 | *
|
---|
3458 | * Gets called after the successful completion of an online merge from
|
---|
3459 | * Console::onlineMergeMedium(), which gets invoked indirectly above in
|
---|
3460 | * the call to IInternalSessionControl::onlineMergeMedium.
|
---|
3461 | *
|
---|
3462 | * This updates the medium information and medium state so that the VM
|
---|
3463 | * can continue with the updated state of the medium chain.
|
---|
3464 | */
|
---|
3465 | STDMETHODIMP SessionMachine::FinishOnlineMergeMedium(IMediumAttachment *aMediumAttachment,
|
---|
3466 | IMedium *aSource,
|
---|
3467 | IMedium *aTarget,
|
---|
3468 | BOOL aMergeForward,
|
---|
3469 | IMedium *aParentForTarget,
|
---|
3470 | ComSafeArrayIn(IMedium *, aChildrenToReparent))
|
---|
3471 | {
|
---|
3472 | HRESULT rc = S_OK;
|
---|
3473 | ComObjPtr<Medium> pSource(static_cast<Medium *>(aSource));
|
---|
3474 | ComObjPtr<Medium> pTarget(static_cast<Medium *>(aTarget));
|
---|
3475 | ComObjPtr<Medium> pParentForTarget(static_cast<Medium *>(aParentForTarget));
|
---|
3476 | bool fSourceHasChildren = false;
|
---|
3477 |
|
---|
3478 | // all hard disks but the target were successfully deleted by
|
---|
3479 | // the merge; reparent target if necessary and uninitialize media
|
---|
3480 |
|
---|
3481 | AutoWriteLock treeLock(mParent->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3482 |
|
---|
3483 | // Declare this here to make sure the object does not get uninitialized
|
---|
3484 | // before this method completes. Would normally happen as halfway through
|
---|
3485 | // we delete the last reference to the no longer existing medium object.
|
---|
3486 | ComObjPtr<Medium> targetChild;
|
---|
3487 |
|
---|
3488 | if (aMergeForward)
|
---|
3489 | {
|
---|
3490 | // first, unregister the target since it may become a base
|
---|
3491 | // hard disk which needs re-registration
|
---|
3492 | rc = mParent->unregisterMedium(pTarget);
|
---|
3493 | AssertComRC(rc);
|
---|
3494 |
|
---|
3495 | // then, reparent it and disconnect the deleted branch at
|
---|
3496 | // both ends (chain->parent() is source's parent)
|
---|
3497 | pTarget->deparent();
|
---|
3498 | pTarget->setParent(pParentForTarget);
|
---|
3499 | if (pParentForTarget)
|
---|
3500 | pSource->deparent();
|
---|
3501 |
|
---|
3502 | // then, register again
|
---|
3503 | rc = mParent->registerMedium(pTarget, &pTarget, DeviceType_HardDisk);
|
---|
3504 | AssertComRC(rc);
|
---|
3505 | }
|
---|
3506 | else
|
---|
3507 | {
|
---|
3508 | Assert(pTarget->getChildren().size() == 1);
|
---|
3509 | targetChild = pTarget->getChildren().front();
|
---|
3510 |
|
---|
3511 | // disconnect the deleted branch at the elder end
|
---|
3512 | targetChild->deparent();
|
---|
3513 |
|
---|
3514 | // Update parent UUIDs of the source's children, reparent them and
|
---|
3515 | // disconnect the deleted branch at the younger end
|
---|
3516 | com::SafeIfaceArray<IMedium> childrenToReparent(ComSafeArrayInArg(aChildrenToReparent));
|
---|
3517 | if (childrenToReparent.size() > 0)
|
---|
3518 | {
|
---|
3519 | fSourceHasChildren = true;
|
---|
3520 | // Fix the parent UUID of the images which needs to be moved to
|
---|
3521 | // underneath target. The running machine has the images opened,
|
---|
3522 | // but only for reading since the VM is paused. If anything fails
|
---|
3523 | // we must continue. The worst possible result is that the images
|
---|
3524 | // need manual fixing via VBoxManage to adjust the parent UUID.
|
---|
3525 | MediaList toReparent;
|
---|
3526 | for (size_t i = 0; i < childrenToReparent.size(); i++)
|
---|
3527 | {
|
---|
3528 | Medium *pMedium = static_cast<Medium *>(childrenToReparent[i]);
|
---|
3529 | toReparent.push_back(pMedium);
|
---|
3530 | }
|
---|
3531 | treeLock.release();
|
---|
3532 | pTarget->fixParentUuidOfChildren(toReparent);
|
---|
3533 | treeLock.acquire();
|
---|
3534 |
|
---|
3535 | // obey {parent,child} lock order
|
---|
3536 | AutoWriteLock sourceLock(pSource COMMA_LOCKVAL_SRC_POS);
|
---|
3537 |
|
---|
3538 | for (size_t i = 0; i < childrenToReparent.size(); i++)
|
---|
3539 | {
|
---|
3540 | Medium *pMedium = static_cast<Medium *>(childrenToReparent[i]);
|
---|
3541 | AutoWriteLock childLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
3542 |
|
---|
3543 | pMedium->deparent(); // removes pMedium from source
|
---|
3544 | pMedium->setParent(pTarget);
|
---|
3545 | }
|
---|
3546 | }
|
---|
3547 | }
|
---|
3548 |
|
---|
3549 | /* unregister and uninitialize all hard disks removed by the merge */
|
---|
3550 | MediumLockList *pMediumLockList = NULL;
|
---|
3551 | MediumAttachment *pMediumAttachment = static_cast<MediumAttachment *>(aMediumAttachment);
|
---|
3552 | rc = mData->mSession.mLockedMedia.Get(pMediumAttachment, pMediumLockList);
|
---|
3553 | const ComObjPtr<Medium> &pLast = aMergeForward ? pTarget : pSource;
|
---|
3554 | AssertReturn(SUCCEEDED(rc) && pMediumLockList, E_FAIL);
|
---|
3555 | MediumLockList::Base::iterator lockListBegin =
|
---|
3556 | pMediumLockList->GetBegin();
|
---|
3557 | MediumLockList::Base::iterator lockListEnd =
|
---|
3558 | pMediumLockList->GetEnd();
|
---|
3559 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
3560 | it != lockListEnd;
|
---|
3561 | )
|
---|
3562 | {
|
---|
3563 | MediumLock &mediumLock = *it;
|
---|
3564 | /* Create a real copy of the medium pointer, as the medium
|
---|
3565 | * lock deletion below would invalidate the referenced object. */
|
---|
3566 | const ComObjPtr<Medium> pMedium = mediumLock.GetMedium();
|
---|
3567 |
|
---|
3568 | /* The target and all images not merged (readonly) are skipped */
|
---|
3569 | if ( pMedium == pTarget
|
---|
3570 | || pMedium->getState() == MediumState_LockedRead)
|
---|
3571 | {
|
---|
3572 | ++it;
|
---|
3573 | }
|
---|
3574 | else
|
---|
3575 | {
|
---|
3576 | rc = mParent->unregisterMedium(pMedium);
|
---|
3577 | AssertComRC(rc);
|
---|
3578 |
|
---|
3579 | /* now, uninitialize the deleted hard disk (note that
|
---|
3580 | * due to the Deleting state, uninit() will not touch
|
---|
3581 | * the parent-child relationship so we need to
|
---|
3582 | * uninitialize each disk individually) */
|
---|
3583 |
|
---|
3584 | /* note that the operation initiator hard disk (which is
|
---|
3585 | * normally also the source hard disk) is a special case
|
---|
3586 | * -- there is one more caller added by Task to it which
|
---|
3587 | * we must release. Also, if we are in sync mode, the
|
---|
3588 | * caller may still hold an AutoCaller instance for it
|
---|
3589 | * and therefore we cannot uninit() it (it's therefore
|
---|
3590 | * the caller's responsibility) */
|
---|
3591 | if (pMedium == aSource)
|
---|
3592 | {
|
---|
3593 | Assert(pSource->getChildren().size() == 0);
|
---|
3594 | Assert(pSource->getFirstMachineBackrefId() == NULL);
|
---|
3595 | }
|
---|
3596 |
|
---|
3597 | /* Delete the medium lock list entry, which also releases the
|
---|
3598 | * caller added by MergeChain before uninit() and updates the
|
---|
3599 | * iterator to point to the right place. */
|
---|
3600 | rc = pMediumLockList->RemoveByIterator(it);
|
---|
3601 | AssertComRC(rc);
|
---|
3602 |
|
---|
3603 | pMedium->uninit();
|
---|
3604 | }
|
---|
3605 |
|
---|
3606 | /* Stop as soon as we reached the last medium affected by the merge.
|
---|
3607 | * The remaining images must be kept unchanged. */
|
---|
3608 | if (pMedium == pLast)
|
---|
3609 | break;
|
---|
3610 | }
|
---|
3611 |
|
---|
3612 | /* Could be in principle folded into the previous loop, but let's keep
|
---|
3613 | * things simple. Update the medium locking to be the standard state:
|
---|
3614 | * all parent images locked for reading, just the last diff for writing. */
|
---|
3615 | lockListBegin = pMediumLockList->GetBegin();
|
---|
3616 | lockListEnd = pMediumLockList->GetEnd();
|
---|
3617 | MediumLockList::Base::iterator lockListLast = lockListEnd;
|
---|
3618 | lockListLast--;
|
---|
3619 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
3620 | it != lockListEnd;
|
---|
3621 | ++it)
|
---|
3622 | {
|
---|
3623 | it->UpdateLock(it == lockListLast);
|
---|
3624 | }
|
---|
3625 |
|
---|
3626 | /* If this is a backwards merge of the only remaining snapshot (i.e. the
|
---|
3627 | * source has no children) then update the medium associated with the
|
---|
3628 | * attachment, as the previously associated one (source) is now deleted.
|
---|
3629 | * Without the immediate update the VM could not continue running. */
|
---|
3630 | if (!aMergeForward && !fSourceHasChildren)
|
---|
3631 | {
|
---|
3632 | AutoWriteLock attLock(pMediumAttachment COMMA_LOCKVAL_SRC_POS);
|
---|
3633 | pMediumAttachment->updateMedium(pTarget);
|
---|
3634 | }
|
---|
3635 |
|
---|
3636 | return S_OK;
|
---|
3637 | }
|
---|