1 | /* $Id: vboximg-mount.cpp 76043 2018-12-07 09:28:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * vboximg-mount - Disk Image Flattening FUSE Program.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2018 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 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 |
|
---|
23 | #define LOG_GROUP LOG_GROUP_DEFAULT /** @todo log group */
|
---|
24 |
|
---|
25 | #define FUSE_USE_VERSION 27
|
---|
26 | #if defined(RT_OS_DARWIN) || defined(RT_OS_LINUX) || defined(RT_OS_FEEBSD)
|
---|
27 | # define UNIX_DERIVATIVE
|
---|
28 | #endif
|
---|
29 | #define MAX_READERS (INT32_MAX / 32)
|
---|
30 | #include <fuse.h>
|
---|
31 | #ifdef UNIX_DERIVATIVE
|
---|
32 | #include <errno.h>
|
---|
33 | #include <fcntl.h>
|
---|
34 | #include <stdlib.h>
|
---|
35 | #include <libgen.h>
|
---|
36 | #include <unistd.h>
|
---|
37 | #include <math.h>
|
---|
38 | //#include <stdarg.h>
|
---|
39 | #include <cstdarg>
|
---|
40 | #include <sys/stat.h>
|
---|
41 | #endif
|
---|
42 | #if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD) || defined(RT_OS_LINUX)
|
---|
43 | # include <sys/param.h>
|
---|
44 | # undef PVM /* Blasted old BSD mess still hanging around darwin. */
|
---|
45 | #endif
|
---|
46 | #ifdef RT_OS_LINUX
|
---|
47 | # include <linux/fs.h>
|
---|
48 | # include <linux/hdreg.h>
|
---|
49 | #endif
|
---|
50 | #include <VirtualBox_XPCOM.h>
|
---|
51 | #include <VBox/com/VirtualBox.h>
|
---|
52 | #include <VBox/vd.h>
|
---|
53 | #include <VBox/vd-ifs.h>
|
---|
54 | #include <VBox/log.h>
|
---|
55 | #include <VBox/err.h>
|
---|
56 | #include <VBox/com/ErrorInfo.h>
|
---|
57 | #include <VBox/com/NativeEventQueue.h>
|
---|
58 | #include <VBox/com/com.h>
|
---|
59 | #include <VBox/com/string.h>
|
---|
60 | #include <VBox/com/Guid.h>
|
---|
61 | #include <VBox/com/array.h>
|
---|
62 | #include <VBox/com/errorprint.h>
|
---|
63 |
|
---|
64 | #include <iprt/initterm.h>
|
---|
65 | #include <iprt/assert.h>
|
---|
66 | #include <iprt/message.h>
|
---|
67 | #include <iprt/critsect.h>
|
---|
68 | #include <iprt/asm.h>
|
---|
69 | #include <iprt/mem.h>
|
---|
70 | #include <iprt/string.h>
|
---|
71 | #include <iprt/initterm.h>
|
---|
72 | #include <iprt/stream.h>
|
---|
73 | #include <iprt/types.h>
|
---|
74 | #include <iprt/path.h>
|
---|
75 | #include <iprt/utf16.h>
|
---|
76 |
|
---|
77 | #include "vboximg-mount.h"
|
---|
78 | #include "SelfSizingTable.h"
|
---|
79 |
|
---|
80 | using namespace com;
|
---|
81 |
|
---|
82 | enum {
|
---|
83 | USAGE_FLAG,
|
---|
84 | };
|
---|
85 |
|
---|
86 | /* For getting the basename of the image path */
|
---|
87 | union
|
---|
88 | {
|
---|
89 | RTPATHSPLIT split;
|
---|
90 | uint8_t abPad[RTPATH_MAX + 1024];
|
---|
91 | } g_u;
|
---|
92 |
|
---|
93 | #if 0 /* unused */
|
---|
94 | const uint64_t KB = 1024;
|
---|
95 | const uint64_t MB = KB * KB;
|
---|
96 | const uint64_t GB = MB * KB;
|
---|
97 | const uint64_t TB = GB * KB;
|
---|
98 | const uint64_t PB = TB * KB;
|
---|
99 | #endif
|
---|
100 |
|
---|
101 | enum { PARTITION_TABLE_MBR = 1, PARTITION_TABLE_GPT = 2 };
|
---|
102 |
|
---|
103 | #define GPT_PTABLE_SIZE 32 * BLOCKSIZE /** Max size we to read for GPT partition table */
|
---|
104 | #define MBR_PARTITIONS_MAX 4 /** Fixed number of partitions in Master Boot Record */
|
---|
105 | #define BASENAME_MAX 256 /** Maximum name for the basename of a path (for RTStrNLen()*/
|
---|
106 | #define VBOXIMG_PARTITION_MAX 256 /** How much storage to allocate to store partition info */
|
---|
107 | #define PARTITION_NAME_MAX 72 /** Maximum partition name size (accomodates GPT partition name) */
|
---|
108 | #define BLOCKSIZE 512 /** Commonly used disk block size */
|
---|
109 | #define DOS_BOOT_RECORD_SIGNATURE 0xaa55 /** MBR and EBR (partition table) signature [EOT boundary] */
|
---|
110 | #define NULL_BOOT_RECORD_SIGNATURE 0x0000 /** MBR or EBR null signature value */
|
---|
111 | #define MAX_UUID_LEN 256 /** Max length of a UUID */
|
---|
112 | #define LBA(n) (n * BLOCKSIZE)
|
---|
113 | #define VD_SECTOR_SIZE 512 /** Virtual disk sector/blocksize */
|
---|
114 | #define VD_SECTOR_MASK (VD_SECTOR_SIZE - 1) /** Masks off a blocks worth of data */
|
---|
115 | #define VD_SECTOR_OUT_OF_BOUNDS_MASK (~UINT64_C(VD_SECTOR_MASK)) /** Masks the overflow of a blocks worth of data */
|
---|
116 |
|
---|
117 | #define IS_BIG_ENDIAN (*(uint16_t *)"\0\xff" < 0x100)
|
---|
118 |
|
---|
119 | #define PARTTYPE_IS_NULL(parType) ((uint8_t)parType == 0x00)
|
---|
120 | #define PARTTYPE_IS_GPT(parType) ((uint8_t)parType == 0xee)
|
---|
121 | #define PARTTYPE_IS_EXT(parType) (( (uint8_t)parType) == 0x05 /* Extended */ \
|
---|
122 | || ((uint8_t)parType) == 0x0f /* W95 Extended (LBA) */ \
|
---|
123 | || ((uint8_t)parType) == 0x85) /* Linux Extended */
|
---|
124 |
|
---|
125 | #define SAFENULL(strPtr) (strPtr ? strPtr : "")
|
---|
126 | #define CSTR(arg) Utf8Str(arg).c_str() /* Converts XPCOM string type to C string type */
|
---|
127 |
|
---|
128 | static struct fuse_operations g_vboximgOps; /** FUSE structure that defines allowed ops for this FS */
|
---|
129 |
|
---|
130 | /* Global variables */
|
---|
131 |
|
---|
132 | static PVDISK g_pVDisk; /** Handle for Virtual Disk in contet */
|
---|
133 | static char *g_pvDiskUuid; /** UUID of image (if known, otherwise NULL) */
|
---|
134 | static off_t g_vDiskOffset; /** Biases r/w from start of VD */
|
---|
135 | static off_t g_vDiskSize; /** Limits r/w length for VD */
|
---|
136 | static int32_t g_cReaders; /** Number of readers for VD */
|
---|
137 | static int32_t g_cWriters; /** Number of writers for VD */
|
---|
138 | static RTFOFF g_cbEntireVDisk; /** Size of VD */
|
---|
139 | static char *g_pszBaseImageName; /** Base filename for current VD image */
|
---|
140 | static char *g_pszBaseImagePath; /** Full path to current VD image */
|
---|
141 | static PVDINTERFACE g_pVdIfs; /** @todo Remove when VD I/O becomes threadsafe */
|
---|
142 | static VDINTERFACETHREADSYNC g_VDIfThreadSync; /** @todo Remove when VD I/O becomes threadsafe */
|
---|
143 | static RTCRITSECT g_vdioLock; /** @todo Remove when VD I/O becomes threadsafe */
|
---|
144 | static uint16_t g_lastPartNbr; /** Last partition number found in MBR + EBR chain */
|
---|
145 | static bool g_fGPT; /** True if GPT type partition table was found */
|
---|
146 |
|
---|
147 | /* Table entry containing partition info parsed out of GPT or MBR and EBR chain of specified VD */
|
---|
148 |
|
---|
149 | typedef struct
|
---|
150 | {
|
---|
151 | int idxPartition; /** partition number */
|
---|
152 | char *pszName;
|
---|
153 | off_t offPartition; /** partition offset from start of disk, in bytes */
|
---|
154 | uint64_t cbPartition; /** partition size in bytes */
|
---|
155 | uint8_t fBootable; /** partition bootable */
|
---|
156 | union
|
---|
157 | {
|
---|
158 | uint8_t legacy; /** partition type MBR/EBR */
|
---|
159 | uint128_t gptGuidTypeSpecifier; /** partition type GPT */
|
---|
160 | } partitionType; /** uint8_t for MBR/EBR (legacy) and GUID for GPT */
|
---|
161 | union
|
---|
162 | {
|
---|
163 | MBRPARTITIONENTRY mbrEntry; /** MBR (also EBR partition entry) */
|
---|
164 | GPTPARTITIONENTRY gptEntry; /** GPT partition entry */
|
---|
165 | } partitionEntry;
|
---|
166 | } PARTITIONINFO;
|
---|
167 |
|
---|
168 | PARTITIONINFO g_aParsedPartitionInfo[VBOXIMG_PARTITION_MAX + 1]; /* Note: Element 0 reserved for EntireDisk partitionEntry */
|
---|
169 |
|
---|
170 | static struct vboximgOpts {
|
---|
171 | char *pszVm; /** optional VM UUID */
|
---|
172 | char *pszImage; /** Virtual Disk image UUID or path */
|
---|
173 | int32_t idxPartition; /** Number of partition to constrain FUSE based FS to (optional) 0 - whole disk*/
|
---|
174 | int32_t offset; /** Offset to base virtual disk reads and writes from (altnerative to partition) */
|
---|
175 | int32_t size; /** Size of accessible disk region, starting at offset, default = offset 0 */
|
---|
176 | uint32_t cHddImageDiffMax; /** Max number of differencing images (snapshots) to apply to image */
|
---|
177 | uint32_t fListMediaLong; /** Flag to list virtual disks of all known VMs */
|
---|
178 | uint32_t fList; /** Flag to list virtual disks of all known VMs */
|
---|
179 | uint32_t fListParts; /** Flag to summarily list partitions associated with pszImage */
|
---|
180 | uint32_t fAllowRoot; /** Flag to allow root to access this FUSE FS */
|
---|
181 | uint32_t fRW; /** Flag to allow changes to FUSE-mounted Virtual Disk image */
|
---|
182 | uint32_t fBriefUsage; /** Flag to display only FS-specific program usage options */
|
---|
183 | uint32_t fVerbose; /** Make some noise */
|
---|
184 | } g_vboximgOpts;
|
---|
185 |
|
---|
186 | #define OPTION(fmt, pos, val) { fmt, offsetof(struct vboximgOpts, pos), val }
|
---|
187 |
|
---|
188 | static struct fuse_opt vboximgOptDefs[] = {
|
---|
189 | OPTION("-l", fList, 1),
|
---|
190 | OPTION("--list", fList, 1),
|
---|
191 | OPTION("--root", fAllowRoot, 1),
|
---|
192 | OPTION("--vm=%s", pszVm, 0),
|
---|
193 | OPTION("--maxdiff=%d", cHddImageDiffMax, 1),
|
---|
194 | OPTION("--diff=%d", cHddImageDiffMax, 1),
|
---|
195 | OPTION("--partition=%d", idxPartition, 1),
|
---|
196 | OPTION("-p %d", idxPartition, 1),
|
---|
197 | OPTION("--offset=%d", offset, 1),
|
---|
198 | OPTION("-o %d", offset, 1),
|
---|
199 | OPTION("--size=%d", size, 1),
|
---|
200 | OPTION("-s %d", size, 1),
|
---|
201 | OPTION("--image=%s", pszImage, 0),
|
---|
202 | OPTION("-i %s", pszImage, 0),
|
---|
203 | OPTION("--rw", fRW, 1),
|
---|
204 | OPTION("--verbose", fVerbose, 1),
|
---|
205 | OPTION("-v", fVerbose, 1),
|
---|
206 | OPTION("-h", fBriefUsage, 1),
|
---|
207 | FUSE_OPT_KEY("--help", USAGE_FLAG),
|
---|
208 | FUSE_OPT_END
|
---|
209 | };
|
---|
210 |
|
---|
211 | static void
|
---|
212 | briefUsage()
|
---|
213 | {
|
---|
214 | RTPrintf("usage: vboximg-mount [options] <mountpoint>\n\n"
|
---|
215 | "vboximg-mount options:\n\n"
|
---|
216 | " [ { -l | --list } ] If a disk image is specified [-i, --image], list partition table\n"
|
---|
217 | " for the specified disk image.\n"
|
---|
218 | "\n"
|
---|
219 | " If no image is specified on the command line, list registered VMs\n"
|
---|
220 | " and their disk media. If a verbose flag is specified,\n"
|
---|
221 | " VM and media list will be long format, including snapshot images\n"
|
---|
222 | " and component locations (e.g. paths).\n"
|
---|
223 | "\n"
|
---|
224 | " [ { -i | --image= } <UUID | name | path> ] Virtual Box disk image to use\n"
|
---|
225 | "\n"
|
---|
226 | " [ { -p | --partition= } <partition #> ] Mount specified partition number via FUSE\n"
|
---|
227 | "\n"
|
---|
228 | " [ { -o | --offset= } <byte #> ] Disk I/O will be based on offset from disk start\n"
|
---|
229 | " (Can't use with -p or --partition options)\n"
|
---|
230 | "\n"
|
---|
231 | " [ -s | --size=<bytes> ] Sets size of mounted disk from disk start or from\n"
|
---|
232 | " offset, if specified. (Can't use with\n"
|
---|
233 | " -p or --partition options)\n"
|
---|
234 | "\n"
|
---|
235 | " [ --diff=<diff #> ] Apply diffs (snapshot differencing disk images)\n"
|
---|
236 | " to specified base disk image up to and including\n"
|
---|
237 | " specified diff number.\n"
|
---|
238 | " (0 = Apply no diffs, default = Apply all diffs)\n"
|
---|
239 | "\n"
|
---|
240 | " [ --rw ] Make image writeable (default = readonly)\n"
|
---|
241 | " [ --root ] Same as -o allow_root\n"
|
---|
242 | "\n"
|
---|
243 | " [ --vm < Path | UUID >] VM UUID (limit media list to specific VM)\n"
|
---|
244 | "\n"
|
---|
245 | " [ --verbose] Log extra information\n"
|
---|
246 | " -o opt[,opt...] FUSE mount options\n"
|
---|
247 | " -h Display short usage info showing only the above\n"
|
---|
248 | " -? Display short usage info showing only the above\n"
|
---|
249 | " --help Display long usage info (including FUSE opts)\n\n"
|
---|
250 | );
|
---|
251 | RTPrintf("\n");
|
---|
252 | RTPrintf("When successful, the --image option creates a one-directory-deep filesystem \n");
|
---|
253 | RTPrintf("rooted at the specified mountpoint. The contents of the directory will be \n");
|
---|
254 | RTPrintf("a symbolic link with the base name of the image file pointing to the path of\n");
|
---|
255 | RTPrintf("the virtual disk image, and a regular file named 'vhdd', which represents\n");
|
---|
256 | RTPrintf("the byte stream of the disk image as interpreted by VirtualBox.\n");
|
---|
257 | RTPrintf("It is the vhdd file that the user or a utility will subsequently mount on\n");
|
---|
258 | RTPrintf("the host OS to gain access to the virtual disk contents.\n\n");
|
---|
259 | RTPrintf("If any of the partition, size or offset related options are used, the\n");
|
---|
260 | RTPrintf("constraining start offset (in bytes) and size (in bytes) will be\n");
|
---|
261 | RTPrintf("appended in brackets to the symbolic link basename to indicate\n");
|
---|
262 | RTPrintf("which part of the image is exposed by the FUSE filesystem implementation.\n\n");
|
---|
263 | }
|
---|
264 |
|
---|
265 | static int
|
---|
266 | vboximgOptHandler(void *data, const char *arg, int optKey, struct fuse_args *outargs)
|
---|
267 | {
|
---|
268 | (void) data;
|
---|
269 | (void) arg;
|
---|
270 | switch(optKey)
|
---|
271 | {
|
---|
272 | case USAGE_FLAG:
|
---|
273 | briefUsage();
|
---|
274 | fuse_opt_add_arg(outargs, "-ho");
|
---|
275 | fuse_main(outargs->argc, outargs->argv, &g_vboximgOps, NULL);
|
---|
276 | break;
|
---|
277 | }
|
---|
278 | return 1;
|
---|
279 | }
|
---|
280 |
|
---|
281 | /** @copydoc fuse_operations::open */
|
---|
282 | static int vboximgOp_open(const char *pszPath, struct fuse_file_info *pInfo)
|
---|
283 | {
|
---|
284 | RT_NOREF(pszPath, pInfo);
|
---|
285 | LogFlowFunc(("pszPath=%s\n", pszPath));
|
---|
286 | uint32_t notsup = 0;
|
---|
287 | int rc = 0;
|
---|
288 |
|
---|
289 | #ifdef UNIX_DERIVATIVE
|
---|
290 | # ifdef RT_OS_DARWIN
|
---|
291 | notsup = O_APPEND | O_NONBLOCK | O_SYMLINK | O_NOCTTY | O_SHLOCK | O_EXLOCK |
|
---|
292 | O_ASYNC | O_CREAT | O_TRUNC | O_EXCL | O_EVTONLY;
|
---|
293 | # elif defined(RT_OS_LINUX)
|
---|
294 | notsup = O_APPEND | O_ASYNC | O_DIRECT | O_NOATIME | O_NOCTTY | O_NOFOLLOW | O_NONBLOCK;
|
---|
295 | /* | O_LARGEFILE | O_SYNC | ? */
|
---|
296 | # elif defined(RT_OS_FREEBSD)
|
---|
297 | notsup = O_APPEND | O_ASYNC | O_DIRECT | O_NOCTTY | O_NOFOLLOW | O_NONBLOCK;
|
---|
298 | /* | O_LARGEFILE | O_SYNC | ? */
|
---|
299 | # endif
|
---|
300 | #else
|
---|
301 | # error "Port me"
|
---|
302 | #endif
|
---|
303 |
|
---|
304 | if (pInfo->flags & notsup)
|
---|
305 | rc -EINVAL;
|
---|
306 |
|
---|
307 | #ifdef UNIX_DERIVATIVE
|
---|
308 | if ((pInfo->flags & O_ACCMODE) == O_ACCMODE)
|
---|
309 | rc = -EINVAL;
|
---|
310 | # ifdef O_DIRECTORY
|
---|
311 | if (pInfo->flags & O_DIRECTORY)
|
---|
312 | rc = -ENOTDIR;
|
---|
313 | # endif
|
---|
314 | #endif
|
---|
315 |
|
---|
316 | if (RT_FAILURE(rc))
|
---|
317 | {
|
---|
318 | LogFlowFunc(("rc=%d \"%s\"\n", rc, pszPath));
|
---|
319 | return rc;
|
---|
320 | }
|
---|
321 |
|
---|
322 | int fWriteable = (pInfo->flags & O_ACCMODE) == O_WRONLY
|
---|
323 | || (pInfo->flags & O_ACCMODE) == O_RDWR;
|
---|
324 | if (g_cWriters)
|
---|
325 | rc = -ETXTBSY;
|
---|
326 | else
|
---|
327 | {
|
---|
328 | if (fWriteable)
|
---|
329 | g_cWriters++;
|
---|
330 | else
|
---|
331 | {
|
---|
332 | if (g_cReaders + 1 > MAX_READERS)
|
---|
333 | rc = -EMLINK;
|
---|
334 | else
|
---|
335 | g_cReaders++;
|
---|
336 | }
|
---|
337 | }
|
---|
338 | LogFlowFunc(("rc=%d \"%s\"\n", rc, pszPath));
|
---|
339 | return rc;
|
---|
340 |
|
---|
341 | }
|
---|
342 |
|
---|
343 | /** @todo Remove when VD I/O becomes threadsafe */
|
---|
344 | static DECLCALLBACK(int) vboximgThreadStartRead(void *pvUser)
|
---|
345 | {
|
---|
346 | PRTCRITSECT vdioLock = (PRTCRITSECT)pvUser;
|
---|
347 | return RTCritSectEnter(vdioLock);
|
---|
348 | }
|
---|
349 |
|
---|
350 | static DECLCALLBACK(int) vboximgThreadFinishRead(void *pvUser)
|
---|
351 | {
|
---|
352 | PRTCRITSECT vdioLock = (PRTCRITSECT)pvUser;
|
---|
353 | return RTCritSectLeave(vdioLock);
|
---|
354 | }
|
---|
355 |
|
---|
356 | static DECLCALLBACK(int) vboximgThreadStartWrite(void *pvUser)
|
---|
357 | {
|
---|
358 | PRTCRITSECT vdioLock = (PRTCRITSECT)pvUser;
|
---|
359 | return RTCritSectEnter(vdioLock);
|
---|
360 | }
|
---|
361 |
|
---|
362 | static DECLCALLBACK(int) vboximgThreadFinishWrite(void *pvUser)
|
---|
363 | {
|
---|
364 | PRTCRITSECT vdioLock = (PRTCRITSECT)pvUser;
|
---|
365 | return RTCritSectLeave(vdioLock);
|
---|
366 | }
|
---|
367 | /** @todo (end of to do section) */
|
---|
368 |
|
---|
369 | /** @copydoc fuse_operations::release */
|
---|
370 | static int vboximgOp_release(const char *pszPath, struct fuse_file_info *pInfo)
|
---|
371 | {
|
---|
372 | (void) pszPath;
|
---|
373 |
|
---|
374 | LogFlowFunc(("pszPath=%s\n", pszPath));
|
---|
375 |
|
---|
376 | if ( (pInfo->flags & O_ACCMODE) == O_WRONLY
|
---|
377 | || (pInfo->flags & O_ACCMODE) == O_RDWR)
|
---|
378 | {
|
---|
379 | g_cWriters--;
|
---|
380 | Assert(g_cWriters >= 0);
|
---|
381 | }
|
---|
382 | else if ((pInfo->flags & O_ACCMODE) == O_RDONLY)
|
---|
383 | {
|
---|
384 | g_cReaders--;
|
---|
385 | Assert(g_cReaders >= 0);
|
---|
386 | }
|
---|
387 | else
|
---|
388 | AssertFailed();
|
---|
389 |
|
---|
390 | LogFlowFunc(("\"%s\"\n", pszPath));
|
---|
391 | return 0;
|
---|
392 | }
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * VD read Sanitizer taking care of unaligned accesses.
|
---|
396 | *
|
---|
397 | * @return VBox bootIndicator code.
|
---|
398 | * @param pDisk VD disk container.
|
---|
399 | * @param off Offset to start reading from.
|
---|
400 | * @param pvBuf Pointer to the buffer to read into.
|
---|
401 | * @param cbRead Amount of bytes to read.
|
---|
402 | */
|
---|
403 | static int vdReadSanitizer(PVDISK pDisk, uint64_t off, void *pvBuf, size_t cbRead)
|
---|
404 | {
|
---|
405 | int rc;
|
---|
406 |
|
---|
407 | uint64_t const cbMisalignmentOfStart = off & VD_SECTOR_MASK;
|
---|
408 | uint64_t const cbMisalignmentOfEnd = (off + cbRead) & VD_SECTOR_MASK;
|
---|
409 |
|
---|
410 | if (cbMisalignmentOfStart + cbMisalignmentOfEnd == 0) /* perfectly aligned request; just read it and done */
|
---|
411 | rc = VDRead(pDisk, off, pvBuf, cbRead);
|
---|
412 | else
|
---|
413 | {
|
---|
414 | uint8_t *pbBuf = (uint8_t *)pvBuf;
|
---|
415 | uint8_t abBuf[VD_SECTOR_SIZE];
|
---|
416 |
|
---|
417 | /* If offset not @ sector boundary, read whole sector, then copy unaligned
|
---|
418 | * bytes (requested by user), only up to sector boundary, into user's buffer
|
---|
419 | */
|
---|
420 | if (cbMisalignmentOfStart)
|
---|
421 | {
|
---|
422 | rc = VDRead(pDisk, off - cbMisalignmentOfStart, abBuf, VD_SECTOR_SIZE);
|
---|
423 | if (RT_SUCCESS(rc))
|
---|
424 | {
|
---|
425 | size_t const cbPartial = RT_MIN(VD_SECTOR_SIZE - cbMisalignmentOfStart, cbRead);
|
---|
426 | memcpy(pbBuf, &abBuf[cbMisalignmentOfStart], cbPartial);
|
---|
427 | pbBuf += cbPartial;
|
---|
428 | off += cbPartial; /* Beginning of next sector or EOD */
|
---|
429 | cbRead -= cbPartial; /* # left to read */
|
---|
430 | }
|
---|
431 | }
|
---|
432 | else /* user's offset already aligned, did nothing */
|
---|
433 | rc = VINF_SUCCESS;
|
---|
434 |
|
---|
435 | /* Read remaining aligned sectors, deferring any tail-skewed bytes */
|
---|
436 | if (RT_SUCCESS(rc) && cbRead >= VD_SECTOR_SIZE)
|
---|
437 | {
|
---|
438 | Assert(!(off % VD_SECTOR_SIZE));
|
---|
439 |
|
---|
440 | size_t cbPartial = cbRead - cbMisalignmentOfEnd;
|
---|
441 | Assert(!(cbPartial % VD_SECTOR_SIZE));
|
---|
442 | rc = VDRead(pDisk, off, pbBuf, cbPartial);
|
---|
443 | if (RT_SUCCESS(rc))
|
---|
444 | {
|
---|
445 | pbBuf += cbPartial;
|
---|
446 | off += cbPartial;
|
---|
447 | cbRead -= cbPartial;
|
---|
448 | }
|
---|
449 | }
|
---|
450 |
|
---|
451 | /* Unaligned buffered read of tail. */
|
---|
452 | if (RT_SUCCESS(rc) && cbRead)
|
---|
453 | {
|
---|
454 | Assert(cbRead == cbMisalignmentOfEnd);
|
---|
455 | Assert(cbRead < VD_SECTOR_SIZE);
|
---|
456 | Assert(!(off % VD_SECTOR_SIZE));
|
---|
457 |
|
---|
458 | rc = VDRead(pDisk, off, abBuf, VD_SECTOR_SIZE);
|
---|
459 | if (RT_SUCCESS(rc))
|
---|
460 | memcpy(pbBuf, abBuf, cbRead);
|
---|
461 | }
|
---|
462 | }
|
---|
463 |
|
---|
464 | if (RT_FAILURE(rc))
|
---|
465 | {
|
---|
466 | int sysrc = -RTErrConvertToErrno(rc);
|
---|
467 | LogFlowFunc(("error: %s (vbox err: %d)\n", strerror(sysrc), rc));
|
---|
468 | rc = sysrc;
|
---|
469 | }
|
---|
470 | return cbRead;
|
---|
471 | }
|
---|
472 |
|
---|
473 | /**
|
---|
474 | * VD write Sanitizer taking care of unaligned accesses.
|
---|
475 | *
|
---|
476 | * @return VBox bootIndicator code.
|
---|
477 | * @param pDisk VD disk container.
|
---|
478 | * @param off Offset to start writing to.
|
---|
479 | * @param pvSrc Pointer to the buffer to read from.
|
---|
480 | * @param cbWrite Amount of bytes to write.
|
---|
481 | */
|
---|
482 | static int vdWriteSanitizer(PVDISK pDisk, uint64_t off, const void *pvSrc, size_t cbWrite)
|
---|
483 | {
|
---|
484 | uint8_t const *pbSrc = (uint8_t const *)pvSrc;
|
---|
485 | uint8_t abBuf[4096];
|
---|
486 | int rc;
|
---|
487 | int cbRemaining = cbWrite;
|
---|
488 |
|
---|
489 | /*
|
---|
490 | * Take direct route if the request is sector aligned.
|
---|
491 | */
|
---|
492 | uint64_t const cbMisalignmentOfStart = off & VD_SECTOR_MASK;
|
---|
493 | size_t const cbMisalignmentOfEnd = (off + cbWrite) & VD_SECTOR_MASK;
|
---|
494 | if (!cbMisalignmentOfStart && !cbMisalignmentOfEnd)
|
---|
495 | {
|
---|
496 | rc = VDWrite(pDisk, off, pbSrc, cbWrite);
|
---|
497 | do
|
---|
498 | {
|
---|
499 | size_t cbThisWrite = RT_MIN(cbWrite, sizeof(abBuf));
|
---|
500 | rc = VDWrite(pDisk, off, memcpy(abBuf, pbSrc, cbThisWrite), cbThisWrite);
|
---|
501 | if (RT_SUCCESS(rc))
|
---|
502 | {
|
---|
503 | pbSrc += cbThisWrite;
|
---|
504 | off += cbThisWrite;
|
---|
505 | cbRemaining -= cbThisWrite;
|
---|
506 | }
|
---|
507 | else
|
---|
508 | break;
|
---|
509 | } while (cbRemaining > 0);
|
---|
510 | }
|
---|
511 | else
|
---|
512 | {
|
---|
513 | /*
|
---|
514 | * Unaligned buffered read+write of head. Aligns the offset.
|
---|
515 | */
|
---|
516 | if (cbMisalignmentOfStart)
|
---|
517 | {
|
---|
518 | rc = VDRead(pDisk, off - cbMisalignmentOfStart, abBuf, VD_SECTOR_SIZE);
|
---|
519 | if (RT_SUCCESS(rc))
|
---|
520 | {
|
---|
521 | size_t const cbPartial = RT_MIN(VD_SECTOR_SIZE - cbMisalignmentOfStart, cbWrite);
|
---|
522 | memcpy(&abBuf[cbMisalignmentOfStart], pbSrc, cbPartial);
|
---|
523 | rc = VDWrite(pDisk, off - cbMisalignmentOfStart, abBuf, VD_SECTOR_SIZE);
|
---|
524 | if (RT_SUCCESS(rc))
|
---|
525 | {
|
---|
526 | pbSrc += cbPartial;
|
---|
527 | off += cbPartial;
|
---|
528 | cbRemaining -= cbPartial;
|
---|
529 | }
|
---|
530 | }
|
---|
531 | }
|
---|
532 | else
|
---|
533 | rc = VINF_SUCCESS;
|
---|
534 |
|
---|
535 | /*
|
---|
536 | * Aligned direct write.
|
---|
537 | */
|
---|
538 | if (RT_SUCCESS(rc) && cbWrite >= VD_SECTOR_SIZE)
|
---|
539 | {
|
---|
540 | Assert(!(off % VD_SECTOR_SIZE));
|
---|
541 | size_t cbPartial = cbWrite - cbMisalignmentOfEnd;
|
---|
542 | Assert(!(cbPartial % VD_SECTOR_SIZE));
|
---|
543 | rc = VDWrite(pDisk, off, pbSrc, cbPartial);
|
---|
544 | if (RT_SUCCESS(rc))
|
---|
545 | {
|
---|
546 | pbSrc += cbPartial;
|
---|
547 | off += cbPartial;
|
---|
548 | cbRemaining -= cbPartial;
|
---|
549 | }
|
---|
550 | }
|
---|
551 |
|
---|
552 | /*
|
---|
553 | * Unaligned buffered read + write of tail.
|
---|
554 | */
|
---|
555 | if ( RT_SUCCESS(rc) && cbWrite > 0)
|
---|
556 | {
|
---|
557 | Assert(cbWrite == cbMisalignmentOfEnd);
|
---|
558 | Assert(cbWrite < VD_SECTOR_SIZE);
|
---|
559 | Assert(!(off % VD_SECTOR_SIZE));
|
---|
560 | rc = VDRead(pDisk, off, abBuf, VD_SECTOR_SIZE);
|
---|
561 | if (RT_SUCCESS(rc))
|
---|
562 | {
|
---|
563 | memcpy(abBuf, pbSrc, cbWrite);
|
---|
564 | rc = VDWrite(pDisk, off, abBuf, VD_SECTOR_SIZE);
|
---|
565 | }
|
---|
566 | }
|
---|
567 | }
|
---|
568 | if (RT_FAILURE(rc))
|
---|
569 | {
|
---|
570 | int sysrc = -RTErrConvertToErrno(rc);
|
---|
571 | LogFlowFunc(("error: %s (vbox err: %d)\n", strerror(sysrc), rc));
|
---|
572 | return sysrc;
|
---|
573 | }
|
---|
574 | return cbWrite - cbRemaining;
|
---|
575 | }
|
---|
576 |
|
---|
577 |
|
---|
578 | /** @copydoc fuse_operations::read */
|
---|
579 | static int vboximgOp_read(const char *pszPath, char *pbBuf, size_t cbBuf,
|
---|
580 | off_t offset, struct fuse_file_info *pInfo)
|
---|
581 | {
|
---|
582 | (void) pszPath;
|
---|
583 | (void) pInfo;
|
---|
584 |
|
---|
585 | LogFlowFunc(("my offset=%#llx size=%#zx path=\"%s\"\n", (uint64_t)offset, cbBuf, pszPath));
|
---|
586 |
|
---|
587 | AssertReturn(offset >= 0, -EINVAL);
|
---|
588 | AssertReturn((int)cbBuf >= 0, -EINVAL);
|
---|
589 | AssertReturn((unsigned)cbBuf == cbBuf, -EINVAL);
|
---|
590 |
|
---|
591 | AssertReturn(offset + g_vDiskOffset >= 0, -EINVAL);
|
---|
592 | int64_t adjOff = offset + g_vDiskOffset;
|
---|
593 |
|
---|
594 | int rc = 0;
|
---|
595 | if ((off_t)(adjOff + cbBuf) < adjOff)
|
---|
596 | rc = -EINVAL;
|
---|
597 | else if (adjOff >= g_vDiskSize)
|
---|
598 | return 0;
|
---|
599 | else if (!cbBuf)
|
---|
600 | return 0;
|
---|
601 |
|
---|
602 | if (rc >= 0)
|
---|
603 | rc = vdReadSanitizer(g_pVDisk, adjOff, pbBuf, cbBuf);
|
---|
604 | if (rc < 0)
|
---|
605 | LogFlowFunc(("%s\n", strerror(rc)));
|
---|
606 | return rc;
|
---|
607 | }
|
---|
608 |
|
---|
609 | /** @copydoc fuse_operations::write */
|
---|
610 | static int vboximgOp_write(const char *pszPath, const char *pbBuf, size_t cbBuf,
|
---|
611 | off_t offset, struct fuse_file_info *pInfo)
|
---|
612 | {
|
---|
613 | (void) pszPath;
|
---|
614 | (void) pInfo;
|
---|
615 |
|
---|
616 | LogFlowFunc(("offset=%#llx size=%#zx path=\"%s\"\n", (uint64_t)offset, cbBuf, pszPath));
|
---|
617 |
|
---|
618 | AssertReturn(offset >= 0, -EINVAL);
|
---|
619 | AssertReturn((int)cbBuf >= 0, -EINVAL);
|
---|
620 | AssertReturn((unsigned)cbBuf == cbBuf, -EINVAL);
|
---|
621 | AssertReturn(offset + g_vDiskOffset >= 0, -EINVAL);
|
---|
622 | int64_t adjOff = offset + g_vDiskOffset;
|
---|
623 |
|
---|
624 | int rc = 0;
|
---|
625 | if (!g_vboximgOpts.fRW) {
|
---|
626 | LogFlowFunc(("WARNING: vboximg-mount (FUSE FS) --rw option not specified\n"
|
---|
627 | " (write operation ignored w/o error!)\n"));
|
---|
628 | return cbBuf;
|
---|
629 | } else if ((off_t)(adjOff + cbBuf) < adjOff)
|
---|
630 | rc = -EINVAL;
|
---|
631 | else if (offset >= g_vDiskSize)
|
---|
632 | return 0;
|
---|
633 | else if (!cbBuf)
|
---|
634 | return 0;
|
---|
635 |
|
---|
636 | if (rc >= 0)
|
---|
637 | rc = vdWriteSanitizer(g_pVDisk, adjOff, pbBuf, cbBuf);
|
---|
638 | if (rc < 0)
|
---|
639 | LogFlowFunc(("%s\n", strerror(rc)));
|
---|
640 |
|
---|
641 | return rc;
|
---|
642 | }
|
---|
643 |
|
---|
644 | /** @copydoc fuse_operations::getattr */
|
---|
645 | static int
|
---|
646 | vboximgOp_getattr(const char *pszPath, struct stat *stbuf)
|
---|
647 | {
|
---|
648 | int rc = 0;
|
---|
649 |
|
---|
650 | LogFlowFunc(("pszPath=%s, stat(\"%s\")\n", pszPath, g_pszBaseImagePath));
|
---|
651 |
|
---|
652 | memset(stbuf, 0, sizeof(struct stat));
|
---|
653 |
|
---|
654 | if (RTStrCmp(pszPath, "/") == 0)
|
---|
655 | {
|
---|
656 | stbuf->st_mode = S_IFDIR | 0755;
|
---|
657 | stbuf->st_nlink = 2;
|
---|
658 | }
|
---|
659 | else if (RTStrCmp(pszPath + 1, "vhdd") == 0)
|
---|
660 | {
|
---|
661 | rc = stat(g_pszBaseImagePath, stbuf);
|
---|
662 | if (rc < 0)
|
---|
663 | return rc;
|
---|
664 | /*
|
---|
665 | * st_size represents the size of the FUSE FS-mounted portion of the disk.
|
---|
666 | * By default it is the whole disk, but can be a partition or specified
|
---|
667 | * (or overridden) directly by the { -s | --size } option on the command line.
|
---|
668 | */
|
---|
669 | stbuf->st_size = g_vDiskSize;
|
---|
670 | stbuf->st_nlink = 1;
|
---|
671 | }
|
---|
672 | else if (RTStrNCmp(pszPath + 1, g_pszBaseImageName, strlen(g_pszBaseImageName)) == 0)
|
---|
673 | {
|
---|
674 | /* When the disk is partitioned, the symbolic link named from `basename` of
|
---|
675 | * resolved path to VBox disk image, has appended to it formatted text
|
---|
676 | * representing the offset range of the partition.
|
---|
677 | *
|
---|
678 | * $ vboximg-mount -i /stroll/along/the/path/simple_fixed_disk.vdi -p 1 /mnt/tmpdir
|
---|
679 | * $ ls /mnt/tmpdir
|
---|
680 | * simple_fixed_disk.vdi[20480:2013244928] vhdd
|
---|
681 | */
|
---|
682 | rc = stat(g_pszBaseImagePath, stbuf);
|
---|
683 | if (rc < 0)
|
---|
684 | return rc;
|
---|
685 | stbuf->st_size = 0;
|
---|
686 | stbuf->st_mode = S_IFLNK | 0444;
|
---|
687 | stbuf->st_nlink = 1;
|
---|
688 | stbuf->st_uid = 0;
|
---|
689 | stbuf->st_gid = 0;
|
---|
690 | } else
|
---|
691 | rc = -ENOENT;
|
---|
692 |
|
---|
693 | return rc;
|
---|
694 | }
|
---|
695 |
|
---|
696 | /** @copydoc fuse_operations::readdir */
|
---|
697 | static int
|
---|
698 | vboximgOp_readdir(const char *pszPath, void *pvBuf, fuse_fill_dir_t pfnFiller,
|
---|
699 | off_t offset, struct fuse_file_info *pInfo)
|
---|
700 |
|
---|
701 | {
|
---|
702 | (void) offset;
|
---|
703 | (void) pInfo;
|
---|
704 |
|
---|
705 | if (RTStrCmp(pszPath, "/") != 0)
|
---|
706 | return -ENOENT;
|
---|
707 |
|
---|
708 | /*
|
---|
709 | * mandatory '.', '..', ...
|
---|
710 | */
|
---|
711 | pfnFiller(pvBuf, ".", NULL, 0);
|
---|
712 | pfnFiller(pvBuf, "..", NULL, 0);
|
---|
713 |
|
---|
714 | /*
|
---|
715 | * Create FUSE FS dir entry that is depicted here (and exposed via stat()) as
|
---|
716 | * a symbolic link back to the resolved path to the VBox virtual disk image,
|
---|
717 | * whose symlink name is basename that path. This is a convenience so anyone
|
---|
718 | * listing the dir can figure out easily what the vhdd FUSE node entry
|
---|
719 | * represents.
|
---|
720 | */
|
---|
721 |
|
---|
722 | if (g_vDiskOffset == 0 && (g_vDiskSize == 0 || g_vDiskSize == g_cbEntireVDisk))
|
---|
723 | pfnFiller(pvBuf, g_pszBaseImageName, NULL, 0);
|
---|
724 | else
|
---|
725 | {
|
---|
726 | char tmp[BASENAME_MAX];
|
---|
727 | RTStrPrintf(tmp, sizeof (tmp), "%s[%d:%d]", g_pszBaseImageName, g_vDiskOffset, g_vDiskSize);
|
---|
728 | pfnFiller(pvBuf, tmp, NULL, 0);
|
---|
729 | }
|
---|
730 | /*
|
---|
731 | * Create entry named "vhdd", which getattr() will describe as a
|
---|
732 | * regular file, and thus will go through the open/release/read/write vectors
|
---|
733 | * to access the VirtualBox image as processed by the IRPT VD API.
|
---|
734 | */
|
---|
735 | pfnFiller(pvBuf, "vhdd", NULL, 0);
|
---|
736 | return 0;
|
---|
737 | }
|
---|
738 |
|
---|
739 | /** @copydoc fuse_operations::readlink */
|
---|
740 | static int
|
---|
741 | vboximgOp_readlink(const char *pszPath, char *buf, size_t size)
|
---|
742 | {
|
---|
743 | (void) pszPath;
|
---|
744 | RTStrCopy(buf, size, g_pszBaseImagePath);
|
---|
745 | return 0;
|
---|
746 | }
|
---|
747 |
|
---|
748 | static void
|
---|
749 | listMedia(IMachine *pMachine, char *vmName, char *vmUuid)
|
---|
750 | {
|
---|
751 | int rc = 0;
|
---|
752 | com::SafeIfaceArray<IMediumAttachment> pMediumAttachments;
|
---|
753 |
|
---|
754 | CHECK_ERROR(pMachine, COMGETTER(MediumAttachments)(ComSafeArrayAsOutParam(pMediumAttachments)));
|
---|
755 | int firstIteration = 1;
|
---|
756 | for (size_t i = 0; i < pMediumAttachments.size(); i++)
|
---|
757 | {
|
---|
758 |
|
---|
759 | ComPtr<IMedium> pMedium;
|
---|
760 | DeviceType_T deviceType;
|
---|
761 | Bstr pMediumUuid;
|
---|
762 | Bstr pMediumName;
|
---|
763 | Bstr pMediumPath;
|
---|
764 |
|
---|
765 | CHECK_ERROR(pMediumAttachments[i], COMGETTER(Type)(&deviceType));
|
---|
766 |
|
---|
767 | if (deviceType == DeviceType_HardDisk)
|
---|
768 | {
|
---|
769 | CHECK_ERROR(pMediumAttachments[i], COMGETTER(Medium)(pMedium.asOutParam()));
|
---|
770 | if (pMedium.isNull())
|
---|
771 | return;
|
---|
772 |
|
---|
773 | MediumState_T state;
|
---|
774 | CHECK_ERROR(pMedium, COMGETTER(State)(&state));
|
---|
775 | if (FAILED(rc))
|
---|
776 | return;
|
---|
777 | if (state == MediumState_Inaccessible)
|
---|
778 | {
|
---|
779 | CHECK_ERROR(pMedium, RefreshState(&state));
|
---|
780 | if (FAILED(rc))
|
---|
781 | return;
|
---|
782 | }
|
---|
783 |
|
---|
784 | ComPtr<IMedium> pEarliestAncestor;
|
---|
785 | CHECK_ERROR(pMedium, COMGETTER(Base)(pEarliestAncestor.asOutParam()));
|
---|
786 | ComPtr<IMedium> pChild = pEarliestAncestor;
|
---|
787 | uint32_t ancestorNumber = 0;
|
---|
788 | if (pEarliestAncestor.isNull())
|
---|
789 | return;
|
---|
790 | do
|
---|
791 | {
|
---|
792 | com::SafeIfaceArray<IMedium> aMediumChildren;
|
---|
793 | CHECK_ERROR(pChild, COMGETTER(Name)(pMediumName.asOutParam()));
|
---|
794 | CHECK_ERROR(pChild, COMGETTER(Id)(pMediumUuid.asOutParam()));
|
---|
795 | CHECK_ERROR(pChild, COMGETTER(Location)(pMediumPath.asOutParam()));
|
---|
796 |
|
---|
797 | if (ancestorNumber == 0)
|
---|
798 | {
|
---|
799 | if (g_vboximgOpts.fVerbose)
|
---|
800 | {
|
---|
801 | RTPrintf(" -----------------------\n");
|
---|
802 | RTPrintf(" HDD base: \"%s\"\n", CSTR(pMediumName));
|
---|
803 | RTPrintf(" UUID: %s\n", CSTR(pMediumUuid));
|
---|
804 | RTPrintf(" Location: %s\n\n", CSTR(pMediumPath));
|
---|
805 | }
|
---|
806 | else
|
---|
807 | {
|
---|
808 | if (firstIteration)
|
---|
809 | RTPrintf("\nVM: %s " ANSI_BOLD "%-20s" ANSI_RESET "\n",
|
---|
810 | vmUuid, vmName);
|
---|
811 | RTPrintf(" img: %s " ANSI_BOLD " %s" ANSI_RESET "\n",
|
---|
812 | CSTR(pMediumUuid), CSTR(pMediumName));
|
---|
813 | }
|
---|
814 | }
|
---|
815 | else
|
---|
816 | {
|
---|
817 | if (g_vboximgOpts.fVerbose)
|
---|
818 | {
|
---|
819 | RTPrintf(" Diff %d:\n", ancestorNumber);
|
---|
820 | RTPrintf(" UUID: %s\n", CSTR(pMediumUuid));
|
---|
821 | RTPrintf(" Location: %s\n", CSTR(pMediumPath));
|
---|
822 | }
|
---|
823 | }
|
---|
824 | CHECK_ERROR_BREAK(pChild, COMGETTER(Children)(ComSafeArrayAsOutParam(aMediumChildren)));
|
---|
825 | pChild = (aMediumChildren.size()) ? aMediumChildren[0] : NULL;
|
---|
826 | ++ancestorNumber;
|
---|
827 | firstIteration = 0;
|
---|
828 | } while(pChild);
|
---|
829 | }
|
---|
830 | }
|
---|
831 | }
|
---|
832 | /**
|
---|
833 | * Display all registered VMs on the screen with some information about each
|
---|
834 | *
|
---|
835 | * @param virtualBox VirtualBox instance object.
|
---|
836 | */
|
---|
837 | static void
|
---|
838 | listVMs(IVirtualBox *pVirtualBox)
|
---|
839 | {
|
---|
840 | HRESULT rc = 0;
|
---|
841 | com::SafeIfaceArray<IMachine> pMachines;
|
---|
842 | CHECK_ERROR(pVirtualBox, COMGETTER(Machines)(ComSafeArrayAsOutParam(pMachines)));
|
---|
843 | for (size_t i = 0; i < pMachines.size(); ++i)
|
---|
844 | {
|
---|
845 | ComPtr<IMachine> pMachine = pMachines[i];
|
---|
846 | if (pMachine)
|
---|
847 | {
|
---|
848 | BOOL fAccessible;
|
---|
849 | CHECK_ERROR(pMachines[i], COMGETTER(Accessible)(&fAccessible));
|
---|
850 | if (fAccessible)
|
---|
851 | {
|
---|
852 | Bstr pMachineName;
|
---|
853 | Bstr pMachineUuid;
|
---|
854 | Bstr pDescription;
|
---|
855 | Bstr pMachineLocation;
|
---|
856 |
|
---|
857 | CHECK_ERROR(pMachine, COMGETTER(Name)(pMachineName.asOutParam()));
|
---|
858 | CHECK_ERROR(pMachine, COMGETTER(Id)(pMachineUuid.asOutParam()));
|
---|
859 | CHECK_ERROR(pMachine, COMGETTER(Description)(pDescription.asOutParam()));
|
---|
860 | CHECK_ERROR(pMachine, COMGETTER(SettingsFilePath)(pMachineLocation.asOutParam()));
|
---|
861 |
|
---|
862 | if ( g_vboximgOpts.pszVm == NULL
|
---|
863 | || RTStrNCmp(CSTR(pMachineUuid), g_vboximgOpts.pszVm, MAX_UUID_LEN) == 0
|
---|
864 | || RTStrNCmp((const char *)pMachineName.raw(), g_vboximgOpts.pszVm, MAX_UUID_LEN) == 0)
|
---|
865 | {
|
---|
866 | if (g_vboximgOpts.fVerbose)
|
---|
867 | {
|
---|
868 | RTPrintf("------------------------------------------------------\n");
|
---|
869 | RTPrintf("VM Name: \"%s\"\n", CSTR(pMachineName));
|
---|
870 | RTPrintf("UUID: %s\n", CSTR(pMachineUuid));
|
---|
871 | if (*pDescription.raw() != '\0')
|
---|
872 | RTPrintf("Description: %s\n", CSTR(pDescription));
|
---|
873 | RTPrintf("Location: %s\n", CSTR(pMachineLocation));
|
---|
874 | }
|
---|
875 | listMedia(pMachine, RTStrDup(CSTR(pMachineName)), RTStrDup(CSTR(pMachineUuid)));
|
---|
876 | }
|
---|
877 | else
|
---|
878 | {
|
---|
879 | listMedia(pMachine, RTStrDup(CSTR(pMachineName)), RTStrDup(CSTR(pMachineUuid)));
|
---|
880 | }
|
---|
881 | }
|
---|
882 | }
|
---|
883 | }
|
---|
884 | }
|
---|
885 |
|
---|
886 | static void
|
---|
887 | searchForBaseImage(IVirtualBox *pVirtualBox, char *pszImageString, ComPtr<IMedium> *pBaseImage)
|
---|
888 | {
|
---|
889 | int rc = 0;
|
---|
890 | com::SafeIfaceArray<IMedium> aDisks;
|
---|
891 |
|
---|
892 | CHECK_ERROR(pVirtualBox, COMGETTER(HardDisks)(ComSafeArrayAsOutParam(aDisks)));
|
---|
893 | for (size_t i = 0; i < aDisks.size() && aDisks[i]; i++)
|
---|
894 | {
|
---|
895 | if (aDisks[i])
|
---|
896 | {
|
---|
897 | Bstr pMediumUuid;
|
---|
898 | Bstr pMediumName;
|
---|
899 |
|
---|
900 | CHECK_ERROR(aDisks[i], COMGETTER(Name)(pMediumName.asOutParam()));
|
---|
901 | CHECK_ERROR(aDisks[i], COMGETTER(Id)(pMediumUuid.asOutParam()));
|
---|
902 |
|
---|
903 | if ( RTStrCmp(pszImageString, CSTR(pMediumUuid)) == 0
|
---|
904 | || RTStrCmp(pszImageString, CSTR(pMediumName)) == 0)
|
---|
905 | {
|
---|
906 | *pBaseImage = aDisks[i];
|
---|
907 | return;
|
---|
908 | }
|
---|
909 | }
|
---|
910 | }
|
---|
911 | }
|
---|
912 |
|
---|
913 | uint8_t
|
---|
914 | parsePartitionTable(void)
|
---|
915 | {
|
---|
916 | MBR_t mbr;
|
---|
917 | EBR_t ebr;
|
---|
918 | PTH_t parTblHdr;
|
---|
919 |
|
---|
920 | ASSERT(sizeof (mbr) == 512);
|
---|
921 | ASSERT(sizeof (ebr) == 512);
|
---|
922 | /*
|
---|
923 | * First entry describes entire disk as a single entity
|
---|
924 | */
|
---|
925 | g_aParsedPartitionInfo[0].idxPartition = 0;
|
---|
926 | g_aParsedPartitionInfo[0].offPartition = 0;
|
---|
927 | g_aParsedPartitionInfo[0].cbPartition = VDGetSize(g_pVDisk, 0);
|
---|
928 | g_aParsedPartitionInfo[0].pszName = RTStrDup("EntireDisk");
|
---|
929 |
|
---|
930 | /*
|
---|
931 | * Currently only DOS partitioned disks are supported. Ensure this one conforms
|
---|
932 | */
|
---|
933 | int rc = vdReadSanitizer(g_pVDisk, 0, &mbr, sizeof (mbr));
|
---|
934 | if (RT_FAILURE(rc))
|
---|
935 | return RTMsgErrorExitFailure("Error reading MBR block from disk\n");
|
---|
936 |
|
---|
937 | if (mbr.signature == NULL_BOOT_RECORD_SIGNATURE)
|
---|
938 | return RTMsgErrorExitFailure("Unprt disk (null MBR signature)\n");
|
---|
939 |
|
---|
940 | if (mbr.signature != DOS_BOOT_RECORD_SIGNATURE)
|
---|
941 | return RTMsgErrorExitFailure("Invalid MBR found on image with signature 0x%04hX\n",
|
---|
942 | mbr.signature);
|
---|
943 | /*
|
---|
944 | * Parse the four physical partition entires in the MBR (any one, and only one, can be an EBR)
|
---|
945 | */
|
---|
946 | int idxEbrPartitionInMbr = 0;
|
---|
947 | for (int idxPartition = 1;
|
---|
948 | idxPartition <= MBR_PARTITIONS_MAX;
|
---|
949 | idxPartition++)
|
---|
950 | {
|
---|
951 | MBRPARTITIONENTRY *pMbrPartitionEntry =
|
---|
952 | &g_aParsedPartitionInfo[idxPartition].partitionEntry.mbrEntry;;
|
---|
953 | memcpy (pMbrPartitionEntry, &mbr.partitionEntry[idxPartition - 1], sizeof (MBRPARTITIONENTRY));
|
---|
954 |
|
---|
955 | if (PARTTYPE_IS_NULL(pMbrPartitionEntry->type))
|
---|
956 | continue;
|
---|
957 |
|
---|
958 | if (PARTTYPE_IS_EXT(pMbrPartitionEntry->type))
|
---|
959 | {
|
---|
960 | if (idxEbrPartitionInMbr)
|
---|
961 | return RTMsgErrorExitFailure("Multiple EBRs found found in MBR\n");
|
---|
962 | idxEbrPartitionInMbr = idxPartition;
|
---|
963 | }
|
---|
964 |
|
---|
965 | PARTITIONINFO *ppi = &g_aParsedPartitionInfo[idxPartition];
|
---|
966 |
|
---|
967 | ppi->idxPartition = idxPartition;
|
---|
968 | ppi->offPartition = (off_t) pMbrPartitionEntry->partitionLba * BLOCKSIZE;
|
---|
969 | ppi->cbPartition = (off_t) pMbrPartitionEntry->partitionBlkCnt * BLOCKSIZE;
|
---|
970 | ppi->fBootable = pMbrPartitionEntry->bootIndicator == 0x80;
|
---|
971 | ppi->partitionType.legacy = pMbrPartitionEntry->type;
|
---|
972 |
|
---|
973 | g_lastPartNbr = idxPartition;
|
---|
974 |
|
---|
975 | if (PARTTYPE_IS_GPT(pMbrPartitionEntry->type))
|
---|
976 | {
|
---|
977 | g_fGPT = true;
|
---|
978 | break;
|
---|
979 | }
|
---|
980 | }
|
---|
981 |
|
---|
982 | if (g_fGPT)
|
---|
983 | {
|
---|
984 | g_lastPartNbr = 2; /* from the 'protective MBR' */
|
---|
985 |
|
---|
986 | rc = vdReadSanitizer(g_pVDisk, LBA(1), &parTblHdr, sizeof (parTblHdr));
|
---|
987 | if (RT_FAILURE(rc))
|
---|
988 | return RTMsgErrorExitFailure("Error reading Partition Table Header (LBA 1) from disk\n");
|
---|
989 |
|
---|
990 | uint8_t *pTblBuf = (uint8_t *)RTMemAlloc(GPT_PTABLE_SIZE);
|
---|
991 |
|
---|
992 | RTPrintf( "Virtual disk image:\n\n");
|
---|
993 | RTPrintf(" Path: %s\n", g_pszBaseImagePath);
|
---|
994 | if (g_pvDiskUuid)
|
---|
995 | RTPrintf(" UUID: %s\n\n", g_pvDiskUuid);
|
---|
996 |
|
---|
997 | if (g_vboximgOpts.fVerbose)
|
---|
998 | {
|
---|
999 | RTPrintf(" GPT Partition Table Header:\n\n");
|
---|
1000 | if (RTStrCmp((const char *)&parTblHdr.signature, "EFI PART") == 0)
|
---|
1001 | RTPrintf(
|
---|
1002 | " Signature \"EFI PART\" (0x%llx)\n", parTblHdr.signature);
|
---|
1003 | else
|
---|
1004 | RTPrintf(
|
---|
1005 | " Signature: 0x%llx\n", parTblHdr.signature);
|
---|
1006 | RTPrintf(" Revision: %-8.8x\n", parTblHdr.revision);
|
---|
1007 | RTPrintf(" Current LBA: %lld\n", parTblHdr.headerLba);
|
---|
1008 | RTPrintf(" Backup LBA: %lld\n", parTblHdr.backupLba);
|
---|
1009 | RTPrintf(" Partition entries LBA: %lld\n", parTblHdr.partitionEntriesLba);
|
---|
1010 | RTPrintf(" # of partitions: %d\n", parTblHdr.cPartitionEntries);
|
---|
1011 | RTPrintf(" size of entry: %d\n\n", parTblHdr.cbPartitionEntry);
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | if (!pTblBuf)
|
---|
1015 | return RTMsgErrorExitFailure("Out of memory\n");
|
---|
1016 |
|
---|
1017 | rc = vdReadSanitizer(g_pVDisk, LBA(2), pTblBuf, GPT_PTABLE_SIZE);
|
---|
1018 | if (RT_FAILURE(rc))
|
---|
1019 | return RTMsgErrorExitFailure("Error reading Partition Table blocks from disk\n");
|
---|
1020 |
|
---|
1021 | uint32_t cEntries = parTblHdr.cPartitionEntries;
|
---|
1022 | uint32_t cbEntry = parTblHdr.cbPartitionEntry;
|
---|
1023 | if (cEntries * cbEntry > GPT_PTABLE_SIZE)
|
---|
1024 | {
|
---|
1025 | RTPrintf("Partition entries exceed GPT table read from disk (pruning!)\n");
|
---|
1026 | while (cEntries * cbEntry > GPT_PTABLE_SIZE && cEntries > 0)
|
---|
1027 | --cEntries;
|
---|
1028 | }
|
---|
1029 | uint8_t *pEntryRaw = pTblBuf;
|
---|
1030 | for (uint32_t i = 0; i < cEntries; i++)
|
---|
1031 | {
|
---|
1032 | GPTPARTITIONENTRY *pEntry = (GPTPARTITIONENTRY *)pEntryRaw;
|
---|
1033 | PARTITIONINFO *ppi = &g_aParsedPartitionInfo[g_lastPartNbr];
|
---|
1034 | memcpy(&(ppi->partitionEntry).gptEntry, pEntry, sizeof(GPTPARTITIONENTRY));
|
---|
1035 | if (!pEntry->firstLba)
|
---|
1036 | break;
|
---|
1037 | ppi->offPartition = pEntry->firstLba * BLOCKSIZE;
|
---|
1038 | ppi->cbPartition = (pEntry->lastLba - pEntry->firstLba) * BLOCKSIZE;
|
---|
1039 | ppi->fBootable = pEntry->attrFlags & (1 << GPT_LEGACY_BIOS_BOOTABLE);
|
---|
1040 | ppi->partitionType.gptGuidTypeSpecifier = pEntry->partitionTypeGuid;
|
---|
1041 | size_t cwName = sizeof (pEntry->partitionName) / 2;
|
---|
1042 | RTUtf16LittleToUtf8Ex((PRTUTF16)pEntry->partitionName, RTSTR_MAX, &ppi->pszName, cwName, NULL);
|
---|
1043 | ppi->idxPartition = g_lastPartNbr++;
|
---|
1044 | pEntryRaw += cbEntry;
|
---|
1045 | }
|
---|
1046 | return PARTITION_TABLE_GPT;
|
---|
1047 | }
|
---|
1048 |
|
---|
1049 | /*
|
---|
1050 | * Starting with EBR located in MBR, walk EBR chain to parse the logical partition entries
|
---|
1051 | */
|
---|
1052 | if (idxEbrPartitionInMbr)
|
---|
1053 | {
|
---|
1054 | uint32_t firstEbrLba
|
---|
1055 | = g_aParsedPartitionInfo[idxEbrPartitionInMbr].partitionEntry.mbrEntry.partitionLba;
|
---|
1056 | off_t firstEbrOffset = (off_t)firstEbrLba * BLOCKSIZE;
|
---|
1057 | off_t chainedEbrOffset = 0;
|
---|
1058 |
|
---|
1059 | if (!firstEbrLba)
|
---|
1060 | return RTMsgErrorExitFailure("Inconsistency for logical partition start. Aborting\n");
|
---|
1061 |
|
---|
1062 | for (int idxPartition = 5;
|
---|
1063 | idxPartition <= VBOXIMG_PARTITION_MAX;
|
---|
1064 | idxPartition++)
|
---|
1065 | {
|
---|
1066 |
|
---|
1067 | off_t currentEbrOffset = firstEbrOffset + chainedEbrOffset;
|
---|
1068 | vdReadSanitizer(g_pVDisk, currentEbrOffset, &ebr, sizeof (ebr));
|
---|
1069 |
|
---|
1070 | if (ebr.signature != DOS_BOOT_RECORD_SIGNATURE)
|
---|
1071 | return RTMsgErrorExitFailure("Invalid EBR found on image with signature 0x%04hX\n",
|
---|
1072 | ebr.signature);
|
---|
1073 |
|
---|
1074 | MBRPARTITIONENTRY *pEbrPartitionEntry =
|
---|
1075 | &g_aParsedPartitionInfo[idxPartition].partitionEntry.mbrEntry; /* EBR entry struct same as MBR */
|
---|
1076 | memcpy(pEbrPartitionEntry, &ebr.partitionEntry, sizeof (MBRPARTITIONENTRY));
|
---|
1077 |
|
---|
1078 | if (pEbrPartitionEntry->type == NULL_BOOT_RECORD_SIGNATURE)
|
---|
1079 | return RTMsgErrorExitFailure("Logical partition with type 0 encountered");
|
---|
1080 |
|
---|
1081 | if (!pEbrPartitionEntry->partitionLba)
|
---|
1082 | return RTMsgErrorExitFailure("Logical partition invalid partition start offset (LBA) encountered");
|
---|
1083 |
|
---|
1084 | PARTITIONINFO *ppi = &g_aParsedPartitionInfo[idxPartition];
|
---|
1085 | ppi->idxPartition = idxPartition;
|
---|
1086 | ppi->offPartition = currentEbrOffset + (off_t)pEbrPartitionEntry->partitionLba * BLOCKSIZE;
|
---|
1087 | ppi->cbPartition = (off_t)pEbrPartitionEntry->partitionBlkCnt * BLOCKSIZE;
|
---|
1088 | ppi->fBootable = pEbrPartitionEntry->bootIndicator == 0x80;
|
---|
1089 | ppi->partitionType.legacy = pEbrPartitionEntry->type;
|
---|
1090 |
|
---|
1091 | g_lastPartNbr = idxPartition;
|
---|
1092 |
|
---|
1093 | if (ebr.chainingPartitionEntry.type == 0) /* end of chain */
|
---|
1094 | break;
|
---|
1095 |
|
---|
1096 | if (!PARTTYPE_IS_EXT(ebr.chainingPartitionEntry.type))
|
---|
1097 | return RTMsgErrorExitFailure("Logical partition chain broken");
|
---|
1098 |
|
---|
1099 | chainedEbrOffset = ebr.chainingPartitionEntry.partitionLba * BLOCKSIZE;
|
---|
1100 | }
|
---|
1101 | }
|
---|
1102 | return PARTITION_TABLE_MBR;
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 | const char *getClassicPartitionDesc(uint8_t type)
|
---|
1106 | {
|
---|
1107 | for (uint32_t i = 0; i < sizeof (g_partitionDescTable) / sizeof (struct PartitionDesc); i++)
|
---|
1108 | {
|
---|
1109 | if (g_partitionDescTable[i].type == type)
|
---|
1110 | return g_partitionDescTable[i].desc;
|
---|
1111 | }
|
---|
1112 | return "????";
|
---|
1113 | }
|
---|
1114 |
|
---|
1115 | void
|
---|
1116 | displayGptPartitionTable(void)
|
---|
1117 | {
|
---|
1118 |
|
---|
1119 | void *colBoot = NULL;
|
---|
1120 |
|
---|
1121 | SELFSIZINGTABLE tbl(2);
|
---|
1122 |
|
---|
1123 | /* Note: Omitting partition name column because type/UUID seems suffcient */
|
---|
1124 | void *colPartNbr = tbl.addCol("#", "%3d", 1);
|
---|
1125 |
|
---|
1126 | /* If none of the partitions supports legacy BIOS boot, don't show column */
|
---|
1127 | for (int idxPartition = 2; idxPartition <= g_lastPartNbr; idxPartition++)
|
---|
1128 | if (g_aParsedPartitionInfo[idxPartition].fBootable) {
|
---|
1129 | colBoot = tbl.addCol("Boot", "%c ", 1);
|
---|
1130 | break;
|
---|
1131 | }
|
---|
1132 |
|
---|
1133 | void *colStart = tbl.addCol("Start", "%lld", 1);
|
---|
1134 | void *colSectors = tbl.addCol("Sectors", "%lld", -1, 2);
|
---|
1135 | void *colSize = tbl.addCol("Size", "%d.%d%c", 1);
|
---|
1136 | void *colOffset = tbl.addCol("Offset", "%lld", 1);
|
---|
1137 | /* Need to see how other OSes with GPT schemes use this field. Seems like type covers it
|
---|
1138 | void *colName = tbl.addCol("Name", "%s", -1); */
|
---|
1139 | void *colType = tbl.addCol("Type", "%s", -1, 2);
|
---|
1140 |
|
---|
1141 | for (int idxPartition = 2; idxPartition <= g_lastPartNbr; idxPartition++)
|
---|
1142 | {
|
---|
1143 | PARTITIONINFO *ppi = &g_aParsedPartitionInfo[idxPartition];
|
---|
1144 | if (ppi->idxPartition)
|
---|
1145 | {
|
---|
1146 | uint8_t exp = log2((double)ppi->cbPartition);
|
---|
1147 | char scaledMagnitude = ((char []){ ' ', 'K', 'M', 'G', 'T', 'P' })[exp / 10];
|
---|
1148 |
|
---|
1149 | /* This workaround is because IPRT RT*Printf* funcs don't handle floating point format specifiers */
|
---|
1150 | double cbPartitionScaled = (double)ppi->cbPartition / pow(2, (double)(((uint8_t)(exp / 10)) * 10));
|
---|
1151 | uint8_t cbPartitionIntPart = cbPartitionScaled;
|
---|
1152 | uint8_t cbPartitionFracPart = (cbPartitionScaled - (double)cbPartitionIntPart) * 10;
|
---|
1153 |
|
---|
1154 | char abGuid[GUID_STRING_LENGTH * 2];
|
---|
1155 | RTStrPrintf(abGuid, sizeof(abGuid), "%RTuuid", &ppi->partitionType.gptGuidTypeSpecifier);
|
---|
1156 |
|
---|
1157 | char *pszPartitionTypeDesc = NULL;
|
---|
1158 | for (uint32_t i = 0; i < sizeof(g_gptPartitionTypes) / sizeof(GPTPARTITIONTYPE); i++)
|
---|
1159 | if (RTStrNICmp(abGuid, g_gptPartitionTypes[i].gptPartitionUuid, GUID_STRING_LENGTH) == 0)
|
---|
1160 | {
|
---|
1161 | pszPartitionTypeDesc = (char *)g_gptPartitionTypes[i].gptPartitionTypeDesc;
|
---|
1162 | break;
|
---|
1163 | }
|
---|
1164 |
|
---|
1165 | if (!pszPartitionTypeDesc)
|
---|
1166 | RTPrintf("Couldn't find GPT partitiontype for GUID: %s\n", abGuid);
|
---|
1167 |
|
---|
1168 | void *row = tbl.addRow();
|
---|
1169 | tbl.setCell(row, colPartNbr, idxPartition - 1);
|
---|
1170 | if (colBoot)
|
---|
1171 | tbl.setCell(row, colBoot, ppi->fBootable ? '*' : ' ');
|
---|
1172 | tbl.setCell(row, colStart, ppi->offPartition / BLOCKSIZE);
|
---|
1173 | tbl.setCell(row, colSectors, ppi->cbPartition / BLOCKSIZE);
|
---|
1174 | tbl.setCell(row, colSize, cbPartitionIntPart, cbPartitionFracPart, scaledMagnitude);
|
---|
1175 | tbl.setCell(row, colOffset, ppi->offPartition);
|
---|
1176 | /* tbl.setCell(row, colName, ppi->pszName); ... see comment for column definition */
|
---|
1177 | tbl.setCell(row, colType, SAFENULL(pszPartitionTypeDesc));
|
---|
1178 | }
|
---|
1179 | }
|
---|
1180 | tbl.displayTable();
|
---|
1181 | RTPrintf ("\n");
|
---|
1182 | }
|
---|
1183 |
|
---|
1184 | void
|
---|
1185 | displayLegacyPartitionTable(void)
|
---|
1186 | {
|
---|
1187 | /*
|
---|
1188 | * Partition table is most readable and concise when headers and columns
|
---|
1189 | * are adapted to the actual data, to avoid insufficient or excessive whitespace.
|
---|
1190 | */
|
---|
1191 | RTPrintf( "Virtual disk image:\n\n");
|
---|
1192 | RTPrintf(" Path: %s\n", g_pszBaseImagePath);
|
---|
1193 | if (g_pvDiskUuid)
|
---|
1194 | RTPrintf(" UUID: %s\n\n", g_pvDiskUuid);
|
---|
1195 |
|
---|
1196 | SELFSIZINGTABLE tbl(2);
|
---|
1197 |
|
---|
1198 | void *colPartition = tbl.addCol("Partition", "%s%d", -1);
|
---|
1199 | void *colBoot = tbl.addCol("Boot", "%c ", 1);
|
---|
1200 | void *colStart = tbl.addCol("Start", "%lld", 1);
|
---|
1201 | void *colSectors = tbl.addCol("Sectors", "%lld", -1, 2);
|
---|
1202 | void *colSize = tbl.addCol("Size", "%d.%d%c", 1);
|
---|
1203 | void *colOffset = tbl.addCol("Offset", "%lld", 1);
|
---|
1204 | void *colId = tbl.addCol("Id", "%2x", 1);
|
---|
1205 | void *colType = tbl.addCol("Type", "%s", -1, 2);
|
---|
1206 |
|
---|
1207 | for (int idxPartition = 1; idxPartition <= g_lastPartNbr; idxPartition++)
|
---|
1208 | {
|
---|
1209 | PARTITIONINFO *p = &g_aParsedPartitionInfo[idxPartition];
|
---|
1210 | if (p->idxPartition)
|
---|
1211 | {
|
---|
1212 | uint8_t exp = log2((double)p->cbPartition);
|
---|
1213 | char scaledMagnitude = ((char []){ ' ', 'K', 'M', 'G', 'T', 'P' })[exp / 10];
|
---|
1214 |
|
---|
1215 | /* This workaround is because IPRT RT*Printf* funcs don't handle floating point format specifiers */
|
---|
1216 | double cbPartitionScaled = (double)p->cbPartition / pow(2, (double)(((uint8_t)(exp / 10)) * 10));
|
---|
1217 | uint8_t cbPartitionIntPart = cbPartitionScaled;
|
---|
1218 | uint8_t cbPartitionFracPart = (cbPartitionScaled - (double)cbPartitionIntPart) * 10;
|
---|
1219 |
|
---|
1220 | void *row = tbl.addRow();
|
---|
1221 |
|
---|
1222 | tbl.setCell(row, colPartition, g_pszBaseImageName, idxPartition);
|
---|
1223 | tbl.setCell(row, colBoot, p->fBootable ? '*' : ' ');
|
---|
1224 | tbl.setCell(row, colStart, p->offPartition / BLOCKSIZE);
|
---|
1225 | tbl.setCell(row, colSectors, p->cbPartition / BLOCKSIZE);
|
---|
1226 | tbl.setCell(row, colSize, cbPartitionIntPart, cbPartitionFracPart, scaledMagnitude);
|
---|
1227 | tbl.setCell(row, colOffset, p->offPartition);
|
---|
1228 | tbl.setCell(row, colId, p->partitionType.legacy);
|
---|
1229 | tbl.setCell(row, colType, getClassicPartitionDesc((p->partitionType).legacy));
|
---|
1230 | }
|
---|
1231 | }
|
---|
1232 | tbl.displayTable();
|
---|
1233 | RTPrintf ("\n");
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 | int
|
---|
1237 | main(int argc, char **argv)
|
---|
1238 | {
|
---|
1239 |
|
---|
1240 | int rc = RTR3InitExe(argc, &argv, 0);
|
---|
1241 | if (RT_FAILURE(rc))
|
---|
1242 | return RTMsgErrorExitFailure("RTR3InitExe failed, rc=%Rrc\n", rc);
|
---|
1243 |
|
---|
1244 | rc = VDInit();
|
---|
1245 | if (RT_FAILURE(rc))
|
---|
1246 | return RTMsgErrorExitFailure("VDInit failed, rc=%Rrc\n", rc);
|
---|
1247 |
|
---|
1248 | memset(&g_vboximgOps, 0, sizeof(g_vboximgOps));
|
---|
1249 | g_vboximgOps.open = vboximgOp_open;
|
---|
1250 | g_vboximgOps.read = vboximgOp_read;
|
---|
1251 | g_vboximgOps.write = vboximgOp_write;
|
---|
1252 | g_vboximgOps.getattr = vboximgOp_getattr;
|
---|
1253 | g_vboximgOps.release = vboximgOp_release;
|
---|
1254 | g_vboximgOps.readdir = vboximgOp_readdir;
|
---|
1255 | g_vboximgOps.readlink = vboximgOp_readlink;
|
---|
1256 |
|
---|
1257 | struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
|
---|
1258 | memset(&g_vboximgOpts, 0, sizeof(g_vboximgOpts));
|
---|
1259 |
|
---|
1260 | rc = fuse_opt_parse(&args, &g_vboximgOpts, vboximgOptDefs, vboximgOptHandler);
|
---|
1261 |
|
---|
1262 | if (g_vboximgOpts.fAllowRoot)
|
---|
1263 | fuse_opt_add_arg(&args, "-oallow_root");
|
---|
1264 |
|
---|
1265 | if (rc == -1)
|
---|
1266 | return RTMsgErrorExitFailure("Couldn't parse fuse options, rc=%Rrc\n", rc);
|
---|
1267 |
|
---|
1268 | if (argc < 2 || RTStrCmp(argv[1], "-?" ) == 0 || g_vboximgOpts.fBriefUsage)
|
---|
1269 | {
|
---|
1270 | briefUsage();
|
---|
1271 | return 0;
|
---|
1272 | }
|
---|
1273 |
|
---|
1274 | /*
|
---|
1275 | * Initialize COM.
|
---|
1276 | */
|
---|
1277 | using namespace com;
|
---|
1278 | HRESULT hrc = com::Initialize();
|
---|
1279 | if (FAILED(hrc))
|
---|
1280 | {
|
---|
1281 | # ifdef VBOX_WITH_XPCOM
|
---|
1282 | if (hrc == NS_ERROR_FILE_ACCESS_DENIED)
|
---|
1283 | {
|
---|
1284 | char szHome[RTPATH_MAX] = "";
|
---|
1285 | com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome));
|
---|
1286 | return RTMsgErrorExit(RTEXITCODE_FAILURE,
|
---|
1287 | "Failed to initialize COM because the global settings directory '%s' is not accessible!", szHome);
|
---|
1288 | }
|
---|
1289 | # endif
|
---|
1290 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize COM! (hrc=%Rhrc)", hrc);
|
---|
1291 | }
|
---|
1292 |
|
---|
1293 | /*
|
---|
1294 | * Get the remote VirtualBox object and create a local session object.
|
---|
1295 | */
|
---|
1296 | ComPtr<IVirtualBoxClient> pVirtualBoxClient;
|
---|
1297 | ComPtr<IVirtualBox> pVirtualBox;
|
---|
1298 |
|
---|
1299 | hrc = pVirtualBoxClient.createInprocObject(CLSID_VirtualBoxClient);
|
---|
1300 | if (SUCCEEDED(hrc))
|
---|
1301 | hrc = pVirtualBoxClient->COMGETTER(VirtualBox)(pVirtualBox.asOutParam());
|
---|
1302 | if (FAILED(hrc))
|
---|
1303 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to get IVirtualBox object! (hrc=%Rhrc)", hrc);
|
---|
1304 |
|
---|
1305 | if (g_vboximgOpts.fVerbose)
|
---|
1306 | RTPrintf("vboximg: VirtualBox XPCOM object created\n");
|
---|
1307 |
|
---|
1308 | if (g_vboximgOpts.fList && g_vboximgOpts.pszImage == NULL)
|
---|
1309 | {
|
---|
1310 | listVMs(pVirtualBox);
|
---|
1311 | return 0;
|
---|
1312 | }
|
---|
1313 |
|
---|
1314 | ComPtr<IMedium> pBaseImageMedium = NULL;
|
---|
1315 | char *pszFormat;
|
---|
1316 | VDTYPE enmType;
|
---|
1317 | searchForBaseImage(pVirtualBox, g_vboximgOpts.pszImage, &pBaseImageMedium);
|
---|
1318 | if (pBaseImageMedium == NULL)
|
---|
1319 | {
|
---|
1320 | /*
|
---|
1321 | * Try to locate base image pMedium via the VirtualBox API, given the user-provided path
|
---|
1322 | * resolving symlinks back to hard path.
|
---|
1323 | */
|
---|
1324 | int cbNameMax = pathconf(g_vboximgOpts.pszImage, _PC_PATH_MAX);
|
---|
1325 | if (cbNameMax < 0)
|
---|
1326 | return cbNameMax;
|
---|
1327 |
|
---|
1328 | g_pszBaseImagePath = RTStrDup(g_vboximgOpts.pszImage);
|
---|
1329 | if (g_pszBaseImagePath == NULL)
|
---|
1330 | return RTMsgErrorExitFailure("out of memory\n");
|
---|
1331 |
|
---|
1332 | if (access(g_pszBaseImagePath, F_OK) < 0)
|
---|
1333 | return RTMsgErrorExitFailure("Virtual disk image not found: \"%s\"\n", g_pszBaseImagePath);
|
---|
1334 |
|
---|
1335 | if (access(g_pszBaseImagePath, R_OK) < 0)
|
---|
1336 | return RTMsgErrorExitFailure(
|
---|
1337 | "Virtual disk image not readable: \"%s\"\n", g_pszBaseImagePath);
|
---|
1338 |
|
---|
1339 | if (g_vboximgOpts.fRW && access(g_vboximgOpts.pszImage, W_OK) < 0)
|
---|
1340 | return RTMsgErrorExitFailure(
|
---|
1341 | "Virtual disk image not writeable: \"%s\"\n", g_pszBaseImagePath);
|
---|
1342 | rc = RTPathSplit(g_pszBaseImagePath, &g_u.split, sizeof(g_u), 0);
|
---|
1343 |
|
---|
1344 | if (RT_FAILURE(rc))
|
---|
1345 | return RTMsgErrorExitFailure(
|
---|
1346 | "RTPathSplit failed on '%s': %Rrc", g_pszBaseImagePath);
|
---|
1347 |
|
---|
1348 | if (!(g_u.split.fProps & RTPATH_PROP_FILENAME))
|
---|
1349 | return RTMsgErrorExitFailure(
|
---|
1350 | "RTPATH_PROP_FILENAME not set for: '%s'", g_pszBaseImagePath);
|
---|
1351 |
|
---|
1352 | g_pszBaseImageName = g_u.split.apszComps[g_u.split.cComps - 1];
|
---|
1353 | searchForBaseImage(pVirtualBox, g_pszBaseImageName, &pBaseImageMedium);
|
---|
1354 |
|
---|
1355 | if (pBaseImageMedium == NULL)
|
---|
1356 | {
|
---|
1357 | /*
|
---|
1358 | * Can't find the user specified image Medium via the VirtualBox API
|
---|
1359 | * Try to 'mount' the image via the user-provided path (without differencing images)
|
---|
1360 | * Create VirtualBox disk container and open main image
|
---|
1361 | */
|
---|
1362 | rc = VDGetFormat(NULL /* pVDIIfsDisk */, NULL /* pVDIIfsImage*/,
|
---|
1363 | g_pszBaseImagePath, &pszFormat, &enmType);
|
---|
1364 | if (RT_FAILURE(rc))
|
---|
1365 | return RTMsgErrorExitFailure("VDGetFormat(%s,) "
|
---|
1366 | "failed, rc=%Rrc\n", g_pszBaseImagePath, rc);
|
---|
1367 |
|
---|
1368 | g_pVDisk = NULL;
|
---|
1369 | rc = VDCreate(NULL /* pVDIIfsDisk */, enmType, &g_pVDisk);
|
---|
1370 | if (RT_SUCCESS(rc))
|
---|
1371 | {
|
---|
1372 | rc = VDOpen(g_pVDisk, pszFormat, g_pszBaseImagePath, 0, NULL /* pVDIfsImage */);
|
---|
1373 | if (RT_FAILURE(rc))
|
---|
1374 | {
|
---|
1375 | VDClose(g_pVDisk, false /* fDeletes */);
|
---|
1376 | return RTMsgErrorExitFailure("VDCreate(,%s,%s,,,) failed,"
|
---|
1377 | " rc=%Rrc\n", pszFormat, g_pszBaseImagePath, rc);
|
---|
1378 | }
|
---|
1379 | }
|
---|
1380 | else
|
---|
1381 | return RTMsgErrorExitFailure("VDCreate failed, rc=%Rrc\n", rc);
|
---|
1382 | }
|
---|
1383 | } else {
|
---|
1384 | Bstr pMediumUuid;
|
---|
1385 | CHECK_ERROR(pBaseImageMedium, COMGETTER(Id)(pMediumUuid.asOutParam()));
|
---|
1386 | g_pvDiskUuid = RTStrDup((char *)CSTR(pMediumUuid));
|
---|
1387 | }
|
---|
1388 |
|
---|
1389 | if (g_pVDisk == NULL)
|
---|
1390 | {
|
---|
1391 |
|
---|
1392 | com::SafeIfaceArray<IMedium> aMediumChildren;
|
---|
1393 | ComPtr<IMedium> pChild = pBaseImageMedium;
|
---|
1394 | uint32_t diffNumber = 0; /* diff # 0 = base image */
|
---|
1395 | do
|
---|
1396 | {
|
---|
1397 | Bstr pMediumName;
|
---|
1398 | Bstr pMediumPath;
|
---|
1399 |
|
---|
1400 | CHECK_ERROR(pChild, COMGETTER(Name)(pMediumName.asOutParam()));
|
---|
1401 | CHECK_ERROR(pChild, COMGETTER(Location)(pMediumPath.asOutParam()));
|
---|
1402 |
|
---|
1403 | if (pChild == pBaseImageMedium)
|
---|
1404 | {
|
---|
1405 | free((void *)g_pszBaseImageName);
|
---|
1406 | g_pszBaseImageName = RTStrDup(CSTR(pMediumName));
|
---|
1407 |
|
---|
1408 | free((void *)g_pszBaseImagePath);
|
---|
1409 | g_pszBaseImagePath = RTStrDup(CSTR(pMediumPath));
|
---|
1410 | if (g_pszBaseImageName == NULL)
|
---|
1411 | return RTMsgErrorExitFailure("out of memory\n");
|
---|
1412 |
|
---|
1413 | if (g_pszBaseImagePath == NULL)
|
---|
1414 | return RTMsgErrorExitFailure("out of memory\n");
|
---|
1415 | /*
|
---|
1416 | * Create HDD container to open base image and differencing images into
|
---|
1417 | */
|
---|
1418 | rc = VDGetFormat(NULL /* pVDIIfsDisk */, NULL /* pVDIIfsImage*/,
|
---|
1419 | g_pszBaseImagePath, &pszFormat, &enmType);
|
---|
1420 | if (RT_FAILURE(rc))
|
---|
1421 | return RTMsgErrorExitFailure("VDGetFormat(,,%s,,) "
|
---|
1422 | "failed (during HDD container creation), rc=%Rrc\n", g_pszBaseImagePath, rc);
|
---|
1423 | if (g_vboximgOpts.fVerbose)
|
---|
1424 | RTPrintf("Creating container for base image of format %s\n", pszFormat);
|
---|
1425 | /** @todo Remove I/O CB's and crit sect. when VDRead()/VDWrite() are made threadsafe */
|
---|
1426 | rc = RTCritSectInit(&g_vdioLock);
|
---|
1427 | if (RT_SUCCESS(rc))
|
---|
1428 | {
|
---|
1429 | g_VDIfThreadSync.pfnStartRead = vboximgThreadStartRead;
|
---|
1430 | g_VDIfThreadSync.pfnFinishRead = vboximgThreadFinishRead;
|
---|
1431 | g_VDIfThreadSync.pfnStartWrite = vboximgThreadStartWrite;
|
---|
1432 | g_VDIfThreadSync.pfnFinishWrite = vboximgThreadFinishWrite;
|
---|
1433 | rc = VDInterfaceAdd(&g_VDIfThreadSync.Core, "vboximg_ThreadSync", VDINTERFACETYPE_THREADSYNC,
|
---|
1434 | &g_vdioLock, sizeof(VDINTERFACETHREADSYNC), &g_pVdIfs);
|
---|
1435 | }
|
---|
1436 | else
|
---|
1437 | return RTMsgErrorExitFailure("ERROR: Failed to create critsects "
|
---|
1438 | "for virtual disk I/O, rc=%Rrc\n", rc);
|
---|
1439 |
|
---|
1440 | g_pVDisk = NULL;
|
---|
1441 | rc = VDCreate(g_pVdIfs, enmType, &g_pVDisk);
|
---|
1442 | if (NS_FAILED(rc))
|
---|
1443 | return RTMsgErrorExitFailure("ERROR: Couldn't create virtual disk container\n");
|
---|
1444 | }
|
---|
1445 | /** @todo (end of to do section) */
|
---|
1446 |
|
---|
1447 | if ( g_vboximgOpts.cHddImageDiffMax != 0 && diffNumber > g_vboximgOpts.cHddImageDiffMax)
|
---|
1448 | break;
|
---|
1449 |
|
---|
1450 | if (g_vboximgOpts.fVerbose)
|
---|
1451 | {
|
---|
1452 | if (diffNumber == 0)
|
---|
1453 | RTPrintf("\nvboximg-mount: Opening base image into container:\n %s\n",
|
---|
1454 | g_pszBaseImagePath);
|
---|
1455 | else
|
---|
1456 | RTPrintf("\nvboximg-mount: Opening difference image #%d into container:\n %s\n",
|
---|
1457 | diffNumber, g_pszBaseImagePath);
|
---|
1458 | }
|
---|
1459 |
|
---|
1460 | rc = VDOpen(g_pVDisk, pszFormat, g_pszBaseImagePath, 0, NULL /* pVDIfsImage */);
|
---|
1461 | if (RT_FAILURE(rc))
|
---|
1462 | {
|
---|
1463 | VDClose(g_pVDisk, false /* fDeletes */);
|
---|
1464 | return RTMsgErrorExitFailure("VDOpen(,,%s,,) failed, rc=%Rrc\n",
|
---|
1465 | g_pszBaseImagePath, rc);
|
---|
1466 | }
|
---|
1467 |
|
---|
1468 | CHECK_ERROR(pChild, COMGETTER(Children)(ComSafeArrayAsOutParam(aMediumChildren)));
|
---|
1469 |
|
---|
1470 | if (aMediumChildren.size() != 0) {
|
---|
1471 | pChild = aMediumChildren[0];
|
---|
1472 | }
|
---|
1473 |
|
---|
1474 | aMediumChildren.setNull();
|
---|
1475 |
|
---|
1476 | ++diffNumber;
|
---|
1477 |
|
---|
1478 |
|
---|
1479 | } while(NS_SUCCEEDED(rc) && aMediumChildren.size());
|
---|
1480 | }
|
---|
1481 |
|
---|
1482 | g_cReaders = VDIsReadOnly(g_pVDisk) ? INT32_MAX / 2 : 0;
|
---|
1483 | g_cWriters = 0;
|
---|
1484 | g_cbEntireVDisk = VDGetSize(g_pVDisk, 0 /* base */);
|
---|
1485 |
|
---|
1486 | if (g_vboximgOpts.fList)
|
---|
1487 | {
|
---|
1488 | if (g_pVDisk == NULL)
|
---|
1489 | return RTMsgErrorExitFailure("No valid --image to list partitions from\n");
|
---|
1490 |
|
---|
1491 | RTPrintf("\n");
|
---|
1492 |
|
---|
1493 | rc = parsePartitionTable();
|
---|
1494 | switch(rc)
|
---|
1495 | {
|
---|
1496 | case PARTITION_TABLE_MBR:
|
---|
1497 | displayLegacyPartitionTable();
|
---|
1498 | break;
|
---|
1499 | case PARTITION_TABLE_GPT:
|
---|
1500 | displayGptPartitionTable();
|
---|
1501 | break;
|
---|
1502 | default:
|
---|
1503 | return rc;
|
---|
1504 | }
|
---|
1505 | return 0;
|
---|
1506 | }
|
---|
1507 | if (g_vboximgOpts.idxPartition >= 0)
|
---|
1508 | {
|
---|
1509 | if (g_vboximgOpts.offset)
|
---|
1510 | return RTMsgErrorExitFailure("--offset and --partition are mutually exclusive options\n");
|
---|
1511 |
|
---|
1512 | if (g_vboximgOpts.size)
|
---|
1513 | return RTMsgErrorExitFailure("--size and --partition are mutually exclusive options\n");
|
---|
1514 |
|
---|
1515 | /*
|
---|
1516 | * --partition option specified. That will set the global offset and limit
|
---|
1517 | * honored by the disk read and write sanitizers to constrain operations
|
---|
1518 | * to within the specified partion based on an initial parsing of the MBR
|
---|
1519 | */
|
---|
1520 | rc = parsePartitionTable();
|
---|
1521 | if (rc < 0)
|
---|
1522 | return RTMsgErrorExitFailure("Error parsing disk MBR/Partition table\n");
|
---|
1523 | int partNbr = g_vboximgOpts.idxPartition;
|
---|
1524 |
|
---|
1525 | if (partNbr < 0 || partNbr > g_lastPartNbr)
|
---|
1526 | return RTMsgErrorExitFailure("Non-valid partition number specified\n");
|
---|
1527 |
|
---|
1528 | if (partNbr == 0)
|
---|
1529 | {
|
---|
1530 | g_vDiskOffset = 0;
|
---|
1531 | g_vDiskSize = VDGetSize(g_pVDisk, 0);
|
---|
1532 | if (g_vboximgOpts.fVerbose)
|
---|
1533 | RTPrintf("Partition 0 specified - Whole disk will be accessible\n");
|
---|
1534 | } else {
|
---|
1535 | for (int i = 0; i < g_lastPartNbr; i++)
|
---|
1536 | {
|
---|
1537 | /* If GPT, display vboximg's representation of partition table starts at partition 2
|
---|
1538 | * but the table is displayed calling it partition 1, because the protective MBR
|
---|
1539 | * record is relatively pointless to display or reference in this context */
|
---|
1540 |
|
---|
1541 | if (g_aParsedPartitionInfo[i].idxPartition == partNbr + g_fGPT ? 1 : 0)
|
---|
1542 | {
|
---|
1543 | g_vDiskOffset = g_aParsedPartitionInfo[i].offPartition;
|
---|
1544 | g_vDiskSize = g_vDiskOffset + g_aParsedPartitionInfo[i].cbPartition;
|
---|
1545 | if (g_vboximgOpts.fVerbose)
|
---|
1546 | RTPrintf("Partition %d specified. Only sectors %llu to %llu of disk will be accessible\n",
|
---|
1547 | g_vboximgOpts.idxPartition, g_vDiskOffset / BLOCKSIZE, g_vDiskSize / BLOCKSIZE);
|
---|
1548 | }
|
---|
1549 | }
|
---|
1550 | }
|
---|
1551 | } else {
|
---|
1552 | if (g_vboximgOpts.offset) {
|
---|
1553 | if (g_vboximgOpts.offset < 0 || g_vboximgOpts.offset + g_vboximgOpts.size > g_cbEntireVDisk)
|
---|
1554 | return RTMsgErrorExitFailure("User specified offset out of range of virtual disk\n");
|
---|
1555 |
|
---|
1556 | if (g_vboximgOpts.fVerbose)
|
---|
1557 | RTPrintf("Setting r/w bias (offset) to user requested value for sector %llu\n", g_vDiskOffset / BLOCKSIZE);
|
---|
1558 |
|
---|
1559 | g_vDiskOffset = g_vboximgOpts.offset;
|
---|
1560 | }
|
---|
1561 | if (g_vboximgOpts.size) {
|
---|
1562 | if (g_vboximgOpts.size < 0 || g_vboximgOpts.offset + g_vboximgOpts.size > g_cbEntireVDisk)
|
---|
1563 | return RTMsgErrorExitFailure("User specified size out of range of virtual disk\n");
|
---|
1564 |
|
---|
1565 | if (g_vboximgOpts.fVerbose)
|
---|
1566 | RTPrintf("Setting r/w size limit to user requested value %llu\n", g_vDiskSize / BLOCKSIZE);
|
---|
1567 |
|
---|
1568 | g_vDiskSize = g_vboximgOpts.size;
|
---|
1569 | }
|
---|
1570 | }
|
---|
1571 | if (g_vDiskSize == 0)
|
---|
1572 | g_vDiskSize = g_cbEntireVDisk - g_vDiskOffset;
|
---|
1573 |
|
---|
1574 | /*
|
---|
1575 | * Hand control over to libfuse.
|
---|
1576 | */
|
---|
1577 | if (g_vboximgOpts.fVerbose)
|
---|
1578 | RTPrintf("\nvboximg-mount: Going into background...\n");
|
---|
1579 |
|
---|
1580 | rc = fuse_main(args.argc, args.argv, &g_vboximgOps, NULL);
|
---|
1581 |
|
---|
1582 | int rc2 = VDClose(g_pVDisk, false /* fDelete */);
|
---|
1583 | AssertRC(rc2);
|
---|
1584 | RTPrintf("vboximg-mount: fuse_main -> %d\n", rc);
|
---|
1585 | return rc;
|
---|
1586 | }
|
---|
1587 |
|
---|