VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/MachineImplCloneVM.cpp@ 38041

Last change on this file since 38041 was 38041, checked in by vboxsync, 14 years ago

Main/MachineImpl: fix automatic diff creation when cloning a VM (linked clone and full clone case)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 42.5 KB
Line 
1/* $Id: MachineImplCloneVM.cpp 38041 2011-07-18 21:01:18Z vboxsync $ */
2/** @file
3 * Implementation of MachineCloneVM
4 */
5
6/*
7 * Copyright (C) 2011 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include "MachineImplCloneVM.h"
19
20#include "VirtualBoxImpl.h"
21#include "MediumImpl.h"
22#include "HostImpl.h"
23
24#include <iprt/path.h>
25#include <iprt/dir.h>
26#include <iprt/cpp/utils.h>
27
28#include <VBox/com/list.h>
29#include <VBox/com/MultiResult.h>
30
31// typedefs
32/////////////////////////////////////////////////////////////////////////////
33
34typedef struct
35{
36 Utf8Str strBaseName;
37 ComPtr<IMedium> pMedium;
38 ULONG uWeight;
39} MEDIUMTASK;
40
41typedef struct
42{
43 RTCList<MEDIUMTASK> chain;
44 bool fCreateDiffs;
45 bool fAttachLinked;
46} MEDIUMTASKCHAIN;
47
48typedef struct
49{
50 Guid snapshotUuid;
51 Utf8Str strSaveStateFile;
52 ULONG uWeight;
53} SAVESTATETASK;
54
55// The private class
56/////////////////////////////////////////////////////////////////////////////
57
58struct MachineCloneVMPrivate
59{
60 MachineCloneVMPrivate(MachineCloneVM *a_q, ComObjPtr<Machine> &a_pSrcMachine, ComObjPtr<Machine> &a_pTrgMachine, CloneMode_T a_mode, const RTCList<CloneOptions_T> &opts)
61 : q_ptr(a_q)
62 , p(a_pSrcMachine)
63 , pSrcMachine(a_pSrcMachine)
64 , pTrgMachine(a_pTrgMachine)
65 , mode(a_mode)
66 , options(opts)
67 {}
68
69 /* Thread management */
70 int startWorker()
71 {
72 return RTThreadCreate(NULL,
73 MachineCloneVMPrivate::workerThread,
74 static_cast<void*>(this),
75 0,
76 RTTHREADTYPE_MAIN_WORKER,
77 0,
78 "MachineClone");
79 }
80
81 static int workerThread(RTTHREAD /* Thread */, void *pvUser)
82 {
83 MachineCloneVMPrivate *pTask = static_cast<MachineCloneVMPrivate*>(pvUser);
84 AssertReturn(pTask, VERR_INVALID_POINTER);
85
86 HRESULT rc = pTask->q_ptr->run();
87
88 pTask->pProgress->notifyComplete(rc);
89
90 pTask->q_ptr->destroy();
91
92 return VINF_SUCCESS;
93 }
94
95 /* Private helper methods */
96 HRESULT createMachineList(const ComPtr<ISnapshot> &pSnapshot, RTCList< ComObjPtr<Machine> > &machineList) const;
97 settings::Snapshot findSnapshot(settings::MachineConfigFile *pMCF, const settings::SnapshotsList &snl, const Guid &id) const;
98 void updateMACAddresses(settings::NetworkAdaptersList &nwl) const;
99 void updateMACAddresses(settings::SnapshotsList &sl) const;
100 void updateStorageLists(settings::StorageControllersList &sc, const Bstr &bstrOldId, const Bstr &bstrNewId) const;
101 void updateSnapshotStorageLists(settings::SnapshotsList &sl, const Bstr &bstrOldId, const Bstr &bstrNewId) const;
102 void updateStateFile(settings::SnapshotsList &snl, const Guid &id, const Utf8Str &strFile) const;
103 static int copyStateFileProgress(unsigned uPercentage, void *pvUser);
104
105 /* Private q and parent pointer */
106 MachineCloneVM *q_ptr;
107 ComObjPtr<Machine> p;
108
109 /* Private helper members */
110 ComObjPtr<Machine> pSrcMachine;
111 ComObjPtr<Machine> pTrgMachine;
112 ComPtr<IMachine> pOldMachineState;
113 ComObjPtr<Progress> pProgress;
114 Guid snapshotId;
115 CloneMode_T mode;
116 RTCList<CloneOptions_T> options;
117 RTCList<MEDIUMTASKCHAIN> llMedias;
118 RTCList<SAVESTATETASK> llSaveStateFiles; /* Snapshot UUID -> File path */
119};
120
121HRESULT MachineCloneVMPrivate::createMachineList(const ComPtr<ISnapshot> &pSnapshot, RTCList< ComObjPtr<Machine> > &machineList) const
122{
123 HRESULT rc = S_OK;
124 Bstr name;
125 rc = pSnapshot->COMGETTER(Name)(name.asOutParam());
126 if (FAILED(rc)) return rc;
127
128 ComPtr<IMachine> pMachine;
129 rc = pSnapshot->COMGETTER(Machine)(pMachine.asOutParam());
130 if (FAILED(rc)) return rc;
131 machineList.append((Machine*)(IMachine*)pMachine);
132
133 SafeIfaceArray<ISnapshot> sfaChilds;
134 rc = pSnapshot->COMGETTER(Children)(ComSafeArrayAsOutParam(sfaChilds));
135 if (FAILED(rc)) return rc;
136 for (size_t i = 0; i < sfaChilds.size(); ++i)
137 {
138 rc = createMachineList(sfaChilds[i], machineList);
139 if (FAILED(rc)) return rc;
140 }
141
142 return rc;
143}
144
145settings::Snapshot MachineCloneVMPrivate::findSnapshot(settings::MachineConfigFile *pMCF, const settings::SnapshotsList &snl, const Guid &id) const
146{
147 settings::SnapshotsList::const_iterator it;
148 for (it = snl.begin(); it != snl.end(); ++it)
149 {
150 if (it->uuid == id)
151 return *it;
152 else if (!it->llChildSnapshots.empty())
153 return findSnapshot(pMCF, it->llChildSnapshots, id);
154 }
155 return settings::Snapshot();
156}
157
158void MachineCloneVMPrivate::updateMACAddresses(settings::NetworkAdaptersList &nwl) const
159{
160 const bool fNotNAT = options.contains(CloneOptions_KeepNATMACs);
161 settings::NetworkAdaptersList::iterator it;
162 for (it = nwl.begin(); it != nwl.end(); ++it)
163 {
164 if ( fNotNAT
165 && it->mode == NetworkAttachmentType_NAT)
166 continue;
167 Host::generateMACAddress(it->strMACAddress);
168 }
169}
170
171void MachineCloneVMPrivate::updateMACAddresses(settings::SnapshotsList &sl) const
172{
173 settings::SnapshotsList::iterator it;
174 for (it = sl.begin(); it != sl.end(); ++it)
175 {
176 updateMACAddresses(it->hardware.llNetworkAdapters);
177 if (!it->llChildSnapshots.empty())
178 updateMACAddresses(it->llChildSnapshots);
179 }
180}
181
182void MachineCloneVMPrivate::updateStorageLists(settings::StorageControllersList &sc, const Bstr &bstrOldId, const Bstr &bstrNewId) const
183{
184 settings::StorageControllersList::iterator it3;
185 for (it3 = sc.begin();
186 it3 != sc.end();
187 ++it3)
188 {
189 settings::AttachedDevicesList &llAttachments = it3->llAttachedDevices;
190 settings::AttachedDevicesList::iterator it4;
191 for (it4 = llAttachments.begin();
192 it4 != llAttachments.end();
193 ++it4)
194 {
195 if ( it4->deviceType == DeviceType_HardDisk
196 && it4->uuid == bstrOldId)
197 {
198 it4->uuid = bstrNewId;
199 }
200 }
201 }
202}
203
204void MachineCloneVMPrivate::updateSnapshotStorageLists(settings::SnapshotsList &sl, const Bstr &bstrOldId, const Bstr &bstrNewId) const
205{
206 settings::SnapshotsList::iterator it;
207 for ( it = sl.begin();
208 it != sl.end();
209 ++it)
210 {
211 updateStorageLists(it->storage.llStorageControllers, bstrOldId, bstrNewId);
212 if (!it->llChildSnapshots.empty())
213 updateSnapshotStorageLists(it->llChildSnapshots, bstrOldId, bstrNewId);
214 }
215}
216
217void MachineCloneVMPrivate::updateStateFile(settings::SnapshotsList &snl, const Guid &id, const Utf8Str &strFile) const
218{
219 settings::SnapshotsList::iterator it;
220 for (it = snl.begin(); it != snl.end(); ++it)
221 {
222 if (it->uuid == id)
223 it->strStateFile = strFile;
224 else if (!it->llChildSnapshots.empty())
225 updateStateFile(it->llChildSnapshots, id, strFile);
226 }
227}
228
229/* static */
230int MachineCloneVMPrivate::copyStateFileProgress(unsigned uPercentage, void *pvUser)
231{
232 ComObjPtr<Progress> pProgress = *static_cast< ComObjPtr<Progress>* >(pvUser);
233
234 BOOL fCanceled = false;
235 HRESULT rc = pProgress->COMGETTER(Canceled)(&fCanceled);
236 if (FAILED(rc)) return VERR_GENERAL_FAILURE;
237 /* If canceled by the user tell it to the copy operation. */
238 if (fCanceled) return VERR_CANCELLED;
239 /* Set the new process. */
240 rc = pProgress->SetCurrentOperationProgress(uPercentage);
241 if (FAILED(rc)) return VERR_GENERAL_FAILURE;
242
243 return VINF_SUCCESS;
244}
245
246// The public class
247/////////////////////////////////////////////////////////////////////////////
248
249MachineCloneVM::MachineCloneVM(ComObjPtr<Machine> pSrcMachine, ComObjPtr<Machine> pTrgMachine, CloneMode_T mode, const RTCList<CloneOptions_T> &opts)
250 : d_ptr(new MachineCloneVMPrivate(this, pSrcMachine, pTrgMachine, mode, opts))
251{
252}
253
254MachineCloneVM::~MachineCloneVM()
255{
256 delete d_ptr;
257}
258
259HRESULT MachineCloneVM::start(IProgress **pProgress)
260{
261 DPTR(MachineCloneVM);
262 ComObjPtr<Machine> &p = d->p;
263
264 HRESULT rc;
265 try
266 {
267 /* Handle the special case that someone is requesting a _full_ clone
268 * with all snapshots (and the current state), but uses a snapshot
269 * machine (and not the current one) as source machine. In this case we
270 * just replace the source (snapshot) machine with the current machine. */
271 if ( d->mode == CloneMode_AllStates
272 && d->pSrcMachine->isSnapshotMachine())
273 {
274 Bstr bstrSrcMachineId;
275 rc = d->pSrcMachine->COMGETTER(Id)(bstrSrcMachineId.asOutParam());
276 if (FAILED(rc)) throw rc;
277 ComPtr<IMachine> newSrcMachine;
278 rc = d->pSrcMachine->getVirtualBox()->FindMachine(bstrSrcMachineId.raw(), newSrcMachine.asOutParam());
279 if (FAILED(rc)) throw rc;
280 d->pSrcMachine = (Machine*)(IMachine*)newSrcMachine;
281 }
282
283 /* Lock the target machine early (so nobody mess around with it in the meantime). */
284 AutoWriteLock trgLock(d->pTrgMachine COMMA_LOCKVAL_SRC_POS);
285
286 if (d->pSrcMachine->isSnapshotMachine())
287 d->snapshotId = d->pSrcMachine->getSnapshotId();
288
289 /* Add the current machine and all snapshot machines below this machine
290 * in a list for further processing. */
291 RTCList< ComObjPtr<Machine> > machineList;
292
293 /* Include current state? */
294 if ( d->mode == CloneMode_MachineState
295 || d->mode == CloneMode_AllStates)
296 machineList.append(d->pSrcMachine);
297 /* Should be done a depth copy with all child snapshots? */
298 if ( d->mode == CloneMode_MachineAndChildStates
299 || d->mode == CloneMode_AllStates)
300 {
301 ULONG cSnapshots = 0;
302 rc = d->pSrcMachine->COMGETTER(SnapshotCount)(&cSnapshots);
303 if (FAILED(rc)) throw rc;
304 if (cSnapshots > 0)
305 {
306 Utf8Str id;
307 if ( d->mode == CloneMode_MachineAndChildStates
308 && !d->snapshotId.isEmpty())
309 id = d->snapshotId.toString();
310 ComPtr<ISnapshot> pSnapshot;
311 rc = d->pSrcMachine->FindSnapshot(Bstr(id).raw(), pSnapshot.asOutParam());
312 if (FAILED(rc)) throw rc;
313 rc = d->createMachineList(pSnapshot, machineList);
314 if (FAILED(rc)) throw rc;
315 if (d->mode == CloneMode_MachineAndChildStates)
316 {
317 rc = pSnapshot->COMGETTER(Machine)(d->pOldMachineState.asOutParam());
318 if (FAILED(rc)) throw rc;
319 }
320 }
321 }
322
323 /* Go over every machine and walk over every attachment this machine has. */
324 ULONG uCount = 2; /* One init task and the machine creation. */
325 ULONG uTotalWeight = 2; /* The init task and the machine creation is worth one. */
326 for (size_t i = 0; i < machineList.size(); ++i)
327 {
328 ComObjPtr<Machine> machine = machineList.at(i);
329 /* If this is the Snapshot Machine we want to clone, we need to
330 * create a new diff file for the new "current state". */
331 bool fCreateDiffs = false;
332 if (machine == d->pOldMachineState)
333 fCreateDiffs = true;
334 /* If we want to create a linked clone just attach the medium
335 * associated with the snapshot. The rest is taken care of by
336 * attach already, so no need to duplicate this. */
337 bool fAttachLinked = false;
338 if (d->options.contains(CloneOptions_Link))
339 fAttachLinked = true;
340 SafeIfaceArray<IMediumAttachment> sfaAttachments;
341 rc = machine->COMGETTER(MediumAttachments)(ComSafeArrayAsOutParam(sfaAttachments));
342 if (FAILED(rc)) throw rc;
343 /* Add all attachments (and their parents) of the different
344 * machines to a worker list. */
345 for (size_t a = 0; a < sfaAttachments.size(); ++a)
346 {
347 const ComPtr<IMediumAttachment> &pAtt = sfaAttachments[a];
348 DeviceType_T type;
349 rc = pAtt->COMGETTER(Type)(&type);
350 if (FAILED(rc)) throw rc;
351
352 /* Only harddisk's are of interest. */
353 if (type != DeviceType_HardDisk)
354 continue;
355
356 /* Valid medium attached? */
357 ComPtr<IMedium> pSrcMedium;
358 rc = pAtt->COMGETTER(Medium)(pSrcMedium.asOutParam());
359 if (FAILED(rc)) throw rc;
360 if (pSrcMedium.isNull())
361 continue;
362
363 /* Build up a child->parent list of this attachment. (Note: we are
364 * not interested of any child's not attached to this VM. So this
365 * will not create a full copy of the base/child relationship.) */
366 MEDIUMTASKCHAIN mtc;
367 mtc.fCreateDiffs = fCreateDiffs;
368 mtc.fAttachLinked = fAttachLinked;
369
370 /* If the current state without any snapshots is cloned, we
371 * don't need any diff images in the new clone. Add the last
372 * medium to the list of medias to create only (the clone
373 * operation of IMedium will create a merged copy
374 * automatically). */
375 if (d->mode == CloneMode_MachineState)
376 {
377 /* Refresh the state so that the file size get read. */
378 MediumState_T e;
379 rc = pSrcMedium->RefreshState(&e);
380 if (FAILED(rc)) throw rc;
381 LONG64 lSize;
382 rc = pSrcMedium->COMGETTER(Size)(&lSize);
383 if (FAILED(rc)) throw rc;
384
385 ComPtr<IMedium> pBaseMedium;
386 rc = pSrcMedium->COMGETTER(Base)(pBaseMedium.asOutParam());
387 if (FAILED(rc)) throw rc;
388
389 MEDIUMTASK mt;
390
391 Bstr bstrBaseName;
392 rc = pBaseMedium->COMGETTER(Name)(bstrBaseName.asOutParam());
393 if (FAILED(rc)) throw rc;
394
395 /* Save the base name. */
396 mt.strBaseName = bstrBaseName;
397
398 /* Save the current medium, for later cloning. */
399 mt.pMedium = pSrcMedium;
400 if (fAttachLinked)
401 mt.uWeight = 0; /* dummy */
402 else
403 mt.uWeight = (lSize + _1M - 1) / _1M;
404 mtc.chain.append(mt);
405 }
406 else
407 {
408 /** @todo r=klaus this puts way too many images in the list
409 * when cloning a snapshot (sub)tree, which means that more
410 * images are cloned than necessary. It is just the easiest
411 * way to get a working VM, as getting the image
412 * parent/child relationships right for only the bare
413 * minimum cloning is rather tricky. */
414 while (!pSrcMedium.isNull())
415 {
416 /* Refresh the state so that the file size get read. */
417 MediumState_T e;
418 rc = pSrcMedium->RefreshState(&e);
419 if (FAILED(rc)) throw rc;
420 LONG64 lSize;
421 rc = pSrcMedium->COMGETTER(Size)(&lSize);
422 if (FAILED(rc)) throw rc;
423
424 /* Save the current medium, for later cloning. */
425 MEDIUMTASK mt;
426 mt.pMedium = pSrcMedium;
427 mt.uWeight = (lSize + _1M - 1) / _1M;
428 mtc.chain.append(mt);
429
430 /* Query next parent. */
431 rc = pSrcMedium->COMGETTER(Parent)(pSrcMedium.asOutParam());
432 if (FAILED(rc)) throw rc;
433 }
434 }
435
436 if (fAttachLinked)
437 {
438 /* Implicit diff creation as part of attach is a pretty cheap
439 * operation, and does only need one operation per attachment. */
440 ++uCount;
441 uTotalWeight += 1; /* 1MB per attachment */
442 }
443 else
444 {
445 /* Currently the copying of diff images involves reading at least
446 * the biggest parent in the previous chain. So even if the new
447 * diff image is small in size, it could need some time to create
448 * it. Adding the biggest size in the chain should balance this a
449 * little bit more, i.e. the weight is the sum of the data which
450 * needs to be read and written. */
451 uint64_t uMaxSize = 0;
452 for (size_t e = mtc.chain.size(); e > 0; --e)
453 {
454 MEDIUMTASK &mt = mtc.chain.at(e - 1);
455 mt.uWeight += uMaxSize;
456
457 /* Calculate progress data */
458 ++uCount;
459 uTotalWeight += mt.uWeight;
460
461 /* Save the max size for better weighting of diff image
462 * creation. */
463 uMaxSize = RT_MAX(uMaxSize, mt.uWeight);
464 }
465 }
466 d->llMedias.append(mtc);
467 }
468 Bstr bstrSrcSaveStatePath;
469 rc = machine->COMGETTER(StateFilePath)(bstrSrcSaveStatePath.asOutParam());
470 if (FAILED(rc)) throw rc;
471 if (!bstrSrcSaveStatePath.isEmpty())
472 {
473 SAVESTATETASK sst;
474 sst.snapshotUuid = machine->getSnapshotId();
475 sst.strSaveStateFile = bstrSrcSaveStatePath;
476 uint64_t cbSize;
477 int vrc = RTFileQuerySize(sst.strSaveStateFile.c_str(), &cbSize);
478 if (RT_FAILURE(vrc))
479 throw p->setError(VBOX_E_IPRT_ERROR, p->tr("Could not query file size of '%s' (%Rrc)"), sst.strSaveStateFile.c_str(), vrc);
480 /* same rule as above: count both the data which needs to
481 * be read and written */
482 sst.uWeight = 2 * (cbSize + _1M - 1) / _1M;
483 d->llSaveStateFiles.append(sst);
484 ++uCount;
485 uTotalWeight += sst.uWeight;
486 }
487 }
488
489 rc = d->pProgress.createObject();
490 if (FAILED(rc)) throw rc;
491 rc = d->pProgress->init(p->getVirtualBox(),
492 static_cast<IMachine*>(d->pSrcMachine) /* aInitiator */,
493 Bstr(p->tr("Cloning Machine")).raw(),
494 true /* fCancellable */,
495 uCount,
496 uTotalWeight,
497 Bstr(p->tr("Initialize Cloning")).raw(),
498 1);
499 if (FAILED(rc)) throw rc;
500
501 int vrc = d->startWorker();
502
503 if (RT_FAILURE(vrc))
504 p->setError(VBOX_E_IPRT_ERROR, "Could not create machine clone thread (%Rrc)", vrc);
505 }
506 catch (HRESULT rc2)
507 {
508 rc = rc2;
509 }
510
511 if (SUCCEEDED(rc))
512 d->pProgress.queryInterfaceTo(pProgress);
513
514 return rc;
515}
516
517HRESULT MachineCloneVM::run()
518{
519 DPTR(MachineCloneVM);
520 ComObjPtr<Machine> &p = d->p;
521
522 AutoCaller autoCaller(p);
523 if (FAILED(autoCaller.rc())) return autoCaller.rc();
524
525 AutoReadLock srcLock(p COMMA_LOCKVAL_SRC_POS);
526 AutoWriteLock trgLock(d->pTrgMachine COMMA_LOCKVAL_SRC_POS);
527
528 HRESULT rc = S_OK;
529
530 /*
531 * Todo:
532 * - What about log files?
533 */
534
535 /* Where should all the media go? */
536 Utf8Str strTrgSnapshotFolder;
537 Utf8Str strTrgMachineFolder = d->pTrgMachine->getSettingsFileFull();
538 strTrgMachineFolder.stripFilename();
539
540 RTCList<ComObjPtr<Medium> > newMedia; /* All created images */
541 RTCList<Utf8Str> newFiles; /* All extra created files (save states, ...) */
542 try
543 {
544 /* Copy all the configuration from this machine to an empty
545 * configuration dataset. */
546 settings::MachineConfigFile trgMCF = *d->pSrcMachine->mData->pMachineConfigFile;
547
548 /* Reset media registry. */
549 trgMCF.mediaRegistry.llHardDisks.clear();
550 /* If we got a valid snapshot id, replace the hardware/storage section
551 * with the stuff from the snapshot. */
552 settings::Snapshot sn;
553 if (!d->snapshotId.isEmpty())
554 sn = d->findSnapshot(&trgMCF, trgMCF.llFirstSnapshot, d->snapshotId);
555
556 if (d->mode == CloneMode_MachineState)
557 {
558 if (!sn.uuid.isEmpty())
559 {
560 trgMCF.hardwareMachine = sn.hardware;
561 trgMCF.storageMachine = sn.storage;
562 }
563
564 /* Remove any hint on snapshots. */
565 trgMCF.llFirstSnapshot.clear();
566 trgMCF.uuidCurrentSnapshot.clear();
567 }
568 else if ( d->mode == CloneMode_MachineAndChildStates
569 && !sn.uuid.isEmpty())
570 {
571 /* Copy the snapshot data to the current machine. */
572 trgMCF.hardwareMachine = sn.hardware;
573 trgMCF.storageMachine = sn.storage;
574
575 /* The snapshot will be the root one. */
576 trgMCF.uuidCurrentSnapshot = sn.uuid;
577 trgMCF.llFirstSnapshot.clear();
578 trgMCF.llFirstSnapshot.push_back(sn);
579 }
580
581 /* Generate new MAC addresses for all machines when not forbidden. */
582 if (!d->options.contains(CloneOptions_KeepAllMACs))
583 {
584 d->updateMACAddresses(trgMCF.hardwareMachine.llNetworkAdapters);
585 d->updateMACAddresses(trgMCF.llFirstSnapshot);
586 }
587
588 /* When the current snapshot folder is absolute we reset it to the
589 * default relative folder. */
590 if (RTPathStartsWithRoot(trgMCF.machineUserData.strSnapshotFolder.c_str()))
591 trgMCF.machineUserData.strSnapshotFolder = "Snapshots";
592 trgMCF.strStateFile = "";
593 /* Force writing of setting file. */
594 trgMCF.fCurrentStateModified = true;
595 /* Set the new name. */
596 const Utf8Str strOldVMName = trgMCF.machineUserData.strName;
597 trgMCF.machineUserData.strName = d->pTrgMachine->mUserData->s.strName;
598 trgMCF.uuid = d->pTrgMachine->mData->mUuid;
599
600 Bstr bstrSrcSnapshotFolder;
601 rc = d->pSrcMachine->COMGETTER(SnapshotFolder)(bstrSrcSnapshotFolder.asOutParam());
602 if (FAILED(rc)) throw rc;
603 /* The absolute name of the snapshot folder. */
604 strTrgSnapshotFolder = Utf8StrFmt("%s%c%s", strTrgMachineFolder.c_str(), RTPATH_DELIMITER, trgMCF.machineUserData.strSnapshotFolder.c_str());
605
606 /* Should we rename the disk names. */
607 bool fKeepDiskNames = d->options.contains(CloneOptions_KeepDiskNames);
608
609 /* We need to create a map with the already created medias. This is
610 * necessary, cause different snapshots could have the same
611 * parents/parent chain. If a medium is in this map already, it isn't
612 * cloned a second time, but simply used. */
613 typedef std::map<Utf8Str, ComObjPtr<Medium> > TStrMediumMap;
614 typedef std::pair<Utf8Str, ComObjPtr<Medium> > TStrMediumPair;
615 TStrMediumMap map;
616 GuidList llRegistriesThatNeedSaving;
617 size_t cDisks = 0;
618 for (size_t i = 0; i < d->llMedias.size(); ++i)
619 {
620 const MEDIUMTASKCHAIN &mtc = d->llMedias.at(i);
621 ComObjPtr<Medium> pNewParent;
622 for (size_t a = mtc.chain.size(); a > 0; --a)
623 {
624 const MEDIUMTASK &mt = mtc.chain.at(a - 1);
625 ComPtr<IMedium> pMedium = mt.pMedium;
626
627 Bstr bstrSrcName;
628 rc = pMedium->COMGETTER(Name)(bstrSrcName.asOutParam());
629 if (FAILED(rc)) throw rc;
630
631 rc = d->pProgress->SetNextOperation(BstrFmt(p->tr("Cloning Disk '%ls' ..."), bstrSrcName.raw()).raw(), mt.uWeight);
632 if (FAILED(rc)) throw rc;
633
634 Bstr bstrSrcId;
635 rc = pMedium->COMGETTER(Id)(bstrSrcId.asOutParam());
636 if (FAILED(rc)) throw rc;
637
638 if (mtc.fAttachLinked)
639 {
640 IMedium *pTmp = pMedium;
641 ComObjPtr<Medium> pLMedium = static_cast<Medium*>(pTmp);
642 if (pLMedium.isNull())
643 throw E_POINTER;
644 ComObjPtr<Medium> pBase = pLMedium->getBase();
645 if (pBase->isReadOnly())
646 {
647 ComObjPtr<Medium> pDiff;
648 /* create the diff under the snapshot medium */
649 rc = createDiffHelper(pLMedium, strTrgSnapshotFolder,
650 &newMedia, &pDiff);
651 if (FAILED(rc)) throw rc;
652 map.insert(TStrMediumPair(Utf8Str(bstrSrcId), pDiff));
653 /* diff image has to be used... */
654 pNewParent = pDiff;
655 }
656 else
657 {
658 /* Attach the medium directly, as its type is not
659 * subject to diff creation. */
660 newMedia.append(pLMedium);
661 map.insert(TStrMediumPair(Utf8Str(bstrSrcId), pLMedium));
662 pNewParent = pLMedium;
663 }
664 }
665 else
666 {
667 /* Is a clone already there? */
668 TStrMediumMap::iterator it = map.find(Utf8Str(bstrSrcId));
669 if (it != map.end())
670 pNewParent = it->second;
671 else
672 {
673 ComPtr<IMediumFormat> pSrcFormat;
674 rc = pMedium->COMGETTER(MediumFormat)(pSrcFormat.asOutParam());
675 ULONG uSrcCaps = 0;
676 rc = pSrcFormat->COMGETTER(Capabilities)(&uSrcCaps);
677 if (FAILED(rc)) throw rc;
678
679 /* Default format? */
680 Utf8Str strDefaultFormat;
681 p->mParent->getDefaultHardDiskFormat(strDefaultFormat);
682 Bstr bstrSrcFormat(strDefaultFormat);
683 ULONG srcVar = MediumVariant_Standard;
684 /* Is the source file based? */
685 if ((uSrcCaps & MediumFormatCapabilities_File) == MediumFormatCapabilities_File)
686 {
687 /* Yes, just use the source format. Otherwise the defaults
688 * will be used. */
689 rc = pMedium->COMGETTER(Format)(bstrSrcFormat.asOutParam());
690 if (FAILED(rc)) throw rc;
691 rc = pMedium->COMGETTER(Variant)(&srcVar);
692 if (FAILED(rc)) throw rc;
693 }
694
695 Guid newId;
696 newId.create();
697 Utf8Str strNewName(bstrSrcName);
698 if (!fKeepDiskNames)
699 {
700 Utf8Str strSrcTest = bstrSrcName;
701 /* Check if we have to use another name. */
702 if (!mt.strBaseName.isEmpty())
703 strSrcTest = mt.strBaseName;
704 strSrcTest.stripExt();
705 /* If the old disk name was in {uuid} format we also
706 * want the new name in this format, but with the
707 * updated id of course. If the old disk was called
708 * like the VM name, we change it to the new VM name.
709 * For all other disks we rename them with this
710 * template: "new name-disk1.vdi". */
711 if (strSrcTest == strOldVMName)
712 strNewName = Utf8StrFmt("%s%s", trgMCF.machineUserData.strName.c_str(), RTPathExt(Utf8Str(bstrSrcName).c_str()));
713 else if ( strSrcTest.startsWith("{")
714 && strSrcTest.endsWith("}"))
715 {
716 strSrcTest = strSrcTest.substr(1, strSrcTest.length() - 2);
717 if (isValidGuid(strSrcTest))
718 strNewName = Utf8StrFmt("%s%s", newId.toStringCurly().c_str(), RTPathExt(strNewName.c_str()));
719 }
720 else
721 strNewName = Utf8StrFmt("%s-disk%d%s", trgMCF.machineUserData.strName.c_str(), ++cDisks, RTPathExt(Utf8Str(bstrSrcName).c_str()));
722 }
723
724 /* Check if this medium comes from the snapshot folder, if
725 * so, put it there in the cloned machine as well.
726 * Otherwise it goes to the machine folder. */
727 Bstr bstrSrcPath;
728 Utf8Str strFile = Utf8StrFmt("%s%c%s", strTrgMachineFolder.c_str(), RTPATH_DELIMITER, strNewName.c_str());
729 rc = pMedium->COMGETTER(Location)(bstrSrcPath.asOutParam());
730 if (FAILED(rc)) throw rc;
731 if ( !bstrSrcPath.isEmpty()
732 && RTPathStartsWith(Utf8Str(bstrSrcPath).c_str(), Utf8Str(bstrSrcSnapshotFolder).c_str())
733 && !(fKeepDiskNames || mt.strBaseName.isEmpty()))
734 strFile = Utf8StrFmt("%s%c%s", strTrgSnapshotFolder.c_str(), RTPATH_DELIMITER, strNewName.c_str());
735
736 /* Start creating the clone. */
737 ComObjPtr<Medium> pTarget;
738 rc = pTarget.createObject();
739 if (FAILED(rc)) throw rc;
740
741 rc = pTarget->init(p->mParent,
742 Utf8Str(bstrSrcFormat),
743 strFile,
744 Guid::Empty, /* empty media registry */
745 NULL /* llRegistriesThatNeedSaving */);
746 if (FAILED(rc)) throw rc;
747
748 /* Update the new uuid. */
749 pTarget->updateId(newId);
750
751 srcLock.release();
752 /* Do the disk cloning. */
753 ComPtr<IProgress> progress2;
754 rc = pMedium->CloneTo(pTarget,
755 srcVar,
756 pNewParent,
757 progress2.asOutParam());
758 if (FAILED(rc)) throw rc;
759
760 /* Wait until the async process has finished. */
761 rc = d->pProgress->WaitForAsyncProgressCompletion(progress2);
762 srcLock.acquire();
763 if (FAILED(rc)) throw rc;
764
765 /* Check the result of the async process. */
766 LONG iRc;
767 rc = progress2->COMGETTER(ResultCode)(&iRc);
768 if (FAILED(rc)) throw rc;
769 if (FAILED(iRc))
770 {
771 /* If the thread of the progress object has an error, then
772 * retrieve the error info from there, or it'll be lost. */
773 ProgressErrorInfo info(progress2);
774 throw p->setError(iRc, Utf8Str(info.getText()).c_str());
775 }
776 /* Remember created medium. */
777 newMedia.append(pTarget);
778 /* Get the medium type from the source and set it to the
779 * new medium. */
780 MediumType_T type;
781 rc = pMedium->COMGETTER(Type)(&type);
782 if (FAILED(rc)) throw rc;
783 rc = pTarget->COMSETTER(Type)(type);
784 if (FAILED(rc)) throw rc;
785 map.insert(TStrMediumPair(Utf8Str(bstrSrcId), pTarget));
786 /* register the new harddisk */
787 {
788 AutoWriteLock tlock(p->mParent->getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
789 rc = p->mParent->registerHardDisk(pTarget, NULL /* pllRegistriesThatNeedSaving */);
790 if (FAILED(rc)) throw rc;
791 }
792 /* This medium becomes the parent of the next medium in the
793 * chain. */
794 pNewParent = pTarget;
795 }
796 }
797 }
798
799 /* Create diffs for the last image chain. */
800 if (mtc.fCreateDiffs)
801 {
802 const MEDIUMTASK &mt = mtc.chain.first();
803 ComPtr<IMedium> pMedium = mt.pMedium;
804 IMedium *pTmp = pMedium;
805 ComObjPtr<Medium> pLMedium = static_cast<Medium*>(pTmp);
806 if (pLMedium.isNull())
807 throw E_POINTER;
808 ComObjPtr<Medium> pBase = pLMedium->getBase();
809 if (pBase->isReadOnly())
810 {
811 ComObjPtr<Medium> pDiff;
812 rc = createDiffHelper(pNewParent, strTrgSnapshotFolder,
813 &newMedia, &pDiff);
814 if (FAILED(rc)) throw rc;
815 /* diff image has to be used... */
816 pNewParent = pDiff;
817 }
818 else
819 {
820 /* Attach the medium directly, as its type is not
821 * subject to diff creation. */
822 newMedia.append(pNewParent);
823 }
824 }
825 Bstr bstrSrcId;
826 rc = mtc.chain.first().pMedium->COMGETTER(Id)(bstrSrcId.asOutParam());
827 if (FAILED(rc)) throw rc;
828 Bstr bstrTrgId;
829 rc = pNewParent->COMGETTER(Id)(bstrTrgId.asOutParam());
830 if (FAILED(rc)) throw rc;
831 /* We have to patch the configuration, so it contains the new
832 * medium uuid instead of the old one. */
833 d->updateStorageLists(trgMCF.storageMachine.llStorageControllers, bstrSrcId, bstrTrgId);
834 d->updateSnapshotStorageLists(trgMCF.llFirstSnapshot, bstrSrcId, bstrTrgId);
835 }
836 /* Make sure all disks know of the new machine uuid. We do this last to
837 * be able to change the medium type above. */
838 for (size_t i = newMedia.size(); i > 0; --i)
839 {
840 const ComObjPtr<Medium> &pMedium = newMedia.at(i - 1);
841 AutoCaller mac(pMedium);
842 if (FAILED(mac.rc())) throw mac.rc();
843 AutoWriteLock mlock(pMedium COMMA_LOCKVAL_SRC_POS);
844 Guid uuid = d->pTrgMachine->mData->mUuid;
845 if (d->options.contains(CloneOptions_Link))
846 {
847 ComObjPtr<Medium> pParent = pMedium->getParent();
848 mlock.release();
849 if (!pParent.isNull())
850 {
851 AutoCaller mac2(pParent);
852 if (FAILED(mac2.rc())) throw mac2.rc();
853 AutoReadLock mlock2(pParent COMMA_LOCKVAL_SRC_POS);
854 if (pParent->getFirstRegistryMachineId(uuid))
855 VirtualBox::addGuidToListUniquely(llRegistriesThatNeedSaving, uuid);
856 }
857 mlock.acquire();
858 }
859 pMedium->addRegistry(uuid, false /* fRecurse */);
860 }
861 /* Check if a snapshot folder is necessary and if so doesn't already
862 * exists. */
863 if ( !d->llSaveStateFiles.isEmpty()
864 && !RTDirExists(strTrgSnapshotFolder.c_str()))
865 {
866 int vrc = RTDirCreateFullPath(strTrgSnapshotFolder.c_str(), 0777);
867 if (RT_FAILURE(vrc))
868 throw p->setError(VBOX_E_IPRT_ERROR,
869 p->tr("Could not create snapshots folder '%s' (%Rrc)"), strTrgSnapshotFolder.c_str(), vrc);
870 }
871 /* Clone all save state files. */
872 for (size_t i = 0; i < d->llSaveStateFiles.size(); ++i)
873 {
874 SAVESTATETASK sst = d->llSaveStateFiles.at(i);
875 const Utf8Str &strTrgSaveState = Utf8StrFmt("%s%c%s", strTrgSnapshotFolder.c_str(), RTPATH_DELIMITER, RTPathFilename(sst.strSaveStateFile.c_str()));
876
877 /* Move to next sub-operation. */
878 rc = d->pProgress->SetNextOperation(BstrFmt(p->tr("Copy save state file '%s' ..."), RTPathFilename(sst.strSaveStateFile.c_str())).raw(), sst.uWeight);
879 if (FAILED(rc)) throw rc;
880 /* Copy the file only if it was not copied already. */
881 if (!newFiles.contains(strTrgSaveState.c_str()))
882 {
883 int vrc = RTFileCopyEx(sst.strSaveStateFile.c_str(), strTrgSaveState.c_str(), 0, MachineCloneVMPrivate::copyStateFileProgress, &d->pProgress);
884 if (RT_FAILURE(vrc))
885 throw p->setError(VBOX_E_IPRT_ERROR,
886 p->tr("Could not copy state file '%s' to '%s' (%Rrc)"), sst.strSaveStateFile.c_str(), strTrgSaveState.c_str(), vrc);
887 newFiles.append(strTrgSaveState);
888 }
889 /* Update the path in the configuration either for the current
890 * machine state or the snapshots. */
891 if (sst.snapshotUuid.isEmpty())
892 trgMCF.strStateFile = strTrgSaveState;
893 else
894 d->updateStateFile(trgMCF.llFirstSnapshot, sst.snapshotUuid, strTrgSaveState);
895 }
896
897 {
898 rc = d->pProgress->SetNextOperation(BstrFmt(p->tr("Create Machine Clone '%s' ..."), trgMCF.machineUserData.strName.c_str()).raw(), 1);
899 if (FAILED(rc)) throw rc;
900 /* After modifying the new machine config, we can copy the stuff
901 * over to the new machine. The machine have to be mutable for
902 * this. */
903 rc = d->pTrgMachine->checkStateDependency(p->MutableStateDep);
904 if (FAILED(rc)) throw rc;
905 rc = d->pTrgMachine->loadMachineDataFromSettings(trgMCF,
906 &d->pTrgMachine->mData->mUuid);
907 if (FAILED(rc)) throw rc;
908 }
909
910 /* Now save the new configuration to disk. */
911 rc = d->pTrgMachine->SaveSettings();
912 if (FAILED(rc)) throw rc;
913 trgLock.release();
914 if (!llRegistriesThatNeedSaving.empty())
915 {
916 srcLock.release();
917 rc = p->mParent->saveRegistries(llRegistriesThatNeedSaving);
918 if (FAILED(rc)) throw rc;
919 }
920 }
921 catch (HRESULT rc2)
922 {
923 rc = rc2;
924 }
925 catch (...)
926 {
927 rc = VirtualBox::handleUnexpectedExceptions(RT_SRC_POS);
928 }
929
930 MultiResult mrc(rc);
931 /* Cleanup on failure (CANCEL also) */
932 if (FAILED(rc))
933 {
934 int vrc = VINF_SUCCESS;
935 /* Delete all created files. */
936 for (size_t i = 0; i < newFiles.size(); ++i)
937 {
938 vrc = RTFileDelete(newFiles.at(i).c_str());
939 if (RT_FAILURE(vrc))
940 mrc = p->setError(VBOX_E_IPRT_ERROR, p->tr("Could not delete file '%s' (%Rrc)"), newFiles.at(i).c_str(), vrc);
941 }
942 /* Delete all already created medias. (Reverse, cause there could be
943 * parent->child relations.) */
944 for (size_t i = newMedia.size(); i > 0; --i)
945 {
946 const ComObjPtr<Medium> &pMedium = newMedia.at(i - 1);
947 mrc = pMedium->deleteStorage(NULL /* aProgress */,
948 true /* aWait */,
949 NULL /* llRegistriesThatNeedSaving */);
950 pMedium->Close();
951 }
952 /* Delete the snapshot folder when not empty. */
953 if (!strTrgSnapshotFolder.isEmpty())
954 RTDirRemove(strTrgSnapshotFolder.c_str());
955 /* Delete the machine folder when not empty. */
956 RTDirRemove(strTrgMachineFolder.c_str());
957 }
958
959 return mrc;
960}
961
962HRESULT MachineCloneVM::createDiffHelper(const ComObjPtr<Medium> &pParent,
963 const Utf8Str &strSnapshotFolder,
964 RTCList< ComObjPtr<Medium> > *pNewMedia,
965 ComObjPtr<Medium> *ppDiff)
966{
967 DPTR(MachineCloneVM);
968 ComObjPtr<Machine> &p = d->p;
969 HRESULT rc = S_OK;
970
971 try
972 {
973 Bstr bstrSrcId;
974 rc = pParent->COMGETTER(Id)(bstrSrcId.asOutParam());
975 if (FAILED(rc)) throw rc;
976 ComObjPtr<Medium> diff;
977 diff.createObject();
978 rc = diff->init(p->mParent,
979 pParent->getPreferredDiffFormat(),
980 Utf8StrFmt("%s%c", strSnapshotFolder.c_str(), RTPATH_DELIMITER),
981 Guid::Empty, /* empty media registry */
982 NULL); /* pllRegistriesThatNeedSaving */
983 if (FAILED(rc)) throw rc;
984 MediumLockList *pMediumLockList(new MediumLockList());
985 rc = diff->createMediumLockList(true /* fFailIfInaccessible */,
986 true /* fMediumLockWrite */,
987 pParent,
988 *pMediumLockList);
989 if (FAILED(rc)) throw rc;
990 rc = pMediumLockList->Lock();
991 if (FAILED(rc)) throw rc;
992 /* this already registers the new diff image */
993 rc = pParent->createDiffStorage(diff, MediumVariant_Standard,
994 pMediumLockList,
995 NULL /* aProgress */,
996 true /* aWait */,
997 NULL); // pllRegistriesThatNeedSaving
998 delete pMediumLockList;
999 if (FAILED(rc)) throw rc;
1000 /* Remember created medium. */
1001 pNewMedia->append(diff);
1002 *ppDiff = diff;
1003 }
1004 catch (HRESULT rc2)
1005 {
1006 rc = rc2;
1007 }
1008 catch (...)
1009 {
1010 rc = VirtualBox::handleUnexpectedExceptions(RT_SRC_POS);
1011 }
1012
1013 return rc;
1014}
1015
1016void MachineCloneVM::destroy()
1017{
1018 delete this;
1019}
1020
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette