1 | /* $Id: SnapshotImpl.cpp 96888 2022-09-26 19:29:50Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * COM class implementation for Snapshot and SnapshotMachine in VBoxSVC.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 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 | ISnapshot* iSnapshot = aSnapshot;
|
---|
2235 | ComObjPtr<Snapshot> pSnapshot(static_cast<Snapshot*>(iSnapshot));
|
---|
2236 | ComObjPtr<SnapshotMachine> pSnapMachine = pSnapshot->i_getSnapshotMachine();
|
---|
2237 |
|
---|
2238 | // create a progress object. The number of operations is:
|
---|
2239 | // 1 (preparing) + # of hard disks + 1 (if we need to copy the saved state file) */
|
---|
2240 | LogFlowThisFunc(("Going thru snapshot machine attachments to determine progress setup\n"));
|
---|
2241 |
|
---|
2242 | ULONG ulOpCount = 1; // one for preparations
|
---|
2243 | ULONG ulTotalWeight = 1; // one for preparations
|
---|
2244 | for (MediumAttachmentList::iterator
|
---|
2245 | it = pSnapMachine->mMediumAttachments->begin();
|
---|
2246 | it != pSnapMachine->mMediumAttachments->end();
|
---|
2247 | ++it)
|
---|
2248 | {
|
---|
2249 | ComObjPtr<MediumAttachment> &pAttach = *it;
|
---|
2250 | AutoReadLock attachLock(pAttach COMMA_LOCKVAL_SRC_POS);
|
---|
2251 | if (pAttach->i_getType() == DeviceType_HardDisk)
|
---|
2252 | {
|
---|
2253 | ++ulOpCount;
|
---|
2254 | ++ulTotalWeight; // assume one MB weight for each differencing hard disk to manage
|
---|
2255 | Assert(pAttach->i_getMedium());
|
---|
2256 | LogFlowThisFunc(("op %d: considering hard disk attachment %s\n", ulOpCount,
|
---|
2257 | pAttach->i_getMedium()->i_getName().c_str()));
|
---|
2258 | }
|
---|
2259 | }
|
---|
2260 |
|
---|
2261 | ComObjPtr<Progress> pProgress;
|
---|
2262 | pProgress.createObject();
|
---|
2263 | pProgress->init(mParent, static_cast<IMachine*>(this),
|
---|
2264 | BstrFmt(tr("Restoring snapshot '%s'"), pSnapshot->i_getName().c_str()).raw(),
|
---|
2265 | FALSE /* aCancelable */,
|
---|
2266 | ulOpCount,
|
---|
2267 | ulTotalWeight,
|
---|
2268 | Bstr(tr("Restoring machine settings")).raw(),
|
---|
2269 | 1);
|
---|
2270 |
|
---|
2271 | /* create and start the task on a separate thread (note that it will not
|
---|
2272 | * start working until we release alock) */
|
---|
2273 | RestoreSnapshotTask *pTask = new RestoreSnapshotTask(this,
|
---|
2274 | pProgress,
|
---|
2275 | "RestoreSnap",
|
---|
2276 | pSnapshot);
|
---|
2277 | rc = pTask->createThread();
|
---|
2278 | pTask = NULL;
|
---|
2279 | if (FAILED(rc))
|
---|
2280 | return rc;
|
---|
2281 |
|
---|
2282 | /* set the proper machine state (note: after creating a Task instance) */
|
---|
2283 | i_setMachineState(MachineState_RestoringSnapshot);
|
---|
2284 |
|
---|
2285 | /* return the progress to the caller */
|
---|
2286 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2287 |
|
---|
2288 | LogFlowThisFuncLeave();
|
---|
2289 |
|
---|
2290 | return S_OK;
|
---|
2291 | }
|
---|
2292 |
|
---|
2293 | /**
|
---|
2294 | * Worker method for the restore snapshot thread created by SessionMachine::RestoreSnapshot().
|
---|
2295 | * This method gets called indirectly through SessionMachine::taskHandler() which then
|
---|
2296 | * calls RestoreSnapshotTask::handler().
|
---|
2297 | *
|
---|
2298 | * The RestoreSnapshotTask contains the progress object returned to the console by
|
---|
2299 | * SessionMachine::RestoreSnapshot, through which progress and results are reported.
|
---|
2300 | *
|
---|
2301 | * @note Locks mParent + this object for writing.
|
---|
2302 | *
|
---|
2303 | * @param task Task data.
|
---|
2304 | */
|
---|
2305 | void SessionMachine::i_restoreSnapshotHandler(RestoreSnapshotTask &task)
|
---|
2306 | {
|
---|
2307 | LogFlowThisFuncEnter();
|
---|
2308 |
|
---|
2309 | AutoCaller autoCaller(this);
|
---|
2310 |
|
---|
2311 | LogFlowThisFunc(("state=%d\n", getObjectState().getState()));
|
---|
2312 | if (!autoCaller.isOk())
|
---|
2313 | {
|
---|
2314 | /* we might have been uninitialized because the session was accidentally
|
---|
2315 | * closed by the client, so don't assert */
|
---|
2316 | task.m_pProgress->i_notifyComplete(E_FAIL,
|
---|
2317 | COM_IIDOF(IMachine),
|
---|
2318 | getComponentName(),
|
---|
2319 | tr("The session has been accidentally closed"));
|
---|
2320 |
|
---|
2321 | LogFlowThisFuncLeave();
|
---|
2322 | return;
|
---|
2323 | }
|
---|
2324 |
|
---|
2325 | HRESULT rc = S_OK;
|
---|
2326 | Guid snapshotId;
|
---|
2327 | std::set<ComObjPtr<Medium> > pMediumsForNotify;
|
---|
2328 | std::map<Guid, std::pair<DeviceType_T, BOOL> > uIdsForNotify;
|
---|
2329 |
|
---|
2330 | try
|
---|
2331 | {
|
---|
2332 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2333 |
|
---|
2334 | /* Discard all current changes to mUserData (name, OSType etc.).
|
---|
2335 | * Note that the machine is powered off, so there is no need to inform
|
---|
2336 | * the direct session. */
|
---|
2337 | if (mData->flModifications)
|
---|
2338 | i_rollback(false /* aNotify */);
|
---|
2339 |
|
---|
2340 | /* Delete the saved state file if the machine was Saved prior to this
|
---|
2341 | * operation */
|
---|
2342 | if (task.m_machineStateBackup == MachineState_Saved || task.m_machineStateBackup == MachineState_AbortedSaved)
|
---|
2343 | {
|
---|
2344 | Assert(!mSSData->strStateFilePath.isEmpty());
|
---|
2345 |
|
---|
2346 | // release the saved state file AFTER unsetting the member variable
|
---|
2347 | // so that releaseSavedStateFile() won't think it's still in use
|
---|
2348 | Utf8Str strStateFile(mSSData->strStateFilePath);
|
---|
2349 | mSSData->strStateFilePath.setNull();
|
---|
2350 | i_releaseSavedStateFile(strStateFile, NULL /* pSnapshotToIgnore */ );
|
---|
2351 |
|
---|
2352 | task.modifyBackedUpState(MachineState_PoweredOff);
|
---|
2353 |
|
---|
2354 | rc = i_saveStateSettings(SaveSTS_StateFilePath);
|
---|
2355 | if (FAILED(rc))
|
---|
2356 | throw rc;
|
---|
2357 | }
|
---|
2358 |
|
---|
2359 | RTTIMESPEC snapshotTimeStamp;
|
---|
2360 | RTTimeSpecSetMilli(&snapshotTimeStamp, 0);
|
---|
2361 |
|
---|
2362 | {
|
---|
2363 | AutoReadLock snapshotLock(task.m_pSnapshot COMMA_LOCKVAL_SRC_POS);
|
---|
2364 |
|
---|
2365 | /* remember the timestamp of the snapshot we're restoring from */
|
---|
2366 | snapshotTimeStamp = task.m_pSnapshot->i_getTimeStamp();
|
---|
2367 |
|
---|
2368 | // save the snapshot ID (paranoia, here we hold the lock)
|
---|
2369 | snapshotId = task.m_pSnapshot->i_getId();
|
---|
2370 |
|
---|
2371 | ComPtr<SnapshotMachine> pSnapshotMachine(task.m_pSnapshot->i_getSnapshotMachine());
|
---|
2372 |
|
---|
2373 | /* copy all hardware data from the snapshot */
|
---|
2374 | i_copyFrom(pSnapshotMachine);
|
---|
2375 |
|
---|
2376 | LogFlowThisFunc(("Restoring hard disks from the snapshot...\n"));
|
---|
2377 |
|
---|
2378 | // restore the attachments from the snapshot
|
---|
2379 | i_setModified(IsModified_Storage);
|
---|
2380 | mMediumAttachments.backup();
|
---|
2381 | mMediumAttachments->clear();
|
---|
2382 | for (MediumAttachmentList::const_iterator
|
---|
2383 | it = pSnapshotMachine->mMediumAttachments->begin();
|
---|
2384 | it != pSnapshotMachine->mMediumAttachments->end();
|
---|
2385 | ++it)
|
---|
2386 | {
|
---|
2387 | ComObjPtr<MediumAttachment> pAttach;
|
---|
2388 | pAttach.createObject();
|
---|
2389 | pAttach->initCopy(this, *it);
|
---|
2390 | mMediumAttachments->push_back(pAttach);
|
---|
2391 | }
|
---|
2392 |
|
---|
2393 | /* release the locks before the potentially lengthy operation */
|
---|
2394 | snapshotLock.release();
|
---|
2395 | alock.release();
|
---|
2396 |
|
---|
2397 | rc = i_createImplicitDiffs(task.m_pProgress,
|
---|
2398 | 1,
|
---|
2399 | false /* aOnline */);
|
---|
2400 | if (FAILED(rc))
|
---|
2401 | throw rc;
|
---|
2402 |
|
---|
2403 | alock.acquire();
|
---|
2404 | snapshotLock.acquire();
|
---|
2405 |
|
---|
2406 | /* Note: on success, current (old) hard disks will be
|
---|
2407 | * deassociated/deleted on #commit() called from #i_saveSettings() at
|
---|
2408 | * the end. On failure, newly created implicit diffs will be
|
---|
2409 | * deleted by #rollback() at the end. */
|
---|
2410 |
|
---|
2411 | /* should not have a saved state file associated at this point */
|
---|
2412 | Assert(mSSData->strStateFilePath.isEmpty());
|
---|
2413 |
|
---|
2414 | const Utf8Str &strSnapshotStateFile = task.m_pSnapshot->i_getStateFilePath();
|
---|
2415 |
|
---|
2416 | if (strSnapshotStateFile.isNotEmpty())
|
---|
2417 | // online snapshot: then share the state file
|
---|
2418 | mSSData->strStateFilePath = strSnapshotStateFile;
|
---|
2419 |
|
---|
2420 | const Utf8Str srcNVRAM(pSnapshotMachine->mNvramStore->i_getNonVolatileStorageFile());
|
---|
2421 | const Utf8Str dstNVRAM(mNvramStore->i_getNonVolatileStorageFile());
|
---|
2422 | if (dstNVRAM.isNotEmpty() && RTFileExists(dstNVRAM.c_str()))
|
---|
2423 | RTFileDelete(dstNVRAM.c_str());
|
---|
2424 | if (srcNVRAM.isNotEmpty() && dstNVRAM.isNotEmpty() && RTFileExists(srcNVRAM.c_str()))
|
---|
2425 | RTFileCopy(srcNVRAM.c_str(), dstNVRAM.c_str());
|
---|
2426 |
|
---|
2427 | LogFlowThisFunc(("Setting new current snapshot {%RTuuid}\n", task.m_pSnapshot->i_getId().raw()));
|
---|
2428 | /* make the snapshot we restored from the current snapshot */
|
---|
2429 | mData->mCurrentSnapshot = task.m_pSnapshot;
|
---|
2430 | }
|
---|
2431 |
|
---|
2432 | // store parent of newly created diffs for notify
|
---|
2433 | {
|
---|
2434 | MediumAttachmentList &oldAtts = *mMediumAttachments.backedUpData();
|
---|
2435 | for (MediumAttachmentList::const_iterator
|
---|
2436 | it = mMediumAttachments->begin();
|
---|
2437 | it != mMediumAttachments->end();
|
---|
2438 | ++it)
|
---|
2439 | {
|
---|
2440 | MediumAttachment *pAttach = *it;
|
---|
2441 | Medium *pMedium = pAttach->i_getMedium();
|
---|
2442 | if (!pMedium)
|
---|
2443 | continue;
|
---|
2444 |
|
---|
2445 | bool fFound = false;
|
---|
2446 | /* was this medium attached before? */
|
---|
2447 | for (MediumAttachmentList::iterator
|
---|
2448 | oldIt = oldAtts.begin();
|
---|
2449 | oldIt != oldAtts.end();
|
---|
2450 | ++oldIt)
|
---|
2451 | {
|
---|
2452 | MediumAttachment *pOldAttach = *oldIt;
|
---|
2453 | if (pOldAttach->i_getMedium() == pMedium)
|
---|
2454 | {
|
---|
2455 | fFound = true;
|
---|
2456 | break;
|
---|
2457 | }
|
---|
2458 | }
|
---|
2459 | if (!fFound)
|
---|
2460 | {
|
---|
2461 | pMediumsForNotify.insert(pMedium->i_getParent());
|
---|
2462 | uIdsForNotify[pMedium->i_getId()] = std::pair<DeviceType_T, BOOL>(pMedium->i_getDeviceType(), TRUE);
|
---|
2463 | }
|
---|
2464 | }
|
---|
2465 | }
|
---|
2466 |
|
---|
2467 | /* grab differencing hard disks from the old attachments that will
|
---|
2468 | * become unused and need to be auto-deleted */
|
---|
2469 | std::list< ComObjPtr<MediumAttachment> > llDiffAttachmentsToDelete;
|
---|
2470 |
|
---|
2471 | for (MediumAttachmentList::const_iterator
|
---|
2472 | it = mMediumAttachments.backedUpData()->begin();
|
---|
2473 | it != mMediumAttachments.backedUpData()->end();
|
---|
2474 | ++it)
|
---|
2475 | {
|
---|
2476 | ComObjPtr<MediumAttachment> pAttach = *it;
|
---|
2477 | ComObjPtr<Medium> pMedium = pAttach->i_getMedium();
|
---|
2478 |
|
---|
2479 | /* while the hard disk is attached, the number of children or the
|
---|
2480 | * parent cannot change, so no lock */
|
---|
2481 | if ( !pMedium.isNull()
|
---|
2482 | && pAttach->i_getType() == DeviceType_HardDisk
|
---|
2483 | && !pMedium->i_getParent().isNull()
|
---|
2484 | && pMedium->i_getChildren().size() == 0
|
---|
2485 | )
|
---|
2486 | {
|
---|
2487 | LogFlowThisFunc(("Picked differencing image '%s' for deletion\n", pMedium->i_getName().c_str()));
|
---|
2488 |
|
---|
2489 | llDiffAttachmentsToDelete.push_back(pAttach);
|
---|
2490 | }
|
---|
2491 | }
|
---|
2492 |
|
---|
2493 | /* we have already deleted the current state, so set the execution
|
---|
2494 | * state accordingly no matter of the delete snapshot result */
|
---|
2495 | if (mSSData->strStateFilePath.isNotEmpty())
|
---|
2496 | task.modifyBackedUpState(MachineState_Saved);
|
---|
2497 | else
|
---|
2498 | task.modifyBackedUpState(MachineState_PoweredOff);
|
---|
2499 |
|
---|
2500 | /* Paranoia: no one must have saved the settings in the mean time. If
|
---|
2501 | * it happens nevertheless we'll close our eyes and continue below. */
|
---|
2502 | Assert(mMediumAttachments.isBackedUp());
|
---|
2503 |
|
---|
2504 | /* assign the timestamp from the snapshot */
|
---|
2505 | Assert(RTTimeSpecGetMilli(&snapshotTimeStamp) != 0);
|
---|
2506 | mData->mLastStateChange = snapshotTimeStamp;
|
---|
2507 |
|
---|
2508 | // detach the current-state diffs that we detected above and build a list of
|
---|
2509 | // image files to delete _after_ i_saveSettings()
|
---|
2510 |
|
---|
2511 | MediaList llDiffsToDelete;
|
---|
2512 |
|
---|
2513 | for (std::list< ComObjPtr<MediumAttachment> >::iterator it = llDiffAttachmentsToDelete.begin();
|
---|
2514 | it != llDiffAttachmentsToDelete.end();
|
---|
2515 | ++it)
|
---|
2516 | {
|
---|
2517 | ComObjPtr<MediumAttachment> pAttach = *it; // guaranteed to have only attachments where medium != NULL
|
---|
2518 | ComObjPtr<Medium> pMedium = pAttach->i_getMedium();
|
---|
2519 |
|
---|
2520 | AutoWriteLock mlock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
2521 |
|
---|
2522 | LogFlowThisFunc(("Detaching old current state in differencing image '%s'\n", pMedium->i_getName().c_str()));
|
---|
2523 |
|
---|
2524 | // Normally we "detach" the medium by removing the attachment object
|
---|
2525 | // from the current machine data; i_saveSettings() below would then
|
---|
2526 | // compare the current machine data with the one in the backup
|
---|
2527 | // and actually call Medium::removeBackReference(). But that works only half
|
---|
2528 | // the time in our case so instead we force a detachment here:
|
---|
2529 | // remove from machine data
|
---|
2530 | mMediumAttachments->remove(pAttach);
|
---|
2531 | // Remove it from the backup or else i_saveSettings will try to detach
|
---|
2532 | // it again and assert. The paranoia check avoids crashes (see
|
---|
2533 | // assert above) if this code is buggy and saves settings in the
|
---|
2534 | // wrong place.
|
---|
2535 | if (mMediumAttachments.isBackedUp())
|
---|
2536 | mMediumAttachments.backedUpData()->remove(pAttach);
|
---|
2537 | // then clean up backrefs
|
---|
2538 | pMedium->i_removeBackReference(mData->mUuid);
|
---|
2539 |
|
---|
2540 | llDiffsToDelete.push_back(pMedium);
|
---|
2541 | }
|
---|
2542 |
|
---|
2543 | // save machine settings, reset the modified flag and commit;
|
---|
2544 | bool fNeedsGlobalSaveSettings = false;
|
---|
2545 | rc = i_saveSettings(&fNeedsGlobalSaveSettings, alock,
|
---|
2546 | SaveS_ResetCurStateModified);
|
---|
2547 | if (FAILED(rc))
|
---|
2548 | throw rc;
|
---|
2549 |
|
---|
2550 | // release the locks before updating registry and deleting image files
|
---|
2551 | alock.release();
|
---|
2552 |
|
---|
2553 | // unconditionally add the parent registry.
|
---|
2554 | mParent->i_markRegistryModified(mParent->i_getGlobalRegistryId());
|
---|
2555 |
|
---|
2556 | // from here on we cannot roll back on failure any more
|
---|
2557 |
|
---|
2558 | for (MediaList::iterator it = llDiffsToDelete.begin();
|
---|
2559 | it != llDiffsToDelete.end();
|
---|
2560 | ++it)
|
---|
2561 | {
|
---|
2562 | ComObjPtr<Medium> &pMedium = *it;
|
---|
2563 | LogFlowThisFunc(("Deleting old current state in differencing image '%s'\n", pMedium->i_getName().c_str()));
|
---|
2564 |
|
---|
2565 | ComObjPtr<Medium> pParent = pMedium->i_getParent();
|
---|
2566 | // store the id here because it becomes NULL after deleting storage.
|
---|
2567 | com::Guid id = pMedium->i_getId();
|
---|
2568 | HRESULT rc2 = pMedium->i_deleteStorage(NULL /* aProgress */,
|
---|
2569 | true /* aWait */,
|
---|
2570 | false /* aNotify */);
|
---|
2571 | // ignore errors here because we cannot roll back after i_saveSettings() above
|
---|
2572 | if (SUCCEEDED(rc2))
|
---|
2573 | {
|
---|
2574 | pMediumsForNotify.insert(pParent);
|
---|
2575 | uIdsForNotify[id] = std::pair<DeviceType_T, BOOL>(pMedium->i_getDeviceType(), FALSE);
|
---|
2576 | pMedium->uninit();
|
---|
2577 | }
|
---|
2578 | }
|
---|
2579 | }
|
---|
2580 | catch (HRESULT aRC)
|
---|
2581 | {
|
---|
2582 | rc = aRC;
|
---|
2583 | }
|
---|
2584 |
|
---|
2585 | if (FAILED(rc))
|
---|
2586 | {
|
---|
2587 | /* preserve existing error info */
|
---|
2588 | ErrorInfoKeeper eik;
|
---|
2589 |
|
---|
2590 | /* undo all changes on failure */
|
---|
2591 | i_rollback(false /* aNotify */);
|
---|
2592 |
|
---|
2593 | }
|
---|
2594 |
|
---|
2595 | mParent->i_saveModifiedRegistries();
|
---|
2596 |
|
---|
2597 | /* restore the machine state */
|
---|
2598 | i_setMachineState(task.m_machineStateBackup);
|
---|
2599 |
|
---|
2600 | /* set the result (this will try to fetch current error info on failure) */
|
---|
2601 | task.m_pProgress->i_notifyComplete(rc);
|
---|
2602 |
|
---|
2603 | if (SUCCEEDED(rc))
|
---|
2604 | {
|
---|
2605 | mParent->i_onSnapshotRestored(mData->mUuid, snapshotId);
|
---|
2606 | for (std::map<Guid, std::pair<DeviceType_T, BOOL> >::const_iterator it = uIdsForNotify.begin();
|
---|
2607 | it != uIdsForNotify.end();
|
---|
2608 | ++it)
|
---|
2609 | {
|
---|
2610 | mParent->i_onMediumRegistered(it->first, it->second.first, it->second.second);
|
---|
2611 | }
|
---|
2612 | for (std::set<ComObjPtr<Medium> >::const_iterator it = pMediumsForNotify.begin();
|
---|
2613 | it != pMediumsForNotify.end();
|
---|
2614 | ++it)
|
---|
2615 | {
|
---|
2616 | if (it->isNotNull())
|
---|
2617 | mParent->i_onMediumConfigChanged(*it);
|
---|
2618 | }
|
---|
2619 | }
|
---|
2620 |
|
---|
2621 | LogFlowThisFunc(("Done restoring snapshot (rc=%08X)\n", rc));
|
---|
2622 |
|
---|
2623 | LogFlowThisFuncLeave();
|
---|
2624 | }
|
---|
2625 |
|
---|
2626 | ////////////////////////////////////////////////////////////////////////////////
|
---|
2627 | //
|
---|
2628 | // DeleteSnapshot methods (SessionMachine and related tasks)
|
---|
2629 | //
|
---|
2630 | ////////////////////////////////////////////////////////////////////////////////
|
---|
2631 |
|
---|
2632 | HRESULT Machine::deleteSnapshot(const com::Guid &aId, ComPtr<IProgress> &aProgress)
|
---|
2633 | {
|
---|
2634 | NOREF(aId);
|
---|
2635 | NOREF(aProgress);
|
---|
2636 | ReturnComNotImplemented();
|
---|
2637 | }
|
---|
2638 |
|
---|
2639 | HRESULT SessionMachine::deleteSnapshot(const com::Guid &aId, ComPtr<IProgress> &aProgress)
|
---|
2640 | {
|
---|
2641 | return i_deleteSnapshot(aId, aId,
|
---|
2642 | FALSE /* fDeleteAllChildren */,
|
---|
2643 | aProgress);
|
---|
2644 | }
|
---|
2645 |
|
---|
2646 | HRESULT Machine::deleteSnapshotAndAllChildren(const com::Guid &aId, ComPtr<IProgress> &aProgress)
|
---|
2647 | {
|
---|
2648 | NOREF(aId);
|
---|
2649 | NOREF(aProgress);
|
---|
2650 | ReturnComNotImplemented();
|
---|
2651 | }
|
---|
2652 |
|
---|
2653 | HRESULT SessionMachine::deleteSnapshotAndAllChildren(const com::Guid &aId, ComPtr<IProgress> &aProgress)
|
---|
2654 | {
|
---|
2655 | return i_deleteSnapshot(aId, aId,
|
---|
2656 | TRUE /* fDeleteAllChildren */,
|
---|
2657 | aProgress);
|
---|
2658 | }
|
---|
2659 |
|
---|
2660 | HRESULT Machine::deleteSnapshotRange(const com::Guid &aStartId, const com::Guid &aEndId, ComPtr<IProgress> &aProgress)
|
---|
2661 | {
|
---|
2662 | NOREF(aStartId);
|
---|
2663 | NOREF(aEndId);
|
---|
2664 | NOREF(aProgress);
|
---|
2665 | ReturnComNotImplemented();
|
---|
2666 | }
|
---|
2667 |
|
---|
2668 | HRESULT SessionMachine::deleteSnapshotRange(const com::Guid &aStartId, const com::Guid &aEndId, ComPtr<IProgress> &aProgress)
|
---|
2669 | {
|
---|
2670 | return i_deleteSnapshot(aStartId, aEndId,
|
---|
2671 | FALSE /* fDeleteAllChildren */,
|
---|
2672 | aProgress);
|
---|
2673 | }
|
---|
2674 |
|
---|
2675 |
|
---|
2676 | /**
|
---|
2677 | * Implementation for SessionMachine::i_deleteSnapshot().
|
---|
2678 | *
|
---|
2679 | * Gets called from SessionMachine::DeleteSnapshot(). Deleting a snapshot
|
---|
2680 | * happens entirely on the server side if the machine is not running, and
|
---|
2681 | * if it is running then the merges are done via internal session callbacks.
|
---|
2682 | *
|
---|
2683 | * This creates a new thread that does the work and returns a progress
|
---|
2684 | * object to the client.
|
---|
2685 | *
|
---|
2686 | * Actual work then takes place in SessionMachine::i_deleteSnapshotHandler().
|
---|
2687 | *
|
---|
2688 | * @note Locks mParent + this + children objects for writing!
|
---|
2689 | */
|
---|
2690 | HRESULT SessionMachine::i_deleteSnapshot(const com::Guid &aStartId,
|
---|
2691 | const com::Guid &aEndId,
|
---|
2692 | BOOL aDeleteAllChildren,
|
---|
2693 | ComPtr<IProgress> &aProgress)
|
---|
2694 | {
|
---|
2695 | LogFlowThisFuncEnter();
|
---|
2696 |
|
---|
2697 | AssertReturn(!aStartId.isZero() && !aEndId.isZero() && aStartId.isValid() && aEndId.isValid(), E_INVALIDARG);
|
---|
2698 |
|
---|
2699 | /** @todo implement the "and all children" and "range" variants */
|
---|
2700 | if (aDeleteAllChildren || aStartId != aEndId)
|
---|
2701 | ReturnComNotImplemented();
|
---|
2702 |
|
---|
2703 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2704 |
|
---|
2705 | if (Global::IsTransient(mData->mMachineState))
|
---|
2706 | return setError(VBOX_E_INVALID_VM_STATE,
|
---|
2707 | tr("Cannot delete a snapshot of the machine while it is changing the state (machine state: %s)"),
|
---|
2708 | Global::stringifyMachineState(mData->mMachineState));
|
---|
2709 |
|
---|
2710 | // be very picky about machine states
|
---|
2711 | if ( Global::IsOnlineOrTransient(mData->mMachineState)
|
---|
2712 | && mData->mMachineState != MachineState_PoweredOff
|
---|
2713 | && mData->mMachineState != MachineState_Saved
|
---|
2714 | && mData->mMachineState != MachineState_Teleported
|
---|
2715 | && mData->mMachineState != MachineState_Aborted
|
---|
2716 | && mData->mMachineState != MachineState_AbortedSaved
|
---|
2717 | && mData->mMachineState != MachineState_Running
|
---|
2718 | && mData->mMachineState != MachineState_Paused)
|
---|
2719 | return setError(VBOX_E_INVALID_VM_STATE,
|
---|
2720 | tr("Invalid machine state: %s"),
|
---|
2721 | Global::stringifyMachineState(mData->mMachineState));
|
---|
2722 |
|
---|
2723 | HRESULT rc = i_checkStateDependency(MutableOrSavedOrRunningStateDep);
|
---|
2724 | if (FAILED(rc))
|
---|
2725 | return rc;
|
---|
2726 |
|
---|
2727 | ComObjPtr<Snapshot> pSnapshot;
|
---|
2728 | rc = i_findSnapshotById(aStartId, pSnapshot, true /* aSetError */);
|
---|
2729 | if (FAILED(rc))
|
---|
2730 | return rc;
|
---|
2731 |
|
---|
2732 | AutoWriteLock snapshotLock(pSnapshot COMMA_LOCKVAL_SRC_POS);
|
---|
2733 | Utf8Str str;
|
---|
2734 |
|
---|
2735 | size_t childrenCount = pSnapshot->i_getChildrenCount();
|
---|
2736 | if (childrenCount > 1)
|
---|
2737 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2738 | 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",
|
---|
2739 | "", childrenCount),
|
---|
2740 | pSnapshot->i_getName().c_str(),
|
---|
2741 | mUserData->s.strName.c_str(),
|
---|
2742 | childrenCount);
|
---|
2743 |
|
---|
2744 | if (pSnapshot == mData->mCurrentSnapshot && childrenCount >= 1)
|
---|
2745 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2746 | tr("Snapshot '%s' of the machine '%s' cannot be deleted, because it is the current snapshot and has one child snapshot"),
|
---|
2747 | pSnapshot->i_getName().c_str(),
|
---|
2748 | mUserData->s.strName.c_str());
|
---|
2749 |
|
---|
2750 | /* If the snapshot being deleted is the current one, ensure current
|
---|
2751 | * settings are committed and saved.
|
---|
2752 | */
|
---|
2753 | if (pSnapshot == mData->mCurrentSnapshot)
|
---|
2754 | {
|
---|
2755 | if (mData->flModifications)
|
---|
2756 | {
|
---|
2757 | snapshotLock.release();
|
---|
2758 | rc = i_saveSettings(NULL, alock);
|
---|
2759 | snapshotLock.acquire();
|
---|
2760 | // no need to change for whether VirtualBox.xml needs saving since
|
---|
2761 | // we can't have a machine XML rename pending at this point
|
---|
2762 | if (FAILED(rc)) return rc;
|
---|
2763 | }
|
---|
2764 | }
|
---|
2765 |
|
---|
2766 | ComObjPtr<SnapshotMachine> pSnapMachine = pSnapshot->i_getSnapshotMachine();
|
---|
2767 |
|
---|
2768 | /* create a progress object. The number of operations is:
|
---|
2769 | * 1 (preparing) + 1 if the snapshot is online + # of normal hard disks
|
---|
2770 | */
|
---|
2771 | LogFlowThisFunc(("Going thru snapshot machine attachments to determine progress setup\n"));
|
---|
2772 |
|
---|
2773 | ULONG ulOpCount = 1; // one for preparations
|
---|
2774 | ULONG ulTotalWeight = 1; // one for preparations
|
---|
2775 |
|
---|
2776 | if (pSnapshot->i_getStateFilePath().isNotEmpty())
|
---|
2777 | {
|
---|
2778 | ++ulOpCount;
|
---|
2779 | ++ulTotalWeight; // assume 1 MB for deleting the state file
|
---|
2780 | }
|
---|
2781 |
|
---|
2782 | bool fDeleteOnline = mData->mMachineState == MachineState_Running || mData->mMachineState == MachineState_Paused;
|
---|
2783 |
|
---|
2784 | // count normal hard disks and add their sizes to the weight
|
---|
2785 | for (MediumAttachmentList::iterator
|
---|
2786 | it = pSnapMachine->mMediumAttachments->begin();
|
---|
2787 | it != pSnapMachine->mMediumAttachments->end();
|
---|
2788 | ++it)
|
---|
2789 | {
|
---|
2790 | ComObjPtr<MediumAttachment> &pAttach = *it;
|
---|
2791 | AutoReadLock attachLock(pAttach COMMA_LOCKVAL_SRC_POS);
|
---|
2792 | if (pAttach->i_getType() == DeviceType_HardDisk)
|
---|
2793 | {
|
---|
2794 | ComObjPtr<Medium> pHD = pAttach->i_getMedium();
|
---|
2795 | Assert(pHD);
|
---|
2796 | AutoReadLock mlock(pHD COMMA_LOCKVAL_SRC_POS);
|
---|
2797 |
|
---|
2798 | MediumType_T type = pHD->i_getType();
|
---|
2799 | // writethrough and shareable images are unaffected by snapshots,
|
---|
2800 | // so do nothing for them
|
---|
2801 | if ( type != MediumType_Writethrough
|
---|
2802 | && type != MediumType_Shareable
|
---|
2803 | && type != MediumType_Readonly)
|
---|
2804 | {
|
---|
2805 | // normal or immutable media need attention
|
---|
2806 | ++ulOpCount;
|
---|
2807 | // offline merge includes medium resizing
|
---|
2808 | if (!fDeleteOnline)
|
---|
2809 | ++ulOpCount;
|
---|
2810 | ulTotalWeight += (ULONG)(pHD->i_getSize() / _1M);
|
---|
2811 | }
|
---|
2812 | LogFlowThisFunc(("op %d: considering hard disk attachment %s\n", ulOpCount, pHD->i_getName().c_str()));
|
---|
2813 | }
|
---|
2814 | }
|
---|
2815 |
|
---|
2816 | ComObjPtr<Progress> pProgress;
|
---|
2817 | pProgress.createObject();
|
---|
2818 | pProgress->init(mParent, static_cast<IMachine*>(this),
|
---|
2819 | BstrFmt(tr("Deleting snapshot '%s'"), pSnapshot->i_getName().c_str()).raw(),
|
---|
2820 | FALSE /* aCancelable */,
|
---|
2821 | ulOpCount,
|
---|
2822 | ulTotalWeight,
|
---|
2823 | Bstr(tr("Setting up")).raw(),
|
---|
2824 | 1);
|
---|
2825 |
|
---|
2826 | /* create and start the task on a separate thread */
|
---|
2827 | DeleteSnapshotTask *pTask = new DeleteSnapshotTask(this, pProgress,
|
---|
2828 | "DeleteSnap",
|
---|
2829 | fDeleteOnline,
|
---|
2830 | pSnapshot);
|
---|
2831 | rc = pTask->createThread();
|
---|
2832 | pTask = NULL;
|
---|
2833 | if (FAILED(rc))
|
---|
2834 | return rc;
|
---|
2835 |
|
---|
2836 | // the task might start running but will block on acquiring the machine's write lock
|
---|
2837 | // which we acquired above; once this function leaves, the task will be unblocked;
|
---|
2838 | // set the proper machine state here now (note: after creating a Task instance)
|
---|
2839 | if (mData->mMachineState == MachineState_Running)
|
---|
2840 | {
|
---|
2841 | i_setMachineState(MachineState_DeletingSnapshotOnline);
|
---|
2842 | i_updateMachineStateOnClient();
|
---|
2843 | }
|
---|
2844 | else if (mData->mMachineState == MachineState_Paused)
|
---|
2845 | {
|
---|
2846 | i_setMachineState(MachineState_DeletingSnapshotPaused);
|
---|
2847 | i_updateMachineStateOnClient();
|
---|
2848 | }
|
---|
2849 | else
|
---|
2850 | i_setMachineState(MachineState_DeletingSnapshot);
|
---|
2851 |
|
---|
2852 | /* return the progress to the caller */
|
---|
2853 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2854 |
|
---|
2855 | LogFlowThisFuncLeave();
|
---|
2856 |
|
---|
2857 | return S_OK;
|
---|
2858 | }
|
---|
2859 |
|
---|
2860 | /**
|
---|
2861 | * Helper struct for SessionMachine::deleteSnapshotHandler().
|
---|
2862 | */
|
---|
2863 | struct MediumDeleteRec
|
---|
2864 | {
|
---|
2865 | MediumDeleteRec()
|
---|
2866 | : mfNeedsOnlineMerge(false),
|
---|
2867 | mpMediumLockList(NULL)
|
---|
2868 | {}
|
---|
2869 |
|
---|
2870 | MediumDeleteRec(const ComObjPtr<Medium> &aHd,
|
---|
2871 | const ComObjPtr<Medium> &aSource,
|
---|
2872 | const ComObjPtr<Medium> &aTarget,
|
---|
2873 | const ComObjPtr<MediumAttachment> &aOnlineMediumAttachment,
|
---|
2874 | bool fMergeForward,
|
---|
2875 | const ComObjPtr<Medium> &aParentForTarget,
|
---|
2876 | MediumLockList *aChildrenToReparent,
|
---|
2877 | bool fNeedsOnlineMerge,
|
---|
2878 | MediumLockList *aMediumLockList,
|
---|
2879 | const ComPtr<IToken> &aHDLockToken)
|
---|
2880 | : mpHD(aHd),
|
---|
2881 | mpSource(aSource),
|
---|
2882 | mpTarget(aTarget),
|
---|
2883 | mpOnlineMediumAttachment(aOnlineMediumAttachment),
|
---|
2884 | mfMergeForward(fMergeForward),
|
---|
2885 | mpParentForTarget(aParentForTarget),
|
---|
2886 | mpChildrenToReparent(aChildrenToReparent),
|
---|
2887 | mfNeedsOnlineMerge(fNeedsOnlineMerge),
|
---|
2888 | mpMediumLockList(aMediumLockList),
|
---|
2889 | mpHDLockToken(aHDLockToken)
|
---|
2890 | {}
|
---|
2891 |
|
---|
2892 | MediumDeleteRec(const ComObjPtr<Medium> &aHd,
|
---|
2893 | const ComObjPtr<Medium> &aSource,
|
---|
2894 | const ComObjPtr<Medium> &aTarget,
|
---|
2895 | const ComObjPtr<MediumAttachment> &aOnlineMediumAttachment,
|
---|
2896 | bool fMergeForward,
|
---|
2897 | const ComObjPtr<Medium> &aParentForTarget,
|
---|
2898 | MediumLockList *aChildrenToReparent,
|
---|
2899 | bool fNeedsOnlineMerge,
|
---|
2900 | MediumLockList *aMediumLockList,
|
---|
2901 | const ComPtr<IToken> &aHDLockToken,
|
---|
2902 | const Guid &aMachineId,
|
---|
2903 | const Guid &aSnapshotId)
|
---|
2904 | : mpHD(aHd),
|
---|
2905 | mpSource(aSource),
|
---|
2906 | mpTarget(aTarget),
|
---|
2907 | mpOnlineMediumAttachment(aOnlineMediumAttachment),
|
---|
2908 | mfMergeForward(fMergeForward),
|
---|
2909 | mpParentForTarget(aParentForTarget),
|
---|
2910 | mpChildrenToReparent(aChildrenToReparent),
|
---|
2911 | mfNeedsOnlineMerge(fNeedsOnlineMerge),
|
---|
2912 | mpMediumLockList(aMediumLockList),
|
---|
2913 | mpHDLockToken(aHDLockToken),
|
---|
2914 | mMachineId(aMachineId),
|
---|
2915 | mSnapshotId(aSnapshotId)
|
---|
2916 | {}
|
---|
2917 |
|
---|
2918 | ComObjPtr<Medium> mpHD;
|
---|
2919 | ComObjPtr<Medium> mpSource;
|
---|
2920 | ComObjPtr<Medium> mpTarget;
|
---|
2921 | ComObjPtr<MediumAttachment> mpOnlineMediumAttachment;
|
---|
2922 | bool mfMergeForward;
|
---|
2923 | ComObjPtr<Medium> mpParentForTarget;
|
---|
2924 | MediumLockList *mpChildrenToReparent;
|
---|
2925 | bool mfNeedsOnlineMerge;
|
---|
2926 | MediumLockList *mpMediumLockList;
|
---|
2927 | /** optional lock token, used only in case mpHD is not merged/deleted */
|
---|
2928 | ComPtr<IToken> mpHDLockToken;
|
---|
2929 | /* these are for reattaching the hard disk in case of a failure: */
|
---|
2930 | Guid mMachineId;
|
---|
2931 | Guid mSnapshotId;
|
---|
2932 | };
|
---|
2933 |
|
---|
2934 | typedef std::list<MediumDeleteRec> MediumDeleteRecList;
|
---|
2935 |
|
---|
2936 | /**
|
---|
2937 | * Worker method for the delete snapshot thread created by
|
---|
2938 | * SessionMachine::DeleteSnapshot(). This method gets called indirectly
|
---|
2939 | * through SessionMachine::taskHandler() which then calls
|
---|
2940 | * DeleteSnapshotTask::handler().
|
---|
2941 | *
|
---|
2942 | * The DeleteSnapshotTask contains the progress object returned to the console
|
---|
2943 | * by SessionMachine::DeleteSnapshot, through which progress and results are
|
---|
2944 | * reported.
|
---|
2945 | *
|
---|
2946 | * SessionMachine::DeleteSnapshot() has set the machine state to
|
---|
2947 | * MachineState_DeletingSnapshot right after creating this task. Since we block
|
---|
2948 | * on the machine write lock at the beginning, once that has been acquired, we
|
---|
2949 | * can assume that the machine state is indeed that.
|
---|
2950 | *
|
---|
2951 | * @note Locks the machine + the snapshot + the media tree for writing!
|
---|
2952 | *
|
---|
2953 | * @param task Task data.
|
---|
2954 | */
|
---|
2955 | void SessionMachine::i_deleteSnapshotHandler(DeleteSnapshotTask &task)
|
---|
2956 | {
|
---|
2957 | LogFlowThisFuncEnter();
|
---|
2958 |
|
---|
2959 | MultiResult mrc(S_OK);
|
---|
2960 | AutoCaller autoCaller(this);
|
---|
2961 | LogFlowThisFunc(("state=%d\n", getObjectState().getState()));
|
---|
2962 | if (FAILED(autoCaller.rc()))
|
---|
2963 | {
|
---|
2964 | /* we might have been uninitialized because the session was accidentally
|
---|
2965 | * closed by the client, so don't assert */
|
---|
2966 | mrc = setError(E_FAIL,
|
---|
2967 | tr("The session has been accidentally closed"));
|
---|
2968 | task.m_pProgress->i_notifyComplete(mrc);
|
---|
2969 | LogFlowThisFuncLeave();
|
---|
2970 | return;
|
---|
2971 | }
|
---|
2972 |
|
---|
2973 | MediumDeleteRecList toDelete;
|
---|
2974 | Guid snapshotId;
|
---|
2975 | std::set<ComObjPtr<Medium> > pMediumsForNotify;
|
---|
2976 | std::map<Guid,DeviceType_T> uIdsForNotify;
|
---|
2977 |
|
---|
2978 | try
|
---|
2979 | {
|
---|
2980 | HRESULT rc = S_OK;
|
---|
2981 |
|
---|
2982 | /* Locking order: */
|
---|
2983 | AutoMultiWriteLock2 multiLock(this->lockHandle(), // machine
|
---|
2984 | task.m_pSnapshot->lockHandle() // snapshot
|
---|
2985 | COMMA_LOCKVAL_SRC_POS);
|
---|
2986 | // once we have this lock, we know that SessionMachine::DeleteSnapshot()
|
---|
2987 | // has exited after setting the machine state to MachineState_DeletingSnapshot
|
---|
2988 |
|
---|
2989 | AutoWriteLock treeLock(mParent->i_getMediaTreeLockHandle()
|
---|
2990 | COMMA_LOCKVAL_SRC_POS);
|
---|
2991 |
|
---|
2992 | ComObjPtr<SnapshotMachine> pSnapMachine = task.m_pSnapshot->i_getSnapshotMachine();
|
---|
2993 | // no need to lock the snapshot machine since it is const by definition
|
---|
2994 | Guid machineId = pSnapMachine->i_getId();
|
---|
2995 |
|
---|
2996 | // save the snapshot ID (for callbacks)
|
---|
2997 | snapshotId = task.m_pSnapshot->i_getId();
|
---|
2998 |
|
---|
2999 | // first pass:
|
---|
3000 | LogFlowThisFunc(("1: Checking hard disk merge prerequisites...\n"));
|
---|
3001 |
|
---|
3002 | // Go thru the attachments of the snapshot machine (the media in here
|
---|
3003 | // point to the disk states _before_ the snapshot was taken, i.e. the
|
---|
3004 | // state we're restoring to; for each such medium, we will need to
|
---|
3005 | // merge it with its one and only child (the diff image holding the
|
---|
3006 | // changes written after the snapshot was taken).
|
---|
3007 | for (MediumAttachmentList::iterator
|
---|
3008 | it = pSnapMachine->mMediumAttachments->begin();
|
---|
3009 | it != pSnapMachine->mMediumAttachments->end();
|
---|
3010 | ++it)
|
---|
3011 | {
|
---|
3012 | ComObjPtr<MediumAttachment> &pAttach = *it;
|
---|
3013 | AutoReadLock attachLock(pAttach COMMA_LOCKVAL_SRC_POS);
|
---|
3014 | if (pAttach->i_getType() != DeviceType_HardDisk)
|
---|
3015 | continue;
|
---|
3016 |
|
---|
3017 | ComObjPtr<Medium> pHD = pAttach->i_getMedium();
|
---|
3018 | Assert(!pHD.isNull());
|
---|
3019 |
|
---|
3020 | {
|
---|
3021 | // writethrough, shareable and readonly images are
|
---|
3022 | // unaffected by snapshots, skip them
|
---|
3023 | AutoReadLock medlock(pHD COMMA_LOCKVAL_SRC_POS);
|
---|
3024 | MediumType_T type = pHD->i_getType();
|
---|
3025 | if ( type == MediumType_Writethrough
|
---|
3026 | || type == MediumType_Shareable
|
---|
3027 | || type == MediumType_Readonly)
|
---|
3028 | continue;
|
---|
3029 | }
|
---|
3030 |
|
---|
3031 | #ifdef DEBUG
|
---|
3032 | pHD->i_dumpBackRefs();
|
---|
3033 | #endif
|
---|
3034 |
|
---|
3035 | // needs to be merged with child or deleted, check prerequisites
|
---|
3036 | ComObjPtr<Medium> pTarget;
|
---|
3037 | ComObjPtr<Medium> pSource;
|
---|
3038 | bool fMergeForward = false;
|
---|
3039 | ComObjPtr<Medium> pParentForTarget;
|
---|
3040 | MediumLockList *pChildrenToReparent = NULL;
|
---|
3041 | bool fNeedsOnlineMerge = false;
|
---|
3042 | bool fOnlineMergePossible = task.m_fDeleteOnline;
|
---|
3043 | MediumLockList *pMediumLockList = NULL;
|
---|
3044 | MediumLockList *pVMMALockList = NULL;
|
---|
3045 | ComPtr<IToken> pHDLockToken;
|
---|
3046 | ComObjPtr<MediumAttachment> pOnlineMediumAttachment;
|
---|
3047 | if (fOnlineMergePossible)
|
---|
3048 | {
|
---|
3049 | // Look up the corresponding medium attachment in the currently
|
---|
3050 | // running VM. Any failure prevents a live merge. Could be made
|
---|
3051 | // a tad smarter by trying a few candidates, so that e.g. disks
|
---|
3052 | // which are simply moved to a different controller slot do not
|
---|
3053 | // prevent online merging in general.
|
---|
3054 | pOnlineMediumAttachment =
|
---|
3055 | i_findAttachment(*mMediumAttachments.data(),
|
---|
3056 | pAttach->i_getControllerName(),
|
---|
3057 | pAttach->i_getPort(),
|
---|
3058 | pAttach->i_getDevice());
|
---|
3059 | if (pOnlineMediumAttachment)
|
---|
3060 | {
|
---|
3061 | rc = mData->mSession.mLockedMedia.Get(pOnlineMediumAttachment,
|
---|
3062 | pVMMALockList);
|
---|
3063 | if (FAILED(rc))
|
---|
3064 | fOnlineMergePossible = false;
|
---|
3065 | }
|
---|
3066 | else
|
---|
3067 | fOnlineMergePossible = false;
|
---|
3068 | }
|
---|
3069 |
|
---|
3070 | // no need to hold the lock any longer
|
---|
3071 | attachLock.release();
|
---|
3072 |
|
---|
3073 | treeLock.release();
|
---|
3074 | rc = i_prepareDeleteSnapshotMedium(pHD, machineId, snapshotId,
|
---|
3075 | fOnlineMergePossible,
|
---|
3076 | pVMMALockList, pSource, pTarget,
|
---|
3077 | fMergeForward, pParentForTarget,
|
---|
3078 | pChildrenToReparent,
|
---|
3079 | fNeedsOnlineMerge,
|
---|
3080 | pMediumLockList,
|
---|
3081 | pHDLockToken);
|
---|
3082 | treeLock.acquire();
|
---|
3083 | if (FAILED(rc))
|
---|
3084 | throw rc;
|
---|
3085 |
|
---|
3086 | // For simplicity, prepareDeleteSnapshotMedium selects the merge
|
---|
3087 | // direction in the following way: we merge pHD onto its child
|
---|
3088 | // (forward merge), not the other way round, because that saves us
|
---|
3089 | // from unnecessarily shuffling around the attachments for the
|
---|
3090 | // machine that follows the snapshot (next snapshot or current
|
---|
3091 | // state), unless it's a base image. Backwards merges of the first
|
---|
3092 | // snapshot into the base image is essential, as it ensures that
|
---|
3093 | // when all snapshots are deleted the only remaining image is a
|
---|
3094 | // base image. Important e.g. for medium formats which do not have
|
---|
3095 | // a file representation such as iSCSI.
|
---|
3096 |
|
---|
3097 | // not going to merge a big source into a small target on online merge. Otherwise it will be resized
|
---|
3098 | if (fNeedsOnlineMerge && pSource->i_getLogicalSize() > pTarget->i_getLogicalSize())
|
---|
3099 | {
|
---|
3100 | rc = setError(E_FAIL,
|
---|
3101 | 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",
|
---|
3102 | "", pSource->i_getLogicalSize()),
|
---|
3103 | pTarget->i_getLocationFull().c_str(), pSource->i_getLogicalSize());
|
---|
3104 | throw rc;
|
---|
3105 | }
|
---|
3106 |
|
---|
3107 | // a couple paranoia checks for backward merges
|
---|
3108 | if (pMediumLockList != NULL && !fMergeForward)
|
---|
3109 | {
|
---|
3110 | // parent is null -> this disk is a base hard disk: we will
|
---|
3111 | // then do a backward merge, i.e. merge its only child onto the
|
---|
3112 | // base disk. Here we need then to update the attachment that
|
---|
3113 | // refers to the child and have it point to the parent instead
|
---|
3114 | Assert(pHD->i_getChildren().size() == 1);
|
---|
3115 |
|
---|
3116 | ComObjPtr<Medium> pReplaceHD = pHD->i_getChildren().front();
|
---|
3117 |
|
---|
3118 | ComAssertThrow(pReplaceHD == pSource, E_FAIL);
|
---|
3119 | }
|
---|
3120 |
|
---|
3121 | Guid replaceMachineId;
|
---|
3122 | Guid replaceSnapshotId;
|
---|
3123 |
|
---|
3124 | const Guid *pReplaceMachineId = pSource->i_getFirstMachineBackrefId();
|
---|
3125 | // minimal sanity checking
|
---|
3126 | Assert(!pReplaceMachineId || *pReplaceMachineId == mData->mUuid);
|
---|
3127 | if (pReplaceMachineId)
|
---|
3128 | replaceMachineId = *pReplaceMachineId;
|
---|
3129 |
|
---|
3130 | const Guid *pSnapshotId = pSource->i_getFirstMachineBackrefSnapshotId();
|
---|
3131 | if (pSnapshotId)
|
---|
3132 | replaceSnapshotId = *pSnapshotId;
|
---|
3133 |
|
---|
3134 | if (replaceMachineId.isValid() && !replaceMachineId.isZero())
|
---|
3135 | {
|
---|
3136 | // Adjust the backreferences, otherwise merging will assert.
|
---|
3137 | // Note that the medium attachment object stays associated
|
---|
3138 | // with the snapshot until the merge was successful.
|
---|
3139 | HRESULT rc2 = S_OK;
|
---|
3140 | rc2 = pSource->i_removeBackReference(replaceMachineId, replaceSnapshotId);
|
---|
3141 | AssertComRC(rc2);
|
---|
3142 |
|
---|
3143 | toDelete.push_back(MediumDeleteRec(pHD, pSource, pTarget,
|
---|
3144 | pOnlineMediumAttachment,
|
---|
3145 | fMergeForward,
|
---|
3146 | pParentForTarget,
|
---|
3147 | pChildrenToReparent,
|
---|
3148 | fNeedsOnlineMerge,
|
---|
3149 | pMediumLockList,
|
---|
3150 | pHDLockToken,
|
---|
3151 | replaceMachineId,
|
---|
3152 | replaceSnapshotId));
|
---|
3153 | }
|
---|
3154 | else
|
---|
3155 | toDelete.push_back(MediumDeleteRec(pHD, pSource, pTarget,
|
---|
3156 | pOnlineMediumAttachment,
|
---|
3157 | fMergeForward,
|
---|
3158 | pParentForTarget,
|
---|
3159 | pChildrenToReparent,
|
---|
3160 | fNeedsOnlineMerge,
|
---|
3161 | pMediumLockList,
|
---|
3162 | pHDLockToken));
|
---|
3163 | }
|
---|
3164 |
|
---|
3165 | {
|
---|
3166 | /* check available space on the storage */
|
---|
3167 | RTFOFF pcbTotal = 0;
|
---|
3168 | RTFOFF pcbFree = 0;
|
---|
3169 | uint32_t pcbBlock = 0;
|
---|
3170 | uint32_t pcbSector = 0;
|
---|
3171 | std::multimap<uint32_t, uint64_t> neededStorageFreeSpace;
|
---|
3172 | std::map<uint32_t, const char*> serialMapToStoragePath;
|
---|
3173 |
|
---|
3174 | for (MediumDeleteRecList::const_iterator
|
---|
3175 | it = toDelete.begin();
|
---|
3176 | it != toDelete.end();
|
---|
3177 | ++it)
|
---|
3178 | {
|
---|
3179 | uint64_t diskSize = 0;
|
---|
3180 | uint32_t pu32Serial = 0;
|
---|
3181 | ComObjPtr<Medium> pSource_local = it->mpSource;
|
---|
3182 | ComObjPtr<Medium> pTarget_local = it->mpTarget;
|
---|
3183 | ComPtr<IMediumFormat> pTargetFormat;
|
---|
3184 |
|
---|
3185 | {
|
---|
3186 | if ( pSource_local.isNull()
|
---|
3187 | || pSource_local == pTarget_local)
|
---|
3188 | continue;
|
---|
3189 | }
|
---|
3190 |
|
---|
3191 | rc = pTarget_local->COMGETTER(MediumFormat)(pTargetFormat.asOutParam());
|
---|
3192 | if (FAILED(rc))
|
---|
3193 | throw rc;
|
---|
3194 |
|
---|
3195 | if (pTarget_local->i_isMediumFormatFile())
|
---|
3196 | {
|
---|
3197 | int vrc = RTFsQuerySerial(pTarget_local->i_getLocationFull().c_str(), &pu32Serial);
|
---|
3198 | if (RT_FAILURE(vrc))
|
---|
3199 | {
|
---|
3200 | rc = setError(E_FAIL,
|
---|
3201 | tr("Unable to merge storage '%s'. Can't get storage UID"),
|
---|
3202 | pTarget_local->i_getLocationFull().c_str());
|
---|
3203 | throw rc;
|
---|
3204 | }
|
---|
3205 |
|
---|
3206 | pSource_local->COMGETTER(Size)((LONG64*)&diskSize);
|
---|
3207 |
|
---|
3208 | /** @todo r=klaus this is too pessimistic... should take
|
---|
3209 | * the current size and maximum size of the target image
|
---|
3210 | * into account, because a X GB image with Y GB capacity
|
---|
3211 | * can only grow by Y-X GB (ignoring overhead, which
|
---|
3212 | * unfortunately is hard to estimate, some have next to
|
---|
3213 | * nothing, some have a certain percentage...) */
|
---|
3214 | /* store needed free space in multimap */
|
---|
3215 | neededStorageFreeSpace.insert(std::make_pair(pu32Serial, diskSize));
|
---|
3216 | /* linking storage UID with snapshot path, it is a helper container (just for easy finding needed path) */
|
---|
3217 | serialMapToStoragePath.insert(std::make_pair(pu32Serial, pTarget_local->i_getLocationFull().c_str()));
|
---|
3218 | }
|
---|
3219 | }
|
---|
3220 |
|
---|
3221 | while (!neededStorageFreeSpace.empty())
|
---|
3222 | {
|
---|
3223 | std::pair<std::multimap<uint32_t,uint64_t>::iterator,std::multimap<uint32_t,uint64_t>::iterator> ret;
|
---|
3224 | uint64_t commonSourceStoragesSize = 0;
|
---|
3225 |
|
---|
3226 | /* find all records in multimap with identical storage UID */
|
---|
3227 | ret = neededStorageFreeSpace.equal_range(neededStorageFreeSpace.begin()->first);
|
---|
3228 | std::multimap<uint32_t,uint64_t>::const_iterator it_ns = ret.first;
|
---|
3229 |
|
---|
3230 | for (; it_ns != ret.second ; ++it_ns)
|
---|
3231 | {
|
---|
3232 | commonSourceStoragesSize += it_ns->second;
|
---|
3233 | }
|
---|
3234 |
|
---|
3235 | /* find appropriate path by storage UID */
|
---|
3236 | std::map<uint32_t,const char*>::const_iterator it_sm = serialMapToStoragePath.find(ret.first->first);
|
---|
3237 | /* get info about a storage */
|
---|
3238 | if (it_sm == serialMapToStoragePath.end())
|
---|
3239 | {
|
---|
3240 | LogFlowThisFunc(("Path to the storage wasn't found...\n"));
|
---|
3241 |
|
---|
3242 | rc = setError(E_INVALIDARG,
|
---|
3243 | tr("Unable to merge storage '%s'. Path to the storage wasn't found"),
|
---|
3244 | it_sm->second);
|
---|
3245 | throw rc;
|
---|
3246 | }
|
---|
3247 |
|
---|
3248 | int vrc = RTFsQuerySizes(it_sm->second, &pcbTotal, &pcbFree, &pcbBlock, &pcbSector);
|
---|
3249 | if (RT_FAILURE(vrc))
|
---|
3250 | {
|
---|
3251 | rc = setError(E_FAIL,
|
---|
3252 | tr("Unable to merge storage '%s'. Can't get the storage size"),
|
---|
3253 | it_sm->second);
|
---|
3254 | throw rc;
|
---|
3255 | }
|
---|
3256 |
|
---|
3257 | if (commonSourceStoragesSize > (uint64_t)pcbFree)
|
---|
3258 | {
|
---|
3259 | LogFlowThisFunc(("Not enough free space to merge...\n"));
|
---|
3260 |
|
---|
3261 | rc = setError(E_OUTOFMEMORY,
|
---|
3262 | tr("Unable to merge storage '%s'. Not enough free storage space"),
|
---|
3263 | it_sm->second);
|
---|
3264 | throw rc;
|
---|
3265 | }
|
---|
3266 |
|
---|
3267 | neededStorageFreeSpace.erase(ret.first, ret.second);
|
---|
3268 | }
|
---|
3269 |
|
---|
3270 | serialMapToStoragePath.clear();
|
---|
3271 | }
|
---|
3272 |
|
---|
3273 | // we can release the locks now since the machine state is MachineState_DeletingSnapshot
|
---|
3274 | treeLock.release();
|
---|
3275 | multiLock.release();
|
---|
3276 |
|
---|
3277 | /* Now we checked that we can successfully merge all normal hard disks
|
---|
3278 | * (unless a runtime error like end-of-disc happens). Now get rid of
|
---|
3279 | * the saved state (if present), as that will free some disk space.
|
---|
3280 | * The snapshot itself will be deleted as late as possible, so that
|
---|
3281 | * the user can repeat the delete operation if he runs out of disk
|
---|
3282 | * space or cancels the delete operation. */
|
---|
3283 |
|
---|
3284 | /* second pass: */
|
---|
3285 | LogFlowThisFunc(("2: Deleting saved state...\n"));
|
---|
3286 |
|
---|
3287 | {
|
---|
3288 | // saveAllSnapshots() needs a machine lock, and the snapshots
|
---|
3289 | // tree is protected by the machine lock as well
|
---|
3290 | AutoWriteLock machineLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3291 |
|
---|
3292 | Utf8Str stateFilePath = task.m_pSnapshot->i_getStateFilePath();
|
---|
3293 | if (!stateFilePath.isEmpty())
|
---|
3294 | {
|
---|
3295 | task.m_pProgress->SetNextOperation(Bstr(tr("Deleting the execution state")).raw(),
|
---|
3296 | 1); // weight
|
---|
3297 |
|
---|
3298 | i_releaseSavedStateFile(stateFilePath, task.m_pSnapshot /* pSnapshotToIgnore */);
|
---|
3299 |
|
---|
3300 | // machine will need saving now
|
---|
3301 | machineLock.release();
|
---|
3302 | mParent->i_markRegistryModified(i_getId());
|
---|
3303 | }
|
---|
3304 | }
|
---|
3305 |
|
---|
3306 | /* third pass: */
|
---|
3307 | LogFlowThisFunc(("3: Performing actual hard disk merging...\n"));
|
---|
3308 |
|
---|
3309 | /// @todo NEWMEDIA turn the following errors into warnings because the
|
---|
3310 | /// snapshot itself has been already deleted (and interpret these
|
---|
3311 | /// warnings properly on the GUI side)
|
---|
3312 | for (MediumDeleteRecList::iterator it = toDelete.begin();
|
---|
3313 | it != toDelete.end();)
|
---|
3314 | {
|
---|
3315 | const ComObjPtr<Medium> &pMedium(it->mpHD);
|
---|
3316 | ULONG ulWeight;
|
---|
3317 |
|
---|
3318 | {
|
---|
3319 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
3320 | ulWeight = (ULONG)(pMedium->i_getSize() / _1M);
|
---|
3321 | }
|
---|
3322 |
|
---|
3323 | const char *pszOperationText = it->mfNeedsOnlineMerge ?
|
---|
3324 | tr("Merging differencing image '%s'")
|
---|
3325 | : tr("Resizing before merge differencing image '%s'");
|
---|
3326 |
|
---|
3327 | task.m_pProgress->SetNextOperation(BstrFmt(pszOperationText,
|
---|
3328 | pMedium->i_getName().c_str()).raw(),
|
---|
3329 | ulWeight);
|
---|
3330 |
|
---|
3331 | bool fNeedSourceUninit = false;
|
---|
3332 | bool fReparentTarget = false;
|
---|
3333 | if (it->mpMediumLockList == NULL)
|
---|
3334 | {
|
---|
3335 | /* no real merge needed, just updating state and delete
|
---|
3336 | * diff files if necessary */
|
---|
3337 | AutoMultiWriteLock2 mLock(&mParent->i_getMediaTreeLockHandle(), pMedium->lockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3338 |
|
---|
3339 | Assert( !it->mfMergeForward
|
---|
3340 | || pMedium->i_getChildren().size() == 0);
|
---|
3341 |
|
---|
3342 | /* Delete the differencing hard disk (has no children). Two
|
---|
3343 | * exceptions: if it's the last medium in the chain or if it's
|
---|
3344 | * a backward merge we don't want to handle due to complexity.
|
---|
3345 | * In both cases leave the image in place. If it's the first
|
---|
3346 | * exception the user can delete it later if he wants. */
|
---|
3347 | if (!pMedium->i_getParent().isNull())
|
---|
3348 | {
|
---|
3349 | Assert(pMedium->i_getState() == MediumState_Deleting);
|
---|
3350 | /* No need to hold the lock any longer. */
|
---|
3351 | mLock.release();
|
---|
3352 | ComObjPtr<Medium> pParent = pMedium->i_getParent();
|
---|
3353 | Guid uMedium = pMedium->i_getId();
|
---|
3354 | DeviceType_T uMediumType = pMedium->i_getDeviceType();
|
---|
3355 | rc = pMedium->i_deleteStorage(&task.m_pProgress,
|
---|
3356 | true /* aWait */,
|
---|
3357 | false /* aNotify */);
|
---|
3358 | if (FAILED(rc))
|
---|
3359 | throw rc;
|
---|
3360 |
|
---|
3361 | pMediumsForNotify.insert(pParent);
|
---|
3362 | uIdsForNotify[uMedium] = uMediumType;
|
---|
3363 |
|
---|
3364 | // need to uninit the deleted medium
|
---|
3365 | fNeedSourceUninit = true;
|
---|
3366 | }
|
---|
3367 | }
|
---|
3368 | else
|
---|
3369 | {
|
---|
3370 | {
|
---|
3371 | //store ids before merging for notify
|
---|
3372 | pMediumsForNotify.insert(it->mpTarget);
|
---|
3373 | if (it->mfMergeForward)
|
---|
3374 | pMediumsForNotify.insert(it->mpSource->i_getParent());
|
---|
3375 | else
|
---|
3376 | {
|
---|
3377 | //children which will be reparented to target
|
---|
3378 | for (MediaList::const_iterator iit = it->mpSource->i_getChildren().begin();
|
---|
3379 | iit != it->mpSource->i_getChildren().end();
|
---|
3380 | ++iit)
|
---|
3381 | {
|
---|
3382 | pMediumsForNotify.insert(*iit);
|
---|
3383 | }
|
---|
3384 | }
|
---|
3385 | if (it->mfMergeForward)
|
---|
3386 | {
|
---|
3387 | for (ComObjPtr<Medium> pTmpMedium = it->mpTarget->i_getParent();
|
---|
3388 | pTmpMedium && pTmpMedium != it->mpSource;
|
---|
3389 | pTmpMedium = pTmpMedium->i_getParent())
|
---|
3390 | {
|
---|
3391 | uIdsForNotify[pTmpMedium->i_getId()] = pTmpMedium->i_getDeviceType();
|
---|
3392 | }
|
---|
3393 | uIdsForNotify[it->mpSource->i_getId()] = it->mpSource->i_getDeviceType();
|
---|
3394 | }
|
---|
3395 | else
|
---|
3396 | {
|
---|
3397 | for (ComObjPtr<Medium> pTmpMedium = it->mpSource;
|
---|
3398 | pTmpMedium && pTmpMedium != it->mpTarget;
|
---|
3399 | pTmpMedium = pTmpMedium->i_getParent())
|
---|
3400 | {
|
---|
3401 | uIdsForNotify[pTmpMedium->i_getId()] = pTmpMedium->i_getDeviceType();
|
---|
3402 | }
|
---|
3403 | }
|
---|
3404 | }
|
---|
3405 |
|
---|
3406 | bool fNeedsSave = false;
|
---|
3407 | if (it->mfNeedsOnlineMerge)
|
---|
3408 | {
|
---|
3409 | // Put the medium merge information (MediumDeleteRec) where
|
---|
3410 | // SessionMachine::FinishOnlineMergeMedium can get at it.
|
---|
3411 | // This callback will arrive while onlineMergeMedium is
|
---|
3412 | // still executing, and there can't be two tasks.
|
---|
3413 | /// @todo r=klaus this hack needs to go, and the logic needs to be "unconvoluted", putting SessionMachine in charge of coordinating the reconfig/resume.
|
---|
3414 | mConsoleTaskData.mDeleteSnapshotInfo = (void *)&(*it);
|
---|
3415 | // online medium merge, in the direction decided earlier
|
---|
3416 | rc = i_onlineMergeMedium(it->mpOnlineMediumAttachment,
|
---|
3417 | it->mpSource,
|
---|
3418 | it->mpTarget,
|
---|
3419 | it->mfMergeForward,
|
---|
3420 | it->mpParentForTarget,
|
---|
3421 | it->mpChildrenToReparent,
|
---|
3422 | it->mpMediumLockList,
|
---|
3423 | task.m_pProgress,
|
---|
3424 | &fNeedsSave);
|
---|
3425 | mConsoleTaskData.mDeleteSnapshotInfo = NULL;
|
---|
3426 | }
|
---|
3427 | else
|
---|
3428 | {
|
---|
3429 | // normal medium merge, in the direction decided earlier
|
---|
3430 | rc = it->mpSource->i_mergeTo(it->mpTarget,
|
---|
3431 | it->mfMergeForward,
|
---|
3432 | it->mpParentForTarget,
|
---|
3433 | it->mpChildrenToReparent,
|
---|
3434 | it->mpMediumLockList,
|
---|
3435 | &task.m_pProgress,
|
---|
3436 | true /* aWait */,
|
---|
3437 | false /* aNotify */);
|
---|
3438 | }
|
---|
3439 |
|
---|
3440 | // If the merge failed, we need to do our best to have a usable
|
---|
3441 | // VM configuration afterwards. The return code doesn't tell
|
---|
3442 | // whether the merge completed and so we have to check if the
|
---|
3443 | // source medium (diff images are always file based at the
|
---|
3444 | // moment) is still there or not. Be careful not to lose the
|
---|
3445 | // error code below, before the "Delayed failure exit".
|
---|
3446 | if (FAILED(rc))
|
---|
3447 | {
|
---|
3448 | AutoReadLock mlock(it->mpSource COMMA_LOCKVAL_SRC_POS);
|
---|
3449 | if (!it->mpSource->i_isMediumFormatFile())
|
---|
3450 | // Diff medium not backed by a file - cannot get status so
|
---|
3451 | // be pessimistic.
|
---|
3452 | throw rc;
|
---|
3453 | const Utf8Str &loc = it->mpSource->i_getLocationFull();
|
---|
3454 | // Source medium is still there, so merge failed early.
|
---|
3455 | if (RTFileExists(loc.c_str()))
|
---|
3456 | throw rc;
|
---|
3457 |
|
---|
3458 | // Source medium is gone. Assume the merge succeeded and
|
---|
3459 | // thus it's safe to remove the attachment. We use the
|
---|
3460 | // "Delayed failure exit" below.
|
---|
3461 | }
|
---|
3462 |
|
---|
3463 | // need to change the medium attachment for backward merges
|
---|
3464 | fReparentTarget = !it->mfMergeForward;
|
---|
3465 |
|
---|
3466 | if (!it->mfNeedsOnlineMerge)
|
---|
3467 | {
|
---|
3468 | // need to uninit the medium deleted by the merge
|
---|
3469 | fNeedSourceUninit = true;
|
---|
3470 |
|
---|
3471 | // delete the no longer needed medium lock list, which
|
---|
3472 | // implicitly handled the unlocking
|
---|
3473 | delete it->mpMediumLockList;
|
---|
3474 | it->mpMediumLockList = NULL;
|
---|
3475 | }
|
---|
3476 | }
|
---|
3477 |
|
---|
3478 | // Now that the medium is successfully merged/deleted/whatever,
|
---|
3479 | // remove the medium attachment from the snapshot. For a backwards
|
---|
3480 | // merge the target attachment needs to be removed from the
|
---|
3481 | // snapshot, as the VM will take it over. For forward merges the
|
---|
3482 | // source medium attachment needs to be removed.
|
---|
3483 | ComObjPtr<MediumAttachment> pAtt;
|
---|
3484 | if (fReparentTarget)
|
---|
3485 | {
|
---|
3486 | pAtt = i_findAttachment(*(pSnapMachine->mMediumAttachments.data()),
|
---|
3487 | it->mpTarget);
|
---|
3488 | it->mpTarget->i_removeBackReference(machineId, snapshotId);
|
---|
3489 | }
|
---|
3490 | else
|
---|
3491 | pAtt = i_findAttachment(*(pSnapMachine->mMediumAttachments.data()),
|
---|
3492 | it->mpSource);
|
---|
3493 | pSnapMachine->mMediumAttachments->remove(pAtt);
|
---|
3494 |
|
---|
3495 | if (fReparentTarget)
|
---|
3496 | {
|
---|
3497 | // Search for old source attachment and replace with target.
|
---|
3498 | // There can be only one child snapshot in this case.
|
---|
3499 | ComObjPtr<Machine> pMachine = this;
|
---|
3500 | Guid childSnapshotId;
|
---|
3501 | ComObjPtr<Snapshot> pChildSnapshot = task.m_pSnapshot->i_getFirstChild();
|
---|
3502 | if (pChildSnapshot)
|
---|
3503 | {
|
---|
3504 | pMachine = pChildSnapshot->i_getSnapshotMachine();
|
---|
3505 | childSnapshotId = pChildSnapshot->i_getId();
|
---|
3506 | }
|
---|
3507 | pAtt = i_findAttachment(*(pMachine->mMediumAttachments).data(), it->mpSource);
|
---|
3508 | if (pAtt)
|
---|
3509 | {
|
---|
3510 | AutoWriteLock attLock(pAtt COMMA_LOCKVAL_SRC_POS);
|
---|
3511 | pAtt->i_updateMedium(it->mpTarget);
|
---|
3512 | it->mpTarget->i_addBackReference(pMachine->mData->mUuid, childSnapshotId);
|
---|
3513 | }
|
---|
3514 | else
|
---|
3515 | {
|
---|
3516 | // If no attachment is found do not change anything. Maybe
|
---|
3517 | // the source medium was not attached to the snapshot.
|
---|
3518 | // If this is an online deletion the attachment was updated
|
---|
3519 | // already to allow the VM continue execution immediately.
|
---|
3520 | // Needs a bit of special treatment due to this difference.
|
---|
3521 | if (it->mfNeedsOnlineMerge)
|
---|
3522 | it->mpTarget->i_addBackReference(pMachine->mData->mUuid, childSnapshotId);
|
---|
3523 | }
|
---|
3524 | }
|
---|
3525 |
|
---|
3526 | if (fNeedSourceUninit)
|
---|
3527 | {
|
---|
3528 | // make sure that the diff image to be deleted has no parent,
|
---|
3529 | // even in error cases (where the deparenting may be missing)
|
---|
3530 | if (it->mpSource->i_getParent())
|
---|
3531 | it->mpSource->i_deparent();
|
---|
3532 | it->mpSource->uninit();
|
---|
3533 | }
|
---|
3534 |
|
---|
3535 | // One attachment is merged, must save the settings
|
---|
3536 | mParent->i_markRegistryModified(i_getId());
|
---|
3537 |
|
---|
3538 | // prevent calling cancelDeleteSnapshotMedium() for this attachment
|
---|
3539 | it = toDelete.erase(it);
|
---|
3540 |
|
---|
3541 | // Delayed failure exit when the merge cleanup failed but the
|
---|
3542 | // merge actually succeeded.
|
---|
3543 | if (FAILED(rc))
|
---|
3544 | throw rc;
|
---|
3545 | }
|
---|
3546 |
|
---|
3547 | /* 3a: delete NVRAM file if present. */
|
---|
3548 | {
|
---|
3549 | Utf8Str NVRAMPath = pSnapMachine->mNvramStore->i_getNonVolatileStorageFile();
|
---|
3550 | if (NVRAMPath.isNotEmpty() && RTFileExists(NVRAMPath.c_str()))
|
---|
3551 | RTFileDelete(NVRAMPath.c_str());
|
---|
3552 | }
|
---|
3553 |
|
---|
3554 | /* third pass: */
|
---|
3555 | {
|
---|
3556 | // beginSnapshotDelete() needs the machine lock, and the snapshots
|
---|
3557 | // tree is protected by the machine lock as well
|
---|
3558 | AutoWriteLock machineLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3559 |
|
---|
3560 | task.m_pSnapshot->i_beginSnapshotDelete();
|
---|
3561 | task.m_pSnapshot->uninit();
|
---|
3562 |
|
---|
3563 | machineLock.release();
|
---|
3564 | mParent->i_markRegistryModified(i_getId());
|
---|
3565 | }
|
---|
3566 | }
|
---|
3567 | catch (HRESULT aRC) {
|
---|
3568 | mrc = aRC;
|
---|
3569 | }
|
---|
3570 |
|
---|
3571 | if (FAILED(mrc))
|
---|
3572 | {
|
---|
3573 | // preserve existing error info so that the result can
|
---|
3574 | // be properly reported to the progress object below
|
---|
3575 | ErrorInfoKeeper eik;
|
---|
3576 |
|
---|
3577 | AutoMultiWriteLock2 multiLock(this->lockHandle(), // machine
|
---|
3578 | &mParent->i_getMediaTreeLockHandle() // media tree
|
---|
3579 | COMMA_LOCKVAL_SRC_POS);
|
---|
3580 |
|
---|
3581 | // un-prepare the remaining hard disks
|
---|
3582 | for (MediumDeleteRecList::const_iterator it = toDelete.begin();
|
---|
3583 | it != toDelete.end();
|
---|
3584 | ++it)
|
---|
3585 | i_cancelDeleteSnapshotMedium(it->mpHD, it->mpSource,
|
---|
3586 | it->mpChildrenToReparent,
|
---|
3587 | it->mfNeedsOnlineMerge,
|
---|
3588 | it->mpMediumLockList, it->mpHDLockToken,
|
---|
3589 | it->mMachineId, it->mSnapshotId);
|
---|
3590 | }
|
---|
3591 |
|
---|
3592 | // whether we were successful or not, we need to set the machine
|
---|
3593 | // state and save the machine settings;
|
---|
3594 | {
|
---|
3595 | // preserve existing error info so that the result can
|
---|
3596 | // be properly reported to the progress object below
|
---|
3597 | ErrorInfoKeeper eik;
|
---|
3598 |
|
---|
3599 | // restore the machine state that was saved when the
|
---|
3600 | // task was started
|
---|
3601 | i_setMachineState(task.m_machineStateBackup);
|
---|
3602 | if (Global::IsOnline(mData->mMachineState))
|
---|
3603 | i_updateMachineStateOnClient();
|
---|
3604 |
|
---|
3605 | mParent->i_saveModifiedRegistries();
|
---|
3606 | }
|
---|
3607 |
|
---|
3608 | // report the result (this will try to fetch current error info on failure)
|
---|
3609 | task.m_pProgress->i_notifyComplete(mrc);
|
---|
3610 |
|
---|
3611 | if (SUCCEEDED(mrc))
|
---|
3612 | {
|
---|
3613 | mParent->i_onSnapshotDeleted(mData->mUuid, snapshotId);
|
---|
3614 | for (std::map<Guid, DeviceType_T>::const_iterator it = uIdsForNotify.begin();
|
---|
3615 | it != uIdsForNotify.end();
|
---|
3616 | ++it)
|
---|
3617 | {
|
---|
3618 | mParent->i_onMediumRegistered(it->first, it->second, FALSE);
|
---|
3619 | }
|
---|
3620 | for (std::set<ComObjPtr<Medium> >::const_iterator it = pMediumsForNotify.begin();
|
---|
3621 | it != pMediumsForNotify.end();
|
---|
3622 | ++it)
|
---|
3623 | {
|
---|
3624 | if (it->isNotNull())
|
---|
3625 | mParent->i_onMediumConfigChanged(*it);
|
---|
3626 | }
|
---|
3627 | }
|
---|
3628 |
|
---|
3629 | LogFlowThisFunc(("Done deleting snapshot (rc=%08X)\n", (HRESULT)mrc));
|
---|
3630 | LogFlowThisFuncLeave();
|
---|
3631 | }
|
---|
3632 |
|
---|
3633 | /**
|
---|
3634 | * Checks that this hard disk (part of a snapshot) may be deleted/merged and
|
---|
3635 | * performs necessary state changes. Must not be called for writethrough disks
|
---|
3636 | * because there is nothing to delete/merge then.
|
---|
3637 | *
|
---|
3638 | * This method is to be called prior to calling #deleteSnapshotMedium().
|
---|
3639 | * If #deleteSnapshotMedium() is not called or fails, the state modifications
|
---|
3640 | * performed by this method must be undone by #cancelDeleteSnapshotMedium().
|
---|
3641 | *
|
---|
3642 | * @return COM status code
|
---|
3643 | * @param aHD Hard disk which is connected to the snapshot.
|
---|
3644 | * @param aMachineId UUID of machine this hard disk is attached to.
|
---|
3645 | * @param aSnapshotId UUID of snapshot this hard disk is attached to. May
|
---|
3646 | * be a zero UUID if no snapshot is applicable.
|
---|
3647 | * @param fOnlineMergePossible Flag whether an online merge is possible.
|
---|
3648 | * @param aVMMALockList Medium lock list for the medium attachment of this VM.
|
---|
3649 | * Only used if @a fOnlineMergePossible is @c true, and
|
---|
3650 | * must be non-NULL in this case.
|
---|
3651 | * @param aSource Source hard disk for merge (out).
|
---|
3652 | * @param aTarget Target hard disk for merge (out).
|
---|
3653 | * @param aMergeForward Merge direction decision (out).
|
---|
3654 | * @param aParentForTarget New parent if target needs to be reparented (out).
|
---|
3655 | * @param aChildrenToReparent MediumLockList with children which have to be
|
---|
3656 | * reparented to the target (out).
|
---|
3657 | * @param fNeedsOnlineMerge Whether this merge needs to be done online (out).
|
---|
3658 | * If this is set to @a true then the @a aVMMALockList
|
---|
3659 | * parameter has been modified and is returned as
|
---|
3660 | * @a aMediumLockList.
|
---|
3661 | * @param aMediumLockList Where to store the created medium lock list (may
|
---|
3662 | * return NULL if no real merge is necessary).
|
---|
3663 | * @param aHDLockToken Where to store the write lock token for aHD, in case
|
---|
3664 | * it is not merged or deleted (out).
|
---|
3665 | *
|
---|
3666 | * @note Caller must hold media tree lock for writing. This locks this object
|
---|
3667 | * and every medium object on the merge chain for writing.
|
---|
3668 | */
|
---|
3669 | HRESULT SessionMachine::i_prepareDeleteSnapshotMedium(const ComObjPtr<Medium> &aHD,
|
---|
3670 | const Guid &aMachineId,
|
---|
3671 | const Guid &aSnapshotId,
|
---|
3672 | bool fOnlineMergePossible,
|
---|
3673 | MediumLockList *aVMMALockList,
|
---|
3674 | ComObjPtr<Medium> &aSource,
|
---|
3675 | ComObjPtr<Medium> &aTarget,
|
---|
3676 | bool &aMergeForward,
|
---|
3677 | ComObjPtr<Medium> &aParentForTarget,
|
---|
3678 | MediumLockList * &aChildrenToReparent,
|
---|
3679 | bool &fNeedsOnlineMerge,
|
---|
3680 | MediumLockList * &aMediumLockList,
|
---|
3681 | ComPtr<IToken> &aHDLockToken)
|
---|
3682 | {
|
---|
3683 | Assert(!mParent->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
3684 | Assert(!fOnlineMergePossible || RT_VALID_PTR(aVMMALockList));
|
---|
3685 |
|
---|
3686 | AutoWriteLock alock(aHD COMMA_LOCKVAL_SRC_POS);
|
---|
3687 |
|
---|
3688 | // Medium must not be writethrough/shareable/readonly at this point
|
---|
3689 | MediumType_T type = aHD->i_getType();
|
---|
3690 | AssertReturn( type != MediumType_Writethrough
|
---|
3691 | && type != MediumType_Shareable
|
---|
3692 | && type != MediumType_Readonly, E_FAIL);
|
---|
3693 |
|
---|
3694 | aChildrenToReparent = NULL;
|
---|
3695 | aMediumLockList = NULL;
|
---|
3696 | fNeedsOnlineMerge = false;
|
---|
3697 |
|
---|
3698 | if (aHD->i_getChildren().size() == 0)
|
---|
3699 | {
|
---|
3700 | /* This technically is no merge, set those values nevertheless.
|
---|
3701 | * Helps with updating the medium attachments. */
|
---|
3702 | aSource = aHD;
|
---|
3703 | aTarget = aHD;
|
---|
3704 |
|
---|
3705 | /* special treatment of the last hard disk in the chain: */
|
---|
3706 | if (aHD->i_getParent().isNull())
|
---|
3707 | {
|
---|
3708 | /* lock only, to prevent any usage until the snapshot deletion
|
---|
3709 | * is completed */
|
---|
3710 | alock.release();
|
---|
3711 | return aHD->LockWrite(aHDLockToken.asOutParam());
|
---|
3712 | }
|
---|
3713 |
|
---|
3714 | /* the differencing hard disk w/o children will be deleted, protect it
|
---|
3715 | * from attaching to other VMs (this is why Deleting) */
|
---|
3716 | return aHD->i_markForDeletion();
|
---|
3717 | }
|
---|
3718 |
|
---|
3719 | /* not going multi-merge as it's too expensive */
|
---|
3720 | if (aHD->i_getChildren().size() > 1)
|
---|
3721 | return setError(E_FAIL,
|
---|
3722 | tr("Hard disk '%s' has more than one child hard disk (%d)"),
|
---|
3723 | aHD->i_getLocationFull().c_str(),
|
---|
3724 | aHD->i_getChildren().size());
|
---|
3725 |
|
---|
3726 | ComObjPtr<Medium> pChild = aHD->i_getChildren().front();
|
---|
3727 |
|
---|
3728 | AutoWriteLock childLock(pChild COMMA_LOCKVAL_SRC_POS);
|
---|
3729 |
|
---|
3730 | /* the rest is a normal merge setup */
|
---|
3731 | if (aHD->i_getParent().isNull())
|
---|
3732 | {
|
---|
3733 | /* base hard disk, backward merge */
|
---|
3734 | const Guid *pMachineId1 = pChild->i_getFirstMachineBackrefId();
|
---|
3735 | const Guid *pMachineId2 = aHD->i_getFirstMachineBackrefId();
|
---|
3736 | if (pMachineId1 && pMachineId2 && *pMachineId1 != *pMachineId2)
|
---|
3737 | {
|
---|
3738 | /* backward merge is too tricky, we'll just detach on snapshot
|
---|
3739 | * deletion, so lock only, to prevent any usage */
|
---|
3740 | childLock.release();
|
---|
3741 | alock.release();
|
---|
3742 | return aHD->LockWrite(aHDLockToken.asOutParam());
|
---|
3743 | }
|
---|
3744 |
|
---|
3745 | aSource = pChild;
|
---|
3746 | aTarget = aHD;
|
---|
3747 | }
|
---|
3748 | else
|
---|
3749 | {
|
---|
3750 | /* Determine best merge direction. */
|
---|
3751 | bool fMergeForward = true;
|
---|
3752 |
|
---|
3753 | childLock.release();
|
---|
3754 | alock.release();
|
---|
3755 | HRESULT rc = aHD->i_queryPreferredMergeDirection(pChild, fMergeForward);
|
---|
3756 | alock.acquire();
|
---|
3757 | childLock.acquire();
|
---|
3758 |
|
---|
3759 | if (FAILED(rc) && rc != E_FAIL)
|
---|
3760 | return rc;
|
---|
3761 |
|
---|
3762 | if (fMergeForward)
|
---|
3763 | {
|
---|
3764 | aSource = aHD;
|
---|
3765 | aTarget = pChild;
|
---|
3766 | LogFlowThisFunc(("Forward merging selected\n"));
|
---|
3767 | }
|
---|
3768 | else
|
---|
3769 | {
|
---|
3770 | aSource = pChild;
|
---|
3771 | aTarget = aHD;
|
---|
3772 | LogFlowThisFunc(("Backward merging selected\n"));
|
---|
3773 | }
|
---|
3774 | }
|
---|
3775 |
|
---|
3776 | HRESULT rc;
|
---|
3777 | childLock.release();
|
---|
3778 | alock.release();
|
---|
3779 | rc = aSource->i_prepareMergeTo(aTarget, &aMachineId, &aSnapshotId,
|
---|
3780 | !fOnlineMergePossible /* fLockMedia */,
|
---|
3781 | aMergeForward, aParentForTarget,
|
---|
3782 | aChildrenToReparent, aMediumLockList);
|
---|
3783 | alock.acquire();
|
---|
3784 | childLock.acquire();
|
---|
3785 | if (SUCCEEDED(rc) && fOnlineMergePossible)
|
---|
3786 | {
|
---|
3787 | /* Try to lock the newly constructed medium lock list. If it succeeds
|
---|
3788 | * this can be handled as an offline merge, i.e. without the need of
|
---|
3789 | * asking the VM to do the merging. Only continue with the online
|
---|
3790 | * merging preparation if applicable. */
|
---|
3791 | childLock.release();
|
---|
3792 | alock.release();
|
---|
3793 | rc = aMediumLockList->Lock();
|
---|
3794 | alock.acquire();
|
---|
3795 | childLock.acquire();
|
---|
3796 | if (FAILED(rc))
|
---|
3797 | {
|
---|
3798 | /* Locking failed, this cannot be done as an offline merge. Try to
|
---|
3799 | * combine the locking information into the lock list of the medium
|
---|
3800 | * attachment in the running VM. If that fails or locking the
|
---|
3801 | * resulting lock list fails then the merge cannot be done online.
|
---|
3802 | * It can be repeated by the user when the VM is shut down. */
|
---|
3803 | MediumLockList::Base::iterator lockListVMMABegin =
|
---|
3804 | aVMMALockList->GetBegin();
|
---|
3805 | MediumLockList::Base::iterator lockListVMMAEnd =
|
---|
3806 | aVMMALockList->GetEnd();
|
---|
3807 | MediumLockList::Base::iterator lockListBegin =
|
---|
3808 | aMediumLockList->GetBegin();
|
---|
3809 | MediumLockList::Base::iterator lockListEnd =
|
---|
3810 | aMediumLockList->GetEnd();
|
---|
3811 | for (MediumLockList::Base::iterator it = lockListVMMABegin,
|
---|
3812 | it2 = lockListBegin;
|
---|
3813 | it2 != lockListEnd;
|
---|
3814 | ++it, ++it2)
|
---|
3815 | {
|
---|
3816 | if ( it == lockListVMMAEnd
|
---|
3817 | || it->GetMedium() != it2->GetMedium())
|
---|
3818 | {
|
---|
3819 | fOnlineMergePossible = false;
|
---|
3820 | break;
|
---|
3821 | }
|
---|
3822 | bool fLockReq = (it2->GetLockRequest() || it->GetLockRequest());
|
---|
3823 | childLock.release();
|
---|
3824 | alock.release();
|
---|
3825 | rc = it->UpdateLock(fLockReq);
|
---|
3826 | alock.acquire();
|
---|
3827 | childLock.acquire();
|
---|
3828 | if (FAILED(rc))
|
---|
3829 | {
|
---|
3830 | // could not update the lock, trigger cleanup below
|
---|
3831 | fOnlineMergePossible = false;
|
---|
3832 | break;
|
---|
3833 | }
|
---|
3834 | }
|
---|
3835 |
|
---|
3836 | if (fOnlineMergePossible)
|
---|
3837 | {
|
---|
3838 | /* we will lock the children of the source for reparenting */
|
---|
3839 | if (aChildrenToReparent && !aChildrenToReparent->IsEmpty())
|
---|
3840 | {
|
---|
3841 | /* Cannot just call aChildrenToReparent->Lock(), as one of
|
---|
3842 | * the children is the one under which the current state of
|
---|
3843 | * the VM is located, and this means it is already locked
|
---|
3844 | * (for reading). Note that no special unlocking is needed,
|
---|
3845 | * because cancelMergeTo will unlock everything locked in
|
---|
3846 | * its context (using the unlock on destruction), and both
|
---|
3847 | * cancelDeleteSnapshotMedium (in case something fails) and
|
---|
3848 | * FinishOnlineMergeMedium re-define the read/write lock
|
---|
3849 | * state of everything which the VM need, search for the
|
---|
3850 | * UpdateLock method calls. */
|
---|
3851 | childLock.release();
|
---|
3852 | alock.release();
|
---|
3853 | rc = aChildrenToReparent->Lock(true /* fSkipOverLockedMedia */);
|
---|
3854 | alock.acquire();
|
---|
3855 | childLock.acquire();
|
---|
3856 | MediumLockList::Base::iterator childrenToReparentBegin = aChildrenToReparent->GetBegin();
|
---|
3857 | MediumLockList::Base::iterator childrenToReparentEnd = aChildrenToReparent->GetEnd();
|
---|
3858 | for (MediumLockList::Base::iterator it = childrenToReparentBegin;
|
---|
3859 | it != childrenToReparentEnd;
|
---|
3860 | ++it)
|
---|
3861 | {
|
---|
3862 | ComObjPtr<Medium> pMedium = it->GetMedium();
|
---|
3863 | AutoReadLock mediumLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
3864 | if (!it->IsLocked())
|
---|
3865 | {
|
---|
3866 | mediumLock.release();
|
---|
3867 | childLock.release();
|
---|
3868 | alock.release();
|
---|
3869 | rc = aVMMALockList->Update(pMedium, true);
|
---|
3870 | alock.acquire();
|
---|
3871 | childLock.acquire();
|
---|
3872 | mediumLock.acquire();
|
---|
3873 | if (FAILED(rc))
|
---|
3874 | throw rc;
|
---|
3875 | }
|
---|
3876 | }
|
---|
3877 | }
|
---|
3878 | }
|
---|
3879 |
|
---|
3880 | if (fOnlineMergePossible)
|
---|
3881 | {
|
---|
3882 | childLock.release();
|
---|
3883 | alock.release();
|
---|
3884 | rc = aVMMALockList->Lock();
|
---|
3885 | alock.acquire();
|
---|
3886 | childLock.acquire();
|
---|
3887 | if (FAILED(rc))
|
---|
3888 | {
|
---|
3889 | aSource->i_cancelMergeTo(aChildrenToReparent, aMediumLockList);
|
---|
3890 | rc = setError(rc,
|
---|
3891 | tr("Cannot lock hard disk '%s' for a live merge"),
|
---|
3892 | aHD->i_getLocationFull().c_str());
|
---|
3893 | }
|
---|
3894 | else
|
---|
3895 | {
|
---|
3896 | delete aMediumLockList;
|
---|
3897 | aMediumLockList = aVMMALockList;
|
---|
3898 | fNeedsOnlineMerge = true;
|
---|
3899 | }
|
---|
3900 | }
|
---|
3901 | else
|
---|
3902 | {
|
---|
3903 | aSource->i_cancelMergeTo(aChildrenToReparent, aMediumLockList);
|
---|
3904 | rc = setError(rc,
|
---|
3905 | tr("Failed to construct lock list for a live merge of hard disk '%s'"),
|
---|
3906 | aHD->i_getLocationFull().c_str());
|
---|
3907 | }
|
---|
3908 |
|
---|
3909 | // fix the VM's lock list if anything failed
|
---|
3910 | if (FAILED(rc))
|
---|
3911 | {
|
---|
3912 | lockListVMMABegin = aVMMALockList->GetBegin();
|
---|
3913 | lockListVMMAEnd = aVMMALockList->GetEnd();
|
---|
3914 | MediumLockList::Base::iterator lockListLast = lockListVMMAEnd;
|
---|
3915 | --lockListLast;
|
---|
3916 | for (MediumLockList::Base::iterator it = lockListVMMABegin;
|
---|
3917 | it != lockListVMMAEnd;
|
---|
3918 | ++it)
|
---|
3919 | {
|
---|
3920 | childLock.release();
|
---|
3921 | alock.release();
|
---|
3922 | it->UpdateLock(it == lockListLast);
|
---|
3923 | alock.acquire();
|
---|
3924 | childLock.acquire();
|
---|
3925 | ComObjPtr<Medium> pMedium = it->GetMedium();
|
---|
3926 | AutoWriteLock mediumLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
3927 | // blindly apply this, only needed for medium objects which
|
---|
3928 | // would be deleted as part of the merge
|
---|
3929 | pMedium->i_unmarkLockedForDeletion();
|
---|
3930 | }
|
---|
3931 | }
|
---|
3932 | }
|
---|
3933 | }
|
---|
3934 | else if (FAILED(rc))
|
---|
3935 | {
|
---|
3936 | aSource->i_cancelMergeTo(aChildrenToReparent, aMediumLockList);
|
---|
3937 | rc = setError(rc,
|
---|
3938 | tr("Cannot lock hard disk '%s' when deleting a snapshot"),
|
---|
3939 | aHD->i_getLocationFull().c_str());
|
---|
3940 | }
|
---|
3941 |
|
---|
3942 | return rc;
|
---|
3943 | }
|
---|
3944 |
|
---|
3945 | /**
|
---|
3946 | * Cancels the deletion/merging of this hard disk (part of a snapshot). Undoes
|
---|
3947 | * what #prepareDeleteSnapshotMedium() did. Must be called if
|
---|
3948 | * #deleteSnapshotMedium() is not called or fails.
|
---|
3949 | *
|
---|
3950 | * @param aHD Hard disk which is connected to the snapshot.
|
---|
3951 | * @param aSource Source hard disk for merge.
|
---|
3952 | * @param aChildrenToReparent Children to unlock.
|
---|
3953 | * @param fNeedsOnlineMerge Whether this merge needs to be done online.
|
---|
3954 | * @param aMediumLockList Medium locks to cancel.
|
---|
3955 | * @param aHDLockToken Optional write lock token for aHD.
|
---|
3956 | * @param aMachineId Machine id to attach the medium to.
|
---|
3957 | * @param aSnapshotId Snapshot id to attach the medium to.
|
---|
3958 | *
|
---|
3959 | * @note Locks the medium tree and the hard disks in the chain for writing.
|
---|
3960 | */
|
---|
3961 | void SessionMachine::i_cancelDeleteSnapshotMedium(const ComObjPtr<Medium> &aHD,
|
---|
3962 | const ComObjPtr<Medium> &aSource,
|
---|
3963 | MediumLockList *aChildrenToReparent,
|
---|
3964 | bool fNeedsOnlineMerge,
|
---|
3965 | MediumLockList *aMediumLockList,
|
---|
3966 | const ComPtr<IToken> &aHDLockToken,
|
---|
3967 | const Guid &aMachineId,
|
---|
3968 | const Guid &aSnapshotId)
|
---|
3969 | {
|
---|
3970 | if (aMediumLockList == NULL)
|
---|
3971 | {
|
---|
3972 | AutoMultiWriteLock2 mLock(&mParent->i_getMediaTreeLockHandle(), aHD->lockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3973 |
|
---|
3974 | Assert(aHD->i_getChildren().size() == 0);
|
---|
3975 |
|
---|
3976 | if (aHD->i_getParent().isNull())
|
---|
3977 | {
|
---|
3978 | Assert(!aHDLockToken.isNull());
|
---|
3979 | if (!aHDLockToken.isNull())
|
---|
3980 | {
|
---|
3981 | HRESULT rc = aHDLockToken->Abandon();
|
---|
3982 | AssertComRC(rc);
|
---|
3983 | }
|
---|
3984 | }
|
---|
3985 | else
|
---|
3986 | {
|
---|
3987 | HRESULT rc = aHD->i_unmarkForDeletion();
|
---|
3988 | AssertComRC(rc);
|
---|
3989 | }
|
---|
3990 | }
|
---|
3991 | else
|
---|
3992 | {
|
---|
3993 | if (fNeedsOnlineMerge)
|
---|
3994 | {
|
---|
3995 | // Online merge uses the medium lock list of the VM, so give
|
---|
3996 | // an empty list to cancelMergeTo so that it works as designed.
|
---|
3997 | aSource->i_cancelMergeTo(aChildrenToReparent, new MediumLockList());
|
---|
3998 |
|
---|
3999 | // clean up the VM medium lock list ourselves
|
---|
4000 | MediumLockList::Base::iterator lockListBegin =
|
---|
4001 | aMediumLockList->GetBegin();
|
---|
4002 | MediumLockList::Base::iterator lockListEnd =
|
---|
4003 | aMediumLockList->GetEnd();
|
---|
4004 | MediumLockList::Base::iterator lockListLast = lockListEnd;
|
---|
4005 | --lockListLast;
|
---|
4006 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
4007 | it != lockListEnd;
|
---|
4008 | ++it)
|
---|
4009 | {
|
---|
4010 | ComObjPtr<Medium> pMedium = it->GetMedium();
|
---|
4011 | AutoWriteLock mediumLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4012 | if (pMedium->i_getState() == MediumState_Deleting)
|
---|
4013 | pMedium->i_unmarkForDeletion();
|
---|
4014 | else
|
---|
4015 | {
|
---|
4016 | // blindly apply this, only needed for medium objects which
|
---|
4017 | // would be deleted as part of the merge
|
---|
4018 | pMedium->i_unmarkLockedForDeletion();
|
---|
4019 | }
|
---|
4020 | mediumLock.release();
|
---|
4021 | it->UpdateLock(it == lockListLast);
|
---|
4022 | mediumLock.acquire();
|
---|
4023 | }
|
---|
4024 | }
|
---|
4025 | else
|
---|
4026 | {
|
---|
4027 | aSource->i_cancelMergeTo(aChildrenToReparent, aMediumLockList);
|
---|
4028 | }
|
---|
4029 | }
|
---|
4030 |
|
---|
4031 | if (aMachineId.isValid() && !aMachineId.isZero())
|
---|
4032 | {
|
---|
4033 | // reattach the source media to the snapshot
|
---|
4034 | HRESULT rc = aSource->i_addBackReference(aMachineId, aSnapshotId);
|
---|
4035 | AssertComRC(rc);
|
---|
4036 | }
|
---|
4037 | }
|
---|
4038 |
|
---|
4039 | /**
|
---|
4040 | * Perform an online merge of a hard disk, i.e. the equivalent of
|
---|
4041 | * Medium::mergeTo(), just for running VMs. If this fails you need to call
|
---|
4042 | * #cancelDeleteSnapshotMedium().
|
---|
4043 | *
|
---|
4044 | * @return COM status code
|
---|
4045 | * @param aMediumAttachment Identify where the disk is attached in the VM.
|
---|
4046 | * @param aSource Source hard disk for merge.
|
---|
4047 | * @param aTarget Target hard disk for merge.
|
---|
4048 | * @param fMergeForward Merge direction.
|
---|
4049 | * @param aParentForTarget New parent if target needs to be reparented.
|
---|
4050 | * @param aChildrenToReparent Medium lock list with children which have to be
|
---|
4051 | * reparented to the target.
|
---|
4052 | * @param aMediumLockList Where to store the created medium lock list (may
|
---|
4053 | * return NULL if no real merge is necessary).
|
---|
4054 | * @param aProgress Progress indicator.
|
---|
4055 | * @param pfNeedsMachineSaveSettings Whether the VM settings need to be saved (out).
|
---|
4056 | */
|
---|
4057 | HRESULT SessionMachine::i_onlineMergeMedium(const ComObjPtr<MediumAttachment> &aMediumAttachment,
|
---|
4058 | const ComObjPtr<Medium> &aSource,
|
---|
4059 | const ComObjPtr<Medium> &aTarget,
|
---|
4060 | bool fMergeForward,
|
---|
4061 | const ComObjPtr<Medium> &aParentForTarget,
|
---|
4062 | MediumLockList *aChildrenToReparent,
|
---|
4063 | MediumLockList *aMediumLockList,
|
---|
4064 | ComObjPtr<Progress> &aProgress,
|
---|
4065 | bool *pfNeedsMachineSaveSettings)
|
---|
4066 | {
|
---|
4067 | AssertReturn(aSource != NULL, E_FAIL);
|
---|
4068 | AssertReturn(aTarget != NULL, E_FAIL);
|
---|
4069 | AssertReturn(aSource != aTarget, E_FAIL);
|
---|
4070 | AssertReturn(aMediumLockList != NULL, E_FAIL);
|
---|
4071 | NOREF(fMergeForward);
|
---|
4072 | NOREF(aParentForTarget);
|
---|
4073 | NOREF(aChildrenToReparent);
|
---|
4074 |
|
---|
4075 | HRESULT rc = S_OK;
|
---|
4076 |
|
---|
4077 | try
|
---|
4078 | {
|
---|
4079 | // Similar code appears in Medium::taskMergeHandle, so
|
---|
4080 | // if you make any changes below check whether they are applicable
|
---|
4081 | // in that context as well.
|
---|
4082 |
|
---|
4083 | unsigned uTargetIdx = (unsigned)-1;
|
---|
4084 | unsigned uSourceIdx = (unsigned)-1;
|
---|
4085 | /* Sanity check all hard disks in the chain. */
|
---|
4086 | MediumLockList::Base::iterator lockListBegin =
|
---|
4087 | aMediumLockList->GetBegin();
|
---|
4088 | MediumLockList::Base::iterator lockListEnd =
|
---|
4089 | aMediumLockList->GetEnd();
|
---|
4090 | unsigned i = 0;
|
---|
4091 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
4092 | it != lockListEnd;
|
---|
4093 | ++it)
|
---|
4094 | {
|
---|
4095 | MediumLock &mediumLock = *it;
|
---|
4096 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
4097 |
|
---|
4098 | if (pMedium == aSource)
|
---|
4099 | uSourceIdx = i;
|
---|
4100 | else if (pMedium == aTarget)
|
---|
4101 | uTargetIdx = i;
|
---|
4102 |
|
---|
4103 | // In Medium::taskMergeHandler there is lots of consistency
|
---|
4104 | // checking which we cannot do here, as the state details are
|
---|
4105 | // impossible to get outside the Medium class. The locking should
|
---|
4106 | // have done the checks already.
|
---|
4107 |
|
---|
4108 | i++;
|
---|
4109 | }
|
---|
4110 |
|
---|
4111 | ComAssertThrow( uSourceIdx != (unsigned)-1
|
---|
4112 | && uTargetIdx != (unsigned)-1, E_FAIL);
|
---|
4113 |
|
---|
4114 | ComPtr<IInternalSessionControl> directControl;
|
---|
4115 | {
|
---|
4116 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4117 |
|
---|
4118 | if (mData->mSession.mState != SessionState_Locked)
|
---|
4119 | throw setError(VBOX_E_INVALID_VM_STATE,
|
---|
4120 | tr("Machine is not locked by a session (session state: %s)"),
|
---|
4121 | Global::stringifySessionState(mData->mSession.mState));
|
---|
4122 | directControl = mData->mSession.mDirectControl;
|
---|
4123 | }
|
---|
4124 |
|
---|
4125 | // Must not hold any locks here, as this will call back to finish
|
---|
4126 | // updating the medium attachment, chain linking and state.
|
---|
4127 | rc = directControl->OnlineMergeMedium(aMediumAttachment,
|
---|
4128 | uSourceIdx, uTargetIdx,
|
---|
4129 | aProgress);
|
---|
4130 | if (FAILED(rc))
|
---|
4131 | throw rc;
|
---|
4132 | }
|
---|
4133 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4134 |
|
---|
4135 | // The callback mentioned above takes care of update the medium state
|
---|
4136 |
|
---|
4137 | if (pfNeedsMachineSaveSettings)
|
---|
4138 | *pfNeedsMachineSaveSettings = true;
|
---|
4139 |
|
---|
4140 | return rc;
|
---|
4141 | }
|
---|
4142 |
|
---|
4143 | /**
|
---|
4144 | * Implementation for IInternalMachineControl::finishOnlineMergeMedium().
|
---|
4145 | *
|
---|
4146 | * Gets called after the successful completion of an online merge from
|
---|
4147 | * Console::onlineMergeMedium(), which gets invoked indirectly above in
|
---|
4148 | * the call to IInternalSessionControl::onlineMergeMedium.
|
---|
4149 | *
|
---|
4150 | * This updates the medium information and medium state so that the VM
|
---|
4151 | * can continue with the updated state of the medium chain.
|
---|
4152 | */
|
---|
4153 | HRESULT SessionMachine::finishOnlineMergeMedium()
|
---|
4154 | {
|
---|
4155 | HRESULT rc = S_OK;
|
---|
4156 | MediumDeleteRec *pDeleteRec = (MediumDeleteRec *)mConsoleTaskData.mDeleteSnapshotInfo;
|
---|
4157 | AssertReturn(pDeleteRec, E_FAIL);
|
---|
4158 | bool fSourceHasChildren = false;
|
---|
4159 |
|
---|
4160 | // all hard disks but the target were successfully deleted by
|
---|
4161 | // the merge; reparent target if necessary and uninitialize media
|
---|
4162 |
|
---|
4163 | AutoWriteLock treeLock(mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4164 |
|
---|
4165 | // Declare this here to make sure the object does not get uninitialized
|
---|
4166 | // before this method completes. Would normally happen as halfway through
|
---|
4167 | // we delete the last reference to the no longer existing medium object.
|
---|
4168 | ComObjPtr<Medium> targetChild;
|
---|
4169 |
|
---|
4170 | if (pDeleteRec->mfMergeForward)
|
---|
4171 | {
|
---|
4172 | // first, unregister the target since it may become a base
|
---|
4173 | // hard disk which needs re-registration
|
---|
4174 | rc = mParent->i_unregisterMedium(pDeleteRec->mpTarget);
|
---|
4175 | AssertComRC(rc);
|
---|
4176 |
|
---|
4177 | // then, reparent it and disconnect the deleted branch at
|
---|
4178 | // both ends (chain->parent() is source's parent)
|
---|
4179 | pDeleteRec->mpTarget->i_deparent();
|
---|
4180 | pDeleteRec->mpTarget->i_setParent(pDeleteRec->mpParentForTarget);
|
---|
4181 | if (pDeleteRec->mpParentForTarget)
|
---|
4182 | pDeleteRec->mpSource->i_deparent();
|
---|
4183 |
|
---|
4184 | // then, register again
|
---|
4185 | rc = mParent->i_registerMedium(pDeleteRec->mpTarget, &pDeleteRec->mpTarget, treeLock);
|
---|
4186 | AssertComRC(rc);
|
---|
4187 | }
|
---|
4188 | else
|
---|
4189 | {
|
---|
4190 | Assert(pDeleteRec->mpTarget->i_getChildren().size() == 1);
|
---|
4191 | targetChild = pDeleteRec->mpTarget->i_getChildren().front();
|
---|
4192 |
|
---|
4193 | // disconnect the deleted branch at the elder end
|
---|
4194 | targetChild->i_deparent();
|
---|
4195 |
|
---|
4196 | // Update parent UUIDs of the source's children, reparent them and
|
---|
4197 | // disconnect the deleted branch at the younger end
|
---|
4198 | if (pDeleteRec->mpChildrenToReparent && !pDeleteRec->mpChildrenToReparent->IsEmpty())
|
---|
4199 | {
|
---|
4200 | fSourceHasChildren = true;
|
---|
4201 | // Fix the parent UUID of the images which needs to be moved to
|
---|
4202 | // underneath target. The running machine has the images opened,
|
---|
4203 | // but only for reading since the VM is paused. If anything fails
|
---|
4204 | // we must continue. The worst possible result is that the images
|
---|
4205 | // need manual fixing via VBoxManage to adjust the parent UUID.
|
---|
4206 | treeLock.release();
|
---|
4207 | pDeleteRec->mpTarget->i_fixParentUuidOfChildren(pDeleteRec->mpChildrenToReparent);
|
---|
4208 | // The childen are still write locked, unlock them now and don't
|
---|
4209 | // rely on the destructor doing it very late.
|
---|
4210 | pDeleteRec->mpChildrenToReparent->Unlock();
|
---|
4211 | treeLock.acquire();
|
---|
4212 |
|
---|
4213 | // obey {parent,child} lock order
|
---|
4214 | AutoWriteLock sourceLock(pDeleteRec->mpSource COMMA_LOCKVAL_SRC_POS);
|
---|
4215 |
|
---|
4216 | MediumLockList::Base::iterator childrenBegin = pDeleteRec->mpChildrenToReparent->GetBegin();
|
---|
4217 | MediumLockList::Base::iterator childrenEnd = pDeleteRec->mpChildrenToReparent->GetEnd();
|
---|
4218 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
4219 | it != childrenEnd;
|
---|
4220 | ++it)
|
---|
4221 | {
|
---|
4222 | Medium *pMedium = it->GetMedium();
|
---|
4223 | AutoWriteLock childLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4224 |
|
---|
4225 | pMedium->i_deparent(); // removes pMedium from source
|
---|
4226 | pMedium->i_setParent(pDeleteRec->mpTarget);
|
---|
4227 | }
|
---|
4228 | }
|
---|
4229 | }
|
---|
4230 |
|
---|
4231 | /* unregister and uninitialize all hard disks removed by the merge */
|
---|
4232 | MediumLockList *pMediumLockList = NULL;
|
---|
4233 | rc = mData->mSession.mLockedMedia.Get(pDeleteRec->mpOnlineMediumAttachment, pMediumLockList);
|
---|
4234 | const ComObjPtr<Medium> &pLast = pDeleteRec->mfMergeForward ? pDeleteRec->mpTarget : pDeleteRec->mpSource;
|
---|
4235 | AssertReturn(SUCCEEDED(rc) && pMediumLockList, E_FAIL);
|
---|
4236 | MediumLockList::Base::iterator lockListBegin =
|
---|
4237 | pMediumLockList->GetBegin();
|
---|
4238 | MediumLockList::Base::iterator lockListEnd =
|
---|
4239 | pMediumLockList->GetEnd();
|
---|
4240 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
4241 | it != lockListEnd;
|
---|
4242 | )
|
---|
4243 | {
|
---|
4244 | MediumLock &mediumLock = *it;
|
---|
4245 | /* Create a real copy of the medium pointer, as the medium
|
---|
4246 | * lock deletion below would invalidate the referenced object. */
|
---|
4247 | const ComObjPtr<Medium> pMedium = mediumLock.GetMedium();
|
---|
4248 |
|
---|
4249 | /* The target and all images not merged (readonly) are skipped */
|
---|
4250 | if ( pMedium == pDeleteRec->mpTarget
|
---|
4251 | || pMedium->i_getState() == MediumState_LockedRead)
|
---|
4252 | {
|
---|
4253 | ++it;
|
---|
4254 | }
|
---|
4255 | else
|
---|
4256 | {
|
---|
4257 | rc = mParent->i_unregisterMedium(pMedium);
|
---|
4258 | AssertComRC(rc);
|
---|
4259 |
|
---|
4260 | /* now, uninitialize the deleted hard disk (note that
|
---|
4261 | * due to the Deleting state, uninit() will not touch
|
---|
4262 | * the parent-child relationship so we need to
|
---|
4263 | * uninitialize each disk individually) */
|
---|
4264 |
|
---|
4265 | /* note that the operation initiator hard disk (which is
|
---|
4266 | * normally also the source hard disk) is a special case
|
---|
4267 | * -- there is one more caller added by Task to it which
|
---|
4268 | * we must release. Also, if we are in sync mode, the
|
---|
4269 | * caller may still hold an AutoCaller instance for it
|
---|
4270 | * and therefore we cannot uninit() it (it's therefore
|
---|
4271 | * the caller's responsibility) */
|
---|
4272 | if (pMedium == pDeleteRec->mpSource)
|
---|
4273 | {
|
---|
4274 | Assert(pDeleteRec->mpSource->i_getChildren().size() == 0);
|
---|
4275 | Assert(pDeleteRec->mpSource->i_getFirstMachineBackrefId() == NULL);
|
---|
4276 | }
|
---|
4277 |
|
---|
4278 | /* Delete the medium lock list entry, which also releases the
|
---|
4279 | * caller added by MergeChain before uninit() and updates the
|
---|
4280 | * iterator to point to the right place. */
|
---|
4281 | rc = pMediumLockList->RemoveByIterator(it);
|
---|
4282 | AssertComRC(rc);
|
---|
4283 |
|
---|
4284 | treeLock.release();
|
---|
4285 | pMedium->uninit();
|
---|
4286 | treeLock.acquire();
|
---|
4287 | }
|
---|
4288 |
|
---|
4289 | /* Stop as soon as we reached the last medium affected by the merge.
|
---|
4290 | * The remaining images must be kept unchanged. */
|
---|
4291 | if (pMedium == pLast)
|
---|
4292 | break;
|
---|
4293 | }
|
---|
4294 |
|
---|
4295 | /* Could be in principle folded into the previous loop, but let's keep
|
---|
4296 | * things simple. Update the medium locking to be the standard state:
|
---|
4297 | * all parent images locked for reading, just the last diff for writing. */
|
---|
4298 | lockListBegin = pMediumLockList->GetBegin();
|
---|
4299 | lockListEnd = pMediumLockList->GetEnd();
|
---|
4300 | MediumLockList::Base::iterator lockListLast = lockListEnd;
|
---|
4301 | --lockListLast;
|
---|
4302 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
4303 | it != lockListEnd;
|
---|
4304 | ++it)
|
---|
4305 | {
|
---|
4306 | it->UpdateLock(it == lockListLast);
|
---|
4307 | }
|
---|
4308 |
|
---|
4309 | /* If this is a backwards merge of the only remaining snapshot (i.e. the
|
---|
4310 | * source has no children) then update the medium associated with the
|
---|
4311 | * attachment, as the previously associated one (source) is now deleted.
|
---|
4312 | * Without the immediate update the VM could not continue running. */
|
---|
4313 | if (!pDeleteRec->mfMergeForward && !fSourceHasChildren)
|
---|
4314 | {
|
---|
4315 | AutoWriteLock attLock(pDeleteRec->mpOnlineMediumAttachment COMMA_LOCKVAL_SRC_POS);
|
---|
4316 | pDeleteRec->mpOnlineMediumAttachment->i_updateMedium(pDeleteRec->mpTarget);
|
---|
4317 | }
|
---|
4318 |
|
---|
4319 | return S_OK;
|
---|
4320 | }
|
---|