VirtualBox

source: vbox/trunk/src/VBox/Main/testcase/tstOVF.cpp@ 96131

最後變更 在這個檔案從96131是 96131,由 vboxsync 提交於 3 年 前

FE/Qt+Main/testcase/tstOVF.cpp: A few small updates to the VBox GUI to
recognize Virtio-SCSI controllers in order to round out the support for
exporting an appliance which contains a Virtio-SCSI controller.
bugref:9745

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 14.0 KB
 
1/* $Id: tstOVF.cpp 96131 2022-08-10 15:49:43Z vboxsync $ */
2/** @file
3 *
4 * tstOVF - testcases for OVF import and export
5 */
6
7/*
8 * Copyright (C) 2010-2022 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.alldomusa.eu.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19#include <VBox/com/VirtualBox.h>
20
21#include <VBox/com/com.h>
22#include <VBox/com/array.h>
23#include <VBox/com/string.h>
24#include <VBox/com/ErrorInfo.h>
25#include <VBox/com/errorprint.h>
26
27#include <iprt/initterm.h>
28#include <iprt/stream.h>
29#include <iprt/file.h>
30#include <iprt/path.h>
31#include <iprt/param.h>
32
33#include <list>
34
35using namespace com;
36
37// main
38///////////////////////////////////////////////////////////////////////////////
39
40/**
41 * Quick hack exception structure.
42 *
43 */
44struct MyError
45{
46 MyError(HRESULT rc,
47 const char *pcsz,
48 IProgress *pProgress = NULL)
49 : m_rc(rc)
50 {
51 m_str = "ERROR: ";
52 m_str += pcsz;
53
54 if (pProgress)
55 {
56 com::ProgressErrorInfo info(pProgress);
57 com::GluePrintErrorInfo(info);
58 }
59 else if (rc != S_OK)
60 {
61 com::ErrorInfo info;
62 if (!info.isFullAvailable() && !info.isBasicAvailable())
63 com::GluePrintRCMessage(rc);
64 else
65 com::GluePrintErrorInfo(info);
66 }
67 }
68
69 Utf8Str m_str;
70 HRESULT m_rc;
71};
72
73/**
74 * Imports the given OVF file, with all bells and whistles.
75 * Throws MyError on errors.
76 * @param pcszPrefix Descriptive short prefix string for console output.
77 * @param pVirtualBox VirtualBox instance.
78 * @param pcszOVF0 File to import.
79 * @param llMachinesCreated out: UUIDs of machines that were created so that caller can clean up.
80 */
81void importOVF(const char *pcszPrefix,
82 ComPtr<IVirtualBox> &pVirtualBox,
83 const char *pcszOVF0,
84 std::list<Guid> &llMachinesCreated)
85{
86 char szAbsOVF[RTPATH_MAX];
87 RTPathExecDir(szAbsOVF, sizeof(szAbsOVF));
88 RTPathAppend(szAbsOVF, sizeof(szAbsOVF), pcszOVF0);
89
90 RTPrintf("%s: reading appliance \"%s\"...\n", pcszPrefix, szAbsOVF);
91 ComPtr<IAppliance> pAppl;
92 HRESULT rc = pVirtualBox->CreateAppliance(pAppl.asOutParam());
93 if (FAILED(rc)) throw MyError(rc, "failed to create appliance\n");
94
95 ComPtr<IProgress> pProgress;
96 rc = pAppl->Read(Bstr(szAbsOVF).raw(), pProgress.asOutParam());
97 if (FAILED(rc)) throw MyError(rc, "Appliance::Read() failed\n");
98 rc = pProgress->WaitForCompletion(-1);
99 if (FAILED(rc)) throw MyError(rc, "Progress::WaitForCompletion() failed\n");
100 LONG rc2;
101 pProgress->COMGETTER(ResultCode)(&rc2);
102 if (FAILED(rc2)) throw MyError(rc2, "Appliance::Read() failed\n", pProgress);
103
104 RTPrintf("%s: interpreting appliance \"%s\"...\n", pcszPrefix, szAbsOVF);
105 rc = pAppl->Interpret();
106 if (FAILED(rc)) throw MyError(rc, "Appliance::Interpret() failed\n");
107
108 com::SafeIfaceArray<IVirtualSystemDescription> aDescriptions;
109 rc = pAppl->COMGETTER(VirtualSystemDescriptions)(ComSafeArrayAsOutParam(aDescriptions));
110 for (uint32_t u = 0;
111 u < aDescriptions.size();
112 ++u)
113 {
114 ComPtr<IVirtualSystemDescription> pVSys = aDescriptions[u];
115
116 com::SafeArray<VirtualSystemDescriptionType_T> aTypes;
117 com::SafeArray<BSTR> aRefs;
118 com::SafeArray<BSTR> aOvfValues;
119 com::SafeArray<BSTR> aVBoxValues;
120 com::SafeArray<BSTR> aExtraConfigValues;
121 rc = pVSys->GetDescription(ComSafeArrayAsOutParam(aTypes),
122 ComSafeArrayAsOutParam(aRefs),
123 ComSafeArrayAsOutParam(aOvfValues),
124 ComSafeArrayAsOutParam(aVBoxValues),
125 ComSafeArrayAsOutParam(aExtraConfigValues));
126 if (FAILED(rc)) throw MyError(rc, "VirtualSystemDescription::GetDescription() failed\n");
127
128 for (uint32_t u2 = 0;
129 u2 < aTypes.size();
130 ++u2)
131 {
132 const char *pcszType;
133
134 VirtualSystemDescriptionType_T t = aTypes[u2];
135 switch (t)
136 {
137 case VirtualSystemDescriptionType_OS:
138 pcszType = "ostype";
139 break;
140
141 case VirtualSystemDescriptionType_Name:
142 pcszType = "name";
143 break;
144
145 case VirtualSystemDescriptionType_Product:
146 pcszType = "product";
147 break;
148
149 case VirtualSystemDescriptionType_ProductUrl:
150 pcszType = "producturl";
151 break;
152
153 case VirtualSystemDescriptionType_Vendor:
154 pcszType = "vendor";
155 break;
156
157 case VirtualSystemDescriptionType_VendorUrl:
158 pcszType = "vendorurl";
159 break;
160
161 case VirtualSystemDescriptionType_Version:
162 pcszType = "version";
163 break;
164
165 case VirtualSystemDescriptionType_Description:
166 pcszType = "description";
167 break;
168
169 case VirtualSystemDescriptionType_License:
170 pcszType = "license";
171 break;
172
173 case VirtualSystemDescriptionType_CPU:
174 pcszType = "cpu";
175 break;
176
177 case VirtualSystemDescriptionType_Memory:
178 pcszType = "memory";
179 break;
180
181 case VirtualSystemDescriptionType_HardDiskControllerIDE:
182 pcszType = "ide";
183 break;
184
185 case VirtualSystemDescriptionType_HardDiskControllerSATA:
186 pcszType = "sata";
187 break;
188
189 case VirtualSystemDescriptionType_HardDiskControllerSAS:
190 pcszType = "sas";
191 break;
192
193 case VirtualSystemDescriptionType_HardDiskControllerSCSI:
194 pcszType = "scsi";
195 break;
196
197 case VirtualSystemDescriptionType_HardDiskControllerVirtioSCSI:
198 pcszType = "virtio-scsi";
199 break;
200
201 case VirtualSystemDescriptionType_HardDiskImage:
202 pcszType = "hd";
203 break;
204
205 case VirtualSystemDescriptionType_CDROM:
206 pcszType = "cdrom";
207 break;
208
209 case VirtualSystemDescriptionType_Floppy:
210 pcszType = "floppy";
211 break;
212
213 case VirtualSystemDescriptionType_NetworkAdapter:
214 pcszType = "net";
215 break;
216
217 case VirtualSystemDescriptionType_USBController:
218 pcszType = "usb";
219 break;
220
221 case VirtualSystemDescriptionType_SoundCard:
222 pcszType = "sound";
223 break;
224
225 default:
226 throw MyError(E_UNEXPECTED, "Invalid VirtualSystemDescriptionType\n");
227 break;
228 }
229
230 RTPrintf(" vsys %2u item %2u: type %2d (%s), ovf: \"%ls\", vbox: \"%ls\", extra: \"%ls\"\n",
231 u, u2, t, pcszType,
232 aOvfValues[u2],
233 aVBoxValues[u2],
234 aExtraConfigValues[u2]);
235 }
236 }
237
238 RTPrintf("%s: importing %d machine(s)...\n", pcszPrefix, aDescriptions.size());
239 SafeArray<ImportOptions_T> sfaOptions;
240 rc = pAppl->ImportMachines(ComSafeArrayAsInParam(sfaOptions), pProgress.asOutParam());
241 if (FAILED(rc)) throw MyError(rc, "Appliance::ImportMachines() failed\n");
242 rc = pProgress->WaitForCompletion(-1);
243 if (FAILED(rc)) throw MyError(rc, "Progress::WaitForCompletion() failed\n");
244 pProgress->COMGETTER(ResultCode)(&rc2);
245 if (FAILED(rc2)) throw MyError(rc2, "Appliance::ImportMachines() failed\n", pProgress);
246
247 com::SafeArray<BSTR> aMachineUUIDs;
248 rc = pAppl->COMGETTER(Machines)(ComSafeArrayAsOutParam(aMachineUUIDs));
249 if (FAILED(rc)) throw MyError(rc, "Appliance::GetMachines() failed\n");
250
251 for (size_t u = 0;
252 u < aMachineUUIDs.size();
253 ++u)
254 {
255 RTPrintf("%s: created machine %u: %ls\n", pcszPrefix, u, aMachineUUIDs[u]);
256 llMachinesCreated.push_back(Guid(Bstr(aMachineUUIDs[u])));
257 }
258
259 RTPrintf("%s: success!\n", pcszPrefix);
260}
261
262/**
263 * Copies ovf-testcases/ovf-dummy.vmdk to the given target and appends that
264 * target as a string to the given list so that the caller can delete it
265 * again later.
266 * @param llFiles2Delete List of strings to append the target file path to.
267 * @param pcszDest Target for dummy VMDK.
268 */
269void copyDummyDiskImage(const char *pcszPrefix,
270 std::list<Utf8Str> &llFiles2Delete,
271 const char *pcszDest)
272{
273 char szSrc[RTPATH_MAX];
274 char szDst[RTPATH_MAX];
275 RTPathExecDir(szSrc, sizeof(szSrc));
276 RTPathAppend(szSrc, sizeof(szSrc), "ovf-testcases/ovf-dummy.vmdk");
277 RTPathExecDir(szDst, sizeof(szDst));
278 RTPathAppend(szDst, sizeof(szDst), pcszDest);
279 RTPrintf("%s: copying ovf-dummy.vmdk to \"%s\"...\n", pcszPrefix, pcszDest);
280
281 int vrc = RTFileCopy(szSrc, szDst);
282 if (RT_FAILURE(vrc)) throw MyError(0, Utf8StrFmt("Cannot copy ovf-dummy.vmdk to %s: %Rra\n", pcszDest, vrc).c_str());
283 llFiles2Delete.push_back(szDst);
284}
285
286/**
287 *
288 * @param argc
289 * @param argv[]
290 * @return
291 */
292int main(int argc, char *argv[])
293{
294 RTR3InitExe(argc, &argv, 0);
295
296 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
297 HRESULT rc = S_OK;
298
299 std::list<Utf8Str> llFiles2Delete;
300 std::list<Guid> llMachinesCreated;
301
302 ComPtr<IVirtualBoxClient> pVirtualBoxClient;
303 ComPtr<IVirtualBox> pVirtualBox;
304
305 try
306 {
307 RTPrintf("Initializing COM...\n");
308 rc = com::Initialize();
309 if (FAILED(rc)) throw MyError(rc, "failed to initialize COM!\n");
310
311 ComPtr<ISession> pSession;
312
313 RTPrintf("Creating VirtualBox object...\n");
314 rc = pVirtualBoxClient.createInprocObject(CLSID_VirtualBoxClient);
315 if (SUCCEEDED(rc))
316 rc = pVirtualBoxClient->COMGETTER(VirtualBox)(pVirtualBox.asOutParam());
317 if (FAILED(rc)) throw MyError(rc, "failed to create the VirtualBox object!\n");
318
319 rc = pSession.createInprocObject(CLSID_Session);
320 if (FAILED(rc)) throw MyError(rc, "failed to create a session object!\n");
321
322 // for each testcase, we will copy the dummy VMDK image to the subdirectory with the OVF testcase
323 // so that the import will find the disks it expects; this is just for testing the import since
324 // the imported machines will obviously not be usable.
325 // llFiles2Delete receives the paths of all the files that we need to clean up later.
326
327 // testcase 1: import ovf-joomla-0.9/joomla-1.1.4-ovf.ovf
328 copyDummyDiskImage("joomla-0.9", llFiles2Delete, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf-0.vmdk");
329 copyDummyDiskImage("joomla-0.9", llFiles2Delete, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf-1.vmdk");
330 importOVF("joomla-0.9", pVirtualBox, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf.ovf", llMachinesCreated);
331
332 // testcase 2: import ovf-winxp-vbox-sharedfolders/winxp.ovf
333 copyDummyDiskImage("winxp-vbox-sharedfolders", llFiles2Delete, "ovf-testcases/ovf-winxp-vbox-sharedfolders/Windows 5.1 XP 1 merged.vmdk");
334 copyDummyDiskImage("winxp-vbox-sharedfolders", llFiles2Delete, "ovf-testcases/ovf-winxp-vbox-sharedfolders/smallvdi.vmdk");
335 importOVF("winxp-vbox-sharedfolders", pVirtualBox, "ovf-testcases/ovf-winxp-vbox-sharedfolders/winxp.ovf", llMachinesCreated);
336
337 // testcase 3: import ovf-winxp-vbox-sharedfolders/winxp.ovf
338 importOVF("winhost-audio-nodisks", pVirtualBox, "ovf-testcases/ovf-winhost-audio-nodisks/WinXP.ovf", llMachinesCreated);
339
340 RTPrintf("Machine imports done, no errors. Cleaning up...\n");
341 }
342 catch (MyError &e)
343 {
344 rc = e.m_rc;
345 RTPrintf("%s", e.m_str.c_str());
346 rcExit = RTEXITCODE_FAILURE;
347 }
348
349 try
350 {
351 // clean up the machines created
352 for (std::list<Guid>::const_iterator it = llMachinesCreated.begin();
353 it != llMachinesCreated.end();
354 ++it)
355 {
356 const Guid &uuid = *it;
357 Bstr bstrUUID(uuid.toUtf16());
358 ComPtr<IMachine> pMachine;
359 rc = pVirtualBox->FindMachine(bstrUUID.raw(), pMachine.asOutParam());
360 if (FAILED(rc)) throw MyError(rc, "VirtualBox::FindMachine() failed\n");
361
362 RTPrintf(" Deleting machine %ls...\n", bstrUUID.raw());
363 SafeIfaceArray<IMedium> sfaMedia;
364 rc = pMachine->Unregister(CleanupMode_DetachAllReturnHardDisksOnly,
365 ComSafeArrayAsOutParam(sfaMedia));
366 if (FAILED(rc)) throw MyError(rc, "Machine::Unregister() failed\n");
367
368 ComPtr<IProgress> pProgress;
369 rc = pMachine->DeleteConfig(ComSafeArrayAsInParam(sfaMedia), pProgress.asOutParam());
370 if (FAILED(rc)) throw MyError(rc, "Machine::DeleteSettings() failed\n");
371 rc = pProgress->WaitForCompletion(-1);
372 if (FAILED(rc)) throw MyError(rc, "Progress::WaitForCompletion() failed\n");
373 }
374 }
375 catch (MyError &e)
376 {
377 rc = e.m_rc;
378 RTPrintf("%s", e.m_str.c_str());
379 rcExit = RTEXITCODE_FAILURE;
380 }
381
382 // clean up the VMDK copies that we made in copyDummyDiskImage()
383 for (std::list<Utf8Str>::const_iterator it = llFiles2Delete.begin();
384 it != llFiles2Delete.end();
385 ++it)
386 {
387 const Utf8Str &strFile = *it;
388 RTPrintf("Deleting file %s...\n", strFile.c_str());
389 RTFileDelete(strFile.c_str());
390 }
391
392 pVirtualBox.setNull();
393 pVirtualBoxClient.setNull();
394
395 RTPrintf("Shutting down COM...\n");
396 com::Shutdown();
397 RTPrintf("tstOVF all done: %s\n", rcExit ? "ERROR" : "SUCCESS");
398
399 return rcExit;
400}
401
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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