VirtualBox

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


Ignore:
Timestamp:
May 14, 2008 1:51:24 PM (17 years ago)
Author:
vboxsync
Message:

Improve raw vmdk disk size detection on Linux, implement disk size detecion on OS X.

File:
1 edited

Legend:

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

    r8516 r8811  
    5353#include <windows.h>
    5454#include <winioctl.h>
    55 #elif RT_OS_LINUX
     55#elif defined(RT_OS_LINUX) || defined(RT_OS_DARWIN)
    5656#include <errno.h>
    5757#include <sys/ioctl.h>
    5858#include <sys/types.h>
    5959#include <sys/stat.h>
     60#include <fcntl.h>
    6061#include <unistd.h>
     62#endif
     63#ifdef RT_OS_LINUX
     64#include <sys/utsname.h>
    6165#include <linux/hdreg.h>
    6266#include <linux/fs.h>
    63 #endif /* !RT_OS_WINDOWS && !RT_OS_LINUX */
     67#endif /* RT_OS_LINUX */
     68#ifdef RT_OS_DARWIN
     69#include <sys/disk.h>
     70#endif /* RT_OS_DARWIN */
    6471
    6572using namespace com;
     
    800807    if (!fstat(RawFile, &DevStat) && S_ISBLK(DevStat.st_mode))
    801808    {
    802         /** @todo add a BLKGETSIZE64 ioctl here, as it eliminates the 2 TByte
    803          * limit. But be careful, Linux 2.4.18 is the first revision with
    804          * working BLKGETSIZE64, and in 2.5.x it's pretty random. 2.6.0 works. */
    805         long cBlocks;
    806         if (!ioctl(RawFile, BLKGETSIZE, &cBlocks))
    807             cbSize = (uint64_t)cBlocks * 512;
     809#ifdef BLKGETSIZE64
     810        /* BLKGETSIZE64 is broken up to 2.4.17 and in many 2.5.x. In 2.6.0
     811         * it works without problems. */
     812        struct utsname utsname;
     813        if (    uname(&utsname) == 0
     814            &&  (   (strncmp(utsname.release, "2.5.", 4) == 0 && atoi(&utsname.release[4]) >= 18)
     815                 || (strncmp(utsname.release, "2.", 2) == 0 && atoi(&utsname.release[2]) >= 6)))
     816        {
     817            uint64_t cbBlk;
     818            if (!ioctl(RawFile, BLKGETSIZE64, &cbBlk))
     819                cbSize = cbBlk;
     820        }
     821#endif /* BLKGETSIZE64 */
     822        if (!cbSize)
     823        {
     824            long cBlocks;
     825            if (!ioctl(RawFile, BLKGETSIZE, &cBlocks))
     826                cbSize = (uint64_t)cBlocks << 9;
     827            else
     828                return RTErrConvertFromErrno(errno);
     829        }
     830    }
     831    else
     832    {
     833        RTPrintf("File '%s' is no disk\n", rawdisk.raw());
     834        return VERR_INVALID_PARAMETER;
     835    }
     836#elif defined(RT_OS_DARWIN)
     837    struct stat DevStat;
     838    if (!fstat(RawFile, &DevStat) && S_ISBLK(DevStat.st_mode))
     839    {
     840        uint64_t cBlocks;
     841        uint32_t cbBlock;
     842        if (!ioctl(fd, DKIOCGETBLOCKCOUNT, &cBlocks))
     843        {
     844            if (!ioctl(fd, DKIOCGETBLOCKSIZE, &cbBlock))
     845                cbSize = cBlocks * cbBlock;
     846            else
     847                return RTErrConvertFromErrno(errno);
     848        }
    808849        else
    809850            return RTErrConvertFromErrno(errno);
     
    814855        return VERR_INVALID_PARAMETER;
    815856    }
    816 #else /* !RT_OS_WINDOWS && !RT_OS_LINUX */
     857#else /* all unrecognized OSes */
    817858    /* Hopefully this works on all other hosts. If it doesn't, it'll just fail
    818859     * creating the VMDK, so no real harm done. */
     
    823864        return vrc;
    824865    }
    825 #endif /* !RT_OS_WINDOWS && !RT_OS_LINUX */
     866#endif
    826867
    827868    PVBOXHDD pDisk = NULL;
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