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

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