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

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