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

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

r3333@localhost: bruno | 2009-08-08 01:58:31 +0200

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