VirtualBox

Ignore:
Timestamp:
Dec 20, 2011 11:10:43 AM (13 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
75481
Message:

FE/VBoxManage: Add support for raw image files in createrawvmdk (based on a patch from Pawel Jakub Dawidek)

File:
1 edited

Legend:

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

    r39576 r39660  
    11161116    else
    11171117    {
     1118        /*
     1119         * Could be raw image, remember error code and try to get the size first
     1120         * before failing.
     1121         */
    11181122        vrc = RTErrConvertFromWin32(GetLastError());
    1119         RTMsgError("Cannot get the geometry of the raw disk '%s': %Rrc", rawdisk.c_str(), vrc);
    1120         goto out;
     1123        if (RT_FAILURE(RTFileGetSize(hRawFile, &cbSize)))
     1124        {
     1125            RTMsgError("Cannot get the geometry of the raw disk '%s': %Rrc", rawdisk.c_str(), vrc);
     1126            goto out;
     1127        }
     1128        else
     1129            vrc = VINF_SUCCESS;
    11211130    }
    11221131#elif defined(RT_OS_LINUX)
    11231132    struct stat DevStat;
    1124     if (!fstat(RTFileToNative(hRawFile), &DevStat) && S_ISBLK(DevStat.st_mode))
    1125     {
     1133    if(!fstat(RTFileToNative(hRawFile), &DevStat))
     1134    {
     1135        if (S_ISBLK(DevStat.st_mode))
     1136        {
    11261137#ifdef BLKGETSIZE64
    1127         /* BLKGETSIZE64 is broken up to 2.4.17 and in many 2.5.x. In 2.6.0
    1128          * it works without problems. */
    1129         struct utsname utsname;
    1130         if (    uname(&utsname) == 0
    1131             &&  (   (strncmp(utsname.release, "2.5.", 4) == 0 && atoi(&utsname.release[4]) >= 18)
    1132                  || (strncmp(utsname.release, "2.", 2) == 0 && atoi(&utsname.release[2]) >= 6)))
    1133         {
    1134             uint64_t cbBlk;
    1135             if (!ioctl(RTFileToNative(hRawFile), BLKGETSIZE64, &cbBlk))
    1136                 cbSize = cbBlk;
    1137         }
     1138            /* BLKGETSIZE64 is broken up to 2.4.17 and in many 2.5.x. In 2.6.0
     1139             * it works without problems. */
     1140            struct utsname utsname;
     1141            if (    uname(&utsname) == 0
     1142                &&  (   (strncmp(utsname.release, "2.5.", 4) == 0 && atoi(&utsname.release[4]) >= 18)
     1143                     || (strncmp(utsname.release, "2.", 2) == 0 && atoi(&utsname.release[2]) >= 6)))
     1144            {
     1145                uint64_t cbBlk;
     1146                if (!ioctl(RTFileToNative(hRawFile), BLKGETSIZE64, &cbBlk))
     1147                    cbSize = cbBlk;
     1148            }
    11381149#endif /* BLKGETSIZE64 */
    1139         if (!cbSize)
    1140         {
    1141             long cBlocks;
    1142             if (!ioctl(RTFileToNative(hRawFile), BLKGETSIZE, &cBlocks))
    1143                 cbSize = (uint64_t)cBlocks << 9;
     1150            if (!cbSize)
     1151            {
     1152                long cBlocks;
     1153                if (!ioctl(RTFileToNative(hRawFile), BLKGETSIZE, &cBlocks))
     1154                    cbSize = (uint64_t)cBlocks << 9;
     1155                else
     1156                {
     1157                    vrc = RTErrConvertFromErrno(errno);
     1158                    RTMsgError("Cannot get the size of the raw disk '%s': %Rrc", rawdisk.c_str(), vrc);
     1159                    goto out;
     1160                }
     1161            }
     1162        }
     1163        else if (S_ISREG(DevStat.st_mode))
     1164        {
     1165            vrc = RTFileGetSize(hRawFile, &cbSize);
     1166            if (RT_FAILURE(vrc))
     1167            {
     1168                RTMsgError("Failed to get size of file '%s': %Rrc", rawdisk.c_str(), vrc);
     1169                goto out;
     1170            }
     1171            else if (fRelative)
     1172            {
     1173                RTMsgError("The -relative parameter is invalid for raw images");
     1174                vrc = VERR_INVALID_PARAMETER;
     1175                goto out;
     1176            }
     1177        }
     1178        else
     1179        {
     1180            RTMsgError("File '%s' is no block device", rawdisk.c_str());
     1181            vrc = VERR_INVALID_PARAMETER;
     1182            goto out;
     1183        }
     1184    }
     1185    else
     1186    {
     1187        vrc = RTErrConvertFromErrno(errno);
     1188        RTMsgError("Failed to get file informtation for raw disk '%s': %Rrc",
     1189                   rawdisk.c_str(), vrc);
     1190    }
     1191#elif defined(RT_OS_DARWIN)
     1192    struct stat DevStat;
     1193    if (!fstat(RTFileToNative(hRawFile), &DevStat))
     1194    {
     1195        if (S_ISBLK(DevStat.st_mode))
     1196        {
     1197            uint64_t cBlocks;
     1198            uint32_t cbBlock;
     1199            if (!ioctl(RTFileToNative(hRawFile), DKIOCGETBLOCKCOUNT, &cBlocks))
     1200            {
     1201                if (!ioctl(RTFileToNative(hRawFile), DKIOCGETBLOCKSIZE, &cbBlock))
     1202                    cbSize = cBlocks * cbBlock;
     1203                else
     1204                {
     1205                    RTMsgError("Cannot get the block size for file '%s': %Rrc", rawdisk.c_str(), vrc);
     1206                    vrc = RTErrConvertFromErrno(errno);
     1207                    goto out;
     1208                }
     1209            }
     1210            else
     1211            {
     1212                vrc = RTErrConvertFromErrno(errno);
     1213                RTMsgError("Cannot get the block count for file '%s': %Rrc", rawdisk.c_str(), vrc);
     1214                goto out;
     1215            }
     1216        }
     1217        else if (S_ISREG(DevStat.st_mode))
     1218        {
     1219            fRelative = false; /* Must be false for raw image files. */
     1220            vrc = RTFileGetSize(hRawFile, &cbSize);
     1221            if (RT_FAILURE(vrc))
     1222            {
     1223                RTMsgError("Failed to get size of file '%s': %Rrc", rawdisk.c_str(), vrc);
     1224                goto out;
     1225            }
     1226        }
     1227        else
     1228        {
     1229            RTMsgError("File '%s' is neither block device nor regular file", rawdisk.c_str());
     1230            vrc = VERR_INVALID_PARAMETER;
     1231            goto out;
     1232        }
     1233    }
     1234    else
     1235    {
     1236        vrc = RTErrConvertFromErrno(errno);
     1237        RTMsgError("Failed to get file informtation for raw disk '%s': %Rrc",
     1238                   rawdisk.c_str(), vrc);
     1239    }
     1240#elif defined(RT_OS_SOLARIS)
     1241    struct stat DevStat;
     1242    if (!fstat(RTFileToNative(hRawFile), &DevStat))
     1243    {
     1244        if (S_ISBLK(DevStat.st_mode) || S_ISCHR(DevStat.st_mode))
     1245        {
     1246            struct dk_minfo mediainfo;
     1247            if (!ioctl(RTFileToNative(hRawFile), DKIOCGMEDIAINFO, &mediainfo))
     1248                cbSize = mediainfo.dki_capacity * mediainfo.dki_lbsize;
    11441249            else
    11451250            {
     
    11491254            }
    11501255        }
     1256        else if (S_ISREG(DevStat.st_mode))
     1257        {
     1258            vrc = RTFileGetSize(hRawFile, &cbSize);
     1259            if (RT_FAILURE(vrc))
     1260            {
     1261                RTMsgError("Failed to get size of file '%s': %Rrc", rawdisk.c_str(), vrc);
     1262                goto out;
     1263            }
     1264        }
     1265        else
     1266        {
     1267            RTMsgError("File '%s' is no block or char device", rawdisk.c_str());
     1268            vrc = VERR_INVALID_PARAMETER;
     1269            goto out;
     1270        }
    11511271    }
    11521272    else
    11531273    {
    1154         RTMsgError("File '%s' is no block device", rawdisk.c_str());
    1155         vrc = VERR_INVALID_PARAMETER;
    1156         goto out;
    1157     }
    1158 #elif defined(RT_OS_DARWIN)
    1159     struct stat DevStat;
    1160     if (!fstat(RTFileToNative(hRawFile), &DevStat) && S_ISBLK(DevStat.st_mode))
    1161     {
    1162         uint64_t cBlocks;
    1163         uint32_t cbBlock;
    1164         if (!ioctl(RTFileToNative(hRawFile), DKIOCGETBLOCKCOUNT, &cBlocks))
    1165         {
    1166             if (!ioctl(RTFileToNative(hRawFile), DKIOCGETBLOCKSIZE, &cbBlock))
    1167                 cbSize = cBlocks * cbBlock;
    1168             else
    1169             {
    1170                 RTMsgError("Cannot get the block size for file '%s': %Rrc", rawdisk.c_str(), vrc);
    1171                 vrc = RTErrConvertFromErrno(errno);
    1172                 goto out;
    1173             }
    1174         }
    1175         else
    1176         {
    1177             vrc = RTErrConvertFromErrno(errno);
    1178             RTMsgError("Cannot get the block count for file '%s': %Rrc", rawdisk.c_str(), vrc);
    1179             goto out;
    1180         }
    1181     }
    1182     else
    1183     {
    1184         RTMsgError("File '%s' is no block device", rawdisk.c_str());
    1185         vrc = VERR_INVALID_PARAMETER;
    1186         goto out;
    1187     }
    1188 #elif defined(RT_OS_SOLARIS)
    1189     struct stat DevStat;
    1190     if (!fstat(RTFileToNative(hRawFile), &DevStat) && (   S_ISBLK(DevStat.st_mode)
    1191                                       || S_ISCHR(DevStat.st_mode)))
    1192     {
    1193         struct dk_minfo mediainfo;
    1194         if (!ioctl(RTFileToNative(hRawFile), DKIOCGMEDIAINFO, &mediainfo))
    1195             cbSize = mediainfo.dki_capacity * mediainfo.dki_lbsize;
    1196         else
    1197         {
    1198             vrc = RTErrConvertFromErrno(errno);
    1199             RTMsgError("Cannot get the size of the raw disk '%s': %Rrc", rawdisk.c_str(), vrc);
    1200             goto out;
    1201         }
    1202     }
    1203     else
    1204     {
    1205         RTMsgError("File '%s' is no block or char device", rawdisk.c_str());
    1206         vrc = VERR_INVALID_PARAMETER;
    1207         goto out;
     1274        vrc = RTErrConvertFromErrno(errno);
     1275        RTMsgError("Failed to get file informtation for raw disk '%s': %Rrc",
     1276                   rawdisk.c_str(), vrc);
    12081277    }
    12091278#elif defined(RT_OS_FREEBSD)
    12101279    struct stat DevStat;
    1211     if (!fstat(RTFileToNative(hRawFile), &DevStat) && S_ISCHR(DevStat.st_mode))
    1212     {
    1213         off_t cbMedia = 0;
    1214         if (!ioctl(RTFileToNative(hRawFile), DIOCGMEDIASIZE, &cbMedia))
    1215         {
    1216             cbSize = cbMedia;
     1280    if (!fstat(RTFileToNative(hRawFile), &DevStat))
     1281    {
     1282        if (S_ISCHR(DevStat.st_mode))
     1283        {
     1284            off_t cbMedia = 0;
     1285            if (!ioctl(RTFileToNative(hRawFile), DIOCGMEDIASIZE, &cbMedia))
     1286                cbSize = cbMedia;
     1287            else
     1288            {
     1289                vrc = RTErrConvertFromErrno(errno);
     1290                RTMsgError("Cannot get the block count for file '%s': %Rrc", rawdisk.c_str(), vrc);
     1291                goto out;
     1292            }
     1293        }
     1294        else if (S_ISREG(DevStat.st_mode))
     1295        {
     1296            if (fRelative)
     1297            {
     1298                RTMsgError("The -relative parameter is invalid for raw images");
     1299                vrc = VERR_INVALID_PARAMETER;
     1300                goto out;
     1301            }
     1302            cbSize = DevStat.st_size;
    12171303        }
    12181304        else
    12191305        {
    1220             vrc = RTErrConvertFromErrno(errno);
    1221             RTMsgError("Cannot get the block count for file '%s': %Rrc", rawdisk.c_str(), vrc);
     1306            RTMsgError("File '%s' is neither character device nor regular file", rawdisk.c_str());
     1307            vrc = VERR_INVALID_PARAMETER;
    12221308            goto out;
    12231309        }
     
    12251311    else
    12261312    {
    1227         RTMsgError("File '%s' is no character device", rawdisk.c_str());
    1228         vrc = VERR_INVALID_PARAMETER;
    1229         goto out;
     1313        vrc = RTErrConvertFromErrno(errno);
     1314        RTMsgError("Failed to get file informtation for raw disk '%s': %Rrc",
     1315                   rawdisk.c_str(), vrc);
    12301316    }
    12311317#else /* all unrecognized OSes */
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