VirtualBox

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

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

FE/VBoxManage: Enable support for disk hotplug when using the CLI

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