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