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