VirtualBox

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

Last change on this file since 42442 was 42442, checked in by vboxsync, 13 years ago

Elinate use of NULL object references in the API - 6124

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

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