VirtualBox

source: vbox/trunk/src/VBox/ImageMounter/vboximg-mount/vboximgMedia.cpp@ 81749

Last change on this file since 81749 was 81749, checked in by vboxsync, 5 years ago

vboximg-mount: Fix crash when the size is 0, log2() returns -HUGE_VAL in that case

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.9 KB
Line 
1/* $Id: vboximgMedia.cpp 81749 2019-11-08 10:10:23Z vboxsync $ $Revision: 81749 $ */
2/** @file
3 * vboximgMedia.cpp - Disk Image Flattening FUSE Program.
4 */
5
6/*
7 * Copyright (C) 2009-2019 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#include <VirtualBox_XPCOM.h>
19#include <VBox/com/VirtualBox.h>
20#include <VBox/vd.h>
21#include <VBox/vd-ifs.h>
22#include <VBox/log.h>
23#include <iprt/errcore.h>
24#include <VBox/com/ErrorInfo.h>
25#include <VBox/com/NativeEventQueue.h>
26#include <VBox/com/com.h>
27#include <VBox/com/string.h>
28#include <VBox/com/Guid.h>
29#include <VBox/com/array.h>
30#include <VBox/com/errorprint.h>
31#include <VBox/vd-plugin.h>
32#include <iprt/initterm.h>
33#include <iprt/assert.h>
34#include <iprt/message.h>
35#include <iprt/critsect.h>
36#include <iprt/asm.h>
37#include <iprt/mem.h>
38#include <iprt/string.h>
39#include <iprt/initterm.h>
40#include <iprt/stream.h>
41#include <iprt/types.h>
42#include <iprt/path.h>
43#include <iprt/utf16.h>
44#include <math.h>
45#include "vboximgOpts.h"
46
47using namespace com;
48
49extern VBOXIMGOPTS g_vboximgOpts;
50
51#define SAFENULL(strPtr) (strPtr ? strPtr : "") /** Makes null harmless to print */
52#define CSTR(arg) Utf8Str(arg).c_str() /** Converts XPCOM string type to C string type */
53#define MAX_UUID_LEN 256 /** Max length of a UUID */
54#define VM_MAX_NAME 32 /** Max length of VM name we handle */
55
56typedef struct MEDIUMINFO
57{
58 char *pszName;
59 char *pszUuid;
60 char *pszBaseUuid;
61 char *pszPath;
62 char *pszDescription;
63 char *pszState;
64 char *pszType;
65 char *pszFormat;
66 bool fSnapshot;
67 PRInt64 cbSize;
68 MediumType_T type;
69 MediumState_T state;
70 ~MEDIUMINFO()
71 {
72 RTMemFree(pszName);
73 RTMemFree(pszUuid);
74 RTMemFree(pszPath);
75 RTMemFree(pszDescription);
76 RTMemFree(pszFormat);
77 }
78} MEDIUMINFO;
79
80
81char *vboximgScaledSize(size_t size)
82{
83 uint64_t exp = 0;
84 if (size > 0)
85 exp = log2((double)size);
86 char scaledMagnitude = ((char []){ ' ', 'K', 'M', 'G', 'T', 'P' })[exp / 10];
87 /* This workaround is because IPRT RT*Printf* funcs don't handle floating point format specifiers */
88 double cbScaled = (double)size / pow(2, (double)(((uint64_t)(exp / 10)) * 10));
89 uint64_t intPart = cbScaled;
90 uint64_t fracPart = (cbScaled - (double)intPart) * 10;
91 char tmp[256];
92 RTStrPrintf(tmp, sizeof (tmp), "%d.%d%c", intPart, fracPart, scaledMagnitude);
93 return RTStrDup(tmp);
94}
95
96static int
97getMediumInfo(IMachine *pMachine, IMedium *pMedium, MEDIUMINFO **ppMediumInfo)
98{
99 NOREF(pMachine);
100 int rc;
101 MEDIUMINFO *info = new MEDIUMINFO();
102 *ppMediumInfo = info;
103
104 Bstr name;
105 Bstr uuid;
106 Bstr baseUuid;
107 Bstr path;
108 Bstr description;
109 Bstr format;
110 PRInt64 *pSize = &info->cbSize;
111 ComPtr<IMedium> pBase;
112 MediumType_T *pType = &info->type;
113 MediumState_T *pState = &info->state;
114
115 *pState = MediumState_NotCreated;
116
117 CHECK_ERROR(pMedium, RefreshState(pState));
118 CHECK_ERROR(pMedium, COMGETTER(Id)(uuid.asOutParam()));
119 CHECK_ERROR(pMedium, COMGETTER(Base)(pBase.asOutParam()));
120 CHECK_ERROR(pBase, COMGETTER(Id)(baseUuid.asOutParam()));
121
122 CHECK_ERROR(pMedium, COMGETTER(State)(pState));
123
124 CHECK_ERROR(pMedium, COMGETTER(Location)(path.asOutParam()));
125 CHECK_ERROR(pMedium, COMGETTER(Format)(format.asOutParam()));
126 CHECK_ERROR(pMedium, COMGETTER(Type)(pType));
127 CHECK_ERROR(pMedium, COMGETTER(Size)(pSize));
128
129 info->pszUuid = RTStrDup((char *)CSTR(uuid));
130 info->pszBaseUuid = RTStrDup((char *)CSTR(baseUuid));
131 info->pszPath = RTStrDup((char *)CSTR(path));
132 info->pszFormat = RTStrDup((char *)CSTR(format));
133 info->fSnapshot = RTStrCmp(CSTR(uuid), CSTR(baseUuid)) != 0;
134
135 if (info->fSnapshot)
136 {
137 /** @todo Determine the VM snapshot this and set name and description
138 * to the snapshot name/description
139 */
140 CHECK_ERROR(pMedium, COMGETTER(Name)(name.asOutParam()));
141 CHECK_ERROR(pMedium, COMGETTER(Description)(description.asOutParam()));
142 }
143 else
144 {
145 CHECK_ERROR(pMedium, COMGETTER(Name)(name.asOutParam()));
146 CHECK_ERROR(pMedium, COMGETTER(Description)(description.asOutParam()));
147 }
148
149 info->pszName = RTStrDup((char *)CSTR(name));
150 info->pszDescription = RTStrDup((char *)CSTR(description));
151
152 switch(*pType)
153 {
154 case MediumType_Normal:
155 info->pszType = (char *)"normal";
156 break;
157 case MediumType_Immutable:
158 info->pszType = (char *)"immutable";
159 break;
160 case MediumType_Writethrough:
161 info->pszType = (char *)"writethrough";
162 break;
163 case MediumType_Shareable:
164 info->pszType = (char *)"shareable";
165 break;
166 case MediumType_Readonly:
167 info->pszType = (char *)"readonly";
168 break;
169 case MediumType_MultiAttach:
170 info->pszType = (char *)"multiattach";
171 break;
172 default:
173 info->pszType = (char *)"?";
174 }
175
176 switch(*pState)
177 {
178 case MediumState_NotCreated:
179 info->pszState = (char *)"uncreated";
180 break;
181 case MediumState_Created:
182 info->pszState = (char *)"created";
183 break;
184 case MediumState_LockedRead:
185 info->pszState = (char *)"rlock";
186 break;
187 case MediumState_LockedWrite:
188 info->pszState = (char *)"wlock";
189 break;
190 case MediumState_Inaccessible:
191 info->pszState = (char *)"no access";
192 break;
193 case MediumState_Creating:
194 info->pszState = (char *)"creating";
195 break;
196 case MediumState_Deleting:
197 info->pszState = (char *)"deleting";
198 break;
199 default:
200 info->pszState = (char *)"?";
201 }
202 return VINF_SUCCESS;
203}
204
205static void displayMediumInfo(MEDIUMINFO *pInfo, int nestLevel, bool fLast)
206{
207 char *pszSzScaled = vboximgScaledSize(pInfo->cbSize);
208 int cPad = nestLevel * 2;
209 if (g_vboximgOpts.fWide && !g_vboximgOpts.fVerbose)
210 {
211 RTPrintf("%3s %-*s %7s %-9s %9s %-*s %s\n",
212 !fLast ? (pInfo->fSnapshot ? " | " : " +-") : (pInfo->fSnapshot ? " " : " +-"),
213 VM_MAX_NAME, pInfo->fSnapshot ? "+- <snapshot>" : pInfo->pszName,
214 pszSzScaled,
215 pInfo->pszFormat,
216 pInfo->pszState,
217 cPad, "", pInfo->pszUuid);
218 }
219 else
220 {
221 if (!pInfo->fSnapshot)
222 {
223 RTPrintf(" Image: %s\n", pInfo->pszName);
224 if (pInfo->pszDescription && RTStrNLen(pInfo->pszDescription, 256) > 0)
225 RTPrintf("Desc: %s\n", pInfo->pszDescription);
226 RTPrintf(" UUID: %s\n", pInfo->pszUuid);
227 if (g_vboximgOpts.fVerbose)
228 {
229 RTPrintf(" Path: %s\n", pInfo->pszPath);
230 RTPrintf(" Format: %s\n", pInfo->pszFormat);
231 RTPrintf(" Size: %s\n", pszSzScaled);
232 RTPrintf(" State: %s\n", pInfo->pszState);
233 RTPrintf(" Type: %s\n", pInfo->pszType);
234 }
235 RTPrintf("\n");
236 }
237 else
238 {
239 RTPrintf(" Snapshot: %s\n", pInfo->pszUuid);
240 if (g_vboximgOpts.fVerbose)
241 {
242 RTPrintf(" Name: %s\n", pInfo->pszName);
243 RTPrintf(" Desc: %s\n", pInfo->pszDescription);
244 }
245 RTPrintf(" Size: %s\n", pszSzScaled);
246 if (g_vboximgOpts.fVerbose)
247 RTPrintf(" Path: %s\n", pInfo->pszPath);
248 RTPrintf("\n");
249 }
250 }
251 RTMemFree(pszSzScaled);
252}
253
254static int vboximgListBranch(IMachine *pMachine, IMedium *pMedium, uint8_t nestLevel, bool fLast)
255{
256 int rc;
257 MEDIUMINFO *pMediumInfo;
258 rc = getMediumInfo(pMachine, pMedium, &pMediumInfo);
259 if (FAILED(rc))
260 return rc;
261 displayMediumInfo(pMediumInfo, nestLevel, fLast);
262 com::SafeIfaceArray<IMedium> pChildren;
263 CHECK_ERROR_RET(pMedium, COMGETTER(Children)(ComSafeArrayAsOutParam(pChildren)), rc);
264 for (size_t i = 0; i < pChildren.size(); i++)
265 vboximgListBranch(pMachine, pChildren[i], nestLevel + 1, fLast);
266 delete pMediumInfo;
267 return VINF_SUCCESS;
268}
269
270static int
271listMedia(IVirtualBox *pVirtualBox, IMachine *pMachine, char *vmName, char *vmUuid)
272{
273
274 NOREF(pVirtualBox);
275 NOREF(vmName);
276 NOREF(vmUuid);
277
278 int rc = 0;
279 com::SafeIfaceArray<IMediumAttachment> pMediumAttachments;
280
281 CHECK_ERROR(pMachine, COMGETTER(MediumAttachments)(ComSafeArrayAsOutParam(pMediumAttachments)));
282 for (size_t i = 0; i < pMediumAttachments.size(); i++)
283 {
284 bool fLast = (i == pMediumAttachments.size() - 1);
285 DeviceType_T deviceType;
286
287 CHECK_ERROR(pMediumAttachments[i], COMGETTER(Type)(&deviceType));
288 if (deviceType != DeviceType_HardDisk)
289 continue;
290
291 ComPtr<IMedium> pMedium;
292 CHECK_ERROR(pMediumAttachments[i], COMGETTER(Medium)(pMedium.asOutParam()));
293
294 ComPtr<IMedium> pBase;
295 CHECK_ERROR(pMedium, COMGETTER(Base)(pBase.asOutParam()));
296 if (g_vboximgOpts.fWide && !g_vboximgOpts.fVerbose)
297 RTPrintf(" |\n");
298 else
299 RTPrintf("\n");
300 rc = vboximgListBranch(pMachine, pBase, 0, fLast);
301 if (FAILED(rc))
302 {
303 RTPrintf("vboximgListBranch failed %d\n", rc);
304 return rc;
305 }
306
307 }
308 return VINF_SUCCESS;
309}
310/**
311 * Display all registered VMs on the screen with some information about each
312 *
313 * @param virtualBox VirtualBox instance object.
314 */
315int
316vboximgListVMs(IVirtualBox *pVirtualBox)
317{
318 HRESULT rc = 0;
319 com::SafeIfaceArray<IMachine> pMachines;
320 CHECK_ERROR(pVirtualBox, COMGETTER(Machines)(ComSafeArrayAsOutParam(pMachines)));
321 if (g_vboximgOpts.fWide)
322 {
323 RTPrintf("\n");
324 RTPrintf("VM Image Size Type State UUID (hierarchy)\n");
325 }
326 for (size_t i = 0; i < pMachines.size(); ++i)
327 {
328 ComPtr<IMachine> pMachine = pMachines[i];
329 if (pMachine)
330 {
331 BOOL fAccessible;
332 CHECK_ERROR(pMachines[i], COMGETTER(Accessible)(&fAccessible));
333 if (fAccessible)
334 {
335 Bstr machineName;
336 Bstr machineUuid;
337 Bstr description;
338 Bstr machineLocation;
339
340 CHECK_ERROR(pMachine, COMGETTER(Name)(machineName.asOutParam()));
341 CHECK_ERROR(pMachine, COMGETTER(Id)(machineUuid.asOutParam()));
342 CHECK_ERROR(pMachine, COMGETTER(Description)(description.asOutParam()));
343 CHECK_ERROR(pMachine, COMGETTER(SettingsFilePath)(machineLocation.asOutParam()));
344
345
346 if ( g_vboximgOpts.pszVm == NULL
347 || RTStrNCmp(CSTR(machineUuid), g_vboximgOpts.pszVm, MAX_UUID_LEN) == 0
348 || RTStrNCmp((const char *)machineName.raw(), g_vboximgOpts.pszVm, MAX_UUID_LEN) == 0)
349 {
350 if (g_vboximgOpts.fVerbose)
351 {
352 RTPrintf("-----------------------------------------------------------------\n");
353 RTPrintf("VM Name: \"%s\"\n", CSTR(machineName));
354 RTPrintf("UUID: %s\n", CSTR(machineUuid));
355 if (*description.raw() != '\0')
356 RTPrintf("Desc: %s\n", CSTR(description));
357 RTPrintf("Path: %s\n", CSTR(machineLocation));
358 }
359 else
360 {
361 if (g_vboximgOpts.fWide & !g_vboximgOpts.fVerbose)
362 {
363 RTPrintf("----------------------------------------------------------------- "
364 "------------------------------------\n");
365 RTPrintf("%-*s %*s %s\n", VM_MAX_NAME, CSTR(machineName), 33, "", CSTR(machineUuid));
366 }
367 else
368 {
369 RTPrintf("-----------------------------------------------------------------\n");
370 RTPrintf("VM: %s\n", CSTR(machineName));
371 RTPrintf("UUID: %s\n", CSTR(machineUuid));
372 }
373 }
374 rc = listMedia(pVirtualBox, pMachine,
375 RTStrDup(CSTR(machineName)), RTStrDup(CSTR(machineUuid)));
376 RTPrintf("\n");
377 }
378 }
379 }
380 }
381 return rc;
382}
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