1 | /* $Id: vboximgMedia.cpp 80511 2019-08-30 11:03:01Z vboxsync $ $Revision: 80511 $ */
|
---|
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 |
|
---|
47 | using namespace com;
|
---|
48 |
|
---|
49 | extern 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 |
|
---|
56 | typedef 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 |
|
---|
81 | char *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 |
|
---|
94 | static int
|
---|
95 | getMediumInfo(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 |
|
---|
203 | static void displayMediumInfo(MEDIUMINFO *pInfo, int nestLevel, bool fLast)
|
---|
204 | {
|
---|
205 | char *pszSzScaled = vboximgScaledSize(pInfo->cbSize);
|
---|
206 | int cPad = nestLevel * 2;
|
---|
207 | if (g_vboximgOpts.fWide && !g_vboximgOpts.fVerbose)
|
---|
208 | {
|
---|
209 | RTPrintf("%3s %-*s %7s %-9s %9s %-*s %s\n",
|
---|
210 | !fLast ? (pInfo->fSnapshot ? " | " : " +-") : (pInfo->fSnapshot ? " " : " +-"),
|
---|
211 | VM_MAX_NAME, pInfo->fSnapshot ? "+- <snapshot>" : pInfo->pszName,
|
---|
212 | pszSzScaled,
|
---|
213 | pInfo->pszFormat,
|
---|
214 | pInfo->pszState,
|
---|
215 | cPad, "", pInfo->pszUuid);
|
---|
216 | }
|
---|
217 | else
|
---|
218 | {
|
---|
219 | if (!pInfo->fSnapshot)
|
---|
220 | {
|
---|
221 | RTPrintf(" Image: %s\n", pInfo->pszName);
|
---|
222 | if (pInfo->pszDescription && RTStrNLen(pInfo->pszDescription, 256) > 0)
|
---|
223 | RTPrintf("Desc: %s\n", pInfo->pszDescription);
|
---|
224 | RTPrintf(" UUID: %s\n", pInfo->pszUuid);
|
---|
225 | if (g_vboximgOpts.fVerbose)
|
---|
226 | {
|
---|
227 | RTPrintf(" Path: %s\n", pInfo->pszPath);
|
---|
228 | RTPrintf(" Format: %s\n", pInfo->pszFormat);
|
---|
229 | RTPrintf(" Size: %s\n", pszSzScaled);
|
---|
230 | RTPrintf(" State: %s\n", pInfo->pszState);
|
---|
231 | RTPrintf(" Type: %s\n", pInfo->pszType);
|
---|
232 | }
|
---|
233 | RTPrintf("\n");
|
---|
234 | }
|
---|
235 | else
|
---|
236 | {
|
---|
237 | RTPrintf(" Snapshot: %s\n", pInfo->pszUuid);
|
---|
238 | if (g_vboximgOpts.fVerbose)
|
---|
239 | {
|
---|
240 | RTPrintf(" Name: %s\n", pInfo->pszName);
|
---|
241 | RTPrintf(" Desc: %s\n", pInfo->pszDescription);
|
---|
242 | }
|
---|
243 | RTPrintf(" Size: %s\n", pszSzScaled);
|
---|
244 | if (g_vboximgOpts.fVerbose)
|
---|
245 | RTPrintf(" Path: %s\n", pInfo->pszPath);
|
---|
246 | RTPrintf("\n");
|
---|
247 | }
|
---|
248 | }
|
---|
249 | RTMemFree(pszSzScaled);
|
---|
250 | }
|
---|
251 |
|
---|
252 | static int vboximgListBranch(IMachine *pMachine, IMedium *pMedium, uint8_t nestLevel, bool fLast)
|
---|
253 | {
|
---|
254 | int rc;
|
---|
255 | MEDIUMINFO *pMediumInfo;
|
---|
256 | rc = getMediumInfo(pMachine, pMedium, &pMediumInfo);
|
---|
257 | if (FAILED(rc))
|
---|
258 | return rc;
|
---|
259 | displayMediumInfo(pMediumInfo, nestLevel, fLast);
|
---|
260 | com::SafeIfaceArray<IMedium> pChildren;
|
---|
261 | CHECK_ERROR_RET(pMedium, COMGETTER(Children)(ComSafeArrayAsOutParam(pChildren)), rc);
|
---|
262 | for (size_t i = 0; i < pChildren.size(); i++)
|
---|
263 | vboximgListBranch(pMachine, pChildren[i], nestLevel + 1, fLast);
|
---|
264 | delete pMediumInfo;
|
---|
265 | return VINF_SUCCESS;
|
---|
266 | }
|
---|
267 |
|
---|
268 | static int
|
---|
269 | listMedia(IVirtualBox *pVirtualBox, IMachine *pMachine, char *vmName, char *vmUuid)
|
---|
270 | {
|
---|
271 |
|
---|
272 | NOREF(pVirtualBox);
|
---|
273 | NOREF(vmName);
|
---|
274 | NOREF(vmUuid);
|
---|
275 |
|
---|
276 | int rc = 0;
|
---|
277 | com::SafeIfaceArray<IMediumAttachment> pMediumAttachments;
|
---|
278 |
|
---|
279 | CHECK_ERROR(pMachine, COMGETTER(MediumAttachments)(ComSafeArrayAsOutParam(pMediumAttachments)));
|
---|
280 | for (size_t i = 0; i < pMediumAttachments.size(); i++)
|
---|
281 | {
|
---|
282 | bool fLast = (i == pMediumAttachments.size() - 1);
|
---|
283 | DeviceType_T deviceType;
|
---|
284 |
|
---|
285 | CHECK_ERROR(pMediumAttachments[i], COMGETTER(Type)(&deviceType));
|
---|
286 | if (deviceType != DeviceType_HardDisk)
|
---|
287 | continue;
|
---|
288 |
|
---|
289 | ComPtr<IMedium> pMedium;
|
---|
290 | CHECK_ERROR(pMediumAttachments[i], COMGETTER(Medium)(pMedium.asOutParam()));
|
---|
291 |
|
---|
292 | ComPtr<IMedium> pBase;
|
---|
293 | CHECK_ERROR(pMedium, COMGETTER(Base)(pBase.asOutParam()));
|
---|
294 | if (g_vboximgOpts.fWide && !g_vboximgOpts.fVerbose)
|
---|
295 | RTPrintf(" |\n");
|
---|
296 | else
|
---|
297 | RTPrintf("\n");
|
---|
298 | rc = vboximgListBranch(pMachine, pBase, 0, fLast);
|
---|
299 | if (FAILED(rc))
|
---|
300 | {
|
---|
301 | RTPrintf("vboximgListBranch failed %d\n", rc);
|
---|
302 | return rc;
|
---|
303 | }
|
---|
304 |
|
---|
305 | }
|
---|
306 | return VINF_SUCCESS;
|
---|
307 | }
|
---|
308 | /**
|
---|
309 | * Display all registered VMs on the screen with some information about each
|
---|
310 | *
|
---|
311 | * @param virtualBox VirtualBox instance object.
|
---|
312 | */
|
---|
313 | int
|
---|
314 | vboximgListVMs(IVirtualBox *pVirtualBox)
|
---|
315 | {
|
---|
316 | HRESULT rc = 0;
|
---|
317 | com::SafeIfaceArray<IMachine> pMachines;
|
---|
318 | CHECK_ERROR(pVirtualBox, COMGETTER(Machines)(ComSafeArrayAsOutParam(pMachines)));
|
---|
319 | if (g_vboximgOpts.fWide)
|
---|
320 | {
|
---|
321 | RTPrintf("\n");
|
---|
322 | RTPrintf("VM Image Size Type State UUID (hierarchy)\n");
|
---|
323 | }
|
---|
324 | for (size_t i = 0; i < pMachines.size(); ++i)
|
---|
325 | {
|
---|
326 | ComPtr<IMachine> pMachine = pMachines[i];
|
---|
327 | if (pMachine)
|
---|
328 | {
|
---|
329 | BOOL fAccessible;
|
---|
330 | CHECK_ERROR(pMachines[i], COMGETTER(Accessible)(&fAccessible));
|
---|
331 | if (fAccessible)
|
---|
332 | {
|
---|
333 | Bstr machineName;
|
---|
334 | Bstr machineUuid;
|
---|
335 | Bstr description;
|
---|
336 | Bstr machineLocation;
|
---|
337 |
|
---|
338 | CHECK_ERROR(pMachine, COMGETTER(Name)(machineName.asOutParam()));
|
---|
339 | CHECK_ERROR(pMachine, COMGETTER(Id)(machineUuid.asOutParam()));
|
---|
340 | CHECK_ERROR(pMachine, COMGETTER(Description)(description.asOutParam()));
|
---|
341 | CHECK_ERROR(pMachine, COMGETTER(SettingsFilePath)(machineLocation.asOutParam()));
|
---|
342 |
|
---|
343 |
|
---|
344 | if ( g_vboximgOpts.pszVm == NULL
|
---|
345 | || RTStrNCmp(CSTR(machineUuid), g_vboximgOpts.pszVm, MAX_UUID_LEN) == 0
|
---|
346 | || RTStrNCmp((const char *)machineName.raw(), g_vboximgOpts.pszVm, MAX_UUID_LEN) == 0)
|
---|
347 | {
|
---|
348 | if (g_vboximgOpts.fVerbose)
|
---|
349 | {
|
---|
350 | RTPrintf("-----------------------------------------------------------------\n");
|
---|
351 | RTPrintf("VM Name: \"%s\"\n", CSTR(machineName));
|
---|
352 | RTPrintf("UUID: %s\n", CSTR(machineUuid));
|
---|
353 | if (*description.raw() != '\0')
|
---|
354 | RTPrintf("Desc: %s\n", CSTR(description));
|
---|
355 | RTPrintf("Path: %s\n", CSTR(machineLocation));
|
---|
356 | }
|
---|
357 | else
|
---|
358 | {
|
---|
359 | if (g_vboximgOpts.fWide & !g_vboximgOpts.fVerbose)
|
---|
360 | {
|
---|
361 | RTPrintf("----------------------------------------------------------------- "
|
---|
362 | "------------------------------------\n");
|
---|
363 | RTPrintf("%-*s %*s %s\n", VM_MAX_NAME, CSTR(machineName), 33, "", CSTR(machineUuid));
|
---|
364 | }
|
---|
365 | else
|
---|
366 | {
|
---|
367 | RTPrintf("-----------------------------------------------------------------\n");
|
---|
368 | RTPrintf("VM: %s\n", CSTR(machineName));
|
---|
369 | RTPrintf("UUID: %s\n", CSTR(machineUuid));
|
---|
370 | }
|
---|
371 | }
|
---|
372 | rc = listMedia(pVirtualBox, pMachine,
|
---|
373 | RTStrDup(CSTR(machineName)), RTStrDup(CSTR(machineUuid)));
|
---|
374 | RTPrintf("\n");
|
---|
375 | }
|
---|
376 | }
|
---|
377 | }
|
---|
378 | }
|
---|
379 | return rc;
|
---|
380 | }
|
---|