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

Last change on this file since 2850 was 2850, checked in by bruno, 6 years ago

svn merge -r 2773:2849 2.2.9 in 2.2.10

  • Adds 3 binaries called potentially by udev o support USB key mount at restore time (Victor Gattegno)
  • Really support both mkisofs and genisoimage everywhere
  • Try to handle netfs_user better in all cases (NFS and SSHFS)
    • Improve logging in init script
    • Format improvement
    • Removes a warning when trying to launch udevadm and it doesn't exist (RHEL 5 e.g.)
    • Fix syntax description in mondoarchive man page for -E & -I with |
  • Adds download entries for new distro supported (Mageia, Fedora 15, Ubuntu 11.04)

-Fix mindi-get-perl-modules when perl dirs in @INC are symlinks (case on Ubuntu 11.04)

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