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

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