-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvixDiskLibSample.cpp
More file actions
1945 lines (1722 loc) · 59.4 KB
/
vixDiskLibSample.cpp
File metadata and controls
1945 lines (1722 loc) · 59.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* *************************************************
* Copyright 2007-2015 VMware, Inc. All rights reserved.
* *************************************************/
/*
* vixDiskLibSample.cpp --
*
* Sample program to demonstrate usage of vixDiskLib DLL.
*/
#ifdef _WIN32
#include <windows.h>
#include <tchar.h>
#include <process.h>
#else
#include <dlfcn.h>
#include <sys/time.h>
#endif
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include <stdexcept>
#include "vixDiskLib.h"
using std::cout;
using std::string;
using std::endl;
using std::vector;
#define COMMAND_CREATE (1 << 0)
#define COMMAND_DUMP (1 << 1)
#define COMMAND_FILL (1 << 2)
#define COMMAND_INFO (1 << 3)
#define COMMAND_REDO (1 << 4)
#define COMMAND_DUMP_META (1 << 5)
#define COMMAND_READ_META (1 << 6)
#define COMMAND_WRITE_META (1 << 7)
#define COMMAND_MULTITHREAD (1 << 8)
#define COMMAND_CLONE (1 << 9)
#define COMMAND_READBENCH (1 << 10)
#define COMMAND_WRITEBENCH (1 << 11)
#define COMMAND_CHECKREPAIR (1 << 12)
#define VIXDISKLIB_VERSION_MAJOR 6
#define VIXDISKLIB_VERSION_MINOR 0
// Default buffer size (in sectors) for read/write benchmarks
#define DEFAULT_BUFSIZE 128
// Print updated statistics for read/write benchmarks roughly every
// BUFS_PER_STAT sectors (current value is 64MBytes worth of data)
#define BUFS_PER_STAT (128 * 1024)
// Character array for randonm filename generation
static const char randChars[] = "0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
// Per-thread information for multi-threaded VixDiskLib test.
struct ThreadData {
std::string dstDisk;
VixDiskLibHandle srcHandle;
VixDiskLibHandle dstHandle;
VixDiskLibSectorType numSectors;
};
static struct {
int command;
VixDiskLibAdapterType adapterType;
char *transportModes;
char *diskPath;
char *parentPath;
char *metaKey;
char *metaVal;
int filler;
unsigned mbSize;
VixDiskLibSectorType numSectors;
VixDiskLibSectorType startSector;
VixDiskLibSectorType bufSize;
uint32 openFlags;
unsigned numThreads;
Bool success;
Bool isRemote;
char *host;
char *userName;
char *password;
char *thumbPrint;
int port;
char *srcPath;
VixDiskLibConnection connection;
char *vmxSpec;
bool useInitEx;
char *cfgFile;
char *libdir;
char *ssMoRef;
int repair;
} appGlobals;
static int ParseArguments(int argc, char* argv[]);
static void DoCreate(void);
static void DoRedo(void);
static void DoFill(void);
static void DoDump(void);
static void DoReadMetadata(void);
static void DoWriteMetadata(void);
static void DoDumpMetadata(void);
static void DoInfo(void);
static void DoTestMultiThread(void);
static void DoClone(void);
static int BitCount(int number);
static void DumpBytes(const uint8 *buf, size_t n, int step);
static void DoRWBench(bool read);
static void DoCheckRepair(Bool repair);
#define THROW_ERROR(vixError) \
throw VixDiskLibErrWrapper((vixError), __FILE__, __LINE__)
#define CHECK_AND_THROW(vixError) \
do { \
if (VIX_FAILED((vixError))) { \
throw VixDiskLibErrWrapper((vixError), __FILE__, __LINE__); \
} \
} while (0)
#ifdef DYNAMIC_LOADING
static VixError
(*VixDiskLib_InitEx_Ptr)(uint32 majorVersion,
uint32 minorVersion,
VixDiskLibGenericLogFunc *log,
VixDiskLibGenericLogFunc *warn,
VixDiskLibGenericLogFunc *panic,
const char* libDir,
const char* configFile);
static VixError
(*VixDiskLib_Init_Ptr)(uint32 majorVersion,
uint32 minorVersion,
VixDiskLibGenericLogFunc *log,
VixDiskLibGenericLogFunc *warn,
VixDiskLibGenericLogFunc *panic,
const char* libDir);
static void
(*VixDiskLib_Exit_Ptr)(void);
static const char *
(*VixDiskLib_ListTransportModes_Ptr)(void);
static VixError
(*VixDiskLib_Cleanup_Ptr)(const VixDiskLibConnectParams *connectParams,
uint32 *numCleanedUp, uint32 *numRemaining);
static VixError
(*VixDiskLib_Connect_Ptr)(const VixDiskLibConnectParams *connectParams,
VixDiskLibConnection *connection);
static VixError
(*VixDiskLib_ConnectEx_Ptr)(const VixDiskLibConnectParams *connectParams,
Bool readOnly,
const char *snapshotRef,
const char *transportModes,
VixDiskLibConnection *connection);
static VixError
(*VixDiskLib_Disconnect_Ptr)(VixDiskLibConnection connection);
static VixError
(*VixDiskLib_Create_Ptr)(const VixDiskLibConnection connection,
const char *path,
const VixDiskLibCreateParams *createParams,
VixDiskLibProgressFunc progressFunc,
void *progressCallbackData);
static VixError
(*VixDiskLib_CreateChild_Ptr)(VixDiskLibHandle diskHandle,
const char *childPath,
VixDiskLibDiskType diskType,
VixDiskLibProgressFunc progressFunc,
void *progressCallbackData);
static VixError
(*VixDiskLib_Open_Ptr)(const VixDiskLibConnection connection,
const char *path,
uint32 flags,
VixDiskLibHandle *diskHandle);
static VixError
(*VixDiskLib_GetInfo_Ptr)(VixDiskLibHandle diskHandle,
VixDiskLibInfo **info);
static void
(*VixDiskLib_FreeInfo_Ptr)(VixDiskLibInfo *info);
static const char *
(*VixDiskLib_GetTransportMode_Ptr)(VixDiskLibHandle diskHandle);
static VixError
(*VixDiskLib_Close_Ptr)(VixDiskLibHandle diskHandle);
static VixError
(*VixDiskLib_Read_Ptr)(VixDiskLibHandle diskHandle,
VixDiskLibSectorType startSector,
VixDiskLibSectorType numSectors,
uint8 *readBuffer);
static VixError
(*VixDiskLib_Write_Ptr)(VixDiskLibHandle diskHandle,
VixDiskLibSectorType startSector,
VixDiskLibSectorType numSectors,
const uint8 *writeBuffer);
static VixError
(*VixDiskLib_ReadMetadata_Ptr)(VixDiskLibHandle diskHandle,
const char *key,
char *buf,
size_t bufLen,
size_t *requiredLen);
static VixError
(*VixDiskLib_WriteMetadata_Ptr)(VixDiskLibHandle diskHandle,
const char *key,
const char *val);
static VixError
(*VixDiskLib_GetMetadataKeys_Ptr)(VixDiskLibHandle diskHandle,
char *keys,
size_t maxLen,
size_t *requiredLen);
static VixError
(*VixDiskLib_Unlink_Ptr)(VixDiskLibConnection connection,
const char *path);
static VixError
(*VixDiskLib_Grow_Ptr)(VixDiskLibConnection connection,
const char *path,
VixDiskLibSectorType capacity,
Bool updateGeometry,
VixDiskLibProgressFunc progressFunc,
void *progressCallbackData);
static VixError
(*VixDiskLib_Shrink_Ptr)(VixDiskLibHandle diskHandle,
VixDiskLibProgressFunc progressFunc,
void *progressCallbackData);
static VixError
(*VixDiskLib_Defragment_Ptr)(VixDiskLibHandle diskHandle,
VixDiskLibProgressFunc progressFunc,
void *progressCallbackData);
static VixError
(*VixDiskLib_Rename_Ptr)(const char *srcFileName,
const char *dstFileName);
static VixError
(*VixDiskLib_Clone_Ptr)(const VixDiskLibConnection dstConnection,
const char *dstPath,
const VixDiskLibConnection srcConnection,
const char *srcPath,
const VixDiskLibCreateParams *vixCreateParams,
VixDiskLibProgressFunc progressFunc,
void *progressCallbackData,
Bool overWrite);
static char *
(*VixDiskLib_GetErrorText_Ptr)(VixError err, const char *locale);
static void
(*VixDiskLib_FreeErrorText_Ptr)(char* errMsg);
static VixError
(*VixDiskLib_Attach_Ptr)(VixDiskLibHandle parent, VixDiskLibHandle child);
static VixError
(*VixDiskLib_SpaceNeededForClone_Ptr)(VixDiskLibHandle diskHandle,
VixDiskLibDiskType cloneDiskType,
uint64* spaceNeeded);
static VixError
(*VixDiskLib_CheckRepair_Ptr)(const VixDiskLibConnection connection,
const char *filename,
Bool repair);
/*
*----------------------------------------------------------------------
*
* LoadOneFunc --
*
* Loads a single vixDiskLib function from shared library / DLL.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
#ifdef _WIN32
static void
LoadOneFunc(HINSTANCE hInstLib, void** pFunction, const char* funcName)
{
std::stringstream strStream;
*pFunction = GetProcAddress(hInstLib, funcName);
if (*pFunction == NULL) {
strStream << "Failed to load " << funcName << ". Error = " <<
GetLastError() << "\n";
throw std::runtime_error(strStream.str().c_str());
}
}
#else
static void
LoadOneFunc(void* dlHandle, void** pFunction, const char* funcName)
{
std::stringstream strStream;
*pFunction = dlsym(dlHandle, funcName);
char* dlErrStr = dlerror();
if (*pFunction == NULL || dlErrStr != NULL) {
strStream << "Failed to load " << funcName << ". Error = " <<
dlErrStr << "\n";
throw std::runtime_error(strStream.str().c_str());
}
}
#endif
#define LOAD_ONE_FUNC(handle, funcName) \
LoadOneFunc(handle, (void**)&(funcName##_Ptr), #funcName)
#ifdef _WIN32
#define IS_HANDLE_INVALID(handle) ((handle) == INVALID_HANDLE_VALUE)
#else
#define IS_HANDLE_INVALID(handle) ((handle) == NULL)
#endif
/*
*----------------------------------------------------------------------
*
* DynLoadDiskLib --
*
* Dynamically loads VixDiskLib and bind to the functions.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
DynLoadDiskLib(void)
{
#ifdef _WIN32
HINSTANCE hInstLib = LoadLibrary("vixDiskLib.dll");
#else
void* hInstLib = dlopen("libvixDiskLib.so", RTLD_LAZY);
#endif
// If the handle is valid, try to get the function address.
if (IS_HANDLE_INVALID(hInstLib)) {
cout << "Can't load vixDiskLib shared library / DLL : lasterror = " <<
#ifdef _WIN32
GetLastError() <<
#else
dlerror() <<
#endif
"\n";
exit(EXIT_FAILURE);
}
try {
LOAD_ONE_FUNC(hInstLib, VixDiskLib_InitEx);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_Init);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_Exit);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_ListTransportModes);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_Cleanup);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_Connect);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_ConnectEx);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_Disconnect);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_Create);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_CreateChild);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_Open);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_GetInfo);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_FreeInfo);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_GetTransportMode);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_Close);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_Read);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_Write);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_ReadMetadata);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_WriteMetadata);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_GetMetadataKeys);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_Unlink);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_Grow);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_Shrink);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_Defragment);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_Rename);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_Clone);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_GetErrorText);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_FreeErrorText);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_Attach);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_SpaceNeededForClone);
LOAD_ONE_FUNC(hInstLib, VixDiskLib_CheckRepair);
} catch (const std::runtime_error& exc) {
cout << "Error while dynamically loading : " << exc.what() << "\n";
exit(EXIT_FAILURE);
}
}
#define VixDiskLib_InitEx (*VixDiskLib_InitEx_Ptr)
#define VixDiskLib_Init (*VixDiskLib_Init_Ptr)
#define VixDiskLib_Exit (*VixDiskLib_Exit_Ptr)
#define VixDiskLib_ListTransportModes (*VixDiskLib_ListTransportModes_Ptr)
#define VixDiskLib_Cleanup (*VixDiskLib_Cleanup_Ptr)
#define VixDiskLib_Connect (*VixDiskLib_Connect_Ptr)
#define VixDiskLib_ConnectEx (*VixDiskLib_ConnectEx_Ptr)
#define VixDiskLib_Disconnect (*VixDiskLib_Disconnect_Ptr)
#define VixDiskLib_Create (*VixDiskLib_Create_Ptr)
#define VixDiskLib_CreateChild (*VixDiskLib_CreateChild_Ptr)
#define VixDiskLib_Open (*VixDiskLib_Open_Ptr)
#define VixDiskLib_GetInfo (*VixDiskLib_GetInfo_Ptr)
#define VixDiskLib_FreeInfo (*VixDiskLib_FreeInfo_Ptr)
#define VixDiskLib_GetTransportMode (*VixDiskLib_GetTransportMode_Ptr)
#define VixDiskLib_Close (*VixDiskLib_Close_Ptr)
#define VixDiskLib_Read (*VixDiskLib_Read_Ptr)
#define VixDiskLib_Write (*VixDiskLib_Write_Ptr)
#define VixDiskLib_ReadMetadata (*VixDiskLib_ReadMetadata_Ptr)
#define VixDiskLib_WriteMetadata (*VixDiskLib_WriteMetadata_Ptr)
#define VixDiskLib_GetMetadataKeys (*VixDiskLib_GetMetadataKeys_Ptr)
#define VixDiskLib_Unlink (*VixDiskLib_Unlink_Ptr)
#define VixDiskLib_Grow (*VixDiskLib_Grow_Ptr)
#define VixDiskLib_Shrink (*VixDiskLib_Shrink_Ptr)
#define VixDiskLib_Defragment (*VixDiskLib_Defragment_Ptr)
#define VixDiskLib_Rename (*VixDiskLib_Rename_Ptr)
#define VixDiskLib_Clone (*VixDiskLib_Clone_Ptr)
#define VixDiskLib_GetErrorText (*VixDiskLib_GetErrorText_Ptr)
#define VixDiskLib_FreeErrorText (*VixDiskLib_FreeErrorText_Ptr)
#define VixDiskLib_Attach (*VixDiskLib_Attach_Ptr)
#define VixDiskLib_SpaceNeededForClone (*VixDiskLib_SpaceNeededForClone_Ptr)
#define VixDiskLib_CheckRepair (*VixDiskLib_CheckRepair_Ptr)
#endif // DYNAMIC_LOADING
/*
*----------------------------------------------------------------------
*
* GenerateRandomFilename --
*
* Generate and return a random filename.
*
* Results:
* None
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
GenerateRandomFilename(const string& prefix, string& randomFilename)
{
string retStr;
int strLen = sizeof(randChars) - 1;
for (unsigned int i = 0; i < 8; i++)
{
retStr += randChars[rand() % strLen];
}
randomFilename = prefix + retStr;
}
#ifdef _WIN32
/*
*----------------------------------------------------------------------
*
* gettimeofday --
*
* Mimics BSD style gettimeofday in a way that is close enough
* for some I/O benchmarking.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
gettimeofday(struct timeval *tv,
void *)
{
uint64 ticks = GetTickCount();
tv->tv_sec = ticks / 1000;
tv->tv_usec = 1000 * (ticks % 1000);
}
#endif
/*
*--------------------------------------------------------------------------
*
* LogFunc --
*
* Callback for VixDiskLib Log messages.
*
* Results:
* None.
*
* Side effects:
* None.
*
*--------------------------------------------------------------------------
*/
static void
LogFunc(const char *fmt, va_list args)
{
printf("Log: ");
vprintf(fmt, args);
}
/*
*--------------------------------------------------------------------------
*
* WarnFunc --
*
* Callback for VixDiskLib Warning messages.
*
* Results:
* None.
*
* Side effects:
* None.
*
*--------------------------------------------------------------------------
*/
static void
WarnFunc(const char *fmt, va_list args)
{
printf("Warning: ");
vprintf(fmt, args);
}
/*
*--------------------------------------------------------------------------
*
* PanicFunc --
*
* Callback for VixDiskLib Panic messages.
*
* Results:
* None.
*
* Side effects:
* None.
*
*--------------------------------------------------------------------------
*/
static void
PanicFunc(const char *fmt, va_list args)
{
printf("Panic: ");
vprintf(fmt, args);
exit(10);
}
typedef void (VixDiskLibGenericLogFunc)(const char *fmt, va_list args);
// Wrapper class for VixDiskLib disk objects.
class VixDiskLibErrWrapper
{
public:
explicit VixDiskLibErrWrapper(VixError errCode, const char* file, int line)
:
_errCode(errCode),
_file(file),
_line(line)
{
char* msg = VixDiskLib_GetErrorText(errCode, NULL);
_desc = msg;
VixDiskLib_FreeErrorText(msg);
}
VixDiskLibErrWrapper(const char* description, const char* file, int line)
:
_errCode(VIX_E_FAIL),
_desc(description),
_file(file),
_line(line)
{
}
string Description() const { return _desc; }
VixError ErrorCode() const { return _errCode; }
string File() const { return _file; }
int Line() const { return _line; }
private:
VixError _errCode;
string _desc;
string _file;
int _line;
};
class VixDisk
{
public:
VixDiskLibHandle Handle() { return _handle; }
VixDisk(VixDiskLibConnection connection, char *path, uint32 flags)
{
_handle = NULL;
VixError vixError = VixDiskLib_Open(connection, path, flags, &_handle);
CHECK_AND_THROW(vixError);
printf("Disk \"%s\" is open using transport mode \"%s\".\n",
path, VixDiskLib_GetTransportMode(_handle));
}
~VixDisk()
{
if (_handle) {
VixDiskLib_Close(_handle);
}
_handle = NULL;
}
private:
VixDiskLibHandle _handle;
};
/*
*--------------------------------------------------------------------------
*
* PrintUsage --
*
* Displays the usage message.
*
* Results:
* 1.
*
* Side effects:
* None.
*
*--------------------------------------------------------------------------
*/
static int
PrintUsage(void)
{
printf("Usage: vixdisklibsample.exe command [options] diskPath\n\n");
printf("List of commands (all commands are mutually exclusive):\n");
printf(" -create : creates a sparse virtual disk with capacity "
"specified by -cap\n");
printf(" -redo parentPath : creates a redo log 'diskPath' "
"for base disk 'parentPath'\n");
printf(" -info : displays information for specified virtual disk\n");
printf(" -dump : dumps the contents of specified range of sectors "
"in hexadecimal\n");
printf(" -fill : fills specified range of sectors with byte value "
"specified by -val\n");
printf(" -wmeta key value : writes (key,value) entry into disk's metadata table\n");
printf(" -rmeta key : displays the value of the specified metada entry\n");
printf(" -meta : dumps all entries of the disk's metadata\n");
printf(" -clone sourcePath : clone source vmdk possibly to a remote site\n");
printf(" -readbench blocksize: Does a read benchmark on a disk using the \n");
printf("specified I/O block size (in sectors).\n");
printf(" -writebench blocksize: Does a write benchmark on a disk using the\n");
printf("specified I/O block size (in sectors). WARNING: This will\n");
printf("overwrite the contents of the disk specified.\n");
printf(" -check repair: Check a sparse disk for internal consistency, "
"where repair is a boolean value to indicate if a repair operation "
"should be attempted.\n\n");
printf("options:\n");
printf(" -adapter [ide|scsi] : bus adapter type for 'create' option "
"(default='scsi')\n");
printf(" -start n : start sector for 'dump/fill' options (default=0)\n");
printf(" -count n : number of sectors for 'dump/fill' options (default=1)\n");
printf(" -val byte : byte value to fill with for 'write' option (default=255)\n");
printf(" -cap megabytes : capacity in MB for -create option (default=100)\n");
printf(" -single : open file as single disk link (default=open entire chain)\n");
printf(" -multithread n: start n threads and copy the file to n new files\n");
printf(" -host hostname : hostname/IP address of VC/vSphere host (Mandatory)\n");
printf(" -user userid : user name on host (Mandatory) \n");
printf(" -password password : password on host. (Mandatory)\n");
printf(" -port port : port to use to connect to host (default = 443) \n");
printf(" -vm moref=id : id is the managed object reference of the VM \n");
printf(" -libdir dir : Folder location of the VDDK installation. "
"On Windows, the bin folder holds the plugin. On Linux, it is "
"the lib64 directory\n");
printf(" -initex configfile : Specify path and filename of config file \n");
printf(" -ssmoref moref : Managed object reference of VM snapshot \n");
printf(" -mode mode : Mode string to pass into VixDiskLib_ConnectEx. "
"Valid modes are: nbd, nbdssl, san, hotadd \n");
printf(" -thumb string : Provides a SSL thumbprint string for validation. "
"Format: xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx\n");
return 1;
}
/*
*--------------------------------------------------------------------------
*
* main --
*
* Main routine of the program.
*
* Results:
* None.
*
* Side effects:
* None.
*
*--------------------------------------------------------------------------
*/
int
main(int argc, char* argv[])
{
int retval;
bool bVixInit(false);
memset(&appGlobals, 0, sizeof appGlobals);
appGlobals.command = 0;
appGlobals.adapterType = VIXDISKLIB_ADAPTER_SCSI_BUSLOGIC;
appGlobals.startSector = 0;
appGlobals.numSectors = 1;
appGlobals.mbSize = 100;
appGlobals.filler = 0xff;
appGlobals.openFlags = 0;
appGlobals.numThreads = 1;
appGlobals.success = TRUE;
appGlobals.isRemote = FALSE;
retval = ParseArguments(argc, argv);
if (retval) {
return retval;
}
#ifdef DYNAMIC_LOADING
DynLoadDiskLib();
#endif
// Initialize random generator
struct timeval time;
gettimeofday(&time, NULL);
srand((time.tv_sec * 1000) + (time.tv_usec/1000));
VixDiskLibConnectParams cnxParams = {0};
VixError vixError;
try {
if (appGlobals.isRemote) {
cnxParams.vmxSpec = appGlobals.vmxSpec;
cnxParams.serverName = appGlobals.host;
cnxParams.credType = VIXDISKLIB_CRED_UID;
cnxParams.creds.uid.userName = appGlobals.userName;
cnxParams.creds.uid.password = appGlobals.password;
cnxParams.thumbPrint = appGlobals.thumbPrint;
cnxParams.port = appGlobals.port;
}
if (appGlobals.useInitEx) {
vixError = VixDiskLib_InitEx(VIXDISKLIB_VERSION_MAJOR,
VIXDISKLIB_VERSION_MINOR,
&LogFunc, &WarnFunc, &PanicFunc,
appGlobals.libdir,
appGlobals.cfgFile);
} else {
vixError = VixDiskLib_Init(VIXDISKLIB_VERSION_MAJOR,
VIXDISKLIB_VERSION_MINOR,
NULL, NULL, NULL, // Log, warn, panic
appGlobals.libdir);
}
CHECK_AND_THROW(vixError);
bVixInit = true;
if (appGlobals.vmxSpec != NULL) {
vixError = VixDiskLib_PrepareForAccess(&cnxParams, "Sample");
CHECK_AND_THROW(vixError);
}
if (appGlobals.ssMoRef == NULL && appGlobals.transportModes == NULL) {
vixError = VixDiskLib_Connect(&cnxParams,
&appGlobals.connection);
} else {
Bool ro = (appGlobals.openFlags & VIXDISKLIB_FLAG_OPEN_READ_ONLY);
vixError = VixDiskLib_ConnectEx(&cnxParams, ro, appGlobals.ssMoRef,
appGlobals.transportModes,
&appGlobals.connection);
}
CHECK_AND_THROW(vixError);
if (appGlobals.command & COMMAND_INFO) {
DoInfo();
} else if (appGlobals.command & COMMAND_CREATE) {
DoCreate();
} else if (appGlobals.command & COMMAND_REDO) {
DoRedo();
} else if (appGlobals.command & COMMAND_FILL) {
DoFill();
} else if (appGlobals.command & COMMAND_DUMP) {
DoDump();
} else if (appGlobals.command & COMMAND_READ_META) {
DoReadMetadata();
} else if (appGlobals.command & COMMAND_WRITE_META) {
DoWriteMetadata();
} else if (appGlobals.command & COMMAND_DUMP_META) {
DoDumpMetadata();
} else if (appGlobals.command & COMMAND_MULTITHREAD) {
DoTestMultiThread();
} else if (appGlobals.command & COMMAND_CLONE) {
DoClone();
} else if (appGlobals.command & COMMAND_READBENCH) {
DoRWBench(true);
} else if (appGlobals.command & COMMAND_WRITEBENCH) {
DoRWBench(false);
} else if (appGlobals.command & COMMAND_CHECKREPAIR) {
DoCheckRepair(appGlobals.repair);
}
retval = 0;
} catch (const VixDiskLibErrWrapper& e) {
cout << "Error: [" << e.File() << ":" << e.Line() << "] " <<
std::hex << e.ErrorCode() << " " << e.Description() << "\n";
retval = 1;
}
if (appGlobals.vmxSpec != NULL) {
vixError = VixDiskLib_EndAccess(&cnxParams, "Sample");
}
if (appGlobals.connection != NULL) {
VixDiskLib_Disconnect(appGlobals.connection);
}
if (bVixInit) {
VixDiskLib_Exit();
}
return retval;
}
/*
*--------------------------------------------------------------------------
*
* ParseArguments --
*
* Parses the arguments passed on the command line.
*
* Results:
* None.
*
* Side effects:
* None.
*
*--------------------------------------------------------------------------
*/
static int
ParseArguments(int argc, char* argv[])
{
int i;
if (argc < 3) {
printf("Error: Too few arguments. See usage below.\n\n");
return PrintUsage();
}
for (i = 1; i < argc - 1; i++) {
if (!strcmp(argv[i], "-info")) {
appGlobals.command |= COMMAND_INFO;
appGlobals.openFlags |= VIXDISKLIB_FLAG_OPEN_READ_ONLY;
} else if (!strcmp(argv[i], "-create")) {
appGlobals.command |= COMMAND_CREATE;
} else if (!strcmp(argv[i], "-dump")) {
appGlobals.command |= COMMAND_DUMP;
appGlobals.openFlags |= VIXDISKLIB_FLAG_OPEN_READ_ONLY;
} else if (!strcmp(argv[i], "-fill")) {
appGlobals.command |= COMMAND_FILL;
} else if (!strcmp(argv[i], "-meta")) {
appGlobals.command |= COMMAND_DUMP_META;
appGlobals.openFlags |= VIXDISKLIB_FLAG_OPEN_READ_ONLY;
} else if (!strcmp(argv[i], "-single")) {
appGlobals.openFlags |= VIXDISKLIB_FLAG_OPEN_SINGLE_LINK;
} else if (!strcmp(argv[i], "-adapter")) {
if (i >= argc - 2) {
printf("Error: The -adaptor option requires the adapter type "
"to be specified. The type must be 'ide' or 'scsi'. "
"See usage below.\n\n");
return PrintUsage();
}
appGlobals.adapterType = strcmp(argv[i], "scsi") == 0 ?
VIXDISKLIB_ADAPTER_SCSI_BUSLOGIC :
VIXDISKLIB_ADAPTER_IDE;
++i;
} else if (!strcmp(argv[i], "-rmeta")) {
appGlobals.command |= COMMAND_READ_META;
if (i >= argc - 2) {
printf("Error: The -rmeta command requires a key value to "
"be specified. See usage below.\n\n");
return PrintUsage();
}
appGlobals.metaKey = argv[++i];
appGlobals.openFlags |= VIXDISKLIB_FLAG_OPEN_READ_ONLY;
} else if (!strcmp(argv[i], "-wmeta")) {
appGlobals.command |= COMMAND_WRITE_META;
if (i >= argc - 3) {
printf("Error: The -wmeta command requires key and value to "
"be specified. See usage below.\n\n");
return PrintUsage();
}
appGlobals.metaKey = argv[++i];
appGlobals.metaVal = argv[++i];
} else if (!strcmp(argv[i], "-redo")) {
if (i >= argc - 2) {
printf("Error: The -redo command requires the parentPath to "
"be specified. See usage below.\n\n");
return PrintUsage();
}
appGlobals.command |= COMMAND_REDO;
appGlobals.parentPath = argv[++i];
} else if (!strcmp(argv[i], "-val")) {
if (i >= argc - 2) {
printf("Error: The -val option requires a byte value to "
"be specified. See usage below.\n\n");
return PrintUsage();
}
appGlobals.filler = strtol(argv[++i], NULL, 0);
} else if (!strcmp(argv[i], "-start")) {
if (i >= argc - 2) {
printf("Error: The -start option requires a sector number to "
"be specified. See usage below.\n\n");
return PrintUsage();
}
appGlobals.startSector = strtol(argv[++i], NULL, 0);
} else if (!strcmp(argv[i], "-count")) {
if (i >= argc - 2) {
printf("Error: The -count option requires the number of "
"sectors to be specified. See usage below.\n\n");
return PrintUsage();
}
appGlobals.numSectors = strtol(argv[++i], NULL, 0);
} else if (!strcmp(argv[i], "-cap")) {
if (i >= argc - 2) {
printf("Error: The -cap option requires the capacity in MB "
"to be specified. See usage below.\n\n");
return PrintUsage();
}
appGlobals.mbSize = strtol(argv[++i], NULL, 0);
} else if (!strcmp(argv[i], "-clone")) {
if (i >= argc - 2) {
printf("Error: The -clone command requires the path of the "
"source vmdk to be specified. See usage below.\n\n");
return PrintUsage();
}
appGlobals.srcPath = argv[++i];
appGlobals.command |= COMMAND_CLONE;
} else if (!strcmp(argv[i], "-readbench")) {
if (0 && i >= argc - 2) {
printf("Error: The -readbench command requires a block size "
"(in sectors) to be specified. See usage below.\n\n");
return PrintUsage();
}
appGlobals.bufSize = strtol(argv[++i], NULL, 0);
appGlobals.command |= COMMAND_READBENCH;
appGlobals.openFlags |= VIXDISKLIB_FLAG_OPEN_READ_ONLY;
} else if (!strcmp(argv[i], "-writebench")) {
if (i >= argc - 2) {
printf("Error: The -writebench command requires a block size "
"(in sectors) to be specified. See usage below.\n\n");
return PrintUsage();
}
appGlobals.bufSize = strtol(argv[++i], NULL, 0);
appGlobals.command |= COMMAND_WRITEBENCH;