Changeset 128 in MondoRescue for branches/2.05/mondo/mondo/common/libmondo-devices.c
- Timestamp:
- Nov 19, 2005, 2:27:41 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.05/mondo/mondo/common/libmondo-devices.c
r126 r128 247 247 #elif linux 248 248 #define u64 unsigned long long 249 #include <linux/fs.h> 249 #include <linux/fs.h> /* for BLKGETSIZE64 */ 250 250 #include <linux/hdreg.h> 251 251 #endif … … 262 262 extern char *g_selfmounted_isodir; 263 263 264 static char g_cdrw_drive_is_here[MAX_STR_LEN /4]="";265 static char g_cdrom_drive_is_here[MAX_STR_LEN /4]="";266 static char g_dvd_drive_is_here[MAX_STR_LEN /4]="";264 static char g_cdrw_drive_is_here[MAX_STR_LEN / 4] = ""; 265 static char g_cdrom_drive_is_here[MAX_STR_LEN / 4] = ""; 266 static char g_dvd_drive_is_here[MAX_STR_LEN / 4] = ""; 267 267 268 268 … … 271 271 * @ingroup globalGroup 272 272 */ 273 bool g_restoring_live_from_cd =FALSE;274 275 extern t_bkptype g_backup_media_type; 273 bool g_restoring_live_from_cd = FALSE; 274 275 extern t_bkptype g_backup_media_type; // set by main() 276 276 277 277 … … 280 280 void set_g_cdrom_and_g_dvd_to_bkpinfo_value(struct s_bkpinfo *bkpinfo) 281 281 { 282 strcpy(g_cdrom_drive_is_here, bkpinfo->media_device);// just in case283 strcpy(g_dvd_drive_is_here, bkpinfo->media_device);// just in case282 strcpy(g_cdrom_drive_is_here, bkpinfo->media_device); // just in case 283 strcpy(g_dvd_drive_is_here, bkpinfo->media_device); // just in case 284 284 } 285 285 … … 293 293 { 294 294 // log_it("rctada: Retracting all CD trays", __LINE__); 295 if (strlen(g_cdrom_drive_is_here)>0) 296 { inject_device(g_cdrom_drive_is_here); } 297 if (strlen(g_dvd_drive_is_here)>0) 298 { inject_device(g_dvd_drive_is_here); } 299 if (strlen(g_cdrw_drive_is_here)>0) 300 { inject_device(g_cdrw_drive_is_here); } 295 if (strlen(g_cdrom_drive_is_here) > 0) { 296 inject_device(g_cdrom_drive_is_here); 297 } 298 if (strlen(g_dvd_drive_is_here) > 0) { 299 inject_device(g_dvd_drive_is_here); 300 } 301 if (strlen(g_cdrw_drive_is_here) > 0) { 302 inject_device(g_cdrw_drive_is_here); 303 } 301 304 // log_it("rctada: killing autorun"); 302 305 // run_program_and_log_output("killall autorun", TRUE); 303 if (!run_program_and_log_output("ps | grep autorun | grep -v grep", 5)) 304 { 305 log_it("autorun detected; sleeping for 2 seconds"); 306 sleep(2); 307 } 308 log_it("rctada: Unmounting all CD drives", __LINE__); 309 run_program_and_log_output("umount /dev/cdr* /dev/dvd*", 5); 306 if (!run_program_and_log_output("ps | grep autorun | grep -v grep", 5)) { 307 log_it("autorun detected; sleeping for 2 seconds"); 308 sleep(2); 309 } 310 log_it("rctada: Unmounting all CD drives", __LINE__); 311 run_program_and_log_output("umount /dev/cdr* /dev/dvd*", 5); 310 312 } 311 313 … … 317 319 * @ingroup utilityGroup 318 320 */ 319 bool 320 am_I_in_disaster_recovery_mode (void) 321 { 322 char *tmp, *comment; 323 bool is_this_a_ramdisk = FALSE; 324 325 malloc_string(tmp); 326 malloc_string(comment); 327 strcpy(tmp, where_is_root_mounted()); 328 sprintf(comment, "root is mounted at %s\n", tmp); 329 log_msg(0, comment); 330 log_msg(0, "No, Schlomo, that doesn't mean %s is the root partition. It's just a debugging message. Relax. It's part of am_I_in_disaster_recovery_mode().", tmp); 321 bool am_I_in_disaster_recovery_mode(void) 322 { 323 char *tmp, *comment; 324 bool is_this_a_ramdisk = FALSE; 325 326 malloc_string(tmp); 327 malloc_string(comment); 328 strcpy(tmp, where_is_root_mounted()); 329 sprintf(comment, "root is mounted at %s\n", tmp); 330 log_msg(0, comment); 331 log_msg(0, 332 "No, Schlomo, that doesn't mean %s is the root partition. It's just a debugging message. Relax. It's part of am_I_in_disaster_recovery_mode().", 333 tmp); 331 334 332 335 #ifdef __FreeBSD__ 333 if (strstr(tmp, "/dev/md")) 334 { 335 is_this_a_ramdisk = TRUE; 336 } 336 if (strstr(tmp, "/dev/md")) { 337 is_this_a_ramdisk = TRUE; 338 } 337 339 #else 338 if (!strncmp(tmp, "/dev/ram", 8) || (!strncmp(tmp, "/dev/rd", 7) && !strcmp(tmp, "/dev/rd/") && strncmp(tmp, "/dev/rd/cd", 10)) || strstr(tmp, "rootfs") 339 || !strcmp(tmp, "/dev/root")) 340 { is_this_a_ramdisk = TRUE; } 341 else 342 { is_this_a_ramdisk = FALSE; } 343 #endif 344 345 if (is_this_a_ramdisk) 346 { 347 if (!does_file_exist("/THIS-IS-A-RAMDISK") && !does_file_exist("/tmp/mountlist.txt.sample")) 348 { 349 log_to_screen("Using /dev/root is stupid of you but I'll forgive you."); 350 is_this_a_ramdisk = FALSE; 351 } 352 } 353 if (does_file_exist("/THIS-IS-A-RAMDISK")) 354 { 355 is_this_a_ramdisk = TRUE; 356 } 357 paranoid_free(tmp); 358 paranoid_free(comment); 359 log_msg(1, "Is this a ramdisk? result = %d", is_this_a_ramdisk); 360 return(is_this_a_ramdisk); 340 if (!strncmp(tmp, "/dev/ram", 8) 341 || (!strncmp(tmp, "/dev/rd", 7) && !strcmp(tmp, "/dev/rd/") 342 && strncmp(tmp, "/dev/rd/cd", 10)) || strstr(tmp, "rootfs") 343 || !strcmp(tmp, "/dev/root")) { 344 is_this_a_ramdisk = TRUE; 345 } else { 346 is_this_a_ramdisk = FALSE; 347 } 348 #endif 349 350 if (is_this_a_ramdisk) { 351 if (!does_file_exist("/THIS-IS-A-RAMDISK") 352 && !does_file_exist("/tmp/mountlist.txt.sample")) { 353 log_to_screen 354 ("Using /dev/root is stupid of you but I'll forgive you."); 355 is_this_a_ramdisk = FALSE; 356 } 357 } 358 if (does_file_exist("/THIS-IS-A-RAMDISK")) { 359 is_this_a_ramdisk = TRUE; 360 } 361 paranoid_free(tmp); 362 paranoid_free(comment); 363 log_msg(1, "Is this a ramdisk? result = %d", is_this_a_ramdisk); 364 return (is_this_a_ramdisk); 361 365 } 362 366 … … 373 377 static char *bkptype_to_string(t_bkptype bt) 374 378 { 375 static char output[MAX_STR_LEN/4];376 switch(bt) 377 { 378 case none:strcpy(output, "none");379 static char output[MAX_STR_LEN / 4]; 380 switch (bt) { 381 case none: 382 strcpy(output, "none"); 379 383 break; 380 case iso: strcpy(output, "iso"); 384 case iso: 385 strcpy(output, "iso"); 381 386 break; 382 case cdr: strcpy(output, "cdr"); 387 case cdr: 388 strcpy(output, "cdr"); 383 389 break; 384 case cdrw: strcpy(output, "cdrw"); 390 case cdrw: 391 strcpy(output, "cdrw"); 385 392 break; 386 case cdstream: strcpy(output, "cdstream"); 393 case cdstream: 394 strcpy(output, "cdstream"); 387 395 break; 388 case nfs: strcpy(output, "nfs"); 396 case nfs: 397 strcpy(output, "nfs"); 389 398 break; 390 case tape: strcpy(output, "tape"); 399 case tape: 400 strcpy(output, "tape"); 391 401 break; 392 case udev: strcpy(output, "udev"); 402 case udev: 403 strcpy(output, "udev"); 393 404 break; 394 default: strcpy(output, "default"); 395 } 396 return(output); 405 default: 406 strcpy(output, "default"); 407 } 408 return (output); 397 409 } 398 410 … … 408 420 * @return the return value of the @c eject command. (0=success, nonzero=failure) 409 421 */ 410 int eject_device(char*dev) 411 { 412 char *command; 413 int res1=0, res2=0; 414 415 malloc_string(command); 416 417 if (IS_THIS_A_STREAMING_BACKUP(g_backup_media_type) && g_backup_media_type!=udev) 418 { 419 sprintf(command, "mt -f %s offline", dev); 420 res1 = run_program_and_log_output(command, 1); 421 } 422 else 423 { 424 res1 = 0; 425 } 422 int eject_device(char *dev) 423 { 424 char *command; 425 int res1 = 0, res2 = 0; 426 427 malloc_string(command); 428 429 if (IS_THIS_A_STREAMING_BACKUP(g_backup_media_type) 430 && g_backup_media_type != udev) { 431 sprintf(command, "mt -f %s offline", dev); 432 res1 = run_program_and_log_output(command, 1); 433 } else { 434 res1 = 0; 435 } 426 436 427 437 #ifdef __FreeBSD__ 428 if (strstr (dev, "acd")) { 429 sprintf (command, "cdcontrol -f %s eject", dev); 430 } else { 431 sprintf (command, "camcontrol eject `echo %s | sed 's|/dev/||'`", dev); 432 } 438 if (strstr(dev, "acd")) { 439 sprintf(command, "cdcontrol -f %s eject", dev); 440 } else { 441 sprintf(command, "camcontrol eject `echo %s | sed 's|/dev/||'`", 442 dev); 443 } 433 444 #else 434 sprintf(command, "eject %s", dev); 435 #endif 436 437 log_msg(3, "Ejecting %s", dev); 438 res2 = run_program_and_log_output(command, 1); 439 paranoid_free(command); 440 if (res1 && res2) { return(1); } 441 else { return(0); } 445 sprintf(command, "eject %s", dev); 446 #endif 447 448 log_msg(3, "Ejecting %s", dev); 449 res2 = run_program_and_log_output(command, 1); 450 paranoid_free(command); 451 if (res1 && res2) { 452 return (1); 453 } else { 454 return (0); 455 } 442 456 } 443 457 … … 447 461 * @return 0 for success, nonzero for failure. 448 462 */ 449 int inject_device(char *dev)450 { 451 452 453 454 463 int inject_device(char *dev) 464 { 465 char *command; 466 int i; 467 468 malloc_string(command); 455 469 456 470 457 471 #ifdef __FreeBSD__ 458 if (strstr (dev, "acd")) { 459 sprintf (command, "cdcontrol -f %s close", dev); 460 } else { 461 sprintf (command, "camcontrol load `echo %s | sed 's|/dev/||'`", dev); 462 } 472 if (strstr(dev, "acd")) { 473 sprintf(command, "cdcontrol -f %s close", dev); 474 } else { 475 sprintf(command, "camcontrol load `echo %s | sed 's|/dev/||'`", 476 dev); 477 } 463 478 #else 464 465 #endif 466 467 468 return(i);479 sprintf(command, "eject -t %s", dev); 480 #endif 481 i = run_program_and_log_output(command, FALSE); 482 paranoid_free(command); 483 return (i); 469 484 } 470 485 … … 475 490 * @return TRUE if it exists, FALSE if it doesn't. 476 491 */ 477 bool 478 does_device_exist (char *device) 479 { 480 481 /*@ buffers ************************************************************/ 482 char *tmp; 483 bool ret; 484 485 malloc_string(tmp); 486 assert_string_is_neither_NULL_nor_zerolength(device); 487 488 sprintf (tmp, "ls %s > /dev/null 2> /dev/null", device); 489 490 if (system (tmp)) 491 { 492 ret = FALSE; 493 } 494 else 495 { 496 ret = TRUE; 497 } 498 paranoid_free(tmp); 499 return(ret); 492 bool does_device_exist(char *device) 493 { 494 495 /*@ buffers *********************************************************** */ 496 char *tmp; 497 bool ret; 498 499 malloc_string(tmp); 500 assert_string_is_neither_NULL_nor_zerolength(device); 501 502 sprintf(tmp, "ls %s > /dev/null 2> /dev/null", device); 503 504 if (system(tmp)) { 505 ret = FALSE; 506 } else { 507 ret = TRUE; 508 } 509 paranoid_free(tmp); 510 return (ret); 500 511 } 501 512 … … 505 516 * @return TRUE (there's a Linux/FreeBSD partition) or FALSE (Microsoft has taken over yet another innocent PC). 506 517 */ 507 bool 508 does_nonMS_partition_exist (void) 518 bool does_nonMS_partition_exist(void) 509 519 { 510 520 #if __FreeBSD__ 511 return !system ("for drive in /dev/ad? /dev/da?; do fdisk $drive | grep -q FreeBSD && exit 0; done; false"); 521 return 522 !system 523 ("for drive in /dev/ad? /dev/da?; do fdisk $drive | grep -q FreeBSD && exit 0; done; false"); 512 524 #else 513 return !system ("parted2fdisk -l 2>/dev/null | grep '^/dev/' | egrep -qv '(MS|DOS|FAT|NTFS)'"); 525 return 526 !system 527 ("parted2fdisk -l 2>/dev/null | grep '^/dev/' | egrep -qv '(MS|DOS|FAT|NTFS)'"); 514 528 #endif 515 529 } … … 521 535 * @return 0 if it exists, nonzero otherwise. 522 536 */ 523 int 524 does_partition_exist (const char *drive, int partno) 525 { 526 /*@ buffers *****************************************************/ 527 char *program; 528 char *incoming; 529 char *searchstr; 530 char *tmp; 531 532 /*@ ints ********************************************************/ 533 int res = 0; 534 535 /*@ pointers ****************************************************/ 536 FILE *fin; 537 538 539 /*@ end vars ****************************************************/ 540 assert_string_is_neither_NULL_nor_zerolength(drive); 541 assert(partno>=0 && partno<999); 542 543 malloc_string(program); 544 malloc_string(incoming); 545 malloc_string(searchstr); 546 malloc_string(tmp); 537 int does_partition_exist(const char *drive, int partno) 538 { 539 /*@ buffers **************************************************** */ 540 char *program; 541 char *incoming; 542 char *searchstr; 543 char *tmp; 544 545 /*@ ints ******************************************************* */ 546 int res = 0; 547 548 /*@ pointers *************************************************** */ 549 FILE *fin; 550 551 552 /*@ end vars *************************************************** */ 553 assert_string_is_neither_NULL_nor_zerolength(drive); 554 assert(partno >= 0 && partno < 999); 555 556 malloc_string(program); 557 malloc_string(incoming); 558 malloc_string(searchstr); 559 malloc_string(tmp); 547 560 548 561 #ifdef __FreeBSD__ 549 // We assume here that this is running from mondorestore. (It is.) 550 sprintf (program, "ls %s >/dev/null 2>&1", drive, build_partition_name (tmp, drive, partno)); 551 return system (program); 562 // We assume here that this is running from mondorestore. (It is.) 563 sprintf(program, "ls %s >/dev/null 2>&1", drive, 564 build_partition_name(tmp, drive, partno)); 565 return system(program); 552 566 #else 553 tmp[0] = '\0'; 554 #endif 555 556 sprintf (program, "parted2fdisk -l %s 2> /dev/null", drive); 557 fin = popen (program, "r"); 558 if (!fin) 559 { 560 log_it("program=%s", program); 561 log_OS_error("Cannot popen-in program"); 562 return (0); 563 } 564 (void) build_partition_name (searchstr, drive, partno); 565 strcat(searchstr, " "); 566 for (res = 0; !res && fgets (incoming, MAX_STR_LEN - 1, fin); ) 567 { 568 if (strstr (incoming, searchstr)) 569 { 570 res = 1; 571 } 572 } 573 if (pclose (fin)) { log_OS_error("Cannot pclose fin"); } 574 paranoid_free(program); 575 paranoid_free(incoming); 576 paranoid_free(searchstr); 577 paranoid_free(tmp); 578 return (res); 567 tmp[0] = '\0'; 568 #endif 569 570 sprintf(program, "parted2fdisk -l %s 2> /dev/null", drive); 571 fin = popen(program, "r"); 572 if (!fin) { 573 log_it("program=%s", program); 574 log_OS_error("Cannot popen-in program"); 575 return (0); 576 } 577 (void) build_partition_name(searchstr, drive, partno); 578 strcat(searchstr, " "); 579 for (res = 0; !res && fgets(incoming, MAX_STR_LEN - 1, fin);) { 580 if (strstr(incoming, searchstr)) { 581 res = 1; 582 } 583 } 584 if (pclose(fin)) { 585 log_OS_error("Cannot pclose fin"); 586 } 587 paranoid_free(program); 588 paranoid_free(incoming); 589 paranoid_free(searchstr); 590 paranoid_free(tmp); 591 return (res); 579 592 } 580 593 … … 589 602 * @return TRUE if it exists, FALSE if it doesn't. 590 603 */ 591 bool 592 does_string_exist_in_boot_block (char *dev, char *str) 593 { 594 /*@ buffers *****************************************************/ 595 char *command; 596 597 /*@ end vars ****************************************************/ 598 int i; 599 600 assert_string_is_neither_NULL_nor_zerolength(dev); 601 assert_string_is_neither_NULL_nor_zerolength(str); 602 603 malloc_string(command); 604 sprintf (command, 605 "dd if=%s bs=446 count=1 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null", 606 dev, str); 607 i = system(command); 608 paranoid_free(command); 609 if (i) 610 { 611 return (FALSE); 612 } 613 else 614 { 615 return (TRUE); 616 } 604 bool does_string_exist_in_boot_block(char *dev, char *str) 605 { 606 /*@ buffers **************************************************** */ 607 char *command; 608 609 /*@ end vars *************************************************** */ 610 int i; 611 612 assert_string_is_neither_NULL_nor_zerolength(dev); 613 assert_string_is_neither_NULL_nor_zerolength(str); 614 615 malloc_string(command); 616 sprintf(command, 617 "dd if=%s bs=446 count=1 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null", 618 dev, str); 619 i = system(command); 620 paranoid_free(command); 621 if (i) { 622 return (FALSE); 623 } else { 624 return (TRUE); 625 } 617 626 } 618 627 … … 624 633 * @param n The number of 512-byte sectors to search. 625 634 */ 626 bool 627 does_string_exist_in_first_N_blocks (char *dev, char *str, int n) 628 { 629 /*@ buffers *****************************************************/ 630 char *command; 631 /*@ end vars ****************************************************/ 632 int i; 633 634 malloc_string(command); 635 sprintf (command, 636 "dd if=%s bs=512 count=%i 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null", 637 dev, n, str); 638 i = system(command); 639 paranoid_free(command); 640 if (i) 641 { 642 return (FALSE); 643 } 644 else 645 { 646 return (TRUE); 647 } 635 bool does_string_exist_in_first_N_blocks(char *dev, char *str, int n) 636 { 637 /*@ buffers **************************************************** */ 638 char *command; 639 /*@ end vars *************************************************** */ 640 int i; 641 642 malloc_string(command); 643 sprintf(command, 644 "dd if=%s bs=512 count=%i 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null", 645 dev, n, str); 646 i = system(command); 647 paranoid_free(command); 648 if (i) { 649 return (FALSE); 650 } else { 651 return (TRUE); 652 } 648 653 } 649 654 … … 658 663 * @see mount_CDROM_here 659 664 */ 660 int 661 find_and_mount_actual_cd (struct s_bkpinfo *bkpinfo, char *mountpoint) 662 { 663 /*@ buffers ******************************************************/ 664 665 /*@ int's *******************************************************/ 666 int res; 667 char *dev; 668 669 /*@ end vars *****************************************************/ 670 671 malloc_string(dev); 672 assert(bkpinfo!=NULL); 673 assert_string_is_neither_NULL_nor_zerolength(mountpoint); 674 675 if (g_backup_media_type == dvd) 676 { 677 strcpy(dev, g_dvd_drive_is_here); 678 if (!dev[0]) 679 { find_dvd_device(dev,FALSE); } 680 } 681 else 682 { 683 strcpy(dev, g_cdrom_drive_is_here); 684 if (!dev[0]) 685 { find_cdrom_device(dev,FALSE); } 686 } 687 688 if (bkpinfo->backup_media_type != iso) 689 { retract_CD_tray_and_defeat_autorun(); } 690 691 if (!dev[0] || (res=mount_CDROM_here (dev, mountpoint))) 692 { 693 if (!popup_and_get_string 665 int find_and_mount_actual_cd(struct s_bkpinfo *bkpinfo, char *mountpoint) 666 { 667 /*@ buffers ***************************************************** */ 668 669 /*@ int's ****************************************************** */ 670 int res; 671 char *dev; 672 673 /*@ end vars **************************************************** */ 674 675 malloc_string(dev); 676 assert(bkpinfo != NULL); 677 assert_string_is_neither_NULL_nor_zerolength(mountpoint); 678 679 if (g_backup_media_type == dvd) { 680 strcpy(dev, g_dvd_drive_is_here); 681 if (!dev[0]) { 682 find_dvd_device(dev, FALSE); 683 } 684 } else { 685 strcpy(dev, g_cdrom_drive_is_here); 686 if (!dev[0]) { 687 find_cdrom_device(dev, FALSE); 688 } 689 } 690 691 if (bkpinfo->backup_media_type != iso) { 692 retract_CD_tray_and_defeat_autorun(); 693 } 694 695 if (!dev[0] || (res = mount_CDROM_here(dev, mountpoint))) { 696 if (!popup_and_get_string 694 697 ("CD-ROM device", "Please enter your CD-ROM's /dev device", 695 dev, MAX_STR_LEN/4)) 696 { res = 1; } 697 else 698 { res=mount_CDROM_here (dev, mountpoint); } 699 } 700 if (res) { log_msg(1, "mount failed"); } 701 else { log_msg(1, "mount succeeded with %s", dev); } 702 paranoid_free(dev); 703 return (res); 698 dev, MAX_STR_LEN / 4)) { 699 res = 1; 700 } else { 701 res = mount_CDROM_here(dev, mountpoint); 702 } 703 } 704 if (res) { 705 log_msg(1, "mount failed"); 706 } else { 707 log_msg(1, "mount succeeded with %s", dev); 708 } 709 paranoid_free(dev); 710 return (res); 704 711 } 705 712 … … 720 727 * @return 0 for success, nonzero for failure. 721 728 */ 722 int 723 find_cdrw_device (char *cdrw_device) 724 { 725 /*@ buffers *************************/ 726 char *comment; 727 char *tmp; 728 char *cdr_exe; 729 char *command; 730 731 malloc_string(comment); 732 malloc_string(tmp); 733 malloc_string(cdr_exe); 734 malloc_string(command); 735 if (g_cdrw_drive_is_here[0]) 736 { 737 strcpy(cdrw_device, g_cdrw_drive_is_here); 738 log_msg(3, "Been there, done that. Returning %s", cdrw_device); 739 paranoid_free(comment); 740 paranoid_free(tmp); 741 paranoid_free(cdr_exe); 742 paranoid_free(command); 743 return(0); 744 } 745 if (g_backup_media_type == dvd) 746 { 747 log_msg(1, "This is dumb. You're calling find_cdrw_device() but you're backing up to DVD. WTF?"); 748 paranoid_free(comment); 749 paranoid_free(tmp); 750 paranoid_free(cdr_exe); 751 paranoid_free(command); 752 return(1); 753 } 754 run_program_and_log_output("insmod ide-scsi", -1); 755 if (find_home_of_exe("cdrecord")) 756 { strcpy(cdr_exe, "cdrecord"); } 757 else 758 { strcpy(cdr_exe, "dvdrecord"); } 759 tmp[0]='\0'; 760 if (find_home_of_exe(cdr_exe)) 761 { 762 sprintf(command, "%s -scanbus 2> /dev/null | tr -s '\t' ' ' | grep \"[0-9]*,[0-9]*,[0-9]*\" | grep -v \"[0-9]*) \\*\" | grep CD | cut -d' ' -f2 | head -n1", cdr_exe); 763 strcpy (tmp, call_program_and_get_last_line_of_output(command)); 764 } 765 if (strlen (tmp) < 2) 766 { 767 paranoid_free(comment); 768 paranoid_free(tmp); 769 paranoid_free(cdr_exe); 770 paranoid_free(command); 771 return 1; 772 } 773 else 774 { 775 strcpy (cdrw_device, tmp); 776 sprintf (comment, "Found CDRW device - %s", cdrw_device); 777 log_it (comment); 778 strcpy (g_cdrw_drive_is_here, cdrw_device); 779 paranoid_free(comment); 780 paranoid_free(tmp); 781 paranoid_free(cdr_exe); 782 paranoid_free(command); 783 return (0); 784 } 729 int find_cdrw_device(char *cdrw_device) 730 { 731 /*@ buffers ************************ */ 732 char *comment; 733 char *tmp; 734 char *cdr_exe; 735 char *command; 736 737 malloc_string(comment); 738 malloc_string(tmp); 739 malloc_string(cdr_exe); 740 malloc_string(command); 741 if (g_cdrw_drive_is_here[0]) { 742 strcpy(cdrw_device, g_cdrw_drive_is_here); 743 log_msg(3, "Been there, done that. Returning %s", cdrw_device); 744 paranoid_free(comment); 745 paranoid_free(tmp); 746 paranoid_free(cdr_exe); 747 paranoid_free(command); 748 return (0); 749 } 750 if (g_backup_media_type == dvd) { 751 log_msg(1, 752 "This is dumb. You're calling find_cdrw_device() but you're backing up to DVD. WTF?"); 753 paranoid_free(comment); 754 paranoid_free(tmp); 755 paranoid_free(cdr_exe); 756 paranoid_free(command); 757 return (1); 758 } 759 run_program_and_log_output("insmod ide-scsi", -1); 760 if (find_home_of_exe("cdrecord")) { 761 strcpy(cdr_exe, "cdrecord"); 762 } else { 763 strcpy(cdr_exe, "dvdrecord"); 764 } 765 tmp[0] = '\0'; 766 if (find_home_of_exe(cdr_exe)) { 767 sprintf(command, 768 "%s -scanbus 2> /dev/null | tr -s '\t' ' ' | grep \"[0-9]*,[0-9]*,[0-9]*\" | grep -v \"[0-9]*) \\*\" | grep CD | cut -d' ' -f2 | head -n1", 769 cdr_exe); 770 strcpy(tmp, call_program_and_get_last_line_of_output(command)); 771 } 772 if (strlen(tmp) < 2) { 773 paranoid_free(comment); 774 paranoid_free(tmp); 775 paranoid_free(cdr_exe); 776 paranoid_free(command); 777 return 1; 778 } else { 779 strcpy(cdrw_device, tmp); 780 sprintf(comment, "Found CDRW device - %s", cdrw_device); 781 log_it(comment); 782 strcpy(g_cdrw_drive_is_here, cdrw_device); 783 paranoid_free(comment); 784 paranoid_free(tmp); 785 paranoid_free(cdr_exe); 786 paranoid_free(command); 787 return (0); 788 } 785 789 } 786 790 … … 797 801 * @return 0 for success, nonzero for failure. 798 802 */ 799 int 800 find_cdrom_device (char *output, bool try_to_mount) 801 { 802 /*@ pointers *****************************************************/ 803 FILE *fin; 804 char *p; 805 char *q; 806 char *r; 807 int retval=0; 808 809 /*@ bool's *******************************************************/ 810 bool found_it = FALSE; 811 812 /*@ buffers ******************************************************/ 813 char *tmp; 814 char *cdr_exe; 815 char *phrase_one; 816 char *phrase_two; 817 char *command; 818 char *dvd_last_resort; 819 char *mountpoint; 820 static char the_last_place_i_found_it[MAX_STR_LEN] = ""; 821 822 /*@ intialize ****************************************************/ 823 malloc_string(tmp); 824 malloc_string(cdr_exe); 825 malloc_string(phrase_one); 826 malloc_string(phrase_two); 827 malloc_string(command); 828 malloc_string(dvd_last_resort); 829 malloc_string(mountpoint); 830 831 output[0] = '\0'; 832 phrase_one[0] = '\0'; 833 phrase_two[0] = '\0'; 834 dvd_last_resort[0] = '\0'; 835 836 /*@ end vars *****************************************************/ 837 838 if (g_cdrom_drive_is_here[0] && !isdigit(g_cdrom_drive_is_here[0])) 839 { 840 strcpy(output, g_cdrom_drive_is_here); 841 log_msg(3, "Been there, done that. Returning %s", output); 842 retval=0; 843 goto end_of_find_cdrom_device; 844 } 845 if (the_last_place_i_found_it[0]!='\0' && !try_to_mount) 846 { 847 strcpy(output, the_last_place_i_found_it); 848 log_msg(3, "find_cdrom_device() --- returning last found location - '%s'", output); 849 retval=0; 850 goto end_of_find_cdrom_device; 851 } 852 853 sprintf(mountpoint,"/tmp/cd.%d", (int)(random()%32767)); 854 make_hole_for_dir(mountpoint); 855 856 if (find_home_of_exe("cdrecord")) 857 { strcpy(cdr_exe, "cdrecord"); } 858 else 859 { strcpy(cdr_exe, "dvdrecord"); } 860 tmp[0]='\0'; 861 if (!find_home_of_exe(cdr_exe)) 862 { 863 strcpy(output, "/dev/cdrom"); 864 log_msg(4, "Can't find cdrecord; assuming %s", output); 865 if (!does_device_exist(output)) 866 { 867 log_msg(4, "That didn't work. Sorry."); 868 retval=1; 869 goto end_of_find_cdrom_device; 870 } 871 else 872 { 873 retval=0; 874 goto end_of_find_cdrom_device; 875 } 876 } 877 878 sprintf(command, "%s -scanbus 2> /dev/null", cdr_exe); 879 fin = popen (command, "r"); 880 if (!fin) { log_msg(4, "command=%s", command); log_OS_error("Cannot popen command"); return (1); } 881 for (fgets (tmp, MAX_STR_LEN, fin); !feof (fin); 882 fgets (tmp, MAX_STR_LEN, fin)) 883 { 884 p = strchr (tmp, '\''); 885 if (p) 886 { 887 q = strchr (++p, '\''); 888 if (q) 889 { 890 for (r = q; *(r - 1) == ' '; r--); 891 *r = '\0'; 892 strcpy (phrase_one, p); 893 p = strchr (++q, '\''); 894 if (p) 895 { 896 q = strchr (++p, '\''); 897 if (q) 898 { 899 while (*(q - 1) == ' ') 900 { 901 q--; 902 } 903 *q = '\0'; 904 strcpy (phrase_two, p); 905 } 906 } 907 } 908 } 909 } 910 paranoid_pclose(fin); 803 int find_cdrom_device(char *output, bool try_to_mount) 804 { 805 /*@ pointers **************************************************** */ 806 FILE *fin; 807 char *p; 808 char *q; 809 char *r; 810 int retval = 0; 811 812 /*@ bool's ****************************************************** */ 813 bool found_it = FALSE; 814 815 /*@ buffers ***************************************************** */ 816 char *tmp; 817 char *cdr_exe; 818 char *phrase_one; 819 char *phrase_two; 820 char *command; 821 char *dvd_last_resort; 822 char *mountpoint; 823 static char the_last_place_i_found_it[MAX_STR_LEN] = ""; 824 825 /*@ intialize *************************************************** */ 826 malloc_string(tmp); 827 malloc_string(cdr_exe); 828 malloc_string(phrase_one); 829 malloc_string(phrase_two); 830 malloc_string(command); 831 malloc_string(dvd_last_resort); 832 malloc_string(mountpoint); 833 834 output[0] = '\0'; 835 phrase_one[0] = '\0'; 836 phrase_two[0] = '\0'; 837 dvd_last_resort[0] = '\0'; 838 839 /*@ end vars **************************************************** */ 840 841 if (g_cdrom_drive_is_here[0] && !isdigit(g_cdrom_drive_is_here[0])) { 842 strcpy(output, g_cdrom_drive_is_here); 843 log_msg(3, "Been there, done that. Returning %s", output); 844 retval = 0; 845 goto end_of_find_cdrom_device; 846 } 847 if (the_last_place_i_found_it[0] != '\0' && !try_to_mount) { 848 strcpy(output, the_last_place_i_found_it); 849 log_msg(3, 850 "find_cdrom_device() --- returning last found location - '%s'", 851 output); 852 retval = 0; 853 goto end_of_find_cdrom_device; 854 } 855 856 sprintf(mountpoint, "/tmp/cd.%d", (int) (random() % 32767)); 857 make_hole_for_dir(mountpoint); 858 859 if (find_home_of_exe("cdrecord")) { 860 strcpy(cdr_exe, "cdrecord"); 861 } else { 862 strcpy(cdr_exe, "dvdrecord"); 863 } 864 tmp[0] = '\0'; 865 if (!find_home_of_exe(cdr_exe)) { 866 strcpy(output, "/dev/cdrom"); 867 log_msg(4, "Can't find cdrecord; assuming %s", output); 868 if (!does_device_exist(output)) { 869 log_msg(4, "That didn't work. Sorry."); 870 retval = 1; 871 goto end_of_find_cdrom_device; 872 } else { 873 retval = 0; 874 goto end_of_find_cdrom_device; 875 } 876 } 877 878 sprintf(command, "%s -scanbus 2> /dev/null", cdr_exe); 879 fin = popen(command, "r"); 880 if (!fin) { 881 log_msg(4, "command=%s", command); 882 log_OS_error("Cannot popen command"); 883 return (1); 884 } 885 for (fgets(tmp, MAX_STR_LEN, fin); !feof(fin); 886 fgets(tmp, MAX_STR_LEN, fin)) { 887 p = strchr(tmp, '\''); 888 if (p) { 889 q = strchr(++p, '\''); 890 if (q) { 891 for (r = q; *(r - 1) == ' '; r--); 892 *r = '\0'; 893 strcpy(phrase_one, p); 894 p = strchr(++q, '\''); 895 if (p) { 896 q = strchr(++p, '\''); 897 if (q) { 898 while (*(q - 1) == ' ') { 899 q--; 900 } 901 *q = '\0'; 902 strcpy(phrase_two, p); 903 } 904 } 905 } 906 } 907 } 908 paranoid_pclose(fin); 911 909 912 910 #ifndef __FreeBSD__ 913 if (strlen(phrase_two)==0) 914 { log_msg(4, "Not running phase two. String is empty."); } 915 else 916 { 917 sprintf (command, "dmesg | grep \"%s\" 2> /dev/null", phrase_two); 918 fin = popen (command, "r"); 919 if (!fin) 920 { 921 log_msg(4, "Cannot run 2nd command - non-fatal, fortunately"); 922 } 923 else 924 { 925 for (fgets (tmp, MAX_STR_LEN, fin); !feof (fin); fgets (tmp, MAX_STR_LEN, fin)) 926 { 927 log_msg(5, "--> '%s'", tmp); 928 if (tmp[0] != ' ' && tmp[1] != ' ') 929 { 930 p = strchr (tmp, ':'); 931 if (p) 932 { 933 *p = '\0'; 934 if (strstr(tmp, "DVD")) 935 { 936 sprintf(dvd_last_resort, "/dev/%s", tmp); 937 log_msg(4, "Ignoring '%s' because it's a DVD drive", tmp); 938 } 939 else 940 { 941 sprintf (output, "/dev/%s", tmp); 942 found_it = TRUE; 943 } 944 } 945 } 946 } 947 paranoid_pclose(fin); 948 } 949 } 911 if (strlen(phrase_two) == 0) { 912 log_msg(4, "Not running phase two. String is empty."); 913 } else { 914 sprintf(command, "dmesg | grep \"%s\" 2> /dev/null", phrase_two); 915 fin = popen(command, "r"); 916 if (!fin) { 917 log_msg(4, "Cannot run 2nd command - non-fatal, fortunately"); 918 } else { 919 for (fgets(tmp, MAX_STR_LEN, fin); !feof(fin); 920 fgets(tmp, MAX_STR_LEN, fin)) { 921 log_msg(5, "--> '%s'", tmp); 922 if (tmp[0] != ' ' && tmp[1] != ' ') { 923 p = strchr(tmp, ':'); 924 if (p) { 925 *p = '\0'; 926 if (strstr(tmp, "DVD")) { 927 sprintf(dvd_last_resort, "/dev/%s", tmp); 928 log_msg(4, 929 "Ignoring '%s' because it's a DVD drive", 930 tmp); 931 } else { 932 sprintf(output, "/dev/%s", tmp); 933 found_it = TRUE; 934 } 935 } 936 } 937 } 938 paranoid_pclose(fin); 939 } 940 } 950 941 951 942 #endif 952 943 #ifdef __FreeBSD__ 953 if (!found_it) 954 { log_msg(4, "OK, approach 2"); if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/cdrom"))) 955 { if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/cdrom1"))) 956 { if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/dvd"))) 957 { if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/acd0"))) 958 { if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/cd01"))) 959 { if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/acd1"))) 960 { if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/cd1"))) 961 { retval=1; goto end_of_find_cdrom_device; } 962 } } } } } } } 944 if (!found_it) { 945 log_msg(4, "OK, approach 2"); 946 if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/cdrom"))) { 947 if (! 948 (found_it = 949 set_dev_to_this_if_rx_OK(output, "/dev/cdrom1"))) { 950 if (! 951 (found_it = 952 set_dev_to_this_if_rx_OK(output, "/dev/dvd"))) { 953 if (! 954 (found_it = 955 set_dev_to_this_if_rx_OK(output, "/dev/acd0"))) { 956 if (! 957 (found_it = 958 set_dev_to_this_if_rx_OK(output, 959 "/dev/cd01"))) { 960 if (! 961 (found_it = 962 set_dev_to_this_if_rx_OK(output, 963 "/dev/acd1"))) { 964 if (! 965 (found_it = 966 set_dev_to_this_if_rx_OK(output, 967 "/dev/cd1"))) 968 { 969 retval = 1; 970 goto end_of_find_cdrom_device; 971 } 972 } 973 } 974 } 975 } 976 } 977 } 978 } 963 979 #else 964 if (!found_it && strlen(dvd_last_resort)>0) 965 { 966 log_msg(4, "Well, I'll use the DVD - %s - as a last resort", dvd_last_resort); 967 strcpy(output, dvd_last_resort); 968 found_it = TRUE; 969 } 970 if (found_it) 971 { 972 sprintf(tmp, "grep \"%s=ide-scsi\" /proc/cmdline &> /dev/null", strrchr(output,'/')+1); 973 if (system(tmp) == 0) 974 { 975 log_msg(4, "%s is not right. It's being SCSI-emulated. Continuing.", output); 976 found_it = FALSE; 977 output[0] = '\0'; 978 } 979 } 980 981 if (found_it) 982 { 983 log_msg(4, "(find_cdrom_device) --> '%s'", output); 984 if (!does_device_exist(output)) { found_it=FALSE; log_msg(4, "OK, I was wrong, I haven't found it... yet."); } 985 } 986 987 if (!found_it) 988 { log_msg(4, "OK, approach 2"); if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/scd0"))) 989 { if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/sr0" ))) 990 { if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/cdrom"))) 991 { if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/cdrom0"))) 992 { if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/cdrom1"))) 993 { if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/sr1"))) 994 { if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/dvd"))) 995 { if (!(found_it = set_dev_to_this_if_rx_OK(output, g_cdrw_drive_is_here))) 996 { retval=1; goto end_of_find_cdrom_device; } 997 } } } } } } } } 998 #endif 999 1000 if (found_it && try_to_mount) 1001 { 1002 if (mount_CDROM_here (output, mountpoint)) 1003 { 1004 log_msg(4, "[Cardigans] I've changed my mind"); 1005 found_it = FALSE; 1006 } 1007 else 1008 { 1009 sprintf(tmp, "%s/archives", mountpoint); 1010 if (!does_file_exist(tmp)) 1011 { 1012 log_msg(4, "[Cardigans] I'll take it back"); 1013 found_it = FALSE; 1014 } 1015 else 1016 { 1017 sprintf(command, "umount %s", output); 1018 paranoid_system(command); 1019 log_msg(4, "I'm confident the Mondo CD is in %s", output); 1020 } 1021 } 1022 } 1023 unlink(mountpoint); 1024 1025 if (found_it) 1026 { 1027 if (!does_file_exist(output)) { log_msg(3, "I still haven't found it."); return(1); } 1028 log_msg(3, "(find_cdrom_device) --> '%s'", output); 1029 strcpy(the_last_place_i_found_it, output); 1030 strcpy(g_cdrom_drive_is_here, output); 1031 retval=0; 1032 goto end_of_find_cdrom_device; 1033 } 1034 1035 sprintf(command, "%s -scanbus | grep \"[0-9],[0-9],[0-9]\" | grep \"[D|C][V|D]\" | grep -n \"\" | grep \"%s\" | cut -d':' -f2", cdr_exe, g_cdrw_drive_is_here); 1036 log_msg(1, "command=%s", command); 1037 strcpy(tmp, call_program_and_get_last_line_of_output(command)); 1038 if (tmp[0]) 1039 { 1040 strcpy(output, tmp); 1041 log_msg(4, "Finally found it at %s", output); 1042 retval=0; 1043 goto end_of_find_cdrom_device; 1044 } 1045 else 1046 { 1047 log_msg(4, "Still couldn't find it."); 1048 retval=1; 1049 goto end_of_find_cdrom_device; 1050 } 1051 end_of_find_cdrom_device: 1052 paranoid_free(tmp); 1053 paranoid_free(cdr_exe); 1054 paranoid_free(phrase_one); 1055 paranoid_free(phrase_two); 1056 paranoid_free(command); 1057 paranoid_free(dvd_last_resort); 1058 paranoid_free(mountpoint); 1059 return(retval); 1060 } 1061 1062 1063 1064 1065 1066 int find_dvd_device(char*output, bool try_to_mount) 1067 { 1068 char *command; 1069 char *tmp; 1070 int retval=0, devno=-1; 1071 1072 malloc_string(command); 1073 malloc_string(tmp); 1074 1075 if (g_dvd_drive_is_here[0]) 1076 { 1077 strcpy(output, g_dvd_drive_is_here); 1078 log_msg(3, "Been there, done that. Returning %s", output); 1079 return(0); 1080 } 1081 1082 sprintf(tmp, call_program_and_get_last_line_of_output 1083 ( 1084 "dvdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1" 1085 ) 980 if (!found_it && strlen(dvd_last_resort) > 0) { 981 log_msg(4, "Well, I'll use the DVD - %s - as a last resort", 982 dvd_last_resort); 983 strcpy(output, dvd_last_resort); 984 found_it = TRUE; 985 } 986 if (found_it) { 987 sprintf(tmp, "grep \"%s=ide-scsi\" /proc/cmdline &> /dev/null", 988 strrchr(output, '/') + 1); 989 if (system(tmp) == 0) { 990 log_msg(4, 991 "%s is not right. It's being SCSI-emulated. Continuing.", 992 output); 993 found_it = FALSE; 994 output[0] = '\0'; 995 } 996 } 997 998 if (found_it) { 999 log_msg(4, "(find_cdrom_device) --> '%s'", output); 1000 if (!does_device_exist(output)) { 1001 found_it = FALSE; 1002 log_msg(4, "OK, I was wrong, I haven't found it... yet."); 1003 } 1004 } 1005 1006 if (!found_it) { 1007 log_msg(4, "OK, approach 2"); 1008 if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/scd0"))) { 1009 if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/sr0"))) { 1010 if (! 1011 (found_it = 1012 set_dev_to_this_if_rx_OK(output, "/dev/cdrom"))) { 1013 if (! 1014 (found_it = 1015 set_dev_to_this_if_rx_OK(output, 1016 "/dev/cdrom0"))) { 1017 if (! 1018 (found_it = 1019 set_dev_to_this_if_rx_OK(output, 1020 "/dev/cdrom1"))) { 1021 if (! 1022 (found_it = 1023 set_dev_to_this_if_rx_OK(output, 1024 "/dev/sr1"))) { 1025 if (! 1026 (found_it = 1027 set_dev_to_this_if_rx_OK(output, 1028 "/dev/dvd"))) 1029 { 1030 if (! 1031 (found_it = 1032 set_dev_to_this_if_rx_OK(output, 1033 g_cdrw_drive_is_here))) 1034 { 1035 retval = 1; 1036 goto end_of_find_cdrom_device; 1037 } 1038 } 1039 } 1040 } 1041 } 1042 } 1043 } 1044 } 1045 } 1046 #endif 1047 1048 if (found_it && try_to_mount) { 1049 if (mount_CDROM_here(output, mountpoint)) { 1050 log_msg(4, "[Cardigans] I've changed my mind"); 1051 found_it = FALSE; 1052 } else { 1053 sprintf(tmp, "%s/archives", mountpoint); 1054 if (!does_file_exist(tmp)) { 1055 log_msg(4, "[Cardigans] I'll take it back"); 1056 found_it = FALSE; 1057 } else { 1058 sprintf(command, "umount %s", output); 1059 paranoid_system(command); 1060 log_msg(4, "I'm confident the Mondo CD is in %s", output); 1061 } 1062 } 1063 } 1064 unlink(mountpoint); 1065 1066 if (found_it) { 1067 if (!does_file_exist(output)) { 1068 log_msg(3, "I still haven't found it."); 1069 return (1); 1070 } 1071 log_msg(3, "(find_cdrom_device) --> '%s'", output); 1072 strcpy(the_last_place_i_found_it, output); 1073 strcpy(g_cdrom_drive_is_here, output); 1074 retval = 0; 1075 goto end_of_find_cdrom_device; 1076 } 1077 1078 sprintf(command, 1079 "%s -scanbus | grep \"[0-9],[0-9],[0-9]\" | grep \"[D|C][V|D]\" | grep -n \"\" | grep \"%s\" | cut -d':' -f2", 1080 cdr_exe, g_cdrw_drive_is_here); 1081 log_msg(1, "command=%s", command); 1082 strcpy(tmp, call_program_and_get_last_line_of_output(command)); 1083 if (tmp[0]) { 1084 strcpy(output, tmp); 1085 log_msg(4, "Finally found it at %s", output); 1086 retval = 0; 1087 goto end_of_find_cdrom_device; 1088 } else { 1089 log_msg(4, "Still couldn't find it."); 1090 retval = 1; 1091 goto end_of_find_cdrom_device; 1092 } 1093 end_of_find_cdrom_device: 1094 paranoid_free(tmp); 1095 paranoid_free(cdr_exe); 1096 paranoid_free(phrase_one); 1097 paranoid_free(phrase_two); 1098 paranoid_free(command); 1099 paranoid_free(dvd_last_resort); 1100 paranoid_free(mountpoint); 1101 return (retval); 1102 } 1103 1104 1105 1106 1107 1108 int find_dvd_device(char *output, bool try_to_mount) 1109 { 1110 char *command; 1111 char *tmp; 1112 int retval = 0, devno = -1; 1113 1114 malloc_string(command); 1115 malloc_string(tmp); 1116 1117 if (g_dvd_drive_is_here[0]) { 1118 strcpy(output, g_dvd_drive_is_here); 1119 log_msg(3, "Been there, done that. Returning %s", output); 1120 return (0); 1121 } 1122 1123 sprintf(tmp, call_program_and_get_last_line_of_output 1124 ("dvdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1") 1086 1125 ); 1087 log_msg(5, "tmp = '%s'", tmp); 1088 if (!tmp[0]) sprintf (tmp, call_program_and_get_last_line_of_output 1089 ( 1090 "cdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1" 1091 ) 1092 ); 1093 if (tmp[0]) 1094 { 1095 devno=atoi(tmp) - 1; 1096 } 1097 if (devno>=0) 1098 { 1099 retval=0; 1100 sprintf(output, "/dev/scd%d", devno); 1101 strcpy(g_dvd_drive_is_here, output); 1102 log_msg(2, "I think DVD is at %s", output); 1103 } 1104 else 1105 { 1106 log_msg(2,"I cannot find DVD"); 1107 retval=1; 1108 } 1109 1110 if (try_to_mount) 1111 { 1112 log_msg(1, "Ignoring the fact that try_to_mount==TRUE"); 1113 } 1114 return(retval); 1126 log_msg(5, "tmp = '%s'", tmp); 1127 if (!tmp[0]) 1128 sprintf(tmp, call_program_and_get_last_line_of_output 1129 ("cdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1") 1130 ); 1131 if (tmp[0]) { 1132 devno = atoi(tmp) - 1; 1133 } 1134 if (devno >= 0) { 1135 retval = 0; 1136 sprintf(output, "/dev/scd%d", devno); 1137 strcpy(g_dvd_drive_is_here, output); 1138 log_msg(2, "I think DVD is at %s", output); 1139 } else { 1140 log_msg(2, "I cannot find DVD"); 1141 retval = 1; 1142 } 1143 1144 if (try_to_mount) { 1145 log_msg(1, "Ignoring the fact that try_to_mount==TRUE"); 1146 } 1147 return (retval); 1115 1148 } 1116 1149 … … 1127 1160 * @return size in megabytes. 1128 1161 */ 1129 long 1130 get_phys_size_of_drive (char *drive) 1131 { 1132 int fd; 1162 long get_phys_size_of_drive(char *drive) 1163 { 1164 int fd; 1133 1165 #if linux 1134 unsigned long long s=0;1135 int fileid, cylinders =0, cylindersleft=0;1136 int cylindersize =0;1166 unsigned long long s = 0; 1167 int fileid, cylinders = 0, cylindersleft = 0; 1168 int cylindersize = 0; 1137 1169 int gotgeo = 0; 1138 1170 1139 1140 1171 1172 struct hd_geometry hdgeo; 1141 1173 #elif __FreeBSD__ 1142 1143 #endif 1144 1145 long outvalA=-1;1146 long outvalB=-1;1147 long outvalC=-1;1148 1149 if ((fd = open(drive, O_RDONLY)) != -1) {1150 if (ioctl(fd,1174 off_t s; 1175 #endif 1176 1177 long outvalA = -1; 1178 long outvalB = -1; 1179 long outvalC = -1; 1180 1181 if ((fd = open(drive, O_RDONLY)) != -1) { 1182 if (ioctl(fd, 1151 1183 #if linux 1152 1153 1154 1155 1156 1184 #ifdef BLKGETSIZE64 1185 BLKGETSIZE64, 1186 #else 1187 BLKGETSIZE, 1188 #endif 1157 1189 #elif __FreeBSD__ 1158 1159 #endif 1160 1161 close(fd);1162 1163 1190 DIOCGMEDIASIZE, 1191 #endif 1192 &s) != -1) { 1193 close(fd); 1194 // s>>11 works for older disks but not for newer ones 1195 outvalB = 1164 1196 #if linux 1165 #ifdef BLKGETSIZE64 1166 s >> 20 1167 #else 1168 s >> 11 1169 #endif 1197 #ifdef BLKGETSIZE64 1198 s >> 20 1170 1199 #else 1171 s >> 20 1172 #endif 1173 ; 1174 } 1175 } 1176 1177 if (outvalB<=0) 1178 { log_msg (1, "Error getting size of %s: %s", drive, strerror (errno)); } 1200 s >> 11 1201 #endif 1202 #else 1203 s >> 20 1204 #endif 1205 ; 1206 } 1207 } 1208 1209 if (outvalB <= 0) { 1210 log_msg(1, "Error getting size of %s: %s", drive, strerror(errno)); 1211 } 1179 1212 #if linux 1180 fileid = open(drive, O_RDONLY);1213 fileid = open(drive, O_RDONLY); 1181 1214 if (fileid) { 1182 if (ioctl 1215 if (ioctl(fileid, HDIO_GETGEO, &hdgeo) != -1) { 1183 1216 if (hdgeo.cylinders && hdgeo.heads && hdgeo.sectors) { 1184 1217 cylindersleft = cylinders = hdgeo.cylinders; 1185 1218 cylindersize = hdgeo.heads * hdgeo.sectors / 2; 1186 1219 outvalA = cylindersize * cylinders / 1024; 1187 log_msg (2, "Got Harddisk geometry, C:%d, H:%d, S:%d", hdgeo.cylinders, hdgeo.heads, hdgeo.sectors); 1220 log_msg(2, "Got Harddisk geometry, C:%d, H:%d, S:%d", 1221 hdgeo.cylinders, hdgeo.heads, hdgeo.sectors); 1188 1222 gotgeo = 1; 1189 1223 } else { 1190 log_msg 1224 log_msg(1, "Harddisk geometry wrong"); 1191 1225 } 1192 1226 } else { 1193 log_msg (1, "Error in ioctl() getting new hard disk geometry (%s), resizing in unsafe mode", strerror(errno)); 1227 log_msg(1, 1228 "Error in ioctl() getting new hard disk geometry (%s), resizing in unsafe mode", 1229 strerror(errno)); 1194 1230 } 1195 1231 close(fileid); 1196 1232 } else { 1197 log_msg (1, "Failed to open %s for reading: %s", drive, strerror(errno)); 1233 log_msg(1, "Failed to open %s for reading: %s", drive, 1234 strerror(errno)); 1198 1235 } 1199 1236 if (!gotgeo) { 1200 log_msg 1237 log_msg(1, "Failed to get harddisk geometry, using old mode"); 1201 1238 } 1202 1239 /* … … 1216 1253 // NEWER DISKS will give sane value for outvalB (close to outvalA, in other words) :) 1217 1254 1218 1219 1255 outvalC = (outvalA > outvalB) ? outvalA : outvalB; 1256 1220 1257 // log_msg (5, "drive = %s, error = %s", drive, strerror (errno)); 1221 1258 // fatal_error ("GPSOD: Unable to get size of drive"); 1222 log_msg(1, "%s --> %ld or %ld --> %ld", drive, outvalA, outvalB, outvalC); 1223 1224 return(outvalC); 1225 } 1226 1259 log_msg(1, "%s --> %ld or %ld --> %ld", drive, outvalA, outvalB, 1260 outvalC); 1261 1262 return (outvalC); 1263 } 1264 1227 1265 /* The old version */ 1228 #if 0 1229 long 1230 get_phys_size_of_drive (char *drive) 1231 { 1232 /*@ pointers *****************************************************/ 1266 #if 0 1267 long get_phys_size_of_drive(char *drive) 1268 { 1269 /*@ pointers **************************************************** */ 1233 1270 #if linux 1234 1235 1236 1237 1238 /*@ buffers ***************************************************** */1239 1240 1241 1242 /*@ long ******************************************************** */1243 1244 1245 1246 1247 1248 #endif 1249 1250 1251 1271 FILE *fin; 1272 char *p; 1273 char *q; 1274 char *r; 1275 /*@ buffers ***************************************************** */ 1276 char *tmp; 1277 char *command; 1278 1279 /*@ long ******************************************************** */ 1280 long outL; 1281 long tempLa; 1282 long tempLb; 1283 long tempLc; 1284 1285 #endif 1286 1287 struct hd_geometry hdgeo; 1288 int fd; 1252 1289 1253 1290 #ifdef __FreeBSD__ 1254 1255 1256 if ((fd = open(drive, O_RDONLY)) != -1) {1257 if (ioctl(fd, DIOCGMEDIASIZE, &o) != -1) {1258 close(fd);1259 1260 1261 close(fd);1262 1263 log_msg (4, "drive = %s, error = %s", drive, strerror(errno));1264 fatal_error("GPSOD: Unable to get size of drive");1291 off_t o; 1292 1293 if ((fd = open(drive, O_RDONLY)) != -1) { 1294 if (ioctl(fd, DIOCGMEDIASIZE, &o) != -1) { 1295 close(fd); 1296 return (long) (o / (off_t) (1024 * 1024)); 1297 } 1298 close(fd); 1299 } 1300 log_msg(4, "drive = %s, error = %s", drive, strerror(errno)); 1301 fatal_error("GPSOD: Unable to get size of drive"); 1265 1302 #else 1266 1303 1267 malloc_string(tmp); 1268 malloc_string(command); 1269 1270 if ((fd = open (drive, O_RDONLY)) != -1) { 1271 if (ioctl (fd, HDIO_GETGEO, &hdgeo) != -1) { 1272 close (fd); 1273 log_msg (2, "Geometry of drive %s is C:%d, H:%d, S%d, its size is %d MB", drive, hdgeo.cylinders, hdgeo.heads, hdgeo.sectors, (hdgeo.cylinders * hdgeo.heads * hdgeo.sectors / 2 / 1024)); 1274 if ( hdgeo.cylinders && hdgeo.heads && hdgeo.sectors ) { 1275 return ((long) (hdgeo.cylinders * hdgeo.heads * hdgeo.sectors / 2 / 1024)); 1276 } 1277 } 1278 close (fd); 1279 } 1280 1281 assert_string_is_neither_NULL_nor_zerolength(drive); 1282 1283 sprintf (command, 1284 "parted2fdisk -l %s | head -n4 | tr -s '\n' '\t' | tr -s ' ' '\t' | cut -f8,14,16", 1285 drive); 1286 strcpy (tmp, call_program_and_get_last_line_of_output (command)); 1287 if (tmp[0]) 1288 { 1289 p = tmp; 1290 q = strchr (p, ' '); 1291 if (q) 1292 { 1293 *(q++) = '\0'; 1294 r = strchr (q, ' '); 1295 if (r) 1296 { 1297 *(r++) = '\0'; 1298 tempLa = atol (p); 1299 tempLb = atol (q); 1300 tempLc = atol (r); 1301 outL = tempLa * tempLb / 1024 * tempLc / 1024; 1302 if (outL > 100) 1303 { 1304 paranoid_free(tmp); 1305 paranoid_free(command); 1306 return (outL); 1307 } 1308 } 1309 } 1310 } 1311 1312 /* try to grep for 'Drive xxxx: yyy MB' */ 1313 sprintf(command, "parted2fdisk -l %s | grep MB | tr -s ' ' '\t' | cut -f3", drive); 1314 strcpy(tmp, call_program_and_get_last_line_of_output(command)); 1315 if (atol(tmp) > 0) 1316 { 1317 paranoid_free(tmp); 1318 paranoid_free(command); 1319 return (atol(tmp)); 1320 } 1321 1322 /* else, do it the old-fashioned way */ 1323 p = strrchr (drive, (int) '/'); 1324 if (p) 1325 { 1326 strcpy (tmp, p + 1); 1327 } 1328 else 1329 { 1330 paranoid_free(tmp); 1331 paranoid_free(command); 1332 return (-1); 1333 } 1334 sprintf (command, "dmesg | grep %s 2> /dev/null", tmp); 1335 if (!(fin = popen (command, "r"))) 1336 { log_OS_error("Cannot popen dmesg command"); } 1337 else 1338 { 1339 fgets (tmp, MAX_STR_LEN - 1, fin); 1340 while (!feof (fin) && !strstr (tmp, "GB") && !strstr (tmp, "MB")) 1341 { 1342 fgets (tmp, MAX_STR_LEN - 1, fin); 1343 } 1344 if (pclose (fin)) { log_OS_error("Cannot pclose dmesg fin"); } 1345 } 1346 if (!(p = strstr (tmp, "GB")) && !(p = strstr (tmp, "MB"))) 1347 { 1348 log_msg (3, "Cannot find %s's size: dmesg isn't helping either.", 1349 drive); 1350 paranoid_free(tmp); 1351 paranoid_free(command); 1352 return (-1); 1353 } 1354 for (; !isdigit (*(p - 1)); p--); 1355 *p = '\0'; 1356 for (p--; isdigit (*(p - 1)); p--); 1357 outL = atol (p); 1358 if (outL <= 0) 1359 { 1360 paranoid_free(tmp); 1361 paranoid_free(command); 1362 return (-1); 1363 } 1364 if (strstr (tmp, "GB")) 1365 { 1366 outL = outL * 1024; 1367 } 1368 paranoid_free(tmp); 1369 paranoid_free(command); 1370 return (outL * 19 / 20); 1371 #endif 1372 } 1373 #endif /* 0 */ 1304 malloc_string(tmp); 1305 malloc_string(command); 1306 1307 if ((fd = open(drive, O_RDONLY)) != -1) { 1308 if (ioctl(fd, HDIO_GETGEO, &hdgeo) != -1) { 1309 close(fd); 1310 log_msg(2, 1311 "Geometry of drive %s is C:%d, H:%d, S%d, its size is %d MB", 1312 drive, hdgeo.cylinders, hdgeo.heads, hdgeo.sectors, 1313 (hdgeo.cylinders * hdgeo.heads * hdgeo.sectors / 2 / 1314 1024)); 1315 if (hdgeo.cylinders && hdgeo.heads && hdgeo.sectors) { 1316 return ((long) 1317 (hdgeo.cylinders * hdgeo.heads * hdgeo.sectors / 1318 2 / 1024)); 1319 } 1320 } 1321 close(fd); 1322 } 1323 1324 assert_string_is_neither_NULL_nor_zerolength(drive); 1325 1326 sprintf(command, 1327 "parted2fdisk -l %s | head -n4 | tr -s '\n' '\t' | tr -s ' ' '\t' | cut -f8,14,16", 1328 drive); 1329 strcpy(tmp, call_program_and_get_last_line_of_output(command)); 1330 if (tmp[0]) { 1331 p = tmp; 1332 q = strchr(p, ' '); 1333 if (q) { 1334 *(q++) = '\0'; 1335 r = strchr(q, ' '); 1336 if (r) { 1337 *(r++) = '\0'; 1338 tempLa = atol(p); 1339 tempLb = atol(q); 1340 tempLc = atol(r); 1341 outL = tempLa * tempLb / 1024 * tempLc / 1024; 1342 if (outL > 100) { 1343 paranoid_free(tmp); 1344 paranoid_free(command); 1345 return (outL); 1346 } 1347 } 1348 } 1349 } 1350 1351 /* try to grep for 'Drive xxxx: yyy MB' */ 1352 sprintf(command, 1353 "parted2fdisk -l %s | grep MB | tr -s ' ' '\t' | cut -f3", 1354 drive); 1355 strcpy(tmp, call_program_and_get_last_line_of_output(command)); 1356 if (atol(tmp) > 0) { 1357 paranoid_free(tmp); 1358 paranoid_free(command); 1359 return (atol(tmp)); 1360 } 1361 1362 /* else, do it the old-fashioned way */ 1363 p = strrchr(drive, (int) '/'); 1364 if (p) { 1365 strcpy(tmp, p + 1); 1366 } else { 1367 paranoid_free(tmp); 1368 paranoid_free(command); 1369 return (-1); 1370 } 1371 sprintf(command, "dmesg | grep %s 2> /dev/null", tmp); 1372 if (!(fin = popen(command, "r"))) { 1373 log_OS_error("Cannot popen dmesg command"); 1374 } else { 1375 fgets(tmp, MAX_STR_LEN - 1, fin); 1376 while (!feof(fin) && !strstr(tmp, "GB") && !strstr(tmp, "MB")) { 1377 fgets(tmp, MAX_STR_LEN - 1, fin); 1378 } 1379 if (pclose(fin)) { 1380 log_OS_error("Cannot pclose dmesg fin"); 1381 } 1382 } 1383 if (!(p = strstr(tmp, "GB")) && !(p = strstr(tmp, "MB"))) { 1384 log_msg(3, "Cannot find %s's size: dmesg isn't helping either.", 1385 drive); 1386 paranoid_free(tmp); 1387 paranoid_free(command); 1388 return (-1); 1389 } 1390 for (; !isdigit(*(p - 1)); p--); 1391 *p = '\0'; 1392 for (p--; isdigit(*(p - 1)); p--); 1393 outL = atol(p); 1394 if (outL <= 0) { 1395 paranoid_free(tmp); 1396 paranoid_free(command); 1397 return (-1); 1398 } 1399 if (strstr(tmp, "GB")) { 1400 outL = outL * 1024; 1401 } 1402 paranoid_free(tmp); 1403 paranoid_free(command); 1404 return (outL * 19 / 20); 1405 #endif 1406 } 1407 #endif /* 0 */ 1374 1408 1375 1409 … … 1383 1417 * @return TRUE if the format is supported, FALSE if not. 1384 1418 */ 1385 bool 1386 is_this_a_valid_disk_format (char *format) 1387 { 1388 char *good_formats; 1419 bool is_this_a_valid_disk_format(char *format) 1420 { 1421 char *good_formats; 1389 1422 char *command; 1390 1423 char *format_sz; 1391 1424 1392 1393 1394 1395 1396 1397 1398 1399 1400 sprintf(format_sz, "%s ", format);1425 FILE *pin; 1426 int retval; 1427 malloc_string(good_formats); 1428 malloc_string(command); 1429 malloc_string(format_sz); 1430 1431 assert_string_is_neither_NULL_nor_zerolength(format); 1432 1433 sprintf(format_sz, "%s ", format); 1401 1434 1402 1435 #ifdef __FreeBSD__ 1403 sprintf(command,1404 1436 sprintf(command, 1437 "lsvfs | tr -s '\t' ' ' | grep -v Filesys | grep -v -- -- | cut -d' ' -f1 | tr -s '\n' ' '"); 1405 1438 #else 1406 sprintf (command, 1407 "cat /proc/filesystems | grep -v nodev | tr -s '\t' ' ' | cut -d' ' -f2 | tr -s '\n' ' '"); 1408 #endif 1409 1410 pin = popen (command, "r"); 1411 if (!pin) 1412 { 1413 log_OS_error ("Unable to read good formats"); 1414 retval=0; 1415 } 1416 else 1417 { 1418 strcpy (good_formats, " "); 1419 (void) fgets (good_formats + 1, MAX_STR_LEN, pin); 1420 if (pclose (pin)) 1421 { log_OS_error("Cannot pclose good formats"); } 1422 strip_spaces (good_formats); 1423 strcat (good_formats, " swap lvm raid ntfs 7 "); // " ntfs 7 " -- um, cheating much? :) 1424 if (strstr (good_formats, format_sz)) 1425 { 1426 retval=1; 1427 } 1428 else 1429 { 1430 retval=0; 1431 } 1432 } 1433 paranoid_free(good_formats); 1434 paranoid_free(command); 1435 paranoid_free(format_sz); 1436 return (retval); 1439 sprintf(command, 1440 "cat /proc/filesystems | grep -v nodev | tr -s '\t' ' ' | cut -d' ' -f2 | tr -s '\n' ' '"); 1441 #endif 1442 1443 pin = popen(command, "r"); 1444 if (!pin) { 1445 log_OS_error("Unable to read good formats"); 1446 retval = 0; 1447 } else { 1448 strcpy(good_formats, " "); 1449 (void) fgets(good_formats + 1, MAX_STR_LEN, pin); 1450 if (pclose(pin)) { 1451 log_OS_error("Cannot pclose good formats"); 1452 } 1453 strip_spaces(good_formats); 1454 strcat(good_formats, " swap lvm raid ntfs 7 "); // " ntfs 7 " -- um, cheating much? :) 1455 if (strstr(good_formats, format_sz)) { 1456 retval = 1; 1457 } else { 1458 retval = 0; 1459 } 1460 } 1461 paranoid_free(good_formats); 1462 paranoid_free(command); 1463 paranoid_free(format_sz); 1464 return (retval); 1437 1465 } 1438 1466 … … 1445 1473 * @return TRUE if it's mounted, FALSE if not. 1446 1474 */ 1447 bool is_this_device_mounted 1448 { 1449 1450 /*@ pointers **************************************************** */1451 1452 1453 /*@ buffers ***************************************************** */1454 1455 1456 1457 1458 int retval=0;1459 1475 bool is_this_device_mounted(char *device_raw) 1476 { 1477 1478 /*@ pointers **************************************************** */ 1479 FILE *fin; 1480 1481 /*@ buffers ***************************************************** */ 1482 char *incoming; 1483 char *device_with_tab; 1484 char *device_with_space; 1485 char *tmp; 1486 int retval = 0; 1487 1460 1488 #ifdef __FreeBSD__ 1461 1489 #define SWAPLIST_COMMAND "swapinfo" 1462 1490 #else 1463 1464 #endif 1465 1466 /*@ end vars **************************************************** */1467 1468 1469 1470 1471 1472 assert(device_raw!=NULL);1491 #define SWAPLIST_COMMAND "cat /proc/swaps" 1492 #endif 1493 1494 /*@ end vars **************************************************** */ 1495 1496 malloc_string(incoming); 1497 malloc_string(device_with_tab); 1498 malloc_string(device_with_space); 1499 malloc_string(tmp); 1500 assert(device_raw != NULL); 1473 1501 // assert_string_is_neither_NULL_nor_zerolength(device_raw); 1474 if (device_raw[0] != '/' && !strstr(device_raw, ":/")) 1475 { 1476 log_msg(1, "%s needs to have a '/' prefixed - I'll do it", device_raw); 1477 sprintf(tmp, "/%s", device_raw); 1478 } 1479 else 1480 { 1481 strcpy(tmp, device_raw); 1482 } 1483 log_msg(1, "Is %s mounted?", tmp); 1484 if (!strcmp(tmp, "/proc") || !strcmp(tmp, "proc")) 1485 { 1486 log_msg(1, "I don't know how the heck /proc made it into the mountlist. I'll ignore it."); 1487 return(0); 1488 } 1489 sprintf (device_with_tab, "%s\t", tmp); 1490 sprintf (device_with_space, "%s ", tmp); 1491 1492 if (!(fin = popen ("mount", "r"))) { log_OS_error("Cannot popen 'mount'"); return(FALSE); } 1493 for (fgets (incoming, MAX_STR_LEN - 1, fin); !feof (fin); 1494 fgets (incoming, MAX_STR_LEN - 1, fin)) 1495 { 1496 if ( strstr (incoming, device_with_space) //> incoming 1497 || strstr (incoming, device_with_tab) ) // > incoming) 1498 { 1499 paranoid_pclose (fin); 1500 retval=1; 1501 goto end_of_func; 1502 } 1503 } 1504 paranoid_pclose(fin); 1505 sprintf (tmp, "%s | grep -w \"%s\" > /dev/null 2> /dev/null", 1506 SWAPLIST_COMMAND, device_with_space); 1507 log_msg(4, "tmp (command) = '%s'", tmp); 1508 if (!system (tmp)) 1509 { 1510 retval=1; 1511 goto end_of_func; 1512 } 1513 end_of_func: 1514 paranoid_free(incoming); 1515 paranoid_free(device_with_tab); 1516 paranoid_free(device_with_space); 1517 paranoid_free(tmp); 1518 return (retval); 1502 if (device_raw[0] != '/' && !strstr(device_raw, ":/")) { 1503 log_msg(1, "%s needs to have a '/' prefixed - I'll do it", 1504 device_raw); 1505 sprintf(tmp, "/%s", device_raw); 1506 } else { 1507 strcpy(tmp, device_raw); 1508 } 1509 log_msg(1, "Is %s mounted?", tmp); 1510 if (!strcmp(tmp, "/proc") || !strcmp(tmp, "proc")) { 1511 log_msg(1, 1512 "I don't know how the heck /proc made it into the mountlist. I'll ignore it."); 1513 return (0); 1514 } 1515 sprintf(device_with_tab, "%s\t", tmp); 1516 sprintf(device_with_space, "%s ", tmp); 1517 1518 if (!(fin = popen("mount", "r"))) { 1519 log_OS_error("Cannot popen 'mount'"); 1520 return (FALSE); 1521 } 1522 for (fgets(incoming, MAX_STR_LEN - 1, fin); !feof(fin); 1523 fgets(incoming, MAX_STR_LEN - 1, fin)) { 1524 if (strstr(incoming, device_with_space) //> incoming 1525 || strstr(incoming, device_with_tab)) // > incoming) 1526 { 1527 paranoid_pclose(fin); 1528 retval = 1; 1529 goto end_of_func; 1530 } 1531 } 1532 paranoid_pclose(fin); 1533 sprintf(tmp, "%s | grep -w \"%s\" > /dev/null 2> /dev/null", 1534 SWAPLIST_COMMAND, device_with_space); 1535 log_msg(4, "tmp (command) = '%s'", tmp); 1536 if (!system(tmp)) { 1537 retval = 1; 1538 goto end_of_func; 1539 } 1540 end_of_func: 1541 paranoid_free(incoming); 1542 paranoid_free(device_with_tab); 1543 paranoid_free(device_with_space); 1544 paranoid_free(tmp); 1545 return (retval); 1519 1546 } 1520 1547 … … 1526 1553 * @return /dev entry for the device, or NULL if it couldn't be allocated. 1527 1554 */ 1528 char * 1529 make_vn (char *fname) 1530 { 1531 char *device = (char*) malloc (MAX_STR_LEN);1532 char *mddevice = (char*) malloc (32);1533 char command[MAX_STR_LEN];1534 int vndev = 2; 1535 if (atoi (call_program_and_get_last_line_of_output ("/sbin/sysctl -n kern.osreldate")) < 1536 1537 1538 sprintf(mddevice, "vn%ic", vndev++);1539 sprintf(command, "vnconfig %s %s", mddevice, fname);1540 1541 1542 1543 1544 while (system(command));1545 1546 sprintf(command, "mdconfig -a -t vnode -f %s", fname);1547 mddevice = call_program_and_get_last_line_of_output(command);1548 if (!strstr(mddevice, "md")) {1549 1550 1551 1552 sprintf(device, "/dev/%s", mddevice);1553 1555 char *make_vn(char *fname) 1556 { 1557 char *device = (char *) malloc(MAX_STR_LEN); 1558 char *mddevice = (char *) malloc(32); 1559 char command[MAX_STR_LEN]; 1560 int vndev = 2; 1561 if (atoi 1562 (call_program_and_get_last_line_of_output 1563 ("/sbin/sysctl -n kern.osreldate")) < 500000) { 1564 do { 1565 sprintf(mddevice, "vn%ic", vndev++); 1566 sprintf(command, "vnconfig %s %s", mddevice, fname); 1567 if (vndev > 10) { 1568 return NULL; 1569 } 1570 } 1571 while (system(command)); 1572 } else { 1573 sprintf(command, "mdconfig -a -t vnode -f %s", fname); 1574 mddevice = call_program_and_get_last_line_of_output(command); 1575 if (!strstr(mddevice, "md")) { 1576 return NULL; 1577 } 1578 } 1579 sprintf(device, "/dev/%s", mddevice); 1580 return device; 1554 1581 } 1555 1582 … … 1564 1591 * @return 0 for success, nonzero for failure. 1565 1592 */ 1566 int 1567 kick_vn (char *dname) 1568 { 1569 char command[MAX_STR_LEN]; 1570 1571 if (strncmp (dname, "/dev/", 5) == 0) { 1572 dname += 5; 1573 } 1574 1575 if (atoi (call_program_and_get_last_line_of_output ("/sbin/sysctl -n kern.osreldate")) < 1576 500000) { 1577 sprintf (command, "vnconfig -d %s", dname); 1578 return system (command); 1579 } else { 1580 sprintf (command, "mdconfig -d -u %s", dname); 1581 return system (command); 1582 } 1583 /*NOTREACHED*/ 1584 return 255; 1593 int kick_vn(char *dname) 1594 { 1595 char command[MAX_STR_LEN]; 1596 1597 if (strncmp(dname, "/dev/", 5) == 0) { 1598 dname += 5; 1599 } 1600 1601 if (atoi 1602 (call_program_and_get_last_line_of_output 1603 ("/sbin/sysctl -n kern.osreldate")) < 500000) { 1604 sprintf(command, "vnconfig -d %s", dname); 1605 return system(command); 1606 } else { 1607 sprintf(command, "mdconfig -d -u %s", dname); 1608 return system(command); 1609 } 1610 /*NOTREACHED*/ return 255; 1585 1611 } 1586 1612 #endif … … 1593 1619 * @return 0 for success, nonzero for failure. 1594 1620 */ 1595 int 1596 mount_CDROM_here (char *device, char *mountpoint) 1597 { 1598 /*@ buffer *******************************************************/ 1599 char *command; 1600 char *dev; 1601 char *options; 1602 int retval; 1603 1604 malloc_string(command); 1605 malloc_string(dev); 1606 malloc_string(options); 1607 assert_string_is_neither_NULL_nor_zerolength(device); 1608 assert_string_is_neither_NULL_nor_zerolength(mountpoint); 1609 1610 make_hole_for_dir(mountpoint); 1611 strcpy(options, "ro"); 1612 if (isdigit(device[0])) 1613 { find_cdrom_device(device, FALSE); } 1614 else 1615 { strcpy(dev, device); } 1616 if (g_ISO_restore_mode) 1617 { 1621 int mount_CDROM_here(char *device, char *mountpoint) 1622 { 1623 /*@ buffer ****************************************************** */ 1624 char *command; 1625 char *dev; 1626 char *options; 1627 int retval; 1628 1629 malloc_string(command); 1630 malloc_string(dev); 1631 malloc_string(options); 1632 assert_string_is_neither_NULL_nor_zerolength(device); 1633 assert_string_is_neither_NULL_nor_zerolength(mountpoint); 1634 1635 make_hole_for_dir(mountpoint); 1636 strcpy(options, "ro"); 1637 if (isdigit(device[0])) { 1638 find_cdrom_device(device, FALSE); 1639 } else { 1640 strcpy(dev, device); 1641 } 1642 if (g_ISO_restore_mode) { 1618 1643 1619 1644 #ifdef __FreeBSD__ 1620 strcpy (dev, make_vn (device)); 1621 if (!dev) { 1622 sprintf (command, "Unable to mount ISO (make_vn(%s) failed)", device); 1623 fatal_error (command); 1624 } 1625 strcpy (device, dev); 1645 strcpy(dev, make_vn(device)); 1646 if (!dev) { 1647 sprintf(command, "Unable to mount ISO (make_vn(%s) failed)", 1648 device); 1649 fatal_error(command); 1650 } 1651 strcpy(device, dev); 1626 1652 #else 1627 strcat(options,",loop"); 1628 #endif 1629 1630 } 1631 log_msg(4, "(mount_CDROM_here --- device=%s, mountpoint=%s", device, mountpoint); 1632 /*@ end vars ****************************************************/ 1653 strcat(options, ",loop"); 1654 #endif 1655 1656 } 1657 log_msg(4, "(mount_CDROM_here --- device=%s, mountpoint=%s", device, 1658 mountpoint); 1659 /*@ end vars *************************************************** */ 1633 1660 1634 1661 #ifdef __FreeBSD__ 1635 sprintf(command, "mount_cd9660 -r %s %s 2>> %s",1636 1662 sprintf(command, "mount_cd9660 -r %s %s 2>> %s", 1663 device, mountpoint, MONDO_LOGFILE); 1637 1664 1638 1665 #else 1639 sprintf (command, "mount %s -o %s -t iso9660 %s 2>> %s", 1640 device, options, mountpoint, MONDO_LOGFILE); 1641 #endif 1642 1643 log_msg (4, command); 1644 if (strncmp(device, "/dev/", 5) == 0) 1645 { 1646 retract_CD_tray_and_defeat_autorun(); 1647 } 1648 retval = system (command); 1649 log_msg(1, "system(%s) returned %d", command, retval); 1650 1651 paranoid_free(command); 1652 paranoid_free(dev); 1653 paranoid_free(options); 1654 return(retval); 1666 sprintf(command, "mount %s -o %s -t iso9660 %s 2>> %s", 1667 device, options, mountpoint, MONDO_LOGFILE); 1668 #endif 1669 1670 log_msg(4, command); 1671 if (strncmp(device, "/dev/", 5) == 0) { 1672 retract_CD_tray_and_defeat_autorun(); 1673 } 1674 retval = system(command); 1675 log_msg(1, "system(%s) returned %d", command, retval); 1676 1677 paranoid_free(command); 1678 paranoid_free(dev); 1679 paranoid_free(options); 1680 return (retval); 1655 1681 } 1656 1682 … … 1672 1698 */ 1673 1699 void 1674 insist_on_this_cd_number (struct s_bkpinfo*bkpinfo, int cd_number_i_want)1675 { 1676 1677 /*@ int ************************************************************* */1678 1679 1680 1681 /*@ buffers ********************************************************* */1682 1683 1684 1685 assert(bkpinfo!=NULL);1686 1700 insist_on_this_cd_number(struct s_bkpinfo *bkpinfo, int cd_number_i_want) 1701 { 1702 1703 /*@ int ************************************************************* */ 1704 int res = 0; 1705 1706 1707 /*@ buffers ********************************************************* */ 1708 char *tmp; 1709 char *request; 1710 1711 assert(bkpinfo != NULL); 1712 assert(cd_number_i_want > 0); 1687 1713 1688 1714 // log_msg(3, "Insisting on CD number %d", cd_number_i_want); 1689 1715 1690 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) 1691 { 1692 log_msg(3,"No need to insist_on_this_cd_number when the backup type isn't CD-R(W) or NFS or ISO");1693 1694 1695 1696 1697 sprintf(tmp, "mkdir -p "MNT_CDROM);1698 1699 if (g_ISO_restore_mode || bkpinfo->backup_media_type == iso || bkpinfo->backup_media_type == nfs) 1700 1701 1702 1716 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) { 1717 log_msg(3, 1718 "No need to insist_on_this_cd_number when the backup type isn't CD-R(W) or NFS or ISO"); 1719 return; 1720 } 1721 malloc_string(tmp); 1722 malloc_string(request); 1723 sprintf(tmp, "mkdir -p " MNT_CDROM); 1724 run_program_and_log_output(tmp, 5); 1725 if (g_ISO_restore_mode || bkpinfo->backup_media_type == iso 1726 || bkpinfo->backup_media_type == nfs) { 1727 log_msg(3, "Remounting CD"); 1728 g_ISO_restore_mode = TRUE; 1703 1729 // FIXME --- I'm tempted to do something about this... 1704 1730 // Why unmount and remount again and again? 1705 if (is_this_device_mounted(MNT_CDROM)) { run_program_and_log_output("umount "MNT_CDROM, 5); } 1706 system("mkdir -p /tmp/isodir &> /dev/null"); 1707 sprintf(tmp, "%s/%s/%s-%d.iso", bkpinfo->isodir, bkpinfo->nfs_remote_dir, bkpinfo->prefix, cd_number_i_want); 1708 if (!does_file_exist(tmp)) 1709 { 1710 sprintf(tmp, "/tmp/isodir/%s/%s-%d.iso", bkpinfo->nfs_remote_dir, bkpinfo->prefix, cd_number_i_want); 1711 if (does_file_exist(tmp)) 1712 { 1713 log_msg(1, "FIXME - hacking bkpinfo->isodir from '%s' to /tmp/isodir", bkpinfo->isodir); 1714 strcpy(bkpinfo->isodir, "/tmp/isodir"); 1715 } 1716 } 1717 log_msg(3, "Mounting %s at %s", tmp, MNT_CDROM); 1718 if (mount_CDROM_here(tmp, MNT_CDROM)) { fatal_error("Mommy!"); } 1719 // g_current_media_number = cd_number_i_want; 1720 // return; 1721 } 1722 if ((res=what_number_cd_is_this (bkpinfo)) != cd_number_i_want) 1723 { 1724 log_msg(3, "Currently, we hold %d but we want %d", res, cd_number_i_want); 1725 sprintf (tmp, "Insisting on %s #%d", media_descriptor_string(bkpinfo->backup_media_type), cd_number_i_want); 1726 sprintf (request, "Please insert %s #%d and press Enter.", 1727 media_descriptor_string(bkpinfo->backup_media_type), 1728 cd_number_i_want); 1729 log_msg (3, tmp); 1730 while (what_number_cd_is_this (bkpinfo) != cd_number_i_want) 1731 { 1732 paranoid_system ("sync"); 1733 if (is_this_device_mounted (MNT_CDROM)) 1734 { 1735 res = run_program_and_log_output ("umount "MNT_CDROM, FALSE); 1736 } 1737 else 1738 { 1739 res = 0; 1740 } 1741 if (res) 1742 { 1743 log_to_screen ("WARNING - failed to unmount CD-ROM drive"); 1744 } 1745 if (!bkpinfo->please_dont_eject) 1746 { res = eject_device(bkpinfo->media_device); } 1747 else 1748 { res = 0; } 1749 if (res) 1750 { 1751 log_to_screen ("WARNING - failed to eject CD-ROM disk"); 1752 } 1753 popup_and_OK (request); 1754 if (!bkpinfo->please_dont_eject) 1755 { 1756 inject_device(bkpinfo->media_device); 1757 } 1758 paranoid_system ("sync"); 1759 } 1760 log_msg (1, "Thankyou. Proceeding..."); 1761 g_current_media_number = cd_number_i_want; 1762 } 1763 paranoid_free(tmp); 1764 paranoid_free(request); 1731 if (is_this_device_mounted(MNT_CDROM)) { 1732 run_program_and_log_output("umount " MNT_CDROM, 5); 1733 } 1734 system("mkdir -p /tmp/isodir &> /dev/null"); 1735 sprintf(tmp, "%s/%s/%s-%d.iso", bkpinfo->isodir, 1736 bkpinfo->nfs_remote_dir, bkpinfo->prefix, 1737 cd_number_i_want); 1738 if (!does_file_exist(tmp)) { 1739 sprintf(tmp, "/tmp/isodir/%s/%s-%d.iso", 1740 bkpinfo->nfs_remote_dir, bkpinfo->prefix, 1741 cd_number_i_want); 1742 if (does_file_exist(tmp)) { 1743 log_msg(1, 1744 "FIXME - hacking bkpinfo->isodir from '%s' to /tmp/isodir", 1745 bkpinfo->isodir); 1746 strcpy(bkpinfo->isodir, "/tmp/isodir"); 1747 } 1748 } 1749 log_msg(3, "Mounting %s at %s", tmp, MNT_CDROM); 1750 if (mount_CDROM_here(tmp, MNT_CDROM)) { 1751 fatal_error("Mommy!"); 1752 } 1753 // g_current_media_number = cd_number_i_want; 1754 // return; 1755 } 1756 if ((res = what_number_cd_is_this(bkpinfo)) != cd_number_i_want) { 1757 log_msg(3, "Currently, we hold %d but we want %d", res, 1758 cd_number_i_want); 1759 sprintf(tmp, "Insisting on %s #%d", 1760 media_descriptor_string(bkpinfo->backup_media_type), 1761 cd_number_i_want); 1762 sprintf(request, "Please insert %s #%d and press Enter.", 1763 media_descriptor_string(bkpinfo->backup_media_type), 1764 cd_number_i_want); 1765 log_msg(3, tmp); 1766 while (what_number_cd_is_this(bkpinfo) != cd_number_i_want) { 1767 paranoid_system("sync"); 1768 if (is_this_device_mounted(MNT_CDROM)) { 1769 res = 1770 run_program_and_log_output("umount " MNT_CDROM, FALSE); 1771 } else { 1772 res = 0; 1773 } 1774 if (res) { 1775 log_to_screen("WARNING - failed to unmount CD-ROM drive"); 1776 } 1777 if (!bkpinfo->please_dont_eject) { 1778 res = eject_device(bkpinfo->media_device); 1779 } else { 1780 res = 0; 1781 } 1782 if (res) { 1783 log_to_screen("WARNING - failed to eject CD-ROM disk"); 1784 } 1785 popup_and_OK(request); 1786 if (!bkpinfo->please_dont_eject) { 1787 inject_device(bkpinfo->media_device); 1788 } 1789 paranoid_system("sync"); 1790 } 1791 log_msg(1, "Thankyou. Proceeding..."); 1792 g_current_media_number = cd_number_i_want; 1793 } 1794 paranoid_free(tmp); 1795 paranoid_free(request); 1765 1796 } 1766 1797 … … 1781 1812 * @ingroup archiveGroup 1782 1813 */ 1783 int interactively_obtain_media_parameters_from_user(struct s_bkpinfo *bkpinfo, bool archiving_to_media) 1814 int interactively_obtain_media_parameters_from_user(struct s_bkpinfo 1815 *bkpinfo, 1816 bool 1817 archiving_to_media) 1784 1818 // archiving_to_media is TRUE if I'm being called by mondoarchive 1785 1819 // archiving_to_media is FALSE if I'm being called by mondorestore 1786 1820 { 1787 1788 1789 1790 1791 1792 1793 FILE*fin;1794 1795 1796 1797 1798 1799 1800 assert(bkpinfo!=NULL);1801 1802 1821 char *tmp; 1822 char *sz_size; 1823 char *command; 1824 char *comment; 1825 char *prompt; 1826 int i; 1827 FILE *fin; 1828 1829 malloc_string(tmp); 1830 malloc_string(sz_size); 1831 malloc_string(command); 1832 malloc_string(comment); 1833 malloc_string(prompt); 1834 assert(bkpinfo != NULL); 1835 sz_size[0] = '\0'; 1836 bkpinfo->nonbootable_backup = FALSE; 1803 1837 1804 1838 // Tape, CD, NFS, ...? 1805 srandom(getpid()); 1806 bkpinfo->backup_media_type = (g_restoring_live_from_cd) ? cdr : which_backup_media_type(bkpinfo->restore_data); 1807 if (bkpinfo->backup_media_type == none) 1808 { log_to_screen("User has chosen not to backup the PC"); finish(1); } 1809 if (bkpinfo->backup_media_type == tape && bkpinfo->restore_data) 1810 { popup_and_OK("Please remove CD/floppy from drive(s)"); } 1811 log_msg(3, "media type = %s", bkptype_to_string(bkpinfo->backup_media_type)); 1812 if (archiving_to_media) 1813 { 1814 sensibly_set_tmpdir_and_scratchdir(bkpinfo); 1815 } 1816 bkpinfo->cdrw_speed = (bkpinfo->backup_media_type == cdstream) ? 2 : 4; 1817 bkpinfo->compression_level = (bkpinfo->backup_media_type == cdstream) ? 1:5; 1818 bkpinfo->use_lzo = (bkpinfo->backup_media_type == cdstream) ? TRUE : FALSE; 1839 srandom(getpid()); 1840 bkpinfo->backup_media_type = 1841 (g_restoring_live_from_cd) ? cdr : 1842 which_backup_media_type(bkpinfo->restore_data); 1843 if (bkpinfo->backup_media_type == none) { 1844 log_to_screen("User has chosen not to backup the PC"); 1845 finish(1); 1846 } 1847 if (bkpinfo->backup_media_type == tape && bkpinfo->restore_data) { 1848 popup_and_OK("Please remove CD/floppy from drive(s)"); 1849 } 1850 log_msg(3, "media type = %s", 1851 bkptype_to_string(bkpinfo->backup_media_type)); 1852 if (archiving_to_media) { 1853 sensibly_set_tmpdir_and_scratchdir(bkpinfo); 1854 } 1855 bkpinfo->cdrw_speed = (bkpinfo->backup_media_type == cdstream) ? 2 : 4; 1856 bkpinfo->compression_level = 1857 (bkpinfo->backup_media_type == cdstream) ? 1 : 5; 1858 bkpinfo->use_lzo = 1859 (bkpinfo->backup_media_type == cdstream) ? TRUE : FALSE; 1819 1860 1820 1861 /* … … 1831 1872 */ 1832 1873 1833 1874 mvaddstr_and_log_it(2, 0, " "); 1834 1875 1835 1876 // Find device's /dev (or SCSI) entry 1836 switch(bkpinfo->backup_media_type) 1837 { 1838 case cdr: 1839 case cdrw: 1840 case dvd: 1841 if (archiving_to_media) 1842 { 1843 if (ask_me_yes_or_no("Is your computer a laptop, or does the CD writer incorporate BurnProof technology?")) 1844 { bkpinfo->manual_cd_tray = TRUE; } 1845 if ((bkpinfo->compression_level = which_compression_level()) == -1) 1846 { log_to_screen("User has chosen not to backup the PC"); finish(1); } 1847 sprintf(comment, "What speed is your %s (re)writer?", media_descriptor_string(bkpinfo->backup_media_type)); 1848 if (bkpinfo->backup_media_type==dvd) 1849 { 1850 find_dvd_device(bkpinfo->media_device, FALSE); 1851 strcpy(tmp, "1"); 1852 sprintf(sz_size, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4582 MB 1853 log_msg(1, "Setting to DVD defaults"); 1854 } 1855 else 1856 { 1857 strcpy(bkpinfo->media_device, VANILLA_SCSI_CDROM); 1858 strcpy(tmp, "4"); 1859 strcpy(sz_size, "650"); 1860 log_msg(1, "Setting to CD defaults"); 1861 } 1862 if (bkpinfo->backup_media_type != dvd) 1863 { 1864 if (!popup_and_get_string("Speed", comment, tmp, 4)) 1865 { log_to_screen("User has chosen not to backup the PC"); finish(1); } 1866 } 1867 bkpinfo->cdrw_speed = atoi(tmp); // if DVD then this shouldn't ever be used anyway :) 1868 sprintf(comment, "How much data (in Megabytes) will each %s store?", media_descriptor_string(bkpinfo->backup_media_type)); 1869 if (!popup_and_get_string("Size", comment, sz_size, 5)) 1870 { log_to_screen("User has chosen not to backup the PC"); finish(1); } 1871 for(i=0; i<=MAX_NOOF_MEDIA; i++) { bkpinfo->media_size[i] = atoi(sz_size); } 1872 if (bkpinfo->media_size[0] <=0) 1873 { log_to_screen("User has chosen not to backup the PC"); finish(1); } 1874 } 1875 case cdstream: 1876 if (bkpinfo->disaster_recovery) 1877 { 1878 strcpy(bkpinfo->media_device, "/dev/cdrom"); 1879 log_msg(2, "CD-ROM device assumed to be at %s", bkpinfo->media_device); 1880 } 1881 else if (bkpinfo->restore_data 1882 || bkpinfo->backup_media_type == dvd) 1883 { 1884 if (!bkpinfo->media_device[0]) 1885 { strcpy(bkpinfo->media_device, "/dev/cdrom"); } // just for the heck of it :) 1886 log_msg(1, "bkpinfo->media_device = %s", bkpinfo->media_device); 1887 if (bkpinfo->backup_media_type == dvd || find_cdrom_device (bkpinfo->media_device, FALSE)) 1888 { 1889 log_msg(1, "bkpinfo->media_device = %s", bkpinfo->media_device); 1890 sprintf(comment, "Please specify your %s drive's /dev entry", media_descriptor_string(bkpinfo->backup_media_type)); 1891 if (!popup_and_get_string("Device?", comment, bkpinfo->media_device, MAX_STR_LEN/4)) 1892 { log_to_screen("User has chosen not to backup the PC"); finish(1); } 1893 } 1894 log_msg(2, "%s device found at %s", media_descriptor_string(bkpinfo->backup_media_type), bkpinfo->media_device); 1895 } 1896 else 1897 { 1898 if (find_cdrw_device(bkpinfo->media_device)) { bkpinfo->media_device[0]='\0'; } 1899 if (bkpinfo->media_device[0]) 1900 { 1901 sprintf(tmp, "I think I've found your %s burner at SCSI node %s; am I right on the money?", media_descriptor_string(bkpinfo->backup_media_type), bkpinfo->media_device); 1902 if (!ask_me_yes_or_no(tmp)) { bkpinfo->media_device[0]='\0'; } 1903 } 1904 if (!bkpinfo->media_device[0]) 1905 { 1906 if (g_kernel_version < 2.6) 1907 { 1908 i=popup_and_get_string("Device node?", "What is the SCSI node of your CD (re)writer, please?", bkpinfo->media_device, MAX_STR_LEN/4); 1909 } 1910 else 1911 { 1912 i=popup_and_get_string("/dev entry?", "What is the /dev entry of your CD (re)writer, please?", bkpinfo->media_device, MAX_STR_LEN/4); 1913 } 1914 if (!i) 1915 { log_to_screen("User has chosen not to backup the PC"); finish(1); } 1916 } 1917 } 1918 if (bkpinfo->backup_media_type == cdstream) 1919 { 1920 for(i=0; i<=MAX_NOOF_MEDIA; i++) { bkpinfo->media_size[i] = 650; } 1921 } 1922 break; 1923 case udev: 1924 if (!ask_me_yes_or_no("This option is for advanced users only. Are you sure?")) 1925 { log_to_screen("User has chosen not to backup the PC"); finish(1); } 1926 case tape: 1927 1928 if (find_tape_device_and_size(bkpinfo->media_device, sz_size)) 1929 { 1930 log_msg(3, "Ok, using vanilla scsi tape."); 1931 strcpy(bkpinfo->media_device, VANILLA_SCSI_TAPE); 1932 if ((fin=fopen(bkpinfo->media_device, "r"))) 1933 { paranoid_fclose(fin); } 1934 else 1935 { strcpy(bkpinfo->media_device, "/dev/osst0"); } 1936 } 1937 if (bkpinfo->media_device[0]) 1938 { 1939 if ((fin=fopen(bkpinfo->media_device, "r"))) 1940 { paranoid_fclose(fin); } 1941 else 1942 { 1943 if (does_file_exist("/tmp/mondo-restore.cfg")) 1944 { 1945 read_cfg_var("/tmp/mondo-restore.cfg", "media-dev", bkpinfo->media_device); 1946 } 1947 } 1948 sprintf(tmp, "I think I've found your tape streamer at %s; am I right on the money?", bkpinfo->media_device); 1949 } 1950 if (bkpinfo->media_device[0]) 1951 { 1952 sprintf(tmp, "I think I've found your tape streamer at %s; am I right on the money?", bkpinfo->media_device); 1953 if (!ask_me_yes_or_no(tmp)) { bkpinfo->media_device[0]='\0'; } 1954 } 1955 if (!bkpinfo->media_device[0]) 1956 { 1957 if (!popup_and_get_string("Device name?", "What is the /dev entry of your tape streamer?", bkpinfo->media_device, MAX_STR_LEN/4)) 1958 { log_to_screen("User has chosen not to backup the PC"); finish(1); } 1959 } 1960 sprintf(tmp, "ls -l %s", bkpinfo->media_device); 1961 if (run_program_and_log_output(tmp, FALSE)) 1962 { log_to_screen("User has not specified a valid /dev entry"); finish(1); } 1963 log_msg(4, "sz_size = %s", sz_size); 1964 sz_size[0] = '\0'; 1877 switch (bkpinfo->backup_media_type) { 1878 case cdr: 1879 case cdrw: 1880 case dvd: 1881 if (archiving_to_media) { 1882 if (ask_me_yes_or_no 1883 ("Is your computer a laptop, or does the CD writer incorporate BurnProof technology?")) 1884 { 1885 bkpinfo->manual_cd_tray = TRUE; 1886 } 1887 if ((bkpinfo->compression_level = 1888 which_compression_level()) == -1) { 1889 log_to_screen("User has chosen not to backup the PC"); 1890 finish(1); 1891 } 1892 sprintf(comment, "What speed is your %s (re)writer?", 1893 media_descriptor_string(bkpinfo->backup_media_type)); 1894 if (bkpinfo->backup_media_type == dvd) { 1895 find_dvd_device(bkpinfo->media_device, FALSE); 1896 strcpy(tmp, "1"); 1897 sprintf(sz_size, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4582 MB 1898 log_msg(1, "Setting to DVD defaults"); 1899 } else { 1900 strcpy(bkpinfo->media_device, VANILLA_SCSI_CDROM); 1901 strcpy(tmp, "4"); 1902 strcpy(sz_size, "650"); 1903 log_msg(1, "Setting to CD defaults"); 1904 } 1905 if (bkpinfo->backup_media_type != dvd) { 1906 if (!popup_and_get_string("Speed", comment, tmp, 4)) { 1907 log_to_screen("User has chosen not to backup the PC"); 1908 finish(1); 1909 } 1910 } 1911 bkpinfo->cdrw_speed = atoi(tmp); // if DVD then this shouldn't ever be used anyway :) 1912 sprintf(comment, 1913 "How much data (in Megabytes) will each %s store?", 1914 media_descriptor_string(bkpinfo->backup_media_type)); 1915 if (!popup_and_get_string("Size", comment, sz_size, 5)) { 1916 log_to_screen("User has chosen not to backup the PC"); 1917 finish(1); 1918 } 1919 for (i = 0; i <= MAX_NOOF_MEDIA; i++) { 1920 bkpinfo->media_size[i] = atoi(sz_size); 1921 } 1922 if (bkpinfo->media_size[0] <= 0) { 1923 log_to_screen("User has chosen not to backup the PC"); 1924 finish(1); 1925 } 1926 } 1927 case cdstream: 1928 if (bkpinfo->disaster_recovery) { 1929 strcpy(bkpinfo->media_device, "/dev/cdrom"); 1930 log_msg(2, "CD-ROM device assumed to be at %s", 1931 bkpinfo->media_device); 1932 } else if (bkpinfo->restore_data 1933 || bkpinfo->backup_media_type == dvd) { 1934 if (!bkpinfo->media_device[0]) { 1935 strcpy(bkpinfo->media_device, "/dev/cdrom"); 1936 } // just for the heck of it :) 1937 log_msg(1, "bkpinfo->media_device = %s", 1938 bkpinfo->media_device); 1939 if (bkpinfo->backup_media_type == dvd 1940 || find_cdrom_device(bkpinfo->media_device, FALSE)) { 1941 log_msg(1, "bkpinfo->media_device = %s", 1942 bkpinfo->media_device); 1943 sprintf(comment, 1944 "Please specify your %s drive's /dev entry", 1945 media_descriptor_string(bkpinfo-> 1946 backup_media_type)); 1947 if (!popup_and_get_string 1948 ("Device?", comment, bkpinfo->media_device, 1949 MAX_STR_LEN / 4)) { 1950 log_to_screen("User has chosen not to backup the PC"); 1951 finish(1); 1952 } 1953 } 1954 log_msg(2, "%s device found at %s", 1955 media_descriptor_string(bkpinfo->backup_media_type), 1956 bkpinfo->media_device); 1957 } else { 1958 if (find_cdrw_device(bkpinfo->media_device)) { 1959 bkpinfo->media_device[0] = '\0'; 1960 } 1961 if (bkpinfo->media_device[0]) { 1962 sprintf(tmp, 1963 "I think I've found your %s burner at SCSI node %s; am I right on the money?", 1964 media_descriptor_string(bkpinfo-> 1965 backup_media_type), 1966 bkpinfo->media_device); 1967 if (!ask_me_yes_or_no(tmp)) { 1968 bkpinfo->media_device[0] = '\0'; 1969 } 1970 } 1971 if (!bkpinfo->media_device[0]) { 1972 if (g_kernel_version < 2.6) { 1973 i = popup_and_get_string("Device node?", 1974 "What is the SCSI node of your CD (re)writer, please?", 1975 bkpinfo->media_device, 1976 MAX_STR_LEN / 4); 1977 } else { 1978 i = popup_and_get_string("/dev entry?", 1979 "What is the /dev entry of your CD (re)writer, please?", 1980 bkpinfo->media_device, 1981 MAX_STR_LEN / 4); 1982 } 1983 if (!i) { 1984 log_to_screen("User has chosen not to backup the PC"); 1985 finish(1); 1986 } 1987 } 1988 } 1989 if (bkpinfo->backup_media_type == cdstream) { 1990 for (i = 0; i <= MAX_NOOF_MEDIA; i++) { 1991 bkpinfo->media_size[i] = 650; 1992 } 1993 } 1994 break; 1995 case udev: 1996 if (!ask_me_yes_or_no 1997 ("This option is for advanced users only. Are you sure?")) { 1998 log_to_screen("User has chosen not to backup the PC"); 1999 finish(1); 2000 } 2001 case tape: 2002 2003 if (find_tape_device_and_size(bkpinfo->media_device, sz_size)) { 2004 log_msg(3, "Ok, using vanilla scsi tape."); 2005 strcpy(bkpinfo->media_device, VANILLA_SCSI_TAPE); 2006 if ((fin = fopen(bkpinfo->media_device, "r"))) { 2007 paranoid_fclose(fin); 2008 } else { 2009 strcpy(bkpinfo->media_device, "/dev/osst0"); 2010 } 2011 } 2012 if (bkpinfo->media_device[0]) { 2013 if ((fin = fopen(bkpinfo->media_device, "r"))) { 2014 paranoid_fclose(fin); 2015 } else { 2016 if (does_file_exist("/tmp/mondo-restore.cfg")) { 2017 read_cfg_var("/tmp/mondo-restore.cfg", "media-dev", 2018 bkpinfo->media_device); 2019 } 2020 } 2021 sprintf(tmp, 2022 "I think I've found your tape streamer at %s; am I right on the money?", 2023 bkpinfo->media_device); 2024 } 2025 if (bkpinfo->media_device[0]) { 2026 sprintf(tmp, 2027 "I think I've found your tape streamer at %s; am I right on the money?", 2028 bkpinfo->media_device); 2029 if (!ask_me_yes_or_no(tmp)) { 2030 bkpinfo->media_device[0] = '\0'; 2031 } 2032 } 2033 if (!bkpinfo->media_device[0]) { 2034 if (!popup_and_get_string 2035 ("Device name?", 2036 "What is the /dev entry of your tape streamer?", 2037 bkpinfo->media_device, MAX_STR_LEN / 4)) { 2038 log_to_screen("User has chosen not to backup the PC"); 2039 finish(1); 2040 } 2041 } 2042 sprintf(tmp, "ls -l %s", bkpinfo->media_device); 2043 if (run_program_and_log_output(tmp, FALSE)) { 2044 log_to_screen("User has not specified a valid /dev entry"); 2045 finish(1); 2046 } 2047 log_msg(4, "sz_size = %s", sz_size); 2048 sz_size[0] = '\0'; 1965 2049 /* 1966 2050 if ((size_sz[0]=='\0' || atol(size_sz)==0) && archiving_to_media) … … 1970 2054 } 1971 2055 */ 1972 if (sz_size[0]=='\0') 1973 { bkpinfo->media_size[0] = 0; } 1974 else 1975 { bkpinfo->media_size[0] = friendly_sizestr_to_sizelong (sz_size)/2 - 50; } 1976 log_msg(4, "media_size[0] = %ld", bkpinfo->media_size[0]); 1977 if (bkpinfo->media_size[0] <= 0) { bkpinfo->media_size[0] = 0; } 1978 for(i=1; i<=MAX_NOOF_MEDIA; i++) { bkpinfo->media_size[i] = bkpinfo->media_size[0]; } 1979 if (archiving_to_media) 1980 { 1981 if ((bkpinfo->compression_level = which_compression_level()) == -1) 1982 { log_to_screen("User has chosen not to backup the PC"); finish(1); } 1983 } 1984 break; 1985 1986 1987 1988 case nfs: 1989 if (!bkpinfo->nfs_mount[0]) { strcpy(bkpinfo->nfs_mount, call_program_and_get_last_line_of_output("mount | grep \":\" | cut -d' ' -f1 | head -n1")); } 2056 if (sz_size[0] == '\0') { 2057 bkpinfo->media_size[0] = 0; 2058 } else { 2059 bkpinfo->media_size[0] = 2060 friendly_sizestr_to_sizelong(sz_size) / 2 - 50; 2061 } 2062 log_msg(4, "media_size[0] = %ld", bkpinfo->media_size[0]); 2063 if (bkpinfo->media_size[0] <= 0) { 2064 bkpinfo->media_size[0] = 0; 2065 } 2066 for (i = 1; i <= MAX_NOOF_MEDIA; i++) { 2067 bkpinfo->media_size[i] = bkpinfo->media_size[0]; 2068 } 2069 if (archiving_to_media) { 2070 if ((bkpinfo->compression_level = 2071 which_compression_level()) == -1) { 2072 log_to_screen("User has chosen not to backup the PC"); 2073 finish(1); 2074 } 2075 } 2076 break; 2077 2078 2079 2080 case nfs: 2081 if (!bkpinfo->nfs_mount[0]) { 2082 strcpy(bkpinfo->nfs_mount, 2083 call_program_and_get_last_line_of_output 2084 ("mount | grep \":\" | cut -d' ' -f1 | head -n1")); 2085 } 1990 2086 #ifdef __FreeBSD__ 1991 2087 if (TRUE) 1992 2088 #else 1993 if (!bkpinfo->disaster_recovery ) 1994 #endif 1995 { 1996 if (!popup_and_get_string("NFS dir.", "Please enter path and directory where archives are stored remotely. (Mondo has taken a guess at the correct value. If it is incorrect, delete it and type the correct one.)", bkpinfo->nfs_mount, MAX_STR_LEN/4)) 1997 { 1998 log_to_screen("User has chosen not to backup the PC"); 1999 finish(1); 2000 } 2001 if (!bkpinfo->restore_data) 2002 { 2003 if ((bkpinfo->compression_level = which_compression_level()) == -1) 2004 { 2005 log_to_screen("User has chosen not to backup the PC"); 2006 finish(1); 2007 } 2008 } 2009 // check whether already mounted - we better remove 2010 // surrounding spaces and trailing '/' for this 2011 strip_spaces (bkpinfo->nfs_mount); 2012 if (bkpinfo->nfs_mount[strlen(bkpinfo->nfs_mount) - 1] == '/') 2013 bkpinfo->nfs_mount[strlen(bkpinfo->nfs_mount) - 1] = '\0'; 2014 sprintf(command, "mount | grep \"%s \" | cut -d' ' -f3",bkpinfo->nfs_mount); 2015 strcpy(bkpinfo->isodir, call_program_and_get_last_line_of_output(command)); 2016 } 2017 if (bkpinfo->disaster_recovery) 2018 { 2019 system("umount /tmp/isodir 2> /dev/null"); 2020 if (!popup_and_get_string("NFS share", "Which remote NFS share should I mount?", bkpinfo->nfs_mount, MAX_STR_LEN)) 2021 { 2022 log_to_screen("User has chosen not to backup the PC"); 2023 finish(1); 2024 } 2025 } 2026 if (!is_this_device_mounted(bkpinfo->nfs_mount)) 2027 { 2028 sprintf(bkpinfo->isodir, "/tmp/isodir.mondo.%d", (int)(random()%32768)); 2029 sprintf(command, "mkdir -p %s", bkpinfo->isodir); 2030 run_program_and_log_output(command, 5); 2031 sprintf(tmp, "mount %s -t nfs %s", bkpinfo->nfs_mount, bkpinfo->isodir); 2032 run_program_and_log_output(tmp, 5); 2033 malloc_string(g_selfmounted_isodir); 2034 strcpy(g_selfmounted_isodir, bkpinfo->isodir); 2035 } 2036 if (!is_this_device_mounted(bkpinfo->nfs_mount)) 2037 { 2038 popup_and_OK("Please mount that partition before you try to backup to or restore from it."); 2039 finish(1); 2040 } 2041 strcpy(tmp, bkpinfo->nfs_remote_dir); 2042 if (!popup_and_get_string("Directory", "Which directory within that mountpoint?", tmp, MAX_STR_LEN)) 2043 { 2044 log_to_screen("User has chosen not to backup the PC"); 2045 finish(1); 2046 } 2047 strcpy(bkpinfo->nfs_remote_dir, tmp); 2048 // check whether writable - we better remove surrounding spaces for this 2049 strip_spaces (bkpinfo->nfs_remote_dir); 2050 sprintf (command, "echo hi > %s/%s/.dummy.txt", bkpinfo->isodir, 2051 bkpinfo->nfs_remote_dir); 2052 while (run_program_and_log_output (command, FALSE)) { 2053 strcpy(tmp, bkpinfo->nfs_remote_dir); 2054 sprintf (prompt, 2055 "Directory '%s' under mountpoint '%s' does not exist or is not writable. You can fix this or change the directory and retry or cancel the backup.", 2056 bkpinfo->nfs_remote_dir, bkpinfo->isodir); 2057 if(!popup_and_get_string ("Directory", prompt, tmp, MAX_STR_LEN)) { 2058 log_to_screen("User has chosen not to backup the PC"); 2059 finish(1); 2060 } 2061 strcpy(bkpinfo->nfs_remote_dir, tmp); 2062 // check whether writable - we better remove surrounding space s for this 2063 strip_spaces (bkpinfo->nfs_remote_dir); 2064 sprintf (command, "echo hi > %s/%s/.dummy.txt", bkpinfo->isodir, 2065 bkpinfo->nfs_remote_dir); 2066 } 2067 2068 for(i=0; i<=MAX_NOOF_MEDIA; i++) { bkpinfo->media_size[i] = 650; } 2069 log_msg(3, "Just set nfs_remote_dir to %s", bkpinfo->nfs_remote_dir); 2070 log_msg(3, "isodir is still %s", bkpinfo->isodir); 2071 break; 2072 2073 case iso: 2074 if (!bkpinfo->disaster_recovery) 2075 { 2076 if (!popup_and_get_string("Storage dir.", "Please enter the full path that contains your ISO images. Example: /mnt/raid0_0", bkpinfo->isodir, MAX_STR_LEN/4)) 2077 { log_to_screen("User has chosen not to backup the PC"); finish(1); } 2078 if (archiving_to_media) { 2079 if ((bkpinfo->compression_level = which_compression_level()) == -1) 2080 { log_to_screen("User has chosen not to backup the PC"); finish(1); } 2081 if (!popup_and_get_string ("ISO size.", "Please enter how big you want each ISO image to be (in megabytes). This should be less than or equal to the size of the CD-R[W]'s or DVD's you plan to backup to.", sz_size, 16)) 2082 { log_to_screen ("User has chosen not to backup the PC"); finish(1); } 2083 for(i=0; i<=MAX_NOOF_MEDIA; i++) { bkpinfo->media_size[i] = atoi (sz_size); } 2084 if (!popup_and_get_string("Prefix.", "Please enter the prefix that will be prepended to your ISO filename. Example: machine1 to obtain machine1-[1-9]*.iso files", bkpinfo->prefix, MAX_STR_LEN/4)) 2085 { log_to_screen("User has chosen not to backup the PC"); finish(1); } 2086 } else { 2087 for (i=0;i<=MAX_NOOF_MEDIA; i++) { bkpinfo->media_size[i] = 650;} 2088 } 2089 } 2090 break; 2091 default: 2092 fatal_error("I, Mojo Jojo, shall defeat those pesky Powerpuff Girls!"); 2093 } 2094 2095 if (archiving_to_media) 2096 { 2089 if (!bkpinfo->disaster_recovery) 2090 #endif 2091 { 2092 if (!popup_and_get_string 2093 ("NFS dir.", 2094 "Please enter path and directory where archives are stored remotely. (Mondo has taken a guess at the correct value. If it is incorrect, delete it and type the correct one.)", 2095 bkpinfo->nfs_mount, MAX_STR_LEN / 4)) { 2096 log_to_screen("User has chosen not to backup the PC"); 2097 finish(1); 2098 } 2099 if (!bkpinfo->restore_data) { 2100 if ((bkpinfo->compression_level = 2101 which_compression_level()) == -1) { 2102 log_to_screen("User has chosen not to backup the PC"); 2103 finish(1); 2104 } 2105 } 2106 // check whether already mounted - we better remove 2107 // surrounding spaces and trailing '/' for this 2108 strip_spaces(bkpinfo->nfs_mount); 2109 if (bkpinfo->nfs_mount[strlen(bkpinfo->nfs_mount) - 1] == '/') 2110 bkpinfo->nfs_mount[strlen(bkpinfo->nfs_mount) - 1] = '\0'; 2111 sprintf(command, "mount | grep \"%s \" | cut -d' ' -f3", 2112 bkpinfo->nfs_mount); 2113 strcpy(bkpinfo->isodir, 2114 call_program_and_get_last_line_of_output(command)); 2115 } 2116 if (bkpinfo->disaster_recovery) { 2117 system("umount /tmp/isodir 2> /dev/null"); 2118 if (!popup_and_get_string 2119 ("NFS share", "Which remote NFS share should I mount?", 2120 bkpinfo->nfs_mount, MAX_STR_LEN)) { 2121 log_to_screen("User has chosen not to backup the PC"); 2122 finish(1); 2123 } 2124 } 2125 if (!is_this_device_mounted(bkpinfo->nfs_mount)) { 2126 sprintf(bkpinfo->isodir, "/tmp/isodir.mondo.%d", 2127 (int) (random() % 32768)); 2128 sprintf(command, "mkdir -p %s", bkpinfo->isodir); 2129 run_program_and_log_output(command, 5); 2130 sprintf(tmp, "mount %s -t nfs %s", bkpinfo->nfs_mount, 2131 bkpinfo->isodir); 2132 run_program_and_log_output(tmp, 5); 2133 malloc_string(g_selfmounted_isodir); 2134 strcpy(g_selfmounted_isodir, bkpinfo->isodir); 2135 } 2136 if (!is_this_device_mounted(bkpinfo->nfs_mount)) { 2137 popup_and_OK 2138 ("Please mount that partition before you try to backup to or restore from it."); 2139 finish(1); 2140 } 2141 strcpy(tmp, bkpinfo->nfs_remote_dir); 2142 if (!popup_and_get_string 2143 ("Directory", "Which directory within that mountpoint?", tmp, 2144 MAX_STR_LEN)) { 2145 log_to_screen("User has chosen not to backup the PC"); 2146 finish(1); 2147 } 2148 strcpy(bkpinfo->nfs_remote_dir, tmp); 2149 // check whether writable - we better remove surrounding spaces for this 2150 strip_spaces(bkpinfo->nfs_remote_dir); 2151 sprintf(command, "echo hi > %s/%s/.dummy.txt", bkpinfo->isodir, 2152 bkpinfo->nfs_remote_dir); 2153 while (run_program_and_log_output(command, FALSE)) { 2154 strcpy(tmp, bkpinfo->nfs_remote_dir); 2155 sprintf(prompt, 2156 "Directory '%s' under mountpoint '%s' does not exist or is not writable. You can fix this or change the directory and retry or cancel the backup.", 2157 bkpinfo->nfs_remote_dir, bkpinfo->isodir); 2158 if (!popup_and_get_string 2159 ("Directory", prompt, tmp, MAX_STR_LEN)) { 2160 log_to_screen("User has chosen not to backup the PC"); 2161 finish(1); 2162 } 2163 strcpy(bkpinfo->nfs_remote_dir, tmp); 2164 // check whether writable - we better remove surrounding space s for this 2165 strip_spaces(bkpinfo->nfs_remote_dir); 2166 sprintf(command, "echo hi > %s/%s/.dummy.txt", bkpinfo->isodir, 2167 bkpinfo->nfs_remote_dir); 2168 } 2169 2170 for (i = 0; i <= MAX_NOOF_MEDIA; i++) { 2171 bkpinfo->media_size[i] = 650; 2172 } 2173 log_msg(3, "Just set nfs_remote_dir to %s", 2174 bkpinfo->nfs_remote_dir); 2175 log_msg(3, "isodir is still %s", bkpinfo->isodir); 2176 break; 2177 2178 case iso: 2179 if (!bkpinfo->disaster_recovery) { 2180 if (!popup_and_get_string 2181 ("Storage dir.", 2182 "Please enter the full path that contains your ISO images. Example: /mnt/raid0_0", 2183 bkpinfo->isodir, MAX_STR_LEN / 4)) { 2184 log_to_screen("User has chosen not to backup the PC"); 2185 finish(1); 2186 } 2187 if (archiving_to_media) { 2188 if ((bkpinfo->compression_level = 2189 which_compression_level()) == -1) { 2190 log_to_screen("User has chosen not to backup the PC"); 2191 finish(1); 2192 } 2193 if (!popup_and_get_string 2194 ("ISO size.", 2195 "Please enter how big you want each ISO image to be (in megabytes). This should be less than or equal to the size of the CD-R[W]'s or DVD's you plan to backup to.", 2196 sz_size, 16)) { 2197 log_to_screen("User has chosen not to backup the PC"); 2198 finish(1); 2199 } 2200 for (i = 0; i <= MAX_NOOF_MEDIA; i++) { 2201 bkpinfo->media_size[i] = atoi(sz_size); 2202 } 2203 if (!popup_and_get_string 2204 ("Prefix.", 2205 "Please enter the prefix that will be prepended to your ISO filename. Example: machine1 to obtain machine1-[1-9]*.iso files", 2206 bkpinfo->prefix, MAX_STR_LEN / 4)) { 2207 log_to_screen("User has chosen not to backup the PC"); 2208 finish(1); 2209 } 2210 } else { 2211 for (i = 0; i <= MAX_NOOF_MEDIA; i++) { 2212 bkpinfo->media_size[i] = 650; 2213 } 2214 } 2215 } 2216 break; 2217 default: 2218 fatal_error 2219 ("I, Mojo Jojo, shall defeat those pesky Powerpuff Girls!"); 2220 } 2221 2222 if (archiving_to_media) { 2097 2223 2098 2224 #ifdef __FreeBSD__ 2099 strcpy (bkpinfo->boot_device, call_program_and_get_last_line_of_output 2100 ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/\\([0-9]\\).*/\\1/'")); 2225 strcpy(bkpinfo->boot_device, 2226 call_program_and_get_last_line_of_output 2227 ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/\\([0-9]\\).*/\\1/'")); 2101 2228 #else 2102 strcpy (bkpinfo->boot_device, call_program_and_get_last_line_of_output 2103 ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'")); 2104 #endif 2105 i = which_boot_loader(bkpinfo->boot_device); 2106 if (i=='U') // unknown 2107 { 2229 strcpy(bkpinfo->boot_device, 2230 call_program_and_get_last_line_of_output 2231 ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'")); 2232 #endif 2233 i = which_boot_loader(bkpinfo->boot_device); 2234 if (i == 'U') // unknown 2235 { 2108 2236 2109 2237 #ifdef __FreeBSD__ 2110 if (!popup_and_get_string("Boot device", "What is your boot device? (e.g. /dev/ad0)", bkpinfo->boot_device, MAX_STR_LEN/4)) 2111 { log_to_screen("User has chosen not to backup the PC"); finish(1); } 2112 i = which_boot_loader (bkpinfo->boot_device); 2238 if (!popup_and_get_string 2239 ("Boot device", 2240 "What is your boot device? (e.g. /dev/ad0)", 2241 bkpinfo->boot_device, MAX_STR_LEN / 4)) { 2242 log_to_screen("User has chosen not to backup the PC"); 2243 finish(1); 2244 } 2245 i = which_boot_loader(bkpinfo->boot_device); 2113 2246 #else 2114 if (!popup_and_get_string("Boot device", "What is your boot device? (e.g. /dev/hda)", bkpinfo->boot_device, MAX_STR_LEN/4)) 2115 { log_to_screen("User has chosen not to backup the PC"); finish(1); } 2116 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "LILO")) { i='L'; } 2117 else if (does_string_exist_in_boot_block(bkpinfo->boot_device, "ELILO")) { i='E'; } 2118 else if (does_string_exist_in_boot_block(bkpinfo->boot_device, "GRUB")) { i='G'; } 2119 else { i = 'U'; } 2120 #endif 2121 if (i == 'U') 2122 { 2123 if (ask_me_yes_or_no("Unidentified boot loader. Shall I restore it byte-for-byte at restore time and hope for the best?")) 2247 if (!popup_and_get_string 2248 ("Boot device", 2249 "What is your boot device? (e.g. /dev/hda)", 2250 bkpinfo->boot_device, MAX_STR_LEN / 4)) { 2251 log_to_screen("User has chosen not to backup the PC"); 2252 finish(1); 2253 } 2254 if (does_string_exist_in_boot_block 2255 (bkpinfo->boot_device, "LILO")) { 2256 i = 'L'; 2257 } else 2258 if (does_string_exist_in_boot_block 2259 (bkpinfo->boot_device, "ELILO")) { 2260 i = 'E'; 2261 } else 2262 if (does_string_exist_in_boot_block 2263 (bkpinfo->boot_device, "GRUB")) { 2264 i = 'G'; 2265 } else { 2266 i = 'U'; 2267 } 2268 #endif 2269 if (i == 'U') { 2270 if (ask_me_yes_or_no 2271 ("Unidentified boot loader. Shall I restore it byte-for-byte at restore time and hope for the best?")) 2272 { 2273 i = 'R'; // raw 2274 } else { 2275 log_to_screen 2276 ("I cannot find your boot loader. Please run mondoarchive with parameters."); 2277 finish(1); 2278 } 2279 } 2280 } 2281 bkpinfo->boot_loader = i; 2282 strcpy(bkpinfo->include_paths, "/"); 2283 if (!popup_and_get_string 2284 ("Backup paths", 2285 "Please enter paths which you want me to backup. The default is '/' (i.e. everything).", 2286 bkpinfo->include_paths, MAX_STR_LEN)) { 2287 log_to_screen("User has chosen not to backup the PC"); 2288 finish(1); 2289 } 2290 strcpy(tmp, list_of_NFS_mounts_only()); 2291 if (strlen(tmp) > 2) { 2292 if (bkpinfo->exclude_paths[0]) { 2293 strcat(bkpinfo->exclude_paths, " "); 2294 } 2295 strncpy(bkpinfo->exclude_paths, tmp, MAX_STR_LEN); 2296 } 2297 // NTFS 2298 strcpy(tmp, 2299 call_program_and_get_last_line_of_output 2300 ("parted2fdisk -l | grep -i ntfs | awk '{ print $1};' | tr -s '\\n' ' ' | awk '{ print $0};'")); 2301 if (strlen(tmp) > 2) { 2302 if (!popup_and_get_string 2303 ("NTFS partitions", 2304 "Please enter/confirm the NTFS partitions you wish to backup as well.", 2305 tmp, MAX_STR_LEN / 4)) { 2306 log_to_screen("User has chosen not to backup the PC"); 2307 finish(1); 2308 } 2309 strncpy(bkpinfo->image_devs, tmp, MAX_STR_LEN / 4); 2310 } 2311 2312 2313 if (!popup_and_get_string 2314 ("Exclude paths", 2315 "Please enter paths which you do NOT want to backup. Separate them with spaces. NB: /tmp and /proc are always excluded. :-) Just hit 'Enter' if you want to do a full system backup.", 2316 bkpinfo->exclude_paths, MAX_STR_LEN)) { 2317 log_to_screen("User has chosen not to backup the PC"); 2318 finish(1); 2319 } 2320 bkpinfo->make_cd_use_lilo = FALSE; 2321 bkpinfo->backup_data = TRUE; 2322 bkpinfo->verify_data = 2323 ask_me_yes_or_no 2324 ("Will you want to verify your backups after Mondo has created them?"); 2325 2326 #ifndef __FreeBSD__ 2327 if (!ask_me_yes_or_no 2328 ("Are you confident that your kernel is a sane, sensible, standard Linux kernel? Say 'no' if you are using a Gentoo <1.4 or Debian <3.0, please.")) 2329 #endif 2124 2330 { 2125 i = 'R'; // raw 2126 } 2127 else 2128 { 2129 log_to_screen("I cannot find your boot loader. Please run mondoarchive with parameters."); finish(1); 2130 } 2131 } 2132 } 2133 bkpinfo->boot_loader = i; 2134 strcpy(bkpinfo->include_paths, "/"); 2135 if (!popup_and_get_string("Backup paths", "Please enter paths which you want me to backup. The default is '/' (i.e. everything).", bkpinfo->include_paths, MAX_STR_LEN)) 2136 { log_to_screen("User has chosen not to backup the PC"); finish(1); } 2137 strcpy(tmp, list_of_NFS_mounts_only()); 2138 if (strlen(tmp)>2) 2139 { 2140 if (bkpinfo->exclude_paths[0]) { strcat(bkpinfo->exclude_paths, " "); } 2141 strncpy(bkpinfo->exclude_paths, tmp, MAX_STR_LEN); 2142 } 2143 2144 // NTFS 2145 strcpy(tmp, call_program_and_get_last_line_of_output("parted2fdisk -l | grep -i ntfs | awk '{ print $1};' | tr -s '\\n' ' ' | awk '{ print $0};'")); 2146 if (strlen(tmp) > 2) 2147 { 2148 if (!popup_and_get_string("NTFS partitions", "Please enter/confirm the NTFS partitions you wish to backup as well.", tmp, MAX_STR_LEN/4)) 2149 { log_to_screen ("User has chosen not to backup the PC"); finish(1); } 2150 strncpy (bkpinfo->image_devs, tmp, MAX_STR_LEN/4); 2151 } 2152 2153 2154 if (!popup_and_get_string("Exclude paths", "Please enter paths which you do NOT want to backup. Separate them with spaces. NB: /tmp and /proc are always excluded. :-) Just hit 'Enter' if you want to do a full system backup.", bkpinfo->exclude_paths, MAX_STR_LEN)) 2155 { log_to_screen("User has chosen not to backup the PC"); finish(1); } 2156 bkpinfo->make_cd_use_lilo = FALSE; 2157 bkpinfo->backup_data = TRUE; 2158 bkpinfo->verify_data = ask_me_yes_or_no("Will you want to verify your backups after Mondo has created them?"); 2159 2160 #ifndef __FreeBSD__ 2161 if (!ask_me_yes_or_no("Are you confident that your kernel is a sane, sensible, standard Linux kernel? Say 'no' if you are using a Gentoo <1.4 or Debian <3.0, please.")) 2162 #endif 2163 { strcpy(bkpinfo->kernel_path, "FAILSAFE"); } 2164 2165 if (!ask_me_yes_or_no("Are you sure you want to proceed? Hit 'no' to abort.")) 2166 { log_to_screen("User has chosen not to backup the PC"); finish(1); } 2167 } 2168 else 2169 { 2170 bkpinfo->restore_data = TRUE; // probably... 2171 } 2172 2173 if (bkpinfo->backup_media_type == iso || bkpinfo->backup_media_type == nfs) 2174 { 2175 g_ISO_restore_mode = TRUE; 2176 } 2177 2331 strcpy(bkpinfo->kernel_path, "FAILSAFE"); 2332 } 2333 2334 if (!ask_me_yes_or_no 2335 ("Are you sure you want to proceed? Hit 'no' to abort.")) { 2336 log_to_screen("User has chosen not to backup the PC"); 2337 finish(1); 2338 } 2339 } else { 2340 bkpinfo->restore_data = TRUE; // probably... 2341 } 2342 2343 if (bkpinfo->backup_media_type == iso 2344 || bkpinfo->backup_media_type == nfs) { 2345 g_ISO_restore_mode = TRUE; 2346 } 2178 2347 #ifdef __FreeSD__ 2179 2348 // skip 2180 2349 #else 2181 if (bkpinfo->backup_media_type == nfs) 2182 { 2183 sprintf(tmp, "mount | grep \"%s\" | cut -d' ' -f3",bkpinfo->nfs_mount);2350 if (bkpinfo->backup_media_type == nfs) { 2351 sprintf(tmp, "mount | grep \"%s\" | cut -d' ' -f3", 2352 bkpinfo->nfs_mount); 2184 2353 // strcpy(bkpinfo->isodir, call_program_and_get_last_line_of_output(tmp)); 2185 log_msg(3, "I think the NFS mount is mounted at %s", bkpinfo->isodir); 2186 } 2187 log_it("isodir = %s", bkpinfo->isodir); 2188 log_it("nfs_mount = '%s'", bkpinfo->nfs_mount); 2189 #endif 2190 2191 log_it("media device = %s", bkpinfo->media_device); 2192 log_it("media size = %ld", bkpinfo->media_size[1]); 2193 log_it("media type = %s", bkptype_to_string(bkpinfo->backup_media_type)); 2194 log_it("compression = %ld", bkpinfo->compression_level); 2195 log_it("include_paths = '%s'", bkpinfo->include_paths); 2196 log_it("exclude_paths = '%s'", bkpinfo->exclude_paths); 2197 log_it("scratchdir = '%s'", bkpinfo->scratchdir); 2198 log_it("tmpdir = '%s'", bkpinfo->tmpdir); 2199 log_it("boot_device = '%s' (loader=%c)", bkpinfo->boot_device, bkpinfo->boot_loader); 2200 if (bkpinfo->media_size[0] < 0) 2201 { 2202 if (archiving_to_media) 2203 { 2204 fatal_error("Media size is less than zero."); 2205 } 2206 else 2207 { 2208 log_msg(2, "Warning - media size is less than zero."); 2209 bkpinfo->media_size[0] = 0; 2210 } 2211 } 2212 paranoid_free(tmp); 2213 paranoid_free(sz_size); 2214 paranoid_free(command); 2215 paranoid_free(comment); 2216 paranoid_free(prompt); 2217 return(0); 2354 log_msg(3, "I think the NFS mount is mounted at %s", 2355 bkpinfo->isodir); 2356 } 2357 log_it("isodir = %s", bkpinfo->isodir); 2358 log_it("nfs_mount = '%s'", bkpinfo->nfs_mount); 2359 #endif 2360 2361 log_it("media device = %s", bkpinfo->media_device); 2362 log_it("media size = %ld", bkpinfo->media_size[1]); 2363 log_it("media type = %s", 2364 bkptype_to_string(bkpinfo->backup_media_type)); 2365 log_it("compression = %ld", bkpinfo->compression_level); 2366 log_it("include_paths = '%s'", bkpinfo->include_paths); 2367 log_it("exclude_paths = '%s'", bkpinfo->exclude_paths); 2368 log_it("scratchdir = '%s'", bkpinfo->scratchdir); 2369 log_it("tmpdir = '%s'", bkpinfo->tmpdir); 2370 log_it("boot_device = '%s' (loader=%c)", bkpinfo->boot_device, 2371 bkpinfo->boot_loader); 2372 if (bkpinfo->media_size[0] < 0) { 2373 if (archiving_to_media) { 2374 fatal_error("Media size is less than zero."); 2375 } else { 2376 log_msg(2, "Warning - media size is less than zero."); 2377 bkpinfo->media_size[0] = 0; 2378 } 2379 } 2380 paranoid_free(tmp); 2381 paranoid_free(sz_size); 2382 paranoid_free(command); 2383 paranoid_free(comment); 2384 paranoid_free(prompt); 2385 return (0); 2218 2386 } 2219 2387 … … 2232 2400 char *list_of_NFS_devices_and_mounts(void) 2233 2401 { 2234 char *exclude_these_devices; 2235 char *exclude_these_directories; 2236 static char result_sz[512]; 2237 2238 malloc_string(exclude_these_devices); 2239 malloc_string(exclude_these_directories); 2240 strcpy(exclude_these_directories, call_program_and_get_last_line_of_output("mount -t coda,ncpfs,nfs,smbfs | tr -s '\t' ' ' | cut -d' ' -f3 | tr -s '\n' ' ' | awk '{print $0;}'")); 2241 strcpy(exclude_these_devices, call_program_and_get_last_line_of_output("cat /etc/fstab | tr -s '\t' ' ' | grep -E '( (coda|ncpfs|nfs|smbfs) )' | cut -d' ' -f1 | tr -s '\n' ' ' | awk '{print $0;}'")); 2242 sprintf(result_sz, "%s %s", exclude_these_directories, exclude_these_devices); 2243 paranoid_free(exclude_these_devices); 2244 paranoid_free(exclude_these_directories); 2245 return(result_sz); 2402 char *exclude_these_devices; 2403 char *exclude_these_directories; 2404 static char result_sz[512]; 2405 2406 malloc_string(exclude_these_devices); 2407 malloc_string(exclude_these_directories); 2408 strcpy(exclude_these_directories, 2409 call_program_and_get_last_line_of_output 2410 ("mount -t coda,ncpfs,nfs,smbfs | tr -s '\t' ' ' | cut -d' ' -f3 | tr -s '\n' ' ' | awk '{print $0;}'")); 2411 strcpy(exclude_these_devices, 2412 call_program_and_get_last_line_of_output 2413 ("cat /etc/fstab | tr -s '\t' ' ' | grep -E '( (coda|ncpfs|nfs|smbfs) )' | cut -d' ' -f1 | tr -s '\n' ' ' | awk '{print $0;}'")); 2414 sprintf(result_sz, "%s %s", exclude_these_directories, 2415 exclude_these_devices); 2416 paranoid_free(exclude_these_devices); 2417 paranoid_free(exclude_these_directories); 2418 return (result_sz); 2246 2419 } 2247 2420 … … 2257 2430 char *list_of_NFS_mounts_only(void) 2258 2431 { 2259 char *exclude_these_devices; 2260 char *exclude_these_directories; 2261 static char result_sz[512]; 2262 2263 malloc_string(exclude_these_devices); 2264 malloc_string(exclude_these_directories); 2265 strcpy(exclude_these_directories, call_program_and_get_last_line_of_output("mount -t coda,ncpfs,nfs,smbfs | tr -s '\t' ' ' | cut -d' ' -f3 | tr -s '\n' ' ' | awk '{print $0;}'")); 2266 strcpy(exclude_these_devices, call_program_and_get_last_line_of_output("cat /etc/fstab | tr -s '\t' ' ' | grep -E '( (coda|ncpfs|nfs|smbfs) )' | cut -d' ' -f1 | tr -s '\n' ' ' | awk '{print $0;}'")); 2267 sprintf(result_sz, "%s", exclude_these_directories); 2268 paranoid_free(exclude_these_devices); 2269 paranoid_free(exclude_these_directories); 2270 return(result_sz); 2432 char *exclude_these_devices; 2433 char *exclude_these_directories; 2434 static char result_sz[512]; 2435 2436 malloc_string(exclude_these_devices); 2437 malloc_string(exclude_these_directories); 2438 strcpy(exclude_these_directories, 2439 call_program_and_get_last_line_of_output 2440 ("mount -t coda,ncpfs,nfs,smbfs | tr -s '\t' ' ' | cut -d' ' -f3 | tr -s '\n' ' ' | awk '{print $0;}'")); 2441 strcpy(exclude_these_devices, 2442 call_program_and_get_last_line_of_output 2443 ("cat /etc/fstab | tr -s '\t' ' ' | grep -E '( (coda|ncpfs|nfs|smbfs) )' | cut -d' ' -f1 | tr -s '\n' ' ' | awk '{print $0;}'")); 2444 sprintf(result_sz, "%s", exclude_these_directories); 2445 paranoid_free(exclude_these_devices); 2446 paranoid_free(exclude_these_directories); 2447 return (result_sz); 2271 2448 } 2272 2449 … … 2284 2461 * @ingroup deviceGroup 2285 2462 */ 2286 void make_fifo(char*store_name_here, char*stub) 2287 { 2288 char *tmp; 2289 2290 malloc_string(tmp); 2291 assert_string_is_neither_NULL_nor_zerolength(stub); 2292 2293 sprintf(store_name_here, "%s%d%d", stub, (int)(random()%32768), (int)(random()%32768)); 2294 make_hole_for_file(store_name_here); 2295 mkfifo(store_name_here, S_IRWXU|S_IRWXG); 2296 sprintf(tmp, "chmod 770 %s", store_name_here); 2297 paranoid_system(tmp); 2298 paranoid_free(tmp); 2463 void make_fifo(char *store_name_here, char *stub) 2464 { 2465 char *tmp; 2466 2467 malloc_string(tmp); 2468 assert_string_is_neither_NULL_nor_zerolength(stub); 2469 2470 sprintf(store_name_here, "%s%d%d", stub, (int) (random() % 32768), 2471 (int) (random() % 32768)); 2472 make_hole_for_file(store_name_here); 2473 mkfifo(store_name_here, S_IRWXU | S_IRWXG); 2474 sprintf(tmp, "chmod 770 %s", store_name_here); 2475 paranoid_system(tmp); 2476 paranoid_free(tmp); 2299 2477 } 2300 2478 … … 2312 2490 void sensibly_set_tmpdir_and_scratchdir(struct s_bkpinfo *bkpinfo) 2313 2491 { 2314 2315 2316 2317 2318 2319 assert(bkpinfo!=NULL);2492 char *tmp, *command, *sz; 2493 2494 malloc_string(tmp); 2495 malloc_string(command); 2496 malloc_string(sz); 2497 assert(bkpinfo != NULL); 2320 2498 2321 2499 #ifdef __FreeBSD__ 2322 strcpy(tmp, call_program_and_get_last_line_of_output("df -m -t nonfs,msdosfs,ntfs,smbfs,smb,cifs | tr -s '\t' ' ' | grep -v none | grep -v Filesystem | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'")); 2500 strcpy(tmp, 2501 call_program_and_get_last_line_of_output 2502 ("df -m -t nonfs,msdosfs,ntfs,smbfs,smb,cifs | tr -s '\t' ' ' | grep -v none | grep -v Filesystem | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'")); 2323 2503 #else 2324 strcpy(tmp, call_program_and_get_last_line_of_output("df -m -x nfs -x vfat -x ntfs -x smbfs -x smb -x cifs | sed 's/ /devdev/' | tr -s '\t' ' ' | grep -v none | grep -v Filesystem | grep -v /dev/shm | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'")); 2325 #endif 2326 2327 if (tmp[0]!='/') { strcpy(sz, tmp); strcpy(tmp, "/"); strcat(tmp, sz); } 2328 if (!tmp[0]) { 2329 fatal_error ("I couldn't figure out the tempdir!"); 2330 } 2331 sprintf(bkpinfo->tmpdir, "%s/tmp.mondo.%d", tmp, (int)(random()%32768)); 2332 log_it("bkpinfo->tmpdir is being set to %s", bkpinfo->tmpdir); 2333 2334 sprintf(bkpinfo->scratchdir, "%s/mondo.scratch.%d", tmp, (int)(random()%32768)); 2335 log_it("bkpinfo->scratchdir is being set to %s", bkpinfo->scratchdir); 2336 2337 sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir, bkpinfo->scratchdir); 2338 2339 sprintf(command, "rm -Rf %s/tmp.mondo.* %s/mondo.scratch.*", tmp, tmp); 2340 paranoid_system(command); 2341 paranoid_free(tmp); 2342 paranoid_free(command); 2343 paranoid_free(sz); 2504 strcpy(tmp, 2505 call_program_and_get_last_line_of_output 2506 ("df -m -x nfs -x vfat -x ntfs -x smbfs -x smb -x cifs | sed 's/ /devdev/' | tr -s '\t' ' ' | grep -v none | grep -v Filesystem | grep -v /dev/shm | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'")); 2507 #endif 2508 2509 if (tmp[0] != '/') { 2510 strcpy(sz, tmp); 2511 strcpy(tmp, "/"); 2512 strcat(tmp, sz); 2513 } 2514 if (!tmp[0]) { 2515 fatal_error("I couldn't figure out the tempdir!"); 2516 } 2517 sprintf(bkpinfo->tmpdir, "%s/tmp.mondo.%d", tmp, 2518 (int) (random() % 32768)); 2519 log_it("bkpinfo->tmpdir is being set to %s", bkpinfo->tmpdir); 2520 2521 sprintf(bkpinfo->scratchdir, "%s/mondo.scratch.%d", tmp, 2522 (int) (random() % 32768)); 2523 log_it("bkpinfo->scratchdir is being set to %s", bkpinfo->scratchdir); 2524 2525 sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir, 2526 bkpinfo->scratchdir); 2527 2528 sprintf(command, "rm -Rf %s/tmp.mondo.* %s/mondo.scratch.*", tmp, tmp); 2529 paranoid_system(command); 2530 paranoid_free(tmp); 2531 paranoid_free(command); 2532 paranoid_free(sz); 2344 2533 } 2345 2534 … … 2360 2549 * @return TRUE if @p dev exists, FALSE if it doesn't. 2361 2550 */ 2362 bool set_dev_to_this_if_rx_OK(char *output, char*dev) 2363 { 2364 char *command; 2365 2366 malloc_string(command); 2367 if (!dev || dev[0]=='\0') { output[0]='\0'; return(FALSE); } 2551 bool set_dev_to_this_if_rx_OK(char *output, char *dev) 2552 { 2553 char *command; 2554 2555 malloc_string(command); 2556 if (!dev || dev[0] == '\0') { 2557 output[0] = '\0'; 2558 return (FALSE); 2559 } 2368 2560 // assert_string_is_neither_NULL_nor_zerolength(dev); 2369 log_msg(10, "Injecting %s", dev); 2370 inject_device(dev); 2371 if (!does_file_exist(dev)) { log_msg(10, "%s doesn't exist. Returning FALSE.", dev); return(FALSE); } 2372 sprintf(command, "dd bs=%ld count=1 if=%s of=/dev/null &> /dev/null", 512L, dev); 2373 if (!run_program_and_log_output(command, FALSE) && !run_program_and_log_output(command, FALSE)) 2374 { strcpy(output, dev); log_msg(4, "Found it - %s", dev); return(TRUE); } 2375 else 2376 { output[0]='\0'; log_msg(4, "It's not %s",dev); return(FALSE); } 2561 log_msg(10, "Injecting %s", dev); 2562 inject_device(dev); 2563 if (!does_file_exist(dev)) { 2564 log_msg(10, "%s doesn't exist. Returning FALSE.", dev); 2565 return (FALSE); 2566 } 2567 sprintf(command, "dd bs=%ld count=1 if=%s of=/dev/null &> /dev/null", 2568 512L, dev); 2569 if (!run_program_and_log_output(command, FALSE) 2570 && !run_program_and_log_output(command, FALSE)) { 2571 strcpy(output, dev); 2572 log_msg(4, "Found it - %s", dev); 2573 return (TRUE); 2574 } else { 2575 output[0] = '\0'; 2576 log_msg(4, "It's not %s", dev); 2577 return (FALSE); 2578 } 2377 2579 } 2378 2580 … … 2390 2592 int what_number_cd_is_this(struct s_bkpinfo *bkpinfo) 2391 2593 { 2392 int cd_number=-1;2393 2394 2395 2396 2397 2398 assert(bkpinfo!=NULL);2594 int cd_number = -1; 2595 char *mountdev; 2596 char *tmp; 2597 2598 malloc_string(mountdev); 2599 malloc_string(tmp); 2600 assert(bkpinfo != NULL); 2399 2601 // log_it("Asking what_number_cd_is_this"); 2400 if (g_ISO_restore_mode) 2401 { 2402 sprintf(tmp, "mount | grep iso9660 | awk '{print $3;}'"); 2602 if (g_ISO_restore_mode) { 2603 sprintf(tmp, "mount | grep iso9660 | awk '{print $3;}'"); 2403 2604 // log_it("tmp = %s", tmp); 2404 2605 2405 2406 2606 strcpy(mountdev, call_program_and_get_last_line_of_output(tmp)); 2607 strcat(mountdev, "/archives/THIS-CD-NUMBER"); 2407 2608 // log_it("mountdev = %s", mountdev); 2408 2609 cd_number = atoi(last_line_of_file(mountdev)); 2409 2610 // log_it("cd_number = %d", cd_number); 2410 paranoid_free(mountdev); 2411 paranoid_free(tmp); 2412 return(cd_number); 2413 } 2414 2415 strcpy(mountdev, bkpinfo->media_device); 2416 if (!mountdev[0]) 2417 { 2418 log_it("(what_number_cd_is_this) Warning - media_device unknown. Finding out..."); 2419 find_cdrom_device(bkpinfo->media_device, FALSE); 2420 } 2421 if (! is_this_device_mounted(MNT_CDROM)) 2422 { mount_CDROM_here(mountdev, MNT_CDROM); } 2423 cd_number = atoi(last_line_of_file(MNT_CDROM"/archives/THIS-CD-NUMBER")); 2611 paranoid_free(mountdev); 2612 paranoid_free(tmp); 2613 return (cd_number); 2614 } 2615 2616 strcpy(mountdev, bkpinfo->media_device); 2617 if (!mountdev[0]) { 2618 log_it 2619 ("(what_number_cd_is_this) Warning - media_device unknown. Finding out..."); 2620 find_cdrom_device(bkpinfo->media_device, FALSE); 2621 } 2622 if (!is_this_device_mounted(MNT_CDROM)) { 2623 mount_CDROM_here(mountdev, MNT_CDROM); 2624 } 2625 cd_number = 2626 atoi(last_line_of_file(MNT_CDROM "/archives/THIS-CD-NUMBER")); 2424 2627 // log_it("cd_number..later.. = %d", cd_number); 2425 paranoid_free(mountdev);2426 paranoid_free(tmp);2427 return(cd_number);2628 paranoid_free(mountdev); 2629 paranoid_free(tmp); 2630 return (cd_number); 2428 2631 } 2429 2632 … … 2441 2644 * The mountpoint (where it's mounted) will obviously be '/'. 2442 2645 */ 2443 char * 2444 where_is_root_mounted () 2445 { 2446 /*@ buffers *****************/ 2447 static char tmp[MAX_STR_LEN]; 2646 char *where_is_root_mounted() 2647 { 2648 /*@ buffers **************** */ 2649 static char tmp[MAX_STR_LEN]; 2448 2650 2449 2651 2450 2652 #ifdef __FreeBSD__ 2451 strcpy(tmp, call_program_and_get_last_line_of_output2452 2653 strcpy(tmp, call_program_and_get_last_line_of_output 2654 ("mount | grep \" on / \" | cut -d' ' -f1")); 2453 2655 #else 2454 strcpy (tmp, call_program_and_get_last_line_of_output 2455 ("mount | grep \" on / \" | cut -d' ' -f1 | sed s/[0-9]// | sed s/[0-9]//")); 2456 if (strstr(tmp, "/dev/cciss/")) { 2457 strcpy (tmp, call_program_and_get_last_line_of_output 2458 ("mount | grep \" on / \" | cut -d' ' -f1 | cut -dp -f1")); 2459 } 2460 if (strstr(tmp, "/dev/md")) 2461 { 2462 strcpy(tmp, 2463 call_program_and_get_last_line_of_output 2464 ("mount | grep \" on / \" | cut -d' ' -f1")); 2465 } 2466 #endif 2467 2468 return (tmp); 2656 strcpy(tmp, call_program_and_get_last_line_of_output 2657 ("mount | grep \" on / \" | cut -d' ' -f1 | sed s/[0-9]// | sed s/[0-9]//")); 2658 if (strstr(tmp, "/dev/cciss/")) { 2659 strcpy(tmp, call_program_and_get_last_line_of_output 2660 ("mount | grep \" on / \" | cut -d' ' -f1 | cut -dp -f1")); 2661 } 2662 if (strstr(tmp, "/dev/md")) { 2663 strcpy(tmp, 2664 call_program_and_get_last_line_of_output 2665 ("mount | grep \" on / \" | cut -d' ' -f1")); 2666 } 2667 #endif 2668 2669 return (tmp); 2469 2670 } 2470 2671 … … 2477 2678 */ 2478 2679 #ifdef __FreeBSD__ 2479 char which_boot_loader (char *which_device) 2480 { 2481 int count_lilos = 0; 2482 int count_grubs = 0; 2483 int count_boot0s = 0; 2484 int count_dangerouslydedicated = 0; 2485 2486 log_it ("looking at drive %s's MBR", which_device); 2487 if (does_string_exist_in_boot_block (which_device, "GRUB")) 2488 { count_grubs++; } 2489 if (does_string_exist_in_boot_block (which_device, "LILO")) 2490 { count_lilos++; } 2491 if (does_string_exist_in_boot_block (which_device, "Drive")) 2492 { count_boot0s++; } 2493 if (does_string_exist_in_first_N_blocks (which_device, "FreeBSD/i386", 17)) 2494 { count_dangerouslydedicated++; } 2495 log_it ("%d grubs and %d lilos and %d elilos and %d boot0s and %d DD\n", count_grubs, count_lilos, count_elilos, count_boot0s, count_dangerouslydedicated); 2496 2497 if (count_grubs && !count_lilos) 2498 { return ('G'); } 2499 else if (count_lilos && !count_grubs) 2500 { return ('L'); } 2501 else if (count_grubs == 1 && count_lilos == 1) 2502 { 2503 log_it ("I'll bet you used to use LILO but switched to GRUB..."); 2504 return ('G'); 2505 } 2506 else if (count_boot0s == 1) 2507 { return ('B'); } 2508 else if (count_dangerouslydedicated) 2509 { return ('D'); } 2510 else 2511 { 2512 log_it ("Unknown boot loader"); 2513 return ('U'); 2514 } 2680 char which_boot_loader(char *which_device) 2681 { 2682 int count_lilos = 0; 2683 int count_grubs = 0; 2684 int count_boot0s = 0; 2685 int count_dangerouslydedicated = 0; 2686 2687 log_it("looking at drive %s's MBR", which_device); 2688 if (does_string_exist_in_boot_block(which_device, "GRUB")) { 2689 count_grubs++; 2690 } 2691 if (does_string_exist_in_boot_block(which_device, "LILO")) { 2692 count_lilos++; 2693 } 2694 if (does_string_exist_in_boot_block(which_device, "Drive")) { 2695 count_boot0s++; 2696 } 2697 if (does_string_exist_in_first_N_blocks 2698 (which_device, "FreeBSD/i386", 17)) { 2699 count_dangerouslydedicated++; 2700 } 2701 log_it("%d grubs and %d lilos and %d elilos and %d boot0s and %d DD\n", 2702 count_grubs, count_lilos, count_elilos, count_boot0s, 2703 count_dangerouslydedicated); 2704 2705 if (count_grubs && !count_lilos) { 2706 return ('G'); 2707 } else if (count_lilos && !count_grubs) { 2708 return ('L'); 2709 } else if (count_grubs == 1 && count_lilos == 1) { 2710 log_it("I'll bet you used to use LILO but switched to GRUB..."); 2711 return ('G'); 2712 } else if (count_boot0s == 1) { 2713 return ('B'); 2714 } else if (count_dangerouslydedicated) { 2715 return ('D'); 2716 } else { 2717 log_it("Unknown boot loader"); 2718 return ('U'); 2719 } 2515 2720 } 2516 2721 2517 2722 #else 2518 2723 2519 char which_boot_loader 2520 { 2521 /*@ buffer ***************************************************** */2522 2523 2524 2525 /*@ pointers *************************************************** */2526 2527 2528 /*@ int ******************************************************** */2529 2530 2531 2532 /*@ end vars *************************************************** */2533 2534 2535 2724 char which_boot_loader(char *which_device) 2725 { 2726 /*@ buffer ***************************************************** */ 2727 char *list_drives_cmd; 2728 char *current_drive; 2729 2730 /*@ pointers *************************************************** */ 2731 FILE *pdrives; 2732 2733 /*@ int ******************************************************** */ 2734 int count_lilos = 0; 2735 int count_grubs = 0; 2736 2737 /*@ end vars *************************************************** */ 2738 2739 malloc_string(list_drives_cmd); 2740 malloc_string(current_drive); 2536 2741 2537 2742 #ifdef __IA64__ 2538 /* No choice for it */ 2539 return ('E'); 2540 #endif 2541 assert(which_device!=NULL); 2542 // sprintf (list_drives_cmd, 2543 // "fdisk -l | grep /dev | grep cyl | tr ':' ' ' | cut -d' ' -f2"); 2544 2545 sprintf (list_drives_cmd, 2546 // "parted2fdisk 2547 "fdisk -l 2>/dev/null | grep \"/dev/.*:\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/; echo %s", where_is_root_mounted()); 2548 log_it("list_drives_cmd = %s", list_drives_cmd); 2549 2550 if (!(pdrives = popen (list_drives_cmd, "r"))) 2551 { 2552 log_OS_error ("Unable to open list of drives"); 2553 paranoid_free(list_drives_cmd); 2554 paranoid_free(current_drive); 2555 return ('\0'); 2556 } 2557 for (fgets (current_drive, MAX_STR_LEN, pdrives); !feof (pdrives); 2558 fgets (current_drive, MAX_STR_LEN, pdrives)) 2559 { 2560 strip_spaces (current_drive); 2561 log_it ("looking at drive %s's MBR", current_drive); 2562 if (does_string_exist_in_boot_block (current_drive, "GRUB")) 2563 { 2564 count_grubs++; 2565 strcpy (which_device, current_drive); 2566 break; 2567 } 2568 if (does_string_exist_in_boot_block (current_drive, "LILO")) 2569 { 2570 count_lilos++; 2571 strcpy (which_device, current_drive); 2572 break; 2573 } 2574 } 2575 if (pclose (pdrives)) { log_OS_error("Cannot pclose pdrives"); } 2576 log_it ("%d grubs and %d lilos\n", count_grubs, count_lilos); 2577 paranoid_free(list_drives_cmd); 2578 paranoid_free(current_drive); 2579 if (count_grubs && !count_lilos) 2580 { 2581 return ('G'); 2582 } 2583 else if (count_lilos && !count_grubs) 2584 { 2585 return ('L'); 2586 } 2587 else if (count_grubs == 1 && count_lilos == 1) 2588 { 2589 log_it ("I'll bet you used to use LILO but switched to GRUB..."); 2590 return ('G'); 2591 } 2592 else 2593 { 2594 log_it ("Unknown boot loader"); 2595 return ('U'); 2596 } 2743 /* No choice for it */ 2744 return ('E'); 2745 #endif 2746 assert(which_device != NULL); 2747 // sprintf (list_drives_cmd, 2748 // "fdisk -l | grep /dev | grep cyl | tr ':' ' ' | cut -d' ' -f2"); 2749 2750 sprintf(list_drives_cmd, 2751 // "parted2fdisk 2752 "fdisk -l 2>/dev/null | grep \"/dev/.*:\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/; echo %s", 2753 where_is_root_mounted()); 2754 log_it("list_drives_cmd = %s", list_drives_cmd); 2755 2756 if (!(pdrives = popen(list_drives_cmd, "r"))) { 2757 log_OS_error("Unable to open list of drives"); 2758 paranoid_free(list_drives_cmd); 2759 paranoid_free(current_drive); 2760 return ('\0'); 2761 } 2762 for (fgets(current_drive, MAX_STR_LEN, pdrives); !feof(pdrives); 2763 fgets(current_drive, MAX_STR_LEN, pdrives)) { 2764 strip_spaces(current_drive); 2765 log_it("looking at drive %s's MBR", current_drive); 2766 if (does_string_exist_in_boot_block(current_drive, "GRUB")) { 2767 count_grubs++; 2768 strcpy(which_device, current_drive); 2769 break; 2770 } 2771 if (does_string_exist_in_boot_block(current_drive, "LILO")) { 2772 count_lilos++; 2773 strcpy(which_device, current_drive); 2774 break; 2775 } 2776 } 2777 if (pclose(pdrives)) { 2778 log_OS_error("Cannot pclose pdrives"); 2779 } 2780 log_it("%d grubs and %d lilos\n", count_grubs, count_lilos); 2781 paranoid_free(list_drives_cmd); 2782 paranoid_free(current_drive); 2783 if (count_grubs && !count_lilos) { 2784 return ('G'); 2785 } else if (count_lilos && !count_grubs) { 2786 return ('L'); 2787 } else if (count_grubs == 1 && count_lilos == 1) { 2788 log_it("I'll bet you used to use LILO but switched to GRUB..."); 2789 return ('G'); 2790 } else { 2791 log_it("Unknown boot loader"); 2792 return ('U'); 2793 } 2597 2794 } 2598 2795 #endif … … 2606 2803 * @return 0 for success, 1 for failure. 2607 2804 */ 2608 int 2609 zero_out_a_device (char *device) 2610 { 2611 FILE *fout; 2612 int i; 2613 2614 assert_string_is_neither_NULL_nor_zerolength(device); 2615 2616 log_it ("Zeroing drive %s", device); 2617 if (!(fout=fopen (device, "w"))) 2618 { 2619 log_OS_error ("Unable to open/write to device"); 2620 return (1); 2621 } 2622 for (i = 0; i < 16384; i++) 2623 { 2624 fputc ('\0', fout); 2625 } 2626 paranoid_fclose(fout); 2627 log_it ("Device successfully zeroed."); 2628 return (0); 2805 int zero_out_a_device(char *device) 2806 { 2807 FILE *fout; 2808 int i; 2809 2810 assert_string_is_neither_NULL_nor_zerolength(device); 2811 2812 log_it("Zeroing drive %s", device); 2813 if (!(fout = fopen(device, "w"))) { 2814 log_OS_error("Unable to open/write to device"); 2815 return (1); 2816 } 2817 for (i = 0; i < 16384; i++) { 2818 fputc('\0', fout); 2819 } 2820 paranoid_fclose(fout); 2821 log_it("Device successfully zeroed."); 2822 return (0); 2629 2823 } 2630 2824 … … 2636 2830 * @bug Won't work with file v4.0; needs to be written in C. 2637 2831 */ 2638 char *resolve_softlinks_to_get_to_actual_device_file(char *incoming)2639 { 2640 2641 2642 2643 2644 2645 2646 2647 struct stat statbuf; 2648 2649 2650 2651 2652 if (!does_file_exist(incoming)) 2653 { 2654 log_it("resolve_softlinks_to_get_to_actual_device_file --- device not found");2655 2656 } 2657 else 2658 { 2659 strcpy(curr_fname, incoming); 2660 lstat(curr_fname, &statbuf);2661 while(S_ISLNK(statbuf.st_mode)) 2662 { 2663 log_msg(1, "curr_fname = %s", curr_fname);2664 sprintf(command, "file %s", curr_fname);2665 strcpy(tmp, call_program_and_get_last_line_of_output(command));2666 for(p=tmp + strlen(tmp); p != tmp && *p != '`' && *p != ' '; p--);2667 p++;2668 strcpy(scratch, p);2669 for(p=scratch; *p != '\0' && *p != '\''; p++);2670 *p = '\0';2671 log_msg(0, "curr_fname %s --> '%s' --> %s", curr_fname, tmp, scratch);2672 if (scratch[0] == '/')2673 {2674 strcpy(curr_fname, scratch); // copy whole thing because it's an absolute softlink2675 }2676 else2677 { // copy over the basename cos it's a relative softlink2678 p = curr_fname + strlen(curr_fname);2679 while(p!=curr_fname && *p != '/') { p--;}2680 if (*p == '/') { p++;}2681 2682 2683 2684 }2685 2686 2687 2688 2689 2690 2691 return(output);2832 char *resolve_softlinks_to_get_to_actual_device_file(char *incoming) 2833 { 2834 static char output[MAX_STR_LEN]; 2835 char *command; 2836 char *curr_fname; 2837 char *scratch; 2838 char *tmp; 2839 char *p; 2840 2841 struct stat statbuf; 2842 command = malloc(1000); 2843 malloc_string(tmp); 2844 malloc_string(scratch); 2845 malloc_string(curr_fname); 2846 if (!does_file_exist(incoming)) { 2847 log_it 2848 ("resolve_softlinks_to_get_to_actual_device_file --- device not found"); 2849 strcpy(output, incoming); 2850 } else { 2851 strcpy(curr_fname, incoming); 2852 lstat(curr_fname, &statbuf); 2853 while (S_ISLNK(statbuf.st_mode)) { 2854 log_msg(1, "curr_fname = %s", curr_fname); 2855 sprintf(command, "file %s", curr_fname); 2856 strcpy(tmp, call_program_and_get_last_line_of_output(command)); 2857 for (p = tmp + strlen(tmp); p != tmp && *p != '`' && *p != ' '; 2858 p--); 2859 p++; 2860 strcpy(scratch, p); 2861 for (p = scratch; *p != '\0' && *p != '\''; p++); 2862 *p = '\0'; 2863 log_msg(0, "curr_fname %s --> '%s' --> %s", curr_fname, tmp, 2864 scratch); 2865 if (scratch[0] == '/') { 2866 strcpy(curr_fname, scratch); // copy whole thing because it's an absolute softlink 2867 } else { // copy over the basename cos it's a relative softlink 2868 p = curr_fname + strlen(curr_fname); 2869 while (p != curr_fname && *p != '/') { 2870 p--; 2871 } 2872 if (*p == '/') { 2873 p++; 2874 } 2875 strcpy(p, scratch); 2876 } 2877 lstat(curr_fname, &statbuf); 2878 } 2879 strcpy(output, curr_fname); 2880 log_it("resolved %s to %s", incoming, output); 2881 } 2882 paranoid_free(command); 2883 paranoid_free(curr_fname); 2884 paranoid_free(tmp); 2885 return (output); 2692 2886 } 2693 2887 … … 2700 2894 char *which_partition_format(const char *drive) 2701 2895 { 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2896 static char output[4]; 2897 char *tmp; 2898 char *command; 2899 char *fdisk; 2900 struct stat buf; 2901 2902 malloc_string(tmp); 2903 malloc_string(command); 2904 malloc_string(fdisk); 2905 log_msg(0, "Looking for partition table format type"); 2712 2906 // BERLIOS: Do that temporarily: we need to put back parted2fdisk everywhere 2713 2907 #ifdef __IA64__ 2714 2715 2716 #endif 2717 2908 sprintf(fdisk, "/usr/local/bin/fdisk"); 2909 if (stat(fdisk, &buf) != 0) { 2910 #endif 2911 sprintf(fdisk, "/sbin/fdisk"); 2718 2912 #ifdef __IA64__ 2719 2720 #endif 2721 log_msg(1, "Using %s",fdisk);2722 2723 2724 if (strstr(tmp,"GPT") == NULL) {2725 strcpy(output,"MBR");2726 2727 strcpy(output,"GPT");2728 2729 log_msg(0, "Found %s partition table format type",output);2730 2731 2732 2733 return(output);2913 } 2914 #endif 2915 log_msg(1, "Using %s", fdisk); 2916 sprintf(command, "%s -l %s | grep 'EFI GPT'", fdisk, drive); 2917 strcpy(tmp, call_program_and_get_last_line_of_output(command)); 2918 if (strstr(tmp, "GPT") == NULL) { 2919 strcpy(output, "MBR"); 2920 } else { 2921 strcpy(output, "GPT"); 2922 } 2923 log_msg(0, "Found %s partition table format type", output); 2924 paranoid_free(command); 2925 paranoid_free(tmp); 2926 paranoid_free(fdisk); 2927 return (output); 2734 2928 } 2735 2929 2736 2930 /* @} - end of deviceGroup */ 2737
Note:
See TracChangeset
for help on using the changeset viewer.