source: MondoRescue/branches/3.3/mondo/src/common/libmondo-devices.c@ 3873

Last change on this file since 3873 was 3872, checked in by Bruno Cornec, 3 months ago

Rewrite find_dvd_device

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