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