summaryrefslogtreecommitdiffstats
path: root/src/VBox/Storage/testcase/tstVDFill.cpp
blob: 5d46eddd6f5b3dee5b04c2f31e4cae13819845c6 (plain)
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
/** @file
 *
 * Test utility to fill a given image with random data up to a certain size (sequentially).
 */

/*
 * Copyright (C) 2016-2022 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include <VBox/vd.h>
#include <iprt/errcore.h>
#include <VBox/log.h>
#include <iprt/asm.h>
#include <iprt/dir.h>
#include <iprt/string.h>
#include <iprt/stream.h>
#include <iprt/file.h>
#include <iprt/mem.h>
#include <iprt/initterm.h>
#include <iprt/getopt.h>
#include <iprt/rand.h>


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
/** The error count. */
unsigned g_cErrors = 0;
/** Global RNG state. */
RTRAND   g_hRand;

#define TSTVDFILL_TEST_PATTERN_SIZE _1M

static DECLCALLBACK(void) tstVDError(void *pvUser, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va)
{
    RT_NOREF1(pvUser);
    g_cErrors++;
    RTPrintf("tstVDFill: Error %Rrc at %s:%u (%s): ", rc, RT_SRC_POS_ARGS);
    RTPrintfV(pszFormat, va);
    RTPrintf("\n");
}

static DECLCALLBACK(int) tstVDMessage(void *pvUser, const char *pszFormat, va_list va)
{
    RT_NOREF1(pvUser);
    RTPrintf("tstVDFill: ");
    RTPrintfV(pszFormat, va);
    return VINF_SUCCESS;
}

static int tstFill(const char *pszFilename, const char *pszFormat, bool fStreamOptimized, uint64_t cbDisk, uint64_t cbFill)
{
    int rc;
    PVDISK pVD = NULL;
    VDGEOMETRY       PCHS = { 0, 0, 0 };
    VDGEOMETRY       LCHS = { 0, 0, 0 };
    PVDINTERFACE     pVDIfs = NULL;
    VDINTERFACEERROR VDIfError;

    /** Buffer storing the random test pattern. */
    uint8_t *pbTestPattern = NULL;

    /* Create the virtual disk test data */
    pbTestPattern = (uint8_t *)RTMemAlloc(TSTVDFILL_TEST_PATTERN_SIZE);

    RTRandAdvBytes(g_hRand, pbTestPattern, TSTVDFILL_TEST_PATTERN_SIZE);

    RTPrintf("Disk size is %llu bytes\n", cbDisk);

    /* Create error interface. */
    /* Create error interface. */
    VDIfError.pfnError = tstVDError;
    VDIfError.pfnMessage = tstVDMessage;

    rc = VDInterfaceAdd(&VDIfError.Core, "tstVD_Error", VDINTERFACETYPE_ERROR,
                        NULL, sizeof(VDINTERFACEERROR), &pVDIfs);
    AssertRC(rc);

#define CHECK(str) \
    do \
    { \
        RTPrintf("%s rc=%Rrc\n", str, rc); \
        if (RT_FAILURE(rc)) \
        { \
            if (pbTestPattern) \
                RTMemFree(pbTestPattern); \
            VDDestroy(pVD); \
            g_cErrors++; \
            return rc; \
        } \
    } while (0)

    rc = VDCreate(pVDIfs, VDTYPE_HDD, &pVD);
    CHECK("VDCreate()");

    rc = VDCreateBase(pVD, pszFormat, pszFilename, cbDisk,
                      fStreamOptimized ? VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED : VD_IMAGE_FLAGS_NONE,
                      "Test image", &PCHS, &LCHS, NULL, VD_OPEN_FLAGS_NORMAL,
                      NULL, NULL);
    CHECK("VDCreateBase()");

    uint64_t uOff = 0;
    uint64_t cbGb = 0;
    while (   uOff < cbFill
           && RT_SUCCESS(rc))
    {
        size_t cbThisWrite = RT_MIN(TSTVDFILL_TEST_PATTERN_SIZE, cbFill - uOff);
        rc = VDWrite(pVD, uOff, pbTestPattern, cbThisWrite);
        if (RT_SUCCESS(rc))
        {
            uOff += cbThisWrite;
            cbGb += cbThisWrite;
            /* Print a message for every GB we wrote. */
            if (cbGb >= _1G)
            {
                RTStrmPrintf(g_pStdErr, "Wrote %llu bytes\n", uOff);
                cbGb = 0;
            }
        }
    }

    VDDestroy(pVD);
    if (pbTestPattern)
        RTMemFree(pbTestPattern);

#undef CHECK
    return rc;
}

/**
 * Shows help message.
 */
static void printUsage(void)
{
    RTPrintf("Usage:\n"
             "--disk-size <size in MB>    Size of the disk\n"
             "--fill-size <size in MB>    How much to fill\n"
             "--filename <filename>       Filename of the image\n"
             "--format <VDI|VMDK|...>     Format to use\n"
             "--streamoptimized           Use the stream optimized format\n"
             "--help                      Show this text\n");
}

static const RTGETOPTDEF g_aOptions[] =
{
    { "--disk-size",       's', RTGETOPT_REQ_UINT64 },
    { "--fill-size",       'f', RTGETOPT_REQ_UINT64 },
    { "--filename",        'p', RTGETOPT_REQ_STRING },
    { "--format",          't', RTGETOPT_REQ_STRING },
    { "--streamoptimized", 'r', RTGETOPT_REQ_NOTHING },
    { "--help",            'h', RTGETOPT_REQ_NOTHING }
};

int main(int argc, char *argv[])
{
    RTR3InitExe(argc, &argv, 0);
    int rc;
    RTGETOPTUNION ValueUnion;
    RTGETOPTSTATE GetState;
    char c;
    uint64_t cbDisk = 0;
    uint64_t cbFill = 0;
    const char *pszFilename = NULL;
    const char *pszFormat = NULL;
    bool fStreamOptimized = false;

    rc = VDInit();
    if (RT_FAILURE(rc))
        return RTEXITCODE_FAILURE;

    RTGetOptInit(&GetState, argc, argv, g_aOptions,
                 RT_ELEMENTS(g_aOptions), 1, RTGETOPTINIT_FLAGS_NO_STD_OPTS);

    while (   RT_SUCCESS(rc)
           && (c = RTGetOpt(&GetState, &ValueUnion)))
    {
        switch (c)
        {
            case 's':
                cbDisk = ValueUnion.u64 * _1M;
                break;
            case 'f':
                cbFill = ValueUnion.u64 * _1M;
                break;
            case 'p':
                pszFilename = ValueUnion.psz;
                break;
            case 't':
                pszFormat = ValueUnion.psz;
                break;
            case 'r':
                fStreamOptimized = true;
                break;
            case 'h':
            default:
                printUsage();
                break;
        }
    }

    if (!cbDisk || !cbFill || !pszFilename || !pszFormat)
    {
        RTPrintf("tstVDFill: Arguments missing!\n");
        return 1;
    }

    rc = RTRandAdvCreateParkMiller(&g_hRand);
    if (RT_FAILURE(rc))
    {
        RTPrintf("tstVDFill: Creating RNG failed rc=%Rrc\n", rc);
        return 1;
    }

    RTRandAdvSeed(g_hRand, 0x12345678);

    rc = tstFill(pszFilename, pszFormat, fStreamOptimized, cbDisk, cbFill);
    if (RT_FAILURE(rc))
        RTPrintf("tstVDFill: Filling disk failed! rc=%Rrc\n", rc);

    rc = VDShutdown();
    if (RT_FAILURE(rc))
        RTPrintf("tstVDFill: unloading backends failed! rc=%Rrc\n", rc);

    return RTEXITCODE_SUCCESS;
}