source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-devices.c@ 2324

Last change on this file since 2324 was 2324, checked in by Bruno Cornec, 15 years ago

r3335@localhost: bruno | 2009-08-08 23:04:12 +0200

  • Change mr_asprintf to avoid the need of pointer and be consistent with the other mr_mem functions
  • Property svn:keywords set to Id
File size: 69.6 KB
Line 
1/* libmondo-devices.c Subroutines for handling devices
2 $Id: libmondo-devices.c 2324 2009-08-18 13:13:54Z bruno $
3*/
4
5/**
6 * @file
7 * Functions to handle interactions with backup devices.
8 */
9
10#include "my-stuff.h"
11#include "mr_mem.h"
12#include "mondostructures.h"
13#include "libmondo-files-EXT.h"
14#include "libmondo-devices.h"
15#include "lib-common-externs.h"
16#include "libmondo-string-EXT.h"
17#include "libmondo-tools-EXT.h"
18#include "libmondo-gui-EXT.h"
19#include "libmondo-fork-EXT.h"
20#include "libmondo-stream-EXT.h"
21
22extern void mr_strip_spaces(char *);
23
24#include <sys/types.h>
25#ifdef __FreeBSD__
26#define DKTYPENAMES
27#define FSTYPENAMES
28#include <sys/disklabel.h>
29#include <sys/disk.h>
30#elif linux
31#define u64 unsigned long long
32#include <linux/fs.h> /* for BLKGETSIZE64 */
33#include <linux/hdreg.h>
34#endif
35
36/*@unused@*/
37//static char cvsid[] = "$Id: libmondo-devices.c 2324 2009-08-18 13:13:54Z bruno $";
38
39extern int g_current_media_number;
40extern double g_kernel_version;
41
42extern bool g_ISO_restore_mode;
43extern char *g_selfmounted_isodir;
44extern char *MONDO_LOGFILE;
45
46static char g_cdrw_drive_is_here[MAX_STR_LEN / 4] = "";
47static char g_cdrom_drive_is_here[MAX_STR_LEN / 4] = "";
48static char g_dvd_drive_is_here[MAX_STR_LEN / 4] = "";
49
50
51/**
52 * ????? @bug ?????
53 * @ingroup globalGroup
54 */
55bool g_restoring_live_from_cd = FALSE;
56bool g_restoring_live_from_nfs = FALSE;
57
58extern t_bkptype g_backup_media_type; // set by main()
59
60/* Reference to global bkpinfo */
61extern struct s_bkpinfo *bkpinfo;
62
63
64
65
66void set_g_cdrom_and_g_dvd_to_bkpinfo_value()
67{
68 strcpy(g_cdrom_drive_is_here, bkpinfo->media_device); // just in case
69 strcpy(g_dvd_drive_is_here, bkpinfo->media_device); // just in case
70}
71
72
73
74/**
75 * Retract all CD trays and wait for autorun to complete.
76 * @ingroup deviceGroup
77 */
78void retract_CD_tray_and_defeat_autorun(void)
79{
80// log_it("rctada: Retracting all CD trays", __LINE__);
81 if (strlen(g_cdrom_drive_is_here) > 0) {
82 inject_device(g_cdrom_drive_is_here);
83 }
84 if (strlen(g_dvd_drive_is_here) > 0) {
85 inject_device(g_dvd_drive_is_here);
86 }
87 if (strlen(g_cdrw_drive_is_here) > 0) {
88 inject_device(g_cdrw_drive_is_here);
89 }
90// log_it("rctada: killing autorun");
91// run_program_and_log_output("killall autorun", TRUE);
92 if (!run_program_and_log_output("ps | grep autorun | grep -v grep", 5)) {
93 log_it("autorun detected; sleeping for 2 seconds");
94 sleep(2);
95 }
96 log_it("rctada: Unmounting all CD drives", __LINE__);
97 run_program_and_log_output("umount /dev/cdr* /dev/dvd*", 5);
98}
99
100
101
102/**
103 * Determine whether we're booted off a ramdisk.
104 * @return @c TRUE (we are) or @c FALSE (we aren't).
105 * @ingroup utilityGroup
106 */
107bool am_I_in_disaster_recovery_mode(void)
108{
109 char *tmp = NULL;
110 bool is_this_a_ramdisk = FALSE;
111
112 mr_asprintf(tmp, "%s", where_is_root_mounted());
113 log_msg(0, "root is currently mounted at %s\n", tmp);
114
115#ifdef __FreeBSD__
116 if (strstr(tmp, "/dev/md")) {
117 is_this_a_ramdisk = TRUE;
118 }
119#else
120 if (!strncmp(tmp, "/dev/ram", 8)
121 || (!strncmp(tmp, "/dev/rd", 7) && !strcmp(tmp, "/dev/rd/")
122 && strncmp(tmp, "/dev/rd/cd", 10)) || strstr(tmp, "rootfs")
123 || !strcmp(tmp, "/dev/root")) {
124 is_this_a_ramdisk = TRUE;
125 } else {
126 is_this_a_ramdisk = FALSE;
127 }
128#endif
129 mr_free(tmp);
130
131 if (is_this_a_ramdisk) {
132 if (!does_file_exist("/THIS-IS-A-RAMDISK")) {
133 log_to_screen("Using /dev/root is stupid of you but I'll forgive you.");
134 is_this_a_ramdisk = FALSE;
135 }
136 }
137 if (does_file_exist("/THIS-IS-A-RAMDISK")) {
138 is_this_a_ramdisk = TRUE;
139 }
140 log_msg(1, "Is this a ramdisk? result = %s", (is_this_a_ramdisk) ? "TRUE" : "FALSE");
141 return (is_this_a_ramdisk);
142}
143
144
145
146
147
148/**
149 * Turn @c bkpinfo->backup_media_type into a human-readable string.
150 * @return The human readable string (e.g. @c cdr becomes <tt>"cdr"</tt>).
151 * @note The returned string points to static storage that will be overwritten with each call.
152 * @ingroup stringGroup
153 */
154static char *bkptype_to_string(t_bkptype bt)
155{
156 static char output[MAX_STR_LEN / 4];
157 switch (bt) {
158 case none:
159 strcpy(output, "none");
160 break;
161 case iso:
162 strcpy(output, "iso");
163 break;
164 case cdr:
165 strcpy(output, "cdr");
166 break;
167 case cdrw:
168 strcpy(output, "cdrw");
169 break;
170 case cdstream:
171 strcpy(output, "cdstream");
172 break;
173 case nfs:
174 strcpy(output, "nfs");
175 break;
176 case tape:
177 strcpy(output, "tape");
178 break;
179 case udev:
180 strcpy(output, "udev");
181 break;
182 case usb:
183 strcpy(output, "usb");
184 break;
185 default:
186 strcpy(output, "default");
187 }
188 return (output);
189}
190
191
192
193/**
194 * @addtogroup deviceGroup
195 * @{
196 */
197/**
198 * Eject the tray of the specified CD device.
199 * @param dev The device to eject.
200 * @return the return value of the @c eject command. (0=success, nonzero=failure)
201 */
202int eject_device(char *dev)
203{
204 char *command = NULL;
205 int res1 = 0, res2 = 0;
206
207 if (IS_THIS_A_STREAMING_BACKUP(g_backup_media_type)
208 && g_backup_media_type != udev) {
209 mr_asprintf(command, "mt -f %s offline", dev);
210 res1 = run_program_and_log_output(command, 1);
211 mr_free(command);
212 } else {
213 res1 = 0;
214 }
215
216#ifdef __FreeBSD__
217 if (strstr(dev, "acd")) {
218 mr_asprintf(command, "cdcontrol -f %s eject", dev);
219 } else {
220 mr_asprintf(command, "camcontrol eject `echo %s | sed 's|/dev/||'`", dev);
221 }
222#else
223 mr_asprintf(command, "eject %s", dev);
224#endif
225
226 log_msg(3, "Ejecting %s", dev);
227 res2 = run_program_and_log_output(command, 1);
228 mr_free(command);
229 if (res1 && res2) {
230 return (1);
231 } else {
232 return (0);
233 }
234}
235
236/**
237 * Load (inject) the tray of the specified CD device.
238 * @param dev The device to load/inject.
239 * @return 0 for success, nonzero for failure.
240 */
241int inject_device(char *dev)
242{
243 char *command = NULL;
244 int i;
245
246#ifdef __FreeBSD__
247 if (strstr(dev, "acd")) {
248 mr_asprintf(command, "cdcontrol -f %s close", dev);
249 } else {
250 mr_asprintf(command, "camcontrol load `echo %s | sed 's|/dev/||'`", dev);
251 }
252#else
253 mr_asprintf(command, "eject -t %s", dev);
254#endif
255 i = run_program_and_log_output(command, FALSE);
256 mr_free(command);
257 return (i);
258}
259
260
261/**
262 * Determine whether the specified @p device (really, you can use any file)
263 * exists.
264 * @return TRUE if it exists, FALSE if it doesn't.
265 */
266bool does_device_exist(char *device)
267{
268
269 /*@ buffers *********************************************************** */
270 char *tmp = NULL;
271 bool ret;
272
273 assert_string_is_neither_NULL_nor_zerolength(device);
274
275 mr_asprintf(tmp, "ls %s > /dev/null 2> /dev/null", device);
276
277 if (system(tmp)) {
278 ret = FALSE;
279 } else {
280 ret = TRUE;
281 }
282 mr_free(tmp);
283 return (ret);
284}
285
286
287/**
288 * Determine whether a non-Microsoft partition exists on any connected hard drive.
289 * @return TRUE (there's a Linux/FreeBSD partition) or FALSE (Microsoft has taken over yet another innocent PC).
290 */
291bool does_nonMS_partition_exist(void)
292{
293#if __FreeBSD__
294 return
295 !system
296 ("for drive in /dev/ad? /dev/da?; do fdisk $drive | grep -q FreeBSD && exit 0; done; false");
297#else
298 return
299 !system
300 ("parted2fdisk -l 2>/dev/null | grep '^/dev/' | grep -Eqv '(MS|DOS|FAT|NTFS)'");
301#endif
302}
303
304/**
305 * Determine whether the specified @p partno exists on the specified @p drive.
306 * @param drive The drive to search for the partition in.
307 * @param partno The partition number to look for.
308 * @return 0 if it exists, nonzero otherwise.
309 */
310int does_partition_exist(const char *drive, int partno)
311{
312 /*@ buffers **************************************************** */
313 char *program = NULL;
314 char *incoming;
315 char *searchstr = NULL;
316 char *tmp = NULL;
317
318 /*@ ints ******************************************************* */
319 int res = 0;
320
321 /*@ pointers *************************************************** */
322 FILE *fin;
323
324
325 /*@ end vars *************************************************** */
326 assert_string_is_neither_NULL_nor_zerolength(drive);
327 assert(partno >= 0 && partno < 999);
328
329 malloc_string(incoming);
330
331#ifdef __FreeBSD__
332 // We assume here that this is running from mondorestore. (It is.)
333 tmp = build_partition_name(drive, partno);
334 mr_asprintf(program, "ls %s %s >/dev/null 2>&1", drive, tmp);
335 mr_free(tmp);
336
337 res = system(program);
338 mr_free(program);
339 return (res);
340#else
341 /* To avoid compiler warnings */
342 tmp = NULL;
343#endif
344
345 mr_asprintf(program, "parted2fdisk -l %s 2> /dev/null", drive);
346 fin = popen(program, "r");
347 if (!fin) {
348 log_it("program=%s", program);
349 log_OS_error("Cannot popen-in program");
350 mr_free(program);
351 return (0);
352 }
353 mr_free(program);
354
355 searchstr = build_partition_name(drive, partno);
356 mr_strcat(searchstr, " ");
357 for (res = 0; !res && fgets(incoming, MAX_STR_LEN - 1, fin);) {
358 if (strstr(incoming, searchstr)) {
359 res = 1;
360 }
361 }
362 mr_free(searchstr);
363
364 if (pclose(fin)) {
365 log_OS_error("Cannot pclose fin");
366 }
367 paranoid_free(incoming);
368 return (res);
369}
370
371
372
373
374
375/**
376 * Determine whether given NULL-terminated @p str exists in the MBR of @p dev.
377 * @param dev The device to look in.
378 * @param str The string to look for.
379 * @return TRUE if it exists, FALSE if it doesn't.
380 */
381bool does_string_exist_in_boot_block(char *dev, char *str)
382{
383 /*@ buffers **************************************************** */
384 char *command = NULL;
385
386 /*@ end vars *************************************************** */
387 int i;
388
389 assert_string_is_neither_NULL_nor_zerolength(dev);
390 assert_string_is_neither_NULL_nor_zerolength(str);
391
392 mr_asprintf(command, "dd if=%s bs=446 count=1 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null", dev, str);
393 i = system(command);
394 mr_free(command);
395 if (i) {
396 return (FALSE);
397 } else {
398 return (TRUE);
399 }
400}
401
402/**
403 * Determine whether specified @p str exists in the first @p n sectors of
404 * @p dev.
405 * @param dev The device to look in.
406 * @param str The string to look for.
407 * @param n The number of 512-byte sectors to search.
408 */
409bool does_string_exist_in_first_N_blocks(char *dev, char *str, int n)
410{
411 /*@ buffers **************************************************** */
412 char *command = NULL;
413 /*@ end vars *************************************************** */
414 int i;
415
416 mr_asprintf(command, "dd if=%s bs=512 count=%i 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null", dev, n, str);
417 i = system(command);
418 mr_free(command);
419 if (i) {
420 return (FALSE);
421 } else {
422 return (TRUE);
423 }
424}
425
426
427
428/**
429 * Try to mount CD-ROM at @p mountpoint. If the CD-ROM is not found or has
430 * not been specified, call find_cdrom_device() to find it.
431 * @param bkpinfo The backup information structure. The only field used is @c bkpinfo->media_device.
432 * @param mountpoint Where to mount the CD-ROM.
433 * @return 0 for success, nonzero for failure.
434 * @see mount_CDROM_here
435 */
436int find_and_mount_actual_cd(char *mountpoint)
437{
438 /*@ buffers ***************************************************** */
439
440 /*@ int's ****************************************************** */
441 int res;
442 char *dev;
443 char *p = NULL;
444
445 /*@ end vars **************************************************** */
446
447 malloc_string(dev);
448 assert(bkpinfo != NULL);
449 assert_string_is_neither_NULL_nor_zerolength(mountpoint);
450
451 if (g_backup_media_type == dvd) {
452 strcpy(dev, g_dvd_drive_is_here);
453 if (!dev[0]) {
454 find_dvd_device(dev, FALSE);
455 }
456 } else {
457 strcpy(dev, g_cdrom_drive_is_here);
458 if (!dev[0]) {
459 find_cdrom_device(dev, FALSE);
460 }
461 }
462
463 if (bkpinfo->backup_media_type != iso) {
464 retract_CD_tray_and_defeat_autorun();
465 }
466
467 if (!dev[0] || (res = mount_CDROM_here(dev, mountpoint))) {
468 p = popup_and_get_string ("CD-ROM device", "Please enter your CD-ROM's /dev device",dev);
469 if (p == NULL) {
470 res = 1;
471 } else {
472 res = mount_CDROM_here(p, mountpoint);
473 }
474 mr_free(p);
475 }
476 if (res) {
477 log_msg(1, "mount failed");
478 } else {
479 log_msg(1, "mount succeeded with %s", dev);
480 }
481 paranoid_free(dev);
482 return (res);
483}
484
485
486
487
488
489
490/**
491 * Locate a CD-R/W writer's SCSI node.
492 * @param cdrw_device SCSI node will be placed here.
493 * @return 0 for success, nonzero for failure.
494 */
495int find_cdrw_device(char *cdrw_device)
496{
497 /*@ buffers ************************ */
498 char *tmp = NULL;
499 char *cdr_exe = NULL;
500 char *command = NULL;
501
502 if (g_cdrw_drive_is_here[0]) {
503 strcpy(cdrw_device, g_cdrw_drive_is_here);
504 log_msg(3, "Been there, done that. Returning %s", cdrw_device);
505 return (0);
506 }
507 if (g_backup_media_type == dvd) {
508 log_msg(1,
509 "This is dumb. You're calling find_cdrw_device() but you're backing up to DVD. WTF?");
510 return (1);
511 }
512 run_program_and_log_output("insmod ide-scsi", -1);
513 if (find_home_of_exe("cdrecord")) {
514 mr_asprintf(cdr_exe, "cdrecord");
515 } else {
516 mr_asprintf(cdr_exe, "dvdrecord");
517 }
518 if (find_home_of_exe(cdr_exe)) {
519 mr_asprintf(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);
520 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
521 mr_free(command);
522 }
523 if ((tmp == NULL) || (strlen(tmp) < 2)) {
524 mr_free(tmp);
525 mr_free(cdr_exe);
526 return 1;
527 } else {
528 strcpy(cdrw_device, tmp);
529 log_it("Found CDRW device - %s", cdrw_device);
530 strcpy(g_cdrw_drive_is_here, cdrw_device);
531 mr_free(tmp);
532 mr_free(cdr_exe);
533 return (0);
534 }
535}
536
537
538/**
539 * Attempt to locate a CD-ROM device's /dev entry.
540 * Several different methods may be used to find the device, including
541 * calling @c cdrecord, searching @c dmesg, and trial-and-error.
542 * @param output Where to put the located /dev entry.
543 * @param try_to_mount Whether to mount the CD as part of the test; if mount
544 * fails then return failure.
545 * @return 0 for success, nonzero for failure.
546 */
547int find_cdrom_device(char *output, bool try_to_mount)
548{
549 /*@ pointers **************************************************** */
550 FILE *fin;
551 char *p;
552 char *q;
553 char *r;
554 int retval = 0;
555
556 /*@ bool's ****************************************************** */
557 bool found_it = FALSE;
558
559 /*@ buffers ***************************************************** */
560 char *tmp;
561 char *tmp1 = NULL;
562 char *cdr_exe = NULL;
563 char *phrase_one;
564 char *phrase_two;
565 char *command = NULL;
566#ifndef __FreeBSD__
567 char *dvd_last_resort = NULL;
568#endif
569 char *mountpoint = NULL;
570 static char the_last_place_i_found_it[MAX_STR_LEN] = "";
571
572 /*@ intialize *************************************************** */
573 malloc_string(tmp);
574 malloc_string(phrase_one);
575 malloc_string(phrase_two);
576
577 output[0] = '\0';
578 phrase_one[0] = '\0';
579 phrase_two[0] = '\0';
580#ifndef __FreeBSD__
581 mr_asprintf(dvd_last_resort, "%s", "");;
582#endif
583
584 /*@ end vars **************************************************** */
585
586 if (g_cdrom_drive_is_here[0] && !isdigit(g_cdrom_drive_is_here[0])) {
587 strcpy(output, g_cdrom_drive_is_here);
588 log_msg(3, "Been there, done that. Returning %s", output);
589 retval = 0;
590 goto end_of_find_cdrom_device;
591 }
592 if (the_last_place_i_found_it[0] != '\0' && !try_to_mount) {
593 strcpy(output, the_last_place_i_found_it);
594 log_msg(3,
595 "find_cdrom_device() --- returning last found location - '%s'",
596 output);
597 retval = 0;
598 goto end_of_find_cdrom_device;
599 }
600
601 mr_asprintf(mountpoint, "%s/cd.mnt", bkpinfo->tmpdir);
602 make_hole_for_dir(mountpoint);
603
604 if (find_home_of_exe("cdrecord")) {
605 mr_asprintf(cdr_exe, "cdrecord");
606 } else {
607 mr_asprintf(cdr_exe, "dvdrecord");
608 }
609 tmp[0] = '\0';
610 if (!find_home_of_exe(cdr_exe)) {
611 strcpy(output, "/dev/cdrom");
612 log_msg(4, "Can't find cdrecord; assuming %s", output);
613 if (!does_device_exist(output)) {
614 log_msg(4, "That didn't work. Sorry.");
615 retval = 1;
616 goto end_of_find_cdrom_device;
617 } else {
618 retval = 0;
619 goto end_of_find_cdrom_device;
620 }
621 }
622
623 mr_asprintf(command, "%s -scanbus 2> /dev/null", cdr_exe);
624 fin = popen(command, "r");
625 log_msg(4, "command=%s", command);
626 mr_free(command);
627
628 if (!fin) {
629 log_OS_error("Cannot popen command");
630 mr_free(cdr_exe);
631 return (1);
632 }
633
634 for ((void)fgets(tmp, MAX_STR_LEN, fin); !feof(fin);
635 (void)fgets(tmp, MAX_STR_LEN, fin)) {
636 p = strchr(tmp, '\'');
637 if (p) {
638 q = strchr(++p, '\'');
639 if (q) {
640 for (r = q; *(r - 1) == ' '; r--);
641 *r = '\0';
642 strcpy(phrase_one, p);
643 p = strchr(++q, '\'');
644 if (p) {
645 q = strchr(++p, '\'');
646 if (q) {
647 while (*(q - 1) == ' ') {
648 q--;
649 }
650 *q = '\0';
651 strcpy(phrase_two, p);
652 }
653 }
654 }
655 }
656 }
657 paranoid_pclose(fin);
658
659#ifndef __FreeBSD__
660 if (strlen(phrase_two) == 0) {
661 log_msg(4, "Not running phase two. String is empty.");
662 } else {
663 mr_asprintf(command, "dmesg | grep \"%s\" 2> /dev/null", phrase_two);
664 fin = popen(command, "r");
665 mr_free(command);
666
667 if (!fin) {
668 log_msg(4, "Cannot run 2nd command - non-fatal, fortunately");
669 } else {
670 for ((void)fgets(tmp, MAX_STR_LEN, fin); !feof(fin);
671 (void)fgets(tmp, MAX_STR_LEN, fin)) {
672 log_msg(5, "--> '%s'", tmp);
673 if (tmp[0] != ' ' && tmp[1] != ' ') {
674 p = strchr(tmp, ':');
675 if (p) {
676 *p = '\0';
677 if (strstr(tmp, "DVD")) {
678 mr_free(dvd_last_resort);
679 mr_asprintf(dvd_last_resort, "/dev/%s", tmp);
680 log_msg(4,
681 "Ignoring '%s' because it's a DVD drive",
682 tmp);
683 } else {
684 sprintf(output, "/dev/%s", tmp);
685 found_it = TRUE;
686 }
687 }
688 }
689 }
690 paranoid_pclose(fin);
691 }
692 }
693
694#endif
695#ifdef __FreeBSD__
696 if (!found_it) {
697 log_msg(4, "OK, approach 2");
698 if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/cdrom"))) {
699 if (!
700 (found_it =
701 set_dev_to_this_if_rx_OK(output, "/dev/cdrom1"))) {
702 if (!
703 (found_it =
704 set_dev_to_this_if_rx_OK(output, "/dev/dvd"))) {
705 if (!
706 (found_it =
707 set_dev_to_this_if_rx_OK(output, "/dev/acd0"))) {
708 if (!
709 (found_it =
710 set_dev_to_this_if_rx_OK(output,
711 "/dev/cd01"))) {
712 if (!
713 (found_it =
714 set_dev_to_this_if_rx_OK(output,
715 "/dev/acd1"))) {
716 if (!
717 (found_it =
718 set_dev_to_this_if_rx_OK(output,
719 "/dev/cd1")))
720 {
721 retval = 1;
722 goto end_of_find_cdrom_device;
723 }
724 }
725 }
726 }
727 }
728 }
729 }
730 }
731#else
732 if (!found_it && strlen(dvd_last_resort) > 0) {
733 log_msg(4, "Well, I'll use the DVD - %s - as a last resort", dvd_last_resort);
734 strcpy(output, dvd_last_resort);
735 found_it = TRUE;
736 }
737 mr_free(dvd_last_resort);
738 if (found_it) {
739 sprintf(tmp, "grep \"%s=ide-scsi\" /proc/cmdline &> /dev/null",
740 strrchr(output, '/') + 1);
741 if (system(tmp) == 0) {
742 log_msg(4,
743 "%s is not right. It's being SCSI-emulated. Continuing.",
744 output);
745 found_it = FALSE;
746 output[0] = '\0';
747 }
748 }
749
750 if (found_it) {
751 log_msg(4, "(find_cdrom_device) --> '%s'", output);
752 if (!does_device_exist(output)) {
753 found_it = FALSE;
754 log_msg(4, "OK, I was wrong, I haven't found it... yet.");
755 }
756 }
757
758 if (!found_it) {
759 log_msg(4, "OK, approach 2");
760 if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/scd0"))) {
761 if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/sr0"))) {
762 if (!
763 (found_it =
764 set_dev_to_this_if_rx_OK(output, "/dev/cdrom"))) {
765 if (!
766 (found_it =
767 set_dev_to_this_if_rx_OK(output,
768 "/dev/cdrom0"))) {
769 if (!
770 (found_it =
771 set_dev_to_this_if_rx_OK(output,
772 "/dev/cdrom1"))) {
773 if (!
774 (found_it =
775 set_dev_to_this_if_rx_OK(output,
776 "/dev/sr1"))) {
777 if (!
778 (found_it =
779 set_dev_to_this_if_rx_OK(output,
780 "/dev/dvd")))
781 {
782 if (!
783 (found_it =
784 set_dev_to_this_if_rx_OK(output,
785 g_cdrw_drive_is_here)))
786 {
787 retval = 1;
788 goto end_of_find_cdrom_device;
789 }
790 }
791 }
792 }
793 }
794 }
795 }
796 }
797 }
798#endif
799
800 if (found_it && try_to_mount) {
801 if (mount_CDROM_here(output, mountpoint)) {
802 log_msg(4, "[Cardigans] I've changed my mind");
803 found_it = FALSE;
804 } else {
805 sprintf(tmp, "%s/archives", mountpoint);
806 if (!does_file_exist(tmp)) {
807 log_msg(4, "[Cardigans] I'll take it back");
808 found_it = FALSE;
809 } else {
810 mr_asprintf(command, "umount %s", output);
811 paranoid_system(command);
812 mr_free(command);
813
814 log_msg(4, "I'm confident the Mondo CD is in %s", output);
815 }
816 }
817 }
818 unlink(mountpoint);
819 mr_free(mountpoint);
820
821 if (found_it) {
822 if (!does_file_exist(output)) {
823 log_msg(3, "I still haven't found it.");
824 return (1);
825 }
826 log_msg(3, "(find_cdrom_device) --> '%s'", output);
827 strcpy(the_last_place_i_found_it, output);
828 strcpy(g_cdrom_drive_is_here, output);
829 retval = 0;
830 goto end_of_find_cdrom_device;
831 }
832
833 mr_asprintf(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);
834 log_msg(1, "command=%s", command);
835 mr_asprintf(tmp1, "%s", call_program_and_get_last_line_of_output(command));
836 mr_free(command);
837
838 if (strlen(tmp1) > 0) {
839 strcpy(output, tmp1);
840 log_msg(4, "Finally found it at %s", output);
841 retval = 0;
842 } else {
843 log_msg(4, "Still couldn't find it.");
844 retval = 1;
845 }
846 mr_free(tmp1);
847
848 end_of_find_cdrom_device:
849 paranoid_free(tmp);
850 mr_free(cdr_exe);
851 paranoid_free(phrase_one);
852 paranoid_free(phrase_two);
853 return (retval);
854}
855
856
857
858
859
860int find_dvd_device(char *output, bool try_to_mount)
861{
862 char *tmp = NULL;
863 int retval = 0, devno = -1;
864
865 if (g_dvd_drive_is_here[0]) {
866 strcpy(output, g_dvd_drive_is_here);
867 log_msg(3, "Been there, done that. Returning %s", output);
868 return (0);
869 }
870
871 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output("dvdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1")
872 );
873 log_msg(5, "tmp = '%s'", tmp);
874 if (!tmp[0])
875 mr_free(tmp);
876 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output ("cdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1")
877 );
878 if (tmp[0]) {
879 devno = atoi(tmp) - 1;
880 }
881 mr_free(tmp);
882
883 if (devno >= 0) {
884 retval = 0;
885 sprintf(output, "/dev/scd%d", devno);
886 strcpy(g_dvd_drive_is_here, output);
887 log_msg(2, "I think DVD is at %s", output);
888 } else {
889 log_msg(2, "I cannot find DVD");
890 retval = 1;
891 }
892
893 if (try_to_mount) {
894 log_msg(1, "Ignoring the fact that try_to_mount==TRUE");
895 }
896 return (retval);
897}
898
899
900
901
902
903#include <sys/ioctl.h>
904
905/**
906 * Find the size of the specified @p drive, in megabytes. Uses @c ioctl calls
907 * and @c dmesg.
908 * @param drive The device to find the size of.
909 * @return size in megabytes.
910 */
911long get_phys_size_of_drive(char *drive)
912{
913 int fd;
914#if linux
915 unsigned long long s = 0;
916 int fileid, cylinders = 0, cylindersleft = 0;
917 int cylindersize = 0;
918 int gotgeo = 0;
919
920
921 struct hd_geometry hdgeo;
922#elif __FreeBSD__
923 off_t s;
924#endif
925
926 long outvalA = -1;
927 long outvalB = -1;
928 long outvalC = -1;
929
930 if ((fd = open(drive, O_RDONLY)) != -1) {
931 if (ioctl(fd,
932#if linux
933#ifdef BLKGETSIZE64
934 BLKGETSIZE64,
935#else
936 BLKGETSIZE,
937#endif
938#elif __FreeBSD__
939 DIOCGMEDIASIZE,
940#endif
941 &s) != -1) {
942 close(fd);
943 // s>>11 works for older disks but not for newer ones
944 outvalB =
945#if linux
946#ifdef BLKGETSIZE64
947 s >> 20
948#else
949 s >> 11
950#endif
951#else
952 s >> 20
953#endif
954 ;
955 }
956 }
957
958 if (outvalB <= 0) {
959 log_msg(1, "Error getting size of %s: %s", drive, strerror(errno));
960#if linux
961 fileid = open(drive, O_RDONLY);
962 if (fileid != -1) {
963 if (ioctl(fileid, HDIO_GETGEO, &hdgeo) != -1) {
964 if (hdgeo.cylinders && hdgeo.heads && hdgeo.sectors) {
965 cylindersleft = cylinders = hdgeo.cylinders;
966 cylindersize = hdgeo.heads * hdgeo.sectors / 2;
967 outvalA = cylindersize * cylinders / 1024;
968 log_msg(2, "Got Harddisk geometry, C:%d, H:%d, S:%d",
969 hdgeo.cylinders, hdgeo.heads, hdgeo.sectors);
970 gotgeo = 1;
971 } else {
972 log_msg(1, "Harddisk geometry wrong");
973 }
974 } else {
975 log_msg(1,
976 "Error in ioctl() getting new hard disk geometry (%s), resizing in unsafe mode",
977 strerror(errno));
978 }
979 close(fileid);
980 } else {
981 log_msg(1, "Failed to open %s for reading: %s", drive,
982 strerror(errno));
983 }
984 if (!gotgeo) {
985 log_msg(1, "Failed to get harddisk geometry, using old mode");
986 }
987#endif
988 }
989// OLDER DISKS will give ridiculously low value for outvalB (so outvalA is returned) :)
990// NEWER DISKS will give sane value for outvalB (close to outvalA, in other words) :)
991
992 outvalC = (outvalA > outvalB) ? outvalA : outvalB;
993
994// log_msg (5, "drive = %s, error = %s", drive, strerror (errno));
995// fatal_error ("GPSOD: Unable to get size of drive");
996 log_msg(1, "%s --> %ld or %ld --> %ld", drive, outvalA, outvalB,
997 outvalC);
998
999 return (outvalC);
1000}
1001
1002/**
1003 * Determine whether @p format is supported by the kernel. Uses /proc/filesystems
1004 * under Linux and @c lsvfs under FreeBSD.
1005 * @param format The format to test.
1006 * @return TRUE if the format is supported, FALSE if not.
1007 */
1008bool is_this_a_valid_disk_format(char *format)
1009{
1010 char *good_formats = NULL;
1011 char *command = NULL;
1012 char *format_sz = NULL;
1013
1014 FILE *pin;
1015 int retval;
1016
1017 assert_string_is_neither_NULL_nor_zerolength(format);
1018
1019 mr_asprintf(format_sz, "%s ", format);
1020
1021#ifdef __FreeBSD__
1022 mr_asprintf(command, "lsvfs | tr -s '\t' ' ' | grep -v Filesys | grep -v -- -- | cut -d' ' -f1 | tr -s '\n' ' '");
1023#else
1024 mr_asprintf(command, "grep -v nodev /proc/filesystems | tr -s '\t' ' ' | cut -d' ' -f2 | tr -s '\n' ' '");
1025#endif
1026
1027 pin = popen(command, "r");
1028 mr_free(command);
1029
1030 if (!pin) {
1031 log_OS_error("Unable to read good formats");
1032 retval = 0;
1033 } else {
1034 mr_getline(&good_formats, pin);
1035 if (pclose(pin)) {
1036 log_OS_error("Cannot pclose good formats");
1037 }
1038 mr_strip_spaces(good_formats);
1039 mr_strcat(good_formats, " swap lvm raid ntfs-3g ntfs 7 "); // " ntfs 7 " -- um, cheating much? :)
1040 if (strstr(good_formats, format_sz)) {
1041 retval = 1;
1042 } else {
1043 retval = 0;
1044 }
1045 mr_free(good_formats);
1046 }
1047 mr_free(format_sz);
1048
1049 return (retval);
1050}
1051
1052
1053/** @def SWAPLIST_COMMAND The command to list the swap files/partitions in use. */
1054
1055/**
1056 * Determine whether @p device_raw is currently mounted.
1057 * @param device_raw The device to check.
1058 * @return TRUE if it's mounted, FALSE if not.
1059 */
1060bool is_this_device_mounted(char *device_raw)
1061{
1062
1063 /*@ pointers **************************************************** */
1064 FILE *fin;
1065
1066 /*@ buffers ***************************************************** */
1067 char *incoming;
1068 char *device_with_tab = NULL;
1069 char *device_with_space = NULL;
1070 char *tmp = NULL;
1071 bool retval = FALSE;
1072
1073#ifdef __FreeBSD__
1074#define SWAPLIST_COMMAND "swapinfo"
1075#else
1076#define SWAPLIST_COMMAND "cat /proc/swaps"
1077#endif
1078
1079 /*@ end vars **************************************************** */
1080
1081 malloc_string(incoming);
1082 assert(device_raw != NULL);
1083 if (device_raw[0] != '/' && !strstr(device_raw, ":/")) {
1084 log_msg(1, "%s needs to have a '/' prefixed - I'll do it",
1085 device_raw);
1086 mr_asprintf(tmp, "/%s", device_raw);
1087 } else {
1088 mr_asprintf(tmp, "%s", device_raw);
1089 }
1090 log_msg(1, "Is %s mounted?", tmp);
1091 if (!strcmp(tmp, "/proc") || !strcmp(tmp, "proc")) {
1092 log_msg(1,
1093 "I don't know how the heck /proc made it into the mountlist. I'll ignore it.");
1094 mr_free(tmp);
1095 return(FALSE);
1096 }
1097 mr_asprintf(device_with_tab, "%s\t", tmp);
1098 mr_asprintf(device_with_space, "%s ", tmp);
1099 mr_free(tmp);
1100
1101 if (!(fin = popen("mount", "r"))) {
1102 log_OS_error("Cannot popen 'mount'");
1103 return(FALSE);
1104 }
1105 for ((void)fgets(incoming, MAX_STR_LEN - 1, fin); !feof(fin);
1106 (void)fgets(incoming, MAX_STR_LEN - 1, fin)) {
1107 if (strstr(incoming, device_with_space) //> incoming
1108 || strstr(incoming, device_with_tab)) // > incoming)
1109 {
1110 paranoid_pclose(fin);
1111 paranoid_free(incoming);
1112 return(TRUE);
1113 }
1114 }
1115 mr_free(device_with_tab);
1116 mr_free(device_with_space);
1117 paranoid_pclose(fin);
1118 mr_asprintf(tmp, "%s | grep -E \"^%s\" > /dev/null 2> /dev/null", SWAPLIST_COMMAND, device_with_space);
1119 log_msg(4, "tmp (command) = '%s'", tmp);
1120 if (!system(tmp)) {
1121 retval = TRUE;
1122 }
1123 mr_free(tmp);
1124 paranoid_free(incoming);
1125 return(retval);
1126}
1127
1128#ifdef __FreeBSD__
1129// CODE IS FREEBSD-SPECIFIC
1130/**
1131 * Create a loopback device for specified @p fname.
1132 * @param fname The file to associate with a device.
1133 * @return /dev entry for the device, or NULL if it couldn't be allocated.
1134 */
1135char *make_vn(char *fname)
1136{
1137 char *device = (char *) malloc(MAX_STR_LEN);
1138 char *mddevice = (char *) malloc(32);
1139 char command[MAX_STR_LEN];
1140 int vndev = 2;
1141 if (atoi
1142 (call_program_and_get_last_line_of_output
1143 ("/sbin/sysctl -n kern.osreldate")) < 500000) {
1144 do {
1145 sprintf(mddevice, "vn%ic", vndev++);
1146 sprintf(command, "vnconfig %s %s", mddevice, fname);
1147 if (vndev > 10) {
1148 return NULL;
1149 }
1150 }
1151 while (system(command));
1152 } else {
1153 sprintf(command, "mdconfig -a -t vnode -f %s", fname);
1154 mddevice = call_program_and_get_last_line_of_output(command);
1155 if (!strstr(mddevice, "md")) {
1156 return NULL;
1157 }
1158 }
1159 sprintf(device, "/dev/%s", mddevice);
1160 return device;
1161}
1162
1163
1164
1165// CODE IS FREEBSD-SPECIFIC
1166/**
1167 * Deallocate specified @p dname.
1168 * This should be called when you are done with the device created by make_vn(),
1169 * so the system does not run out of @c vn devices.
1170 * @param dname The device to deallocate.
1171 * @return 0 for success, nonzero for failure.
1172 */
1173int kick_vn(char *dname)
1174{
1175 char *command = NULL;
1176 int res = 0;
1177
1178 if (strncmp(dname, "/dev/", 5) == 0) {
1179 dname += 5;
1180 }
1181
1182 if (atoi
1183 (call_program_and_get_last_line_of_output
1184 ("/sbin/sysctl -n kern.osreldate")) < 500000) {
1185 mr_asprintf(command, "vnconfig -d %s", dname);
1186 } else {
1187 mr_asprintf(command, "mdconfig -d -u %s", dname);
1188 }
1189 res = system(command);
1190 mr_free(command);
1191 return(res);
1192}
1193#endif
1194
1195
1196/**
1197 * Mount the CD-ROM at @p mountpoint.
1198 * @param device The device (or file if g_ISO_restore_mode) to mount.
1199 * @param mountpoint The place to mount it.
1200 * @return 0 for success, nonzero for failure.
1201 */
1202int mount_USB_here(char *device, char *mountpoint)
1203{
1204 /*@ buffer ****************************************************** */
1205 char *command = NULL;
1206 int retval;
1207
1208 assert_string_is_neither_NULL_nor_zerolength(device);
1209 assert_string_is_neither_NULL_nor_zerolength(mountpoint);
1210
1211 make_hole_for_dir(mountpoint);
1212 if (isdigit(device[0])) {
1213 return(1);
1214 }
1215 log_msg(4, "(mount_USB_here --- device=%s, mountpoint=%s", device,
1216 mountpoint);
1217
1218#ifdef __FreeBSD__
1219 mr_asprintf(command, "mount_vfat %s %s 2>> %s", device, mountpoint, MONDO_LOGFILE);
1220
1221#else
1222 mr_asprintf(command, "mount %s -t vfat %s 2>> %s", device, mountpoint, MONDO_LOGFILE);
1223#endif
1224
1225 log_msg(4, command);
1226 retval = system(command);
1227 log_msg(1, "system(%s) returned %d", command, retval);
1228 mr_free(command);
1229
1230 return (retval);
1231}
1232
1233/**
1234 * Mount the CD-ROM at @p mountpoint.
1235 * @param device The device (or file if g_ISO_restore_mode) to mount.
1236 * @param mountpoint The place to mount it.
1237 * @return 0 for success, nonzero for failure.
1238 */
1239int mount_CDROM_here(char *device, char *mountpoint)
1240{
1241 /*@ buffer ****************************************************** */
1242 char *command = NULL;
1243 int retval;
1244#ifdef __FreeBSD__
1245 char *dev = NULL;
1246#else
1247 char *options = NULL;
1248#endif
1249
1250 assert_string_is_neither_NULL_nor_zerolength(device);
1251 assert_string_is_neither_NULL_nor_zerolength(mountpoint);
1252
1253 make_hole_for_dir(mountpoint);
1254
1255 if (isdigit(device[0])) {
1256 find_cdrom_device(device, FALSE);
1257 }
1258#ifndef __FreeBSD__
1259 mr_asprintf(options, "ro");
1260#endif
1261
1262 if (g_ISO_restore_mode) {
1263
1264#ifdef __FreeBSD__
1265 mr_asprintf(dev, "%s", make_vn(device));
1266 if (!dev) {
1267 mr_asprintf(command, "Unable to mount ISO (make_vn(%s) failed)", device);
1268 fatal_error(command);
1269 }
1270 strcpy(device, dev);
1271 paranoid_free(dev);
1272#else
1273 mr_strcat(options, ",loop");
1274#endif
1275
1276 }
1277 log_msg(4, "(mount_CDROM_here --- device=%s, mountpoint=%s", device,
1278 mountpoint);
1279 /*@ end vars *************************************************** */
1280
1281#ifdef __FreeBSD__
1282 mr_asprintf(command, "mount_cd9660 -r %s %s 2>> %s", device, mountpoint, MONDO_LOGFILE);
1283
1284#else
1285 mr_asprintf(command, "mount %s -o %s -t iso9660 %s 2>> %s", device, options, mountpoint, MONDO_LOGFILE);
1286 paranoid_free(options);
1287#endif
1288
1289 log_msg(4, command);
1290 if (strncmp(device, "/dev/", 5) == 0) {
1291 retract_CD_tray_and_defeat_autorun();
1292 }
1293 retval = system(command);
1294 log_msg(1, "system(%s) returned %d", command, retval);
1295 paranoid_free(command);
1296
1297 return (retval);
1298}
1299
1300
1301
1302
1303
1304
1305/**
1306 * Ask the user for CD number @p cd_number_i_want.
1307 * Sets g_current_media_number once the correct CD is inserted.
1308 * @param bkpinfo The backup information structure. Fields used:
1309 * - @c bkpinfo->backup_media_type
1310 * - @c bkpinfo->prefix
1311 * - @c bkpinfo->isodir
1312 * - @c bkpinfo->media_device
1313 * - @c bkpinfo->please_dont_eject_when_restoring
1314 * @param cd_number_i_want The CD number to ask for.
1315 */
1316void
1317insist_on_this_cd_number(int cd_number_i_want)
1318{
1319
1320 /*@ int ************************************************************* */
1321 int res = 0;
1322
1323
1324 /*@ buffers ********************************************************* */
1325 char *tmp = NULL;
1326 char *p = NULL;
1327 char *mds = NULL;
1328 char *request = NULL;
1329
1330 assert(bkpinfo != NULL);
1331 assert(cd_number_i_want > 0);
1332
1333// log_msg(3, "Insisting on CD number %d", cd_number_i_want);
1334
1335 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1336 log_msg(3,
1337 "No need to insist_on_this_cd_number when the backup type isn't CD-R(W) or NFS or ISO");
1338 return;
1339 }
1340 mr_asprintf(tmp, "mkdir -p " MNT_CDROM);
1341 run_program_and_log_output(tmp, 5);
1342 mr_free(tmp);
1343
1344 if (g_ISO_restore_mode || bkpinfo->backup_media_type == iso
1345 || bkpinfo->backup_media_type == nfs) {
1346 log_msg(3, "Remounting CD");
1347 g_ISO_restore_mode = TRUE;
1348 // FIXME --- I'm tempted to do something about this...
1349 // Why unmount and remount again and again?
1350 if (is_this_device_mounted(MNT_CDROM)) {
1351 run_program_and_log_output("umount " MNT_CDROM, 5);
1352 }
1353 mr_asprintf(tmp, "mkdir -p %s/isodir &> /dev/null", bkpinfo->tmpdir);
1354 (void)system(tmp);
1355 mr_free(tmp);
1356
1357 mr_asprintf(tmp, "%s/%s/%s-%d.iso", bkpinfo->isodir, bkpinfo->nfs_remote_dir, bkpinfo->prefix, cd_number_i_want);
1358 if (!does_file_exist(tmp)) {
1359 mr_free(tmp);
1360 mr_asprintf(tmp, "%s/isodir/%s/%s-%d.iso", bkpinfo->tmpdir, bkpinfo->nfs_remote_dir, bkpinfo->prefix, cd_number_i_want);
1361 if (does_file_exist(tmp)) {
1362 log_msg(1, "FIXME - hacking bkpinfo->isodir from '%s' to %s/isodir", bkpinfo->isodir, bkpinfo->tmpdir);
1363 mr_free(bkpinfo->isodir);
1364 mr_asprintf(bkpinfo->isodir, "%s/isodir", bkpinfo->tmpdir);
1365 }
1366 }
1367 log_msg(3, "Mounting %s at %s", tmp, MNT_CDROM);
1368 if (mount_CDROM_here(tmp, MNT_CDROM)) {
1369 mr_free(tmp);
1370 fatal_error("Mommy!");
1371 }
1372 mr_free(tmp);
1373 }
1374 if ((res = what_number_cd_is_this()) != cd_number_i_want) {
1375 log_msg(3, "Currently, we hold %d but we want %d", res,
1376 cd_number_i_want);
1377 mds = media_descriptor_string(bkpinfo->backup_media_type);
1378 log_msg(3, "Insisting on %s #%d", mds, cd_number_i_want);
1379 mr_asprintf(request, "Please insert %s #%d and press Enter.", mds, cd_number_i_want);
1380 mr_free(mds);
1381
1382 while (what_number_cd_is_this() != cd_number_i_want) {
1383 paranoid_system("sync");
1384 if (is_this_device_mounted(MNT_CDROM)) {
1385 res =
1386 run_program_and_log_output("umount " MNT_CDROM, FALSE);
1387 } else {
1388 res = 0;
1389 }
1390 if (res) {
1391 log_to_screen("WARNING - failed to unmount CD-ROM drive");
1392 }
1393 if (!bkpinfo->please_dont_eject) {
1394 res = eject_device(bkpinfo->media_device);
1395 } else {
1396 res = 0;
1397 }
1398 if (res) {
1399 log_to_screen("WARNING - failed to eject CD-ROM disk");
1400 }
1401 popup_and_OK(request);
1402 if (!bkpinfo->please_dont_eject) {
1403 inject_device(bkpinfo->media_device);
1404 }
1405 paranoid_system("sync");
1406 }
1407 mr_free(request);
1408
1409 log_msg(1, "Thankyou. Proceeding...");
1410 g_current_media_number = cd_number_i_want;
1411 }
1412}
1413
1414/* @} - end of deviceGroup */
1415
1416
1417/**
1418 * Ask user for details of backup/restore information.
1419 * Called when @c mondoarchive doesn't get any parameters.
1420 * @param bkpinfo The backup information structure to fill out with the user's data.
1421 * @param archiving_to_media TRUE if archiving, FALSE if restoring.
1422 * @return 0, always.
1423 * @bug No point of `int' return value.
1424 * @ingroup archiveGroup
1425 */
1426int interactively_obtain_media_parameters_from_user(bool archiving_to_media)
1427// archiving_to_media is TRUE if I'm being called by mondoarchive
1428// archiving_to_media is FALSE if I'm being called by mondorestore
1429{
1430 char *tmp = NULL;
1431 char *p = NULL;
1432 char *mds = NULL;
1433 char *sz_size = NULL;
1434 char *command = NULL;
1435 char *comment = NULL;
1436 char *prompt;
1437 int i;
1438 FILE *fin;
1439
1440 malloc_string(prompt);
1441 malloc_string(tmp1);
1442 assert(bkpinfo != NULL);
1443 bkpinfo->nonbootable_backup = FALSE;
1444
1445 // Tape, CD, NFS, ...?
1446 srandom(getpid());
1447 bkpinfo->backup_media_type =
1448 (g_restoring_live_from_cd) ? cdr :
1449 which_backup_media_type(bkpinfo->restore_data);
1450 if (bkpinfo->backup_media_type == none) {
1451 log_to_screen("User has chosen not to backup the PC");
1452 finish(1);
1453 }
1454 /* Why asking to remove the media with tape ?
1455 if (bkpinfo->backup_media_type == tape && bkpinfo->restore_data) {
1456 popup_and_OK("Please remove media from drive(s)");
1457 }
1458 */
1459 log_msg(3, "media type = %s", bkptype_to_string(bkpinfo->backup_media_type));
1460 bkpinfo->cdrw_speed = (bkpinfo->backup_media_type == cdstream) ? 2 : 4;
1461 bkpinfo->compression_level =
1462 (bkpinfo->backup_media_type == cdstream) ? 1 : 5;
1463 bkpinfo->use_lzo =
1464 (bkpinfo->backup_media_type == cdstream) ? TRUE : FALSE;
1465 mvaddstr_and_log_it(2, 0, " ");
1466
1467 // Find device's /dev (or SCSI) entry
1468 switch (bkpinfo->backup_media_type) {
1469 case cdr:
1470 case cdrw:
1471 case dvd:
1472 case usb:
1473 /* Never try to eject a USB device */
1474 if (bkpinfo->backup_media_type == usb) {
1475 bkpinfo->please_dont_eject = TRUE;
1476 }
1477 if (archiving_to_media) {
1478 if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) {
1479 if (ask_me_yes_or_no
1480 ("Is your computer a laptop, or does the CD writer incorporate BurnProof technology?"))
1481 {
1482 bkpinfo->manual_cd_tray = TRUE;
1483 }
1484 }
1485 if ((bkpinfo->compression_level =
1486 which_compression_level()) == -1) {
1487 log_to_screen("User has chosen not to backup the PC");
1488 finish(1);
1489 }
1490 mds = media_descriptor_string(bkpinfo->backup_media_type);
1491 mr_asprintf(comment, "What speed is your %s (re)writer?", mds);
1492 if (bkpinfo->backup_media_type == dvd) {
1493 find_dvd_device(bkpinfo->media_device, FALSE);
1494 mr_asprintf(tmp, "1");
1495 mr_asprintf(sz_size, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4482 MB
1496 log_msg(1, "Setting to DVD defaults");
1497 } else {
1498 strcpy(bkpinfo->media_device, VANILLA_SCSI_CDROM);
1499 mr_asprintf(tmp, "4");
1500 mr_asprintf(sz_size, "%d", 650);
1501 log_msg(1, "Setting to CD defaults");
1502 }
1503 if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) {
1504 p = popup_and_get_string("Speed", comment, tmp);
1505 mr_free(tmp);
1506
1507 if (p == NULL) {
1508 log_to_screen("User has chosen not to backup the PC");
1509 mr_free(comment);
1510 finish(1);
1511 }
1512 /* tmp now has the new value given by the user */
1513 tmp = p;
1514 }
1515 mr_free(comment);
1516
1517 bkpinfo->cdrw_speed = atoi(tmp1); // if DVD then this shouldn't ever be used anyway :)
1518
1519 mr_asprintf(comment, "How much data (in Megabytes) will each %s store?", mds);
1520 mr_free(mds);
1521 p = popup_and_get_string("Size", comment, sz_size);
1522 mr_free(sz_size);
1523 mr_free(comment);
1524
1525 if (p == NULL) {
1526 log_to_screen("User has chosen not to backup the PC");
1527 finish(1);
1528 }
1529 sz_size = p;
1530
1531 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1532 bkpinfo->media_size[i] = atoi(sz_size);
1533 }
1534
1535 if (bkpinfo->media_size[0] <= 0) {
1536 log_to_screen("User has chosen not to backup the PC");
1537 finish(1);
1538 }
1539 }
1540 /* No break because we continue even for usb */
1541 case cdstream:
1542 mds = media_descriptor_string(bkpinfo->backup_media_type);
1543
1544 if ((bkpinfo->disaster_recovery) && (bkpinfo->backup_media_type != usb)) {
1545 strcpy(bkpinfo->media_device, "/dev/cdrom");
1546 log_msg(2, "CD-ROM device assumed to be at %s",
1547 bkpinfo->media_device);
1548 } else if ((bkpinfo->restore_data && (bkpinfo->backup_media_type != usb))
1549 || bkpinfo->backup_media_type == dvd) {
1550 if (!bkpinfo->media_device[0]) {
1551 strcpy(bkpinfo->media_device, "/dev/cdrom");
1552 } // just for the heck of it :)
1553 log_msg(1, "bkpinfo->media_device = %s",
1554 bkpinfo->media_device);
1555 if (bkpinfo->backup_media_type == dvd
1556 || find_cdrom_device(bkpinfo->media_device, FALSE)) {
1557 log_msg(1, "bkpinfo->media_device = %s",
1558 bkpinfo->media_device);
1559 mr_asprintf(comment, "Please specify your %s drive's /dev entry", mds);
1560 p = popup_and_get_string("Device?", comment, bkpinfo->media_device);
1561 mr_free(comment);
1562
1563 if (p == NULL) {
1564 log_to_screen("User has chosen not to backup the PC");
1565 finish(1);
1566 }
1567 strcpy(bkpinfo->media_device, p);
1568 mr_free(p);
1569 }
1570 log_msg(2, "%s device found at %s", mds, bkpinfo->media_device);
1571 } else {
1572 if ((find_cdrw_device(bkpinfo->media_device)) && (bkpinfo->backup_media_type != usb)) {
1573 bkpinfo->media_device[0] = '\0';
1574 }
1575 if (bkpinfo->media_device[0]) {
1576 if (bkpinfo->backup_media_type == usb) {
1577 mr_asprintf(tmp, "I think your %s media corresponds to %s. Is this correct?", mds, bkpinfo->media_device);
1578 } else {
1579 mr_asprintf(tmp, "I think I've found your %s burner at SCSI node %s. Is this correct? (Say no if you have an IDE burner and you are running a 2.6 kernel. You will then be prompted for further details.)", mds, bkpinfo->media_device);
1580 }
1581 if (!ask_me_yes_or_no(tmp)) {
1582 bkpinfo->media_device[0] = '\0';
1583 }
1584 mr_free(tmp);
1585 }
1586 if (!bkpinfo->media_device[0]) {
1587 if (bkpinfo->backup_media_type == usb) {
1588 p = popup_and_get_string("/dev entry?", "What is the /dev entry of your USB Disk/Key, please?", bkpinfo->media_device);
1589 } else {
1590 if (g_kernel_version < 2.6) {
1591 p = popup_and_get_string("Device node?", "What is the SCSI node of your CD (re)writer, please?", bkpinfo->media_device);
1592 } else {
1593 p = popup_and_get_string("/dev entry?", "What is the /dev entry of your CD (re)writer, please?", bkpinfo->media_device);
1594 }
1595 }
1596 if (p == NULL) {
1597 log_to_screen("User has chosen not to backup the PC");
1598 finish(1);
1599 }
1600 strcpy(bkpinfo->media_device, p);
1601 mr_free(p);
1602 }
1603 }
1604 mr_free(mds);
1605
1606 if (bkpinfo->backup_media_type == cdstream) {
1607 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1608 bkpinfo->media_size[i] = 650;
1609 }
1610 }
1611 break;
1612 case udev:
1613 if (!ask_me_yes_or_no
1614 ("This option is for advanced users only. Are you sure?")) {
1615 log_to_screen("User has chosen not to backup the PC");
1616 finish(1);
1617 }
1618 case tape:
1619
1620 if ((!bkpinfo->restore_mode) && (!mr_find_tape_device())) {
1621 log_msg(3, "Ok, using vanilla scsi tape.");
1622 strcpy(bkpinfo->media_device, VANILLA_SCSI_TAPE);
1623 if ((fin = fopen(bkpinfo->media_device, "r"))) {
1624 paranoid_fclose(fin);
1625 } else {
1626 strcpy(bkpinfo->media_device, "/dev/osst0");
1627 }
1628 }
1629 if (bkpinfo->media_device[0]) {
1630 if ((fin = fopen(bkpinfo->media_device, "r"))) {
1631 paranoid_fclose(fin);
1632 } else {
1633 if (does_file_exist("/tmp/mondo-restore.cfg")) {
1634 read_cfg_var("/tmp/mondo-restore.cfg", "media-dev",
1635 bkpinfo->media_device);
1636 }
1637 }
1638 }
1639 if (bkpinfo->media_device[0]) {
1640 mr_asprintf(tmp, "I think I've found your tape streamer at %s; am I right on the money?", bkpinfo->media_device);
1641 if (!ask_me_yes_or_no(tmp)) {
1642 bkpinfo->media_device[0] = '\0';
1643 }
1644 mr_free(tmp);
1645 }
1646 if (!bkpinfo->media_device[0]) {
1647 p = popup_and_get_string("Device name?", "What is the /dev entry of your tape streamer?", bkpinfo->media_device);
1648 if (p == NULL) {
1649 log_to_screen("User has chosen not to backup the PC");
1650 finish(1);
1651 }
1652 strcpy(bkpinfo->media_device, p);
1653 mr_free(p);
1654 }
1655 mr_asprintf(tmp, "ls -l %s", bkpinfo->media_device);
1656 if (run_program_and_log_output(tmp, FALSE)) {
1657 log_to_screen("User has not specified a valid /dev entry");
1658 finish(1);
1659 }
1660 mr_free(tmp);
1661
1662 bkpinfo->use_obdr = ask_me_yes_or_no
1663 ("Do you want to activate OBDR support for your tapes ?");
1664 bkpinfo->media_size[0] = 0;
1665 log_msg(4, "media_size[0] = %ld", bkpinfo->media_size[0]);
1666 if (bkpinfo->media_size[0] <= 0) {
1667 bkpinfo->media_size[0] = 0;
1668 }
1669 for (i = 1; i <= MAX_NOOF_MEDIA; i++) {
1670 bkpinfo->media_size[i] = bkpinfo->media_size[0];
1671 }
1672 if (archiving_to_media) {
1673 if ((bkpinfo->compression_level =
1674 which_compression_level()) == -1) {
1675 log_to_screen("User has chosen not to backup the PC");
1676 finish(1);
1677 }
1678 }
1679 break;
1680
1681
1682
1683 case nfs:
1684 /* Never try to eject a NFS device */
1685 bkpinfo->please_dont_eject = TRUE;
1686
1687 /* Initiate bkpinfo nfs_mount path from running environment if not already done */
1688 if (!bkpinfo->nfs_mount[0]) {
1689 strcpy(bkpinfo->nfs_mount,
1690 call_program_and_get_last_line_of_output
1691 ("mount | grep \":\" | cut -d' ' -f1 | head -n1"));
1692 }
1693#ifdef __FreeBSD__
1694 if (TRUE)
1695#else
1696 if (!bkpinfo->disaster_recovery)
1697#endif
1698 {
1699 p = 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);
1700 if (p == NULL) {
1701 log_to_screen("User has chosen not to backup the PC");
1702 finish(1);
1703 }
1704 strcpy(bkpinfo->nfs_mount, p);
1705 mr_free(p);
1706 if (!bkpinfo->restore_data) {
1707 if ((bkpinfo->compression_level =
1708 which_compression_level()) == -1) {
1709 log_to_screen("User has chosen not to backup the PC");
1710 finish(1);
1711 }
1712 }
1713 // check whether already mounted - we better remove
1714 // surrounding spaces and trailing '/' for this
1715 strip_spaces(bkpinfo->nfs_mount);
1716 if (bkpinfo->nfs_mount[strlen(bkpinfo->nfs_mount) - 1] == '/')
1717 bkpinfo->nfs_mount[strlen(bkpinfo->nfs_mount) - 1] = '\0';
1718 mr_asprintf(command, "mount | grep \"%s \" | cut -d' ' -f3", bkpinfo->nfs_mount);
1719 mr_free(bkpinfo->isodir);
1720 mr_asprintf(bkpinfo->isodir, "%s", call_program_and_get_last_line_of_output(command));
1721 mr_free(command);
1722
1723 if (!bkpinfo->restore_data) {
1724 mr_asprintf(comment, "How much data (in Megabytes) will each media store?");
1725 // BERLIOS: 4480 shouldn't be hardcoded here
1726 sz_size = popup_and_get_string("Size", comment, "4480");
1727 mr_free(comment);
1728 if (sz_size == NULL) {
1729 log_to_screen("User has chosen not to backup the PC");
1730 finish(1);
1731 }
1732 } else {
1733 mr_asprintf(sz_size, "0");
1734 }
1735 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1736 bkpinfo->media_size[i] = atoi(sz_size);
1737 }
1738 mr_free(sz_size);
1739 if (bkpinfo->media_size[0] < 0) {
1740 log_to_screen("User has chosen not to backup the PC");
1741 finish(1);
1742 }
1743 }
1744 if (bkpinfo->disaster_recovery) {
1745 mr_asprintf(command ,"umount %s/isodir 2> /dev/null", bkpinfo->tmpdir);
1746 (void)system(command);
1747 mr_free(command);
1748
1749 p = popup_and_get_string("NFS share", "Which remote NFS share should I mount?", bkpinfo->nfs_mount);
1750 if (p == NULL) {
1751 log_to_screen("User has chosen not to backup the PC");
1752 finish(1);
1753 }
1754 strcpy(bkpinfo->nfs_mount, p);
1755 mr_free(p);
1756 }
1757 /* Initiate bkpinfo isodir path from running environment if mount already done */
1758 mr_free(bkpinfo->isodir);
1759 if (is_this_device_mounted(bkpinfo->nfs_mount)) {
1760 mr_asprintf(bkpinfo->isodir, "%s", call_program_and_get_last_line_of_output("mount | grep \":\" | cut -d' ' -f3 | head -n1"));
1761
1762 } else {
1763 mr_asprintf(bkpinfo->isodir, "%s/nfsdir", bkpinfo->tmpdir);
1764 mr_asprintf(command, "mkdir -p %s", bkpinfo->isodir);
1765 run_program_and_log_output(command, 5);
1766 mr_free(command);
1767
1768 mr_asprintf(tmp, "mount -t nfs -o nolock %s %s", bkpinfo->nfs_mount, bkpinfo->isodir);
1769 run_program_and_log_output(tmp, 3);
1770 mr_free(tmp);
1771
1772 malloc_string(g_selfmounted_isodir);
1773 strcpy(g_selfmounted_isodir, bkpinfo->isodir);
1774 }
1775 if (!is_this_device_mounted(bkpinfo->nfs_mount)) {
1776 popup_and_OK("Please mount that partition before you try to backup to or restore from it.");
1777 finish(1);
1778 }
1779 p = popup_and_get_string("Directory", "Which directory within that mountpoint?", bkpinfo->nfs_remote_dir);
1780 if (p == NULL) {
1781 log_to_screen("User has chosen not to backup the PC");
1782 finish(1);
1783 }
1784 strcpy(bkpinfo->nfs_remote_dir, p);
1785 mr_free(p);
1786
1787 // check whether writable - we better remove surrounding spaces for this
1788 strip_spaces(bkpinfo->nfs_remote_dir);
1789
1790 p = 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);
1791 if (p == NULL) {
1792 log_to_screen("User has chosen not to backup the PC");
1793 finish(1);
1794 }
1795 mr_free(bkpinfo->prefix);
1796 bkpinfo->prefix = p;
1797 log_msg(3, "prefix set to %s", bkpinfo->prefix);
1798
1799 log_msg(3, "Just set nfs_remote_dir to %s", bkpinfo->nfs_remote_dir);
1800 log_msg(3, "isodir is still %s", bkpinfo->isodir);
1801 break;
1802
1803 case iso:
1804 if (!bkpinfo->disaster_recovery) {
1805 p = popup_and_get_string("Storage dir.", "Please enter the full path name to the directory for your ISO images. Example: /mnt/raid0_0", bkpinfo->isodir);
1806 if (p == NULL) {
1807 log_to_screen("User has chosen not to backup the PC");
1808 finish(1);
1809 }
1810 bkpinfo->isodir = p;
1811
1812 if (archiving_to_media) {
1813 if ((bkpinfo->compression_level =
1814 which_compression_level()) == -1) {
1815 log_to_screen("User has chosen not to backup the PC");
1816 finish(1);
1817 }
1818 p = 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);
1819 if (p == NULL) {
1820 log_to_screen("User has chosen not to backup the PC");
1821 finish(1);
1822 }
1823 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1824 bkpinfo->media_size[i] = atoi(p);
1825 }
1826 mr_free(p);
1827 } else {
1828 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1829 bkpinfo->media_size[i] = 650;
1830 }
1831 }
1832 }
1833 p = 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);
1834 if (p == NULL) {
1835 log_to_screen("User has chosen not to backup the PC");
1836 finish(1);
1837 }
1838 mr_free(bkpinfo->prefix);
1839 bkpinfo->prefix = p;
1840 log_msg(3, "prefix set to %s", bkpinfo->prefix);
1841 break;
1842
1843 default:
1844 fatal_error
1845 ("I, Mojo Jojo, shall defeat those pesky Powerpuff Girls!");
1846 }
1847
1848 if (archiving_to_media) {
1849
1850#ifdef __FreeBSD__
1851 strcpy(bkpinfo->boot_device,
1852 call_program_and_get_last_line_of_output
1853 ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/\\([0-9]\\).*/\\1/'"));
1854#else
1855 strcpy(bkpinfo->boot_device,
1856 call_program_and_get_last_line_of_output
1857 ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'"));
1858#endif
1859 i = which_boot_loader(bkpinfo->boot_device);
1860 if (i == 'U') // unknown
1861 {
1862
1863#ifdef __FreeBSD__
1864 p = popup_and_get_string("Boot device", "What is your boot device? (e.g. /dev/ad0)", bkpinfo->boot_device);
1865 if (!p == NULL) {
1866 log_to_screen("User has chosen not to backup the PC");
1867 finish(1);
1868 }
1869 strcpy(bkpinfo->boot_device, p);
1870 mr_free(p);
1871 i = which_boot_loader(bkpinfo->boot_device);
1872#else
1873 p = popup_and_get_string("Boot device", "What is your boot device? (e.g. /dev/hda)", bkpinfo->boot_device);
1874 if (p == NULL) {
1875 log_to_screen("User has chosen not to backup the PC");
1876 finish(1);
1877 }
1878 strcpy(bkpinfo->boot_device, p);
1879 mr_free(p);
1880
1881 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "LILO")) {
1882 i = 'L';
1883 } else
1884 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "ELILO")) {
1885 i = 'E';
1886 } else
1887 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "GRUB")) {
1888 i = 'G';
1889 } else {
1890 i = 'U';
1891 }
1892#endif
1893 if (i == 'U') {
1894 if (ask_me_yes_or_no
1895 ("Unidentified boot loader. Shall I restore it byte-for-byte at restore time and hope for the best?"))
1896 {
1897 i = 'R'; // raw
1898 } else {
1899 log_to_screen
1900 ("I cannot find your boot loader. Please run mondoarchive with parameters.");
1901 finish(1);
1902 }
1903 }
1904 }
1905 bkpinfo->boot_loader = i;
1906 mr_free(bkpinfo->include_paths);
1907 mr_asprintf(p, "/");
1908 bkpinfo->include_paths = p;
1909
1910 p = popup_and_get_string("Backup paths", "Please enter paths which you want me to backup. The default is '/' (i.e. everything).", bkpinfo->include_paths);
1911 if (p == NULL) {
1912 log_to_screen("User has chosen not to backup the PC");
1913 finish(1);
1914 }
1915 mr_free(bkpinfo->include_paths);
1916 bkpinfo->include_paths = p;
1917
1918 mr_asprintf(tmp, "%s", list_of_NFS_mounts_only());
1919 if (strlen(tmp) > 2) {
1920 mr_strcat(bkpinfo->exclude_paths, " %s",tmp);
1921 }
1922 mr_free(tmp);
1923// NTFS
1924 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output("parted2fdisk -l | grep -i ntfs | awk '{ print $1};' | tr -s '\\n' ' ' | awk '{ print $0};'"));
1925 if (strlen(tmp) > 2) {
1926 p = popup_and_get_string("NTFS partitions", "Please enter/confirm the NTFS partitions you wish to backup as well.", tmp);
1927
1928 if (p == NULL) {
1929 log_to_screen("User has chosen not to backup the PC");
1930 finish(1);
1931 }
1932 strncpy(bkpinfo->image_devs, p, MAX_STR_LEN / 4);
1933 mr_free(p);
1934 }
1935
1936
1937 p = 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);
1938 if (p == NULL) {
1939 log_to_screen("User has chosen not to backup the PC");
1940 finish(1);
1941 }
1942 mr_free(bkpinfo->exclude_paths);
1943 bkpinfo->exclude_paths = p;
1944
1945 p = popup_and_get_string("Temporary directory", "Please enter your temporary directory.", bkpinfo->tmpdir);
1946 if (p == NULL) {
1947 log_to_screen("User has chosen not to backup the PC");
1948 finish(1);
1949 }
1950 mr_free(bkpinfo->tmpdir);
1951 bkpinfo->tmpdir = p;
1952
1953 p = popup_and_get_string("Scratch directory", "Please enter your scratch directory.", bkpinfo->scratchdir);
1954 if (p == NULL) {
1955 log_to_screen("User has chosen not to backup the PC");
1956 finish(1);
1957 }
1958 mr_free(bkpinfo->scratchdir);
1959 bkpinfo->scratchdir = p;
1960
1961// Interactive mode:
1962#ifdef __IA64__
1963 bkpinfo->make_cd_use_lilo = TRUE;
1964#else
1965 bkpinfo->make_cd_use_lilo = FALSE;
1966#endif
1967 bkpinfo->backup_data = TRUE;
1968 bkpinfo->verify_data =
1969 ask_me_yes_or_no
1970 ("Will you want to verify your backups after Mondo has created them?");
1971
1972#ifndef __FreeBSD__
1973 if (!ask_me_yes_or_no
1974 ("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."))
1975#endif
1976 {
1977 strcpy(bkpinfo->kernel_path, "FAILSAFE");
1978 }
1979
1980 if (!ask_me_yes_or_no
1981 ("Are you sure you want to proceed? Hit 'no' to abort.")) {
1982 log_to_screen("User has chosen not to backup the PC");
1983 finish(1);
1984 }
1985 } else {
1986 bkpinfo->restore_data = TRUE; // probably...
1987 }
1988
1989 if (bkpinfo->backup_media_type == iso
1990 || bkpinfo->backup_media_type == nfs) {
1991 g_ISO_restore_mode = TRUE;
1992 }
1993#ifdef __FreeSD__
1994// skip
1995#else
1996 if (bkpinfo->backup_media_type == nfs) {
1997 log_msg(3, "I think the NFS mount is mounted at %s", bkpinfo->isodir);
1998 }
1999 log_it("isodir = %s", bkpinfo->isodir);
2000 log_it("nfs_mount = '%s'", bkpinfo->nfs_mount);
2001 if (bkpinfo->nfs_user) {
2002 log_it("nfs_user = '%s'", bkpinfo->nfs_user);
2003 }
2004#endif
2005
2006 log_it("media device = %s", bkpinfo->media_device);
2007 log_it("media size = %ld", bkpinfo->media_size[1]);
2008 log_it("media type = %s", bkptype_to_string(bkpinfo->backup_media_type));
2009 log_it("prefix = %s", bkpinfo->prefix);
2010 log_it("compression = %ld", bkpinfo->compression_level);
2011 log_it("include_paths = '%s'", bkpinfo->include_paths);
2012 log_it("exclude_paths = '%s'", bkpinfo->exclude_paths);
2013 log_it("scratchdir = '%s'", bkpinfo->scratchdir);
2014 log_it("tmpdir = '%s'", bkpinfo->tmpdir);
2015 log_it("image_devs = '%s'", bkpinfo->image_devs);
2016 log_it("boot_device = '%s' (loader=%c)", bkpinfo->boot_device, bkpinfo->boot_loader);
2017 if (bkpinfo->media_size[0] < 0) {
2018 if (archiving_to_media) {
2019 fatal_error("Media size is less than zero.");
2020 } else {
2021 log_msg(2, "Warning - media size is less than zero.");
2022 bkpinfo->media_size[0] = 0;
2023 }
2024 }
2025 paranoid_free(sz_size);
2026 paranoid_free(tmp1);
2027 paranoid_free(prompt);
2028 return (0);
2029}
2030
2031
2032
2033
2034/**
2035 * @addtogroup utilityGroup
2036 * @{
2037 */
2038/**
2039 * Get a space-separated list of NFS devices and mounts.
2040 * @return The list created.
2041 * @note The return value points to static data that will be overwritten with each call.
2042 */
2043char *list_of_NFS_devices_and_mounts(void)
2044{
2045 char *exclude_these_devices = NULL;
2046 char *exclude_these_directories = NULL;
2047 static char result_sz[1024];
2048
2049 mr_asprintf(exclude_these_directories,"%s",list_of_NFS_mounts_only());
2050 mr_asprintf(exclude_these_devices,"%s", call_program_and_get_last_line_of_output("tr -s '\t' ' ' < /etc/fstab | grep -E '( (coda|ncpfs|nfs|nfs4|smbfs|cifs|afs|gfs|ocfs|ocfs2|mvfs|nsspool|nsvol) )' | cut -d' ' -f1 | tr -s '\n' ' ' | awk '{print $0;}'"));
2051 snprintf(result_sz, 1023, "%s %s", exclude_these_directories, exclude_these_devices);
2052 mr_free(exclude_these_devices);
2053 mr_free(exclude_these_directories);
2054 return (result_sz);
2055}
2056
2057
2058
2059
2060/**
2061 * Get a space-separated list of NFS mounts.
2062 * @return The list created.
2063 * @note The return value points to static data that will be overwritten with each call.
2064 * @bug Even though we only want the mounts, the devices are still checked.
2065 */
2066char *list_of_NFS_mounts_only(void)
2067{
2068 char *exclude_these_directories = NULL;
2069 static char result_sz[512];
2070
2071 mr_asprintf(exclude_these_directories,"%s", call_program_and_get_last_line_of_output("mount -t coda,ncpfs,nfs,nfs4,smbfs,cifs,afs,gfs,ocfs,ocfs2,mvfs,nsspool,nssvol | tr -s '\t' ' ' | cut -d' ' -f3 | tr -s '\n' ' ' | awk '{print $0;}'"));
2072 snprintf(result_sz, 511, "%s", exclude_these_directories);
2073 mr_free(exclude_these_directories);
2074 return (result_sz);
2075}
2076
2077/* @} - end of utilityGroup */
2078
2079
2080
2081
2082
2083/**
2084 * Create a randomly-named FIFO. The format is @p stub "." [random] [random] where
2085 * [random] is a random number between 1 and 32767.
2086 * @param store_name_here Where to store the new filename.
2087 * @param stub A random number will be appended to this to make the FIFO's name.
2088 * @ingroup deviceGroup
2089 */
2090void make_fifo(char *store_name_here, char *stub)
2091{
2092 char *tmp = NULL;
2093
2094 assert_string_is_neither_NULL_nor_zerolength(stub);
2095
2096 sprintf(store_name_here, "%s%d%d", stub, (int) (random() % 32768),
2097 (int) (random() % 32768));
2098 make_hole_for_file(store_name_here);
2099 mkfifo(store_name_here, S_IRWXU | S_IRWXG);
2100 mr_asprintf(tmp, "chmod 770 %s", store_name_here);
2101 paranoid_system(tmp);
2102 mr_free(tmp);
2103}
2104
2105
2106
2107
2108
2109
2110/**
2111 * Set the tmpdir and scratchdir to reside on the partition with the most free space.
2112 * Automatically excludes DOS, NTFS, SMB, and NFS filesystems.
2113 * @param bkpinfo The backup information structure. @c bkpinfo->tmpdir and @c bkpinfo->scratchdir will be set.
2114 * @ingroup utilityGroup
2115 */
2116void sensibly_set_scratchdir()
2117{
2118 char *tmp = NULL;
2119 char *command = NULL;
2120 char *sz = NULL;
2121
2122#ifdef __FreeBSD__
2123 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output("LANGUAGE=C df -m -P -t nonfs,msdosfs,ntfs,ntfs-3g,smbfs,smb,cifs,afs,gfs,ocfs,ocfs2,mvfs,nsspool,nssvol | tr -s '\t' ' ' | grep -vE \"none|Filesystem\" | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'"));
2124#else
2125 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output("LANGUAGE=C df -m -P -x nfs -x nfs4 -x vfat -x ntfs -x ntfs-3g -x smbfs -x smb -x cifs -x afs -x gfs -x ocfs -x ocfs2 -x mvfs -x nsspool -x nssvol -x iso9660| sed 's/ /devdev/' | tr -s '\t' ' ' | grep -vE \"none|Filesystem|/dev/shm\" | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'"));
2126#endif
2127
2128 if (tmp[0] != '/') {
2129 mr_asprintf(sz, "%s", tmp);
2130 mr_free(tmp);
2131 mr_asprintf(tmp, "/%s", sz);
2132 mr_free(sz);
2133 }
2134 if (!tmp[0]) {
2135 fatal_error("I couldn't figure out the scratchdir!");
2136 }
2137 mr_free(bkpinfo->scratchdir);
2138 mr_asprintf(bkpinfo->scratchdir , "%s/mondo.scratch.%d", tmp, (int) (random() % 32768));
2139 log_it("bkpinfo->scratchdir is being set to %s", bkpinfo->scratchdir);
2140
2141 mr_asprintf(command, "rm -Rf %s/mondo.scratch.*", tmp);
2142 mr_free(tmp);
2143
2144 paranoid_system(command);
2145 mr_free(command);
2146}
2147
2148
2149
2150
2151
2152
2153/**
2154 * @addtogroup deviceGroup
2155 * @{
2156 */
2157/**
2158 * If we can read @p dev, set @p output to it.
2159 * If @p dev cannot be read, set @p output to "".
2160 * @param dev The device to check for.
2161 * @param output Set to @p dev if @p dev exists, "" otherwise.
2162 * @return TRUE if @p dev exists, FALSE if it doesn't.
2163 */
2164bool set_dev_to_this_if_rx_OK(char *output, char *dev)
2165{
2166 char *command = NULL;
2167 bool res;
2168
2169 if (!dev || dev[0] == '\0') {
2170 output[0] = '\0';
2171 return (FALSE);
2172 }
2173 log_msg(10, "Injecting %s", dev);
2174 inject_device(dev);
2175 if (!does_file_exist(dev)) {
2176 log_msg(10, "%s doesn't exist. Returning FALSE.", dev);
2177 return (FALSE);
2178 }
2179 mr_asprintf(command, "dd bs=%ld count=1 if=%s of=/dev/null &> /dev/null", 512L, dev);
2180 if (!run_program_and_log_output(command, FALSE)
2181 && !run_program_and_log_output(command, FALSE)) {
2182 strcpy(output, dev);
2183 log_msg(4, "Found it - %s", dev);
2184 res = TRUE;
2185 } else {
2186 output[0] = '\0';
2187 log_msg(4, "It's not %s", dev);
2188 res = FALSE;
2189 }
2190 mr_free(command);
2191 return(res);
2192}
2193
2194
2195
2196
2197
2198/**
2199 * Find out what number CD is in the drive.
2200 * @param bkpinfo The backup information structure. The @c bkpinfo->media_device field is the only one used.
2201 * @return The current CD number, or -1 if it could not be found.
2202 * @note If the CD is not mounted, it will be mounted
2203 * (and remain mounted after this function returns).
2204 */
2205int what_number_cd_is_this()
2206{
2207 int cd_number = -1;
2208 char *mountdev = NULL;
2209 char *tmp = NULL;
2210
2211 assert(bkpinfo != NULL);
2212 // log_it("Asking what_number_cd_is_this");
2213 if (g_ISO_restore_mode) {
2214 mr_asprintf(tmp, "mount | grep iso9660 | awk '{print $3;}'");
2215 mr_asprintf(mountdev, "%s%s", call_program_and_get_last_line_of_output(tmp), "/archives/THIS-CD-NUMBER");
2216 cd_number = atoi(last_line_of_file(mountdev));
2217 mr_free(mountdev);
2218 mr_free(tmp);
2219
2220 return (cd_number);
2221 }
2222
2223 mr_asprintf(mountdev, "%s", bkpinfo->media_device);
2224 if (!mountdev[0]) {
2225 log_it("(what_number_cd_is_this) Warning - media_device unknown. Finding out...");
2226 find_cdrom_device(bkpinfo->media_device, FALSE);
2227 }
2228 if (!is_this_device_mounted(MNT_CDROM)) {
2229 if (bkpinfo->backup_media_type == usb) {
2230 mount_USB_here(mountdev, MNT_CDROM);
2231 } else {
2232 mount_CDROM_here(mountdev, MNT_CDROM);
2233 }
2234 }
2235 mr_free(mountdev);
2236
2237 cd_number = atoi(last_line_of_file(MNT_CDROM "/archives/THIS-CD-NUMBER"));
2238 return (cd_number);
2239}
2240
2241
2242
2243
2244
2245
2246
2247/**
2248 * Find out what device is mounted as root (/).
2249 * @return Root device.
2250 * @note The returned string points to static storage and will be overwritten with every call.
2251 * @bug A bit of a misnomer; it's actually finding out the root device.
2252 * The mountpoint (where it's mounted) will obviously be '/'.
2253 */
2254char *where_is_root_mounted()
2255{
2256 /*@ buffers **************** */
2257 static char tmp[MAX_STR_LEN];
2258
2259
2260#ifdef __FreeBSD__
2261 strcpy(tmp, call_program_and_get_last_line_of_output
2262 ("mount | grep \" on / \" | cut -d' ' -f1"));
2263#else
2264 strcpy(tmp, call_program_and_get_last_line_of_output
2265 ("mount | grep \" on / \" | cut -d' ' -f1 | sed s/[0-9]// | sed s/[0-9]//"));
2266 if (strstr(tmp, "/dev/cciss/")) {
2267 strcpy(tmp, call_program_and_get_last_line_of_output
2268 ("mount | grep \" on / \" | cut -d' ' -f1 | cut -dp -f1"));
2269 }
2270 if (strstr(tmp, "/dev/md")) {
2271 strcpy(tmp,
2272 call_program_and_get_last_line_of_output
2273 ("mount | grep \" on / \" | cut -d' ' -f1"));
2274 }
2275#endif
2276
2277 return (tmp);
2278}
2279
2280
2281/**
2282 * Find out which boot loader is in use.
2283 * @param which_device Device to look for the boot loader on.
2284 * @return 'L' for LILO, 'E'for ELILO, 'G' for GRUB, 'B' or 'D' for FreeBSD boot loaders, or 'U' for Unknown.
2285 * @note Under Linux, all drives are examined, not just @p which_device.
2286 */
2287#ifdef __FreeBSD__
2288char which_boot_loader(char *which_device)
2289{
2290 int count_lilos = 0;
2291 int count_grubs = 0;
2292 int count_boot0s = 0;
2293 int count_dangerouslydedicated = 0;
2294
2295 log_it("looking at drive %s's MBR", which_device);
2296 if (does_string_exist_in_boot_block(which_device, "GRUB")) {
2297 count_grubs++;
2298 }
2299 if (does_string_exist_in_boot_block(which_device, "LILO")) {
2300 count_lilos++;
2301 }
2302 if (does_string_exist_in_boot_block(which_device, "Drive")) {
2303 count_boot0s++;
2304 }
2305 if (does_string_exist_in_first_N_blocks
2306 (which_device, "FreeBSD/i386", 17)) {
2307 count_dangerouslydedicated++;
2308 }
2309 log_it("%d grubs and %d lilos and %d elilos and %d boot0s and %d DD\n",
2310 count_grubs, count_lilos, count_elilos, count_boot0s,
2311 count_dangerouslydedicated);
2312
2313 if (count_grubs && !count_lilos) {
2314 return ('G');
2315 } else if (count_lilos && !count_grubs) {
2316 return ('L');
2317 } else if (count_grubs == 1 && count_lilos == 1) {
2318 log_it("I'll bet you used to use LILO but switched to GRUB...");
2319 return ('G');
2320 } else if (count_boot0s == 1) {
2321 return ('B');
2322 } else if (count_dangerouslydedicated) {
2323 return ('D');
2324 } else {
2325 log_it("Unknown boot loader");
2326 return ('U');
2327 }
2328}
2329
2330#else
2331
2332char which_boot_loader(char *which_device)
2333{
2334 /*@ buffer ***************************************************** */
2335 char *list_drives_cmd = NULL;
2336 char *current_drive;
2337
2338 /*@ pointers *************************************************** */
2339 FILE *pdrives;
2340
2341 /*@ int ******************************************************** */
2342 int count_lilos = 0;
2343 int count_grubs = 0;
2344
2345 /*@ end vars *************************************************** */
2346
2347 malloc_string(current_drive);
2348
2349#ifdef __IA64__
2350 /* No choice for it */
2351 return ('E');
2352#endif
2353 assert(which_device != NULL);
2354
2355 mr_asprintf(list_drives_cmd, "parted2fdisk -l 2>/dev/null | grep \"/dev/.*:\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/; echo %s", where_is_root_mounted());
2356 log_it("list_drives_cmd = %s", list_drives_cmd);
2357
2358 if (!(pdrives = popen(list_drives_cmd, "r"))) {
2359 log_OS_error("Unable to open list of drives");
2360 mr_free(list_drives_cmd);
2361 paranoid_free(current_drive);
2362 return ('\0');
2363 }
2364 mr_free(list_drives_cmd);
2365
2366 for ((void)fgets(current_drive, MAX_STR_LEN, pdrives); !feof(pdrives);
2367 (void)fgets(current_drive, MAX_STR_LEN, pdrives)) {
2368 strip_spaces(current_drive);
2369 log_it("looking at drive %s's MBR", current_drive);
2370 if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
2371 count_grubs++;
2372 strcpy(which_device, current_drive);
2373 break;
2374 }
2375 if (does_string_exist_in_boot_block(current_drive, "LILO")) {
2376 count_lilos++;
2377 strcpy(which_device, current_drive);
2378 break;
2379 }
2380 }
2381 if (pclose(pdrives)) {
2382 log_OS_error("Cannot pclose pdrives");
2383 }
2384 log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
2385 if (count_grubs && !count_lilos) {
2386 paranoid_free(current_drive);
2387 return ('G');
2388 } else if (count_lilos && !count_grubs) {
2389 paranoid_free(current_drive);
2390 return ('L');
2391 } else if (count_grubs == 1 && count_lilos == 1) {
2392 log_it("I'll bet you used to use LILO but switched to GRUB...");
2393 paranoid_free(current_drive);
2394 return ('G');
2395 } else {
2396 // We need to look on each partition then
2397 mr_asprintf(list_drives_cmd, "parted2fdisk -l 2>/dev/null | grep -E \"^/dev/\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/");
2398 log_it("list_drives_cmd = %s", list_drives_cmd);
2399
2400 if (!(pdrives = popen(list_drives_cmd, "r"))) {
2401 log_OS_error("Unable to open list of drives");
2402 mr_free(list_drives_cmd);
2403 paranoid_free(current_drive);
2404 return ('\0');
2405 }
2406 mr_free(list_drives_cmd);
2407
2408 for ((void)fgets(current_drive, MAX_STR_LEN, pdrives); !feof(pdrives);
2409 (void)fgets(current_drive, MAX_STR_LEN, pdrives)) {
2410 strip_spaces(current_drive);
2411 log_it("looking at partition %s's BR", current_drive);
2412 if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
2413 count_grubs++;
2414 strcpy(which_device, current_drive);
2415 break;
2416 }
2417 if (does_string_exist_in_boot_block(current_drive, "LILO")) {
2418 count_lilos++;
2419 strcpy(which_device, current_drive);
2420 break;
2421 }
2422 }
2423 if (pclose(pdrives)) {
2424 log_OS_error("Cannot pclose pdrives");
2425 }
2426 log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
2427 paranoid_free(current_drive);
2428 if (count_grubs && !count_lilos) {
2429 return ('G');
2430 } else if (count_lilos && !count_grubs) {
2431 return ('L');
2432 } else if (count_grubs == 1 && count_lilos == 1) {
2433 log_it("I'll bet you used to use LILO but switched to GRUB...");
2434 return ('G');
2435 } else {
2436 log_it("Unknown boot loader");
2437 return ('U');
2438 }
2439 }
2440}
2441#endif
2442
2443
2444
2445
2446/**
2447 * Write zeroes over the first 16K of @p device.
2448 * @param device The device to zero.
2449 * @return 0 for success, 1 for failure.
2450 */
2451int zero_out_a_device(char *device)
2452{
2453 FILE *fout;
2454 int i;
2455
2456 assert_string_is_neither_NULL_nor_zerolength(device);
2457
2458 log_it("Zeroing drive %s", device);
2459 if (!(fout = fopen(device, "w"))) {
2460 log_OS_error("Unable to open/write to device");
2461 return (1);
2462 }
2463 for (i = 0; i < 16384; i++) {
2464 fputc('\0', fout);
2465 }
2466 paranoid_fclose(fout);
2467 log_it("Device successfully zeroed.");
2468 return (0);
2469}
2470
2471/**
2472 * Return the device pointed to by @p incoming.
2473 * @param incoming The device to resolve symlinks for.
2474 * @return The path to the real device file.
2475 * @note The returned string points to static storage that will be overwritten with each call.
2476 * @bug Won't work with file v4.0; needs to be written in C.
2477 */
2478char *resolve_softlinks_to_get_to_actual_device_file(char *incoming)
2479{
2480 static char output[MAX_STR_LEN];
2481 char *command = NULL;
2482 char *curr_fname;
2483 char *scratch = NULL;
2484 char *tmp = NULL;
2485 char *p;
2486
2487 struct stat statbuf;
2488 malloc_string(curr_fname);
2489 if (!does_file_exist(incoming)) {
2490 log_it
2491 ("resolve_softlinks_to_get_to_actual_device_file --- device not found");
2492 strcpy(output, incoming);
2493 } else {
2494 strcpy(curr_fname, incoming);
2495 lstat(curr_fname, &statbuf);
2496 while (S_ISLNK(statbuf.st_mode)) {
2497 log_msg(1, "curr_fname = %s", curr_fname);
2498 mr_asprintf(command, "file %s", curr_fname);
2499 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
2500 mr_free(command);
2501 for (p = tmp + strlen(tmp); p != tmp && *p != '`' && *p != ' ';
2502 p--);
2503 p++;
2504 mr_asprintf(scratch, "%s", p);
2505 for (p = scratch; *p != '\0' && *p != '\''; p++);
2506 *p = '\0';
2507 log_msg(0, "curr_fname %s --> '%s' --> %s", curr_fname, tmp, scratch);
2508 mr_free(tmp);
2509
2510 if (scratch[0] == '/') {
2511 strcpy(curr_fname, scratch); // copy whole thing because it's an absolute softlink
2512 } else { // copy over the basename cos it's a relative softlink
2513 p = curr_fname + strlen(curr_fname);
2514 while (p != curr_fname && *p != '/') {
2515 p--;
2516 }
2517 if (*p == '/') {
2518 p++;
2519 }
2520 strcpy(p, scratch);
2521 }
2522 mr_free(scratch);
2523 lstat(curr_fname, &statbuf);
2524 }
2525 strcpy(output, curr_fname);
2526 log_it("resolved %s to %s", incoming, output);
2527 }
2528 paranoid_free(curr_fname);
2529 return (output);
2530}
2531
2532/* @} - end of deviceGroup */
2533
2534
2535/**
2536 * Return the type of partition format (GPT or MBR)
2537 */
2538char *which_partition_format(const char *drive)
2539{
2540 static char output[4];
2541 char *tmp = NULL;
2542 char *command = NULL;
2543 char *fdisk = NULL;
2544#ifdef __IA64__
2545 struct stat buf;
2546#endif
2547 log_msg(0, "Looking for partition table format type");
2548 mr_asprintf(fdisk, "/sbin/parted2fdisk");
2549 log_msg(1, "Using %s", fdisk);
2550 mr_asprintf(command, "%s -l %s | grep 'EFI GPT'", fdisk, drive);
2551 mr_free(fdisk);
2552
2553 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
2554 mr_free(command);
2555
2556 if (strstr(tmp, "GPT") == NULL) {
2557 strcpy(output, "MBR");
2558 } else {
2559 strcpy(output, "GPT");
2560 }
2561 mr_free(tmp);
2562
2563 log_msg(0, "Found %s partition table format type", output);
2564 return (output);
2565}
2566
2567/* @} - end of deviceGroup */
Note: See TracBrowser for help on using the repository browser.