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

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

r3332@localhost: bruno | 2009-08-07 23:59:34 +0200

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