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

Last change on this file since 2715 was 2715, checked in by Bruno Cornec, 13 years ago
  • Document in man page and HOWTO the previous syntax modifications for -E/-I options and '|' separator
  • Fix #444. mondoarchive now supports inclusion/exclusion of dirs with spaces in their name. this required a syntax change for the -E/-I options where the list of dirs needs to be separated by '|' now instead of ' '.
  • Fix issus with mindi when launched from a dir containing spaces
  • Fix a but when no -E option is give on CLI (null) string generated
  • Fix #459 by correcting an old README containing info on the edit-mountlist binary which doesn't exist.

svn merge -r 2709:2714 svn+ssh://bruno@svn.mondorescue.org/mondo/svn/mondorescue/branches/2.2.9 .

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