VirtualBox

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

Last change on this file since 68838 was 67227, checked in by vboxsync, 8 years ago

VBoxManage: bugref:8344: when moving a medium, create an absolute path before calling the API function

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 69.2 KB
Line 
1/* $Id: VBoxManageDisk.cpp 67227 2017-06-02 09:28:40Z vboxsync $ */
2/** @file
3 * VBoxManage - The disk/medium related commands.
4 */
5
6/*
7 * Copyright (C) 2006-2016 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
45// funcs
46///////////////////////////////////////////////////////////////////////////////
47
48
49static DECLCALLBACK(void) handleVDError(void *pvUser, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va)
50{
51 RT_NOREF(pvUser);
52 RTMsgErrorV(pszFormat, va);
53 RTMsgError("Error code %Rrc at %s(%u) in function %s", rc, RT_SRC_POS_ARGS);
54}
55
56static int parseMediumVariant(const char *psz, MediumVariant_T *pMediumVariant)
57{
58 int rc = VINF_SUCCESS;
59 unsigned uMediumVariant = (unsigned)(*pMediumVariant);
60 while (psz && *psz && RT_SUCCESS(rc))
61 {
62 size_t len;
63 const char *pszComma = strchr(psz, ',');
64 if (pszComma)
65 len = pszComma - psz;
66 else
67 len = strlen(psz);
68 if (len > 0)
69 {
70 // Parsing is intentionally inconsistent: "standard" resets the
71 // variant, whereas the other flags are cumulative.
72 if (!RTStrNICmp(psz, "standard", len))
73 uMediumVariant = MediumVariant_Standard;
74 else if ( !RTStrNICmp(psz, "fixed", len)
75 || !RTStrNICmp(psz, "static", len))
76 uMediumVariant |= MediumVariant_Fixed;
77 else if (!RTStrNICmp(psz, "Diff", len))
78 uMediumVariant |= MediumVariant_Diff;
79 else if (!RTStrNICmp(psz, "split2g", len))
80 uMediumVariant |= MediumVariant_VmdkSplit2G;
81 else if ( !RTStrNICmp(psz, "stream", len)
82 || !RTStrNICmp(psz, "streamoptimized", len))
83 uMediumVariant |= MediumVariant_VmdkStreamOptimized;
84 else if (!RTStrNICmp(psz, "esx", len))
85 uMediumVariant |= MediumVariant_VmdkESX;
86 else
87 rc = VERR_PARSE_ERROR;
88 }
89 if (pszComma)
90 psz += len + 1;
91 else
92 psz += len;
93 }
94
95 if (RT_SUCCESS(rc))
96 *pMediumVariant = (MediumVariant_T)uMediumVariant;
97 return rc;
98}
99
100int parseMediumType(const char *psz, MediumType_T *penmMediumType)
101{
102 int rc = VINF_SUCCESS;
103 MediumType_T enmMediumType = MediumType_Normal;
104 if (!RTStrICmp(psz, "normal"))
105 enmMediumType = MediumType_Normal;
106 else if (!RTStrICmp(psz, "immutable"))
107 enmMediumType = MediumType_Immutable;
108 else if (!RTStrICmp(psz, "writethrough"))
109 enmMediumType = MediumType_Writethrough;
110 else if (!RTStrICmp(psz, "shareable"))
111 enmMediumType = MediumType_Shareable;
112 else if (!RTStrICmp(psz, "readonly"))
113 enmMediumType = MediumType_Readonly;
114 else if (!RTStrICmp(psz, "multiattach"))
115 enmMediumType = MediumType_MultiAttach;
116 else
117 rc = VERR_PARSE_ERROR;
118
119 if (RT_SUCCESS(rc))
120 *penmMediumType = enmMediumType;
121 return rc;
122}
123
124/** @todo move this into getopt, as getting bool values is generic */
125int parseBool(const char *psz, bool *pb)
126{
127 int rc = VINF_SUCCESS;
128 if ( !RTStrICmp(psz, "on")
129 || !RTStrICmp(psz, "yes")
130 || !RTStrICmp(psz, "true")
131 || !RTStrICmp(psz, "1")
132 || !RTStrICmp(psz, "enable")
133 || !RTStrICmp(psz, "enabled"))
134 {
135 *pb = true;
136 }
137 else if ( !RTStrICmp(psz, "off")
138 || !RTStrICmp(psz, "no")
139 || !RTStrICmp(psz, "false")
140 || !RTStrICmp(psz, "0")
141 || !RTStrICmp(psz, "disable")
142 || !RTStrICmp(psz, "disabled"))
143 {
144 *pb = false;
145 }
146 else
147 rc = VERR_PARSE_ERROR;
148
149 return rc;
150}
151
152HRESULT openMedium(HandlerArg *a, const char *pszFilenameOrUuid,
153 DeviceType_T enmDevType, AccessMode_T enmAccessMode,
154 ComPtr<IMedium> &pMedium, bool fForceNewUuidOnOpen,
155 bool fSilent)
156{
157 HRESULT rc;
158 Guid id(pszFilenameOrUuid);
159 char szFilenameAbs[RTPATH_MAX] = "";
160
161 /* If it is no UUID, convert the filename to an absolute one. */
162 if (!id.isValid())
163 {
164 int irc = RTPathAbs(pszFilenameOrUuid, szFilenameAbs, sizeof(szFilenameAbs));
165 if (RT_FAILURE(irc))
166 {
167 if (!fSilent)
168 RTMsgError("Cannot convert filename \"%s\" to absolute path", pszFilenameOrUuid);
169 return E_FAIL;
170 }
171 pszFilenameOrUuid = szFilenameAbs;
172 }
173
174 if (!fSilent)
175 CHECK_ERROR(a->virtualBox, OpenMedium(Bstr(pszFilenameOrUuid).raw(),
176 enmDevType,
177 enmAccessMode,
178 fForceNewUuidOnOpen,
179 pMedium.asOutParam()));
180 else
181 rc = a->virtualBox->OpenMedium(Bstr(pszFilenameOrUuid).raw(),
182 enmDevType,
183 enmAccessMode,
184 fForceNewUuidOnOpen,
185 pMedium.asOutParam());
186
187 return rc;
188}
189
190static HRESULT createMedium(HandlerArg *a, const char *pszFormat,
191 const char *pszFilename, DeviceType_T enmDevType,
192 AccessMode_T enmAccessMode, ComPtr<IMedium> &pMedium)
193{
194 HRESULT rc;
195 char szFilenameAbs[RTPATH_MAX] = "";
196
197 /** @todo laziness shortcut. should really check the MediumFormatCapabilities */
198 if (RTStrICmp(pszFormat, "iSCSI"))
199 {
200 int irc = RTPathAbs(pszFilename, szFilenameAbs, sizeof(szFilenameAbs));
201 if (RT_FAILURE(irc))
202 {
203 RTMsgError("Cannot convert filename \"%s\" to absolute path", pszFilename);
204 return E_FAIL;
205 }
206 pszFilename = szFilenameAbs;
207 }
208
209 CHECK_ERROR(a->virtualBox, CreateMedium(Bstr(pszFormat).raw(),
210 Bstr(pszFilename).raw(),
211 enmAccessMode,
212 enmDevType,
213 pMedium.asOutParam()));
214 return rc;
215}
216
217static const RTGETOPTDEF g_aCreateMediumOptions[] =
218{
219 { "disk", 'H', RTGETOPT_REQ_NOTHING },
220 { "dvd", 'D', RTGETOPT_REQ_NOTHING },
221 { "floppy", 'L', RTGETOPT_REQ_NOTHING },
222 { "--filename", 'f', RTGETOPT_REQ_STRING },
223 { "-filename", 'f', RTGETOPT_REQ_STRING }, // deprecated
224 { "--diffparent", 'd', RTGETOPT_REQ_STRING },
225 { "--size", 's', RTGETOPT_REQ_UINT64 },
226 { "-size", 's', RTGETOPT_REQ_UINT64 }, // deprecated
227 { "--sizebyte", 'S', RTGETOPT_REQ_UINT64 },
228 { "--format", 'o', RTGETOPT_REQ_STRING },
229 { "-format", 'o', RTGETOPT_REQ_STRING }, // deprecated
230 { "--static", 'F', RTGETOPT_REQ_NOTHING },
231 { "-static", 'F', RTGETOPT_REQ_NOTHING }, // deprecated
232 { "--variant", 'm', RTGETOPT_REQ_STRING },
233 { "-variant", 'm', RTGETOPT_REQ_STRING }, // deprecated
234};
235
236RTEXITCODE handleCreateMedium(HandlerArg *a)
237{
238 HRESULT rc;
239 int vrc;
240 const char *filename = NULL;
241 const char *diffparent = NULL;
242 uint64_t size = 0;
243 enum {
244 CMD_NONE,
245 CMD_DISK,
246 CMD_DVD,
247 CMD_FLOPPY
248 } cmd = CMD_NONE;
249 const char *format = NULL;
250 bool fBase = true;
251 MediumVariant_T enmMediumVariant = MediumVariant_Standard;
252
253 int c;
254 RTGETOPTUNION ValueUnion;
255 RTGETOPTSTATE GetState;
256 // start at 0 because main() has hacked both the argc and argv given to us
257 RTGetOptInit(&GetState, a->argc, a->argv, g_aCreateMediumOptions, RT_ELEMENTS(g_aCreateMediumOptions),
258 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
259 while ((c = RTGetOpt(&GetState, &ValueUnion)))
260 {
261 switch (c)
262 {
263 case 'H': // disk
264 if (cmd != CMD_NONE)
265 return errorSyntax(USAGE_CREATEMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
266 cmd = CMD_DISK;
267 break;
268
269 case 'D': // DVD
270 if (cmd != CMD_NONE)
271 return errorSyntax(USAGE_CREATEMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
272 cmd = CMD_DVD;
273 break;
274
275 case 'L': // floppy
276 if (cmd != CMD_NONE)
277 return errorSyntax(USAGE_CREATEMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
278 cmd = CMD_FLOPPY;
279 break;
280
281 case 'f': // --filename
282 filename = ValueUnion.psz;
283 break;
284
285 case 'd': // --diffparent
286 diffparent = ValueUnion.psz;
287 fBase = false;
288 break;
289
290 case 's': // --size
291 size = ValueUnion.u64 * _1M;
292 break;
293
294 case 'S': // --sizebyte
295 size = ValueUnion.u64;
296 break;
297
298 case 'o': // --format
299 format = ValueUnion.psz;
300 break;
301
302 case 'F': // --static ("fixed"/"flat")
303 {
304 unsigned uMediumVariant = (unsigned)enmMediumVariant;
305 uMediumVariant |= MediumVariant_Fixed;
306 enmMediumVariant = (MediumVariant_T)uMediumVariant;
307 break;
308 }
309
310 case 'm': // --variant
311 vrc = parseMediumVariant(ValueUnion.psz, &enmMediumVariant);
312 if (RT_FAILURE(vrc))
313 return errorArgument("Invalid medium variant '%s'", ValueUnion.psz);
314 break;
315
316 case VINF_GETOPT_NOT_OPTION:
317 return errorSyntax(USAGE_CREATEMEDIUM, "Invalid parameter '%s'", ValueUnion.psz);
318
319 default:
320 if (c > 0)
321 {
322 if (RT_C_IS_PRINT(c))
323 return errorSyntax(USAGE_CREATEMEDIUM, "Invalid option -%c", c);
324 else
325 return errorSyntax(USAGE_CREATEMEDIUM, "Invalid option case %i", c);
326 }
327 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
328 return errorSyntax(USAGE_CREATEMEDIUM, "unknown option: %s\n", ValueUnion.psz);
329 else if (ValueUnion.pDef)
330 return errorSyntax(USAGE_CREATEMEDIUM, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
331 else
332 return errorSyntax(USAGE_CREATEMEDIUM, "error: %Rrs", c);
333 }
334 }
335
336 /* check the outcome */
337 if (cmd == CMD_NONE)
338 cmd = CMD_DISK;
339 ComPtr<IMedium> pParentMedium;
340 if (fBase)
341 {
342 if ( !filename
343 || !*filename
344 || size == 0)
345 return errorSyntax(USAGE_CREATEMEDIUM, "Parameters --filename and --size are required");
346 if (!format || !*format)
347 {
348 if (cmd == CMD_DISK)
349 format = "VDI";
350 else if (cmd == CMD_DVD || cmd == CMD_FLOPPY)
351 {
352 format = "RAW";
353 unsigned uMediumVariant = (unsigned)enmMediumVariant;
354 uMediumVariant |= MediumVariant_Fixed;
355 enmMediumVariant = (MediumVariant_T)uMediumVariant;
356 }
357 }
358 }
359 else
360 {
361 if ( !filename
362 || !*filename)
363 return errorSyntax(USAGE_CREATEMEDIUM, "Parameters --filename is required");
364 size = 0;
365 if (cmd != CMD_DISK)
366 return errorSyntax(USAGE_CREATEMEDIUM, "Creating a differencing medium is only supported for hard disks");
367 enmMediumVariant = MediumVariant_Diff;
368 if (!format || !*format)
369 {
370 const char *pszExt = RTPathSuffix(filename);
371 /* Skip over . if there is an extension. */
372 if (pszExt)
373 pszExt++;
374 if (!pszExt || !*pszExt)
375 format = "VDI";
376 else
377 format = pszExt;
378 }
379 rc = openMedium(a, diffparent, DeviceType_HardDisk,
380 AccessMode_ReadWrite, pParentMedium,
381 false /* fForceNewUuidOnOpen */, false /* fSilent */);
382 if (FAILED(rc))
383 return RTEXITCODE_FAILURE;
384 if (pParentMedium.isNull())
385 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Invalid parent hard disk reference, avoiding crash");
386 MediumState_T state;
387 CHECK_ERROR(pParentMedium, COMGETTER(State)(&state));
388 if (FAILED(rc))
389 return RTEXITCODE_FAILURE;
390 if (state == MediumState_Inaccessible)
391 {
392 CHECK_ERROR(pParentMedium, RefreshState(&state));
393 if (FAILED(rc))
394 return RTEXITCODE_FAILURE;
395 }
396 }
397 /* check for filename extension */
398 /** @todo use IMediumFormat to cover all extensions generically */
399 Utf8Str strName(filename);
400 if (!RTPathHasSuffix(strName.c_str()))
401 {
402 Utf8Str strFormat(format);
403 if (cmd == CMD_DISK)
404 {
405 if (strFormat.compare("vmdk", RTCString::CaseInsensitive) == 0)
406 strName.append(".vmdk");
407 else if (strFormat.compare("vhd", RTCString::CaseInsensitive) == 0)
408 strName.append(".vhd");
409 else
410 strName.append(".vdi");
411 } else if (cmd == CMD_DVD)
412 strName.append(".iso");
413 else if (cmd == CMD_FLOPPY)
414 strName.append(".img");
415 filename = strName.c_str();
416 }
417
418 ComPtr<IMedium> pMedium;
419 if (cmd == CMD_DISK)
420 rc = createMedium(a, format, filename, DeviceType_HardDisk,
421 AccessMode_ReadWrite, pMedium);
422 else if (cmd == CMD_DVD)
423 rc = createMedium(a, format, filename, DeviceType_DVD,
424 AccessMode_ReadOnly, pMedium);
425 else if (cmd == CMD_FLOPPY)
426 rc = createMedium(a, format, filename, DeviceType_Floppy,
427 AccessMode_ReadWrite, pMedium);
428 else
429 rc = E_INVALIDARG; /* cannot happen but make gcc happy */
430
431 if (SUCCEEDED(rc) && pMedium)
432 {
433 ComPtr<IProgress> pProgress;
434 com::SafeArray<MediumVariant_T> l_variants(sizeof(MediumVariant_T)*8);
435
436 for (ULONG i = 0; i < l_variants.size(); ++i)
437 {
438 ULONG temp = enmMediumVariant;
439 temp &= 1<<i;
440 l_variants [i] = (MediumVariant_T)temp;
441 }
442
443 if (fBase)
444 CHECK_ERROR(pMedium, CreateBaseStorage(size, ComSafeArrayAsInParam(l_variants), pProgress.asOutParam()));
445 else
446 CHECK_ERROR(pParentMedium, CreateDiffStorage(pMedium, ComSafeArrayAsInParam(l_variants), pProgress.asOutParam()));
447 if (SUCCEEDED(rc) && pProgress)
448 {
449 rc = showProgress(pProgress);
450 CHECK_PROGRESS_ERROR(pProgress, ("Failed to create medium"));
451 }
452 }
453
454 if (SUCCEEDED(rc) && pMedium)
455 {
456 Bstr uuid;
457 CHECK_ERROR(pMedium, COMGETTER(Id)(uuid.asOutParam()));
458 RTPrintf("Medium created. UUID: %s\n", Utf8Str(uuid).c_str());
459
460 //CHECK_ERROR(pMedium, Close());
461 }
462 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
463}
464
465static const RTGETOPTDEF g_aModifyMediumOptions[] =
466{
467 { "disk", 'H', RTGETOPT_REQ_NOTHING },
468 { "dvd", 'D', RTGETOPT_REQ_NOTHING },
469 { "floppy", 'L', RTGETOPT_REQ_NOTHING },
470 { "--type", 't', RTGETOPT_REQ_STRING },
471 { "-type", 't', RTGETOPT_REQ_STRING }, // deprecated
472 { "settype", 't', RTGETOPT_REQ_STRING }, // deprecated
473 { "--autoreset", 'z', RTGETOPT_REQ_STRING },
474 { "-autoreset", 'z', RTGETOPT_REQ_STRING }, // deprecated
475 { "autoreset", 'z', RTGETOPT_REQ_STRING }, // deprecated
476 { "--property", 'p', RTGETOPT_REQ_STRING },
477 { "--compact", 'c', RTGETOPT_REQ_NOTHING },
478 { "-compact", 'c', RTGETOPT_REQ_NOTHING }, // deprecated
479 { "compact", 'c', RTGETOPT_REQ_NOTHING }, // deprecated
480 { "--resize", 'r', RTGETOPT_REQ_UINT64 },
481 { "--resizebyte", 'R', RTGETOPT_REQ_UINT64 },
482 { "--move", 'm', RTGETOPT_REQ_STRING },
483 { "--description", 'd', RTGETOPT_REQ_STRING }
484};
485
486RTEXITCODE handleModifyMedium(HandlerArg *a)
487{
488 HRESULT rc;
489 int vrc;
490 enum {
491 CMD_NONE,
492 CMD_DISK,
493 CMD_DVD,
494 CMD_FLOPPY
495 } cmd = CMD_NONE;
496 ComPtr<IMedium> pMedium;
497 MediumType_T enmMediumType = MediumType_Normal; /* Shut up MSC */
498 bool AutoReset = false;
499 SafeArray<BSTR> mediumPropNames;
500 SafeArray<BSTR> mediumPropValues;
501 bool fModifyMediumType = false;
502 bool fModifyAutoReset = false;
503 bool fModifyProperties = false;
504 bool fModifyCompact = false;
505 bool fModifyResize = false;
506 bool fModifyResizeMB = false;
507 bool fModifyLocation = false;
508 bool fModifyDescription = false;
509 uint64_t cbResize = 0;
510 const char *pszFilenameOrUuid = NULL;
511 char *pszNewLocation = NULL;
512
513 int c;
514 RTGETOPTUNION ValueUnion;
515 RTGETOPTSTATE GetState;
516 // start at 0 because main() has hacked both the argc and argv given to us
517 RTGetOptInit(&GetState, a->argc, a->argv, g_aModifyMediumOptions, RT_ELEMENTS(g_aModifyMediumOptions),
518 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
519 while ((c = RTGetOpt(&GetState, &ValueUnion)))
520 {
521 switch (c)
522 {
523 case 'H': // disk
524 if (cmd != CMD_NONE)
525 return errorSyntax(USAGE_MODIFYMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
526 cmd = CMD_DISK;
527 break;
528
529 case 'D': // DVD
530 if (cmd != CMD_NONE)
531 return errorSyntax(USAGE_MODIFYMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
532 cmd = CMD_DVD;
533 break;
534
535 case 'L': // floppy
536 if (cmd != CMD_NONE)
537 return errorSyntax(USAGE_MODIFYMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
538 cmd = CMD_FLOPPY;
539 break;
540
541 case 't': // --type
542 vrc = parseMediumType(ValueUnion.psz, &enmMediumType);
543 if (RT_FAILURE(vrc))
544 return errorArgument("Invalid medium type '%s'", ValueUnion.psz);
545 fModifyMediumType = true;
546 break;
547
548 case 'z': // --autoreset
549 vrc = parseBool(ValueUnion.psz, &AutoReset);
550 if (RT_FAILURE(vrc))
551 return errorArgument("Invalid autoreset parameter '%s'", ValueUnion.psz);
552 fModifyAutoReset = true;
553 break;
554
555 case 'p': // --property
556 {
557 /* Parse 'name=value' */
558 char *pszProperty = RTStrDup(ValueUnion.psz);
559 if (pszProperty)
560 {
561 char *pDelimiter = strchr(pszProperty, '=');
562 if (pDelimiter)
563 {
564 *pDelimiter = '\0';
565
566 Bstr bstrName(pszProperty);
567 Bstr bstrValue(&pDelimiter[1]);
568 bstrName.detachTo(mediumPropNames.appendedRaw());
569 bstrValue.detachTo(mediumPropValues.appendedRaw());
570 fModifyProperties = true;
571 }
572 else
573 {
574 errorArgument("Invalid --property argument '%s'", ValueUnion.psz);
575 rc = E_FAIL;
576 }
577 RTStrFree(pszProperty);
578 }
579 else
580 {
581 RTStrmPrintf(g_pStdErr, "Error: Failed to allocate memory for medium property '%s'\n", ValueUnion.psz);
582 rc = E_FAIL;
583 }
584 break;
585 }
586
587 case 'c': // --compact
588 fModifyCompact = true;
589 break;
590
591 case 'r': // --resize
592 cbResize = ValueUnion.u64 * _1M;
593 fModifyResize = true;
594 fModifyResizeMB = true; // do sanity check!
595 break;
596
597 case 'R': // --resizebyte
598 cbResize = ValueUnion.u64;
599 fModifyResize = true;
600 break;
601
602 case 'm': // --move
603 /* Get a new location */
604 pszNewLocation = RTPathAbsDup(ValueUnion.psz);
605 fModifyLocation = true;
606 break;
607
608 case 'd': // --description
609 /* Get a new description */
610 pszNewLocation = RTStrDup(ValueUnion.psz);
611 fModifyDescription = true;
612 break;
613
614 case VINF_GETOPT_NOT_OPTION:
615 if (!pszFilenameOrUuid)
616 pszFilenameOrUuid = ValueUnion.psz;
617 else
618 return errorSyntax(USAGE_MODIFYMEDIUM, "Invalid parameter '%s'", ValueUnion.psz);
619 break;
620
621 default:
622 if (c > 0)
623 {
624 if (RT_C_IS_PRINT(c))
625 return errorSyntax(USAGE_MODIFYMEDIUM, "Invalid option -%c", c);
626 else
627 return errorSyntax(USAGE_MODIFYMEDIUM, "Invalid option case %i", c);
628 }
629 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
630 return errorSyntax(USAGE_MODIFYMEDIUM, "unknown option: %s\n", ValueUnion.psz);
631 else if (ValueUnion.pDef)
632 return errorSyntax(USAGE_MODIFYMEDIUM, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
633 else
634 return errorSyntax(USAGE_MODIFYMEDIUM, "error: %Rrs", c);
635 }
636 }
637
638 if (cmd == CMD_NONE)
639 cmd = CMD_DISK;
640
641 if (!pszFilenameOrUuid)
642 return errorSyntax(USAGE_MODIFYMEDIUM, "Medium name or UUID required");
643
644 if (!fModifyMediumType
645 && !fModifyAutoReset
646 && !fModifyProperties
647 && !fModifyCompact
648 && !fModifyResize
649 && !fModifyLocation
650 && !fModifyDescription)
651 return errorSyntax(USAGE_MODIFYMEDIUM, "No operation specified");
652
653 /* Always open the medium if necessary, there is no other way. */
654 if (cmd == CMD_DISK)
655 rc = openMedium(a, pszFilenameOrUuid, DeviceType_HardDisk,
656 AccessMode_ReadWrite, pMedium,
657 false /* fForceNewUuidOnOpen */, false /* fSilent */);
658 else if (cmd == CMD_DVD)
659 rc = openMedium(a, pszFilenameOrUuid, DeviceType_DVD,
660 AccessMode_ReadOnly, pMedium,
661 false /* fForceNewUuidOnOpen */, false /* fSilent */);
662 else if (cmd == CMD_FLOPPY)
663 rc = openMedium(a, pszFilenameOrUuid, DeviceType_Floppy,
664 AccessMode_ReadWrite, pMedium,
665 false /* fForceNewUuidOnOpen */, false /* fSilent */);
666 else
667 rc = E_INVALIDARG; /* cannot happen but make gcc happy */
668 if (FAILED(rc))
669 return RTEXITCODE_FAILURE;
670 if (pMedium.isNull())
671 {
672 RTMsgError("Invalid medium reference, avoiding crash");
673 return RTEXITCODE_FAILURE;
674 }
675
676 if ( fModifyResize
677 && fModifyResizeMB)
678 {
679 // Sanity check
680 //
681 // In general users should know what they do but in this case users have no
682 // alternative to VBoxManage. If happens that one wants to resize the disk
683 // and uses --resize and does not consider that this parameter expects the
684 // new medium size in MB not Byte. If the operation is started and then
685 // aborted by the user, the result is most likely a medium which doesn't
686 // work anymore.
687 MediumState_T state;
688 pMedium->RefreshState(&state);
689 LONG64 logicalSize;
690 pMedium->COMGETTER(LogicalSize)(&logicalSize);
691 if (cbResize > (uint64_t)logicalSize * 1000)
692 {
693 RTMsgError("Error: Attempt to resize the medium from %RU64.%RU64 MB to %RU64.%RU64 MB. Use --resizebyte if this is intended!\n",
694 logicalSize / _1M, (logicalSize % _1M) / (_1M / 10), cbResize / _1M, (cbResize % _1M) / (_1M / 10));
695 return RTEXITCODE_FAILURE;
696 }
697 }
698
699 if (fModifyMediumType)
700 {
701 MediumType_T enmCurrMediumType;
702 CHECK_ERROR(pMedium, COMGETTER(Type)(&enmCurrMediumType));
703
704 if (enmCurrMediumType != enmMediumType)
705 CHECK_ERROR(pMedium, COMSETTER(Type)(enmMediumType));
706 }
707
708 if (fModifyAutoReset)
709 {
710 CHECK_ERROR(pMedium, COMSETTER(AutoReset)(AutoReset));
711 }
712
713 if (fModifyProperties)
714 {
715 CHECK_ERROR(pMedium, SetProperties(ComSafeArrayAsInParam(mediumPropNames), ComSafeArrayAsInParam(mediumPropValues)));
716 }
717
718 if (fModifyCompact)
719 {
720 ComPtr<IProgress> pProgress;
721 CHECK_ERROR(pMedium, Compact(pProgress.asOutParam()));
722 if (SUCCEEDED(rc))
723 rc = showProgress(pProgress);
724 if (FAILED(rc))
725 {
726 if (rc == E_NOTIMPL)
727 RTMsgError("Compact medium operation is not implemented!");
728 else if (rc == VBOX_E_NOT_SUPPORTED)
729 RTMsgError("Compact medium operation for this format is not implemented yet!");
730 else if (!pProgress.isNull())
731 CHECK_PROGRESS_ERROR(pProgress, ("Failed to compact medium"));
732 else
733 RTMsgError("Failed to compact medium!");
734 }
735 }
736
737 if (fModifyResize)
738 {
739 ComPtr<IProgress> pProgress;
740 CHECK_ERROR(pMedium, Resize(cbResize, pProgress.asOutParam()));
741 if (SUCCEEDED(rc))
742 rc = showProgress(pProgress);
743 if (FAILED(rc))
744 {
745 if (rc == E_NOTIMPL)
746 RTMsgError("Resize medium operation is not implemented!");
747 else if (rc == VBOX_E_NOT_SUPPORTED)
748 RTMsgError("Resize medium operation for this format is not implemented yet!");
749 else if (!pProgress.isNull())
750 CHECK_PROGRESS_ERROR(pProgress, ("Failed to resize medium"));
751 else
752 RTMsgError("Failed to resize medium!");
753 }
754 }
755
756 if (fModifyLocation)
757 {
758 do
759 {
760 ComPtr<IProgress> pProgress;
761 Utf8Str strLocation(pszNewLocation);
762 RTStrFree(pszNewLocation);
763 CHECK_ERROR(pMedium, SetLocation(Bstr(strLocation).raw(), pProgress.asOutParam()));
764
765 if (SUCCEEDED(rc) && !pProgress.isNull())
766 {
767 rc = showProgress(pProgress);
768 CHECK_PROGRESS_ERROR(pProgress, ("Failed to move medium"));
769 }
770
771 Bstr uuid;
772 CHECK_ERROR_BREAK(pMedium, COMGETTER(Id)(uuid.asOutParam()));
773
774 RTPrintf("Move medium with UUID %s finished \n", Utf8Str(uuid).c_str());
775 }
776 while (0);
777 }
778
779 if (fModifyDescription)
780 {
781 CHECK_ERROR(pMedium, COMSETTER(Description)(Bstr(pszNewLocation).raw()));
782
783 RTPrintf("Medium description has been changed. \n");
784 }
785
786 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
787}
788
789static const RTGETOPTDEF g_aCloneMediumOptions[] =
790{
791 { "disk", 'd', RTGETOPT_REQ_NOTHING },
792 { "dvd", 'D', RTGETOPT_REQ_NOTHING },
793 { "floppy", 'f', RTGETOPT_REQ_NOTHING },
794 { "--format", 'o', RTGETOPT_REQ_STRING },
795 { "-format", 'o', RTGETOPT_REQ_STRING },
796 { "--static", 'F', RTGETOPT_REQ_NOTHING },
797 { "-static", 'F', RTGETOPT_REQ_NOTHING },
798 { "--existing", 'E', RTGETOPT_REQ_NOTHING },
799 { "--variant", 'm', RTGETOPT_REQ_STRING },
800 { "-variant", 'm', RTGETOPT_REQ_STRING },
801};
802
803RTEXITCODE handleCloneMedium(HandlerArg *a)
804{
805 HRESULT rc;
806 int vrc;
807 enum {
808 CMD_NONE,
809 CMD_DISK,
810 CMD_DVD,
811 CMD_FLOPPY
812 } cmd = CMD_NONE;
813 const char *pszSrc = NULL;
814 const char *pszDst = NULL;
815 Bstr format;
816 MediumVariant_T enmMediumVariant = MediumVariant_Standard;
817 bool fExisting = false;
818
819 int c;
820 RTGETOPTUNION ValueUnion;
821 RTGETOPTSTATE GetState;
822 // start at 0 because main() has hacked both the argc and argv given to us
823 RTGetOptInit(&GetState, a->argc, a->argv, g_aCloneMediumOptions, RT_ELEMENTS(g_aCloneMediumOptions),
824 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
825 while ((c = RTGetOpt(&GetState, &ValueUnion)))
826 {
827 switch (c)
828 {
829 case 'd': // disk
830 if (cmd != CMD_NONE)
831 return errorSyntax(USAGE_CLONEMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
832 cmd = CMD_DISK;
833 break;
834
835 case 'D': // DVD
836 if (cmd != CMD_NONE)
837 return errorSyntax(USAGE_CLONEMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
838 cmd = CMD_DVD;
839 break;
840
841 case 'f': // floppy
842 if (cmd != CMD_NONE)
843 return errorSyntax(USAGE_CLONEMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
844 cmd = CMD_FLOPPY;
845 break;
846
847 case 'o': // --format
848 format = ValueUnion.psz;
849 break;
850
851 case 'F': // --static
852 {
853 unsigned uMediumVariant = (unsigned)enmMediumVariant;
854 uMediumVariant |= MediumVariant_Fixed;
855 enmMediumVariant = (MediumVariant_T)uMediumVariant;
856 break;
857 }
858
859 case 'E': // --existing
860 fExisting = true;
861 break;
862
863 case 'm': // --variant
864 vrc = parseMediumVariant(ValueUnion.psz, &enmMediumVariant);
865 if (RT_FAILURE(vrc))
866 return errorArgument("Invalid medium variant '%s'", ValueUnion.psz);
867 break;
868
869 case VINF_GETOPT_NOT_OPTION:
870 if (!pszSrc)
871 pszSrc = ValueUnion.psz;
872 else if (!pszDst)
873 pszDst = ValueUnion.psz;
874 else
875 return errorSyntax(USAGE_CLONEMEDIUM, "Invalid parameter '%s'", ValueUnion.psz);
876 break;
877
878 default:
879 if (c > 0)
880 {
881 if (RT_C_IS_GRAPH(c))
882 return errorSyntax(USAGE_CLONEMEDIUM, "unhandled option: -%c", c);
883 else
884 return errorSyntax(USAGE_CLONEMEDIUM, "unhandled option: %i", c);
885 }
886 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
887 return errorSyntax(USAGE_CLONEMEDIUM, "unknown option: %s", ValueUnion.psz);
888 else if (ValueUnion.pDef)
889 return errorSyntax(USAGE_CLONEMEDIUM, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
890 else
891 return errorSyntax(USAGE_CLONEMEDIUM, "error: %Rrs", c);
892 }
893 }
894
895 if (cmd == CMD_NONE)
896 cmd = CMD_DISK;
897 if (!pszSrc)
898 return errorSyntax(USAGE_CLONEMEDIUM, "Mandatory UUID or input file parameter missing");
899 if (!pszDst)
900 return errorSyntax(USAGE_CLONEMEDIUM, "Mandatory output file parameter missing");
901 if (fExisting && (!format.isEmpty() || enmMediumVariant != MediumType_Normal))
902 return errorSyntax(USAGE_CLONEMEDIUM, "Specified options which cannot be used with --existing");
903
904 ComPtr<IMedium> pSrcMedium;
905 ComPtr<IMedium> pDstMedium;
906
907 if (cmd == CMD_DISK)
908 rc = openMedium(a, pszSrc, DeviceType_HardDisk, AccessMode_ReadOnly, pSrcMedium,
909 false /* fForceNewUuidOnOpen */, false /* fSilent */);
910 else if (cmd == CMD_DVD)
911 rc = openMedium(a, pszSrc, DeviceType_DVD, AccessMode_ReadOnly, pSrcMedium,
912 false /* fForceNewUuidOnOpen */, false /* fSilent */);
913 else if (cmd == CMD_FLOPPY)
914 rc = openMedium(a, pszSrc, DeviceType_Floppy, AccessMode_ReadOnly, pSrcMedium,
915 false /* fForceNewUuidOnOpen */, false /* fSilent */);
916 else
917 rc = E_INVALIDARG; /* cannot happen but make gcc happy */
918 if (FAILED(rc))
919 return RTEXITCODE_FAILURE;
920
921 do
922 {
923 /* open/create destination medium */
924 if (fExisting)
925 {
926 if (cmd == CMD_DISK)
927 rc = openMedium(a, pszDst, DeviceType_HardDisk, AccessMode_ReadWrite, pDstMedium,
928 false /* fForceNewUuidOnOpen */, false /* fSilent */);
929 else if (cmd == CMD_DVD)
930 rc = openMedium(a, pszDst, DeviceType_DVD, AccessMode_ReadOnly, pDstMedium,
931 false /* fForceNewUuidOnOpen */, false /* fSilent */);
932 else if (cmd == CMD_FLOPPY)
933 rc = openMedium(a, pszDst, DeviceType_Floppy, AccessMode_ReadWrite, pDstMedium,
934 false /* fForceNewUuidOnOpen */, false /* fSilent */);
935 if (FAILED(rc))
936 break;
937
938 /* Perform accessibility check now. */
939 MediumState_T state;
940 CHECK_ERROR_BREAK(pDstMedium, RefreshState(&state));
941 CHECK_ERROR_BREAK(pDstMedium, COMGETTER(Format)(format.asOutParam()));
942 }
943 else
944 {
945 /*
946 * In case the format is unspecified check that the source medium supports
947 * image creation and use the same format for the destination image.
948 * Use the default image format if it is not supported.
949 */
950 if (format.isEmpty())
951 {
952 ComPtr<IMediumFormat> pMediumFmt;
953 com::SafeArray<MediumFormatCapabilities_T> l_caps;
954 CHECK_ERROR_BREAK(pSrcMedium, COMGETTER(MediumFormat)(pMediumFmt.asOutParam()));
955 CHECK_ERROR_BREAK(pMediumFmt, COMGETTER(Capabilities)(ComSafeArrayAsOutParam(l_caps)));
956 ULONG caps=0;
957 for (size_t i = 0; i < l_caps.size(); i++)
958 caps |= l_caps[i];
959 if (caps & ( MediumFormatCapabilities_CreateDynamic
960 | MediumFormatCapabilities_CreateFixed))
961 CHECK_ERROR_BREAK(pMediumFmt, COMGETTER(Id)(format.asOutParam()));
962 }
963 Utf8Str strFormat(format);
964 if (cmd == CMD_DISK)
965 rc = createMedium(a, strFormat.c_str(), pszDst, DeviceType_HardDisk,
966 AccessMode_ReadWrite, pDstMedium);
967 else if (cmd == CMD_DVD)
968 rc = createMedium(a, strFormat.c_str(), pszDst, DeviceType_DVD,
969 AccessMode_ReadOnly, pDstMedium);
970 else if (cmd == CMD_FLOPPY)
971 rc = createMedium(a, strFormat.c_str(), pszDst, DeviceType_Floppy,
972 AccessMode_ReadWrite, pDstMedium);
973 if (FAILED(rc))
974 break;
975 }
976
977 ComPtr<IProgress> pProgress;
978 com::SafeArray<MediumVariant_T> l_variants(sizeof(MediumVariant_T)*8);
979
980 for (ULONG i = 0; i < l_variants.size(); ++i)
981 {
982 ULONG temp = enmMediumVariant;
983 temp &= 1<<i;
984 l_variants [i] = (MediumVariant_T)temp;
985 }
986
987 CHECK_ERROR_BREAK(pSrcMedium, CloneTo(pDstMedium, ComSafeArrayAsInParam(l_variants), NULL, pProgress.asOutParam()));
988
989 rc = showProgress(pProgress);
990 CHECK_PROGRESS_ERROR_BREAK(pProgress, ("Failed to clone medium"));
991
992 Bstr uuid;
993 CHECK_ERROR_BREAK(pDstMedium, COMGETTER(Id)(uuid.asOutParam()));
994
995 RTPrintf("Clone medium created in format '%ls'. UUID: %s\n",
996 format.raw(), Utf8Str(uuid).c_str());
997 }
998 while (0);
999
1000 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
1001}
1002
1003static const RTGETOPTDEF g_aConvertFromRawHardDiskOptions[] =
1004{
1005 { "--format", 'o', RTGETOPT_REQ_STRING },
1006 { "-format", 'o', RTGETOPT_REQ_STRING },
1007 { "--static", 'F', RTGETOPT_REQ_NOTHING },
1008 { "-static", 'F', RTGETOPT_REQ_NOTHING },
1009 { "--variant", 'm', RTGETOPT_REQ_STRING },
1010 { "-variant", 'm', RTGETOPT_REQ_STRING },
1011 { "--uuid", 'u', RTGETOPT_REQ_STRING },
1012};
1013
1014RTEXITCODE handleConvertFromRaw(HandlerArg *a)
1015{
1016 int rc = VINF_SUCCESS;
1017 bool fReadFromStdIn = false;
1018 const char *format = "VDI";
1019 const char *srcfilename = NULL;
1020 const char *dstfilename = NULL;
1021 const char *filesize = NULL;
1022 unsigned uImageFlags = VD_IMAGE_FLAGS_NONE;
1023 void *pvBuf = NULL;
1024 RTUUID uuid;
1025 PCRTUUID pUuid = NULL;
1026
1027 int c;
1028 RTGETOPTUNION ValueUnion;
1029 RTGETOPTSTATE GetState;
1030 // start at 0 because main() has hacked both the argc and argv given to us
1031 RTGetOptInit(&GetState, a->argc, a->argv, g_aConvertFromRawHardDiskOptions, RT_ELEMENTS(g_aConvertFromRawHardDiskOptions),
1032 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
1033 while ((c = RTGetOpt(&GetState, &ValueUnion)))
1034 {
1035 switch (c)
1036 {
1037 case 'u': // --uuid
1038 if (RT_FAILURE(RTUuidFromStr(&uuid, ValueUnion.psz)))
1039 return errorSyntax(USAGE_CONVERTFROMRAW, "Invalid UUID '%s'", ValueUnion.psz);
1040 pUuid = &uuid;
1041 break;
1042 case 'o': // --format
1043 format = ValueUnion.psz;
1044 break;
1045
1046 case 'm': // --variant
1047 {
1048 MediumVariant_T enmMediumVariant = MediumVariant_Standard;
1049 rc = parseMediumVariant(ValueUnion.psz, &enmMediumVariant);
1050 if (RT_FAILURE(rc))
1051 return errorArgument("Invalid medium variant '%s'", ValueUnion.psz);
1052 /// @todo cleaner solution than assuming 1:1 mapping?
1053 uImageFlags = (unsigned)enmMediumVariant;
1054 break;
1055 }
1056 case VINF_GETOPT_NOT_OPTION:
1057 if (!srcfilename)
1058 {
1059 srcfilename = ValueUnion.psz;
1060 fReadFromStdIn = !strcmp(srcfilename, "stdin");
1061 }
1062 else if (!dstfilename)
1063 dstfilename = ValueUnion.psz;
1064 else if (fReadFromStdIn && !filesize)
1065 filesize = ValueUnion.psz;
1066 else
1067 return errorSyntax(USAGE_CONVERTFROMRAW, "Invalid parameter '%s'", ValueUnion.psz);
1068 break;
1069
1070 default:
1071 return errorGetOpt(USAGE_CONVERTFROMRAW, c, &ValueUnion);
1072 }
1073 }
1074
1075 if (!srcfilename || !dstfilename || (fReadFromStdIn && !filesize))
1076 return errorSyntax(USAGE_CONVERTFROMRAW, "Incorrect number of parameters");
1077 RTStrmPrintf(g_pStdErr, "Converting from raw image file=\"%s\" to file=\"%s\"...\n",
1078 srcfilename, dstfilename);
1079
1080 PVDISK pDisk = NULL;
1081
1082 PVDINTERFACE pVDIfs = NULL;
1083 VDINTERFACEERROR vdInterfaceError;
1084 vdInterfaceError.pfnError = handleVDError;
1085 vdInterfaceError.pfnMessage = NULL;
1086
1087 rc = VDInterfaceAdd(&vdInterfaceError.Core, "VBoxManage_IError", VDINTERFACETYPE_ERROR,
1088 NULL, sizeof(VDINTERFACEERROR), &pVDIfs);
1089 AssertRC(rc);
1090
1091 /* open raw image file. */
1092 RTFILE File;
1093 if (fReadFromStdIn)
1094 rc = RTFileFromNative(&File, RTFILE_NATIVE_STDIN);
1095 else
1096 rc = RTFileOpen(&File, srcfilename, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
1097 if (RT_FAILURE(rc))
1098 {
1099 RTMsgError("Cannot open file \"%s\": %Rrc", srcfilename, rc);
1100 goto out;
1101 }
1102
1103 uint64_t cbFile;
1104 /* get image size. */
1105 if (fReadFromStdIn)
1106 cbFile = RTStrToUInt64(filesize);
1107 else
1108 rc = RTFileGetSize(File, &cbFile);
1109 if (RT_FAILURE(rc))
1110 {
1111 RTMsgError("Cannot get image size for file \"%s\": %Rrc", srcfilename, rc);
1112 goto out;
1113 }
1114
1115 RTStrmPrintf(g_pStdErr, "Creating %s image with size %RU64 bytes (%RU64MB)...\n",
1116 (uImageFlags & VD_IMAGE_FLAGS_FIXED) ? "fixed" : "dynamic", cbFile, (cbFile + _1M - 1) / _1M);
1117 char pszComment[256];
1118 RTStrPrintf(pszComment, sizeof(pszComment), "Converted image from %s", srcfilename);
1119 rc = VDCreate(pVDIfs, VDTYPE_HDD, &pDisk);
1120 if (RT_FAILURE(rc))
1121 {
1122 RTMsgError("Cannot create the virtual disk container: %Rrc", rc);
1123 goto out;
1124 }
1125
1126 Assert(RT_MIN(cbFile / 512 / 16 / 63, 16383) -
1127 (unsigned int)RT_MIN(cbFile / 512 / 16 / 63, 16383) == 0);
1128 VDGEOMETRY PCHS, LCHS;
1129 PCHS.cCylinders = (unsigned int)RT_MIN(cbFile / 512 / 16 / 63, 16383);
1130 PCHS.cHeads = 16;
1131 PCHS.cSectors = 63;
1132 LCHS.cCylinders = 0;
1133 LCHS.cHeads = 0;
1134 LCHS.cSectors = 0;
1135 rc = VDCreateBase(pDisk, format, dstfilename, cbFile,
1136 uImageFlags, pszComment, &PCHS, &LCHS, pUuid,
1137 VD_OPEN_FLAGS_NORMAL, NULL, NULL);
1138 if (RT_FAILURE(rc))
1139 {
1140 RTMsgError("Cannot create the disk image \"%s\": %Rrc", dstfilename, rc);
1141 goto out;
1142 }
1143
1144 size_t cbBuffer;
1145 cbBuffer = _1M;
1146 pvBuf = RTMemAlloc(cbBuffer);
1147 if (!pvBuf)
1148 {
1149 rc = VERR_NO_MEMORY;
1150 RTMsgError("Out of memory allocating buffers for image \"%s\": %Rrc", dstfilename, rc);
1151 goto out;
1152 }
1153
1154 uint64_t offFile;
1155 offFile = 0;
1156 while (offFile < cbFile)
1157 {
1158 size_t cbRead;
1159 size_t cbToRead;
1160 cbRead = 0;
1161 cbToRead = cbFile - offFile >= (uint64_t)cbBuffer ?
1162 cbBuffer : (size_t)(cbFile - offFile);
1163 rc = RTFileRead(File, pvBuf, cbToRead, &cbRead);
1164 if (RT_FAILURE(rc) || !cbRead)
1165 break;
1166 rc = VDWrite(pDisk, offFile, pvBuf, cbRead);
1167 if (RT_FAILURE(rc))
1168 {
1169 RTMsgError("Failed to write to disk image \"%s\": %Rrc", dstfilename, rc);
1170 goto out;
1171 }
1172 offFile += cbRead;
1173 }
1174
1175out:
1176 if (pvBuf)
1177 RTMemFree(pvBuf);
1178 if (pDisk)
1179 VDClose(pDisk, RT_FAILURE(rc));
1180 if (File != NIL_RTFILE)
1181 RTFileClose(File);
1182
1183 return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
1184}
1185
1186HRESULT showMediumInfo(const ComPtr<IVirtualBox> &pVirtualBox,
1187 const ComPtr<IMedium> &pMedium,
1188 const char *pszParentUUID,
1189 bool fOptLong)
1190{
1191 HRESULT rc = S_OK;
1192 do
1193 {
1194 Bstr uuid;
1195 pMedium->COMGETTER(Id)(uuid.asOutParam());
1196 RTPrintf("UUID: %ls\n", uuid.raw());
1197 if (pszParentUUID)
1198 RTPrintf("Parent UUID: %s\n", pszParentUUID);
1199
1200 /* check for accessibility */
1201 MediumState_T enmState;
1202 CHECK_ERROR_BREAK(pMedium, RefreshState(&enmState));
1203 const char *pszState = "unknown";
1204 switch (enmState)
1205 {
1206 case MediumState_NotCreated:
1207 pszState = "not created";
1208 break;
1209 case MediumState_Created:
1210 pszState = "created";
1211 break;
1212 case MediumState_LockedRead:
1213 pszState = "locked read";
1214 break;
1215 case MediumState_LockedWrite:
1216 pszState = "locked write";
1217 break;
1218 case MediumState_Inaccessible:
1219 pszState = "inaccessible";
1220 break;
1221 case MediumState_Creating:
1222 pszState = "creating";
1223 break;
1224 case MediumState_Deleting:
1225 pszState = "deleting";
1226 break;
1227 }
1228 RTPrintf("State: %s\n", pszState);
1229
1230 if (fOptLong && enmState == MediumState_Inaccessible)
1231 {
1232 Bstr err;
1233 CHECK_ERROR_BREAK(pMedium, COMGETTER(LastAccessError)(err.asOutParam()));
1234 RTPrintf("Access Error: %ls\n", err.raw());
1235 }
1236
1237 if (fOptLong)
1238 {
1239 Bstr description;
1240 pMedium->COMGETTER(Description)(description.asOutParam());
1241 if (!description.isEmpty())
1242 RTPrintf("Description: %ls\n", description.raw());
1243 }
1244
1245 MediumType_T type;
1246 pMedium->COMGETTER(Type)(&type);
1247 const char *typeStr = "unknown";
1248 switch (type)
1249 {
1250 case MediumType_Normal:
1251 if (pszParentUUID && Guid(pszParentUUID).isValid())
1252 typeStr = "normal (differencing)";
1253 else
1254 typeStr = "normal (base)";
1255 break;
1256 case MediumType_Immutable:
1257 typeStr = "immutable";
1258 break;
1259 case MediumType_Writethrough:
1260 typeStr = "writethrough";
1261 break;
1262 case MediumType_Shareable:
1263 typeStr = "shareable";
1264 break;
1265 case MediumType_Readonly:
1266 typeStr = "readonly";
1267 break;
1268 case MediumType_MultiAttach:
1269 typeStr = "multiattach";
1270 break;
1271 }
1272 RTPrintf("Type: %s\n", typeStr);
1273
1274 /* print out information specific for differencing media */
1275 if (fOptLong && pszParentUUID && Guid(pszParentUUID).isValid())
1276 {
1277 BOOL autoReset = FALSE;
1278 pMedium->COMGETTER(AutoReset)(&autoReset);
1279 RTPrintf("Auto-Reset: %s\n", autoReset ? "on" : "off");
1280 }
1281
1282 Bstr loc;
1283 pMedium->COMGETTER(Location)(loc.asOutParam());
1284 RTPrintf("Location: %ls\n", loc.raw());
1285
1286 Bstr format;
1287 pMedium->COMGETTER(Format)(format.asOutParam());
1288 RTPrintf("Storage format: %ls\n", format.raw());
1289
1290 if (fOptLong)
1291 {
1292 com::SafeArray<MediumVariant_T> safeArray_variant;
1293
1294 pMedium->COMGETTER(Variant)(ComSafeArrayAsOutParam(safeArray_variant));
1295 ULONG variant=0;
1296 for (size_t i = 0; i < safeArray_variant.size(); i++)
1297 variant |= safeArray_variant[i];
1298
1299 const char *variantStr = "unknown";
1300 switch (variant & ~(MediumVariant_Fixed | MediumVariant_Diff))
1301 {
1302 case MediumVariant_VmdkSplit2G:
1303 variantStr = "split2G";
1304 break;
1305 case MediumVariant_VmdkStreamOptimized:
1306 variantStr = "streamOptimized";
1307 break;
1308 case MediumVariant_VmdkESX:
1309 variantStr = "ESX";
1310 break;
1311 case MediumVariant_Standard:
1312 variantStr = "default";
1313 break;
1314 }
1315 const char *variantTypeStr = "dynamic";
1316 if (variant & MediumVariant_Fixed)
1317 variantTypeStr = "fixed";
1318 else if (variant & MediumVariant_Diff)
1319 variantTypeStr = "differencing";
1320 RTPrintf("Format variant: %s %s\n", variantTypeStr, variantStr);
1321 }
1322
1323 LONG64 logicalSize;
1324 pMedium->COMGETTER(LogicalSize)(&logicalSize);
1325 RTPrintf("Capacity: %lld MBytes\n", logicalSize >> 20);
1326 if (fOptLong)
1327 {
1328 LONG64 actualSize;
1329 pMedium->COMGETTER(Size)(&actualSize);
1330 RTPrintf("Size on disk: %lld MBytes\n", actualSize >> 20);
1331 }
1332
1333 Bstr strCipher;
1334 Bstr strPasswordId;
1335 HRESULT rc2 = pMedium->GetEncryptionSettings(strCipher.asOutParam(), strPasswordId.asOutParam());
1336 if (SUCCEEDED(rc2))
1337 {
1338 RTPrintf("Encryption: enabled\n");
1339 if (fOptLong)
1340 {
1341 RTPrintf("Cipher: %ls\n", strCipher.raw());
1342 RTPrintf("Password ID: %ls\n", strPasswordId.raw());
1343 }
1344 }
1345 else
1346 RTPrintf("Encryption: disabled\n");
1347
1348 if (fOptLong)
1349 {
1350 com::SafeArray<BSTR> names;
1351 com::SafeArray<BSTR> values;
1352 pMedium->GetProperties(Bstr().raw(), ComSafeArrayAsOutParam(names), ComSafeArrayAsOutParam(values));
1353 size_t cNames = names.size();
1354 size_t cValues = values.size();
1355 bool fFirst = true;
1356 for (size_t i = 0; i < cNames; i++)
1357 {
1358 Bstr value;
1359 if (i < cValues)
1360 value = values[i];
1361 RTPrintf("%s%ls=%ls\n",
1362 fFirst ? "Property: " : " ",
1363 names[i], value.raw());
1364 }
1365 }
1366
1367 if (fOptLong)
1368 {
1369 bool fFirst = true;
1370 com::SafeArray<BSTR> machineIds;
1371 pMedium->COMGETTER(MachineIds)(ComSafeArrayAsOutParam(machineIds));
1372 for (size_t i = 0; i < machineIds.size(); i++)
1373 {
1374 ComPtr<IMachine> pMachine;
1375 CHECK_ERROR(pVirtualBox, FindMachine(machineIds[i], pMachine.asOutParam()));
1376 if (pMachine)
1377 {
1378 Bstr name;
1379 pMachine->COMGETTER(Name)(name.asOutParam());
1380 pMachine->COMGETTER(Id)(uuid.asOutParam());
1381 RTPrintf("%s%ls (UUID: %ls)",
1382 fFirst ? "In use by VMs: " : " ",
1383 name.raw(), machineIds[i]);
1384 fFirst = false;
1385 com::SafeArray<BSTR> snapshotIds;
1386 pMedium->GetSnapshotIds(machineIds[i],
1387 ComSafeArrayAsOutParam(snapshotIds));
1388 for (size_t j = 0; j < snapshotIds.size(); j++)
1389 {
1390 ComPtr<ISnapshot> pSnapshot;
1391 pMachine->FindSnapshot(snapshotIds[j], pSnapshot.asOutParam());
1392 if (pSnapshot)
1393 {
1394 Bstr snapshotName;
1395 pSnapshot->COMGETTER(Name)(snapshotName.asOutParam());
1396 RTPrintf(" [%ls (UUID: %ls)]", snapshotName.raw(), snapshotIds[j]);
1397 }
1398 }
1399 RTPrintf("\n");
1400 }
1401 }
1402 }
1403
1404 if (fOptLong)
1405 {
1406 com::SafeIfaceArray<IMedium> children;
1407 pMedium->COMGETTER(Children)(ComSafeArrayAsOutParam(children));
1408 bool fFirst = true;
1409 for (size_t i = 0; i < children.size(); i++)
1410 {
1411 ComPtr<IMedium> pChild(children[i]);
1412 if (pChild)
1413 {
1414 Bstr childUUID;
1415 pChild->COMGETTER(Id)(childUUID.asOutParam());
1416 RTPrintf("%s%ls\n",
1417 fFirst ? "Child UUIDs: " : " ",
1418 childUUID.raw());
1419 fFirst = false;
1420 }
1421 }
1422 }
1423 }
1424 while (0);
1425
1426 return rc;
1427}
1428
1429static const RTGETOPTDEF g_aShowMediumInfoOptions[] =
1430{
1431 { "disk", 'd', RTGETOPT_REQ_NOTHING },
1432 { "dvd", 'D', RTGETOPT_REQ_NOTHING },
1433 { "floppy", 'f', RTGETOPT_REQ_NOTHING },
1434};
1435
1436RTEXITCODE handleShowMediumInfo(HandlerArg *a)
1437{
1438 enum {
1439 CMD_NONE,
1440 CMD_DISK,
1441 CMD_DVD,
1442 CMD_FLOPPY
1443 } cmd = CMD_NONE;
1444 const char *pszFilenameOrUuid = NULL;
1445
1446 int c;
1447 RTGETOPTUNION ValueUnion;
1448 RTGETOPTSTATE GetState;
1449 // start at 0 because main() has hacked both the argc and argv given to us
1450 RTGetOptInit(&GetState, a->argc, a->argv, g_aShowMediumInfoOptions, RT_ELEMENTS(g_aShowMediumInfoOptions),
1451 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
1452 while ((c = RTGetOpt(&GetState, &ValueUnion)))
1453 {
1454 switch (c)
1455 {
1456 case 'd': // disk
1457 if (cmd != CMD_NONE)
1458 return errorSyntax(USAGE_SHOWMEDIUMINFO, "Only one command can be specified: '%s'", ValueUnion.psz);
1459 cmd = CMD_DISK;
1460 break;
1461
1462 case 'D': // DVD
1463 if (cmd != CMD_NONE)
1464 return errorSyntax(USAGE_SHOWMEDIUMINFO, "Only one command can be specified: '%s'", ValueUnion.psz);
1465 cmd = CMD_DVD;
1466 break;
1467
1468 case 'f': // floppy
1469 if (cmd != CMD_NONE)
1470 return errorSyntax(USAGE_SHOWMEDIUMINFO, "Only one command can be specified: '%s'", ValueUnion.psz);
1471 cmd = CMD_FLOPPY;
1472 break;
1473
1474 case VINF_GETOPT_NOT_OPTION:
1475 if (!pszFilenameOrUuid)
1476 pszFilenameOrUuid = ValueUnion.psz;
1477 else
1478 return errorSyntax(USAGE_SHOWMEDIUMINFO, "Invalid parameter '%s'", ValueUnion.psz);
1479 break;
1480
1481 default:
1482 if (c > 0)
1483 {
1484 if (RT_C_IS_PRINT(c))
1485 return errorSyntax(USAGE_SHOWMEDIUMINFO, "Invalid option -%c", c);
1486 else
1487 return errorSyntax(USAGE_SHOWMEDIUMINFO, "Invalid option case %i", c);
1488 }
1489 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
1490 return errorSyntax(USAGE_SHOWMEDIUMINFO, "unknown option: %s\n", ValueUnion.psz);
1491 else if (ValueUnion.pDef)
1492 return errorSyntax(USAGE_SHOWMEDIUMINFO, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
1493 else
1494 return errorSyntax(USAGE_SHOWMEDIUMINFO, "error: %Rrs", c);
1495 }
1496 }
1497
1498 if (cmd == CMD_NONE)
1499 cmd = CMD_DISK;
1500
1501 /* check for required options */
1502 if (!pszFilenameOrUuid)
1503 return errorSyntax(USAGE_SHOWMEDIUMINFO, "Medium name or UUID required");
1504
1505 HRESULT rc = S_OK; /* Prevents warning. */
1506
1507 ComPtr<IMedium> pMedium;
1508 if (cmd == CMD_DISK)
1509 rc = openMedium(a, pszFilenameOrUuid, DeviceType_HardDisk,
1510 AccessMode_ReadOnly, pMedium,
1511 false /* fForceNewUuidOnOpen */, false /* fSilent */);
1512 else if (cmd == CMD_DVD)
1513 rc = openMedium(a, pszFilenameOrUuid, DeviceType_DVD,
1514 AccessMode_ReadOnly, pMedium,
1515 false /* fForceNewUuidOnOpen */, false /* fSilent */);
1516 else if (cmd == CMD_FLOPPY)
1517 rc = openMedium(a, pszFilenameOrUuid, DeviceType_Floppy,
1518 AccessMode_ReadOnly, pMedium,
1519 false /* fForceNewUuidOnOpen */, false /* fSilent */);
1520 if (FAILED(rc))
1521 return RTEXITCODE_FAILURE;
1522
1523 Utf8Str strParentUUID("base");
1524 ComPtr<IMedium> pParent;
1525 pMedium->COMGETTER(Parent)(pParent.asOutParam());
1526 if (!pParent.isNull())
1527 {
1528 Bstr bstrParentUUID;
1529 pParent->COMGETTER(Id)(bstrParentUUID.asOutParam());
1530 strParentUUID = bstrParentUUID;
1531 }
1532
1533 rc = showMediumInfo(a->virtualBox, pMedium, strParentUUID.c_str(), true);
1534
1535 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
1536}
1537
1538static const RTGETOPTDEF g_aCloseMediumOptions[] =
1539{
1540 { "disk", 'd', RTGETOPT_REQ_NOTHING },
1541 { "dvd", 'D', RTGETOPT_REQ_NOTHING },
1542 { "floppy", 'f', RTGETOPT_REQ_NOTHING },
1543 { "--delete", 'r', RTGETOPT_REQ_NOTHING },
1544};
1545
1546RTEXITCODE handleCloseMedium(HandlerArg *a)
1547{
1548 HRESULT rc = S_OK;
1549 enum {
1550 CMD_NONE,
1551 CMD_DISK,
1552 CMD_DVD,
1553 CMD_FLOPPY
1554 } cmd = CMD_NONE;
1555 const char *pszFilenameOrUuid = NULL;
1556 bool fDelete = false;
1557
1558 int c;
1559 RTGETOPTUNION ValueUnion;
1560 RTGETOPTSTATE GetState;
1561 // start at 0 because main() has hacked both the argc and argv given to us
1562 RTGetOptInit(&GetState, a->argc, a->argv, g_aCloseMediumOptions, RT_ELEMENTS(g_aCloseMediumOptions),
1563 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
1564 while ((c = RTGetOpt(&GetState, &ValueUnion)))
1565 {
1566 switch (c)
1567 {
1568 case 'd': // disk
1569 if (cmd != CMD_NONE)
1570 return errorSyntax(USAGE_CLOSEMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
1571 cmd = CMD_DISK;
1572 break;
1573
1574 case 'D': // DVD
1575 if (cmd != CMD_NONE)
1576 return errorSyntax(USAGE_CLOSEMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
1577 cmd = CMD_DVD;
1578 break;
1579
1580 case 'f': // floppy
1581 if (cmd != CMD_NONE)
1582 return errorSyntax(USAGE_CLOSEMEDIUM, "Only one command can be specified: '%s'", ValueUnion.psz);
1583 cmd = CMD_FLOPPY;
1584 break;
1585
1586 case 'r': // --delete
1587 fDelete = true;
1588 break;
1589
1590 case VINF_GETOPT_NOT_OPTION:
1591 if (!pszFilenameOrUuid)
1592 pszFilenameOrUuid = ValueUnion.psz;
1593 else
1594 return errorSyntax(USAGE_CLOSEMEDIUM, "Invalid parameter '%s'", ValueUnion.psz);
1595 break;
1596
1597 default:
1598 if (c > 0)
1599 {
1600 if (RT_C_IS_PRINT(c))
1601 return errorSyntax(USAGE_CLOSEMEDIUM, "Invalid option -%c", c);
1602 else
1603 return errorSyntax(USAGE_CLOSEMEDIUM, "Invalid option case %i", c);
1604 }
1605 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
1606 return errorSyntax(USAGE_CLOSEMEDIUM, "unknown option: %s\n", ValueUnion.psz);
1607 else if (ValueUnion.pDef)
1608 return errorSyntax(USAGE_CLOSEMEDIUM, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
1609 else
1610 return errorSyntax(USAGE_CLOSEMEDIUM, "error: %Rrs", c);
1611 }
1612 }
1613
1614 /* check for required options */
1615 if (cmd == CMD_NONE)
1616 cmd = CMD_DISK;
1617 if (!pszFilenameOrUuid)
1618 return errorSyntax(USAGE_CLOSEMEDIUM, "Medium name or UUID required");
1619
1620 ComPtr<IMedium> pMedium;
1621 if (cmd == CMD_DISK)
1622 rc = openMedium(a, pszFilenameOrUuid, DeviceType_HardDisk,
1623 AccessMode_ReadWrite, pMedium,
1624 false /* fForceNewUuidOnOpen */, false /* fSilent */);
1625 else if (cmd == CMD_DVD)
1626 rc = openMedium(a, pszFilenameOrUuid, DeviceType_DVD,
1627 AccessMode_ReadOnly, pMedium,
1628 false /* fForceNewUuidOnOpen */, false /* fSilent */);
1629 else if (cmd == CMD_FLOPPY)
1630 rc = openMedium(a, pszFilenameOrUuid, DeviceType_Floppy,
1631 AccessMode_ReadWrite, pMedium,
1632 false /* fForceNewUuidOnOpen */, false /* fSilent */);
1633
1634 if (SUCCEEDED(rc) && pMedium)
1635 {
1636 if (fDelete)
1637 {
1638 ComPtr<IProgress> pProgress;
1639 CHECK_ERROR(pMedium, DeleteStorage(pProgress.asOutParam()));
1640 if (SUCCEEDED(rc))
1641 {
1642 rc = showProgress(pProgress);
1643 CHECK_PROGRESS_ERROR(pProgress, ("Failed to delete medium"));
1644 }
1645 else
1646 RTMsgError("Failed to delete medium. Error code %Rrc", rc);
1647 }
1648 CHECK_ERROR(pMedium, Close());
1649 }
1650
1651 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
1652}
1653
1654RTEXITCODE handleMediumProperty(HandlerArg *a)
1655{
1656 HRESULT rc = S_OK;
1657 const char *pszCmd = NULL;
1658 enum {
1659 CMD_NONE,
1660 CMD_DISK,
1661 CMD_DVD,
1662 CMD_FLOPPY
1663 } cmd = CMD_NONE;
1664 const char *pszAction = NULL;
1665 const char *pszFilenameOrUuid = NULL;
1666 const char *pszProperty = NULL;
1667 ComPtr<IMedium> pMedium;
1668
1669 pszCmd = (a->argc > 0) ? a->argv[0] : "";
1670 if ( !RTStrICmp(pszCmd, "disk")
1671 || !RTStrICmp(pszCmd, "dvd")
1672 || !RTStrICmp(pszCmd, "floppy"))
1673 {
1674 if (!RTStrICmp(pszCmd, "disk"))
1675 cmd = CMD_DISK;
1676 else if (!RTStrICmp(pszCmd, "dvd"))
1677 cmd = CMD_DVD;
1678 else if (!RTStrICmp(pszCmd, "floppy"))
1679 cmd = CMD_FLOPPY;
1680 else
1681 {
1682 AssertMsgFailed(("unexpected parameter %s\n", pszCmd));
1683 cmd = CMD_DISK;
1684 }
1685 a->argv++;
1686 a->argc--;
1687 }
1688 else
1689 {
1690 pszCmd = NULL;
1691 cmd = CMD_DISK;
1692 }
1693
1694 if (a->argc == 0)
1695 return errorSyntax(USAGE_MEDIUMPROPERTY, "Missing action");
1696
1697 pszAction = a->argv[0];
1698 if ( RTStrICmp(pszAction, "set")
1699 && RTStrICmp(pszAction, "get")
1700 && RTStrICmp(pszAction, "delete"))
1701 return errorSyntax(USAGE_MEDIUMPROPERTY, "Invalid action given: %s", pszAction);
1702
1703 if ( ( !RTStrICmp(pszAction, "set")
1704 && a->argc != 4)
1705 || ( RTStrICmp(pszAction, "set")
1706 && a->argc != 3))
1707 return errorSyntax(USAGE_MEDIUMPROPERTY, "Invalid number of arguments given for action: %s", pszAction);
1708
1709 pszFilenameOrUuid = a->argv[1];
1710 pszProperty = a->argv[2];
1711
1712 if (cmd == CMD_DISK)
1713 rc = openMedium(a, pszFilenameOrUuid, DeviceType_HardDisk,
1714 AccessMode_ReadWrite, pMedium,
1715 false /* fForceNewUuidOnOpen */, false /* fSilent */);
1716 else if (cmd == CMD_DVD)
1717 rc = openMedium(a, pszFilenameOrUuid, DeviceType_DVD,
1718 AccessMode_ReadOnly, pMedium,
1719 false /* fForceNewUuidOnOpen */, false /* fSilent */);
1720 else if (cmd == CMD_FLOPPY)
1721 rc = openMedium(a, pszFilenameOrUuid, DeviceType_Floppy,
1722 AccessMode_ReadWrite, pMedium,
1723 false /* fForceNewUuidOnOpen */, false /* fSilent */);
1724 if (SUCCEEDED(rc) && !pMedium.isNull())
1725 {
1726 if (!RTStrICmp(pszAction, "set"))
1727 {
1728 const char *pszValue = a->argv[3];
1729 CHECK_ERROR(pMedium, SetProperty(Bstr(pszProperty).raw(), Bstr(pszValue).raw()));
1730 }
1731 else if (!RTStrICmp(pszAction, "get"))
1732 {
1733 Bstr strVal;
1734 CHECK_ERROR(pMedium, GetProperty(Bstr(pszProperty).raw(), strVal.asOutParam()));
1735 if (SUCCEEDED(rc))
1736 RTPrintf("%s=%ls\n", pszProperty, strVal.raw());
1737 }
1738 else if (!RTStrICmp(pszAction, "delete"))
1739 {
1740 CHECK_ERROR(pMedium, SetProperty(Bstr(pszProperty).raw(), Bstr().raw()));
1741 /** @todo */
1742 }
1743 }
1744
1745 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
1746}
1747
1748static const RTGETOPTDEF g_aEncryptMediumOptions[] =
1749{
1750 { "--newpassword", 'n', RTGETOPT_REQ_STRING },
1751 { "--oldpassword", 'o', RTGETOPT_REQ_STRING },
1752 { "--cipher", 'c', RTGETOPT_REQ_STRING },
1753 { "--newpasswordid", 'i', RTGETOPT_REQ_STRING }
1754};
1755
1756RTEXITCODE handleEncryptMedium(HandlerArg *a)
1757{
1758 HRESULT rc;
1759 ComPtr<IMedium> hardDisk;
1760 const char *pszPasswordNew = NULL;
1761 const char *pszPasswordOld = NULL;
1762 const char *pszCipher = NULL;
1763 const char *pszFilenameOrUuid = NULL;
1764 const char *pszNewPasswordId = NULL;
1765 Utf8Str strPasswordNew;
1766 Utf8Str strPasswordOld;
1767
1768 int c;
1769 RTGETOPTUNION ValueUnion;
1770 RTGETOPTSTATE GetState;
1771 // start at 0 because main() has hacked both the argc and argv given to us
1772 RTGetOptInit(&GetState, a->argc, a->argv, g_aEncryptMediumOptions, RT_ELEMENTS(g_aEncryptMediumOptions),
1773 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
1774 while ((c = RTGetOpt(&GetState, &ValueUnion)))
1775 {
1776 switch (c)
1777 {
1778 case 'n': // --newpassword
1779 pszPasswordNew = ValueUnion.psz;
1780 break;
1781
1782 case 'o': // --oldpassword
1783 pszPasswordOld = ValueUnion.psz;
1784 break;
1785
1786 case 'c': // --cipher
1787 pszCipher = ValueUnion.psz;
1788 break;
1789
1790 case 'i': // --newpasswordid
1791 pszNewPasswordId = ValueUnion.psz;
1792 break;
1793
1794 case VINF_GETOPT_NOT_OPTION:
1795 if (!pszFilenameOrUuid)
1796 pszFilenameOrUuid = ValueUnion.psz;
1797 else
1798 return errorSyntax(USAGE_ENCRYPTMEDIUM, "Invalid parameter '%s'", ValueUnion.psz);
1799 break;
1800
1801 default:
1802 if (c > 0)
1803 {
1804 if (RT_C_IS_PRINT(c))
1805 return errorSyntax(USAGE_ENCRYPTMEDIUM, "Invalid option -%c", c);
1806 else
1807 return errorSyntax(USAGE_ENCRYPTMEDIUM, "Invalid option case %i", c);
1808 }
1809 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
1810 return errorSyntax(USAGE_ENCRYPTMEDIUM, "unknown option: %s\n", ValueUnion.psz);
1811 else if (ValueUnion.pDef)
1812 return errorSyntax(USAGE_ENCRYPTMEDIUM, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
1813 else
1814 return errorSyntax(USAGE_ENCRYPTMEDIUM, "error: %Rrs", c);
1815 }
1816 }
1817
1818 if (!pszFilenameOrUuid)
1819 return errorSyntax(USAGE_ENCRYPTMEDIUM, "Disk name or UUID required");
1820
1821 if (!pszPasswordNew && !pszPasswordOld)
1822 return errorSyntax(USAGE_ENCRYPTMEDIUM, "No password specified");
1823
1824 if ( (pszPasswordNew && !pszNewPasswordId)
1825 || (!pszPasswordNew && pszNewPasswordId))
1826 return errorSyntax(USAGE_ENCRYPTMEDIUM, "A new password must always have a valid identifier set at the same time");
1827
1828 if (pszPasswordNew)
1829 {
1830 if (!RTStrCmp(pszPasswordNew, "-"))
1831 {
1832 /* Get password from console. */
1833 RTEXITCODE rcExit = readPasswordFromConsole(&strPasswordNew, "Enter new password:");
1834 if (rcExit == RTEXITCODE_FAILURE)
1835 return rcExit;
1836 }
1837 else
1838 {
1839 RTEXITCODE rcExit = readPasswordFile(pszPasswordNew, &strPasswordNew);
1840 if (rcExit == RTEXITCODE_FAILURE)
1841 {
1842 RTMsgError("Failed to read new password from file");
1843 return rcExit;
1844 }
1845 }
1846 }
1847
1848 if (pszPasswordOld)
1849 {
1850 if (!RTStrCmp(pszPasswordOld, "-"))
1851 {
1852 /* Get password from console. */
1853 RTEXITCODE rcExit = readPasswordFromConsole(&strPasswordOld, "Enter old password:");
1854 if (rcExit == RTEXITCODE_FAILURE)
1855 return rcExit;
1856 }
1857 else
1858 {
1859 RTEXITCODE rcExit = readPasswordFile(pszPasswordOld, &strPasswordOld);
1860 if (rcExit == RTEXITCODE_FAILURE)
1861 {
1862 RTMsgError("Failed to read old password from file");
1863 return rcExit;
1864 }
1865 }
1866 }
1867
1868 /* Always open the medium if necessary, there is no other way. */
1869 rc = openMedium(a, pszFilenameOrUuid, DeviceType_HardDisk,
1870 AccessMode_ReadWrite, hardDisk,
1871 false /* fForceNewUuidOnOpen */, false /* fSilent */);
1872 if (FAILED(rc))
1873 return RTEXITCODE_FAILURE;
1874 if (hardDisk.isNull())
1875 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Invalid hard disk reference, avoiding crash");
1876
1877 ComPtr<IProgress> progress;
1878 CHECK_ERROR(hardDisk, ChangeEncryption(Bstr(strPasswordOld).raw(), Bstr(pszCipher).raw(),
1879 Bstr(strPasswordNew).raw(), Bstr(pszNewPasswordId).raw(),
1880 progress.asOutParam()));
1881 if (SUCCEEDED(rc))
1882 rc = showProgress(progress);
1883 if (FAILED(rc))
1884 {
1885 if (rc == E_NOTIMPL)
1886 RTMsgError("Encrypt hard disk operation is not implemented!");
1887 else if (rc == VBOX_E_NOT_SUPPORTED)
1888 RTMsgError("Encrypt hard disk operation for this cipher is not implemented yet!");
1889 else if (!progress.isNull())
1890 CHECK_PROGRESS_ERROR(progress, ("Failed to encrypt hard disk"));
1891 else
1892 RTMsgError("Failed to encrypt hard disk!");
1893 }
1894
1895 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
1896}
1897
1898RTEXITCODE handleCheckMediumPassword(HandlerArg *a)
1899{
1900 HRESULT rc;
1901 ComPtr<IMedium> hardDisk;
1902 const char *pszFilenameOrUuid = NULL;
1903 Utf8Str strPassword;
1904
1905 if (a->argc != 2)
1906 return errorSyntax(USAGE_MEDIUMENCCHKPWD, "Invalid number of arguments: %d", a->argc);
1907
1908 pszFilenameOrUuid = a->argv[0];
1909
1910 if (!RTStrCmp(a->argv[1], "-"))
1911 {
1912 /* Get password from console. */
1913 RTEXITCODE rcExit = readPasswordFromConsole(&strPassword, "Enter password:");
1914 if (rcExit == RTEXITCODE_FAILURE)
1915 return rcExit;
1916 }
1917 else
1918 {
1919 RTEXITCODE rcExit = readPasswordFile(a->argv[1], &strPassword);
1920 if (rcExit == RTEXITCODE_FAILURE)
1921 {
1922 RTMsgError("Failed to read password from file");
1923 return rcExit;
1924 }
1925 }
1926
1927 /* Always open the medium if necessary, there is no other way. */
1928 rc = openMedium(a, pszFilenameOrUuid, DeviceType_HardDisk,
1929 AccessMode_ReadWrite, hardDisk,
1930 false /* fForceNewUuidOnOpen */, false /* fSilent */);
1931 if (FAILED(rc))
1932 return RTEXITCODE_FAILURE;
1933 if (hardDisk.isNull())
1934 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Invalid hard disk reference, avoiding crash");
1935
1936 CHECK_ERROR(hardDisk, CheckEncryptionPassword(Bstr(strPassword).raw()));
1937 if (SUCCEEDED(rc))
1938 RTPrintf("The given password is correct\n");
1939 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
1940}
1941
1942#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