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

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