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

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