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