source: MondoRescue/branches/3.3/mondo/src/common/libmondo-devices.c@ 3874

Last change on this file since 3874 was 3874, checked in by Bruno Cornec, 16 months ago

remove differentiated support for cdrw

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