VirtualBox

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

Last change on this file since 78331 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • 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 76553 2019-01-01 01:45:53Z vboxsync $ $Revision: 76553 $ */
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 = log2((double)size);
84 char scaledMagnitude = ((char []){ ' ', 'K', 'M', 'G', 'T', 'P' })[exp / 10];
85 /* This workaround is because IPRT RT*Printf* funcs don't handle floating point format specifiers */
86 double cbScaled = (double)size / pow(2, (double)(((uint64_t)(exp / 10)) * 10));
87 uint64_t intPart = cbScaled;
88 uint64_t fracPart = (cbScaled - (double)intPart) * 10;
89 char tmp[256];
90 RTStrPrintf(tmp, sizeof (tmp), "%d.%d%c", intPart, fracPart, scaledMagnitude);
91 return RTStrDup(tmp);
92}
93
94static int
95getMediumInfo(IMachine *pMachine, IMedium *pMedium, MEDIUMINFO **ppMediumInfo)
96{
97 NOREF(pMachine);
98 int rc;
99 MEDIUMINFO *info = new MEDIUMINFO();
100 *ppMediumInfo = info;
101
102 Bstr name;
103 Bstr uuid;
104 Bstr baseUuid;
105 Bstr path;
106 Bstr description;
107 Bstr format;
108 PRInt64 *pSize = &info->cbSize;
109 ComPtr<IMedium> pBase;
110 MediumType_T *pType = &info->type;
111 MediumState_T *pState = &info->state;
112
113 *pState = MediumState_NotCreated;
114
115 CHECK_ERROR(pMedium, RefreshState(pState));
116 CHECK_ERROR(pMedium, COMGETTER(Id)(uuid.asOutParam()));
117 CHECK_ERROR(pMedium, COMGETTER(Base)(pBase.asOutParam()));
118 CHECK_ERROR(pBase, COMGETTER(Id)(baseUuid.asOutParam()));
119
120 CHECK_ERROR(pMedium, COMGETTER(State)(pState));
121
122 CHECK_ERROR(pMedium, COMGETTER(Location)(path.asOutParam()));
123 CHECK_ERROR(pMedium, COMGETTER(Format)(format.asOutParam()));
124 CHECK_ERROR(pMedium, COMGETTER(Type)(pType));
125 CHECK_ERROR(pMedium, COMGETTER(Size)(pSize));
126
127 info->pszUuid = RTStrDup((char *)CSTR(uuid));
128 info->pszBaseUuid = RTStrDup((char *)CSTR(baseUuid));
129 info->pszPath = RTStrDup((char *)CSTR(path));
130 info->pszFormat = RTStrDup((char *)CSTR(format));
131 info->fSnapshot = RTStrCmp(CSTR(uuid), CSTR(baseUuid)) != 0;
132
133 if (info->fSnapshot)
134 {
135 /** @todo Determine the VM snapshot this and set name and description
136 * to the snapshot name/description
137 */
138 CHECK_ERROR(pMedium, COMGETTER(Name)(name.asOutParam()));
139 CHECK_ERROR(pMedium, COMGETTER(Description)(description.asOutParam()));
140 }
141 else
142 {
143 CHECK_ERROR(pMedium, COMGETTER(Name)(name.asOutParam()));
144 CHECK_ERROR(pMedium, COMGETTER(Description)(description.asOutParam()));
145 }
146
147 info->pszName = RTStrDup((char *)CSTR(name));
148 info->pszDescription = RTStrDup((char *)CSTR(description));
149
150 switch(*pType)
151 {
152 case MediumType_Normal:
153 info->pszType = (char *)"normal";
154 break;
155 case MediumType_Immutable:
156 info->pszType = (char *)"immutable";
157 break;
158 case MediumType_Writethrough:
159 info->pszType = (char *)"writethrough";
160 break;
161 case MediumType_Shareable:
162 info->pszType = (char *)"shareable";
163 break;
164 case MediumType_Readonly:
165 info->pszType = (char *)"readonly";
166 break;
167 case MediumType_MultiAttach:
168 info->pszType = (char *)"multiattach";
169 break;
170 default:
171 info->pszType = (char *)"?";
172 }
173
174 switch(*pState)
175 {
176 case MediumState_NotCreated:
177 info->pszState = (char *)"uncreated";
178 break;
179 case MediumState_Created:
180 info->pszState = (char *)"created";
181 break;
182 case MediumState_LockedRead:
183 info->pszState = (char *)"rlock";
184 break;
185 case MediumState_LockedWrite:
186 info->pszState = (char *)"wlock";
187 break;
188 case MediumState_Inaccessible:
189 info->pszState = (char *)"no access";
190 break;
191 case MediumState_Creating:
192 info->pszState = (char *)"creating";
193 break;
194 case MediumState_Deleting:
195 info->pszState = (char *)"deleting";
196 break;
197 default:
198 info->pszState = (char *)"?";
199 }
200 return VINF_SUCCESS;
201}
202
203static void displayMediumInfo(MEDIUMINFO *pInfo, int nestLevel, bool fLast)
204{
205
206 char *cbScaled = vboximgScaledSize(pInfo->cbSize);
207 int cPad = nestLevel * 2;
208 if (g_vboximgOpts.fWide && !g_vboximgOpts.fVerbose)
209 {
210 RTPrintf("%3s %-*s %7s %-9s %9s %-*s %s\n",
211 !fLast ? (pInfo->fSnapshot ? " | " : " +-") : (pInfo->fSnapshot ? " " : " +-"),
212 VM_MAX_NAME, pInfo->fSnapshot ? "+- <snapshot>" : pInfo->pszName,
213 cbScaled,
214 pInfo->pszFormat,
215 pInfo->pszState,
216 cPad, "", pInfo->pszUuid);
217 RTMemFree(cbScaled);
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", cbScaled);
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", cbScaled);
246 if (g_vboximgOpts.fVerbose)
247 RTPrintf(" Path: %s\n", pInfo->pszPath);
248 RTPrintf("\n");
249 }
250 }
251}
252
253static int vboximgListBranch(IMachine *pMachine, IMedium *pMedium, uint8_t nestLevel, bool fLast)
254{
255 int rc;
256 MEDIUMINFO *pMediumInfo;
257 rc = getMediumInfo(pMachine, pMedium, &pMediumInfo);
258 if (FAILED(rc))
259 return rc;
260 displayMediumInfo(pMediumInfo, nestLevel, fLast);
261 com::SafeIfaceArray<IMedium> pChildren;
262 CHECK_ERROR_RET(pMedium, COMGETTER(Children)(ComSafeArrayAsOutParam(pChildren)), rc);
263 for (size_t i = 0; i < pChildren.size(); i++)
264 vboximgListBranch(pMachine, pChildren[i], nestLevel + 1, fLast);
265 delete pMediumInfo;
266 return VINF_SUCCESS;
267}
268
269static int
270listMedia(IVirtualBox *pVirtualBox, IMachine *pMachine, char *vmName, char *vmUuid)
271{
272
273 NOREF(pVirtualBox);
274 NOREF(vmName);
275 NOREF(vmUuid);
276
277 int rc = 0;
278 com::SafeIfaceArray<IMediumAttachment> pMediumAttachments;
279
280 CHECK_ERROR(pMachine, COMGETTER(MediumAttachments)(ComSafeArrayAsOutParam(pMediumAttachments)));
281 for (size_t i = 0; i < pMediumAttachments.size(); i++)
282 {
283 bool fLast = (i == pMediumAttachments.size() - 1);
284 DeviceType_T deviceType;
285
286 CHECK_ERROR(pMediumAttachments[i], COMGETTER(Type)(&deviceType));
287 if (deviceType != DeviceType_HardDisk)
288 continue;
289
290 ComPtr<IMedium> pMedium;
291 CHECK_ERROR(pMediumAttachments[i], COMGETTER(Medium)(pMedium.asOutParam()));
292
293 ComPtr<IMedium> pBase;
294 CHECK_ERROR(pMedium, COMGETTER(Base)(pBase.asOutParam()));
295 if (g_vboximgOpts.fWide && !g_vboximgOpts.fVerbose)
296 RTPrintf(" |\n");
297 else
298 RTPrintf("\n");
299 rc = vboximgListBranch(pMachine, pBase, 0, fLast);
300 if (FAILED(rc))
301 {
302 RTPrintf("vboximgListBranch failed %d\n", rc);
303 return rc;
304 }
305
306 }
307 return VINF_SUCCESS;
308}
309/**
310 * Display all registered VMs on the screen with some information about each
311 *
312 * @param virtualBox VirtualBox instance object.
313 */
314int
315vboximgListVMs(IVirtualBox *pVirtualBox)
316{
317 HRESULT rc = 0;
318 com::SafeIfaceArray<IMachine> pMachines;
319 CHECK_ERROR(pVirtualBox, COMGETTER(Machines)(ComSafeArrayAsOutParam(pMachines)));
320 if (g_vboximgOpts.fWide)
321 {
322 RTPrintf("\n");
323 RTPrintf("VM Image Size Type State UUID (hierarchy)\n");
324 }
325 for (size_t i = 0; i < pMachines.size(); ++i)
326 {
327 ComPtr<IMachine> pMachine = pMachines[i];
328 if (pMachine)
329 {
330 BOOL fAccessible;
331 CHECK_ERROR(pMachines[i], COMGETTER(Accessible)(&fAccessible));
332 if (fAccessible)
333 {
334 Bstr machineName;
335 Bstr machineUuid;
336 Bstr description;
337 Bstr machineLocation;
338
339 CHECK_ERROR(pMachine, COMGETTER(Name)(machineName.asOutParam()));
340 CHECK_ERROR(pMachine, COMGETTER(Id)(machineUuid.asOutParam()));
341 CHECK_ERROR(pMachine, COMGETTER(Description)(description.asOutParam()));
342 CHECK_ERROR(pMachine, COMGETTER(SettingsFilePath)(machineLocation.asOutParam()));
343
344
345 if ( g_vboximgOpts.pszVm == NULL
346 || RTStrNCmp(CSTR(machineUuid), g_vboximgOpts.pszVm, MAX_UUID_LEN) == 0
347 || RTStrNCmp((const char *)machineName.raw(), g_vboximgOpts.pszVm, MAX_UUID_LEN) == 0)
348 {
349 if (g_vboximgOpts.fVerbose)
350 {
351 RTPrintf("-----------------------------------------------------------------\n");
352 RTPrintf("VM Name: \"%s\"\n", CSTR(machineName));
353 RTPrintf("UUID: %s\n", CSTR(machineUuid));
354 if (*description.raw() != '\0')
355 RTPrintf("Desc: %s\n", CSTR(description));
356 RTPrintf("Path: %s\n", CSTR(machineLocation));
357 }
358 else
359 {
360 if (g_vboximgOpts.fWide & !g_vboximgOpts.fVerbose)
361 {
362 RTPrintf("----------------------------------------------------------------- "
363 "------------------------------------\n");
364 RTPrintf("%-*s %*s %s\n", VM_MAX_NAME, CSTR(machineName), 33, "", CSTR(machineUuid));
365 }
366 else
367 {
368 RTPrintf("-----------------------------------------------------------------\n");
369 RTPrintf("VM: %s\n", CSTR(machineName));
370 RTPrintf("UUID: %s\n", CSTR(machineUuid));
371 }
372 }
373 rc = listMedia(pVirtualBox, pMachine,
374 RTStrDup(CSTR(machineName)), RTStrDup(CSTR(machineUuid)));
375 RTPrintf("\n");
376 }
377 }
378 }
379 }
380 return rc;
381}
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