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

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