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

Last change on this file since 3148 was 3148, checked in by Bruno Cornec, 7 years ago

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