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

Last change on this file since 3610 was 3610, checked in by Bruno Cornec, 7 years ago
  • Change interfaces for the call_program_and_get_last_line_of_output and where_is_root_mounted(now return dynamically allocated string)
  • removes useless function store_netfs_config
  • Property svn:keywords set to Id
File size: 98.2 KB
Line 
1/* libmondo-devices.c Subroutines for handling devices
2 $Id: libmondo-devices.c 3610 2016-11-05 17:12:23Z 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 3610 2016-11-05 17:12:23Z 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 tmp = 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 tmp = 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\" "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 tmp1 = 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 tmp = 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 tmp = 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 > /dev/null", device_raw);
1074 if (system(tmp) == 0) {
1075 retval = TRUE;
1076 }
1077 mr_free(tmp);
1078 return(retval);
1079}
1080
1081/* previous C version replaced by a call to a perl script, fixing issues with symlinks between devices - RHEL7 e.g.
1082 FILE *fin;
1083
1084 char *incoming = NULL;
1085 char *device_with_tab = NULL;
1086 char *device_with_space = NULL;
1087 char *tmp = NULL;
1088 bool retval = FALSE;
1089
1090#ifdef __FreeBSD__
1091#define SWAPLIST_COMMAND "swapinfo"
1092#else
1093#define SWAPLIST_COMMAND "cat /proc/swaps"
1094#endif
1095
1096
1097 if (device_raw == NULL) {
1098 return(FALSE);
1099 }
1100
1101 if (device_raw[0] != '/' && !strstr(device_raw, ":/")) {
1102 log_msg(1, "%s needs to have a '/' prefixed - I'll do it", device_raw);
1103 mr_asprintf(tmp, "/%s", device_raw);
1104 } else {
1105 mr_asprintf(tmp, "%s", device_raw);
1106 }
1107 log_msg(1, "Is %s mounted?", tmp);
1108 if (!strcmp(tmp, "/proc") || !strcmp(tmp, "proc")) {
1109 log_msg(1, "I don't know how the heck /proc made it into the mountlist. I'll ignore it.");
1110 mr_free(tmp);
1111 return(FALSE);
1112 }
1113 mr_asprintf(device_with_tab, "%s\t", tmp);
1114 mr_asprintf(device_with_space, "%s ", tmp);
1115 mr_free(tmp);
1116
1117 if (!(fin = popen("mount", "r"))) {
1118 log_OS_error("Cannot popen 'mount'");
1119 return(FALSE);
1120 }
1121
1122 for (mr_getline(incoming, fin); !feof(fin); mr_getline(incoming, fin)) {
1123 if (strstr(incoming, device_with_space) || strstr(incoming, device_with_tab)) {
1124 paranoid_pclose(fin);
1125 mr_free(incoming);
1126 return(TRUE);
1127 }
1128 mr_free(incoming);
1129 }
1130 mr_free(incoming);
1131 mr_free(device_with_tab);
1132 paranoid_pclose(fin);
1133
1134 mr_asprintf(tmp, "%s | grep -E \"^%s\" > /dev/null 2> /dev/null", SWAPLIST_COMMAND, device_with_space);
1135 mr_free(device_with_space);
1136 log_msg(4, "tmp (command) = '%s'", tmp);
1137 if (!system(tmp)) {
1138 retval = TRUE;
1139 }
1140 mr_free(tmp);
1141 return(retval);
1142}
1143*/
1144
1145#ifdef __FreeBSD__
1146// CODE IS FREEBSD-SPECIFIC
1147/**
1148 * Create a loopback device for specified @p fname.
1149 * @param fname The file to associate with a device.
1150 * @return /dev entry for the device, or NULL if it couldn't be allocated.
1151 */
1152char *make_vn(char *fname)
1153{
1154 char *device = (char *) malloc(MAX_STR_LEN);
1155 char *mddevice = NULL;
1156 char *command = NULL;
1157 char *tmp = NULL;
1158 int vndev = 2;
1159
1160 tmp = call_program_and_get_last_line_of_output("/sbin/sysctl -n kern.osreldate");
1161 if (atoi(tmp) < 500000) {
1162 do {
1163 mr_asprintf(mddevice, "vn%ic", vndev++);
1164 mr_free(command);
1165 mr_asprintf(command, "vnconfig %s %s", mddevice, fname);
1166 if (vndev > 10) {
1167 mr_free(tmp);
1168 mr_free(mddevice);
1169 mr_free(command);
1170 return NULL;
1171 }
1172 }
1173 while (system(command));
1174 } else {
1175 mr_asprintf(command, "mdconfig -a -t vnode -f %s", fname);
1176 mddevice = call_program_and_get_last_line_of_output(command);
1177 if (!strstr(mddevice, "md")) {
1178 mr_free(tmp);
1179 mr_free(command);
1180 mr_free(mddevice);
1181 return NULL;
1182 }
1183 }
1184 mr_free(tmp);
1185 mr_free(command);
1186 sprintf(device, "/dev/%s", mddevice);
1187 mr_free(mddevice);
1188 return device;
1189}
1190
1191
1192
1193// CODE IS FREEBSD-SPECIFIC
1194/**
1195 * Deallocate specified @p dname.
1196 * This should be called when you are done with the device created by make_vn(),
1197 * so the system does not run out of @c vn devices.
1198 * @param dname The device to deallocate.
1199 * @return 0 for success, nonzero for failure.
1200 */
1201int kick_vn(char *dname)
1202{
1203 char *command = NULL;
1204 char *tmp = NULL;
1205 int res = 0;
1206
1207 if (strncmp(dname, "/dev/", 5) == 0) {
1208 dname += 5;
1209 }
1210
1211 tmp = call_program_and_get_last_line_of_output("/sbin/sysctl -n kern.osreldate");
1212 if (atoi(tmp) < 500000) {
1213 mr_asprintf(command, "vnconfig -d %s", dname);
1214 } else {
1215 mr_asprintf(command, "mdconfig -d -u %s", dname);
1216 }
1217 mr_free(tmp);
1218 res = system(command);
1219 mr_free(command);
1220 return(res);
1221}
1222#endif
1223
1224
1225/**
1226 * Mount the CD-ROM at @p mountpoint.
1227 * @param device The device (or file if g_ISO_restore_mode) to mount.
1228 * @param mountpoint The place to mount it.
1229 * @return 0 for success, nonzero for failure.
1230 */
1231int mount_USB_here(char *device, char *mountpoint)
1232{
1233 /*@ buffer ****************************************************** */
1234 char *command = NULL;
1235 int retval;
1236
1237 assert_string_is_neither_NULL_nor_zerolength(device);
1238 assert_string_is_neither_NULL_nor_zerolength(mountpoint);
1239
1240 make_hole_for_dir(mountpoint);
1241 if (isdigit(device[0])) {
1242 return(1);
1243 }
1244 log_msg(4, "(mount_USB_here --- device=%s, mountpoint=%s", device, mountpoint);
1245
1246#ifdef __FreeBSD__
1247 mr_asprintf(command, "mount_vfat %s %s 2>> %s", device, mountpoint, MONDO_LOGFILE);
1248
1249#else
1250 mr_asprintf(command, "mount %s -t vfat %s 2>> %s", device, mountpoint, MONDO_LOGFILE);
1251#endif
1252
1253 log_msg(4, command);
1254 retval = system(command);
1255 log_msg(1, "system(%s) returned %d", command, retval);
1256 mr_free(command);
1257
1258 return (retval);
1259}
1260
1261/**
1262 * Mount the CD-ROM at @p mountpoint.
1263 * @param device The device (or file if g_ISO_restore_mode) to mount.
1264 * @param mountpoint The place to mount it.
1265 * @return 0 for success, nonzero for failure.
1266 */
1267int mount_CDROM_here(char *device, const char *mountpoint)
1268{
1269 /*@ buffer ****************************************************** */
1270 char *command = NULL;
1271 int retval;
1272#ifdef __FreeBSD__
1273 char *dev = NULL;
1274#else
1275 char *options = NULL;
1276#endif
1277
1278 assert_string_is_neither_NULL_nor_zerolength(device);
1279 assert_string_is_neither_NULL_nor_zerolength(mountpoint);
1280
1281 make_hole_for_dir(mountpoint);
1282
1283 if (isdigit(device[0])) {
1284 find_cdrom_device(device, FALSE);
1285 }
1286#ifndef __FreeBSD__
1287 mr_asprintf(options, "ro");
1288#endif
1289
1290 if (g_ISO_restore_mode) {
1291
1292#ifdef __FreeBSD__
1293 mr_asprintf(dev, "%s", make_vn(device));
1294 if (!dev) {
1295 mr_asprintf(command, "Unable to mount ISO (make_vn(%s) failed)", device);
1296 fatal_error(command);
1297 }
1298 strcpy(device, dev);
1299 paranoid_free(dev);
1300#else
1301 mr_strcat(options, ",loop");
1302#endif
1303
1304 }
1305 log_msg(4, "(mount_CDROM_here --- device=%s, mountpoint=%s", device, mountpoint);
1306 /*@ end vars *************************************************** */
1307
1308#ifdef __FreeBSD__
1309 mr_asprintf(command, "mount_cd9660 -r %s %s 2>> %s", device, mountpoint, MONDO_LOGFILE);
1310
1311#else
1312 mr_asprintf(command, "mount %s -o %s -t iso9660 %s 2>> %s", device, options, mountpoint, MONDO_LOGFILE);
1313 paranoid_free(options);
1314#endif
1315
1316 log_msg(4, command);
1317 if (strncmp(device, "/dev/", 5) == 0) {
1318 retract_CD_tray_and_defeat_autorun();
1319 }
1320 retval = system(command);
1321 log_msg(1, "system(%s) returned %d", command, retval);
1322 mr_free(command);
1323
1324 return (retval);
1325}
1326
1327
1328/**
1329* Mount the CD-ROM or USB device at /mnt/cdrom.
1330* @param bkpinfo The backup information structure. Fields used:
1331* - @c bkpinfo->backup_media_type
1332* - @c bkpinfo->disaster_recovery
1333* - @c bkpinfo->isodir
1334* - @c bkpinfo->media_device
1335* @return 0 for success, nonzero for failure.
1336*/
1337int mount_media()
1338{
1339char *mount_cmd = NULL;
1340char *mountdir = NULL;
1341int i, res;
1342#ifdef __FreeBSD__
1343 char mdd[32];
1344 char *mddev = mdd;
1345#endif
1346
1347 if (bkpinfo->backup_media_type == tape || bkpinfo->backup_media_type == udev) {
1348 log_msg(8, "Tape/udev. Therefore, no need to mount a media.");
1349 return 0;
1350 }
1351
1352 if (!run_program_and_log_output("mount | grep -F " MNT_CDROM, FALSE)) {
1353 log_msg(2, "mount_media() - media already mounted. Fair enough.");
1354 return (0);
1355 }
1356
1357 if (bkpinfo->backup_media_type == netfs) {
1358 log_msg(2, "Mounting for Network thingy");
1359 log_msg(2, "isodir = %s", bkpinfo->isodir);
1360 if ((!bkpinfo->isodir[0] || !strcmp(bkpinfo->isodir, "/")) && am_I_in_disaster_recovery_mode()) {
1361 strcpy(bkpinfo->isodir, "/tmp/isodir");
1362 log_msg(1, "isodir is being set to %s", bkpinfo->isodir);
1363 }
1364#ifdef __FreeBSD__
1365 if (bkpinfo->netfs_remote_dir != NULL) {
1366 // NETFS
1367 mr_asprintf(mount_cmd, "/mnt/isodir/%s/%s/%s-%d.iso", bkpinfo->isodir, bkpinfo->netfs_remote_dir, bkpinfo->prefix, g_current_media_number);
1368 } else {
1369 // ISO
1370 mr_asprintf(mount_cmd, "/mnt/isodir/%s/%s-%d.iso", bkpinfo->isodir, bkpinfo->prefix, g_current_media_number);
1371 }
1372 mddev = make_vn(mount_cmd);
1373 mr_free(mount_cmd);
1374
1375 mr_asprintf(mount_cmd, "mount_cd9660 -r %s " MNT_CDROM, mddev);
1376#else
1377 if (bkpinfo->netfs_remote_dir != NULL) {
1378 // NETFS
1379 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);
1380 } else {
1381 // ISO
1382 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);
1383 }
1384#endif
1385
1386 } else if (bkpinfo->backup_media_type == iso) {
1387 if (bkpinfo->subdir) {
1388 mr_asprintf(mountdir, "%s/%s", bkpinfo->isodir, bkpinfo->subdir);
1389 } else {
1390 mr_asprintf(mountdir, "%s", bkpinfo->isodir);
1391 }
1392#ifdef __FreeBSD__
1393 mr_asprintf(mount_cmd, "%s/%s-%d.iso", mountdir, bkpinfo->prefix, g_current_media_number);
1394 mddev = make_vn(mount_cmd);
1395 mr_free(mount_cmd);
1396
1397 mr_asprintf(mount_cmd, "mount_cd9660 -r %s %s", mddev, MNT_CDROM);
1398#else
1399 mr_asprintf(mount_cmd, "mount %s/%s-%d.iso -t iso9660 -o loop,ro %s", mountdir, bkpinfo->prefix, g_current_media_number, MNT_CDROM);
1400#endif
1401 mr_free(mountdir);
1402 } else if (bkpinfo->backup_media_type == usb) {
1403 mr_asprintf(mount_cmd, "mount -t vfat %s %s", bkpinfo->media_device, MNT_CDROM);
1404 } else if (strstr(bkpinfo->media_device, "/dev/")) {
1405#ifdef __FreeBSD__
1406 mr_asprintf(mount_cmd, "mount_cd9660 -r %s %s", bkpinfo->media_device, MNT_CDROM);
1407#else
1408 mr_asprintf(mount_cmd, "mount %s -t iso9660 -o ro %s", bkpinfo->media_device, MNT_CDROM);
1409#endif
1410 } else {
1411 if (bkpinfo->disaster_recovery
1412 && does_file_exist("/tmp/CDROM-LIVES-HERE")) {
1413 strcpy(bkpinfo->media_device,
1414 last_line_of_file("/tmp/CDROM-LIVES-HERE"));
1415 } else {
1416 find_cdrom_device(bkpinfo->media_device, TRUE);
1417 }
1418
1419#ifdef __FreeBSD__
1420 mr_asprintf(mount_cmd, "mount_cd9660 -r %s %s", bkpinfo->media_device, MNT_CDROM);
1421#else
1422 mr_asprintf(mount_cmd, "mount %s -t iso9660 -o ro %s", bkpinfo->media_device, MNT_CDROM);
1423#endif
1424 }
1425
1426 log_msg(2, "(mount_media) --- command = %s", mount_cmd);
1427 for (i = 0; i < 2; i++) {
1428 res = run_program_and_log_output(mount_cmd, FALSE);
1429 if (!res) {
1430 break;
1431 } else {
1432 log_msg(2, "Failed to mount device.");
1433 sleep(5);
1434 sync();
1435 }
1436 }
1437 mr_free(mount_cmd);
1438
1439 if (res) {
1440 log_msg(2, "Failed, despite %d attempts", i);
1441 } else {
1442 log_msg(2, "Mounted media drive OK");
1443 }
1444 return (res);
1445}
1446/**************************************************************************
1447*END_MOUNT_CDROM *
1448**************************************************************************/
1449
1450
1451
1452
1453/**
1454 * Ask the user for CD number @p cd_number_i_want.
1455 * Sets g_current_media_number once the correct CD is inserted.
1456 * @param bkpinfo The backup information structure. Fields used:
1457 * - @c bkpinfo->backup_media_type
1458 * - @c bkpinfo->prefix
1459 * - @c bkpinfo->isodir
1460 * - @c bkpinfo->media_device
1461 * - @c bkpinfo->please_dont_eject_when_restoring
1462 * @param cd_number_i_want The CD number to ask for.
1463 */
1464void
1465insist_on_this_cd_number(int cd_number_i_want)
1466{
1467
1468 /*@ int ************************************************************* */
1469 int res = 0;
1470
1471
1472 /*@ buffers ********************************************************* */
1473 char *tmp = NULL;
1474 char *mds = NULL;
1475 char *request = NULL;
1476
1477 assert(bkpinfo != NULL);
1478 assert(cd_number_i_want > 0);
1479
1480// log_msg(3, "Insisting on CD number %d", cd_number_i_want);
1481
1482 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1483 log_msg(3,
1484 "No need to insist_on_this_cd_number when the backup type isn't CD-R(W) or NFS or ISO");
1485 return;
1486 }
1487 mr_asprintf(tmp, "mkdir -p " MNT_CDROM);
1488 run_program_and_log_output(tmp, 5);
1489 mr_free(tmp);
1490
1491 if (g_ISO_restore_mode || bkpinfo->backup_media_type == iso || bkpinfo->backup_media_type == netfs) {
1492 g_ISO_restore_mode = TRUE;
1493 }
1494 if ((res = what_number_cd_is_this()) != cd_number_i_want) {
1495 log_msg(3, "Currently, we hold %d but we want %d", res, cd_number_i_want);
1496
1497 /* Now we need to umount the current media to have the next mounted after */
1498 run_program_and_log_output("umount -d " MNT_CDROM, FALSE);
1499 log_msg(3, "Mounting next media %d",cd_number_i_want);
1500 g_current_media_number = cd_number_i_want;
1501 mount_media();
1502
1503 mds = media_descriptor_string(bkpinfo->backup_media_type);
1504 log_msg(3, "Insisting on %s #%d", mds, cd_number_i_want);
1505 mr_asprintf(request, "Please insert %s #%d and press Enter.", mds, cd_number_i_want);
1506 mr_free(mds);
1507
1508 while (what_number_cd_is_this() != cd_number_i_want) {
1509 sync();
1510 if (is_this_device_mounted(MNT_CDROM)) {
1511 res =
1512 run_program_and_log_output("umount -d " MNT_CDROM, FALSE);
1513 } else {
1514 res = 0;
1515 }
1516 if (res) {
1517 log_to_screen("WARNING - failed to unmount CD-ROM drive");
1518 }
1519 if (!bkpinfo->please_dont_eject) {
1520 res = eject_device(bkpinfo->media_device);
1521 } else {
1522 res = 0;
1523 }
1524 if (res) {
1525 log_to_screen("WARNING - failed to eject CD-ROM disk");
1526 }
1527 popup_and_OK(request);
1528 if (!bkpinfo->please_dont_eject) {
1529 inject_device(bkpinfo->media_device);
1530 }
1531 sync();
1532 }
1533 mr_free(request);
1534
1535 log_msg(1, "Thankyou. Proceeding...");
1536 g_current_media_number = cd_number_i_want;
1537 }
1538}
1539
1540/* @} - end of deviceGroup */
1541
1542
1543
1544/**
1545 * Creates a singly linked list of all of the non-NETFS mounted file systems.
1546 * @param DSFptr A pointer to the structure MOUNTED_FS_STRUCT used to hold
1547 * the list of mounted file systems.
1548 * @return None.
1549 */
1550static void add_mounted_fs_struct (MOUNTED_FS_STRUCT *DSFptr)
1551{
1552 assert (DSFptr);
1553 if (DSF_Head == NULL) {
1554 DSF_Head = DSFptr;
1555 } else {
1556 DSF_Tail->next = DSFptr;
1557 }
1558 DSFptr->next = NULL;
1559 DSF_Tail = DSFptr;
1560}
1561
1562/**
1563 * Find the structure, in the singly linked list of all of the non-NETFS
1564 * mounted file systems, that contains the specified device.
1565 * @param device The device to find
1566 * @return NULL if it didn't find the device, a pointer to the
1567 * structure if it did.
1568 */
1569static MOUNTED_FS_STRUCT *find_device_in_list (char *device)
1570{
1571 MOUNTED_FS_STRUCT *DSFptr = NULL;
1572
1573 DSFptr = DSF_Head;
1574 while (DSFptr != NULL) {
1575 if (!strcmp(DSFptr->device, device)) {
1576 break;
1577 }
1578 DSFptr = DSFptr->next;
1579 }
1580 return (DSFptr);
1581}
1582
1583/**
1584 * Find the structure, in the singly linked list of all of the non-NETFS
1585 * mounted file systems, that contains the specified mount point.
1586 * @param mount_point The mount point to find
1587 * @return NULL is it didn't find the mount point, a pointer to the
1588 * structure if it did.
1589 */
1590static MOUNTED_FS_STRUCT *find_mount_point_in_list (char *mount_point)
1591{
1592 MOUNTED_FS_STRUCT *DSFptr = NULL;
1593
1594 DSFptr = DSF_Head;
1595 while (DSFptr != NULL) {
1596 if (!strcmp(DSFptr->mount_point, mount_point)) {
1597 break;
1598 }
1599 DSFptr = DSFptr->next;
1600 }
1601 return (DSFptr);
1602}
1603
1604/**
1605 * Frees the memory for all of the structures on the linked list of
1606 * all of the non-NETFS mounted file systems.
1607 */
1608static void free_mounted_fs_list (void) {
1609 MOUNTED_FS_STRUCT *DSFptr = NULL;
1610 MOUNTED_FS_STRUCT *DSFnext = NULL;
1611
1612 DSFptr = DSF_Head;
1613 while (DSFptr != NULL) {
1614 DSFnext = DSFptr->next;
1615 paranoid_free(DSFptr);
1616 DSFptr = DSFnext;
1617 }
1618 DSF_Head = NULL;
1619 DSF_Tail = NULL;
1620}
1621
1622
1623/**
1624 * Creates a linked list of all of the non-NETFS mounted file systems.
1625 * We use a linked list because we don't know how many mounted file
1626 * there are (and there can be a lot).
1627 * @return 0 on success and greated than 0 on failure.
1628 */
1629static int create_list_of_non_NETFS_mounted_file_systems (void)
1630{
1631 int i = 0;
1632 int mount_cnt = 0;
1633 int lastpos = 0;
1634 char *mounted_file_system = NULL;
1635 char *command = NULL;
1636 char *token = NULL;
1637 char token_chars[] =" :\t\r\f\a\0";
1638 MOUNTED_FS_STRUCT *DSFptr = NULL;
1639
1640 free_mounted_fs_list();
1641 /********
1642 * Find the number of mounted file system entries and their respective mount points.
1643 * I can't return all of the entries as one string because it's length can be longer
1644 * than MAX_STR_LEN which is used in call_program_and_get_last_line_of_output().
1645 * So start looping and get the number of mounted file systems and query them one by one.
1646 ********/
1647 /* Get the number of mounted file systems ((those that start with "/dev/" */
1648 mr_asprintf(command, "mount 2>/dev/null | awk '{if($1 ~ \"^/dev/\"){print $0}}'|wc -l");
1649 log_msg(5, "Running: %s", command);
1650 mounted_file_system = call_program_and_get_last_line_of_output(command);
1651 mr_free(command);
1652
1653 mount_cnt = atoi(mounted_file_system);
1654 log_msg (5, "mount_cnt: %d", mount_cnt);
1655 mr_free(mounted_file_system);
1656
1657 for (i=mount_cnt; i > 0; i--) {
1658 mr_asprintf(command, "mount 2>/dev/null | awk '{if($1 ~ \"^/dev/\"){print $1,$3}}'|head -n %d", i);
1659 log_msg(5, "Running: %s", command);
1660 mounted_file_system = call_program_and_get_last_line_of_output(command);
1661 mr_free(command);
1662
1663 log_msg (5, "mounted_file_system: %s", mounted_file_system);
1664 if ((token = mr_strtok(mounted_file_system, token_chars, &lastpos)) == NULL) {
1665 log_msg (4, "Could not get the list of mounted file systems");
1666 mr_free(mounted_file_system);
1667 mr_free(token);
1668 return (1);
1669 }
1670 if (token) {
1671 log_msg (5, "token: %s", token);
1672 }
1673 while (token != NULL) {
1674 log_msg (5, "token: %s", token);
1675 if ((DSFptr = (MOUNTED_FS_STRUCT *) calloc(1, sizeof(MOUNTED_FS_STRUCT))) == NULL) {
1676 fatal_error ("Cannot allocate memory");
1677 }
1678 add_mounted_fs_struct(DSFptr);
1679 strcpy(DSFptr->device, token);
1680 mr_free(token);
1681 if ((token = mr_strtok(mounted_file_system, token_chars, &lastpos)) == NULL) {
1682 log_msg (5, "Ran out of entries on the mounted file systems list");
1683 mr_free(mounted_file_system);
1684 mr_free(token);
1685 return (1);
1686 }
1687 log_msg (5, "token: %s", token);
1688 strcpy(DSFptr->mount_point, token);
1689 mr_free(token);
1690 token = mr_strtok(mounted_file_system, token_chars, &lastpos);
1691 }
1692 mr_free(mounted_file_system);
1693 }
1694 return (0);
1695}
1696
1697
1698
1699/**
1700 * Given a whole disk device special file, determine which mounted file systems
1701 * are on the dsf's partitions and which mounted file systems are not.
1702 * @param dsf The whole disk device special file.
1703 * @param included_dsf_list A char pointer used to hold the list of mount points
1704 * that are on the dsf. Memory for the array will be allocated within the function.
1705 * @param excluded_dsf_list A char pointer used to hold the list of mount points
1706 * that are not on the dsf. Memory for the array will be allocated within the function.
1707 * @return 0 on success, -1 if no device special file was passed in, -2 if a device
1708 * special file was passed in but it has no partitions on it, or 1 on failure
1709 */
1710static int get_dsf_mount_list (const char *dsf, char **included_dsf_list, char **excluded_dsf_list) {
1711 int i = 0;
1712 int c = 0;
1713 int lastpos = 0;
1714 char *VG = NULL;
1715 char *tmp = NULL;
1716 char *command = NULL;
1717 char *partition_list = NULL;
1718 char partitions[64][MAX_STR_LEN];
1719 char *mount_list = NULL;
1720 char *token = NULL;
1721 char *ndsf = NULL;
1722 char token_chars[] =" \t\r\f\a\0";
1723 MOUNTED_FS_STRUCT *DSFptr = NULL;
1724
1725 memset((char *)partitions, 0, sizeof(partitions));
1726
1727 log_msg(5, "dsf: %s", dsf);
1728
1729 /********
1730 * See if a device special file was passed in (i.e. it must start with /dev/
1731 ********/
1732 if (strncmp(dsf, "/dev/", 5)) {
1733 log_msg (4, "%s does not start with /dev/ and (probably) is not a device special file", dsf);
1734 return (-1);
1735 }
1736 log_msg(4, " %s looks like a device special file", dsf);
1737 /* Verify that the dsf exists */
1738 mr_asprintf(command, "ls -al %s 2>/dev/null | wc -l", dsf);
1739 log_msg(5, " Executing: %s", command);
1740 tmp = call_program_and_get_last_line_of_output(command);
1741 mr_free(command);
1742
1743 log_msg(5, " Return value: %s", tmp);
1744 c = atoi(tmp);
1745 mr_free(tmp);
1746
1747 if (!c) {
1748 log_to_screen("Cannot find device special file %s", dsf);
1749 return (1);
1750 }
1751 log_msg(4, " %s device special file exists", dsf);
1752
1753 /* Get a list of the mounted file systems */
1754 if (create_list_of_non_NETFS_mounted_file_systems()) {
1755 log_to_screen ("Could not get the list of mounted file systems");
1756 return (1);
1757 }
1758 log_msg (5, "Processing dsf: %s", dsf);
1759 /********
1760 * Get a list of the dsf's partitions. There could be no partitions on the disk
1761 * or a dsf of a partition was passed in (e.g. /dev/sda1 instead of /dev/sda).
1762 * Either way, it's an error.
1763 ********/
1764 mr_asprintf(command, "mr-parted2fdisk -l %s 2>/dev/null|grep -E \"^/dev/\"|awk '{printf(\"%%s \", $1)}END{print \"\"}'", dsf);
1765 log_msg(5, "Executing: %s", command);
1766 partition_list = call_program_and_get_last_line_of_output(command);
1767 mr_free(command);
1768 log_msg(4, "Partition list for %s: %s", dsf, partition_list);
1769 if (!strlen(partition_list)) {
1770 /* There were no partitions on the disk */
1771 log_msg(4, "No partitions on device special file %s", dsf);
1772 log_msg(4, "I guess it's a partition itself");
1773 strcpy(partitions[0], dsf);
1774 ndsf = truncate_to_drive_name(dsf);
1775 } else {
1776 /* Fill the partition list */
1777 i = 0;
1778 lastpos = 0;
1779 while ((token = mr_strtok(partition_list, token_chars, &lastpos)) != NULL) {
1780 log_msg (4, "Found partition: %s", token);
1781 strcpy(partitions[i++], token);
1782 mr_free(token);
1783 }
1784 mr_asprintf(ndsf, "%s", dsf);
1785 }
1786 mr_free(partition_list);
1787
1788 /* In any case we want to exclude the dsf itself from all MondRescue activities
1789 * at restore time (LVM, fdisk, ...) so we want it in our exclude_dev list */
1790 if ((DSFptr = (MOUNTED_FS_STRUCT *) calloc(1, sizeof(MOUNTED_FS_STRUCT))) == NULL) {
1791 fatal_error ("Cannot allocate memory");
1792 }
1793 add_mounted_fs_struct(DSFptr);
1794 strcpy(DSFptr->device, dsf);
1795 DSFptr->check = 1;
1796
1797 /* For the rest ndsf is the new dsf to deal with */
1798 /********
1799 * At this point, we have a list of all of the partitions on the dsf. Now try to
1800 * see which partitions have a file system on them.
1801 *
1802 * Loop through each partition on the disk and:
1803 *
1804 * - If the partition is swap, it ignores it.
1805 *
1806 * - If the partition is mounted (e.g. /dev/sda1 is mounted on /boot), it adds an entry
1807 * to the linked list, copies to it the device name and mount point, and sets check == 1.
1808 *
1809 * - If the partition is part of a Volume Group that has Logical Volumes mounted, it adds
1810 * an entry to the linked list for each mounted Logical Volume in that Volume Group, copying
1811 * to it the device name and mount point, and sets check == 1. Note that if the Volume Group
1812 * contains more than one disk, it will still add the entry even if the Logical Volume's
1813 * extents are not on the dsf that was passed in to the function. For example, Volume Group
1814 * VolGroup00 contains the disks /dev/sda1 and /dev/sdb1, and the Logical Volumes LogVol01,
1815 * which is mounted on /var and has all of its extents on /dev/sda1, and LogVol02, which is
1816 * mounted as /usr and has all of its extents on /dev/sdb1. If you pass /dev/sda into the
1817 * function, both /var and /usr will be archived even though /usr is actually on/dev/sdb.
1818 *
1819 * - If the partition is part of a Volume Group that has Logical Volumes used in a mounted
1820 * software raid device, it adds an entry to the linked list, copies to it the software raid
1821 * device name and mount point, and sets check == 1.
1822 *
1823 * - If the partition is part of a mounted software raid device, it adds an entry to the linked
1824 * list, copies to it the software raid device name and mount point, and sets check == 1.
1825 *
1826 ********/
1827 for (i=0; strlen(partitions[i]); i++) {
1828 log_msg(4, "Processing partition: %s", partitions[i]);
1829 /* See if it's swap. If it is, ignore it. */
1830 mr_asprintf(command, "mr-parted2fdisk -l %s 2>/dev/null | awk '{if(($1==\"%s\")&&(toupper($0) ~ \"SWAP\")){print $1;exit}}'", ndsf, partitions[i]);
1831 log_msg(5, " Running: %s", command);
1832 tmp = call_program_and_get_last_line_of_output(command);
1833 mr_free(command);
1834
1835 log_msg(5, " Return value: %s", tmp);
1836 c = strlen(tmp);
1837 mr_free(tmp);
1838
1839 if (c) {
1840 log_msg(4, "It's swap. Ignoring partition %s", partitions[i]);
1841 continue;
1842 }
1843
1844 /* It's not swap. See if we can find the mount point from the mount command. */
1845 mr_asprintf(command, "mount 2>/dev/null | awk '{if((NF>0)&&($1==\"%s\")){print $3}}'", partitions[i]);
1846 tmp = call_program_and_get_last_line_of_output(command);
1847 mr_free(command);
1848
1849 if (strlen(tmp)) {
1850 log_msg(4, " %s is mounted: %s", partitions[i], tmp);
1851 if ((DSFptr = find_mount_point_in_list(tmp)) == NULL) {
1852 log_msg (4, "Can't find mount point %s in mounted file systems list", tmp);
1853 mr_free(tmp);
1854 return (1);
1855 }
1856 DSFptr->check = 1;
1857 mr_free(tmp);
1858 continue;
1859 }
1860 mr_free(tmp);
1861
1862 /* It's not swap and it's not mounted. See if it's LVM */
1863 log_msg(4, " It's not mounted. Checking to see if it's LVM...");
1864
1865 /* Check for LVM */
1866 mr_asprintf(command, "pvdisplay -c %s 2> /dev/null", partitions[i]);
1867 log_msg(5, " Running: %s", command);
1868 tmp = call_program_and_get_last_line_of_output(command);
1869 mr_free(command);
1870
1871 if (strlen(tmp)) {
1872 log_msg(4, "Found an LVM partition at %s. Find the VG it's in...", partitions[i]);
1873 /* It's LVM: Find the VG it's in */
1874 mr_asprintf(command, "pvdisplay -v %s 2>/dev/null|grep \"VG Name\"|awk '{print $NF}'", partitions[i]);
1875 log_msg(5, " Running: %s", command);
1876 VG = call_program_and_get_last_line_of_output(command);
1877 mr_free(command);
1878
1879 log_msg(4, " Volume Group: %s", VG);
1880 if (strlen(VG)) {
1881 /* Found the Volume Group. Now find all of the VG's mount points */
1882 log_msg(4, " Found the Volume Group. Now find all of the VG's mount points");
1883 mr_asprintf(command, "mount 2>/dev/null|grep -E \"/dev/mapper/%s|/dev/%s/\"|awk '{printf(\"%%s \",$3)}END{print \"\"}'", VG, VG);
1884 log_msg(5, " Running: %s", command);
1885 mount_list = call_program_and_get_last_line_of_output(command);
1886 mr_free(command);
1887
1888 log_msg(4, " VG %s mount_list: %s", VG, mount_list);
1889 lastpos = 0;
1890 while ((token = mr_strtok(mount_list, token_chars, &lastpos)) != NULL) {
1891 log_msg (5, "mount point token: %s", token);
1892 if ((DSFptr = find_mount_point_in_list(token)) == NULL) {
1893 log_msg (4, "Can't find mount point %s in mounted file systems list", token);
1894 mr_free(tmp);
1895 mr_free(token);
1896 return (1);
1897 }
1898 DSFptr->check = 1;
1899 mr_free(token);
1900 }
1901 /********
1902 * Now we want to see if there are any software raid devices using
1903 * any of the Logical Volumes on the Volume Group.
1904 *******/
1905 mr_free(mount_list);
1906
1907 mr_asprintf(command, "%s", "cat /proc/mdstat|grep -iv Personal|awk '{if($0~\"^.*[ ]+:\"){printf(\"/dev/%s \", $1)}}END{print \"\"}'");
1908 log_msg (5, "Running: %s", command);
1909 mount_list = call_program_and_get_last_line_of_output(command);
1910 mr_free(command);
1911 log_msg(4, " Software raid device list: %s", mount_list);
1912 lastpos = 0;
1913 while ((token = mr_strtok(mount_list, token_chars, &lastpos)) != NULL) {
1914 mr_asprintf(command, "mdadm --detail %s 2>/dev/null | grep -c %s", token, VG);
1915 log_msg (5, "Running: %s", command);
1916 mr_free(tmp);
1917 tmp = call_program_and_get_last_line_of_output(command);
1918 mr_free(command);
1919 log_msg(4, "Number of Software raid device: %s", tmp);
1920 if (atoi(tmp)) {
1921 /* This device is on our disk */
1922 if ((DSFptr = find_device_in_list(token)) == NULL) {
1923 log_msg (4, "Can't find device %s in mounted file systems list", token);
1924 mr_free(tmp);
1925 mr_free(token);
1926 return (1);
1927 }
1928 DSFptr->check = 1;
1929 }
1930 }
1931 mr_free(token);
1932 paranoid_free(mount_list);
1933 } else {
1934 log_msg (4, "Error finding Volume Group for partition %s", partitions[i]);
1935 mr_free(tmp);
1936 return (1);
1937 }
1938 mr_free(tmp);
1939 mr_free(VG);
1940 continue;
1941 } else {
1942 log_msg (4, "Error finding partition type for the partition %s", partitions[i]);
1943 }
1944 mr_free(tmp);
1945
1946 /********
1947 * It's not swap, mounted, or LVM. See if it's used in a software raid device.
1948 ********/
1949 log_msg (5, "It's not swap, mounted, or LVM. See if it's used in a software raid device.");
1950 mr_asprintf(command, "mdadm --examine %s 2>/dev/null | awk '{if($1 == \"UUID\"){print $3}}'", partitions[i]);
1951 log_msg(4, " Running: %s", command);
1952 tmp = call_program_and_get_last_line_of_output(command);
1953 mr_free(command);
1954
1955 if (!strlen(tmp)) {
1956 log_msg(4, " Partition %s is not used in a non-LVM software raid device", partitions[i]);
1957 mr_free(tmp);
1958 continue;
1959 }
1960 log_msg (5, " UUID: %s", tmp);
1961
1962 /* Get the Software raid device list */
1963 mr_asprintf(command, "%s", "cat /proc/mdstat|grep -iv Personal|awk '{if($0~\"^.*[ ]+:\"){printf(\"/dev/%s \", $1)}}END{print \"\"}'");
1964 log_msg (5, " Running: %s", command);
1965 mount_list = call_program_and_get_last_line_of_output(command);
1966 mr_free(command);
1967
1968 log_msg(4, " Software raid device list: %s", mount_list);
1969 /* Loop through the software raid device list to see if we can find the partition */
1970 lastpos = 0;
1971 while ((token = mr_strtok(mount_list, token_chars, &lastpos)) != NULL) {
1972 mr_asprintf(command, "mdadm --detail %s 2>/dev/null | grep -c %s", token, tmp);
1973 log_msg(4, " Running: %s", command);
1974 mr_free(tmp);
1975 tmp = call_program_and_get_last_line_of_output(command);
1976 mr_free(command);
1977
1978 if (!atoi(tmp)) {
1979 log_msg (4," Didn't find partition %s in software raid device %s", partitions[i], token);
1980 } else {
1981 if ((DSFptr = find_device_in_list(token)) == NULL) {
1982 log_msg (4, "Can't find device %s in mounted file systems list", token);
1983 mr_free(tmp);
1984 mr_free(token);
1985 return (1);
1986 }
1987 DSFptr->check = 1;
1988 break;
1989 }
1990 mr_free(token);
1991 }
1992 mr_free(tmp);
1993 mr_free(mount_list);
1994 }
1995
1996 /* Determine how much memory to allocate for included_dsf_list and excluded_dsf_list */
1997 i = 0;
1998 DSFptr= DSF_Head;
1999 while (DSFptr != NULL) {
2000 i += strlen(DSFptr->mount_point) + 1;
2001 DSFptr = DSFptr->next;
2002 }
2003 log_msg (5, "i: %d", i);
2004 if ((*included_dsf_list = (char *) calloc(i+100, sizeof(char))) == NULL) {
2005 fatal_error ("Cannot allocate memory");
2006 }
2007 if ((*excluded_dsf_list = (char *) calloc(i+100, sizeof(char))) == NULL) {
2008 fatal_error ("Cannot allocate memory");
2009 }
2010 DSFptr= DSF_Head;
2011 while (DSFptr != NULL) {
2012 if (DSFptr->check) {
2013 log_msg (4, "%s is mounted on %s and is on disk %s", DSFptr->device, DSFptr->mount_point, ndsf);
2014 strcat(*included_dsf_list, DSFptr->mount_point);
2015 strcat(*included_dsf_list, "|");
2016 } else {
2017 log_msg (4, "%s is mounted on %s and is NOT on disk %s", DSFptr->device, DSFptr->mount_point, ndsf);
2018 strcat(*excluded_dsf_list, DSFptr->mount_point);
2019 strcat(*excluded_dsf_list, "|");
2020 }
2021 DSFptr = DSFptr->next;
2022 }
2023 mr_free(ndsf);
2024
2025 log_msg (5, "included_dsf_list: %s", *included_dsf_list);
2026 log_msg (5, "excluded_dsf_list: %s", *excluded_dsf_list);
2027 return (0);
2028}
2029
2030
2031/* Update the bkpinfo structure for exclude & include paths
2032 * in order to handle correctly paths corresponding to devices */
2033void mr_make_devlist_from_pathlist(char *pathlist, char mode) {
2034
2035char *token = NULL;
2036int lastpos = 0;
2037char *mounted_on_dsf = NULL;
2038char *not_mounted_on_dsf = NULL;
2039char token_chars[] ="|\t\r\f\a\0\n";
2040char *tmp = NULL;
2041char *tmp1 = NULL;
2042char *tmp2 = NULL;
2043
2044if (pathlist == NULL) {
2045 return;
2046}
2047while ((token = mr_strtok(pathlist, token_chars, &lastpos)) != NULL) {
2048 switch (get_dsf_mount_list(token, &mounted_on_dsf, &not_mounted_on_dsf)) {
2049 case 1:
2050 if (mode == 'E') {
2051 log_msg(1, "WARNING ! %s doesn't exist in -E option", token);
2052 } else {
2053 log_msg(1, "ERROR ! %s doesn't exist in -I option", token);
2054 fatal_error("Error processing -I option");
2055 }
2056 break;
2057 /* Everything is OK; proceed to archive data */
2058 case 0:
2059 if (mode == 'E') {
2060 if (strlen(mounted_on_dsf)) {
2061 log_to_screen("Excluding the following file systems on %s:", token);
2062 log_to_screen("==> %s", mounted_on_dsf);
2063 log_msg (5, "Adding to bkpinfo->exclude_paths due to -E option: %s", mounted_on_dsf);
2064 if (bkpinfo->exclude_paths) {
2065 mr_strcat(bkpinfo->exclude_paths,"|%s",mounted_on_dsf);
2066 } else {
2067 mr_asprintf(bkpinfo->exclude_paths,"%s",mounted_on_dsf);
2068 }
2069 if (bkpinfo->exclude_devs) {
2070 mr_strcat(bkpinfo->exclude_devs,"|%s",token);
2071 } else {
2072 mr_asprintf(bkpinfo->exclude_devs,"%s",token);
2073 }
2074 }
2075 } else {
2076 log_to_screen("Archiving only the following file systems on %s:", token);
2077 log_to_screen("==> %s", mounted_on_dsf);
2078 mr_free(bkpinfo->include_paths);
2079 mr_asprintf(bkpinfo->include_paths, "%s", "/");
2080 if (strlen(not_mounted_on_dsf)) {
2081 log_msg (5, "Adding to bkpinfo->exclude_paths due to -I option: %s", not_mounted_on_dsf);
2082 log_to_screen("Not archiving the following file systems:");
2083 log_to_screen("==> %s", not_mounted_on_dsf);
2084 if (bkpinfo->exclude_paths) {
2085 mr_strcat(bkpinfo->exclude_paths, "|%s",not_mounted_on_dsf);
2086 } else {
2087 mr_asprintf(bkpinfo->exclude_paths,"%s",not_mounted_on_dsf);
2088 }
2089 }
2090 }
2091 break;
2092 /* It's a dsf but not a whole disk dsf */
2093 case -2:
2094 log_to_screen("Could %s be a partition instead of a whole disk device special file?\nIgnored.", token);
2095 break;
2096 /* A device special file was not passed in. Process it as a path. */
2097 case -1:
2098 /* Adds a | to ensure correct detection even at both ends */
2099 mr_asprintf(tmp1,"|%s",token);
2100 mr_asprintf(tmp2,"|%s|",token);
2101 if (mode == 'E') {
2102 /* Add the token if not already in the list */
2103 mr_asprintf(tmp,"|%s|",bkpinfo->exclude_paths);
2104 if (strstr(tmp,tmp2) == NULL) {
2105 if (bkpinfo->exclude_paths) {
2106 mr_strcat(bkpinfo->exclude_paths,tmp1);
2107 mr_free(tmp1);
2108 } else {
2109 bkpinfo->exclude_paths = tmp1;
2110 }
2111 }
2112 } else {
2113 /* Add the token if not already in the list */
2114 mr_asprintf(tmp,"|%s|",bkpinfo->include_paths);
2115 if (strstr(tmp,tmp2) == NULL) {
2116 mr_strcat(bkpinfo->include_paths, "%s", tmp1);
2117 }
2118 mr_free(tmp1);
2119 }
2120 mr_free(tmp);
2121 mr_free(tmp2);
2122 break;
2123 }
2124 mr_free(token);
2125
2126 if (bkpinfo->include_paths != NULL) {
2127 log_msg(1, "include_paths is now '%s'", bkpinfo->include_paths);
2128 }
2129 if (bkpinfo->exclude_paths != NULL) {
2130 log_msg(1, "exclude_paths is now '%s'", bkpinfo->exclude_paths);
2131 }
2132 if (bkpinfo->exclude_devs != NULL) {
2133 log_msg(1, "exclude_devs is now '%s'", bkpinfo->exclude_devs);
2134 }
2135}
2136}
2137
2138
2139/**
2140 * Return the type of boot of the system (UEFI, EFI or BIOS)
2141 */
2142t_boot mr_boot_type(void) {
2143
2144 t_boot ret = BIOS;
2145 DIR *fd = NULL;
2146
2147#ifdef __IA64__
2148 return(EFI);
2149#endif
2150 /* Try to detect whether we are in fact in UEFI mode */
2151 fd = opendir("/sys/firmware/efi");
2152 if (fd != NULL) {
2153 ret = UEFI;
2154 log_msg(2, "UEFI boot mode detected");
2155 closedir(fd);
2156 }
2157 return(ret);
2158}
2159
2160
2161/**
2162 * Ask user for details of backup/restore information.
2163 * Called when @c mondoarchive doesn't get any parameters.
2164 * @param bkpinfo The backup information structure to fill out with the user's data.
2165 * @param archiving_to_media TRUE if archiving, FALSE if restoring.
2166 * @return 0, always.
2167 * @bug No point of `int' return value.
2168 * @ingroup archiveGroup
2169 */
2170int interactively_obtain_media_parameters_from_user(bool archiving_to_media)
2171// archiving_to_media is TRUE if I'm being called by mondoarchive
2172// archiving_to_media is FALSE if I'm being called by mondorestore
2173{
2174 char *tmp = NULL;
2175 char *sz = NULL;
2176 char *tmpro = NULL;
2177 char *tmp1 = NULL;
2178 char *mds = NULL;
2179 char *oldtmp = NULL;
2180 char *q = NULL;
2181 char p[16*MAX_STR_LEN];
2182 char *sz_size = NULL;
2183 char *command = NULL;
2184 char *compression_type = NULL;
2185 char *comment = NULL;
2186 int i;
2187 FILE *fin;
2188
2189 malloc_string(tmp1);
2190 assert(bkpinfo != NULL);
2191 bkpinfo->nonbootable_backup = FALSE;
2192
2193 // Tape, CD, NETFS, ...?
2194 srandom(getpid());
2195 bkpinfo->backup_media_type =
2196 (g_restoring_live_from_cd) ? cdr :
2197 which_backup_media_type(bkpinfo->restore_data);
2198 if (bkpinfo->backup_media_type == none) {
2199 log_to_screen("User has chosen not to backup the PC");
2200 finish(1);
2201 }
2202 /* Why asking to remove the media with tape ?
2203 if (bkpinfo->backup_media_type == tape && bkpinfo->restore_data) {
2204 popup_and_OK("Please remove media from drive(s)");
2205 }
2206 */
2207 if (archiving_to_media) {
2208 setup_tmpdir(NULL);
2209 /*
2210 * Set the scratchdir to reside on the partition with the most free space.
2211 * Automatically excludes DOS, NTFS, SMB, and NFS filesystems.
2212 */
2213#ifdef __FreeBSD__
2214 tmp = call_program_and_get_last_line_of_output("df -m -P -t nonfs,msdosfs,ntfs,ntfs-3g,vmhgfs,smbfs,smb,cifs,afs,gfs,ocfs,ocfs2,mvfs,nsspool,nssvol | grep -vE \"none|Filesystem\" | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -nr | awk '{print $NF;}' | while read x ; do test -w $x && echo $x && break ; done");
2215#else
2216 tmp = call_program_and_get_last_line_of_output("df -m -P -x nfs -x nfs4 -x fuse.sshfs -x fuse -x vfat -x ntfs -x ntfs-3g -x vmhgfs -x smbfs -x smb -x cifs -x afs -x gfs -x ocfs -x ocfs2 -x mvfs -x nsspool -x nssvol -x iso9660 | grep -vE \"none|Filesystem|/dev/shm\" | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -nr | awk '{print $NF;}' | while read x ; do test -w $x && echo $x && break ; done");
2217#endif
2218
2219 if (tmp[0] != '/') {
2220 mr_asprintf(sz, "%s", tmp);
2221 mr_free(tmp);
2222 mr_asprintf(tmp, "/%s", sz);
2223 mr_free(sz);
2224 }
2225 setup_scratchdir(tmp);
2226 mr_free(tmp);
2227 }
2228 log_msg(3, "media type = %s", bkptype_to_string(bkpinfo->backup_media_type));
2229 bkpinfo->cdrw_speed = (bkpinfo->backup_media_type == cdstream) ? 2 : 4;
2230 bkpinfo->compression_level = (bkpinfo->backup_media_type == cdstream) ? 1 : 5;
2231 bkpinfo->use_lzo = (bkpinfo->backup_media_type == cdstream) ? TRUE : FALSE;
2232 mvaddstr_and_log_it(2, 0, " ");
2233
2234 // Find device's /dev (or SCSI) entry
2235 switch (bkpinfo->backup_media_type) {
2236 case cdr:
2237 case cdrw:
2238 case dvd:
2239 case usb:
2240 /* Never try to eject a USB device */
2241 if (bkpinfo->backup_media_type == usb) {
2242 bkpinfo->please_dont_eject = TRUE;
2243 }
2244 if (archiving_to_media) {
2245 if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) {
2246 if (ask_me_yes_or_no("Is your computer a laptop, or does the CD writer incorporate BurnProof technology?")) {
2247 bkpinfo->manual_cd_tray = TRUE;
2248 }
2249 }
2250 if ((compression_type = which_compression_type()) == NULL) {
2251 log_to_screen("User has chosen not to backup the PC");
2252 finish(1);
2253 }
2254
2255 if ((bkpinfo->compression_level = which_compression_level()) == -1) {
2256 log_to_screen("User has chosen not to backup the PC");
2257 finish(1);
2258 }
2259 mds = media_descriptor_string(bkpinfo->backup_media_type);
2260 mr_asprintf(comment, "What speed is your %s (re)writer?", mds);
2261 if (bkpinfo->backup_media_type == dvd) {
2262 find_dvd_device(bkpinfo->media_device, FALSE);
2263 strcpy(tmp1, "1");
2264 mr_asprintf(sz_size, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4482 MB
2265 log_msg(1, "Setting to DVD defaults");
2266 } else {
2267 strcpy(bkpinfo->media_device, VANILLA_SCSI_CDROM);
2268 strcpy(tmp1, "4");
2269 mr_asprintf(sz_size, "%d", 650);
2270 log_msg(1, "Setting to CD defaults");
2271 }
2272 if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) {
2273 if (!popup_and_get_string("Speed", comment, tmp1, 4)) {
2274 log_to_screen("User has chosen not to backup the PC");
2275 mr_free(comment);
2276 finish(1);
2277 }
2278 }
2279 mr_free(comment);
2280 bkpinfo->cdrw_speed = atoi(tmp1); // if DVD then this shouldn't ever be used anyway :)
2281
2282 strcpy(tmp1, sz_size);
2283 mr_asprintf(comment, "How much data (in Megabytes) will each %s store?", mds);
2284 mr_free(mds);
2285 if (!popup_and_get_string("Size", comment, tmp1, 5)) {
2286 log_to_screen("User has chosen not to backup the PC");
2287 finish(1);
2288 }
2289 mr_asprintf(sz_size, "%s", tmp1);
2290 bkpinfo->media_size = atoi(sz_size);
2291
2292 if (bkpinfo->media_size <= 0) {
2293 log_to_screen("User has chosen not to backup the PC");
2294 finish(1);
2295 }
2296 }
2297 /* No break because we continue even for usb */
2298 case cdstream:
2299 mds = media_descriptor_string(bkpinfo->backup_media_type);
2300
2301 if ((bkpinfo->disaster_recovery) && (bkpinfo->backup_media_type != usb)) {
2302 strcpy(bkpinfo->media_device, "/dev/cdrom");
2303 log_msg(2, "CD-ROM device assumed to be at %s", bkpinfo->media_device);
2304 } else if ((bkpinfo->restore_data && (bkpinfo->backup_media_type != usb)) || bkpinfo->backup_media_type == dvd) {
2305 if (!bkpinfo->media_device[0]) {
2306 strcpy(bkpinfo->media_device, "/dev/cdrom");
2307 } // just for the heck of it :)
2308 log_msg(1, "bkpinfo->media_device = %s", bkpinfo->media_device);
2309 if (bkpinfo->backup_media_type == dvd || find_cdrom_device(bkpinfo->media_device, FALSE)) {
2310 log_msg(1, "bkpinfo->media_device = %s", bkpinfo->media_device);
2311 mr_asprintf(comment, "Please specify your %s drive's /dev entry", mds);
2312 if (!popup_and_get_string("Device?", comment, bkpinfo->media_device, MAX_STR_LEN / 4)) {
2313 log_to_screen("User has chosen not to backup the PC");
2314 finish(1);
2315 }
2316 }
2317 log_msg(2, "%s device found at %s", mds, bkpinfo->media_device);
2318 } else {
2319 if ((find_cdrw_device(bkpinfo->media_device)) && (bkpinfo->backup_media_type != usb)) {
2320 bkpinfo->media_device[0] = '\0';
2321 }
2322 if (bkpinfo->media_device[0]) {
2323 if (bkpinfo->backup_media_type == usb) {
2324 mr_asprintf(tmp, "I think your %s media corresponds to %s. Is this correct?", mds, bkpinfo->media_device);
2325 } else {
2326 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);
2327 }
2328 if (!ask_me_yes_or_no(tmp)) {
2329 bkpinfo->media_device[0] = '\0';
2330 }
2331 mr_free(tmp);
2332 }
2333 if (!bkpinfo->media_device[0]) {
2334 if (bkpinfo->backup_media_type == usb) {
2335 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);
2336 } else {
2337 if (g_kernel_version < 2.6) {
2338 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);
2339 } else {
2340 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);
2341 }
2342 }
2343 if (!i) {
2344 log_to_screen("User has chosen not to backup the PC");
2345 finish(1);
2346 }
2347 }
2348 }
2349 mr_free(mds);
2350
2351 if (bkpinfo->backup_media_type == cdstream) {
2352 bkpinfo->media_size = 650;
2353 }
2354 break;
2355 case udev:
2356 if (!ask_me_yes_or_no
2357 ("This option is for advanced users only. Are you sure?")) {
2358 log_to_screen("User has chosen not to backup the PC");
2359 finish(1);
2360 }
2361 case tape:
2362
2363 if ((!bkpinfo->restore_mode) && (find_tape_device_and_size(bkpinfo->media_device, sz_size))) {
2364 log_msg(3, "Ok, using vanilla scsi tape.");
2365 strcpy(bkpinfo->media_device, VANILLA_SCSI_TAPE);
2366 if ((fin = fopen(bkpinfo->media_device, "r"))) {
2367 paranoid_fclose(fin);
2368 } else {
2369 strcpy(bkpinfo->media_device, "/dev/osst0");
2370 }
2371 }
2372 if (bkpinfo->media_device[0]) {
2373 if ((fin = fopen(bkpinfo->media_device, "r"))) {
2374 paranoid_fclose(fin);
2375 } else {
2376 if (does_file_exist("/tmp/mondorestore.cfg")) {
2377 read_cfg_var("/tmp/mondorestore.cfg", "media-dev", bkpinfo->media_device);
2378 }
2379 }
2380 }
2381 if (bkpinfo->media_device[0]) {
2382 mr_asprintf(tmp, "I think I've found your tape streamer at %s; am I right on the money?", bkpinfo->media_device);
2383 if (!ask_me_yes_or_no(tmp)) {
2384 bkpinfo->media_device[0] = '\0';
2385 }
2386 mr_free(tmp);
2387 }
2388 if (!bkpinfo->media_device[0]) {
2389 if (!popup_and_get_string("Device name?", "What is the /dev entry of your tape streamer?", bkpinfo->media_device, MAX_STR_LEN / 4)) {
2390 log_to_screen("User has chosen not to backup the PC");
2391 finish(1);
2392 }
2393 }
2394 mr_asprintf(tmp, "ls -l %s", bkpinfo->media_device);
2395 if (run_program_and_log_output(tmp, FALSE)) {
2396 log_to_screen("User has not specified a valid /dev entry");
2397 finish(1);
2398 }
2399 mr_free(tmp);
2400
2401 mr_asprintf(sz_size,"%ld",bkpinfo->internal_tape_block_size);
2402 tmp = mr_popup_and_get_string("Tape block size?", "What is the block size of your tape streamer?", sz_size);
2403 if (tmp == NULL) {
2404 log_to_screen("User has chosen not to backup the PC");
2405 finish(1);
2406 }
2407 bkpinfo->internal_tape_block_size = atol(tmp);
2408 mr_free(sz_size);
2409 mr_free(tmp);
2410
2411 if (bkpinfo->internal_tape_block_size <= 0) {
2412 log_to_screen("User has chosen not to backup the PC");
2413 finish(1);
2414 }
2415
2416 bkpinfo->media_size = 0;
2417 log_msg(4, "media_size = %ld", bkpinfo->media_size);
2418
2419 bkpinfo->use_obdr = ask_me_yes_or_no
2420 ("Do you want to activate OBDR support for your tapes ?");
2421 if (bkpinfo->use_obdr) {
2422 log_msg(4, "obdr mode = TRUE");
2423 } else {
2424 log_msg(4, "obdr mode = FALSE");
2425 }
2426 if (archiving_to_media) {
2427 if ((compression_type = which_compression_type()) == NULL) {
2428 log_to_screen("User has chosen not to backup the PC");
2429 finish(1);
2430 }
2431 if ((bkpinfo->compression_level = which_compression_level()) == -1) {
2432 log_to_screen("User has chosen not to backup the PC");
2433 finish(1);
2434 }
2435 }
2436 break;
2437
2438
2439
2440 case netfs:
2441 /* Never try to eject a NETFS device */
2442 bkpinfo->please_dont_eject = TRUE;
2443 /* Force NFS to be the protocol by default */
2444 if (bkpinfo->netfs_proto == NULL) {
2445 mr_asprintf(bkpinfo->netfs_proto, "nfs");
2446 }
2447
2448 /* Initiate bkpinfo netfs_mount path from running environment if not already done */
2449 if (bkpinfo->netfs_mount == NULL) {
2450 bkpinfo->netfs_mount = call_program_and_get_last_line_of_output("mount | grep \":\" | cut -d' ' -f1 | head -n1");
2451 }
2452#ifdef __FreeBSD__
2453 if (TRUE)
2454#else
2455 if (!bkpinfo->disaster_recovery)
2456#endif
2457 {
2458 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)) {
2459 log_to_screen("User has chosen not to backup the PC");
2460 finish(1);
2461 }
2462 mr_free(bkpinfo->netfs_mount);
2463 mr_asprintf(bkpinfo->netfs_mount, "%s", p);
2464 if (!bkpinfo->restore_data) {
2465 if ((compression_type = which_compression_type()) == NULL) {
2466 log_to_screen("User has chosen not to backup the PC");
2467 finish(1);
2468 }
2469
2470 if ((bkpinfo->compression_level = which_compression_level()) == -1) {
2471 log_to_screen("User has chosen not to backup the PC");
2472 finish(1);
2473 }
2474 }
2475 // check whether already mounted - we better remove
2476 // surrounding spaces and trailing '/' for this
2477 mr_strip_spaces(bkpinfo->netfs_mount);
2478 if (bkpinfo->netfs_mount[strlen(bkpinfo->netfs_mount) - 1] == '/')
2479 bkpinfo->netfs_mount[strlen(bkpinfo->netfs_mount) - 1] = '\0';
2480 q = strchr(bkpinfo->netfs_mount, '@');
2481 if (q != NULL) {
2482 /* User found. Store the 2 values */
2483 q++;
2484 /* new netfs mount */
2485 strcpy(tmp1,q);
2486 } else {
2487 strcpy(tmp1,bkpinfo->netfs_mount);
2488 }
2489 mr_asprintf(command, "mount | grep \"%s \" | cut -d' ' -f3", tmp1);
2490 tmp = call_program_and_get_last_line_of_output(command);
2491 strcpy(bkpinfo->isodir, tmp);
2492 mr_free(tmp);
2493 mr_free(command);
2494
2495 if (!bkpinfo->restore_data) {
2496 mr_asprintf(sz_size, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4482 MB
2497 mr_asprintf(comment, "How much data (in Megabytes) will each media store?");
2498 strcpy(tmp1, sz_size);
2499 mr_free(sz_size);
2500 if (!popup_and_get_string("Size", comment, tmp1, 5)) {
2501 log_to_screen("User has chosen not to backup the PC");
2502 finish(1);
2503 }
2504 mr_free(comment);
2505 mr_asprintf(sz_size, "%s", tmp1);
2506 } else {
2507 mr_asprintf(sz_size, "0");
2508 }
2509 bkpinfo->media_size = atoi(sz_size);
2510 mr_free(sz_size);
2511
2512 if (bkpinfo->media_size < 0) {
2513 log_to_screen("User has chosen not to backup the PC");
2514 finish(1);
2515 }
2516 }
2517 if (bkpinfo->disaster_recovery) {
2518 mr_asprintf(command ,"umount %s/isodir 2> /dev/null", bkpinfo->tmpdir);
2519 paranoid_system(command);
2520 mr_free(command);
2521
2522 }
2523 strcpy(tmp1, bkpinfo->netfs_proto);
2524 if (!popup_and_get_string("Network protocol", "Which protocol should I use (nfs/sshfs/smbfs) ?",tmp1, MAX_STR_LEN)) {
2525 log_to_screen("User has chosen not to backup the PC");
2526 finish(1);
2527 }
2528 mr_free(bkpinfo->netfs_proto);
2529 mr_asprintf(bkpinfo->netfs_proto, "%s", tmp1);
2530
2531 strcpy(tmp1, bkpinfo->netfs_mount);
2532 if (!popup_and_get_string("Network share", "Which remote share should I mount?", tmp1, MAX_STR_LEN)) {
2533 log_to_screen("User has chosen not to backup the PC");
2534 finish(1);
2535 }
2536 mr_free(bkpinfo->netfs_mount);
2537 mr_asprintf(bkpinfo->netfs_mount, "%s", tmp1);
2538
2539 if (bkpinfo->netfs_user) {
2540 strcpy(tmp1, bkpinfo->netfs_user);
2541 } else {
2542 strcpy(tmp1, "");
2543 }
2544 if (!popup_and_get_string("Network user", "Which user should I use if any ?",tmp1, MAX_STR_LEN)) {
2545 log_to_screen("User has chosen not to backup the PC");
2546 finish(1);
2547 }
2548 mr_free(bkpinfo->netfs_user);
2549 if (strcmp(tmp1, "") != 0) {
2550 mr_asprintf(bkpinfo->netfs_user, "%s", tmp1);
2551 }
2552
2553 /* Initiate bkpinfo isodir path from running environment if mount already done */
2554 if (is_this_device_mounted(bkpinfo->netfs_mount)) {
2555 tmp = call_program_and_get_last_line_of_output("mount | grep \":\" | cut -d' ' -f3 | head -n1");
2556 strcpy(bkpinfo->isodir, tmp);
2557 mr_free(tmp);
2558 } else {
2559 // Why netfsdir ?
2560 sprintf(bkpinfo->isodir, "%s/netfsdir", bkpinfo->tmpdir);
2561 mr_asprintf(command, "mkdir -p %s", bkpinfo->isodir);
2562 run_program_and_log_output(command, 5);
2563 mr_free(command);
2564
2565 if (bkpinfo->restore_data) {
2566 /* mount th FS read-only in restore mode to avoid any erase of whatever */
2567 mr_asprintf(tmpro, "-o ro");
2568 } else {
2569 mr_asprintf(tmpro, "");
2570 }
2571
2572 /* Build the mount string */
2573 if (strstr(bkpinfo->netfs_proto, "smbfs")) {
2574 mr_asprintf(tmp, "mount -t cifs %s %s %s",bkpinfo->netfs_mount, bkpinfo->isodir,tmpro);
2575 if (bkpinfo->netfs_user) {
2576 mr_strcat(tmp, " -o user=%s", bkpinfo->netfs_user);
2577 }
2578 else {
2579 if (strstr(bkpinfo->netfs_proto, "sshfs")) {
2580 mr_asprintf(tmp, "sshfs %s ",tmpro);
2581 } else {
2582 mr_asprintf(tmp, "mount -t %s -o nolock %s ", bkpinfo->netfs_proto,tmpro);
2583 }
2584 if (bkpinfo->netfs_user) {
2585 mr_strcat(tmp, "%s@", bkpinfo->netfs_user);
2586 }
2587 mr_strcat(tmp, "%s %s", bkpinfo->netfs_mount, bkpinfo->isodir);
2588 }
2589 run_program_and_log_output(tmp, 3);
2590 mr_free(tmp);
2591
2592 malloc_string(g_selfmounted_isodir);
2593 strcpy(g_selfmounted_isodir, bkpinfo->isodir);
2594 }
2595 }
2596 if (!is_this_device_mounted(bkpinfo->netfs_mount)) {
2597 popup_and_OK("Please mount that partition before you try to backup to or restore from it.");
2598 finish(1);
2599 }
2600 if (bkpinfo->netfs_remote_dir == NULL) {
2601 fatal_error("bkpinfo->netfs_remote_dir should not be NULL");
2602 }
2603 strcpy(tmp1, bkpinfo->netfs_remote_dir);
2604 if (!popup_and_get_string ("Directory", "Which directory within that mountpoint?", tmp1, MAX_STR_LEN)) {
2605 log_to_screen("User has chosen not to backup the PC");
2606 finish(1);
2607 }
2608 mr_free(bkpinfo->netfs_remote_dir);
2609 mr_asprintf(bkpinfo->netfs_remote_dir, "%s", tmp1);
2610
2611 // check whether writable - we better remove surrounding spaces for this
2612 mr_strip_spaces(bkpinfo->netfs_remote_dir);
2613
2614 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)) {
2615 log_to_screen("User has chosen not to backup the PC");
2616 finish(1);
2617 }
2618 log_msg(3, "prefix set to %s", bkpinfo->prefix);
2619 log_msg(3, "Just set netfs_remote_dir to %s", bkpinfo->netfs_remote_dir);
2620 log_msg(3, "isodir is still %s", bkpinfo->isodir);
2621 break;
2622
2623 case iso:
2624 if (!bkpinfo->disaster_recovery) {
2625 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)) {
2626 log_to_screen("User has chosen not to backup the PC");
2627 finish(1);
2628 }
2629 if (archiving_to_media) {
2630 if ((compression_type = which_compression_type()) == NULL) {
2631 log_to_screen("User has chosen not to backup the PC");
2632 finish(1);
2633 }
2634 if ((bkpinfo->compression_level = which_compression_level()) == -1) {
2635 log_to_screen("User has chosen not to backup the PC");
2636 finish(1);
2637 }
2638 sprintf(tmp1, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4482 MB
2639 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)) {
2640 log_to_screen("User has chosen not to backup the PC");
2641 finish(1);
2642 }
2643 bkpinfo->media_size = atoi(tmp1);
2644 } else {
2645 bkpinfo->media_size = 650;
2646 }
2647 }
2648 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)) {
2649 log_to_screen("User has chosen not to backup the PC");
2650 finish(1);
2651 }
2652 log_msg(3, "prefix set to %s", bkpinfo->prefix);
2653 break;
2654 default:
2655 fatal_error("I, Mojo Jojo, shall defeat those pesky Powerpuff Girls!");
2656 }
2657
2658 if (archiving_to_media) {
2659 /* Needs to be done before calling which_boot_loader */
2660 bkpinfo->boot_type = mr_boot_type();
2661 mr_free(bkpinfo->boot_device);
2662#ifdef __FreeBSD__
2663 bkpinfo->boot_device = call_program_and_get_last_line_of_output("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/\\([0-9]\\).*/\\1/'");
2664#else
2665 bkpinfo->boot_device = call_program_and_get_last_line_of_output("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'");
2666#endif
2667 i = which_boot_loader(bkpinfo->boot_device);
2668 if (i == 'U') // unknown
2669 {
2670
2671#ifdef __FreeBSD__
2672 if (!popup_and_get_string("Boot device", "What is your boot device? (e.g. /dev/ad0)", bkpinfo->boot_device, MAX_STR_LEN / 4)) {
2673 log_to_screen("User has chosen not to backup the PC");
2674 finish(1);
2675 }
2676 i = which_boot_loader(bkpinfo->boot_device);
2677#else
2678 strcpy(tmp1, bkpinfo->boot_device);
2679 if (!popup_and_get_string("Boot device", "What is your boot device? (e.g. /dev/hda)", tmp1, MAX_STR_LEN / 4)) {
2680 log_to_screen("User has chosen not to backup the PC");
2681 finish(1);
2682 }
2683 mr_free(bkpinfo->boot_device);
2684 mr_asprintf(bkpinfo->boot_device, "%s", tmp1);
2685
2686 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "ELILO")) {
2687 i = 'E';
2688 } else
2689 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "LILO")) {
2690 i = 'L';
2691 } else
2692 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "GRUB")) {
2693 i = 'G';
2694 } else {
2695 i = 'U';
2696 }
2697#endif
2698 if (i == 'U') {
2699 if (ask_me_yes_or_no("Unidentified boot loader. Shall I restore it byte-for-byte at restore time and hope for the best?")) {
2700 i = 'R'; // raw
2701 } else {
2702 log_to_screen("I cannot find your boot loader. Please run mondoarchive with parameters.");
2703 finish(1);
2704 }
2705 }
2706 }
2707 bkpinfo->boot_loader = i;
2708 /* TODO: Check consisytency of boot type and boot loader */
2709
2710 if (bkpinfo->include_paths) {
2711 strcpy(tmp1, bkpinfo->include_paths);
2712 mr_free(bkpinfo->include_paths);
2713 } else {
2714 strcpy(tmp1, "/");
2715 }
2716 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)) {
2717 log_to_screen("User has chosen not to backup the PC");
2718 finish(1);
2719 }
2720 mr_asprintf(bkpinfo->include_paths, "%s", tmp1);
2721
2722 tmp = list_of_NETFS_mounts_only();
2723 if (strlen(tmp) > 2) {
2724 mr_strcat(bkpinfo->exclude_paths, "|%s",tmp);
2725 }
2726 mr_free(tmp);
2727// NTFS
2728 tmp = call_program_and_get_last_line_of_output("mr-parted2fdisk -l 2>/dev/null | grep -i ntfs | awk '{ print $1};' | tr -s '\\n' ' ' | awk '{ print $0};'");
2729 strncpy(tmp1, tmp, MAX_STR_LEN / 4);
2730 mr_free(tmp);
2731 if (strlen(tmp1) > 2) {
2732 if (!popup_and_get_string("NTFS partitions", "Please enter/confirm the NTFS partitions you wish to backup as well.", tmp1, MAX_STR_LEN / 4)) {
2733 log_to_screen("User has chosen not to backup the PC");
2734 finish(1);
2735 }
2736 strncpy(bkpinfo->image_devs, tmp1, MAX_STR_LEN / 4);
2737 }
2738
2739 if (bkpinfo->exclude_paths != NULL ) {
2740 strncpy(p,bkpinfo->exclude_paths,(16*MAX_STR_LEN)-1);
2741 } else {
2742 p[0] = '\0';
2743 }
2744 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);
2745 if (p == NULL) {
2746 log_to_screen("User has chosen not to backup the PC");
2747 finish(1);
2748 }
2749 mr_free(bkpinfo->exclude_paths);
2750 mr_asprintf(tmp, "%s", p);
2751 bkpinfo->exclude_paths = tmp;
2752
2753 mr_asprintf(oldtmp, "%s", bkpinfo->tmpdir);
2754 if (bkpinfo->tmpdir != NULL ) {
2755 strncpy(p,bkpinfo->tmpdir,(16*MAX_STR_LEN)-1);
2756 } else {
2757 p[0] = '\0';
2758 }
2759 if (!popup_and_get_string("Temporary directory", "Please enter your temporary directory.", p, (4*MAX_STR_LEN)-1)) {
2760 log_to_screen("User has chosen not to backup the PC");
2761 finish(1);
2762 }
2763 /* if modified to another path */
2764 if (strcmp(p,oldtmp) != 0) {
2765 setup_tmpdir(p);
2766 }
2767 mr_free(oldtmp);
2768
2769 mr_asprintf(oldtmp, "%s", bkpinfo->scratchdir);
2770 if (bkpinfo->scratchdir != NULL ) {
2771 strncpy(p,bkpinfo->scratchdir,(16*MAX_STR_LEN)-1);
2772 } else {
2773 p[0] = '\0';
2774 }
2775 if (!popup_and_get_string("Scratch directory", "Please enter your scratch directory.", p, (4*MAX_STR_LEN)-1)) {
2776 log_to_screen("User has chosen not to backup the PC");
2777 finish(1);
2778 }
2779 /* if modified to another path */
2780 if (strcmp(p,oldtmp) != 0) {
2781 setup_scratchdir(p);
2782 }
2783 mr_free(oldtmp);
2784
2785 if (ask_me_yes_or_no("Do you want to backup extended attributes?")) {
2786 if (find_home_of_exe("getfattr")) {
2787 mr_free(g_getfattr);
2788 mr_asprintf(g_getfattr,"getfattr");
2789 }
2790 if (find_home_of_exe("getfacl")) {
2791 mr_free(g_getfacl);
2792 mr_asprintf(g_getfacl,"getfacl");
2793 }
2794 log_it("Backup of extended attributes");
2795 }
2796// Interactive mode:
2797#ifdef __IA64__
2798 bkpinfo->make_cd_use_lilo = TRUE;
2799#else
2800 bkpinfo->make_cd_use_lilo = FALSE;
2801#endif
2802 bkpinfo->backup_data = TRUE;
2803 if (strcmp(compression_type,"lzo") == 0) {
2804 strcpy(bkpinfo->zip_exe, "lzop");
2805 strcpy(bkpinfo->zip_suffix, "lzo");
2806 } else if (strcmp(compression_type,"gzip") == 0) {
2807 strcpy(bkpinfo->zip_exe, "gzip");
2808 strcpy(bkpinfo->zip_suffix, "gz");
2809 } else if (strcmp(compression_type,"lzma") == 0) {
2810 //strcpy(bkpinfo->zip_exe, "xy");
2811 //strcpy(bkpinfo->zip_suffix, "xy");
2812 } else if (strcmp(compression_type,"bzip2") == 0) {
2813 strcpy(bkpinfo->zip_exe, "bzip2");
2814 strcpy(bkpinfo->zip_suffix, "bz2");
2815 } else {
2816 bkpinfo->zip_exe[0] = bkpinfo->zip_suffix[0] = '\0';
2817 }
2818#if __FreeBSD__ == 5
2819 strcpy(bkpinfo->kernel_path, "/boot/kernel/kernel");
2820#elif __FreeBSD__ == 4
2821 strcpy(bkpinfo->kernel_path, "/kernel");
2822#elif linux
2823 if (figure_out_kernel_path_interactively_if_necessary(bkpinfo->kernel_path)) {
2824 fatal_error("Kernel not found. Please specify manually with the '-k' switch.");
2825 }
2826#else
2827#error "I don't know about this system!"
2828#endif
2829
2830
2831 bkpinfo->verify_data =
2832 ask_me_yes_or_no
2833 ("Will you want to verify your backups after Mondo has created them?");
2834
2835 if (!ask_me_yes_or_no
2836 ("Are you sure you want to proceed? Hit 'no' to abort.")) {
2837 log_to_screen("User has chosen not to backup the PC");
2838 finish(1);
2839 }
2840 } else {
2841 bkpinfo->restore_data = TRUE; // probably...
2842 }
2843 mr_free(compression_type);
2844
2845 if (bkpinfo->backup_media_type == iso
2846 || bkpinfo->backup_media_type == netfs) {
2847 g_ISO_restore_mode = TRUE;
2848 }
2849#ifdef __FreeSD__
2850// skip
2851#else
2852 if (bkpinfo->backup_media_type == netfs) {
2853 log_msg(3, "I think the Remote mount is mounted at %s", bkpinfo->isodir);
2854 }
2855 log_it("isodir = %s", bkpinfo->isodir);
2856 if (bkpinfo->netfs_mount) {
2857 log_it("netfs_mount = '%s'", bkpinfo->netfs_mount);
2858 }
2859 if (bkpinfo->netfs_user) {
2860 log_it("netfs_user = '%s'", bkpinfo->netfs_user);
2861 }
2862 if (bkpinfo->netfs_proto) {
2863 log_it("netfs_proto = '%s'", bkpinfo->netfs_proto);
2864 }
2865#endif
2866
2867 log_it("media device = %s", bkpinfo->media_device);
2868 log_it("media size = %ld", bkpinfo->media_size);
2869 log_it("media type = %s", bkptype_to_string(bkpinfo->backup_media_type));
2870 if (bkpinfo->prefix) {
2871 log_it("prefix = %s", bkpinfo->prefix);
2872 }
2873 log_it("compression = %ld", bkpinfo->compression_level);
2874 log_it("exclude_path = %s", bkpinfo->exclude_paths);
2875 if (bkpinfo->include_paths) {
2876 log_it("include_path = %s", bkpinfo->include_paths);
2877 }
2878
2879 /* Handle devices passed in bkpinfo and print result */
2880 /* the mr_make_devlist_from_pathlist function appends */
2881 /* to the *_paths variables so copy before */
2882 mr_make_devlist_from_pathlist(bkpinfo->exclude_paths, 'E');
2883 mr_make_devlist_from_pathlist(bkpinfo->include_paths, 'I');
2884
2885 log_it("scratchdir = '%s'", bkpinfo->scratchdir);
2886 log_it("tmpdir = '%s'", bkpinfo->tmpdir);
2887 if (bkpinfo->image_devs) {
2888 log_it("image_devs = '%s'", bkpinfo->image_devs);
2889 }
2890 log_it("boot_device = '%s' (loader=%c)", bkpinfo->boot_device, bkpinfo->boot_loader);
2891 if (bkpinfo->media_size < 0) {
2892 if (archiving_to_media) {
2893 fatal_error("Media size is less than zero.");
2894 } else {
2895 log_msg(2, "Warning - media size is less than zero.");
2896 bkpinfo->media_size = 0;
2897 }
2898 }
2899 paranoid_free(sz_size);
2900 paranoid_free(tmp1);
2901 return (0);
2902}
2903
2904
2905/**
2906 * Get a |-separated list of NETFS mounts.
2907 * @return The list created.
2908 * @note The return value points to allocated string that needs to be freed by
2909 * caller
2910 * @bug Even though we only want the mounts, the devices are still checked.
2911 */
2912char *list_of_NETFS_mounts_only(void)
2913{
2914 char *exclude_these_directories = NULL;
2915
2916 exclude_these_directories = call_program_and_get_last_line_of_output("mount -t coda,ncpfs,fuse.sshfs,nfs,nfs4,vmhgfs,smbfs,cifs,afs,gfs,ocfs,ocfs2,mvfs,nsspool,nssvol | tr -s '\t' ' ' | cut -d' ' -f3 | tr -s '\n' '|' | awk '{print $0;}'");
2917 log_msg(9,"list_of_NETFS_mounts_only returns %s",exclude_these_directories);
2918 return(exclude_these_directories);
2919}
2920
2921/* @} - end of utilityGroup */
2922
2923
2924
2925
2926
2927/**
2928 * Create a randomly-named FIFO. The format is @p stub "." [random] [random] where
2929 * [random] is a random number between 1 and 32767.
2930 * @param store_name_here Where to store the new filename.
2931 * @param stub A random number will be appended to this to make the FIFO's name.
2932 * @ingroup deviceGroup
2933 */
2934void make_fifo(char *store_name_here, char *stub)
2935{
2936 char *tmp = NULL;
2937
2938 assert_string_is_neither_NULL_nor_zerolength(stub);
2939
2940 sprintf(store_name_here, "%s%d%d", stub, (int) (random() % 32768),
2941 (int) (random() % 32768));
2942 make_hole_for_file(store_name_here);
2943 mkfifo(store_name_here, S_IRWXU | S_IRWXG);
2944 mr_asprintf(tmp, "chmod 770 %s", store_name_here);
2945 paranoid_system(tmp);
2946 mr_free(tmp);
2947}
2948
2949
2950
2951/**
2952 * @addtogroup deviceGroup
2953 * @{
2954 */
2955/**
2956 * If we can read @p dev, set @p output to it.
2957 * If @p dev cannot be read, set @p output to "".
2958 * @param dev The device to check for.
2959 * @param output Set to @p dev if @p dev exists, "" otherwise.
2960 * @return TRUE if @p dev exists, FALSE if it doesn't.
2961 */
2962bool set_dev_to_this_if_rx_OK(char *output, char *dev)
2963{
2964 char *command = NULL;
2965 bool ret=FALSE;
2966
2967 if (!dev || dev[0] == '\0') {
2968 output[0] = '\0';
2969 return(ret);
2970 }
2971// assert_string_is_neither_NULL_nor_zerolength(dev);
2972 if (!bkpinfo->please_dont_eject) {
2973 log_msg(10, "Injecting %s", dev);
2974 inject_device(dev);
2975 }
2976 if (!does_file_exist(dev)) {
2977 log_msg(10, "%s doesn't exist. Returning FALSE.", dev);
2978 return(ret);
2979 }
2980 mr_asprintf(command, "dd bs=%ld count=1 if=%s of=/dev/null &> /dev/null", 512L, dev);
2981 if (!run_program_and_log_output(command, FALSE) && !run_program_and_log_output(command, FALSE)) {
2982 strcpy(output, dev);
2983 log_msg(4, "Found it - %s", dev);
2984 ret = TRUE;
2985 } else {
2986 output[0] = '\0';
2987 log_msg(4, "It's not %s", dev);
2988 }
2989 mr_free(command);
2990 return(ret);
2991}
2992
2993
2994
2995
2996
2997/**
2998 * Find out what number CD is in the drive.
2999 * @param bkpinfo The backup information structure. The @c bkpinfo->media_device field is the only one used.
3000 * @return The current CD number, or -1 if it could not be found.
3001 * @note If the CD is not mounted, it will be mounted
3002 * (and remain mounted after this function returns).
3003 */
3004int what_number_cd_is_this()
3005{
3006 int cd_number = -1;
3007 char *mountdev = NULL;
3008 char *tmp = NULL;
3009
3010 assert(bkpinfo != NULL);
3011// log_it("Asking what_number_cd_is_this");
3012 if (g_ISO_restore_mode) {
3013 tmp = call_program_and_get_last_line_of_output("mount | grep iso9660 | awk '{print $3;}'");
3014 mr_asprintf(mountdev, "%s%s", tmp, "/archives/THIS-CD-NUMBER");
3015 mr_free(tmp);
3016 cd_number = atoi(last_line_of_file(mountdev));
3017 mr_free(mountdev);
3018
3019 return (cd_number);
3020 }
3021
3022 mr_asprintf(mountdev, "%s", bkpinfo->media_device);
3023 if (!mountdev[0]) {
3024 log_it
3025 ("(what_number_cd_is_this) Warning - media_device unknown. Finding out...");
3026 find_cdrom_device(bkpinfo->media_device, FALSE);
3027 }
3028 if (!is_this_device_mounted(MNT_CDROM)) {
3029 if (bkpinfo->backup_media_type == usb) {
3030 mount_USB_here(mountdev, MNT_CDROM);
3031 } else {
3032 mount_CDROM_here(mountdev, MNT_CDROM);
3033 }
3034 }
3035 mr_free(mountdev);
3036
3037 cd_number = atoi(last_line_of_file(MNT_CDROM "/archives/THIS-CD-NUMBER"));
3038 return (cd_number);
3039}
3040
3041
3042/**
3043 * Find out what device is mounted as root (/).
3044 * @return Root device.
3045 * @note The returned string points to storage that needs to be freed by
3046 * caller
3047 * @bug A bit of a misnomer; it's actually finding out the root device.
3048 * The mountpoint (where it's mounted) will obviously be '/'.
3049 */
3050char *where_is_root_mounted() {
3051
3052/*@ buffers **************** */
3053char *tmp = NULL;
3054
3055#ifdef __FreeBSD__
3056 tmp = call_program_and_get_last_line_of_output("mount | grep \" on / \" | cut -d' ' -f1");
3057#else
3058 tmp = call_program_and_get_last_line_of_output("mount | grep \" on / \" | cut -d' ' -f1 | sed s/[0-9]// | sed s/[0-9]//");
3059 if (strstr(tmp, "/dev/cciss/")) {
3060 mr_free(tmp);
3061 tmp = call_program_and_get_last_line_of_output("mount | grep \" on / \" | cut -d' ' -f1 | cut -dp -f1");
3062 }
3063 if (strstr(tmp, "/dev/md")) {
3064 mr_free(tmp);
3065 tmp = call_program_and_get_last_line_of_output("mount | grep \" on / \" | cut -d' ' -f1");
3066 }
3067#endif
3068
3069return (tmp);
3070}
3071
3072
3073/**
3074 * Find out which boot loader is in use.
3075 * @param which_device Device to look for the boot loader on.
3076 * @return 'L' for LILO, 'E'for ELILO, 'G' for GRUB, 'B' or 'D' for FreeBSD boot loaders, or 'U' for Unknown.
3077 * @note Under Linux, all drives are examined, not just @p which_device.
3078 */
3079#ifdef __FreeBSD__
3080char which_boot_loader(char *which_device)
3081{
3082 int count_lilos = 0;
3083 int count_grubs = 0;
3084 int count_boot0s = 0;
3085 int count_dangerouslydedicated = 0;
3086
3087 log_it("looking at drive %s's MBR", which_device);
3088 if (does_string_exist_in_boot_block(which_device, "GRUB")) {
3089 count_grubs++;
3090 }
3091 if (does_string_exist_in_boot_block(which_device, "LILO")) {
3092 count_lilos++;
3093 }
3094 if (does_string_exist_in_boot_block(which_device, "Drive")) {
3095 count_boot0s++;
3096 }
3097 if (does_string_exist_in_first_N_blocks
3098 (which_device, "FreeBSD/i386", 17)) {
3099 count_dangerouslydedicated++;
3100 }
3101 log_it("%d grubs and %d lilos and %d elilos and %d boot0s and %d DD\n",
3102 count_grubs, count_lilos, count_elilos, count_boot0s,
3103 count_dangerouslydedicated);
3104
3105 if (count_grubs && !count_lilos) {
3106 return ('G');
3107 } else if (count_lilos && !count_grubs) {
3108 return ('L');
3109 } else if (count_grubs == 1 && count_lilos == 1) {
3110 log_it("I'll bet you used to use LILO but switched to GRUB...");
3111 return ('G');
3112 } else if (count_boot0s == 1) {
3113 return ('B');
3114 } else if (count_dangerouslydedicated) {
3115 return ('D');
3116 } else {
3117 log_it("Unknown boot loader");
3118 return ('U');
3119 }
3120}
3121
3122#else
3123
3124char which_boot_loader(char *which_device)
3125{
3126 /*@ buffer ***************************************************** */
3127 char *list_drives_cmd = NULL;
3128 char *current_drive;
3129 char *tmp;
3130
3131 /*@ pointers *************************************************** */
3132 FILE *pdrives;
3133
3134 /*@ int ******************************************************** */
3135 int count_lilos = 0;
3136 int count_grubs = 0;
3137
3138 /*@ end vars *************************************************** */
3139
3140#ifdef __IA64__
3141 /* No choice for it */
3142 return ('E');
3143#endif
3144 if (bkpinfo->boot_type == EFI) {
3145 /* No choice for it */
3146 return ('E');
3147 }
3148
3149 if (bkpinfo->boot_type == UEFI) {
3150 /* hardcoded for now. We can for sure do a better job here ! */
3151 /* RHEL, SLES, Mageia, Debian, Ubuntu use grub as boot loader as it seems for UEFI */
3152 return ('G');
3153 }
3154
3155 assert(which_device != NULL);
3156
3157 tmp = where_is_root_mounted();
3158 mr_asprintf(list_drives_cmd, "mr-parted2fdisk -l 2>/dev/null | grep \"/dev/.*:\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/; echo %s", tmp);
3159 log_it("list_drives_cmd = %s", list_drives_cmd);
3160 mr_free(tmp);
3161 if (!(pdrives = popen(list_drives_cmd, "r"))) {
3162 log_OS_error("Unable to open list of drives");
3163 mr_free(list_drives_cmd);
3164 return ('\0');
3165 }
3166 mr_free(list_drives_cmd);
3167
3168 malloc_string(current_drive);
3169 if (bkpinfo->boot_type == BIOS) {
3170 for (tmp = fgets(current_drive, MAX_STR_LEN, pdrives); !feof(pdrives) && (tmp != NULL); tmp = fgets(current_drive, MAX_STR_LEN, pdrives)) {
3171 strip_spaces(current_drive);
3172 log_it("looking at drive %s's MBR", current_drive);
3173 if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
3174 count_grubs++;
3175 strcpy(which_device, current_drive);
3176 break;
3177 }
3178 if (does_string_exist_in_boot_block(current_drive, "LILO")) {
3179 count_lilos++;
3180 strcpy(which_device, current_drive);
3181 break;
3182 }
3183 }
3184 if (pclose(pdrives)) {
3185 log_OS_error("Cannot pclose pdrives");
3186 }
3187 log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
3188 if (count_grubs && !count_lilos) {
3189 paranoid_free(current_drive);
3190 return ('G');
3191 } else if (count_lilos && !count_grubs) {
3192 paranoid_free(current_drive);
3193 return ('L');
3194 } else if (count_grubs == 1 && count_lilos == 1) {
3195 log_it("I'll bet you used to use LILO but switched to GRUB...");
3196 paranoid_free(current_drive);
3197 return ('G');
3198 } else {
3199 // We need to look on each partition then
3200 mr_asprintf(list_drives_cmd, "mr-parted2fdisk -l 2>/dev/null | grep -E \"^/dev/\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/");
3201 log_it("list_drives_cmd = %s", list_drives_cmd);
3202
3203 if (!(pdrives = popen(list_drives_cmd, "r"))) {
3204 log_OS_error("Unable to open list of drives");
3205 mr_free(list_drives_cmd);
3206 paranoid_free(current_drive);
3207 return ('\0');
3208 }
3209 mr_free(list_drives_cmd);
3210
3211 for (tmp = fgets(current_drive, MAX_STR_LEN, pdrives); !feof(pdrives) && (tmp != NULL);
3212 tmp = fgets(current_drive, MAX_STR_LEN, pdrives)) {
3213 strip_spaces(current_drive);
3214 log_it("looking at partition %s's BR", current_drive);
3215 if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
3216 count_grubs++;
3217 strcpy(which_device, current_drive);
3218 break;
3219 }
3220 if (does_string_exist_in_boot_block(current_drive, "LILO")) {
3221 count_lilos++;
3222 strcpy(which_device, current_drive);
3223 break;
3224 }
3225 }
3226 if (pclose(pdrives)) {
3227 log_OS_error("Cannot pclose pdrives");
3228 }
3229 log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
3230 paranoid_free(current_drive);
3231 if (count_grubs && !count_lilos) {
3232 return ('G');
3233 } else if (count_lilos && !count_grubs) {
3234 return ('L');
3235 } else if (count_grubs == 1 && count_lilos == 1) {
3236 log_it("I'll bet you used to use LILO but switched to GRUB...");
3237 return ('G');
3238 } else {
3239 log_it("Unknown boot loader");
3240 return ('U');
3241 }
3242 }
3243 }
3244}
3245#endif
3246
3247
3248
3249
3250/**
3251 * Write zeroes over the first 16K of @p device.
3252 * @param device The device to zero.
3253 * @return 0 for success, 1 for failure.
3254 */
3255int zero_out_a_device(char *device)
3256{
3257 FILE *fout;
3258 int i;
3259
3260 assert_string_is_neither_NULL_nor_zerolength(device);
3261
3262 log_it("Zeroing drive %s", device);
3263 if (!(fout = fopen(device, "w"))) {
3264 log_OS_error("Unable to open/write to device");
3265 return (1);
3266 }
3267 for (i = 0; i < 16384; i++) {
3268 fputc('\0', fout);
3269 }
3270 paranoid_fclose(fout);
3271 log_it("Device successfully zeroed.");
3272 return (0);
3273}
3274
3275/**
3276 * Return the device pointed to by @p incoming.
3277 * @param incoming The device to resolve symlinks for.
3278 * @return The path to the real device file.
3279 * @note The returned string points to static storage that will be overwritten with each call.
3280 * @bug Won't work with file v4.0; needs to be written in C.
3281 */
3282char *resolve_softlinks_to_get_to_actual_device_file(char *incoming)
3283{
3284 static char output[MAX_STR_LEN];
3285 char *command = NULL;
3286 char *curr_fname = NULL;
3287 char *scratch = NULL;
3288 char *tmp = NULL;
3289 char *p;
3290
3291 struct stat statbuf;
3292 malloc_string(curr_fname);
3293 if (!does_file_exist(incoming)) {
3294 log_it
3295 ("resolve_softlinks_to_get_to_actual_device_file --- device not found");
3296 strcpy(output, incoming);
3297 } else {
3298 strcpy(curr_fname, incoming);
3299 lstat(curr_fname, &statbuf);
3300 while (S_ISLNK(statbuf.st_mode)) {
3301 log_msg(1, "curr_fname = %s", curr_fname);
3302 mr_asprintf(command, "file %s", curr_fname);
3303 tmp = call_program_and_get_last_line_of_output(command);
3304 mr_free(command);
3305 for (p = tmp + strlen(tmp); p != tmp && *p != '`' && *p != ' ';
3306 p--);
3307 p++;
3308 mr_asprintf(scratch, "%s", p);
3309 for (p = scratch; *p != '\0' && *p != '\''; p++);
3310 *p = '\0';
3311 log_msg(0, "curr_fname %s --> '%s' --> %s", curr_fname, tmp, scratch);
3312 mr_free(tmp);
3313
3314 if (scratch[0] == '/') {
3315 strcpy(curr_fname, scratch); // copy whole thing because it's an absolute softlink
3316 } else { // copy over the basename cos it's a relative softlink
3317 p = curr_fname + strlen(curr_fname);
3318 while (p != curr_fname && *p != '/') {
3319 p--;
3320 }
3321 if (*p == '/') {
3322 p++;
3323 }
3324 strcpy(p, scratch);
3325 }
3326 mr_free(scratch);
3327 lstat(curr_fname, &statbuf);
3328 }
3329 strcpy(output, curr_fname);
3330 log_it("resolved %s to %s", incoming, output);
3331 }
3332 paranoid_free(curr_fname);
3333 return (output);
3334}
3335
3336/* @} - end of deviceGroup */
3337
3338/**
3339 * Return the type of partition format (GPT or MBR)
3340 */
3341char *which_partition_format(const char *drive)
3342{
3343 char *output = NULL;
3344 char *command = NULL;
3345
3346 mr_asprintf(command, "mr-disk-type %s", drive);
3347 output = call_program_and_get_last_line_of_output(command);
3348 mr_free(command);
3349
3350 log_msg(0, "Found %s partition table format type on %s", output, drive);
3351 return (output);
3352}
3353/* @} - end of deviceGroup */
3354
3355
Note: See TracBrowser for help on using the repository browser.