1 | /** @file
|
---|
2 | * Virtual Disk Image (VDI), Core Code.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License as published by the Free Software Foundation,
|
---|
12 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
13 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
14 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | */
|
---|
16 |
|
---|
17 | /*******************************************************************************
|
---|
18 | * Header Files *
|
---|
19 | *******************************************************************************/
|
---|
20 | #define LOG_GROUP LOG_GROUP_DRV_VBOXHDD
|
---|
21 | #include <VBox/VBoxHDD.h>
|
---|
22 | #include "VDICore.h"
|
---|
23 | #include <VBox/err.h>
|
---|
24 |
|
---|
25 | #include <VBox/log.h>
|
---|
26 | #include <iprt/alloc.h>
|
---|
27 | #include <iprt/assert.h>
|
---|
28 | #include <iprt/uuid.h>
|
---|
29 | #include <iprt/file.h>
|
---|
30 | #include <iprt/string.h>
|
---|
31 | #include <iprt/asm.h>
|
---|
32 |
|
---|
33 |
|
---|
34 | /*******************************************************************************
|
---|
35 | * Internal Functions *
|
---|
36 | *******************************************************************************/
|
---|
37 | static unsigned getPowerOfTwo(unsigned uNumber);
|
---|
38 | static void vdiInitPreHeader(PVDIPREHEADER pPreHdr);
|
---|
39 | static int vdiValidatePreHeader(PVDIPREHEADER pPreHdr);
|
---|
40 | static void vdiInitHeader(PVDIHEADER pHeader, VDIIMAGETYPE enmType, uint32_t fFlags,
|
---|
41 | const char *pszComment, uint64_t cbDisk, uint32_t cbBlock,
|
---|
42 | uint32_t cbBlockExtra);
|
---|
43 | static int vdiValidateHeader(PVDIHEADER pHeader);
|
---|
44 | static int vdiCreateImage(const char *pszFilename, VDIIMAGETYPE enmType, unsigned fFlags,
|
---|
45 | uint64_t cbSize, const char *pszComment, PVDIIMAGEDESC pParent,
|
---|
46 | PFNVMPROGRESS pfnProgress, void *pvUser);
|
---|
47 | static void vdiInitImageDesc(PVDIIMAGEDESC pImage);
|
---|
48 | static void vdiSetupImageDesc(PVDIIMAGEDESC pImage);
|
---|
49 | static int vdiOpenImage(PVDIIMAGEDESC *ppImage, const char *pszFilename, unsigned fOpen,
|
---|
50 | PVDIIMAGEDESC pParent);
|
---|
51 | static int vdiUpdateHeader(PVDIIMAGEDESC pImage);
|
---|
52 | static int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock);
|
---|
53 | static int vdiUpdateBlocks(PVDIIMAGEDESC pImage);
|
---|
54 | static void vdiSetModifiedFlag(PVDIIMAGEDESC pImage);
|
---|
55 | static void vdiResetModifiedFlag(PVDIIMAGEDESC pImage);
|
---|
56 | static void vdiDisableLastModifiedUpdate(PVDIIMAGEDESC pImage);
|
---|
57 | #if 0 /* unused */
|
---|
58 | static void vdiEnableLastModifiedUpdate(PVDIIMAGEDESC pImage);
|
---|
59 | #endif
|
---|
60 | static void vdiCloseImage(PVDIIMAGEDESC pImage);
|
---|
61 | static int vdiReadInBlock(PVDIIMAGEDESC pImage, unsigned uBlock, unsigned offRead,
|
---|
62 | size_t cbToRead, void *pvBuf);
|
---|
63 | static int vdiFillBlockByZeroes(PVDIDISK pDisk, PVDIIMAGEDESC pImage, unsigned uBlock);
|
---|
64 | static int vdiWriteInBlock(PVDIDISK pDisk, PVDIIMAGEDESC pImage, unsigned uBlock,
|
---|
65 | unsigned offWrite, size_t cbToWrite, const void *pvBuf);
|
---|
66 | static int vdiCopyBlock(PVDIDISK pDisk, PVDIIMAGEDESC pImage, unsigned uBlock);
|
---|
67 | static int vdiMergeImages(PVDIIMAGEDESC pImageFrom, PVDIIMAGEDESC pImageTo, bool fParentToChild,
|
---|
68 | PFNVMPROGRESS pfnProgress, void *pvUser);
|
---|
69 | static void vdiAddImageToList(PVDIDISK pDisk, PVDIIMAGEDESC pImage);
|
---|
70 | static void vdiRemoveImageFromList(PVDIDISK pDisk, PVDIIMAGEDESC pImage);
|
---|
71 | static PVDIIMAGEDESC vdiGetImageByNumber(PVDIDISK pDisk, int nImage);
|
---|
72 | static int vdiUpdateReadOnlyHeader(PVDIIMAGEDESC pImage);
|
---|
73 |
|
---|
74 | static int vdiCommitToImage(PVDIDISK pDisk, PVDIIMAGEDESC pDstImage,
|
---|
75 | PFNVMPROGRESS pfnProgress, void *pvUser);
|
---|
76 | static void vdiDumpImage(PVDIIMAGEDESC pImage);
|
---|
77 |
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * internal: return power of 2 or 0 if num error.
|
---|
81 | */
|
---|
82 | static unsigned getPowerOfTwo(unsigned uNumber)
|
---|
83 | {
|
---|
84 | if (uNumber == 0)
|
---|
85 | return 0;
|
---|
86 | unsigned uPower2 = 0;
|
---|
87 | while ((uNumber & 1) == 0)
|
---|
88 | {
|
---|
89 | uNumber >>= 1;
|
---|
90 | uPower2++;
|
---|
91 | }
|
---|
92 | return uNumber == 1 ? uPower2 : 0;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * internal: init HDD preheader.
|
---|
97 | */
|
---|
98 | static void vdiInitPreHeader(PVDIPREHEADER pPreHdr)
|
---|
99 | {
|
---|
100 | pPreHdr->u32Signature = VDI_IMAGE_SIGNATURE;
|
---|
101 | pPreHdr->u32Version = VDI_IMAGE_VERSION;
|
---|
102 | memset(pPreHdr->szFileInfo, 0, sizeof(pPreHdr->szFileInfo));
|
---|
103 | strncat(pPreHdr->szFileInfo, VDI_IMAGE_FILE_INFO, sizeof(pPreHdr->szFileInfo));
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * internal: check HDD preheader.
|
---|
108 | */
|
---|
109 | static int vdiValidatePreHeader(PVDIPREHEADER pPreHdr)
|
---|
110 | {
|
---|
111 | if (pPreHdr->u32Signature != VDI_IMAGE_SIGNATURE)
|
---|
112 | return VERR_VDI_INVALID_SIGNATURE;
|
---|
113 |
|
---|
114 | if ( pPreHdr->u32Version != VDI_IMAGE_VERSION
|
---|
115 | && pPreHdr->u32Version != 0x00000002) /* old version. */
|
---|
116 | return VERR_VDI_UNSUPPORTED_VERSION;
|
---|
117 |
|
---|
118 | return VINF_SUCCESS;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * internal: init HDD header. Always use latest header version.
|
---|
123 | * @param pHeader Assumes it was initially initialized to all zeros.
|
---|
124 | */
|
---|
125 | static void vdiInitHeader(PVDIHEADER pHeader, VDIIMAGETYPE enmType, uint32_t fFlags,
|
---|
126 | const char *pszComment, uint64_t cbDisk, uint32_t cbBlock,
|
---|
127 | uint32_t cbBlockExtra)
|
---|
128 | {
|
---|
129 | pHeader->uVersion = VDI_IMAGE_VERSION;
|
---|
130 | pHeader->u.v1.cbHeader = sizeof(VDIHEADER1);
|
---|
131 | pHeader->u.v1.u32Type = (uint32_t)enmType;
|
---|
132 | pHeader->u.v1.fFlags = fFlags;
|
---|
133 | #ifdef VBOX_STRICT
|
---|
134 | char achZero[VDI_IMAGE_COMMENT_SIZE] = {0};
|
---|
135 | Assert(!memcmp(pHeader->u.v1.szComment, achZero, VDI_IMAGE_COMMENT_SIZE));
|
---|
136 | #endif
|
---|
137 | pHeader->u.v1.szComment[0] = '\0';
|
---|
138 | if (pszComment)
|
---|
139 | {
|
---|
140 | AssertMsg(strlen(pszComment) < sizeof(pHeader->u.v1.szComment),
|
---|
141 | ("HDD Comment is too long, cb=%d\n", strlen(pszComment)));
|
---|
142 | strncat(pHeader->u.v1.szComment, pszComment, sizeof(pHeader->u.v1.szComment));
|
---|
143 | }
|
---|
144 |
|
---|
145 | /* Mark the geometry not-calculated. */
|
---|
146 | pHeader->u.v1.Geometry.cCylinders = 0;
|
---|
147 | pHeader->u.v1.Geometry.cHeads = 0;
|
---|
148 | pHeader->u.v1.Geometry.cSectors = 0;
|
---|
149 | pHeader->u.v1.Geometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
150 | pHeader->u.v1.u32Translation = PDMBIOSTRANSLATION_AUTO;
|
---|
151 |
|
---|
152 | pHeader->u.v1.cbDisk = cbDisk;
|
---|
153 | pHeader->u.v1.cbBlock = cbBlock;
|
---|
154 | pHeader->u.v1.cBlocks = (uint32_t)(cbDisk / cbBlock);
|
---|
155 | if (cbDisk % cbBlock)
|
---|
156 | pHeader->u.v1.cBlocks++;
|
---|
157 | pHeader->u.v1.cbBlockExtra = cbBlockExtra;
|
---|
158 | pHeader->u.v1.cBlocksAllocated = 0;
|
---|
159 |
|
---|
160 | /* Init offsets. */
|
---|
161 | pHeader->u.v1.offBlocks = RT_ALIGN_32(sizeof(VDIPREHEADER) + sizeof(VDIHEADER1), VDI_GEOMETRY_SECTOR_SIZE);
|
---|
162 | pHeader->u.v1.offData = RT_ALIGN_32(pHeader->u.v1.offBlocks + (pHeader->u.v1.cBlocks * sizeof(VDIIMAGEBLOCKPOINTER)), VDI_GEOMETRY_SECTOR_SIZE);
|
---|
163 |
|
---|
164 | /* Init uuids. */
|
---|
165 | RTUuidCreate(&pHeader->u.v1.uuidCreate);
|
---|
166 | RTUuidClear(&pHeader->u.v1.uuidModify);
|
---|
167 | RTUuidClear(&pHeader->u.v1.uuidLinkage);
|
---|
168 | RTUuidClear(&pHeader->u.v1.uuidParentModify);
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * internal: check HDD header.
|
---|
173 | */
|
---|
174 | static int vdiValidateHeader(PVDIHEADER pHeader)
|
---|
175 | {
|
---|
176 | /* Check verion-dependend header parameters. */
|
---|
177 | switch (GET_MAJOR_HEADER_VERSION(pHeader))
|
---|
178 | {
|
---|
179 | case 0:
|
---|
180 | {
|
---|
181 | /* Old header version. */
|
---|
182 | break;
|
---|
183 | }
|
---|
184 | case 1:
|
---|
185 | {
|
---|
186 | /* Current header version. */
|
---|
187 |
|
---|
188 | if (pHeader->u.v1.cbHeader < sizeof(VDIHEADER1))
|
---|
189 | {
|
---|
190 | LogRel(("VDI: v1 header size wrong (%d < %d)\n",
|
---|
191 | pHeader->u.v1.cbHeader, sizeof(VDIHEADER1)));
|
---|
192 | return VERR_VDI_INVALID_HEADER;
|
---|
193 | }
|
---|
194 |
|
---|
195 | if (getImageBlocksOffset(pHeader) < (sizeof(VDIPREHEADER) + sizeof(VDIHEADER1)))
|
---|
196 | {
|
---|
197 | LogRel(("VDI: v1 blocks offset wrong (%d < %d)\n",
|
---|
198 | getImageBlocksOffset(pHeader), sizeof(VDIPREHEADER) + sizeof(VDIHEADER1)));
|
---|
199 | return VERR_VDI_INVALID_HEADER;
|
---|
200 | }
|
---|
201 |
|
---|
202 | if (getImageDataOffset(pHeader) < (getImageBlocksOffset(pHeader) + getImageBlocks(pHeader) * sizeof(VDIIMAGEBLOCKPOINTER)))
|
---|
203 | {
|
---|
204 | LogRel(("VDI: v1 image data offset wrong (%d < %d)\n",
|
---|
205 | getImageDataOffset(pHeader), getImageBlocksOffset(pHeader) + getImageBlocks(pHeader) * sizeof(VDIIMAGEBLOCKPOINTER)));
|
---|
206 | return VERR_VDI_INVALID_HEADER;
|
---|
207 | }
|
---|
208 |
|
---|
209 | if ( getImageType(pHeader) == VDI_IMAGE_TYPE_UNDO
|
---|
210 | || getImageType(pHeader) == VDI_IMAGE_TYPE_DIFF)
|
---|
211 | {
|
---|
212 | if (RTUuidIsNull(getImageParentUUID(pHeader)))
|
---|
213 | {
|
---|
214 | LogRel(("VDI: v1 uuid of parent is 0)\n"));
|
---|
215 | return VERR_VDI_INVALID_HEADER;
|
---|
216 | }
|
---|
217 | if (RTUuidIsNull(getImageParentModificationUUID(pHeader)))
|
---|
218 | {
|
---|
219 | LogRel(("VDI: v1 uuid of parent modification is 0\n"));
|
---|
220 | return VERR_VDI_INVALID_HEADER;
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | break;
|
---|
225 | }
|
---|
226 | default:
|
---|
227 | /* Unsupported. */
|
---|
228 | return VERR_VDI_UNSUPPORTED_VERSION;
|
---|
229 | }
|
---|
230 |
|
---|
231 | /* Check common header parameters. */
|
---|
232 |
|
---|
233 | bool fFailed = false;
|
---|
234 |
|
---|
235 | if ( getImageType(pHeader) < VDI_IMAGE_TYPE_FIRST
|
---|
236 | || getImageType(pHeader) > VDI_IMAGE_TYPE_LAST)
|
---|
237 | {
|
---|
238 | LogRel(("VDI: bad image type %d\n", getImageType(pHeader)));
|
---|
239 | fFailed = true;
|
---|
240 | }
|
---|
241 |
|
---|
242 | if (getImageFlags(pHeader) & ~VDI_IMAGE_FLAGS_MASK)
|
---|
243 | {
|
---|
244 | LogRel(("VDI: bad image flags %08x\n", getImageFlags(pHeader)));
|
---|
245 | fFailed = true;
|
---|
246 | }
|
---|
247 |
|
---|
248 | if ((getImageGeometry(pHeader))->cbSector != VDI_GEOMETRY_SECTOR_SIZE)
|
---|
249 | {
|
---|
250 | LogRel(("VDI: wrong sector size (%d != %d)\n",
|
---|
251 | (getImageGeometry(pHeader))->cbSector, VDI_GEOMETRY_SECTOR_SIZE));
|
---|
252 | fFailed = true;
|
---|
253 | }
|
---|
254 |
|
---|
255 | if ( getImageDiskSize(pHeader) == 0
|
---|
256 | || getImageBlockSize(pHeader) == 0
|
---|
257 | || getImageBlocks(pHeader) == 0
|
---|
258 | || getPowerOfTwo(getImageBlockSize(pHeader)) == 0)
|
---|
259 | {
|
---|
260 | LogRel(("VDI: wrong size (%lld, %d, %d, %d)\n",
|
---|
261 | getImageDiskSize(pHeader), getImageBlockSize(pHeader),
|
---|
262 | getImageBlocks(pHeader), getPowerOfTwo(getImageBlockSize(pHeader))));
|
---|
263 | fFailed = true;
|
---|
264 | }
|
---|
265 |
|
---|
266 | if (getImageBlocksAllocated(pHeader) > getImageBlocks(pHeader))
|
---|
267 | {
|
---|
268 | LogRel(("VDI: too many blocks allocated (%d > %d)\n"
|
---|
269 | " blocksize=%d disksize=%lld\n",
|
---|
270 | getImageBlocksAllocated(pHeader), getImageBlocks(pHeader),
|
---|
271 | getImageBlockSize(pHeader), getImageDiskSize(pHeader)));
|
---|
272 | fFailed = true;
|
---|
273 | }
|
---|
274 |
|
---|
275 | if ( getImageExtraBlockSize(pHeader) != 0
|
---|
276 | && getPowerOfTwo(getImageExtraBlockSize(pHeader)) == 0)
|
---|
277 | {
|
---|
278 | LogRel(("VDI: wrong extra size (%d, %d)\n",
|
---|
279 | getImageExtraBlockSize(pHeader), getPowerOfTwo(getImageExtraBlockSize(pHeader))));
|
---|
280 | fFailed = true;
|
---|
281 | }
|
---|
282 |
|
---|
283 | if ((uint64_t)getImageBlockSize(pHeader) * getImageBlocks(pHeader) < getImageDiskSize(pHeader))
|
---|
284 | {
|
---|
285 | LogRel(("VDI: wrong disk size (%d, %d, %lld)\n",
|
---|
286 | getImageBlockSize(pHeader), getImageBlocks(pHeader), getImageDiskSize(pHeader)));
|
---|
287 | fFailed = true;
|
---|
288 | }
|
---|
289 |
|
---|
290 | if (RTUuidIsNull(getImageCreationUUID(pHeader)))
|
---|
291 | {
|
---|
292 | LogRel(("VDI: uuid of creator is 0\n"));
|
---|
293 | fFailed = true;
|
---|
294 | }
|
---|
295 |
|
---|
296 | if (RTUuidIsNull(getImageModificationUUID(pHeader)))
|
---|
297 | {
|
---|
298 | LogRel(("VDI: uuid of modificator is 0\n"));
|
---|
299 | fFailed = true;
|
---|
300 | }
|
---|
301 |
|
---|
302 | return fFailed ? VERR_VDI_INVALID_HEADER : VINF_SUCCESS;
|
---|
303 | }
|
---|
304 |
|
---|
305 | /**
|
---|
306 | * internal: init VDIIMAGEDESC structure.
|
---|
307 | */
|
---|
308 | static void vdiInitImageDesc(PVDIIMAGEDESC pImage)
|
---|
309 | {
|
---|
310 | pImage->pPrev = NULL;
|
---|
311 | pImage->pNext = NULL;
|
---|
312 | pImage->File = NIL_RTFILE;
|
---|
313 | pImage->paBlocks = NULL;
|
---|
314 | }
|
---|
315 |
|
---|
316 | /**
|
---|
317 | * internal: setup VDIIMAGEDESC structure by image header.
|
---|
318 | */
|
---|
319 | static void vdiSetupImageDesc(PVDIIMAGEDESC pImage)
|
---|
320 | {
|
---|
321 | pImage->fFlags = getImageFlags(&pImage->Header);
|
---|
322 | pImage->offStartBlocks = getImageBlocksOffset(&pImage->Header);
|
---|
323 | pImage->offStartData = getImageDataOffset(&pImage->Header);
|
---|
324 | pImage->uBlockMask = getImageBlockSize(&pImage->Header) - 1;
|
---|
325 | pImage->uShiftIndex2Offset =
|
---|
326 | pImage->uShiftOffset2Index = getPowerOfTwo(getImageBlockSize(&pImage->Header));
|
---|
327 | pImage->offStartBlockData = getImageExtraBlockSize(&pImage->Header);
|
---|
328 | if (pImage->offStartBlockData != 0)
|
---|
329 | pImage->uShiftIndex2Offset += getPowerOfTwo(pImage->offStartBlockData);
|
---|
330 | }
|
---|
331 |
|
---|
332 | /**
|
---|
333 | * internal: create image.
|
---|
334 | */
|
---|
335 | static int vdiCreateImage(const char *pszFilename, VDIIMAGETYPE enmType, unsigned fFlags,
|
---|
336 | uint64_t cbSize, const char *pszComment, PVDIIMAGEDESC pParent,
|
---|
337 | PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
338 | {
|
---|
339 | /* Check args. */
|
---|
340 | Assert(pszFilename);
|
---|
341 | Assert(enmType >= VDI_IMAGE_TYPE_FIRST && enmType <= VDI_IMAGE_TYPE_LAST);
|
---|
342 | Assert(!(fFlags & ~VDI_IMAGE_FLAGS_MASK));
|
---|
343 | Assert(cbSize);
|
---|
344 |
|
---|
345 | /* Special check for comment length. */
|
---|
346 | if ( pszComment
|
---|
347 | && strlen(pszComment) >= VDI_IMAGE_COMMENT_SIZE)
|
---|
348 | {
|
---|
349 | Log(("vdiCreateImage: pszComment is too long, cb=%d\n", strlen(pszComment)));
|
---|
350 | return VERR_VDI_COMMENT_TOO_LONG;
|
---|
351 | }
|
---|
352 |
|
---|
353 | if ( enmType == VDI_IMAGE_TYPE_UNDO
|
---|
354 | || enmType == VDI_IMAGE_TYPE_DIFF)
|
---|
355 | {
|
---|
356 | Assert(pParent);
|
---|
357 | if ((pParent->PreHeader.u32Version >> 16) != VDI_IMAGE_VERSION_MAJOR)
|
---|
358 | {
|
---|
359 | /* Invalid parent image version. */
|
---|
360 | Log(("vdiCreateImage: unsupported parent version=%08X\n", pParent->PreHeader.u32Version));
|
---|
361 | return VERR_VDI_UNSUPPORTED_VERSION;
|
---|
362 | }
|
---|
363 |
|
---|
364 | /* get image params from the parent image. */
|
---|
365 | fFlags = getImageFlags(&pParent->Header);
|
---|
366 | cbSize = getImageDiskSize(&pParent->Header);
|
---|
367 | }
|
---|
368 |
|
---|
369 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
|
---|
370 | if (!pImage)
|
---|
371 | return VERR_NO_MEMORY;
|
---|
372 | vdiInitImageDesc(pImage);
|
---|
373 |
|
---|
374 | vdiInitPreHeader(&pImage->PreHeader);
|
---|
375 | vdiInitHeader(&pImage->Header, enmType, fFlags, pszComment, cbSize, VDI_IMAGE_DEFAULT_BLOCK_SIZE, 0);
|
---|
376 |
|
---|
377 | if ( enmType == VDI_IMAGE_TYPE_UNDO
|
---|
378 | || enmType == VDI_IMAGE_TYPE_DIFF)
|
---|
379 | {
|
---|
380 | /* Set up linkage information. */
|
---|
381 | pImage->Header.u.v1.uuidLinkage = *getImageCreationUUID(&pParent->Header);
|
---|
382 | pImage->Header.u.v1.uuidParentModify = *getImageModificationUUID(&pParent->Header);
|
---|
383 | }
|
---|
384 |
|
---|
385 | pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
|
---|
386 | if (!pImage->paBlocks)
|
---|
387 | {
|
---|
388 | RTMemFree(pImage);
|
---|
389 | return VERR_NO_MEMORY;
|
---|
390 | }
|
---|
391 |
|
---|
392 | if (enmType != VDI_IMAGE_TYPE_FIXED)
|
---|
393 | {
|
---|
394 | /* for growing images mark all blocks in paBlocks as free. */
|
---|
395 | for (unsigned i = 0; i < pImage->Header.u.v1.cBlocks; i++)
|
---|
396 | pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
|
---|
397 | }
|
---|
398 | else
|
---|
399 | {
|
---|
400 | /* for fixed images mark all blocks in paBlocks as allocated */
|
---|
401 | for (unsigned i = 0; i < pImage->Header.u.v1.cBlocks; i++)
|
---|
402 | pImage->paBlocks[i] = i;
|
---|
403 | pImage->Header.u.v1.cBlocksAllocated = pImage->Header.u.v1.cBlocks;
|
---|
404 | }
|
---|
405 |
|
---|
406 | /* Setup image parameters. */
|
---|
407 | vdiSetupImageDesc(pImage);
|
---|
408 |
|
---|
409 | /* create file */
|
---|
410 | int rc = RTFileOpen(&pImage->File,
|
---|
411 | pszFilename,
|
---|
412 | RTFILE_O_READWRITE | RTFILE_O_CREATE | RTFILE_O_DENY_ALL);
|
---|
413 | if (VBOX_SUCCESS(rc))
|
---|
414 | {
|
---|
415 | /* Lock image exclusively to close any wrong access by VDI API calls. */
|
---|
416 | uint64_t cbLock = pImage->offStartData
|
---|
417 | + ((uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset);
|
---|
418 | rc = RTFileLock(pImage->File,
|
---|
419 | RTFILE_LOCK_WRITE | RTFILE_LOCK_IMMEDIATELY, 0, cbLock);
|
---|
420 | if (VBOX_FAILURE(rc))
|
---|
421 | {
|
---|
422 | cbLock = 0; /* Not locked. */
|
---|
423 | goto l_create_failed;
|
---|
424 | }
|
---|
425 |
|
---|
426 | if (enmType == VDI_IMAGE_TYPE_FIXED)
|
---|
427 | {
|
---|
428 | /*
|
---|
429 | * Allocate & commit whole file if fixed image, it must be more
|
---|
430 | * effective than expanding file by write operations.
|
---|
431 | */
|
---|
432 | rc = RTFileSetSize(pImage->File,
|
---|
433 | pImage->offStartData
|
---|
434 | + ((uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset));
|
---|
435 | }
|
---|
436 | else
|
---|
437 | {
|
---|
438 | /* Set file size to hold header and blocks array. */
|
---|
439 | rc = RTFileSetSize(pImage->File, pImage->offStartData);
|
---|
440 | }
|
---|
441 | if (VBOX_FAILURE(rc))
|
---|
442 | goto l_create_failed;
|
---|
443 |
|
---|
444 | /* Generate image last-modify uuid */
|
---|
445 | RTUuidCreate(getImageModificationUUID(&pImage->Header));
|
---|
446 |
|
---|
447 | /* Write pre-header. */
|
---|
448 | rc = RTFileWrite(pImage->File, &pImage->PreHeader, sizeof(pImage->PreHeader), NULL);
|
---|
449 | if (VBOX_FAILURE(rc))
|
---|
450 | goto l_create_failed;
|
---|
451 |
|
---|
452 | /* Write header. */
|
---|
453 | rc = RTFileWrite(pImage->File, &pImage->Header.u.v1, sizeof(pImage->Header.u.v1), NULL);
|
---|
454 | if (VBOX_FAILURE(rc))
|
---|
455 | goto l_create_failed;
|
---|
456 |
|
---|
457 | /* Write blocks array. */
|
---|
458 | rc = RTFileSeek(pImage->File, pImage->offStartBlocks, RTFILE_SEEK_BEGIN, NULL);
|
---|
459 | if (VBOX_FAILURE(rc))
|
---|
460 | goto l_create_failed;
|
---|
461 | rc = RTFileWrite(pImage->File,
|
---|
462 | pImage->paBlocks,
|
---|
463 | getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
464 | NULL);
|
---|
465 | if (VBOX_FAILURE(rc))
|
---|
466 | goto l_create_failed;
|
---|
467 |
|
---|
468 | if ( (enmType == VDI_IMAGE_TYPE_FIXED)
|
---|
469 | && (fFlags & VDI_IMAGE_FLAGS_ZERO_EXPAND))
|
---|
470 | {
|
---|
471 | /* Fill image with zeroes. */
|
---|
472 |
|
---|
473 | rc = RTFileSeek(pImage->File, pImage->offStartData, RTFILE_SEEK_BEGIN, NULL);
|
---|
474 | if (VBOX_FAILURE(rc))
|
---|
475 | goto l_create_failed;
|
---|
476 |
|
---|
477 | /* alloc tmp zero-filled buffer */
|
---|
478 | void *pvBuf = RTMemTmpAllocZ(VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
479 | if (pvBuf)
|
---|
480 | {
|
---|
481 | uint64_t cbFill = (uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset;
|
---|
482 | uint64_t cbDisk = cbFill;
|
---|
483 |
|
---|
484 | /* do loop to fill all image. */
|
---|
485 | while (cbFill > 0)
|
---|
486 | {
|
---|
487 | unsigned to_fill = (unsigned)RT_MIN(cbFill, VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
488 |
|
---|
489 | rc = RTFileWrite(pImage->File, pvBuf, to_fill, NULL);
|
---|
490 | if (VBOX_FAILURE(rc))
|
---|
491 | break;
|
---|
492 |
|
---|
493 | cbFill -= to_fill;
|
---|
494 |
|
---|
495 | if (pfnProgress)
|
---|
496 | {
|
---|
497 | rc = pfnProgress(NULL /* WARNING! pVM=NULL */,
|
---|
498 | (unsigned)(((cbDisk - cbFill) * 100) / cbDisk),
|
---|
499 | pvUser);
|
---|
500 | if (VBOX_FAILURE(rc))
|
---|
501 | break;
|
---|
502 | }
|
---|
503 | }
|
---|
504 | RTMemTmpFree(pvBuf);
|
---|
505 | }
|
---|
506 | else
|
---|
507 | {
|
---|
508 | /* alloc error */
|
---|
509 | rc = VERR_NO_MEMORY;
|
---|
510 | }
|
---|
511 | }
|
---|
512 |
|
---|
513 | l_create_failed:
|
---|
514 |
|
---|
515 | if (cbLock)
|
---|
516 | RTFileUnlock(pImage->File, 0, cbLock);
|
---|
517 |
|
---|
518 | RTFileClose(pImage->File);
|
---|
519 |
|
---|
520 | /* Delete image file if error occured while creating */
|
---|
521 | if (VBOX_FAILURE(rc))
|
---|
522 | RTFileDelete(pszFilename);
|
---|
523 | }
|
---|
524 |
|
---|
525 | RTMemFree(pImage->paBlocks);
|
---|
526 | RTMemFree(pImage);
|
---|
527 |
|
---|
528 | if ( VBOX_SUCCESS(rc)
|
---|
529 | && pfnProgress)
|
---|
530 | pfnProgress(NULL /* WARNING! pVM=NULL */, 100, pvUser);
|
---|
531 |
|
---|
532 | Log(("vdiCreateImage: done, filename=\"%s\", rc=%Vrc\n", pszFilename, rc));
|
---|
533 |
|
---|
534 | return rc;
|
---|
535 | }
|
---|
536 |
|
---|
537 | /**
|
---|
538 | * Open an image.
|
---|
539 | * @internal
|
---|
540 | */
|
---|
541 | static int vdiOpenImage(PVDIIMAGEDESC *ppImage, const char *pszFilename,
|
---|
542 | unsigned fOpen, PVDIIMAGEDESC pParent)
|
---|
543 | {
|
---|
544 | /*
|
---|
545 | * Validate input.
|
---|
546 | */
|
---|
547 | Assert(ppImage);
|
---|
548 | Assert(pszFilename);
|
---|
549 | Assert(!(fOpen & ~VDI_OPEN_FLAGS_MASK));
|
---|
550 |
|
---|
551 | PVDIIMAGEDESC pImage;
|
---|
552 | size_t cchFilename = strlen(pszFilename);
|
---|
553 | if (cchFilename >= sizeof(pImage->szFilename))
|
---|
554 | {
|
---|
555 | AssertMsgFailed(("filename=\"%s\" is too long (%d bytes)!\n", pszFilename, cchFilename));
|
---|
556 | return VERR_FILENAME_TOO_LONG;
|
---|
557 | }
|
---|
558 |
|
---|
559 | pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
|
---|
560 | if (!pImage)
|
---|
561 | return VERR_NO_MEMORY;
|
---|
562 | vdiInitImageDesc(pImage);
|
---|
563 |
|
---|
564 | memcpy(pImage->szFilename, pszFilename, cchFilename);
|
---|
565 | pImage->fOpen = fOpen;
|
---|
566 |
|
---|
567 | /*
|
---|
568 | * Open the image.
|
---|
569 | */
|
---|
570 | int rc = RTFileOpen(&pImage->File,
|
---|
571 | pImage->szFilename,
|
---|
572 | fOpen & VDI_OPEN_FLAGS_READONLY
|
---|
573 | ? RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE
|
---|
574 | : RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
575 | if (VBOX_FAILURE(rc))
|
---|
576 | {
|
---|
577 | if (!(fOpen & VDI_OPEN_FLAGS_READONLY))
|
---|
578 | {
|
---|
579 | /* Try to open image for reading only. */
|
---|
580 | rc = RTFileOpen(&pImage->File,
|
---|
581 | pImage->szFilename,
|
---|
582 | RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
583 | if (VBOX_SUCCESS(rc))
|
---|
584 | pImage->fOpen |= VDI_OPEN_FLAGS_READONLY;
|
---|
585 | }
|
---|
586 | if (VBOX_FAILURE(rc))
|
---|
587 | {
|
---|
588 | RTMemFree(pImage);
|
---|
589 | return rc;
|
---|
590 | }
|
---|
591 | }
|
---|
592 | /* Set up current image r/w state. */
|
---|
593 | pImage->fReadOnly = !!(pImage->fOpen & VDI_OPEN_FLAGS_READONLY);
|
---|
594 |
|
---|
595 | /*
|
---|
596 | * Set initial file lock for reading header only.
|
---|
597 | * Length of lock doesn't matter, it just must include image header.
|
---|
598 | */
|
---|
599 | uint64_t cbLock = _1M;
|
---|
600 | rc = RTFileLock(pImage->File, RTFILE_LOCK_READ | RTFILE_LOCK_IMMEDIATELY, 0, cbLock);
|
---|
601 | if (VBOX_FAILURE(rc))
|
---|
602 | {
|
---|
603 | cbLock = 0;
|
---|
604 | goto l_open_failed;
|
---|
605 | }
|
---|
606 |
|
---|
607 | /* Read pre-header. */
|
---|
608 | rc = RTFileRead(pImage->File, &pImage->PreHeader, sizeof(pImage->PreHeader), NULL);
|
---|
609 | if (VBOX_FAILURE(rc))
|
---|
610 | goto l_open_failed;
|
---|
611 | rc = vdiValidatePreHeader(&pImage->PreHeader);
|
---|
612 | if (VBOX_FAILURE(rc))
|
---|
613 | goto l_open_failed;
|
---|
614 |
|
---|
615 | /* Read header. */
|
---|
616 | pImage->Header.uVersion = pImage->PreHeader.u32Version;
|
---|
617 | switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
|
---|
618 | {
|
---|
619 | case 0:
|
---|
620 | rc = RTFileRead(pImage->File, &pImage->Header.u.v0, sizeof(pImage->Header.u.v0), NULL);
|
---|
621 | break;
|
---|
622 | case 1:
|
---|
623 | rc = RTFileRead(pImage->File, &pImage->Header.u.v1, sizeof(pImage->Header.u.v1), NULL);
|
---|
624 | break;
|
---|
625 | default:
|
---|
626 | rc = VERR_VDI_UNSUPPORTED_VERSION;
|
---|
627 | break;
|
---|
628 | }
|
---|
629 | if (VBOX_FAILURE(rc))
|
---|
630 | goto l_open_failed;
|
---|
631 |
|
---|
632 | rc = vdiValidateHeader(&pImage->Header);
|
---|
633 | if (VBOX_FAILURE(rc))
|
---|
634 | goto l_open_failed;
|
---|
635 |
|
---|
636 | /* Check diff image correctness. */
|
---|
637 | if (pParent)
|
---|
638 | {
|
---|
639 | if (pImage->PreHeader.u32Version != pParent->PreHeader.u32Version)
|
---|
640 | {
|
---|
641 | rc = VERR_VDI_IMAGES_VERSION_MISMATCH;
|
---|
642 | goto l_open_failed;
|
---|
643 | }
|
---|
644 |
|
---|
645 | if ( getImageType(&pImage->Header) != VDI_IMAGE_TYPE_UNDO
|
---|
646 | && getImageType(&pImage->Header) != VDI_IMAGE_TYPE_DIFF)
|
---|
647 | {
|
---|
648 | rc = VERR_VDI_WRONG_DIFF_IMAGE;
|
---|
649 | goto l_open_failed;
|
---|
650 | }
|
---|
651 |
|
---|
652 | if ( getImageDiskSize(&pImage->Header) != getImageDiskSize(&pParent->Header)
|
---|
653 | || getImageBlockSize(&pImage->Header) != getImageBlockSize(&pParent->Header)
|
---|
654 | || getImageBlocks(&pImage->Header) != getImageBlocks(&pParent->Header)
|
---|
655 | || getImageExtraBlockSize(&pImage->Header) != getImageExtraBlockSize(&pParent->Header))
|
---|
656 | {
|
---|
657 | rc = VERR_VDI_WRONG_DIFF_IMAGE;
|
---|
658 | goto l_open_failed;
|
---|
659 | }
|
---|
660 |
|
---|
661 | /* Check linkage data. */
|
---|
662 | if ( RTUuidCompare(getImageParentUUID(&pImage->Header),
|
---|
663 | getImageCreationUUID(&pParent->Header))
|
---|
664 | || RTUuidCompare(getImageParentModificationUUID(&pImage->Header),
|
---|
665 | getImageModificationUUID(&pParent->Header)))
|
---|
666 | {
|
---|
667 | rc = VERR_VDI_IMAGES_UUID_MISMATCH;
|
---|
668 | goto l_open_failed;
|
---|
669 | }
|
---|
670 | }
|
---|
671 |
|
---|
672 | /* Setup image parameters by header. */
|
---|
673 | vdiSetupImageDesc(pImage);
|
---|
674 |
|
---|
675 | /* reset modified flag into first-modified state. */
|
---|
676 | pImage->fModified = VDI_IMAGE_MODIFIED_FIRST;
|
---|
677 |
|
---|
678 | /* Image is validated, set working file lock on it. */
|
---|
679 | rc = RTFileUnlock(pImage->File, 0, cbLock);
|
---|
680 | AssertRC(rc);
|
---|
681 | cbLock = pImage->offStartData
|
---|
682 | + ((uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset);
|
---|
683 | rc = RTFileLock(pImage->File,
|
---|
684 | (pImage->fReadOnly) ?
|
---|
685 | RTFILE_LOCK_READ | RTFILE_LOCK_IMMEDIATELY :
|
---|
686 | RTFILE_LOCK_WRITE | RTFILE_LOCK_IMMEDIATELY,
|
---|
687 | 0,
|
---|
688 | cbLock);
|
---|
689 | if ( VBOX_FAILURE(rc)
|
---|
690 | && !pImage->fReadOnly)
|
---|
691 | {
|
---|
692 | /* Failed to lock image for writing, try read-only lock. */
|
---|
693 | rc = RTFileLock(pImage->File,
|
---|
694 | RTFILE_LOCK_READ | RTFILE_LOCK_IMMEDIATELY, 0, cbLock);
|
---|
695 | if (VBOX_SUCCESS(rc))
|
---|
696 | pImage->fReadOnly = true;
|
---|
697 | }
|
---|
698 | if (VBOX_FAILURE(rc))
|
---|
699 | {
|
---|
700 | cbLock = 0; /* Not locked. */
|
---|
701 | goto l_open_failed;
|
---|
702 | }
|
---|
703 |
|
---|
704 | /* Allocate memory for blocks array. */
|
---|
705 | pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
|
---|
706 | if (!pImage->paBlocks)
|
---|
707 | {
|
---|
708 | rc = VERR_NO_MEMORY;
|
---|
709 | goto l_open_failed;
|
---|
710 | }
|
---|
711 |
|
---|
712 | /* Read blocks array. */
|
---|
713 | rc = RTFileSeek(pImage->File, pImage->offStartBlocks, RTFILE_SEEK_BEGIN, NULL);
|
---|
714 | if (VBOX_FAILURE(rc))
|
---|
715 | goto l_open_failed;
|
---|
716 | rc = RTFileRead(pImage->File, pImage->paBlocks,
|
---|
717 | getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER), NULL);
|
---|
718 | if (VBOX_FAILURE(rc))
|
---|
719 | goto l_open_failed;
|
---|
720 |
|
---|
721 | /* all done. */
|
---|
722 | *ppImage = pImage;
|
---|
723 | return VINF_SUCCESS;
|
---|
724 |
|
---|
725 | l_open_failed:
|
---|
726 | /* Clean up. */
|
---|
727 | if (pImage->paBlocks)
|
---|
728 | RTMemFree(pImage->paBlocks);
|
---|
729 | if (cbLock)
|
---|
730 | RTFileUnlock(pImage->File, 0, cbLock);
|
---|
731 | RTFileClose(pImage->File);
|
---|
732 | RTMemFree(pImage);
|
---|
733 | Log(("vdiOpenImage: failed, filename=\"%s\", rc=%Vrc\n", pszFilename, rc));
|
---|
734 | return rc;
|
---|
735 | }
|
---|
736 |
|
---|
737 | /**
|
---|
738 | * internal: save header to file.
|
---|
739 | */
|
---|
740 | static int vdiUpdateHeader(PVDIIMAGEDESC pImage)
|
---|
741 | {
|
---|
742 | /* Seek to header start. */
|
---|
743 | int rc = RTFileSeek(pImage->File, sizeof(VDIPREHEADER), RTFILE_SEEK_BEGIN, NULL);
|
---|
744 | if (VBOX_SUCCESS(rc))
|
---|
745 | {
|
---|
746 | switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
|
---|
747 | {
|
---|
748 | case 0:
|
---|
749 | rc = RTFileWrite(pImage->File, &pImage->Header.u.v0, sizeof(pImage->Header.u.v0), NULL);
|
---|
750 | break;
|
---|
751 | case 1:
|
---|
752 | rc = RTFileWrite(pImage->File, &pImage->Header.u.v1, sizeof(pImage->Header.u.v1), NULL);
|
---|
753 | break;
|
---|
754 | default:
|
---|
755 | rc = VERR_VDI_UNSUPPORTED_VERSION;
|
---|
756 | break;
|
---|
757 | }
|
---|
758 | }
|
---|
759 | AssertMsgRC(rc, ("vdiUpdateHeader failed, filename=\"%s\" rc=%Vrc\n", pImage->szFilename, rc));
|
---|
760 | return rc;
|
---|
761 | }
|
---|
762 |
|
---|
763 | /**
|
---|
764 | * internal: save block pointer to file, save header to file.
|
---|
765 | */
|
---|
766 | static int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock)
|
---|
767 | {
|
---|
768 | /* Update image header. */
|
---|
769 | int rc = vdiUpdateHeader(pImage);
|
---|
770 | if (VBOX_SUCCESS(rc))
|
---|
771 | {
|
---|
772 | /* write only one block pointer. */
|
---|
773 | rc = RTFileSeek(pImage->File,
|
---|
774 | pImage->offStartBlocks + uBlock * sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
775 | RTFILE_SEEK_BEGIN,
|
---|
776 | NULL);
|
---|
777 | if (VBOX_SUCCESS(rc))
|
---|
778 | rc = RTFileWrite(pImage->File,
|
---|
779 | &pImage->paBlocks[uBlock],
|
---|
780 | sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
781 | NULL);
|
---|
782 | AssertMsgRC(rc, ("vdiUpdateBlockInfo failed to update block=%u, filename=\"%s\", rc=%Vrc\n",
|
---|
783 | uBlock, pImage->szFilename, rc));
|
---|
784 | }
|
---|
785 | return rc;
|
---|
786 | }
|
---|
787 |
|
---|
788 | /**
|
---|
789 | * internal: save blocks array to file, save header to file.
|
---|
790 | */
|
---|
791 | static int vdiUpdateBlocks(PVDIIMAGEDESC pImage)
|
---|
792 | {
|
---|
793 | /* Update image header. */
|
---|
794 | int rc = vdiUpdateHeader(pImage);
|
---|
795 | if (VBOX_SUCCESS(rc))
|
---|
796 | {
|
---|
797 | /* write the block pointers array. */
|
---|
798 | rc = RTFileSeek(pImage->File, pImage->offStartBlocks, RTFILE_SEEK_BEGIN, NULL);
|
---|
799 | if (VBOX_SUCCESS(rc))
|
---|
800 | rc = RTFileWrite(pImage->File,
|
---|
801 | pImage->paBlocks,
|
---|
802 | sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header),
|
---|
803 | NULL);
|
---|
804 | AssertMsgRC(rc, ("vdiUpdateBlocks failed, filename=\"%s\", rc=%Vrc\n",
|
---|
805 | pImage->szFilename, rc));
|
---|
806 | }
|
---|
807 | return rc;
|
---|
808 | }
|
---|
809 |
|
---|
810 | /**
|
---|
811 | * internal: mark image as modified, if this is the first change - update image header
|
---|
812 | * on disk with a new uuidModify value.
|
---|
813 | */
|
---|
814 | static void vdiSetModifiedFlag(PVDIIMAGEDESC pImage)
|
---|
815 | {
|
---|
816 | pImage->fModified |= VDI_IMAGE_MODIFIED_FLAG;
|
---|
817 | if (pImage->fModified & VDI_IMAGE_MODIFIED_FIRST)
|
---|
818 | {
|
---|
819 | pImage->fModified &= ~VDI_IMAGE_MODIFIED_FIRST;
|
---|
820 |
|
---|
821 | /* first modify - generate uuidModify and save to file. */
|
---|
822 | vdiResetModifiedFlag(pImage);
|
---|
823 |
|
---|
824 | if (!(pImage->fModified | VDI_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
|
---|
825 | {
|
---|
826 | /* save header to file,
|
---|
827 | * note: no rc checking.
|
---|
828 | */
|
---|
829 | vdiUpdateHeader(pImage);
|
---|
830 | }
|
---|
831 | }
|
---|
832 | }
|
---|
833 |
|
---|
834 | /**
|
---|
835 | * internal: generate new uuidModify if the image was changed.
|
---|
836 | */
|
---|
837 | static void vdiResetModifiedFlag(PVDIIMAGEDESC pImage)
|
---|
838 | {
|
---|
839 | if (pImage->fModified & VDI_IMAGE_MODIFIED_FLAG)
|
---|
840 | {
|
---|
841 | /* generate new last-modified uuid */
|
---|
842 | if (!(pImage->fModified | VDI_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
|
---|
843 | RTUuidCreate(getImageModificationUUID(&pImage->Header));
|
---|
844 |
|
---|
845 | pImage->fModified &= ~VDI_IMAGE_MODIFIED_FLAG;
|
---|
846 | }
|
---|
847 | }
|
---|
848 |
|
---|
849 | /**
|
---|
850 | * internal: disables updates of the last-modified UUID
|
---|
851 | * when performing image writes.
|
---|
852 | */
|
---|
853 | static void vdiDisableLastModifiedUpdate(PVDIIMAGEDESC pImage)
|
---|
854 | {
|
---|
855 | pImage->fModified |= VDI_IMAGE_MODIFIED_DISABLE_UUID_UPDATE;
|
---|
856 | }
|
---|
857 |
|
---|
858 | #if 0 /* unused */
|
---|
859 | /**
|
---|
860 | * internal: enables updates of the last-modified UUID
|
---|
861 | * when performing image writes.
|
---|
862 | */
|
---|
863 | static void vdiEnableLastModifiedUpdate(PVDIIMAGEDESC pImage)
|
---|
864 | {
|
---|
865 | pImage->fModified &= ~VDI_IMAGE_MODIFIED_DISABLE_UUID_UPDATE;
|
---|
866 | }
|
---|
867 | #endif
|
---|
868 |
|
---|
869 | /**
|
---|
870 | * Flush the image file to disk.
|
---|
871 | */
|
---|
872 | void vdiFlushImage(PVDIIMAGEDESC pImage)
|
---|
873 | {
|
---|
874 | if (!pImage->fReadOnly)
|
---|
875 | {
|
---|
876 | /* Update last-modified uuid if need. */
|
---|
877 | vdiResetModifiedFlag(pImage);
|
---|
878 |
|
---|
879 | /* Save header. */
|
---|
880 | int rc = vdiUpdateHeader(pImage);
|
---|
881 | AssertMsgRC(rc, ("vdiUpdateHeader() failed, filename=\"%s\", rc=%Vrc\n",
|
---|
882 | pImage->szFilename, rc));
|
---|
883 | RTFileFlush(pImage->File);
|
---|
884 | }
|
---|
885 | }
|
---|
886 |
|
---|
887 | /**
|
---|
888 | * internal: close image file.
|
---|
889 | */
|
---|
890 | static void vdiCloseImage(PVDIIMAGEDESC pImage)
|
---|
891 | {
|
---|
892 | /* Params checking. */
|
---|
893 | Assert(pImage);
|
---|
894 | Assert(pImage->File != NIL_RTFILE);
|
---|
895 |
|
---|
896 | vdiFlushImage(pImage);
|
---|
897 | RTFileUnlock(pImage->File,
|
---|
898 | 0,
|
---|
899 | pImage->offStartData
|
---|
900 | + ((uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset));
|
---|
901 | RTFileClose(pImage->File);
|
---|
902 |
|
---|
903 | /* free image resources */
|
---|
904 | RTMemFree(pImage->paBlocks);
|
---|
905 | RTMemFree(pImage);
|
---|
906 | }
|
---|
907 |
|
---|
908 | /**
|
---|
909 | * internal: read data inside image block.
|
---|
910 | *
|
---|
911 | * note: uBlock must be valid, readed data must not overlap block bounds.
|
---|
912 | */
|
---|
913 | static int vdiReadInBlock(PVDIIMAGEDESC pImage, unsigned uBlock, unsigned offRead,
|
---|
914 | size_t cbToRead, void *pvBuf)
|
---|
915 | {
|
---|
916 | if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
|
---|
917 | {
|
---|
918 | /* block present in image file */
|
---|
919 | uint64_t u64Offset = ((uint64_t)pImage->paBlocks[uBlock] << pImage->uShiftIndex2Offset)
|
---|
920 | + (pImage->offStartData + pImage->offStartBlockData + offRead);
|
---|
921 | int rc = RTFileSeek(pImage->File, u64Offset, RTFILE_SEEK_BEGIN, NULL);
|
---|
922 | if (VBOX_SUCCESS(rc))
|
---|
923 | rc = RTFileRead(pImage->File, pvBuf, cbToRead, NULL);
|
---|
924 | if (VBOX_FAILURE(rc))
|
---|
925 | Log(("vdiReadInBlock: rc=%Vrc filename=\"%s\" uBlock=%u offRead=%u cbToRead=%u u64Offset=%llu\n",
|
---|
926 | rc, pImage->szFilename, uBlock, offRead, cbToRead, u64Offset));
|
---|
927 | return rc;
|
---|
928 | }
|
---|
929 |
|
---|
930 | /* Returns zeroes for both free and zero block types. */
|
---|
931 | memset(pvBuf, 0, cbToRead);
|
---|
932 | return VINF_SUCCESS;
|
---|
933 | }
|
---|
934 |
|
---|
935 | /**
|
---|
936 | * Read data from virtual HDD.
|
---|
937 | *
|
---|
938 | * @returns VBox status code.
|
---|
939 | * @param pDisk Pointer to VDI HDD container.
|
---|
940 | * @param offStart Offset of first reading byte from start of disk.
|
---|
941 | * @param pvBuf Pointer to buffer for reading data.
|
---|
942 | * @param cbToRead Number of bytes to read.
|
---|
943 | */
|
---|
944 | VBOXDDU_DECL(int) VDIDiskRead(PVDIDISK pDisk, uint64_t offStart, void *pvBuf, size_t cbToRead)
|
---|
945 | {
|
---|
946 | /* sanity check */
|
---|
947 | Assert(pDisk);
|
---|
948 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
949 |
|
---|
950 | PVDIIMAGEDESC pImage = pDisk->pLast;
|
---|
951 | Assert(pImage);
|
---|
952 |
|
---|
953 | /* Check params. */
|
---|
954 | if ( offStart + cbToRead > getImageDiskSize(&pImage->Header)
|
---|
955 | || cbToRead == 0)
|
---|
956 | {
|
---|
957 | AssertMsgFailed(("offStart=%llu cbToRead=%u\n", offStart, cbToRead));
|
---|
958 | return VERR_INVALID_PARAMETER;
|
---|
959 | }
|
---|
960 |
|
---|
961 | /* Calculate starting block number and offset inside it. */
|
---|
962 | unsigned uBlock = (unsigned)(offStart >> pImage->uShiftOffset2Index);
|
---|
963 | unsigned offRead = (unsigned)offStart & pImage->uBlockMask;
|
---|
964 |
|
---|
965 | /* Save block size here for speed optimization. */
|
---|
966 | unsigned cbBlock = getImageBlockSize(&pImage->Header);
|
---|
967 |
|
---|
968 | /* loop through blocks */
|
---|
969 | int rc;
|
---|
970 | for (;;)
|
---|
971 | {
|
---|
972 | size_t to_read;
|
---|
973 | if ((offRead + cbToRead) <= cbBlock)
|
---|
974 | to_read = cbToRead;
|
---|
975 | else
|
---|
976 | to_read = cbBlock - offRead;
|
---|
977 |
|
---|
978 | if (pDisk->cImages > 1)
|
---|
979 | {
|
---|
980 | /* Differencing images are used, handle them. */
|
---|
981 | pImage = pDisk->pLast;
|
---|
982 |
|
---|
983 | /* Search for image with allocated block. */
|
---|
984 | while (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE)
|
---|
985 | {
|
---|
986 | pImage = pImage->pPrev;
|
---|
987 | if (!pImage)
|
---|
988 | {
|
---|
989 | /* Block is not allocated in all images of chain. */
|
---|
990 | pImage = pDisk->pLast;
|
---|
991 | break;
|
---|
992 | }
|
---|
993 | }
|
---|
994 | }
|
---|
995 |
|
---|
996 | rc = vdiReadInBlock(pImage, uBlock, offRead, to_read, pvBuf);
|
---|
997 |
|
---|
998 | cbToRead -= to_read;
|
---|
999 | if ( cbToRead == 0
|
---|
1000 | || VBOX_FAILURE(rc))
|
---|
1001 | break;
|
---|
1002 |
|
---|
1003 | /* goto next block */
|
---|
1004 | uBlock++;
|
---|
1005 | offRead = 0;
|
---|
1006 | pvBuf = (char *)pvBuf + to_read;
|
---|
1007 | }
|
---|
1008 |
|
---|
1009 | return rc;
|
---|
1010 | }
|
---|
1011 |
|
---|
1012 | /**
|
---|
1013 | * internal: fill the whole block with zeroes.
|
---|
1014 | *
|
---|
1015 | * note: block id must be valid, block must be already allocated in file.
|
---|
1016 | * note: if pDisk is NULL, the default buffer size is used
|
---|
1017 | */
|
---|
1018 | static int vdiFillBlockByZeroes(PVDIDISK pDisk, PVDIIMAGEDESC pImage, unsigned uBlock)
|
---|
1019 | {
|
---|
1020 | int rc;
|
---|
1021 |
|
---|
1022 | /* seek to start of block in file. */
|
---|
1023 | uint64_t u64Offset = ((uint64_t)pImage->paBlocks[uBlock] << pImage->uShiftIndex2Offset)
|
---|
1024 | + (pImage->offStartData + pImage->offStartBlockData);
|
---|
1025 | rc = RTFileSeek(pImage->File, u64Offset, RTFILE_SEEK_BEGIN, NULL);
|
---|
1026 | if (VBOX_FAILURE(rc))
|
---|
1027 | {
|
---|
1028 | Log(("vdiFillBlockByZeroes: seek rc=%Vrc filename=\"%s\" uBlock=%u u64Offset=%llu\n",
|
---|
1029 | rc, pImage->szFilename, uBlock, u64Offset));
|
---|
1030 | return rc;
|
---|
1031 | }
|
---|
1032 |
|
---|
1033 | /* alloc tmp zero-filled buffer */
|
---|
1034 | void *pvBuf = RTMemTmpAllocZ(pDisk ? pDisk->cbBuf : VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
1035 | if (!pvBuf)
|
---|
1036 | return VERR_NO_MEMORY;
|
---|
1037 |
|
---|
1038 | unsigned cbFill = getImageBlockSize(&pImage->Header);
|
---|
1039 |
|
---|
1040 | /* do loop, because buffer size may be less then block size */
|
---|
1041 | while (cbFill > 0)
|
---|
1042 | {
|
---|
1043 | unsigned to_fill = RT_MIN(cbFill, pDisk ? pDisk->cbBuf : VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
1044 | rc = RTFileWrite(pImage->File, pvBuf, to_fill, NULL);
|
---|
1045 | if (VBOX_FAILURE(rc))
|
---|
1046 | {
|
---|
1047 | Log(("vdiFillBlockByZeroes: write rc=%Vrc filename=\"%s\" uBlock=%u u64Offset=%llu cbFill=%u to_fill=%u\n",
|
---|
1048 | rc, pImage->szFilename, uBlock, u64Offset, cbFill, to_fill));
|
---|
1049 | break;
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | cbFill -= to_fill;
|
---|
1053 | }
|
---|
1054 |
|
---|
1055 | RTMemTmpFree(pvBuf);
|
---|
1056 | return rc;
|
---|
1057 | }
|
---|
1058 |
|
---|
1059 | /**
|
---|
1060 | * internal: write data inside image block.
|
---|
1061 | *
|
---|
1062 | * note: uBlock must be valid, written data must not overlap block bounds.
|
---|
1063 | */
|
---|
1064 | static int vdiWriteInBlock(PVDIDISK pDisk, PVDIIMAGEDESC pImage, unsigned uBlock, unsigned offWrite, size_t cbToWrite, const void *pvBuf)
|
---|
1065 | {
|
---|
1066 | int rc;
|
---|
1067 |
|
---|
1068 | /* Check if we can write into file. */
|
---|
1069 | if (pImage->fReadOnly)
|
---|
1070 | {
|
---|
1071 | Log(("vdiWriteInBlock: failed, image \"%s\" is read-only!\n", pImage->szFilename));
|
---|
1072 | return VERR_WRITE_PROTECT;
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | /* This could be optimized a little (not setting it when writing zeroes
|
---|
1076 | * to a zeroed block). Won't buy us much, because it's very unlikely
|
---|
1077 | * that only such zero data block writes occur while the VDI is opened. */
|
---|
1078 | vdiSetModifiedFlag(pImage);
|
---|
1079 |
|
---|
1080 | if (!IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
|
---|
1081 | {
|
---|
1082 | if (!pDisk || !pDisk->fHonorZeroWrites)
|
---|
1083 | {
|
---|
1084 | /* If the destination block is unallocated at this point, it's either
|
---|
1085 | * a zero block or a block which hasn't been used so far (which also
|
---|
1086 | * means that it's a zero block. Don't need to write anything to this
|
---|
1087 | * block if the data consists of just zeroes. */
|
---|
1088 | Assert(cbToWrite % 4 == 0);
|
---|
1089 | if (ASMBitFirstSet((volatile void *)pvBuf, cbToWrite * 8) == -1)
|
---|
1090 | {
|
---|
1091 | pImage->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
|
---|
1092 | return VINF_SUCCESS;
|
---|
1093 | }
|
---|
1094 | }
|
---|
1095 |
|
---|
1096 | /* need to allocate a new block in image file */
|
---|
1097 |
|
---|
1098 | /* expand file by one block */
|
---|
1099 | uint64_t u64Size = (((uint64_t)(getImageBlocksAllocated(&pImage->Header) + 1)) << pImage->uShiftIndex2Offset)
|
---|
1100 | + pImage->offStartData;
|
---|
1101 | rc = RTFileSetSize(pImage->File, u64Size);
|
---|
1102 | if (VBOX_FAILURE(rc))
|
---|
1103 | {
|
---|
1104 | Log(("vdiWriteInBlock: set size rc=%Vrc filename=\"%s\" uBlock=%u u64Size=%llu\n",
|
---|
1105 | rc, pImage->szFilename, uBlock, u64Size));
|
---|
1106 | return rc;
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 | unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
|
---|
1110 | pImage->paBlocks[uBlock] = cBlocksAllocated;
|
---|
1111 | setImageBlocksAllocated(&pImage->Header, cBlocksAllocated + 1);
|
---|
1112 |
|
---|
1113 | if ( pImage->fFlags & VDI_IMAGE_FLAGS_ZERO_EXPAND
|
---|
1114 | || pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO)
|
---|
1115 | {
|
---|
1116 | /* Fill newly allocated block by zeroes. */
|
---|
1117 |
|
---|
1118 | if (offWrite || cbToWrite != getImageBlockSize(&pImage->Header))
|
---|
1119 | {
|
---|
1120 | rc = vdiFillBlockByZeroes(pDisk, pImage, uBlock);
|
---|
1121 | if (VBOX_FAILURE(rc))
|
---|
1122 | return rc;
|
---|
1123 | }
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 | rc = vdiUpdateBlockInfo(pImage, uBlock);
|
---|
1127 | if (VBOX_FAILURE(rc))
|
---|
1128 | return rc;
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 | /* Now block present in image file, write data inside it. */
|
---|
1132 | uint64_t u64Offset = ((uint64_t)pImage->paBlocks[uBlock] << pImage->uShiftIndex2Offset)
|
---|
1133 | + (pImage->offStartData + pImage->offStartBlockData + offWrite);
|
---|
1134 | rc = RTFileSeek(pImage->File, u64Offset, RTFILE_SEEK_BEGIN, NULL);
|
---|
1135 | if (VBOX_SUCCESS(rc))
|
---|
1136 | {
|
---|
1137 | rc = RTFileWrite(pImage->File, pvBuf, cbToWrite, NULL);
|
---|
1138 | if (VBOX_FAILURE(rc))
|
---|
1139 | Log(("vdiWriteInBlock: write rc=%Vrc filename=\"%s\" uBlock=%u offWrite=%u u64Offset=%llu cbToWrite=%u\n",
|
---|
1140 | rc, pImage->szFilename, uBlock, offWrite, u64Offset, cbToWrite));
|
---|
1141 | }
|
---|
1142 | else
|
---|
1143 | Log(("vdiWriteInBlock: seek rc=%Vrc filename=\"%s\" uBlock=%u offWrite=%u u64Offset=%llu\n",
|
---|
1144 | rc, pImage->szFilename, uBlock, offWrite, u64Offset));
|
---|
1145 |
|
---|
1146 | return rc;
|
---|
1147 | }
|
---|
1148 |
|
---|
1149 | /**
|
---|
1150 | * internal: copy data block from one (parent) image to last image.
|
---|
1151 | */
|
---|
1152 | static int vdiCopyBlock(PVDIDISK pDisk, PVDIIMAGEDESC pImage, unsigned uBlock)
|
---|
1153 | {
|
---|
1154 | Assert(pImage != pDisk->pLast);
|
---|
1155 |
|
---|
1156 | if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO)
|
---|
1157 | {
|
---|
1158 | /*
|
---|
1159 | * if src block is zero, set dst block to zero too.
|
---|
1160 | */
|
---|
1161 | pDisk->pLast->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
|
---|
1162 | return VINF_SUCCESS;
|
---|
1163 | }
|
---|
1164 |
|
---|
1165 | /* alloc tmp buffer */
|
---|
1166 | void *pvBuf = RTMemTmpAlloc(pDisk->cbBuf);
|
---|
1167 | if (!pvBuf)
|
---|
1168 | return VERR_NO_MEMORY;
|
---|
1169 |
|
---|
1170 | int rc = VINF_SUCCESS;
|
---|
1171 |
|
---|
1172 | unsigned cbCopy = getImageBlockSize(&pImage->Header);
|
---|
1173 | unsigned offCopy = 0;
|
---|
1174 |
|
---|
1175 | /* do loop, because buffer size may be less then block size */
|
---|
1176 | while (cbCopy > 0)
|
---|
1177 | {
|
---|
1178 | unsigned to_copy = RT_MIN(cbCopy, pDisk->cbBuf);
|
---|
1179 | rc = vdiReadInBlock(pImage, uBlock, offCopy, to_copy, pvBuf);
|
---|
1180 | if (VBOX_FAILURE(rc))
|
---|
1181 | break;
|
---|
1182 |
|
---|
1183 | rc = vdiWriteInBlock(pDisk, pDisk->pLast, uBlock, offCopy, to_copy, pvBuf);
|
---|
1184 | if (VBOX_FAILURE(rc))
|
---|
1185 | break;
|
---|
1186 |
|
---|
1187 | cbCopy -= to_copy;
|
---|
1188 | offCopy += to_copy;
|
---|
1189 | }
|
---|
1190 |
|
---|
1191 | RTMemTmpFree(pvBuf);
|
---|
1192 | return rc;
|
---|
1193 | }
|
---|
1194 |
|
---|
1195 | /**
|
---|
1196 | * Write data to virtual HDD.
|
---|
1197 | *
|
---|
1198 | * @returns VBox status code.
|
---|
1199 | * @param pDisk Pointer to VDI HDD container.
|
---|
1200 | * @param offStart Offset of first writing byte from start of HDD.
|
---|
1201 | * @param pvBuf Pointer to buffer of writing data.
|
---|
1202 | * @param cbToWrite Number of bytes to write.
|
---|
1203 | */
|
---|
1204 | VBOXDDU_DECL(int) VDIDiskWrite(PVDIDISK pDisk, uint64_t offStart, const void *pvBuf, size_t cbToWrite)
|
---|
1205 | {
|
---|
1206 | /* sanity check */
|
---|
1207 | Assert(pDisk);
|
---|
1208 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
1209 |
|
---|
1210 | PVDIIMAGEDESC pImage = pDisk->pLast;
|
---|
1211 | Assert(pImage);
|
---|
1212 |
|
---|
1213 | /* Check params. */
|
---|
1214 | if ( offStart + cbToWrite > getImageDiskSize(&pImage->Header)
|
---|
1215 | || cbToWrite == 0)
|
---|
1216 | {
|
---|
1217 | AssertMsgFailed(("offStart=%llu cbToWrite=%u\n", offStart, cbToWrite));
|
---|
1218 | return VERR_INVALID_PARAMETER;
|
---|
1219 | }
|
---|
1220 |
|
---|
1221 | /* Calculate starting block number and offset inside it. */
|
---|
1222 | unsigned uBlock = (unsigned)(offStart >> pImage->uShiftOffset2Index);
|
---|
1223 | unsigned offWrite = (unsigned)offStart & pImage->uBlockMask;
|
---|
1224 | unsigned cbBlock = getImageBlockSize(&pImage->Header);
|
---|
1225 |
|
---|
1226 | /* loop through blocks */
|
---|
1227 | int rc;
|
---|
1228 | for (;;)
|
---|
1229 | {
|
---|
1230 | unsigned to_write;
|
---|
1231 | if (offWrite + cbToWrite <= cbBlock)
|
---|
1232 | to_write = cbToWrite;
|
---|
1233 | else
|
---|
1234 | to_write = cbBlock - offWrite;
|
---|
1235 |
|
---|
1236 | /* All callers write less than a VDI block right now (assuming
|
---|
1237 | * default VDI block size). So not worth optimizing for the case
|
---|
1238 | * where a full block is overwritten (no copying required).
|
---|
1239 | * Checking whether a block is all zeroes after the write is too
|
---|
1240 | * expensive (would require reading the rest of the block). */
|
---|
1241 |
|
---|
1242 | if (pDisk->cImages > 1)
|
---|
1243 | {
|
---|
1244 | /* Differencing images are used, handle them. */
|
---|
1245 |
|
---|
1246 | /* Search for image with allocated block. */
|
---|
1247 | while (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE)
|
---|
1248 | {
|
---|
1249 | pImage = pImage->pPrev;
|
---|
1250 | if (!pImage)
|
---|
1251 | {
|
---|
1252 | /* Block is not allocated in all images of chain. */
|
---|
1253 | pImage = pDisk->pLast;
|
---|
1254 | break;
|
---|
1255 | }
|
---|
1256 | }
|
---|
1257 |
|
---|
1258 | if (pImage != pDisk->pLast)
|
---|
1259 | {
|
---|
1260 | /* One of parent image has a block data, copy it into last image. */
|
---|
1261 | rc = vdiCopyBlock(pDisk, pImage, uBlock);
|
---|
1262 | if (VBOX_FAILURE(rc))
|
---|
1263 | break;
|
---|
1264 | pImage = pDisk->pLast;
|
---|
1265 | }
|
---|
1266 | }
|
---|
1267 |
|
---|
1268 | /* Actually write the data into block. */
|
---|
1269 | rc = vdiWriteInBlock(pDisk, pImage, uBlock, offWrite, to_write, pvBuf);
|
---|
1270 |
|
---|
1271 | cbToWrite -= to_write;
|
---|
1272 | if ( cbToWrite == 0
|
---|
1273 | || VBOX_FAILURE(rc))
|
---|
1274 | break;
|
---|
1275 |
|
---|
1276 | /* goto next block */
|
---|
1277 | uBlock++;
|
---|
1278 | offWrite = 0;
|
---|
1279 | pvBuf = (char *)pvBuf + to_write;
|
---|
1280 | }
|
---|
1281 |
|
---|
1282 | return rc;
|
---|
1283 | }
|
---|
1284 |
|
---|
1285 | /**
|
---|
1286 | * internal: commit one image to another, no changes to header, just
|
---|
1287 | * plain copy operation. Blocks that are not allocated in the source
|
---|
1288 | * image (i.e. inherited by its parent(s)) are not merged.
|
---|
1289 | *
|
---|
1290 | * @param pImageFrom source image
|
---|
1291 | * @param pImageTo target image (will receive all the modifications)
|
---|
1292 | * @param fParentToChild true if the source image is parent of the target one,
|
---|
1293 | * false of the target image is the parent of the source.
|
---|
1294 | * @param pfnProgress progress callback (NULL if not to be used)
|
---|
1295 | * @param pvUser user argument for the progress callback
|
---|
1296 | *
|
---|
1297 | * @note the target image has to be opened read/write
|
---|
1298 | * @note this method does not check whether merging is possible!
|
---|
1299 | */
|
---|
1300 | static int vdiMergeImages(PVDIIMAGEDESC pImageFrom, PVDIIMAGEDESC pImageTo, bool fParentToChild,
|
---|
1301 | PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
1302 | {
|
---|
1303 | Assert(pImageFrom);
|
---|
1304 | Assert(pImageTo);
|
---|
1305 |
|
---|
1306 | Log(("vdiMergeImages: merging from image \"%s\" to image \"%s\" (fParentToChild=%d)\n",
|
---|
1307 | pImageFrom->szFilename, pImageTo->szFilename, fParentToChild));
|
---|
1308 |
|
---|
1309 | /* alloc tmp buffer */
|
---|
1310 | void *pvBuf = RTMemTmpAlloc(VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
1311 | if (!pvBuf)
|
---|
1312 | return VERR_NO_MEMORY;
|
---|
1313 |
|
---|
1314 | int rc = VINF_SUCCESS;
|
---|
1315 |
|
---|
1316 | if (!fParentToChild)
|
---|
1317 | {
|
---|
1318 | /*
|
---|
1319 | * Commit the child image to the parent image.
|
---|
1320 | * Child is the source (from), parent is the target (to).
|
---|
1321 | */
|
---|
1322 |
|
---|
1323 | unsigned cBlocks = getImageBlocks(&pImageFrom->Header);
|
---|
1324 |
|
---|
1325 | for (unsigned uBlock = 0; uBlock < cBlocks; uBlock++)
|
---|
1326 | {
|
---|
1327 | /* only process blocks that are allocated in the source image */
|
---|
1328 | if (pImageFrom->paBlocks[uBlock] != VDI_IMAGE_BLOCK_FREE)
|
---|
1329 | {
|
---|
1330 | /* Found used block in source image, commit it. */
|
---|
1331 | if ( pImageFrom->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO
|
---|
1332 | && !IS_VDI_IMAGE_BLOCK_ALLOCATED(pImageTo->paBlocks[uBlock]))
|
---|
1333 | {
|
---|
1334 | /* Block is zero in the source image and not allocated in the target image. */
|
---|
1335 | pImageTo->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
|
---|
1336 | vdiSetModifiedFlag(pImageTo);
|
---|
1337 | }
|
---|
1338 | else
|
---|
1339 | {
|
---|
1340 | /* Block is not zero / allocated in source image. */
|
---|
1341 | unsigned cbCommit = getImageBlockSize(&pImageFrom->Header);
|
---|
1342 | unsigned offCommit = 0;
|
---|
1343 |
|
---|
1344 | /* do loop, because buffer size may be less then block size */
|
---|
1345 | while (cbCommit > 0)
|
---|
1346 | {
|
---|
1347 | unsigned cbToCopy = RT_MIN(cbCommit, VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
1348 |
|
---|
1349 | rc = vdiReadInBlock(pImageFrom, uBlock, offCommit, cbToCopy, pvBuf);
|
---|
1350 | if (VBOX_FAILURE(rc))
|
---|
1351 | break;
|
---|
1352 |
|
---|
1353 | rc = vdiWriteInBlock(NULL, pImageTo, uBlock, offCommit, cbToCopy, pvBuf);
|
---|
1354 | if (VBOX_FAILURE(rc))
|
---|
1355 | break;
|
---|
1356 |
|
---|
1357 | cbCommit -= cbToCopy;
|
---|
1358 | offCommit += cbToCopy;
|
---|
1359 | }
|
---|
1360 | if (VBOX_FAILURE(rc))
|
---|
1361 | break;
|
---|
1362 | }
|
---|
1363 | }
|
---|
1364 |
|
---|
1365 | if (pfnProgress)
|
---|
1366 | {
|
---|
1367 | pfnProgress(NULL /* WARNING! pVM=NULL */,
|
---|
1368 | (uBlock * 100) / cBlocks,
|
---|
1369 | pvUser);
|
---|
1370 | /* Note: commiting is non breakable operation, skipping rc here. */
|
---|
1371 | }
|
---|
1372 | }
|
---|
1373 | }
|
---|
1374 | else
|
---|
1375 | {
|
---|
1376 | /*
|
---|
1377 | * Commit the parent image to the child image.
|
---|
1378 | * Parent is the source (from), child is the target (to).
|
---|
1379 | */
|
---|
1380 |
|
---|
1381 | unsigned cBlocks = getImageBlocks(&pImageFrom->Header);
|
---|
1382 |
|
---|
1383 | for (unsigned uBlock = 0; uBlock < cBlocks; uBlock++)
|
---|
1384 | {
|
---|
1385 | /*
|
---|
1386 | * only process blocks that are allocated or zero in the source image
|
---|
1387 | * and NEITHER allocated NOR zero in the target image
|
---|
1388 | */
|
---|
1389 | if (pImageFrom->paBlocks[uBlock] != VDI_IMAGE_BLOCK_FREE &&
|
---|
1390 | pImageTo->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE)
|
---|
1391 | {
|
---|
1392 | /* Found used block in source image (but unused in target), commit it. */
|
---|
1393 | if ( pImageFrom->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO)
|
---|
1394 | {
|
---|
1395 | /* Block is zero in the source image and not allocated in the target image. */
|
---|
1396 | pImageTo->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
|
---|
1397 | vdiSetModifiedFlag(pImageTo);
|
---|
1398 | }
|
---|
1399 | else
|
---|
1400 | {
|
---|
1401 | /* Block is not zero / allocated in source image. */
|
---|
1402 | unsigned cbCommit = getImageBlockSize(&pImageFrom->Header);
|
---|
1403 | unsigned offCommit = 0;
|
---|
1404 |
|
---|
1405 | /* do loop, because buffer size may be less then block size */
|
---|
1406 | while (cbCommit > 0)
|
---|
1407 | {
|
---|
1408 | unsigned cbToCopy = RT_MIN(cbCommit, VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
1409 |
|
---|
1410 | rc = vdiReadInBlock(pImageFrom, uBlock, offCommit, cbToCopy, pvBuf);
|
---|
1411 | if (VBOX_FAILURE(rc))
|
---|
1412 | break;
|
---|
1413 |
|
---|
1414 | rc = vdiWriteInBlock(NULL, pImageTo, uBlock, offCommit, cbToCopy, pvBuf);
|
---|
1415 | if (VBOX_FAILURE(rc))
|
---|
1416 | break;
|
---|
1417 |
|
---|
1418 | cbCommit -= cbToCopy;
|
---|
1419 | offCommit += cbToCopy;
|
---|
1420 | }
|
---|
1421 | if (VBOX_FAILURE(rc))
|
---|
1422 | break;
|
---|
1423 | }
|
---|
1424 | }
|
---|
1425 |
|
---|
1426 | if (pfnProgress)
|
---|
1427 | {
|
---|
1428 | pfnProgress(NULL /* WARNING! pVM=NULL */,
|
---|
1429 | (uBlock * 100) / cBlocks,
|
---|
1430 | pvUser);
|
---|
1431 | /* Note: commiting is non breakable operation, skipping rc here. */
|
---|
1432 | }
|
---|
1433 | }
|
---|
1434 | }
|
---|
1435 |
|
---|
1436 | RTMemTmpFree(pvBuf);
|
---|
1437 | return rc;
|
---|
1438 | }
|
---|
1439 |
|
---|
1440 | /**
|
---|
1441 | * internal: commit last image(s) to selected previous image.
|
---|
1442 | * note: all images accessed across this call must be opened in R/W mode.
|
---|
1443 | * @remark Only used by tstVDI.
|
---|
1444 | */
|
---|
1445 | static int vdiCommitToImage(PVDIDISK pDisk, PVDIIMAGEDESC pDstImage,
|
---|
1446 | PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
1447 | {
|
---|
1448 | /* sanity check */
|
---|
1449 | Assert(pDisk);
|
---|
1450 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
1451 | Assert(pDstImage);
|
---|
1452 |
|
---|
1453 | PVDIIMAGEDESC pImage = pDisk->pLast;
|
---|
1454 | Assert(pImage);
|
---|
1455 | Log(("vdiCommitToImage: commiting from image \"%s\" to image \"%s\"\n",
|
---|
1456 | pImage->szFilename, pDstImage->szFilename));
|
---|
1457 | if (pDstImage == pImage)
|
---|
1458 | {
|
---|
1459 | Log(("vdiCommitToImage: attempt to commit to the same image!\n"));
|
---|
1460 | return VERR_VDI_NO_DIFF_IMAGES;
|
---|
1461 | }
|
---|
1462 |
|
---|
1463 | /* Scan images for pDstImage. */
|
---|
1464 | while (pImage && pImage != pDstImage)
|
---|
1465 | pImage = pImage->pPrev;
|
---|
1466 | if (!pImage)
|
---|
1467 | {
|
---|
1468 | AssertMsgFailed(("Invalid arguments: pDstImage is not in images chain\n"));
|
---|
1469 | return VERR_INVALID_PARAMETER;
|
---|
1470 | }
|
---|
1471 | pImage = pDisk->pLast;
|
---|
1472 |
|
---|
1473 | /* alloc tmp buffer */
|
---|
1474 | void *pvBuf = RTMemTmpAlloc(pDisk->cbBuf);
|
---|
1475 | if (!pvBuf)
|
---|
1476 | return VERR_NO_MEMORY;
|
---|
1477 |
|
---|
1478 | int rc = VINF_SUCCESS;
|
---|
1479 | unsigned cBlocks = getImageBlocks(&pImage->Header);
|
---|
1480 |
|
---|
1481 | for (unsigned uBlock = 0; uBlock < cBlocks; uBlock++)
|
---|
1482 | {
|
---|
1483 | pImage = pDisk->pLast;
|
---|
1484 |
|
---|
1485 | /* Find allocated block to commit. */
|
---|
1486 | while ( pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE
|
---|
1487 | && pImage != pDstImage)
|
---|
1488 | pImage = pImage->pPrev;
|
---|
1489 |
|
---|
1490 | if (pImage != pDstImage)
|
---|
1491 | {
|
---|
1492 | /* Found used block in diff image (pImage), commit it. */
|
---|
1493 | if ( pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO
|
---|
1494 | && !IS_VDI_IMAGE_BLOCK_ALLOCATED(pDstImage->paBlocks[uBlock]))
|
---|
1495 | {
|
---|
1496 | /* Block is zero in difference image and not allocated in primary image. */
|
---|
1497 | pDstImage->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
|
---|
1498 | vdiSetModifiedFlag(pDstImage);
|
---|
1499 | }
|
---|
1500 | else
|
---|
1501 | {
|
---|
1502 | /* Block is not zero / allocated in primary image. */
|
---|
1503 | unsigned cbCommit = getImageBlockSize(&pImage->Header);
|
---|
1504 | unsigned offCommit = 0;
|
---|
1505 |
|
---|
1506 | /* do loop, because buffer size may be less then block size */
|
---|
1507 | while (cbCommit > 0)
|
---|
1508 | {
|
---|
1509 | unsigned cbToCopy = RT_MIN(cbCommit, pDisk->cbBuf);
|
---|
1510 |
|
---|
1511 | rc = vdiReadInBlock(pImage, uBlock, offCommit, cbToCopy, pvBuf);
|
---|
1512 | if (VBOX_FAILURE(rc))
|
---|
1513 | break;
|
---|
1514 |
|
---|
1515 | rc = vdiWriteInBlock(pDisk, pDstImage, uBlock, offCommit, cbToCopy, pvBuf);
|
---|
1516 | if (VBOX_FAILURE(rc))
|
---|
1517 | break;
|
---|
1518 |
|
---|
1519 | cbCommit -= cbToCopy;
|
---|
1520 | offCommit += cbToCopy;
|
---|
1521 | }
|
---|
1522 | if (VBOX_FAILURE(rc))
|
---|
1523 | break;
|
---|
1524 | }
|
---|
1525 | pImage->paBlocks[uBlock] = VDI_IMAGE_BLOCK_FREE;
|
---|
1526 | }
|
---|
1527 |
|
---|
1528 | if (pfnProgress)
|
---|
1529 | {
|
---|
1530 | pfnProgress(NULL /* WARNING! pVM=NULL */,
|
---|
1531 | (uBlock * 100) / cBlocks,
|
---|
1532 | pvUser);
|
---|
1533 | /* Note: commiting is non breakable operation, skipping rc here. */
|
---|
1534 | }
|
---|
1535 | }
|
---|
1536 |
|
---|
1537 | RTMemTmpFree(pvBuf);
|
---|
1538 |
|
---|
1539 | /* Go forward and update linkage information. */
|
---|
1540 | for (pImage = pDstImage; pImage; pImage = pImage->pNext)
|
---|
1541 | {
|
---|
1542 | /* generate new last-modified uuid. */
|
---|
1543 | RTUuidCreate(getImageModificationUUID(&pImage->Header));
|
---|
1544 |
|
---|
1545 | /* fix up linkage. */
|
---|
1546 | if (pImage != pDstImage)
|
---|
1547 | *getImageParentModificationUUID(&pImage->Header) = *getImageModificationUUID(&pImage->pPrev->Header);
|
---|
1548 |
|
---|
1549 | /* reset modified flag. */
|
---|
1550 | pImage->fModified = 0;
|
---|
1551 | }
|
---|
1552 |
|
---|
1553 | /* Process committed images - truncate them. */
|
---|
1554 | for (pImage = pDisk->pLast; pImage != pDstImage; pImage = pImage->pPrev)
|
---|
1555 | {
|
---|
1556 | /* note: can't understand how to do error works here? */
|
---|
1557 |
|
---|
1558 | setImageBlocksAllocated(&pImage->Header, 0);
|
---|
1559 |
|
---|
1560 | /* Truncate file. */
|
---|
1561 | int rc2 = RTFileSetSize(pImage->File, pImage->offStartData);
|
---|
1562 | if (VBOX_FAILURE(rc2))
|
---|
1563 | {
|
---|
1564 | rc = rc2;
|
---|
1565 | Log(("vdiCommitToImage: set size (truncate) rc=%Vrc filename=\"%s\"\n",
|
---|
1566 | rc, pImage->szFilename));
|
---|
1567 | }
|
---|
1568 |
|
---|
1569 | /* Save header and blocks array. */
|
---|
1570 | rc2 = vdiUpdateBlocks(pImage);
|
---|
1571 | if (VBOX_FAILURE(rc2))
|
---|
1572 | {
|
---|
1573 | rc = rc2;
|
---|
1574 | Log(("vdiCommitToImage: update blocks and header rc=%Vrc filename=\"%s\"\n",
|
---|
1575 | rc, pImage->szFilename));
|
---|
1576 | }
|
---|
1577 | }
|
---|
1578 |
|
---|
1579 | if (pfnProgress)
|
---|
1580 | {
|
---|
1581 | pfnProgress(NULL /* WARNING! pVM=NULL */, 100, pvUser);
|
---|
1582 | /* Note: commiting is non breakable operation, skipping rc here. */
|
---|
1583 | }
|
---|
1584 |
|
---|
1585 | Log(("vdiCommitToImage: done, rc=%Vrc\n", rc));
|
---|
1586 |
|
---|
1587 | return rc;
|
---|
1588 | }
|
---|
1589 |
|
---|
1590 | /**
|
---|
1591 | * Checks if image is available and not broken, returns some useful image parameters if requested.
|
---|
1592 | *
|
---|
1593 | * @returns VBox status code.
|
---|
1594 | * @param pszFilename Name of the image file to check.
|
---|
1595 | * @param puVersion Where to store the version of image. NULL is ok.
|
---|
1596 | * @param penmType Where to store the type of image. NULL is ok.
|
---|
1597 | * @param pcbSize Where to store the size of image in bytes. NULL is ok.
|
---|
1598 | * @param pUuid Where to store the uuid of image creation. NULL is ok.
|
---|
1599 | * @param pParentUuid Where to store the UUID of the parent image. NULL is ok.
|
---|
1600 | * @param pszComment Where to store the comment string of image. NULL is ok.
|
---|
1601 | * @param cbComment The size of pszComment buffer. 0 is ok.
|
---|
1602 | */
|
---|
1603 | VBOXDDU_DECL(int) VDICheckImage(const char *pszFilename, unsigned *puVersion, PVDIIMAGETYPE penmType,
|
---|
1604 | uint64_t *pcbSize, PRTUUID pUuid, PRTUUID pParentUuid,
|
---|
1605 | char *pszComment, unsigned cbComment)
|
---|
1606 | {
|
---|
1607 | LogFlow(("VDICheckImage:\n"));
|
---|
1608 |
|
---|
1609 | /* Check arguments. */
|
---|
1610 | if ( !pszFilename
|
---|
1611 | || *pszFilename == '\0')
|
---|
1612 | {
|
---|
1613 | AssertMsgFailed(("Invalid arguments: pszFilename=%p\n", pszFilename));
|
---|
1614 | return VERR_INVALID_PARAMETER;
|
---|
1615 | }
|
---|
1616 |
|
---|
1617 | PVDIIMAGEDESC pImage;
|
---|
1618 | int rc = vdiOpenImage(&pImage, pszFilename, VDI_OPEN_FLAGS_READONLY, NULL);
|
---|
1619 | if (VBOX_SUCCESS(rc))
|
---|
1620 | {
|
---|
1621 | Log(("VDICheckImage: filename=\"%s\" version=%08X type=%X cbDisk=%llu uuid={%Vuuid}\n",
|
---|
1622 | pszFilename,
|
---|
1623 | pImage->PreHeader.u32Version,
|
---|
1624 | getImageType(&pImage->Header),
|
---|
1625 | getImageDiskSize(&pImage->Header),
|
---|
1626 | getImageCreationUUID(&pImage->Header)));
|
---|
1627 |
|
---|
1628 | if ( pszComment
|
---|
1629 | && cbComment > 0)
|
---|
1630 | {
|
---|
1631 | char *pszTmp = getImageComment(&pImage->Header);
|
---|
1632 | unsigned cb = strlen(pszTmp);
|
---|
1633 | if (cbComment > cb)
|
---|
1634 | memcpy(pszComment, pszTmp, cb + 1);
|
---|
1635 | else
|
---|
1636 | rc = VERR_BUFFER_OVERFLOW;
|
---|
1637 | }
|
---|
1638 | if (VBOX_SUCCESS(rc))
|
---|
1639 | {
|
---|
1640 | if (puVersion)
|
---|
1641 | *puVersion = pImage->PreHeader.u32Version;
|
---|
1642 | if (penmType)
|
---|
1643 | *penmType = getImageType(&pImage->Header);
|
---|
1644 | if (pcbSize)
|
---|
1645 | *pcbSize = getImageDiskSize(&pImage->Header);
|
---|
1646 | if (pUuid)
|
---|
1647 | *pUuid = *getImageCreationUUID(&pImage->Header);
|
---|
1648 | if (pParentUuid)
|
---|
1649 | *pParentUuid = *getImageParentUUID(&pImage->Header);
|
---|
1650 | }
|
---|
1651 | vdiCloseImage(pImage);
|
---|
1652 | }
|
---|
1653 |
|
---|
1654 | LogFlow(("VDICheckImage: returns %Vrc\n", rc));
|
---|
1655 | return rc;
|
---|
1656 | }
|
---|
1657 |
|
---|
1658 | /**
|
---|
1659 | * Changes an image's comment string.
|
---|
1660 | *
|
---|
1661 | * @returns VBox status code.
|
---|
1662 | * @param pszFilename Name of the image file to operate on.
|
---|
1663 | * @param pszComment New comment string (UTF-8). NULL is allowed to reset the comment.
|
---|
1664 | */
|
---|
1665 | VBOXDDU_DECL(int) VDISetImageComment(const char *pszFilename, const char *pszComment)
|
---|
1666 | {
|
---|
1667 | LogFlow(("VDISetImageComment:\n"));
|
---|
1668 |
|
---|
1669 | /*
|
---|
1670 | * Validate arguments.
|
---|
1671 | */
|
---|
1672 | if ( !pszFilename
|
---|
1673 | || *pszFilename == '\0')
|
---|
1674 | {
|
---|
1675 | AssertMsgFailed(("Invalid arguments: pszFilename=%p\n", pszFilename));
|
---|
1676 | return VERR_INVALID_PARAMETER;
|
---|
1677 | }
|
---|
1678 |
|
---|
1679 | const size_t cchComment = pszComment ? strlen(pszComment) : 0;
|
---|
1680 | if (cchComment >= VDI_IMAGE_COMMENT_SIZE)
|
---|
1681 | {
|
---|
1682 | Log(("VDISetImageComment: pszComment is too long, %d bytes!\n", cchComment));
|
---|
1683 | return VERR_VDI_COMMENT_TOO_LONG;
|
---|
1684 | }
|
---|
1685 |
|
---|
1686 | /*
|
---|
1687 | * Open the image for updating.
|
---|
1688 | */
|
---|
1689 | PVDIIMAGEDESC pImage;
|
---|
1690 | int rc = vdiOpenImage(&pImage, pszFilename, VDI_OPEN_FLAGS_NORMAL, NULL);
|
---|
1691 | if (VBOX_FAILURE(rc))
|
---|
1692 | {
|
---|
1693 | Log(("VDISetImageComment: vdiOpenImage rc=%Vrc filename=\"%s\"!\n", rc, pszFilename));
|
---|
1694 | return rc;
|
---|
1695 | }
|
---|
1696 | if (!pImage->fReadOnly)
|
---|
1697 | {
|
---|
1698 | /* we don't support old style images */
|
---|
1699 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
1700 | {
|
---|
1701 | /*
|
---|
1702 | * Update the comment field, making sure to zero out all of the previous comment.
|
---|
1703 | */
|
---|
1704 | memset(pImage->Header.u.v1.szComment, '\0', VDI_IMAGE_COMMENT_SIZE);
|
---|
1705 | memcpy(pImage->Header.u.v1.szComment, pszComment, cchComment);
|
---|
1706 |
|
---|
1707 | /* write out new the header */
|
---|
1708 | rc = vdiUpdateHeader(pImage);
|
---|
1709 | AssertMsgRC(rc, ("vdiUpdateHeader() failed, filename=\"%s\", rc=%Vrc\n",
|
---|
1710 | pImage->szFilename, rc));
|
---|
1711 | }
|
---|
1712 | else
|
---|
1713 | {
|
---|
1714 | Log(("VDISetImageComment: Unsupported version!\n"));
|
---|
1715 | rc = VERR_VDI_UNSUPPORTED_VERSION;
|
---|
1716 | }
|
---|
1717 | }
|
---|
1718 | else
|
---|
1719 | {
|
---|
1720 | Log(("VDISetImageComment: image \"%s\" is opened as read-only!\n", pszFilename));
|
---|
1721 | rc = VERR_VDI_IMAGE_READ_ONLY;
|
---|
1722 | }
|
---|
1723 |
|
---|
1724 | vdiCloseImage(pImage);
|
---|
1725 | return rc;
|
---|
1726 | }
|
---|
1727 |
|
---|
1728 | /**
|
---|
1729 | * Creates a new base image file.
|
---|
1730 | *
|
---|
1731 | * @returns VBox status code.
|
---|
1732 | * @param pszFilename Name of the creating image file.
|
---|
1733 | * @param enmType Image type, only base image types are acceptable.
|
---|
1734 | * @param cbSize Image size in bytes.
|
---|
1735 | * @param pszComment Pointer to image comment. NULL is ok.
|
---|
1736 | * @param pfnProgress Progress callback. Optional.
|
---|
1737 | * @param pvUser User argument for the progress callback.
|
---|
1738 | */
|
---|
1739 | VBOXDDU_DECL(int) VDICreateBaseImage(const char *pszFilename, VDIIMAGETYPE enmType, uint64_t cbSize,
|
---|
1740 | const char *pszComment, PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
1741 | {
|
---|
1742 | LogFlow(("VDICreateBaseImage:\n"));
|
---|
1743 |
|
---|
1744 | /* Check arguments. */
|
---|
1745 | if ( !pszFilename
|
---|
1746 | || *pszFilename == '\0'
|
---|
1747 | || (enmType != VDI_IMAGE_TYPE_NORMAL && enmType != VDI_IMAGE_TYPE_FIXED)
|
---|
1748 | || cbSize < VDI_IMAGE_DEFAULT_BLOCK_SIZE)
|
---|
1749 | {
|
---|
1750 | AssertMsgFailed(("Invalid arguments: pszFilename=%p enmType=%x cbSize=%llu\n",
|
---|
1751 | pszFilename, enmType, cbSize));
|
---|
1752 | return VERR_INVALID_PARAMETER;
|
---|
1753 | }
|
---|
1754 |
|
---|
1755 | int rc = vdiCreateImage(pszFilename, enmType, VDI_IMAGE_FLAGS_DEFAULT, cbSize, pszComment, NULL,
|
---|
1756 | pfnProgress, pvUser);
|
---|
1757 | LogFlow(("VDICreateBaseImage: returns %Vrc for filename=\"%s\"\n", rc, pszFilename));
|
---|
1758 | return rc;
|
---|
1759 | }
|
---|
1760 |
|
---|
1761 | /**
|
---|
1762 | * Creates a differencing dynamically growing image file for specified parent image.
|
---|
1763 | *
|
---|
1764 | * @returns VBox status code.
|
---|
1765 | * @param pszFilename Name of the creating differencing image file.
|
---|
1766 | * @param pszParent Name of the parent image file. May be base or diff image type.
|
---|
1767 | * @param pszComment Pointer to image comment. NULL is ok.
|
---|
1768 | * @param pfnProgress Progress callback. Optional.
|
---|
1769 | * @param pvUser User argument for the progress callback.
|
---|
1770 | */
|
---|
1771 | VBOXDDU_DECL(int) VDICreateDifferenceImage(const char *pszFilename, const char *pszParent,
|
---|
1772 | const char *pszComment, PFNVMPROGRESS pfnProgress,
|
---|
1773 | void *pvUser)
|
---|
1774 | {
|
---|
1775 | LogFlow(("VDICreateDifferenceImage:\n"));
|
---|
1776 |
|
---|
1777 | /* Check arguments. */
|
---|
1778 | if ( !pszFilename
|
---|
1779 | || *pszFilename == '\0'
|
---|
1780 | || !pszParent
|
---|
1781 | || *pszParent == '\0')
|
---|
1782 | {
|
---|
1783 | AssertMsgFailed(("Invalid arguments: pszFilename=%p pszParent=%p\n",
|
---|
1784 | pszFilename, pszParent));
|
---|
1785 | return VERR_INVALID_PARAMETER;
|
---|
1786 | }
|
---|
1787 |
|
---|
1788 | PVDIIMAGEDESC pParent;
|
---|
1789 | int rc = vdiOpenImage(&pParent, pszParent, VDI_OPEN_FLAGS_READONLY, NULL);
|
---|
1790 | if (VBOX_SUCCESS(rc))
|
---|
1791 | {
|
---|
1792 | rc = vdiCreateImage(pszFilename, VDI_IMAGE_TYPE_DIFF, VDI_IMAGE_FLAGS_DEFAULT,
|
---|
1793 | getImageDiskSize(&pParent->Header), pszComment, pParent,
|
---|
1794 | pfnProgress, pvUser);
|
---|
1795 | vdiCloseImage(pParent);
|
---|
1796 | }
|
---|
1797 | LogFlow(("VDICreateDifferenceImage: returns %Vrc for filename=\"%s\"\n", rc, pszFilename));
|
---|
1798 | return rc;
|
---|
1799 | }
|
---|
1800 |
|
---|
1801 | /**
|
---|
1802 | * Deletes an image. Only valid image files can be deleted by this call.
|
---|
1803 | *
|
---|
1804 | * @returns VBox status code.
|
---|
1805 | * @param pszFilename Name of the image file to check.
|
---|
1806 | */
|
---|
1807 | VBOXDDU_DECL(int) VDIDeleteImage(const char *pszFilename)
|
---|
1808 | {
|
---|
1809 | LogFlow(("VDIDeleteImage:\n"));
|
---|
1810 | /* Check arguments. */
|
---|
1811 | if ( !pszFilename
|
---|
1812 | || *pszFilename == '\0')
|
---|
1813 | {
|
---|
1814 | AssertMsgFailed(("Invalid arguments: pszFilename=%p\n", pszFilename));
|
---|
1815 | return VERR_INVALID_PARAMETER;
|
---|
1816 | }
|
---|
1817 |
|
---|
1818 | int rc = VDICheckImage(pszFilename, NULL, NULL, NULL, NULL, NULL, NULL, 0);
|
---|
1819 | if (VBOX_SUCCESS(rc))
|
---|
1820 | rc = RTFileDelete(pszFilename);
|
---|
1821 |
|
---|
1822 | LogFlow(("VDIDeleteImage: returns %Vrc for filename=\"%s\"\n", rc, pszFilename));
|
---|
1823 | return rc;
|
---|
1824 | }
|
---|
1825 |
|
---|
1826 | /**
|
---|
1827 | * Makes a copy of image file with a new (other) creation uuid.
|
---|
1828 | *
|
---|
1829 | * @returns VBox status code.
|
---|
1830 | * @param pszDstFilename Name of the image file to create.
|
---|
1831 | * @param pszSrcFilename Name of the image file to copy from.
|
---|
1832 | * @param pszComment Pointer to image comment. If NULL specified comment
|
---|
1833 | * will be copied from source image.
|
---|
1834 | * @param pfnProgress Progress callback. Optional.
|
---|
1835 | * @param pvUser User argument for the progress callback.
|
---|
1836 | */
|
---|
1837 | VBOXDDU_DECL(int) VDICopyImage(const char *pszDstFilename, const char *pszSrcFilename,
|
---|
1838 | const char *pszComment, PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
1839 | {
|
---|
1840 | LogFlow(("VDICopyImage:\n"));
|
---|
1841 |
|
---|
1842 | /* Check arguments. */
|
---|
1843 | if ( !pszDstFilename
|
---|
1844 | || *pszDstFilename == '\0'
|
---|
1845 | || !pszSrcFilename
|
---|
1846 | || *pszSrcFilename == '\0')
|
---|
1847 | {
|
---|
1848 | AssertMsgFailed(("Invalid arguments: pszDstFilename=%p pszSrcFilename=%p\n",
|
---|
1849 | pszDstFilename, pszSrcFilename));
|
---|
1850 | return VERR_INVALID_PARAMETER;
|
---|
1851 | }
|
---|
1852 |
|
---|
1853 | /* Special check for comment length. */
|
---|
1854 | if ( pszComment
|
---|
1855 | && strlen(pszComment) >= VDI_IMAGE_COMMENT_SIZE)
|
---|
1856 | {
|
---|
1857 | Log(("VDICopyImage: pszComment is too long, cb=%d\n", strlen(pszComment)));
|
---|
1858 | return VERR_VDI_COMMENT_TOO_LONG;
|
---|
1859 | }
|
---|
1860 |
|
---|
1861 | PVDIIMAGEDESC pImage;
|
---|
1862 | int rc = vdiOpenImage(&pImage, pszSrcFilename, VDI_OPEN_FLAGS_READONLY, NULL);
|
---|
1863 | if (VBOX_FAILURE(rc))
|
---|
1864 | {
|
---|
1865 | Log(("VDICopyImage: src image \"%s\" open failed rc=%Vrc\n", pszSrcFilename, rc));
|
---|
1866 | return rc;
|
---|
1867 | }
|
---|
1868 |
|
---|
1869 | uint64_t cbFile = pImage->offStartData
|
---|
1870 | + ((uint64_t)getImageBlocksAllocated(&pImage->Header) << pImage->uShiftIndex2Offset);
|
---|
1871 |
|
---|
1872 | /* create file */
|
---|
1873 | RTFILE File;
|
---|
1874 | rc = RTFileOpen(&File,
|
---|
1875 | pszDstFilename,
|
---|
1876 | RTFILE_O_READWRITE | RTFILE_O_CREATE | RTFILE_O_DENY_ALL);
|
---|
1877 | if (VBOX_SUCCESS(rc))
|
---|
1878 | {
|
---|
1879 | /* lock new image exclusively to close any wrong access by VDI API calls. */
|
---|
1880 | rc = RTFileLock(File, RTFILE_LOCK_WRITE | RTFILE_LOCK_IMMEDIATELY, 0, cbFile);
|
---|
1881 | if (VBOX_SUCCESS(rc))
|
---|
1882 | {
|
---|
1883 | /* Set the size of a new file. */
|
---|
1884 | rc = RTFileSetSize(File, cbFile);
|
---|
1885 | if (VBOX_SUCCESS(rc))
|
---|
1886 | {
|
---|
1887 | /* A dirty trick - use original image data to fill the new image. */
|
---|
1888 | RTFILE oldFileHandle = pImage->File;
|
---|
1889 | pImage->File = File;
|
---|
1890 | pImage->fReadOnly = false;
|
---|
1891 |
|
---|
1892 | /* generate a new image creation uuid. */
|
---|
1893 | RTUuidCreate(getImageCreationUUID(&pImage->Header));
|
---|
1894 | /* generate a new image last-modified uuid. */
|
---|
1895 | RTUuidCreate(getImageModificationUUID(&pImage->Header));
|
---|
1896 | /* set image comment, if present. */
|
---|
1897 | if (pszComment)
|
---|
1898 | strncpy(getImageComment(&pImage->Header), pszComment, VDI_IMAGE_COMMENT_SIZE);
|
---|
1899 |
|
---|
1900 | /* Write the pre-header to new image. */
|
---|
1901 | rc = RTFileSeek(pImage->File, 0, RTFILE_SEEK_BEGIN, NULL);
|
---|
1902 | if (VBOX_SUCCESS(rc))
|
---|
1903 | rc = RTFileWrite(pImage->File,
|
---|
1904 | &pImage->PreHeader,
|
---|
1905 | sizeof(pImage->PreHeader),
|
---|
1906 | NULL);
|
---|
1907 |
|
---|
1908 | /* Write the header and the blocks array to new image. */
|
---|
1909 | if (VBOX_SUCCESS(rc))
|
---|
1910 | rc = vdiUpdateBlocks(pImage);
|
---|
1911 |
|
---|
1912 | pImage->File = oldFileHandle;
|
---|
1913 | pImage->fReadOnly = true;
|
---|
1914 |
|
---|
1915 | /* Seek to the data start in both images. */
|
---|
1916 | if (VBOX_SUCCESS(rc))
|
---|
1917 | rc = RTFileSeek(pImage->File,
|
---|
1918 | pImage->offStartData,
|
---|
1919 | RTFILE_SEEK_BEGIN,
|
---|
1920 | NULL);
|
---|
1921 | if (VBOX_SUCCESS(rc))
|
---|
1922 | rc = RTFileSeek(File,
|
---|
1923 | pImage->offStartData,
|
---|
1924 | RTFILE_SEEK_BEGIN,
|
---|
1925 | NULL);
|
---|
1926 |
|
---|
1927 | if (VBOX_SUCCESS(rc))
|
---|
1928 | {
|
---|
1929 | /* alloc tmp buffer */
|
---|
1930 | void *pvBuf = RTMemTmpAlloc(VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
1931 | if (pvBuf)
|
---|
1932 | {
|
---|
1933 | /* Main copy loop. */
|
---|
1934 | uint64_t cbData = cbFile - pImage->offStartData;
|
---|
1935 | unsigned cBlocks = (unsigned)(cbData / VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
1936 | unsigned c = 0;
|
---|
1937 |
|
---|
1938 | while (cbData)
|
---|
1939 | {
|
---|
1940 | unsigned cbToCopy = (unsigned)RT_MIN(cbData, VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
1941 |
|
---|
1942 | /* Read. */
|
---|
1943 | rc = RTFileRead(pImage->File, pvBuf, cbToCopy, NULL);
|
---|
1944 | if (VBOX_FAILURE(rc))
|
---|
1945 | break;
|
---|
1946 |
|
---|
1947 | /* Write. */
|
---|
1948 | rc = RTFileWrite(File, pvBuf, cbToCopy, NULL);
|
---|
1949 | if (VBOX_FAILURE(rc))
|
---|
1950 | break;
|
---|
1951 |
|
---|
1952 | if (pfnProgress)
|
---|
1953 | {
|
---|
1954 | c++;
|
---|
1955 | rc = pfnProgress(NULL /* WARNING! pVM=NULL */,
|
---|
1956 | (c * 100) / cBlocks,
|
---|
1957 | pvUser);
|
---|
1958 | if (VBOX_FAILURE(rc))
|
---|
1959 | break;
|
---|
1960 | }
|
---|
1961 | cbData -= cbToCopy;
|
---|
1962 | }
|
---|
1963 |
|
---|
1964 | RTMemTmpFree(pvBuf);
|
---|
1965 | }
|
---|
1966 | else
|
---|
1967 | rc = VERR_NO_MEMORY;
|
---|
1968 | }
|
---|
1969 | }
|
---|
1970 |
|
---|
1971 | RTFileUnlock(File, 0, cbFile);
|
---|
1972 | }
|
---|
1973 |
|
---|
1974 | RTFileClose(File);
|
---|
1975 |
|
---|
1976 | if (VBOX_FAILURE(rc))
|
---|
1977 | RTFileDelete(pszDstFilename);
|
---|
1978 |
|
---|
1979 | if (pfnProgress)
|
---|
1980 | pfnProgress(NULL /* WARNING! pVM=NULL */, 100, pvUser);
|
---|
1981 | }
|
---|
1982 |
|
---|
1983 | vdiCloseImage(pImage);
|
---|
1984 |
|
---|
1985 | LogFlow(("VDICopyImage: returns %Vrc for pszSrcFilename=\"%s\" pszDstFilename=\"%s\"\n",
|
---|
1986 | rc, pszSrcFilename, pszDstFilename));
|
---|
1987 | return rc;
|
---|
1988 | }
|
---|
1989 |
|
---|
1990 | /**
|
---|
1991 | * Shrinks growing image file by removing zeroed data blocks.
|
---|
1992 | *
|
---|
1993 | * @returns VBox status code.
|
---|
1994 | * @param pszFilename Name of the image file to shrink.
|
---|
1995 | * @param pfnProgress Progress callback. Optional.
|
---|
1996 | * @param pvUser User argument for the progress callback.
|
---|
1997 | */
|
---|
1998 | VBOXDDU_DECL(int) VDIShrinkImage(const char *pszFilename, PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
1999 | {
|
---|
2000 | LogFlow(("VDIShrinkImage:\n"));
|
---|
2001 |
|
---|
2002 | /* Check arguments. */
|
---|
2003 | if ( !pszFilename
|
---|
2004 | || *pszFilename == '\0')
|
---|
2005 | {
|
---|
2006 | AssertMsgFailed(("Invalid arguments: pszFilename=%p\n", pszFilename));
|
---|
2007 | return VERR_INVALID_PARAMETER;
|
---|
2008 | }
|
---|
2009 |
|
---|
2010 | PVDIIMAGEDESC pImage;
|
---|
2011 | int rc = vdiOpenImage(&pImage, pszFilename, VDI_OPEN_FLAGS_NORMAL, NULL);
|
---|
2012 | if (VBOX_FAILURE(rc))
|
---|
2013 | {
|
---|
2014 | Log(("VDIShrinkImage: vdiOpenImage rc=%Vrc filename=\"%s\"\n", rc, pszFilename));
|
---|
2015 | return rc;
|
---|
2016 | }
|
---|
2017 | if (pImage->fReadOnly)
|
---|
2018 | {
|
---|
2019 | Log(("VDIShrinkImage: image \"%s\" is opened as read-only!\n", pszFilename));
|
---|
2020 | vdiCloseImage(pImage);
|
---|
2021 | return VERR_VDI_IMAGE_READ_ONLY;
|
---|
2022 | }
|
---|
2023 |
|
---|
2024 | /* Do debug dump. */
|
---|
2025 | vdiDumpImage(pImage);
|
---|
2026 |
|
---|
2027 | /* Working data. */
|
---|
2028 | unsigned cbBlock = getImageBlockSize(&pImage->Header);
|
---|
2029 | unsigned cBlocks = getImageBlocks(&pImage->Header);
|
---|
2030 | unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
|
---|
2031 |
|
---|
2032 | uint64_t cbFile;
|
---|
2033 | rc = RTFileGetSize(pImage->File, &cbFile);
|
---|
2034 | if (VBOX_FAILURE(rc))
|
---|
2035 | {
|
---|
2036 | Log(("VDIShrinkImage: RTFileGetSize rc=%Vrc for file=\"%s\"\n", rc, pszFilename));
|
---|
2037 | vdiCloseImage(pImage);
|
---|
2038 | return rc;
|
---|
2039 | }
|
---|
2040 |
|
---|
2041 | uint64_t cbData = cbFile - pImage->offStartData;
|
---|
2042 | unsigned cBlocksAllocated2 = (unsigned)(cbData >> pImage->uShiftIndex2Offset);
|
---|
2043 | if (cbData != (uint64_t)cBlocksAllocated << pImage->uShiftIndex2Offset)
|
---|
2044 | Log(("VDIShrinkImage: invalid image file length, cbBlock=%u cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu\n",
|
---|
2045 | cbBlock, cBlocks, cBlocksAllocated, cBlocksAllocated2, cbData));
|
---|
2046 |
|
---|
2047 | /* Allocate second blocks array for back resolving. */
|
---|
2048 | PVDIIMAGEBLOCKPOINTER paBlocks2 =
|
---|
2049 | (PVDIIMAGEBLOCKPOINTER)RTMemTmpAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * cBlocks);
|
---|
2050 | if (!paBlocks2)
|
---|
2051 | {
|
---|
2052 | Log(("VDIShrinkImage: failed to allocate paBlocks2 buffer (%u bytes)\n", sizeof(VDIIMAGEBLOCKPOINTER) * cBlocks));
|
---|
2053 | vdiCloseImage(pImage);
|
---|
2054 | return VERR_NO_MEMORY;
|
---|
2055 | }
|
---|
2056 |
|
---|
2057 | /* Init second blocks array. */
|
---|
2058 | for (unsigned n = 0; n < cBlocks; n++)
|
---|
2059 | paBlocks2[n] = VDI_IMAGE_BLOCK_FREE;
|
---|
2060 |
|
---|
2061 | /* Fill second blocks array, check for allocational errors. */
|
---|
2062 | for (unsigned n = 0; n < cBlocks; n++)
|
---|
2063 | {
|
---|
2064 | if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[n]))
|
---|
2065 | {
|
---|
2066 | unsigned uBlock = pImage->paBlocks[n];
|
---|
2067 | if (uBlock < cBlocksAllocated2)
|
---|
2068 | {
|
---|
2069 | if (paBlocks2[uBlock] == VDI_IMAGE_BLOCK_FREE)
|
---|
2070 | paBlocks2[uBlock] = n;
|
---|
2071 | else
|
---|
2072 | {
|
---|
2073 | Log(("VDIShrinkImage: block n=%u -> uBlock=%u is already in use!\n", n, uBlock));
|
---|
2074 | /* free second link to block. */
|
---|
2075 | pImage->paBlocks[n] = VDI_IMAGE_BLOCK_FREE;
|
---|
2076 | }
|
---|
2077 | }
|
---|
2078 | else
|
---|
2079 | {
|
---|
2080 | Log(("VDIShrinkImage: block n=%u -> uBlock=%u is out of blocks range! (cbBlock=%u cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu)\n",
|
---|
2081 | n, uBlock, cbBlock, cBlocks, cBlocksAllocated, cBlocksAllocated2, cbData));
|
---|
2082 | /* free link to invalid block. */
|
---|
2083 | pImage->paBlocks[n] = VDI_IMAGE_BLOCK_FREE;
|
---|
2084 | }
|
---|
2085 | }
|
---|
2086 | }
|
---|
2087 |
|
---|
2088 | /* Allocate a working buffer for one block. */
|
---|
2089 | void *pvBuf = RTMemTmpAlloc(cbBlock);
|
---|
2090 | if (pvBuf)
|
---|
2091 | {
|
---|
2092 | /* Main voodoo loop, search holes and fill it. */
|
---|
2093 | unsigned uBlockWrite = 0;
|
---|
2094 | for (unsigned uBlock = 0; uBlock < cBlocksAllocated2; uBlock++)
|
---|
2095 | {
|
---|
2096 | if (paBlocks2[uBlock] != VDI_IMAGE_BLOCK_FREE)
|
---|
2097 | {
|
---|
2098 | /* Read the block from file and check for zeroes. */
|
---|
2099 | uint64_t u64Offset = ((uint64_t)uBlock << pImage->uShiftIndex2Offset)
|
---|
2100 | + (pImage->offStartData + pImage->offStartBlockData);
|
---|
2101 | rc = RTFileSeek(pImage->File, u64Offset, RTFILE_SEEK_BEGIN, NULL);
|
---|
2102 | if (VBOX_FAILURE(rc))
|
---|
2103 | {
|
---|
2104 | Log(("VDIShrinkImage: seek rc=%Vrc filename=\"%s\" uBlock=%u cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu\n",
|
---|
2105 | rc, pImage->szFilename, uBlock, cBlocks, cBlocksAllocated, cBlocksAllocated2, cbData));
|
---|
2106 | break;
|
---|
2107 | }
|
---|
2108 | rc = RTFileRead(pImage->File, pvBuf, cbBlock, NULL);
|
---|
2109 | if (VBOX_FAILURE(rc))
|
---|
2110 | {
|
---|
2111 | Log(("VDIShrinkImage: read rc=%Vrc filename=\"%s\" cbBlock=%u uBlock=%u cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu\n",
|
---|
2112 | rc, pImage->szFilename, cbBlock, uBlock, cBlocks, cBlocksAllocated, cBlocksAllocated2, cbData));
|
---|
2113 | break;
|
---|
2114 | }
|
---|
2115 |
|
---|
2116 | /* Check block for data. */
|
---|
2117 | Assert(cbBlock % 4 == 0);
|
---|
2118 | if (ASMBitFirstSet(pvBuf, cbBlock * 8) != -1)
|
---|
2119 | {
|
---|
2120 | /* Block has a data, may be it must be moved. */
|
---|
2121 | if (uBlockWrite < uBlock)
|
---|
2122 | {
|
---|
2123 | /* Move the block. */
|
---|
2124 | u64Offset = ((uint64_t)uBlockWrite << pImage->uShiftIndex2Offset)
|
---|
2125 | + (pImage->offStartData + pImage->offStartBlockData);
|
---|
2126 | rc = RTFileSeek(pImage->File, u64Offset, RTFILE_SEEK_BEGIN, NULL);
|
---|
2127 | if (VBOX_FAILURE(rc))
|
---|
2128 | {
|
---|
2129 | Log(("VDIShrinkImage: seek(2) rc=%Vrc filename=\"%s\" uBlockWrite=%u cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu\n",
|
---|
2130 | rc, pImage->szFilename, uBlockWrite, cBlocks, cBlocksAllocated, cBlocksAllocated2, cbData));
|
---|
2131 | break;
|
---|
2132 | }
|
---|
2133 | rc = RTFileWrite(pImage->File, pvBuf, cbBlock, NULL);
|
---|
2134 | if (VBOX_FAILURE(rc))
|
---|
2135 | {
|
---|
2136 | Log(("VDIShrinkImage: write rc=%Vrc filename=\"%s\" cbBlock=%u uBlockWrite=%u cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu\n",
|
---|
2137 | rc, pImage->szFilename, cbBlock, uBlockWrite, cBlocks, cBlocksAllocated, cBlocksAllocated2, cbData));
|
---|
2138 | break;
|
---|
2139 | }
|
---|
2140 | }
|
---|
2141 | /* Fix the block pointer. */
|
---|
2142 | pImage->paBlocks[paBlocks2[uBlock]] = uBlockWrite;
|
---|
2143 | uBlockWrite++;
|
---|
2144 | }
|
---|
2145 | else
|
---|
2146 | {
|
---|
2147 | Log(("VDIShrinkImage: found a zeroed block, uBlock=%u\n", uBlock));
|
---|
2148 |
|
---|
2149 | /* Fix the block pointer. */
|
---|
2150 | pImage->paBlocks[paBlocks2[uBlock]] = VDI_IMAGE_BLOCK_ZERO;
|
---|
2151 | }
|
---|
2152 | }
|
---|
2153 | else
|
---|
2154 | Log(("VDIShrinkImage: found an unused block, uBlock=%u\n", uBlock));
|
---|
2155 |
|
---|
2156 | if (pfnProgress)
|
---|
2157 | {
|
---|
2158 | pfnProgress(NULL /* WARNING! pVM=NULL */,
|
---|
2159 | (uBlock * 100) / cBlocksAllocated2,
|
---|
2160 | pvUser);
|
---|
2161 | /* Shrink is unbreakable operation! */
|
---|
2162 | }
|
---|
2163 | }
|
---|
2164 |
|
---|
2165 | RTMemTmpFree(pvBuf);
|
---|
2166 |
|
---|
2167 | if ( VBOX_SUCCESS(rc)
|
---|
2168 | && uBlockWrite < cBlocksAllocated2)
|
---|
2169 | {
|
---|
2170 | /* File size must be shrinked. */
|
---|
2171 | Log(("VDIShrinkImage: shrinking file size from %llu to %llu bytes\n",
|
---|
2172 | cbFile,
|
---|
2173 | pImage->offStartData + ((uint64_t)uBlockWrite << pImage->uShiftIndex2Offset)));
|
---|
2174 | rc = RTFileSetSize(pImage->File,
|
---|
2175 | pImage->offStartData + ((uint64_t)uBlockWrite << pImage->uShiftIndex2Offset));
|
---|
2176 | if (VBOX_FAILURE(rc))
|
---|
2177 | Log(("VDIShrinkImage: RTFileSetSize rc=%Vrc\n", rc));
|
---|
2178 | }
|
---|
2179 | cBlocksAllocated2 = uBlockWrite;
|
---|
2180 | }
|
---|
2181 | else
|
---|
2182 | {
|
---|
2183 | Log(("VDIShrinkImage: failed to allocate working buffer (%u bytes)\n", cbBlock));
|
---|
2184 | rc = VERR_NO_MEMORY;
|
---|
2185 | }
|
---|
2186 |
|
---|
2187 | /* Save header and blocks array. */
|
---|
2188 | if (VBOX_SUCCESS(rc))
|
---|
2189 | {
|
---|
2190 | setImageBlocksAllocated(&pImage->Header, cBlocksAllocated2);
|
---|
2191 | rc = vdiUpdateBlocks(pImage);
|
---|
2192 | if (pfnProgress)
|
---|
2193 | pfnProgress(NULL /* WARNING! pVM=NULL */, 100, pvUser);
|
---|
2194 | }
|
---|
2195 |
|
---|
2196 | /* Do debug dump. */
|
---|
2197 | vdiDumpImage(pImage);
|
---|
2198 |
|
---|
2199 | /* Clean up. */
|
---|
2200 | RTMemTmpFree(paBlocks2);
|
---|
2201 | vdiCloseImage(pImage);
|
---|
2202 |
|
---|
2203 | LogFlow(("VDIShrinkImage: returns %Vrc for filename=\"%s\"\n", rc, pszFilename));
|
---|
2204 | return rc;
|
---|
2205 | }
|
---|
2206 |
|
---|
2207 | /**
|
---|
2208 | * Converts image file from older VDI formats to current one.
|
---|
2209 | *
|
---|
2210 | * @returns VBox status code.
|
---|
2211 | * @param pszFilename Name of the image file to convert.
|
---|
2212 | * @param pfnProgress Progress callback. Optional.
|
---|
2213 | * @param pvUser User argument for the progress callback.
|
---|
2214 | * @remark Only used by vditool
|
---|
2215 | */
|
---|
2216 | VBOXDDU_DECL(int) VDIConvertImage(const char *pszFilename, PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
2217 | {
|
---|
2218 | LogFlow(("VDIConvertImage:\n"));
|
---|
2219 |
|
---|
2220 | /* Check arguments. */
|
---|
2221 | if ( !pszFilename
|
---|
2222 | || *pszFilename == '\0')
|
---|
2223 | {
|
---|
2224 | AssertMsgFailed(("Invalid arguments: pszFilename=%p\n", pszFilename));
|
---|
2225 | return VERR_INVALID_PARAMETER;
|
---|
2226 | }
|
---|
2227 |
|
---|
2228 | PVDIIMAGEDESC pImage;
|
---|
2229 | int rc = vdiOpenImage(&pImage, pszFilename, VDI_OPEN_FLAGS_NORMAL, NULL);
|
---|
2230 | if (VBOX_FAILURE(rc))
|
---|
2231 | {
|
---|
2232 | Log(("VDIConvertImage: vdiOpenImage rc=%Vrc filename=\"%s\"\n", rc, pszFilename));
|
---|
2233 | return rc;
|
---|
2234 | }
|
---|
2235 |
|
---|
2236 | VDIHEADER Header = {0};
|
---|
2237 | int off;
|
---|
2238 | uint64_t cbFile;
|
---|
2239 | uint64_t cbData;
|
---|
2240 |
|
---|
2241 | if (pImage->fReadOnly)
|
---|
2242 | {
|
---|
2243 | Log(("VDIConvertImage: image \"%s\" is opened as read-only!\n", pszFilename));
|
---|
2244 | rc = VERR_VDI_IMAGE_READ_ONLY;
|
---|
2245 | goto l_conversion_failed;
|
---|
2246 | }
|
---|
2247 |
|
---|
2248 | if (pImage->PreHeader.u32Version != 0x00000002)
|
---|
2249 | {
|
---|
2250 | Log(("VDIConvertImage: unsupported version=%08X filename=\"%s\"\n",
|
---|
2251 | pImage->PreHeader.u32Version, pszFilename));
|
---|
2252 | rc = VERR_VDI_UNSUPPORTED_VERSION;
|
---|
2253 | goto l_conversion_failed;
|
---|
2254 | }
|
---|
2255 |
|
---|
2256 | /* Build new version header from old one. */
|
---|
2257 | vdiInitHeader(&Header,
|
---|
2258 | getImageType(&pImage->Header),
|
---|
2259 | VDI_IMAGE_FLAGS_DEFAULT, /* Safety issue: Always use default flags. */
|
---|
2260 | getImageComment(&pImage->Header),
|
---|
2261 | getImageDiskSize(&pImage->Header),
|
---|
2262 | getImageBlockSize(&pImage->Header),
|
---|
2263 | 0);
|
---|
2264 | setImageBlocksAllocated(&Header, getImageBlocksAllocated(&pImage->Header));
|
---|
2265 | *getImageGeometry(&Header) = *getImageGeometry(&pImage->Header);
|
---|
2266 | setImageTranslation(&Header, getImageTranslation(&pImage->Header));
|
---|
2267 | *getImageCreationUUID(&Header) = *getImageCreationUUID(&pImage->Header);
|
---|
2268 | *getImageModificationUUID(&Header) = *getImageModificationUUID(&pImage->Header);
|
---|
2269 |
|
---|
2270 | /* Calc data offset. */
|
---|
2271 | off = getImageDataOffset(&Header) - getImageDataOffset(&pImage->Header);
|
---|
2272 | if (off <= 0)
|
---|
2273 | {
|
---|
2274 | rc = VERR_VDI_INVALID_HEADER;
|
---|
2275 | goto l_conversion_failed;
|
---|
2276 | }
|
---|
2277 |
|
---|
2278 | rc = RTFileGetSize(pImage->File, &cbFile);
|
---|
2279 | if (VBOX_FAILURE(rc))
|
---|
2280 | goto l_conversion_failed;
|
---|
2281 |
|
---|
2282 | /* Check file size. */
|
---|
2283 | cbData = cbFile - getImageDataOffset(&pImage->Header);
|
---|
2284 | if (cbData != (uint64_t)getImageBlocksAllocated(&pImage->Header) << pImage->uShiftIndex2Offset)
|
---|
2285 | {
|
---|
2286 | AssertMsgFailed(("Invalid file size, broken image?\n"));
|
---|
2287 | rc = VERR_VDI_INVALID_HEADER;
|
---|
2288 | goto l_conversion_failed;
|
---|
2289 | }
|
---|
2290 |
|
---|
2291 | /* Expand file. */
|
---|
2292 | rc = RTFileSetSize(pImage->File, cbFile + off);
|
---|
2293 | if (VBOX_FAILURE(rc))
|
---|
2294 | goto l_conversion_failed;
|
---|
2295 |
|
---|
2296 | if (cbData > 0)
|
---|
2297 | {
|
---|
2298 | /* Calc current file position to move data from. */
|
---|
2299 | uint64_t offFile;
|
---|
2300 | if (cbData > VDIDISK_DEFAULT_BUFFER_SIZE)
|
---|
2301 | offFile = cbFile - VDIDISK_DEFAULT_BUFFER_SIZE;
|
---|
2302 | else
|
---|
2303 | offFile = getImageDataOffset(&pImage->Header);
|
---|
2304 |
|
---|
2305 | unsigned cMoves = (unsigned)(cbData / VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
2306 | unsigned c = 0;
|
---|
2307 |
|
---|
2308 | /* alloc tmp buffer */
|
---|
2309 | void *pvBuf = RTMemTmpAlloc(VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
2310 | if (pvBuf)
|
---|
2311 | {
|
---|
2312 | /* Move data. */
|
---|
2313 | for (;;)
|
---|
2314 | {
|
---|
2315 | unsigned cbToMove = (unsigned)RT_MIN(cbData, VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
2316 |
|
---|
2317 | /* Read. */
|
---|
2318 | rc = RTFileSeek(pImage->File, offFile, RTFILE_SEEK_BEGIN, NULL);
|
---|
2319 | if (VBOX_FAILURE(rc))
|
---|
2320 | break;
|
---|
2321 | rc = RTFileRead(pImage->File, pvBuf, cbToMove, NULL);
|
---|
2322 | if (VBOX_FAILURE(rc))
|
---|
2323 | break;
|
---|
2324 |
|
---|
2325 | /* Write. */
|
---|
2326 | rc = RTFileSeek(pImage->File, offFile + off, RTFILE_SEEK_BEGIN, NULL);
|
---|
2327 | if (VBOX_FAILURE(rc))
|
---|
2328 | break;
|
---|
2329 | rc = RTFileWrite(pImage->File, pvBuf, cbToMove, NULL);
|
---|
2330 | if (VBOX_FAILURE(rc))
|
---|
2331 | break;
|
---|
2332 |
|
---|
2333 | if (pfnProgress)
|
---|
2334 | {
|
---|
2335 | c++;
|
---|
2336 | pfnProgress(NULL /* WARNING! pVM=NULL */,
|
---|
2337 | (c * 100) / cMoves,
|
---|
2338 | pvUser);
|
---|
2339 | /* Note: conversion is non breakable operation, skipping rc here. */
|
---|
2340 | }
|
---|
2341 |
|
---|
2342 | cbData -= cbToMove;
|
---|
2343 | if (cbData == 0)
|
---|
2344 | break;
|
---|
2345 |
|
---|
2346 | if (cbData > VDIDISK_DEFAULT_BUFFER_SIZE)
|
---|
2347 | offFile -= VDIDISK_DEFAULT_BUFFER_SIZE;
|
---|
2348 | else
|
---|
2349 | offFile = getImageDataOffset(&pImage->Header);
|
---|
2350 | }
|
---|
2351 |
|
---|
2352 | /* Fill the beginning of file with zeroes to wipe out old headers etc. */
|
---|
2353 | if (VBOX_SUCCESS(rc))
|
---|
2354 | {
|
---|
2355 | Assert(offFile + off <= VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
2356 | rc = RTFileSeek(pImage->File, 0, RTFILE_SEEK_BEGIN, NULL);
|
---|
2357 | if (VBOX_SUCCESS(rc))
|
---|
2358 | {
|
---|
2359 | memset(pvBuf, 0, (unsigned)offFile + off);
|
---|
2360 | rc = RTFileWrite(pImage->File, pvBuf, (unsigned)offFile + off, NULL);
|
---|
2361 | }
|
---|
2362 | }
|
---|
2363 |
|
---|
2364 | RTMemTmpFree(pvBuf);
|
---|
2365 | }
|
---|
2366 | else
|
---|
2367 | rc = VERR_NO_MEMORY;
|
---|
2368 |
|
---|
2369 | if (VBOX_FAILURE(rc))
|
---|
2370 | goto l_conversion_failed;
|
---|
2371 | }
|
---|
2372 |
|
---|
2373 | if (pfnProgress)
|
---|
2374 | {
|
---|
2375 | pfnProgress(NULL /* WARNING! pVM=NULL */, 100, pvUser);
|
---|
2376 | /* Note: conversion is non breakable operation, skipping rc here. */
|
---|
2377 | }
|
---|
2378 |
|
---|
2379 | /* Data moved, now we need to save new pre header, header and blocks array. */
|
---|
2380 |
|
---|
2381 | vdiInitPreHeader(&pImage->PreHeader);
|
---|
2382 | pImage->Header = Header;
|
---|
2383 |
|
---|
2384 | /* Setup image parameters by header. */
|
---|
2385 | vdiSetupImageDesc(pImage);
|
---|
2386 |
|
---|
2387 | /* Write pre-header. */
|
---|
2388 | rc = RTFileSeek(pImage->File, 0, RTFILE_SEEK_BEGIN, NULL);
|
---|
2389 | if (VBOX_FAILURE(rc))
|
---|
2390 | goto l_conversion_failed;
|
---|
2391 | rc = RTFileWrite(pImage->File, &pImage->PreHeader, sizeof(pImage->PreHeader), NULL);
|
---|
2392 | if (VBOX_FAILURE(rc))
|
---|
2393 | goto l_conversion_failed;
|
---|
2394 |
|
---|
2395 | /* Write header and blocks array. */
|
---|
2396 | rc = vdiUpdateBlocks(pImage);
|
---|
2397 |
|
---|
2398 | l_conversion_failed:
|
---|
2399 | vdiCloseImage(pImage);
|
---|
2400 |
|
---|
2401 | LogFlow(("VDIConvertImage: returns %Vrc for filename=\"%s\"\n", rc, pszFilename));
|
---|
2402 | return rc;
|
---|
2403 | }
|
---|
2404 |
|
---|
2405 | /**
|
---|
2406 | * Queries the image's UUID and parent UUIDs.
|
---|
2407 | *
|
---|
2408 | * @returns VBox status code.
|
---|
2409 | * @param pszFilename Name of the image file to operate on.
|
---|
2410 | * @param pUuid Where to store image UUID (can be NULL).
|
---|
2411 | * @param pModificationUuid Where to store modification UUID (can be NULL).
|
---|
2412 | * @param pParentUuuid Where to store parent UUID (can be NULL).
|
---|
2413 | * @param pParentModificationUuid Where to store parent modification UUID (can be NULL).
|
---|
2414 | */
|
---|
2415 | VBOXDDU_DECL(int) VDIGetImageUUIDs(const char *pszFilename,
|
---|
2416 | PRTUUID pUuid, PRTUUID pModificationUuid,
|
---|
2417 | PRTUUID pParentUuid, PRTUUID pParentModificationUuid)
|
---|
2418 | {
|
---|
2419 | LogFlow(("VDIGetImageUUIDs:\n"));
|
---|
2420 |
|
---|
2421 | /* Check arguments. */
|
---|
2422 | if ( !pszFilename
|
---|
2423 | || *pszFilename == '\0')
|
---|
2424 | {
|
---|
2425 | AssertMsgFailed(("Invalid arguments: pszFilename=%p\n", pszFilename));
|
---|
2426 | return VERR_INVALID_PARAMETER;
|
---|
2427 | }
|
---|
2428 |
|
---|
2429 | /*
|
---|
2430 | * Try open the specified image.
|
---|
2431 | */
|
---|
2432 | PVDIIMAGEDESC pImage;
|
---|
2433 | int rc = vdiOpenImage(&pImage, pszFilename, VDI_OPEN_FLAGS_NORMAL, NULL);
|
---|
2434 | if (VBOX_FAILURE(rc))
|
---|
2435 | {
|
---|
2436 | Log(("VDIGetImageUUIDs: vdiOpenImage rc=%Vrc filename=\"%s\"\n", rc, pszFilename));
|
---|
2437 | return rc;
|
---|
2438 | }
|
---|
2439 |
|
---|
2440 | /*
|
---|
2441 | * Query data.
|
---|
2442 | */
|
---|
2443 | if (pUuid)
|
---|
2444 | {
|
---|
2445 | PCRTUUID pTmpUuid = getImageCreationUUID(&pImage->Header);
|
---|
2446 | if (pTmpUuid)
|
---|
2447 | *pUuid = *pTmpUuid;
|
---|
2448 | else
|
---|
2449 | RTUuidClear(pUuid);
|
---|
2450 | }
|
---|
2451 | if (pModificationUuid)
|
---|
2452 | {
|
---|
2453 | PCRTUUID pTmpUuid = getImageModificationUUID(&pImage->Header);
|
---|
2454 | if (pTmpUuid)
|
---|
2455 | *pModificationUuid = *pTmpUuid;
|
---|
2456 | else
|
---|
2457 | RTUuidClear(pModificationUuid);
|
---|
2458 | }
|
---|
2459 | if (pParentUuid)
|
---|
2460 | {
|
---|
2461 | PCRTUUID pTmpUuid = getImageParentUUID(&pImage->Header);
|
---|
2462 | if (pTmpUuid)
|
---|
2463 | *pParentUuid = *pTmpUuid;
|
---|
2464 | else
|
---|
2465 | RTUuidClear(pParentUuid);
|
---|
2466 | }
|
---|
2467 | if (pParentModificationUuid)
|
---|
2468 | {
|
---|
2469 | PCRTUUID pTmpUuid = getImageParentModificationUUID(&pImage->Header);
|
---|
2470 | if (pTmpUuid)
|
---|
2471 | *pParentModificationUuid = *pTmpUuid;
|
---|
2472 | else
|
---|
2473 | RTUuidClear(pParentModificationUuid);
|
---|
2474 | }
|
---|
2475 |
|
---|
2476 | /*
|
---|
2477 | * Close the image.
|
---|
2478 | */
|
---|
2479 | vdiCloseImage(pImage);
|
---|
2480 |
|
---|
2481 | return VINF_SUCCESS;
|
---|
2482 | }
|
---|
2483 |
|
---|
2484 | /**
|
---|
2485 | * Changes the image's UUID and parent UUIDs.
|
---|
2486 | *
|
---|
2487 | * @returns VBox status code.
|
---|
2488 | * @param pszFilename Name of the image file to operate on.
|
---|
2489 | * @param pUuid Optional parameter, new UUID of the image.
|
---|
2490 | * @param pModificationUuid Optional parameter, new modification UUID of the image.
|
---|
2491 | * @param pParentUuuid Optional parameter, new parent UUID of the image.
|
---|
2492 | * @param pParentModificationUuid Optional parameter, new parent modification UUID of the image.
|
---|
2493 | */
|
---|
2494 | VBOXDDU_DECL(int) VDISetImageUUIDs(const char *pszFilename,
|
---|
2495 | PCRTUUID pUuid, PCRTUUID pModificationUuid,
|
---|
2496 | PCRTUUID pParentUuid, PCRTUUID pParentModificationUuid)
|
---|
2497 | {
|
---|
2498 | LogFlow(("VDISetImageUUIDs:\n"));
|
---|
2499 |
|
---|
2500 | /* Check arguments. */
|
---|
2501 | if ( !pszFilename
|
---|
2502 | || *pszFilename == '\0')
|
---|
2503 | {
|
---|
2504 | AssertMsgFailed(("Invalid arguments: pszFilename=%p\n", pszFilename));
|
---|
2505 | return VERR_INVALID_PARAMETER;
|
---|
2506 | }
|
---|
2507 |
|
---|
2508 | PVDIIMAGEDESC pImage;
|
---|
2509 | int rc = vdiOpenImage(&pImage, pszFilename, VDI_OPEN_FLAGS_NORMAL, NULL);
|
---|
2510 | if (VBOX_FAILURE(rc))
|
---|
2511 | {
|
---|
2512 | Log(("VDISetImageUUIDs: vdiOpenImage rc=%Vrc filename=\"%s\"\n", rc, pszFilename));
|
---|
2513 | return rc;
|
---|
2514 | }
|
---|
2515 | if (!pImage->fReadOnly)
|
---|
2516 | {
|
---|
2517 | /* we only support new images */
|
---|
2518 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
2519 | {
|
---|
2520 | if (pUuid)
|
---|
2521 | pImage->Header.u.v1.uuidCreate = *pUuid;
|
---|
2522 |
|
---|
2523 | if (pModificationUuid)
|
---|
2524 | pImage->Header.u.v1.uuidModify = *pModificationUuid;
|
---|
2525 |
|
---|
2526 | if (pParentUuid)
|
---|
2527 | pImage->Header.u.v1.uuidLinkage = *pParentUuid;
|
---|
2528 |
|
---|
2529 | if (pParentModificationUuid)
|
---|
2530 | pImage->Header.u.v1.uuidParentModify = *pParentModificationUuid;
|
---|
2531 |
|
---|
2532 | /* write out new header */
|
---|
2533 | rc = vdiUpdateHeader(pImage);
|
---|
2534 | AssertMsgRC(rc, ("vdiUpdateHeader() failed, filename=\"%s\", rc=%Vrc\n",
|
---|
2535 | pImage->szFilename, rc));
|
---|
2536 | }
|
---|
2537 | else
|
---|
2538 | {
|
---|
2539 | Log(("VDISetImageUUIDs: Version is not supported!\n"));
|
---|
2540 | rc = VERR_VDI_UNSUPPORTED_VERSION;
|
---|
2541 | }
|
---|
2542 | }
|
---|
2543 | else
|
---|
2544 | {
|
---|
2545 | Log(("VDISetImageUUIDs: image \"%s\" is opened as read-only!\n", pszFilename));
|
---|
2546 | rc = VERR_VDI_IMAGE_READ_ONLY;
|
---|
2547 | }
|
---|
2548 |
|
---|
2549 | vdiCloseImage(pImage);
|
---|
2550 | return rc;
|
---|
2551 | }
|
---|
2552 |
|
---|
2553 | /**
|
---|
2554 | * Merges two images having a parent/child relationship (both directions).
|
---|
2555 | *
|
---|
2556 | * @returns VBox status code.
|
---|
2557 | * @param pszFilenameFrom Name of the image file to merge from.
|
---|
2558 | * @param pszFilenameTo Name of the image file to merge into.
|
---|
2559 | * @param pfnProgress Progress callback. Optional. NULL if not to be used.
|
---|
2560 | * @param pvUser User argument for the progress callback.
|
---|
2561 | */
|
---|
2562 | VBOXDDU_DECL(int) VDIMergeImage(const char *pszFilenameFrom, const char *pszFilenameTo,
|
---|
2563 | PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
2564 | {
|
---|
2565 | LogFlow(("VDIMergeImage:\n"));
|
---|
2566 |
|
---|
2567 | /* Check arguments. */
|
---|
2568 | if ( !pszFilenameFrom
|
---|
2569 | || *pszFilenameFrom == '\0'
|
---|
2570 | || !pszFilenameTo
|
---|
2571 | || *pszFilenameTo == '\0')
|
---|
2572 | {
|
---|
2573 | AssertMsgFailed(("Invalid arguments: pszFilenameFrom=%p, pszFilenameTo=%p\n", pszFilenameFrom, pszFilenameTo));
|
---|
2574 | return VERR_INVALID_PARAMETER;
|
---|
2575 | }
|
---|
2576 |
|
---|
2577 | PVDIIMAGEDESC pImageFrom;
|
---|
2578 | int rc = vdiOpenImage(&pImageFrom, pszFilenameFrom, VDI_OPEN_FLAGS_READONLY, NULL);
|
---|
2579 | if (VBOX_FAILURE(rc))
|
---|
2580 | {
|
---|
2581 | Log(("VDIMergeImage: vdiOpenImage rc=%Vrc pstFilenameFrom=\"%s\"\n", rc, pszFilenameFrom));
|
---|
2582 | return rc;
|
---|
2583 | }
|
---|
2584 |
|
---|
2585 | PVDIIMAGEDESC pImageTo;
|
---|
2586 | rc = vdiOpenImage(&pImageTo, pszFilenameTo, VDI_OPEN_FLAGS_NORMAL, NULL);
|
---|
2587 | if (VBOX_FAILURE(rc))
|
---|
2588 | {
|
---|
2589 | Log(("VDIMergeImage: vdiOpenImage rc=%Vrc pszFilenameTo=\"%s\"\n", rc, pszFilenameTo));
|
---|
2590 | vdiCloseImage(pImageFrom);
|
---|
2591 | return rc;
|
---|
2592 | }
|
---|
2593 | if (pImageTo->fReadOnly)
|
---|
2594 | {
|
---|
2595 | Log(("VDIMergeImage: image \"%s\" is opened as read-only!\n", pszFilenameTo));
|
---|
2596 | vdiCloseImage(pImageFrom);
|
---|
2597 | vdiCloseImage(pImageTo);
|
---|
2598 | return VERR_VDI_IMAGE_READ_ONLY;
|
---|
2599 | }
|
---|
2600 |
|
---|
2601 | /*
|
---|
2602 | * when merging, we should not update the modification uuid of the target
|
---|
2603 | * image, because from the point of view of its children, it hasn't been
|
---|
2604 | * logically changed after the successful merge.
|
---|
2605 | */
|
---|
2606 | vdiDisableLastModifiedUpdate(pImageTo);
|
---|
2607 |
|
---|
2608 | /*
|
---|
2609 | * Check in which direction we merge
|
---|
2610 | */
|
---|
2611 |
|
---|
2612 | bool bParentToChild = false;
|
---|
2613 | if ( getImageParentUUID(&pImageFrom->Header)
|
---|
2614 | && !RTUuidCompare(getImageParentUUID(&pImageFrom->Header),
|
---|
2615 | getImageCreationUUID(&pImageTo->Header))
|
---|
2616 | && !RTUuidCompare(getImageParentModificationUUID(&pImageFrom->Header),
|
---|
2617 | getImageModificationUUID(&pImageTo->Header)))
|
---|
2618 | {
|
---|
2619 | /* we merge from a child to its parent */
|
---|
2620 | }
|
---|
2621 | else
|
---|
2622 | if ( getImageParentUUID(&pImageTo->Header)
|
---|
2623 | && !RTUuidCompare(getImageParentUUID(&pImageTo->Header),
|
---|
2624 | getImageCreationUUID(&pImageFrom->Header))
|
---|
2625 | && !RTUuidCompare(getImageParentModificationUUID(&pImageTo->Header),
|
---|
2626 | getImageModificationUUID(&pImageFrom->Header)))
|
---|
2627 | {
|
---|
2628 | /* we merge from a parent to its child */
|
---|
2629 | bParentToChild = true;
|
---|
2630 | }
|
---|
2631 | else
|
---|
2632 | {
|
---|
2633 | /* the images are not related, we can't merge! */
|
---|
2634 | Log(("VDIMergeImages: images do not have a parent/child or child/parent relationship!\n"));
|
---|
2635 | rc = VERR_VDI_IMAGES_UUID_MISMATCH;
|
---|
2636 | }
|
---|
2637 |
|
---|
2638 | rc = vdiMergeImages(pImageFrom, pImageTo, bParentToChild, pfnProgress, pvUser);
|
---|
2639 |
|
---|
2640 | if (pfnProgress)
|
---|
2641 | {
|
---|
2642 | pfnProgress(NULL /* WARNING! pVM=NULL */, 100, pvUser);
|
---|
2643 | /* Note: commiting is non breakable operation, skipping rc here. */
|
---|
2644 | }
|
---|
2645 |
|
---|
2646 | /* cleanup */
|
---|
2647 | vdiCloseImage(pImageFrom);
|
---|
2648 | vdiCloseImage(pImageTo);
|
---|
2649 |
|
---|
2650 | Log(("VDIMergeImage: done, returning with rc = %Vrc\n", rc));
|
---|
2651 | return rc;
|
---|
2652 | }
|
---|
2653 |
|
---|
2654 |
|
---|
2655 | /**
|
---|
2656 | * Initialize the VDIDISK structure.
|
---|
2657 | */
|
---|
2658 | void vdiInitVDIDisk(PVDIDISK pDisk)
|
---|
2659 | {
|
---|
2660 | Assert(pDisk);
|
---|
2661 | pDisk->u32Signature = VDIDISK_SIGNATURE;
|
---|
2662 | pDisk->cImages = 0;
|
---|
2663 | pDisk->pBase = NULL;
|
---|
2664 | pDisk->pLast = NULL;
|
---|
2665 | pDisk->cbBlock = VDI_IMAGE_DEFAULT_BLOCK_SIZE;
|
---|
2666 | pDisk->cbBuf = VDIDISK_DEFAULT_BUFFER_SIZE;
|
---|
2667 | pDisk->fHonorZeroWrites = false;
|
---|
2668 | }
|
---|
2669 |
|
---|
2670 | /**
|
---|
2671 | * internal: add image structure to the end of images list.
|
---|
2672 | */
|
---|
2673 | static void vdiAddImageToList(PVDIDISK pDisk, PVDIIMAGEDESC pImage)
|
---|
2674 | {
|
---|
2675 | pImage->pPrev = NULL;
|
---|
2676 | pImage->pNext = NULL;
|
---|
2677 |
|
---|
2678 | if (pDisk->pBase)
|
---|
2679 | {
|
---|
2680 | Assert(pDisk->cImages > 0);
|
---|
2681 | pImage->pPrev = pDisk->pLast;
|
---|
2682 | pDisk->pLast->pNext = pImage;
|
---|
2683 | pDisk->pLast = pImage;
|
---|
2684 | }
|
---|
2685 | else
|
---|
2686 | {
|
---|
2687 | Assert(pDisk->cImages == 0);
|
---|
2688 | pDisk->pBase = pImage;
|
---|
2689 | pDisk->pLast = pImage;
|
---|
2690 | }
|
---|
2691 |
|
---|
2692 | pDisk->cImages++;
|
---|
2693 | }
|
---|
2694 |
|
---|
2695 | /**
|
---|
2696 | * internal: remove image structure from the images list.
|
---|
2697 | */
|
---|
2698 | static void vdiRemoveImageFromList(PVDIDISK pDisk, PVDIIMAGEDESC pImage)
|
---|
2699 | {
|
---|
2700 | Assert(pDisk->cImages > 0);
|
---|
2701 |
|
---|
2702 | if (pImage->pPrev)
|
---|
2703 | pImage->pPrev->pNext = pImage->pNext;
|
---|
2704 | else
|
---|
2705 | pDisk->pBase = pImage->pNext;
|
---|
2706 |
|
---|
2707 | if (pImage->pNext)
|
---|
2708 | pImage->pNext->pPrev = pImage->pPrev;
|
---|
2709 | else
|
---|
2710 | pDisk->pLast = pImage->pPrev;
|
---|
2711 |
|
---|
2712 | pImage->pPrev = NULL;
|
---|
2713 | pImage->pNext = NULL;
|
---|
2714 |
|
---|
2715 | pDisk->cImages--;
|
---|
2716 | }
|
---|
2717 |
|
---|
2718 | /**
|
---|
2719 | * Allocates and initializes VDI HDD container.
|
---|
2720 | *
|
---|
2721 | * @returns Pointer to newly created HDD container with no one opened image file.
|
---|
2722 | * @returns NULL on failure, typically out of memory.
|
---|
2723 | */
|
---|
2724 | VBOXDDU_DECL(PVDIDISK) VDIDiskCreate(void)
|
---|
2725 | {
|
---|
2726 | PVDIDISK pDisk = (PVDIDISK)RTMemAllocZ(sizeof(VDIDISK));
|
---|
2727 | if (pDisk)
|
---|
2728 | vdiInitVDIDisk(pDisk);
|
---|
2729 | LogFlow(("VDIDiskCreate: returns pDisk=%X\n", pDisk));
|
---|
2730 | return pDisk;
|
---|
2731 | }
|
---|
2732 |
|
---|
2733 | /**
|
---|
2734 | * Destroys VDI HDD container. If container has opened image files they will be closed.
|
---|
2735 | *
|
---|
2736 | * @param pDisk Pointer to VDI HDD container.
|
---|
2737 | */
|
---|
2738 | VBOXDDU_DECL(void) VDIDiskDestroy(PVDIDISK pDisk)
|
---|
2739 | {
|
---|
2740 | LogFlow(("VDIDiskDestroy: pDisk=%X\n", pDisk));
|
---|
2741 | /* sanity check */
|
---|
2742 | Assert(pDisk);
|
---|
2743 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
2744 |
|
---|
2745 | if (pDisk)
|
---|
2746 | {
|
---|
2747 | VDIDiskCloseAllImages(pDisk);
|
---|
2748 | RTMemFree(pDisk);
|
---|
2749 | }
|
---|
2750 | }
|
---|
2751 |
|
---|
2752 | /**
|
---|
2753 | * Get working buffer size of VDI HDD container.
|
---|
2754 | *
|
---|
2755 | * @returns Working buffer size in bytes.
|
---|
2756 | */
|
---|
2757 | VBOXDDU_DECL(unsigned) VDIDiskGetBufferSize(PVDIDISK pDisk)
|
---|
2758 | {
|
---|
2759 | /* sanity check */
|
---|
2760 | Assert(pDisk);
|
---|
2761 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
2762 |
|
---|
2763 | LogFlow(("VDIDiskGetBufferSize: returns %u\n", pDisk->cbBuf));
|
---|
2764 | return pDisk->cbBuf;
|
---|
2765 | }
|
---|
2766 |
|
---|
2767 | /**
|
---|
2768 | * Get read/write mode of VDI HDD.
|
---|
2769 | *
|
---|
2770 | * @returns Disk ReadOnly status.
|
---|
2771 | * @returns true if no one VDI image is opened in HDD container.
|
---|
2772 | */
|
---|
2773 | VBOXDDU_DECL(bool) VDIDiskIsReadOnly(PVDIDISK pDisk)
|
---|
2774 | {
|
---|
2775 | /* sanity check */
|
---|
2776 | Assert(pDisk);
|
---|
2777 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
2778 |
|
---|
2779 | if (pDisk->pLast)
|
---|
2780 | {
|
---|
2781 | LogFlow(("VDIDiskIsReadOnly: returns %u\n", pDisk->pLast->fReadOnly));
|
---|
2782 | return pDisk->pLast->fReadOnly;
|
---|
2783 | }
|
---|
2784 |
|
---|
2785 | AssertMsgFailed(("No one disk image is opened!\n"));
|
---|
2786 | return true;
|
---|
2787 | }
|
---|
2788 |
|
---|
2789 | /**
|
---|
2790 | * Get disk size of VDI HDD container.
|
---|
2791 | *
|
---|
2792 | * @returns Virtual disk size in bytes.
|
---|
2793 | * @returns 0 if no one VDI image is opened in HDD container.
|
---|
2794 | */
|
---|
2795 | VBOXDDU_DECL(uint64_t) VDIDiskGetSize(PVDIDISK pDisk)
|
---|
2796 | {
|
---|
2797 | /* sanity check */
|
---|
2798 | Assert(pDisk);
|
---|
2799 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
2800 |
|
---|
2801 | if (pDisk->pBase)
|
---|
2802 | {
|
---|
2803 | LogFlow(("VDIDiskGetSize: returns %llu\n", getImageDiskSize(&pDisk->pBase->Header)));
|
---|
2804 | return getImageDiskSize(&pDisk->pBase->Header);
|
---|
2805 | }
|
---|
2806 |
|
---|
2807 | AssertMsgFailed(("No one disk image is opened!\n"));
|
---|
2808 | return 0;
|
---|
2809 | }
|
---|
2810 |
|
---|
2811 | /**
|
---|
2812 | * Get block size of VDI HDD container.
|
---|
2813 | *
|
---|
2814 | * @returns VDI image block size in bytes.
|
---|
2815 | * @returns 0 if no one VDI image is opened in HDD container.
|
---|
2816 | */
|
---|
2817 | VBOXDDU_DECL(unsigned) VDIDiskGetBlockSize(PVDIDISK pDisk)
|
---|
2818 | {
|
---|
2819 | /* sanity check */
|
---|
2820 | Assert(pDisk);
|
---|
2821 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
2822 |
|
---|
2823 | if (pDisk->pBase)
|
---|
2824 | {
|
---|
2825 | LogFlow(("VDIDiskGetBlockSize: returns %u\n", getImageBlockSize(&pDisk->pBase->Header)));
|
---|
2826 | return getImageBlockSize(&pDisk->pBase->Header);
|
---|
2827 | }
|
---|
2828 |
|
---|
2829 | AssertMsgFailed(("No one disk image is opened!\n"));
|
---|
2830 | return 0;
|
---|
2831 | }
|
---|
2832 |
|
---|
2833 | /**
|
---|
2834 | * Get virtual disk geometry stored in image file.
|
---|
2835 | *
|
---|
2836 | * @returns VBox status code.
|
---|
2837 | * @returns VERR_VDI_NOT_OPENED if no one VDI image is opened in HDD container.
|
---|
2838 | * @returns VERR_VDI_GEOMETRY_NOT_SET if no geometry has been setted.
|
---|
2839 | * @param pDisk Pointer to VDI HDD container.
|
---|
2840 | * @param pcCylinders Where to store the number of cylinders. NULL is ok.
|
---|
2841 | * @param pcHeads Where to store the number of heads. NULL is ok.
|
---|
2842 | * @param pcSectors Where to store the number of sectors. NULL is ok.
|
---|
2843 | */
|
---|
2844 | VBOXDDU_DECL(int) VDIDiskGetGeometry(PVDIDISK pDisk, unsigned *pcCylinders, unsigned *pcHeads, unsigned *pcSectors)
|
---|
2845 | {
|
---|
2846 | /* sanity check */
|
---|
2847 | Assert(pDisk);
|
---|
2848 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
2849 |
|
---|
2850 | if (pDisk->pBase)
|
---|
2851 | {
|
---|
2852 | int rc = VINF_SUCCESS;
|
---|
2853 | PVDIDISKGEOMETRY pGeometry = getImageGeometry(&pDisk->pBase->Header);
|
---|
2854 | LogFlow(("VDIDiskGetGeometry: C/H/S = %u/%u/%u\n",
|
---|
2855 | pGeometry->cCylinders, pGeometry->cHeads, pGeometry->cSectors));
|
---|
2856 | if ( pGeometry->cCylinders > 0
|
---|
2857 | && pGeometry->cHeads > 0
|
---|
2858 | && pGeometry->cSectors > 0)
|
---|
2859 | {
|
---|
2860 | if (pcCylinders)
|
---|
2861 | *pcCylinders = pGeometry->cCylinders;
|
---|
2862 | if (pcHeads)
|
---|
2863 | *pcHeads = pGeometry->cHeads;
|
---|
2864 | if (pcSectors)
|
---|
2865 | *pcSectors = pGeometry->cSectors;
|
---|
2866 | }
|
---|
2867 | else
|
---|
2868 | rc = VERR_VDI_GEOMETRY_NOT_SET;
|
---|
2869 |
|
---|
2870 | LogFlow(("VDIDiskGetGeometry: returns %Vrc\n", rc));
|
---|
2871 | return rc;
|
---|
2872 | }
|
---|
2873 |
|
---|
2874 | AssertMsgFailed(("No one disk image is opened!\n"));
|
---|
2875 | return VERR_VDI_NOT_OPENED;
|
---|
2876 | }
|
---|
2877 |
|
---|
2878 | /**
|
---|
2879 | * Store virtual disk geometry into base image file of HDD container.
|
---|
2880 | *
|
---|
2881 | * Note that in case of unrecoverable error all images of HDD container will be closed.
|
---|
2882 | *
|
---|
2883 | * @returns VBox status code.
|
---|
2884 | * @returns VERR_VDI_NOT_OPENED if no one VDI image is opened in HDD container.
|
---|
2885 | * @param pDisk Pointer to VDI HDD container.
|
---|
2886 | * @param cCylinders Number of cylinders.
|
---|
2887 | * @param cHeads Number of heads.
|
---|
2888 | * @param cSectors Number of sectors.
|
---|
2889 | */
|
---|
2890 | VBOXDDU_DECL(int) VDIDiskSetGeometry(PVDIDISK pDisk, unsigned cCylinders, unsigned cHeads, unsigned cSectors)
|
---|
2891 | {
|
---|
2892 | LogFlow(("VDIDiskSetGeometry: C/H/S = %u/%u/%u\n", cCylinders, cHeads, cSectors));
|
---|
2893 | /* sanity check */
|
---|
2894 | Assert(pDisk);
|
---|
2895 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
2896 |
|
---|
2897 | if (pDisk->pBase)
|
---|
2898 | {
|
---|
2899 | PVDIDISKGEOMETRY pGeometry = getImageGeometry(&pDisk->pBase->Header);
|
---|
2900 | pGeometry->cCylinders = cCylinders;
|
---|
2901 | pGeometry->cHeads = cHeads;
|
---|
2902 | pGeometry->cSectors = cSectors;
|
---|
2903 | pGeometry->cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
2904 |
|
---|
2905 | /* Update header information in base image file. */
|
---|
2906 | int rc = vdiUpdateReadOnlyHeader(pDisk->pBase);
|
---|
2907 | LogFlow(("VDIDiskSetGeometry: returns %Vrc\n", rc));
|
---|
2908 | return rc;
|
---|
2909 | }
|
---|
2910 |
|
---|
2911 | AssertMsgFailed(("No one disk image is opened!\n"));
|
---|
2912 | return VERR_VDI_NOT_OPENED;
|
---|
2913 | }
|
---|
2914 |
|
---|
2915 | /**
|
---|
2916 | * Get virtual disk translation mode stored in image file.
|
---|
2917 | *
|
---|
2918 | * @returns VBox status code.
|
---|
2919 | * @returns VERR_VDI_NOT_OPENED if no one VDI image is opened in HDD container.
|
---|
2920 | * @param pDisk Pointer to VDI HDD container.
|
---|
2921 | * @param penmTranslation Where to store the translation mode (see pdm.h).
|
---|
2922 | */
|
---|
2923 | VBOXDDU_DECL(int) VDIDiskGetTranslation(PVDIDISK pDisk, PPDMBIOSTRANSLATION penmTranslation)
|
---|
2924 | {
|
---|
2925 | /* sanity check */
|
---|
2926 | Assert(pDisk);
|
---|
2927 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
2928 | Assert(penmTranslation);
|
---|
2929 |
|
---|
2930 | if (pDisk->pBase)
|
---|
2931 | {
|
---|
2932 | *penmTranslation = getImageTranslation(&pDisk->pBase->Header);
|
---|
2933 | LogFlow(("VDIDiskGetTranslation: translation=%d\n", *penmTranslation));
|
---|
2934 | return VINF_SUCCESS;
|
---|
2935 | }
|
---|
2936 |
|
---|
2937 | AssertMsgFailed(("No one disk image is opened!\n"));
|
---|
2938 | return VERR_VDI_NOT_OPENED;
|
---|
2939 | }
|
---|
2940 |
|
---|
2941 | /**
|
---|
2942 | * Store virtual disk translation mode into base image file of HDD container.
|
---|
2943 | *
|
---|
2944 | * Note that in case of unrecoverable error all images of HDD container will be closed.
|
---|
2945 | *
|
---|
2946 | * @returns VBox status code.
|
---|
2947 | * @returns VERR_VDI_NOT_OPENED if no one VDI image is opened in HDD container.
|
---|
2948 | * @param pDisk Pointer to VDI HDD container.
|
---|
2949 | * @param enmTranslation Translation mode (see pdm.h).
|
---|
2950 | */
|
---|
2951 | VBOXDDU_DECL(int) VDIDiskSetTranslation(PVDIDISK pDisk, PDMBIOSTRANSLATION enmTranslation)
|
---|
2952 | {
|
---|
2953 | LogFlow(("VDIDiskSetTranslation: enmTranslation=%d\n", enmTranslation));
|
---|
2954 | /* sanity check */
|
---|
2955 | Assert(pDisk);
|
---|
2956 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
2957 |
|
---|
2958 | if (pDisk->pBase)
|
---|
2959 | {
|
---|
2960 | setImageTranslation(&pDisk->pBase->Header, enmTranslation);
|
---|
2961 |
|
---|
2962 | /* Update header information in base image file. */
|
---|
2963 | int rc = vdiUpdateReadOnlyHeader(pDisk->pBase);
|
---|
2964 | LogFlow(("VDIDiskSetTranslation: returns %Vrc\n", rc));
|
---|
2965 | return rc;
|
---|
2966 | }
|
---|
2967 |
|
---|
2968 | AssertMsgFailed(("No one disk image is opened!\n"));
|
---|
2969 | return VERR_VDI_NOT_OPENED;
|
---|
2970 | }
|
---|
2971 |
|
---|
2972 | /**
|
---|
2973 | * Get number of opened images in HDD container.
|
---|
2974 | *
|
---|
2975 | * @returns Number of opened images for HDD container. 0 if no images is opened.
|
---|
2976 | * @param pDisk Pointer to VDI HDD container.
|
---|
2977 | */
|
---|
2978 | VBOXDDU_DECL(int) VDIDiskGetImagesCount(PVDIDISK pDisk)
|
---|
2979 | {
|
---|
2980 | /* sanity check */
|
---|
2981 | Assert(pDisk);
|
---|
2982 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
2983 |
|
---|
2984 | LogFlow(("VDIDiskGetImagesCount: returns %d\n", pDisk->cImages));
|
---|
2985 | return pDisk->cImages;
|
---|
2986 | }
|
---|
2987 |
|
---|
2988 | static PVDIIMAGEDESC vdiGetImageByNumber(PVDIDISK pDisk, int nImage)
|
---|
2989 | {
|
---|
2990 | PVDIIMAGEDESC pImage = pDisk->pBase;
|
---|
2991 | while (pImage && nImage)
|
---|
2992 | {
|
---|
2993 | pImage = pImage->pNext;
|
---|
2994 | nImage--;
|
---|
2995 | }
|
---|
2996 | return pImage;
|
---|
2997 | }
|
---|
2998 |
|
---|
2999 | /**
|
---|
3000 | * Get version of opened image of HDD container.
|
---|
3001 | *
|
---|
3002 | * @returns VBox status code.
|
---|
3003 | * @returns VERR_VDI_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
3004 | * @param pDisk Pointer to VDI HDD container.
|
---|
3005 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
3006 | * @param puVersion Where to store the image version.
|
---|
3007 | */
|
---|
3008 | VBOXDDU_DECL(int) VDIDiskGetImageVersion(PVDIDISK pDisk, int nImage, unsigned *puVersion)
|
---|
3009 | {
|
---|
3010 | /* sanity check */
|
---|
3011 | Assert(pDisk);
|
---|
3012 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3013 | Assert(puVersion);
|
---|
3014 |
|
---|
3015 | PVDIIMAGEDESC pImage = vdiGetImageByNumber(pDisk, nImage);
|
---|
3016 | Assert(pImage);
|
---|
3017 |
|
---|
3018 | if (pImage)
|
---|
3019 | {
|
---|
3020 | *puVersion = pImage->PreHeader.u32Version;
|
---|
3021 | LogFlow(("VDIDiskGetImageVersion: returns %08X\n", pImage->PreHeader.u32Version));
|
---|
3022 | return VINF_SUCCESS;
|
---|
3023 | }
|
---|
3024 |
|
---|
3025 | AssertMsgFailed(("Image %d was not found (cImages=%d)\n", nImage, pDisk->cImages));
|
---|
3026 | return VERR_VDI_IMAGE_NOT_FOUND;
|
---|
3027 | }
|
---|
3028 |
|
---|
3029 | /**
|
---|
3030 | * Get filename of opened image of HDD container.
|
---|
3031 | *
|
---|
3032 | * @returns VBox status code.
|
---|
3033 | * @returns VERR_VDI_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
3034 | * @returns VERR_BUFFER_OVERFLOW if pszFilename buffer too small to hold filename.
|
---|
3035 | * @param pDisk Pointer to VDI HDD container.
|
---|
3036 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
3037 | * @param pszFilename Where to store the image file name.
|
---|
3038 | * @param cbFilename Size of buffer pszFilename points to.
|
---|
3039 | */
|
---|
3040 | VBOXDDU_DECL(int) VDIDiskGetImageFilename(PVDIDISK pDisk, int nImage, char *pszFilename, unsigned cbFilename)
|
---|
3041 | {
|
---|
3042 | /* sanity check */
|
---|
3043 | Assert(pDisk);
|
---|
3044 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3045 | Assert(pszFilename);
|
---|
3046 |
|
---|
3047 | PVDIIMAGEDESC pImage = vdiGetImageByNumber(pDisk, nImage);
|
---|
3048 | Assert(pImage);
|
---|
3049 |
|
---|
3050 | if (pImage)
|
---|
3051 | {
|
---|
3052 | unsigned cb = strlen(pImage->szFilename);
|
---|
3053 | if (cb < cbFilename)
|
---|
3054 | {
|
---|
3055 | /* memcpy is much better than strncpy. */
|
---|
3056 | memcpy(pszFilename, pImage->szFilename, cb + 1);
|
---|
3057 | LogFlow(("VDIDiskGetImageFilename: returns VINF_SUCCESS, filename=\"%s\" nImage=%d\n",
|
---|
3058 | pszFilename, nImage));
|
---|
3059 | return VINF_SUCCESS;
|
---|
3060 | }
|
---|
3061 | else
|
---|
3062 | {
|
---|
3063 | AssertMsgFailed(("Out of buffer space, cbFilename=%d needed=%d\n", cbFilename, cb + 1));
|
---|
3064 | return VERR_BUFFER_OVERFLOW;
|
---|
3065 | }
|
---|
3066 | }
|
---|
3067 |
|
---|
3068 | AssertMsgFailed(("Image %d was not found (cImages=%d)\n", nImage, pDisk->cImages));
|
---|
3069 | return VERR_VDI_IMAGE_NOT_FOUND;
|
---|
3070 | }
|
---|
3071 |
|
---|
3072 | /**
|
---|
3073 | * Get the comment line of opened image of HDD container.
|
---|
3074 | *
|
---|
3075 | * @returns VBox status code.
|
---|
3076 | * @returns VERR_VDI_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
3077 | * @returns VERR_BUFFER_OVERFLOW if pszComment buffer too small to hold comment text.
|
---|
3078 | * @param pDisk Pointer to VDI HDD container.
|
---|
3079 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
3080 | * @param pszComment Where to store the comment string of image. NULL is ok.
|
---|
3081 | * @param cbComment The size of pszComment buffer. 0 is ok.
|
---|
3082 | */
|
---|
3083 | VBOXDDU_DECL(int) VDIDiskGetImageComment(PVDIDISK pDisk, int nImage, char *pszComment, unsigned cbComment)
|
---|
3084 | {
|
---|
3085 | /* sanity check */
|
---|
3086 | Assert(pDisk);
|
---|
3087 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3088 | Assert(pszComment);
|
---|
3089 |
|
---|
3090 | PVDIIMAGEDESC pImage = vdiGetImageByNumber(pDisk, nImage);
|
---|
3091 | Assert(pImage);
|
---|
3092 |
|
---|
3093 | if (pImage)
|
---|
3094 | {
|
---|
3095 | char *pszTmp = getImageComment(&pImage->Header);
|
---|
3096 | unsigned cb = strlen(pszTmp);
|
---|
3097 | if (cb < cbComment)
|
---|
3098 | {
|
---|
3099 | /* memcpy is much better than strncpy. */
|
---|
3100 | memcpy(pszComment, pszTmp, cb + 1);
|
---|
3101 | LogFlow(("VDIDiskGetImageComment: returns VINF_SUCCESS, comment=\"%s\" nImage=%d\n",
|
---|
3102 | pszTmp, nImage));
|
---|
3103 | return VINF_SUCCESS;
|
---|
3104 | }
|
---|
3105 | else
|
---|
3106 | {
|
---|
3107 | AssertMsgFailed(("Out of buffer space, cbComment=%d needed=%d\n", cbComment, cb + 1));
|
---|
3108 | return VERR_BUFFER_OVERFLOW;
|
---|
3109 | }
|
---|
3110 | }
|
---|
3111 |
|
---|
3112 | AssertMsgFailed(("Image %d was not found (cImages=%d)\n", nImage, pDisk->cImages));
|
---|
3113 | return VERR_VDI_IMAGE_NOT_FOUND;
|
---|
3114 | }
|
---|
3115 |
|
---|
3116 | /**
|
---|
3117 | * Get type of opened image of HDD container.
|
---|
3118 | *
|
---|
3119 | * @returns VBox status code.
|
---|
3120 | * @returns VERR_VDI_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
3121 | * @param pDisk Pointer to VDI HDD container.
|
---|
3122 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
3123 | * @param penmType Where to store the image type.
|
---|
3124 | */
|
---|
3125 | VBOXDDU_DECL(int) VDIDiskGetImageType(PVDIDISK pDisk, int nImage, PVDIIMAGETYPE penmType)
|
---|
3126 | {
|
---|
3127 | /* sanity check */
|
---|
3128 | Assert(pDisk);
|
---|
3129 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3130 | Assert(penmType);
|
---|
3131 |
|
---|
3132 | PVDIIMAGEDESC pImage = vdiGetImageByNumber(pDisk, nImage);
|
---|
3133 | Assert(pImage);
|
---|
3134 |
|
---|
3135 | if (pImage)
|
---|
3136 | {
|
---|
3137 | *penmType = getImageType(&pImage->Header);
|
---|
3138 | LogFlow(("VDIDiskGetImageType: returns VINF_SUCCESS, type=%X nImage=%d\n",
|
---|
3139 | *penmType, nImage));
|
---|
3140 | return VINF_SUCCESS;
|
---|
3141 | }
|
---|
3142 |
|
---|
3143 | AssertMsgFailed(("Image %d was not found (cImages=%d)\n", nImage, pDisk->cImages));
|
---|
3144 | return VERR_VDI_IMAGE_NOT_FOUND;
|
---|
3145 | }
|
---|
3146 |
|
---|
3147 | /**
|
---|
3148 | * Get flags of opened image of HDD container.
|
---|
3149 | *
|
---|
3150 | * @returns VBox status code.
|
---|
3151 | * @returns VERR_VDI_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
3152 | * @param pDisk Pointer to VDI HDD container.
|
---|
3153 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
3154 | * @param pfFlags Where to store the image flags.
|
---|
3155 | */
|
---|
3156 | VBOXDDU_DECL(int) VDIDiskGetImageFlags(PVDIDISK pDisk, int nImage, unsigned *pfFlags)
|
---|
3157 | {
|
---|
3158 | /* sanity check */
|
---|
3159 | Assert(pDisk);
|
---|
3160 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3161 | Assert(pfFlags);
|
---|
3162 |
|
---|
3163 | PVDIIMAGEDESC pImage = vdiGetImageByNumber(pDisk, nImage);
|
---|
3164 | Assert(pImage);
|
---|
3165 |
|
---|
3166 | if (pImage)
|
---|
3167 | {
|
---|
3168 | *pfFlags = getImageFlags(&pImage->Header);
|
---|
3169 | LogFlow(("VDIDiskGetImageFlags: returns VINF_SUCCESS, flags=%08X nImage=%d\n",
|
---|
3170 | *pfFlags, nImage));
|
---|
3171 | return VINF_SUCCESS;
|
---|
3172 | }
|
---|
3173 |
|
---|
3174 | AssertMsgFailed(("Image %d was not found (cImages=%d)\n", nImage, pDisk->cImages));
|
---|
3175 | return VERR_VDI_IMAGE_NOT_FOUND;
|
---|
3176 | }
|
---|
3177 |
|
---|
3178 | /**
|
---|
3179 | * Get Uuid of opened image of HDD container.
|
---|
3180 | *
|
---|
3181 | * @returns VBox status code.
|
---|
3182 | * @returns VERR_VDI_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
3183 | * @param pDisk Pointer to VDI HDD container.
|
---|
3184 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
3185 | * @param pUuid Where to store the image creation uuid.
|
---|
3186 | */
|
---|
3187 | VBOXDDU_DECL(int) VDIDiskGetImageUuid(PVDIDISK pDisk, int nImage, PRTUUID pUuid)
|
---|
3188 | {
|
---|
3189 | /* sanity check */
|
---|
3190 | Assert(pDisk);
|
---|
3191 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3192 | Assert(pUuid);
|
---|
3193 |
|
---|
3194 | PVDIIMAGEDESC pImage = vdiGetImageByNumber(pDisk, nImage);
|
---|
3195 | Assert(pImage);
|
---|
3196 |
|
---|
3197 | if (pImage)
|
---|
3198 | {
|
---|
3199 | *pUuid = *getImageCreationUUID(&pImage->Header);
|
---|
3200 | LogFlow(("VDIDiskGetImageUuid: returns VINF_SUCCESS, uuid={%Vuuid} nImage=%d\n",
|
---|
3201 | pUuid, nImage));
|
---|
3202 | return VINF_SUCCESS;
|
---|
3203 | }
|
---|
3204 |
|
---|
3205 | AssertMsgFailed(("Image %d was not found (cImages=%d)\n", nImage, pDisk->cImages));
|
---|
3206 | return VERR_VDI_IMAGE_NOT_FOUND;
|
---|
3207 | }
|
---|
3208 |
|
---|
3209 | /**
|
---|
3210 | * Get last modification Uuid of opened image of HDD container.
|
---|
3211 | *
|
---|
3212 | * @returns VBox status code.
|
---|
3213 | * @returns VERR_VDI_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
3214 | * @param pDisk Pointer to VDI HDD container.
|
---|
3215 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
3216 | * @param pUuid Where to store the image modification uuid.
|
---|
3217 | */
|
---|
3218 | VBOXDDU_DECL(int) VDIDiskGetImageModificationUuid(PVDIDISK pDisk, int nImage, PRTUUID pUuid)
|
---|
3219 | {
|
---|
3220 | /* sanity check */
|
---|
3221 | Assert(pDisk);
|
---|
3222 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3223 | Assert(pUuid);
|
---|
3224 |
|
---|
3225 | PVDIIMAGEDESC pImage = vdiGetImageByNumber(pDisk, nImage);
|
---|
3226 | Assert(pImage);
|
---|
3227 |
|
---|
3228 | if (pImage)
|
---|
3229 | {
|
---|
3230 | *pUuid = *getImageModificationUUID(&pImage->Header);
|
---|
3231 | LogFlow(("VDIDiskGetImageModificationUuid: returns VINF_SUCCESS, uuid={%Vuuid} nImage=%d\n",
|
---|
3232 | pUuid, nImage));
|
---|
3233 | return VINF_SUCCESS;
|
---|
3234 | }
|
---|
3235 |
|
---|
3236 | AssertMsgFailed(("Image %d was not found (cImages=%d)\n", nImage, pDisk->cImages));
|
---|
3237 | return VERR_VDI_IMAGE_NOT_FOUND;
|
---|
3238 | }
|
---|
3239 |
|
---|
3240 | /**
|
---|
3241 | * Get Uuid of opened image's parent image.
|
---|
3242 | *
|
---|
3243 | * @returns VBox status code.
|
---|
3244 | * @returns VERR_VDI_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
3245 | * @param pDisk Pointer to VDI HDD container.
|
---|
3246 | * @param nImage Image number, counts from 0. 0 is always base image of the container.
|
---|
3247 | * @param pUuid Where to store the image creation uuid.
|
---|
3248 | */
|
---|
3249 | VBOXDDU_DECL(int) VDIDiskGetParentImageUuid(PVDIDISK pDisk, int nImage, PRTUUID pUuid)
|
---|
3250 | {
|
---|
3251 | /* sanity check */
|
---|
3252 | Assert(pDisk);
|
---|
3253 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3254 | Assert(pUuid);
|
---|
3255 |
|
---|
3256 | PVDIIMAGEDESC pImage = vdiGetImageByNumber(pDisk, nImage);
|
---|
3257 | if (pImage)
|
---|
3258 | {
|
---|
3259 | *pUuid = *getImageParentUUID(&pImage->Header);
|
---|
3260 | LogFlow(("VDIDiskGetParentImageUuid: returns VINF_SUCCESS, *pUuid={%Vuuid} nImage=%d\n",
|
---|
3261 | pUuid, nImage));
|
---|
3262 | return VINF_SUCCESS;
|
---|
3263 | }
|
---|
3264 |
|
---|
3265 | AssertMsgFailed(("Image %d was not found (cImages=%d)\n", nImage, pDisk->cImages));
|
---|
3266 | return VERR_VDI_IMAGE_NOT_FOUND;
|
---|
3267 | }
|
---|
3268 |
|
---|
3269 | /**
|
---|
3270 | * Relock the image as read/write or read-only.
|
---|
3271 | */
|
---|
3272 | int vdiChangeImageMode(PVDIIMAGEDESC pImage, bool fReadOnly)
|
---|
3273 | {
|
---|
3274 | Assert(pImage);
|
---|
3275 |
|
---|
3276 | if ( !fReadOnly
|
---|
3277 | && pImage->fOpen & VDI_OPEN_FLAGS_READONLY)
|
---|
3278 | {
|
---|
3279 | /* Can't switch read-only opened image to read-write mode. */
|
---|
3280 | Log(("vdiChangeImageMode: can't switch r/o image to r/w mode, filename=\"%s\" fOpen=%X\n",
|
---|
3281 | pImage->szFilename, pImage->fOpen));
|
---|
3282 | return VERR_VDI_IMAGE_READ_ONLY;
|
---|
3283 | }
|
---|
3284 |
|
---|
3285 | /* Flush last image changes if was r/w mode. */
|
---|
3286 | vdiFlushImage(pImage);
|
---|
3287 |
|
---|
3288 | /* Change image locking. */
|
---|
3289 | uint64_t cbLock = pImage->offStartData
|
---|
3290 | + ((uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset);
|
---|
3291 | int rc = RTFileChangeLock(pImage->File,
|
---|
3292 | (fReadOnly) ?
|
---|
3293 | RTFILE_LOCK_READ | RTFILE_LOCK_IMMEDIATELY :
|
---|
3294 | RTFILE_LOCK_WRITE | RTFILE_LOCK_IMMEDIATELY,
|
---|
3295 | 0,
|
---|
3296 | cbLock);
|
---|
3297 | if (VBOX_SUCCESS(rc))
|
---|
3298 | {
|
---|
3299 | pImage->fReadOnly = fReadOnly;
|
---|
3300 | Log(("vdiChangeImageMode: Image \"%s\" mode changed to %s\n",
|
---|
3301 | pImage->szFilename, (pImage->fReadOnly) ? "read-only" : "read/write"));
|
---|
3302 | return VINF_SUCCESS;
|
---|
3303 | }
|
---|
3304 |
|
---|
3305 | /* Check for the most bad error in the world. Damn! It must never happens in real life! */
|
---|
3306 | if (rc == VERR_FILE_LOCK_LOST)
|
---|
3307 | {
|
---|
3308 | /* And what we can do now?! */
|
---|
3309 | AssertMsgFailed(("Image lock has been lost for file=\"%s\"", pImage->szFilename));
|
---|
3310 | Log(("vdiChangeImageMode: image lock has been lost for file=\"%s\", blocking on r/o lock wait",
|
---|
3311 | pImage->szFilename));
|
---|
3312 |
|
---|
3313 | /* Try to obtain read lock in blocking mode. Maybe it's a very bad method. */
|
---|
3314 | rc = RTFileLock(pImage->File, RTFILE_LOCK_READ | RTFILE_LOCK_WAIT, 0, cbLock);
|
---|
3315 | AssertReleaseRC(rc);
|
---|
3316 |
|
---|
3317 | pImage->fReadOnly = false;
|
---|
3318 | if (pImage->fReadOnly != fReadOnly)
|
---|
3319 | rc = VERR_FILE_LOCK_VIOLATION;
|
---|
3320 | }
|
---|
3321 |
|
---|
3322 | Log(("vdiChangeImageMode: Image \"%s\" mode change failed with rc=%Vrc, mode is %s\n",
|
---|
3323 | pImage->szFilename, rc, (pImage->fReadOnly) ? "read-only" : "read/write"));
|
---|
3324 |
|
---|
3325 | return rc;
|
---|
3326 | }
|
---|
3327 |
|
---|
3328 | /**
|
---|
3329 | * internal: try to save header in image file even if image is in read-only mode.
|
---|
3330 | */
|
---|
3331 | static int vdiUpdateReadOnlyHeader(PVDIIMAGEDESC pImage)
|
---|
3332 | {
|
---|
3333 | int rc = VINF_SUCCESS;
|
---|
3334 |
|
---|
3335 | if (pImage->fReadOnly)
|
---|
3336 | {
|
---|
3337 | rc = vdiChangeImageMode(pImage, false);
|
---|
3338 | if (VBOX_SUCCESS(rc))
|
---|
3339 | {
|
---|
3340 | vdiFlushImage(pImage);
|
---|
3341 | rc = vdiChangeImageMode(pImage, true);
|
---|
3342 | AssertReleaseRC(rc);
|
---|
3343 | }
|
---|
3344 | }
|
---|
3345 | else
|
---|
3346 | vdiFlushImage(pImage);
|
---|
3347 |
|
---|
3348 | return rc;
|
---|
3349 | }
|
---|
3350 |
|
---|
3351 | /**
|
---|
3352 | * Opens an image file.
|
---|
3353 | *
|
---|
3354 | * The first opened image file in a HDD container must have a base image type,
|
---|
3355 | * others (next opened images) must be a differencing or undo images.
|
---|
3356 | * Linkage is checked for differencing image to be in consistence with the previously opened image.
|
---|
3357 | * When a next differencing image is opened and the last image was opened in read/write access
|
---|
3358 | * mode, then the last image is reopened in read-only with deny write sharing mode. This allows
|
---|
3359 | * other processes to use images in read-only mode too.
|
---|
3360 | *
|
---|
3361 | * Note that the image can be opened in read-only mode if a read/write open is not possible.
|
---|
3362 | * Use VDIDiskIsReadOnly to check open mode.
|
---|
3363 | *
|
---|
3364 | * @returns VBox status code.
|
---|
3365 | * @param pDisk Pointer to VDI HDD container.
|
---|
3366 | * @param pszFilename Name of the image file to open.
|
---|
3367 | * @param fOpen Image file open mode, see VDI_OPEN_FLAGS_* constants.
|
---|
3368 | */
|
---|
3369 | VBOXDDU_DECL(int) VDIDiskOpenImage(PVDIDISK pDisk, const char *pszFilename, unsigned fOpen)
|
---|
3370 | {
|
---|
3371 | /* sanity check */
|
---|
3372 | Assert(pDisk);
|
---|
3373 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3374 |
|
---|
3375 | /* Check arguments. */
|
---|
3376 | if ( !pszFilename
|
---|
3377 | || *pszFilename == '\0'
|
---|
3378 | || (fOpen & ~VDI_OPEN_FLAGS_MASK))
|
---|
3379 | {
|
---|
3380 | AssertMsgFailed(("Invalid arguments: pszFilename=%p fOpen=%x\n", pszFilename, fOpen));
|
---|
3381 | return VERR_INVALID_PARAMETER;
|
---|
3382 | }
|
---|
3383 | LogFlow(("VDIDiskOpenImage: pszFilename=\"%s\" fOpen=%X\n", pszFilename, fOpen));
|
---|
3384 |
|
---|
3385 | PVDIIMAGEDESC pImage;
|
---|
3386 | int rc = vdiOpenImage(&pImage, pszFilename, fOpen, pDisk->pLast);
|
---|
3387 | if (VBOX_SUCCESS(rc))
|
---|
3388 | {
|
---|
3389 | if (pDisk->pLast)
|
---|
3390 | {
|
---|
3391 | /* Opening differencing image. */
|
---|
3392 | if (!pDisk->pLast->fReadOnly)
|
---|
3393 | {
|
---|
3394 | /*
|
---|
3395 | * Previous image is opened in read/write mode -> switch it into read-only.
|
---|
3396 | */
|
---|
3397 | rc = vdiChangeImageMode(pDisk->pLast, true);
|
---|
3398 | }
|
---|
3399 | }
|
---|
3400 | else
|
---|
3401 | {
|
---|
3402 | /* Opening base image, check its type. */
|
---|
3403 | if ( getImageType(&pImage->Header) != VDI_IMAGE_TYPE_NORMAL
|
---|
3404 | && getImageType(&pImage->Header) != VDI_IMAGE_TYPE_FIXED)
|
---|
3405 | {
|
---|
3406 | rc = VERR_VDI_INVALID_TYPE;
|
---|
3407 | }
|
---|
3408 | }
|
---|
3409 |
|
---|
3410 | if (VBOX_SUCCESS(rc))
|
---|
3411 | vdiAddImageToList(pDisk, pImage);
|
---|
3412 | else
|
---|
3413 | vdiCloseImage(pImage);
|
---|
3414 | }
|
---|
3415 |
|
---|
3416 | LogFlow(("VDIDiskOpenImage: returns %Vrc\n", rc));
|
---|
3417 | return rc;
|
---|
3418 | }
|
---|
3419 |
|
---|
3420 | /**
|
---|
3421 | * Closes the last opened image file in the HDD container. Leaves all changes inside it.
|
---|
3422 | * If previous image file was opened in read-only mode (that is normal) and closing image
|
---|
3423 | * was opened in read-write mode (the whole disk was in read-write mode) - the previous image
|
---|
3424 | * will be reopened in read/write mode.
|
---|
3425 | *
|
---|
3426 | * @param pDisk Pointer to VDI HDD container.
|
---|
3427 | */
|
---|
3428 | VBOXDDU_DECL(void) VDIDiskCloseImage(PVDIDISK pDisk)
|
---|
3429 | {
|
---|
3430 | /* sanity check */
|
---|
3431 | Assert(pDisk);
|
---|
3432 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3433 |
|
---|
3434 | PVDIIMAGEDESC pImage = pDisk->pLast;
|
---|
3435 | if (pImage)
|
---|
3436 | {
|
---|
3437 | LogFlow(("VDIDiskCloseImage: closing image \"%s\"\n", pImage->szFilename));
|
---|
3438 |
|
---|
3439 | bool fWasReadOnly = pImage->fReadOnly;
|
---|
3440 | vdiRemoveImageFromList(pDisk, pImage);
|
---|
3441 | vdiCloseImage(pImage);
|
---|
3442 |
|
---|
3443 | if ( !fWasReadOnly
|
---|
3444 | && pDisk->pLast
|
---|
3445 | && pDisk->pLast->fReadOnly
|
---|
3446 | && !(pDisk->pLast->fOpen & VDI_OPEN_FLAGS_READONLY))
|
---|
3447 | {
|
---|
3448 | /*
|
---|
3449 | * Closed image was opened in read/write mode, previous image was opened
|
---|
3450 | * in read-only mode, try to switch it into read/write.
|
---|
3451 | */
|
---|
3452 | int rc = vdiChangeImageMode(pDisk->pLast, false);
|
---|
3453 | NOREF(rc); /* gcc still hates unused variables... */
|
---|
3454 | }
|
---|
3455 |
|
---|
3456 | return;
|
---|
3457 | }
|
---|
3458 | AssertMsgFailed(("No images to close\n"));
|
---|
3459 | }
|
---|
3460 |
|
---|
3461 | /**
|
---|
3462 | * Closes all opened image files in HDD container.
|
---|
3463 | *
|
---|
3464 | * @param pDisk Pointer to VDI HDD container.
|
---|
3465 | */
|
---|
3466 | VBOXDDU_DECL(void) VDIDiskCloseAllImages(PVDIDISK pDisk)
|
---|
3467 | {
|
---|
3468 | LogFlow(("VDIDiskCloseAllImages:\n"));
|
---|
3469 | /* sanity check */
|
---|
3470 | Assert(pDisk);
|
---|
3471 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3472 |
|
---|
3473 | PVDIIMAGEDESC pImage = pDisk->pLast;
|
---|
3474 | while (pImage)
|
---|
3475 | {
|
---|
3476 | PVDIIMAGEDESC pPrev = pImage->pPrev;
|
---|
3477 | vdiRemoveImageFromList(pDisk, pImage);
|
---|
3478 | vdiCloseImage(pImage);
|
---|
3479 | pImage = pPrev;
|
---|
3480 | }
|
---|
3481 | Assert(pDisk->pLast == NULL);
|
---|
3482 | }
|
---|
3483 |
|
---|
3484 | /**
|
---|
3485 | * Commits last opened differencing/undo image file of HDD container to previous one.
|
---|
3486 | * If previous image file was opened in read-only mode (that must be always so) it is reopened
|
---|
3487 | * as read/write to do commit operation.
|
---|
3488 | * After successfull commit the previous image file again reopened in read-only mode, last opened
|
---|
3489 | * image file is cleared of data and remains open and active in HDD container.
|
---|
3490 | * If you want to delete image after commit you must do it manually by VDIDiskCloseImage and
|
---|
3491 | * VDIDeleteImage calls.
|
---|
3492 | *
|
---|
3493 | * Note that in case of unrecoverable error all images of HDD container will be closed.
|
---|
3494 | *
|
---|
3495 | * @returns VBox status code.
|
---|
3496 | * @param pDisk Pointer to VDI HDD container.
|
---|
3497 | * @param pfnProgress Progress callback. Optional.
|
---|
3498 | * @param pvUser User argument for the progress callback.
|
---|
3499 | * @remark Only used by tstVDI.
|
---|
3500 | */
|
---|
3501 | VBOXDDU_DECL(int) VDIDiskCommitLastDiff(PVDIDISK pDisk, PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
3502 | {
|
---|
3503 | LogFlow(("VDIDiskCommitLastDiff:\n"));
|
---|
3504 | /* sanity check */
|
---|
3505 | Assert(pDisk);
|
---|
3506 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3507 |
|
---|
3508 | int rc = VINF_SUCCESS;
|
---|
3509 | PVDIIMAGEDESC pImage = pDisk->pLast;
|
---|
3510 | if (!pImage)
|
---|
3511 | {
|
---|
3512 | AssertMsgFailed(("No one disk image is opened!\n"));
|
---|
3513 | return VERR_VDI_NOT_OPENED;
|
---|
3514 | }
|
---|
3515 |
|
---|
3516 | if (pImage->fReadOnly)
|
---|
3517 | {
|
---|
3518 | AssertMsgFailed(("Image \"%s\" is read-only!\n", pImage->szFilename));
|
---|
3519 | return VERR_VDI_IMAGE_READ_ONLY;
|
---|
3520 | }
|
---|
3521 |
|
---|
3522 | if (!pImage->pPrev)
|
---|
3523 | {
|
---|
3524 | AssertMsgFailed(("No images to commit to!\n"));
|
---|
3525 | return VERR_VDI_NO_DIFF_IMAGES;
|
---|
3526 | }
|
---|
3527 |
|
---|
3528 | bool fWasReadOnly = pImage->pPrev->fReadOnly;
|
---|
3529 | if (fWasReadOnly)
|
---|
3530 | {
|
---|
3531 | /* Change previous image mode to r/w. */
|
---|
3532 | rc = vdiChangeImageMode(pImage->pPrev, false);
|
---|
3533 | if (VBOX_FAILURE(rc))
|
---|
3534 | {
|
---|
3535 | Log(("VDIDiskCommitLastDiff: can't switch previous image into r/w mode, rc=%Vrc\n", rc));
|
---|
3536 | return rc;
|
---|
3537 | }
|
---|
3538 | }
|
---|
3539 |
|
---|
3540 | rc = vdiCommitToImage(pDisk, pImage->pPrev, pfnProgress, pvUser);
|
---|
3541 | if (VBOX_SUCCESS(rc) && fWasReadOnly)
|
---|
3542 | {
|
---|
3543 | /* Change previous image mode back to r/o. */
|
---|
3544 | rc = vdiChangeImageMode(pImage->pPrev, true);
|
---|
3545 | }
|
---|
3546 |
|
---|
3547 | if (VBOX_FAILURE(rc))
|
---|
3548 | {
|
---|
3549 | /* Failed! Close all images, can't work with VHDD at all. */
|
---|
3550 | VDIDiskCloseAllImages(pDisk);
|
---|
3551 | AssertMsgFailed(("Fatal: commit failed, rc=%Vrc\n", rc));
|
---|
3552 | }
|
---|
3553 |
|
---|
3554 | return rc;
|
---|
3555 | }
|
---|
3556 |
|
---|
3557 | /**
|
---|
3558 | * Creates and opens a new differencing image file in HDD container.
|
---|
3559 | * See comments for VDIDiskOpenImage function about differencing images.
|
---|
3560 | *
|
---|
3561 | * @returns VBox status code.
|
---|
3562 | * @param pDisk Pointer to VDI HDD container.
|
---|
3563 | * @param pszFilename Name of the image file to create and open.
|
---|
3564 | * @param pszComment Pointer to image comment. NULL is ok.
|
---|
3565 | * @param pfnProgress Progress callback. Optional.
|
---|
3566 | * @param pvUser User argument for the progress callback.
|
---|
3567 | * @remark Only used by tstVDI.
|
---|
3568 | */
|
---|
3569 | VBOXDDU_DECL(int) VDIDiskCreateOpenDifferenceImage(PVDIDISK pDisk, const char *pszFilename,
|
---|
3570 | const char *pszComment, PFNVMPROGRESS pfnProgress,
|
---|
3571 | void *pvUser)
|
---|
3572 | {
|
---|
3573 | LogFlow(("VDIDiskCreateOpenDifferenceImage:\n"));
|
---|
3574 | /* sanity check */
|
---|
3575 | Assert(pDisk);
|
---|
3576 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3577 | Assert(pszFilename);
|
---|
3578 |
|
---|
3579 | if (!pDisk->pLast)
|
---|
3580 | {
|
---|
3581 | AssertMsgFailed(("No one disk image is opened!\n"));
|
---|
3582 | return VERR_VDI_NOT_OPENED;
|
---|
3583 | }
|
---|
3584 |
|
---|
3585 | /* Flush last parent image changes if possible. */
|
---|
3586 | vdiFlushImage(pDisk->pLast);
|
---|
3587 |
|
---|
3588 | int rc = vdiCreateImage(pszFilename,
|
---|
3589 | VDI_IMAGE_TYPE_DIFF,
|
---|
3590 | VDI_IMAGE_FLAGS_DEFAULT,
|
---|
3591 | getImageDiskSize(&pDisk->pLast->Header),
|
---|
3592 | pszComment,
|
---|
3593 | pDisk->pLast,
|
---|
3594 | pfnProgress, pvUser);
|
---|
3595 | if (VBOX_SUCCESS(rc))
|
---|
3596 | {
|
---|
3597 | rc = VDIDiskOpenImage(pDisk, pszFilename, VDI_OPEN_FLAGS_NORMAL);
|
---|
3598 | if (VBOX_FAILURE(rc))
|
---|
3599 | VDIDeleteImage(pszFilename);
|
---|
3600 | }
|
---|
3601 | LogFlow(("VDIDiskCreateOpenDifferenceImage: returns %Vrc, filename=\"%s\"\n", rc, pszFilename));
|
---|
3602 | return rc;
|
---|
3603 | }
|
---|
3604 |
|
---|
3605 | /**
|
---|
3606 | * internal: debug image dump.
|
---|
3607 | *
|
---|
3608 | * @remark Only used by tstVDI.
|
---|
3609 | */
|
---|
3610 | static void vdiDumpImage(PVDIIMAGEDESC pImage)
|
---|
3611 | {
|
---|
3612 | RTLogPrintf("Dumping VDI image \"%s\" mode=%s fOpen=%X File=%08X\n",
|
---|
3613 | pImage->szFilename,
|
---|
3614 | (pImage->fReadOnly) ? "r/o" : "r/w",
|
---|
3615 | pImage->fOpen,
|
---|
3616 | pImage->File);
|
---|
3617 | RTLogPrintf("Header: Version=%08X Type=%X Flags=%X Size=%llu\n",
|
---|
3618 | pImage->PreHeader.u32Version,
|
---|
3619 | getImageType(&pImage->Header),
|
---|
3620 | getImageFlags(&pImage->Header),
|
---|
3621 | getImageDiskSize(&pImage->Header));
|
---|
3622 | RTLogPrintf("Header: cbBlock=%u cbBlockExtra=%u cBlocks=%u cBlocksAllocated=%u\n",
|
---|
3623 | getImageBlockSize(&pImage->Header),
|
---|
3624 | getImageExtraBlockSize(&pImage->Header),
|
---|
3625 | getImageBlocks(&pImage->Header),
|
---|
3626 | getImageBlocksAllocated(&pImage->Header));
|
---|
3627 | RTLogPrintf("Header: offBlocks=%u offData=%u\n",
|
---|
3628 | getImageBlocksOffset(&pImage->Header),
|
---|
3629 | getImageDataOffset(&pImage->Header));
|
---|
3630 | PVDIDISKGEOMETRY pg = getImageGeometry(&pImage->Header);
|
---|
3631 | RTLogPrintf("Header: Geometry: C/H/S=%u/%u/%u cbSector=%u Mode=%u\n",
|
---|
3632 | pg->cCylinders, pg->cHeads, pg->cSectors, pg->cbSector,
|
---|
3633 | getImageTranslation(&pImage->Header));
|
---|
3634 | RTLogPrintf("Header: uuidCreation={%Vuuid}\n", getImageCreationUUID(&pImage->Header));
|
---|
3635 | RTLogPrintf("Header: uuidModification={%Vuuid}\n", getImageModificationUUID(&pImage->Header));
|
---|
3636 | RTLogPrintf("Header: uuidParent={%Vuuid}\n", getImageParentUUID(&pImage->Header));
|
---|
3637 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) >= 1)
|
---|
3638 | RTLogPrintf("Header: uuidParentModification={%Vuuid}\n", getImageParentModificationUUID(&pImage->Header));
|
---|
3639 | RTLogPrintf("Image: fFlags=%08X offStartBlocks=%u offStartData=%u\n",
|
---|
3640 | pImage->fFlags, pImage->offStartBlocks, pImage->offStartData);
|
---|
3641 | RTLogPrintf("Image: uBlockMask=%08X uShiftIndex2Offset=%u uShiftOffset2Index=%u offStartBlockData=%u\n",
|
---|
3642 | pImage->uBlockMask,
|
---|
3643 | pImage->uShiftIndex2Offset,
|
---|
3644 | pImage->uShiftOffset2Index,
|
---|
3645 | pImage->offStartBlockData);
|
---|
3646 |
|
---|
3647 | unsigned uBlock, cBlocksNotFree, cBadBlocks, cBlocks = getImageBlocks(&pImage->Header);
|
---|
3648 | for (uBlock=0, cBlocksNotFree=0, cBadBlocks=0; uBlock<cBlocks; uBlock++)
|
---|
3649 | {
|
---|
3650 | if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
|
---|
3651 | {
|
---|
3652 | cBlocksNotFree++;
|
---|
3653 | if (pImage->paBlocks[uBlock] >= cBlocks)
|
---|
3654 | cBadBlocks++;
|
---|
3655 | }
|
---|
3656 | }
|
---|
3657 | if (cBlocksNotFree != getImageBlocksAllocated(&pImage->Header))
|
---|
3658 | {
|
---|
3659 | RTLogPrintf("!! WARNING: %u blocks actually allocated (cBlocksAllocated=%u) !!\n",
|
---|
3660 | cBlocksNotFree, getImageBlocksAllocated(&pImage->Header));
|
---|
3661 | }
|
---|
3662 | if (cBadBlocks)
|
---|
3663 | {
|
---|
3664 | RTLogPrintf("!! WARNING: %u bad blocks found !!\n",
|
---|
3665 | cBadBlocks);
|
---|
3666 | }
|
---|
3667 | }
|
---|
3668 |
|
---|
3669 | /**
|
---|
3670 | * Debug helper - dumps all opened images of HDD container into the log file.
|
---|
3671 | *
|
---|
3672 | * @param pDisk Pointer to VDI HDD container.
|
---|
3673 | * @remark Only used by tstVDI and vditool
|
---|
3674 | */
|
---|
3675 | VBOXDDU_DECL(void) VDIDiskDumpImages(PVDIDISK pDisk)
|
---|
3676 | {
|
---|
3677 | /* sanity check */
|
---|
3678 | Assert(pDisk);
|
---|
3679 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3680 |
|
---|
3681 | RTLogPrintf("--- Dumping VDI Disk, Images=%u\n", pDisk->cImages);
|
---|
3682 | for (PVDIIMAGEDESC pImage = pDisk->pBase; pImage; pImage = pImage->pNext)
|
---|
3683 | vdiDumpImage(pImage);
|
---|
3684 | }
|
---|
3685 |
|
---|