Index: trunk/src/VBox/Main/HardDisk2Impl.cpp
===================================================================
--- trunk/src/VBox/Main/HardDisk2Impl.cpp	(revision 14930)
+++ trunk/src/VBox/Main/HardDisk2Impl.cpp	(revision 14931)
@@ -1874,5 +1874,5 @@
            HardDiskFormatCapabilities_CreateFixed)))
         return setError (VBOX_E_NOT_SUPPORTED,
-            tr ("Hard disk format '%ls' does not support storage deletion "),
+            tr ("Hard disk format '%ls' does not support storage deletion"),
             mm.format.raw());
 
@@ -2410,8 +2410,9 @@
                   E_FAIL);
 
-    /* are we dealing with a hard disk just opened from the existing path? */
-    bool isNew = mm.format.isNull();
-
-    if (isNew ||
+    /* are we dealing with a new hard disk constructed using the existing
+     * location? */
+    bool isImport = mm.format.isNull();
+
+    if (isImport ||
         (mm.formatObj->capabilities() & HardDiskFormatCapabilities_File))
     {
@@ -2466,6 +2467,6 @@
                 location.raw(), vrc);
 
-        /* detect the backend from the storage unit if new */
-        if (isNew)
+        /* detect the backend from the storage unit if importing */
+        if (isImport)
         {
             char *backendName = NULL;
@@ -2510,7 +2511,12 @@
             m.locationFull = locationFull;
 
-            /* assign a new UUID if we generated it */
-            if (!id.isEmpty())
+            if (m.state == MediaState_NotCreated)
+            {
+                /* assign a new UUID (this UUID will be used when calling
+                 * VDCreateBase/VDCreateDiff as a wanted UUID). Note that we
+                 * also do that if we didn't generate it to make sure it is
+                 * either generated by us or reset to null */
                 unconst (m.id) = id;
+            }
         }
         else
@@ -2657,6 +2663,7 @@
         Utf8Str location (m.locationFull);
 
-        /* are we dealing with a hard disk opened from the existing path? */
-        bool isNew = m.id.isEmpty();
+        /* are we dealing with a new hard disk constructed using the existing
+         * location? */
+        bool isImport = m.id.isEmpty();
 
         PVBOXHDD hdd;
@@ -2673,5 +2680,5 @@
              * time in VirtualBox (such as VMDK for which VDOpen() needs to
              * generate an UUID if it is missing) */
-            if (!isNew)
+            if (!isImport)
                 flags |= VD_OPEN_FLAGS_READONLY;
 
@@ -2686,25 +2693,39 @@
             }
 
-            /* check the UUID */
-            RTUUID uuid;
-            vrc = VDGetUuid (hdd, 0, &uuid);
-            ComAssertRCThrow (vrc, E_FAIL);
-
-            if (isNew)
+            if (mm.formatObj->capabilities() & HardDiskFormatCapabilities_Uuid)
             {
-                unconst (m.id) = uuid;
+                /* check the UUID */
+                RTUUID uuid;
+                vrc = VDGetUuid (hdd, 0, &uuid);
+                ComAssertRCThrow (vrc, E_FAIL);
+
+                if (isImport)
+                {
+                    unconst (m.id) = uuid;
+                }
+                else
+                {
+                    Assert (!m.id.isEmpty());
+
+                    if (m.id != uuid)
+                    {
+                        lastAccessError = Utf8StrFmt (
+                            tr ("UUID {%RTuuid} of the hard disk '%ls' does "
+                                "not match the value {%RTuuid} stored in the "
+                                "media registry ('%ls')"),
+                            &uuid, m.locationFull.raw(), m.id.raw(),
+                            mVirtualBox->settingsFileName().raw());
+                        throw S_OK;
+                    }
+                }
             }
             else
             {
-                if (m.id != uuid)
-                {
-                    lastAccessError = Utf8StrFmt (
-                        tr ("UUID {%RTuuid} of the hard disk '%ls' "
-                            "does not match the value {%RTuuid} stored in the "
-                            "media registry ('%ls')"),
-                        &uuid, m.locationFull.raw(), m.id.raw(),
-                        mVirtualBox->settingsFileName().raw());
-                    throw S_OK;
-                }
+                /* the backend does not support storing UUIDs within the
+                 * underlying storage so use what we store in XML */
+
+                /* generate an UUID for an imported UUID-less hard disk */
+                if (isImport)
+                    unconst (m.id).create();
             }
 
@@ -2716,8 +2737,9 @@
             if (type == VD_IMAGE_TYPE_DIFF)
             {
-                vrc = VDGetParentUuid (hdd, 0, &uuid);
+                RTUUID parentId;
+                vrc = VDGetParentUuid (hdd, 0, &parentId);
                 ComAssertRCThrow (vrc, E_FAIL);
 
-                if (isNew)
+                if (isImport)
                 {
                     /* the parent must be known to us. Note that we freely
@@ -2728,5 +2750,5 @@
                      * fail if this method reporst MediaState_Inaccessible) */
 
-                    Guid id = uuid;
+                    Guid id = parentId;
                     ComObjPtr <HardDisk2> parent;
                     rc = mVirtualBox->findHardDisk2 (&id, NULL,
@@ -2739,5 +2761,5 @@
                                 "hard disk '%ls' is not found in the media "
                                 "registry ('%ls')"),
-                            &uuid, m.locationFull.raw(),
+                            &parentId, m.locationFull.raw(),
                             mVirtualBox->settingsFileName().raw());
                         throw S_OK;
@@ -2777,5 +2799,5 @@
                     AutoReadLock parentLock (mParent);
                     if (mParent->state() != MediaState_Inaccessible &&
-                        mParent->id() != uuid)
+                        mParent->id() != parentId)
                     {
                         lastAccessError = Utf8StrFmt (
@@ -2783,5 +2805,5 @@
                                 "does not match UUID {%RTuuid} of its parent "
                                 "hard disk stored in the media registry ('%ls')"),
-                            &uuid, m.locationFull.raw(),
+                            &parentId, m.locationFull.raw(),
                             mParent->id().raw(),
                             mVirtualBox->settingsFileName().raw());
@@ -3118,6 +3140,9 @@
 
             /* these parameters we need after creation */
-            RTUUID uuid;
             uint64_t size = 0, logicalSize = 0;
+
+            /* the object may request a specific UUID (through a special
+             * form of the setLocation() argumet) */
+            Guid id = that->m.id;
 
             try
@@ -3129,4 +3154,5 @@
                 Utf8Str format (that->mm.format);
                 Utf8Str location (that->m.locationFull);
+                ULONG capabilities = that->mm.formatObj->capabilities();
 
                 /* unlock before the potentially lengthy operation */
@@ -3151,5 +3177,6 @@
                                         task->d.size * _1M,
                                         VD_IMAGE_FLAGS_NONE,
-                                        NULL, &geo, &geo, NULL,
+                                        NULL, &geo, &geo,
+                                        id.isEmpty() ? NULL : id.raw(),
                                         VD_OPEN_FLAGS_NORMAL,
                                         NULL, that->mm.vdDiskIfaces);
@@ -3163,6 +3190,20 @@
                     }
 
-                    vrc = VDGetUuid (hdd, 0, &uuid);
-                    ComAssertRCThrow (vrc, E_FAIL);
+                    if (capabilities & HardDiskFormatCapabilities_Uuid)
+                    {
+                        RTUUID uuid;
+                        vrc = VDGetUuid (hdd, 0, &uuid);
+                        ComAssertRCThrow (vrc, E_FAIL);
+
+                        if (!id.isEmpty())
+                            Assert (id == uuid);
+                        else
+                            id = uuid;
+                    }
+                    else
+                    {
+                        /* we have to generate an UUID ourselves */
+                        id.create();
+                    }
 
                     size = VDGetFileSize (hdd, 0);
@@ -3181,5 +3222,5 @@
                 thatLock.enter();
 
-                unconst (that->m.id) = uuid;
+                unconst (that->m.id) = id;
 
                 that->m.size = size;
@@ -3707,4 +3748,7 @@
             that->m.state = MediaState_NotCreated;
 
+            /* Reset UUID to prevent Create* from reusing it again */
+            unconst (that->m.id).clear();
+
             break;
         }
Index: trunk/src/VBox/Main/MediumImpl.cpp
===================================================================
--- trunk/src/VBox/Main/MediumImpl.cpp	(revision 14930)
+++ trunk/src/VBox/Main/MediumImpl.cpp	(revision 14931)
@@ -59,5 +59,6 @@
     CheckComRCReturnRC (autoCaller.rc());
 
-    /* m.id is constant during life time, no need to lock */
+    AutoReadLock alock (this);
+
     m.id.cloneTo (aId);
 
Index: trunk/src/VBox/Main/VirtualBoxImpl.cpp
===================================================================
--- trunk/src/VBox/Main/VirtualBoxImpl.cpp	(revision 14930)
+++ trunk/src/VBox/Main/VirtualBoxImpl.cpp	(revision 14931)
@@ -490,5 +490,6 @@
 # error "Port me!"
 #endif
-    /* Unload plugin backends. */
+
+    /* Unload hard disk plugin backends. */
     VDShutdown();
 
Index: trunk/src/VBox/Main/idl/VirtualBox.xidl
===================================================================
--- trunk/src/VBox/Main/idl/VirtualBox.xidl	(revision 14930)
+++ trunk/src/VBox/Main/idl/VirtualBox.xidl	(revision 14931)
@@ -6647,4 +6647,10 @@
         UUID of the medium. For a newly created medium, this value is a randomly
         generated UUID.
+
+        <note>
+          For media in one of MediaState::NotCreated, MediaState::Creating or
+          MediaState::Deleting states, the value of this property is undefined
+          and will most likely be an empty UUID.
+        </note>
       </desc>
     </attribute>
@@ -7601,4 +7607,10 @@
           <link to="IHardDiskFormat::capabilities"/>.
         </result>
+
+        <note>
+          If the deletion operation fails, it is not guaranteed that the storage
+          unit still exists. You may check the <link to="IMedium::state"/> value
+          to answer this question.
+        </note>
       </desc>
       <param name="progress" type="IProgress" dir="return">
