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

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