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

Last change on this file since 3867 was 3867, checked in by Bruno Cornec, 4 months ago

Fix find_home_of_exe calls + errors

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