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

Last change on this file since 3877 was 3877, checked in by Bruno Cornec, 4 months ago

More compiler fixes

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