Index: /trunk/src/VBox/Main/src-server/MachineImplCloneVM.cpp
===================================================================
--- /trunk/src/VBox/Main/src-server/MachineImplCloneVM.cpp	(revision 37557)
+++ /trunk/src/VBox/Main/src-server/MachineImplCloneVM.cpp	(revision 37558)
@@ -454,5 +454,5 @@
     AutoWriteLock trgLock(d->pTrgMachine COMMA_LOCKVAL_SRC_POS);
 
-    MultiResult rc = S_OK;
+    HRESULT rc = S_OK;
 
     /*
@@ -587,9 +587,4 @@
                     }
 
-                    /* Start creating the clone. */
-                    ComObjPtr<Medium> pTarget;
-                    rc = pTarget.createObject();
-                    if (FAILED(rc)) throw rc;
-
                     /* Check if this medium comes from the snapshot folder, if
                      * so, put it there in the cloned machine as well.
@@ -605,9 +600,14 @@
                         strFile = Utf8StrFmt("%s%c%lS", strTrgMachineFolder.c_str(), RTPATH_DELIMITER, bstrSrcName.raw());
 
+                    /* Start creating the clone. */
+                    ComObjPtr<Medium> pTarget;
+                    rc = pTarget.createObject();
+                    if (FAILED(rc)) throw rc;
+
                     rc = pTarget->init(p->mParent,
                                        Utf8Str(bstrSrcFormat),
                                        strFile,
-                                       d->pTrgMachine->mData->mUuid,  /* media registry */
-                                       NULL                           /* llRegistriesThatNeedSaving */);
+                                       Guid(),  /* empty media registry */
+                                       NULL     /* llRegistriesThatNeedSaving */);
                     if (FAILED(rc)) throw rc;
 
@@ -638,4 +638,11 @@
                     }
 
+                    /* Get the medium type from the source and set it to the
+                     * new medium. */
+                    MediumType_T type;
+                    rc = pMedium->COMGETTER(Type)(&type);
+                    if (FAILED(rc)) throw rc;
+                    rc = pTarget->COMSETTER(Type)(type);
+                    if (FAILED(rc)) throw rc;
                     map.insert(TStrMediumPair(Utf8Str(bstrSrcId), pTarget));
 
@@ -654,5 +661,4 @@
                 rc = pNewParent->COMGETTER(Id)(bstrSrcId.asOutParam());
                 if (FAILED(rc)) throw rc;
-                GuidList *pllRegistriesThatNeedSaving;
                 ComObjPtr<Medium> diff;
                 diff.createObject();
@@ -660,8 +666,8 @@
                                 pNewParent->getPreferredDiffFormat(),
                                 Utf8StrFmt("%s%c", strTrgSnapshotFolder.c_str(), RTPATH_DELIMITER),
-                                d->pTrgMachine->mData->mUuid,
-                                NULL); // pllRegistriesThatNeedSaving
-                if (FAILED(rc)) throw rc;
-                MediumLockList *pMediumLockList(new MediumLockList()); /* todo: deleteeeeeeeee */
+                                Guid(), /* empty media registry */
+                                NULL);  /* pllRegistriesThatNeedSaving */
+                if (FAILED(rc)) throw rc;
+                MediumLockList *pMediumLockList(new MediumLockList());
                 rc = diff->createMediumLockList(true /* fFailIfInaccessible */,
                                                 true /* fMediumLockWrite */,
@@ -691,4 +697,8 @@
             d->updateStorageLists(trgMCF.storageMachine.llStorageControllers, bstrSrcId, bstrTrgId);
             d->updateSnapshotStorageLists(trgMCF.llFirstSnapshot, bstrSrcId, bstrTrgId);
+            /* Make sure all disks know of the new machine uuid. We do this
+             * last to be able to change the medium type above. */
+            rc = pNewParent->addRegistry(d->pTrgMachine->mData->mUuid, true /* fRecursive */);
+            if (FAILED(rc)) throw rc;
         }
         /* Clone all save state files. */
@@ -783,14 +793,4 @@
         }
 
-        /* The medias are created before the machine was there. We have to make
-         * sure the new medias know of there new parent or we get in trouble
-         * when the media registry is saved for this VM, especially in case of
-         * difference image chain's. See VirtualBox::saveMediaRegistry.*/
-//        for (size_t i = 0; i < newBaseMedias.size(); ++i)
-//        {
-//            rc = newBaseMedias.at(i)->addRegistry(d->pTrgMachine->mData->mUuid, true /* fRecursive */);
-//            if (FAILED(rc)) throw rc;
-//        }
-
         /* Now save the new configuration to disk. */
         rc = d->pTrgMachine->SaveSettings();
@@ -806,4 +806,5 @@
     }
 
+    MultiResult mrc(rc);
     /* Cleanup on failure (CANCEL also) */
     if (FAILED(rc))
@@ -815,5 +816,5 @@
             vrc = RTFileDelete(newFiles.at(i).c_str());
             if (RT_FAILURE(vrc))
-                rc = p->setError(VBOX_E_IPRT_ERROR, p->tr("Could not delete file '%s' (%Rrc)"), newFiles.at(i).c_str(), vrc);
+                mrc = p->setError(VBOX_E_IPRT_ERROR, p->tr("Could not delete file '%s' (%Rrc)"), newFiles.at(i).c_str(), vrc);
         }
         /* Delete all already created medias. (Reverse, cause there could be
@@ -826,5 +827,5 @@
             {
                 AutoCaller mac(pMedium);
-                if (FAILED(mac.rc())) { continue; rc = mac.rc(); }
+                if (FAILED(mac.rc())) { continue; mrc = mac.rc(); }
                 AutoReadLock mlock(pMedium COMMA_LOCKVAL_SRC_POS);
                 fFile = pMedium->isMediumFormatFile();
@@ -835,5 +836,5 @@
                 vrc = RTFileDelete(strLoc.c_str());
                 if (RT_FAILURE(vrc))
-                    rc = p->setError(VBOX_E_IPRT_ERROR, p->tr("Could not delete file '%s' (%Rrc)"), strLoc.c_str(), vrc);
+                    mrc = p->setError(VBOX_E_IPRT_ERROR, p->tr("Could not delete file '%s' (%Rrc)"), strLoc.c_str(), vrc);
             }
         }
@@ -845,5 +846,5 @@
     }
 
-    return rc;
+    return mrc;
 }
 
