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

Last change on this file since 2400 was 2400, checked in by Bruno Cornec, 15 years ago
  • Use protocol name when displaying restore progress instead of netfs
  • Ask for network protocol name at restore time

(Backport from 2.2.9)

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