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

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

svn merge -r 2849:2935 ../3.0

  • Adds 3 binaries called potentially by udev o support USB key mount at restore time (Victor Gattegno)
  • mindi only sets the boot flag of a USB device if none is present already (should avoid flip/flop situation)
  • Fix a compatibility bug in analyze-my-lvm whete exclude lists with '|' was incorrectly analyzed (' ' still expected)
  • Precise that -J is not supported by mondorestore yet.
  • Fix a compatibility bug in mindi where exclude lists with '|' was incorrectly analyzed (' ' still expected)
  • Install an MBR on USB devices when possible
  • Improve error message in case mondorestore isn't found
  • Adds support for more virtio devices for KVM restore
  • Improve mbr.bin handling
  • Fix an issue in test of FindKernel? for Xen and non Xen cases
    • Fix #492 by removing an extra useless fi
    • Move fvat nd fat modules to CDROM_MODS in order to have them in the initrd to support saving logs to a USB device e.g.
    • Fix an error when dmidecode reports comments before system name
    • Fix previous broken patch on comment management for bkphw
    • Improve msgs printed during kbd analysis
    • Fix a small remaining pb with new comment support
    • Try to again improve the mount/umount stuff in mondo -Adds mention to OEL, CentOS, ScientifLinux? as RHEL clones
  • Fix a panadoid_free done on the wrong variable (solve a memory leak)
  • Solve #495 by skipping the potential (auto-read-only) when analyzin /proc/mdstat result
  • Fix some markup in man pages
  • adds the post-nuke script sample for Debian (Guillaume Pernot/Andrée? Leidenfrost)
  • Avoid a useless umount when changing of afio set, let insist_on_cd check and perform if needed
  • Copy everything under .ssh of root account to allow for sshfs mount without passwd at restore time.
  • Adds support for bnx2i driver
  • Try to fix #496 by supporting the protocol in the output of the mount command (sshfs#...)
  • Uses the -d option of the mount command to avoid leaving loop devices in use. Probably provoked by the usage of a more recent busybox, but anyway that option doesn't hurt, and does nothing for non-loop devices. Should fix the problems reported on the ML with NFS and more than 8 ISO images (8 being the default number of loop devices available)
  • Attempt to fix #436 by adding some newtDrawForm where appropriate.
  • Fix bugs in the kernel detection routine of mindi (some echo were done without capturing their output, confusing the return). This should also help the Xen detection issue
  • Change the logging function to be nearer from 2.2.10, and start using some
  • Initialize some global fields with an empty string, now that newt Draws more regularly progress forms
  • Mindi progression still not good, but showing now 3% when calling mindi. Would need a specific handling for the progress bar to evolve more afterwards
  • Add compatibility with the latest SSSTK + PSP content for HP ProLiant? + branding homogeneous now. We only take conrep out of the SSSTK pending a package availability. Using hpacuscripting now. Review of parameters called following test made.
  • Fix #501 by pushing BOOT_SIZE to 36864 for certain Ubuntu cases
  • Fix some remaining inconsistencis in the ProLiant? HW backup. ready for usage.
  • Fix an issue on udevadm call (needs an intermediate variable)
  • Fix a remaining ProLiant? tool call
  • Remove again some wrong variable initialization for Net in order to not have strange messages now we call Draw more often
  • Adds support for hwaddr (MAC addresses) at restore time, and map to the correct NIC if it's found at restore time (based on code from Stefan Heijmans)
  • Try to Fix #499 by changing init in order to never exit, and rebooting using sysrq in /proc (not portable). Maybe should move to using a real init, and a separated rcS script.
  • Fixes the shell msg error by using setsid as per Busybox FAQ
  • Fix #491 by correctly checking Xen kernel used for a dom0 env (apollo13)
  • Improves logging in Die()
  • Improve again ProLiant? HW support, by adding references to the yet to come SSSTK rpm so that conrep is called at its future right place.
  • Improve mindi support for kernel 3.x
  • Fix #499 for good, by using a real init from busybox and moving our init script to etc/init.d/rcS. We also now call the reboot from busybox to exit, which indeed reset the system correctly (in a VM at least which wasn't the case before). Reauires a new mindi-busybox as well.
  • Attempt to solve #503 by handling /lib64 separately for udev and only if not a link (to /lib) for Ubuntu 64
  • Improve again previous tests for bug #503
  • Improve -d option description for NFS
  • Allow http download for ftp content
  • New italian mirror
  • Fix #505 by documenting environment variables
    • Fix a bug where on OBDR mode compression was turned off to create the OBDR content but not back on (report from the mailing list)
  • solve http://trac.mondorescue.org/ticket/520 by Victor Gattegno
  • Fix #524 as option -U needs a size also
  • Removes useless gendf option
  • Fix #511 now stabgrub is called also after conf file modifications
  • Adds support for SUID binaries
  • remove a useless xenkernelname variable from mindi
  • Should fix #512 by clearing old scratchdir before changing to a new
  • Simplify the function engaging SCSI for Smart Array controllers, and support as many controller as we have
  • Add some binaries used on SLES 10 by udev
  • Fix a syntax issue in newt-specific.c
  • Fix a memory free bug where a used variable was freed to early
  • Fix a memory free bug where a used variable was misused in asprintf/free
  • Fix #498 by using an external program (hpsa_obdr_mode) which can set the mode of the tape to CD-ROM or Sequential at will.

That program will have to be downloaded from http://cciss.sf.net

  • Adds support for the route command in mindi-busybox
  • Force block size back to 32768 in OBDR mode after skipping the 2 tape marks, to read the additional rootfs data corerctly - Fix #498 for good it seems
  • Adds support for a new pata_atiixp driver
  • Fix #525 and improve printing when no /etc/raidtab, but still md devices
  • Exclude /run similarly to /sys and /proc
  • Fix a bug in mindi when having multiple swap partiions with same starting string (/dev/dm-1 and /dev/dm-16). We now add a space in the grep to avoid mischoice.
  • Fix grub installation if we had to go in the hack part which wasn't using the correct variables to make decisions !
  • Remove all mention of mondorescue.com from code
  • Fix some website typo on the download page and precise that we now develop on Mageia
  • Exclude sysfs devpts and tmpfs partitions found in RHEL 6.2 from analyze by mindi (made error messages)
  • Force activation of first partition which should not be optional as everything is destroyed before
  • Fix a bug in analyze-my-lvm where LVs were created in random order (the one of lvscan) instead of using the dm-xxx order, which creates issues at restore time between the mapping created then and the one we had at backup time which could be inconssistent

-mondoarchive now exists if the mt command is missing when needed

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