Changeset 688 in MondoRescue for trunk/mondo/mondo/mondorestore/mondo-prep.c
- Timestamp:
- Jul 17, 2006, 3:44:46 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/mondo/mondo/mondorestore/mondo-prep.c
r687 r688 1 1 /*************************************************************************** 2 mondo-prep.c - description 3 ----------------- 4 5 begin: Fri Apr 19 16:40:35 EDT 2002 6 copyright : (C) 2002 Mondo Hugo Rabson 7 email : Hugo Rabson <hugorabson@msn.com> 8 edited by : by Stan Benoit 4/2002 9 email : troff@nakedsoul.org 10 cvsid : $Id$ 11 ***************************************************************************/ 12 13 /*************************************************************************** 14 * * 15 * This program is free software; you can redistribute it and/or modify * 16 * it under the terms of the GNU General Public License as published by * 17 * the Free Software Foundation; either version 2 of the License, or * 18 * (at your option) any later version. * 19 * * 20 ***************************************************************************/ 21 22 /* mondo-prep.c Hugo Rabson 23 24 25 26 07/20 27 - when starting RAID, don't do it twice :) 28 - moved Joshua's new get_phys_size_of_drive() code 29 from resize_drive... into get_phys_size_of_drive() 30 31 06/29 32 - make sure software RAID devices are formatted IF user says they're to be 33 - drivelist is struct now, not char[][] 34 35 06/26 36 - drop make_relevant_partition_bootable(); do it yourself in C (mostly) 37 - offer to reboot if partition table is locked up by the kernel 38 39 06/22 40 - be sure not to resize non-NTFS images when restoring 41 - set non-NTFS images' partition types properly 42 43 06/19 44 - shut down all LVMs and VGs before prepping 45 46 05/07 47 - usage of parted2fdisk instead of fdisk alone (ia32/ia64 compatibility) 48 BCO 49 50 03/31 51 - rewrote partitioning and formatting code to call fdisk once per disk 52 53 10/21/2003 54 - suspend/resume Newt gui before/after calling do_my_funky_lvm_stuff() 55 56 10/20 57 - rewrote format_everything() - what a mess it was. 58 It now does things in three phases:- 59 - formats software RAID devices (/dev/md0, etc.) 60 - formats and configures LVM devices 61 - formats regular partitions (/dev/hda1, /dev/sdb2, etc.) 62 and any LVMs recently prepped 63 64 10/07 65 - use strstr(format, "raid") instead of strcmp(format,"raid") to determin 66 if partition is a RAID component 67 68 09/23 69 - better comments 70 71 09/18 72 - better logging of RAID activity 73 74 05/05 75 - added Joshua Oreman's FreeBSD patches 76 77 04/30 78 - added textonly mode 79 80 04/24 81 - added lots of assert()'s and log_OS_error()'s 82 83 04/21 84 - format_everything() --- don't let bar go too far 85 - mkfs -c to check for bad blocks when formatting 86 87 04/04 88 - misc clean-up (Tom Mortell) 89 90 01/15/2003 91 - added code for LVM and SW Raid (Brian Borgeson) 92 93 12/10/2002 94 - line 1238: friendlier output 95 96 11/20 97 - when wiping a given device in preparation for partitioning + formatting 98 it, don't wipe the MBR; just the partition table. That allows for 99 stupid-ass Compaq users who like to play with their MBR's. 100 - disable mountlist.txt-->mountlist.txt.pre-resize copying (superfluous) 101 102 09/09 103 - run_program_and_log_output() now takes boolean operator to specify 104 whether it will log its activities in the event of _success_ 105 106 07/01 - 07/31 107 - added hooks to libmondo 108 - RAID enhancements (Philippe de Muyter) 109 110 01/01 - 03/31 111 - partition_device() will refuse to partition /dev/mdX now (cos it's a 112 virtual drive); however, it will return 0 (i.e. not an error) 113 - make_list_of_drives() will exclude /dev/md* from list 114 - mkreiserfs -ff instead of -q (Andy Glass) 115 - don't resize drive if drive not found (or if its size cannot be det'd) 116 - when generating list of drives from mountlist, skip the 'p' at the end 117 of drive name if /dev/ida/ or /dev/cciss/; just do it (Michael Eisenberg) 118 - don't specify journal size when formatting ext3 119 (used to have -Jsize=10 in the call to mkfs) 120 - handles files >2GB in size 121 - format interactively, if Interactive Mode 122 - removed reference to g_tape_size 123 - when executing /tmp/i-want-my-lvm, only record the error# if the command 124 was _not_ an 'insmod' command 125 - pass partition size to fdisk in Kilobytes now, not Megabytes 126 - log fdisk's output to /tmp/mondo-restore.log if it fails 127 - don't try to format partitions of type 'image' 128 - don't type to set types of 'image' partitions 129 - if format code is 1 or 2 chars then assume it is a hex string 130 - took out all '/ /' comments 131 - don't extrapolate/add partition from RAID dev to mountlist if it's already 132 present in mountlist 133 - less repetitive logging in the event of vacuum-packing of last part'n 134 - no extrapolation at all: RAID partitions should be listed in mountlist 135 already, thanks to either Mindi v0.5x or the mountlist editor itself 136 - no longer say, 'and logging to...' when setting a partition's type 137 - don't run mkfs on RAID partitions (/dev/hd*, /dev/sd*); just set type 138 - the setting of a partition's type now takes place in a separate subroutine 139 from the subroutine that actually creates the partition 140 - no need to set type if 83: 83 is the default (under fdisk) 141 - turned on '-Wall'; cleaned up some cruft 142 - if vacuum-packing partition (i.e. size=0MB --> max) then say, "(maximum)" 143 not, "(0 MB)" 144 145 11/22/2001 146 - preliminary code review 147 - created on Nov 22nd, 2001 148 */ 2 * $Id 3 **/ 149 4 150 5 /** … … 159 14 #include "../common/libmondo.h" 160 15 #include "mondo-rstr-tools-EXT.h" 16 161 17 #include <sys/ioctl.h> 162 18 #include <linux/hdreg.h> 163 19 #include <math.h> 20 #include <unistd.h> 164 21 165 22 … … 185 42 extern char *g_mountlist_fname; 186 43 extern long g_current_progress, g_maximum_progress; 187 188 44 extern bool g_text_mode; 189 190 45 extern void pause_for_N_seconds(int, char *); 191 46 192 193 47 FILE *g_fprep = NULL; 194 195 196 197 48 int g_partition_table_locked_up = 0; 198 199 200 201 202 203 204 205 206 49 207 50 … … 209 52 { 210 53 char *command; 211 char *tmp;212 54 int lino; 213 55 int i; … … 218 60 // If LVMs are present and a zero-and-reboot wasn't recently undertaken 219 61 // then zero & insist on reboot. 220 malloc_string(command);221 malloc_string(tmp);222 62 buf = malloc(blocksize); 223 63 if (does_file_exist("/tmp/i-want-my-lvm")) // FIXME - cheating :) … … 226 66 make_list_of_drives_in_mountlist(mountlist, drivelist); 227 67 for (lino = 0; lino < drivelist->entries; lino++) { 228 sprintf(command,229 "dd if=%s bs=512 count=1 2> /dev/null | grep \"%s\"",230 drivelist->el[lino].device, MONDO_WAS_HERE);68 asprintf(&command, 69 "dd if=%s bs=512 count=1 2> /dev/null | grep \"%s\"", 70 drivelist->el[lino].device, MONDO_WAS_HERE); 231 71 if (!run_program_and_log_output(command, 1)) { 232 72 log_msg(1, "Found MONDO_WAS_HERE marker on drive#%d (%s)", 233 73 lino, drivelist->el[lino].device); 74 paranoid_free(command); 234 75 break; 235 76 } 77 paranoid_free(command); 236 78 } 237 79 … … 239 81 // zero & reboot 240 82 log_to_screen 241 (_("I am sorry for the inconvenience but I must ask you to reboot.")); 242 log_to_screen 243 (_("I need to reset the Master Boot Record; in order to be")); 244 log_to_screen 245 (_("sure the kernel notices, I must reboot after doing it.")); 83 (_ 84 ("I am sorry for the inconvenience but I must ask you to reboot.")); 85 log_to_screen(_ 86 ("I need to reset the Master Boot Record; in order to be")); 87 log_to_screen(_ 88 ("sure the kernel notices, I must reboot after doing it.")); 246 89 log_to_screen("Please hit 'Enter' to reboot."); 247 90 for (lino = 0; lino < drivelist->entries; lino++) { … … 265 108 } 266 109 } 267 sy stem("sync");268 sy stem("sync");269 sy stem("sync");110 sync(); 111 sync(); 112 sync(); 270 113 popup_and_OK 271 (_("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.")); 114 (_ 115 ("I must reboot now. Please leave the boot media in the drive and repeat your actions - e.g. type 'nuke' - and it should work fine.")); 272 116 system("reboot"); 273 117 } 274 118 } 275 119 // Still here? Cool! 276 paranoid_free(command);277 paranoid_free(tmp);278 120 log_msg(1, "Cool. I didn't have to wipe anything."); 279 121 } 280 281 282 283 284 122 285 123 … … 311 149 return (i); 312 150 } 313 314 315 316 317 318 319 320 321 151 322 152 … … 330 160 * @return The number of errors encountered (0 for success). 331 161 */ 332 333 334 162 int do_my_funky_lvm_stuff(bool just_erase_existing_volumes, 335 163 bool vacuum_pack) 336 164 { 337 /** buffers **********************************************/338 char *tmp;339 char *incoming;340 char *command;341 char *lvscan_sz;342 char *lvremove_sz;343 char *pvscan_sz;344 char *vgscan_sz;345 char *vgcreate_sz;346 char *vgchange_sz;347 char *vgremove_sz;348 // char *do_this_last;349 350 165 /** char **************************************************/ 351 char *p; 352 char *q; 166 char *tmp = NULL; 167 char *tmp1 = NULL; 168 char *incoming = NULL; 169 char *command = NULL; 170 char *lvscan_sz = NULL; 171 char *lvremove_sz = NULL; 172 char *pvscan_sz = NULL; 173 char *vgscan_sz = NULL; 174 char *vgchange_sz = NULL; 175 char *vgremove_sz = NULL; 176 char *p = NULL; 177 char *q = NULL; 353 178 354 179 /** int ***************************************************/ 355 180 int retval = 0; 356 181 int res = 0; 357 int i ;182 int i = 0; 358 183 int lvmversion = 1; 359 184 long extents; 360 185 fpos_t orig_pos; 186 size_t n = 0; 187 size_t n1 = 0; 361 188 362 189 /** pointers **********************************************/ … … 374 201 } 375 202 376 malloc_string(tmp);377 malloc_string(incoming);378 malloc_string(lvscan_sz);379 malloc_string(lvremove_sz);380 malloc_string(vgscan_sz);381 malloc_string(pvscan_sz);382 malloc_string(vgcreate_sz);383 malloc_string(vgchange_sz);384 malloc_string(vgremove_sz);385 // malloc_string(do_this_last); // postpone lvcreate call if necessary386 command = malloc(512);387 388 // do_this_last[0] = '\0';389 203 iamhere("STARTING"); 390 204 log_msg(1, "OK, opened i-want-my-lvm. Shutting down LVM volumes..."); 391 205 if (find_home_of_exe("lvm")) // found it :) cool 392 206 { 393 strcpy(lvscan_sz, "lvm lvscan"); 394 strcpy(lvremove_sz, "lvm lvremove"); 395 strcpy(vgscan_sz, "lvm vgscan"); 396 strcpy(pvscan_sz, "lvm pvscan"); 397 strcpy(vgcreate_sz, "lvm vgcreate"); 398 strcpy(vgchange_sz, "lvm vgchange"); 399 strcpy(vgremove_sz, "lvm vgremove"); 207 asprintf(&lvscan_sz, "lvm lvscan"); 208 asprintf(&lvremove_sz, "lvm lvremove"); 209 asprintf(&vgscan_sz, "lvm vgscan"); 210 asprintf(&pvscan_sz, "lvm pvscan"); 211 asprintf(&vgchange_sz, "lvm vgchange"); 212 asprintf(&vgremove_sz, "lvm vgremove"); 400 213 } else { 401 strcpy(lvscan_sz, "lvscan"); 402 strcpy(lvremove_sz, "lvremove"); 403 strcpy(vgscan_sz, "vgscan"); 404 strcpy(pvscan_sz, "pvscan"); 405 strcpy(vgcreate_sz, "vgcreate"); 406 strcpy(vgchange_sz, "vgchange"); 407 strcpy(vgremove_sz, "vgremove"); 408 } 409 sprintf(command, 214 asprintf(&lvscan_sz, "lvscan"); 215 asprintf(&lvremove_sz, "lvremove"); 216 asprintf(&vgscan_sz, "vgscan"); 217 asprintf(&pvscan_sz, "pvscan"); 218 asprintf(&vgchange_sz, "vgchange"); 219 asprintf(&vgremove_sz, "vgremove"); 220 } 221 asprintf(&command, 410 222 "for i in `%s | cut -d\"'\" -f2 | sort -r` ; do echo \"Shutting down lv $i\" >> " 411 223 MONDO_LOGFILE "; %s -f $i; done", lvscan_sz, lvremove_sz); 224 paranoid_free(lvscan_sz); 225 paranoid_free(lvremove_sz); 226 412 227 run_program_and_log_output(command, 5); 228 paranoid_free(command); 229 413 230 sleep(1); 414 sprintf(command,231 asprintf(&command, 415 232 "for i in `%s | grep -i lvm | cut -d'\"' -f2` ; do %s -a n $i ; %s $i; echo \"Shutting down vg $i\" >> " 416 MONDO_LOGFILE "; done; %s -a n", vgscan_sz, vgchange_sz, 417 vgremove_sz, vgremove_sz); 233 MONDO_LOGFILE "; done; %s -a n", vgscan_sz, vgchange_sz, vgremove_sz, vgremove_sz); 234 paranoid_free(vgchange_sz); 235 paranoid_free(vgremove_sz); 236 418 237 run_program_and_log_output(command, 5); 238 paranoid_free(command); 239 419 240 if (just_erase_existing_volumes) { 420 241 paranoid_fclose(fin); 421 242 log_msg(1, "Closed i-want-my-lvm. Finished erasing LVMs."); 422 retval = 0; 423 goto end_of_i_want_my_lvm; 243 sync(); 244 sync(); 245 sync(); 246 sleep(1); 247 iamhere("ENDING"); 248 log_msg(1, "Not many errors. Returning 0."); 249 return (0); 424 250 } 425 251 426 252 log_msg(1, "OK, rewound i-want-my-lvm. Doing funky stuff..."); 427 253 rewind(fin); 428 for ( fgets(incoming, 512, fin); !feof(fin); fgets(incoming, 512, fin)) {254 for (getline(&incoming, &n1, fin); !feof(fin); getline(&incoming, &n1, fin)) { 429 255 fgetpos(fin, &orig_pos); 256 /* BERLIOS : Strange no ? */ 430 257 if (incoming[0] != '#') { 431 258 continue; … … 433 260 if (res && strstr(command, "create") && vacuum_pack) { 434 261 sleep(2); 435 sy stem("sync");436 sy stem("sync");437 sy stem("sync");262 sync(); 263 sync(); 264 sync(); 438 265 } 439 266 if ((p = strstr(incoming, "vgcreate"))) { 440 267 // include next line(s) if they end in /dev (cos we've got a broken i-want-my-lvm) 441 for ( fgets(tmp, 512, fin); !feof(fin); fgets(tmp, 512, fin)) {268 for (getline(&tmp, &n, fin); !feof(fin); getline(&tmp, &n, fin)) { 442 269 if (tmp[0] == '#') { 443 270 fsetpos(fin, &orig_pos); … … 445 272 } else { 446 273 fgetpos(fin, &orig_pos); 447 strcat(incoming, tmp); 274 asprintf(&tmp1, "%s%s", incoming, tmp); 275 paranoid_free(incoming); 276 incoming = tmp1; 448 277 } 449 278 } 279 paranoid_free(tmp); 280 450 281 for (q = incoming; *q != '\0'; q++) { 451 282 if (*q < 32) { … … 453 284 } 454 285 } 455 strcpy(tmp, p + strlen("vgcreate") + 1); 456 for (q = tmp; *q > 32; q++); 286 malloc_string(tmp1); 287 strcpy(tmp1, p + strlen("vgcreate") + 1); 288 for (q = tmp1; *q > 32; q++); 457 289 *q = '\0'; 458 log_msg(1, "Deleting old entries at /dev/%s", tmp); 459 // sprintf(command, "%s -f %s", vgremove_sz, tmp); 460 // run_program_and_log_output(command, 1); 461 sprintf(command, "rm -Rf /dev/%s", tmp); 462 run_program_and_log_output(command, 1); 290 log_msg(1, "Deleting old entries at /dev/%s", tmp1); 291 asprintf(&tmp, "rm -Rf /dev/%s", tmp1); 292 paranoid_free(tmp1); 293 run_program_and_log_output(tmp, 1); 294 paranoid_free(tmp); 295 463 296 run_program_and_log_output(vgscan_sz, 1); 464 297 run_program_and_log_output(pvscan_sz, 1); … … 468 301 } 469 302 for (p = incoming + 1; *p == ' '; p++); 470 strcpy(command, p); 303 paranoid_free(command); 304 asprintf(&command, p); 471 305 for (p = command; *p != '\0'; p++); 472 306 for (; *(p - 1) < 32; p--); … … 485 319 if (strstr(command, "lvm lvcreate")) 486 320 lvmversion = 2; 321 /* BERLIOS : this tmp may be uninitialized ? 487 322 log_it("%s... so I'll get creative.", tmp); 323 */ 488 324 if (lvmversion == 2) { 489 strcpy(tmp, call_program_and_get_last_line_of_output 490 ("tail -n5 "MONDO_LOGFILE" | grep Insufficient | tail -n1")); 325 tmp = call_program_and_get_last_line_of_output 326 ("tail -n5 " MONDO_LOGFILE 327 " | grep Insufficient | tail -n1"); 491 328 } else { 492 strcpy(tmp, call_program_and_get_last_line_of_output 493 ("tail -n5 "MONDO_LOGFILE" | grep lvcreate | tail -n1")); 329 tmp = call_program_and_get_last_line_of_output 330 ("tail -n5 " MONDO_LOGFILE 331 " | grep lvcreate | tail -n1"); 494 332 } 495 333 for (p = tmp; *p != '\0' && !isdigit(*p); p++); 496 334 extents = atol(p); 497 335 log_msg(5, "p='%s' --> extents=%ld", p, extents); 336 paranoid_free(tmp); 498 337 p = strstr(command, "-L"); 499 338 if (!p) { … … 543 382 retval++; 544 383 } 545 sprintf(tmp, "echo \"%s\" >> /tmp/out.sh", command);384 asprintf(&tmp, "echo \"%s\" >> /tmp/out.sh", command); 546 385 system(tmp); 386 paranoid_free(tmp); 547 387 sleep(1); 548 388 } 549 389 paranoid_fclose(fin); 550 log_msg(1, "Closed i-want-my-lvm. Finished doing funky stuff.");551 end_of_i_want_my_lvm:552 paranoid_free(tmp);553 paranoid_free(incoming);554 paranoid_free(command);555 paranoid_free(lvscan_sz);556 paranoid_free(lvremove_sz);557 390 paranoid_free(vgscan_sz); 558 391 paranoid_free(pvscan_sz); 559 paranoid_free( vgcreate_sz);560 paranoid_free( vgchange_sz);561 paranoid_free(vgremove_sz); 562 // paranoid_free(do_this_last);563 sy stem("sync");564 sy stem("sync");565 sy stem("sync");392 paranoid_free(command); 393 paranoid_free(incoming); 394 395 log_msg(1, "Closed i-want-my-lvm. Finished doing funky stuff."); 396 sync(); 397 sync(); 398 sync(); 566 399 sleep(1); 567 400 iamhere("ENDING"); … … 589 422 *old_mountlist) 590 423 { 591 /** pointers *********************************************************/ 592 FILE *fin; 593 594 /** int **************************************************************/ 595 int lino; 596 int j; 597 598 /** buffers **********************************************************/ 599 char *incoming; 600 char *tmp; 601 602 /** pointers *********************************************************/ 603 char *p; 424 FILE *fin = NULL; 425 int lino = 0; 426 int j = 0; 427 char *incoming = NULL; 428 char *tmp = NULL; 429 char *p = NULL; 430 size_t n = 0; 604 431 605 432 /** init *************************************************************/ … … 608 435 /** end **************************************************************/ 609 436 610 malloc_string(incoming);611 malloc_string(tmp);612 437 assert(new_mountlist != NULL); 613 438 assert(old_mountlist != NULL); … … 615 440 #ifdef __FreeBSD__ 616 441 log_to_screen 617 (_("I don't know how to extrapolate the mountlist on FreeBSD. Sorry.")); 442 (_ 443 ("I don't know how to extrapolate the mountlist on FreeBSD. Sorry.")); 618 444 return (1); 619 445 #endif … … 624 450 if (!does_file_exist("/etc/raidtab")) { 625 451 log_to_screen 626 (_("Cannot find /etc/raidtab - cannot extrapolate the fdisk entries")); 452 (_ 453 ("Cannot find /etc/raidtab - cannot extrapolate the fdisk entries")); 627 454 finish(1); 628 455 } … … 631 458 finish(1); 632 459 } 633 for ( fgets(incoming, MAX_STR_LEN - 1, fin); !feof(fin)460 for (getline(&incoming, &n, fin); !feof(fin) 634 461 && !strstr(incoming, old_mountlist->el[lino].device); 635 fgets(incoming, MAX_STR_LEN - 1, fin));462 getline(&incoming, &n, fin)); 636 463 if (!feof(fin)) { 637 sprintf(tmp, "Investigating %s",464 asprintf(&tmp, "Investigating %s", 638 465 old_mountlist->el[lino].device); 639 466 log_it(tmp); 640 for (fgets(incoming, MAX_STR_LEN - 1, fin); !feof(fin) 467 paranoid_free(tmp); 468 469 for (getline(&incoming, &n, fin); !feof(fin) 641 470 && !strstr(incoming, "raiddev"); 642 fgets(incoming, MAX_STR_LEN - 1, fin)) {471 getline(&incoming, &n, fin)) { 643 472 if (strstr(incoming, OSSWAP("device", "drive")) 644 473 && !strchr(incoming, '#')) { … … 647 476 *p = '\0'; 648 477 for (p--; p > incoming && *(p - 1) > 32; p--); 649 sprintf(tmp, "Extrapolating %s", p);478 asprintf(&tmp, "Extrapolating %s", p); 650 479 log_it(tmp); 480 paranoid_free(tmp); 481 651 482 for (j = 0; 652 483 j < new_mountlist->entries … … 666 497 new_mountlist->entries++; 667 498 } else { 668 sprintf(tmp, 669 "Not adding %s to mountlist: it's already there", 670 p); 499 asprintf(&tmp, 500 "Not adding %s to mountlist: it's already there", p); 671 501 log_it(tmp); 502 paranoid_free(tmp); 672 503 } 673 504 } 674 505 } 675 506 } 507 paranoid_free(incoming); 676 508 paranoid_fclose(fin); 677 509 } else { … … 687 519 } 688 520 } 689 paranoid_free(incoming);690 paranoid_free(tmp);691 521 692 522 return (0); … … 702 532 * @return 0 for success, nonzero for failure. 703 533 */ 704 int create_raid_device_via_mdadm(struct raidlist_itself *raidlist, char *device) 534 int create_raid_device_via_mdadm(struct raidlist_itself *raidlist, 535 char *device) 705 536 { 706 537 /** int **************************************************************/ 707 int i= 0;708 int j= 0;709 710 538 int i = 0; 539 int j = 0; 540 int res = 0; 541 711 542 /** buffers ***********************************************************/ 712 char *devices = NULL; 713 char *strtmp = NULL; 714 char *level = NULL; 715 char *program = NULL; 716 717 // leave straight away if raidlist is initial or has no entries 718 if (!raidlist || raidlist->entries == 0) { 719 log_msg(1, "No RAID arrays found."); 720 return 1; 721 } else { 722 log_msg(1, "%d RAID arrays found.", raidlist->entries); 723 } 724 // find raidlist entry for requested device 725 for (i = 0; i < raidlist->entries; i++) { 726 if (!strcmp(raidlist->el[i].raid_device, device)) break; 727 } 728 // check whether RAID device was found in raidlist 729 if (i == raidlist->entries) { 730 log_msg(1, "RAID device %s not found in list.", device); 731 return 1; 732 } 733 // create device list from normal disks followed by spare ones 734 asprintf(&devices, raidlist->el[i].data_disks.el[0].device); 735 for (j = 1; j < raidlist->el[i].data_disks.entries; j++) { 736 asprintf(&strtmp, "%s", devices); 737 paranoid_free(devices); 738 asprintf(&devices, "%s %s", strtmp, 739 raidlist->el[i].data_disks.el[j].device); 740 paranoid_free(strtmp); 741 } 742 for (j = 0; j < raidlist->el[i].spare_disks.entries; j++) { 743 asprintf(&strtmp, "%s", devices); 744 paranoid_free(devices); 745 asprintf(&devices, "%s %s", strtmp, 746 raidlist->el[i].spare_disks.el[j].device); 747 paranoid_free(strtmp); 748 } 749 // translate RAID level 750 if (raidlist->el[i].raid_level == -2) { 751 asprintf(&level, "multipath"); 752 } else if (raidlist->el[i].raid_level == -1) { 753 asprintf(&level, "linear"); 754 } else { 755 asprintf(&level, "raid%d", raidlist->el[i].raid_level); 756 } 757 // create RAID device: 758 // - RAID device, number of devices and devices mandatory 759 // - parity algorithm, chunk size and spare devices optional 760 // - faulty devices ignored 761 // - persistent superblock always used as this is recommended 762 asprintf(&program, 763 "mdadm --create --force --run --auto=yes %s --level=%s --raid-devices=%d", 764 raidlist->el[i].raid_device, level, 765 raidlist->el[i].data_disks.entries); 766 if (raidlist->el[i].parity != -1) { 767 asprintf(&strtmp, "%s", program); 768 paranoid_free(program); 769 switch(raidlist->el[i].parity) { 770 case 0: 771 asprintf(&program, "%s --parity=%s", strtmp, "la"); 772 break; 773 case 1: 774 asprintf(&program, "%s --parity=%s", strtmp, "ra"); 775 break; 776 case 2: 777 asprintf(&program, "%s --parity=%s", strtmp, "ls"); 778 break; 779 case 3: 780 asprintf(&program, "%s --parity=%s", strtmp, "rs"); 781 break; 782 default: 783 fatal_error("Unknown RAID parity algorithm."); 784 break; 785 } 786 paranoid_free(strtmp); 787 } 788 if (raidlist->el[i].chunk_size != -1) { 789 asprintf(&strtmp, "%s", program); 790 paranoid_free(program); 791 asprintf(&program, "%s --chunk=%d", strtmp, raidlist->el[i].chunk_size); 792 paranoid_free(strtmp); 793 } 794 if (raidlist->el[i].spare_disks.entries > 0) { 795 asprintf(&strtmp, "%s", program); 796 paranoid_free(program); 797 asprintf(&program, "%s --spare-devices=%d", strtmp, 798 raidlist->el[i].spare_disks.entries); 799 paranoid_free(strtmp); 800 } 801 asprintf(&strtmp, "%s", program); 802 paranoid_free(program); 803 asprintf(&program, "%s %s", strtmp, devices); 804 paranoid_free(strtmp); 805 res = run_program_and_log_output(program, 1); 806 // free memory 807 paranoid_free(devices); 808 paranoid_free(level); 809 paranoid_free(program); 810 // return to calling instance 811 return res; 543 char *devices = NULL; 544 char *strtmp = NULL; 545 char *level = NULL; 546 char *program = NULL; 547 548 // leave straight away if raidlist is initial or has no entries 549 if (!raidlist || raidlist->entries == 0) { 550 log_msg(1, "No RAID arrays found."); 551 return 1; 552 } else { 553 log_msg(1, "%d RAID arrays found.", raidlist->entries); 554 } 555 // find raidlist entry for requested device 556 for (i = 0; i < raidlist->entries; i++) { 557 if (!strcmp(raidlist->el[i].raid_device, device)) 558 break; 559 } 560 // check whether RAID device was found in raidlist 561 if (i == raidlist->entries) { 562 log_msg(1, "RAID device %s not found in list.", device); 563 return 1; 564 } 565 // create device list from normal disks followed by spare ones 566 asprintf(&devices, raidlist->el[i].data_disks.el[0].device); 567 for (j = 1; j < raidlist->el[i].data_disks.entries; j++) { 568 asprintf(&strtmp, "%s", devices); 569 paranoid_free(devices); 570 asprintf(&devices, "%s %s", strtmp, 571 raidlist->el[i].data_disks.el[j].device); 572 paranoid_free(strtmp); 573 } 574 for (j = 0; j < raidlist->el[i].spare_disks.entries; j++) { 575 asprintf(&strtmp, "%s", devices); 576 paranoid_free(devices); 577 asprintf(&devices, "%s %s", strtmp, 578 raidlist->el[i].spare_disks.el[j].device); 579 paranoid_free(strtmp); 580 } 581 // translate RAID level 582 if (raidlist->el[i].raid_level == -2) { 583 asprintf(&level, "multipath"); 584 } else if (raidlist->el[i].raid_level == -1) { 585 asprintf(&level, "linear"); 586 } else { 587 asprintf(&level, "raid%d", raidlist->el[i].raid_level); 588 } 589 // create RAID device: 590 // - RAID device, number of devices and devices mandatory 591 // - parity algorithm, chunk size and spare devices optional 592 // - faulty devices ignored 593 // - persistent superblock always used as this is recommended 594 asprintf(&program, 595 "mdadm --create --force --run --auto=yes %s --level=%s --raid-devices=%d", 596 raidlist->el[i].raid_device, level, 597 raidlist->el[i].data_disks.entries); 598 if (raidlist->el[i].parity != -1) { 599 asprintf(&strtmp, "%s", program); 600 paranoid_free(program); 601 switch (raidlist->el[i].parity) { 602 case 0: 603 asprintf(&program, "%s --parity=%s", strtmp, "la"); 604 break; 605 case 1: 606 asprintf(&program, "%s --parity=%s", strtmp, "ra"); 607 break; 608 case 2: 609 asprintf(&program, "%s --parity=%s", strtmp, "ls"); 610 break; 611 case 3: 612 asprintf(&program, "%s --parity=%s", strtmp, "rs"); 613 break; 614 default: 615 fatal_error("Unknown RAID parity algorithm."); 616 break; 617 } 618 paranoid_free(strtmp); 619 } 620 if (raidlist->el[i].chunk_size != -1) { 621 asprintf(&strtmp, "%s", program); 622 paranoid_free(program); 623 asprintf(&program, "%s --chunk=%d", strtmp, 624 raidlist->el[i].chunk_size); 625 paranoid_free(strtmp); 626 } 627 if (raidlist->el[i].spare_disks.entries > 0) { 628 asprintf(&strtmp, "%s", program); 629 paranoid_free(program); 630 asprintf(&program, "%s --spare-devices=%d", strtmp, 631 raidlist->el[i].spare_disks.entries); 632 paranoid_free(strtmp); 633 } 634 asprintf(&strtmp, "%s", program); 635 paranoid_free(program); 636 asprintf(&program, "%s %s", strtmp, devices); 637 paranoid_free(strtmp); 638 res = run_program_and_log_output(program, 1); 639 // free memory 640 paranoid_free(devices); 641 paranoid_free(level); 642 paranoid_free(program); 643 // return to calling instance 644 return res; 812 645 } 813 646 … … 823 656 * @return 0 for success, nonzero for failure. 824 657 */ 825 int format_device(char *device, char *format, struct raidlist_itself *raidlist) 658 int format_device(char *device, char *format, 659 struct raidlist_itself *raidlist) 826 660 { 827 661 /** int **************************************************************/ 828 int res ;662 int res = 0; 829 663 int retval = 0; 830 664 #ifdef __FreeBSD__ … … 833 667 834 668 /** buffers ***********************************************************/ 835 char *program; 836 char *tmp; 669 char *program = NULL; 670 char *tmp = NULL; 671 char *tmp1 = NULL; 672 char *line = NULL; 673 char *status = NULL; 674 FILE *pin; 675 FILE *fin; 676 size_t n = 0; 677 size_t n1 = 0; 837 678 838 679 /** end ****************************************************************/ 839 680 840 malloc_string(program);841 malloc_string(tmp);842 681 assert_string_is_neither_NULL_nor_zerolength(device); 843 682 assert(format != NULL); 844 683 845 684 if (strstr(format, "raid")) { // do not form RAID disks; do it to /dev/md* instead 846 sprintf(tmp, "Not formatting %s (it is a RAID disk)", device);685 asprintf(&tmp, "Not formatting %s (it is a RAID disk)", device); 847 686 log_it(tmp); 848 paranoid_free(program);849 687 paranoid_free(tmp); 850 688 return (0); … … 853 691 if (strcmp(format, "swap") == 0) { 854 692 log_it("Not formatting %s - it's swap", device); 855 paranoid_free(program);856 paranoid_free(tmp);857 693 return (0); 858 694 } 859 695 #endif 860 696 if (strlen(format) <= 2) { 861 sprintf(tmp,697 asprintf(&tmp, 862 698 "%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", 863 699 device, format); 864 700 log_it(tmp); 865 paranoid_free(program);866 701 paranoid_free(tmp); 867 702 return (0); 868 703 } 869 704 if (is_this_device_mounted(device)) { 870 sprintf(tmp, _("%s is mounted - cannot format it "), device);705 asprintf(&tmp, _("%s is mounted - cannot format it "), device); 871 706 log_to_screen(tmp); 872 paranoid_free(program);873 707 paranoid_free(tmp); 874 708 return (1); … … 880 714 if (!does_file_exist("/tmp/raidconf.txt")) { 881 715 log_to_screen 882 (_("/tmp/raidconf.txt does not exist. I therefore cannot start Vinum.")); 716 (_ 717 ("/tmp/raidconf.txt does not exist. I therefore cannot start Vinum.")); 883 718 } else { 884 719 int res; … … 888 723 if (res) { 889 724 log_to_screen 890 (_("`vinum create /tmp/raidconf.txt' returned errors. Please fix them and re-run mondorestore.")); 725 (_ 726 ("`vinum create /tmp/raidconf.txt' returned errors. Please fix them and re-run mondorestore.")); 891 727 finish(1); 892 728 } … … 896 732 897 733 if (vinum_started_yet) { 898 FILE *fin; 899 char line[MAX_STR_LEN]; 900 sprintf(tmp, 901 _("Initializing Vinum device %s (this may take a *long* time)"), 734 asprintf(&tmp, 735 _ 736 ("Initializing Vinum device %s (this may take a *long* time)"), 902 737 device); 903 738 log_to_screen(tmp); 739 paranoid_free(tmp); 740 904 741 /* format raid partition */ 905 // sprintf (program, "mkraid --really-force %s", device); --- disabled -- BB, 02/12/2003 906 sprintf(program, 742 asprintf(&program, 907 743 "for plex in `vinum lv -r %s | grep '^P' | tr '\t' ' ' | tr -s ' ' | cut -d' ' -f2`; do echo $plex; done > /tmp/plexes", 908 744 basename(device)); 909 745 system(program); 746 910 747 if (g_fprep) { 911 748 fprintf(g_fprep, "%s\n", program); 912 749 } 750 paranoid_free(program); 751 913 752 fin = fopen("/tmp/plexes", "r"); 914 while ( fgets(line, MAX_STR_LEN - 1, fin)) {753 while (getline(&line, &n, fin)) { 915 754 if (strchr(line, '\n')) 916 755 *(strchr(line, '\n')) = '\0'; // get rid of the \n on the end 917 756 918 sprintf(tmp, "Initializing plex: %s", line);757 asprintf(&tmp, "Initializing plex: %s", line); 919 758 open_evalcall_form(tmp); 920 sprintf(tmp, "vinum init %s", line); 759 paranoid_free(tmp); 760 761 asprintf(&tmp, "vinum init %s", line); 921 762 system(tmp); 763 paranoid_free(tmp); 764 922 765 while (1) { 923 sprintf(tmp,766 asprintf(&tmp, 924 767 "vinum lp -r %s | grep '^S' | head -1 | tr -s ' ' | cut -d: -f2 | cut -f1 | sed 's/^ //' | sed 's/I //' | sed 's/%%//'", 925 768 line); 926 FILE *pin = popen(tmp, "r"); 927 char status[MAX_STR_LEN / 4]; 928 fgets(status, MAX_STR_LEN / 4 - 1, pin); 769 pin = popen(tmp, "r"); 770 paranoid_free(tmp); 771 772 getline(&status, &n1, pin); 929 773 pclose(pin); 930 774 … … 934 778 update_evalcall_form(atoi(status)); 935 779 usleep(250000); 780 paranoid_free(status); 936 781 } 937 782 close_evalcall_form(); 938 783 } 784 paranoid_free(line); 939 785 fclose(fin); 940 786 unlink("/tmp/plexes"); … … 942 788 } 943 789 #else 944 sprintf(tmp, _("Initializing RAID device %s"), device);790 asprintf(&tmp, _("Initializing RAID device %s"), device); 945 791 log_to_screen(tmp); 792 paranoid_free(tmp); 946 793 947 794 // Shouldn't be necessary. 948 795 log_to_screen(_("Stopping %s"), device); 949 796 stop_raid_device(device); 950 sy stem("sync");797 sync(); 951 798 sleep(1); 799 /* BERLIOS: This code is wrong as program has not been initialized 952 800 if (g_fprep) { 953 801 fprintf(g_fprep, "%s\n", program); 954 802 } 803 */ 955 804 956 805 log_msg(1, "Making %s", device); 957 806 // use mkraid if it exists, otherwise use mdadm 958 if (run_program_and_log_output("which mkraid", FALSE)) { 959 res = create_raid_device_via_mdadm(raidlist, device); 960 log_msg(1, "Creating RAID device %s via mdadm returned %d", device, res); 807 if (run_program_and_log_output("which mkraid", FALSE)) { 808 res = create_raid_device_via_mdadm(raidlist, device); 809 log_msg(1, "Creating RAID device %s via mdadm returned %d", 810 device, res); 961 811 } else { 962 sprintf(program, "mkraid --really-force %s", device);812 asprintf(&program, "mkraid --really-force %s", device); 963 813 res = run_program_and_log_output(program, 1); 964 814 log_msg(1, "%s returned %d", program, res); 965 sy stem("sync");815 sync(); 966 816 sleep(3); 967 817 start_raid_device(device); … … 969 819 fprintf(g_fprep, "%s\n", program); 970 820 } 971 } 972 system("sync"); 821 paranoid_free(program); 822 } 823 sync(); 973 824 sleep(2); 974 // log_to_screen("Starting %s", device); 975 // sprintf(program, "raidstart %s", device); 976 // res = run_program_and_log_output(program, 1); 977 // log_msg(1, "%s returned %d", program, res); 978 // system("sync"); sleep(1); 979 #endif 980 system("sync"); 825 #endif 826 sync(); 981 827 sleep(1); 982 828 newtResume(); … … 987 833 if (!strcmp(format, "lvm")) { 988 834 log_msg(1, "Don't format %s - it's part of an lvm volume", device); 989 paranoid_free(program);990 paranoid_free(tmp);991 835 return (0); 992 836 } 837 /* This function allocates program */ 993 838 res = which_format_command_do_i_need(format, program); 994 sprintf(tmp, "%s %s", program, device);995 839 if (strstr(program, "kludge")) { 996 strcat(tmp, " /"); 997 } 998 sprintf(program, "sh -c 'echo -en \"y\\ny\\ny\\n\" | %s'", tmp); 999 sprintf(tmp, "Formatting %s as %s", device, format); 840 asprintf(&tmp, "%s %s /", program, device); 841 } else { 842 asprintf(&tmp, "%s %s", program, device); 843 } 844 paranoid_free(program); 845 846 asprintf(&program, "sh -c 'echo -en \"y\\ny\\ny\\n\" | %s'", tmp); 847 paranoid_free(tmp); 848 849 asprintf(&tmp, "Formatting %s as %s", device, format); 1000 850 update_progress_form(tmp); 851 1001 852 res = run_program_and_log_output(program, FALSE); 1002 853 if (res && strstr(program, "kludge")) { 1003 sprintf(tmp, "Kludge failed; using regular mkfs.%s to format %s",1004 format, device);1005 854 #ifdef __FreeBSD__ 1006 sprintf(program, "newfs_msdos -F 32 %s", device); 855 paranoid_free(program); 856 asprintf(&program, "newfs_msdos -F 32 %s", device); 1007 857 #else 1008 858 #ifdef __IA64__ 1009 859 /* For EFI partitions take fat16 1010 860 * as we want to make small ones */ 1011 sprintf(program, "mkfs -t %s -F 16 %s", format, device); 861 paranoid_free(program); 862 asprintf(&program, "mkfs -t %s -F 16 %s", format, device); 1012 863 #else 1013 sprintf(program, "mkfs -t %s -F 32 %s", format, device); 864 paranoid_free(program); 865 asprintf(&program, "mkfs -t %s -F 32 %s", format, device); 1014 866 #endif 1015 867 #endif … … 1019 871 } 1020 872 } 873 paranoid_free(program); 874 1021 875 retval += res; 1022 876 if (retval) { 1023 strcat(tmp, _("...failed"));877 asprintf(&tmp1, "%s%s",tmp, _("...failed")); 1024 878 } else { 1025 strcat(tmp, _("...OK")); 1026 } 1027 1028 log_to_screen(tmp); 1029 paranoid_free(program); 879 asprintf(&tmp1, "%s%s",tmp, _("...OK")); 880 } 1030 881 paranoid_free(tmp); 1031 system("sync"); 882 883 log_to_screen(tmp1); 884 paranoid_free(tmp1); 885 sync(); 1032 886 sleep(1); 1033 887 return (retval); 1034 888 } 1035 1036 1037 1038 889 1039 890 … … 1044 895 * @return The number of errors encountered (0 for success). 1045 896 */ 1046 int format_everything(struct mountlist_itself *mountlist, bool interactively,1047 897 int format_everything(struct mountlist_itself *mountlist, 898 bool interactively, struct raidlist_itself *raidlist) 1048 899 { 1049 900 /** int **************************************************************/ … … 1068 919 1069 920 assert(mountlist != NULL); 1070 malloc_string(tmp); 1071 sprintf(tmp, "format_everything (mountlist, interactively = %s", 921 asprintf(&tmp, "format_everything (mountlist, interactively = %s", 1072 922 (interactively) ? "true" : "false"); 1073 923 log_it(tmp); 924 paranoid_free(tmp); 925 1074 926 mvaddstr_and_log_it(g_currentY, 0, _("Formatting partitions ")); 1075 927 open_progress_form(_("Formatting partitions"), … … 1085 937 log_msg(1, "Stopping all RAID devices"); 1086 938 stop_all_raid_devices(mountlist); 1087 sy stem("sync");1088 sy stem("sync");1089 sy stem("sync");939 sync(); 940 sync(); 941 sync(); 1090 942 sleep(2); 1091 943 log_msg(1, "Prepare soft-RAIDs"); // prep and format too … … 1096 948 if (interactively) { 1097 949 // ask user if we should format the current device 1098 sprintf(tmp, "Shall I format %s (%s) ?", me->device,950 asprintf(&tmp, "Shall I format %s (%s) ?", me->device, 1099 951 me->mountpoint); 1100 952 do_it = ask_me_yes_or_no(tmp); 953 paranoid_free(tmp); 1101 954 } else { 1102 955 do_it = TRUE; … … 1109 962 } 1110 963 } 1111 sy stem("sync");1112 sy stem("sync");1113 sy stem("sync");964 sync(); 965 sync(); 966 sync(); 1114 967 sleep(2); 1115 968 // This last step is probably necessary … … 1126 979 newtSuspend(); 1127 980 } 1128 /*1129 for(i=0; i<3; i++)1130 {1131 res = do_my_funky_lvm_stuff(FALSE, FALSE);1132 if (!res) { break; }1133 sleep(3);1134 res = do_my_funky_lvm_stuff(TRUE, FALSE);1135 sleep(3);1136 }1137 if (res) {1138 log_msg(1, "Vacuum-packing...");1139 */1140 981 res = do_my_funky_lvm_stuff(FALSE, TRUE); 1141 /*1142 }1143 */1144 982 if (!g_text_mode) { 1145 983 newtResume(); … … 1150 988 log_to_screen("Failed to initialize LVM"); 1151 989 } 1152 // retval += res;1153 990 if (res) { 1154 991 retval++; … … 1162 999 me = &mountlist->el[lino]; // the current mountlist entry 1163 1000 if (!strcmp(me->mountpoint, "image")) { 1164 sprintf(tmp, "Not formatting %s - it's an image", me->device);1001 asprintf(&tmp, "Not formatting %s - it's an image", me->device); 1165 1002 log_it(tmp); 1003 paranoid_free(tmp); 1166 1004 } else if (!strcmp(me->format, "raid")) { 1167 sprintf(tmp, "Not formatting %s - it's a raid-let",1005 asprintf(&tmp, "Not formatting %s - it's a raid-let", 1168 1006 me->device); 1169 1007 log_it(tmp); 1008 paranoid_free(tmp); 1170 1009 continue; 1171 1010 } else if (!strcmp(me->format, "lvm")) { 1172 sprintf(tmp, "Not formatting %s - it's an LVM", me->device);1011 asprintf(&tmp, "Not formatting %s - it's an LVM", me->device); 1173 1012 log_it(tmp); 1013 paranoid_free(tmp); 1174 1014 continue; 1175 1015 } else if (!strncmp(me->device, "/dev/md", 7)) { 1176 sprintf(tmp, "Already formatted %s - it's a soft-RAID dev",1016 asprintf(&tmp, "Already formatted %s - it's a soft-RAID dev", 1177 1017 me->device); 1178 1018 log_it(tmp); 1019 paranoid_free(tmp); 1179 1020 continue; 1180 1021 } else if (!does_file_exist(me->device) 1181 1022 && strncmp(me->device, "/dev/hd", 7) 1182 1023 && strncmp(me->device, "/dev/sd", 7)) { 1183 sprintf(tmp,1024 asprintf(&tmp, 1184 1025 "Not formatting %s yet - doesn't exist - probably an LVM", 1185 1026 me->device); 1186 1027 log_it(tmp); 1028 paranoid_free(tmp); 1187 1029 continue; 1188 1030 } else { 1189 1031 if (interactively) { 1190 1032 // ask user if we should format the current device 1191 sprintf(tmp, "Shall I format %s (%s) ?", me->device,1033 asprintf(&tmp, "Shall I format %s (%s) ?", me->device, 1192 1034 me->mountpoint); 1193 1035 do_it = ask_me_yes_or_no(tmp); 1036 paranoid_free(tmp); 1194 1037 } else { 1195 1038 do_it = TRUE; … … 1215 1058 mvaddstr_and_log_it(g_currentY++, 74, _("Failed.")); 1216 1059 log_to_screen 1217 (_("Errors occurred during the formatting of your hard drives.")); 1060 (_ 1061 ("Errors occurred during the formatting of your hard drives.")); 1218 1062 } else { 1219 1063 mvaddstr_and_log_it(g_currentY++, 74, _("Done.")); 1220 1064 } 1221 1065 1222 sprintf(tmp, "format_everything () - %s",1066 asprintf(&tmp, "format_everything () - %s", 1223 1067 (retval) ? "failed!" : "finished successfully"); 1224 1068 log_it(tmp); 1069 paranoid_free(tmp); 1225 1070 1226 1071 if (g_partition_table_locked_up > 0) { … … 1228 1073 //123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 1229 1074 log_to_screen 1230 (_("Partition table locked up %d times. At least one 'mkfs' (format) command"), 1075 (_ 1076 ("Partition table locked up %d times. At least one 'mkfs' (format) command"), 1231 1077 g_partition_table_locked_up); 1232 log_to_screen 1233 (_("failed. I think these two events are related. Sometimes, fdisk's ioctl() call"));1234 log_to_screen 1235 (_("to refresh its copy of the partition table causes the kernel to lock the "));1236 log_to_screen 1237 (_("partition table. I believe this has just happened."));1078 log_to_screen(_ 1079 ("failed. I think these two events are related. Sometimes, fdisk's ioctl() call")); 1080 log_to_screen(_ 1081 ("to refresh its copy of the partition table causes the kernel to lock the ")); 1082 log_to_screen(_ 1083 ("partition table. I believe this has just happened.")); 1238 1084 if (ask_me_yes_or_no 1239 (_("Please choose 'yes' to reboot and try again; or 'no' to ignore this warning and continue."))) 1085 (_ 1086 ("Please choose 'yes' to reboot and try again; or 'no' to ignore this warning and continue."))) 1240 1087 { 1241 sy stem("sync");1242 sy stem("sync");1243 sy stem("sync");1088 sync(); 1089 sync(); 1090 sync(); 1244 1091 system("reboot"); 1245 1092 } 1246 1093 } else { 1247 1094 log_to_screen 1248 (_("Partition table locked up %d time%c. However, disk formatting succeeded."), 1095 (_ 1096 ("Partition table locked up %d time%c. However, disk formatting succeeded."), 1249 1097 g_partition_table_locked_up, 1250 1098 (g_partition_table_locked_up == 1) ? '.' : 's'); … … 1254 1102 system("clear"); 1255 1103 newtResume(); 1256 paranoid_free(tmp);1257 1104 return (retval); 1258 1105 } … … 1281 1128 /** end **************************************************************/ 1282 1129 1283 malloc_string(tmp);1284 1130 assert_string_is_neither_NULL_nor_zerolength(drivename); 1285 1131 1286 1132 if (devno_we_must_allow_for >= 5) { 1287 sprintf(tmp, "Making dummy primary %s%d", drivename, 1);1133 asprintf(&tmp, "Making dummy primary %s%d", drivename, 1); 1288 1134 log_it(tmp); 1135 paranoid_free(tmp); 1136 1289 1137 g_maximum_progress++; 1290 1138 res = … … 1299 1147 } 1300 1148 for (; current_devno < devno_we_must_allow_for; current_devno++) { 1301 sprintf(tmp, "Creating dummy partition %s%d", drivename,1149 asprintf(&tmp, "Creating dummy partition %s%d", drivename, 1302 1150 current_devno); 1303 1151 log_it(tmp); 1152 paranoid_free(tmp); 1153 1304 1154 g_maximum_progress++; 1305 1155 res = … … 1309 1159 previous_devno = current_devno; 1310 1160 } 1311 paranoid_free(tmp);1312 1161 return (previous_devno); 1313 1162 } … … 1340 1189 } 1341 1190 } 1191 1342 1192 1343 1193 /* The following 2 functions are stolen from /usr/src/sbin/disklabel/disklabel.c */ … … 1442 1292 static struct disklabel loclab; 1443 1293 struct partition *dp; 1444 char lnamebuf[BBSIZE];1294 char *lnamebuf; 1445 1295 int f; 1446 1296 u_int secsize, u; 1447 1297 off_t mediasize; 1448 1298 1449 (void) snprintf(lnamebuf, BBSIZE, "%s", dkname);1299 asprintf(&lnamebuf, "%s", dkname); 1450 1300 if ((f = open(lnamebuf, O_RDONLY)) == -1) { 1451 1301 warn("cannot open %s", lnamebuf); 1302 paranoid_free(lnamebuf); 1452 1303 return (NULL); 1453 1304 } 1305 paranoid_free(lnamebuf); 1454 1306 1455 1307 /* New world order */ … … 1522 1374 char *drivename, struct disklabel *ret) 1523 1375 { 1524 char subdev_str[MAX_STR_LEN];1525 char command[MAX_STR_LEN];1376 char *subdev_str; 1377 char *command; 1526 1378 struct disklabel *lp; 1527 1379 int i, lo = 0; … … 1533 1385 for (c = 'a'; c <= 'z'; ++c) { 1534 1386 int idx; 1535 sprintf(subdev_str, "%s%c", drivename, c);1387 asprintf(&subdev_str, "%s%c", drivename, c); 1536 1388 if ((idx = find_device_in_mountlist(mountlist, subdev_str)) < 0) { 1537 1389 lp->d_partitions[c - 'a'].p_size = 0; … … 1558 1410 lp->d_partitions[c - 'a'].p_fstype = FS_OTHER; 1559 1411 } 1412 paranoid_free(subdev_str); 1560 1413 } 1561 1414 … … 1588 1441 display_disklabel(ftmp, lp); 1589 1442 fclose(ftmp); 1590 sprintf(command, "disklabel -wr %s auto", canonical_name(drivename));1443 asprintf(&command, "disklabel -wr %s auto", canonical_name(drivename)); 1591 1444 retval += run_program_and_log_output(command, TRUE); 1592 sprintf(command, "disklabel -R %s /tmp/disklabel", 1445 paranoid_free(command); 1446 1447 asprintf(&command, "disklabel -R %s /tmp/disklabel", 1593 1448 canonical_name(drivename)); 1594 1449 retval += run_program_and_log_output(command, TRUE); 1450 paranoid_free(command); 1595 1451 if (ret) 1596 1452 *ret = *lp; … … 1609 1465 { 1610 1466 /** int *************************************************************/ 1611 int current_devno ;1467 int current_devno = 0; 1612 1468 int previous_devno = 0; 1613 int lino ;1469 int lino = 0; 1614 1470 int retval = 0; 1615 int i ;1471 int i = 0; 1616 1472 FILE *pout_to_fdisk = NULL; 1617 1473 1618 1474 #ifdef __FreeBSD__ 1619 1475 bool fbsd_part = FALSE; 1620 char subdev_str[MAX_STR_LEN];1476 char *subdev_str = NULL; 1621 1477 #endif 1622 1478 … … 1625 1481 1626 1482 /** buffers *********************************************************/ 1627 char *device_str; 1628 char *format; 1629 char *tmp; 1483 char *device_str = NULL; 1484 char *format = NULL; 1485 char *tmp = NULL; 1486 char *tmp1 = NULL; 1630 1487 1631 1488 /** end *************************************************************/ … … 1634 1491 assert_string_is_neither_NULL_nor_zerolength(drivename); 1635 1492 1636 malloc_string(device_str); 1637 malloc_string(format); 1638 malloc_string(tmp); 1639 1640 sprintf(tmp, "Partitioning drive %s", drivename); 1493 asprintf(&tmp, "Partitioning drive %s", drivename); 1641 1494 log_it(tmp); 1495 paranoid_free(tmp); 1642 1496 1643 1497 #if __FreeBSD__ … … 1646 1500 #else 1647 1501 make_hole_for_file(FDISK_LOG); 1648 sprintf(tmp, "parted2fdisk %s >> %s 2>> %s", drivename, FDISK_LOG, FDISK_LOG); 1502 asprintf(&tmp, "parted2fdisk %s >> %s 2>> %s", drivename, FDISK_LOG, 1503 FDISK_LOG); 1649 1504 pout_to_fdisk = popen(tmp, "w"); 1505 paranoid_free(tmp); 1506 1650 1507 if (!pout_to_fdisk) { 1651 log_to_screen(_("Cannot call parted2fdisk to configure %s"), drivename); 1652 paranoid_free(device_str); 1653 paranoid_free(format); 1654 paranoid_free(tmp); 1508 log_to_screen(_("Cannot call parted2fdisk to configure %s"), 1509 drivename); 1655 1510 return (1); 1656 1511 } 1657 1512 #endif 1658 1513 for (current_devno = 1; current_devno < 99; current_devno++) { 1659 build_partition_name(device_str,drivename, current_devno);1514 device_str = build_partition_name(drivename, current_devno); 1660 1515 lino = find_device_in_mountlist(mountlist, device_str); 1661 1516 … … 1670 1525 // try DangerouslyDedicated mode 1671 1526 for (c = 'a'; c <= 'z'; c++) { 1672 sprintf(subdev_str, "%s%c", drivename, c); 1673 if (find_device_in_mountlist(mountlist, subdev_str) > 1674 0) { 1527 asprintf(&subdev_str, "%s%c", drivename, c); 1528 if (find_device_in_mountlist(mountlist, subdev_str) > 0) { 1675 1529 fbsd_part = TRUE; 1676 1530 } 1531 paranoid_free(subdev_str); 1677 1532 } 1678 1533 if (fbsd_part) { … … 1685 1540 if (system(command)) { 1686 1541 log_to_screen 1687 (_("Warning! Unable to make the drive bootable.")); 1542 (_ 1543 ("Warning! Unable to make the drive bootable.")); 1688 1544 } 1689 1545 paranoid_free(device_str); 1690 paranoid_free(format); 1691 paranoid_free(tmp); 1546 1692 1547 return r; 1693 1548 } 1694 1549 } 1695 1550 for (c = 'a'; c <= 'z'; c++) { 1696 sprintf(subdev_str, "%s%c", device_str, c);1551 asprintf(&subdev_str, "%s%c", device_str, c); 1697 1552 if (find_device_in_mountlist(mountlist, subdev_str) > 0) { 1698 1553 fbsd_part = TRUE; 1699 1554 } 1555 paranoid_free(subdev_str); 1700 1556 } 1701 1557 // Now we check the subpartitions of the current partition. … … 1703 1559 int i, line; 1704 1560 1705 strcpy(format, "ufs");1561 asprintf(&format, "ufs"); 1706 1562 partsize = 0; 1707 1563 for (i = 'a'; i < 'z'; ++i) { 1708 sprintf(subdev_str, "%s%c", device_str, i);1564 asprintf(&subdev_str, "%s%c", device_str, i); 1709 1565 line = find_device_in_mountlist(mountlist, subdev_str); 1566 paranoid_free(subdev_str); 1567 1710 1568 if (line > 0) { 1711 1569 // We found one! Add its size to the total size. … … 1730 1588 #if __FreeBSD__ 1731 1589 // FreeBSD doesn't let you write to blk devices in <512byte chunks. 1732 // sprintf(tmp, "dd if=/dev/zero of=%s count=1 bs=512", drivename);1733 // if (run_program_and_log_output(tmp, TRUE)) {1734 1590 file = open(drivename, O_WRONLY); 1735 1591 if (!file) { 1736 sprintf(tmp,1592 asprintf(&tmp, 1737 1593 _("Warning - unable to open %s for wiping it's partition table"), 1738 1594 drivename); 1739 1595 log_to_screen(tmp); 1596 paranoid_free(tmp); 1740 1597 } 1741 1598 1742 1599 for (i = 0; i < 512; i++) { 1743 1600 if (!write(file, "\0", 1)) { 1744 sprintf(tmp, _("Warning - unable to write to %s"),1601 asprintf(&tmp, _("Warning - unable to write to %s"), 1745 1602 drivename); 1746 1603 log_to_screen(tmp); 1604 paranoid_free(tmp); 1747 1605 } 1748 1606 } 1749 sy stem("sync");1607 sync(); 1750 1608 #else 1751 1609 iamhere("New, kernel-friendly partition remover"); … … 1754 1612 fflush(pout_to_fdisk); 1755 1613 } 1756 // sprintf(tmp, "dd if=/dev/zero of=%s count=1 bs=512", drivename);1757 // run_program_and_log_output(tmp, 1);1758 1614 #endif 1759 1615 if (current_devno > 1) { … … 1767 1623 #endif 1768 1624 1769 strcpy(format, mountlist->el[lino].format);1625 asprintf(&format, mountlist->el[lino].format); 1770 1626 partsize = mountlist->el[lino].size; 1771 1627 … … 1776 1632 if (current_devno == 5 && previous_devno == 4) { 1777 1633 log_to_screen 1778 (_("You must leave at least one partition spare as the Extended partition.")); 1634 (_ 1635 ("You must leave at least one partition spare as the Extended partition.")); 1779 1636 paranoid_free(device_str); 1780 1637 paranoid_free(format); 1781 paranoid_free(tmp); 1638 1782 1639 return (1); 1783 1640 } … … 1789 1646 #ifdef __FreeBSD__ 1790 1647 if ((current_devno <= 4) && fbsd_part) { 1791 sprintf(tmp, "disklabel -B %s", basename(device_str));1648 asprintf(&tmp, "disklabel -B %s", basename(device_str)); 1792 1649 retval += label_drive_or_slice(mountlist, device_str, 0); 1793 1650 if (system(tmp)) { … … 1795 1652 (_("Warning! Unable to make the slice bootable.")); 1796 1653 } 1654 paranoid_free(tmp); 1797 1655 } 1798 1656 #endif … … 1800 1658 previous_devno = current_devno; 1801 1659 } 1660 paranoid_free(device_str); 1661 paranoid_free(format); 1802 1662 1803 1663 if (pout_to_fdisk) { 1804 // mark relevant partition as bootable 1805 sprintf(tmp, "a\n%s\n", 1806 call_program_and_get_last_line_of_output 1807 ("make-me-bootable /tmp/mountlist.txt dummy")); 1664 // mark relevant partition as bootable 1665 tmp1 = call_program_and_get_last_line_of_output 1666 ("make-me-bootable /tmp/mountlist.txt dummy"); 1667 asprintf(&tmp, "a\n%s\n", tmp1); 1668 paranoid_free(tmp1); 1669 1808 1670 fput_string_one_char_at_a_time(pout_to_fdisk, tmp); 1809 // close fdisk 1671 paranoid_free(tmp); 1672 1673 // close fdisk 1810 1674 fput_string_one_char_at_a_time(pout_to_fdisk, "w\n"); 1811 sy stem("sync");1675 sync(); 1812 1676 paranoid_pclose(pout_to_fdisk); 1813 1677 log_msg(0, 1814 1678 "------------------- fdisk.log looks like this ------------------"); 1815 sprintf(tmp, "cat %s >> %s", FDISK_LOG, MONDO_LOGFILE);1679 asprintf(&tmp, "cat %s >> %s", FDISK_LOG, MONDO_LOGFILE); 1816 1680 system(tmp); 1681 paranoid_free(tmp); 1682 1817 1683 log_msg(0, 1818 1684 "------------------- end of fdisk.log... word! ------------------"); 1819 sprintf(tmp, "tail -n6 %s | grep -F \"16: \"", FDISK_LOG);1685 asprintf(&tmp, "tail -n6 %s | grep -F \"16: \"", FDISK_LOG); 1820 1686 if (!run_program_and_log_output(tmp, 5)) { 1821 1687 g_partition_table_locked_up++; 1822 1688 log_to_screen 1823 (_("A flaw in the Linux kernel has locked the partition table.")); 1824 } 1825 } 1826 paranoid_free(device_str); 1827 paranoid_free(format); 1828 paranoid_free(tmp); 1689 (_ 1690 ("A flaw in the Linux kernel has locked the partition table.")); 1691 } 1692 paranoid_free(tmp); 1693 } 1829 1694 return (retval); 1830 1695 } 1696 1831 1697 1832 1698 /** … … 1851 1717 char *partition_name; 1852 1718 char *tmp; 1853 char *logfile;1854 1719 char *output; 1855 1720 … … 1861 1726 /** end ***************************************************************/ 1862 1727 1863 malloc_string(program);1864 malloc_string(partition_name);1865 malloc_string(tmp);1866 malloc_string(logfile);1867 malloc_string(output);1868 1869 1728 assert_string_is_neither_NULL_nor_zerolength(drive); 1870 1729 assert(format != NULL); … … 1874 1733 1875 1734 if (!strncmp(drive, RAID_DEVICE_STUB, strlen(RAID_DEVICE_STUB))) { 1876 sprintf(tmp, "Not partitioning %s - it is a virtual drive", drive);1735 asprintf(&tmp, "Not partitioning %s - it is a virtual drive", drive); 1877 1736 log_it(tmp); 1878 paranoid_free(program);1879 paranoid_free(partition_name);1880 1737 paranoid_free(tmp); 1881 paranoid_free(logfile);1882 paranoid_free(output);1883 1738 return (0); 1884 1739 } 1885 build_partition_name(partition_name,drive, partno);1740 partition_name = build_partition_name(drive, partno); 1886 1741 if (partsize <= 0) { 1887 sprintf(tmp, "Partitioning device %s (max size)", partition_name);1742 asprintf(&tmp, "Partitioning device %s (max size)", partition_name); 1888 1743 } else { 1889 sprintf(tmp, "Partitioning device %s (%lld MB)", partition_name,1744 asprintf(&tmp, "Partitioning device %s (%lld MB)", partition_name, 1890 1745 (long long) partsize / 1024); 1891 1746 } 1892 1747 update_progress_form(tmp); 1893 1748 log_it(tmp); 1749 paranoid_free(tmp); 1894 1750 1895 1751 if (is_this_device_mounted(partition_name)) { 1896 sprintf(tmp, _("%s is mounted, and should not be partitioned"),1752 asprintf(&tmp, _("%s is mounted, and should not be partitioned"), 1897 1753 partition_name); 1754 paranoid_free(partition_name); 1755 1898 1756 log_to_screen(tmp); 1899 paranoid_free(program);1900 paranoid_free(partition_name);1901 1757 paranoid_free(tmp); 1902 paranoid_free(logfile);1903 paranoid_free(output);1904 1758 return (1); 1905 /* 1906 } else if (does_partition_exist(drive, partno)) { 1907 sprintf(tmp, "%s already has a partition", partition_name); 1908 log_to_screen(tmp); 1909 return (1); 1910 */ 1911 } 1912 1913 1914 /* sprintf(tmp,"Partitioning %s ",partition_name); */ 1915 /* mvaddstr_and_log_it(g_currentY+1,30,tmp); */ 1759 } 1760 1916 1761 p = (char *) strrchr(partition_name, '/'); 1917 sprintf(logfile, "/tmp/fdisk.log.%s", ++p); 1918 sprintf(program, "parted2fdisk %s >> %s 2>> %s", drive, MONDO_LOGFILE, 1762 asprintf(&program, "parted2fdisk %s >> %s 2>> %s", drive, MONDO_LOGFILE, 1919 1763 MONDO_LOGFILE); 1920 1764 1921 /* BERLIOS: sho ould not be called each time */1765 /* BERLIOS: should not be called each time */ 1922 1766 part_table_fmt = which_partition_format(drive); 1923 output[0] = '\0';1924 1767 /* make it a primary/extended/logical */ 1925 1768 if (partno <= 4) { 1926 sprintf(output + strlen(output),"n\np\n%d\n", partno);1769 asprintf(&output,"n\np\n%d\n", partno); 1927 1770 } else { 1928 1771 /* MBR needs an extended partition if more than 4 partitions */ … … 1931 1774 if (prev_partno >= 4) { 1932 1775 log_to_screen 1933 (_("You need to leave at least one partition free, for 'extended/logical'")); 1776 (_ 1777 ("You need to leave at least one partition free, for 'extended/logical'")); 1778 paranoid_free(partition_name); 1934 1779 paranoid_free(program); 1935 paranoid_free(partition_name); 1936 paranoid_free(tmp); 1937 paranoid_free(logfile); 1780 1938 1781 paranoid_free(output); 1939 1782 return (1); 1940 1783 } else { 1941 sprintf(output + strlen(output), "n\ne\n%d\n\n\n", 1942 prev_partno + 1); 1784 asprintf(&output,"n\ne\n%d\n\n\n",prev_partno + 1); 1943 1785 } 1944 1786 } 1945 strcat(output + strlen(output), "n\nl\n"); 1787 asprintf(&tmp,"%sn\nl\n",output); 1788 paranoid_free(output); 1789 output = tmp; 1946 1790 } else { 1947 1791 /* GPT allows more than 4 primary partitions */ 1948 sprintf(output + strlen(output), "n\np\n%d\n",partno);1792 asprintf(&output,"n\np\n%d\n",partno); 1949 1793 } 1950 1794 } 1951 1795 paranoid_free(part_table_fmt); 1952 strcat(output + strlen(output), "\n"); /*start block (ENTER for next free blk */ 1796 /*start block (ENTER for next free blk */ 1797 asprintf(&tmp,"%s\n",output); 1798 paranoid_free(output); 1799 output = tmp; 1800 1953 1801 if (partsize > 0) { 1954 1802 if (!strcmp(format, "7")) { … … 1956 1804 partsize += 512; 1957 1805 } 1958 sprintf(output + strlen(output), "+%lldK", (long long) (partsize)); 1959 } 1960 strcat(output + strlen(output), "\n"); 1806 asprintf(&tmp,"%s+%lldK", output, (long long) (partsize)); 1807 paranoid_free(output); 1808 output = tmp; 1809 } 1810 asprintf(&tmp,"%s\n",output); 1811 paranoid_free(output); 1812 output = tmp; 1961 1813 #if 0 1962 1814 /* 1963 1815 #endif 1964 sprintf(tmp,"PARTSIZE = +%ld",(long)partsize);1816 asprintf(&tmp,"PARTSIZE = +%ld",(long)partsize); 1965 1817 log_it(tmp); 1818 paranoid_free(tmp); 1819 1966 1820 log_it("---fdisk command---"); 1967 1821 log_it(output); … … 1977 1831 fput_string_one_char_at_a_time(pout_to_fdisk, output); 1978 1832 fput_string_one_char_at_a_time(pout_to_fdisk, "\n\np\n"); 1979 strcpy(tmp, last_line_of_file(FDISK_LOG));1833 tmp = last_line_of_file(FDISK_LOG); 1980 1834 if (strstr(tmp, " (m ")) { 1981 1835 log_msg(1, "Successfully created %s%d", drive, partno); … … 1985 1839 partno); 1986 1840 } 1841 paranoid_free(tmp); 1842 1987 1843 if (!retval) { 1988 1844 log_msg(1, "Trying to set %s%d's partition type now", drive, … … 2002 1858 } 2003 1859 } else { 2004 strcat(output, "w\n\n"); 1860 asprintf(&tmp,"%sw\n\n",output); 1861 paranoid_free(output); 1862 output = tmp; 1863 2005 1864 if (g_fprep) { 2006 1865 fprintf(g_fprep, "echo \"%s\" | %s\n", output, program); … … 2020 1879 format, -1); 2021 1880 if (res) { 2022 sprintf(tmp, "Failed to vacuum-pack %s", partition_name);1881 asprintf(&tmp, "Failed to vacuum-pack %s", partition_name); 2023 1882 log_it(tmp); 1883 paranoid_free(tmp); 1884 2024 1885 retval++; 2025 1886 } else { … … 2032 1893 partsize); 2033 1894 if (retval) { 2034 sprintf(tmp, "Partitioned %s but failed to set its type",1895 asprintf(&tmp, "Partitioned %s but failed to set its type", 2035 1896 partition_name); 2036 1897 log_it(tmp); 1898 paranoid_free(tmp); 2037 1899 } else { 2038 1900 if (partsize > 0) { 2039 sprintf(tmp, "Partition %s created+configured OK",1901 asprintf(&tmp, "Partition %s created+configured OK", 2040 1902 partition_name); 2041 1903 log_to_screen(tmp); 1904 paranoid_free(tmp); 2042 1905 } else { 2043 1906 log_it("Returning from a successful vacuum-pack"); … … 2045 1908 } 2046 1909 } else { 2047 sprintf(tmp, "Failed to partition %s", partition_name);1910 asprintf(&tmp, "Failed to partition %s", partition_name); 2048 1911 if (partsize > 0) { 2049 1912 log_to_screen(tmp); … … 2051 1914 log_it(tmp); 2052 1915 } 1916 paranoid_free(tmp); 2053 1917 retval++; 2054 1918 } … … 2056 1920 g_current_progress++; 2057 1921 log_it("partition_device() --- leaving"); 1922 paranoid_free(partition_name); 2058 1923 paranoid_free(program); 2059 paranoid_free(partition_name);2060 paranoid_free(tmp);2061 paranoid_free(logfile);2062 1924 paranoid_free(output); 2063 1925 return (retval); 2064 1926 } 2065 2066 1927 2067 1928 … … 2124 1985 open_progress_form(_("Partitioning devices"), 2125 1986 _("I am now going to partition all your drives."), 2126 _("This should not take more than five minutes."), "",2127 mountlist->entries);1987 _("This should not take more than five minutes."), 1988 "", mountlist->entries); 2128 1989 2129 1990 make_list_of_drives_in_mountlist(mountlist, drivelist); … … 2138 1999 mvaddstr_and_log_it(g_currentY++, 74, _("Failed.")); 2139 2000 log_to_screen 2140 (_("Errors occurred during the partitioning of your hard drives.")); 2001 (_ 2002 ("Errors occurred during the partitioning of your hard drives.")); 2141 2003 } else { 2142 2004 mvaddstr_and_log_it(g_currentY++, 74, _("Done.")); … … 2168 2030 { 2169 2031 /** buffers *********************************************************/ 2170 char *partition ;2171 char *command ;2172 char *output ;2173 char *tmp ;2174 char * partcode;2175 char * logfile;2032 char *partition = NULL; 2033 char *command = NULL; 2034 char *output = NULL; 2035 char *tmp = NULL; 2036 char *tmp1 = NULL; 2037 char *partcode = NULL; 2176 2038 2177 2039 /** pointers *********************************************************/ 2178 char *p ;2179 FILE *fout ;2040 char *p = NULL; 2041 FILE *fout = NULL; 2180 2042 2181 2043 /** int **************************************************************/ … … 2187 2049 assert(format != NULL); 2188 2050 2189 malloc_string(partition); 2190 malloc_string(command); 2191 malloc_string(output); 2192 malloc_string(tmp); 2193 malloc_string(partcode); 2194 malloc_string(logfile); 2195 2196 build_partition_name(partition, drive, partno); 2051 partition = build_partition_name(drive, partno); 2197 2052 p = (char *) strrchr(partition, '/'); 2198 sprintf(logfile, "/tmp/fdisk-set-type.%s.log", ++p);2199 2053 if (strcmp(format, "swap") == 0) { 2200 strcpy(partcode, "82");2054 asprintf(&partcode, "82"); 2201 2055 } else if (strcmp(format, "vfat") == 0) { 2202 2056 if (partsize / 1024 > 8192) { 2203 strcpy(partcode, "c");2057 asprintf(&partcode, "c"); 2204 2058 } else { 2205 strcpy(partcode, "b");2059 asprintf(&partcode, "b"); 2206 2060 } 2207 2061 } else if (strcmp(format, "ext2") == 0 … … 2209 2063 || strcmp(format, "ext3") == 0 || strcmp(format, "xfs") == 0 2210 2064 || strcmp(format, "jfs") == 0) { 2211 strcpy(partcode, "83");2065 asprintf(&partcode, "83"); 2212 2066 } else if (strcmp(format, "minix") == 0) { 2213 strcpy(partcode, "81");2067 asprintf(&partcode, "81"); 2214 2068 } else if (strcmp(format, "raid") == 0) { 2215 strcpy(partcode, "fd");2069 asprintf(&partcode, "fd"); 2216 2070 } else if ((strcmp(format, "ufs") == 0) 2217 2071 || (strcmp(format, "ffs") == 0)) { /* raid autodetect */ 2218 strcpy(partcode, "a5");2072 asprintf(&partcode, "a5"); 2219 2073 } else if (strcmp(format, "lvm") == 0) { 2220 strcpy(partcode, "8e");2074 asprintf(&partcode, "8e"); 2221 2075 } else if (format[0] == '\0') { /* LVM physical partition */ 2222 partcode[0] = '\0';2076 asprintf(&partcode, ""); 2223 2077 } else if (strlen(format) >= 1 && strlen(format) <= 2) { 2224 strcpy(partcode, format);2078 asprintf(&partcode, format); 2225 2079 } else { 2226 2080 /* probably an image */ 2227 sprintf(tmp,2081 asprintf(&tmp, 2228 2082 "Unknown format ('%s') - using supplied string anyway", 2229 2083 format); 2230 2084 mvaddstr_and_log_it(g_currentY++, 0, tmp); 2085 paranoid_free(tmp); 2231 2086 #ifdef __FreeBSD__ 2232 strcpy(partcode, format); // was a52087 asprintf(&partcode, format); // was a5 2233 2088 #else 2234 strcpy(partcode, format); // was 832235 #endif 2236 } 2237 sprintf(tmp, "Setting %s's type to %s (%s)", partition, format,2089 asprintf(&partcode, format); // was 83 2090 #endif 2091 } 2092 asprintf(&tmp, "Setting %s's type to %s (%s)", partition, format, 2238 2093 partcode); 2094 paranoid_free(partition); 2095 2239 2096 log_msg(1, tmp); 2097 paranoid_free(tmp); 2240 2098 if (partcode[0] != '\0' && strcmp(partcode, "83")) { /* no need to set type if 83: 83 is default */ 2241 2099 … … 2243 2101 res = 0; 2244 2102 fput_string_one_char_at_a_time(pout_to_fdisk, "t\n"); 2103 tmp1 = last_line_of_file(FDISK_LOG); 2245 2104 if (partno > 1 2246 || strstr( last_line_of_file(FDISK_LOG), " (1-4)")) {2105 || strstr(tmp1, " (1-4)")) { 2247 2106 log_msg(5, "Specifying partno (%d) - yay", partno); 2248 sprintf(tmp, "%d\n", partno);2107 asprintf(&tmp, "%d\n", partno); 2249 2108 fput_string_one_char_at_a_time(pout_to_fdisk, tmp); 2250 log_msg(5, "A - last line = '%s'", 2251 last_line_of_file(FDISK_LOG)); 2252 } 2253 2254 sprintf(tmp, "%s\n", partcode); 2109 paranoid_free(tmp); 2110 paranoid_free(tmp1); 2111 tmp1 = last_line_of_file(FDISK_LOG); 2112 log_msg(5, "A - last line = '%s'", tmp1); 2113 } 2114 paranoid_free(tmp1); 2115 2116 asprintf(&tmp, "%s\n", partcode); 2255 2117 fput_string_one_char_at_a_time(pout_to_fdisk, tmp); 2256 log_msg(5, "B - last line = '%s'", 2257 last_line_of_file(FDISK_LOG)); 2118 paranoid_free(tmp); 2119 tmp1 = last_line_of_file(FDISK_LOG); 2120 log_msg(5, "B - last line = '%s'",tmp1); 2121 paranoid_free(tmp1); 2122 2258 2123 fput_string_one_char_at_a_time(pout_to_fdisk, "\n"); 2259 log_msg(5, "C - last line = '%s'", 2260 last_line_of_file(FDISK_LOG)); 2261 2262 strcpy(tmp, last_line_of_file(FDISK_LOG)); 2124 tmp1 = last_line_of_file(FDISK_LOG); 2125 log_msg(5, "C - last line = '%s'",tmp1); 2126 paranoid_free(tmp1); 2127 2128 tmp = last_line_of_file(FDISK_LOG); 2263 2129 if (!strstr(tmp, " (m ")) { 2264 2130 log_msg(1, "last line = '%s'; part type set failed", tmp); … … 2266 2132 fput_string_one_char_at_a_time(pout_to_fdisk, "\n"); 2267 2133 } 2134 paranoid_free(tmp); 2268 2135 fput_string_one_char_at_a_time(pout_to_fdisk, "p\n"); 2269 2136 } else { 2270 sprintf(output, "t\n%d\n%s\n", partno, partcode); 2271 strcat(output, "w\n"); 2272 sprintf(command, "parted2fdisk %s >> %s 2>> %s", drive, 2137 asprintf(&output, "t\n%d\n%s\nw\n", partno, partcode); 2138 asprintf(&command, "parted2fdisk %s >> %s 2>> %s", drive, 2273 2139 MONDO_LOGFILE, MONDO_LOGFILE); 2274 2140 log_msg(5, "output = '%s'", output); … … 2284 2150 paranoid_pclose(fout); 2285 2151 } 2286 } 2152 paranoid_free(command); 2153 paranoid_free(output); 2154 } 2155 /* BERLIOS: Useless as command not initialized in all cases 2287 2156 if (res) { 2288 2157 log_OS_error(command); 2289 2158 } 2290 } 2291 2292 paranoid_free(partition); 2293 paranoid_free(command); 2294 paranoid_free(output); 2295 paranoid_free(tmp); 2159 */ 2160 } 2296 2161 paranoid_free(partcode); 2297 paranoid_free(logfile); 2162 2298 2163 2299 2164 return (res); … … 2313 2178 2314 2179 assert_string_is_neither_NULL_nor_zerolength(raid_device); 2315 malloc_string(program);2316 2180 2317 2181 #ifdef __FreeBSD__ … … 2320 2184 return 1; 2321 2185 } 2322 sprintf(program, "vinum start -f %s", raid_device);2186 asprintf(&program, "vinum start -f %s", raid_device); 2323 2187 #else 2324 sprintf(program, "raidstart %s", raid_device);2188 asprintf(&program, "raidstart %s", raid_device); 2325 2189 // sprintf (program, "raidstart " RAID_DEVICE_STUB "*"); 2326 2190 #endif … … 2330 2194 fprintf(g_fprep, "%s\n", program); 2331 2195 } 2196 paranoid_free(program); 2197 2332 2198 if (res) { 2333 2199 log_msg(1, "Warning - failed to start RAID device %s", … … 2338 2204 return (retval); 2339 2205 } 2340 2341 2206 2342 2207 … … 2358 2223 2359 2224 assert_string_is_neither_NULL_nor_zerolength(raid_device); 2360 malloc_string(program);2361 2225 2362 2226 #ifdef __FreeBSD__ … … 2365 2229 return 1; 2366 2230 } 2367 sprintf(program, "vinum stop -f %s", raid_device);2231 asprintf(&program, "vinum stop -f %s", raid_device); 2368 2232 #else 2369 2370 2371 sprintf(program, "mdadm -S %s", raid_device);2233 // use raidstop if it exists, otherwise use mdadm 2234 if (run_program_and_log_output("which raidstop", FALSE)) { 2235 asprintf(&program, "mdadm -S %s", raid_device); 2372 2236 } else { 2373 sprintf(program, "raidstop %s", raid_device);2237 asprintf(&program, "raidstop %s", raid_device); 2374 2238 } 2375 2239 #endif … … 2379 2243 fprintf(g_fprep, "%s\n", program); 2380 2244 } 2245 paranoid_free(program); 2246 2381 2247 if (res) { 2382 2248 log_msg(1, "Warning - failed to stop RAID device %s", raid_device); … … 2409 2275 return (retval); 2410 2276 } 2277 2411 2278 2412 2279 /** … … 2421 2288 int retval = 0; 2422 2289 #ifndef __FreeBSD__ 2423 int res ;2290 int res = 0; 2424 2291 #endif 2425 2292 2426 2293 /** char ************************************************************/ 2427 char *incoming ;2294 char *incoming = NULL; 2428 2295 #ifndef __FreeBSD__ 2429 char *dev ;2296 char *dev = NULL; 2430 2297 #endif 2431 2298 /** pointers ********************************************************/ 2432 2299 #ifndef __FreeBSD__ 2433 char *p; 2434 #endif 2435 FILE *fin; 2436 int i; 2300 char *p = NULL; 2301 #endif 2302 FILE *fin = NULL; 2303 int i = 0; 2304 size_t n = 0; 2437 2305 2438 2306 /** end ****************************************************************/ 2439 2307 2440 malloc_string(dev);2441 malloc_string(incoming);2442 2308 assert(mountlist != NULL); 2443 2309 … … 2449 2315 "r"); 2450 2316 if (!fin) { 2451 paranoid_free(dev);2452 paranoid_free(incoming);2453 2317 return (1); 2454 2318 } 2455 for ( fgets(incoming, MAX_STR_LEN - 1, fin); !feof(fin);2456 fgets(incoming, MAX_STR_LEN - 1, fin)) {2319 for (getline(&incoming, &n, fin); !feof(fin); 2320 getline(&incoming, &n, fin)) { 2457 2321 retval += stop_raid_device(incoming); 2458 2322 } … … 2461 2325 if (!fin) { 2462 2326 log_OS_error("/proc/mdstat"); 2463 paranoid_free(dev);2464 paranoid_free(incoming);2465 2327 return (1); 2466 2328 } 2467 for ( fgets(incoming, MAX_STR_LEN - 1, fin); !feof(fin);2468 fgets(incoming, MAX_STR_LEN - 1, fin)) {2329 for (getline(&incoming, &n, fin); !feof(fin); 2330 getline(&incoming, &n, fin)) { 2469 2331 for (p = incoming; 2470 2332 *p != '\0' && (*p != 'm' || *(p + 1) != 'd' 2471 2333 || !isdigit(*(p + 2))); p++); 2472 2334 if (*p != '\0') { 2473 sprintf(dev, "/dev/%s", p); 2335 asprintf(&dev, "/dev/%s", p); 2336 /* BERLIOS : 32 Hard coded value */ 2474 2337 for (p = dev; *p > 32; p++); 2475 2338 *p = '\0'; 2476 2339 res = stop_raid_device(dev); 2477 } 2478 } 2479 #endif 2340 paranoid_free(dev); 2341 } 2342 } 2343 #endif 2344 paranoid_free(incoming); 2480 2345 } 2481 2346 paranoid_fclose(fin); … … 2483 2348 log_msg(1, "Warning - unable to stop some RAID devices"); 2484 2349 } 2485 paranoid_free(dev); 2486 paranoid_free(incoming); 2487 system("sync"); 2488 system("sync"); 2489 system("sync"); 2350 sync(); 2351 sync(); 2352 sync(); 2490 2353 sleep(1); 2491 2354 return (retval); 2492 2355 } 2493 2494 2356 2495 2357 … … 2510 2372 /** end ***************************************************************/ 2511 2373 2512 malloc_string(tmp);2513 2374 assert_string_is_neither_NULL_nor_zerolength(format); 2514 2375 assert(program != NULL); … … 2542 2403 sprintf(program, "mkfs -t %s -c", format); // -c checks for bad blocks 2543 2404 #endif 2544 sprintf(tmp, "Unknown format (%s) - assuming '%s' will do", format,2405 asprintf(&tmp, "Unknown format (%s) - assuming '%s' will do", format, 2545 2406 program); 2546 2407 log_it(tmp); 2408 paranoid_free(tmp); 2547 2409 res = 0; 2548 2410 } 2549 paranoid_free(tmp);2550 2411 return (res); 2551 2412 } … … 2573 2434 /** end *************************************************************/ 2574 2435 2575 malloc_string(tmp);2576 2436 assert(mountlist != NULL); 2577 2437 assert_string_is_neither_NULL_nor_zerolength(drive_name); … … 2584 2444 original_size_of_drive += mountlist->el[partno].size; 2585 2445 } else { 2586 sprintf(tmp, "Skipping %s", mountlist->el[partno].device);2446 asprintf(&tmp, "Skipping %s", mountlist->el[partno].device); 2587 2447 // log_to_screen(tmp); 2448 paranoid_free(tmp); 2588 2449 } 2589 2450 } 2590 2451 original_size_of_drive = original_size_of_drive / 1024; 2591 paranoid_free(tmp);2592 2452 return (original_size_of_drive); 2593 2453 } … … 2632 2492 /** end **************************************************************/ 2633 2493 2634 malloc_string(tmp);2635 2494 assert(mountlist != NULL); 2636 2495 assert_string_is_neither_NULL_nor_zerolength(drive_name); … … 2639 2498 if (strncmp(drive_name, RAID_DEVICE_STUB, strlen(RAID_DEVICE_STUB)) 2640 2499 == 0) { 2641 paranoid_free(tmp);2642 2500 return; 2643 2501 } … … 2653 2511 if (current_size_of_drive <= 0) { 2654 2512 log_it("Not resizing to match %s - can't find drive", drive_name); 2655 paranoid_free(tmp);2656 2513 return; 2657 2514 } 2658 sprintf(tmp, _("Expanding entries to suit drive %s (%ld MB)"), drive_name,2659 current_size_of_drive);2515 asprintf(&tmp, _("Expanding entries to suit drive %s (%ld MB)"), 2516 drive_name, current_size_of_drive); 2660 2517 log_to_screen(tmp); 2518 paranoid_free(tmp); 2661 2519 2662 2520 drivemntlist = malloc(sizeof(struct mountlist_reference)); … … 2675 2533 2676 2534 if (original_size_of_drive <= 0) { 2677 sprintf(tmp, _("Cannot resize %s's entries. Drive not found."),2535 asprintf(&tmp, _("Cannot resize %s's entries. Drive not found."), 2678 2536 drive_name); 2679 2537 log_to_screen(tmp); … … 2683 2541 factor = 2684 2542 (float) (current_size_of_drive) / (float) (original_size_of_drive); 2685 sprintf(tmp, "Disk %s was %ld MB; is now %ld MB; factor = %f",2543 asprintf(&tmp, "Disk %s was %ld MB; is now %ld MB; factor = %f", 2686 2544 drive_name, original_size_of_drive, current_size_of_drive, 2687 2545 factor); 2688 2546 log_to_screen(tmp); 2547 paranoid_free(tmp); 2689 2548 2690 2549 lastpart = drivemntlist->entries - 1; … … 2705 2564 newsizL = (long) new_size; 2706 2565 } 2707 sprintf(tmp, _("Changing %s from %lld KB to %ld KB"),2566 asprintf(&tmp, _("Changing %s from %lld KB to %ld KB"), 2708 2567 drivemntlist->el[partno]->device, 2709 2568 drivemntlist->el[partno]->size, newsizL); 2710 2569 log_to_screen(tmp); 2570 paranoid_free(tmp); 2711 2571 drivemntlist->el[partno]->size = newsizL; 2712 2572 } 2713 2573 final_size = get_phys_size_of_drive(drive_name); 2714 sprintf(tmp, _("final_size = %ld MB"), final_size);2574 asprintf(&tmp, _("final_size = %ld MB"), final_size); 2715 2575 log_to_screen(tmp); 2716 2576 paranoid_free(tmp); … … 2778 2638 log_msg(1, "Creating list of partitions for drive %s", drive_name); 2779 2639 2780 tmp_drive_name = strdup(drive_name);2640 asprintf(&tmp_drive_name,drive_name); 2781 2641 if (!tmp_drive_name) 2782 2642 fatal_error("Out of memory"); … … 2798 2658 } 2799 2659 } 2800 if (tmp_drive_name) 2801 free(tmp_drive_name); 2660 paranoid_free(tmp_drive_name); 2802 2661 } 2803 2662
Note:
See TracChangeset
for help on using the changeset viewer.