VirtualBox

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

Last change on this file since 54791 was 54725, checked in by vboxsync, 10 years ago

FE/VBoxManage: Change the way passwords are provided when encrypting a medium. Instead of giving it directly on the command line read it from standard input or from a file

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