VirtualBox

Changeset 51286 in vbox


Ignore:
Timestamp:
May 19, 2014 11:29:30 AM (11 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
93754
Message:

Storage/vbox-img: add command to tweak the LCHS geometry of images

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Storage/testcase/vbox-img.cpp

    r48847 r51286  
    55
    66/*
    7  * Copyright (C) 2010-2012 Oracle Corporation
     7 * Copyright (C) 2010-2014 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    3535#include <iprt/vfs.h>
    3636
    37 const char *g_pszProgName = "";
     37static const char *g_pszProgName = "";
    3838static void printUsage(PRTSTREAM pStrm)
    3939{
     
    4545                 "                [--parentuuid <uuid>]\n"
    4646                 "                [--zeroparentuuid]\n"
     47                 "\n"
     48                 "   geometry     --filename <filename>\n"
     49                 "                [--format VDI|VMDK|VHD|...]\n"
     50                 "                [--clearchs]\n"
     51                 "                [--cylinders <number>]\n"
     52                 "                [--heads <number>]\n"
     53                 "                [--sectors <number>]\n"
    4754                 "\n"
    4855                 "   convert      --srcfilename <filename>\n"
     
    7582}
    7683
    77 void showLogo(PRTSTREAM pStrm)
     84static void showLogo(PRTSTREAM pStrm)
    7885{
    7986    static bool s_fShown; /* show only once */
     
    96103};
    97104
    98 PVDINTERFACE pVDIfs;
     105static PVDINTERFACE pVDIfs;
    99106
    100107static DECLCALLBACK(void) handleVDError(void *pvUser, int rc, RT_SRC_POS_DECL,
     
    116123 * Print a usage synopsis and the syntax error message.
    117124 */
    118 int errorSyntax(const char *pszFormat, ...)
     125static int errorSyntax(const char *pszFormat, ...)
    119126{
    120127    va_list args;
     
    127134}
    128135
    129 int errorRuntime(const char *pszFormat, ...)
     136static int errorRuntime(const char *pszFormat, ...)
    130137{
    131138    va_list args;
     
    185192
    186193
    187 int handleSetUUID(HandlerArg *a)
     194static int handleSetUUID(HandlerArg *a)
    188195{
    189196    const char *pszFilename = NULL;
     
    301308        if (RT_FAILURE(rc))
    302309            return errorRuntime("Cannot set parent UUID of virtual disk image \"%s\": %Rrc\n",
     310                                pszFilename, rc);
     311    }
     312
     313    VDDestroy(pVD);
     314
     315    if (pszFormat)
     316    {
     317        RTStrFree(pszFormat);
     318        pszFormat = NULL;
     319    }
     320
     321    return 0;
     322}
     323
     324
     325static int handleGeometry(HandlerArg *a)
     326{
     327    const char *pszFilename = NULL;
     328    char *pszFormat = NULL;
     329    VDTYPE enmType = VDTYPE_INVALID;
     330    uint16_t cCylinders = 0;
     331    uint8_t cHeads = 0;
     332    uint8_t cSectors = 0;
     333    bool fCylinders = false;
     334    bool fHeads = false;
     335    bool fSectors = false;
     336    int rc;
     337
     338    /* Parse the command line. */
     339    static const RTGETOPTDEF s_aOptions[] =
     340    {
     341        { "--filename", 'f', RTGETOPT_REQ_STRING },
     342        { "--format", 'o', RTGETOPT_REQ_STRING },
     343        { "--clearchs", 'C', RTGETOPT_REQ_NOTHING },
     344        { "--cylinders", 'c', RTGETOPT_REQ_UINT16 },
     345        { "--heads", 'e', RTGETOPT_REQ_UINT8 },
     346        { "--sectors", 's', RTGETOPT_REQ_UINT8 }
     347    };
     348    int ch;
     349    RTGETOPTUNION ValueUnion;
     350    RTGETOPTSTATE GetState;
     351    RTGetOptInit(&GetState, a->argc, a->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0 /* fFlags */);
     352    while ((ch = RTGetOpt(&GetState, &ValueUnion)))
     353    {
     354        switch (ch)
     355        {
     356            case 'f':   // --filename
     357                pszFilename = ValueUnion.psz;
     358                break;
     359            case 'o':   // --format
     360                pszFormat = RTStrDup(ValueUnion.psz);
     361                break;
     362            case 'C':   // --clearchs
     363                cCylinders = 0;
     364                cHeads = 0;
     365                cSectors = 0;
     366                fCylinders = true;
     367                fHeads = true;
     368                fSectors = true;
     369                break;
     370            case 'c':   // --cylinders
     371                cCylinders = ValueUnion.u16;
     372                fCylinders = true;
     373                break;
     374            case 'e':   // --heads
     375                cHeads = ValueUnion.u8;
     376                fHeads = true;
     377                break;
     378            case 's':   // --sectors
     379                cSectors = ValueUnion.u8;
     380                fSectors = true;
     381                break;
     382
     383            default:
     384                ch = RTGetOptPrintError(ch, &ValueUnion);
     385                printUsage(g_pStdErr);
     386                return ch;
     387        }
     388    }
     389
     390    /* Check for mandatory parameters. */
     391    if (!pszFilename)
     392        return errorSyntax("Mandatory --filename option missing\n");
     393
     394    /* Autodetect image format. */
     395    if (!pszFormat)
     396    {
     397        /* Don't pass error interface, as that would triggers error messages
     398         * because some backends fail to open the image. */
     399        rc = VDGetFormat(NULL, NULL, pszFilename, &pszFormat, &enmType);
     400        if (RT_FAILURE(rc))
     401            return errorRuntime("Format autodetect failed: %Rrc\n", rc);
     402    }
     403
     404    PVBOXHDD pVD = NULL;
     405    rc = VDCreate(pVDIfs, enmType, &pVD);
     406    if (RT_FAILURE(rc))
     407        return errorRuntime("Cannot create the virtual disk container: %Rrc\n", rc);
     408
     409    /* Open in info mode to be able to open diff images without their parent. */
     410    rc = VDOpen(pVD, pszFormat, pszFilename, VD_OPEN_FLAGS_INFO, NULL);
     411    if (RT_FAILURE(rc))
     412        return errorRuntime("Cannot open the virtual disk image \"%s\": %Rrc\n",
     413                            pszFilename, rc);
     414
     415    VDGEOMETRY oldLCHSGeometry;
     416    rc = VDGetLCHSGeometry(pVD, VD_LAST_IMAGE, &oldLCHSGeometry);
     417    if (rc == VERR_VD_GEOMETRY_NOT_SET)
     418    {
     419        memset(&oldLCHSGeometry, 0, sizeof(oldLCHSGeometry));
     420        rc = VINF_SUCCESS;
     421    }
     422    if (RT_FAILURE(rc))
     423        return errorRuntime("Cannot get LCHS geometry of virtual disk image \"%s\": %Rrc\n",
     424                            pszFilename, rc);
     425
     426    RTPrintf("Old image LCHS: %u/%u/%u\n", oldLCHSGeometry.cCylinders, oldLCHSGeometry.cHeads, oldLCHSGeometry.cSectors);
     427
     428    VDGEOMETRY newLCHSGeometry = oldLCHSGeometry;
     429    if (fCylinders)
     430        newLCHSGeometry.cCylinders = cCylinders;
     431    if (fHeads)
     432        newLCHSGeometry.cHeads = cHeads;
     433    if (fSectors)
     434        newLCHSGeometry.cSectors = cSectors;
     435
     436    if (fCylinders || fHeads || fSectors)
     437    {
     438        RTPrintf("New image LCHS: %u/%u/%u\n", newLCHSGeometry.cCylinders, newLCHSGeometry.cHeads, newLCHSGeometry.cSectors);
     439
     440        rc = VDSetLCHSGeometry(pVD, VD_LAST_IMAGE, &newLCHSGeometry);
     441        if (RT_FAILURE(rc))
     442            return errorRuntime("Cannot set LCHS geometry of virtual disk image \"%s\": %Rrc\n",
    303443                                pszFilename, rc);
    304444    }
     
    688828}
    689829
    690 int handleConvert(HandlerArg *a)
     830static int handleConvert(HandlerArg *a)
    691831{
    692832    const char *pszSrcFilename = NULL;
     
    9141054
    9151055
    916 int handleInfo(HandlerArg *a)
     1056static int handleInfo(HandlerArg *a)
    9171057{
    9181058    int rc = VINF_SUCCESS;
     
    10631203} VBOXIMGVFS, *PVBOXIMGVFS;
    10641204
    1065 int handleCompact(HandlerArg *a)
     1205static int handleCompact(HandlerArg *a)
    10661206{
    10671207    int rc = VINF_SUCCESS;
     
    12441384
    12451385
    1246 int handleCreateCache(HandlerArg *a)
     1386static int handleCreateCache(HandlerArg *a)
    12471387{
    12481388    int rc = VINF_SUCCESS;
     
    13391479}
    13401480
    1341 int handleCreateBase(HandlerArg *a)
     1481static int handleCreateBase(HandlerArg *a)
    13421482{
    13431483    int rc = VINF_SUCCESS;
     
    13531493    VDINTERFACECONFIG vdIfCfg;
    13541494
    1355     memset(&LCHSGeometry, 0, sizeof(VDGEOMETRY));
    1356     memset(&PCHSGeometry, 0, sizeof(VDGEOMETRY));
     1495    memset(&LCHSGeometry, 0, sizeof(LCHSGeometry));
     1496    memset(&PCHSGeometry, 0, sizeof(PCHSGeometry));
    13571497
    13581498    /* Parse the command line. */
     
    14411581
    14421582
    1443 int handleRepair(HandlerArg *a)
     1583static int handleRepair(HandlerArg *a)
    14441584{
    14451585    int rc = VINF_SUCCESS;
     
    15081648
    15091649
    1510 int handleClearComment(HandlerArg *a)
     1650static int handleClearComment(HandlerArg *a)
    15111651{
    15121652    int rc = VINF_SUCCESS;
     
    16491789    {
    16501790        { "setuuid",      handleSetUUID      },
     1791        { "geometry",     handleGeometry     },
    16511792        { "convert",      handleConvert      },
    16521793        { "info",         handleInfo         },
Note: See TracChangeset for help on using the changeset viewer.

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