VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxManage/VBoxManageStorageController.cpp@ 44868

Last change on this file since 44868 was 44132, checked in by vboxsync, 12 years ago

VBoxManage storageattach: check if the Additions image is really known

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 45.9 KB
Line 
1/* $Id: VBoxManageStorageController.cpp 44132 2012-12-14 10:43:16Z vboxsync $ */
2/** @file
3 * VBoxManage - The storage controller related commands.
4 */
5
6/*
7 * Copyright (C) 2006-2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifndef VBOX_ONLY_DOCS
19
20/*******************************************************************************
21* Header Files *
22*******************************************************************************/
23#include <VBox/com/com.h>
24#include <VBox/com/array.h>
25#include <VBox/com/ErrorInfo.h>
26#include <VBox/com/errorprint.h>
27#include <VBox/com/VirtualBox.h>
28
29#include <iprt/path.h>
30#include <iprt/param.h>
31#include <iprt/string.h>
32#include <iprt/ctype.h>
33#include <iprt/stream.h>
34#include <iprt/getopt.h>
35#include <VBox/log.h>
36
37#include "VBoxManage.h"
38using namespace com;
39
40
41// funcs
42///////////////////////////////////////////////////////////////////////////////
43
44
45static const RTGETOPTDEF g_aStorageAttachOptions[] =
46{
47 { "--storagectl", 's', RTGETOPT_REQ_STRING },
48 { "--port", 'p', RTGETOPT_REQ_UINT32 },
49 { "--device", 'd', RTGETOPT_REQ_UINT32 },
50 { "--type", 't', RTGETOPT_REQ_STRING },
51 { "--medium", 'm', RTGETOPT_REQ_STRING },
52 { "--mtype", 'M', RTGETOPT_REQ_STRING },
53 { "--passthrough", 'h', RTGETOPT_REQ_STRING },
54 { "--tempeject", 'e', RTGETOPT_REQ_STRING },
55 { "--nonrotational", 'n', RTGETOPT_REQ_STRING },
56 { "--discard", 'u', RTGETOPT_REQ_STRING },
57 { "--bandwidthgroup", 'b', RTGETOPT_REQ_STRING },
58 { "--forceunmount", 'f', RTGETOPT_REQ_NOTHING },
59 { "--comment", 'C', RTGETOPT_REQ_STRING },
60 { "--setuuid", 'q', RTGETOPT_REQ_STRING },
61 { "--setparentuuid", 'Q', RTGETOPT_REQ_STRING },
62 // iSCSI options
63 { "--server", 'S', RTGETOPT_REQ_STRING },
64 { "--target", 'T', RTGETOPT_REQ_STRING },
65 { "--tport", 'P', RTGETOPT_REQ_STRING },
66 { "--lun", 'L', RTGETOPT_REQ_STRING },
67 { "--encodedlun", 'E', RTGETOPT_REQ_STRING },
68 { "--username", 'U', RTGETOPT_REQ_STRING },
69 { "--password", 'W', RTGETOPT_REQ_STRING },
70 { "--initiator", 'N', RTGETOPT_REQ_STRING },
71 { "--intnet", 'I', RTGETOPT_REQ_NOTHING },
72};
73
74int handleStorageAttach(HandlerArg *a)
75{
76 int c = VERR_INTERNAL_ERROR; /* initialized to shut up gcc */
77 HRESULT rc = S_OK;
78 ULONG port = ~0U;
79 ULONG device = ~0U;
80 bool fForceUnmount = false;
81 bool fSetMediumType = false;
82 bool fSetNewUuid = false;
83 bool fSetNewParentUuid = false;
84 MediumType_T mediumType = MediumType_Normal;
85 Bstr bstrComment;
86 const char *pszCtl = NULL;
87 DeviceType_T devTypeRequested = DeviceType_Null;
88 const char *pszMedium = NULL;
89 const char *pszPassThrough = NULL;
90 const char *pszTempEject = NULL;
91 const char *pszNonRotational = NULL;
92 const char *pszDiscard = NULL;
93 const char *pszBandwidthGroup = NULL;
94 Bstr bstrNewUuid;
95 Bstr bstrNewParentUuid;
96 // iSCSI options
97 Bstr bstrServer;
98 Bstr bstrTarget;
99 Bstr bstrPort;
100 Bstr bstrLun;
101 Bstr bstrUsername;
102 Bstr bstrPassword;
103 Bstr bstrInitiator;
104 Bstr bstrIso;
105 Utf8Str strIso;
106 bool fIntNet = false;
107
108 RTGETOPTUNION ValueUnion;
109 RTGETOPTSTATE GetState;
110 ComPtr<IMachine> machine;
111 ComPtr<IStorageController> storageCtl;
112 ComPtr<ISystemProperties> systemProperties;
113
114 RTGetOptInit(&GetState, a->argc, a->argv, g_aStorageAttachOptions,
115 RT_ELEMENTS(g_aStorageAttachOptions), 1, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
116
117 while ( SUCCEEDED(rc)
118 && (c = RTGetOpt(&GetState, &ValueUnion)))
119 {
120 switch (c)
121 {
122 case 's': // storage controller name
123 {
124 if (ValueUnion.psz)
125 pszCtl = ValueUnion.psz;
126 else
127 rc = E_FAIL;
128 break;
129 }
130
131 case 'p': // port
132 {
133 port = ValueUnion.u32;
134 break;
135 }
136
137 case 'd': // device
138 {
139 device = ValueUnion.u32;
140 break;
141 }
142
143 case 'm': // medium <none|emptydrive|additions|uuid|filename|host:<drive>|iSCSI>
144 {
145 if (ValueUnion.psz)
146 pszMedium = ValueUnion.psz;
147 else
148 rc = E_FAIL;
149 break;
150 }
151
152 case 't': // type <dvddrive|hdd|fdd>
153 {
154 if (ValueUnion.psz)
155 {
156 if (!RTStrICmp(ValueUnion.psz, "hdd"))
157 devTypeRequested = DeviceType_HardDisk;
158 else if (!RTStrICmp(ValueUnion.psz, "fdd"))
159 devTypeRequested = DeviceType_Floppy;
160 else if (!RTStrICmp(ValueUnion.psz, "dvddrive"))
161 devTypeRequested = DeviceType_DVD;
162 else
163 return errorArgument("Invalid --type argument '%s'", ValueUnion.psz);
164 }
165 else
166 rc = E_FAIL;
167 break;
168 }
169
170 case 'h': // passthrough <on|off>
171 {
172 if (ValueUnion.psz)
173 pszPassThrough = ValueUnion.psz;
174 else
175 rc = E_FAIL;
176 break;
177 }
178
179 case 'e': // tempeject <on|off>
180 {
181 if (ValueUnion.psz)
182 pszTempEject = ValueUnion.psz;
183 else
184 rc = E_FAIL;
185 break;
186 }
187
188 case 'n': // nonrotational <on|off>
189 {
190 if (ValueUnion.psz)
191 pszNonRotational = ValueUnion.psz;
192 else
193 rc = E_FAIL;
194 break;
195 }
196
197 case 'u': // nonrotational <on|off>
198 {
199 if (ValueUnion.psz)
200 pszDiscard = ValueUnion.psz;
201 else
202 rc = E_FAIL;
203 break;
204 }
205
206 case 'b': // bandwidthgroup <name>
207 {
208 if (ValueUnion.psz)
209 pszBandwidthGroup = ValueUnion.psz;
210 else
211 rc = E_FAIL;
212 break;
213 }
214
215 case 'f': // force unmount medium during runtime
216 {
217 fForceUnmount = true;
218 break;
219 }
220
221 case 'C':
222 if (ValueUnion.psz)
223 bstrComment = ValueUnion.psz;
224 else
225 rc = E_FAIL;
226 break;
227
228 case 'q':
229 if (ValueUnion.psz)
230 {
231 bstrNewUuid = ValueUnion.psz;
232 fSetNewUuid = true;
233 }
234 else
235 rc = E_FAIL;
236 break;
237
238 case 'Q':
239 if (ValueUnion.psz)
240 {
241 bstrNewParentUuid = ValueUnion.psz;
242 fSetNewParentUuid = true;
243 }
244 else
245 rc = E_FAIL;
246 break;
247
248 case 'S': // --server
249 bstrServer = ValueUnion.psz;
250 break;
251
252 case 'T': // --target
253 bstrTarget = ValueUnion.psz;
254 break;
255
256 case 'P': // --tport
257 bstrPort = ValueUnion.psz;
258 break;
259
260 case 'L': // --lun
261 bstrLun = ValueUnion.psz;
262 break;
263
264 case 'E': // --encodedlun
265 bstrLun = BstrFmt("enc%s", ValueUnion.psz);
266 break;
267
268 case 'U': // --username
269 bstrUsername = ValueUnion.psz;
270 break;
271
272 case 'W': // --password
273 bstrPassword = ValueUnion.psz;
274 break;
275
276 case 'N': // --initiator
277 bstrInitiator = ValueUnion.psz;
278 break;
279
280 case 'M': // --type
281 {
282 int vrc = parseDiskType(ValueUnion.psz, &mediumType);
283 if (RT_FAILURE(vrc))
284 return errorArgument("Invalid hard disk type '%s'", ValueUnion.psz);
285 fSetMediumType = true;
286 break;
287 }
288
289 case 'I': // --intnet
290 fIntNet = true;
291 break;
292
293 default:
294 {
295 errorGetOpt(USAGE_STORAGEATTACH, c, &ValueUnion);
296 rc = E_FAIL;
297 break;
298 }
299 }
300 }
301
302 if (FAILED(rc))
303 return 1;
304
305 if (!pszCtl)
306 return errorSyntax(USAGE_STORAGEATTACH, "Storage controller name not specified");
307
308 /* get the virtualbox system properties */
309 CHECK_ERROR_RET(a->virtualBox, COMGETTER(SystemProperties)(systemProperties.asOutParam()), 1);
310
311 // find the machine, lock it, get the mutable session machine
312 CHECK_ERROR_RET(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
313 machine.asOutParam()), 1);
314 CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Shared), 1);
315 SessionType_T st;
316 CHECK_ERROR_RET(a->session, COMGETTER(Type)(&st), 1);
317 a->session->COMGETTER(Machine)(machine.asOutParam());
318
319 try
320 {
321 bool fRunTime = (st == SessionType_Shared);
322
323 if (fRunTime)
324 {
325 if (pszPassThrough)
326 throw Utf8Str("Drive passthrough state cannot be changed while the VM is running\n");
327 else if (pszBandwidthGroup)
328 throw Utf8Str("Bandwidth group cannot be changed while the VM is running\n");
329 }
330
331 /* check if the storage controller is present */
332 rc = machine->GetStorageControllerByName(Bstr(pszCtl).raw(),
333 storageCtl.asOutParam());
334 if (FAILED(rc))
335 throw Utf8StrFmt("Could not find a controller named '%s'\n", pszCtl);
336
337 StorageBus_T storageBus = StorageBus_Null;
338 CHECK_ERROR_RET(storageCtl, COMGETTER(Bus)(&storageBus), 1);
339 ULONG maxPorts = 0;
340 CHECK_ERROR_RET(systemProperties, GetMaxPortCountForStorageBus(storageBus, &maxPorts), 1);
341 ULONG maxDevices = 0;
342 CHECK_ERROR_RET(systemProperties, GetMaxDevicesPerPortForStorageBus(storageBus, &maxDevices), 1);
343
344 if (port == ~0U)
345 {
346 if (maxPorts == 1)
347 port = 0;
348 else
349 return errorSyntax(USAGE_STORAGEATTACH, "Port not specified");
350 }
351 if (device == ~0U)
352 {
353 if (maxDevices == 1)
354 device = 0;
355 else
356 return errorSyntax(USAGE_STORAGEATTACH, "Device not specified");
357 }
358
359 /* for sata controller check if the port count is big enough
360 * to accommodate the current port which is being assigned
361 * else just increase the port count
362 */
363 {
364 ULONG ulPortCount = 0;
365 ULONG ulMaxPortCount = 0;
366
367 CHECK_ERROR(storageCtl, COMGETTER(MaxPortCount)(&ulMaxPortCount));
368 CHECK_ERROR(storageCtl, COMGETTER(PortCount)(&ulPortCount));
369
370 if ( (ulPortCount != ulMaxPortCount)
371 && (port >= ulPortCount)
372 && (port < ulMaxPortCount))
373 CHECK_ERROR(storageCtl, COMSETTER(PortCount)(port + 1));
374 }
375
376 StorageControllerType_T ctlType = StorageControllerType_Null;
377 CHECK_ERROR(storageCtl, COMGETTER(ControllerType)(&ctlType));
378
379 if (!RTStrICmp(pszMedium, "none"))
380 {
381 CHECK_ERROR(machine, DetachDevice(Bstr(pszCtl).raw(), port, device));
382 }
383 else if (!RTStrICmp(pszMedium, "emptydrive"))
384 {
385 if (fRunTime)
386 {
387 ComPtr<IMediumAttachment> mediumAttachment;
388 DeviceType_T deviceType = DeviceType_Null;
389 rc = machine->GetMediumAttachment(Bstr(pszCtl).raw(), port, device,
390 mediumAttachment.asOutParam());
391 if (SUCCEEDED(rc))
392 {
393 mediumAttachment->COMGETTER(Type)(&deviceType);
394
395 if ( (deviceType == DeviceType_DVD)
396 || (deviceType == DeviceType_Floppy))
397 {
398 /* just unmount the floppy/dvd */
399 CHECK_ERROR(machine, UnmountMedium(Bstr(pszCtl).raw(),
400 port,
401 device,
402 fForceUnmount));
403 }
404 }
405 else if (devTypeRequested == DeviceType_DVD)
406 {
407 /*
408 * Try to attach an empty DVD drive as a hotplug operation.
409 * Main will complain if the controller doesn't support hotplugging.
410 */
411 CHECK_ERROR(machine, AttachDeviceWithoutMedium(Bstr(pszCtl).raw(), port, device,
412 devTypeRequested));
413 deviceType = DeviceType_DVD; /* To avoid the error message below. */
414 }
415
416 if ( FAILED(rc)
417 || !( deviceType == DeviceType_DVD
418 || deviceType == DeviceType_Floppy)
419 )
420 throw Utf8StrFmt("No DVD/Floppy Drive attached to the controller '%s'"
421 "at the port: %u, device: %u", pszCtl, port, device);
422
423 }
424 else
425 {
426 DeviceType_T deviceType = DeviceType_Null;
427 com::SafeArray <DeviceType_T> saDeviceTypes;
428 ULONG driveCheck = 0;
429
430 /* check if the device type is supported by the controller */
431 CHECK_ERROR(systemProperties, GetDeviceTypesForStorageBus(storageBus, ComSafeArrayAsOutParam(saDeviceTypes)));
432 for (size_t i = 0; i < saDeviceTypes.size(); ++ i)
433 {
434 if ( (saDeviceTypes[i] == DeviceType_DVD)
435 || (saDeviceTypes[i] == DeviceType_Floppy))
436 driveCheck++;
437 }
438
439 if (!driveCheck)
440 throw Utf8StrFmt("The attachment is not supported by the storage controller '%s'", pszCtl);
441
442 if (storageBus == StorageBus_Floppy)
443 deviceType = DeviceType_Floppy;
444 else
445 deviceType = DeviceType_DVD;
446
447 /* attach a empty floppy/dvd drive after removing previous attachment */
448 machine->DetachDevice(Bstr(pszCtl).raw(), port, device);
449 CHECK_ERROR(machine, AttachDeviceWithoutMedium(Bstr(pszCtl).raw(), port, device,
450 deviceType));
451 }
452 } // end if (!RTStrICmp(pszMedium, "emptydrive"))
453 else
454 {
455 ComPtr<IMedium> pMedium2Mount;
456
457 // not "none", not "emptydrive": then it must be a UUID or filename or hostdrive or iSCSI;
458 // for all these we first need to know the type of drive we're attaching to
459 {
460 /*
461 * try to determine the type of the drive from the
462 * storage controller chipset, the attachment and
463 * the medium being attached
464 */
465 if (ctlType == StorageControllerType_I82078) // floppy controller
466 devTypeRequested = DeviceType_Floppy;
467 else
468 {
469 /*
470 * for SATA/SCSI/IDE it is hard to tell if it is a harddisk or
471 * a dvd being attached so lets check if the medium attachment
472 * and the medium, both are of same type. if yes then we are
473 * sure of its type and don't need the user to enter it manually
474 * else ask the user for the type.
475 */
476 ComPtr<IMediumAttachment> mediumAttachment;
477 rc = machine->GetMediumAttachment(Bstr(pszCtl).raw(), port,
478 device,
479 mediumAttachment.asOutParam());
480 if (SUCCEEDED(rc))
481 {
482 DeviceType_T deviceType;
483 mediumAttachment->COMGETTER(Type)(&deviceType);
484
485 if (pszMedium)
486 {
487 if (!RTStrICmp(pszMedium, "additions"))
488 {
489 ComPtr<ISystemProperties> pProperties;
490 CHECK_ERROR(a->virtualBox,
491 COMGETTER(SystemProperties)(pProperties.asOutParam()));
492 CHECK_ERROR(pProperties, COMGETTER(DefaultAdditionsISO)(bstrIso.asOutParam()));
493 strIso = Utf8Str(bstrIso);
494 if (strIso.isEmpty())
495 throw Utf8Str("Cannot find the Guest Additions ISO image\n");
496 pszMedium = strIso.c_str();
497 if (devTypeRequested == DeviceType_Null)
498 devTypeRequested = DeviceType_DVD;
499 }
500 ComPtr<IMedium> pExistingMedium;
501 rc = openMedium(a, pszMedium, deviceType,
502 AccessMode_ReadWrite,
503 pExistingMedium,
504 false /* fForceNewUuidOnOpen */,
505 true /* fSilent */);
506 if (SUCCEEDED(rc) && pExistingMedium)
507 {
508 if ( (deviceType == DeviceType_DVD)
509 || (deviceType == DeviceType_HardDisk)
510 )
511 devTypeRequested = deviceType;
512 }
513 }
514 else
515 devTypeRequested = deviceType;
516 }
517 }
518 }
519
520 if (devTypeRequested == DeviceType_Null) // still the initializer value?
521 throw Utf8Str("Argument --type must be specified\n");
522
523 /* check if the device type is supported by the controller */
524 {
525 com::SafeArray <DeviceType_T> saDeviceTypes;
526
527 CHECK_ERROR(systemProperties, GetDeviceTypesForStorageBus(storageBus, ComSafeArrayAsOutParam(saDeviceTypes)));
528 if (SUCCEEDED(rc))
529 {
530 ULONG driveCheck = 0;
531 for (size_t i = 0; i < saDeviceTypes.size(); ++ i)
532 if (saDeviceTypes[i] == devTypeRequested)
533 driveCheck++;
534 if (!driveCheck)
535 throw Utf8StrFmt("The given attachment is not supported by the storage controller '%s'", pszCtl);
536 }
537 else
538 goto leave;
539 }
540
541 // find the medium given
542 /* host drive? */
543 if (!RTStrNICmp(pszMedium, "host:", 5))
544 {
545 ComPtr<IHost> host;
546 CHECK_ERROR(a->virtualBox, COMGETTER(Host)(host.asOutParam()));
547
548 if (devTypeRequested == DeviceType_DVD)
549 {
550 rc = host->FindHostDVDDrive(Bstr(pszMedium + 5).raw(),
551 pMedium2Mount.asOutParam());
552 if (!pMedium2Mount)
553 {
554 /* 2nd try: try with the real name, important on Linux+libhal */
555 char szPathReal[RTPATH_MAX];
556 if (RT_FAILURE(RTPathReal(pszMedium + 5, szPathReal, sizeof(szPathReal))))
557 throw Utf8StrFmt("Invalid host DVD drive name \"%s\"", pszMedium + 5);
558 rc = host->FindHostDVDDrive(Bstr(szPathReal).raw(),
559 pMedium2Mount.asOutParam());
560 if (!pMedium2Mount)
561 throw Utf8StrFmt("Invalid host DVD drive name \"%s\"", pszMedium + 5);
562 }
563 }
564 else
565 {
566 // floppy
567 rc = host->FindHostFloppyDrive(Bstr(pszMedium + 5).raw(),
568 pMedium2Mount.asOutParam());
569 if (!pMedium2Mount)
570 throw Utf8StrFmt("Invalid host floppy drive name \"%s\"", pszMedium + 5);
571 }
572 }
573 else if (!RTStrICmp(pszMedium, "iSCSI"))
574 {
575 /* check for required options */
576 if (bstrServer.isEmpty() || bstrTarget.isEmpty())
577 throw Utf8StrFmt("Parameters --server and --target are required for iSCSI media");
578
579 /** @todo move the location stuff to Main, which can use pfnComposeName
580 * from the disk backends to construct the location properly. Also do
581 * not use slashes to separate the parts, as otherwise only the last
582 * element containing information will be shown. */
583 Bstr bstrISCSIMedium;
584 if ( bstrLun.isEmpty()
585 || (bstrLun == "0")
586 || (bstrLun == "enc0")
587 )
588 bstrISCSIMedium = BstrFmt("%ls|%ls", bstrServer.raw(), bstrTarget.raw());
589 else
590 bstrISCSIMedium = BstrFmt("%ls|%ls|%ls", bstrServer.raw(), bstrTarget.raw(), bstrLun.raw());
591
592 CHECK_ERROR(a->virtualBox, CreateHardDisk(Bstr("iSCSI").raw(),
593 bstrISCSIMedium.raw(),
594 pMedium2Mount.asOutParam()));
595 if (FAILED(rc)) goto leave;
596 if (!bstrPort.isEmpty())
597 bstrServer = BstrFmt("%ls:%ls", bstrServer.raw(), bstrPort.raw());
598
599 // set the other iSCSI parameters as properties
600 com::SafeArray <BSTR> names;
601 com::SafeArray <BSTR> values;
602 Bstr("TargetAddress").detachTo(names.appendedRaw());
603 bstrServer.detachTo(values.appendedRaw());
604 Bstr("TargetName").detachTo(names.appendedRaw());
605 bstrTarget.detachTo(values.appendedRaw());
606
607 if (!bstrLun.isEmpty())
608 {
609 Bstr("LUN").detachTo(names.appendedRaw());
610 bstrLun.detachTo(values.appendedRaw());
611 }
612 if (!bstrUsername.isEmpty())
613 {
614 Bstr("InitiatorUsername").detachTo(names.appendedRaw());
615 bstrUsername.detachTo(values.appendedRaw());
616 }
617 if (!bstrPassword.isEmpty())
618 {
619 Bstr("InitiatorSecret").detachTo(names.appendedRaw());
620 bstrPassword.detachTo(values.appendedRaw());
621 }
622 if (!bstrInitiator.isEmpty())
623 {
624 Bstr("InitiatorName").detachTo(names.appendedRaw());
625 bstrInitiator.detachTo(values.appendedRaw());
626 }
627
628 /// @todo add --targetName and --targetPassword options
629
630 if (fIntNet)
631 {
632 Bstr("HostIPStack").detachTo(names.appendedRaw());
633 Bstr("0").detachTo(values.appendedRaw());
634 }
635
636 CHECK_ERROR(pMedium2Mount, SetProperties(ComSafeArrayAsInParam(names),
637 ComSafeArrayAsInParam(values)));
638 if (FAILED(rc)) goto leave;
639 Bstr guid;
640 CHECK_ERROR(pMedium2Mount, COMGETTER(Id)(guid.asOutParam()));
641 if (FAILED(rc)) goto leave;
642 RTPrintf("iSCSI disk created. UUID: %s\n", Utf8Str(guid).c_str());
643 }
644 else
645 {
646 if (!pszMedium)
647 {
648 ComPtr<IMediumAttachment> mediumAttachment;
649 rc = machine->GetMediumAttachment(Bstr(pszCtl).raw(), port,
650 device,
651 mediumAttachment.asOutParam());
652 if (FAILED(rc))
653 throw Utf8Str("Missing --medium argument");
654 }
655 else
656 {
657 Bstr bstrMedium(pszMedium);
658 rc = openMedium(a, pszMedium, devTypeRequested,
659 AccessMode_ReadWrite, pMedium2Mount,
660 fSetNewUuid, false /* fSilent */);
661 if (FAILED(rc) || !pMedium2Mount)
662 throw Utf8StrFmt("Invalid UUID or filename \"%s\"", pszMedium);
663 }
664 }
665
666 // set medium/parent medium UUID, if so desired
667 if (pMedium2Mount && (fSetNewUuid || fSetNewParentUuid))
668 {
669 CHECK_ERROR(pMedium2Mount, SetIds(fSetNewUuid, bstrNewUuid.raw(),
670 fSetNewParentUuid, bstrNewParentUuid.raw()));
671 if (FAILED(rc))
672 throw Utf8Str("Failed to set the medium/parent medium UUID");
673 }
674
675 // set medium type, if so desired
676 if (pMedium2Mount && fSetMediumType)
677 {
678 CHECK_ERROR(pMedium2Mount, COMSETTER(Type)(mediumType));
679 if (FAILED(rc))
680 throw Utf8Str("Failed to set the medium type");
681 }
682
683 if (pMedium2Mount && !bstrComment.isEmpty())
684 {
685 CHECK_ERROR(pMedium2Mount, COMSETTER(Description)(bstrComment.raw()));
686 }
687
688 if (pszMedium)
689 {
690 switch (devTypeRequested)
691 {
692 case DeviceType_DVD:
693 case DeviceType_Floppy:
694 {
695 if (!fRunTime)
696 {
697 ComPtr<IMediumAttachment> mediumAttachment;
698 // check if there is a dvd/floppy drive at the given location, if not attach one first
699 rc = machine->GetMediumAttachment(Bstr(pszCtl).raw(),
700 port,
701 device,
702 mediumAttachment.asOutParam());
703 if (SUCCEEDED(rc))
704 {
705 DeviceType_T deviceType;
706 mediumAttachment->COMGETTER(Type)(&deviceType);
707 if (deviceType != devTypeRequested)
708 {
709 machine->DetachDevice(Bstr(pszCtl).raw(), port, device);
710 rc = machine->AttachDeviceWithoutMedium(Bstr(pszCtl).raw(),
711 port,
712 device,
713 devTypeRequested); // DeviceType_DVD or DeviceType_Floppy
714 }
715 }
716 else
717 {
718 rc = machine->AttachDeviceWithoutMedium(Bstr(pszCtl).raw(),
719 port,
720 device,
721 devTypeRequested); // DeviceType_DVD or DeviceType_Floppy
722 }
723 }
724
725 if (pMedium2Mount)
726 {
727 CHECK_ERROR(machine, MountMedium(Bstr(pszCtl).raw(),
728 port,
729 device,
730 pMedium2Mount,
731 fForceUnmount));
732 }
733 } // end DeviceType_DVD or DeviceType_Floppy:
734 break;
735
736 case DeviceType_HardDisk:
737 {
738 // if there is anything attached at the given location, remove it
739 machine->DetachDevice(Bstr(pszCtl).raw(), port, device);
740 CHECK_ERROR(machine, AttachDevice(Bstr(pszCtl).raw(),
741 port,
742 device,
743 DeviceType_HardDisk,
744 pMedium2Mount));
745 }
746 break;
747 }
748 }
749 }
750
751 if ( pszPassThrough
752 && (SUCCEEDED(rc)))
753 {
754 ComPtr<IMediumAttachment> mattach;
755 CHECK_ERROR(machine, GetMediumAttachment(Bstr(pszCtl).raw(), port,
756 device, mattach.asOutParam()));
757
758 if (SUCCEEDED(rc))
759 {
760 if (!RTStrICmp(pszPassThrough, "on"))
761 {
762 CHECK_ERROR(machine, PassthroughDevice(Bstr(pszCtl).raw(),
763 port, device, TRUE));
764 }
765 else if (!RTStrICmp(pszPassThrough, "off"))
766 {
767 CHECK_ERROR(machine, PassthroughDevice(Bstr(pszCtl).raw(),
768 port, device, FALSE));
769 }
770 else
771 throw Utf8StrFmt("Invalid --passthrough argument '%s'", pszPassThrough);
772 }
773 else
774 throw Utf8StrFmt("Couldn't find the controller attachment for the controller '%s'\n", pszCtl);
775 }
776
777 if ( pszTempEject
778 && (SUCCEEDED(rc)))
779 {
780 ComPtr<IMediumAttachment> mattach;
781 CHECK_ERROR(machine, GetMediumAttachment(Bstr(pszCtl).raw(), port,
782 device, mattach.asOutParam()));
783
784 if (SUCCEEDED(rc))
785 {
786 if (!RTStrICmp(pszTempEject, "on"))
787 {
788 CHECK_ERROR(machine, TemporaryEjectDevice(Bstr(pszCtl).raw(),
789 port, device, TRUE));
790 }
791 else if (!RTStrICmp(pszTempEject, "off"))
792 {
793 CHECK_ERROR(machine, TemporaryEjectDevice(Bstr(pszCtl).raw(),
794 port, device, FALSE));
795 }
796 else
797 throw Utf8StrFmt("Invalid --tempeject argument '%s'", pszTempEject);
798 }
799 else
800 throw Utf8StrFmt("Couldn't find the controller attachment for the controller '%s'\n", pszCtl);
801 }
802
803 if ( pszNonRotational
804 && (SUCCEEDED(rc)))
805 {
806 ComPtr<IMediumAttachment> mattach;
807 CHECK_ERROR(machine, GetMediumAttachment(Bstr(pszCtl).raw(), port,
808 device, mattach.asOutParam()));
809
810 if (SUCCEEDED(rc))
811 {
812 if (!RTStrICmp(pszNonRotational, "on"))
813 {
814 CHECK_ERROR(machine, NonRotationalDevice(Bstr(pszCtl).raw(),
815 port, device, TRUE));
816 }
817 else if (!RTStrICmp(pszNonRotational, "off"))
818 {
819 CHECK_ERROR(machine, NonRotationalDevice(Bstr(pszCtl).raw(),
820 port, device, FALSE));
821 }
822 else
823 throw Utf8StrFmt("Invalid --nonrotational argument '%s'", pszNonRotational);
824 }
825 else
826 throw Utf8StrFmt("Couldn't find the controller attachment for the controller '%s'\n", pszCtl);
827 }
828
829 if ( pszDiscard
830 && (SUCCEEDED(rc)))
831 {
832 ComPtr<IMediumAttachment> mattach;
833 CHECK_ERROR(machine, GetMediumAttachment(Bstr(pszCtl).raw(), port,
834 device, mattach.asOutParam()));
835
836 if (SUCCEEDED(rc))
837 {
838 if (!RTStrICmp(pszDiscard, "on"))
839 {
840 CHECK_ERROR(machine, SetAutoDiscardForDevice(Bstr(pszCtl).raw(),
841 port, device, TRUE));
842 }
843 else if (!RTStrICmp(pszDiscard, "off"))
844 {
845 CHECK_ERROR(machine, SetAutoDiscardForDevice(Bstr(pszCtl).raw(),
846 port, device, FALSE));
847 }
848 else
849 throw Utf8StrFmt("Invalid --discard argument '%s'", pszDiscard);
850 }
851 else
852 throw Utf8StrFmt("Couldn't find the controller attachment for the controller '%s'\n", pszCtl);
853 }
854
855
856 if ( pszBandwidthGroup
857 && !fRunTime
858 && SUCCEEDED(rc))
859 {
860
861 if (!RTStrICmp(pszBandwidthGroup, "none"))
862 {
863 /* Just remove the bandwidth gorup. */
864 CHECK_ERROR(machine, SetNoBandwidthGroupForDevice(Bstr(pszCtl).raw(),
865 port, device));
866 }
867 else
868 {
869 ComPtr<IBandwidthControl> bwCtrl;
870 ComPtr<IBandwidthGroup> bwGroup;
871
872 CHECK_ERROR(machine, COMGETTER(BandwidthControl)(bwCtrl.asOutParam()));
873
874 if (SUCCEEDED(rc))
875 {
876 CHECK_ERROR(bwCtrl, GetBandwidthGroup(Bstr(pszBandwidthGroup).raw(), bwGroup.asOutParam()));
877 if (SUCCEEDED(rc))
878 {
879 CHECK_ERROR(machine, SetBandwidthGroupForDevice(Bstr(pszCtl).raw(),
880 port, device, bwGroup));
881 }
882 }
883 }
884 }
885
886 /* commit changes */
887 if (SUCCEEDED(rc))
888 CHECK_ERROR(machine, SaveSettings());
889 }
890 catch (const Utf8Str &strError)
891 {
892 errorArgument("%s", strError.c_str());
893 rc = E_FAIL;
894 }
895
896 // machine must always be unlocked, even on errors
897leave:
898 a->session->UnlockMachine();
899
900 return SUCCEEDED(rc) ? 0 : 1;
901}
902
903
904static const RTGETOPTDEF g_aStorageControllerOptions[] =
905{
906 { "--name", 'n', RTGETOPT_REQ_STRING },
907 { "--add", 'a', RTGETOPT_REQ_STRING },
908 { "--controller", 'c', RTGETOPT_REQ_STRING },
909 { "--sataportcount", 'p', RTGETOPT_REQ_UINT32 },
910 { "--remove", 'r', RTGETOPT_REQ_NOTHING },
911 { "--hostiocache", 'i', RTGETOPT_REQ_STRING },
912 { "--bootable", 'b', RTGETOPT_REQ_STRING },
913};
914
915int handleStorageController(HandlerArg *a)
916{
917 int c;
918 HRESULT rc = S_OK;
919 const char *pszCtl = NULL;
920 const char *pszBusType = NULL;
921 const char *pszCtlType = NULL;
922 const char *pszHostIOCache = NULL;
923 const char *pszBootable = NULL;
924 ULONG satabootdev = ~0U;
925 ULONG sataidedev = ~0U;
926 ULONG sataportcount = ~0U;
927 bool fRemoveCtl = false;
928 ComPtr<IMachine> machine;
929 RTGETOPTUNION ValueUnion;
930 RTGETOPTSTATE GetState;
931
932 if (a->argc < 4)
933 return errorSyntax(USAGE_STORAGECONTROLLER, "Too few parameters");
934
935 RTGetOptInit (&GetState, a->argc, a->argv, g_aStorageControllerOptions,
936 RT_ELEMENTS(g_aStorageControllerOptions), 1, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
937
938 while ( SUCCEEDED(rc)
939 && (c = RTGetOpt(&GetState, &ValueUnion)))
940 {
941 switch (c)
942 {
943 case 'n': // controller name
944 {
945 if (ValueUnion.psz)
946 pszCtl = ValueUnion.psz;
947 else
948 rc = E_FAIL;
949 break;
950 }
951
952 case 'a': // controller bus type <ide/sata/scsi/floppy>
953 {
954 if (ValueUnion.psz)
955 pszBusType = ValueUnion.psz;
956 else
957 rc = E_FAIL;
958 break;
959 }
960
961 case 'c': // controller <lsilogic/buslogic/intelahci/piix3/piix4/ich6/i82078>
962 {
963 if (ValueUnion.psz)
964 pszCtlType = ValueUnion.psz;
965 else
966 rc = E_FAIL;
967 break;
968 }
969
970 case 'p': // sataportcount
971 {
972 sataportcount = ValueUnion.u32;
973 break;
974 }
975
976 case 'r': // remove controller
977 {
978 fRemoveCtl = true;
979 break;
980 }
981
982 case 'i':
983 {
984 pszHostIOCache = ValueUnion.psz;
985 break;
986 }
987
988 case 'b':
989 {
990 pszBootable = ValueUnion.psz;
991 break;
992 }
993
994 default:
995 {
996 errorGetOpt(USAGE_STORAGECONTROLLER, c, &ValueUnion);
997 rc = E_FAIL;
998 break;
999 }
1000 }
1001 }
1002
1003 if (FAILED(rc))
1004 return 1;
1005
1006 /* try to find the given machine */
1007 CHECK_ERROR_RET(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
1008 machine.asOutParam()), 1);
1009
1010 /* open a session for the VM */
1011 CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Write), 1);
1012
1013 /* get the mutable session machine */
1014 a->session->COMGETTER(Machine)(machine.asOutParam());
1015
1016 if (!pszCtl)
1017 {
1018 /* it's important to always close sessions */
1019 a->session->UnlockMachine();
1020 errorSyntax(USAGE_STORAGECONTROLLER, "Storage controller name not specified\n");
1021 return 1;
1022 }
1023
1024 if (fRemoveCtl)
1025 {
1026 CHECK_ERROR(machine, RemoveStorageController(Bstr(pszCtl).raw()));
1027 }
1028 else
1029 {
1030 if (pszBusType)
1031 {
1032 ComPtr<IStorageController> ctl;
1033
1034 if (!RTStrICmp(pszBusType, "ide"))
1035 {
1036 CHECK_ERROR(machine, AddStorageController(Bstr(pszCtl).raw(),
1037 StorageBus_IDE,
1038 ctl.asOutParam()));
1039 }
1040 else if (!RTStrICmp(pszBusType, "sata"))
1041 {
1042 CHECK_ERROR(machine, AddStorageController(Bstr(pszCtl).raw(),
1043 StorageBus_SATA,
1044 ctl.asOutParam()));
1045 }
1046 else if (!RTStrICmp(pszBusType, "scsi"))
1047 {
1048 CHECK_ERROR(machine, AddStorageController(Bstr(pszCtl).raw(),
1049 StorageBus_SCSI,
1050 ctl.asOutParam()));
1051 }
1052 else if (!RTStrICmp(pszBusType, "floppy"))
1053 {
1054 CHECK_ERROR(machine, AddStorageController(Bstr(pszCtl).raw(),
1055 StorageBus_Floppy,
1056 ctl.asOutParam()));
1057 }
1058 else if (!RTStrICmp(pszBusType, "sas"))
1059 {
1060 CHECK_ERROR(machine, AddStorageController(Bstr(pszCtl).raw(),
1061 StorageBus_SAS,
1062 ctl.asOutParam()));
1063 }
1064 else
1065 {
1066 errorArgument("Invalid --add argument '%s'", pszBusType);
1067 rc = E_FAIL;
1068 }
1069 }
1070
1071 if ( pszCtlType
1072 && SUCCEEDED(rc))
1073 {
1074 ComPtr<IStorageController> ctl;
1075
1076 CHECK_ERROR(machine, GetStorageControllerByName(Bstr(pszCtl).raw(),
1077 ctl.asOutParam()));
1078
1079 if (SUCCEEDED(rc))
1080 {
1081 if (!RTStrICmp(pszCtlType, "lsilogic"))
1082 {
1083 CHECK_ERROR(ctl, COMSETTER(ControllerType)(StorageControllerType_LsiLogic));
1084 }
1085 else if (!RTStrICmp(pszCtlType, "buslogic"))
1086 {
1087 CHECK_ERROR(ctl, COMSETTER(ControllerType)(StorageControllerType_BusLogic));
1088 }
1089 else if (!RTStrICmp(pszCtlType, "intelahci"))
1090 {
1091 CHECK_ERROR(ctl, COMSETTER(ControllerType)(StorageControllerType_IntelAhci));
1092 }
1093 else if (!RTStrICmp(pszCtlType, "piix3"))
1094 {
1095 CHECK_ERROR(ctl, COMSETTER(ControllerType)(StorageControllerType_PIIX3));
1096 }
1097 else if (!RTStrICmp(pszCtlType, "piix4"))
1098 {
1099 CHECK_ERROR(ctl, COMSETTER(ControllerType)(StorageControllerType_PIIX4));
1100 }
1101 else if (!RTStrICmp(pszCtlType, "ich6"))
1102 {
1103 CHECK_ERROR(ctl, COMSETTER(ControllerType)(StorageControllerType_ICH6));
1104 }
1105 else if (!RTStrICmp(pszCtlType, "i82078"))
1106 {
1107 CHECK_ERROR(ctl, COMSETTER(ControllerType)(StorageControllerType_I82078));
1108 }
1109 else if (!RTStrICmp(pszCtlType, "lsilogicsas"))
1110 {
1111 CHECK_ERROR(ctl, COMSETTER(ControllerType)(StorageControllerType_LsiLogicSas));
1112 }
1113 else
1114 {
1115 errorArgument("Invalid --type argument '%s'", pszCtlType);
1116 rc = E_FAIL;
1117 }
1118 }
1119 else
1120 {
1121 errorArgument("Couldn't find the controller with the name: '%s'\n", pszCtl);
1122 rc = E_FAIL;
1123 }
1124 }
1125
1126 if ( (sataportcount != ~0U)
1127 && SUCCEEDED(rc))
1128 {
1129 ComPtr<IStorageController> ctl;
1130
1131 CHECK_ERROR(machine, GetStorageControllerByName(Bstr(pszCtl).raw(),
1132 ctl.asOutParam()));
1133
1134 if (SUCCEEDED(rc))
1135 {
1136 CHECK_ERROR(ctl, COMSETTER(PortCount)(sataportcount));
1137 }
1138 else
1139 {
1140 errorArgument("Couldn't find the controller with the name: '%s'\n", pszCtl);
1141 rc = E_FAIL;
1142 }
1143 }
1144
1145 if ( pszHostIOCache
1146 && SUCCEEDED(rc))
1147 {
1148 ComPtr<IStorageController> ctl;
1149
1150 CHECK_ERROR(machine, GetStorageControllerByName(Bstr(pszCtl).raw(),
1151 ctl.asOutParam()));
1152
1153 if (SUCCEEDED(rc))
1154 {
1155 if (!RTStrICmp(pszHostIOCache, "on"))
1156 {
1157 CHECK_ERROR(ctl, COMSETTER(UseHostIOCache)(TRUE));
1158 }
1159 else if (!RTStrICmp(pszHostIOCache, "off"))
1160 {
1161 CHECK_ERROR(ctl, COMSETTER(UseHostIOCache)(FALSE));
1162 }
1163 else
1164 {
1165 errorArgument("Invalid --hostiocache argument '%s'", pszHostIOCache);
1166 rc = E_FAIL;
1167 }
1168 }
1169 else
1170 {
1171 errorArgument("Couldn't find the controller with the name: '%s'\n", pszCtl);
1172 rc = E_FAIL;
1173 }
1174 }
1175
1176 if ( pszBootable
1177 && SUCCEEDED(rc))
1178 {
1179 if (SUCCEEDED(rc))
1180 {
1181 if (!RTStrICmp(pszBootable, "on"))
1182 {
1183 CHECK_ERROR(machine, SetStorageControllerBootable(Bstr(pszCtl).raw(), TRUE));
1184 }
1185 else if (!RTStrICmp(pszBootable, "off"))
1186 {
1187 CHECK_ERROR(machine, SetStorageControllerBootable(Bstr(pszCtl).raw(), FALSE));
1188 }
1189 else
1190 {
1191 errorArgument("Invalid --bootable argument '%s'", pszBootable);
1192 rc = E_FAIL;
1193 }
1194 }
1195 else
1196 {
1197 errorArgument("Couldn't find the controller with the name: '%s'\n", pszCtl);
1198 rc = E_FAIL;
1199 }
1200 }
1201 }
1202
1203 /* commit changes */
1204 if (SUCCEEDED(rc))
1205 CHECK_ERROR(machine, SaveSettings());
1206
1207 /* it's important to always close sessions */
1208 a->session->UnlockMachine();
1209
1210 return SUCCEEDED(rc) ? 0 : 1;
1211}
1212
1213#endif /* !VBOX_ONLY_DOCS */
1214
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