VirtualBox

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

Last change on this file since 42699 was 42551, checked in by vboxsync, 12 years ago

Main: big API naming cleanup, use all caps acronyms everywhere, including SDK docs
Frontends/VBoxManage: implement guestcontrol execute for new API, disabled by default

  • 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 42551 2012-08-02 16:44:39Z vboxsync $ */
2/** @file
3 * VBoxManage - The storage controller related commands.
4 */
5
6/*
7 * Copyright (C) 2006-2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifndef VBOX_ONLY_DOCS
19
20/*******************************************************************************
21* Header Files *
22*******************************************************************************/
23#include <VBox/com/com.h>
24#include <VBox/com/array.h>
25#include <VBox/com/ErrorInfo.h>
26#include <VBox/com/errorprint.h>
27#include <VBox/com/VirtualBox.h>
28
29#include <iprt/path.h>
30#include <iprt/param.h>
31#include <iprt/string.h>
32#include <iprt/ctype.h>
33#include <iprt/stream.h>
34#include <iprt/getopt.h>
35#include <VBox/log.h>
36
37#include "VBoxManage.h"
38using namespace com;
39
40
41// funcs
42///////////////////////////////////////////////////////////////////////////////
43
44
45static const RTGETOPTDEF g_aStorageAttachOptions[] =
46{
47 { "--storagectl", 's', RTGETOPT_REQ_STRING },
48 { "--port", 'p', RTGETOPT_REQ_UINT32 },
49 { "--device", 'd', RTGETOPT_REQ_UINT32 },
50 { "--type", 't', RTGETOPT_REQ_STRING },
51 { "--medium", 'm', RTGETOPT_REQ_STRING },
52 { "--mtype", 'M', RTGETOPT_REQ_STRING },
53 { "--passthrough", 'h', RTGETOPT_REQ_STRING },
54 { "--tempeject", 'e', RTGETOPT_REQ_STRING },
55 { "--nonrotational", 'n', RTGETOPT_REQ_STRING },
56 { "--discard", 'u', RTGETOPT_REQ_STRING },
57 { "--bandwidthgroup", 'b', RTGETOPT_REQ_STRING },
58 { "--forceunmount", 'f', RTGETOPT_REQ_NOTHING },
59 { "--comment", 'C', RTGETOPT_REQ_STRING },
60 { "--setuuid", 'q', RTGETOPT_REQ_STRING },
61 { "--setparentuuid", 'Q', RTGETOPT_REQ_STRING },
62 // iSCSI options
63 { "--server", 'S', RTGETOPT_REQ_STRING },
64 { "--target", 'T', RTGETOPT_REQ_STRING },
65 { "--tport", 'P', RTGETOPT_REQ_STRING },
66 { "--lun", 'L', RTGETOPT_REQ_STRING },
67 { "--encodedlun", 'E', RTGETOPT_REQ_STRING },
68 { "--username", 'U', RTGETOPT_REQ_STRING },
69 { "--password", 'W', RTGETOPT_REQ_STRING },
70 { "--initiator", 'N', RTGETOPT_REQ_STRING },
71 { "--intnet", 'I', RTGETOPT_REQ_NOTHING },
72};
73
74int handleStorageAttach(HandlerArg *a)
75{
76 int c = VERR_INTERNAL_ERROR; /* initialized to shut up gcc */
77 HRESULT rc = S_OK;
78 ULONG port = ~0U;
79 ULONG device = ~0U;
80 bool fForceUnmount = false;
81 bool fSetMediumType = false;
82 bool fSetNewUuid = false;
83 bool fSetNewParentUuid = false;
84 MediumType_T mediumType = MediumType_Normal;
85 Bstr bstrComment;
86 const char *pszCtl = NULL;
87 DeviceType_T devTypeRequested = DeviceType_Null;
88 const char *pszMedium = NULL;
89 const char *pszPassThrough = NULL;
90 const char *pszTempEject = NULL;
91 const char *pszNonRotational = NULL;
92 const char *pszDiscard = NULL;
93 const char *pszBandwidthGroup = NULL;
94 Bstr bstrNewUuid;
95 Bstr bstrNewParentUuid;
96 // iSCSI options
97 Bstr bstrServer;
98 Bstr bstrTarget;
99 Bstr bstrPort;
100 Bstr bstrLun;
101 Bstr bstrUsername;
102 Bstr bstrPassword;
103 Bstr bstrInitiator;
104 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, AttachDeviceWithoutMedium(Bstr(pszCtl).raw(), port, device,
410 devTypeRequested));
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, AttachDeviceWithoutMedium(Bstr(pszCtl).raw(), port, device,
448 deviceType));
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->AttachDeviceWithoutMedium(Bstr(pszCtl).raw(),
693 port,
694 device,
695 devTypeRequested); // DeviceType_DVD or DeviceType_Floppy
696 }
697 }
698 else
699 {
700 rc = machine->AttachDeviceWithoutMedium(Bstr(pszCtl).raw(),
701 port,
702 device,
703 devTypeRequested); // DeviceType_DVD or DeviceType_Floppy
704 }
705 }
706
707 if (pMedium2Mount)
708 {
709 CHECK_ERROR(machine, MountMedium(Bstr(pszCtl).raw(),
710 port,
711 device,
712 pMedium2Mount,
713 fForceUnmount));
714 }
715 } // end DeviceType_DVD or DeviceType_Floppy:
716 break;
717
718 case DeviceType_HardDisk:
719 {
720 // if there is anything attached at the given location, remove it
721 machine->DetachDevice(Bstr(pszCtl).raw(), port, device);
722 CHECK_ERROR(machine, AttachDevice(Bstr(pszCtl).raw(),
723 port,
724 device,
725 DeviceType_HardDisk,
726 pMedium2Mount));
727 }
728 break;
729 }
730 }
731 }
732
733 if ( pszPassThrough
734 && (SUCCEEDED(rc)))
735 {
736 ComPtr<IMediumAttachment> mattach;
737 CHECK_ERROR(machine, GetMediumAttachment(Bstr(pszCtl).raw(), port,
738 device, mattach.asOutParam()));
739
740 if (SUCCEEDED(rc))
741 {
742 if (!RTStrICmp(pszPassThrough, "on"))
743 {
744 CHECK_ERROR(machine, PassthroughDevice(Bstr(pszCtl).raw(),
745 port, device, TRUE));
746 }
747 else if (!RTStrICmp(pszPassThrough, "off"))
748 {
749 CHECK_ERROR(machine, PassthroughDevice(Bstr(pszCtl).raw(),
750 port, device, FALSE));
751 }
752 else
753 throw Utf8StrFmt("Invalid --passthrough argument '%s'", pszPassThrough);
754 }
755 else
756 throw Utf8StrFmt("Couldn't find the controller attachment for the controller '%s'\n", pszCtl);
757 }
758
759 if ( pszTempEject
760 && (SUCCEEDED(rc)))
761 {
762 ComPtr<IMediumAttachment> mattach;
763 CHECK_ERROR(machine, GetMediumAttachment(Bstr(pszCtl).raw(), port,
764 device, mattach.asOutParam()));
765
766 if (SUCCEEDED(rc))
767 {
768 if (!RTStrICmp(pszTempEject, "on"))
769 {
770 CHECK_ERROR(machine, TemporaryEjectDevice(Bstr(pszCtl).raw(),
771 port, device, TRUE));
772 }
773 else if (!RTStrICmp(pszTempEject, "off"))
774 {
775 CHECK_ERROR(machine, TemporaryEjectDevice(Bstr(pszCtl).raw(),
776 port, device, FALSE));
777 }
778 else
779 throw Utf8StrFmt("Invalid --tempeject argument '%s'", pszTempEject);
780 }
781 else
782 throw Utf8StrFmt("Couldn't find the controller attachment for the controller '%s'\n", pszCtl);
783 }
784
785 if ( pszNonRotational
786 && (SUCCEEDED(rc)))
787 {
788 ComPtr<IMediumAttachment> mattach;
789 CHECK_ERROR(machine, GetMediumAttachment(Bstr(pszCtl).raw(), port,
790 device, mattach.asOutParam()));
791
792 if (SUCCEEDED(rc))
793 {
794 if (!RTStrICmp(pszNonRotational, "on"))
795 {
796 CHECK_ERROR(machine, NonRotationalDevice(Bstr(pszCtl).raw(),
797 port, device, TRUE));
798 }
799 else if (!RTStrICmp(pszNonRotational, "off"))
800 {
801 CHECK_ERROR(machine, NonRotationalDevice(Bstr(pszCtl).raw(),
802 port, device, FALSE));
803 }
804 else
805 throw Utf8StrFmt("Invalid --nonrotational argument '%s'", pszNonRotational);
806 }
807 else
808 throw Utf8StrFmt("Couldn't find the controller attachment for the controller '%s'\n", pszCtl);
809 }
810
811 if ( pszNonRotational
812 && (SUCCEEDED(rc)))
813 {
814 ComPtr<IMediumAttachment> mattach;
815 CHECK_ERROR(machine, GetMediumAttachment(Bstr(pszCtl).raw(), port,
816 device, mattach.asOutParam()));
817
818 if (SUCCEEDED(rc))
819 {
820 if (!RTStrICmp(pszDiscard, "on"))
821 {
822 CHECK_ERROR(machine, SetAutoDiscardForDevice(Bstr(pszCtl).raw(),
823 port, device, TRUE));
824 }
825 else if (!RTStrICmp(pszDiscard, "off"))
826 {
827 CHECK_ERROR(machine, SetAutoDiscardForDevice(Bstr(pszCtl).raw(),
828 port, device, FALSE));
829 }
830 else
831 throw Utf8StrFmt("Invalid --nonrotational argument '%s'", pszNonRotational);
832 }
833 else
834 throw Utf8StrFmt("Couldn't find the controller attachment for the controller '%s'\n", pszCtl);
835 }
836
837
838 if ( pszBandwidthGroup
839 && !fRunTime
840 && SUCCEEDED(rc))
841 {
842
843 if (!RTStrICmp(pszBandwidthGroup, "none"))
844 {
845 /* Just remove the bandwidth gorup. */
846 CHECK_ERROR(machine, SetNoBandwidthGroupForDevice(Bstr(pszCtl).raw(),
847 port, device));
848 }
849 else
850 {
851 ComPtr<IBandwidthControl> bwCtrl;
852 ComPtr<IBandwidthGroup> bwGroup;
853
854 CHECK_ERROR(machine, COMGETTER(BandwidthControl)(bwCtrl.asOutParam()));
855
856 if (SUCCEEDED(rc))
857 {
858 CHECK_ERROR(bwCtrl, GetBandwidthGroup(Bstr(pszBandwidthGroup).raw(), bwGroup.asOutParam()));
859 if (SUCCEEDED(rc))
860 {
861 CHECK_ERROR(machine, SetBandwidthGroupForDevice(Bstr(pszCtl).raw(),
862 port, device, bwGroup));
863 }
864 }
865 }
866 }
867
868 /* commit changes */
869 if (SUCCEEDED(rc))
870 CHECK_ERROR(machine, SaveSettings());
871 }
872 catch (const Utf8Str &strError)
873 {
874 errorArgument("%s", strError.c_str());
875 rc = E_FAIL;
876 }
877
878 // machine must always be unlocked, even on errors
879leave:
880 a->session->UnlockMachine();
881
882 return SUCCEEDED(rc) ? 0 : 1;
883}
884
885
886static const RTGETOPTDEF g_aStorageControllerOptions[] =
887{
888 { "--name", 'n', RTGETOPT_REQ_STRING },
889 { "--add", 'a', RTGETOPT_REQ_STRING },
890 { "--controller", 'c', RTGETOPT_REQ_STRING },
891 { "--sataideemulation", 'e', RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_INDEX },
892 { "--sataportcount", 'p', RTGETOPT_REQ_UINT32 },
893 { "--remove", 'r', RTGETOPT_REQ_NOTHING },
894 { "--hostiocache", 'i', RTGETOPT_REQ_STRING },
895 { "--bootable", 'b', RTGETOPT_REQ_STRING },
896};
897
898int handleStorageController(HandlerArg *a)
899{
900 int c;
901 HRESULT rc = S_OK;
902 const char *pszCtl = NULL;
903 const char *pszBusType = NULL;
904 const char *pszCtlType = NULL;
905 const char *pszHostIOCache = NULL;
906 const char *pszBootable = NULL;
907 ULONG satabootdev = ~0U;
908 ULONG sataidedev = ~0U;
909 ULONG sataportcount = ~0U;
910 bool fRemoveCtl = false;
911 ComPtr<IMachine> machine;
912 RTGETOPTUNION ValueUnion;
913 RTGETOPTSTATE GetState;
914
915 if (a->argc < 4)
916 return errorSyntax(USAGE_STORAGECONTROLLER, "Too few parameters");
917
918 RTGetOptInit (&GetState, a->argc, a->argv, g_aStorageControllerOptions,
919 RT_ELEMENTS(g_aStorageControllerOptions), 1, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
920
921 while ( SUCCEEDED(rc)
922 && (c = RTGetOpt(&GetState, &ValueUnion)))
923 {
924 switch (c)
925 {
926 case 'n': // controller name
927 {
928 if (ValueUnion.psz)
929 pszCtl = ValueUnion.psz;
930 else
931 rc = E_FAIL;
932 break;
933 }
934
935 case 'a': // controller bus type <ide/sata/scsi/floppy>
936 {
937 if (ValueUnion.psz)
938 pszBusType = ValueUnion.psz;
939 else
940 rc = E_FAIL;
941 break;
942 }
943
944 case 'c': // controller <lsilogic/buslogic/intelahci/piix3/piix4/ich6/i82078>
945 {
946 if (ValueUnion.psz)
947 pszCtlType = ValueUnion.psz;
948 else
949 rc = E_FAIL;
950 break;
951 }
952
953 case 'e': // sataideemulation
954 {
955 satabootdev = GetState.uIndex;
956 sataidedev = ValueUnion.u32;
957 break;
958 }
959
960 case 'p': // sataportcount
961 {
962 sataportcount = ValueUnion.u32;
963 break;
964 }
965
966 case 'r': // remove controller
967 {
968 fRemoveCtl = true;
969 break;
970 }
971
972 case 'i':
973 {
974 pszHostIOCache = ValueUnion.psz;
975 break;
976 }
977
978 case 'b':
979 {
980 pszBootable = ValueUnion.psz;
981 break;
982 }
983
984 default:
985 {
986 errorGetOpt(USAGE_STORAGECONTROLLER, c, &ValueUnion);
987 rc = E_FAIL;
988 break;
989 }
990 }
991 }
992
993 if (FAILED(rc))
994 return 1;
995
996 /* try to find the given machine */
997 CHECK_ERROR_RET(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
998 machine.asOutParam()), 1);
999
1000 /* open a session for the VM */
1001 CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Write), 1);
1002
1003 /* get the mutable session machine */
1004 a->session->COMGETTER(Machine)(machine.asOutParam());
1005
1006 if (!pszCtl)
1007 {
1008 /* it's important to always close sessions */
1009 a->session->UnlockMachine();
1010 errorSyntax(USAGE_STORAGECONTROLLER, "Storage controller name not specified\n");
1011 return 1;
1012 }
1013
1014 if (fRemoveCtl)
1015 {
1016 CHECK_ERROR(machine, RemoveStorageController(Bstr(pszCtl).raw()));
1017 }
1018 else
1019 {
1020 if (pszBusType)
1021 {
1022 ComPtr<IStorageController> ctl;
1023
1024 if (!RTStrICmp(pszBusType, "ide"))
1025 {
1026 CHECK_ERROR(machine, AddStorageController(Bstr(pszCtl).raw(),
1027 StorageBus_IDE,
1028 ctl.asOutParam()));
1029 }
1030 else if (!RTStrICmp(pszBusType, "sata"))
1031 {
1032 CHECK_ERROR(machine, AddStorageController(Bstr(pszCtl).raw(),
1033 StorageBus_SATA,
1034 ctl.asOutParam()));
1035 }
1036 else if (!RTStrICmp(pszBusType, "scsi"))
1037 {
1038 CHECK_ERROR(machine, AddStorageController(Bstr(pszCtl).raw(),
1039 StorageBus_SCSI,
1040 ctl.asOutParam()));
1041 }
1042 else if (!RTStrICmp(pszBusType, "floppy"))
1043 {
1044 CHECK_ERROR(machine, AddStorageController(Bstr(pszCtl).raw(),
1045 StorageBus_Floppy,
1046 ctl.asOutParam()));
1047 }
1048 else if (!RTStrICmp(pszBusType, "sas"))
1049 {
1050 CHECK_ERROR(machine, AddStorageController(Bstr(pszCtl).raw(),
1051 StorageBus_SAS,
1052 ctl.asOutParam()));
1053 }
1054 else
1055 {
1056 errorArgument("Invalid --add argument '%s'", pszBusType);
1057 rc = E_FAIL;
1058 }
1059 }
1060
1061 if ( pszCtlType
1062 && SUCCEEDED(rc))
1063 {
1064 ComPtr<IStorageController> ctl;
1065
1066 CHECK_ERROR(machine, GetStorageControllerByName(Bstr(pszCtl).raw(),
1067 ctl.asOutParam()));
1068
1069 if (SUCCEEDED(rc))
1070 {
1071 if (!RTStrICmp(pszCtlType, "lsilogic"))
1072 {
1073 CHECK_ERROR(ctl, COMSETTER(ControllerType)(StorageControllerType_LsiLogic));
1074 }
1075 else if (!RTStrICmp(pszCtlType, "buslogic"))
1076 {
1077 CHECK_ERROR(ctl, COMSETTER(ControllerType)(StorageControllerType_BusLogic));
1078 }
1079 else if (!RTStrICmp(pszCtlType, "intelahci"))
1080 {
1081 CHECK_ERROR(ctl, COMSETTER(ControllerType)(StorageControllerType_IntelAhci));
1082 }
1083 else if (!RTStrICmp(pszCtlType, "piix3"))
1084 {
1085 CHECK_ERROR(ctl, COMSETTER(ControllerType)(StorageControllerType_PIIX3));
1086 }
1087 else if (!RTStrICmp(pszCtlType, "piix4"))
1088 {
1089 CHECK_ERROR(ctl, COMSETTER(ControllerType)(StorageControllerType_PIIX4));
1090 }
1091 else if (!RTStrICmp(pszCtlType, "ich6"))
1092 {
1093 CHECK_ERROR(ctl, COMSETTER(ControllerType)(StorageControllerType_ICH6));
1094 }
1095 else if (!RTStrICmp(pszCtlType, "i82078"))
1096 {
1097 CHECK_ERROR(ctl, COMSETTER(ControllerType)(StorageControllerType_I82078));
1098 }
1099 else if (!RTStrICmp(pszCtlType, "lsilogicsas"))
1100 {
1101 CHECK_ERROR(ctl, COMSETTER(ControllerType)(StorageControllerType_LsiLogicSas));
1102 }
1103 else
1104 {
1105 errorArgument("Invalid --type argument '%s'", pszCtlType);
1106 rc = E_FAIL;
1107 }
1108 }
1109 else
1110 {
1111 errorArgument("Couldn't find the controller with the name: '%s'\n", pszCtl);
1112 rc = E_FAIL;
1113 }
1114 }
1115
1116 if ( (sataportcount != ~0U)
1117 && SUCCEEDED(rc))
1118 {
1119 ComPtr<IStorageController> ctl;
1120
1121 CHECK_ERROR(machine, GetStorageControllerByName(Bstr(pszCtl).raw(),
1122 ctl.asOutParam()));
1123
1124 if (SUCCEEDED(rc))
1125 {
1126 CHECK_ERROR(ctl, COMSETTER(PortCount)(sataportcount));
1127 }
1128 else
1129 {
1130 errorArgument("Couldn't find the controller with the name: '%s'\n", pszCtl);
1131 rc = E_FAIL;
1132 }
1133 }
1134
1135 if ( (sataidedev != ~0U)
1136 && (satabootdev != ~0U)
1137 && SUCCEEDED(rc))
1138 {
1139 ComPtr<IStorageController> ctl;
1140
1141 CHECK_ERROR(machine, GetStorageControllerByName(Bstr(pszCtl).raw(),
1142 ctl.asOutParam()));
1143
1144 if (SUCCEEDED(rc))
1145 {
1146 CHECK_ERROR(ctl, SetIDEEmulationPort(satabootdev, sataidedev));
1147 }
1148 else
1149 {
1150 errorArgument("Couldn't find the controller with the name: '%s'\n", pszCtl);
1151 rc = E_FAIL;
1152 }
1153 }
1154
1155 if ( pszHostIOCache
1156 && SUCCEEDED(rc))
1157 {
1158 ComPtr<IStorageController> ctl;
1159
1160 CHECK_ERROR(machine, GetStorageControllerByName(Bstr(pszCtl).raw(),
1161 ctl.asOutParam()));
1162
1163 if (SUCCEEDED(rc))
1164 {
1165 if (!RTStrICmp(pszHostIOCache, "on"))
1166 {
1167 CHECK_ERROR(ctl, COMSETTER(UseHostIOCache)(TRUE));
1168 }
1169 else if (!RTStrICmp(pszHostIOCache, "off"))
1170 {
1171 CHECK_ERROR(ctl, COMSETTER(UseHostIOCache)(FALSE));
1172 }
1173 else
1174 {
1175 errorArgument("Invalid --hostiocache argument '%s'", pszHostIOCache);
1176 rc = E_FAIL;
1177 }
1178 }
1179 else
1180 {
1181 errorArgument("Couldn't find the controller with the name: '%s'\n", pszCtl);
1182 rc = E_FAIL;
1183 }
1184 }
1185
1186 if ( pszBootable
1187 && SUCCEEDED(rc))
1188 {
1189 if (SUCCEEDED(rc))
1190 {
1191 if (!RTStrICmp(pszBootable, "on"))
1192 {
1193 CHECK_ERROR(machine, SetStorageControllerBootable(Bstr(pszCtl).raw(), TRUE));
1194 }
1195 else if (!RTStrICmp(pszBootable, "off"))
1196 {
1197 CHECK_ERROR(machine, SetStorageControllerBootable(Bstr(pszCtl).raw(), FALSE));
1198 }
1199 else
1200 {
1201 errorArgument("Invalid --bootable argument '%s'", pszBootable);
1202 rc = E_FAIL;
1203 }
1204 }
1205 else
1206 {
1207 errorArgument("Couldn't find the controller with the name: '%s'\n", pszCtl);
1208 rc = E_FAIL;
1209 }
1210 }
1211 }
1212
1213 /* commit changes */
1214 if (SUCCEEDED(rc))
1215 CHECK_ERROR(machine, SaveSettings());
1216
1217 /* it's important to always close sessions */
1218 a->session->UnlockMachine();
1219
1220 return SUCCEEDED(rc) ? 0 : 1;
1221}
1222
1223#endif /* !VBOX_ONLY_DOCS */
1224
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