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