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