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

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

Rewrite find_tape_device ans start for other find_*dev functions

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