source: MondoRescue/branches/3.1/mondo/src/common/libmondo-devices.c@ 3161

Last change on this file since 3161 was 3161, checked in by Bruno Cornec, 11 years ago
  • Apply fixes to 3.1 tree so that at least mondo compiles again (but that branch probably doesn't work at all again).
  • Property svn:keywords set to Id
File size: 96.6 KB
Line 
1/* libmondo-devices.c Subroutines for handling devices
2 $Id: libmondo-devices.c 3161 2013-06-25 20:53:14Z 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 "mr_str.h"
13#include "mondostructures.h"
14#include "libmondo-files-EXT.h"
15#include "libmondo-devices.h"
16#include "lib-common-externs.h"
17#include "libmondo-string-EXT.h"
18#include "libmondo-tools-EXT.h"
19#include "libmondo-gui-EXT.h"
20#include "libmondo-fork-EXT.h"
21#include "libmondo-stream-EXT.h"
22
23extern void mr_strip_spaces(char *);
24
25#include <sys/types.h>
26#ifdef __FreeBSD__
27#define DKTYPENAMES
28#define FSTYPENAMES
29#include <sys/disklabel.h>
30#include <sys/disk.h>
31#elif linux
32#define u64 unsigned long long
33#include <linux/fs.h> /* for BLKGETSIZE64 */
34#include <linux/hdreg.h>
35#endif
36
37/*@unused@*/
38//static char cvsid[] = "$Id: libmondo-devices.c 3161 2013-06-25 20:53:14Z bruno $";
39//
40
41extern char *which_compression_type();
42/* Do we use extended attributes and acl ? */
43extern char *g_getfacl;
44extern char *g_getfattr;
45
46extern int g_current_media_number;
47extern double g_kernel_version;
48
49extern bool g_ISO_restore_mode;
50extern char *g_selfmounted_isodir;
51extern char *MONDO_LOGFILE;
52
53static char g_cdrw_drive_is_here[MAX_STR_LEN / 4] = "";
54static char g_cdrom_drive_is_here[MAX_STR_LEN / 4] = "";
55static char g_dvd_drive_is_here[MAX_STR_LEN / 4] = "";
56
57
58/**
59 * ????? @bug ?????
60 * @ingroup globalGroup
61 */
62extern t_bkptype g_backup_media_type; // set by main()
63
64/* Reference to global bkpinfo */
65extern struct s_bkpinfo *bkpinfo;
66
67/* Stuff that handles the -I and -E option when a whole disk DSF is used */
68typedef struct mounted_fs_struct {
69 char device[MAX_STR_LEN]; /* The name of the device */
70 char mount_point[MAX_STR_LEN]; /* The devices mount point */
71 unsigned char check; /* 1 == included on DSF */
72 struct mounted_fs_struct *next;
73} MOUNTED_FS_STRUCT;
74
75static MOUNTED_FS_STRUCT *DSF_Head = NULL; /* Points to the first entry of mounted_fs_struct list */
76static MOUNTED_FS_STRUCT *DSF_Tail = NULL; /* Points to the last entry of mounted_fs_struct list */
77
78
79void set_g_cdrom_and_g_dvd_to_bkpinfo_value() {
80
81 if (bkpinfo->media_device) {
82 strcpy(g_cdrom_drive_is_here, bkpinfo->media_device); // just in case
83 strcpy(g_dvd_drive_is_here, bkpinfo->media_device); // just in case
84 }
85}
86
87
88
89/**
90 * Retract all CD trays and wait for autorun to complete.
91 * @ingroup deviceGroup
92 */
93void retract_CD_tray_and_defeat_autorun(void)
94{
95// log_it("rctada: Retracting all CD trays", __LINE__);
96 if (!bkpinfo->please_dont_eject) {
97 if (strlen(g_cdrom_drive_is_here) > 0) {
98 inject_device(g_cdrom_drive_is_here);
99 }
100 if (strlen(g_dvd_drive_is_here) > 0) {
101 inject_device(g_dvd_drive_is_here);
102 }
103 if (strlen(g_cdrw_drive_is_here) > 0) {
104 inject_device(g_cdrw_drive_is_here);
105 }
106 }
107// log_it("rctada: killing autorun");
108// run_program_and_log_output("killall autorun", TRUE);
109 if (!run_program_and_log_output("ps | grep autorun | grep -v grep", 5)) {
110 log_it("autorun detected; sleeping for 2 seconds");
111 sleep(2);
112 }
113 log_it("rctada: Unmounting all CD drives", __LINE__);
114 run_program_and_log_output("umount /dev/cdr* /dev/dvd*", 5);
115}
116
117
118
119/**
120 * Determine whether we're booted off a ramdisk.
121 * @return @c TRUE (we are) or @c FALSE (we aren't).
122 * @ingroup utilityGroup
123 */
124bool am_I_in_disaster_recovery_mode(void)
125{
126 char *tmp = NULL;
127 bool is_this_a_ramdisk = FALSE;
128
129 tmp = where_is_root_mounted();
130 log_msg(0, "root is currently mounted at %s", tmp);
131
132#ifdef __FreeBSD__
133 if (strstr(tmp, "/dev/md")) {
134 is_this_a_ramdisk = TRUE;
135 }
136#else
137 if (!strncmp(tmp, "/dev/ram", 8)
138 || (!strncmp(tmp, "/dev/rd", 7) && !strcmp(tmp, "/dev/rd/")
139 && strncmp(tmp, "/dev/rd/cd", 10)) || strstr(tmp, "rootfs")
140 || !strcmp(tmp, "/dev/root")) {
141 is_this_a_ramdisk = TRUE;
142 } else {
143 is_this_a_ramdisk = FALSE;
144 }
145#endif
146 mr_free(tmp);
147
148 log_msg(1, "Is this a ramdisk? result = %s", (is_this_a_ramdisk) ? "TRUE" : "FALSE");
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 char *output;
165 switch (bt) {
166 case none:
167 mr_asprintf(output, "none");
168 break;
169 case iso:
170 mr_asprintf(output, "iso");
171 break;
172 case cdr:
173 mr_asprintf(output, "cdr");
174 break;
175 case cdrw:
176 mr_asprintf(output, "cdrw");
177 break;
178 case cdstream:
179 mr_asprintf(output, "cdstream");
180 break;
181 case netfs:
182 mr_asprintf(output, "netfs");
183 break;
184 case tape:
185 mr_asprintf(output, "tape");
186 break;
187 case udev:
188 mr_asprintf(output, "udev");
189 break;
190 case usb:
191 mr_asprintf(output, "usb");
192 break;
193 default:
194 mr_asprintf(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 (dev == NULL) {
216 return (1);
217 }
218
219 if (IS_THIS_A_STREAMING_BACKUP(g_backup_media_type)
220 && g_backup_media_type != udev) {
221 mr_asprintf(command, "mt -f %s offline", dev);
222 res1 = run_program_and_log_output(command, 1);
223 mr_free(command);
224 } else {
225 res1 = 0;
226 }
227
228#ifdef __FreeBSD__
229 if (strstr(dev, "acd")) {
230 mr_asprintf(command, "cdcontrol -f %s eject", dev);
231 } else {
232 mr_asprintf(command, "camcontrol eject `echo %s | sed 's|/dev/||'`", dev);
233 }
234#else
235 mr_asprintf(command, "eject %s", dev);
236#endif
237
238 log_msg(3, "Ejecting %s", dev);
239 res2 = run_program_and_log_output(command, 1);
240 mr_free(command);
241 if (res1 && res2) {
242 return (1);
243 } else {
244 return (0);
245 }
246}
247
248/**
249 * Load (inject) the tray of the specified CD device.
250 * @param dev The device to load/inject.
251 * @return 0 for success, nonzero for failure.
252 */
253int inject_device(char *dev)
254{
255 char *command = NULL;
256 int i;
257
258 if (dev == NULL) {
259 return (1);
260 }
261
262#ifdef __FreeBSD__
263 if (strstr(dev, "acd")) {
264 mr_asprintf(command, "cdcontrol -f %s close", dev);
265 } else {
266 mr_asprintf(command, "camcontrol load `echo %s | sed 's|/dev/||'`", dev);
267 }
268#else
269 mr_asprintf(command, "eject -t %s", dev);
270#endif
271 i = run_program_and_log_output(command, FALSE);
272 mr_free(command);
273 return (i);
274}
275
276
277/**
278 * Determine whether the specified @p device (really, you can use any file)
279 * exists.
280 * @return TRUE if it exists, FALSE if it doesn't.
281 */
282bool does_device_exist(char *device)
283{
284
285 /*@ buffers *********************************************************** */
286 char *tmp = NULL;
287 bool ret;
288
289 assert_string_is_neither_NULL_nor_zerolength(device);
290
291 mr_asprintf(tmp, "ls %s > /dev/null 2> /dev/null", device);
292
293 if (system(tmp)) {
294 ret = FALSE;
295 } else {
296 ret = TRUE;
297 }
298 mr_free(tmp);
299 return (ret);
300}
301
302
303/**
304 * Determine whether a non-Microsoft partition exists on any connected hard drive.
305 * @return TRUE (there's a Linux/FreeBSD partition) or FALSE (Microsoft has taken over yet another innocent PC).
306 */
307bool does_nonMS_partition_exist(void)
308{
309#if __FreeBSD__
310 return
311 !system
312 ("for drive in /dev/ad? /dev/da?; do fdisk $drive | grep -q FreeBSD && exit 0; done; false");
313#else
314 return
315 !system
316 ("parted2fdisk -l 2>/dev/null | grep '^/dev/' | grep -Eqv '(MS|DOS|FAT|NTFS)'");
317#endif
318}
319
320/**
321 * Determine whether the specified @p partno exists on the specified @p drive.
322 * @param drive The drive to search for the partition in.
323 * @param partno The partition number to look for.
324 * @return 0 if it exists, nonzero otherwise.
325 */
326int does_partition_exist(const char *drive, int partno)
327{
328 /*@ buffers **************************************************** */
329 char *program = NULL;
330 char *incoming = NULL;
331 char *searchstr = NULL;
332
333 /*@ ints ******************************************************* */
334 int res = 0;
335
336 /*@ pointers *************************************************** */
337 FILE *fin;
338
339 /*@ end vars *************************************************** */
340 assert_string_is_neither_NULL_nor_zerolength(drive);
341 assert(partno >= 0 && partno < 999);
342
343#ifdef __FreeBSD__
344 // We assume here that this is running from mondorestore. (It is.)
345 tmp = build_partition_name(drive, partno);
346 mr_asprintf(program, "ls %s %s >/dev/null 2>&1", drive, tmp);
347 mr_free(tmp);
348
349 res = system(program);
350 mr_free(program);
351 return (res);
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, mr_getline(incoming, fin); !res && !feof(fin) ; mr_getline(incoming, fin)) {
367 if (strstr(incoming, searchstr)) {
368 res = 1;
369 }
370 mr_free(incoming);
371 }
372 mr_free(incoming);
373 mr_free(searchstr);
374
375 if (pclose(fin)) {
376 log_OS_error("Cannot pclose fin");
377 }
378 return (res);
379}
380
381
382
383
384
385/**
386 * Determine whether given NULL-terminated @p str exists in the MBR of @p dev.
387 * @param dev The device to look in.
388 * @param str The string to look for.
389 * @return TRUE if it exists, FALSE if it doesn't.
390 */
391bool does_string_exist_in_boot_block(char *dev, char *str)
392{
393 /*@ buffers **************************************************** */
394 char *command = NULL;
395
396 /*@ end vars *************************************************** */
397 int i;
398
399 assert_string_is_neither_NULL_nor_zerolength(dev);
400 assert_string_is_neither_NULL_nor_zerolength(str);
401
402 mr_asprintf(command, "dd if=%s bs=446 count=1 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null", dev, str);
403 i = system(command);
404 mr_free(command);
405 if (i) {
406 return (FALSE);
407 } else {
408 return (TRUE);
409 }
410}
411
412/**
413 * Determine whether specified @p str exists in the first @p n sectors of
414 * @p dev.
415 * @param dev The device to look in.
416 * @param str The string to look for.
417 * @param n The number of 512-byte sectors to search.
418 */
419bool does_string_exist_in_first_N_blocks(char *dev, char *str, int n)
420{
421 /*@ buffers **************************************************** */
422 char *command = NULL;
423 /*@ end vars *************************************************** */
424 int i;
425
426 mr_asprintf(command, "dd if=%s bs=512 count=%i 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null", dev, n, str);
427 i = system(command);
428 mr_free(command);
429 if (i) {
430 return (FALSE);
431 } else {
432 return (TRUE);
433 }
434}
435
436
437
438/**
439 * Try to mount CD-ROM at @p mountpoint. If the CD-ROM is not found or has
440 * not been specified, call find_cdrom_device() to find it.
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 = NULL;
452 char *p = NULL;
453
454 /*@ end vars **************************************************** */
455
456 assert(bkpinfo != NULL);
457 assert_string_is_neither_NULL_nor_zerolength(mountpoint);
458
459 if (g_backup_media_type == dvd) {
460 if (g_dvd_drive_is_here[0]) {
461 mr_asprintf(dev, "%s", g_dvd_drive_is_here);
462 }
463 if (dev == NULL) {
464 dev = find_dvd_device();
465 }
466 } else {
467 if (g_cdrom_drive_is_here[0]) {
468 mr_asprintf(dev, "%s", g_cdrom_drive_is_here);
469 }
470 if (dev == NULL) {
471 dev = find_cdrom_device(FALSE);
472 }
473 }
474
475 if (bkpinfo->backup_media_type != iso) {
476 retract_CD_tray_and_defeat_autorun();
477 }
478
479 if ((dev == NULL) || (res = mount_CDROM_here(dev, mountpoint))) {
480 p = popup_and_get_string ("CD-ROM device", "Please enter your CD-ROM's /dev device",dev);
481 if (p == NULL) {
482 res = 1;
483 } else {
484 res = mount_CDROM_here(p, mountpoint);
485 }
486 mr_free(p);
487 }
488 if (res) {
489 log_msg(1, "mount failed");
490 } else {
491 log_msg(1, "mount succeeded with %s", dev);
492 }
493 mr_free(dev);
494 return (res);
495}
496
497
498
499
500
501
502/**
503 * Locate a CD-R/W writer's SCSI node.
504 * @param cdrw_device SCSI node will be placed here.
505 * @return 0 for success, nonzero for failure.
506 */
507char *find_cdrw_device()
508{
509 /*@ buffers ************************ */
510 char *tmp = NULL;
511 char *tmp1 = NULL;
512 char *cdr_exe = NULL;
513 char *command = NULL;
514 char *cdrw_device = NULL;
515
516 if (g_cdrw_drive_is_here[0]) {
517 mr_asprintf(cdrw_device, "%s", g_cdrw_drive_is_here);
518 log_msg(3, "Been there, done that. Returning %s", cdrw_device);
519 return(cdrw_device);
520 }
521 if (g_backup_media_type == dvd) {
522 log_msg(1, "This is dumb. You're calling find_cdrw_device() but you're backing up to DVD. WTF?");
523 return(NULL);
524 }
525 run_program_and_log_output("insmod ide-scsi", -1);
526 tmp = find_home_of_exe("cdrecord");
527 if (tmp) {
528 mr_asprintf(cdr_exe, "cdrecord");
529 } else {
530 mr_asprintf(cdr_exe, "dvdrecord");
531 }
532 mr_free(tmp);
533
534 tmp1 = find_home_of_exe(cdr_exe);
535 if (tmp1) {
536 mr_asprintf(command, "%s -scanbus 2> /dev/null | tr -s '\t' ' ' | grep \"[0-9]*,[0-9]*,[0-9]*\" | grep -v \"[0-9]*) \\*\" | grep -E '[D|C][V|D]' | cut -d' ' -f2 | head -n1", cdr_exe);
537 tmp = call_program_and_get_last_line_of_output(command,TRUE);
538 mr_free(command);
539 }
540 mr_free(tmp1);
541
542 if ((tmp == NULL) || (strlen(tmp) < 2)) {
543 mr_free(tmp);
544 mr_free(cdr_exe);
545 return(NULL);
546 } else {
547 cdrw_device = tmp;
548 log_it("Found CDRW device - %s", cdrw_device);
549 strcpy(g_cdrw_drive_is_here, cdrw_device);
550 mr_free(cdr_exe);
551 return (cdrw_device);
552 }
553}
554
555
556/**
557 * Attempt to locate a CD-ROM device's /dev entry.
558 * Several different methods may be used to find the device, including
559 * calling @c cdrecord, searching @c dmesg, and trial-and-error.
560 * @param output Where to put the located /dev entry.
561 * @param try_to_mount Whether to mount the CD as part of the test; if mount
562 * fails then return failure.
563 * @return 0 for success, nonzero for failure.
564 */
565char *find_cdrom_device(bool try_to_mount)
566{
567 /*@ pointers **************************************************** */
568 FILE *fin;
569 char *p;
570 char *q;
571 char *r;
572
573 /*@ bool's ****************************************************** */
574 bool found_it = FALSE;
575
576 /*@ buffers ***************************************************** */
577 char *tmp = NULL;
578 char *output = NULL;
579 char *cdr_exe = NULL;
580 char *phrase_two = NULL;
581 char *command = NULL;
582#ifndef __FreeBSD__
583 char *dvd_last_resort = NULL;
584#endif
585 char *mountpoint = NULL;
586 static char the_last_place_i_found_it[MAX_STR_LEN] = "";
587
588 /*@ intialize *************************************************** */
589
590 output = NULL;
591#ifndef __FreeBSD__
592 mr_asprintf(dvd_last_resort, "%s", "");;
593#endif
594
595 /*@ end vars **************************************************** */
596
597 if (g_cdrom_drive_is_here[0] && !isdigit(g_cdrom_drive_is_here[0])) {
598 mr_asprintf(output, "%s", g_cdrom_drive_is_here);
599 log_msg(3, "Been there, done that. Returning %s", output);
600 return(output);
601 }
602 if (the_last_place_i_found_it[0] != '\0' && !try_to_mount) {
603 mr_asprintf(output, "%s", the_last_place_i_found_it);
604 log_msg(3, "find_cdrom_device() --- returning last found location - '%s'", output);
605 return(output);
606 }
607
608 mr_asprintf(mountpoint, "%s/cd.mnt", bkpinfo->tmpdir);
609 make_hole_for_dir(mountpoint);
610
611 tmp = find_home_of_exe("cdrecord");
612 if (tmp) {
613 mr_asprintf(cdr_exe, "cdrecord");
614 } else {
615 mr_asprintf(cdr_exe, "dvdrecord");
616 }
617 mr_free(tmp);
618
619 tmp = find_home_of_exe(cdr_exe);
620 if (!tmp) {
621 mr_asprintf(output, "/dev/cdrom");
622 log_msg(4, "Can't find cdrecord; assuming %s", output);
623 mr_free(cdr_exe);
624 if (!does_device_exist(output)) {
625 log_msg(4, "That didn't work. Sorry.");
626 mr_free(output);
627 }
628 mr_free(tmp);
629 return(output);
630 }
631 mr_free(tmp);
632
633 mr_asprintf(command, "%s -scanbus 2> /dev/null", cdr_exe);
634 fin = popen(command, "r");
635 log_msg(4, "command=%s", command);
636 mr_free(command);
637
638 if (!fin) {
639 log_OS_error("Cannot popen command");
640 mr_free(cdr_exe);
641 return(NULL);
642 }
643
644 for (mr_getline(tmp, fin); !feof(fin); mr_getline(tmp, fin)) {
645 p = strchr(tmp, '\'');
646 if (p) {
647 q = strchr(++p, '\'');
648 if (q) {
649 for (r = q; *(r - 1) == ' '; r--);
650 *r = '\0';
651 p = strchr(++q, '\'');
652 if (p) {
653 q = strchr(++p, '\'');
654 if (q) {
655 while (*(q - 1) == ' ') {
656 q--;
657 }
658 *q = '\0';
659 mr_asprintf(phrase_two, "%s", p);
660 }
661 }
662 }
663 }
664 mr_free(tmp);
665 }
666 mr_free(tmp);
667 paranoid_pclose(fin);
668
669#ifndef __FreeBSD__
670 if (!phrase_two || strlen(phrase_two) == 0) {
671 log_msg(4, "Not running phase two. String is empty.");
672 } else {
673 mr_asprintf(command, "dmesg | grep \"%s\" 2> /dev/null", phrase_two);
674 fin = popen(command, "r");
675 mr_free(command);
676
677 if (!fin) {
678 log_msg(4, "Cannot run 2nd command - non-fatal, fortunately");
679 } else {
680 for (mr_getline(tmp, fin); !feof(fin); mr_getline(tmp, fin)) {
681 log_msg(5, "--> '%s'", tmp);
682 if (tmp[0] != ' ' && tmp[1] != ' ') {
683 p = strchr(tmp, ':');
684 if (p) {
685 *p = '\0';
686 if (strstr(tmp, "DVD")) {
687 mr_free(dvd_last_resort);
688 mr_asprintf(dvd_last_resort, "/dev/%s", tmp);
689 log_msg(4, "Ignoring '%s' because it's a DVD drive", tmp);
690 } else {
691 mr_asprintf(output, "/dev/%s", tmp);
692 found_it = TRUE;
693 }
694 }
695 }
696 mr_free(tmp);
697 }
698 paranoid_pclose(fin);
699 mr_free(tmp);
700 }
701 }
702 mr_free(phrase_two);
703
704#endif
705#ifdef __FreeBSD__
706 if (!found_it) {
707 log_msg(4, "OK, approach 2");
708 if (!(output = set_dev_to_this_if_rx_OK("/dev/cdrom"))) {
709 if (!(output = set_dev_to_this_if_rx_OK("/dev/cdrom1"))) {
710 if (!(output = set_dev_to_this_if_rx_OK("/dev/dvd"))) {
711 if (!(output = set_dev_to_this_if_rx_OK("/dev/acd0"))) {
712 if (!(output = set_dev_to_this_if_rx_OK("/dev/cd01"))) {
713 if (!(output = set_dev_to_this_if_rx_OK("/dev/acd1"))) {
714 if (!(output = set_dev_to_this_if_rx_OK("/dev/cd1"))) {
715 mr_free(cdr_exe);
716 return(NULL);
717 }
718 }
719 }
720 }
721 }
722 }
723 }
724 }
725#else
726 if (!found_it && strlen(dvd_last_resort) > 0) {
727 log_msg(4, "Well, I'll use the DVD - %s - as a last resort", dvd_last_resort);
728 mr_asprintf(output, "%s", dvd_last_resort);
729 found_it = TRUE;
730 }
731 mr_free(dvd_last_resort);
732
733 if (found_it) {
734 mr_asprintf(tmp, "grep \"%s=ide-scsi\" " CMDLINE " &> /dev/null", strrchr(output, '/') + 1);
735 if (system(tmp) == 0) {
736 log_msg(4, "%s is not right. It's being SCSI-emulated. Continuing.", output);
737 found_it = FALSE;
738 mr_free(output);
739 }
740 mr_free(tmp);
741 }
742
743 if (found_it) {
744 log_msg(4, "(find_cdrom_device) --> '%s'", output);
745 if (!does_device_exist(output)) {
746 found_it = FALSE;
747 log_msg(4, "OK, I was wrong, I haven't found it... yet.");
748 mr_free(output);
749 }
750 }
751
752 if (!found_it) {
753 log_msg(4, "OK, approach 2");
754 if (!(output = set_dev_to_this_if_rx_OK("/dev/scd0"))) {
755 if (!(output = set_dev_to_this_if_rx_OK("/dev/sr0"))) {
756 if (!(output = set_dev_to_this_if_rx_OK("/dev/cdrom"))) {
757 if (!(output = set_dev_to_this_if_rx_OK("/dev/cdrom0"))) {
758 if (!(output = set_dev_to_this_if_rx_OK("/dev/cdrom1"))) {
759 if (!(output = set_dev_to_this_if_rx_OK("/dev/sr1"))) {
760 if (!(output = set_dev_to_this_if_rx_OK("/dev/dvd"))) {
761 if (!(output = set_dev_to_this_if_rx_OK(g_cdrw_drive_is_here))) {
762 mr_free(cdr_exe);
763 return(NULL);
764 }
765 }
766 }
767 }
768 }
769 }
770 }
771 }
772 }
773#endif
774
775 if (found_it && try_to_mount) {
776 if (mount_CDROM_here(output, mountpoint)) {
777 log_msg(4, "[Cardigans] I've changed my mind");
778 found_it = FALSE;
779 mr_free(output);
780 } else {
781 mr_asprintf(tmp, "%s/archives", mountpoint);
782 if (!does_file_exist(tmp)) {
783 log_msg(4, "[Cardigans] I'll take it back");
784 found_it = FALSE;
785 mr_free(output);
786 } else {
787 mr_asprintf(command, "umount %s", output);
788 paranoid_system(command);
789 mr_free(command);
790
791 log_msg(4, "I'm confident the Mondo CD is in %s", output);
792 }
793 mr_free(tmp);
794 }
795 }
796 unlink(mountpoint);
797 mr_free(mountpoint);
798
799 if (found_it) {
800 if (!does_file_exist(output)) {
801 log_msg(3, "I still haven't found it.");
802 mr_free(cdr_exe);
803 mr_free(output);
804 return (NULL);
805 }
806 log_msg(3, "(find_cdrom_device) --> '%s'", output);
807 strcpy(the_last_place_i_found_it, output);
808 strcpy(g_cdrom_drive_is_here, output);
809 mr_free(cdr_exe);
810 return (output);
811 }
812
813 mr_asprintf(command, "%s -scanbus | grep \"[0-9],[0-9],[0-9]\" | grep -E \"[D|C][V|D]\" | grep -n \"\" | grep \"%s\" | cut -d':' -f2", cdr_exe, g_cdrw_drive_is_here);
814 mr_free(cdr_exe);
815
816 log_msg(1, "command=%s", command);
817 tmp = call_program_and_get_last_line_of_output(command,TRUE);
818 mr_free(command);
819
820 mr_free(output);
821 if (strlen(tmp) > 0) {
822 mr_asprintf(output, "%s", tmp);
823 log_msg(4, "Finally found it at %s", output);
824 } else {
825 log_msg(4, "Still couldn't find it.");
826 }
827 mr_free(tmp);
828
829 return (output);
830}
831
832
833char *find_dvd_device()
834{
835 char *tmp = NULL;
836 int devno = -1;
837 char *output = NULL;
838
839 if (g_dvd_drive_is_here[0]) {
840 mr_asprintf(output, "%s", g_dvd_drive_is_here);
841 log_msg(3, "Been there, done that. Returning %s", output);
842 return (output);
843 }
844
845 tmp = call_program_and_get_last_line_of_output("dvdrecord -scanbus 2> /dev/null | grep -E '\)\ \'' | grep -n '' | grep -E '[D|C][V|D]' | cut -d':' -f1",TRUE);
846 log_msg(5, "tmp = '%s'", tmp);
847 if (!tmp[0])
848 mr_free(tmp);
849 tmp = call_program_and_get_last_line_of_output("cdrecord -scanbus 2> /dev/null | grep \)\ \' | grep -n '' | grep -E '[D|C][V|D]' | cut -d':' -f1",TRUE);
850 if (tmp[0]) {
851 devno = atoi(tmp) - 1;
852 }
853 mr_free(tmp);
854
855 if (devno >= 0) {
856 mr_asprintf(output, "/dev/scd%d", devno);
857 strcpy(g_dvd_drive_is_here, output);
858 log_msg(2, "I think DVD is at %s", output);
859 } else {
860 log_msg(2, "I cannot find DVD");
861 }
862
863 return (output);
864}
865
866
867
868
869
870#include <sys/ioctl.h>
871
872/**
873 * Find the size of the specified @p drive, in megabytes. Uses @c ioctl calls
874 * and @c dmesg.
875 * @param drive The device to find the size of.
876 * @return size in megabytes.
877 */
878long get_phys_size_of_drive(char *drive)
879{
880 int fd;
881#if linux
882 unsigned long long s = 0;
883 int fileid, cylinders = 0;
884 int cylindersize = 0;
885 int gotgeo = 0;
886
887
888 struct hd_geometry hdgeo;
889#elif __FreeBSD__
890 off_t s;
891#endif
892
893 long outvalA = -1;
894 long outvalB = -1;
895 long outvalC = -1;
896
897 if ((fd = open(drive, O_RDONLY)) != -1) {
898 if (ioctl(fd,
899#if linux
900#ifdef BLKGETSIZE64
901 BLKGETSIZE64,
902#else
903 BLKGETSIZE,
904#endif
905#elif __FreeBSD__
906 DIOCGMEDIASIZE,
907#endif
908 &s) != -1) {
909 close(fd);
910 // s>>11 works for older disks but not for newer ones
911 outvalB =
912#if linux
913#ifdef BLKGETSIZE64
914 s >> 20
915#else
916 s >> 11
917#endif
918#else
919 s >> 20
920#endif
921 ;
922 }
923 }
924
925 if (outvalB <= 0) {
926 log_msg(1, "Error getting size of %s: %s", drive, strerror(errno));
927#if linux
928 fileid = open(drive, O_RDONLY);
929 if (fileid != -1) {
930 if (ioctl(fileid, HDIO_GETGEO, &hdgeo) != -1) {
931 if (hdgeo.cylinders && hdgeo.heads && hdgeo.sectors) {
932 cylindersize = hdgeo.heads * hdgeo.sectors / 2;
933 outvalA = cylindersize * cylinders / 1024;
934 log_msg(2, "Got Harddisk geometry, C:%d, H:%d, S:%d",
935 hdgeo.cylinders, hdgeo.heads, hdgeo.sectors);
936 gotgeo = 1;
937 } else {
938 log_msg(1, "Harddisk geometry wrong");
939 }
940 } else {
941 log_msg(1,
942 "Error in ioctl() getting new hard disk geometry (%s), resizing in unsafe mode",
943 strerror(errno));
944 }
945 close(fileid);
946 } else {
947 log_msg(1, "Failed to open %s for reading: %s", drive,
948 strerror(errno));
949 }
950 if (!gotgeo) {
951 log_msg(1, "Failed to get harddisk geometry, using old mode");
952 }
953#endif
954 }
955// OLDER DISKS will give ridiculously low value for outvalB (so outvalA is returned) :)
956// NEWER DISKS will give sane value for outvalB (close to outvalA, in other words) :)
957
958 outvalC = (outvalA > outvalB) ? outvalA : outvalB;
959
960// log_msg (5, "drive = %s, error = %s", drive, strerror (errno));
961// fatal_error ("GPSOD: Unable to get size of drive");
962 log_msg(1, "%s --> %ld or %ld --> %ld", drive, outvalA, outvalB,
963 outvalC);
964
965 return (outvalC);
966}
967
968/**
969 * Determine whether @p format is supported by the kernel. Uses /proc/filesystems
970 * under Linux and @c lsvfs under FreeBSD.
971 * @param format The format to test.
972 * @return TRUE if the format is supported, FALSE if not.
973 */
974bool is_this_a_valid_disk_format(char *format)
975{
976 char *good_formats = NULL;
977 char *command = NULL;
978 char *format_sz = NULL;
979
980 FILE *pin;
981 int retval;
982
983 assert_string_is_neither_NULL_nor_zerolength(format);
984
985 mr_asprintf(format_sz, "%s ", format);
986
987#ifdef __FreeBSD__
988 mr_asprintf(command, "lsvfs | tr -s '\t' ' ' | grep -v Filesys | grep -v -- -- | cut -d' ' -f1 | tr -s '\n' ' '");
989#else
990 mr_asprintf(command, "grep -v nodev /proc/filesystems | tr -s '\t' ' ' | cut -d' ' -f2 | tr -s '\n' ' '");
991#endif
992
993 pin = popen(command, "r");
994 mr_free(command);
995
996 if (!pin) {
997 log_OS_error("Unable to read good formats");
998 retval = 0;
999 } else {
1000 mr_getline(good_formats, pin);
1001 if (pclose(pin)) {
1002 log_OS_error("Cannot pclose good formats");
1003 }
1004 mr_strip_spaces(good_formats);
1005 mr_strcat(good_formats, " swap lvm raid ntfs-3g ntfs 7 "); // " ntfs 7 " -- um, cheating much? :)
1006 if (strstr(good_formats, format_sz)) {
1007 retval = 1;
1008 } else {
1009 retval = 0;
1010 }
1011 mr_free(good_formats);
1012 }
1013 mr_free(format_sz);
1014
1015 return (retval);
1016}
1017
1018
1019/** @def SWAPLIST_COMMAND The command to list the swap files/partitions in use. */
1020
1021/**
1022 * Determine whether @p device_raw is currently mounted.
1023 * @param device_raw The device to check.
1024 * @return TRUE if it's mounted, FALSE if not.
1025 */
1026bool is_this_device_mounted(char *device_raw)
1027{
1028
1029 /*@ pointers **************************************************** */
1030 FILE *fin;
1031
1032 /*@ buffers ***************************************************** */
1033 char *incoming = NULL;
1034 char *device_with_tab = NULL;
1035 char *device_with_space = NULL;
1036 char *tmp = NULL;
1037 bool retval = FALSE;
1038
1039#ifdef __FreeBSD__
1040#define SWAPLIST_COMMAND "swapinfo"
1041#else
1042#define SWAPLIST_COMMAND "cat /proc/swaps"
1043#endif
1044
1045 /*@ end vars **************************************************** */
1046
1047 if (device_raw == NULL) {
1048 return(FALSE);
1049 }
1050
1051 if (device_raw[0] != '/' && !strstr(device_raw, ":/")) {
1052 log_msg(1, "%s needs to have a '/' prefixed - I'll do it",
1053 device_raw);
1054 mr_asprintf(tmp, "/%s", device_raw);
1055 } else {
1056 mr_asprintf(tmp, "%s", device_raw);
1057 }
1058 log_msg(1, "Is %s mounted?", tmp);
1059 if (!strcmp(tmp, "/proc") || !strcmp(tmp, "proc")) {
1060 log_msg(1,
1061 "I don't know how the heck /proc made it into the mountlist. I'll ignore it.");
1062 mr_free(tmp);
1063 return(FALSE);
1064 }
1065 mr_asprintf(device_with_tab, "%s\t", tmp);
1066 mr_asprintf(device_with_space, "%s ", tmp);
1067 mr_free(tmp);
1068
1069 if (!(fin = popen("mount", "r"))) {
1070 log_OS_error("Cannot popen 'mount'");
1071 return(FALSE);
1072 }
1073
1074 for (mr_getline(incoming, fin); !feof(fin); mr_getline(incoming, fin)) {
1075 if (strstr(incoming, device_with_space) || strstr(incoming, device_with_tab)) {
1076 paranoid_pclose(fin);
1077 mr_free(incoming);
1078 return(TRUE);
1079 }
1080 mr_free(incoming);
1081 }
1082 mr_free(incoming);
1083 mr_free(device_with_tab);
1084 paranoid_pclose(fin);
1085 mr_asprintf(tmp, "%s | grep -E \"^%s\" > /dev/null 2> /dev/null", SWAPLIST_COMMAND, device_with_space);
1086 mr_free(device_with_space);
1087 log_msg(4, "tmp (command) = '%s'", tmp);
1088 if (!system(tmp)) {
1089 retval = TRUE;
1090 }
1091 mr_free(tmp);
1092 return(retval);
1093}
1094
1095#ifdef __FreeBSD__
1096// CODE IS FREEBSD-SPECIFIC
1097/**
1098 * Create a loopback device for specified @p fname.
1099 * @param fname The file to associate with a device.
1100 * @return /dev entry for the device, or NULL if it couldn't be allocated.
1101 */
1102char *make_vn(char *fname)
1103{
1104 char *device = (char *) malloc(MAX_STR_LEN);
1105 char *mddevice = (char *) malloc(32);
1106 char command[MAX_STR_LEN];
1107 char *tmp = NULL;
1108 int vndev = 2;
1109 int i = 2;
1110
1111 tmp = call_program_and_get_last_line_of_output("/sbin/sysctl -n kern.osreldate",TRUE);
1112 i = atoi(tmp);
1113 mr_free(tmp);
1114
1115 if (i < 500000) {
1116 do {
1117 sprintf(mddevice, "vn%ic", vndev++);
1118 sprintf(command, "vnconfig %s %s", mddevice, fname);
1119 if (vndev > 10) {
1120 return NULL;
1121 }
1122 }
1123 while (system(command));
1124 } else {
1125 sprintf(command, "mdconfig -a -t vnode -f %s", fname);
1126 mddevice = call_program_and_get_last_line_of_output(command,TRUE);
1127 if (!strstr(mddevice, "md")) {
1128 return NULL;
1129 }
1130 }
1131 sprintf(device, "/dev/%s", mddevice);
1132 return device;
1133}
1134
1135
1136
1137// CODE IS FREEBSD-SPECIFIC
1138/**
1139 * Deallocate specified @p dname.
1140 * This should be called when you are done with the device created by make_vn(),
1141 * so the system does not run out of @c vn devices.
1142 * @param dname The device to deallocate.
1143 * @return 0 for success, nonzero for failure.
1144 */
1145int kick_vn(char *dname)
1146{
1147 char *command = NULL;
1148 char *tmp = NULL;
1149 int res = 0;
1150 int i = 0;
1151
1152 if (strncmp(dname, "/dev/", 5) == 0) {
1153 dname += 5;
1154 }
1155
1156 tmp = call_program_and_get_last_line_of_output("/sbin/sysctl -n kern.osreldate",TRUE);
1157 i = atoi(tmp);
1158 mr_free(tmp);
1159
1160 if (i < 500000) {
1161 mr_asprintf(command, "vnconfig -d %s", dname);
1162 } else {
1163 mr_asprintf(command, "mdconfig -d -u %s", dname);
1164 }
1165 res = system(command);
1166 mr_free(command);
1167 return(res);
1168}
1169#endif
1170
1171
1172/**
1173 * Mount the CD-ROM at @p mountpoint.
1174 * @param device The device (or file if g_ISO_restore_mode) to mount.
1175 * @param mountpoint The place to mount it.
1176 * @return 0 for success, nonzero for failure.
1177 */
1178int mount_USB_here(char *device, char *mountpoint)
1179{
1180 /*@ buffer ****************************************************** */
1181 char *command = NULL;
1182 int retval;
1183
1184 assert_string_is_neither_NULL_nor_zerolength(device);
1185 assert_string_is_neither_NULL_nor_zerolength(mountpoint);
1186
1187 make_hole_for_dir(mountpoint);
1188 if (isdigit(device[0])) {
1189 return(1);
1190 }
1191 log_msg(4, "(mount_USB_here --- device=%s, mountpoint=%s", device, mountpoint);
1192
1193#ifdef __FreeBSD__
1194 mr_asprintf(command, "mount_vfat %s %s 2>> %s", device, mountpoint, MONDO_LOGFILE);
1195
1196#else
1197 mr_asprintf(command, "mount %s -t vfat %s 2>> %s", device, mountpoint, MONDO_LOGFILE);
1198#endif
1199
1200 log_msg(4, command);
1201 retval = system(command);
1202 log_msg(1, "system(%s) returned %d", command, retval);
1203 mr_free(command);
1204
1205 return (retval);
1206}
1207
1208/**
1209 * Mount the CD-ROM at @p mountpoint.
1210 * @param device The device (or file if g_ISO_restore_mode) to mount.
1211 * @param mountpoint The place to mount it.
1212 * @return 0 for success, nonzero for failure.
1213 */
1214int mount_CDROM_here(char *device, const char *mountpoint)
1215{
1216 /*@ buffer ****************************************************** */
1217 char *command = NULL;
1218 int retval;
1219#ifdef __FreeBSD__
1220 char *dev = NULL;
1221#else
1222 char *options = NULL;
1223#endif
1224
1225 assert_string_is_neither_NULL_nor_zerolength(mountpoint);
1226
1227 make_hole_for_dir(mountpoint);
1228
1229 if ((device == NULL) || (isdigit(device[0]))) {
1230 mr_free(device);
1231 device = find_cdrom_device(FALSE);
1232 }
1233#ifndef __FreeBSD__
1234 mr_asprintf(options, "ro");
1235#endif
1236
1237 if (g_ISO_restore_mode) {
1238
1239#ifdef __FreeBSD__
1240 mr_asprintf(dev, "%s", make_vn(device));
1241 if (!dev) {
1242 mr_asprintf(command, "Unable to mount ISO (make_vn(%s) failed)", device);
1243 fatal_error(command);
1244 }
1245 mr_free(device);
1246 device = dev;
1247#else
1248 mr_strcat(options, ",loop");
1249#endif
1250
1251 }
1252 log_msg(4, "(mount_CDROM_here --- device=%s, mountpoint=%s", device, mountpoint);
1253 /*@ end vars *************************************************** */
1254
1255#ifdef __FreeBSD__
1256 mr_asprintf(command, "mount_cd9660 -r %s %s 2>> %s", device, mountpoint, MONDO_LOGFILE);
1257
1258#else
1259 mr_asprintf(command, "mount %s -o %s -t iso9660 %s 2>> %s", device, options, mountpoint, MONDO_LOGFILE);
1260 paranoid_free(options);
1261#endif
1262
1263 log_msg(4, command);
1264 if (strncmp(device, "/dev/", 5) == 0) {
1265 retract_CD_tray_and_defeat_autorun();
1266 }
1267 retval = system(command);
1268 log_msg(1, "system(%s) returned %d", command, retval);
1269 mr_free(command);
1270
1271 return (retval);
1272}
1273
1274
1275/**
1276* Mount the CD-ROM or USB device at /mnt/cdrom.
1277* @param bkpinfo The backup information structure. Fields used:
1278* - @c bkpinfo->backup_media_type
1279* - @c bkpinfo->disaster_recovery
1280* - @c bkpinfo->isodir
1281* - @c bkpinfo->media_device
1282* @return 0 for success, nonzero for failure.
1283*/
1284int mount_media()
1285{
1286char *mount_cmd = NULL;
1287char *mountdir = NULL;
1288int i, res;
1289#ifdef __FreeBSD__
1290 char mdd[32];
1291 char *mddev = mdd;
1292#endif
1293
1294 if (bkpinfo->backup_media_type == tape || bkpinfo->backup_media_type == udev) {
1295 log_msg(8, "Tape/udev. Therefore, no need to mount a media.");
1296 return 0;
1297 }
1298
1299 if (!run_program_and_log_output("mount | grep -F " MNT_CDROM, FALSE)) {
1300 log_msg(2, "mount_media() - media already mounted. Fair enough.");
1301 return (0);
1302 }
1303
1304 if (bkpinfo->media_device == NULL) {
1305 fatal_error("No media device at that point");
1306 }
1307
1308 if (bkpinfo->backup_media_type == netfs) {
1309 log_msg(2, "Mounting for Network thingy");
1310 log_msg(2, "isodir = %s", bkpinfo->isodir);
1311 if (!strcmp(bkpinfo->isodir, "/") && am_I_in_disaster_recovery_mode()) {
1312 mr_free(bkpinfo->isodir);
1313 mr_asprintf(bkpinfo->isodir, "/tmp/isodir");
1314 log_msg(1, "isodir is being set to %s", bkpinfo->isodir);
1315 }
1316 if ((bkpinfo->isodir == NULL) || (bkpinfo->netfs_remote_dir == NULL) || (bkpinfo->prefix == NULL)) {
1317 fatal_error("Unable to prepare the iso filename");
1318 }
1319#ifdef __FreeBSD__
1320 mr_asprintf(mount_cmd, "/mnt/isodir/%s/%s/%s-%d.iso", bkpinfo->isodir,
1321 bkpinfo->netfs_remote_dir, bkpinfo->prefix, g_current_media_number);
1322 mddev = make_vn(mount_cmd);
1323 mr_free(mount_cmd);
1324
1325 mr_asprintf(mount_cmd, "mount_cd9660 -r %s " MNT_CDROM, mddev);
1326#else
1327 mr_asprintf(mount_cmd, "mount %s/%s/%s-%d.iso -t iso9660 -o loop,ro %s", bkpinfo->isodir, bkpinfo->netfs_remote_dir, bkpinfo->prefix, g_current_media_number, MNT_CDROM);
1328#endif
1329
1330 } else if (bkpinfo->backup_media_type == iso) {
1331 if (bkpinfo->subdir) {
1332 mr_asprintf(mountdir, "%s/%s", bkpinfo->isodir, bkpinfo->subdir);
1333 } else {
1334 mr_asprintf(mountdir, "%s", bkpinfo->isodir);
1335 }
1336#ifdef __FreeBSD__
1337 mr_asprintf(mount_cmd, "%s/%s-%d.iso", mountdir, bkpinfo->prefix, g_current_media_number);
1338 mddev = make_vn(mount_cmd);
1339 mr_free(mount_cmd);
1340
1341 mr_asprintf(mount_cmd, "mount_cd9660 -r %s %s", mddev, MNT_CDROM);
1342#else
1343 mr_asprintf(mount_cmd, "mount %s/%s-%d.iso -t iso9660 -o loop,ro %s", mountdir, bkpinfo->prefix, g_current_media_number, MNT_CDROM);
1344#endif
1345 mr_free(mountdir);
1346 } else if (bkpinfo->backup_media_type == usb) {
1347 mr_asprintf(mount_cmd, "mount -t vfat %s %s", bkpinfo->media_device, MNT_CDROM);
1348 } else if (strstr(bkpinfo->media_device, "/dev/")) {
1349#ifdef __FreeBSD__
1350 mr_asprintf(mount_cmd, "mount_cd9660 -r %s %s", bkpinfo->media_device, MNT_CDROM);
1351#else
1352 mr_asprintf(mount_cmd, "mount %s -t iso9660 -o ro %s", bkpinfo->media_device, MNT_CDROM);
1353#endif
1354 } else {
1355 mr_free(bkpinfo->media_device);
1356 if (bkpinfo->disaster_recovery && does_file_exist("/tmp/CDROM-LIVES-HERE")) {
1357 bkpinfo->media_device = last_line_of_file(MINDI_CACHE"/CDROM-LIVES-HERE");
1358 } else {
1359 bkpinfo->media_device = find_cdrom_device(TRUE);
1360 }
1361
1362#ifdef __FreeBSD__
1363 mr_asprintf(mount_cmd, "mount_cd9660 -r %s %s", bkpinfo->media_device, MNT_CDROM);
1364#else
1365 mr_asprintf(mount_cmd, "mount %s -t iso9660 -o ro %s", bkpinfo->media_device, MNT_CDROM);
1366#endif
1367 }
1368
1369 log_msg(2, "(mount_media) --- command = %s", mount_cmd);
1370 for (i = 0; i < 2; i++) {
1371 res = run_program_and_log_output(mount_cmd, FALSE);
1372 if (!res) {
1373 break;
1374 } else {
1375 log_msg(2, "Failed to mount device.");
1376 sleep(5);
1377 sync();
1378 }
1379 }
1380 mr_free(mount_cmd);
1381
1382 if (res) {
1383 log_msg(2, "Failed, despite %d attempts", i);
1384 } else {
1385 log_msg(2, "Mounted media drive OK");
1386 }
1387 return (res);
1388}
1389/**************************************************************************
1390*END_MOUNT_CDROM *
1391**************************************************************************/
1392
1393
1394
1395
1396/**
1397 * Ask the user for CD number @p cd_number_i_want.
1398 * Sets g_current_media_number once the correct CD is inserted.
1399 * @param bkpinfo The backup information structure. Fields used:
1400 * - @c bkpinfo->backup_media_type
1401 * - @c bkpinfo->prefix
1402 * - @c bkpinfo->isodir
1403 * - @c bkpinfo->media_device
1404 * - @c bkpinfo->please_dont_eject_when_restoring
1405 * @param cd_number_i_want The CD number to ask for.
1406 */
1407void
1408insist_on_this_cd_number(int cd_number_i_want)
1409{
1410
1411 /*@ int ************************************************************* */
1412 int res = 0;
1413
1414
1415 /*@ buffers ********************************************************* */
1416 char *tmp = NULL;
1417 char *mds = NULL;
1418 char *request = NULL;
1419
1420 assert(bkpinfo != NULL);
1421 assert(cd_number_i_want > 0);
1422
1423// log_msg(3, "Insisting on CD number %d", cd_number_i_want);
1424
1425 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1426 log_msg(3,
1427 "No need to insist_on_this_cd_number when the backup type isn't CD-R(W) or NFS or ISO");
1428 return;
1429 }
1430 mr_asprintf(tmp, "mkdir -p " MNT_CDROM);
1431 run_program_and_log_output(tmp, 5);
1432 mr_free(tmp);
1433
1434 if (g_ISO_restore_mode || bkpinfo->backup_media_type == iso || bkpinfo->backup_media_type == netfs) {
1435 // BERLIOS --- I'm tempted to do something about this...
1436 // Why unmount and remount again and again?
1437 g_ISO_restore_mode = TRUE;
1438 if (!is_this_device_mounted(MNT_CDROM)) {
1439 log_msg(3, "Mounting media");
1440 g_current_media_number = cd_number_i_want;
1441 mount_media();
1442 }
1443 }
1444 if ((res = what_number_cd_is_this()) != cd_number_i_want) {
1445 log_msg(3, "Currently, we hold %d but we want %d", res, cd_number_i_want);
1446
1447 /* Now we need to umount the current media to have the next mounted after */
1448 run_program_and_log_output("umount -d " MNT_CDROM, FALSE);
1449 log_msg(3, "Mounting next media %d",cd_number_i_want);
1450 g_current_media_number = cd_number_i_want;
1451 mount_media();
1452
1453 mds = media_descriptor_string(bkpinfo->backup_media_type);
1454 log_msg(3, "Insisting on %s #%d", mds, cd_number_i_want);
1455 mr_asprintf(request, "Please insert %s #%d and press Enter.", mds, cd_number_i_want);
1456 mr_free(mds);
1457
1458 while (what_number_cd_is_this() != cd_number_i_want) {
1459 sync();
1460 if (is_this_device_mounted(MNT_CDROM)) {
1461 res =
1462 run_program_and_log_output("umount -d " MNT_CDROM, FALSE);
1463 } else {
1464 res = 0;
1465 }
1466 if (res) {
1467 log_to_screen("WARNING - failed to unmount CD-ROM drive");
1468 }
1469 if (!bkpinfo->please_dont_eject) {
1470 res = eject_device(bkpinfo->media_device);
1471 } else {
1472 res = 0;
1473 }
1474 if (res) {
1475 log_to_screen("WARNING - failed to eject CD-ROM disk");
1476 }
1477 popup_and_OK(request);
1478 if (!bkpinfo->please_dont_eject) {
1479 inject_device(bkpinfo->media_device);
1480 }
1481 sync();
1482 }
1483 mr_free(request);
1484
1485 log_msg(1, "Thankyou. Proceeding...");
1486 g_current_media_number = cd_number_i_want;
1487 }
1488}
1489
1490/* @} - end of deviceGroup */
1491
1492
1493
1494/**
1495 * Creates a singly linked list of all of the non-NETFS mounted file systems.
1496 * @param DSFptr A pointer to the structure MOUNTED_FS_STRUCT used to hold
1497 * the list of mounted file systems.
1498 * @return None.
1499 */
1500static void add_mounted_fs_struct (MOUNTED_FS_STRUCT *DSFptr)
1501{
1502 assert (DSFptr);
1503 if (DSF_Head == NULL) {
1504 DSF_Head = DSFptr;
1505 } else {
1506 DSF_Tail->next = DSFptr;
1507 }
1508 DSFptr->next = NULL;
1509 DSF_Tail = DSFptr;
1510}
1511
1512/**
1513 * Find the structure, in the singly linked list of all of the non-NETFS
1514 * mounted file systems, that contains the specified device.
1515 * @param device The device to find
1516 * @return NULL if it didn't find the device, a pointer to the
1517 * structure if it did.
1518 */
1519static MOUNTED_FS_STRUCT *find_device_in_list (char *device)
1520{
1521 MOUNTED_FS_STRUCT *DSFptr = NULL;
1522
1523 DSFptr = DSF_Head;
1524 while (DSFptr != NULL) {
1525 if (!strcmp(DSFptr->device, device)) {
1526 break;
1527 }
1528 DSFptr = DSFptr->next;
1529 }
1530 return (DSFptr);
1531}
1532
1533/**
1534 * Find the structure, in the singly linked list of all of the non-NETFS
1535 * mounted file systems, that contains the specified mount point.
1536 * @param mount_point The mount point to find
1537 * @return NULL is it didn't find the mount point, a pointer to the
1538 * structure if it did.
1539 */
1540static MOUNTED_FS_STRUCT *find_mount_point_in_list (char *mount_point)
1541{
1542 MOUNTED_FS_STRUCT *DSFptr = NULL;
1543
1544 DSFptr = DSF_Head;
1545 while (DSFptr != NULL) {
1546 if (!strcmp(DSFptr->mount_point, mount_point)) {
1547 break;
1548 }
1549 DSFptr = DSFptr->next;
1550 }
1551 return (DSFptr);
1552}
1553
1554/**
1555 * Frees the memory for all of the structures on the linked list of
1556 * all of the non-NETFS mounted file systems.
1557 */
1558static void free_mounted_fs_list (void) {
1559 MOUNTED_FS_STRUCT *DSFptr = NULL;
1560 MOUNTED_FS_STRUCT *DSFnext = NULL;
1561
1562 DSFptr = DSF_Head;
1563 while (DSFptr != NULL) {
1564 DSFnext = DSFptr->next;
1565 paranoid_free(DSFptr);
1566 DSFptr = DSFnext;
1567 }
1568 DSF_Head = NULL;
1569 DSF_Tail = NULL;
1570}
1571
1572
1573/**
1574 * Creates a linked list of all of the non-NETFS mounted file systems.
1575 * We use a linked list because we don't know how many mounted file
1576 * there are (and there can be a lot).
1577 * @return 0 on success and greated than 0 on failure.
1578 */
1579static int create_list_of_non_NETFS_mounted_file_systems (void)
1580{
1581 int i = 0;
1582 int mount_cnt = 0;
1583 int lastpos = 0;
1584 char *mounted_file_system = NULL;
1585 char *command = NULL;
1586 char *token = NULL;
1587 char token_chars[] =" :\t\r\f\a\0";
1588 MOUNTED_FS_STRUCT *DSFptr = NULL;
1589
1590 free_mounted_fs_list();
1591 /********
1592 * Find the number of mounted file system entries and their respective mount points.
1593 * I can't return all of the entries as one string because it's length can be longer
1594 * than MAX_STR_LEN which is used in call_program_and_get_last_line_of_output().
1595 * So start looping and get the number of mounted file systems and query them one by one.
1596 ********/
1597 /* Get the number of mounted file systems ((those that start with "/dev/" */
1598 mr_asprintf(command, "mount 2>/dev/null | awk '{if($1 ~ \"^/dev/\"){print $0}}'|wc -l");
1599 log_msg(5, "Running: %s", command);
1600 mounted_file_system = call_program_and_get_last_line_of_output(command,TRUE);
1601 mr_free(command);
1602
1603 mount_cnt = atoi(mounted_file_system);
1604 log_msg (5, "mount_cnt: %d", mount_cnt);
1605 mr_free(mounted_file_system);
1606
1607 for (i=mount_cnt; i > 0; i--) {
1608 mr_asprintf(command, "mount 2>/dev/null | awk '{if($1 ~ \"^/dev/\"){print $1,$3}}'|head -n %d", i);
1609 log_msg(5, "Running: %s", command);
1610 mounted_file_system = call_program_and_get_last_line_of_output(command,TRUE);
1611 mr_free(command);
1612
1613 log_msg (5, "mounted_file_system: %s", mounted_file_system);
1614 if ((token = mr_strtok(mounted_file_system, token_chars, &lastpos)) == NULL) {
1615 log_msg (4, "Could not get the list of mounted file systems");
1616 mr_free(mounted_file_system);
1617 mr_free(token);
1618 return (1);
1619 }
1620 if (token) {
1621 log_msg (5, "token: %s", token);
1622 }
1623 while (token != NULL) {
1624 log_msg (5, "token: %s", token);
1625 if ((DSFptr = (MOUNTED_FS_STRUCT *) calloc(1, sizeof(MOUNTED_FS_STRUCT))) == NULL) {
1626 fatal_error ("Cannot allocate memory");
1627 }
1628 add_mounted_fs_struct(DSFptr);
1629 strcpy(DSFptr->device, token);
1630 mr_free(token);
1631 if ((token = mr_strtok(mounted_file_system, token_chars, &lastpos)) == NULL) {
1632 log_msg (5, "Ran out of entries on the mounted file systems list");
1633 mr_free(mounted_file_system);
1634 mr_free(token);
1635 return (1);
1636 }
1637 log_msg (5, "token: %s", token);
1638 strcpy(DSFptr->mount_point, token);
1639 mr_free(token);
1640 token = mr_strtok(mounted_file_system, token_chars, &lastpos);
1641 }
1642 mr_free(mounted_file_system);
1643 }
1644 return (0);
1645}
1646
1647
1648
1649/**
1650 * Given a whole disk device special file, determine which mounted file systems
1651 * are on the dsf's partitions and which mounted file systems are not.
1652 * @param dsf The whole disk device special file.
1653 * @param included_dsf_list A char pointer used to hold the list of mount points
1654 * that are on the dsf. Memory for the array will be allocated within the function.
1655 * @param excluded_dsf_list A char pointer used to hold the list of mount points
1656 * that are not on the dsf. Memory for the array will be allocated within the function.
1657 * @return 0 on success, -1 if no device special file was passed in, -2 if a device
1658 * special file was passed in but it has no partitions on it, or 1 on failure
1659 */
1660static int get_dsf_mount_list (const char *dsf, char **included_dsf_list, char **excluded_dsf_list) {
1661 int i = 0;
1662 int c = 0;
1663 int lastpos = 0;
1664 char *VG = NULL;
1665 char *tmp = NULL;
1666 char *command = NULL;
1667 char *partition_list = NULL;
1668 char *partitions[64];
1669 char *mount_list = NULL;
1670 char *token = NULL;
1671 char *ndsf = NULL;
1672 char token_chars[] =" \t\r\f\a\0";
1673 MOUNTED_FS_STRUCT *DSFptr = NULL;
1674
1675 memset((char *)partitions, 0, sizeof(partitions));
1676
1677 log_msg(5, "dsf: %s", dsf);
1678
1679 /********
1680 * See if a device special file was passed in (i.e. it must start with /dev/
1681 ********/
1682 if (strncmp(dsf, "/dev/", 5)) {
1683 log_msg (4, "%s does not start with /dev/ and (probably) is not a device special file", dsf);
1684 return (-1);
1685 }
1686 log_msg(4, " %s looks like a device special file", dsf);
1687 /* Verify that the dsf exists */
1688 mr_asprintf(command, "ls -al %s 2>/dev/null | wc -l", dsf);
1689 log_msg(5, " Executing: %s", command);
1690 tmp = call_program_and_get_last_line_of_output(command,TRUE);
1691 mr_free(command);
1692
1693 log_msg(5, " Return value: %s", tmp);
1694 c = atoi(tmp);
1695 mr_free(tmp);
1696
1697 if (!c) {
1698 log_to_screen("Cannot find device special file %s", dsf);
1699 return (1);
1700 }
1701 log_msg(4, " %s device special file exists", dsf);
1702
1703 /* Get a list of the mounted file systems */
1704 if (create_list_of_non_NETFS_mounted_file_systems()) {
1705 log_to_screen ("Could not get the list of mounted file systems");
1706 return (1);
1707 }
1708 log_msg (5, "Processing dsf: %s", dsf);
1709 /********
1710 * Get a list of the dsf's partitions. There could be no partitions on the disk
1711 * or a dsf of a partition was passed in (e.g. /dev/sda1 instead of /dev/sda).
1712 * Either way, it's an error.
1713 ********/
1714 mr_asprintf(command, "parted2fdisk -l %s 2>/dev/null|grep -E \"^/dev/\"|awk '{printf(\"%%s \", $1)}END{print \"\"}'", dsf);
1715 log_msg(5, "Executing: %s", command);
1716 partition_list = call_program_and_get_last_line_of_output(command,TRUE);
1717 mr_free(command);
1718
1719 log_msg(4, "Partition list for %s: %s", dsf, partition_list);
1720 if (!strlen(partition_list)) {
1721 /* There were no partitions on the disk */
1722 log_msg(4, "No partitions on device special file %s", dsf);
1723 log_msg(4, "I guess it's a partition itself");
1724 mr_asprintf(partitions[0], "%s", dsf);
1725 mr_asprintf(tmp, "%s", dsf);
1726 ndsf = truncate_to_drive_name(tmp);
1727 mr_free(tmp);
1728 } else {
1729 /* Fill the partition list */
1730 i = 0;
1731 lastpos = 0;
1732 while ((token = mr_strtok(partition_list, token_chars, &lastpos)) != NULL) {
1733 log_msg (4, "Found partition: %s", token);
1734 partitions[i++] = token;
1735 }
1736 mr_asprintf(ndsf, "%s", dsf);
1737 }
1738 mr_free(partition_list);
1739
1740 /* In any case we want to exclude the dsf itself from all MondRescue activities
1741 * at restore time (LVM, fdisk, ...) so we want it in our exclude_dev list */
1742 if ((DSFptr = (MOUNTED_FS_STRUCT *) calloc(1, sizeof(MOUNTED_FS_STRUCT))) == NULL) {
1743 fatal_error ("Cannot allocate memory");
1744 }
1745 add_mounted_fs_struct(DSFptr);
1746 strcpy(DSFptr->device, dsf);
1747 DSFptr->check = 1;
1748
1749 /* For the rest ndsf is the new dsf to deal with */
1750 /********
1751 * At this point, we have a list of all of the partitions on the dsf. Now try to
1752 * see which partitions have a file system on them.
1753 *
1754 * Loop through each partition on the disk and:
1755 *
1756 * - If the partition is swap, it ignores it.
1757 *
1758 * - If the partition is mounted (e.g. /dev/sda1 is mounted on /boot), it adds an entry
1759 * to the linked list, copies to it the device name and mount point, and sets check == 1.
1760 *
1761 * - If the partition is part of a Volume Group that has Logical Volumes mounted, it adds
1762 * an entry to the linked list for each mounted Logical Volume in that Volume Group, copying
1763 * to it the device name and mount point, and sets check == 1. Note that if the Volume Group
1764 * contains more than one disk, it will still add the entry even if the Logical Volume's
1765 * extents are not on the dsf that was passed in to the function. For example, Volume Group
1766 * VolGroup00 contains the disks /dev/sda1 and /dev/sdb1, and the Logical Volumes LogVol01,
1767 * which is mounted on /var and has all of its extents on /dev/sda1, and LogVol02, which is
1768 * mounted as /usr and has all of its extents on /dev/sdb1. If you pass /dev/sda into the
1769 * function, both /var and /usr will be archived even though /usr is actually on/dev/sdb.
1770 *
1771 * - If the partition is part of a Volume Group that has Logical Volumes used in a mounted
1772 * software raid device, it adds an entry to the linked list, copies to it the software raid
1773 * device name and mount point, and sets check == 1.
1774 *
1775 * - If the partition is part of a mounted software raid device, it adds an entry to the linked
1776 * list, copies to it the software raid device name and mount point, and sets check == 1.
1777 *
1778 ********/
1779 for (i=0; strlen(partitions[i]); i++) {
1780 log_msg(4, "Processing partition: %s", partitions[i]);
1781 /* See if it's swap. If it is, ignore it. */
1782 mr_asprintf(command, "parted2fdisk -l %s 2>/dev/null | awk '{if(($1==\"%s\")&&(toupper($0) ~ \"SWAP\")){print $1;exit}}'", ndsf, partitions[i]);
1783 log_msg(5, " Running: %s", command);
1784 tmp = call_program_and_get_last_line_of_output(command,TRUE);
1785 mr_free(command);
1786
1787 log_msg(5, " Return value: %s", tmp);
1788 c = strlen(tmp);
1789 mr_free(tmp);
1790
1791 if (c) {
1792 log_msg(4, "It's swap. Ignoring partition %s", partitions[i]);
1793 continue;
1794 }
1795
1796 /* It's not swap. See if we can find the mount point from the mount command. */
1797 mr_asprintf(command, "mount 2>/dev/null | awk '{if((NF>0)&&($1==\"%s\")){print $3}}'", partitions[i]);
1798 tmp = call_program_and_get_last_line_of_output(command,TRUE);
1799 mr_free(command);
1800
1801 if (strlen(tmp)) {
1802 log_msg(4, " %s is mounted: %s", partitions[i], tmp);
1803 if ((DSFptr = find_mount_point_in_list(tmp)) == NULL) {
1804 log_msg (4, "Can't find mount point %s in mounted file systems list", tmp);
1805 mr_free(tmp);
1806 return (1);
1807 }
1808 DSFptr->check = 1;
1809 mr_free(tmp);
1810 continue;
1811 }
1812 mr_free(tmp);
1813
1814 /* It's not swap and it's not mounted. See if it's LVM */
1815 log_msg(4, " It's not mounted. Checking to see if it's LVM...");
1816
1817 /* Check for LVM */
1818 mr_asprintf(command, "pvdisplay -c %s 2> /dev/null", partitions[i]);
1819 log_msg(5, " Running: %s", command);
1820 tmp = call_program_and_get_last_line_of_output(command,TRUE);
1821 mr_free(command);
1822
1823 if (strlen(tmp)) {
1824 log_msg(4, "Found an LVM partition at %s. Find the VG it's in...", partitions[i]);
1825 /* It's LVM: Find the VG it's in */
1826 mr_asprintf(command, "pvdisplay -v %s 2>/dev/null|grep \"VG Name\"|awk '{print $NF}'", partitions[i]);
1827 log_msg(5, " Running: %s", command);
1828 strcpy(VG, call_program_and_get_last_line_of_output(command,TRUE));
1829 mr_free(command);
1830
1831 log_msg(4, " Volume Group: %s", VG);
1832 if (strlen(VG)) {
1833 /* Found the Volume Group. Now find all of the VG's mount points */
1834 log_msg(4, " Found the Volume Group. Now find all of the VG's mount points");
1835 mr_asprintf(command, "mount 2>/dev/null|grep -E \"/dev/mapper/%s-|/dev/%s/\"|awk '{printf(\"%%s \",$3)}END{print \"\"}'", VG, VG);
1836 log_msg(5, " Running: %s", command);
1837 mr_asprintf(mount_list, "%s", call_program_and_get_last_line_of_output(command,TRUE));
1838 mr_free(command);
1839
1840 log_msg(4, " VG %s mount_list: %s", VG, mount_list);
1841 lastpos = 0;
1842 while ((token = mr_strtok(mount_list, token_chars, &lastpos)) != NULL) {
1843 log_msg (5, "mount point token: %s", token);
1844 if ((DSFptr = find_mount_point_in_list(token)) == NULL) {
1845 log_msg (4, "Can't find mount point %s in mounted file systems list", token);
1846 mr_free(tmp);
1847 mr_free(token);
1848 return (1);
1849 }
1850 DSFptr->check = 1;
1851 mr_free(token);
1852 }
1853 /********
1854 * Now we want to see if there are any software raid devices using
1855 * any of the Logical Volumes on the Volume Group.
1856 *******/
1857 mr_free(mount_list);
1858
1859 mr_asprintf(command, "%s", "cat /proc/mdstat|grep -iv Personal|awk '{if($0~\"^.*[ ]+:\"){printf(\"/dev/%s \", $1)}}END{print \"\"}'");
1860 log_msg (5, "Running: %s", command);
1861 mr_asprintf(mount_list, "%s", call_program_and_get_last_line_of_output(command,TRUE));
1862 mr_free(command);
1863
1864 log_msg(4, " Software raid device list: %s", mount_list);
1865 lastpos = 0;
1866 while ((token = mr_strtok(mount_list, token_chars, &lastpos)) != NULL) {
1867 mr_asprintf(command, "mdadm --detail %s 2>/dev/null | grep -c %s", token, VG);
1868 log_msg (5, "Running: %s", command);
1869 mr_free(tmp);
1870 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command,TRUE));
1871 mr_free(command);
1872
1873 log_msg(4, "Number of Software raid device: %s", tmp);
1874 if (atoi(tmp)) {
1875 /* This device is on our disk */
1876 if ((DSFptr = find_device_in_list(token)) == NULL) {
1877 log_msg (4, "Can't find device %s in mounted file systems list", token);
1878 mr_free(tmp);
1879 mr_free(VG);
1880 mr_free(token);
1881 return (1);
1882 }
1883 DSFptr->check = 1;
1884 }
1885 }
1886 mr_free(token);
1887 paranoid_free(mount_list);
1888 } else {
1889 log_msg (4, "Error finding Volume Group for partition %s", partitions[i]);
1890 mr_free(tmp);
1891 return (1);
1892 }
1893 mr_free(tmp);
1894 continue;
1895 } else {
1896 log_msg (4, "Error finding partition type for the partition %s", partitions[i]);
1897 }
1898 mr_free(tmp);
1899
1900 /********
1901 * It's not swap, mounted, or LVM. See if it's used in a software raid device.
1902 ********/
1903 log_msg (5, "It's not swap, mounted, or LVM. See if it's used in a software raid device.");
1904 mr_asprintf(command, "mdadm --examine %s 2>/dev/null | awk '{if($1 == \"UUID\"){print $3}}'", partitions[i]);
1905 log_msg(4, " Running: %s", command);
1906 tmp = call_program_and_get_last_line_of_output(command,TRUE);
1907 mr_free(command);
1908
1909 if (!strlen(tmp)) {
1910 log_msg(4, " Partition %s is not used in a non-LVM software raid device", partitions[i]);
1911 mr_free(tmp);
1912 continue;
1913 }
1914 log_msg (5, " UUID: %s", tmp);
1915
1916 /* Get the Software raid device list */
1917 mr_asprintf(command, "%s", "cat /proc/mdstat|grep -iv Personal|awk '{if($0~\"^.*[ ]+:\"){printf(\"/dev/%s \", $1)}}END{print \"\"}'");
1918 log_msg (5, " Running: %s", command);
1919 mount_list = call_program_and_get_last_line_of_output(command,TRUE);
1920 mr_free(command);
1921
1922 log_msg(4, " Software raid device list: %s", mount_list);
1923 /* Loop through the software raid device list to see if we can find the partition */
1924 lastpos = 0;
1925 while ((token = mr_strtok(mount_list, token_chars, &lastpos)) != NULL) {
1926 mr_asprintf(command, "mdadm --detail %s 2>/dev/null | grep -c %s", token, tmp);
1927 log_msg(4, " Running: %s", command);
1928 mr_free(tmp);
1929
1930 tmp = call_program_and_get_last_line_of_output(command,TRUE);
1931 mr_free(command);
1932
1933 c = atoi(tmp);
1934 mr_free(tmp);
1935
1936 if (!c) {
1937 log_msg (4," Didn't find partition %s in software raid device %s", partitions[i], token);
1938 } else {
1939 if ((DSFptr = find_device_in_list(token)) == NULL) {
1940 log_msg (4, "Can't find device %s in mounted file systems list", token);
1941 mr_free(token);
1942 return (1);
1943 }
1944 DSFptr->check = 1;
1945 break;
1946 }
1947 mr_free(token);
1948 }
1949 mr_free(tmp);
1950 mr_free(mount_list);
1951 mr_free(partitions[i]);
1952 }
1953
1954 /* Determine how much memory to allocate for included_dsf_list and excluded_dsf_list */
1955 i = 0;
1956 DSFptr= DSF_Head;
1957 while (DSFptr != NULL) {
1958 i += strlen(DSFptr->mount_point) + 1;
1959 DSFptr = DSFptr->next;
1960 }
1961 log_msg (5, "i: %d", i);
1962 if ((*included_dsf_list = (char *) calloc(i+100, sizeof(char))) == NULL) {
1963 fatal_error ("Cannot allocate memory");
1964 }
1965 if ((*excluded_dsf_list = (char *) calloc(i+100, sizeof(char))) == NULL) {
1966 fatal_error ("Cannot allocate memory");
1967 }
1968 DSFptr= DSF_Head;
1969 while (DSFptr != NULL) {
1970 if (DSFptr->check) {
1971 log_msg (4, "%s is mounted on %s and is on disk %s", DSFptr->device, DSFptr->mount_point, ndsf);
1972 strcat(*included_dsf_list, DSFptr->mount_point);
1973 strcat(*included_dsf_list, "|");
1974 } else {
1975 log_msg (4, "%s is mounted on %s and is NOT on disk %s", DSFptr->device, DSFptr->mount_point, ndsf);
1976 strcat(*excluded_dsf_list, DSFptr->mount_point);
1977 strcat(*excluded_dsf_list, "|");
1978 }
1979 DSFptr = DSFptr->next;
1980 }
1981 mr_free(ndsf);
1982
1983 log_msg (5, "included_dsf_list: %s", *included_dsf_list);
1984 log_msg (5, "excluded_dsf_list: %s", *excluded_dsf_list);
1985 return (0);
1986}
1987
1988
1989/* Update the bkpinfo structure for exclude & include paths
1990 * in order to handle correctly paths corresponding to devices */
1991void mr_make_devlist_from_pathlist(char *pathlist, char mode) {
1992
1993char *token = NULL;
1994int lastpos = 0;
1995char *mounted_on_dsf = NULL;
1996char *not_mounted_on_dsf = NULL;
1997char token_chars[] ="|\t\r\f\a\0\n";
1998char *tmp = NULL;
1999char *tmp1 = NULL;
2000char *tmp2 = NULL;
2001
2002if (pathlist == NULL) {
2003 return;
2004}
2005while ((token = mr_strtok(pathlist, token_chars, &lastpos)) != NULL) {
2006 switch (get_dsf_mount_list(token, &mounted_on_dsf, &not_mounted_on_dsf)) {
2007 case 1:
2008 if (mode == 'E') {
2009 log_msg(1, "WARNING ! %s doesn't exist in -E option", token);
2010 } else {
2011 log_msg(1, "ERROR ! %s doesn't exist in -I option", token);
2012 fatal_error("Error processing -I option");
2013 }
2014 break;
2015 /* Everything is OK; proceed to archive data */
2016 case 0:
2017 if (mode == 'E') {
2018 if (strlen(mounted_on_dsf)) {
2019 log_to_screen("Excluding the following file systems on %s:", token);
2020 log_to_screen("==> %s", mounted_on_dsf);
2021 log_msg (5, "Adding to bkpinfo->exclude_paths due to -E option: %s", mounted_on_dsf);
2022 if (bkpinfo->exclude_paths) {
2023 mr_strcat(bkpinfo->exclude_paths,"|%s",mounted_on_dsf);
2024 } else {
2025 mr_asprintf(bkpinfo->exclude_paths,"%s",mounted_on_dsf);
2026 }
2027 if (bkpinfo->exclude_devs) {
2028 mr_strcat(bkpinfo->exclude_devs,"|%s",token);
2029 } else {
2030 mr_asprintf(bkpinfo->exclude_devs,"%s",token);
2031 }
2032 }
2033 } else {
2034 log_to_screen("Archiving only the following file systems on %s:", token);
2035 log_to_screen("==> %s", mounted_on_dsf);
2036 mr_free(bkpinfo->include_paths);
2037 mr_asprintf(bkpinfo->include_paths, "%s", "/");
2038 if (strlen(not_mounted_on_dsf)) {
2039 log_msg (5, "Adding to bkpinfo->exclude_paths due to -I option: %s", not_mounted_on_dsf);
2040 log_to_screen("Not archiving the following file systems:");
2041 log_to_screen("==> %s", not_mounted_on_dsf);
2042 if (bkpinfo->exclude_paths) {
2043 mr_strcat(bkpinfo->exclude_paths, "|%s",not_mounted_on_dsf);
2044 } else {
2045 mr_asprintf(bkpinfo->exclude_paths,"%s",not_mounted_on_dsf);
2046 }
2047 }
2048 }
2049 break;
2050 /* It's a dsf but not a whole disk dsf */
2051 case -2:
2052 log_to_screen("Could %s be a partition instead of a whole disk device special file?\nIgnored.", token);
2053 break;
2054 /* A device special file was not passed in. Process it as a path. */
2055 case -1:
2056 /* Adds a | to ensure correct detection even at both ends */
2057 mr_asprintf(tmp1,"|%s",token);
2058 mr_asprintf(tmp2,"|%s|",token);
2059 if (mode == 'E') {
2060 /* Add the token if not already in the list */
2061 mr_asprintf(tmp,"|%s|",bkpinfo->exclude_paths);
2062 if (strstr(tmp,tmp2) == NULL) {
2063 if (bkpinfo->exclude_paths) {
2064 mr_strcat(bkpinfo->exclude_paths,tmp1);
2065 mr_free(tmp1);
2066 } else {
2067 bkpinfo->exclude_paths = tmp1;
2068 }
2069 }
2070 } else {
2071 /* Add the token if not already in the list */
2072 mr_asprintf(tmp,"|%s|",bkpinfo->include_paths);
2073 if (strstr(tmp,tmp2) == NULL) {
2074 mr_strcat(bkpinfo->include_paths, "%s", tmp1);
2075 }
2076 mr_free(tmp1);
2077 }
2078 mr_free(tmp);
2079 mr_free(tmp2);
2080 break;
2081 }
2082 mr_free(token);
2083
2084 if (bkpinfo->include_paths != NULL) {
2085 log_msg(1, "include_paths is now '%s'", bkpinfo->include_paths);
2086 }
2087 if (bkpinfo->exclude_paths != NULL) {
2088 log_msg(1, "exclude_paths is now '%s'", bkpinfo->exclude_paths);
2089 }
2090 if (bkpinfo->exclude_devs != NULL) {
2091 log_msg(1, "exclude_devs is now '%s'", bkpinfo->exclude_devs);
2092 }
2093}
2094}
2095
2096
2097/**
2098 * Get a |-separated list of NETFS mounts.
2099 * @return The list created.
2100 * @note The return value points to allocated string that needs to be freed
2101 * @bug Even though we only want the mounts, the devices are still checked.
2102 */
2103char *list_of_NETFS_mounts_only(void)
2104{
2105 char *exclude_these_directories = NULL;
2106
2107 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;}'",TRUE);
2108 log_msg(9,"list_of_NETFS_mounts_only returns %s\n",exclude_these_directories);
2109 return(exclude_these_directories);
2110}
2111
2112/* @} - end of utilityGroup */
2113
2114
2115
2116
2117
2118/**
2119 * Create a randomly-named FIFO. The format is @p stub "." [random] [random] where
2120 * [random] is a random number between 1 and 32767.
2121 * @param store_name_here Where to store the new filename.
2122 * @param stub A random number will be appended to this to make the FIFO's name.
2123 * @ingroup deviceGroup
2124 */
2125void make_fifo(char *store_name_here, char *stub)
2126{
2127 char *tmp = NULL;
2128
2129 assert_string_is_neither_NULL_nor_zerolength(stub);
2130
2131 sprintf(store_name_here, "%s%d%d", stub, (int) (random() % 32768),
2132 (int) (random() % 32768));
2133 make_hole_for_file(store_name_here);
2134 mkfifo(store_name_here, S_IRWXU | S_IRWXG);
2135 mr_asprintf(tmp, "chmod 770 %s", store_name_here);
2136 paranoid_system(tmp);
2137 mr_free(tmp);
2138}
2139
2140
2141
2142
2143
2144
2145/**
2146 * Set the tmpdir and scratchdir to reside on the partition with the most free space.
2147 * Automatically excludes DOS, NTFS, SMB, and NFS filesystems.
2148 * @param bkpinfo The backup information structure. @c bkpinfo->tmpdir and @c bkpinfo->scratchdir will be set.
2149 * @ingroup utilityGroup
2150 */
2151void sensibly_set_scratchdir()
2152{
2153 char *tmp = NULL;
2154 char *command = NULL;
2155 char *sz = NULL;
2156
2157#ifdef __FreeBSD__
2158 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;} | while read x ; do test -w $x && echo $x && break ; done'",TRUE);
2159#else
2160 tmp = call_program_and_get_last_line_of_output("LANGUAGE=C df -m -P -x nfs -x nfs4 -x fuse.sshfs -x fuse -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;}' | while read x ; do test -w $x && echo $x && break ; done",TRUE);
2161#endif
2162
2163 if (tmp[0] != '/') {
2164 mr_asprintf(sz, "%s", tmp);
2165 mr_free(tmp);
2166 mr_asprintf(tmp, "/%s", sz);
2167 mr_free(sz);
2168 }
2169 if (!tmp[0]) {
2170 fatal_error("I couldn't figure out the scratchdir!");
2171 }
2172
2173 /* Cleaning a potential previous scratchdir */
2174 if (bkpinfo->scratchdir) {
2175 mr_asprintf(command, "rm -Rf %s", bkpinfo->scratchdir);
2176 paranoid_system(command);
2177 mr_free(command);
2178 }
2179 mr_free(bkpinfo->scratchdir);
2180
2181 mr_asprintf(bkpinfo->scratchdir , "%s/mondo.scratch.%d", tmp, (int) (random() % 32768));
2182 log_it("bkpinfo->scratchdir is being set to %s", bkpinfo->scratchdir);
2183
2184 /* Cleaning potential previous scratchdir */
2185 mr_asprintf(command, "rm -Rf %s/mondo.scratch.*", tmp);
2186 mr_free(tmp);
2187
2188 paranoid_system(command);
2189 mr_free(command);
2190}
2191
2192
2193
2194
2195
2196
2197/**
2198 * @addtogroup deviceGroup
2199 * @{
2200 */
2201/**
2202 * If we can read @p dev, set @return output to it.
2203 * If @p dev cannot be read, set @p output to NULL.
2204 * @param dev The device to check for.
2205 * @return output Set to @p dev if @p dev exists, NULL otherwise.
2206 */
2207char *set_dev_to_this_if_rx_OK(char *dev)
2208{
2209 char *command = NULL;
2210 char *output = NULL;
2211
2212 if (!dev || dev[0] == '\0') {
2213 return(NULL);
2214 }
2215 log_msg(10, "Injecting %s", dev);
2216 inject_device(dev);
2217 if (!does_file_exist(dev)) {
2218 log_msg(10, "%s doesn't exist. Returning FALSE.", dev);
2219 return(NULL);
2220 }
2221 mr_asprintf(command, "dd bs=%ld count=1 if=%s of=/dev/null &> /dev/null", 512L, dev);
2222 if (!run_program_and_log_output(command, FALSE)
2223 && !run_program_and_log_output(command, FALSE)) {
2224 mr_asprintf(output, "%s", dev);
2225 log_msg(4, "Found it - %s", dev);
2226 } else {
2227 log_msg(4, "It's not %s", dev);
2228 }
2229 mr_free(command);
2230 return(output);
2231}
2232
2233
2234
2235
2236
2237/**
2238 * Find out what number CD is in the drive.
2239 * @param bkpinfo The backup information structure. The @c bkpinfo->media_device field is the only one used.
2240 * @return The current CD number, or -1 if it could not be found.
2241 * @note If the CD is not mounted, it will be mounted
2242 * (and remain mounted after this function returns).
2243 */
2244int what_number_cd_is_this()
2245{
2246 int cd_number = -1;
2247 char *mountdev = NULL;
2248 char *tmp = NULL;
2249
2250 assert(bkpinfo != NULL);
2251 // log_it("Asking what_number_cd_is_this");
2252 if (g_ISO_restore_mode) {
2253 mr_asprintf(tmp, "mount | grep iso9660 | awk '{print $3;}'");
2254 mountdev = call_program_and_get_last_line_of_output(tmp,TRUE);
2255 mr_strcat(mountdev, "/archives/THIS-CD-NUMBER");
2256 mr_free(tmp);
2257
2258 tmp = last_line_of_file(mountdev);
2259 cd_number = atoi(tmp);
2260 mr_free(tmp);
2261 mr_free(mountdev);
2262 return (cd_number);
2263 }
2264
2265 if (bkpinfo->media_device) {
2266 mr_asprintf(mountdev, "%s", bkpinfo->media_device);
2267 }
2268 if (!mountdev) {
2269 log_it("(what_number_cd_is_this) Warning - media_device unknown. Finding out...");
2270 mr_free(bkpinfo->media_device);
2271 bkpinfo->media_device = find_cdrom_device(FALSE);
2272 mr_asprintf(mountdev, "%s", bkpinfo->media_device);
2273 }
2274 if (!is_this_device_mounted(MNT_CDROM)) {
2275 if (bkpinfo->backup_media_type == usb) {
2276 mount_USB_here(mountdev, MNT_CDROM);
2277 } else {
2278 mount_CDROM_here(mountdev, MNT_CDROM);
2279 }
2280 }
2281 mr_free(mountdev);
2282
2283 tmp = last_line_of_file(MNT_CDROM "/archives/THIS-CD-NUMBER");
2284 cd_number = atoi(tmp);
2285 mr_free(tmp);
2286 return (cd_number);
2287}
2288
2289
2290
2291
2292
2293
2294
2295/**
2296 * Find out what device is mounted as root (/).
2297 * @return Root device.
2298 * @note The returned string points to static storage and will be overwritten with every call.
2299 * @bug A bit of a misnomer; it's actually finding out the root device.
2300 * The mountpoint (where it's mounted) will obviously be '/'.
2301 */
2302char *where_is_root_mounted() {
2303 /*@ buffers **************** */
2304 char *output = NULL;
2305
2306
2307#ifdef __FreeBSD__
2308 output = call_program_and_get_last_line_of_output("mount | grep ' on / ' | cut -d' ' -f1",TRUE);
2309#else
2310 output = call_program_and_get_last_line_of_output("mount | grep ' on / ' | cut -d' ' -f1 | sed 's/[0-9]//' | sed 's/[0-9]//'",TRUE);
2311 if (strstr(output, "/dev/cciss/")) {
2312 mr_free(output);
2313 output = call_program_and_get_last_line_of_output("mount | grep ' on / ' | cut -d' ' -f1 | cut -dp -f1",TRUE);
2314 }
2315 if (strstr(output, "/dev/md")) {
2316 mr_free(output);
2317 output = call_program_and_get_last_line_of_output("mount | grep ' on / ' | cut -d' ' -f1",TRUE);
2318 }
2319#endif
2320
2321 return (output);
2322}
2323
2324
2325/**
2326 * Find out which boot loader is in use.
2327 * @param which_device Device to look for the boot loader on.
2328 * @return 'L' for LILO, 'E'for ELILO, 'G' for GRUB, 'B' or 'D' for FreeBSD boot loaders, or 'U' for Unknown.
2329 * @note Under Linux, all drives are examined, not just @p which_device.
2330 */
2331#ifdef __FreeBSD__
2332char which_boot_loader(char *which_device) {
2333 int count_lilos = 0;
2334 int count_grubs = 0;
2335 int count_boot0s = 0;
2336 int count_dangerouslydedicated = 0;
2337
2338 log_it("looking at drive %s's MBR", which_device);
2339 if (does_string_exist_in_boot_block(which_device, "GRUB")) {
2340 count_grubs++;
2341 }
2342 if (does_string_exist_in_boot_block(which_device, "LILO")) {
2343 count_lilos++;
2344 }
2345 if (does_string_exist_in_boot_block(which_device, "Drive")) {
2346 count_boot0s++;
2347 }
2348 if (does_string_exist_in_first_N_blocks(which_device, "FreeBSD/i386", 17)) {
2349 count_dangerouslydedicated++;
2350 }
2351 log_it("%d grubs and %d lilos and %d elilos and %d boot0s and %d DD\n",
2352 count_grubs, count_lilos, count_elilos, count_boot0s,
2353 count_dangerouslydedicated);
2354
2355 if (count_grubs && !count_lilos) {
2356 return ('G');
2357 } else if (count_lilos && !count_grubs) {
2358 return ('L');
2359 } else if (count_grubs == 1 && count_lilos == 1) {
2360 log_it("I'll bet you used to use LILO but switched to GRUB...");
2361 return ('G');
2362 } else if (count_boot0s == 1) {
2363 return ('B');
2364 } else if (count_dangerouslydedicated) {
2365 return ('D');
2366 } else {
2367 log_it("Unknown boot loader");
2368 return ('U');
2369 }
2370}
2371
2372#else
2373
2374char which_boot_loader(char *which_device) {
2375 /*@ buffer ***************************************************** */
2376 char *list_drives_cmd = NULL;
2377 char *tmp = NULL;
2378 char *current_drive = NULL;
2379
2380 /*@ pointers *************************************************** */
2381 FILE *pdrives = NULL;
2382
2383 /*@ int ******************************************************** */
2384 int count_lilos = 0;
2385 int count_grubs = 0;
2386
2387 /*@ end vars *************************************************** */
2388
2389
2390#ifdef __IA64__
2391 /* No choice for it */
2392 return ('E');
2393#endif
2394
2395 tmp = where_is_root_mounted();
2396 mr_asprintf(list_drives_cmd, "parted2fdisk -l 2>/dev/null | grep \"/dev/.*:\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/; echo %s", tmp);
2397 mr_free(tmp);
2398 log_it("list_drives_cmd = %s", list_drives_cmd);
2399
2400 pdrives = popen(list_drives_cmd, "r");
2401 mr_free(list_drives_cmd);
2402
2403 if (!pdrives) {
2404 log_OS_error("Unable to open list of drives");
2405 return ('\0');
2406 }
2407
2408 for (mr_getline(current_drive, pdrives); !feof(pdrives); mr_getline(current_drive, pdrives)) {
2409 mr_strip_spaces(current_drive);
2410 log_it("looking at drive %s's MBR", current_drive);
2411 if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
2412 count_grubs++;
2413 /* BERLIOS : removed as I don't think it's mandatory here
2414 mr_free(which_device);
2415 mr_asprintf(which_device, "%s", current_drive);
2416 */
2417 mr_free(current_drive);
2418 break;
2419 }
2420 if (does_string_exist_in_boot_block(current_drive, "LILO")) {
2421 count_lilos++;
2422 /* BERLIOS : removed as I don't think it's mandatory here
2423 mr_free(which_device);
2424 mr_asprintf(which_device, "%s", current_drive);
2425 */
2426 mr_free(current_drive);
2427 break;
2428 }
2429 mr_free(current_drive);
2430 }
2431 mr_free(current_drive);
2432
2433 if (pclose(pdrives)) {
2434 log_OS_error("Cannot pclose pdrives");
2435 }
2436 log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
2437 if (count_grubs && !count_lilos) {
2438 return ('G');
2439 } else if (count_lilos && !count_grubs) {
2440 return ('L');
2441 } else if (count_grubs == 1 && count_lilos == 1) {
2442 log_it("I'll bet you used to use LILO but switched to GRUB...");
2443 return ('G');
2444 } else {
2445 // We need to look on each partition then
2446 mr_asprintf(list_drives_cmd, "parted2fdisk -l 2>/dev/null | grep -E \"^/dev/\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/");
2447 log_it("list_drives_cmd = %s", list_drives_cmd);
2448
2449 if (!(pdrives = popen(list_drives_cmd, "r"))) {
2450 log_OS_error("Unable to open list of drives");
2451 mr_free(list_drives_cmd);
2452 return ('\0');
2453 }
2454 mr_free(list_drives_cmd);
2455
2456 for (mr_getline(current_drive, pdrives); !feof(pdrives); mr_getline(current_drive, pdrives)) {
2457 mr_strip_spaces(current_drive);
2458 log_it("looking at partition %s's BR", current_drive);
2459 if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
2460 count_grubs++;
2461 /* BERLIOS : removed as I don't think it's mandatory here
2462 mr_free(which_device);
2463 mr_asprintf(which_device, "%s", current_drive);
2464 */
2465 mr_free(current_drive);
2466 break;
2467 }
2468 if (does_string_exist_in_boot_block(current_drive, "LILO")) {
2469 count_lilos++;
2470 /* BERLIOS : removed as I don't think it's mandatory here
2471 mr_free(which_device);
2472 mr_asprintf(which_device, "%s", current_drive);
2473 */
2474 mr_free(current_drive);
2475 break;
2476 }
2477 mr_free(current_drive);
2478 }
2479 mr_free(current_drive);
2480
2481 if (pclose(pdrives)) {
2482 log_OS_error("Cannot pclose pdrives");
2483 }
2484 log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
2485 if (count_grubs && !count_lilos) {
2486 return ('G');
2487 } else if (count_lilos && !count_grubs) {
2488 return ('L');
2489 } else if (count_grubs == 1 && count_lilos == 1) {
2490 log_it("I'll bet you used to use LILO but switched to GRUB...");
2491 return ('G');
2492 } else {
2493 log_it("Unknown boot loader");
2494 return ('U');
2495 }
2496 }
2497}
2498#endif
2499
2500
2501
2502
2503/**
2504 * Write zeroes over the first 16K of @p device.
2505 * @param device The device to zero.
2506 * @return 0 for success, 1 for failure.
2507 */
2508int zero_out_a_device(char *device)
2509{
2510 FILE *fout;
2511 int i;
2512
2513 assert_string_is_neither_NULL_nor_zerolength(device);
2514
2515 log_it("Zeroing drive %s", device);
2516 if (!(fout = fopen(device, "w"))) {
2517 log_OS_error("Unable to open/write to device");
2518 return (1);
2519 }
2520 for (i = 0; i < 16384; i++) {
2521 fputc('\0', fout);
2522 }
2523 paranoid_fclose(fout);
2524 log_it("Device successfully zeroed.");
2525 return (0);
2526}
2527
2528/**
2529 * Return the device pointed to by @p incoming.
2530 * @param incoming The device to resolve symlinks for.
2531 * @return The path to the real device file.
2532 * @note The returned string points to static storage that will be overwritten with each call.
2533 * @bug Won't work with file v4.0; needs to be written in C.
2534 */
2535char *resolve_softlinks_to_get_to_actual_device_file(char *incoming)
2536{
2537 char *output;
2538 char *command = NULL;
2539 char *curr_fname;
2540 char *scratch = NULL;
2541 char *tmp = NULL;
2542 char *p;
2543
2544 struct stat statbuf;
2545 malloc_string(curr_fname);
2546 if (!does_file_exist(incoming)) {
2547 log_it("resolve_softlinks_to_get_to_actual_device_file --- device not found");
2548 mr_asprintf(output, "%s", incoming);
2549 } else {
2550 strcpy(curr_fname, incoming);
2551 lstat(curr_fname, &statbuf);
2552 while (S_ISLNK(statbuf.st_mode)) {
2553 log_msg(1, "curr_fname = %s", curr_fname);
2554 mr_asprintf(command, "file %s", curr_fname);
2555 tmp = call_program_and_get_last_line_of_output(command,TRUE);
2556 mr_free(command);
2557 for (p = tmp + strlen(tmp); p != tmp && *p != '`' && *p != ' '; p--);
2558 p++;
2559 mr_asprintf(scratch, "%s", p);
2560 for (p = scratch; *p != '\0' && *p != '\''; p++);
2561 *p = '\0';
2562 log_msg(0, "curr_fname %s --> '%s' --> %s", curr_fname, tmp, scratch);
2563 mr_free(tmp);
2564
2565 if (scratch[0] == '/') {
2566 strcpy(curr_fname, scratch); // copy whole thing because it's an absolute softlink
2567 } else { // copy over the basename cos it's a relative softlink
2568 p = curr_fname + strlen(curr_fname);
2569 while (p != curr_fname && *p != '/') {
2570 p--;
2571 }
2572 if (*p == '/') {
2573 p++;
2574 }
2575 strcpy(p, scratch);
2576 }
2577 mr_free(scratch);
2578 lstat(curr_fname, &statbuf);
2579 }
2580 mr_asprintf(output, "%s", curr_fname);
2581 log_it("resolved %s to %s", incoming, output);
2582 }
2583 paranoid_free(curr_fname);
2584 return (output);
2585}
2586
2587/* @} - end of deviceGroup */
2588
2589/**
2590 * Return the type of partition format (GPT or MBR)
2591 */
2592char *which_partition_format(const char *drive)
2593{
2594 char *output;
2595 char *tmp = NULL;
2596 char *command = NULL;
2597 char *fdisk = NULL;
2598#ifdef __IA64__
2599 struct stat buf;
2600#endif
2601 mr_asprintf(fdisk, "/sbin/parted2fdisk");
2602 mr_asprintf(command, "%s -l %s | grep 'EFI GPT'", fdisk, drive);
2603 mr_free(fdisk);
2604
2605 tmp = call_program_and_get_last_line_of_output(command,TRUE);
2606 mr_free(command);
2607
2608 if (strstr(tmp, "GPT") == NULL) {
2609 mr_asprintf(output, "MBR");
2610 } else {
2611 mr_asprintf(output, "GPT");
2612 }
2613 mr_free(tmp);
2614
2615 log_msg(0, "Found %s partition table format type", output);
2616 return (output);
2617}
2618/**
2619 * Ask user for details of backup/restore information.
2620 * Called when @c mondoarchive doesn't get any parameters.
2621 * @param bkpinfo The backup information structure to fill out with the user's data.
2622 * @param archiving_to_media TRUE if archiving, FALSE if restoring.
2623 * @return 0, always.
2624 * @bug No point of `int' return value.
2625 * @ingroup archiveGroup
2626 */
2627int interactively_obtain_media_parameters_from_user(bool archiving_to_media)
2628// archiving_to_media is TRUE if I'm being called by mondoarchive
2629// archiving_to_media is FALSE if I'm being called by mondorestore
2630{
2631 char *tmp = NULL;
2632 char *p = NULL;
2633 char *q = NULL;
2634 char *tmpro = NULL;
2635 char *tmp1 = NULL;
2636 char *mds = NULL;
2637 char *sz_size = NULL;
2638 char *command = NULL;
2639 char *comment = NULL;
2640 char *compression_type = NULL;
2641 int i;
2642 FILE *fin;
2643
2644 assert(bkpinfo != NULL);
2645 bkpinfo->nonbootable_backup = FALSE;
2646
2647 // Tape, CD, NETFS, ...?
2648 srandom(getpid());
2649 bkpinfo->backup_media_type = which_backup_media_type(bkpinfo->restore_data);
2650 if (bkpinfo->backup_media_type == none) {
2651 log_to_screen("User has chosen not to backup the PC");
2652 finish(1);
2653 }
2654 /* Why asking to remove the media with tape ?
2655 if (bkpinfo->backup_media_type == tape && bkpinfo->restore_data) {
2656 popup_and_OK("Please remove media from drive(s)");
2657 }
2658 */
2659 tmp = bkptype_to_string(bkpinfo->backup_media_type);
2660 log_msg(3, "media type = %s", tmp);
2661 mr_free(tmp);
2662
2663 bkpinfo->cdrw_speed = (bkpinfo->backup_media_type == cdstream) ? 2 : 4;
2664 bkpinfo->compression_level = (bkpinfo->backup_media_type == cdstream) ? 1 : 5;
2665 bkpinfo->use_lzo = (bkpinfo->backup_media_type == cdstream) ? TRUE : FALSE;
2666 mvaddstr_and_log_it(2, 0, " ");
2667
2668 // Find device's /dev (or SCSI) entry
2669 switch (bkpinfo->backup_media_type) {
2670 case cdr:
2671 case cdrw:
2672 case dvd:
2673 case usb:
2674 /* Never try to eject a USB device */
2675 if (bkpinfo->backup_media_type == usb) {
2676 bkpinfo->please_dont_eject = TRUE;
2677 }
2678 if (archiving_to_media) {
2679 if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) {
2680 if (ask_me_yes_or_no("Is your computer a laptop, or does the CD writer incorporate BurnProof technology?")) {
2681 bkpinfo->manual_cd_tray = TRUE;
2682 }
2683 }
2684 if ((compression_type = which_compression_type()) == NULL) {
2685 log_to_screen("User has chosen not to backup the PC");
2686 finish(1);
2687 }
2688
2689 if ((bkpinfo->compression_level = which_compression_level()) == -1) {
2690 log_to_screen("User has chosen not to backup the PC");
2691 finish(1);
2692 }
2693 mds = media_descriptor_string(bkpinfo->backup_media_type);
2694 mr_asprintf(comment, "What speed is your %s (re)writer?", mds);
2695 mr_free(bkpinfo->media_device);
2696 if (bkpinfo->backup_media_type == dvd) {
2697 bkpinfo->media_device = find_dvd_device();
2698 mr_asprintf(tmp, "1");
2699 mr_asprintf(sz_size, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4482 MB
2700 log_msg(1, "Setting to DVD defaults");
2701 } else {
2702 mr_asprintf(bkpinfo->media_device, "%s", VANILLA_SCSI_CDROM);
2703 mr_asprintf(tmp, "4");
2704 mr_asprintf(sz_size, "%d", 650);
2705 log_msg(1, "Setting to CD defaults");
2706 }
2707 if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) {
2708 p = popup_and_get_string("Speed", comment, tmp);
2709 mr_free(tmp);
2710
2711 if (p == NULL) {
2712 log_to_screen("User has chosen not to backup the PC");
2713 mr_free(comment);
2714 finish(1);
2715 }
2716 /* tmp now has the new value given by the user */
2717 tmp = p;
2718 }
2719 mr_free(comment);
2720
2721 bkpinfo->cdrw_speed = atoi(tmp); // if DVD then this shouldn't ever be used anyway :)
2722 mr_free(tmp);
2723
2724 mr_asprintf(comment, "How much data (in Megabytes) will each %s store?", mds);
2725 mr_free(mds);
2726 p = popup_and_get_string("Size", comment, sz_size);
2727 mr_free(sz_size);
2728 mr_free(comment);
2729
2730 if (p == NULL) {
2731 log_to_screen("User has chosen not to backup the PC");
2732 finish(1);
2733 }
2734 sz_size = p;
2735
2736 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
2737 bkpinfo->media_size[i] = atoi(sz_size);
2738 }
2739
2740 if (bkpinfo->media_size[0] <= 0) {
2741 log_to_screen("User has chosen not to backup the PC");
2742 finish(1);
2743 }
2744 }
2745 /* No break because we continue even for usb */
2746 case cdstream:
2747 mds = media_descriptor_string(bkpinfo->backup_media_type);
2748
2749 mr_free(bkpinfo->media_device);
2750 if ((bkpinfo->disaster_recovery) && (bkpinfo->backup_media_type != usb)) {
2751 mr_asprintf(bkpinfo->media_device, "/dev/cdrom");
2752 log_msg(2, "CD-ROM device assumed to be at %s", bkpinfo->media_device);
2753 } else if ((bkpinfo->restore_data && (bkpinfo->backup_media_type != usb)) || bkpinfo->backup_media_type == dvd) {
2754 if ((bkpinfo->backup_media_type == dvd) || ((bkpinfo->media_device = find_cdrom_device(FALSE)) != NULL)) {
2755 mr_asprintf(comment, "Please specify your %s drive's /dev entry", mds);
2756 p = popup_and_get_string("Device?", comment, bkpinfo->media_device);
2757 mr_free(comment);
2758
2759 if (p == NULL) {
2760 log_to_screen("User has chosen not to backup the PC");
2761 finish(1);
2762 }
2763 mr_free(bkpinfo->media_device);
2764 bkpinfo->media_device = p;
2765 }
2766 if (bkpinfo->media_device != NULL) {
2767 log_msg(2, "%s device found at %s", mds, bkpinfo->media_device);
2768 } else {
2769 log_msg(2, "%s device not found (bkpinfo->media_device is NULL)", mds);
2770 }
2771 } else {
2772 if (((bkpinfo->media_device = find_cdrw_device()) != NULL) && (bkpinfo->backup_media_type != usb)) {
2773 mr_free(bkpinfo->media_device);
2774 }
2775 if (bkpinfo->media_device != NULL) {
2776 if (bkpinfo->backup_media_type == usb) {
2777 mr_asprintf(tmp, "I think your %s media corresponds to %s. Is this correct?", mds, bkpinfo->media_device);
2778 } else {
2779 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);
2780 }
2781 if (!ask_me_yes_or_no(tmp)) {
2782 mr_free(bkpinfo->media_device);
2783 }
2784 mr_free(tmp);
2785 }
2786 if (!bkpinfo->media_device) {
2787 if (bkpinfo->backup_media_type == usb) {
2788 p = popup_and_get_string("/dev entry?", "What is the /dev entry of your USB Disk/Key, please?", NULL);
2789 } else {
2790 if (g_kernel_version < 2.6) {
2791 p = popup_and_get_string("Device node?", "What is the SCSI node of your CD (re)writer, please?", NULL);
2792 } else {
2793 p = popup_and_get_string("/dev entry?", "What is the /dev entry of your CD (re)writer, please?", NULL);
2794 }
2795 }
2796 if (p == NULL) {
2797 log_to_screen("User has chosen not to backup the PC");
2798 finish(1);
2799 }
2800 bkpinfo->media_device = p;
2801 }
2802 }
2803 mr_free(mds);
2804
2805 if (bkpinfo->backup_media_type == cdstream) {
2806 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
2807 bkpinfo->media_size[i] = 650;
2808 }
2809 }
2810 break;
2811 case udev:
2812 if (!ask_me_yes_or_no
2813 ("This option is for advanced users only. Are you sure?")) {
2814 log_to_screen("User has chosen not to backup the PC");
2815 finish(1);
2816 }
2817 case tape:
2818
2819 mr_free(bkpinfo->media_device);
2820 if ((!bkpinfo->restore_mode) && ((bkpinfo->media_device = mr_find_tape_device()) == NULL)) {
2821 log_msg(3, "Ok, using vanilla scsi tape.");
2822 mr_asprintf(bkpinfo->media_device, "%s", VANILLA_SCSI_TAPE);
2823 if ((fin = fopen(bkpinfo->media_device, "r"))) {
2824 paranoid_fclose(fin);
2825 } else {
2826 mr_free(bkpinfo->media_device);
2827 mr_asprintf(bkpinfo->media_device, "/dev/osst0");
2828 }
2829 }
2830 if (bkpinfo->media_device != NULL) {
2831 if ((fin = fopen(bkpinfo->media_device, "r"))) {
2832 paranoid_fclose(fin);
2833 } else {
2834 if (does_file_exist(MINDI_CACHE"/mondorestore.cfg")) {
2835 mr_free(bkpinfo->media_device);
2836 bkpinfo->media_device = read_cfg_var(MINDI_CACHE"/mondorestore.cfg", "media-dev");
2837 }
2838 }
2839 }
2840 if (bkpinfo->media_device != NULL) {
2841 mr_asprintf(tmp, "I think I've found your tape streamer at %s; am I right on the money?", bkpinfo->media_device);
2842 if (!ask_me_yes_or_no(tmp)) {
2843 mr_free(bkpinfo->media_device);
2844 }
2845 mr_free(tmp);
2846 }
2847 if (bkpinfo->media_device == NULL) {
2848 p = popup_and_get_string("Device name?", "What is the /dev entry of your tape streamer?", bkpinfo->media_device);
2849 if (p == NULL) {
2850 log_to_screen("User has chosen not to backup the PC");
2851 finish(1);
2852 }
2853 bkpinfo->media_device = p;
2854 }
2855 mr_asprintf(tmp, "ls -l %s", bkpinfo->media_device);
2856 if (run_program_and_log_output(tmp, FALSE)) {
2857 log_to_screen("User has not specified a valid /dev entry");
2858 finish(1);
2859 }
2860 mr_free(tmp);
2861
2862 bkpinfo->use_obdr = ask_me_yes_or_no("Do you want to activate OBDR support for your tapes ?");
2863 bkpinfo->media_size[0] = 0;
2864 log_msg(4, "media_size[0] = %ld", bkpinfo->media_size[0]);
2865 if (bkpinfo->media_size[0] <= 0) {
2866 bkpinfo->media_size[0] = 0;
2867 }
2868 for (i = 1; i <= MAX_NOOF_MEDIA; i++) {
2869 bkpinfo->media_size[i] = bkpinfo->media_size[0];
2870 }
2871 bkpinfo->use_obdr = ask_me_yes_or_no
2872 ("Do you want to activate OBDR support for your tapes ?");
2873 if (bkpinfo->use_obdr) {
2874 log_msg(4, "obdr mode = TRUE");
2875 } else {
2876 log_msg(4, "obdr mode = FALSE");
2877 }
2878 if (archiving_to_media) {
2879 if ((compression_type = which_compression_type()) == NULL) {
2880 log_to_screen("User has chosen not to backup the PC");
2881 finish(1);
2882 }
2883 if ((bkpinfo->compression_level = which_compression_level()) == -1) {
2884 log_to_screen("User has chosen not to backup the PC");
2885 finish(1);
2886 }
2887 }
2888 break;
2889
2890
2891
2892 case netfs:
2893 /* Never try to eject a NETFS device */
2894 bkpinfo->please_dont_eject = TRUE;
2895 /* Force NFS to be the protocol by default */
2896 if (bkpinfo->netfs_proto == NULL) {
2897 mr_asprintf(bkpinfo->netfs_proto, "nfs");
2898 }
2899
2900 /* Initiate bkpinfo netfs_mount path from running environment if not already done */
2901 if (bkpinfo->netfs_mount == NULL) {
2902 bkpinfo->netfs_mount = call_program_and_get_last_line_of_output("mount | grep \":\" | cut -d' ' -f1 | head -n1",TRUE);
2903 }
2904#ifdef __FreeBSD__
2905 if (TRUE)
2906#else
2907 if (!bkpinfo->disaster_recovery)
2908#endif
2909 {
2910 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);
2911 if (p == NULL) {
2912 log_to_screen("User has chosen not to backup the PC");
2913 finish(1);
2914 }
2915 mr_free(bkpinfo->netfs_mount);
2916 bkpinfo->netfs_mount = p;
2917 if (!bkpinfo->restore_data) {
2918 if ((compression_type = which_compression_type()) == NULL) {
2919 log_to_screen("User has chosen not to backup the PC");
2920 finish(1);
2921 }
2922
2923 if ((bkpinfo->compression_level = which_compression_level()) == -1) {
2924 log_to_screen("User has chosen not to backup the PC");
2925 finish(1);
2926 }
2927 }
2928 // check whether already mounted - we better remove
2929 // surrounding spaces and trailing '/' for this
2930 mr_strip_spaces(bkpinfo->netfs_mount);
2931 if (bkpinfo->netfs_mount[strlen(bkpinfo->netfs_mount) - 1] == '/')
2932 bkpinfo->netfs_mount[strlen(bkpinfo->netfs_mount) - 1] = '\0';
2933 q = strchr(bkpinfo->netfs_mount, '@');
2934 if (q != NULL) {
2935 /* User found. Store the 2 values */
2936 q++;
2937 /* new netfs mount */
2938 mr_asprintf(tmp,"%s",q);
2939 } else {
2940 mr_asprintf(tmp,"%s",bkpinfo->netfs_mount);
2941 }
2942 mr_asprintf(command, "mount | grep \"%s \" | cut -d' ' -f3", tmp);
2943 mr_free(tmp);
2944
2945 mr_free(bkpinfo->isodir);
2946 bkpinfo->isodir = call_program_and_get_last_line_of_output(command,TRUE);
2947 mr_free(command);
2948
2949 if (!bkpinfo->restore_data) {
2950 mr_asprintf(comment, "How much data (in Megabytes) will each media store?");
2951 mr_asprintf(tmp, "%d", DEFAULT_DVD_DISK_SIZE);
2952 sz_size = popup_and_get_string("Size", comment, tmp);
2953 mr_free(comment);
2954 mr_free(tmp);
2955 if (sz_size == NULL) {
2956 log_to_screen("User has chosen not to backup the PC");
2957 finish(1);
2958 }
2959 } else {
2960 mr_asprintf(sz_size, "0");
2961 }
2962 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
2963 bkpinfo->media_size[i] = atoi(sz_size);
2964 }
2965 mr_free(sz_size);
2966 if (bkpinfo->media_size[0] < 0) {
2967 log_to_screen("User has chosen not to backup the PC");
2968 finish(1);
2969 }
2970 }
2971 if (bkpinfo->disaster_recovery) {
2972 mr_asprintf(command ,"umount %s/isodir 2> /dev/null", bkpinfo->tmpdir);
2973 paranoid_system(command);
2974 mr_free(command);
2975
2976 }
2977 p = popup_and_get_string("Network protocol", "Which Network protocol should I use (nfs/sshfs/smbfs) ?", bkpinfo->netfs_proto);
2978 if (p == NULL) {
2979 log_to_screen("User has chosen not to backup the PC");
2980 finish(1);
2981 }
2982 mr_free(bkpinfo->netfs_proto);
2983 bkpinfo->netfs_proto = p;
2984
2985 p = popup_and_get_string("Network share", "Which remote Network share should I mount?", bkpinfo->netfs_mount);
2986 if (p == NULL) {
2987 log_to_screen("User has chosen not to backup the PC");
2988 finish(1);
2989 }
2990 mr_free(bkpinfo->netfs_mount);
2991 bkpinfo->netfs_mount = p;
2992
2993 if (bkpinfo->netfs_user) {
2994 strcpy(tmp1, bkpinfo->netfs_user);
2995 } else {
2996 strcpy(tmp1, "");
2997 }
2998 if (!popup_and_get_string("Network user", "Which user should I use if any ?",tmp1)) {
2999 log_to_screen("User has chosen not to backup the PC");
3000 finish(1);
3001 }
3002 mr_free(bkpinfo->netfs_user);
3003 if (strcmp(tmp1, "") != 0) {
3004 mr_asprintf(bkpinfo->netfs_user, "%s", tmp1);
3005 }
3006
3007 /* Initiate bkpinfo isodir path from running environment if mount already done */
3008 mr_free(bkpinfo->isodir);
3009 if (is_this_device_mounted(bkpinfo->netfs_mount)) {
3010 bkpinfo->isodir = call_program_and_get_last_line_of_output("mount | grep \":\" | cut -d' ' -f3 | head -n1",TRUE);
3011
3012 } else {
3013 mr_asprintf(bkpinfo->isodir, "%s/netfsdir", bkpinfo->tmpdir);
3014 mr_asprintf(command, "mkdir -p %s", bkpinfo->isodir);
3015 run_program_and_log_output(command, 5);
3016 mr_free(command);
3017
3018 if (bkpinfo->restore_data) {
3019 /* mount th FS read-only in restore mode to avoid any erase of whatever */
3020 mr_asprintf(tmpro, "-o ro");
3021 } else {
3022 mr_asprintf(tmpro, "");
3023 }
3024
3025 /* Build the mount string */
3026 if (strstr(bkpinfo->netfs_proto, "smbfs")) {
3027 mr_asprintf(tmp, "mount -t cifs %s %s %s",bkpinfo->netfs_mount, bkpinfo->isodir,tmpro);
3028 if (bkpinfo->netfs_user) {
3029 mr_strcat(tmp, " -o user=%s", bkpinfo->netfs_user);
3030 }
3031 else {
3032 if (strstr(bkpinfo->netfs_proto, "sshfs")) {
3033 mr_asprintf(tmp, "sshfs %s ",tmpro);
3034 } else {
3035 mr_asprintf(tmp, "mount -t %s -o nolock %s ", bkpinfo->netfs_proto,tmpro);
3036 }
3037 if (bkpinfo->netfs_user) {
3038 mr_strcat(tmp, "%s@", bkpinfo->netfs_user);
3039 }
3040 mr_strcat(tmp, "%s %s", bkpinfo->netfs_mount, bkpinfo->isodir);
3041 }
3042 run_program_and_log_output(tmp, 3);
3043 mr_free(tmp);
3044
3045 malloc_string(g_selfmounted_isodir);
3046 strcpy(g_selfmounted_isodir, bkpinfo->isodir);
3047 }
3048 }
3049 if (!is_this_device_mounted(bkpinfo->netfs_mount)) {
3050 popup_and_OK("Please mount that partition before you try to backup to or restore from it.");
3051 finish(1);
3052 }
3053 p = popup_and_get_string("Directory", "Which directory within that mountpoint?", bkpinfo->netfs_remote_dir);
3054 if (p == NULL) {
3055 log_to_screen("User has chosen not to backup the PC");
3056 finish(1);
3057 }
3058 mr_free(bkpinfo->netfs_remote_dir);
3059 bkpinfo->netfs_remote_dir = p;
3060
3061 // check whether writable - we better remove surrounding spaces for this
3062 mr_strip_spaces(bkpinfo->netfs_remote_dir);
3063
3064 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);
3065 if (p == NULL) {
3066 log_to_screen("User has chosen not to backup the PC");
3067 finish(1);
3068 }
3069 mr_free(bkpinfo->prefix);
3070 bkpinfo->prefix = p;
3071 log_msg(3, "prefix set to %s", bkpinfo->prefix);
3072
3073 log_msg(3, "Just set netfs_remote_dir to %s", bkpinfo->netfs_remote_dir);
3074 log_msg(3, "isodir is still %s", bkpinfo->isodir);
3075 break;
3076
3077 case iso:
3078 if (!bkpinfo->disaster_recovery) {
3079 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);
3080 if (p == NULL) {
3081 log_to_screen("User has chosen not to backup the PC");
3082 finish(1);
3083 }
3084 bkpinfo->isodir = p;
3085
3086 if (archiving_to_media) {
3087 if ((compression_type = which_compression_type()) == NULL) {
3088 log_to_screen("User has chosen not to backup the PC");
3089 finish(1);
3090 }
3091 if ((bkpinfo->compression_level = which_compression_level()) == -1) {
3092 log_to_screen("User has chosen not to backup the PC");
3093 finish(1);
3094 }
3095 mr_asprintf(tmp, "%d", DEFAULT_DVD_DISK_SIZE);
3096 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 (700) or DVD's (4480) you plan to backup to.", tmp);
3097 mr_free(tmp);
3098 if (p == NULL) {
3099 log_to_screen("User has chosen not to backup the PC");
3100 finish(1);
3101 }
3102 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
3103 bkpinfo->media_size[i] = atoi(p);
3104 }
3105 mr_free(p);
3106 } else {
3107 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
3108 bkpinfo->media_size[i] = 650;
3109 }
3110 }
3111 }
3112 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);
3113 if (p == NULL) {
3114 log_to_screen("User has chosen not to backup the PC");
3115 finish(1);
3116 }
3117 mr_free(bkpinfo->prefix);
3118 bkpinfo->prefix = p;
3119 log_msg(3, "prefix set to %s", bkpinfo->prefix);
3120 break;
3121
3122 default:
3123 fatal_error
3124 ("I, Mojo Jojo, shall defeat those pesky Powerpuff Girls!");
3125 }
3126
3127 if (archiving_to_media) {
3128
3129 mr_free(bkpinfo->boot_device);
3130#ifdef __FreeBSD__
3131#define EXAMPLEBD "/dev/ad0"
3132 bkpinfo->boot_device = call_program_and_get_last_line_of_output("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/\\([0-9]\\).*/\\1/'",TRUE);
3133#else
3134#define EXAMPLEBD "/dev/hda"
3135 bkpinfo->boot_device = call_program_and_get_last_line_of_output("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'",TRUE);
3136#endif
3137 i = which_boot_loader(bkpinfo->boot_device);
3138 if (i == 'U') // unknown
3139 {
3140
3141 p = popup_and_get_string("Boot device", "What is your boot device? (e.g. "EXAMPLEBD")", bkpinfo->boot_device);
3142#undef EXAMPLEBD
3143 if (p == NULL) {
3144 log_to_screen("User has chosen not to backup the PC");
3145 finish(1);
3146 }
3147 mr_free(bkpinfo->boot_device);
3148 bkpinfo->boot_device = p;
3149#ifdef __FreeBSD__
3150 i = which_boot_loader(bkpinfo->boot_device);
3151#else
3152 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "LILO")) {
3153 i = 'L';
3154 } else
3155 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "ELILO")) {
3156 i = 'E';
3157 } else
3158 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "GRUB")) {
3159 i = 'G';
3160 } else {
3161 i = 'U';
3162 }
3163#endif
3164 if (i == 'U') {
3165 if (ask_me_yes_or_no("Unidentified boot loader. Shall I restore it byte-for-byte at restore time and hope for the best?")) {
3166 i = 'R'; // raw
3167 } else {
3168 log_to_screen("I cannot find your boot loader. Please run mondoarchive with parameters.");
3169 finish(1);
3170 }
3171 }
3172 }
3173 bkpinfo->boot_loader = i;
3174 mr_free(bkpinfo->include_paths);
3175 mr_asprintf(p, "/");
3176 bkpinfo->include_paths = p;
3177
3178 p = popup_and_get_string("Backup paths", "Please enter paths which you want me to backup separated by '|'. The default is '/' (i.e. everything).", bkpinfo->include_paths);
3179 if (p == NULL) {
3180 log_to_screen("User has chosen not to backup the PC");
3181 finish(1);
3182 }
3183 mr_free(bkpinfo->include_paths);
3184 bkpinfo->include_paths = p;
3185
3186 tmp = list_of_NETFS_mounts_only();
3187 if (strlen(tmp) > 2) {
3188 mr_strcat(bkpinfo->exclude_paths, "|%s",tmp);
3189 }
3190 mr_free(tmp);
3191// NTFS
3192 tmp = call_program_and_get_last_line_of_output("parted2fdisk -l | grep -i ntfs | awk '{ print $1};' | tr -s '\\n' ' ' | awk '{ print $0};'",TRUE);
3193 if (strlen(tmp) > 2) {
3194 p = popup_and_get_string("NTFS partitions", "Please enter/confirm the NTFS partitions you wish to backup as well.", tmp);
3195
3196 if (p == NULL) {
3197 log_to_screen("User has chosen not to backup the PC");
3198 finish(1);
3199 }
3200 mr_free(bkpinfo->image_devs);
3201 bkpinfo->image_devs = p;
3202 }
3203
3204 p = popup_and_get_string("Exclude paths", "Please enter paths which you do NOT want to backup. Separate them with '|'. NB: /tmp and /proc are always excluded. :-) Just hit 'Enter' if you want to do a full system backup.", bkpinfo->exclude_paths);
3205 if (p == NULL) {
3206 log_to_screen("User has chosen not to backup the PC");
3207 finish(1);
3208 }
3209 mr_free(bkpinfo->exclude_paths);
3210 bkpinfo->exclude_paths = p;
3211 /* Always needs to be finished by a pipe */
3212 mr_strcat(bkpinfo->exclude_paths, "|");
3213
3214 p = popup_and_get_string("Temporary directory", "Please enter your temporary directory.", bkpinfo->tmpdir);
3215 if (p == NULL) {
3216 log_to_screen("User has chosen not to backup the PC");
3217 finish(1);
3218 }
3219 mr_free(bkpinfo->tmpdir);
3220 bkpinfo->tmpdir = p;
3221
3222 p = popup_and_get_string("Scratch directory", "Please enter your scratch directory.", bkpinfo->scratchdir);
3223 if (p == NULL) {
3224 log_to_screen("User has chosen not to backup the PC");
3225 finish(1);
3226 }
3227 mr_free(bkpinfo->scratchdir);
3228 bkpinfo->scratchdir = p;
3229
3230 if (ask_me_yes_or_no("Do you want to backup extended attributes?")) {
3231 if (find_home_of_exe("getfattr")) {
3232 mr_free(g_getfattr);
3233 mr_asprintf(g_getfattr,"getfattr");
3234 }
3235 if (find_home_of_exe("getfacl")) {
3236 mr_free(g_getfacl);
3237 mr_asprintf(g_getfacl,"getfacl");
3238 }
3239 }
3240// Interactive mode:
3241#ifdef __IA64__
3242 bkpinfo->make_cd_use_lilo = TRUE;
3243#else
3244 bkpinfo->make_cd_use_lilo = FALSE;
3245#endif
3246 bkpinfo->backup_data = TRUE;
3247
3248 mr_free(bkpinfo->zip_exe);
3249 mr_free(bkpinfo->zip_suffix);
3250 if (strcmp(compression_type,"lzo") == 0) {
3251 mr_asprintf(bkpinfo->zip_exe, "lzop");
3252 mr_asprintf(bkpinfo->zip_suffix, "lzo");
3253 } else if (strcmp(compression_type,"gzip") == 0) {
3254 mr_asprintf(bkpinfo->zip_exe, "gzip");
3255 mr_asprintf(bkpinfo->zip_suffix, "gz");
3256 } else if (strcmp(compression_type,"lzma") == 0) {
3257 mr_asprintf(bkpinfo->zip_exe, "xy");
3258 mr_asprintf(bkpinfo->zip_suffix, "xy");
3259 } else if (strcmp(compression_type,"bzip2") == 0) {
3260 mr_asprintf(bkpinfo->zip_exe, "bzip2");
3261 mr_asprintf(bkpinfo->zip_suffix, "bz2");
3262 } else {
3263 bkpinfo->zip_exe = NULL;
3264 bkpinfo->zip_suffix = NULL;
3265 }
3266
3267 bkpinfo->verify_data =
3268 ask_me_yes_or_no
3269 ("Will you want to verify your backups after Mondo has created them?");
3270
3271 if (!ask_me_yes_or_no
3272 ("Are you sure you want to proceed? Hit 'no' to abort.")) {
3273 log_to_screen("User has chosen not to backup the PC");
3274 finish(1);
3275 }
3276 } else {
3277 bkpinfo->restore_data = TRUE; // probably...
3278 }
3279 mr_free(compression_type);
3280
3281 if (bkpinfo->backup_media_type == iso
3282 || bkpinfo->backup_media_type == netfs) {
3283 g_ISO_restore_mode = TRUE;
3284 }
3285#ifdef __FreeSD__
3286// skip
3287#else
3288 if (bkpinfo->backup_media_type == netfs) {
3289 log_msg(3, "I think the Remote mount is mounted at %s", bkpinfo->isodir);
3290 }
3291 log_it("isodir = %s", bkpinfo->isodir);
3292 if (bkpinfo->netfs_mount) {
3293 log_it("netfs_mount = '%s'", bkpinfo->netfs_mount);
3294 }
3295 if (bkpinfo->netfs_user) {
3296 log_it("netfs_user = '%s'", bkpinfo->netfs_user);
3297 }
3298 if (bkpinfo->netfs_proto) {
3299 log_it("netfs_proto = '%s'", bkpinfo->netfs_proto);
3300 }
3301#endif
3302
3303 log_it("media device = %s", bkpinfo->media_device);
3304 log_it("media size = %ld", bkpinfo->media_size[1]);
3305 tmp = bkptype_to_string(bkpinfo->backup_media_type);
3306 log_it("media type = %s", tmp);
3307 mr_free(tmp);
3308
3309 if (bkpinfo->prefix) {
3310 log_it("prefix = %s", bkpinfo->prefix);
3311 }
3312 log_it("compression = %ld", bkpinfo->compression_level);
3313 log_it("exclude_path = %s", bkpinfo->exclude_paths);
3314 log_it("include_path = %s", bkpinfo->include_paths);
3315
3316 /* Handle devices passed in bkpinfo and print result */
3317 /* the mr_make_devlist_from_pathlist function appends
3318 * to the *_paths variables so copy before */
3319 mr_asprintf(tmp, "%s|", bkpinfo->exclude_paths);
3320 mr_make_devlist_from_pathlist(tmp, 'E');
3321 mr_free(tmp);
3322 mr_asprintf(tmp, "%s|", bkpinfo->include_paths);
3323 mr_make_devlist_from_pathlist(tmp, 'I');
3324 mr_free(tmp);
3325
3326 log_it("scratchdir = '%s'", bkpinfo->scratchdir);
3327 log_it("tmpdir = '%s'", bkpinfo->tmpdir);
3328 if (bkpinfo->image_devs) {
3329 log_it("image_devs = '%s'", bkpinfo->image_devs);
3330 }
3331 log_it("boot_device = '%s' (loader=%c)", bkpinfo->boot_device, bkpinfo->boot_loader);
3332 if (bkpinfo->media_size[0] < 0) {
3333 if (archiving_to_media) {
3334 fatal_error("Media size is less than zero.");
3335 } else {
3336 log_msg(2, "Warning - media size is less than zero.");
3337 bkpinfo->media_size[0] = 0;
3338 }
3339 }
3340 paranoid_free(sz_size);
3341 return (0);
3342}
3343
3344
3345/* @} - end of deviceGroup */
Note: See TracBrowser for help on using the repository browser.