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

Last change on this file since 3859 was 3859, checked in by Bruno Cornec, 2 months ago

Fix build_partition_name to allocate dynamically and change proto accordingly

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