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

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

Fix find_dvd_device by removing useless second param and using wodim -inq

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