VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxManage/VBoxManageDisk.cpp@ 73469

Last change on this file since 73469 was 73029, checked in by vboxsync, 6 years ago

VBoxManageDisk.cpp: make gcc happy

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 85.1 KB
Line 
1/* $Id: VBoxManageDisk.cpp 73029 2018-07-10 10:14:16Z vboxsync $ */
2/** @file
3 * VBoxManage - The disk/medium related commands.
4 */
5
6/*
7 * Copyright (C) 2006-2017 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/*********************************************************************************************************************************
22* Header Files *
23*********************************************************************************************************************************/
24#include <VBox/com/com.h>
25#include <VBox/com/array.h>
26#include <VBox/com/ErrorInfo.h>
27#include <VBox/com/errorprint.h>
28#include <VBox/com/VirtualBox.h>
29
30#include <iprt/asm.h>
31#include <iprt/file.h>
32#include <iprt/path.h>
33#include <iprt/param.h>
34#include <iprt/stream.h>
35#include <iprt/string.h>
36#include <iprt/ctype.h>
37#include <iprt/getopt.h>
38#include <VBox/log.h>
39#include <VBox/vd.h>
40
41#include "VBoxManage.h"
42using namespace com;
43
44/** Medium category. */
45typedef enum MEDIUMCATEGORY
46{
47 MEDIUMCATEGORY_NONE = 0,
48 MEDIUMCATEGORY_DISK,
49 MEDIUMCATEGORY_DVD,
50 MEDIUMCATEGORY_FLOPPY
51} MEDIUMCATEGORY;
52
53
54
55// funcs
56///////////////////////////////////////////////////////////////////////////////
57
58
59static DECLCALLBACK(void) handleVDError(void *pvUser, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va)
60{
61 RT_NOREF(pvUser);
62 RTMsgErrorV(pszFormat, va);
63 RTMsgError("Error code %Rrc at %s(%u) in function %s", rc, RT_SRC_POS_ARGS);
64}
65
66static int parseMediumVariant(const char *psz, MediumVariant_T *pMediumVariant)
67{
68 int rc = VINF_SUCCESS;
69 unsigned uMediumVariant = (unsigned)(*pMediumVariant);
70 while (psz && *psz && RT_SUCCESS(rc))
71 {
72 size_t len;
73 const char *pszComma = strchr(psz, ',');
74 if (pszComma)
75 len = pszComma - psz;
76 else
77 len = strlen(psz);
78 if (len > 0)
79 {
80 // Parsing is intentionally inconsistent: "standard" resets the
81 // variant, whereas the other flags are cumulative.
82 if (!RTStrNICmp(psz, "standard", len))
83 uMediumVariant = MediumVariant_Standard;
84 else if ( !RTStrNICmp(psz, "fixed", len)
85 || !RTStrNICmp(psz, "static", len))
86 uMediumVariant |= MediumVariant_Fixed;
87 else if (!RTStrNICmp(psz, "Diff", len))
88 uMediumVariant |= MediumVariant_Diff;
89 else if (!RTStrNICmp(psz, "split2g", len))
90 uMediumVariant |= MediumVariant_VmdkSplit2G;
91 else if ( !RTStrNICmp(psz, "stream", len)
92 || !RTStrNICmp(psz, "streamoptimized", len))
93 uMediumVariant |= MediumVariant_VmdkStreamOptimized;
94 else if (!RTStrNICmp(psz, "esx", len))
95 uMediumVariant |= MediumVariant_VmdkESX;
96 else if (!RTStrNICmp(psz, "formatted", len))
97 uMediumVariant |= MediumVariant_Formatted;
98 else
99 rc = VERR_PARSE_ERROR;
100 }
101 if (pszComma)
102 psz += len + 1;
103 else
104 psz += len;
105 }
106
107 if (RT_SUCCESS(rc))
108 *pMediumVariant = (MediumVariant_T)uMediumVariant;
109 return rc;
110}
111
112int parseMediumType(const char *psz, MediumType_T *penmMediumType)
113{
114 int rc = VINF_SUCCESS;
115 MediumType_T enmMediumType = MediumType_Normal;
116 if (!RTStrICmp(psz, "normal"))
117 enmMediumType = MediumType_Normal;
118 else if (!RTStrICmp(psz, "immutable"))
119 enmMediumType = MediumType_Immutable;
120 else if (!RTStrICmp(psz, "writethrough"))
121 enmMediumType = MediumType_Writethrough;
122 else if (!RTStrICmp(psz, "shareable"))
123 enmMediumType = MediumType_Shareable;
124 else if (!RTStrICmp(psz, "readonly"))
125 enmMediumType = MediumType_Readonly;
126 else if (!RTStrICmp(psz, "multiattach"))
127 enmMediumType = MediumType_MultiAttach;
128 else
129 rc = VERR_PARSE_ERROR;
130
131 if (RT_SUCCESS(rc))
132 *penmMediumType = enmMediumType;
133 return rc;
134}
135
136/** @todo move this into getopt, as getting bool values is generic */
137int parseBool(const char *psz, bool *pb)
138{
139 int rc = VINF_SUCCESS;
140 if ( !RTStrICmp(psz, "on")
141 || !RTStrICmp(psz, "yes")
142 || !RTStrICmp(psz, "true")
143 || !RTStrICmp(psz, "1")
144 || !RTStrICmp(psz, "enable")
145 || !RTStrICmp(psz, "enabled"))
146 {
147 *pb = true;
148 }
149 else if ( !RTStrICmp(psz, "off")
150 || !RTStrICmp(psz, "no")
151 || !RTStrICmp(psz, "false")
152 || !RTStrICmp(psz, "0")
153 || !RTStrICmp(psz, "disable")
154 || !RTStrICmp(psz, "disabled"))
155 {
156 *pb = false;
157 }
158 else
159 rc = VERR_PARSE_ERROR;
160
161 return rc;
162}
163
164HRESULT openMedium(HandlerArg *a, const char *pszFilenameOrUuid,
165 DeviceType_T enmDevType, AccessMode_T enmAccessMode,
166 ComPtr<IMedium> &pMedium, bool fForceNewUuidOnOpen,
167 bool fSilent)
168{
169 HRESULT rc;
170 Guid id(pszFilenameOrUuid);
171 char szFilenameAbs[RTPATH_MAX] = "";
172
173 /* If it is no UUID, convert the filename to an absolute one. */
174 if (!id.isValid())
175 {
176 int irc = RTPathAbs(pszFilenameOrUuid, szFilenameAbs, sizeof(szFilenameAbs));
177 if (RT_FAILURE(irc))
178 {
179 if (!fSilent)
180 RTMsgError("Cannot convert filename \"%s\" to absolute path", pszFilenameOrUuid);
181 return E_FAIL;
182 }
183 pszFilenameOrUuid = szFilenameAbs;
184 }
185
186 if (!fSilent)
187 CHECK_ERROR(a->virtualBox, OpenMedium(Bstr(pszFilenameOrUuid).raw(),
188 enmDevType,
189 enmAccessMode,
190 fForceNewUuidOnOpen,
191 pMedium.asOutParam()));
192 else
193 rc = a->virtualBox->OpenMedium(Bstr(pszFilenameOrUuid).raw(),
194 enmDevType,
195 enmAccessMode,
196 fForceNewUuidOnOpen,
197 pMedium.asOutParam());
198
199 return rc;
200}
201
202static HRESULT createMedium(HandlerArg *a, const char *pszFormat,
203 const char *pszFilename, DeviceType_T enmDevType,
204 AccessMode_T enmAccessMode, ComPtr<IMedium> &pMedium)
205{
206 HRESULT rc;
207 char szFilenameAbs[RTPATH_MAX] = "";
208
209 /** @todo laziness shortcut. should really check the MediumFormatCapabilities */
210 if (RTStrICmp(pszFormat, "iSCSI"))
211 {
212 int irc = RTPathAbs(pszFilename, szFilenameAbs, sizeof(szFilenameAbs));
213 if (RT_FAILURE(irc))
214 {
215 RTMsgError("Cannot convert filename \"%s\" to absolute path", pszFilename);
216 return E_FAIL;
217 }
218 pszFilename = szFilenameAbs;
219 }
220
221 CHECK_ERROR(a->virtualBox, CreateMedium(Bstr(pszFormat).raw(),
222 Bstr(pszFilename).raw(),
223 enmAccessMode,
224 enmDevType,
225 pMedium.asOutParam()));
226 return rc;
227}
228
229static const RTGETOPTDEF g_aCreateMediumOptions[] =
230{
231 { "disk", 'H', RTGETOPT_REQ_NOTHING },
232 { "dvd", 'D', RTGETOPT_REQ_NOTHING },
233 { "floppy", 'L', RTGETOPT_REQ_NOTHING },
234 { "--filename", 'f', RTGETOPT_REQ_STRING },
235 { "-filename", 'f', RTGETOPT_REQ_STRING }, // deprecated
236 { "--diffparent", 'd', RTGETOPT_REQ_STRING },
237 { "--size", 's', RTGETOPT_REQ_UINT64 },
238 { "-size", 's', RTGETOPT_REQ_UINT64 }, // deprecated
239 { "--sizebyte", 'S', RTGETOPT_REQ_UINT64 },
240 { "--format", 'o', RTGETOPT_REQ_STRING },
241 { "-format", 'o', RTGETOPT_REQ_STRING }, // deprecated
242 { "--static", 'F', RTGETOPT_REQ_NOTHING },
243 { "-static", 'F', RTGETOPT_REQ_NOTHING }, // deprecated
244 { "--variant", 'm', RTGETOPT_REQ_STRING },
245 { "-variant", 'm', RTGETOPT_REQ_STRING }, // deprecated
246};
247
248RTEXITCODE handleCreateMedium(HandlerArg *a)
249{
250 HRESULT rc;
251 int vrc;
252 const char *filename = NULL;
253 const char *diffparent = NULL;
254 uint64_t size = 0;
255 enum {
256 CMD_NONE,
257 CMD_DISK,
258 CMD_DVD,
259 CMD_FLOPPY
260 } cmd = CMD_NONE;
261 const char *format = NULL;
262 bool fBase = true;
263 MediumVariant_T enmMediumVariant = MediumVariant_Standard;
264
265 int c;
266 RTGETOPTUNION ValueUnion;
267 RTGETOPTSTATE GetState;
268 // start at 0 because main() has hacked both the argc and argv given to us
269 RTGetOptInit(&GetState, a->argc, a->argv, g_aCreateMediumOptions, RT_ELEMENTS(g_aCreateMediumOptions),
270 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
271 while ((c = RTGetOpt(&GetState, &ValueUnion)))
272 {
273 switch (c)
274 {
275 case 'H': // disk
276 if (cmd != CMD_NONE)
277 return errorSyntax(USAGE_CREATEMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
278 cmd = CMD_DISK;
279 break;
280
281 case 'D': // DVD
282 if (cmd != CMD_NONE)
283 return errorSyntax(USAGE_CREATEMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
284 cmd = CMD_DVD;
285 break;
286
287 case 'L': // floppy
288 if (cmd != CMD_NONE)
289 return errorSyntax(USAGE_CREATEMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
290 cmd = CMD_FLOPPY;
291 break;
292
293 case 'f': // --filename
294 filename = ValueUnion.psz;
295 break;
296
297 case 'd': // --diffparent
298 diffparent = ValueUnion.psz;
299 fBase = false;
300 break;
301
302 case 's': // --size
303 size = ValueUnion.u64 * _1M;
304 break;
305
306 case 'S': // --sizebyte
307 size = ValueUnion.u64;
308 break;
309
310 case 'o': // --format
311 format = ValueUnion.psz;
312 break;
313
314 case 'F': // --static ("fixed"/"flat")
315 {
316 unsigned uMediumVariant = (unsigned)enmMediumVariant;
317 uMediumVariant |= MediumVariant_Fixed;
318 enmMediumVariant = (MediumVariant_T)uMediumVariant;
319 break;
320 }
321
322 case 'm': // --variant
323 vrc = parseMediumVariant(ValueUnion.psz, &enmMediumVariant);
324 if (RT_FAILURE(vrc))
325 return errorArgument("Invalid medium variant '%s'", ValueUnion.psz);
326 break;
327
328 case VINF_GETOPT_NOT_OPTION:
329 return errorSyntax(USAGE_CREATEMEDIUM, "Invalid parameter '%s'", ValueUnion.psz);
330
331 default:
332 if (c > 0)
333 {
334 if (RT_C_IS_PRINT(c))
335 return errorSyntax(USAGE_CREATEMEDIUM, "Invalid option -%c", c);
336 else
337 return errorSyntax(USAGE_CREATEMEDIUM, "Invalid option case %i", c);
338 }
339 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
340 return errorSyntax(USAGE_CREATEMEDIUM, "unknown option: %s\n", ValueUnion.psz);
341 else if (ValueUnion.pDef)
342 return errorSyntax(USAGE_CREATEMEDIUM, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
343 else
344 return errorSyntax(USAGE_CREATEMEDIUM, "error: %Rrs", c);
345 }
346 }
347
348 /* check the outcome */
349 if (cmd == CMD_NONE)
350 cmd = CMD_DISK;
351 ComPtr<IMedium> pParentMedium;
352 if (fBase)
353 {
354 if ( !filename
355 || !*filename
356 || size == 0)
357 return errorSyntax(USAGE_CREATEMEDIUM, "Parameters --filename and --size are required");
358 if (!format || !*format)
359 {
360 if (cmd == CMD_DISK)
361 format = "VDI";
362 else if (cmd == CMD_DVD || cmd == CMD_FLOPPY)
363 {
364 format = "RAW";
365 unsigned uMediumVariant = (unsigned)enmMediumVariant;
366 uMediumVariant |= MediumVariant_Fixed;
367 enmMediumVariant = (MediumVariant_T)uMediumVariant;
368 }
369 }
370 }
371 else
372 {
373 if ( !filename
374 || !*filename)
375 return errorSyntax(USAGE_CREATEMEDIUM, "Parameters --filename is required");
376 size = 0;
377 if (cmd != CMD_DISK)
378 return errorSyntax(USAGE_CREATEMEDIUM, "Creating a differencing medium is only supported for hard disks");
379 enmMediumVariant = MediumVariant_Diff;
380 if (!format || !*format)
381 {
382 const char *pszExt = RTPathSuffix(filename);
383 /* Skip over . if there is an extension. */
384 if (pszExt)
385 pszExt++;
386 if (!pszExt || !*pszExt)
387 format = "VDI";
388 else
389 format = pszExt;
390 }
391 rc = openMedium(a, diffparent, DeviceType_HardDisk,
392 AccessMode_ReadWrite, pParentMedium,
393 false /* fForceNewUuidOnOpen */, false /* fSilent */);
394 if (FAILED(rc))
395 return RTEXITCODE_FAILURE;
396 if (pParentMedium.isNull())
397 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Invalid parent hard disk reference, avoiding crash");
398 MediumState_T state;
399 CHECK_ERROR(pParentMedium, COMGETTER(State)(&state));
400 if (FAILED(rc))
401 return RTEXITCODE_FAILURE;
402 if (state == MediumState_Inaccessible)
403 {
404 CHECK_ERROR(pParentMedium, RefreshState(&state));
405 if (FAILED(rc))
406 return RTEXITCODE_FAILURE;
407 }
408 }
409 /* check for filename extension */
410 /** @todo use IMediumFormat to cover all extensions generically */
411 Utf8Str strName(filename);
412 if (!RTPathHasSuffix(strName.c_str()))
413 {
414 Utf8Str strFormat(format);
415 if (cmd == CMD_DISK)
416 {
417 if (strFormat.compare("vmdk", RTCString::CaseInsensitive) == 0)
418 strName.append(".vmdk");
419 else if (strFormat.compare("vhd", RTCString::CaseInsensitive) == 0)
420 strName.append(".vhd");
421 else
422 strName.append(".vdi");
423 } else if (cmd == CMD_DVD)
424 strName.append(".iso");
425 else if (cmd == CMD_FLOPPY)
426 strName.append(".img");
427 filename = strName.c_str();
428 }
429
430 ComPtr<IMedium> pMedium;
431 if (cmd == CMD_DISK)
432 rc = createMedium(a, format, filename, DeviceType_HardDisk,
433 AccessMode_ReadWrite, pMedium);
434 else if (cmd == CMD_DVD)
435 rc = createMedium(a, format, filename, DeviceType_DVD,
436 AccessMode_ReadOnly, pMedium);
437 else if (cmd == CMD_FLOPPY)
438 rc = createMedium(a, format, filename, DeviceType_Floppy,
439 AccessMode_ReadWrite, pMedium);
440 else
441 rc = E_INVALIDARG; /* cannot happen but make gcc happy */
442
443 if (SUCCEEDED(rc) && pMedium)
444 {
445 ComPtr<IProgress> pProgress;
446 com::SafeArray<MediumVariant_T> l_variants(sizeof(MediumVariant_T)*8);
447
448 for (ULONG i = 0; i < l_variants.size(); ++i)
449 {
450 ULONG temp = enmMediumVariant;
451 temp &= 1<<i;
452 l_variants [i] = (MediumVariant_T)temp;
453 }
454
455 if (fBase)
456 CHECK_ERROR(pMedium, CreateBaseStorage(size, ComSafeArrayAsInParam(l_variants), pProgress.asOutParam()));
457 else
458 CHECK_ERROR(pParentMedium, CreateDiffStorage(pMedium, ComSafeArrayAsInParam(l_variants), pProgress.asOutParam()));
459 if (SUCCEEDED(rc) && pProgress)
460 {
461 rc = showProgress(pProgress);
462 CHECK_PROGRESS_ERROR(pProgress, ("Failed to create medium"));
463 }
464 }
465
466 if (SUCCEEDED(rc) && pMedium)
467 {
468 Bstr uuid;
469 CHECK_ERROR(pMedium, COMGETTER(Id)(uuid.asOutParam()));
470 RTPrintf("Medium created. UUID: %s\n", Utf8Str(uuid).c_str());
471
472 //CHECK_ERROR(pMedium, Close());
473 }
474 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
475}
476
477static const RTGETOPTDEF g_aModifyMediumOptions[] =
478{
479 { "disk", 'H', RTGETOPT_REQ_NOTHING },
480 { "dvd", 'D', RTGETOPT_REQ_NOTHING },
481 { "floppy", 'L', RTGETOPT_REQ_NOTHING },
482 { "--type", 't', RTGETOPT_REQ_STRING },
483 { "-type", 't', RTGETOPT_REQ_STRING }, // deprecated
484 { "settype", 't', RTGETOPT_REQ_STRING }, // deprecated
485 { "--autoreset", 'z', RTGETOPT_REQ_STRING },
486 { "-autoreset", 'z', RTGETOPT_REQ_STRING }, // deprecated
487 { "autoreset", 'z', RTGETOPT_REQ_STRING }, // deprecated
488 { "--property", 'p', RTGETOPT_REQ_STRING },
489 { "--compact", 'c', RTGETOPT_REQ_NOTHING },
490 { "-compact", 'c', RTGETOPT_REQ_NOTHING }, // deprecated
491 { "compact", 'c', RTGETOPT_REQ_NOTHING }, // deprecated
492 { "--resize", 'r', RTGETOPT_REQ_UINT64 },
493 { "--resizebyte", 'R', RTGETOPT_REQ_UINT64 },
494 { "--move", 'm', RTGETOPT_REQ_STRING },
495 { "--description", 'd', RTGETOPT_REQ_STRING }
496};
497
498RTEXITCODE handleModifyMedium(HandlerArg *a)
499{
500 HRESULT rc;
501 int vrc;
502 enum {
503 CMD_NONE,
504 CMD_DISK,
505 CMD_DVD,
506 CMD_FLOPPY
507 } cmd = CMD_NONE;
508 ComPtr<IMedium> pMedium;
509 MediumType_T enmMediumType = MediumType_Normal; /* Shut up MSC */
510 bool AutoReset = false;
511 SafeArray<BSTR> mediumPropNames;
512 SafeArray<BSTR> mediumPropValues;
513 bool fModifyMediumType = false;
514 bool fModifyAutoReset = false;
515 bool fModifyProperties = false;
516 bool fModifyCompact = false;
517 bool fModifyResize = false;
518 bool fModifyResizeMB = false;
519 bool fModifyLocation = false;
520 bool fModifyDescription = false;
521 uint64_t cbResize = 0;
522 const char *pszFilenameOrUuid = NULL;
523 char *pszNewLocation = NULL;
524
525 int c;
526 RTGETOPTUNION ValueUnion;
527 RTGETOPTSTATE GetState;
528 // start at 0 because main() has hacked both the argc and argv given to us
529 RTGetOptInit(&GetState, a->argc, a->argv, g_aModifyMediumOptions, RT_ELEMENTS(g_aModifyMediumOptions),
530 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
531 while ((c = RTGetOpt(&GetState, &ValueUnion)))
532 {
533 switch (c)
534 {
535 case 'H': // disk
536 if (cmd != CMD_NONE)
537 return errorSyntax(USAGE_MODIFYMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
538 cmd = CMD_DISK;
539 break;
540
541 case 'D': // DVD
542 if (cmd != CMD_NONE)
543 return errorSyntax(USAGE_MODIFYMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
544 cmd = CMD_DVD;
545 break;
546
547 case 'L': // floppy
548 if (cmd != CMD_NONE)
549 return errorSyntax(USAGE_MODIFYMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
550 cmd = CMD_FLOPPY;
551 break;
552
553 case 't': // --type
554 vrc = parseMediumType(ValueUnion.psz, &enmMediumType);
555 if (RT_FAILURE(vrc))
556 return errorArgument("Invalid medium type '%s'", ValueUnion.psz);
557 fModifyMediumType = true;
558 break;
559
560 case 'z': // --autoreset
561 vrc = parseBool(ValueUnion.psz, &AutoReset);
562 if (RT_FAILURE(vrc))
563 return errorArgument("Invalid autoreset parameter '%s'", ValueUnion.psz);
564 fModifyAutoReset = true;
565 break;
566
567 case 'p': // --property
568 {
569 /* Parse 'name=value' */
570 char *pszProperty = RTStrDup(ValueUnion.psz);
571 if (pszProperty)
572 {
573 char *pDelimiter = strchr(pszProperty, '=');
574 if (pDelimiter)
575 {
576 *pDelimiter = '\0';
577
578 Bstr bstrName(pszProperty);
579 Bstr bstrValue(&pDelimiter[1]);
580 bstrName.detachTo(mediumPropNames.appendedRaw());
581 bstrValue.detachTo(mediumPropValues.appendedRaw());
582 fModifyProperties = true;
583 }
584 else
585 {
586 errorArgument("Invalid --property argument '%s'", ValueUnion.psz);
587 rc = E_FAIL;
588 }
589 RTStrFree(pszProperty);
590 }
591 else
592 {
593 RTStrmPrintf(g_pStdErr, "Error: Failed to allocate memory for medium property '%s'\n", ValueUnion.psz);
594 rc = E_FAIL;
595 }
596 break;
597 }
598
599 case 'c': // --compact
600 fModifyCompact = true;
601 break;
602
603 case 'r': // --resize
604 cbResize = ValueUnion.u64 * _1M;
605 fModifyResize = true;
606 fModifyResizeMB = true; // do sanity check!
607 break;
608
609 case 'R': // --resizebyte
610 cbResize = ValueUnion.u64;
611 fModifyResize = true;
612 break;
613
614 case 'm': // --move
615 /* Get a new location */
616 pszNewLocation = RTPathAbsDup(ValueUnion.psz);
617 fModifyLocation = true;
618 break;
619
620 case 'd': // --description
621 /* Get a new description */
622 pszNewLocation = RTStrDup(ValueUnion.psz);
623 fModifyDescription = true;
624 break;
625
626 case VINF_GETOPT_NOT_OPTION:
627 if (!pszFilenameOrUuid)
628 pszFilenameOrUuid = ValueUnion.psz;
629 else
630 return errorSyntax(USAGE_MODIFYMEDIUM, "Invalid parameter '%s'", ValueUnion.psz);
631 break;
632
633 default:
634 if (c > 0)
635 {
636 if (RT_C_IS_PRINT(c))
637 return errorSyntax(USAGE_MODIFYMEDIUM, "Invalid option -%c", c);
638 else
639 return errorSyntax(USAGE_MODIFYMEDIUM, "Invalid option case %i", c);
640 }
641 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
642 return errorSyntax(USAGE_MODIFYMEDIUM, "unknown option: %s\n", ValueUnion.psz);
643 else if (ValueUnion.pDef)
644 return errorSyntax(USAGE_MODIFYMEDIUM, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
645 else
646 return errorSyntax(USAGE_MODIFYMEDIUM, "error: %Rrs", c);
647 }
648 }
649
650 if (cmd == CMD_NONE)
651 cmd = CMD_DISK;
652
653 if (!pszFilenameOrUuid)
654 return errorSyntax(USAGE_MODIFYMEDIUM, "Medium name or UUID required");
655
656 if (!fModifyMediumType
657 && !fModifyAutoReset
658 && !fModifyProperties
659 && !fModifyCompact
660 && !fModifyResize
661 && !fModifyLocation
662 && !fModifyDescription)
663 return errorSyntax(USAGE_MODIFYMEDIUM, "No operation specified");
664
665 /* Always open the medium if necessary, there is no other way. */
666 if (cmd == CMD_DISK)
667 rc = openMedium(a, pszFilenameOrUuid, DeviceType_HardDisk,
668 AccessMode_ReadWrite, pMedium,
669 false /* fForceNewUuidOnOpen */, false /* fSilent */);
670 else if (cmd == CMD_DVD)
671 rc = openMedium(a, pszFilenameOrUuid, DeviceType_DVD,
672 AccessMode_ReadOnly, pMedium,
673 false /* fForceNewUuidOnOpen */, false /* fSilent */);
674 else if (cmd == CMD_FLOPPY)
675 rc = openMedium(a, pszFilenameOrUuid, DeviceType_Floppy,
676 AccessMode_ReadWrite, pMedium,
677 false /* fForceNewUuidOnOpen */, false /* fSilent */);
678 else
679 rc = E_INVALIDARG; /* cannot happen but make gcc happy */
680 if (FAILED(rc))
681 return RTEXITCODE_FAILURE;
682 if (pMedium.isNull())
683 {
684 RTMsgError("Invalid medium reference, avoiding crash");
685 return RTEXITCODE_FAILURE;
686 }
687
688 if ( fModifyResize
689 && fModifyResizeMB)
690 {
691 // Sanity check
692 //
693 // In general users should know what they do but in this case users have no
694 // alternative to VBoxManage. If happens that one wants to resize the disk
695 // and uses --resize and does not consider that this parameter expects the
696 // new medium size in MB not Byte. If the operation is started and then
697 // aborted by the user, the result is most likely a medium which doesn't
698 // work anymore.
699 MediumState_T state;
700 pMedium->RefreshState(&state);
701 LONG64 logicalSize;
702 pMedium->COMGETTER(LogicalSize)(&logicalSize);
703 if (cbResize > (uint64_t)logicalSize * 1000)
704 {
705 RTMsgError("Error: Attempt to resize the medium from %RU64.%RU64 MB to %RU64.%RU64 MB. Use --resizebyte if this is intended!\n",
706 logicalSize / _1M, (logicalSize % _1M) / (_1M / 10), cbResize / _1M, (cbResize % _1M) / (_1M / 10));
707 return RTEXITCODE_FAILURE;
708 }
709 }
710
711 if (fModifyMediumType)
712 {
713 MediumType_T enmCurrMediumType;
714 CHECK_ERROR(pMedium, COMGETTER(Type)(&enmCurrMediumType));
715
716 if (enmCurrMediumType != enmMediumType)
717 CHECK_ERROR(pMedium, COMSETTER(Type)(enmMediumType));
718 }
719
720 if (fModifyAutoReset)
721 {
722 CHECK_ERROR(pMedium, COMSETTER(AutoReset)(AutoReset));
723 }
724
725 if (fModifyProperties)
726 {
727 CHECK_ERROR(pMedium, SetProperties(ComSafeArrayAsInParam(mediumPropNames), ComSafeArrayAsInParam(mediumPropValues)));
728 }
729
730 if (fModifyCompact)
731 {
732 ComPtr<IProgress> pProgress;
733 CHECK_ERROR(pMedium, Compact(pProgress.asOutParam()));
734 if (SUCCEEDED(rc))
735 rc = showProgress(pProgress);
736 if (FAILED(rc))
737 {
738 if (rc == E_NOTIMPL)
739 RTMsgError("Compact medium operation is not implemented!");
740 else if (rc == VBOX_E_NOT_SUPPORTED)
741 RTMsgError("Compact medium operation for this format is not implemented yet!");
742 else if (!pProgress.isNull())
743 CHECK_PROGRESS_ERROR(pProgress, ("Failed to compact medium"));
744 else
745 RTMsgError("Failed to compact medium!");
746 }
747 }
748
749 if (fModifyResize)
750 {
751 ComPtr<IProgress> pProgress;
752 CHECK_ERROR(pMedium, Resize(cbResize, pProgress.asOutParam()));
753 if (SUCCEEDED(rc))
754 rc = showProgress(pProgress);
755 if (FAILED(rc))
756 {
757 if (rc == E_NOTIMPL)
758 RTMsgError("Resize medium operation is not implemented!");
759 else if (rc == VBOX_E_NOT_SUPPORTED)
760 RTMsgError("Resize medium operation for this format is not implemented yet!");
761 else if (!pProgress.isNull())
762 CHECK_PROGRESS_ERROR(pProgress, ("Failed to resize medium"));
763 else
764 RTMsgError("Failed to resize medium!");
765 }
766 }
767
768 if (fModifyLocation)
769 {
770 do
771 {
772 ComPtr<IProgress> pProgress;
773 Utf8Str strLocation(pszNewLocation);
774 RTStrFree(pszNewLocation);
775 CHECK_ERROR(pMedium, SetLocation(Bstr(strLocation).raw(), pProgress.asOutParam()));
776
777 if (SUCCEEDED(rc) && !pProgress.isNull())
778 {
779 rc = showProgress(pProgress);
780 CHECK_PROGRESS_ERROR(pProgress, ("Failed to move medium"));
781 }
782
783 Bstr uuid;
784 CHECK_ERROR_BREAK(pMedium, COMGETTER(Id)(uuid.asOutParam()));
785
786 RTPrintf("Move medium with UUID %s finished \n", Utf8Str(uuid).c_str());
787 }
788 while (0);
789 }
790
791 if (fModifyDescription)
792 {
793 CHECK_ERROR(pMedium, COMSETTER(Description)(Bstr(pszNewLocation).raw()));
794
795 RTPrintf("Medium description has been changed. \n");
796 }
797
798 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
799}
800
801static const RTGETOPTDEF g_aCloneMediumOptions[] =
802{
803 { "disk", 'd', RTGETOPT_REQ_NOTHING },
804 { "dvd", 'D', RTGETOPT_REQ_NOTHING },
805 { "floppy", 'f', RTGETOPT_REQ_NOTHING },
806 { "--format", 'o', RTGETOPT_REQ_STRING },
807 { "-format", 'o', RTGETOPT_REQ_STRING },
808 { "--static", 'F', RTGETOPT_REQ_NOTHING },
809 { "-static", 'F', RTGETOPT_REQ_NOTHING },
810 { "--existing", 'E', RTGETOPT_REQ_NOTHING },
811 { "--variant", 'm', RTGETOPT_REQ_STRING },
812 { "-variant", 'm', RTGETOPT_REQ_STRING },
813};
814
815RTEXITCODE handleCloneMedium(HandlerArg *a)
816{
817 HRESULT rc;
818 int vrc;
819 enum {
820 CMD_NONE,
821 CMD_DISK,
822 CMD_DVD,
823 CMD_FLOPPY
824 } cmd = CMD_NONE;
825 const char *pszSrc = NULL;
826 const char *pszDst = NULL;
827 Bstr format;
828 MediumVariant_T enmMediumVariant = MediumVariant_Standard;
829 bool fExisting = false;
830
831 int c;
832 RTGETOPTUNION ValueUnion;
833 RTGETOPTSTATE GetState;
834 // start at 0 because main() has hacked both the argc and argv given to us
835 RTGetOptInit(&GetState, a->argc, a->argv, g_aCloneMediumOptions, RT_ELEMENTS(g_aCloneMediumOptions),
836 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
837 while ((c = RTGetOpt(&GetState, &ValueUnion)))
838 {
839 switch (c)
840 {
841 case 'd': // disk
842 if (cmd != CMD_NONE)
843 return errorSyntax(USAGE_CLONEMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
844 cmd = CMD_DISK;
845 break;
846
847 case 'D': // DVD
848 if (cmd != CMD_NONE)
849 return errorSyntax(USAGE_CLONEMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
850 cmd = CMD_DVD;
851 break;
852
853 case 'f': // floppy
854 if (cmd != CMD_NONE)
855 return errorSyntax(USAGE_CLONEMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
856 cmd = CMD_FLOPPY;
857 break;
858
859 case 'o': // --format
860 format = ValueUnion.psz;
861 break;
862
863 case 'F': // --static
864 {
865 unsigned uMediumVariant = (unsigned)enmMediumVariant;
866 uMediumVariant |= MediumVariant_Fixed;
867 enmMediumVariant = (MediumVariant_T)uMediumVariant;
868 break;
869 }
870
871 case 'E': // --existing
872 fExisting = true;
873 break;
874
875 case 'm': // --variant
876 vrc = parseMediumVariant(ValueUnion.psz, &enmMediumVariant);
877 if (RT_FAILURE(vrc))
878 return errorArgument("Invalid medium variant '%s'", ValueUnion.psz);
879 break;
880
881 case VINF_GETOPT_NOT_OPTION:
882 if (!pszSrc)
883 pszSrc = ValueUnion.psz;
884 else if (!pszDst)
885 pszDst = ValueUnion.psz;
886 else
887 return errorSyntax(USAGE_CLONEMEDIUM, "Invalid parameter '%s'", ValueUnion.psz);
888 break;
889
890 default:
891 if (c > 0)
892 {
893 if (RT_C_IS_GRAPH(c))
894 return errorSyntax(USAGE_CLONEMEDIUM, "unhandled option: -%c", c);
895 else
896 return errorSyntax(USAGE_CLONEMEDIUM, "unhandled option: %i", c);
897 }
898 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
899 return errorSyntax(USAGE_CLONEMEDIUM, "unknown option: %s", ValueUnion.psz);
900 else if (ValueUnion.pDef)
901 return errorSyntax(USAGE_CLONEMEDIUM, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
902 else
903 return errorSyntax(USAGE_CLONEMEDIUM, "error: %Rrs", c);
904 }
905 }
906
907 if (cmd == CMD_NONE)
908 cmd = CMD_DISK;
909 if (!pszSrc)
910 return errorSyntax(USAGE_CLONEMEDIUM, "Mandatory UUID or input file parameter missing");
911 if (!pszDst)
912 return errorSyntax(USAGE_CLONEMEDIUM, "Mandatory output file parameter missing");
913 if (fExisting && (!format.isEmpty() || enmMediumVariant != MediumVariant_Standard))
914 return errorSyntax(USAGE_CLONEMEDIUM, "Specified options which cannot be used with --existing");
915
916 ComPtr<IMedium> pSrcMedium;
917 ComPtr<IMedium> pDstMedium;
918
919 if (cmd == CMD_DISK)
920 rc = openMedium(a, pszSrc, DeviceType_HardDisk, AccessMode_ReadOnly, pSrcMedium,
921 false /* fForceNewUuidOnOpen */, false /* fSilent */);
922 else if (cmd == CMD_DVD)
923 rc = openMedium(a, pszSrc, DeviceType_DVD, AccessMode_ReadOnly, pSrcMedium,
924 false /* fForceNewUuidOnOpen */, false /* fSilent */);
925 else if (cmd == CMD_FLOPPY)
926 rc = openMedium(a, pszSrc, DeviceType_Floppy, AccessMode_ReadOnly, pSrcMedium,
927 false /* fForceNewUuidOnOpen */, false /* fSilent */);
928 else
929 rc = E_INVALIDARG; /* cannot happen but make gcc happy */
930 if (FAILED(rc))
931 return RTEXITCODE_FAILURE;
932
933 do
934 {
935 /* open/create destination medium */
936 if (fExisting)
937 {
938 if (cmd == CMD_DISK)
939 rc = openMedium(a, pszDst, DeviceType_HardDisk, AccessMode_ReadWrite, pDstMedium,
940 false /* fForceNewUuidOnOpen */, false /* fSilent */);
941 else if (cmd == CMD_DVD)
942 rc = openMedium(a, pszDst, DeviceType_DVD, AccessMode_ReadOnly, pDstMedium,
943 false /* fForceNewUuidOnOpen */, false /* fSilent */);
944 else if (cmd == CMD_FLOPPY)
945 rc = openMedium(a, pszDst, DeviceType_Floppy, AccessMode_ReadWrite, pDstMedium,
946 false /* fForceNewUuidOnOpen */, false /* fSilent */);
947 if (FAILED(rc))
948 break;
949
950 /* Perform accessibility check now. */
951 MediumState_T state;
952 CHECK_ERROR_BREAK(pDstMedium, RefreshState(&state));
953 CHECK_ERROR_BREAK(pDstMedium, COMGETTER(Format)(format.asOutParam()));
954 }
955 else
956 {
957 /*
958 * In case the format is unspecified check that the source medium supports
959 * image creation and use the same format for the destination image.
960 * Use the default image format if it is not supported.
961 */
962 if (format.isEmpty())
963 {
964 ComPtr<IMediumFormat> pMediumFmt;
965 com::SafeArray<MediumFormatCapabilities_T> l_caps;
966 CHECK_ERROR_BREAK(pSrcMedium, COMGETTER(MediumFormat)(pMediumFmt.asOutParam()));
967 CHECK_ERROR_BREAK(pMediumFmt, COMGETTER(Capabilities)(ComSafeArrayAsOutParam(l_caps)));
968 ULONG caps=0;
969 for (size_t i = 0; i < l_caps.size(); i++)
970 caps |= l_caps[i];
971 if (caps & ( MediumFormatCapabilities_CreateDynamic
972 | MediumFormatCapabilities_CreateFixed))
973 CHECK_ERROR_BREAK(pMediumFmt, COMGETTER(Id)(format.asOutParam()));
974 }
975 Utf8Str strFormat(format);
976 if (cmd == CMD_DISK)
977 rc = createMedium(a, strFormat.c_str(), pszDst, DeviceType_HardDisk,
978 AccessMode_ReadWrite, pDstMedium);
979 else if (cmd == CMD_DVD)
980 rc = createMedium(a, strFormat.c_str(), pszDst, DeviceType_DVD,
981 AccessMode_ReadOnly, pDstMedium);
982 else if (cmd == CMD_FLOPPY)
983 rc = createMedium(a, strFormat.c_str(), pszDst, DeviceType_Floppy,
984 AccessMode_ReadWrite, pDstMedium);
985 if (FAILED(rc))
986 break;
987 }
988
989 ComPtr<IProgress> pProgress;
990 com::SafeArray<MediumVariant_T> l_variants(sizeof(MediumVariant_T)*8);
991
992 for (ULONG i = 0; i < l_variants.size(); ++i)
993 {
994 ULONG temp = enmMediumVariant;
995 temp &= 1<<i;
996 l_variants [i] = (MediumVariant_T)temp;
997 }
998
999 CHECK_ERROR_BREAK(pSrcMedium, CloneTo(pDstMedium, ComSafeArrayAsInParam(l_variants), NULL, pProgress.asOutParam()));
1000
1001 rc = showProgress(pProgress);
1002 CHECK_PROGRESS_ERROR_BREAK(pProgress, ("Failed to clone medium"));
1003
1004 Bstr uuid;
1005 CHECK_ERROR_BREAK(pDstMedium, COMGETTER(Id)(uuid.asOutParam()));
1006
1007 RTPrintf("Clone medium created in format '%ls'. UUID: %s\n",
1008 format.raw(), Utf8Str(uuid).c_str());
1009 }
1010 while (0);
1011
1012 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
1013}
1014
1015static const RTGETOPTDEF g_aConvertFromRawHardDiskOptions[] =
1016{
1017 { "--format", 'o', RTGETOPT_REQ_STRING },
1018 { "-format", 'o', RTGETOPT_REQ_STRING },
1019 { "--static", 'F', RTGETOPT_REQ_NOTHING },
1020 { "-static", 'F', RTGETOPT_REQ_NOTHING },
1021 { "--variant", 'm', RTGETOPT_REQ_STRING },
1022 { "-variant", 'm', RTGETOPT_REQ_STRING },
1023 { "--uuid", 'u', RTGETOPT_REQ_STRING },
1024};
1025
1026RTEXITCODE handleConvertFromRaw(HandlerArg *a)
1027{
1028 int rc = VINF_SUCCESS;
1029 bool fReadFromStdIn = false;
1030 const char *format = "VDI";
1031 const char *srcfilename = NULL;
1032 const char *dstfilename = NULL;
1033 const char *filesize = NULL;
1034 unsigned uImageFlags = VD_IMAGE_FLAGS_NONE;
1035 void *pvBuf = NULL;
1036 RTUUID uuid;
1037 PCRTUUID pUuid = NULL;
1038
1039 int c;
1040 RTGETOPTUNION ValueUnion;
1041 RTGETOPTSTATE GetState;
1042 // start at 0 because main() has hacked both the argc and argv given to us
1043 RTGetOptInit(&GetState, a->argc, a->argv, g_aConvertFromRawHardDiskOptions, RT_ELEMENTS(g_aConvertFromRawHardDiskOptions),
1044 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
1045 while ((c = RTGetOpt(&GetState, &ValueUnion)))
1046 {
1047 switch (c)
1048 {
1049 case 'u': // --uuid
1050 if (RT_FAILURE(RTUuidFromStr(&uuid, ValueUnion.psz)))
1051 return errorSyntax(USAGE_CONVERTFROMRAW, "Invalid UUID '%s'", ValueUnion.psz);
1052 pUuid = &uuid;
1053 break;
1054 case 'o': // --format
1055 format = ValueUnion.psz;
1056 break;
1057
1058 case 'm': // --variant
1059 {
1060 MediumVariant_T enmMediumVariant = MediumVariant_Standard;
1061 rc = parseMediumVariant(ValueUnion.psz, &enmMediumVariant);
1062 if (RT_FAILURE(rc))
1063 return errorArgument("Invalid medium variant '%s'", ValueUnion.psz);
1064 /// @todo cleaner solution than assuming 1:1 mapping?
1065 uImageFlags = (unsigned)enmMediumVariant;
1066 break;
1067 }
1068 case VINF_GETOPT_NOT_OPTION:
1069 if (!srcfilename)
1070 {
1071 srcfilename = ValueUnion.psz;
1072 fReadFromStdIn = !strcmp(srcfilename, "stdin");
1073 }
1074 else if (!dstfilename)
1075 dstfilename = ValueUnion.psz;
1076 else if (fReadFromStdIn && !filesize)
1077 filesize = ValueUnion.psz;
1078 else
1079 return errorSyntax(USAGE_CONVERTFROMRAW, "Invalid parameter '%s'", ValueUnion.psz);
1080 break;
1081
1082 default:
1083 return errorGetOpt(USAGE_CONVERTFROMRAW, c, &ValueUnion);
1084 }
1085 }
1086
1087 if (!srcfilename || !dstfilename || (fReadFromStdIn && !filesize))
1088 return errorSyntax(USAGE_CONVERTFROMRAW, "Incorrect number of parameters");
1089 RTStrmPrintf(g_pStdErr, "Converting from raw image file=\"%s\" to file=\"%s\"...\n",
1090 srcfilename, dstfilename);
1091
1092 PVDISK pDisk = NULL;
1093
1094 PVDINTERFACE pVDIfs = NULL;
1095 VDINTERFACEERROR vdInterfaceError;
1096 vdInterfaceError.pfnError = handleVDError;
1097 vdInterfaceError.pfnMessage = NULL;
1098
1099 rc = VDInterfaceAdd(&vdInterfaceError.Core, "VBoxManage_IError", VDINTERFACETYPE_ERROR,
1100 NULL, sizeof(VDINTERFACEERROR), &pVDIfs);
1101 AssertRC(rc);
1102
1103 /* open raw image file. */
1104 RTFILE File;
1105 if (fReadFromStdIn)
1106 rc = RTFileFromNative(&File, RTFILE_NATIVE_STDIN);
1107 else
1108 rc = RTFileOpen(&File, srcfilename, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
1109 if (RT_FAILURE(rc))
1110 {
1111 RTMsgError("Cannot open file \"%s\": %Rrc", srcfilename, rc);
1112 goto out;
1113 }
1114
1115 uint64_t cbFile;
1116 /* get image size. */
1117 if (fReadFromStdIn)
1118 cbFile = RTStrToUInt64(filesize);
1119 else
1120 rc = RTFileGetSize(File, &cbFile);
1121 if (RT_FAILURE(rc))
1122 {
1123 RTMsgError("Cannot get image size for file \"%s\": %Rrc", srcfilename, rc);
1124 goto out;
1125 }
1126
1127 RTStrmPrintf(g_pStdErr, "Creating %s image with size %RU64 bytes (%RU64MB)...\n",
1128 (uImageFlags & VD_IMAGE_FLAGS_FIXED) ? "fixed" : "dynamic", cbFile, (cbFile + _1M - 1) / _1M);
1129 char pszComment[256];
1130 RTStrPrintf(pszComment, sizeof(pszComment), "Converted image from %s", srcfilename);
1131 rc = VDCreate(pVDIfs, VDTYPE_HDD, &pDisk);
1132 if (RT_FAILURE(rc))
1133 {
1134 RTMsgError("Cannot create the virtual disk container: %Rrc", rc);
1135 goto out;
1136 }
1137
1138 Assert(RT_MIN(cbFile / 512 / 16 / 63, 16383) -
1139 (unsigned int)RT_MIN(cbFile / 512 / 16 / 63, 16383) == 0);
1140 VDGEOMETRY PCHS, LCHS;
1141 PCHS.cCylinders = (unsigned int)RT_MIN(cbFile / 512 / 16 / 63, 16383);
1142 PCHS.cHeads = 16;
1143 PCHS.cSectors = 63;
1144 LCHS.cCylinders = 0;
1145 LCHS.cHeads = 0;
1146 LCHS.cSectors = 0;
1147 rc = VDCreateBase(pDisk, format, dstfilename, cbFile,
1148 uImageFlags, pszComment, &PCHS, &LCHS, pUuid,
1149 VD_OPEN_FLAGS_NORMAL, NULL, NULL);
1150 if (RT_FAILURE(rc))
1151 {
1152 RTMsgError("Cannot create the disk image \"%s\": %Rrc", dstfilename, rc);
1153 goto out;
1154 }
1155
1156 size_t cbBuffer;
1157 cbBuffer = _1M;
1158 pvBuf = RTMemAlloc(cbBuffer);
1159 if (!pvBuf)
1160 {
1161 rc = VERR_NO_MEMORY;
1162 RTMsgError("Out of memory allocating buffers for image \"%s\": %Rrc", dstfilename, rc);
1163 goto out;
1164 }
1165
1166 uint64_t offFile;
1167 offFile = 0;
1168 while (offFile < cbFile)
1169 {
1170 size_t cbRead;
1171 size_t cbToRead;
1172 cbRead = 0;
1173 cbToRead = cbFile - offFile >= (uint64_t)cbBuffer ?
1174 cbBuffer : (size_t)(cbFile - offFile);
1175 rc = RTFileRead(File, pvBuf, cbToRead, &cbRead);
1176 if (RT_FAILURE(rc) || !cbRead)
1177 break;
1178 rc = VDWrite(pDisk, offFile, pvBuf, cbRead);
1179 if (RT_FAILURE(rc))
1180 {
1181 RTMsgError("Failed to write to disk image \"%s\": %Rrc", dstfilename, rc);
1182 goto out;
1183 }
1184 offFile += cbRead;
1185 }
1186
1187out:
1188 if (pvBuf)
1189 RTMemFree(pvBuf);
1190 if (pDisk)
1191 VDClose(pDisk, RT_FAILURE(rc));
1192 if (File != NIL_RTFILE)
1193 RTFileClose(File);
1194
1195 return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
1196}
1197
1198HRESULT showMediumInfo(const ComPtr<IVirtualBox> &pVirtualBox,
1199 const ComPtr<IMedium> &pMedium,
1200 const char *pszParentUUID,
1201 bool fOptLong)
1202{
1203 HRESULT rc = S_OK;
1204 do
1205 {
1206 Bstr uuid;
1207 pMedium->COMGETTER(Id)(uuid.asOutParam());
1208 RTPrintf("UUID: %ls\n", uuid.raw());
1209 if (pszParentUUID)
1210 RTPrintf("Parent UUID: %s\n", pszParentUUID);
1211
1212 /* check for accessibility */
1213 MediumState_T enmState;
1214 CHECK_ERROR_BREAK(pMedium, RefreshState(&enmState));
1215 const char *pszState = "unknown";
1216 switch (enmState)
1217 {
1218 case MediumState_NotCreated:
1219 pszState = "not created";
1220 break;
1221 case MediumState_Created:
1222 pszState = "created";
1223 break;
1224 case MediumState_LockedRead:
1225 pszState = "locked read";
1226 break;
1227 case MediumState_LockedWrite:
1228 pszState = "locked write";
1229 break;
1230 case MediumState_Inaccessible:
1231 pszState = "inaccessible";
1232 break;
1233 case MediumState_Creating:
1234 pszState = "creating";
1235 break;
1236 case MediumState_Deleting:
1237 pszState = "deleting";
1238 break;
1239#ifdef VBOX_WITH_XPCOM_CPP_ENUM_HACK
1240 case MediumState_32BitHack: break; /* Shut up compiler warnings. */
1241#endif
1242 }
1243 RTPrintf("State: %s\n", pszState);
1244
1245 if (fOptLong && enmState == MediumState_Inaccessible)
1246 {
1247 Bstr err;
1248 CHECK_ERROR_BREAK(pMedium, COMGETTER(LastAccessError)(err.asOutParam()));
1249 RTPrintf("Access Error: %ls\n", err.raw());
1250 }
1251
1252 if (fOptLong)
1253 {
1254 Bstr description;
1255 pMedium->COMGETTER(Description)(description.asOutParam());
1256 if (!description.isEmpty())
1257 RTPrintf("Description: %ls\n", description.raw());
1258 }
1259
1260 MediumType_T type;
1261 pMedium->COMGETTER(Type)(&type);
1262 const char *typeStr = "unknown";
1263 switch (type)
1264 {
1265 case MediumType_Normal:
1266 if (pszParentUUID && Guid(pszParentUUID).isValid())
1267 typeStr = "normal (differencing)";
1268 else
1269 typeStr = "normal (base)";
1270 break;
1271 case MediumType_Immutable:
1272 typeStr = "immutable";
1273 break;
1274 case MediumType_Writethrough:
1275 typeStr = "writethrough";
1276 break;
1277 case MediumType_Shareable:
1278 typeStr = "shareable";
1279 break;
1280 case MediumType_Readonly:
1281 typeStr = "readonly";
1282 break;
1283 case MediumType_MultiAttach:
1284 typeStr = "multiattach";
1285 break;
1286#ifdef VBOX_WITH_XPCOM_CPP_ENUM_HACK
1287 case MediumType_32BitHack: break; /* Shut up compiler warnings. */
1288#endif
1289 }
1290 RTPrintf("Type: %s\n", typeStr);
1291
1292 /* print out information specific for differencing media */
1293 if (fOptLong && pszParentUUID && Guid(pszParentUUID).isValid())
1294 {
1295 BOOL autoReset = FALSE;
1296 pMedium->COMGETTER(AutoReset)(&autoReset);
1297 RTPrintf("Auto-Reset: %s\n", autoReset ? "on" : "off");
1298 }
1299
1300 Bstr loc;
1301 pMedium->COMGETTER(Location)(loc.asOutParam());
1302 RTPrintf("Location: %ls\n", loc.raw());
1303
1304 Bstr format;
1305 pMedium->COMGETTER(Format)(format.asOutParam());
1306 RTPrintf("Storage format: %ls\n", format.raw());
1307
1308 if (fOptLong)
1309 {
1310 com::SafeArray<MediumVariant_T> safeArray_variant;
1311
1312 pMedium->COMGETTER(Variant)(ComSafeArrayAsOutParam(safeArray_variant));
1313 ULONG variant=0;
1314 for (size_t i = 0; i < safeArray_variant.size(); i++)
1315 variant |= safeArray_variant[i];
1316
1317 const char *variantStr = "unknown";
1318 switch (variant & ~(MediumVariant_Fixed | MediumVariant_Diff))
1319 {
1320 case MediumVariant_VmdkSplit2G:
1321 variantStr = "split2G";
1322 break;
1323 case MediumVariant_VmdkStreamOptimized:
1324 variantStr = "streamOptimized";
1325 break;
1326 case MediumVariant_VmdkESX:
1327 variantStr = "ESX";
1328 break;
1329 case MediumVariant_Standard:
1330 variantStr = "default";
1331 break;
1332 }
1333 const char *variantTypeStr = "dynamic";
1334 if (variant & MediumVariant_Fixed)
1335 variantTypeStr = "fixed";
1336 else if (variant & MediumVariant_Diff)
1337 variantTypeStr = "differencing";
1338 RTPrintf("Format variant: %s %s\n", variantTypeStr, variantStr);
1339 }
1340
1341 LONG64 logicalSize;
1342 pMedium->COMGETTER(LogicalSize)(&logicalSize);
1343 RTPrintf("Capacity: %lld MBytes\n", logicalSize >> 20);
1344 if (fOptLong)
1345 {
1346 LONG64 actualSize;
1347 pMedium->COMGETTER(Size)(&actualSize);
1348 RTPrintf("Size on disk: %lld MBytes\n", actualSize >> 20);
1349 }
1350
1351 Bstr strCipher;
1352 Bstr strPasswordId;
1353 HRESULT rc2 = pMedium->GetEncryptionSettings(strCipher.asOutParam(), strPasswordId.asOutParam());
1354 if (SUCCEEDED(rc2))
1355 {
1356 RTPrintf("Encryption: enabled\n");
1357 if (fOptLong)
1358 {
1359 RTPrintf("Cipher: %ls\n", strCipher.raw());
1360 RTPrintf("Password ID: %ls\n", strPasswordId.raw());
1361 }
1362 }
1363 else
1364 RTPrintf("Encryption: disabled\n");
1365
1366 if (fOptLong)
1367 {
1368 com::SafeArray<BSTR> names;
1369 com::SafeArray<BSTR> values;
1370 pMedium->GetProperties(Bstr().raw(), ComSafeArrayAsOutParam(names), ComSafeArrayAsOutParam(values));
1371 size_t cNames = names.size();
1372 size_t cValues = values.size();
1373 bool fFirst = true;
1374 for (size_t i = 0; i < cNames; i++)
1375 {
1376 Bstr value;
1377 if (i < cValues)
1378 value = values[i];
1379 RTPrintf("%s%ls=%ls\n",
1380 fFirst ? "Property: " : " ",
1381 names[i], value.raw());
1382 }
1383 }
1384
1385 if (fOptLong)
1386 {
1387 bool fFirst = true;
1388 com::SafeArray<BSTR> machineIds;
1389 pMedium->COMGETTER(MachineIds)(ComSafeArrayAsOutParam(machineIds));
1390 for (size_t i = 0; i < machineIds.size(); i++)
1391 {
1392 ComPtr<IMachine> pMachine;
1393 CHECK_ERROR(pVirtualBox, FindMachine(machineIds[i], pMachine.asOutParam()));
1394 if (pMachine)
1395 {
1396 Bstr name;
1397 pMachine->COMGETTER(Name)(name.asOutParam());
1398 pMachine->COMGETTER(Id)(uuid.asOutParam());
1399 RTPrintf("%s%ls (UUID: %ls)",
1400 fFirst ? "In use by VMs: " : " ",
1401 name.raw(), machineIds[i]);
1402 fFirst = false;
1403 com::SafeArray<BSTR> snapshotIds;
1404 pMedium->GetSnapshotIds(machineIds[i],
1405 ComSafeArrayAsOutParam(snapshotIds));
1406 for (size_t j = 0; j < snapshotIds.size(); j++)
1407 {
1408 ComPtr<ISnapshot> pSnapshot;
1409 pMachine->FindSnapshot(snapshotIds[j], pSnapshot.asOutParam());
1410 if (pSnapshot)
1411 {
1412 Bstr snapshotName;
1413 pSnapshot->COMGETTER(Name)(snapshotName.asOutParam());
1414 RTPrintf(" [%ls (UUID: %ls)]", snapshotName.raw(), snapshotIds[j]);
1415 }
1416 }
1417 RTPrintf("\n");
1418 }
1419 }
1420 }
1421
1422 if (fOptLong)
1423 {
1424 com::SafeIfaceArray<IMedium> children;
1425 pMedium->COMGETTER(Children)(ComSafeArrayAsOutParam(children));
1426 bool fFirst = true;
1427 for (size_t i = 0; i < children.size(); i++)
1428 {
1429 ComPtr<IMedium> pChild(children[i]);
1430 if (pChild)
1431 {
1432 Bstr childUUID;
1433 pChild->COMGETTER(Id)(childUUID.asOutParam());
1434 RTPrintf("%s%ls\n",
1435 fFirst ? "Child UUIDs: " : " ",
1436 childUUID.raw());
1437 fFirst = false;
1438 }
1439 }
1440 }
1441 }
1442 while (0);
1443
1444 return rc;
1445}
1446
1447static const RTGETOPTDEF g_aShowMediumInfoOptions[] =
1448{
1449 { "disk", 'd', RTGETOPT_REQ_NOTHING },
1450 { "dvd", 'D', RTGETOPT_REQ_NOTHING },
1451 { "floppy", 'f', RTGETOPT_REQ_NOTHING },
1452};
1453
1454RTEXITCODE handleShowMediumInfo(HandlerArg *a)
1455{
1456 enum {
1457 CMD_NONE,
1458 CMD_DISK,
1459 CMD_DVD,
1460 CMD_FLOPPY
1461 } cmd = CMD_NONE;
1462 const char *pszFilenameOrUuid = NULL;
1463
1464 int c;
1465 RTGETOPTUNION ValueUnion;
1466 RTGETOPTSTATE GetState;
1467 // start at 0 because main() has hacked both the argc and argv given to us
1468 RTGetOptInit(&GetState, a->argc, a->argv, g_aShowMediumInfoOptions, RT_ELEMENTS(g_aShowMediumInfoOptions),
1469 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
1470 while ((c = RTGetOpt(&GetState, &ValueUnion)))
1471 {
1472 switch (c)
1473 {
1474 case 'd': // disk
1475 if (cmd != CMD_NONE)
1476 return errorSyntax(USAGE_SHOWMEDIUMINFO, "Only one command can be specified: '%s'", ValueUnion.psz);
1477 cmd = CMD_DISK;
1478 break;
1479
1480 case 'D': // DVD
1481 if (cmd != CMD_NONE)
1482 return errorSyntax(USAGE_SHOWMEDIUMINFO, "Only one command can be specified: '%s'", ValueUnion.psz);
1483 cmd = CMD_DVD;
1484 break;
1485
1486 case 'f': // floppy
1487 if (cmd != CMD_NONE)
1488 return errorSyntax(USAGE_SHOWMEDIUMINFO, "Only one command can be specified: '%s'", ValueUnion.psz);
1489 cmd = CMD_FLOPPY;
1490 break;
1491
1492 case VINF_GETOPT_NOT_OPTION:
1493 if (!pszFilenameOrUuid)
1494 pszFilenameOrUuid = ValueUnion.psz;
1495 else
1496 return errorSyntax(USAGE_SHOWMEDIUMINFO, "Invalid parameter '%s'", ValueUnion.psz);
1497 break;
1498
1499 default:
1500 if (c > 0)
1501 {
1502 if (RT_C_IS_PRINT(c))
1503 return errorSyntax(USAGE_SHOWMEDIUMINFO, "Invalid option -%c", c);
1504 else
1505 return errorSyntax(USAGE_SHOWMEDIUMINFO, "Invalid option case %i", c);
1506 }
1507 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
1508 return errorSyntax(USAGE_SHOWMEDIUMINFO, "unknown option: %s\n", ValueUnion.psz);
1509 else if (ValueUnion.pDef)
1510 return errorSyntax(USAGE_SHOWMEDIUMINFO, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
1511 else
1512 return errorSyntax(USAGE_SHOWMEDIUMINFO, "error: %Rrs", c);
1513 }
1514 }
1515
1516 if (cmd == CMD_NONE)
1517 cmd = CMD_DISK;
1518
1519 /* check for required options */
1520 if (!pszFilenameOrUuid)
1521 return errorSyntax(USAGE_SHOWMEDIUMINFO, "Medium name or UUID required");
1522
1523 HRESULT rc = S_OK; /* Prevents warning. */
1524
1525 ComPtr<IMedium> pMedium;
1526 if (cmd == CMD_DISK)
1527 rc = openMedium(a, pszFilenameOrUuid, DeviceType_HardDisk,
1528 AccessMode_ReadOnly, pMedium,
1529 false /* fForceNewUuidOnOpen */, false /* fSilent */);
1530 else if (cmd == CMD_DVD)
1531 rc = openMedium(a, pszFilenameOrUuid, DeviceType_DVD,
1532 AccessMode_ReadOnly, pMedium,
1533 false /* fForceNewUuidOnOpen */, false /* fSilent */);
1534 else if (cmd == CMD_FLOPPY)
1535 rc = openMedium(a, pszFilenameOrUuid, DeviceType_Floppy,
1536 AccessMode_ReadOnly, pMedium,
1537 false /* fForceNewUuidOnOpen */, false /* fSilent */);
1538 if (FAILED(rc))
1539 return RTEXITCODE_FAILURE;
1540
1541 Utf8Str strParentUUID("base");
1542 ComPtr<IMedium> pParent;
1543 pMedium->COMGETTER(Parent)(pParent.asOutParam());
1544 if (!pParent.isNull())
1545 {
1546 Bstr bstrParentUUID;
1547 pParent->COMGETTER(Id)(bstrParentUUID.asOutParam());
1548 strParentUUID = bstrParentUUID;
1549 }
1550
1551 rc = showMediumInfo(a->virtualBox, pMedium, strParentUUID.c_str(), true);
1552
1553 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
1554}
1555
1556static const RTGETOPTDEF g_aCloseMediumOptions[] =
1557{
1558 { "disk", 'd', RTGETOPT_REQ_NOTHING },
1559 { "dvd", 'D', RTGETOPT_REQ_NOTHING },
1560 { "floppy", 'f', RTGETOPT_REQ_NOTHING },
1561 { "--delete", 'r', RTGETOPT_REQ_NOTHING },
1562};
1563
1564RTEXITCODE handleCloseMedium(HandlerArg *a)
1565{
1566 HRESULT rc = S_OK;
1567 enum {
1568 CMD_NONE,
1569 CMD_DISK,
1570 CMD_DVD,
1571 CMD_FLOPPY
1572 } cmd = CMD_NONE;
1573 const char *pszFilenameOrUuid = NULL;
1574 bool fDelete = false;
1575
1576 int c;
1577 RTGETOPTUNION ValueUnion;
1578 RTGETOPTSTATE GetState;
1579 // start at 0 because main() has hacked both the argc and argv given to us
1580 RTGetOptInit(&GetState, a->argc, a->argv, g_aCloseMediumOptions, RT_ELEMENTS(g_aCloseMediumOptions),
1581 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
1582 while ((c = RTGetOpt(&GetState, &ValueUnion)))
1583 {
1584 switch (c)
1585 {
1586 case 'd': // disk
1587 if (cmd != CMD_NONE)
1588 return errorSyntax(USAGE_CLOSEMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
1589 cmd = CMD_DISK;
1590 break;
1591
1592 case 'D': // DVD
1593 if (cmd != CMD_NONE)
1594 return errorSyntax(USAGE_CLOSEMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
1595 cmd = CMD_DVD;
1596 break;
1597
1598 case 'f': // floppy
1599 if (cmd != CMD_NONE)
1600 return errorSyntax(USAGE_CLOSEMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
1601 cmd = CMD_FLOPPY;
1602 break;
1603
1604 case 'r': // --delete
1605 fDelete = true;
1606 break;
1607
1608 case VINF_GETOPT_NOT_OPTION:
1609 if (!pszFilenameOrUuid)
1610 pszFilenameOrUuid = ValueUnion.psz;
1611 else
1612 return errorSyntax(USAGE_CLOSEMEDIUM, "Invalid parameter '%s'", ValueUnion.psz);
1613 break;
1614
1615 default:
1616 if (c > 0)
1617 {
1618 if (RT_C_IS_PRINT(c))
1619 return errorSyntax(USAGE_CLOSEMEDIUM, "Invalid option -%c", c);
1620 else
1621 return errorSyntax(USAGE_CLOSEMEDIUM, "Invalid option case %i", c);
1622 }
1623 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
1624 return errorSyntax(USAGE_CLOSEMEDIUM, "unknown option: %s\n", ValueUnion.psz);
1625 else if (ValueUnion.pDef)
1626 return errorSyntax(USAGE_CLOSEMEDIUM, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
1627 else
1628 return errorSyntax(USAGE_CLOSEMEDIUM, "error: %Rrs", c);
1629 }
1630 }
1631
1632 /* check for required options */
1633 if (cmd == CMD_NONE)
1634 cmd = CMD_DISK;
1635 if (!pszFilenameOrUuid)
1636 return errorSyntax(USAGE_CLOSEMEDIUM, "Medium name or UUID required");
1637
1638 ComPtr<IMedium> pMedium;
1639 if (cmd == CMD_DISK)
1640 rc = openMedium(a, pszFilenameOrUuid, DeviceType_HardDisk,
1641 AccessMode_ReadWrite, pMedium,
1642 false /* fForceNewUuidOnOpen */, false /* fSilent */);
1643 else if (cmd == CMD_DVD)
1644 rc = openMedium(a, pszFilenameOrUuid, DeviceType_DVD,
1645 AccessMode_ReadOnly, pMedium,
1646 false /* fForceNewUuidOnOpen */, false /* fSilent */);
1647 else if (cmd == CMD_FLOPPY)
1648 rc = openMedium(a, pszFilenameOrUuid, DeviceType_Floppy,
1649 AccessMode_ReadWrite, pMedium,
1650 false /* fForceNewUuidOnOpen */, false /* fSilent */);
1651
1652 if (SUCCEEDED(rc) && pMedium)
1653 {
1654 if (fDelete)
1655 {
1656 ComPtr<IProgress> pProgress;
1657 CHECK_ERROR(pMedium, DeleteStorage(pProgress.asOutParam()));
1658 if (SUCCEEDED(rc))
1659 {
1660 rc = showProgress(pProgress);
1661 CHECK_PROGRESS_ERROR(pProgress, ("Failed to delete medium"));
1662 }
1663 else
1664 RTMsgError("Failed to delete medium. Error code %Rrc", rc);
1665 }
1666 CHECK_ERROR(pMedium, Close());
1667 }
1668
1669 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
1670}
1671
1672RTEXITCODE handleMediumProperty(HandlerArg *a)
1673{
1674 HRESULT rc = S_OK;
1675 const char *pszCmd = NULL;
1676 enum {
1677 CMD_NONE,
1678 CMD_DISK,
1679 CMD_DVD,
1680 CMD_FLOPPY
1681 } cmd = CMD_NONE;
1682 const char *pszAction = NULL;
1683 const char *pszFilenameOrUuid = NULL;
1684 const char *pszProperty = NULL;
1685 ComPtr<IMedium> pMedium;
1686
1687 pszCmd = (a->argc > 0) ? a->argv[0] : "";
1688 if ( !RTStrICmp(pszCmd, "disk")
1689 || !RTStrICmp(pszCmd, "dvd")
1690 || !RTStrICmp(pszCmd, "floppy"))
1691 {
1692 if (!RTStrICmp(pszCmd, "disk"))
1693 cmd = CMD_DISK;
1694 else if (!RTStrICmp(pszCmd, "dvd"))
1695 cmd = CMD_DVD;
1696 else if (!RTStrICmp(pszCmd, "floppy"))
1697 cmd = CMD_FLOPPY;
1698 else
1699 {
1700 AssertMsgFailed(("unexpected parameter %s\n", pszCmd));
1701 cmd = CMD_DISK;
1702 }
1703 a->argv++;
1704 a->argc--;
1705 }
1706 else
1707 {
1708 pszCmd = NULL;
1709 cmd = CMD_DISK;
1710 }
1711
1712 if (a->argc == 0)
1713 return errorSyntax(USAGE_MEDIUMPROPERTY, "Missing action");
1714
1715 pszAction = a->argv[0];
1716 if ( RTStrICmp(pszAction, "set")
1717 && RTStrICmp(pszAction, "get")
1718 && RTStrICmp(pszAction, "delete"))
1719 return errorSyntax(USAGE_MEDIUMPROPERTY, "Invalid action given: %s", pszAction);
1720
1721 if ( ( !RTStrICmp(pszAction, "set")
1722 && a->argc != 4)
1723 || ( RTStrICmp(pszAction, "set")
1724 && a->argc != 3))
1725 return errorSyntax(USAGE_MEDIUMPROPERTY, "Invalid number of arguments given for action: %s", pszAction);
1726
1727 pszFilenameOrUuid = a->argv[1];
1728 pszProperty = a->argv[2];
1729
1730 if (cmd == CMD_DISK)
1731 rc = openMedium(a, pszFilenameOrUuid, DeviceType_HardDisk,
1732 AccessMode_ReadWrite, pMedium,
1733 false /* fForceNewUuidOnOpen */, false /* fSilent */);
1734 else if (cmd == CMD_DVD)
1735 rc = openMedium(a, pszFilenameOrUuid, DeviceType_DVD,
1736 AccessMode_ReadOnly, pMedium,
1737 false /* fForceNewUuidOnOpen */, false /* fSilent */);
1738 else if (cmd == CMD_FLOPPY)
1739 rc = openMedium(a, pszFilenameOrUuid, DeviceType_Floppy,
1740 AccessMode_ReadWrite, pMedium,
1741 false /* fForceNewUuidOnOpen */, false /* fSilent */);
1742 if (SUCCEEDED(rc) && !pMedium.isNull())
1743 {
1744 if (!RTStrICmp(pszAction, "set"))
1745 {
1746 const char *pszValue = a->argv[3];
1747 CHECK_ERROR(pMedium, SetProperty(Bstr(pszProperty).raw(), Bstr(pszValue).raw()));
1748 }
1749 else if (!RTStrICmp(pszAction, "get"))
1750 {
1751 Bstr strVal;
1752 CHECK_ERROR(pMedium, GetProperty(Bstr(pszProperty).raw(), strVal.asOutParam()));
1753 if (SUCCEEDED(rc))
1754 RTPrintf("%s=%ls\n", pszProperty, strVal.raw());
1755 }
1756 else if (!RTStrICmp(pszAction, "delete"))
1757 {
1758 CHECK_ERROR(pMedium, SetProperty(Bstr(pszProperty).raw(), Bstr().raw()));
1759 /** @todo */
1760 }
1761 }
1762
1763 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
1764}
1765
1766static const RTGETOPTDEF g_aEncryptMediumOptions[] =
1767{
1768 { "--newpassword", 'n', RTGETOPT_REQ_STRING },
1769 { "--oldpassword", 'o', RTGETOPT_REQ_STRING },
1770 { "--cipher", 'c', RTGETOPT_REQ_STRING },
1771 { "--newpasswordid", 'i', RTGETOPT_REQ_STRING }
1772};
1773
1774RTEXITCODE handleEncryptMedium(HandlerArg *a)
1775{
1776 HRESULT rc;
1777 ComPtr<IMedium> hardDisk;
1778 const char *pszPasswordNew = NULL;
1779 const char *pszPasswordOld = NULL;
1780 const char *pszCipher = NULL;
1781 const char *pszFilenameOrUuid = NULL;
1782 const char *pszNewPasswordId = NULL;
1783 Utf8Str strPasswordNew;
1784 Utf8Str strPasswordOld;
1785
1786 int c;
1787 RTGETOPTUNION ValueUnion;
1788 RTGETOPTSTATE GetState;
1789 // start at 0 because main() has hacked both the argc and argv given to us
1790 RTGetOptInit(&GetState, a->argc, a->argv, g_aEncryptMediumOptions, RT_ELEMENTS(g_aEncryptMediumOptions),
1791 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
1792 while ((c = RTGetOpt(&GetState, &ValueUnion)))
1793 {
1794 switch (c)
1795 {
1796 case 'n': // --newpassword
1797 pszPasswordNew = ValueUnion.psz;
1798 break;
1799
1800 case 'o': // --oldpassword
1801 pszPasswordOld = ValueUnion.psz;
1802 break;
1803
1804 case 'c': // --cipher
1805 pszCipher = ValueUnion.psz;
1806 break;
1807
1808 case 'i': // --newpasswordid
1809 pszNewPasswordId = ValueUnion.psz;
1810 break;
1811
1812 case VINF_GETOPT_NOT_OPTION:
1813 if (!pszFilenameOrUuid)
1814 pszFilenameOrUuid = ValueUnion.psz;
1815 else
1816 return errorSyntax(USAGE_ENCRYPTMEDIUM, "Invalid parameter '%s'", ValueUnion.psz);
1817 break;
1818
1819 default:
1820 if (c > 0)
1821 {
1822 if (RT_C_IS_PRINT(c))
1823 return errorSyntax(USAGE_ENCRYPTMEDIUM, "Invalid option -%c", c);
1824 else
1825 return errorSyntax(USAGE_ENCRYPTMEDIUM, "Invalid option case %i", c);
1826 }
1827 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
1828 return errorSyntax(USAGE_ENCRYPTMEDIUM, "unknown option: %s\n", ValueUnion.psz);
1829 else if (ValueUnion.pDef)
1830 return errorSyntax(USAGE_ENCRYPTMEDIUM, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
1831 else
1832 return errorSyntax(USAGE_ENCRYPTMEDIUM, "error: %Rrs", c);
1833 }
1834 }
1835
1836 if (!pszFilenameOrUuid)
1837 return errorSyntax(USAGE_ENCRYPTMEDIUM, "Disk name or UUID required");
1838
1839 if (!pszPasswordNew && !pszPasswordOld)
1840 return errorSyntax(USAGE_ENCRYPTMEDIUM, "No password specified");
1841
1842 if ( (pszPasswordNew && !pszNewPasswordId)
1843 || (!pszPasswordNew && pszNewPasswordId))
1844 return errorSyntax(USAGE_ENCRYPTMEDIUM, "A new password must always have a valid identifier set at the same time");
1845
1846 if (pszPasswordNew)
1847 {
1848 if (!RTStrCmp(pszPasswordNew, "-"))
1849 {
1850 /* Get password from console. */
1851 RTEXITCODE rcExit = readPasswordFromConsole(&strPasswordNew, "Enter new password:");
1852 if (rcExit == RTEXITCODE_FAILURE)
1853 return rcExit;
1854 }
1855 else
1856 {
1857 RTEXITCODE rcExit = readPasswordFile(pszPasswordNew, &strPasswordNew);
1858 if (rcExit == RTEXITCODE_FAILURE)
1859 {
1860 RTMsgError("Failed to read new password from file");
1861 return rcExit;
1862 }
1863 }
1864 }
1865
1866 if (pszPasswordOld)
1867 {
1868 if (!RTStrCmp(pszPasswordOld, "-"))
1869 {
1870 /* Get password from console. */
1871 RTEXITCODE rcExit = readPasswordFromConsole(&strPasswordOld, "Enter old password:");
1872 if (rcExit == RTEXITCODE_FAILURE)
1873 return rcExit;
1874 }
1875 else
1876 {
1877 RTEXITCODE rcExit = readPasswordFile(pszPasswordOld, &strPasswordOld);
1878 if (rcExit == RTEXITCODE_FAILURE)
1879 {
1880 RTMsgError("Failed to read old password from file");
1881 return rcExit;
1882 }
1883 }
1884 }
1885
1886 /* Always open the medium if necessary, there is no other way. */
1887 rc = openMedium(a, pszFilenameOrUuid, DeviceType_HardDisk,
1888 AccessMode_ReadWrite, hardDisk,
1889 false /* fForceNewUuidOnOpen */, false /* fSilent */);
1890 if (FAILED(rc))
1891 return RTEXITCODE_FAILURE;
1892 if (hardDisk.isNull())
1893 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Invalid hard disk reference, avoiding crash");
1894
1895 ComPtr<IProgress> progress;
1896 CHECK_ERROR(hardDisk, ChangeEncryption(Bstr(strPasswordOld).raw(), Bstr(pszCipher).raw(),
1897 Bstr(strPasswordNew).raw(), Bstr(pszNewPasswordId).raw(),
1898 progress.asOutParam()));
1899 if (SUCCEEDED(rc))
1900 rc = showProgress(progress);
1901 if (FAILED(rc))
1902 {
1903 if (rc == E_NOTIMPL)
1904 RTMsgError("Encrypt hard disk operation is not implemented!");
1905 else if (rc == VBOX_E_NOT_SUPPORTED)
1906 RTMsgError("Encrypt hard disk operation for this cipher is not implemented yet!");
1907 else if (!progress.isNull())
1908 CHECK_PROGRESS_ERROR(progress, ("Failed to encrypt hard disk"));
1909 else
1910 RTMsgError("Failed to encrypt hard disk!");
1911 }
1912
1913 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
1914}
1915
1916RTEXITCODE handleCheckMediumPassword(HandlerArg *a)
1917{
1918 HRESULT rc;
1919 ComPtr<IMedium> hardDisk;
1920 const char *pszFilenameOrUuid = NULL;
1921 Utf8Str strPassword;
1922
1923 if (a->argc != 2)
1924 return errorSyntax(USAGE_MEDIUMENCCHKPWD, "Invalid number of arguments: %d", a->argc);
1925
1926 pszFilenameOrUuid = a->argv[0];
1927
1928 if (!RTStrCmp(a->argv[1], "-"))
1929 {
1930 /* Get password from console. */
1931 RTEXITCODE rcExit = readPasswordFromConsole(&strPassword, "Enter password:");
1932 if (rcExit == RTEXITCODE_FAILURE)
1933 return rcExit;
1934 }
1935 else
1936 {
1937 RTEXITCODE rcExit = readPasswordFile(a->argv[1], &strPassword);
1938 if (rcExit == RTEXITCODE_FAILURE)
1939 {
1940 RTMsgError("Failed to read password from file");
1941 return rcExit;
1942 }
1943 }
1944
1945 /* Always open the medium if necessary, there is no other way. */
1946 rc = openMedium(a, pszFilenameOrUuid, DeviceType_HardDisk,
1947 AccessMode_ReadWrite, hardDisk,
1948 false /* fForceNewUuidOnOpen */, false /* fSilent */);
1949 if (FAILED(rc))
1950 return RTEXITCODE_FAILURE;
1951 if (hardDisk.isNull())
1952 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Invalid hard disk reference, avoiding crash");
1953
1954 CHECK_ERROR(hardDisk, CheckEncryptionPassword(Bstr(strPassword).raw()));
1955 if (SUCCEEDED(rc))
1956 RTPrintf("The given password is correct\n");
1957 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
1958}
1959
1960
1961/*********************************************************************************************************************************
1962* The mediumio command *
1963*********************************************************************************************************************************/
1964
1965/**
1966 * Common MediumIO options.
1967 */
1968typedef struct MEDIUMIOCOMMONOPT
1969{
1970 const char *pszFilenameOrUuid;
1971 DeviceType_T enmDeviceType;
1972 const char *pszPasswordFile;
1973} MEDIUMIOCOMMONOPT;
1974typedef MEDIUMIOCOMMONOPT *PMEDIUMIOCOMMONOPT;
1975typedef MEDIUMIOCOMMONOPT const *PCMEDIUMIOCOMMONOPT;
1976
1977/* For RTGETOPTDEF array initializer. */
1978#define MEDIUMIOCOMMONOPT_DEFS() \
1979 { "--disk", 'd', RTGETOPT_REQ_STRING }, \
1980 { "--harddisk", 'd', RTGETOPT_REQ_STRING }, \
1981 { "disk", 'd', RTGETOPT_REQ_STRING }, \
1982 { "harddisk", 'd', RTGETOPT_REQ_STRING }, \
1983 { "--dvd", 'D', RTGETOPT_REQ_STRING }, \
1984 { "--iso", 'D', RTGETOPT_REQ_STRING }, \
1985 { "dvd", 'D', RTGETOPT_REQ_STRING }, \
1986 { "iso", 'D', RTGETOPT_REQ_STRING }, \
1987 { "--floppy", 'f', RTGETOPT_REQ_STRING }, \
1988 { "floppy", 'f', RTGETOPT_REQ_STRING }, \
1989 { "--password-file", 'P', RTGETOPT_REQ_STRING }
1990
1991/* For option switch. */
1992#define MEDIUMIOCOMMONOPT_CASES(a_pCommonOpts) \
1993 case 'd': \
1994 (a_pCommonOpts)->enmDeviceType = DeviceType_HardDisk; \
1995 (a_pCommonOpts)->pszFilenameOrUuid = ValueUnion.psz; \
1996 break; \
1997 case 'D': \
1998 (a_pCommonOpts)->enmDeviceType = DeviceType_DVD; \
1999 (a_pCommonOpts)->pszFilenameOrUuid = ValueUnion.psz; \
2000 break; \
2001 case 'f': \
2002 (a_pCommonOpts)->enmDeviceType = DeviceType_Floppy; \
2003 (a_pCommonOpts)->pszFilenameOrUuid = ValueUnion.psz; \
2004 break; \
2005 case 'P': \
2006 (a_pCommonOpts)->pszPasswordFile = ValueUnion.psz; \
2007 break
2008
2009
2010/**
2011 * Worker for mediumio operations that returns a IMediumIO for the specified
2012 * medium.
2013 *
2014 * @returns Exit code.
2015 * @param pHandler The handler state structure (for IVirtualBox).
2016 * @param pCommonOpts Common mediumio options.
2017 * @param fWritable Whether to open writable (true) or read only
2018 * (false).
2019 * @param rPtrMediumIO Where to return the IMediumIO pointer.
2020 * @param pcbMedium Where to return the meidum size. Optional.
2021 */
2022static RTEXITCODE mediumIOOpenMediumForIO(HandlerArg *pHandler, PCMEDIUMIOCOMMONOPT pCommonOpts, bool fWritable,
2023 ComPtr<IMediumIO> &rPtrMediumIO, uint64_t *pcbMedium = NULL)
2024{
2025 /* Clear returns. */
2026 if (pcbMedium)
2027 *pcbMedium = 0;
2028 rPtrMediumIO.setNull();
2029
2030 /*
2031 * Make sure a medium was specified already.
2032 */
2033 if (pCommonOpts->enmDeviceType == DeviceType_Null)
2034 return errorSyntax("No medium specified!");
2035
2036 /*
2037 * Read the password.
2038 */
2039 Bstr bstrPassword;
2040 if (pCommonOpts->pszPasswordFile)
2041 {
2042 Utf8Str strPassword;
2043 RTEXITCODE rcExit;
2044 if (pCommonOpts->pszPasswordFile[0] == '-' && pCommonOpts->pszPasswordFile[1] == '\0')
2045 rcExit = readPasswordFromConsole(&strPassword, "Enter encryption password:");
2046 else
2047 rcExit = readPasswordFile(pCommonOpts->pszPasswordFile, &strPassword);
2048 if (rcExit != RTEXITCODE_SUCCESS)
2049 return rcExit;
2050 bstrPassword = strPassword;
2051 strPassword.assign(strPassword.length(), '*');
2052 }
2053
2054 /*
2055 * Open the medium and then get I/O access to it.
2056 */
2057 ComPtr<IMedium> ptrMedium;
2058 HRESULT hrc = openMedium(pHandler, pCommonOpts->pszFilenameOrUuid, pCommonOpts->enmDeviceType,
2059 fWritable ? AccessMode_ReadWrite : AccessMode_ReadOnly,
2060 ptrMedium, false /* fForceNewUuidOnOpen */, false /* fSilent */);
2061 if (SUCCEEDED(hrc))
2062 {
2063 CHECK_ERROR2I_STMT(ptrMedium, OpenForIO(fWritable, bstrPassword.raw(), rPtrMediumIO.asOutParam()), hrc = hrcCheck);
2064
2065 /*
2066 * If the size is requested get it after we've opened it.
2067 */
2068 if (pcbMedium && SUCCEEDED(hrc))
2069 {
2070 LONG64 cbLogical = 0;
2071 CHECK_ERROR2I_STMT(ptrMedium, COMGETTER(LogicalSize)(&cbLogical), hrc = hrcCheck);
2072 *pcbMedium = cbLogical;
2073 if (!SUCCEEDED(hrc))
2074 rPtrMediumIO.setNull();
2075 }
2076 }
2077
2078 if (bstrPassword.isNotEmpty())
2079 memset(bstrPassword.mutableRaw(), '*', bstrPassword.length() * sizeof(RTUTF16));
2080 return SUCCEEDED(hrc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
2081}
2082
2083
2084/**
2085 * mediumio formatfat
2086 */
2087static RTEXITCODE handleMediumIOFormatFat(HandlerArg *a, int iFirst, PMEDIUMIOCOMMONOPT pCommonOpts)
2088{
2089 /*
2090 * Parse the options.
2091 */
2092 bool fQuick = false;
2093 static const RTGETOPTDEF s_aOptions[] =
2094 {
2095 MEDIUMIOCOMMONOPT_DEFS(),
2096 { "--quick", 'q', RTGETOPT_REQ_NOTHING },
2097 };
2098
2099 RTGETOPTSTATE GetState;
2100 int rc = RTGetOptInit(&GetState, a->argc, a->argv, s_aOptions, RT_ELEMENTS(s_aOptions), iFirst, 0);
2101 AssertRC(rc);
2102 RTGETOPTUNION ValueUnion;
2103 while ((rc = RTGetOpt(&GetState, &ValueUnion)) != 0)
2104 {
2105 switch (rc)
2106 {
2107 MEDIUMIOCOMMONOPT_CASES(pCommonOpts);
2108
2109 case 'q':
2110 fQuick = true;
2111 break;
2112
2113 default:
2114 return errorGetOpt(rc, &ValueUnion);
2115 }
2116 }
2117
2118 /*
2119 * Open the medium for I/O and format it.
2120 */
2121 ComPtr<IMediumIO> ptrMediumIO;
2122 RTEXITCODE rcExit = mediumIOOpenMediumForIO(a, pCommonOpts, true /*fWritable*/, ptrMediumIO);
2123 if (rcExit != RTEXITCODE_SUCCESS)
2124 return rcExit;
2125 CHECK_ERROR2I_RET(ptrMediumIO, FormatFAT(fQuick), RTEXITCODE_FAILURE);
2126 return RTEXITCODE_SUCCESS;
2127}
2128
2129/**
2130 * mediumio cat
2131 */
2132static RTEXITCODE handleMediumIOCat(HandlerArg *a, int iFirst, PMEDIUMIOCOMMONOPT pCommonOpts)
2133{
2134 /*
2135 * Parse the options.
2136 */
2137 static const RTGETOPTDEF s_aOptions[] =
2138 {
2139 MEDIUMIOCOMMONOPT_DEFS(),
2140 { "--hex", 'H', RTGETOPT_REQ_NOTHING },
2141 { "--offset", 'o', RTGETOPT_REQ_UINT64 },
2142 { "--output", 'O', RTGETOPT_REQ_UINT64 },
2143 { "--size", 's', RTGETOPT_REQ_UINT64 },
2144 };
2145 bool fHex = false;
2146 uint64_t off = 0;
2147 const char *pszOutput = NULL;
2148 uint64_t cb = UINT64_MAX;
2149
2150 RTGETOPTSTATE GetState;
2151 int rc = RTGetOptInit(&GetState, a->argc, a->argv, s_aOptions, RT_ELEMENTS(s_aOptions), iFirst, 0);
2152 AssertRC(rc);
2153 RTGETOPTUNION ValueUnion;
2154 while ((rc = RTGetOpt(&GetState, &ValueUnion)) != 0)
2155 {
2156 switch (rc)
2157 {
2158 MEDIUMIOCOMMONOPT_CASES(pCommonOpts);
2159
2160 case 'H':
2161 fHex = true;
2162 break;
2163
2164 case 'o':
2165 off = ValueUnion.u64;
2166 break;
2167
2168 case 'O':
2169 pszOutput = ValueUnion.psz;
2170 break;
2171
2172 case 's':
2173 cb = ValueUnion.u64;
2174 break;
2175
2176 default:
2177 return errorGetOpt(rc, &ValueUnion);
2178 }
2179 }
2180
2181 /*
2182 * Open the medium for I/O.
2183 */
2184 ComPtr<IMediumIO> ptrMediumIO;
2185 uint64_t cbMedium;
2186 RTEXITCODE rcExit = mediumIOOpenMediumForIO(a, pCommonOpts, false /*fWritable*/, ptrMediumIO, &cbMedium);
2187 if (rcExit == RTEXITCODE_SUCCESS)
2188 {
2189 /*
2190 * Do we have an output file or do we write to stdout?
2191 */
2192 PRTSTREAM pOut = NULL;
2193 if (pszOutput && (pszOutput[0] != '-' || pszOutput[1] != '\0'))
2194 {
2195 int vrc = RTStrmOpen(pszOutput, fHex ? "wt" : "wb", &pOut);
2196 if (RT_FAILURE(vrc))
2197 rcExit = RTMsgErrorExitFailure("Error opening '%s' for writing: %Rrc", pszOutput, vrc);
2198 }
2199 else
2200 {
2201 pOut = g_pStdOut;
2202 if (!fHex)
2203 RTStrmSetMode(pOut, true, -1);
2204 }
2205
2206 if (rcExit == RTEXITCODE_SUCCESS)
2207 {
2208 /*
2209 * Adjust 'cb' now that we've got the medium size.
2210 */
2211 if (off >= cbMedium)
2212 {
2213 RTMsgWarning("Specified offset (%#RX64) is beyond the end of the medium (%#RX64)", off, cbMedium);
2214 cb = 0;
2215 }
2216 else if ( cb > cbMedium
2217 || cb + off > cbMedium)
2218 cb = cbMedium - off;
2219
2220 /*
2221 * Hex dump preps. (The duplication detection is making ASSUMPTIONS about
2222 * all the reads being a multiple of cchWidth, except for the final one.)
2223 */
2224 char abHexBuf[16] = { 0 };
2225 size_t cbHexBuf = 0;
2226 unsigned const cchWidth = RT_ELEMENTS(abHexBuf);
2227 uint64_t const offEndDupCheck = cb - cchWidth;
2228 uint64_t cDuplicates = 0;
2229
2230 /*
2231 * Do the reading.
2232 */
2233 while (cb > 0)
2234 {
2235 char szLine[32 + cchWidth * 4 + 32];
2236
2237 /* Do the reading. */
2238 uint32_t const cbToRead = (uint32_t)RT_MIN(cb, _128K);
2239 SafeArray<BYTE> SafeArrayBuf;
2240 HRESULT hrc = ptrMediumIO->Read(off, cbToRead, ComSafeArrayAsOutParam(SafeArrayBuf));
2241 if (FAILED(hrc))
2242 {
2243 RTStrPrintf(szLine, sizeof(szLine), "Read(%zu bytes at %#RX64)", cbToRead, off);
2244 com::GlueHandleComError(ptrMediumIO, szLine, hrc, __FILE__, __LINE__);
2245 break;
2246 }
2247
2248 /* Output the data. */
2249 size_t const cbReturned = SafeArrayBuf.size();
2250 if (cbReturned)
2251 {
2252 BYTE const *pbBuf = SafeArrayBuf.raw();
2253 int vrc = VINF_SUCCESS;
2254 if (!fHex)
2255 vrc = RTStrmWrite(pOut, pbBuf, cbReturned);
2256 else
2257 {
2258 /* hexdump -C */
2259 uint64_t offHex = off;
2260 uint64_t const offHexEnd = off + cbReturned;
2261 while (offHex < offHexEnd)
2262 {
2263 if ( offHex >= offEndDupCheck
2264 || cbHexBuf == 0
2265 || memcmp(pbBuf, abHexBuf, cchWidth) != 0
2266 || ( cDuplicates == 0
2267 && ( offHex + cchWidth >= offEndDupCheck
2268 || memcmp(pbBuf + cchWidth, pbBuf, cchWidth) != 0)) )
2269 {
2270 if (cDuplicates > 0)
2271 {
2272 RTStrmPrintf(pOut, "********** <ditto x %RU64>\n", cDuplicates);
2273 cDuplicates = 0;
2274 }
2275
2276 size_t cch = RTStrPrintf(szLine, sizeof(szLine), "%012RX64:", offHex);
2277 unsigned i;
2278 for (i = 0; i < cchWidth && offHex + i < offHexEnd; i++)
2279 {
2280 static const char s_szHexDigits[17] = "0123456789abcdef";
2281 szLine[cch++] = (i & 7) || i == 0 ? ' ' : '-';
2282 uint8_t const u8 = pbBuf[i];
2283 szLine[cch++] = s_szHexDigits[u8 >> 4];
2284 szLine[cch++] = s_szHexDigits[u8 & 0xf];
2285 }
2286 while (i++ < cchWidth)
2287 {
2288 szLine[cch++] = ' ';
2289 szLine[cch++] = ' ';
2290 szLine[cch++] = ' ';
2291 }
2292 szLine[cch++] = ' ';
2293
2294 for (i = 0; i < cchWidth && offHex + i < offHexEnd; i++)
2295 {
2296 uint8_t const u8 = pbBuf[i];
2297 szLine[cch++] = u8 < 127 && u8 >= 32 ? u8 : '.';
2298 }
2299 szLine[cch++] = '\n';
2300 szLine[cch] = '\0';
2301
2302 vrc = RTStrmWrite(pOut, szLine, cch);
2303 if (RT_FAILURE(vrc))
2304 break;
2305
2306
2307 /* copy bytes over to the duplication detection buffer. */
2308 cbHexBuf = (size_t)RT_MIN(cchWidth, offHexEnd - offHex);
2309 memcpy(abHexBuf, pbBuf, cbHexBuf);
2310 }
2311 else
2312 cDuplicates++;
2313
2314 /* Advance to next line. */
2315 pbBuf += cchWidth;
2316 offHex += cchWidth;
2317 }
2318 }
2319 if (RT_FAILURE(vrc))
2320 {
2321 rcExit = RTMsgErrorExitFailure("Error writing to '%s': %Rrc", pszOutput, vrc);
2322 break;
2323 }
2324 }
2325
2326 /* Advance. */
2327 if (cbReturned != cbToRead)
2328 {
2329 rcExit = RTMsgErrorExitFailure("Expected read() at offset %RU64 (%#RX64) to return %#zx bytes, only got %#zx!\n",
2330 off, off, cbReturned, cbToRead);
2331 break;
2332 }
2333 off += cbReturned;
2334 cb -= cbReturned;
2335 }
2336
2337 /*
2338 * Close output.
2339 */
2340 if (pOut != g_pStdOut)
2341 {
2342 int vrc = RTStrmClose(pOut);
2343 if (RT_FAILURE(vrc))
2344 rcExit = RTMsgErrorExitFailure("Error closing '%s': %Rrc", pszOutput, vrc);
2345 }
2346 else if (!fHex)
2347 RTStrmSetMode(pOut, false, -1);
2348 }
2349 }
2350 return rcExit;
2351}
2352
2353
2354RTEXITCODE handleMediumIO(HandlerArg *a)
2355{
2356 /*
2357 * Parse image-option and sub-command.
2358 */
2359 static const RTGETOPTDEF s_aOptions[] =
2360 {
2361 MEDIUMIOCOMMONOPT_DEFS(),
2362 /* sub-commands */
2363 { "formatfat", 1000, RTGETOPT_REQ_NOTHING },
2364 { "cat", 1001, RTGETOPT_REQ_NOTHING },
2365 };
2366 MEDIUMIOCOMMONOPT CommonOpts = { NULL, DeviceType_Null, NULL };
2367
2368 RTGETOPTSTATE GetState;
2369 int rc = RTGetOptInit(&GetState, a->argc, a->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0);
2370 AssertRC(rc);
2371 RTGETOPTUNION ValueUnion;
2372 while ((rc = RTGetOpt(&GetState, &ValueUnion)) != 0)
2373 {
2374 switch (rc)
2375 {
2376 MEDIUMIOCOMMONOPT_CASES(&CommonOpts);
2377
2378 /* Sub-commands: */
2379 case 1000:
2380 setCurrentSubcommand(HELP_SCOPE_MEDIUMIO_FORMATFAT);
2381 return handleMediumIOFormatFat(a, GetState.iNext, &CommonOpts);
2382 case 1001:
2383 setCurrentSubcommand(HELP_SCOPE_MEDIUMIO_CAT);
2384 return handleMediumIOCat(a, GetState.iNext, &CommonOpts);
2385
2386 case VINF_GETOPT_NOT_OPTION:
2387 return errorUnknownSubcommand(ValueUnion.psz);
2388
2389 default:
2390 return errorGetOpt(rc, &ValueUnion);
2391 }
2392 }
2393 return errorNoSubcommand();
2394}
2395
2396#endif /* !VBOX_ONLY_DOCS */
Note: See TracBrowser for help on using the repository browser.

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