1 | /* $Id: VBoxManageDisk.cpp 40503 2012-03-16 16:24:43Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxManage - The disk related commands.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2012 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef VBOX_ONLY_DOCS
|
---|
19 |
|
---|
20 | /*******************************************************************************
|
---|
21 | * Header Files *
|
---|
22 | *******************************************************************************/
|
---|
23 | #include <VBox/com/com.h>
|
---|
24 | #include <VBox/com/array.h>
|
---|
25 | #include <VBox/com/ErrorInfo.h>
|
---|
26 | #include <VBox/com/errorprint.h>
|
---|
27 | #include <VBox/com/VirtualBox.h>
|
---|
28 |
|
---|
29 | #include <iprt/asm.h>
|
---|
30 | #include <iprt/file.h>
|
---|
31 | #include <iprt/path.h>
|
---|
32 | #include <iprt/param.h>
|
---|
33 | #include <iprt/stream.h>
|
---|
34 | #include <iprt/string.h>
|
---|
35 | #include <iprt/ctype.h>
|
---|
36 | #include <iprt/getopt.h>
|
---|
37 | #include <VBox/log.h>
|
---|
38 | #include <VBox/vd.h>
|
---|
39 |
|
---|
40 | #include "VBoxManage.h"
|
---|
41 | using namespace com;
|
---|
42 |
|
---|
43 |
|
---|
44 | // funcs
|
---|
45 | ///////////////////////////////////////////////////////////////////////////////
|
---|
46 |
|
---|
47 |
|
---|
48 | static DECLCALLBACK(void) handleVDError(void *pvUser, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va)
|
---|
49 | {
|
---|
50 | RTMsgError(pszFormat, va);
|
---|
51 | RTMsgError("Error code %Rrc at %s(%u) in function %s", rc, RT_SRC_POS_ARGS);
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | static int parseDiskVariant(const char *psz, MediumVariant_T *pDiskVariant)
|
---|
56 | {
|
---|
57 | int rc = VINF_SUCCESS;
|
---|
58 | unsigned DiskVariant = (unsigned)(*pDiskVariant);
|
---|
59 | while (psz && *psz && RT_SUCCESS(rc))
|
---|
60 | {
|
---|
61 | size_t len;
|
---|
62 | const char *pszComma = strchr(psz, ',');
|
---|
63 | if (pszComma)
|
---|
64 | len = pszComma - psz;
|
---|
65 | else
|
---|
66 | len = strlen(psz);
|
---|
67 | if (len > 0)
|
---|
68 | {
|
---|
69 | // Parsing is intentionally inconsistent: "standard" resets the
|
---|
70 | // variant, whereas the other flags are cumulative.
|
---|
71 | if (!RTStrNICmp(psz, "standard", len))
|
---|
72 | DiskVariant = MediumVariant_Standard;
|
---|
73 | else if ( !RTStrNICmp(psz, "fixed", len)
|
---|
74 | || !RTStrNICmp(psz, "static", len))
|
---|
75 | DiskVariant |= MediumVariant_Fixed;
|
---|
76 | else if (!RTStrNICmp(psz, "Diff", len))
|
---|
77 | DiskVariant |= MediumVariant_Diff;
|
---|
78 | else if (!RTStrNICmp(psz, "split2g", len))
|
---|
79 | DiskVariant |= MediumVariant_VmdkSplit2G;
|
---|
80 | else if ( !RTStrNICmp(psz, "stream", len)
|
---|
81 | || !RTStrNICmp(psz, "streamoptimized", len))
|
---|
82 | DiskVariant |= MediumVariant_VmdkStreamOptimized;
|
---|
83 | else if (!RTStrNICmp(psz, "esx", len))
|
---|
84 | DiskVariant |= MediumVariant_VmdkESX;
|
---|
85 | else
|
---|
86 | rc = VERR_PARSE_ERROR;
|
---|
87 | }
|
---|
88 | if (pszComma)
|
---|
89 | psz += len + 1;
|
---|
90 | else
|
---|
91 | psz += len;
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (RT_SUCCESS(rc))
|
---|
95 | *pDiskVariant = (MediumVariant_T)DiskVariant;
|
---|
96 | return rc;
|
---|
97 | }
|
---|
98 |
|
---|
99 | int parseDiskType(const char *psz, MediumType_T *pDiskType)
|
---|
100 | {
|
---|
101 | int rc = VINF_SUCCESS;
|
---|
102 | MediumType_T DiskType = MediumType_Normal;
|
---|
103 | if (!RTStrICmp(psz, "normal"))
|
---|
104 | DiskType = MediumType_Normal;
|
---|
105 | else if (!RTStrICmp(psz, "immutable"))
|
---|
106 | DiskType = MediumType_Immutable;
|
---|
107 | else if (!RTStrICmp(psz, "writethrough"))
|
---|
108 | DiskType = MediumType_Writethrough;
|
---|
109 | else if (!RTStrICmp(psz, "shareable"))
|
---|
110 | DiskType = MediumType_Shareable;
|
---|
111 | else if (!RTStrICmp(psz, "readonly"))
|
---|
112 | DiskType = MediumType_Readonly;
|
---|
113 | else if (!RTStrICmp(psz, "multiattach"))
|
---|
114 | DiskType = MediumType_MultiAttach;
|
---|
115 | else
|
---|
116 | rc = VERR_PARSE_ERROR;
|
---|
117 |
|
---|
118 | if (RT_SUCCESS(rc))
|
---|
119 | *pDiskType = DiskType;
|
---|
120 | return rc;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /** @todo move this into getopt, as getting bool values is generic */
|
---|
124 | static int parseBool(const char *psz, bool *pb)
|
---|
125 | {
|
---|
126 | int rc = VINF_SUCCESS;
|
---|
127 | if ( !RTStrICmp(psz, "on")
|
---|
128 | || !RTStrICmp(psz, "yes")
|
---|
129 | || !RTStrICmp(psz, "true")
|
---|
130 | || !RTStrICmp(psz, "1")
|
---|
131 | || !RTStrICmp(psz, "enable")
|
---|
132 | || !RTStrICmp(psz, "enabled"))
|
---|
133 | {
|
---|
134 | *pb = true;
|
---|
135 | }
|
---|
136 | else if ( !RTStrICmp(psz, "off")
|
---|
137 | || !RTStrICmp(psz, "no")
|
---|
138 | || !RTStrICmp(psz, "false")
|
---|
139 | || !RTStrICmp(psz, "0")
|
---|
140 | || !RTStrICmp(psz, "disable")
|
---|
141 | || !RTStrICmp(psz, "disabled"))
|
---|
142 | {
|
---|
143 | *pb = false;
|
---|
144 | }
|
---|
145 | else
|
---|
146 | rc = VERR_PARSE_ERROR;
|
---|
147 |
|
---|
148 | return rc;
|
---|
149 | }
|
---|
150 |
|
---|
151 | HRESULT findMedium(HandlerArg *a, const char *pszFilenameOrUuid,
|
---|
152 | DeviceType_T enmDevType, bool fSilent,
|
---|
153 | ComPtr<IMedium> &pMedium)
|
---|
154 | {
|
---|
155 | HRESULT rc;
|
---|
156 | Guid id(pszFilenameOrUuid);
|
---|
157 | char szFilenameAbs[RTPATH_MAX] = "";
|
---|
158 |
|
---|
159 | /* If it is no UUID, convert the filename to an absolute one. */
|
---|
160 | if (id.isEmpty())
|
---|
161 | {
|
---|
162 | int irc = RTPathAbs(pszFilenameOrUuid, szFilenameAbs, sizeof(szFilenameAbs));
|
---|
163 | if (RT_FAILURE(irc))
|
---|
164 | {
|
---|
165 | if (!fSilent)
|
---|
166 | RTMsgError("Cannot convert filename \"%s\" to absolute path", pszFilenameOrUuid);
|
---|
167 | return E_FAIL;
|
---|
168 | }
|
---|
169 | pszFilenameOrUuid = szFilenameAbs;
|
---|
170 | }
|
---|
171 |
|
---|
172 | if (!fSilent)
|
---|
173 | CHECK_ERROR(a->virtualBox, FindMedium(Bstr(pszFilenameOrUuid).raw(),
|
---|
174 | enmDevType, pMedium.asOutParam()));
|
---|
175 | else
|
---|
176 | rc = a->virtualBox->FindMedium(Bstr(pszFilenameOrUuid).raw(),
|
---|
177 | enmDevType, pMedium.asOutParam());
|
---|
178 | return rc;
|
---|
179 | }
|
---|
180 |
|
---|
181 | HRESULT findOrOpenMedium(HandlerArg *a, const char *pszFilenameOrUuid,
|
---|
182 | DeviceType_T enmDevType, ComPtr<IMedium> &pMedium,
|
---|
183 | bool fForceNewUuidOnOpen, bool *pfWasUnknown)
|
---|
184 | {
|
---|
185 | HRESULT rc;
|
---|
186 | bool fWasUnknown = false;
|
---|
187 | Guid id(pszFilenameOrUuid);
|
---|
188 | char szFilenameAbs[RTPATH_MAX] = "";
|
---|
189 |
|
---|
190 | /* If it is no UUID, convert the filename to an absolute one. */
|
---|
191 | if (id.isEmpty())
|
---|
192 | {
|
---|
193 | int irc = RTPathAbs(pszFilenameOrUuid, szFilenameAbs, sizeof(szFilenameAbs));
|
---|
194 | if (RT_FAILURE(irc))
|
---|
195 | {
|
---|
196 | RTMsgError("Cannot convert filename \"%s\" to absolute path", pszFilenameOrUuid);
|
---|
197 | return E_FAIL;
|
---|
198 | }
|
---|
199 | pszFilenameOrUuid = szFilenameAbs;
|
---|
200 | }
|
---|
201 |
|
---|
202 | rc = a->virtualBox->FindMedium(Bstr(pszFilenameOrUuid).raw(), enmDevType,
|
---|
203 | pMedium.asOutParam());
|
---|
204 | /* If the medium is unknown try to open it. */
|
---|
205 | if (!pMedium)
|
---|
206 | {
|
---|
207 | CHECK_ERROR(a->virtualBox, OpenMedium(Bstr(pszFilenameOrUuid).raw(),
|
---|
208 | enmDevType, AccessMode_ReadWrite,
|
---|
209 | fForceNewUuidOnOpen,
|
---|
210 | pMedium.asOutParam()));
|
---|
211 | if (SUCCEEDED(rc))
|
---|
212 | fWasUnknown = true;
|
---|
213 | }
|
---|
214 | if (RT_VALID_PTR(pfWasUnknown))
|
---|
215 | *pfWasUnknown = fWasUnknown;
|
---|
216 | return rc;
|
---|
217 | }
|
---|
218 |
|
---|
219 | static HRESULT createHardDisk(HandlerArg *a, const char *pszFormat,
|
---|
220 | const char *pszFilename, ComPtr<IMedium> &pMedium)
|
---|
221 | {
|
---|
222 | HRESULT rc;
|
---|
223 | char szFilenameAbs[RTPATH_MAX] = "";
|
---|
224 |
|
---|
225 | /** @todo laziness shortcut. should really check the MediumFormatCapabilities */
|
---|
226 | if (RTStrICmp(pszFormat, "iSCSI"))
|
---|
227 | {
|
---|
228 | int irc = RTPathAbs(pszFilename, szFilenameAbs, sizeof(szFilenameAbs));
|
---|
229 | if (RT_FAILURE(irc))
|
---|
230 | {
|
---|
231 | RTMsgError("Cannot convert filename \"%s\" to absolute path", pszFilename);
|
---|
232 | return E_FAIL;
|
---|
233 | }
|
---|
234 | pszFilename = szFilenameAbs;
|
---|
235 | }
|
---|
236 |
|
---|
237 | CHECK_ERROR(a->virtualBox, CreateHardDisk(Bstr(pszFormat).raw(),
|
---|
238 | Bstr(pszFilename).raw(),
|
---|
239 | pMedium.asOutParam()));
|
---|
240 | return rc;
|
---|
241 | }
|
---|
242 |
|
---|
243 | static const RTGETOPTDEF g_aCreateHardDiskOptions[] =
|
---|
244 | {
|
---|
245 | { "--filename", 'f', RTGETOPT_REQ_STRING },
|
---|
246 | { "-filename", 'f', RTGETOPT_REQ_STRING }, // deprecated
|
---|
247 | { "--diffparent", 'd', RTGETOPT_REQ_STRING },
|
---|
248 | { "--size", 's', RTGETOPT_REQ_UINT64 },
|
---|
249 | { "-size", 's', RTGETOPT_REQ_UINT64 }, // deprecated
|
---|
250 | { "--sizebyte", 'S', RTGETOPT_REQ_UINT64 },
|
---|
251 | { "--format", 'o', RTGETOPT_REQ_STRING },
|
---|
252 | { "-format", 'o', RTGETOPT_REQ_STRING }, // deprecated
|
---|
253 | { "--static", 'F', RTGETOPT_REQ_NOTHING },
|
---|
254 | { "-static", 'F', RTGETOPT_REQ_NOTHING }, // deprecated
|
---|
255 | { "--variant", 'm', RTGETOPT_REQ_STRING },
|
---|
256 | { "-variant", 'm', RTGETOPT_REQ_STRING }, // deprecated
|
---|
257 | };
|
---|
258 |
|
---|
259 | int handleCreateHardDisk(HandlerArg *a)
|
---|
260 | {
|
---|
261 | HRESULT rc;
|
---|
262 | int vrc;
|
---|
263 | const char *filename = NULL;
|
---|
264 | const char *diffparent = NULL;
|
---|
265 | uint64_t size = 0;
|
---|
266 | const char *format = NULL;
|
---|
267 | bool fBase = true;
|
---|
268 | MediumVariant_T DiskVariant = MediumVariant_Standard;
|
---|
269 |
|
---|
270 | int c;
|
---|
271 | RTGETOPTUNION ValueUnion;
|
---|
272 | RTGETOPTSTATE GetState;
|
---|
273 | // start at 0 because main() has hacked both the argc and argv given to us
|
---|
274 | RTGetOptInit(&GetState, a->argc, a->argv, g_aCreateHardDiskOptions, RT_ELEMENTS(g_aCreateHardDiskOptions),
|
---|
275 | 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
|
---|
276 | while ((c = RTGetOpt(&GetState, &ValueUnion)))
|
---|
277 | {
|
---|
278 | switch (c)
|
---|
279 | {
|
---|
280 | case 'f': // --filename
|
---|
281 | filename = ValueUnion.psz;
|
---|
282 | break;
|
---|
283 |
|
---|
284 | case 'd': // --diffparent
|
---|
285 | diffparent = ValueUnion.psz;
|
---|
286 | fBase = false;
|
---|
287 | break;
|
---|
288 |
|
---|
289 | case 's': // --size
|
---|
290 | size = ValueUnion.u64 * _1M;
|
---|
291 | break;
|
---|
292 |
|
---|
293 | case 'S': // --sizebyte
|
---|
294 | size = ValueUnion.u64;
|
---|
295 | break;
|
---|
296 |
|
---|
297 | case 'o': // --format
|
---|
298 | format = ValueUnion.psz;
|
---|
299 | break;
|
---|
300 |
|
---|
301 | case 'F': // --static ("fixed"/"flat")
|
---|
302 | {
|
---|
303 | unsigned uDiskVariant = (unsigned)DiskVariant;
|
---|
304 | uDiskVariant |= MediumVariant_Fixed;
|
---|
305 | DiskVariant = (MediumVariant_T)uDiskVariant;
|
---|
306 | break;
|
---|
307 | }
|
---|
308 |
|
---|
309 | case 'm': // --variant
|
---|
310 | vrc = parseDiskVariant(ValueUnion.psz, &DiskVariant);
|
---|
311 | if (RT_FAILURE(vrc))
|
---|
312 | return errorArgument("Invalid hard disk variant '%s'", ValueUnion.psz);
|
---|
313 | break;
|
---|
314 |
|
---|
315 | case VINF_GETOPT_NOT_OPTION:
|
---|
316 | return errorSyntax(USAGE_CREATEHD, "Invalid parameter '%s'", ValueUnion.psz);
|
---|
317 |
|
---|
318 | default:
|
---|
319 | if (c > 0)
|
---|
320 | {
|
---|
321 | if (RT_C_IS_PRINT(c))
|
---|
322 | return errorSyntax(USAGE_CREATEHD, "Invalid option -%c", c);
|
---|
323 | else
|
---|
324 | return errorSyntax(USAGE_CREATEHD, "Invalid option case %i", c);
|
---|
325 | }
|
---|
326 | else if (c == VERR_GETOPT_UNKNOWN_OPTION)
|
---|
327 | return errorSyntax(USAGE_CREATEHD, "unknown option: %s\n", ValueUnion.psz);
|
---|
328 | else if (ValueUnion.pDef)
|
---|
329 | return errorSyntax(USAGE_CREATEHD, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
|
---|
330 | else
|
---|
331 | return errorSyntax(USAGE_CREATEHD, "error: %Rrs", c);
|
---|
332 | }
|
---|
333 | }
|
---|
334 |
|
---|
335 | /* check the outcome */
|
---|
336 | bool fUnknownParent = false;
|
---|
337 | ComPtr<IMedium> parentHardDisk;
|
---|
338 | if (fBase)
|
---|
339 | {
|
---|
340 | if ( !filename
|
---|
341 | || !*filename
|
---|
342 | || size == 0)
|
---|
343 | return errorSyntax(USAGE_CREATEHD, "Parameters --filename and --size are required");
|
---|
344 | if (!format || !*format)
|
---|
345 | format = "VDI";
|
---|
346 | }
|
---|
347 | else
|
---|
348 | {
|
---|
349 | if ( !filename
|
---|
350 | || !*filename)
|
---|
351 | return errorSyntax(USAGE_CREATEHD, "Parameters --filename is required");
|
---|
352 | size = 0;
|
---|
353 | DiskVariant = MediumVariant_Diff;
|
---|
354 | if (!format || !*format)
|
---|
355 | {
|
---|
356 | const char *pszExt = RTPathExt(filename);
|
---|
357 | /* Skip over . if there is an extension. */
|
---|
358 | if (pszExt)
|
---|
359 | pszExt++;
|
---|
360 | if (!pszExt || !*pszExt)
|
---|
361 | format = "VDI";
|
---|
362 | else
|
---|
363 | format = pszExt;
|
---|
364 | }
|
---|
365 | rc = findOrOpenMedium(a, diffparent, DeviceType_HardDisk,
|
---|
366 | parentHardDisk, false /* fForceNewUuidOnOpen */,
|
---|
367 | &fUnknownParent);
|
---|
368 | if (FAILED(rc))
|
---|
369 | return 1;
|
---|
370 | if (parentHardDisk.isNull())
|
---|
371 | {
|
---|
372 | RTMsgError("Invalid parent hard disk reference, avoiding crash");
|
---|
373 | return 1;
|
---|
374 | }
|
---|
375 | MediumState_T state;
|
---|
376 | CHECK_ERROR(parentHardDisk, COMGETTER(State)(&state));
|
---|
377 | if (FAILED(rc))
|
---|
378 | return 1;
|
---|
379 | if (state == MediumState_Inaccessible)
|
---|
380 | {
|
---|
381 | CHECK_ERROR(parentHardDisk, RefreshState(&state));
|
---|
382 | if (FAILED(rc))
|
---|
383 | return 1;
|
---|
384 | }
|
---|
385 | }
|
---|
386 | /* check for filename extension */
|
---|
387 | /** @todo use IMediumFormat to cover all extensions generically */
|
---|
388 | Utf8Str strName(filename);
|
---|
389 | if (!RTPathHaveExt(strName.c_str()))
|
---|
390 | {
|
---|
391 | Utf8Str strFormat(format);
|
---|
392 | if (strFormat.compare("vmdk", RTCString::CaseInsensitive) == 0)
|
---|
393 | strName.append(".vmdk");
|
---|
394 | else if (strFormat.compare("vhd", RTCString::CaseInsensitive) == 0)
|
---|
395 | strName.append(".vhd");
|
---|
396 | else
|
---|
397 | strName.append(".vdi");
|
---|
398 | filename = strName.c_str();
|
---|
399 | }
|
---|
400 |
|
---|
401 | ComPtr<IMedium> hardDisk;
|
---|
402 | rc = createHardDisk(a, format, filename, hardDisk);
|
---|
403 | if (SUCCEEDED(rc) && hardDisk)
|
---|
404 | {
|
---|
405 | ComPtr<IProgress> progress;
|
---|
406 | if (fBase)
|
---|
407 | CHECK_ERROR(hardDisk, CreateBaseStorage(size, DiskVariant, progress.asOutParam()));
|
---|
408 | else
|
---|
409 | CHECK_ERROR(parentHardDisk, CreateDiffStorage(hardDisk, DiskVariant, progress.asOutParam()));
|
---|
410 | if (SUCCEEDED(rc) && progress)
|
---|
411 | {
|
---|
412 | rc = showProgress(progress);
|
---|
413 | CHECK_PROGRESS_ERROR(progress, ("Failed to create hard disk"));
|
---|
414 | if (SUCCEEDED(rc))
|
---|
415 | {
|
---|
416 | Bstr uuid;
|
---|
417 | CHECK_ERROR(hardDisk, COMGETTER(Id)(uuid.asOutParam()));
|
---|
418 | RTPrintf("Disk image created. UUID: %s\n", Utf8Str(uuid).c_str());
|
---|
419 | }
|
---|
420 | }
|
---|
421 |
|
---|
422 | CHECK_ERROR(hardDisk, Close());
|
---|
423 | if (!fBase && fUnknownParent)
|
---|
424 | CHECK_ERROR(parentHardDisk, Close());
|
---|
425 | }
|
---|
426 | return SUCCEEDED(rc) ? 0 : 1;
|
---|
427 | }
|
---|
428 |
|
---|
429 | static const RTGETOPTDEF g_aModifyHardDiskOptions[] =
|
---|
430 | {
|
---|
431 | { "--type", 't', RTGETOPT_REQ_STRING },
|
---|
432 | { "-type", 't', RTGETOPT_REQ_STRING }, // deprecated
|
---|
433 | { "settype", 't', RTGETOPT_REQ_STRING }, // deprecated
|
---|
434 | { "--autoreset", 'z', RTGETOPT_REQ_STRING },
|
---|
435 | { "-autoreset", 'z', RTGETOPT_REQ_STRING }, // deprecated
|
---|
436 | { "autoreset", 'z', RTGETOPT_REQ_STRING }, // deprecated
|
---|
437 | { "--compact", 'c', RTGETOPT_REQ_NOTHING },
|
---|
438 | { "-compact", 'c', RTGETOPT_REQ_NOTHING }, // deprecated
|
---|
439 | { "compact", 'c', RTGETOPT_REQ_NOTHING }, // deprecated
|
---|
440 | { "--resize", 'r', RTGETOPT_REQ_UINT64 },
|
---|
441 | { "--resizebyte", 'R', RTGETOPT_REQ_UINT64 }
|
---|
442 | };
|
---|
443 |
|
---|
444 | int handleModifyHardDisk(HandlerArg *a)
|
---|
445 | {
|
---|
446 | HRESULT rc;
|
---|
447 | int vrc;
|
---|
448 | ComPtr<IMedium> hardDisk;
|
---|
449 | MediumType_T DiskType;
|
---|
450 | bool AutoReset = false;
|
---|
451 | bool fModifyDiskType = false, fModifyAutoReset = false, fModifyCompact = false;
|
---|
452 | bool fModifyResize = false;
|
---|
453 | uint64_t cbResize = 0;
|
---|
454 | const char *FilenameOrUuid = NULL;
|
---|
455 | bool unknown = false;
|
---|
456 |
|
---|
457 | int c;
|
---|
458 | RTGETOPTUNION ValueUnion;
|
---|
459 | RTGETOPTSTATE GetState;
|
---|
460 | // start at 0 because main() has hacked both the argc and argv given to us
|
---|
461 | RTGetOptInit(&GetState, a->argc, a->argv, g_aModifyHardDiskOptions, RT_ELEMENTS(g_aModifyHardDiskOptions),
|
---|
462 | 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
|
---|
463 | while ((c = RTGetOpt(&GetState, &ValueUnion)))
|
---|
464 | {
|
---|
465 | switch (c)
|
---|
466 | {
|
---|
467 | case 't': // --type
|
---|
468 | vrc = parseDiskType(ValueUnion.psz, &DiskType);
|
---|
469 | if (RT_FAILURE(vrc))
|
---|
470 | return errorArgument("Invalid hard disk type '%s'", ValueUnion.psz);
|
---|
471 | fModifyDiskType = true;
|
---|
472 | break;
|
---|
473 |
|
---|
474 | case 'z': // --autoreset
|
---|
475 | vrc = parseBool(ValueUnion.psz, &AutoReset);
|
---|
476 | if (RT_FAILURE(vrc))
|
---|
477 | return errorArgument("Invalid autoreset parameter '%s'", ValueUnion.psz);
|
---|
478 | fModifyAutoReset = true;
|
---|
479 | break;
|
---|
480 |
|
---|
481 | case 'c': // --compact
|
---|
482 | fModifyCompact = true;
|
---|
483 | break;
|
---|
484 |
|
---|
485 | case 'r': // --resize
|
---|
486 | cbResize = ValueUnion.u64 * _1M;
|
---|
487 | fModifyResize = true;
|
---|
488 | break;
|
---|
489 |
|
---|
490 | case 'R': // --resizebyte
|
---|
491 | cbResize = ValueUnion.u64;
|
---|
492 | fModifyResize = true;
|
---|
493 | break;
|
---|
494 |
|
---|
495 | case VINF_GETOPT_NOT_OPTION:
|
---|
496 | if (!FilenameOrUuid)
|
---|
497 | FilenameOrUuid = ValueUnion.psz;
|
---|
498 | else
|
---|
499 | return errorSyntax(USAGE_CREATEHD, "Invalid parameter '%s'", ValueUnion.psz);
|
---|
500 | break;
|
---|
501 |
|
---|
502 | default:
|
---|
503 | if (c > 0)
|
---|
504 | {
|
---|
505 | if (RT_C_IS_PRINT(c))
|
---|
506 | return errorSyntax(USAGE_MODIFYHD, "Invalid option -%c", c);
|
---|
507 | else
|
---|
508 | return errorSyntax(USAGE_MODIFYHD, "Invalid option case %i", c);
|
---|
509 | }
|
---|
510 | else if (c == VERR_GETOPT_UNKNOWN_OPTION)
|
---|
511 | return errorSyntax(USAGE_MODIFYHD, "unknown option: %s\n", ValueUnion.psz);
|
---|
512 | else if (ValueUnion.pDef)
|
---|
513 | return errorSyntax(USAGE_MODIFYHD, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
|
---|
514 | else
|
---|
515 | return errorSyntax(USAGE_MODIFYHD, "error: %Rrs", c);
|
---|
516 | }
|
---|
517 | }
|
---|
518 |
|
---|
519 | if (!FilenameOrUuid)
|
---|
520 | return errorSyntax(USAGE_MODIFYHD, "Disk name or UUID required");
|
---|
521 |
|
---|
522 | if (!fModifyDiskType && !fModifyAutoReset && !fModifyCompact && !fModifyResize)
|
---|
523 | return errorSyntax(USAGE_MODIFYHD, "No operation specified");
|
---|
524 |
|
---|
525 | /* Depending on the operation the medium must be in the registry or
|
---|
526 | * may be opened on demand. */
|
---|
527 | if (fModifyDiskType || fModifyAutoReset)
|
---|
528 | rc = findMedium(a, FilenameOrUuid, DeviceType_HardDisk, false /* fSilent */, hardDisk);
|
---|
529 | else
|
---|
530 | rc = findOrOpenMedium(a, FilenameOrUuid, DeviceType_HardDisk,
|
---|
531 | hardDisk, false /* fForceNewUuidOnOpen */, &unknown);
|
---|
532 | if (FAILED(rc))
|
---|
533 | return 1;
|
---|
534 | if (hardDisk.isNull())
|
---|
535 | {
|
---|
536 | RTMsgError("Invalid hard disk reference, avoiding crash");
|
---|
537 | return 1;
|
---|
538 | }
|
---|
539 |
|
---|
540 | if (fModifyDiskType)
|
---|
541 | {
|
---|
542 | MediumType_T hddType;
|
---|
543 | CHECK_ERROR(hardDisk, COMGETTER(Type)(&hddType));
|
---|
544 |
|
---|
545 | if (hddType != DiskType)
|
---|
546 | CHECK_ERROR(hardDisk, COMSETTER(Type)(DiskType));
|
---|
547 | }
|
---|
548 |
|
---|
549 | if (fModifyAutoReset)
|
---|
550 | {
|
---|
551 | CHECK_ERROR(hardDisk, COMSETTER(AutoReset)(AutoReset));
|
---|
552 | }
|
---|
553 |
|
---|
554 | if (fModifyCompact)
|
---|
555 | {
|
---|
556 | ComPtr<IProgress> progress;
|
---|
557 | CHECK_ERROR(hardDisk, Compact(progress.asOutParam()));
|
---|
558 | if (SUCCEEDED(rc))
|
---|
559 | rc = showProgress(progress);
|
---|
560 | if (FAILED(rc))
|
---|
561 | {
|
---|
562 | if (rc == E_NOTIMPL)
|
---|
563 | RTMsgError("Compact hard disk operation is not implemented!");
|
---|
564 | else if (rc == VBOX_E_NOT_SUPPORTED)
|
---|
565 | RTMsgError("Compact hard disk operation for this format is not implemented yet!");
|
---|
566 | else
|
---|
567 | CHECK_PROGRESS_ERROR(progress, ("Failed to compact hard disk"));
|
---|
568 | }
|
---|
569 | }
|
---|
570 |
|
---|
571 | if (fModifyResize)
|
---|
572 | {
|
---|
573 | ComPtr<IProgress> progress;
|
---|
574 | CHECK_ERROR(hardDisk, Resize(cbResize, progress.asOutParam()));
|
---|
575 | if (SUCCEEDED(rc))
|
---|
576 | rc = showProgress(progress);
|
---|
577 | if (FAILED(rc))
|
---|
578 | {
|
---|
579 | if (rc == E_NOTIMPL)
|
---|
580 | RTMsgError("Resize hard disk operation is not implemented!");
|
---|
581 | else if (rc == VBOX_E_NOT_SUPPORTED)
|
---|
582 | RTMsgError("Resize hard disk operation for this format is not implemented yet!");
|
---|
583 | else
|
---|
584 | CHECK_PROGRESS_ERROR(progress, ("Failed to resize hard disk"));
|
---|
585 | }
|
---|
586 | }
|
---|
587 |
|
---|
588 | if (unknown)
|
---|
589 | hardDisk->Close();
|
---|
590 |
|
---|
591 | return SUCCEEDED(rc) ? 0 : 1;
|
---|
592 | }
|
---|
593 |
|
---|
594 | static const RTGETOPTDEF g_aCloneHardDiskOptions[] =
|
---|
595 | {
|
---|
596 | { "--format", 'o', RTGETOPT_REQ_STRING },
|
---|
597 | { "-format", 'o', RTGETOPT_REQ_STRING },
|
---|
598 | { "--static", 'F', RTGETOPT_REQ_NOTHING },
|
---|
599 | { "-static", 'F', RTGETOPT_REQ_NOTHING },
|
---|
600 | { "--existing", 'E', RTGETOPT_REQ_NOTHING },
|
---|
601 | { "--variant", 'm', RTGETOPT_REQ_STRING },
|
---|
602 | { "-variant", 'm', RTGETOPT_REQ_STRING },
|
---|
603 | };
|
---|
604 |
|
---|
605 | int handleCloneHardDisk(HandlerArg *a)
|
---|
606 | {
|
---|
607 | HRESULT rc;
|
---|
608 | int vrc;
|
---|
609 | const char *pszSrc = NULL;
|
---|
610 | const char *pszDst = NULL;
|
---|
611 | Bstr format;
|
---|
612 | MediumVariant_T DiskVariant = MediumVariant_Standard;
|
---|
613 | bool fExisting = false;
|
---|
614 |
|
---|
615 | int c;
|
---|
616 | RTGETOPTUNION ValueUnion;
|
---|
617 | RTGETOPTSTATE GetState;
|
---|
618 | // start at 0 because main() has hacked both the argc and argv given to us
|
---|
619 | RTGetOptInit(&GetState, a->argc, a->argv, g_aCloneHardDiskOptions, RT_ELEMENTS(g_aCloneHardDiskOptions),
|
---|
620 | 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
|
---|
621 | while ((c = RTGetOpt(&GetState, &ValueUnion)))
|
---|
622 | {
|
---|
623 | switch (c)
|
---|
624 | {
|
---|
625 | case 'o': // --format
|
---|
626 | format = ValueUnion.psz;
|
---|
627 | break;
|
---|
628 |
|
---|
629 | case 'F': // --static
|
---|
630 | {
|
---|
631 | unsigned uDiskVariant = (unsigned)DiskVariant;
|
---|
632 | uDiskVariant |= MediumVariant_Fixed;
|
---|
633 | DiskVariant = (MediumVariant_T)uDiskVariant;
|
---|
634 | break;
|
---|
635 | }
|
---|
636 |
|
---|
637 | case 'E': // --existing
|
---|
638 | fExisting = true;
|
---|
639 | break;
|
---|
640 |
|
---|
641 | case 'm': // --variant
|
---|
642 | vrc = parseDiskVariant(ValueUnion.psz, &DiskVariant);
|
---|
643 | if (RT_FAILURE(vrc))
|
---|
644 | return errorArgument("Invalid hard disk variant '%s'", ValueUnion.psz);
|
---|
645 | break;
|
---|
646 |
|
---|
647 | case VINF_GETOPT_NOT_OPTION:
|
---|
648 | if (!pszSrc)
|
---|
649 | pszSrc = ValueUnion.psz;
|
---|
650 | else if (!pszDst)
|
---|
651 | pszDst = ValueUnion.psz;
|
---|
652 | else
|
---|
653 | return errorSyntax(USAGE_CLONEHD, "Invalid parameter '%s'", ValueUnion.psz);
|
---|
654 | break;
|
---|
655 |
|
---|
656 | default:
|
---|
657 | if (c > 0)
|
---|
658 | {
|
---|
659 | if (RT_C_IS_GRAPH(c))
|
---|
660 | return errorSyntax(USAGE_CLONEHD, "unhandled option: -%c", c);
|
---|
661 | else
|
---|
662 | return errorSyntax(USAGE_CLONEHD, "unhandled option: %i", c);
|
---|
663 | }
|
---|
664 | else if (c == VERR_GETOPT_UNKNOWN_OPTION)
|
---|
665 | return errorSyntax(USAGE_CLONEHD, "unknown option: %s", ValueUnion.psz);
|
---|
666 | else if (ValueUnion.pDef)
|
---|
667 | return errorSyntax(USAGE_CLONEHD, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
|
---|
668 | else
|
---|
669 | return errorSyntax(USAGE_CLONEHD, "error: %Rrs", c);
|
---|
670 | }
|
---|
671 | }
|
---|
672 |
|
---|
673 | if (!pszSrc)
|
---|
674 | return errorSyntax(USAGE_CLONEHD, "Mandatory UUID or input file parameter missing");
|
---|
675 | if (!pszDst)
|
---|
676 | return errorSyntax(USAGE_CLONEHD, "Mandatory output file parameter missing");
|
---|
677 | if (fExisting && (!format.isEmpty() || DiskVariant != MediumType_Normal))
|
---|
678 | return errorSyntax(USAGE_CLONEHD, "Specified options which cannot be used with --existing");
|
---|
679 |
|
---|
680 | ComPtr<IMedium> srcDisk;
|
---|
681 | ComPtr<IMedium> dstDisk;
|
---|
682 | bool fSrcUnknown = false;
|
---|
683 | bool fDstUnknown = false;
|
---|
684 |
|
---|
685 | rc = findOrOpenMedium(a, pszSrc, DeviceType_HardDisk, srcDisk,
|
---|
686 | false /* fForceNewUuidOnOpen */, &fSrcUnknown);
|
---|
687 | if (FAILED(rc))
|
---|
688 | return 1;
|
---|
689 |
|
---|
690 | do
|
---|
691 | {
|
---|
692 | /* open/create destination hard disk */
|
---|
693 | if (fExisting)
|
---|
694 | {
|
---|
695 | rc = findOrOpenMedium(a, pszDst, DeviceType_HardDisk, dstDisk,
|
---|
696 | false /* fForceNewUuidOnOpen */, &fDstUnknown);
|
---|
697 | if (FAILED(rc))
|
---|
698 | break;
|
---|
699 |
|
---|
700 | /* Perform accessibility check now. */
|
---|
701 | MediumState_T state;
|
---|
702 | CHECK_ERROR_BREAK(dstDisk, RefreshState(&state));
|
---|
703 | CHECK_ERROR_BREAK(dstDisk, COMGETTER(Format)(format.asOutParam()));
|
---|
704 | }
|
---|
705 | else
|
---|
706 | {
|
---|
707 | /* use the format of the source hard disk if unspecified */
|
---|
708 | if (format.isEmpty())
|
---|
709 | CHECK_ERROR_BREAK(srcDisk, COMGETTER(Format)(format.asOutParam()));
|
---|
710 | rc = createHardDisk(a, Utf8Str(format).c_str(), pszDst, dstDisk);
|
---|
711 | if (FAILED(rc))
|
---|
712 | break;
|
---|
713 | fDstUnknown = true;
|
---|
714 | }
|
---|
715 |
|
---|
716 | ComPtr<IProgress> progress;
|
---|
717 | CHECK_ERROR_BREAK(srcDisk, CloneTo(dstDisk, DiskVariant, NULL, progress.asOutParam()));
|
---|
718 |
|
---|
719 | rc = showProgress(progress);
|
---|
720 | CHECK_PROGRESS_ERROR_BREAK(progress, ("Failed to clone hard disk"));
|
---|
721 |
|
---|
722 | Bstr uuid;
|
---|
723 | CHECK_ERROR_BREAK(dstDisk, COMGETTER(Id)(uuid.asOutParam()));
|
---|
724 |
|
---|
725 | RTPrintf("Clone hard disk created in format '%ls'. UUID: %s\n",
|
---|
726 | format.raw(), Utf8Str(uuid).c_str());
|
---|
727 | }
|
---|
728 | while (0);
|
---|
729 |
|
---|
730 | if (fDstUnknown && !dstDisk.isNull())
|
---|
731 | {
|
---|
732 | /* forget the created clone */
|
---|
733 | dstDisk->Close();
|
---|
734 | }
|
---|
735 | if (fSrcUnknown)
|
---|
736 | {
|
---|
737 | /* close the unknown hard disk to forget it again */
|
---|
738 | srcDisk->Close();
|
---|
739 | }
|
---|
740 |
|
---|
741 | return SUCCEEDED(rc) ? 0 : 1;
|
---|
742 | }
|
---|
743 |
|
---|
744 | static const RTGETOPTDEF g_aConvertFromRawHardDiskOptions[] =
|
---|
745 | {
|
---|
746 | { "--format", 'o', RTGETOPT_REQ_STRING },
|
---|
747 | { "-format", 'o', RTGETOPT_REQ_STRING },
|
---|
748 | { "--static", 'F', RTGETOPT_REQ_NOTHING },
|
---|
749 | { "-static", 'F', RTGETOPT_REQ_NOTHING },
|
---|
750 | { "--variant", 'm', RTGETOPT_REQ_STRING },
|
---|
751 | { "-variant", 'm', RTGETOPT_REQ_STRING },
|
---|
752 | { "--uuid", 'u', RTGETOPT_REQ_STRING },
|
---|
753 | };
|
---|
754 |
|
---|
755 | RTEXITCODE handleConvertFromRaw(int argc, char *argv[])
|
---|
756 | {
|
---|
757 | int rc = VINF_SUCCESS;
|
---|
758 | bool fReadFromStdIn = false;
|
---|
759 | const char *format = "VDI";
|
---|
760 | const char *srcfilename = NULL;
|
---|
761 | const char *dstfilename = NULL;
|
---|
762 | const char *filesize = NULL;
|
---|
763 | unsigned uImageFlags = VD_IMAGE_FLAGS_NONE;
|
---|
764 | void *pvBuf = NULL;
|
---|
765 | RTUUID uuid;
|
---|
766 | PCRTUUID pUuid = NULL;
|
---|
767 |
|
---|
768 | int c;
|
---|
769 | RTGETOPTUNION ValueUnion;
|
---|
770 | RTGETOPTSTATE GetState;
|
---|
771 | // start at 0 because main() has hacked both the argc and argv given to us
|
---|
772 | RTGetOptInit(&GetState, argc, argv, g_aConvertFromRawHardDiskOptions, RT_ELEMENTS(g_aConvertFromRawHardDiskOptions),
|
---|
773 | 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
|
---|
774 | while ((c = RTGetOpt(&GetState, &ValueUnion)))
|
---|
775 | {
|
---|
776 | switch (c)
|
---|
777 | {
|
---|
778 | case 'u': // --uuid
|
---|
779 | if (RT_FAILURE(RTUuidFromStr(&uuid, ValueUnion.psz)))
|
---|
780 | return errorSyntax(USAGE_CONVERTFROMRAW, "Invalid UUID '%s'", ValueUnion.psz);
|
---|
781 | pUuid = &uuid;
|
---|
782 | break;
|
---|
783 | case 'o': // --format
|
---|
784 | format = ValueUnion.psz;
|
---|
785 | break;
|
---|
786 |
|
---|
787 | case 'm': // --variant
|
---|
788 | MediumVariant_T DiskVariant;
|
---|
789 | rc = parseDiskVariant(ValueUnion.psz, &DiskVariant);
|
---|
790 | if (RT_FAILURE(rc))
|
---|
791 | return errorArgument("Invalid hard disk variant '%s'", ValueUnion.psz);
|
---|
792 | /// @todo cleaner solution than assuming 1:1 mapping?
|
---|
793 | uImageFlags = (unsigned)DiskVariant;
|
---|
794 | break;
|
---|
795 |
|
---|
796 | case VINF_GETOPT_NOT_OPTION:
|
---|
797 | if (!srcfilename)
|
---|
798 | {
|
---|
799 | srcfilename = ValueUnion.psz;
|
---|
800 | fReadFromStdIn = !strcmp(srcfilename, "stdin");
|
---|
801 | }
|
---|
802 | else if (!dstfilename)
|
---|
803 | dstfilename = ValueUnion.psz;
|
---|
804 | else if (fReadFromStdIn && !filesize)
|
---|
805 | filesize = ValueUnion.psz;
|
---|
806 | else
|
---|
807 | return errorSyntax(USAGE_CONVERTFROMRAW, "Invalid parameter '%s'", ValueUnion.psz);
|
---|
808 | break;
|
---|
809 |
|
---|
810 | default:
|
---|
811 | return errorGetOpt(USAGE_CONVERTFROMRAW, c, &ValueUnion);
|
---|
812 | }
|
---|
813 | }
|
---|
814 |
|
---|
815 | if (!srcfilename || !dstfilename || (fReadFromStdIn && !filesize))
|
---|
816 | return errorSyntax(USAGE_CONVERTFROMRAW, "Incorrect number of parameters");
|
---|
817 | RTStrmPrintf(g_pStdErr, "Converting from raw image file=\"%s\" to file=\"%s\"...\n",
|
---|
818 | srcfilename, dstfilename);
|
---|
819 |
|
---|
820 | PVBOXHDD pDisk = NULL;
|
---|
821 |
|
---|
822 | PVDINTERFACE pVDIfs = NULL;
|
---|
823 | VDINTERFACEERROR vdInterfaceError;
|
---|
824 | vdInterfaceError.pfnError = handleVDError;
|
---|
825 | vdInterfaceError.pfnMessage = NULL;
|
---|
826 |
|
---|
827 | rc = VDInterfaceAdd(&vdInterfaceError.Core, "VBoxManage_IError", VDINTERFACETYPE_ERROR,
|
---|
828 | NULL, sizeof(VDINTERFACEERROR), &pVDIfs);
|
---|
829 | AssertRC(rc);
|
---|
830 |
|
---|
831 | /* open raw image file. */
|
---|
832 | RTFILE File;
|
---|
833 | if (fReadFromStdIn)
|
---|
834 | rc = RTFileFromNative(&File, RTFILE_NATIVE_STDIN);
|
---|
835 | else
|
---|
836 | rc = RTFileOpen(&File, srcfilename, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
|
---|
837 | if (RT_FAILURE(rc))
|
---|
838 | {
|
---|
839 | RTMsgError("Cannot open file \"%s\": %Rrc", srcfilename, rc);
|
---|
840 | goto out;
|
---|
841 | }
|
---|
842 |
|
---|
843 | uint64_t cbFile;
|
---|
844 | /* get image size. */
|
---|
845 | if (fReadFromStdIn)
|
---|
846 | cbFile = RTStrToUInt64(filesize);
|
---|
847 | else
|
---|
848 | rc = RTFileGetSize(File, &cbFile);
|
---|
849 | if (RT_FAILURE(rc))
|
---|
850 | {
|
---|
851 | RTMsgError("Cannot get image size for file \"%s\": %Rrc", srcfilename, rc);
|
---|
852 | goto out;
|
---|
853 | }
|
---|
854 |
|
---|
855 | RTStrmPrintf(g_pStdErr, "Creating %s image with size %RU64 bytes (%RU64MB)...\n",
|
---|
856 | (uImageFlags & VD_IMAGE_FLAGS_FIXED) ? "fixed" : "dynamic", cbFile, (cbFile + _1M - 1) / _1M);
|
---|
857 | char pszComment[256];
|
---|
858 | RTStrPrintf(pszComment, sizeof(pszComment), "Converted image from %s", srcfilename);
|
---|
859 | rc = VDCreate(pVDIfs, VDTYPE_HDD, &pDisk);
|
---|
860 | if (RT_FAILURE(rc))
|
---|
861 | {
|
---|
862 | RTMsgError("Cannot create the virtual disk container: %Rrc", rc);
|
---|
863 | goto out;
|
---|
864 | }
|
---|
865 |
|
---|
866 | Assert(RT_MIN(cbFile / 512 / 16 / 63, 16383) -
|
---|
867 | (unsigned int)RT_MIN(cbFile / 512 / 16 / 63, 16383) == 0);
|
---|
868 | VDGEOMETRY PCHS, LCHS;
|
---|
869 | PCHS.cCylinders = (unsigned int)RT_MIN(cbFile / 512 / 16 / 63, 16383);
|
---|
870 | PCHS.cHeads = 16;
|
---|
871 | PCHS.cSectors = 63;
|
---|
872 | LCHS.cCylinders = 0;
|
---|
873 | LCHS.cHeads = 0;
|
---|
874 | LCHS.cSectors = 0;
|
---|
875 | rc = VDCreateBase(pDisk, format, dstfilename, cbFile,
|
---|
876 | uImageFlags, pszComment, &PCHS, &LCHS, pUuid,
|
---|
877 | VD_OPEN_FLAGS_NORMAL, NULL, NULL);
|
---|
878 | if (RT_FAILURE(rc))
|
---|
879 | {
|
---|
880 | RTMsgError("Cannot create the disk image \"%s\": %Rrc", dstfilename, rc);
|
---|
881 | goto out;
|
---|
882 | }
|
---|
883 |
|
---|
884 | size_t cbBuffer;
|
---|
885 | cbBuffer = _1M;
|
---|
886 | pvBuf = RTMemAlloc(cbBuffer);
|
---|
887 | if (!pvBuf)
|
---|
888 | {
|
---|
889 | rc = VERR_NO_MEMORY;
|
---|
890 | RTMsgError("Out of memory allocating buffers for image \"%s\": %Rrc", dstfilename, rc);
|
---|
891 | goto out;
|
---|
892 | }
|
---|
893 |
|
---|
894 | uint64_t offFile;
|
---|
895 | offFile = 0;
|
---|
896 | while (offFile < cbFile)
|
---|
897 | {
|
---|
898 | size_t cbRead;
|
---|
899 | size_t cbToRead;
|
---|
900 | cbRead = 0;
|
---|
901 | cbToRead = cbFile - offFile >= (uint64_t)cbBuffer ?
|
---|
902 | cbBuffer : (size_t)(cbFile - offFile);
|
---|
903 | rc = RTFileRead(File, pvBuf, cbToRead, &cbRead);
|
---|
904 | if (RT_FAILURE(rc) || !cbRead)
|
---|
905 | break;
|
---|
906 | rc = VDWrite(pDisk, offFile, pvBuf, cbRead);
|
---|
907 | if (RT_FAILURE(rc))
|
---|
908 | {
|
---|
909 | RTMsgError("Failed to write to disk image \"%s\": %Rrc", dstfilename, rc);
|
---|
910 | goto out;
|
---|
911 | }
|
---|
912 | offFile += cbRead;
|
---|
913 | }
|
---|
914 |
|
---|
915 | out:
|
---|
916 | if (pvBuf)
|
---|
917 | RTMemFree(pvBuf);
|
---|
918 | if (pDisk)
|
---|
919 | VDClose(pDisk, RT_FAILURE(rc));
|
---|
920 | if (File != NIL_RTFILE)
|
---|
921 | RTFileClose(File);
|
---|
922 |
|
---|
923 | return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
924 | }
|
---|
925 |
|
---|
926 | static const RTGETOPTDEF g_aShowHardDiskInfoOptions[] =
|
---|
927 | {
|
---|
928 | { "--dummy", 256, RTGETOPT_REQ_NOTHING }, // placeholder for C++
|
---|
929 | };
|
---|
930 |
|
---|
931 | int handleShowHardDiskInfo(HandlerArg *a)
|
---|
932 | {
|
---|
933 | HRESULT rc;
|
---|
934 | const char *FilenameOrUuid = NULL;
|
---|
935 |
|
---|
936 | int c;
|
---|
937 | RTGETOPTUNION ValueUnion;
|
---|
938 | RTGETOPTSTATE GetState;
|
---|
939 | // start at 0 because main() has hacked both the argc and argv given to us
|
---|
940 | RTGetOptInit(&GetState, a->argc, a->argv, g_aShowHardDiskInfoOptions, RT_ELEMENTS(g_aShowHardDiskInfoOptions),
|
---|
941 | 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
|
---|
942 | while ((c = RTGetOpt(&GetState, &ValueUnion)))
|
---|
943 | {
|
---|
944 | switch (c)
|
---|
945 | {
|
---|
946 | case VINF_GETOPT_NOT_OPTION:
|
---|
947 | if (!FilenameOrUuid)
|
---|
948 | FilenameOrUuid = ValueUnion.psz;
|
---|
949 | else
|
---|
950 | return errorSyntax(USAGE_SHOWHDINFO, "Invalid parameter '%s'", ValueUnion.psz);
|
---|
951 | break;
|
---|
952 |
|
---|
953 | default:
|
---|
954 | if (c > 0)
|
---|
955 | {
|
---|
956 | if (RT_C_IS_PRINT(c))
|
---|
957 | return errorSyntax(USAGE_SHOWHDINFO, "Invalid option -%c", c);
|
---|
958 | else
|
---|
959 | return errorSyntax(USAGE_SHOWHDINFO, "Invalid option case %i", c);
|
---|
960 | }
|
---|
961 | else if (c == VERR_GETOPT_UNKNOWN_OPTION)
|
---|
962 | return errorSyntax(USAGE_SHOWHDINFO, "unknown option: %s\n", ValueUnion.psz);
|
---|
963 | else if (ValueUnion.pDef)
|
---|
964 | return errorSyntax(USAGE_SHOWHDINFO, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
|
---|
965 | else
|
---|
966 | return errorSyntax(USAGE_SHOWHDINFO, "error: %Rrs", c);
|
---|
967 | }
|
---|
968 | }
|
---|
969 |
|
---|
970 | /* check for required options */
|
---|
971 | if (!FilenameOrUuid)
|
---|
972 | return errorSyntax(USAGE_SHOWHDINFO, "Disk name or UUID required");
|
---|
973 |
|
---|
974 | ComPtr<IMedium> hardDisk;
|
---|
975 | bool unknown = false;
|
---|
976 |
|
---|
977 | rc = findOrOpenMedium(a, FilenameOrUuid, DeviceType_HardDisk, hardDisk,
|
---|
978 | false /* fForceNewUuidOnOpen */, &unknown);
|
---|
979 | if (FAILED(rc))
|
---|
980 | return 1;
|
---|
981 |
|
---|
982 | do
|
---|
983 | {
|
---|
984 | Bstr uuid;
|
---|
985 | hardDisk->COMGETTER(Id)(uuid.asOutParam());
|
---|
986 | RTPrintf("UUID: %s\n", Utf8Str(uuid).c_str());
|
---|
987 |
|
---|
988 | /* check for accessibility */
|
---|
989 | /// @todo NEWMEDIA check accessibility of all parents
|
---|
990 | /// @todo NEWMEDIA print the full state value
|
---|
991 | MediumState_T state;
|
---|
992 | CHECK_ERROR_BREAK(hardDisk, RefreshState(&state));
|
---|
993 | RTPrintf("Accessible: %s\n", state != MediumState_Inaccessible ? "yes" : "no");
|
---|
994 |
|
---|
995 | if (state == MediumState_Inaccessible)
|
---|
996 | {
|
---|
997 | Bstr err;
|
---|
998 | CHECK_ERROR_BREAK(hardDisk, COMGETTER(LastAccessError)(err.asOutParam()));
|
---|
999 | RTPrintf("Access Error: %ls\n", err.raw());
|
---|
1000 | }
|
---|
1001 |
|
---|
1002 | Bstr description;
|
---|
1003 | hardDisk->COMGETTER(Description)(description.asOutParam());
|
---|
1004 | if (!description.isEmpty())
|
---|
1005 | {
|
---|
1006 | RTPrintf("Description: %ls\n", description.raw());
|
---|
1007 | }
|
---|
1008 |
|
---|
1009 | LONG64 logicalSize;
|
---|
1010 | hardDisk->COMGETTER(LogicalSize)(&logicalSize);
|
---|
1011 | RTPrintf("Logical size: %lld MBytes\n", logicalSize >> 20);
|
---|
1012 | LONG64 actualSize;
|
---|
1013 | hardDisk->COMGETTER(Size)(&actualSize);
|
---|
1014 | RTPrintf("Current size on disk: %lld MBytes\n", actualSize >> 20);
|
---|
1015 |
|
---|
1016 | ComPtr <IMedium> parent;
|
---|
1017 | hardDisk->COMGETTER(Parent)(parent.asOutParam());
|
---|
1018 |
|
---|
1019 | MediumType_T type;
|
---|
1020 | hardDisk->COMGETTER(Type)(&type);
|
---|
1021 | const char *typeStr = "unknown";
|
---|
1022 | switch (type)
|
---|
1023 | {
|
---|
1024 | case MediumType_Normal:
|
---|
1025 | if (!parent.isNull())
|
---|
1026 | typeStr = "normal (differencing)";
|
---|
1027 | else
|
---|
1028 | typeStr = "normal (base)";
|
---|
1029 | break;
|
---|
1030 | case MediumType_Immutable:
|
---|
1031 | typeStr = "immutable";
|
---|
1032 | break;
|
---|
1033 | case MediumType_Writethrough:
|
---|
1034 | typeStr = "writethrough";
|
---|
1035 | break;
|
---|
1036 | case MediumType_Shareable:
|
---|
1037 | typeStr = "shareable";
|
---|
1038 | break;
|
---|
1039 | case MediumType_Readonly:
|
---|
1040 | typeStr = "readonly";
|
---|
1041 | break;
|
---|
1042 | case MediumType_MultiAttach:
|
---|
1043 | typeStr = "multiattach";
|
---|
1044 | break;
|
---|
1045 | }
|
---|
1046 | RTPrintf("Type: %s\n", typeStr);
|
---|
1047 |
|
---|
1048 | Bstr format;
|
---|
1049 | hardDisk->COMGETTER(Format)(format.asOutParam());
|
---|
1050 | RTPrintf("Storage format: %ls\n", format.raw());
|
---|
1051 | ULONG variant;
|
---|
1052 | hardDisk->COMGETTER(Variant)(&variant);
|
---|
1053 | const char *variantStr = "unknown";
|
---|
1054 | switch (variant & ~(MediumVariant_Fixed | MediumVariant_Diff))
|
---|
1055 | {
|
---|
1056 | case MediumVariant_VmdkSplit2G:
|
---|
1057 | variantStr = "split2G";
|
---|
1058 | break;
|
---|
1059 | case MediumVariant_VmdkStreamOptimized:
|
---|
1060 | variantStr = "streamOptimized";
|
---|
1061 | break;
|
---|
1062 | case MediumVariant_VmdkESX:
|
---|
1063 | variantStr = "ESX";
|
---|
1064 | break;
|
---|
1065 | case MediumVariant_Standard:
|
---|
1066 | variantStr = "default";
|
---|
1067 | break;
|
---|
1068 | }
|
---|
1069 | const char *variantTypeStr = "dynamic";
|
---|
1070 | if (variant & MediumVariant_Fixed)
|
---|
1071 | variantTypeStr = "fixed";
|
---|
1072 | else if (variant & MediumVariant_Diff)
|
---|
1073 | variantTypeStr = "differencing";
|
---|
1074 | RTPrintf("Format variant: %s %s\n", variantTypeStr, variantStr);
|
---|
1075 |
|
---|
1076 | /// @todo also dump config parameters (iSCSI)
|
---|
1077 |
|
---|
1078 | if (!unknown)
|
---|
1079 | {
|
---|
1080 | com::SafeArray<BSTR> machineIds;
|
---|
1081 | hardDisk->COMGETTER(MachineIds)(ComSafeArrayAsOutParam(machineIds));
|
---|
1082 | for (size_t j = 0; j < machineIds.size(); ++ j)
|
---|
1083 | {
|
---|
1084 | ComPtr<IMachine> machine;
|
---|
1085 | CHECK_ERROR(a->virtualBox, FindMachine(machineIds[j], machine.asOutParam()));
|
---|
1086 | ASSERT(machine);
|
---|
1087 | Bstr name;
|
---|
1088 | machine->COMGETTER(Name)(name.asOutParam());
|
---|
1089 | machine->COMGETTER(Id)(uuid.asOutParam());
|
---|
1090 | RTPrintf("%s%ls (UUID: %ls)\n",
|
---|
1091 | j == 0 ? "In use by VMs: " : " ",
|
---|
1092 | name.raw(), machineIds[j]);
|
---|
1093 | }
|
---|
1094 | /// @todo NEWMEDIA check usage in snapshots too
|
---|
1095 | /// @todo NEWMEDIA also list children
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | Bstr loc;
|
---|
1099 | hardDisk->COMGETTER(Location)(loc.asOutParam());
|
---|
1100 | RTPrintf("Location: %ls\n", loc.raw());
|
---|
1101 |
|
---|
1102 | /* print out information specific for differencing hard disks */
|
---|
1103 | if (!parent.isNull())
|
---|
1104 | {
|
---|
1105 | BOOL autoReset = FALSE;
|
---|
1106 | hardDisk->COMGETTER(AutoReset)(&autoReset);
|
---|
1107 | RTPrintf("Auto-Reset: %s\n", autoReset ? "on" : "off");
|
---|
1108 | }
|
---|
1109 | }
|
---|
1110 | while (0);
|
---|
1111 |
|
---|
1112 | if (unknown)
|
---|
1113 | {
|
---|
1114 | /* close the unknown hard disk to forget it again */
|
---|
1115 | hardDisk->Close();
|
---|
1116 | }
|
---|
1117 |
|
---|
1118 | return SUCCEEDED(rc) ? 0 : 1;
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | static const RTGETOPTDEF g_aCloseMediumOptions[] =
|
---|
1122 | {
|
---|
1123 | { "disk", 'd', RTGETOPT_REQ_NOTHING },
|
---|
1124 | { "dvd", 'D', RTGETOPT_REQ_NOTHING },
|
---|
1125 | { "floppy", 'f', RTGETOPT_REQ_NOTHING },
|
---|
1126 | { "--delete", 'r', RTGETOPT_REQ_NOTHING },
|
---|
1127 | };
|
---|
1128 |
|
---|
1129 | int handleCloseMedium(HandlerArg *a)
|
---|
1130 | {
|
---|
1131 | HRESULT rc = S_OK;
|
---|
1132 | enum {
|
---|
1133 | CMD_NONE,
|
---|
1134 | CMD_DISK,
|
---|
1135 | CMD_DVD,
|
---|
1136 | CMD_FLOPPY
|
---|
1137 | } cmd = CMD_NONE;
|
---|
1138 | const char *FilenameOrUuid = NULL;
|
---|
1139 | bool fDelete = false;
|
---|
1140 |
|
---|
1141 | int c;
|
---|
1142 | RTGETOPTUNION ValueUnion;
|
---|
1143 | RTGETOPTSTATE GetState;
|
---|
1144 | // start at 0 because main() has hacked both the argc and argv given to us
|
---|
1145 | RTGetOptInit(&GetState, a->argc, a->argv, g_aCloseMediumOptions, RT_ELEMENTS(g_aCloseMediumOptions),
|
---|
1146 | 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
|
---|
1147 | while ((c = RTGetOpt(&GetState, &ValueUnion)))
|
---|
1148 | {
|
---|
1149 | switch (c)
|
---|
1150 | {
|
---|
1151 | case 'd': // disk
|
---|
1152 | if (cmd != CMD_NONE)
|
---|
1153 | return errorSyntax(USAGE_CLOSEMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
|
---|
1154 | cmd = CMD_DISK;
|
---|
1155 | break;
|
---|
1156 |
|
---|
1157 | case 'D': // DVD
|
---|
1158 | if (cmd != CMD_NONE)
|
---|
1159 | return errorSyntax(USAGE_CLOSEMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
|
---|
1160 | cmd = CMD_DVD;
|
---|
1161 | break;
|
---|
1162 |
|
---|
1163 | case 'f': // floppy
|
---|
1164 | if (cmd != CMD_NONE)
|
---|
1165 | return errorSyntax(USAGE_CLOSEMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
|
---|
1166 | cmd = CMD_FLOPPY;
|
---|
1167 | break;
|
---|
1168 |
|
---|
1169 | case 'r': // --delete
|
---|
1170 | fDelete = true;
|
---|
1171 | break;
|
---|
1172 |
|
---|
1173 | case VINF_GETOPT_NOT_OPTION:
|
---|
1174 | if (!FilenameOrUuid)
|
---|
1175 | FilenameOrUuid = ValueUnion.psz;
|
---|
1176 | else
|
---|
1177 | return errorSyntax(USAGE_CLOSEMEDIUM, "Invalid parameter '%s'", ValueUnion.psz);
|
---|
1178 | break;
|
---|
1179 |
|
---|
1180 | default:
|
---|
1181 | if (c > 0)
|
---|
1182 | {
|
---|
1183 | if (RT_C_IS_PRINT(c))
|
---|
1184 | return errorSyntax(USAGE_CLOSEMEDIUM, "Invalid option -%c", c);
|
---|
1185 | else
|
---|
1186 | return errorSyntax(USAGE_CLOSEMEDIUM, "Invalid option case %i", c);
|
---|
1187 | }
|
---|
1188 | else if (c == VERR_GETOPT_UNKNOWN_OPTION)
|
---|
1189 | return errorSyntax(USAGE_CLOSEMEDIUM, "unknown option: %s\n", ValueUnion.psz);
|
---|
1190 | else if (ValueUnion.pDef)
|
---|
1191 | return errorSyntax(USAGE_CLOSEMEDIUM, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
|
---|
1192 | else
|
---|
1193 | return errorSyntax(USAGE_CLOSEMEDIUM, "error: %Rrs", c);
|
---|
1194 | }
|
---|
1195 | }
|
---|
1196 |
|
---|
1197 | /* check for required options */
|
---|
1198 | if (cmd == CMD_NONE)
|
---|
1199 | return errorSyntax(USAGE_CLOSEMEDIUM, "Command variant disk/dvd/floppy required");
|
---|
1200 | if (!FilenameOrUuid)
|
---|
1201 | return errorSyntax(USAGE_CLOSEMEDIUM, "Disk name or UUID required");
|
---|
1202 |
|
---|
1203 | ComPtr<IMedium> medium;
|
---|
1204 |
|
---|
1205 | if (cmd == CMD_DISK)
|
---|
1206 | rc = findMedium(a, FilenameOrUuid, DeviceType_HardDisk, false /* fSilent */, medium);
|
---|
1207 | else if (cmd == CMD_DVD)
|
---|
1208 | rc = findMedium(a, FilenameOrUuid, DeviceType_DVD, false /* fSilent */, medium);
|
---|
1209 | else if (cmd == CMD_FLOPPY)
|
---|
1210 | rc = findMedium(a, FilenameOrUuid, DeviceType_Floppy, false /* fSilent */, medium);
|
---|
1211 |
|
---|
1212 | if (SUCCEEDED(rc) && medium)
|
---|
1213 | {
|
---|
1214 | if (fDelete)
|
---|
1215 | {
|
---|
1216 | ComPtr<IProgress> progress;
|
---|
1217 | CHECK_ERROR(medium, DeleteStorage(progress.asOutParam()));
|
---|
1218 | if (SUCCEEDED(rc))
|
---|
1219 | {
|
---|
1220 | rc = showProgress(progress);
|
---|
1221 | CHECK_PROGRESS_ERROR(progress, ("Failed to delete medium"));
|
---|
1222 | }
|
---|
1223 | else
|
---|
1224 | RTMsgError("Failed to delete medium. Error code %Rrc", rc);
|
---|
1225 | }
|
---|
1226 | CHECK_ERROR(medium, Close());
|
---|
1227 | }
|
---|
1228 |
|
---|
1229 | return SUCCEEDED(rc) ? 0 : 1;
|
---|
1230 | }
|
---|
1231 | #endif /* !VBOX_ONLY_DOCS */
|
---|