VirtualBox

Changeset 11066 in vbox for trunk/src/VBox/Frontends


Ignore:
Timestamp:
Aug 1, 2008 2:47:33 PM (17 years ago)
Author:
vboxsync
Message:

VBoxManage: Implement converting arbitrary image formats supported by VBoxHDD-new (i.e. VDI, VMDK, VHD) to raw.

Location:
trunk/src/VBox/Frontends/VBoxManage
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VBoxManage/VBoxInternalManage.cpp

    r10715 r11066  
    121121             "Commands:\n"
    122122             "\n"
    123              "%s%s%s%s%s%s%s"
     123             "%s%s%s%s%s%s%s%s"
    124124             "WARNING: This is a development tool and shall only be used to analyse\n"
    125125             "         problems. It is completely unsupported and will change in\n"
     
    171171                 "\n"
    172172                 : "",
     173             (u64Cmd & USAGE_CONVERTTORAW) ?
     174                 "  converttoraw [-format <fileformat>] <filename> <outputfile>"
     175#ifdef ENABLE_CONVERT_RAW_TO_STDOUT
     176                 "|stdout"
     177#endif /* ENABLE_CONVERT_RAW_TO_STDOUT */
     178                 "\n"
     179                 "       Convert image to raw, writing to file"
     180#ifdef ENABLE_CONVERT_RAW_TO_STDOUT
     181                 " or stdout"
     182#endif /* ENABLE_CONVERT_RAW_TO_STDOUT */
     183                 ".\n"
     184                 "\n"
     185                 : "",
    173186#ifdef RT_OS_WINDOWS
    174187            (u64Cmd & USAGE_MODINSTALL) ?
     
    12381251}
    12391252
     1253static int CmdConvertToRaw(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
     1254{
     1255    Bstr srcformat;
     1256    Bstr src;
     1257    Bstr dst;
     1258    bool fWriteToStdOut = false;
     1259
     1260    /* Parse the arguments. */
     1261    for (int i = 0; i < argc; i++)
     1262    {
     1263        if (strcmp(argv[i], "-format") == 0)
     1264        {
     1265            if (argc <= i + 1)
     1266            {
     1267                return errorArgument("Missing argument to '%s'", argv[i]);
     1268            }
     1269            i++;
     1270            srcformat = argv[i];
     1271        }
     1272        else if (src.isEmpty())
     1273        {
     1274            src = argv[i];
     1275        }
     1276        else if (dst.isEmpty())
     1277        {
     1278            dst = argv[i];
     1279#ifdef ENABLE_CONVERT_RAW_TO_STDOUT
     1280            if (!strcmp(argv[i], "stdout"))
     1281                fWriteToStdOut = true;
     1282#endif /* ENABLE_CONVERT_RAW_TO_STDOUT */
     1283        }
     1284        else
     1285        {
     1286            return errorSyntax(USAGE_CONVERTTORAW, "Invalid parameter '%s'", Utf8Str(argv[i]).raw());
     1287        }
     1288    }
     1289
     1290    if (src.isEmpty())
     1291        return errorSyntax(USAGE_CONVERTTORAW, "Mandatory filename parameter missing");
     1292    if (dst.isEmpty())
     1293        return errorSyntax(USAGE_CONVERTTORAW, "Mandatory outputfile parameter missing");
     1294
     1295    PVBOXHDD pDisk = NULL;
     1296
     1297    VDINTERFACE      vdInterfaceError;
     1298    VDINTERFACEERROR vdInterfaceErrorCallbacks;
     1299    vdInterfaceErrorCallbacks.cbSize       = sizeof(VDINTERFACEERROR);
     1300    vdInterfaceErrorCallbacks.enmInterface = VDINTERFACETYPE_ERROR;
     1301    vdInterfaceErrorCallbacks.pfnError     = handleVDError;
     1302
     1303    int vrc = VDInterfaceCreate(&vdInterfaceError, "VBoxManage_IError", VDINTERFACETYPE_ERROR,
     1304                                &vdInterfaceErrorCallbacks, NULL, NULL);
     1305    AssertRC(vrc);
     1306
     1307    vrc = VDCreate(&vdInterfaceError, &pDisk);
     1308    if (VBOX_FAILURE(vrc))
     1309    {
     1310        RTPrintf("Error while creating the virtual disk container: %Vrc\n", vrc);
     1311        return vrc;
     1312    }
     1313
     1314    /* Open raw output file. */
     1315    RTFILE outFile;
     1316    vrc = VINF_SUCCESS;
     1317    if (fWriteToStdOut)
     1318        outFile = 1;
     1319    else
     1320        vrc = RTFileOpen(&outFile, Utf8Str(dst).raw(), RTFILE_O_OPEN | RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_ALL);
     1321    if (VBOX_FAILURE(vrc))
     1322    {
     1323        VDCloseAll(pDisk);
     1324        RTPrintf("Error while creating destination file \"%s\": %Vrc\n", Utf8Str(dst).raw(), vrc);
     1325        return vrc;
     1326    }
     1327
     1328    if (srcformat.isEmpty())
     1329    {
     1330        char *pszFormat = NULL;
     1331        vrc = VDGetFormat(Utf8Str(src).raw(), &pszFormat);
     1332        if (VBOX_FAILURE(vrc))
     1333        {
     1334            VDCloseAll(pDisk);
     1335            if (!fWriteToStdOut)
     1336            {
     1337                RTFileClose(outFile);
     1338                RTFileDelete(Utf8Str(dst).raw());
     1339            }
     1340            RTPrintf("No file format specified and autodetect failed - please specify format: %Vrc\n", vrc);
     1341            return vrc;
     1342        }
     1343        srcformat = pszFormat;
     1344        RTStrFree(pszFormat);
     1345    }
     1346    vrc = VDOpen(pDisk, Utf8Str(srcformat).raw(), Utf8Str(src).raw(), VD_OPEN_FLAGS_READONLY);
     1347    if (VBOX_FAILURE(vrc))
     1348    {
     1349        VDCloseAll(pDisk);
     1350        if (!fWriteToStdOut)
     1351        {
     1352            RTFileClose(outFile);
     1353            RTFileDelete(Utf8Str(dst).raw());
     1354        }
     1355        RTPrintf("Error while opening the source image: %Vrc\n", vrc);
     1356        return vrc;
     1357    }
     1358
     1359    uint64_t cbSize = VDGetSize(pDisk, VD_LAST_IMAGE);
     1360    uint64_t offFile = 0;
     1361#define RAW_BUFFER_SIZE _128K
     1362    uint64_t cbBuf = RAW_BUFFER_SIZE;
     1363    void *pvBuf = RTMemAlloc(cbBuf);
     1364    if (pvBuf)
     1365    {
     1366        RTPrintf("Converting image \"%s\" with size %RU64 bytes (%RU64MB) to raw...\n", Utf8Str(src).raw(), cbSize, (cbSize + _1M - 1) / _1M);
     1367        while (offFile < cbSize)
     1368        {
     1369            size_t cb = cbSize - offFile >= (uint64_t)cbBuf ? cbBuf : (size_t)(cbSize - offFile);
     1370            vrc = VDRead(pDisk, offFile, pvBuf, cb);
     1371            if (VBOX_FAILURE(vrc))
     1372                break;
     1373            vrc = RTFileWrite(outFile, pvBuf, cb, NULL);
     1374            if (VBOX_FAILURE(vrc))
     1375                break;
     1376            offFile += cb;
     1377        }
     1378        if (VBOX_FAILURE(vrc))
     1379        {
     1380            VDCloseAll(pDisk);
     1381            if (!fWriteToStdOut)
     1382            {
     1383                RTFileClose(outFile);
     1384                RTFileDelete(Utf8Str(dst).raw());
     1385            }
     1386            RTPrintf("Error copying image data: %Vrc\n", vrc);
     1387            return vrc;
     1388        }
     1389    }
     1390    else
     1391    {
     1392        vrc = VERR_NO_MEMORY;
     1393        VDCloseAll(pDisk);
     1394        if (!fWriteToStdOut)
     1395        {
     1396            RTFileClose(outFile);
     1397            RTFileDelete(Utf8Str(dst).raw());
     1398        }
     1399        RTPrintf("Error allocating read buffer: %Vrc\n", vrc);
     1400        return vrc;
     1401    }
     1402
     1403    if (!fWriteToStdOut)
     1404        RTFileClose(outFile);
     1405    VDCloseAll(pDisk);
     1406    return vrc;
     1407}
     1408
    12401409/**
    12411410 * Unloads the neccessary driver.
     
    13001469    if (!strcmp(pszCmd, "renamevmdk"))
    13011470        return CmdRenameVMDK(argc - 1, &argv[1], aVirtualBox, aSession);
     1471    if (!strcmp(pszCmd, "converttoraw"))
     1472        return CmdConvertToRaw(argc - 1, &argv[1], aVirtualBox, aSession);
    13021473
    13031474    if (!strcmp(pszCmd, "modinstall"))
  • trunk/src/VBox/Frontends/VBoxManage/VBoxManage.h

    r11031 r11066  
    7373#define USAGE_GUESTPROPERTY         RT_BIT_64(40)
    7474#endif  /* VBOX_WITH_GUEST_PROPS defined */
     75#define USAGE_CONVERTTORAW          RT_BIT_64(41)
    7576#define USAGE_ALL                   (~(uint64_t)0)
    7677/** @} */
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette