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

Last change on this file since 3413 was 3413, checked in by Bruno Cornec, 9 years ago

Fix remaining compilation warnings

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