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