Changeset 3286 for branches/3.2


Ignore:
Timestamp:
May 3, 2014, 10:13:12 AM (5 years ago)
Author:
bruno
Message:
  • Improve mondo-prep using mr_asprintf more (was behind other C modules). To be contiued
Location:
branches/3.2
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/3.2/mindi/rootfs/etc/init.d/rcS

    r3285 r3286  
    512512        for d in $MINDI_EXCLUDE_DEVS ; do
    513513            echo "LVM exclusion == $d"
     514            # LVM: $4 to be checked if changed
    514515            EXCLUDE_VGS=`grep " $d" /tmp/i-want-my-lvm | grep vgcreate | awk '{print $4}'`
    515516            vg=`echo $EXCLUDE_VGS | sed "s/ /|/g"`
     
    526527                echo "vgcfgrestore $v" >> /tmp/restorevgs
    527528                # Remove LVs from mountlist
     529                # LVM: sed to be checked if changed
    528530                EXCLUDE_LVS=`grep " $v" /tmp/i-want-my-lvm | grep lvcreate | sed "s/^.*-n \([^ ][^ ]*\) .*$/$1/"`
    529531                for l in $EXCLUDE_LVS; do
     
    537539        done
    538540
     541        # LVM: remove #
    539542        grep -E "^#.*vgchange" /tmp/i-want-my-lvm | sed "s/^#[ ]*//" > /tmp/start-lvm
    540543        chmod +x /tmp/start-lvm
  • branches/3.2/mondo/src/mondorestore/mondo-prep.c

    r3263 r3286  
    5151
    5252
    53 void wipe_MBRs_and_reboot_if_necessary(struct mountlist_itself *mountlist)
    54 {
    55     char *command;
    56     char *tmp;
    57     int lino;
    58     int i;
    59     FILE *fout;
    60     char *buf;
    61     const int blocksize = 512;
    62     struct list_of_disks *drivelist = NULL;
     53void wipe_MBRs_and_reboot_if_necessary(struct mountlist_itself *mountlist) {
     54
     55char *command = NULL;
     56int lino;
     57int i;
     58FILE *fout;
     59char *buf;
     60const int blocksize = 512;
     61struct list_of_disks *drivelist = NULL;
     62
    6363// If LVMs are present and a zero-and-reboot wasn't recently undertaken
    6464// then zero & insist on reboot.
    65     malloc_string(command);
    66     malloc_string(tmp);
    67     buf = malloc(blocksize);
    68     if (does_file_exist("/tmp/i-want-my-lvm"))  // FIXME - cheating :)
    69     {
    70         drivelist = malloc(sizeof(struct list_of_disks));
    71         make_list_of_drives_in_mountlist(mountlist, drivelist);
     65buf = malloc(blocksize);
     66if (does_file_exist("/tmp/i-want-my-lvm")) {    // FIXME - cheating :)
     67    drivelist = malloc(sizeof(struct list_of_disks));
     68    make_list_of_drives_in_mountlist(mountlist, drivelist);
     69    for (lino = 0; lino < drivelist->entries; lino++) {
     70        mr_asprintf(command, "dd if=%s bs=512 count=1 2> /dev/null | grep \"%s\"", drivelist->el[lino].device, MONDO_WAS_HERE);
     71        if (!run_program_and_log_output(command, 1)) {
     72            log_msg(1, "Found MONDO_WAS_HERE marker on drive#%d (%s)", lino, drivelist->el[lino].device);
     73            break;
     74        }
     75        mr_free(command);
     76    }
     77
     78    if (lino == drivelist->entries) {
     79    // zero & reboot
     80        log_to_screen("I am sorry for the inconvenience but I must ask you to reboot.");
     81        log_to_screen("I need to reset the Master Boot Record; in order to be");
     82        log_to_screen("sure the kernel notices, I must reboot after doing it.");
     83        log_to_screen("Please hit 'Enter' to reboot.");
    7284        for (lino = 0; lino < drivelist->entries; lino++) {
    73             sprintf(command,
    74                     "dd if=%s bs=512 count=1 2> /dev/null | grep \"%s\"",
    75                     drivelist->el[lino].device, MONDO_WAS_HERE);
    76             if (!run_program_and_log_output(command, 1)) {
    77                 log_msg(1, "Found MONDO_WAS_HERE marker on drive#%d (%s)",
    78                         lino, drivelist->el[lino].device);
    79                 break;
    80             }
    81         }
    82 
    83         if (lino == drivelist->entries) {
    84 // zero & reboot
    85             log_to_screen
    86                 ("I am sorry for the inconvenience but I must ask you to reboot.");
    87             log_to_screen
    88                 ("I need to reset the Master Boot Record; in order to be");
    89             log_to_screen
    90                 ("sure the kernel notices, I must reboot after doing it.");
    91             log_to_screen("Please hit 'Enter' to reboot.");
    92             for (lino = 0; lino < drivelist->entries; lino++) {
    93                 for (i = 0; i < blocksize; i++) {
    94                     buf[i] = 0;
     85            for (i = 0; i < blocksize; i++) {
     86                buf[i] = 0;
     87            }
     88            sprintf(buf, "%s\n", MONDO_WAS_HERE);
     89            fout = fopen(drivelist->el[lino].device, "w+");
     90            if (!fout) {
     91                log_msg(1, "Unable to open+wipe %s", drivelist->el[lino].device);
     92            } else {
     93                if (1 != fwrite(buf, blocksize, 1, fout)) {
     94                    log_msg(1, "Failed to wipe %s", drivelist->el[lino].device);
     95                } else {
     96                    log_msg(1, "Successfully wiped %s", drivelist->el[lino].device);
    9597                }
    96                 sprintf(buf, "%s\n", MONDO_WAS_HERE);
    97                 fout = fopen(drivelist->el[lino].device, "w+");
    98                 if (!fout) {
    99                     log_msg(1, "Unable to open+wipe %s",
    100                             drivelist->el[lino].device);
    101                 } else {
    102                     if (1 != fwrite(buf, blocksize, 1, fout)) {
    103                         log_msg(1, "Failed to wipe %s",
    104                                 drivelist->el[lino].device);
    105                     } else {
    106                         log_msg(1, "Successfully wiped %s",
    107                                 drivelist->el[lino].device);
    108                     }
    109                     fclose(fout);
    110                 }
    111             }
    112             paranoid_system("sync");
    113             paranoid_system("sync");
    114             paranoid_system("sync");
    115             popup_and_OK
    116                 ("I must now reboot. Please leave the boot media in the drive and repeat your actions - e.g. type 'nuke' - and it should work fine.");
    117             paranoid_system("reboot");
    118         }
    119     }
     98                fclose(fout);
     99            }
     100        }
     101        paranoid_system("sync");
     102        paranoid_system("sync");
     103        paranoid_system("sync");
     104        popup_and_OK("I must now reboot. Please leave the boot media in the drive and repeat your actions - e.g. type 'nuke' - and it should work fine.");
     105        paranoid_system("reboot");
     106    }
     107}
    120108// Still here? Cool!
    121     paranoid_free(command);
    122     paranoid_free(tmp);
    123     log_msg(1, "Cool. I didn't have to wipe anything.");
    124 }
    125 
    126 
    127 
    128 
    129 
    130 
    131 int fput_string_one_char_at_a_time(FILE * fout, char *str)
    132 {
    133     int i, j;
    134     FILE *fq;
    135 
    136     if (ferror(fout)) {
    137         return (-1);
    138     }
    139     log_msg(5, "Writing string '%s', one char at a time", str);
    140     j = strlen(str);
    141     for (i = 0; i < j; i++) {
    142         log_msg(6, "Writing %d ('%c')", str[i], str[i]);
    143         if ((fq = fopen(FDISK_LOG, "a+"))) {
    144             fputc(str[i], fq);
    145             fclose(fq);
    146         }
    147         fputc(str[i], fout);
    148         fflush(fout);
    149         usleep(1000L * 100L);
    150         if (str[i] < 32) {
    151             usleep(1000L * 10L);
    152         }
    153     }
    154     log_msg(5, "Returning");
    155 
    156     return (i);
    157 }
    158 
    159 
    160 
    161 
    162 
    163 
    164 
    165 
     109log_msg(1, "Cool. I didn't have to wipe anything.");
     110}
     111
     112
     113int fput_string_one_char_at_a_time(FILE * fout, char *str) {
     114int i, j;
     115FILE *fq;
     116
     117if (ferror(fout)) {
     118    return (-1);
     119}
     120log_msg(5, "Writing string '%s', one char at a time", str);
     121j = strlen(str);
     122for (i = 0; i < j; i++) {
     123    log_msg(6, "Writing %d ('%c')", str[i], str[i]);
     124    if ((fq = fopen(FDISK_LOG, "a+"))) {
     125        fputc(str[i], fq);
     126        fclose(fq);
     127    }
     128    fputc(str[i], fout);
     129    fflush(fout);
     130    usleep(1000L * 100L);
     131    if (str[i] < 32) {
     132        usleep(1000L * 10L);
     133    }
     134}
     135log_msg(5, "Returning");
     136
     137return (i);
     138}
    166139
    167140
     
    176149 */
    177150
    178 
    179 int do_my_funky_lvm_stuff(bool just_erase_existing_volumes,
    180                             bool vacuum_pack)
    181 {
    182     /** buffers **********************************************/
    183     char *tmp;
    184     char *tmp1 = NULL;
    185     char *incoming;
    186     char *command;
    187     char *lvscan_sz;
    188     char *lvremove_sz;
    189     char *pvscan_sz;
    190     char *vgscan_sz;
    191     char *vgcreate_sz;
    192     char *vgchange_sz;
    193     char *vgremove_sz;
    194 //  char *do_this_last;
    195 
    196     /** char **************************************************/
    197     char *p;
    198     char *q;
    199     char *r;
    200 
    201     /** int ***************************************************/
    202     int retval = 0;
    203     int res = 0;
    204     int i;
    205     int lvmversion = 1;
    206     long extents;
    207     fpos_t orig_pos;
    208 
    209     /** pointers **********************************************/
    210     FILE *fin;
    211 
    212     /** end *****************************************************/
     151int do_my_funky_lvm_stuff(bool just_erase_existing_volumes, bool vacuum_pack) {
     152/** buffers **********************************************/
     153char *tmp;
     154char *tmp1 = NULL;
     155char *incoming;
     156char *command;
     157char *cmd = NULL;
     158char *lvscan_sz = NULL;
     159char *lvremove_sz = NULL;
     160char *pvscan_sz = NULL;
     161char *vgscan_sz = NULL;
     162char *vgchange_sz = NULL;
     163char *vgremove_sz = NULL;
     164
     165/** char **************************************************/
     166char *p;
     167char *q;
     168char *r;
     169
     170/** int ***************************************************/
     171int retval = 0;
     172int res = 0;
     173int i;
     174int lvmversion = 1;
     175long extents;
     176fpos_t orig_pos;
     177
     178/** pointers **********************************************/
     179FILE *fin;
     180
     181/** end *****************************************************/
    213182
    214183#ifdef __FreeBSD__
     
    216185#endif
    217186
    218     if (strstr(call_program_and_get_last_line_of_output("cat /proc/cmdline"), "nolvm")) {
    219         return(0);
    220     }
    221     if (!(fin = fopen("/tmp/i-want-my-lvm", "r"))) {
    222         log_OS_error("/tmp/i-want-my-lvm");
    223         return (1);
    224     }
     187if (strstr(call_program_and_get_last_line_of_output("cat /proc/cmdline"), "nolvm")) {
     188    return(0);
     189}
     190if (!(fin = fopen("/tmp/i-want-my-lvm", "r"))) {
     191    log_OS_error("/tmp/i-want-my-lvm");
     192    return (1);
     193}
     194
     195log_it("STARTING");
     196log_msg(1, "OK, opened i-want-my-lvm. Shutting down LVM volumes...");
     197if (find_home_of_exe("lvm")) {  // found it :) cool
     198    mr_asprintf(lvscan_sz, "lvm lvscan");
     199    mr_asprintf(lvremove_sz, "lvm lvremove");
     200    mr_asprintf(vgscan_sz, "lvm vgscan");
     201    mr_asprintf(pvscan_sz, "lvm pvscan");
     202    mr_asprintf(vgchange_sz, "lvm vgchange");
     203    mr_asprintf(vgremove_sz, "lvm vgremove -f");
     204} else {
     205    mr_asprintf(lvscan_sz, "lvscan");
     206    mr_asprintf(lvremove_sz, "lvremove");
     207    mr_asprintf(vgscan_sz, "vgscan");
     208    mr_asprintf(pvscan_sz, "pvscan");
     209    mr_asprintf(vgchange_sz, "vgchange");
     210    mr_asprintf(vgremove_sz, "vgremove");
     211}
     212mr_asprintf(cmd, "for i in `%s | cut -d\"'\" -f2 | sort -r` ; do echo \"Shutting down lv $i\" >> %s ; %s -f $i; done", lvscan_sz, MONDO_LOGFILE, lvremove_sz);
     213mr_free(lvscan_sz);
     214mr_free(lvremove_sz);
     215
     216run_program_and_log_output(cmd, 5);
     217mr_free(cmd);
     218
     219sleep(1);
     220mr_asprintf(cmd, "for i in `%s | grep -i lvm | cut -d'\"' -f2` ; do %s -a n $i ; %s $i; echo \"Shutting down vg $i\" >> %s ; done", vgscan_sz, vgchange_sz, vgremove_sz, MONDO_LOGFILE);
     221mr_free(vgchange_sz);
     222mr_free(vgremove_sz);
     223
     224run_program_and_log_output(cmd, 5);
     225mr_free(cmd);
     226
     227if (just_erase_existing_volumes) {
     228    paranoid_fclose(fin);
     229    log_msg(1, "Closed i-want-my-lvm. Finished erasing LVMs.");
     230    retval = 0;
     231} else {
    225232
    226233    malloc_string(tmp);
    227234    malloc_string(incoming);
    228     malloc_string(lvscan_sz);
    229     malloc_string(lvremove_sz);
    230     malloc_string(vgscan_sz);
    231     malloc_string(pvscan_sz);
    232     malloc_string(vgcreate_sz);
    233     malloc_string(vgchange_sz);
    234     malloc_string(vgremove_sz);
    235 //  malloc_string(do_this_last); // postpone lvcreate call if necessary
    236235    command = malloc(512);
    237 
    238 //  do_this_last[0] = '\0';
    239     log_it("STARTING");
    240     log_msg(1, "OK, opened i-want-my-lvm. Shutting down LVM volumes...");
    241     if (find_home_of_exe("lvm"))    // found it :) cool
    242     {
    243         strcpy(lvscan_sz, "lvm lvscan");
    244         strcpy(lvremove_sz, "lvm lvremove");
    245         strcpy(vgscan_sz, "lvm vgscan");
    246         strcpy(pvscan_sz, "lvm pvscan");
    247         strcpy(vgcreate_sz, "lvm vgcreate");
    248         strcpy(vgchange_sz, "lvm vgchange");
    249         strcpy(vgremove_sz, "lvm vgremove -f");
    250     } else {
    251         strcpy(lvscan_sz, "lvscan");
    252         strcpy(lvremove_sz, "lvremove");
    253         strcpy(vgscan_sz, "vgscan");
    254         strcpy(pvscan_sz, "pvscan");
    255         strcpy(vgcreate_sz, "vgcreate");
    256         strcpy(vgchange_sz, "vgchange");
    257         strcpy(vgremove_sz, "vgremove");
    258     }
    259     sprintf(command,
    260             "for i in `%s | cut -d\"'\" -f2 | sort -r` ; do echo \"Shutting down lv $i\" >> %s ; %s -f $i; done", lvscan_sz, MONDO_LOGFILE, lvremove_sz);
    261     run_program_and_log_output(command, 5);
    262     sleep(1);
    263     sprintf(command,
    264             "for i in `%s | grep -i lvm | cut -d'\"' -f2` ; do %s -a n $i ; %s $i; echo \"Shutting down vg $i\" >> %s ; done", vgscan_sz, vgchange_sz, vgremove_sz, MONDO_LOGFILE);
    265     run_program_and_log_output(command, 5);
    266     if (just_erase_existing_volumes) {
    267         paranoid_fclose(fin);
    268         log_msg(1, "Closed i-want-my-lvm. Finished erasing LVMs.");
    269         retval = 0;
    270         goto end_of_i_want_my_lvm;
    271     }
    272236
    273237    log_msg(1, "OK, rewound i-want-my-lvm. Doing funky stuff...");
     
    285249        }
    286250        if ((p = strstr(incoming, "vgcreate"))) {
    287 // include next line(s) if they end in /dev (cos we've got a broken i-want-my-lvm)
     251        // include next line(s) if they end in /dev (cos we've got a broken i-want-my-lvm)
    288252            for (q = fgets(tmp, MAX_STR_LEN - 1, fin); !feof(fin) && (q != NULL); q = fgets(tmp, MAX_STR_LEN - 1, fin)) {
    289253                if (tmp[0] == '#') {
     
    304268            *q = '\0';
    305269            log_msg(1, "Deleting old entries at /dev/%s", tmp);
    306 //             sprintf(command, "%s -f %s", vgremove_sz, tmp);
    307 //             run_program_and_log_output(command, 1);
    308             sprintf(command, "rm -Rf /dev/%s", tmp);
    309             run_program_and_log_output(command, 1);
     270            mr_asprintf(cmd, "rm -Rf /dev/%s", tmp);
     271            run_program_and_log_output(cmd, 1);
     272            mr_free(cmd);
     273
    310274            run_program_and_log_output(vgscan_sz, 1);
    311275            run_program_and_log_output(pvscan_sz, 1);
    312             log_msg(3,
    313                     "After working around potentially broken i-want-my-lvm, incoming[] is now '%s'",
    314                     incoming);
     276            log_msg(3, "After working around potentially broken i-want-my-lvm, incoming[] is now '%s'", incoming);
    315277        }
    316278        for (p = incoming + 1; *p == ' '; p++);
     
    397359        sleep(1);
    398360    }
     361
    399362    paranoid_fclose(fin);
    400     log_msg(1, "Closed i-want-my-lvm. Finished doing funky stuff.");
    401     end_of_i_want_my_lvm:
    402363    paranoid_free(tmp);
    403364    paranoid_free(incoming);
    404365    paranoid_free(command);
    405     paranoid_free(lvscan_sz);
    406     paranoid_free(lvremove_sz);
    407     paranoid_free(vgscan_sz);
    408     paranoid_free(pvscan_sz);
    409     paranoid_free(vgcreate_sz);
    410     paranoid_free(vgchange_sz);
    411     paranoid_free(vgremove_sz);
    412 //  paranoid_free(do_this_last);
    413     paranoid_system("sync");
    414     paranoid_system("sync");
    415     paranoid_system("sync");
    416     sleep(1);
    417     log_it("ENDING");
    418     if (retval > 2) {
    419         log_msg(1, "%d errors. I'm reporting this.", retval);
    420         return (retval);
    421     } else {
    422         log_msg(1, "Not many errors. Returning 0.");
    423         return (0);
    424     }
     366    log_msg(1, "Closed i-want-my-lvm. Finished doing funky stuff.");
     367}
     368mr_free(pvscan_sz);
     369mr_free(vgscan_sz);
     370
     371paranoid_system("sync");
     372paranoid_system("sync");
     373paranoid_system("sync");
     374sleep(1);
     375log_it("ENDING");
     376if (retval > 2) {
     377    log_msg(1, "%d errors. I'm reporting this.", retval);
     378    return (retval);
     379} else {
     380    log_msg(1, "Not many errors. Returning 0.");
     381    return (0);
     382}
    425383}
    426384
     
    435393 * @return 0 for success, nonzero for failure.
    436394 */
    437 int extrapolate_mountlist_to_include_raid_partitions(struct mountlist_itself
    438                                                      *new_mountlist, struct mountlist_itself
    439                                                      *old_mountlist)
    440 {
    441     /** pointers *********************************************************/
    442     FILE *fin;
    443 
    444     /** int **************************************************************/
    445     int lino;
    446     int j;
    447 
    448     /** buffers **********************************************************/
    449     char *incoming;
    450     char *tmp;
    451 
    452     /** pointers *********************************************************/
    453     char *p;
    454     char *q;
    455 
    456     /** init *************************************************************/
    457     new_mountlist->entries = 0;
    458 
    459     /** end **************************************************************/
    460 
    461     malloc_string(incoming);
    462     malloc_string(tmp);
    463     assert(new_mountlist != NULL);
    464     assert(old_mountlist != NULL);
     395int extrapolate_mountlist_to_include_raid_partitions(struct mountlist_itself *new_mountlist, struct mountlist_itself *old_mountlist) {
     396
     397/** pointers *********************************************************/
     398FILE *fin;
     399
     400/** int **************************************************************/
     401int lino;
     402int j;
     403
     404/** buffers **********************************************************/
     405char *incoming;
     406
     407/** pointers *********************************************************/
     408char *p;
     409char *q;
     410
     411/** init *************************************************************/
     412new_mountlist->entries = 0;
     413
     414/** end **************************************************************/
     415
     416malloc_string(incoming);
     417assert(new_mountlist != NULL);
     418assert(old_mountlist != NULL);
    465419
    466420#ifdef __FreeBSD__
    467     log_to_screen
    468         ("I don't know how to extrapolate the mountlist on FreeBSD. Sorry.");
     421    log_to_screen("I don't know how to extrapolate the mountlist on FreeBSD. Sorry.");
    469422    return (1);
    470423#endif
    471424
    472     for (lino = 0; lino < old_mountlist->entries; lino++) {
    473         if (strstr(old_mountlist->el[lino].device, RAID_DEVICE_STUB))   // raid
    474         {
    475             if (!does_file_exist("/etc/raidtab")) {
    476                 log_to_screen
    477                     ("Cannot find /etc/raidtab - cannot extrapolate the fdisk entries");
    478                 finish(1);
    479             }
    480             if (!(fin = fopen("/etc/raidtab", "r"))) {
    481                 log_OS_error("Cannot open /etc/raidtab");
    482                 finish(1);
    483             }
     425for (lino = 0; lino < old_mountlist->entries; lino++) {
     426    if (strstr(old_mountlist->el[lino].device, RAID_DEVICE_STUB))   // raid
     427    {
     428        if (!does_file_exist("/etc/raidtab")) {
     429            log_to_screen
     430                ("Cannot find /etc/raidtab - cannot extrapolate the fdisk entries");
     431            finish(1);
     432        }
     433        if (!(fin = fopen("/etc/raidtab", "r"))) {
     434            log_OS_error("Cannot open /etc/raidtab");
     435            finish(1);
     436        }
     437        for (q = fgets(incoming, MAX_STR_LEN - 1, fin); !feof(fin) && (q != NULL)
     438             && !strstr(incoming, old_mountlist->el[lino].device);
     439             q = fgets(incoming, MAX_STR_LEN - 1, fin));
     440        if (!feof(fin)) {
     441            log_it(tmp, "Investigating %s", old_mountlist->el[lino].device);
    484442            for (q = fgets(incoming, MAX_STR_LEN - 1, fin); !feof(fin) && (q != NULL)
    485                  && !strstr(incoming, old_mountlist->el[lino].device);
    486                  q = fgets(incoming, MAX_STR_LEN - 1, fin));
    487             if (!feof(fin)) {
    488                 sprintf(tmp, "Investigating %s",
    489                         old_mountlist->el[lino].device);
    490                 log_it(tmp);
    491                 for (q = fgets(incoming, MAX_STR_LEN - 1, fin); !feof(fin) && (q != NULL)
    492                      && !strstr(incoming, "raiddev");
    493                      q = fgets(incoming, MAX_STR_LEN - 1, fin)) {
    494                     if (strstr(incoming, OSSWAP("device", "drive"))
    495                         && !strchr(incoming, '#')) {
    496                         for (p = incoming + strlen(incoming);
    497                              *(p - 1) <= 32; p--);
    498                         *p = '\0';
    499                         for (p--; p > incoming && *(p - 1) > 32; p--);
    500                         sprintf(tmp, "Extrapolating %s", p);
    501                         log_it(tmp);
    502                         for (j = 0;
    503                              j < new_mountlist->entries
    504                              && strcmp(new_mountlist->el[j].device, p);
    505                              j++);
    506                         if (j >= new_mountlist->entries) {
    507                             strcpy(new_mountlist->
    508                                      el[new_mountlist->entries].device, p);
    509                             strcpy(new_mountlist->
    510                                      el[new_mountlist->entries].mountpoint,
    511                                      "raid");
    512                             strcpy(new_mountlist->
    513                                      el[new_mountlist->entries].format,
    514                                      "raid");
    515                             new_mountlist->el[new_mountlist->entries].
    516                                 size = old_mountlist->el[lino].size;
    517                             new_mountlist->entries++;
    518                         } else {
    519                             sprintf(tmp,
    520                                     "Not adding %s to mountlist: it's already there",
    521                                     p);
    522                             log_it(tmp);
    523                         }
     443                 && !strstr(incoming, "raiddev");
     444                 q = fgets(incoming, MAX_STR_LEN - 1, fin)) {
     445                if (strstr(incoming, OSSWAP("device", "drive"))
     446                    && !strchr(incoming, '#')) {
     447                    for (p = incoming + strlen(incoming);
     448                         *(p - 1) <= 32; p--);
     449                    *p = '\0';
     450                    for (p--; p > incoming && *(p - 1) > 32; p--);
     451                    log_it(tmp, "Extrapolating %s", p);
     452                    for (j = 0;
     453                         j < new_mountlist->entries
     454                         && strcmp(new_mountlist->el[j].device, p);
     455                         j++);
     456                    if (j >= new_mountlist->entries) {
     457                        strcpy(new_mountlist-> el[new_mountlist->entries].device, p);
     458                        strcpy(new_mountlist-> el[new_mountlist->entries].mountpoint, "raid");
     459                        strcpy(new_mountlist-> el[new_mountlist->entries].format, "raid");
     460                        new_mountlist->el[new_mountlist->entries]. size = old_mountlist->el[lino].size;
     461                        new_mountlist->entries++;
     462                    } else {
     463                        log_it(tmp, "Not adding %s to mountlist: it's already there", p);
    524464                    }
    525465                }
    526466            }
    527             paranoid_fclose(fin);
    528         } else {
    529             strcpy(new_mountlist->el[new_mountlist->entries].device,
    530                      old_mountlist->el[lino].device);
    531             strcpy(new_mountlist->el[new_mountlist->entries].mountpoint,
    532                      old_mountlist->el[lino].mountpoint);
    533             strcpy(new_mountlist->el[new_mountlist->entries].format,
    534                      old_mountlist->el[lino].format);
    535             new_mountlist->el[new_mountlist->entries].size =
    536                 old_mountlist->el[lino].size;
    537             new_mountlist->entries++;
    538         }
    539     }
    540     paranoid_free(incoming);
    541     paranoid_free(tmp);
    542 
    543     return (0);
     467        }
     468        paranoid_fclose(fin);
     469    } else {
     470        strcpy(new_mountlist->el[new_mountlist->entries].device, old_mountlist->el[lino].device);
     471        strcpy(new_mountlist->el[new_mountlist->entries].mountpoint, old_mountlist->el[lino].mountpoint);
     472        strcpy(new_mountlist->el[new_mountlist->entries].format, old_mountlist->el[lino].format);
     473        new_mountlist->el[new_mountlist->entries].size = old_mountlist->el[lino].size;
     474        new_mountlist->entries++;
     475    }
     476}
     477paranoid_free(incoming);
     478
     479return (0);
    544480}
    545481
     
    554490 */
    555491int create_raid_device_via_mdadm(struct raidlist_itself *raidlist, char *device, bool test) {
    556     /** int **************************************************************/
    557     int i   = 0;
    558     int j   = 0;
    559     int v   = 0;
    560     int res = 0;
    561    
    562     /** buffers ***********************************************************/
    563     char *devices = NULL;
    564     char *level   = NULL;
    565     char *program = NULL;
    566 
    567     // leave straight away if raidlist is initial or has no entries
    568     if (!raidlist || raidlist->entries == 0) {
    569       log_msg(1, "No RAID arrays found.");
    570       return 1;
    571     } else {
    572       log_msg(1, "%d RAID arrays found.", raidlist->entries);
    573     }
    574     // find raidlist entry for requested device
    575     for (i = 0; i < raidlist->entries; i++) {
    576       if (!strcmp(raidlist->el[i].raid_device, device)) break;
    577     }
    578     // check whether RAID device was found in raidlist
    579     if (i == raidlist->entries) {
    580       log_msg(1, "RAID device %s not found in list.", device);
    581       return 1;
    582     } else {
    583       log_msg(1, "RAID device %s found in list (%d).", device, i);
    584     }
    585 
    586     // create device list from normal disks followed by spare ones
    587     if (raidlist->el[i].data_disks.el[0].device != NULL) {
    588         mr_asprintf(devices, "%s", raidlist->el[i].data_disks.el[0].device);
    589         log_msg(4, "Adding device %s to list", raidlist->el[i].data_disks.el[0].device);
    590     } else {
    591         log_msg(1, "Strange, there are entries but no device");
    592     }
    593     for (j = 1; j < raidlist->el[i].data_disks.entries; j++) {
    594       mr_strcat(devices, " %s", raidlist->el[i].data_disks.el[j].device);
    595       log_msg(4, "Adding device %s to list", raidlist->el[i].data_disks.el[j].device);
    596     }
    597     for (j = 0; j < raidlist->el[i].spare_disks.entries; j++) {
    598       mr_strcat(devices, " %s", raidlist->el[i].spare_disks.el[j].device);
    599       log_msg(4, "Adding spare device %s to list", raidlist->el[i].spare_disks.el[j].device);
    600     }
    601     log_msg(4, "RAID devices: %s", devices);
    602     // translate RAID level
    603     if (raidlist->el[i].raid_level == -2) {
    604       mr_asprintf(level, "multipath");
    605     } else if (raidlist->el[i].raid_level == -1) {
    606       mr_asprintf(level, "linear");
    607     } else {
    608       mr_asprintf(level, "raid%d", raidlist->el[i].raid_level);
    609     }
    610     // create RAID device:
    611     // - RAID device, number of devices and devices mandatory
    612     // - parity algorithm, chunk size and spare devices optional
    613     // - faulty devices ignored
    614     // - persistent superblock always used as this is recommended
    615 
    616     mr_asprintf(program, "mdadm --create --force --run --auto=yes %s --level=%s --raid-devices=%d", raidlist->el[i].raid_device, level, raidlist->el[i].data_disks.entries);
    617     mr_free(level);
    618     log_msg(4, "cmd built: %s", program);
    619     // Restoring the UUID and Version stored at backup time of present
    620     for (v = 0; v < raidlist->el[i].additional_vars.entries ; v++ ) {
    621         log_msg(4,"Working on additional param #%d (Label: %s)",v,raidlist->el[i].additional_vars.el[v].label);
    622         if ((raidlist->el[i].additional_vars.el[v].label != NULL) && (strcmp(raidlist->el[i].additional_vars.el[v].label,"UUID") == 0)) {
    623             // We have a UUID to handle
    624             if (raidlist->el[i].additional_vars.el[v].value != NULL) {
    625                 // force its restoration in order to avoid modifying all conf files using it
    626                 log_it("Managing previous UUID %s", raidlist->el[i].additional_vars.el[v].value);
    627                 mr_strcat(program, " --uuid %s",raidlist->el[i].additional_vars.el[v].value);
    628                 continue;
    629             } else {
    630                 log_msg(1,"Unable to manage previous NULL UUID");
    631             }
    632         }
    633         if ((raidlist->el[i].additional_vars.el[v].label != NULL) && (strcmp(raidlist->el[i].additional_vars.el[v].label,"Version") == 0)) {
    634             // We have a Version to handle
    635             if (raidlist->el[i].additional_vars.el[v].value != NULL) {
    636                 // force its restoration in order to support all complex boot loader + md format cases
    637                 // Also see bug #473
    638                 log_it("Managing previous Version %s", raidlist->el[i].additional_vars.el[v].value);
    639                 mr_strcat(program, " -e %s",raidlist->el[i].additional_vars.el[v].value);
    640                 continue;
    641             } else {
    642                 log_msg(1,"Unable to manage previous NULL Version");
    643             }
    644         }
    645     }
    646         log_msg(4, "cmd built: %s", program);
    647     if (raidlist->el[i].parity != -1) {
    648       switch(raidlist->el[i].parity) {
    649       case 0:
    650         mr_strcat(program, " --parity=%s", "la");
    651         break;
    652       case 1:
    653         mr_strcat(program, " --parity=%s", "ra");
    654         break;
    655       case 2:
    656         mr_strcat(program, " --parity=%s", "ls");
    657         break;
    658       case 3:
    659         mr_strcat(program, " --parity=%s", "rs");
    660         break;
    661       default:
    662         fatal_error("Unknown RAID parity algorithm.");
    663         break;
    664       }
    665     }
    666     log_msg(4, "cmd built: %s", program);
    667     if (raidlist->el[i].chunk_size != -1) {
    668         mr_strcat(program, " --chunk=%d", raidlist->el[i].chunk_size);
    669     }
    670     if (raidlist->el[i].spare_disks.entries > 0) {
    671         mr_strcat(program, " --spare-devices=%d", raidlist->el[i].spare_disks.entries);
    672     }
    673     log_msg(4, "cmd built: %s", program);
    674     mr_strcat(program, " %s", devices);
    675     log_msg(2, "RAID device re-created with the following command:\n%s\n", program);
    676     if (test == TRUE) {
    677         res = run_program_and_log_output(program, 1);
    678     } else {
    679         // test mode, always returns TRUE without executing the mdadm command
    680         res = TRUE;
    681     }
    682     // free memory
    683     mr_free(devices);
    684     mr_free(program);
    685     // return to calling instance
    686     return res;
     492
     493/** int **************************************************************/
     494int i   = 0;
     495int j   = 0;
     496int v   = 0;
     497int res = 0;
     498
     499/** buffers ***********************************************************/
     500char *devices = NULL;
     501char *level   = NULL;
     502char *program = NULL;
     503
     504// leave straight away if raidlist is initial or has no entries
     505if (!raidlist || raidlist->entries == 0) {
     506  log_msg(1, "No RAID arrays found.");
     507  return 1;
     508} else {
     509  log_msg(1, "%d RAID arrays found.", raidlist->entries);
     510}
     511// find raidlist entry for requested device
     512for (i = 0; i < raidlist->entries; i++) {
     513  if (!strcmp(raidlist->el[i].raid_device, device)) break;
     514}
     515// check whether RAID device was found in raidlist
     516if (i == raidlist->entries) {
     517  log_msg(1, "RAID device %s not found in list.", device);
     518  return 1;
     519} else {
     520  log_msg(1, "RAID device %s found in list (%d).", device, i);
     521}
     522
     523// create device list from normal disks followed by spare ones
     524if (raidlist->el[i].data_disks.el[0].device != NULL) {
     525    mr_asprintf(devices, "%s", raidlist->el[i].data_disks.el[0].device);
     526    log_msg(4, "Adding device %s to list", raidlist->el[i].data_disks.el[0].device);
     527} else {
     528    log_msg(1, "Strange, there are entries but no device");
     529}
     530for (j = 1; j < raidlist->el[i].data_disks.entries; j++) {
     531    mr_strcat(devices, " %s", raidlist->el[i].data_disks.el[j].device);
     532    log_msg(4, "Adding device %s to list", raidlist->el[i].data_disks.el[j].device);
     533}
     534for (j = 0; j < raidlist->el[i].spare_disks.entries; j++) {
     535    mr_strcat(devices, " %s", raidlist->el[i].spare_disks.el[j].device);
     536    log_msg(4, "Adding spare device %s to list", raidlist->el[i].spare_disks.el[j].device);
     537}
     538log_msg(4, "RAID devices: %s", devices);
     539// translate RAID level
     540if (raidlist->el[i].raid_level == -2) {
     541    mr_asprintf(level, "multipath");
     542} else if (raidlist->el[i].raid_level == -1) {
     543    mr_asprintf(level, "linear");
     544} else {
     545    mr_asprintf(level, "raid%d", raidlist->el[i].raid_level);
     546}
     547// create RAID device:
     548// - RAID device, number of devices and devices mandatory
     549// - parity algorithm, chunk size and spare devices optional
     550// - faulty devices ignored
     551// - persistent superblock always used as this is recommended
     552
     553mr_asprintf(program, "mdadm --create --force --run --auto=yes %s --level=%s --raid-devices=%d", raidlist->el[i].raid_device, level, raidlist->el[i].data_disks.entries);
     554mr_free(level);
     555log_msg(4, "cmd built: %s", program);
     556// Restoring the UUID and Version stored at backup time of present
     557for (v = 0; v < raidlist->el[i].additional_vars.entries ; v++ ) {
     558    log_msg(4,"Working on additional param #%d (Label: %s)",v,raidlist->el[i].additional_vars.el[v].label);
     559    if ((raidlist->el[i].additional_vars.el[v].label != NULL) && (strcmp(raidlist->el[i].additional_vars.el[v].label,"UUID") == 0)) {
     560        // We have a UUID to handle
     561        if (raidlist->el[i].additional_vars.el[v].value != NULL) {
     562            // force its restoration in order to avoid modifying all conf files using it
     563            log_it("Managing previous UUID %s", raidlist->el[i].additional_vars.el[v].value);
     564            mr_strcat(program, " --uuid %s",raidlist->el[i].additional_vars.el[v].value);
     565            continue;
     566        } else {
     567            log_msg(1,"Unable to manage previous NULL UUID");
     568        }
     569    }
     570    if ((raidlist->el[i].additional_vars.el[v].label != NULL) && (strcmp(raidlist->el[i].additional_vars.el[v].label,"Version") == 0)) {
     571        // We have a Version to handle
     572        if (raidlist->el[i].additional_vars.el[v].value != NULL) {
     573            // force its restoration in order to support all complex boot loader + md format cases
     574            // Also see bug #473
     575            log_it("Managing previous Version %s", raidlist->el[i].additional_vars.el[v].value);
     576            mr_strcat(program, " -e %s",raidlist->el[i].additional_vars.el[v].value);
     577            continue;
     578        } else {
     579            log_msg(1,"Unable to manage previous NULL Version");
     580        }
     581    }
     582}
     583log_msg(4, "cmd built: %s", program);
     584if (raidlist->el[i].parity != -1) {
     585    switch(raidlist->el[i].parity) {
     586    case 0:
     587        mr_strcat(program, " --parity=%s", "la");
     588        break;
     589    case 1:
     590        mr_strcat(program, " --parity=%s", "ra");
     591        break;
     592    case 2:
     593        mr_strcat(program, " --parity=%s", "ls");
     594        break;
     595    case 3:
     596        mr_strcat(program, " --parity=%s", "rs");
     597        break;
     598     default:
     599        fatal_error("Unknown RAID parity algorithm.");
     600        break;
     601    }
     602}
     603log_msg(4, "cmd built: %s", program);
     604if (raidlist->el[i].chunk_size != -1) {
     605    mr_strcat(program, " --chunk=%d", raidlist->el[i].chunk_size);
     606}
     607if (raidlist->el[i].spare_disks.entries > 0) {
     608    mr_strcat(program, " --spare-devices=%d", raidlist->el[i].spare_disks.entries);
     609}
     610log_msg(4, "cmd built: %s", program);
     611mr_strcat(program, " %s", devices);
     612log_msg(2, "RAID device re-created with the following command:\n%s\n", program);
     613if (test == TRUE) {
     614    res = run_program_and_log_output(program, 1);
     615} else {
     616    // test mode, always returns TRUE without executing the mdadm command
     617    res = TRUE;
     618}
     619// free memory
     620mr_free(devices);
     621mr_free(program);
     622// return to calling instance
     623return res;
    687624}
    688625
     
    698635 * @return 0 for success, nonzero for failure.
    699636 */
    700 int format_device(char *device, char *format, struct raidlist_itself *raidlist)
    701 {
    702     /** int **************************************************************/
     637int format_device(char *device, char *format, struct raidlist_itself *raidlist) {
     638
     639/** int **************************************************************/
    703640#ifdef __FreeBSD__
    704641    static bool vinum_started_yet = FALSE;
    705642#endif
    706643
    707     /** buffers ***********************************************************/
    708     char *program;
    709     char *tmp = NULL;
    710     int res = 0;
    711     int retval = 0;
    712 
    713     /** end ****************************************************************/
    714 
    715     malloc_string(program);
    716     assert_string_is_neither_NULL_nor_zerolength(device);
    717     assert(format != NULL);
    718 
    719     if (strstr(format, "raid")) {   // do not form RAID disks; do it to /dev/md* instead
    720         mr_asprintf(tmp, "Not formatting %s (it is a RAID disk)", device);
    721         log_it(tmp);
    722         paranoid_free(tmp);
    723         paranoid_free(program);
    724         return (0);
    725     }
     644/** buffers ***********************************************************/
     645char *program = NULL;
     646char *tmp = NULL;
     647int res = 0;
     648int retval = 0;
     649
     650/** end ****************************************************************/
     651
     652assert_string_is_neither_NULL_nor_zerolength(device);
     653assert(format != NULL);
     654
     655if (strstr(format, "raid")) {   // do not form RAID disks; do it to /dev/md* instead
     656    log_it("Not formatting %s (it is a RAID disk)", device);
     657    return (0);
     658}
    726659#ifdef __FreeBSD__
    727660    if (strcmp(format, "swap") == 0) {
    728661        log_it("Not formatting %s - it's swap", device);
    729         paranoid_free(program);
    730         paranoid_free(tmp);
    731662        return (0);
    732663    }
    733664#endif
    734     if (strlen(format) <= 2) {
    735         mr_asprintf(tmp, "%s has a really small format type ('%s') - this is probably a hexadecimal string, which would suggest the partition is an image --- I shouldn't format it", device, format);
    736         log_it(tmp);
    737         paranoid_free(tmp);
    738         paranoid_free(program);
    739         return (0);
    740     }
    741     if (is_this_device_mounted(device)) {
    742         mr_asprintf(tmp, "%s is mounted - cannot format it       ", device);
    743         log_to_screen(tmp);
    744         paranoid_free(tmp);
    745         paranoid_free(program);
    746         return (1);
    747     }
    748     if (strstr(device, RAID_DEVICE_STUB)) {
    749         newtSuspend();
     665if (strlen(format) <= 2) {
     666    log_it("%s has a really small format type ('%s') - this is probably a hexadecimal string, which would suggest the partition is an image --- I shouldn't format it", device, format);
     667    return (0);
     668}
     669if (is_this_device_mounted(device)) {
     670    log_to_screen("%s is mounted - cannot format it      ", device);
     671    return (1);
     672}
     673
     674if (strstr(device, RAID_DEVICE_STUB)) {
     675    newtSuspend();
    750676#ifdef __FreeBSD__
    751         if (!vinum_started_yet) {
    752             if (!does_file_exist("/tmp/raidconf.txt")) {
    753                 log_to_screen
    754                     ("/tmp/raidconf.txt does not exist. I therefore cannot start Vinum.");
    755             } else {
    756                 int res;
    757                 res =
    758                     run_program_and_log_output
    759                     ("vinum create /tmp/raidconf.txt", TRUE);
    760                 if (res) {
    761                     log_to_screen
    762                         ("`vinum create /tmp/raidconf.txt' returned errors. Please fix them and re-run mondorestore.");
    763                     finish(1);
    764                 }
    765                 vinum_started_yet = TRUE;
    766             }
    767         }
    768 
    769         if (vinum_started_yet) {
    770             FILE *fin;
    771             char line[MAX_STR_LEN];
    772 
    773             mr_asprintf(tmp, "Initializing Vinum device %s (this may take a *long* time)", device);
    774             log_to_screen(tmp);
    775             paranoid_free(tmp);
    776 
    777             /* format raid partition */
    778             //      sprintf (program, "mkraid --really-force %s", device); --- disabled -- BB, 02/12/2003
    779             sprintf(program,
    780                     "for plex in `vinum lv -r %s | grep '^P' | tr '\t' ' ' | tr -s ' ' | cut -d' ' -f2`; do echo $plex; done > /tmp/plexes",
    781                     basename(device));
    782             paranoid_system(program);
    783             if (g_fprep) {
    784                 fprintf(g_fprep, "%s\n", program);
    785             }
    786             fin = fopen("/tmp/plexes", "r");
    787             while (fgets(line, MAX_STR_LEN - 1, fin)) {
    788                 if (strchr(line, '\n'))
    789                     *(strchr(line, '\n')) = '\0';   // get rid of the \n on the end
    790 
    791                 mr_asprintf(tmp, "Initializing plex: %s", line);
    792                 open_evalcall_form(tmp);
    793                 paranoid_free(tmp);
    794 
    795                 mr_asprintf(tmp, "vinum init %s", line);
    796                 paranoid_system(tmp);
    797                 paranoid_free(tmp);
    798 
    799                 while (1) {
    800                     mr_asprintf(tmp, "vinum lp -r %s | grep '^S' | head -1 | tr -s ' ' | cut -d: -f2 | cut -f1 | sed 's/^ //' | sed 's/I //' | sed 's/%%//'", line);
    801                     FILE *pin = popen(tmp, "r");
    802                     paranoid_free(tmp);
    803 
    804                     char status[MAX_STR_LEN / 4];
    805                     if (fgets(status, MAX_STR_LEN / 4 - 1, pin)) {
    806                         // FIXME
    807                     }
    808                     pclose(pin);
    809 
    810                     if (!strcmp(status, "up")) {
    811                         break;  /* it's done */
    812                     }
    813                     update_evalcall_form(atoi(status));
    814                     usleep(250000);
    815                 }
    816                 close_evalcall_form();
    817             }
    818             fclose(fin);
    819             unlink("/tmp/plexes");
    820         }
    821 #else
    822         mr_asprintf(tmp, "Initializing RAID device %s", device);
    823         log_to_screen(tmp);
    824         paranoid_free(tmp);
    825 
    826         // Shouldn't be necessary.
    827         log_to_screen("Stopping %s", device);
    828         stop_raid_device(device);
    829         paranoid_system("sync");
    830         sleep(1);
     677    if (!vinum_started_yet) {
     678        if (!does_file_exist("/tmp/raidconf.txt")) {
     679            log_to_screen("/tmp/raidconf.txt does not exist. I therefore cannot start Vinum.");
     680        } else {
     681            int res;
     682            res = run_program_and_log_output("vinum create /tmp/raidconf.txt", TRUE);
     683            if (res) {
     684                log_to_screen("`vinum create /tmp/raidconf.txt' returned errors. Please fix them and re-run mondorestore.");
     685                finish(1);
     686            }
     687            vinum_started_yet = TRUE;
     688        }
     689    }
     690
     691    if (vinum_started_yet) {
     692        FILE *fin;
     693        char line[MAX_STR_LEN];
     694
     695        log_to_screen(tmp, "Initializing Vinum device %s (this may take a *long* time)", device);
     696
     697        /* format raid partition */
     698        mr_asprintf(program, "for plex in `vinum lv -r %s | grep '^P' | tr '\t' ' ' | tr -s ' ' | cut -d' ' -f2`; do echo $plex; done > /tmp/plexes", basename(device));
     699        paranoid_system(program);
    831700        if (g_fprep) {
    832701            fprintf(g_fprep, "%s\n", program);
    833702        }
    834 
    835         log_msg(1, "Making %s", device);
    836         // use mkraid if it exists, otherwise use mdadm
    837             if (run_program_and_log_output("which mkraid", FALSE)) {
    838                 res = create_raid_device_via_mdadm(raidlist, device, TRUE);
    839                 log_msg(1, "Creating RAID device %s via mdadm returned %d", device, res);
    840         } else {
    841             sprintf(program, "mkraid --really-force %s", device);
    842             res = run_program_and_log_output(program, 1);
    843             log_msg(1, "%s returned %d", program, res);
    844             paranoid_system("sync");
    845             sleep(3);
    846             start_raid_device(device);
    847             if (g_fprep) {
    848                 fprintf(g_fprep, "%s\n", program);
    849             }
    850         }
     703        fin = fopen("/tmp/plexes", "r");
     704        while (fgets(line, MAX_STR_LEN - 1, fin)) {
     705            if (strchr(line, '\n'))
     706                *(strchr(line, '\n')) = '\0';   // get rid of the \n on the end
     707
     708            mr_asprintf(tmp, "Initializing plex: %s", line);
     709            open_evalcall_form(tmp);
     710            mr_free(tmp);
     711
     712            mr_asprintf(tmp, "vinum init %s", line);
     713            paranoid_system(tmp);
     714            mr_free(tmp);
     715
     716            while (1) {
     717                mr_asprintf(tmp, "vinum lp -r %s | grep '^S' | head -1 | tr -s ' ' | cut -d: -f2 | cut -f1 | sed 's/^ //' | sed 's/I //' | sed 's/%%//'", line);
     718                FILE *pin = popen(tmp, "r");
     719                mr_free(tmp);
     720
     721                char status[MAX_STR_LEN / 4];
     722                if (fgets(status, MAX_STR_LEN / 4 - 1, pin)) {
     723                    // FIXME
     724                }
     725                pclose(pin);
     726
     727                if (!strcmp(status, "up")) {
     728                    break;  /* it's done */
     729                }
     730                update_evalcall_form(atoi(status));
     731                usleep(250000);
     732            }
     733            close_evalcall_form();
     734        }
     735        fclose(fin);
     736        unlink("/tmp/plexes");
     737    }
     738#else
     739    log_to_screen("Initializing RAID device %s", device);
     740
     741    // Shouldn't be necessary.
     742    log_to_screen("Stopping %s", device);
     743    stop_raid_device(device);
     744    paranoid_system("sync");
     745    sleep(1);
     746
     747    log_msg(1, "Making %s", device);
     748    // use mkraid if it exists, otherwise use mdadm
     749    if (run_program_and_log_output("which mkraid", FALSE)) {
     750        res = create_raid_device_via_mdadm(raidlist, device, TRUE);
     751        log_msg(1, "Creating RAID device %s via mdadm returned %d", device, res);
     752    } else {
     753        mr_asprintf(program, "mkraid --really-force %s", device);
     754        res = run_program_and_log_output(program, 1);
     755        log_msg(1, "%s returned %d", program, res);
    851756        paranoid_system("sync");
    852         sleep(2);
    853 //      log_to_screen("Starting %s", device);
    854 //      sprintf(program, "raidstart %s", device);
    855 //      res = run_program_and_log_output(program, 1);
    856 //      log_msg(1, "%s returned %d", program, res);
    857 //      paranoid_system("sync"); sleep(1);
    858 #endif
    859         paranoid_system("sync");
    860         sleep(1);
    861         newtResume();
    862     }
    863 //#ifndef __FreeBSD__
    864 //#endif
    865 
    866     if (!strcmp(format, "lvm")) {
    867         log_msg(1, "Don't format %s - it's part of an lvm volume", device);
    868         paranoid_free(program);
    869         paranoid_free(tmp);
    870         return (0);
    871     }
    872     res = which_format_command_do_i_need(format, program);
    873     mr_asprintf(tmp, "%s %s", program, device);
    874     if (strstr(program, "kludge")) {
    875         mr_strcat(tmp, " /");
    876     }
    877     sprintf(program, "sh -c 'echo -en \"y\\ny\\ny\\n\" | %s'", tmp);
    878     paranoid_free(tmp);
    879 
    880     mr_asprintf(tmp, "Formatting %s as %s", device, format);
    881     update_progress_form(tmp);
    882     paranoid_free(tmp);
    883 
    884     res = run_program_and_log_output(program, FALSE);
    885     if (res) {
    886         mr_strcat(tmp, "...failed");
    887     } else {
    888         mr_strcat(tmp, "...OK");
    889     }
    890     log_to_screen(tmp);
    891     paranoid_free(tmp);
    892 
    893     if (res && strstr(program, "kludge")) {
    894         mr_asprintf(tmp, "Kludge failed; using regular mkfs.%s to format %s",
    895                 format, device);
     757        sleep(3);
     758        start_raid_device(device);
     759        if (g_fprep) {
     760            fprintf(g_fprep, "%s\n", program);
     761        }
     762    }
     763    paranoid_system("sync");
     764    sleep(2);
     765#endif
     766    paranoid_system("sync");
     767    sleep(1);
     768    newtResume();
     769}
     770
     771if (!strcmp(format, "lvm")) {
     772    log_msg(1, "Don't format %s - it's part of an lvm volume", device);
     773    mr_free(program);
     774    return (0);
     775}
     776res = which_format_command_do_i_need(format, program);
     777mr_asprintf(tmp, "%s %s", program, device);
     778if (strstr(program, "kludge")) {
     779    mr_strcat(tmp, " /");
     780}
     781mr_free(program);
     782
     783mr_asprintf(program, "sh -c 'echo -en \"y\\ny\\ny\\n\" | %s'", tmp);
     784mr_free(tmp);
     785
     786mr_asprintf(tmp, "Formatting %s as %s", device, format);
     787update_progress_form(tmp);
     788
     789res = run_program_and_log_output(program, FALSE);
     790if (res) {
     791    mr_strcat(tmp, "...failed");
     792} else {
     793    mr_strcat(tmp, "...OK");
     794}
     795log_to_screen(tmp);
     796mr_free(tmp);
     797
     798if (res && strstr(program, "kludge")) {
     799    mr_asprintf(tmp, "Kludge failed; using regular mkfs.%s to format %s", format, device);
     800    mr_free(program);
    896801#ifdef __FreeBSD__
    897         sprintf(program, "newfs_msdos -F 32 %s", device);
     802        mr_asprintf(program, "newfs_msdos -F 32 %s", device);
    898803#else
    899804#ifdef __IA64__
    900805        /* For EFI partitions take fat16
    901806         * as we want to make small ones */
    902         sprintf(program, "mkfs -t %s -F 16 %s", format, device);
     807        mr_asprintf(program, "mkfs -t %s -F 16 %s", format, device);
    903808#else
    904         sprintf(program, "mkfs -t %s -F 32 %s", format, device);
    905 #endif
    906 #endif
    907         res = run_program_and_log_output(program, FALSE);
    908         if (g_fprep) {
    909             fprintf(g_fprep, "%s\n", program);
    910         }
    911         if (retval) {
    912             mr_strcat(tmp, "...failed");
    913         } else {
    914             mr_strcat(tmp, "...OK");
    915         }
    916 
    917         log_to_screen(tmp);
    918         paranoid_free(tmp);
    919 
    920     }
    921     retval += res;
    922     paranoid_free(program);
    923     paranoid_system("sync");
    924     sleep(1);
    925     return (retval);
    926 }
    927 
    928 
    929 
     809        mr_asprintf(program, "mkfs -t %s -F 32 %s", format, device);
     810#endif
     811#endif
     812    res = run_program_and_log_output(program, FALSE);
     813    if (g_fprep) {
     814        fprintf(g_fprep, "%s\n", program);
     815    }
     816    mr_free(program);
     817
     818    if (retval) {
     819        mr_strcat(tmp, "...failed");
     820    } else {
     821        mr_strcat(tmp, "...OK");
     822    }
     823
     824    log_to_screen(tmp);
     825    mr_free(tmp);
     826
     827}
     828retval += res;
     829paranoid_system("sync");
     830sleep(1);
     831return (retval);
     832}
    930833
    931834
     
    936839 * @return The number of errors encountered (0 for success).
    937840 */
    938 int format_everything(struct mountlist_itself *mountlist, bool interactively,
    939                             struct raidlist_itself *raidlist)
    940 {
    941     /** int **************************************************************/
    942     int retval = 0;
    943     int lino;
    944     int res;
    945 //  int i;
    946 //  struct list_of_disks *drivelist;
    947 
    948     /** long *************************************************************/
    949     long progress_step;
    950 
    951     /** bools ************************************************************/
    952     bool do_it;
    953 
    954     /** buffers **********************************************************/
    955     char *tmp;
    956 
    957     /** pointers *********************************************************/
    958     struct mountlist_line *me;  // mountlist entry
    959     /** end **************************************************************/
    960 
    961     assert(mountlist != NULL);
    962     malloc_string(tmp);
    963     sprintf(tmp, "format_everything (mountlist, interactively = %s",
    964             (interactively) ? "true" : "false");
    965     log_it(tmp);
    966     mvaddstr_and_log_it(g_currentY, 0, "Formatting partitions      ");
    967     open_progress_form("Formatting partitions",
    968                          "I am now formatting your hard disk partitions.",
    969                          "This may take up to five minutes.", "",
    970                          mountlist->entries + 1);
    971 
    972     progress_step =
    973         (mountlist->entries >
    974          0) ? g_maximum_progress / mountlist->entries : 1;
     841int format_everything(struct mountlist_itself *mountlist, bool interactively, struct raidlist_itself *raidlist) {
     842
     843/** int **************************************************************/
     844int retval = 0;
     845int lino;
     846int res;
     847
     848/** long *************************************************************/
     849long progress_step;
     850
     851/** bools ************************************************************/
     852bool do_it;
     853
     854/** buffers **********************************************************/
     855char *tmp = NULL;
     856
     857/** pointers *********************************************************/
     858struct mountlist_line *me;  // mountlist entry
     859/** end **************************************************************/
     860
     861assert(mountlist != NULL);
     862log_it("format_everything (mountlist, interactively = %s", (interactively) ? "true" : "false");
     863mvaddstr_and_log_it(g_currentY, 0, "Formatting partitions      ");
     864open_progress_form("Formatting partitions", "I am now formatting your hard disk partitions.", "This may take up to five minutes.", "", mountlist->entries + 1);
     865
     866progress_step = (mountlist->entries > 0) ? g_maximum_progress / mountlist->entries : 1;
    975867// start soft-raids now (because LVM might depend on them)
    976868// ...and for simplicity's sake, let's format them at the same time :)
    977     log_msg(1, "Stopping all RAID devices");
    978     stop_all_raid_devices(mountlist);
    979     paranoid_system("sync");
    980     paranoid_system("sync");
    981     paranoid_system("sync");
    982     sleep(2);
    983     log_msg(1, "Prepare soft-RAIDs");   // prep and format too
    984     for (lino = 0; lino < mountlist->entries; lino++) {
    985         me = &mountlist->el[lino];  // the current mountlist entry
    986         log_msg(2, "Examining %s", me->device);
    987         if (!strncmp(me->device, "/dev/md", 7)) {
    988             if (interactively) {
    989                 // ask user if we should format the current device
    990                 sprintf(tmp, "Shall I format %s (%s) ?", me->device,
    991                         me->mountpoint);
    992                 do_it = ask_me_yes_or_no(tmp);
    993             } else {
    994                 do_it = TRUE;
    995             }
    996             if (do_it) {
    997                 // NB: format_device() also stops/starts RAID device if necessary
    998                 retval += format_device(me->device, me->format, raidlist);
    999             }
    1000             g_current_progress += progress_step;
    1001         }
    1002     }
    1003     paranoid_system("sync");
    1004     paranoid_system("sync");
    1005     paranoid_system("sync");
    1006     sleep(2);
    1007 // This last step is probably necessary
    1008 //  log_to_screen("Re-starting software RAIDs...");
    1009 //  start_all_raid_devices(mountlist);
    1010 //  paranoid_system("sync"); paranoid_system("sync"); paranoid_system("sync");
    1011 //  sleep(5);
     869log_msg(1, "Stopping all RAID devices");
     870stop_all_raid_devices(mountlist);
     871paranoid_system("sync");
     872paranoid_system("sync");
     873paranoid_system("sync");
     874sleep(2);
     875log_msg(1, "Prepare soft-RAIDs");   // prep and format too
     876for (lino = 0; lino < mountlist->entries; lino++) {
     877    me = &mountlist->el[lino];  // the current mountlist entry
     878    log_msg(2, "Examining %s", me->device);
     879    if (!strncmp(me->device, "/dev/md", 7)) {
     880        if (interactively) {
     881            // ask user if we should format the current device
     882            mr_asprintf(tmp, "Shall I format %s (%s) ?", me->device, me->mountpoint);
     883            do_it = ask_me_yes_or_no(tmp);
     884            mr_free(tmp);
     885        } else {
     886            do_it = TRUE;
     887        }
     888        if (do_it) {
     889            // NB: format_device() also stops/starts RAID device if necessary
     890            retval += format_device(me->device, me->format, raidlist);
     891        }
     892        g_current_progress += progress_step;
     893    }
     894}
     895paranoid_system("sync");
     896paranoid_system("sync");
     897paranoid_system("sync");
     898sleep(2);
    1012899// do LVMs now
    1013     log_msg(1, "Creating LVMs");
    1014     if (does_file_exist("/tmp/i-want-my-lvm")) {
    1015         wait_until_software_raids_are_prepped("/proc/mdstat", 100);
    1016         log_to_screen("Configuring LVM");
    1017         if (!g_text_mode) {
    1018             newtSuspend();
    1019         }
    1020 /*
    1021         for(i=0; i<3; i++)
    1022             {
    1023               res = do_my_funky_lvm_stuff(FALSE, FALSE);
    1024               if (!res) { break; }
    1025               sleep(3);
    1026               res = do_my_funky_lvm_stuff(TRUE, FALSE);
    1027               sleep(3);
    1028             }
    1029         if (res) {
    1030             log_msg(1, "Vacuum-packing...");
    1031 */
    1032         res = do_my_funky_lvm_stuff(FALSE, TRUE);
    1033 /*
    1034         }
    1035 */
    1036         if (!g_text_mode) {
    1037             newtResume();
    1038         }
    1039         if (!res) {
    1040             log_to_screen("LVM initialized OK");
     900log_msg(1, "Creating LVMs");
     901if (does_file_exist("/tmp/i-want-my-lvm")) {
     902    wait_until_software_raids_are_prepped("/proc/mdstat", 100);
     903    log_to_screen("Configuring LVM");
     904    if (!g_text_mode) {
     905        newtSuspend();
     906    }
     907    res = do_my_funky_lvm_stuff(FALSE, TRUE);
     908    if (!g_text_mode) {
     909        newtResume();
     910    }
     911    if (!res) {
     912        log_to_screen("LVM initialized OK");
     913    } else {
     914        log_to_screen("Failed to initialize LVM");
     915    }
     916    // retval += res;
     917    if (res) {
     918        retval++;
     919    }
     920    sleep(3);
     921}
     922// do regulars at last
     923sleep(2);                   // woo!
     924log_msg(1, "Formatting regulars");
     925for (lino = 0; lino < mountlist->entries; lino++) {
     926    me = &mountlist->el[lino];  // the current mountlist entry
     927    if (!strcmp(me->mountpoint, "image")) {
     928        log_it("Not formatting %s - it's an image", me->device);
     929    } else if (!strcmp(me->format, "raid")) {
     930        log_it("Not formatting %s - it's a raid-let", me->device);
     931        continue;
     932    } else if (!strcmp(me->format, "lvm")) {
     933        log_it("Not formatting %s - it's an LVM", me->device);
     934        continue;
     935    } else if (!strncmp(me->device, "/dev/md", 7)) {
     936        log_it("Already formatted %s - it's a soft-RAID dev", me->device);
     937        continue;
     938    } else if (!does_file_exist(me->device) && strncmp(me->device, "/dev/hd", 7) && strncmp(me->device, "/dev/sd", 7)) {
     939        log_it("Not formatting %s yet - doesn't exist - probably an LVM", me->device);
     940        continue;
     941    } else {
     942        if (interactively) {
     943            // ask user if we should format the current device
     944            mr_asprintf(tmp, "Shall I format %s (%s) ?", me->device, me->mountpoint);
     945            do_it = ask_me_yes_or_no(tmp);
     946            mr_free(tmp);
    1041947        } else {
    1042             log_to_screen("Failed to initialize LVM");
    1043         }
    1044         // retval += res;
    1045         if (res) {
    1046             retval++;
    1047         }
    1048         sleep(3);
    1049     }
    1050 // do regulars at last
    1051     sleep(2);                   // woo!
    1052     log_msg(1, "Formatting regulars");
    1053     for (lino = 0; lino < mountlist->entries; lino++) {
    1054         me = &mountlist->el[lino];  // the current mountlist entry
    1055         if (!strcmp(me->mountpoint, "image")) {
    1056             sprintf(tmp, "Not formatting %s - it's an image", me->device);
    1057             log_it(tmp);
    1058         } else if (!strcmp(me->format, "raid")) {
    1059             sprintf(tmp, "Not formatting %s - it's a raid-let",
    1060                     me->device);
    1061             log_it(tmp);
    1062             continue;
    1063         } else if (!strcmp(me->format, "lvm")) {
    1064             sprintf(tmp, "Not formatting %s - it's an LVM", me->device);
    1065             log_it(tmp);
    1066             continue;
    1067         } else if (!strncmp(me->device, "/dev/md", 7)) {
    1068             sprintf(tmp, "Already formatted %s - it's a soft-RAID dev",
    1069                     me->device);
    1070             log_it(tmp);
    1071             continue;
    1072         } else if (!does_file_exist(me->device)
    1073                      && strncmp(me->device, "/dev/hd", 7)
    1074                      && strncmp(me->device, "/dev/sd", 7)) {
    1075             sprintf(tmp,
    1076                     "Not formatting %s yet - doesn't exist - probably an LVM",
    1077                     me->device);
    1078             log_it(tmp);
    1079             continue;
    1080         } else {
    1081             if (interactively) {
    1082                 // ask user if we should format the current device
    1083                 sprintf(tmp, "Shall I format %s (%s) ?", me->device,
    1084                         me->mountpoint);
    1085                 do_it = ask_me_yes_or_no(tmp);
    1086             } else {
    1087                 do_it = TRUE;
    1088             }
    1089 
    1090             if (do_it)
    1091                 retval += format_device(me->device, me->format, raidlist);
    1092         }
    1093 
    1094         // update progress bar
    1095         g_current_progress += progress_step;
    1096     }
    1097 
    1098 
    1099     // update progress bar to 100% to compensate for
    1100     // rounding errors of the progress_step calculation
    1101     if (lino >= mountlist->entries)
    1102         g_current_progress = g_maximum_progress;
    1103 
    1104     close_progress_form();
    1105 
    1106     if (retval) {
    1107         mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
    1108         log_to_screen
    1109             ("Errors occurred during the formatting of your hard drives.");
     948            do_it = TRUE;
     949        }
     950
     951        if (do_it)
     952            retval += format_device(me->device, me->format, raidlist);
     953    }
     954
     955    // update progress bar
     956    g_current_progress += progress_step;
     957}
     958
     959
     960// update progress bar to 100% to compensate for
     961// rounding errors of the progress_step calculation
     962if (lino >= mountlist->entries)
     963    g_current_progress = g_maximum_progress;
     964
     965close_progress_form();
     966
     967if (retval) {
     968    mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
     969    log_to_screen("Errors occurred during the formatting of your hard drives.");
     970} else {
     971    mvaddstr_and_log_it(g_currentY++, 74, "Done.");
     972}
     973
     974log_it("format_everything () - %s", (retval) ? "failed!" : "finished successfully");
     975
     976if (g_partition_table_locked_up > 0) {
     977    if (retval > 0 && !interactively) {
     978//123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
     979        log_to_screen("Partition table locked up %d times. At least one 'mkfs' (format) command", g_partition_table_locked_up);
     980        log_to_screen("failed. I think these two events are related. Sometimes, fdisk's ioctl() call");
     981        log_to_screen("to refresh its copy of the partition table causes the kernel to lock the ");
     982        log_to_screen("partition table. I believe this has just happened.");
     983        if (ask_me_yes_or_no("Please choose 'yes' to reboot and try again; or 'no' to ignore this warning and continue.")) {
     984            paranoid_system("sync");
     985            paranoid_system("sync");
     986            paranoid_system("sync");
     987            paranoid_system("reboot");
     988        }
    1110989    } else {
    1111         mvaddstr_and_log_it(g_currentY++, 74, "Done.");
    1112     }
    1113 
    1114     sprintf(tmp, "format_everything () - %s",
    1115             (retval) ? "failed!" : "finished successfully");
    1116     log_it(tmp);
    1117 
    1118     if (g_partition_table_locked_up > 0) {
    1119         if (retval > 0 && !interactively) {
    1120 //123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
    1121             log_to_screen
    1122                 ("Partition table locked up %d times. At least one 'mkfs' (format) command",
    1123                  g_partition_table_locked_up);
    1124             log_to_screen
    1125                 ("failed. I think these two events are related. Sometimes, fdisk's ioctl() call");
    1126             log_to_screen
    1127                 ("to refresh its copy of the partition table causes the kernel to lock the ");
    1128             log_to_screen
    1129                 ("partition table. I believe this has just happened.");
    1130             if (ask_me_yes_or_no
    1131                 ("Please choose 'yes' to reboot and try again; or 'no' to ignore this warning and continue."))
    1132             {
    1133                 paranoid_system("sync");
    1134                 paranoid_system("sync");
    1135                 paranoid_system("sync");
    1136                 paranoid_system("reboot");
    1137             }
    1138         } else {
    1139             log_to_screen
    1140                 ("Partition table locked up %d time%c. However, disk formatting succeeded.",
    1141                  g_partition_table_locked_up,
    1142                  (g_partition_table_locked_up == 1) ? '.' : 's');
    1143         }
    1144     }
    1145     newtSuspend();
    1146     paranoid_system("clear");
    1147     newtResume();
    1148     paranoid_free(tmp);
    1149     return (retval);
     990        log_to_screen("Partition table locked up %d time%c. However, disk formatting succeeded.", g_partition_table_locked_up, (g_partition_table_locked_up == 1) ? '.' : 's');
     991    }
     992}
     993newtSuspend();
     994paranoid_system("clear");
     995newtResume();
     996return (retval);
    1150997}
    1151998
     
    11591006 * @return The number of errors encountered (0 for success).
    11601007 */
    1161 int make_dummy_partitions(FILE * pout_to_fdisk, char *drivename,
    1162                             int devno_we_must_allow_for)
    1163 {
     1008int make_dummy_partitions(FILE * pout_to_fdisk, char *drivename, int devno_we_must_allow_for) {
    11641009    /** int **************************************************************/
    11651010    int current_devno;
Note: See TracChangeset for help on using the changeset viewer.