Changeset 2764


Ignore:
Timestamp:
Apr 20, 2011, 12:18:46 PM (8 years ago)
Author:
bruno
Message:

Function resize_drive_proportionately_to_suit_new_drives

  • Integrate some mr_asprintf
  • Use systematically long long data types in all computation to match the struct mountlist_reference size member and have correct precision
  • Apply JB general_at_itpsg.com patch to make the partition resizing work properly according to the drive size (keeping all calculation in KB and converting only for display)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mondo/src/mondorestore/mondo-prep.c

    r2480 r2764  
    24642464
    24652465    /** float ***********************************************************/
    2466     float factor;
    2467     float new_size;
    2468 //  float newcylinderno;
     2466    long long factor;
     2467    long long new_size;
    24692468
    24702469    /** long *************************************************************/
    2471     long newsizL = 0L;
    2472     long current_size_of_drive = 0L;
    2473     long original_size_of_drive = 0L;
    2474     long final_size = 0L;           /* all in Megabytes */
     2470    long long newsizL = 0LL;
     2471    long long totalsizL = 0LL;
     2472    long long current_size_of_drive = 0LL;  /* use KB interally for precision */
     2473    long long original_size_of_drive = 0LL; /* use KB interally for precision */
     2474    long long final_size = 0LL;             /* all in Megabytes */
    24752475    struct mountlist_reference *drivemntlist;
    24762476
     
    24792479    /** end **************************************************************/
    24802480
    2481     malloc_string(tmp);
    24822481    assert(mountlist != NULL);
    24832482    assert_string_is_neither_NULL_nor_zerolength(drive_name);
     
    24862485        if (strncmp(drive_name, RAID_DEVICE_STUB, strlen(RAID_DEVICE_STUB))
    24872486            == 0) {
    2488             paranoid_free(tmp);
    24892487            return;
    24902488        }
    24912489    }
    24922490
    2493     current_size_of_drive = get_phys_size_of_drive(drive_name);
    2494 
    2495     if (current_size_of_drive <= 0) {
     2491    current_size_of_drive = (long long) get_phys_size_of_drive(drive_name) * 1024LL;
     2492
     2493    if (current_size_of_drive <= 0LL) {
    24962494        log_it("Not resizing to match %s - can't find drive", drive_name);
    2497         paranoid_free(tmp);
    24982495        return;
    24992496    }
    2500     sprintf(tmp, "Expanding entries to suit drive %s (%ld MB)", drive_name,
    2501             current_size_of_drive);
     2497    mr_asprintf(&tmp, "Expanding entries to suit drive %s (%lld MB)", drive_name, current_size_of_drive / 1024);
    25022498    log_to_screen(tmp);
     2499    mr_free(tmp);
    25032500
    25042501    drivemntlist = malloc(sizeof(struct mountlist_reference));
     
    25122509
    25132510    for (partno = 0; partno < drivemntlist->entries; partno++) {
    2514         if (drivemntlist->el[partno]->size > 0) {
    2515             original_size_of_drive += (drivemntlist->el[partno]->size / 1024L);
     2511        if (drivemntlist->el[partno]->size > 0LL) {
     2512            /* Keep KB here */
     2513            original_size_of_drive += drivemntlist->el[partno]->size;
    25162514        }
    25172515    }
    25182516
    25192517    if (original_size_of_drive <= 0) {
    2520         sprintf(tmp, "Cannot resize %s's entries. Drive not found.",
    2521                 drive_name);
     2518        mr_asprintf(&tmp, "Cannot resize %s's entries. Drive not found.", drive_name);
    25222519        log_to_screen(tmp);
    2523         paranoid_free(tmp);
     2520        mr_free(tmp);
    25242521        return;
    25252522    }
    2526     factor =
    2527         (float) (current_size_of_drive) / (float) (original_size_of_drive);
    2528     sprintf(tmp, "Disk %s was %ld MB; is now %ld MB; factor = %f",
    2529             drive_name, original_size_of_drive, current_size_of_drive,
    2530             factor);
     2523    factor = (current_size_of_drive) / (original_size_of_drive);
     2524    mr_asprintf(&tmp, "Disk %s was %lld MB; is now %lld MB; Proportionally resizing partitions (factor ~= %lld)",
     2525            drive_name, original_size_of_drive/1024, current_size_of_drive/1024, factor);
    25312526    log_to_screen(tmp);
     2527    mr_free(tmp);
    25322528
    25332529    lastpart = drivemntlist->entries - 1;
     
    25352531        /* the 'atoi' thing is to make sure we don't try to resize _images_, whose formats will be numeric */
    25362532        if (!atoi(drivemntlist->el[partno]->format)) {
    2537             new_size = (float) (drivemntlist->el[partno]->size) * factor;
     2533            new_size = (drivemntlist->el[partno]->size) * factor;
    25382534        } else {
    25392535            new_size = drivemntlist->el[partno]->size;
     
    25442540                    drivemntlist->el[partno]->device,
    25452541                    drivemntlist->el[partno]->mountpoint);
    2546             newsizL = (long) new_size;  // It looks wrong but it's not
    2547         } else {
    2548             newsizL = (long) new_size;
    2549         }
     2542        }
     2543        newsizL = new_size;
    25502544
    25512545        /* Do not apply the factor if partition was of negative size */
    2552         if (newsizL < 0) {
     2546        if (newsizL < 0LL) {
    25532547            newsizL = drivemntlist->el[partno]->size;
    25542548        }
    2555 
    2556         sprintf(tmp, "Changing %s from %lld KB to %ld KB",
    2557                 drivemntlist->el[partno]->device,
    2558                 drivemntlist->el[partno]->size, newsizL);
     2549        totalsizL += newsizL;
     2550
     2551        mr_asprintf(&tmp, "Changing %s from %lld KB to %lld KB", drivemntlist->el[partno]->device, drivemntlist->el[partno]->size, newsizL);
    25592552        log_to_screen(tmp);
     2553        mr_free(tmp);
    25602554        drivemntlist->el[partno]->size = newsizL;
    25612555    }
    2562     final_size = get_phys_size_of_drive(drive_name);
    2563     sprintf(tmp, "final_size = %ld MB", final_size);
     2556    // Ensures over-allocation alert and prompt for interactive mode does not happen
     2557    if (totalsizL > current_size_of_drive) {
     2558        mr_asprintf(&tmp, "Last partition size calculated would be over-allocated, reducing %s from %lld KB to %lld KB.", drivemntlist->el[lastpart]->device, drivemntlist->el[lastpart]->size, drivemntlist->el[lastpart]->size - (totalsizL - current_size_of_drive));
     2559        drivemntlist->el[drivemntlist->entries-1]->size -= (totalsizL - current_size_of_drive);
     2560    } else if (totalsizL < current_size_of_drive) {
     2561        mr_asprintf(&tmp, "Last partition size calculated would be under-allocated, increasing %s from %lld KB to %lld KB.",drivemntlist->el[lastpart]->device, drivemntlist->el[lastpart]->size, drivemntlist->el[lastpart]->size + (current_size_of_drive - totalsizL));
     2562        drivemntlist->el[drivemntlist->entries-1]->size += (current_size_of_drive - totalsizL);
     2563    }
    25642564    log_to_screen(tmp);
    2565     paranoid_free(tmp);
     2565    mr_free(tmp);
     2566    mr_asprintf(&tmp, "final_size = %lld MB", current_size_of_drive / 1024);
     2567    log_to_screen(tmp);
     2568    mr_free(tmp);
    25662569}
    25672570
Note: See TracChangeset for help on using the changeset viewer.