VirtualBox

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

Last change on this file since 35137 was 35137, checked in by vboxsync, 14 years ago

Frontends/VBoxManage: typo in the medium type error handling code

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