source: MondoRescue/branches/3.2/mondo/src/common/libmondo-devices.c@ 3379

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