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

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

r3329@localhost: bruno | 2009-08-05 00:33:17 +0200
bkpinfo->exclude_paths is now dynamically allocated

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