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

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

r3334@localhost: bruno | 2009-08-08 12:17:37 +0200

  • Change mr_asprintf interface to pass only the char * (makes bkpinfo usage more easy)
  • 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 2323 2009-08-18 13:05:43Z 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 2323 2009-08-18 13:05:43Z 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 *p = NULL;
1337 char *mds = NULL;
1338 char *request = NULL;
1339
1340 assert(bkpinfo != NULL);
1341 assert(cd_number_i_want > 0);
1342
1343// log_msg(3, "Insisting on CD number %d", cd_number_i_want);
1344
1345 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1346 log_msg(3,
1347 "No need to insist_on_this_cd_number when the backup type isn't CD-R(W) or NFS or ISO");
1348 return;
1349 }
1350 mr_asprintf(tmp, "mkdir -p " MNT_CDROM);
1351 run_program_and_log_output(tmp, 5);
1352 mr_free(tmp);
1353
1354 if (g_ISO_restore_mode || bkpinfo->backup_media_type == iso
1355 || bkpinfo->backup_media_type == nfs) {
1356 log_msg(3, "Remounting CD");
1357 g_ISO_restore_mode = TRUE;
1358 // FIXME --- I'm tempted to do something about this...
1359 // Why unmount and remount again and again?
1360 if (is_this_device_mounted(MNT_CDROM)) {
1361 run_program_and_log_output("umount " MNT_CDROM, 5);
1362 }
1363 mr_asprintf(tmp, "mkdir -p %s/isodir &> /dev/null", bkpinfo->tmpdir);
1364 (void)system(tmp);
1365 mr_free(tmp);
1366
1367 mr_asprintf(tmp, "%s/%s/%s-%d.iso", bkpinfo->isodir, bkpinfo->nfs_remote_dir, bkpinfo->prefix, cd_number_i_want);
1368 if (!does_file_exist(tmp)) {
1369 mr_free(tmp);
1370 mr_asprintf(tmp, "%s/isodir/%s/%s-%d.iso", bkpinfo->tmpdir, bkpinfo->nfs_remote_dir, bkpinfo->prefix, cd_number_i_want);
1371 if (does_file_exist(tmp)) {
1372 log_msg(1, "FIXME - hacking bkpinfo->isodir from '%s' to %s/isodir", bkpinfo->isodir, bkpinfo->tmpdir);
1373 mr_free(bkpinfo->isodir);
1374 mr_asprintf(p, "%s/isodir", bkpinfo->tmpdir);
1375 bkpinfo->isodir = p;
1376 }
1377 }
1378 log_msg(3, "Mounting %s at %s", tmp, MNT_CDROM);
1379 if (mount_CDROM_here(tmp, MNT_CDROM)) {
1380 mr_free(tmp);
1381 fatal_error("Mommy!");
1382 }
1383 mr_free(tmp);
1384 }
1385 if ((res = what_number_cd_is_this()) != cd_number_i_want) {
1386 log_msg(3, "Currently, we hold %d but we want %d", res,
1387 cd_number_i_want);
1388 mds = media_descriptor_string(bkpinfo->backup_media_type);
1389 mr_asprintf(tmp, "Insisting on %s #%d", mds, cd_number_i_want);
1390 mr_asprintf(request, "Please insert %s #%d and press Enter.", mds, cd_number_i_want);
1391 mr_free(mds);
1392 log_msg(3, tmp);
1393 mr_free(tmp);
1394
1395 while (what_number_cd_is_this() != cd_number_i_want) {
1396 paranoid_system("sync");
1397 if (is_this_device_mounted(MNT_CDROM)) {
1398 res =
1399 run_program_and_log_output("umount " MNT_CDROM, FALSE);
1400 } else {
1401 res = 0;
1402 }
1403 if (res) {
1404 log_to_screen("WARNING - failed to unmount CD-ROM drive");
1405 }
1406 if (!bkpinfo->please_dont_eject) {
1407 res = eject_device(bkpinfo->media_device);
1408 } else {
1409 res = 0;
1410 }
1411 if (res) {
1412 log_to_screen("WARNING - failed to eject CD-ROM disk");
1413 }
1414 popup_and_OK(request);
1415 if (!bkpinfo->please_dont_eject) {
1416 inject_device(bkpinfo->media_device);
1417 }
1418 paranoid_system("sync");
1419 }
1420 mr_free(request);
1421
1422 log_msg(1, "Thankyou. Proceeding...");
1423 g_current_media_number = cd_number_i_want;
1424 }
1425}
1426
1427/* @} - end of deviceGroup */
1428
1429
1430/**
1431 * Ask user for details of backup/restore information.
1432 * Called when @c mondoarchive doesn't get any parameters.
1433 * @param bkpinfo The backup information structure to fill out with the user's data.
1434 * @param archiving_to_media TRUE if archiving, FALSE if restoring.
1435 * @return 0, always.
1436 * @bug No point of `int' return value.
1437 * @ingroup archiveGroup
1438 */
1439int interactively_obtain_media_parameters_from_user(bool archiving_to_media)
1440// archiving_to_media is TRUE if I'm being called by mondoarchive
1441// archiving_to_media is FALSE if I'm being called by mondorestore
1442{
1443 char *tmp = NULL;
1444 char *p = NULL;
1445 char *mds = NULL;
1446 char *sz_size = NULL;
1447 char *command = NULL;
1448 char *comment = NULL;
1449 char *prompt;
1450 int i;
1451 FILE *fin;
1452
1453 malloc_string(prompt);
1454 malloc_string(tmp1);
1455 assert(bkpinfo != NULL);
1456 bkpinfo->nonbootable_backup = FALSE;
1457
1458 // Tape, CD, NFS, ...?
1459 srandom(getpid());
1460 bkpinfo->backup_media_type =
1461 (g_restoring_live_from_cd) ? cdr :
1462 which_backup_media_type(bkpinfo->restore_data);
1463 if (bkpinfo->backup_media_type == none) {
1464 log_to_screen("User has chosen not to backup the PC");
1465 finish(1);
1466 }
1467 /* Why asking to remove the media with tape ?
1468 if (bkpinfo->backup_media_type == tape && bkpinfo->restore_data) {
1469 popup_and_OK("Please remove media from drive(s)");
1470 }
1471 */
1472 log_msg(3, "media type = %s", bkptype_to_string(bkpinfo->backup_media_type));
1473 bkpinfo->cdrw_speed = (bkpinfo->backup_media_type == cdstream) ? 2 : 4;
1474 bkpinfo->compression_level =
1475 (bkpinfo->backup_media_type == cdstream) ? 1 : 5;
1476 bkpinfo->use_lzo =
1477 (bkpinfo->backup_media_type == cdstream) ? TRUE : FALSE;
1478 mvaddstr_and_log_it(2, 0, " ");
1479
1480 // Find device's /dev (or SCSI) entry
1481 switch (bkpinfo->backup_media_type) {
1482 case cdr:
1483 case cdrw:
1484 case dvd:
1485 case usb:
1486 /* Never try to eject a USB device */
1487 if (bkpinfo->backup_media_type == usb) {
1488 bkpinfo->please_dont_eject = TRUE;
1489 }
1490 if (archiving_to_media) {
1491 if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) {
1492 if (ask_me_yes_or_no
1493 ("Is your computer a laptop, or does the CD writer incorporate BurnProof technology?"))
1494 {
1495 bkpinfo->manual_cd_tray = TRUE;
1496 }
1497 }
1498 if ((bkpinfo->compression_level =
1499 which_compression_level()) == -1) {
1500 log_to_screen("User has chosen not to backup the PC");
1501 finish(1);
1502 }
1503 mds = media_descriptor_string(bkpinfo->backup_media_type);
1504 mr_asprintf(comment, "What speed is your %s (re)writer?", mds);
1505 if (bkpinfo->backup_media_type == dvd) {
1506 find_dvd_device(bkpinfo->media_device, FALSE);
1507 mr_asprintf(tmp, "1");
1508 mr_asprintf(sz_size, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4482 MB
1509 log_msg(1, "Setting to DVD defaults");
1510 } else {
1511 strcpy(bkpinfo->media_device, VANILLA_SCSI_CDROM);
1512 mr_asprintf(tmp, "4");
1513 mr_asprintf(sz_size, "%d", 650);
1514 log_msg(1, "Setting to CD defaults");
1515 }
1516 if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) {
1517 p = popup_and_get_string("Speed", comment, tmp);
1518 mr_free(tmp);
1519
1520 if (p == NULL) {
1521 log_to_screen("User has chosen not to backup the PC");
1522 mr_free(comment);
1523 finish(1);
1524 }
1525 /* tmp now has the new value given by the user */
1526 tmp = p;
1527 }
1528 mr_free(comment);
1529
1530 bkpinfo->cdrw_speed = atoi(tmp1); // if DVD then this shouldn't ever be used anyway :)
1531
1532 mr_asprintf(comment, "How much data (in Megabytes) will each %s store?", mds);
1533 mr_free(mds);
1534 p = popup_and_get_string("Size", comment, sz_size);
1535 mr_free(sz_size);
1536 mr_free(comment);
1537
1538 if (p == NULL) {
1539 log_to_screen("User has chosen not to backup the PC");
1540 finish(1);
1541 }
1542 sz_size = p;
1543
1544 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1545 bkpinfo->media_size[i] = atoi(sz_size);
1546 }
1547
1548 if (bkpinfo->media_size[0] <= 0) {
1549 log_to_screen("User has chosen not to backup the PC");
1550 finish(1);
1551 }
1552 }
1553 /* No break because we continue even for usb */
1554 case cdstream:
1555 mds = media_descriptor_string(bkpinfo->backup_media_type);
1556
1557 if ((bkpinfo->disaster_recovery) && (bkpinfo->backup_media_type != usb)) {
1558 strcpy(bkpinfo->media_device, "/dev/cdrom");
1559 log_msg(2, "CD-ROM device assumed to be at %s",
1560 bkpinfo->media_device);
1561 } else if ((bkpinfo->restore_data && (bkpinfo->backup_media_type != usb))
1562 || bkpinfo->backup_media_type == dvd) {
1563 if (!bkpinfo->media_device[0]) {
1564 strcpy(bkpinfo->media_device, "/dev/cdrom");
1565 } // just for the heck of it :)
1566 log_msg(1, "bkpinfo->media_device = %s",
1567 bkpinfo->media_device);
1568 if (bkpinfo->backup_media_type == dvd
1569 || find_cdrom_device(bkpinfo->media_device, FALSE)) {
1570 log_msg(1, "bkpinfo->media_device = %s",
1571 bkpinfo->media_device);
1572 mr_asprintf(comment, "Please specify your %s drive's /dev entry", mds);
1573 p = popup_and_get_string("Device?", comment, bkpinfo->media_device);
1574 mr_free(comment);
1575
1576 if (p == NULL) {
1577 log_to_screen("User has chosen not to backup the PC");
1578 finish(1);
1579 }
1580 strcpy(bkpinfo->media_device, p);
1581 mr_free(p);
1582 }
1583 log_msg(2, "%s device found at %s", mds, bkpinfo->media_device);
1584 } else {
1585 if ((find_cdrw_device(bkpinfo->media_device)) && (bkpinfo->backup_media_type != usb)) {
1586 bkpinfo->media_device[0] = '\0';
1587 }
1588 if (bkpinfo->media_device[0]) {
1589 if (bkpinfo->backup_media_type == usb) {
1590 mr_asprintf(tmp, "I think your %s media corresponds to %s. Is this correct?", mds, bkpinfo->media_device);
1591 } else {
1592 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);
1593 }
1594 if (!ask_me_yes_or_no(tmp)) {
1595 bkpinfo->media_device[0] = '\0';
1596 }
1597 mr_free(tmp);
1598 }
1599 if (!bkpinfo->media_device[0]) {
1600 if (bkpinfo->backup_media_type == usb) {
1601 p = popup_and_get_string("/dev entry?", "What is the /dev entry of your USB Disk/Key, please?", bkpinfo->media_device);
1602 } else {
1603 if (g_kernel_version < 2.6) {
1604 p = popup_and_get_string("Device node?", "What is the SCSI node of your CD (re)writer, please?", bkpinfo->media_device);
1605 } else {
1606 p = popup_and_get_string("/dev entry?", "What is the /dev entry of your CD (re)writer, please?", bkpinfo->media_device);
1607 }
1608 }
1609 if (p == NULL) {
1610 log_to_screen("User has chosen not to backup the PC");
1611 finish(1);
1612 }
1613 strcpy(bkpinfo->media_device, p);
1614 mr_free(p);
1615 }
1616 }
1617 mr_free(mds);
1618
1619 if (bkpinfo->backup_media_type == cdstream) {
1620 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1621 bkpinfo->media_size[i] = 650;
1622 }
1623 }
1624 break;
1625 case udev:
1626 if (!ask_me_yes_or_no
1627 ("This option is for advanced users only. Are you sure?")) {
1628 log_to_screen("User has chosen not to backup the PC");
1629 finish(1);
1630 }
1631 case tape:
1632
1633 if ((!bkpinfo->restore_mode) && (!mr_find_tape_device())) {
1634 log_msg(3, "Ok, using vanilla scsi tape.");
1635 strcpy(bkpinfo->media_device, VANILLA_SCSI_TAPE);
1636 if ((fin = fopen(bkpinfo->media_device, "r"))) {
1637 paranoid_fclose(fin);
1638 } else {
1639 strcpy(bkpinfo->media_device, "/dev/osst0");
1640 }
1641 }
1642 if (bkpinfo->media_device[0]) {
1643 if ((fin = fopen(bkpinfo->media_device, "r"))) {
1644 paranoid_fclose(fin);
1645 } else {
1646 if (does_file_exist("/tmp/mondo-restore.cfg")) {
1647 read_cfg_var("/tmp/mondo-restore.cfg", "media-dev",
1648 bkpinfo->media_device);
1649 }
1650 }
1651 }
1652 if (bkpinfo->media_device[0]) {
1653 mr_asprintf(tmp, "I think I've found your tape streamer at %s; am I right on the money?", bkpinfo->media_device);
1654 if (!ask_me_yes_or_no(tmp)) {
1655 bkpinfo->media_device[0] = '\0';
1656 }
1657 mr_free(tmp);
1658 }
1659 if (!bkpinfo->media_device[0]) {
1660 p = popup_and_get_string("Device name?", "What is the /dev entry of your tape streamer?", bkpinfo->media_device);
1661 if (p == NULL) {
1662 log_to_screen("User has chosen not to backup the PC");
1663 finish(1);
1664 }
1665 strcpy(bkpinfo->media_device, p);
1666 mr_free(p);
1667 }
1668 mr_asprintf(tmp, "ls -l %s", bkpinfo->media_device);
1669 if (run_program_and_log_output(tmp, FALSE)) {
1670 log_to_screen("User has not specified a valid /dev entry");
1671 finish(1);
1672 }
1673 mr_free(tmp);
1674
1675 bkpinfo->use_obdr = ask_me_yes_or_no
1676 ("Do you want to activate OBDR support for your tapes ?");
1677 bkpinfo->media_size[0] = 0;
1678 log_msg(4, "media_size[0] = %ld", bkpinfo->media_size[0]);
1679 if (bkpinfo->media_size[0] <= 0) {
1680 bkpinfo->media_size[0] = 0;
1681 }
1682 for (i = 1; i <= MAX_NOOF_MEDIA; i++) {
1683 bkpinfo->media_size[i] = bkpinfo->media_size[0];
1684 }
1685 if (archiving_to_media) {
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 break;
1693
1694
1695
1696 case nfs:
1697 /* Never try to eject a NFS device */
1698 bkpinfo->please_dont_eject = TRUE;
1699
1700 /* Initiate bkpinfo nfs_mount path from running environment if not already done */
1701 if (!bkpinfo->nfs_mount[0]) {
1702 strcpy(bkpinfo->nfs_mount,
1703 call_program_and_get_last_line_of_output
1704 ("mount | grep \":\" | cut -d' ' -f1 | head -n1"));
1705 }
1706#ifdef __FreeBSD__
1707 if (TRUE)
1708#else
1709 if (!bkpinfo->disaster_recovery)
1710#endif
1711 {
1712 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);
1713 if (p == NULL) {
1714 log_to_screen("User has chosen not to backup the PC");
1715 finish(1);
1716 }
1717 strcpy(bkpinfo->nfs_mount, p);
1718 mr_free(p);
1719 if (!bkpinfo->restore_data) {
1720 if ((bkpinfo->compression_level =
1721 which_compression_level()) == -1) {
1722 log_to_screen("User has chosen not to backup the PC");
1723 finish(1);
1724 }
1725 }
1726 // check whether already mounted - we better remove
1727 // surrounding spaces and trailing '/' for this
1728 strip_spaces(bkpinfo->nfs_mount);
1729 if (bkpinfo->nfs_mount[strlen(bkpinfo->nfs_mount) - 1] == '/')
1730 bkpinfo->nfs_mount[strlen(bkpinfo->nfs_mount) - 1] = '\0';
1731 mr_asprintf(command, "mount | grep \"%s \" | cut -d' ' -f3", bkpinfo->nfs_mount);
1732 mr_free(bkpinfo->isodir);
1733 mr_asprintf(p, "%s", call_program_and_get_last_line_of_output(command));
1734 bkpinfo->isodir = p;
1735 mr_free(command);
1736
1737 if (!bkpinfo->restore_data) {
1738 mr_asprintf(comment, "How much data (in Megabytes) will each media store?");
1739 // BERLIOS: 4480 shouldn't be hardcoded here
1740 sz_size = popup_and_get_string("Size", comment, "4480");
1741 mr_free(comment);
1742 if (sz_size == NULL) {
1743 log_to_screen("User has chosen not to backup the PC");
1744 finish(1);
1745 }
1746 } else {
1747 mr_asprintf(sz_size, "0");
1748 }
1749 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1750 bkpinfo->media_size[i] = atoi(sz_size);
1751 }
1752 mr_free(sz_size);
1753 if (bkpinfo->media_size[0] < 0) {
1754 log_to_screen("User has chosen not to backup the PC");
1755 finish(1);
1756 }
1757 }
1758 if (bkpinfo->disaster_recovery) {
1759 mr_asprintf(command ,"umount %s/isodir 2> /dev/null", bkpinfo->tmpdir);
1760 (void)system(command);
1761 mr_free(command);
1762
1763 p = popup_and_get_string("NFS share", "Which remote NFS share should I mount?", bkpinfo->nfs_mount);
1764 if (p == NULL) {
1765 log_to_screen("User has chosen not to backup the PC");
1766 finish(1);
1767 }
1768 strcpy(bkpinfo->nfs_mount, p);
1769 mr_free(p);
1770 }
1771 /* Initiate bkpinfo isodir path from running environment if mount already done */
1772 mr_free(bkpinfo->isodir);
1773 if (is_this_device_mounted(bkpinfo->nfs_mount)) {
1774 mr_asprintf(p, "%s", call_program_and_get_last_line_of_output("mount | grep \":\" | cut -d' ' -f3 | head -n1"));
1775 bkpinfo->isodir = p;
1776
1777 } else {
1778 mr_asprintf(p, "%s/nfsdir", bkpinfo->tmpdir);
1779 bkpinfo->isodir = p;
1780 mr_asprintf(command, "mkdir -p %s", bkpinfo->isodir);
1781 run_program_and_log_output(command, 5);
1782 mr_free(command);
1783
1784 mr_asprintf(tmp, "mount -t nfs -o nolock %s %s", bkpinfo->nfs_mount, bkpinfo->isodir);
1785 run_program_and_log_output(tmp, 3);
1786 mr_free(tmp);
1787
1788 malloc_string(g_selfmounted_isodir);
1789 strcpy(g_selfmounted_isodir, bkpinfo->isodir);
1790 }
1791 if (!is_this_device_mounted(bkpinfo->nfs_mount)) {
1792 popup_and_OK
1793 ("Please mount that partition before you try to backup to or restore from it.");
1794 finish(1);
1795 }
1796 p = popup_and_get_string("Directory", "Which directory within that mountpoint?", bkpinfo->nfs_remote_dir);
1797 if (p == NULL) {
1798 log_to_screen("User has chosen not to backup the PC");
1799 finish(1);
1800 }
1801 strcpy(bkpinfo->nfs_remote_dir, p);
1802 mr_free(p);
1803
1804 // check whether writable - we better remove surrounding spaces for this
1805 strip_spaces(bkpinfo->nfs_remote_dir);
1806
1807 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);
1808 if (p == NULL) {
1809 log_to_screen("User has chosen not to backup the PC");
1810 finish(1);
1811 }
1812 mr_free(bkpinfo->prefix);
1813 bkpinfo->prefix = p;
1814 log_msg(3, "prefix set to %s", bkpinfo->prefix);
1815
1816 log_msg(3, "Just set nfs_remote_dir to %s", bkpinfo->nfs_remote_dir);
1817 log_msg(3, "isodir is still %s", bkpinfo->isodir);
1818 break;
1819
1820 case iso:
1821 if (!bkpinfo->disaster_recovery) {
1822 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);
1823 if (p == NULL) {
1824 log_to_screen("User has chosen not to backup the PC");
1825 finish(1);
1826 }
1827 bkpinfo->isodir = p;
1828
1829 if (archiving_to_media) {
1830 if ((bkpinfo->compression_level =
1831 which_compression_level()) == -1) {
1832 log_to_screen("User has chosen not to backup the PC");
1833 finish(1);
1834 }
1835 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);
1836 if (p == NULL) {
1837 log_to_screen("User has chosen not to backup the PC");
1838 finish(1);
1839 }
1840 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1841 bkpinfo->media_size[i] = atoi(p);
1842 }
1843 mr_free(p);
1844 } else {
1845 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1846 bkpinfo->media_size[i] = 650;
1847 }
1848 }
1849 }
1850 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);
1851 if (p == NULL) {
1852 log_to_screen("User has chosen not to backup the PC");
1853 finish(1);
1854 }
1855 mr_free(bkpinfo->prefix);
1856 bkpinfo->prefix = p;
1857 log_msg(3, "prefix set to %s", bkpinfo->prefix);
1858 break;
1859
1860 default:
1861 fatal_error
1862 ("I, Mojo Jojo, shall defeat those pesky Powerpuff Girls!");
1863 }
1864
1865 if (archiving_to_media) {
1866
1867#ifdef __FreeBSD__
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]\\).*/\\1/'"));
1871#else
1872 strcpy(bkpinfo->boot_device,
1873 call_program_and_get_last_line_of_output
1874 ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'"));
1875#endif
1876 i = which_boot_loader(bkpinfo->boot_device);
1877 if (i == 'U') // unknown
1878 {
1879
1880#ifdef __FreeBSD__
1881 p = popup_and_get_string("Boot device", "What is your boot device? (e.g. /dev/ad0)", bkpinfo->boot_device);
1882 if (!p == NULL) {
1883 log_to_screen("User has chosen not to backup the PC");
1884 finish(1);
1885 }
1886 strcpy(bkpinfo->boot_device, p);
1887 mr_free(p);
1888 i = which_boot_loader(bkpinfo->boot_device);
1889#else
1890 p = popup_and_get_string("Boot device", "What is your boot device? (e.g. /dev/hda)", bkpinfo->boot_device);
1891 if (p == NULL) {
1892 log_to_screen("User has chosen not to backup the PC");
1893 finish(1);
1894 }
1895 strcpy(bkpinfo->boot_device, p);
1896 mr_free(p);
1897
1898 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "LILO")) {
1899 i = 'L';
1900 } else
1901 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "ELILO")) {
1902 i = 'E';
1903 } else
1904 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "GRUB")) {
1905 i = 'G';
1906 } else {
1907 i = 'U';
1908 }
1909#endif
1910 if (i == 'U') {
1911 if (ask_me_yes_or_no
1912 ("Unidentified boot loader. Shall I restore it byte-for-byte at restore time and hope for the best?"))
1913 {
1914 i = 'R'; // raw
1915 } else {
1916 log_to_screen
1917 ("I cannot find your boot loader. Please run mondoarchive with parameters.");
1918 finish(1);
1919 }
1920 }
1921 }
1922 bkpinfo->boot_loader = i;
1923 mr_free(bkpinfo->include_paths);
1924 mr_asprintf(p, "/");
1925 bkpinfo->include_paths = p;
1926
1927 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);
1928 if (p == NULL) {
1929 log_to_screen("User has chosen not to backup the PC");
1930 finish(1);
1931 }
1932 mr_free(bkpinfo->include_paths);
1933 bkpinfo->include_paths = p;
1934
1935 mr_asprintf(tmp, "%s", list_of_NFS_mounts_only());
1936 if (strlen(tmp) > 2) {
1937 mr_strcat(bkpinfo->exclude_paths, " %s",tmp);
1938 }
1939 mr_free(tmp);
1940// NTFS
1941 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};'"));
1942 if (strlen(tmp) > 2) {
1943 p = popup_and_get_string("NTFS partitions", "Please enter/confirm the NTFS partitions you wish to backup as well.", tmp);
1944
1945 if (p == NULL) {
1946 log_to_screen("User has chosen not to backup the PC");
1947 finish(1);
1948 }
1949 strncpy(bkpinfo->image_devs, p, MAX_STR_LEN / 4);
1950 mr_free(p);
1951 }
1952
1953
1954 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);
1955 if (p == NULL) {
1956 log_to_screen("User has chosen not to backup the PC");
1957 finish(1);
1958 }
1959 mr_free(bkpinfo->exclude_paths);
1960 bkpinfo->exclude_paths = p;
1961
1962 p = popup_and_get_string("Temporary directory", "Please enter your temporary directory.", bkpinfo->tmpdir);
1963 if (p == NULL) {
1964 log_to_screen("User has chosen not to backup the PC");
1965 finish(1);
1966 }
1967 mr_free(bkpinfo->tmpdir);
1968 bkpinfo->tmpdir = p;
1969
1970 p = popup_and_get_string("Scratch directory", "Please enter your scratch directory.", bkpinfo->scratchdir);
1971 if (p == NULL) {
1972 log_to_screen("User has chosen not to backup the PC");
1973 finish(1);
1974 }
1975 mr_free(bkpinfo->scratchdir);
1976 bkpinfo->scratchdir = p;
1977
1978// Interactive mode:
1979#ifdef __IA64__
1980 bkpinfo->make_cd_use_lilo = TRUE;
1981#else
1982 bkpinfo->make_cd_use_lilo = FALSE;
1983#endif
1984 bkpinfo->backup_data = TRUE;
1985 bkpinfo->verify_data =
1986 ask_me_yes_or_no
1987 ("Will you want to verify your backups after Mondo has created them?");
1988
1989#ifndef __FreeBSD__
1990 if (!ask_me_yes_or_no
1991 ("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."))
1992#endif
1993 {
1994 strcpy(bkpinfo->kernel_path, "FAILSAFE");
1995 }
1996
1997 if (!ask_me_yes_or_no
1998 ("Are you sure you want to proceed? Hit 'no' to abort.")) {
1999 log_to_screen("User has chosen not to backup the PC");
2000 finish(1);
2001 }
2002 } else {
2003 bkpinfo->restore_data = TRUE; // probably...
2004 }
2005
2006 if (bkpinfo->backup_media_type == iso
2007 || bkpinfo->backup_media_type == nfs) {
2008 g_ISO_restore_mode = TRUE;
2009 }
2010#ifdef __FreeSD__
2011// skip
2012#else
2013 if (bkpinfo->backup_media_type == nfs) {
2014 log_msg(3, "I think the NFS mount is mounted at %s", bkpinfo->isodir);
2015 }
2016 log_it("isodir = %s", bkpinfo->isodir);
2017 log_it("nfs_mount = '%s'", bkpinfo->nfs_mount);
2018 if (bkpinfo->nfs_user) {
2019 log_it("nfs_user = '%s'", bkpinfo->nfs_user);
2020 }
2021#endif
2022
2023 log_it("media device = %s", bkpinfo->media_device);
2024 log_it("media size = %ld", bkpinfo->media_size[1]);
2025 log_it("media type = %s", bkptype_to_string(bkpinfo->backup_media_type));
2026 log_it("prefix = %s", bkpinfo->prefix);
2027 log_it("compression = %ld", bkpinfo->compression_level);
2028 log_it("include_paths = '%s'", bkpinfo->include_paths);
2029 log_it("exclude_paths = '%s'", bkpinfo->exclude_paths);
2030 log_it("scratchdir = '%s'", bkpinfo->scratchdir);
2031 log_it("tmpdir = '%s'", bkpinfo->tmpdir);
2032 log_it("image_devs = '%s'", bkpinfo->image_devs);
2033 log_it("boot_device = '%s' (loader=%c)", bkpinfo->boot_device, bkpinfo->boot_loader);
2034 if (bkpinfo->media_size[0] < 0) {
2035 if (archiving_to_media) {
2036 fatal_error("Media size is less than zero.");
2037 } else {
2038 log_msg(2, "Warning - media size is less than zero.");
2039 bkpinfo->media_size[0] = 0;
2040 }
2041 }
2042 paranoid_free(sz_size);
2043 paranoid_free(tmp1);
2044 paranoid_free(prompt);
2045 return (0);
2046}
2047
2048
2049
2050
2051/**
2052 * @addtogroup utilityGroup
2053 * @{
2054 */
2055/**
2056 * Get a space-separated list of NFS devices and mounts.
2057 * @return The list created.
2058 * @note The return value points to static data that will be overwritten with each call.
2059 */
2060char *list_of_NFS_devices_and_mounts(void)
2061{
2062 char *exclude_these_devices = NULL;
2063 char *exclude_these_directories = NULL;
2064 static char result_sz[1024];
2065
2066 mr_asprintf(exclude_these_directories,"%s",list_of_NFS_mounts_only());
2067 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;}'"));
2068 snprintf(result_sz, 1023, "%s %s", exclude_these_directories, exclude_these_devices);
2069 mr_free(exclude_these_devices);
2070 mr_free(exclude_these_directories);
2071 return (result_sz);
2072}
2073
2074
2075
2076
2077/**
2078 * Get a space-separated list of NFS mounts.
2079 * @return The list created.
2080 * @note The return value points to static data that will be overwritten with each call.
2081 * @bug Even though we only want the mounts, the devices are still checked.
2082 */
2083char *list_of_NFS_mounts_only(void)
2084{
2085 char *exclude_these_directories = NULL;
2086 static char result_sz[512];
2087
2088 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;}'"));
2089 snprintf(result_sz, 511, "%s", exclude_these_directories);
2090 mr_free(exclude_these_directories);
2091 return (result_sz);
2092}
2093
2094/* @} - end of utilityGroup */
2095
2096
2097
2098
2099
2100/**
2101 * Create a randomly-named FIFO. The format is @p stub "." [random] [random] where
2102 * [random] is a random number between 1 and 32767.
2103 * @param store_name_here Where to store the new filename.
2104 * @param stub A random number will be appended to this to make the FIFO's name.
2105 * @ingroup deviceGroup
2106 */
2107void make_fifo(char *store_name_here, char *stub)
2108{
2109 char *tmp = NULL;
2110
2111 assert_string_is_neither_NULL_nor_zerolength(stub);
2112
2113 sprintf(store_name_here, "%s%d%d", stub, (int) (random() % 32768),
2114 (int) (random() % 32768));
2115 make_hole_for_file(store_name_here);
2116 mkfifo(store_name_here, S_IRWXU | S_IRWXG);
2117 mr_asprintf(tmp, "chmod 770 %s", store_name_here);
2118 paranoid_system(tmp);
2119 mr_free(tmp);
2120}
2121
2122
2123
2124
2125
2126
2127/**
2128 * Set the tmpdir and scratchdir to reside on the partition with the most free space.
2129 * Automatically excludes DOS, NTFS, SMB, and NFS filesystems.
2130 * @param bkpinfo The backup information structure. @c bkpinfo->tmpdir and @c bkpinfo->scratchdir will be set.
2131 * @ingroup utilityGroup
2132 */
2133void sensibly_set_scratchdir()
2134{
2135 char *tmp = NULL;
2136 char *command = NULL;
2137 char *sz = NULL;
2138
2139#ifdef __FreeBSD__
2140 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;}'"));
2141#else
2142 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;}'"));
2143#endif
2144
2145 if (tmp[0] != '/') {
2146 mr_asprintf(sz, "%s", tmp);
2147 mr_free(tmp);
2148 mr_asprintf(tmp, "/%s", sz);
2149 mr_free(sz);
2150 }
2151 if (!tmp[0]) {
2152 fatal_error("I couldn't figure out the scratchdir!");
2153 }
2154 mr_asprintf(sz, "%s/mondo.scratch.%d", tmp, (int) (random() % 32768));
2155 mr_free(bkpinfo->scratchdir);
2156 bkpinfo->scratchdir = sz;
2157 log_it("bkpinfo->scratchdir is being set to %s", bkpinfo->scratchdir);
2158
2159 mr_asprintf(command, "rm -Rf %s/mondo.scratch.*", tmp);
2160 mr_free(tmp);
2161
2162 paranoid_system(command);
2163 mr_free(command);
2164}
2165
2166
2167
2168
2169
2170
2171/**
2172 * @addtogroup deviceGroup
2173 * @{
2174 */
2175/**
2176 * If we can read @p dev, set @p output to it.
2177 * If @p dev cannot be read, set @p output to "".
2178 * @param dev The device to check for.
2179 * @param output Set to @p dev if @p dev exists, "" otherwise.
2180 * @return TRUE if @p dev exists, FALSE if it doesn't.
2181 */
2182bool set_dev_to_this_if_rx_OK(char *output, char *dev)
2183{
2184 char *command = NULL;
2185 bool res;
2186
2187 if (!dev || dev[0] == '\0') {
2188 output[0] = '\0';
2189 return (FALSE);
2190 }
2191 log_msg(10, "Injecting %s", dev);
2192 inject_device(dev);
2193 if (!does_file_exist(dev)) {
2194 log_msg(10, "%s doesn't exist. Returning FALSE.", dev);
2195 return (FALSE);
2196 }
2197 mr_asprintf(command, "dd bs=%ld count=1 if=%s of=/dev/null &> /dev/null", 512L, dev);
2198 if (!run_program_and_log_output(command, FALSE)
2199 && !run_program_and_log_output(command, FALSE)) {
2200 strcpy(output, dev);
2201 log_msg(4, "Found it - %s", dev);
2202 res = TRUE;
2203 } else {
2204 output[0] = '\0';
2205 log_msg(4, "It's not %s", dev);
2206 res = FALSE;
2207 }
2208 mr_free(command);
2209 return(res);
2210}
2211
2212
2213
2214
2215
2216/**
2217 * Find out what number CD is in the drive.
2218 * @param bkpinfo The backup information structure. The @c bkpinfo->media_device field is the only one used.
2219 * @return The current CD number, or -1 if it could not be found.
2220 * @note If the CD is not mounted, it will be mounted
2221 * (and remain mounted after this function returns).
2222 */
2223int what_number_cd_is_this()
2224{
2225 int cd_number = -1;
2226 char *mountdev = NULL;
2227 char *tmp = NULL;
2228
2229 assert(bkpinfo != NULL);
2230// log_it("Asking what_number_cd_is_this");
2231 if (g_ISO_restore_mode) {
2232 mr_asprintf(tmp, "mount | grep iso9660 | awk '{print $3;}'");
2233
2234 mr_asprintf(mountdev, "%s%s", call_program_and_get_last_line_of_output(tmp), "/archives/THIS-CD-NUMBER");
2235 cd_number = atoi(last_line_of_file(mountdev));
2236 paranoid_free(mountdev);
2237 paranoid_free(tmp);
2238
2239 return (cd_number);
2240 }
2241
2242 mr_asprintf(mountdev, "%s", bkpinfo->media_device);
2243 if (!mountdev[0]) {
2244 log_it
2245 ("(what_number_cd_is_this) Warning - media_device unknown. Finding out...");
2246 find_cdrom_device(bkpinfo->media_device, FALSE);
2247 }
2248 if (!is_this_device_mounted(MNT_CDROM)) {
2249 if (bkpinfo->backup_media_type == usb) {
2250 mount_USB_here(mountdev, MNT_CDROM);
2251 } else {
2252 mount_CDROM_here(mountdev, MNT_CDROM);
2253 }
2254 }
2255 paranoid_free(mountdev);
2256
2257 cd_number = atoi(last_line_of_file(MNT_CDROM "/archives/THIS-CD-NUMBER"));
2258 return (cd_number);
2259}
2260
2261
2262
2263
2264
2265
2266
2267/**
2268 * Find out what device is mounted as root (/).
2269 * @return Root device.
2270 * @note The returned string points to static storage and will be overwritten with every call.
2271 * @bug A bit of a misnomer; it's actually finding out the root device.
2272 * The mountpoint (where it's mounted) will obviously be '/'.
2273 */
2274char *where_is_root_mounted()
2275{
2276 /*@ buffers **************** */
2277 static char tmp[MAX_STR_LEN];
2278
2279
2280#ifdef __FreeBSD__
2281 strcpy(tmp, call_program_and_get_last_line_of_output
2282 ("mount | grep \" on / \" | cut -d' ' -f1"));
2283#else
2284 strcpy(tmp, call_program_and_get_last_line_of_output
2285 ("mount | grep \" on / \" | cut -d' ' -f1 | sed s/[0-9]// | sed s/[0-9]//"));
2286 if (strstr(tmp, "/dev/cciss/")) {
2287 strcpy(tmp, call_program_and_get_last_line_of_output
2288 ("mount | grep \" on / \" | cut -d' ' -f1 | cut -dp -f1"));
2289 }
2290 if (strstr(tmp, "/dev/md")) {
2291 strcpy(tmp,
2292 call_program_and_get_last_line_of_output
2293 ("mount | grep \" on / \" | cut -d' ' -f1"));
2294 }
2295#endif
2296
2297 return (tmp);
2298}
2299
2300
2301/**
2302 * Find out which boot loader is in use.
2303 * @param which_device Device to look for the boot loader on.
2304 * @return 'L' for LILO, 'E'for ELILO, 'G' for GRUB, 'B' or 'D' for FreeBSD boot loaders, or 'U' for Unknown.
2305 * @note Under Linux, all drives are examined, not just @p which_device.
2306 */
2307#ifdef __FreeBSD__
2308char which_boot_loader(char *which_device)
2309{
2310 int count_lilos = 0;
2311 int count_grubs = 0;
2312 int count_boot0s = 0;
2313 int count_dangerouslydedicated = 0;
2314
2315 log_it("looking at drive %s's MBR", which_device);
2316 if (does_string_exist_in_boot_block(which_device, "GRUB")) {
2317 count_grubs++;
2318 }
2319 if (does_string_exist_in_boot_block(which_device, "LILO")) {
2320 count_lilos++;
2321 }
2322 if (does_string_exist_in_boot_block(which_device, "Drive")) {
2323 count_boot0s++;
2324 }
2325 if (does_string_exist_in_first_N_blocks
2326 (which_device, "FreeBSD/i386", 17)) {
2327 count_dangerouslydedicated++;
2328 }
2329 log_it("%d grubs and %d lilos and %d elilos and %d boot0s and %d DD\n",
2330 count_grubs, count_lilos, count_elilos, count_boot0s,
2331 count_dangerouslydedicated);
2332
2333 if (count_grubs && !count_lilos) {
2334 return ('G');
2335 } else if (count_lilos && !count_grubs) {
2336 return ('L');
2337 } else if (count_grubs == 1 && count_lilos == 1) {
2338 log_it("I'll bet you used to use LILO but switched to GRUB...");
2339 return ('G');
2340 } else if (count_boot0s == 1) {
2341 return ('B');
2342 } else if (count_dangerouslydedicated) {
2343 return ('D');
2344 } else {
2345 log_it("Unknown boot loader");
2346 return ('U');
2347 }
2348}
2349
2350#else
2351
2352char which_boot_loader(char *which_device)
2353{
2354 /*@ buffer ***************************************************** */
2355 char *list_drives_cmd = NULL;
2356 char *current_drive;
2357
2358 /*@ pointers *************************************************** */
2359 FILE *pdrives;
2360
2361 /*@ int ******************************************************** */
2362 int count_lilos = 0;
2363 int count_grubs = 0;
2364
2365 /*@ end vars *************************************************** */
2366
2367 malloc_string(current_drive);
2368
2369#ifdef __IA64__
2370 /* No choice for it */
2371 return ('E');
2372#endif
2373 assert(which_device != NULL);
2374
2375 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());
2376 log_it("list_drives_cmd = %s", list_drives_cmd);
2377
2378 if (!(pdrives = popen(list_drives_cmd, "r"))) {
2379 log_OS_error("Unable to open list of drives");
2380 mr_free(list_drives_cmd);
2381 paranoid_free(current_drive);
2382 return ('\0');
2383 }
2384 mr_free(list_drives_cmd);
2385
2386 for ((void)fgets(current_drive, MAX_STR_LEN, pdrives); !feof(pdrives);
2387 (void)fgets(current_drive, MAX_STR_LEN, pdrives)) {
2388 strip_spaces(current_drive);
2389 log_it("looking at drive %s's MBR", current_drive);
2390 if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
2391 count_grubs++;
2392 strcpy(which_device, current_drive);
2393 break;
2394 }
2395 if (does_string_exist_in_boot_block(current_drive, "LILO")) {
2396 count_lilos++;
2397 strcpy(which_device, current_drive);
2398 break;
2399 }
2400 }
2401 if (pclose(pdrives)) {
2402 log_OS_error("Cannot pclose pdrives");
2403 }
2404 log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
2405 if (count_grubs && !count_lilos) {
2406 paranoid_free(current_drive);
2407 return ('G');
2408 } else if (count_lilos && !count_grubs) {
2409 paranoid_free(current_drive);
2410 return ('L');
2411 } else if (count_grubs == 1 && count_lilos == 1) {
2412 log_it("I'll bet you used to use LILO but switched to GRUB...");
2413 paranoid_free(current_drive);
2414 return ('G');
2415 } else {
2416 // We need to look on each partition then
2417 mr_asprintf(list_drives_cmd, "parted2fdisk -l 2>/dev/null | grep -E \"^/dev/\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/");
2418 log_it("list_drives_cmd = %s", list_drives_cmd);
2419
2420 if (!(pdrives = popen(list_drives_cmd, "r"))) {
2421 log_OS_error("Unable to open list of drives");
2422 mr_free(list_drives_cmd);
2423 paranoid_free(current_drive);
2424 return ('\0');
2425 }
2426 mr_free(list_drives_cmd);
2427
2428 for ((void)fgets(current_drive, MAX_STR_LEN, pdrives); !feof(pdrives);
2429 (void)fgets(current_drive, MAX_STR_LEN, pdrives)) {
2430 strip_spaces(current_drive);
2431 log_it("looking at partition %s's BR", current_drive);
2432 if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
2433 count_grubs++;
2434 strcpy(which_device, current_drive);
2435 break;
2436 }
2437 if (does_string_exist_in_boot_block(current_drive, "LILO")) {
2438 count_lilos++;
2439 strcpy(which_device, current_drive);
2440 break;
2441 }
2442 }
2443 if (pclose(pdrives)) {
2444 log_OS_error("Cannot pclose pdrives");
2445 }
2446 log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
2447 paranoid_free(current_drive);
2448 if (count_grubs && !count_lilos) {
2449 return ('G');
2450 } else if (count_lilos && !count_grubs) {
2451 return ('L');
2452 } else if (count_grubs == 1 && count_lilos == 1) {
2453 log_it("I'll bet you used to use LILO but switched to GRUB...");
2454 return ('G');
2455 } else {
2456 log_it("Unknown boot loader");
2457 return ('U');
2458 }
2459 }
2460}
2461#endif
2462
2463
2464
2465
2466/**
2467 * Write zeroes over the first 16K of @p device.
2468 * @param device The device to zero.
2469 * @return 0 for success, 1 for failure.
2470 */
2471int zero_out_a_device(char *device)
2472{
2473 FILE *fout;
2474 int i;
2475
2476 assert_string_is_neither_NULL_nor_zerolength(device);
2477
2478 log_it("Zeroing drive %s", device);
2479 if (!(fout = fopen(device, "w"))) {
2480 log_OS_error("Unable to open/write to device");
2481 return (1);
2482 }
2483 for (i = 0; i < 16384; i++) {
2484 fputc('\0', fout);
2485 }
2486 paranoid_fclose(fout);
2487 log_it("Device successfully zeroed.");
2488 return (0);
2489}
2490
2491/**
2492 * Return the device pointed to by @p incoming.
2493 * @param incoming The device to resolve symlinks for.
2494 * @return The path to the real device file.
2495 * @note The returned string points to static storage that will be overwritten with each call.
2496 * @bug Won't work with file v4.0; needs to be written in C.
2497 */
2498char *resolve_softlinks_to_get_to_actual_device_file(char *incoming)
2499{
2500 static char output[MAX_STR_LEN];
2501 char *command = NULL;
2502 char *curr_fname;
2503 char *scratch = NULL;
2504 char *tmp = NULL;
2505 char *p;
2506
2507 struct stat statbuf;
2508 malloc_string(curr_fname);
2509 if (!does_file_exist(incoming)) {
2510 log_it
2511 ("resolve_softlinks_to_get_to_actual_device_file --- device not found");
2512 strcpy(output, incoming);
2513 } else {
2514 strcpy(curr_fname, incoming);
2515 lstat(curr_fname, &statbuf);
2516 while (S_ISLNK(statbuf.st_mode)) {
2517 log_msg(1, "curr_fname = %s", curr_fname);
2518 mr_asprintf(command, "file %s", curr_fname);
2519 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
2520 mr_free(command);
2521 for (p = tmp + strlen(tmp); p != tmp && *p != '`' && *p != ' ';
2522 p--);
2523 p++;
2524 mr_asprintf(scratch, "%s", p);
2525 for (p = scratch; *p != '\0' && *p != '\''; p++);
2526 *p = '\0';
2527 log_msg(0, "curr_fname %s --> '%s' --> %s", curr_fname, tmp, scratch);
2528 mr_free(tmp);
2529
2530 if (scratch[0] == '/') {
2531 strcpy(curr_fname, scratch); // copy whole thing because it's an absolute softlink
2532 } else { // copy over the basename cos it's a relative softlink
2533 p = curr_fname + strlen(curr_fname);
2534 while (p != curr_fname && *p != '/') {
2535 p--;
2536 }
2537 if (*p == '/') {
2538 p++;
2539 }
2540 strcpy(p, scratch);
2541 }
2542 mr_free(scratch);
2543 lstat(curr_fname, &statbuf);
2544 }
2545 strcpy(output, curr_fname);
2546 log_it("resolved %s to %s", incoming, output);
2547 }
2548 paranoid_free(curr_fname);
2549 return (output);
2550}
2551
2552/* @} - end of deviceGroup */
2553
2554
2555/**
2556 * Return the type of partition format (GPT or MBR)
2557 */
2558char *which_partition_format(const char *drive)
2559{
2560 static char output[4];
2561 char *tmp = NULL;
2562 char *command = NULL;
2563 char *fdisk = NULL;
2564#ifdef __IA64__
2565 struct stat buf;
2566#endif
2567 log_msg(0, "Looking for partition table format type");
2568 mr_asprintf(fdisk, "/sbin/parted2fdisk");
2569 log_msg(1, "Using %s", fdisk);
2570 mr_asprintf(command, "%s -l %s | grep 'EFI GPT'", fdisk, drive);
2571 mr_free(fdisk);
2572
2573 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
2574 mr_free(command);
2575
2576 if (strstr(tmp, "GPT") == NULL) {
2577 strcpy(output, "MBR");
2578 } else {
2579 strcpy(output, "GPT");
2580 }
2581 mr_free(tmp);
2582
2583 log_msg(0, "Found %s partition table format type", output);
2584 return (output);
2585}
2586
2587/* @} - end of deviceGroup */
Note: See TracBrowser for help on using the repository browser.