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

Last change on this file since 2349 was 2349, checked in by Bruno Cornec, 15 years ago
  • Solve tmp dir creation which sometimes was trying to create /%xx dirs. Should fix #293 for good
  • Adds fdisk -l result in Log File at restore time

(Backports from 2.2.9)

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