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

Last change on this file since 2696 was 2696, checked in by Bruno Cornec, 13 years ago
  • Adds preliminary support for hpacucli. Needs test
  • Fix a compilation issue due to bad fatal_error call (no param allowed)
  • Fix some HTML syntax issues in the docs page + link to Wiki articles
  • Boot size pushed to 20MB
  • A bit more precise for error msg from PBDI
  • Exits if the protocol used is not recognized (error with -n fs:// typo)
  • Adds support for r8169 net driver
  • Fix a cast issue
  • Precise a test case to avoid ambiguity
  • Die and related functions placed before any other code as used early, and ash doesn't support function declaration. So will solve #446 by exiting mindi before doing anything weird in it.
  • Fix #445 with another technical solution to avoid vg with similar names to b wrongly excluded (vgroot and vgroot-san e.g.) (Michael Shapiro)
  • Fix a bug on exclusion of path with common content (/home and /path/home e.g.) which was handled correctly only in a certain order (John Pearson <johnp_at_gtagalenco.com.au>)
  • mount-media function is now placed in libmondo-devices.c as used by more programs.
  • Avoids calling mount_media when it's not mandatory (change the way mondorestore was working up to now but could solve some bugs reported)
  • Add xhci support to mindi.
  • Rewrite Xen kernel support to use TryToFindKernelPath systematically.
  • mindi now copies also the /lib/firmware content in order to have it for drivers needing it (bnx2 reported)
  • Push MAX_STR_LEN to 512 to allow supporting more exclude dir (2.2.10 and dyn allocation is the way to go there)
  • Add virtio driver to mindi
  • Initialize extra_cdrom_params in any case tp avoid passing null to cdrecord later on
  • Also escapes white spaces in gen_aux_list to avoid issues in getfacl call
  • Adds multiple missing drivers to mindi (mega_sr, ide_gd_mod, pata_jmicron, cp210x, dca, raid6_pq, xor async_tx, async_memcpy, async_xor)
  • Fix #434 by really testing thet udevd is not running already (chucky)
  • Adds support of pata_sil680 driver
  • Fix again #412 !! by removing calls to tee which voids the return value of the previous command
  • The way grub.unsupported was called for opensuse 11.2 was wrong. It should be done bfore calling grub-install which also exists. And tested for existence. Now this should fix #412.
  • Try to provide a workaround in code to the #401 (over-allocation of space due to rounding errors)
  • Fix a bug when using ACLs and file with spaces in their names by adding double quotes in getfacl invocation (Tom Mortell tomm_at_dslextreme.com). Also adding the same quotes on the touch commands made earlier.
  • Points to the current latest presentation instead of the old one
  • mondo-ref-card added to the docs web page
  • Add the mondoarchive reference card (Lester Wade)
  • Change useless comments for more useful ones
  • mindi is now able to handle compressed kernel with .gz or .bz2 suffix (case of OpenSuSE 11.2)
  • Improves logging for external binary
  • Improve Xen kernel detection and avoid false detection (Michael Shapiro)
  • Updated P2V doc from Lester Wade (lester.wade_at_hp.com)

Baclports from 2.2.9
svn merge -r 2650:2695 svn+ssh://bruno@svn.mondorescue.org/mondo/svn/mondorescue/branches/2.2.9 .

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