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

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

find_dvd|cdrom_device merged into a rewritten find_optical_device - adds find_usb_device and find_device - mount_CDROM_here moved to mount_media (ongoing)

  • Property svn:keywords set to Id
File size: 88.6 KB
Line 
1/* libmondo-devices.c Subroutines for handling devices
2 $Id: libmondo-devices.c 3875 2024-03-08 02:19:14Z 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 3875 2024-03-08 02:19:14Z 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();
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 * Ask user for details of backup/restore information.
1828 * Called when @c mondoarchive doesn't get any parameters.
1829 * @param bkpinfo The backup information structure to fill out with the user's data.
1830 * @param archiving_to_media TRUE if archiving, FALSE if restoring.
1831 * @return 0, always.
1832 * @bug No point of `int' return value.
1833 * @ingroup archiveGroup
1834 */
1835int interactively_obtain_media_parameters_from_user(bool archiving_to_media)
1836// archiving_to_media is TRUE if I'm being called by mondoarchive
1837// archiving_to_media is FALSE if I'm being called by mondorestore
1838{
1839 char *tmp = NULL;
1840 char *sz = NULL;
1841 char *tmpro = NULL;
1842 char *tmp1 = NULL;
1843 char *tmp2 = NULL;
1844 char *mds = NULL;
1845 char *oldtmp = NULL;
1846 char *q = NULL;
1847 char p[16*MAX_STR_LEN];
1848 char *sz_size = NULL;
1849 char *command = NULL;
1850 char *compression_type = NULL;
1851 char *comment = NULL;
1852 int i;
1853 FILE *fin;
1854
1855 malloc_string(tmp1);
1856 assert(bkpinfo != NULL);
1857 bkpinfo->nonbootable_backup = FALSE;
1858
1859 // Tape, CD, NETFS, ...?
1860 srandom(getpid());
1861 bkpinfo->backup_media_type =
1862 (g_restoring_live_from_cd) ? cdr :
1863 which_backup_media_type(bkpinfo->restore_data);
1864 if (bkpinfo->backup_media_type == none) {
1865 log_to_screen("User has chosen not to backup the machine");
1866 finish(1);
1867 }
1868 log_msg(3, "media type = %s", bkptype_to_string(bkpinfo->backup_media_type));
1869
1870 /* Why asking to remove the media with tape ?
1871 if (bkpinfo->backup_media_type == tape && bkpinfo->restore_data) {
1872 popup_and_OK("Please remove media from drive(s)");
1873 }
1874 */
1875 if (archiving_to_media) {
1876 // TODO: Should be common ?
1877 setup_tmpdir(NULL);
1878 /*
1879 * Set the scratchdir to reside on the partition with the most free space.
1880 * Automatically excludes DOS, NTFS, SMB, and NFS filesystems.
1881 */
1882#ifdef __FreeBSD__
1883 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");
1884#else
1885 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");
1886#endif
1887
1888 if (tmp[0] != '/') {
1889 mr_asprintf(sz, "%s", tmp);
1890 mr_free(tmp);
1891 mr_asprintf(tmp, "/%s", sz);
1892 mr_free(sz);
1893 }
1894 setup_scratchdir(tmp);
1895 mr_free(tmp);
1896
1897 if ((compression_type = which_compression_type()) == NULL) {
1898 log_to_screen("User has chosen not to backup the machine");
1899 finish(1);
1900 }
1901
1902 bkpinfo->compression_level = (bkpinfo->backup_media_type == cdstream) ? 1 : 5;
1903 if ((bkpinfo->compression_level = which_compression_level()) == -1) {
1904 log_to_screen("User has chosen not to backup the machine");
1905 finish(1);
1906 }
1907 bkpinfo->cdrw_speed = (bkpinfo->backup_media_type == cdstream) ? 2 : 4;
1908 bkpinfo->use_lzo = (bkpinfo->backup_media_type == cdstream) ? TRUE : FALSE;
1909 }
1910 mvaddstr_and_log_it(2, 0, " ");
1911
1912 // Find device's /dev (or SCSI) entry
1913 switch (bkpinfo->backup_media_type) {
1914 case cdr:
1915 case dvd:
1916 case usb:
1917 /* Never try to eject a USB device */
1918 if (bkpinfo->backup_media_type == usb) {
1919 bkpinfo->please_dont_eject = TRUE;
1920 }
1921 mds = media_descriptor_string(bkpinfo->backup_media_type);
1922 if (bkpinfo->backup_media_type != usb) {
1923 if (ask_me_yes_or_no("Is your computer a laptop type (manual insert of MondoRescue media)?")) {
1924 bkpinfo->manual_cd_tray = TRUE;
1925 }
1926 }
1927
1928 if (bkpinfo->media_device == NULL) {
1929 bkpinfo->media_device = find_device(bkpinfo->backup_media_type);
1930 if (bkpinfo->media_device != NULL) {
1931 log_msg(1, "MondoRescue device found automatically for your %s is %s", mds, bkpinfo->media_device);
1932 } else {
1933 log_msg(1, "No MondoRescue device found yet for your %s", mds);
1934 }
1935 }
1936
1937 if (archiving_to_media) {
1938 if (bkpinfo->backup_media_type == dvd) {
1939 strcpy(tmp1, "1");
1940 mr_asprintf(sz_size, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4482 MB
1941 log_msg(1, "Setting to DVD defaults");
1942 } else {
1943 strcpy(tmp1, "4");
1944 mr_asprintf(sz_size, "%d", 650);
1945 log_msg(1, "Setting to CD defaults");
1946 }
1947
1948 mr_asprintf(comment, "What speed is your %s writer?", mds);
1949 if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) {
1950 if (!popup_and_get_string("Speed", comment, tmp1, 4)) {
1951 log_to_screen("User has chosen not to backup the machine");
1952 mr_free(comment);
1953 finish(1);
1954 }
1955 }
1956 mr_free(comment);
1957 bkpinfo->cdrw_speed = atoi(tmp1); // if DVD then this shouldn't ever be used anyway :)
1958
1959 strcpy(tmp1, sz_size);
1960 mr_asprintf(comment, "How much data (in Megabytes) will each %s store?", mds);
1961 if (!popup_and_get_string("Size", comment, tmp1, 5)) {
1962 log_to_screen("User has chosen not to backup the machine");
1963 finish(1);
1964 }
1965 mr_asprintf(sz_size, "%s", tmp1);
1966 bkpinfo->media_size = atoi(sz_size);
1967
1968 if (bkpinfo->media_size <= 0) {
1969 log_to_screen("User has chosen not to backup the machine");
1970 finish(1);
1971 }
1972 }
1973 /* No break because we continue even for usb */
1974 case cdstream:
1975
1976 // If media_device not found ask
1977 if (bkpinfo->media_device == NULL) {
1978 mr_asprintf(comment, "Please specify your Mondorescue %s media /dev entry", mds);
1979 tmp2 = mr_popup_and_get_string("/dev entry?", comment, bkpinfo->media_device);
1980 if (!tmp2) {
1981 log_to_screen("User has chosen not to backup the machine");
1982 finish(1);
1983 } else {
1984 mr_free(bkpinfo->media_device);
1985 bkpinfo->media_device = tmp2;
1986 }
1987 }
1988 log_msg(2, "%s device found at %s", mds, bkpinfo->media_device);
1989 mr_asprintf(tmp, "MondoRescue thinks your %s media corresponds to %s. Is this correct?", mds, bkpinfo->media_device);
1990 if (!ask_me_yes_or_no(tmp)) {
1991 mr_free(bkpinfo->media_device);
1992 }
1993 mr_free(tmp);
1994
1995 if (bkpinfo->media_device == NULL) {
1996 mr_asprintf(tmp, "Please then specify your Mondorescue %s media /dev entry", mds);
1997 tmp2 = mr_popup_and_get_string("/dev entry?", tmp, bkpinfo->media_device);
1998 mr_free(tmp);
1999 if (tmp2 == NULL) {
2000 log_to_screen("User has chosen not to backup the machine");
2001 finish(1);
2002 } else {
2003 mr_free(bkpinfo->media_device);
2004 bkpinfo->media_device = tmp2;
2005 }
2006 }
2007 mr_free(mds);
2008
2009 if (bkpinfo->backup_media_type == cdstream) {
2010 bkpinfo->media_size = 650;
2011 }
2012 break;
2013 case udev:
2014 if (!ask_me_yes_or_no
2015 ("This option is for advanced users only. Are you sure?")) {
2016 log_to_screen("User has chosen not to backup the machine");
2017 finish(1);
2018 }
2019 case tape:
2020
2021 bkpinfo->media_device = find_tape_device();
2022 if (bkpinfo->media_device != NULL) {
2023 if ((fin = fopen(bkpinfo->media_device, "r"))) {
2024 paranoid_fclose(fin);
2025 } else {
2026 if (does_file_exist("/tmp/mondorestore.cfg")) {
2027 read_cfg_var("/tmp/mondorestore.cfg", "media-dev", bkpinfo->media_device);
2028 }
2029 }
2030 }
2031 if (bkpinfo->media_device != NULL) {
2032 mr_asprintf(tmp, "Mondorescue thinks your tape streamer at %s; is that correct?", bkpinfo->media_device);
2033 if (!ask_me_yes_or_no(tmp)) {
2034 mr_free(bkpinfo->media_device);
2035 }
2036 mr_free(tmp);
2037 }
2038 if (bkpinfo->media_device == NULL) {
2039 tmp2 = mr_popup_and_get_string("Device name?", "What is the /dev entry of your tape streamer?", "");
2040 if (tmp2 == NULL) {
2041 log_to_screen("User has chosen not to backup the machine");
2042 finish(1);
2043 } else {
2044 bkpinfo->media_device = tmp2;
2045 }
2046 }
2047 mr_asprintf(tmp, "ls -l %s", bkpinfo->media_device);
2048 if (run_program_and_log_output(tmp, FALSE)) {
2049 log_to_screen("User has not specified a valid /dev entry");
2050 finish(1);
2051 }
2052 mr_free(tmp);
2053
2054 mr_asprintf(sz_size,"%ld",bkpinfo->internal_tape_block_size);
2055 tmp = mr_popup_and_get_string("Tape block size?", "What is the block size of your tape streamer?", sz_size);
2056 if (tmp == NULL) {
2057 log_to_screen("User has chosen not to backup the machine");
2058 finish(1);
2059 }
2060 bkpinfo->internal_tape_block_size = atol(tmp);
2061 mr_free(sz_size);
2062 mr_free(tmp);
2063
2064 // log the block-size
2065 log_msg(0,"Tape block size= %ld", bkpinfo->internal_tape_block_size);
2066
2067 if (bkpinfo->internal_tape_block_size <= 0) {
2068 log_to_screen("User has chosen not to backup the machine");
2069 finish(1);
2070 }
2071
2072 bkpinfo->media_size = 0;
2073 log_msg(4, "media_size = %ld", bkpinfo->media_size);
2074
2075 if (archiving_to_media) {
2076 bkpinfo->use_obdr = ask_me_yes_or_no("Do you want to activate OBDR support for your tapes ?");
2077 if (bkpinfo->use_obdr) {
2078 log_msg(4, "obdr mode = TRUE");
2079 } else {
2080 log_msg(4, "obdr mode = FALSE");
2081 }
2082 }
2083 break;
2084
2085 case netfs:
2086 /* Never try to eject a NETFS device */
2087 bkpinfo->please_dont_eject = TRUE;
2088 /* Force NFS to be the protocol by default */
2089 if (bkpinfo->netfs_proto == NULL) {
2090 mr_asprintf(bkpinfo->netfs_proto, "nfs");
2091 }
2092
2093 /* Initiate bkpinfo netfs_mount path from running environment if not already done */
2094 if (bkpinfo->netfs_mount == NULL) {
2095 bkpinfo->netfs_mount = call_program_and_get_last_line_of_output("mount | grep \":\" | cut -d' ' -f1 | head -n1");
2096 }
2097#ifdef __FreeBSD__
2098 if (TRUE)
2099#else
2100 if (!bkpinfo->disaster_recovery)
2101#endif
2102 {
2103 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)) {
2104 log_to_screen("User has chosen not to backup the machine");
2105 finish(1);
2106 }
2107 mr_free(bkpinfo->netfs_mount);
2108 // check whether already mounted - we better remove
2109 // surrounding spaces and trailing '/' for this
2110 bkpinfo->netfs_mount = mr_strip_spaces(p);
2111 if (bkpinfo->netfs_mount[strlen(bkpinfo->netfs_mount) - 1] == '/')
2112 bkpinfo->netfs_mount[strlen(bkpinfo->netfs_mount) - 1] = '\0';
2113 q = strchr(bkpinfo->netfs_mount, '@');
2114 if (q != NULL) {
2115 /* User found. Store the 2 values */
2116 q++;
2117 /* new netfs mount */
2118 strcpy(tmp1,q);
2119 } else {
2120 strcpy(tmp1,bkpinfo->netfs_mount);
2121 }
2122 mr_asprintf(command, "mount | grep \"%s \" | cut -d' ' -f3", tmp1);
2123 mr_free(bkpinfo->isodir);
2124 bkpinfo->isodir = call_program_and_get_last_line_of_output(command);
2125 mr_free(command);
2126
2127 if (!bkpinfo->restore_data) {
2128 mr_asprintf(sz_size, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4482 MB
2129 mr_asprintf(comment, "How much data (in Megabytes) will each media store?");
2130 strcpy(tmp1, sz_size);
2131 mr_free(sz_size);
2132 if (!popup_and_get_string("Size", comment, tmp1, 5)) {
2133 log_to_screen("User has chosen not to backup the machine");
2134 finish(1);
2135 }
2136 mr_free(comment);
2137 mr_asprintf(sz_size, "%s", tmp1);
2138 } else {
2139 mr_asprintf(sz_size, "0");
2140 }
2141 bkpinfo->media_size = atoi(sz_size);
2142 mr_free(sz_size);
2143
2144 if (bkpinfo->media_size < 0) {
2145 log_to_screen("User has chosen not to backup the machine");
2146 finish(1);
2147 }
2148 }
2149 /* TODO: Useless I think */
2150 if (bkpinfo->disaster_recovery) {
2151 mr_asprintf(command ,"umount %s/isodir 2> /dev/null", bkpinfo->tmpdir);
2152 paranoid_system(command);
2153 mr_free(command);
2154
2155 }
2156 strcpy(tmp1, bkpinfo->netfs_proto);
2157 if (!popup_and_get_string("Network protocol", "Which protocol should I use (nfs/sshfs/smbfs) ?",tmp1, MAX_STR_LEN-1)) {
2158 log_to_screen("User has chosen not to backup the machine");
2159 finish(1);
2160 }
2161 mr_free(bkpinfo->netfs_proto);
2162 mr_asprintf(bkpinfo->netfs_proto, "%s", tmp1);
2163
2164 strcpy(tmp1, bkpinfo->netfs_mount);
2165 if (!popup_and_get_string("Network share", "Which remote share should I mount?", tmp1, MAX_STR_LEN-1)) {
2166 log_to_screen("User has chosen not to backup the machine");
2167 finish(1);
2168 }
2169 mr_free(bkpinfo->netfs_mount);
2170 mr_asprintf(bkpinfo->netfs_mount, "%s", tmp1);
2171
2172 if (bkpinfo->netfs_user) {
2173 strcpy(tmp1, bkpinfo->netfs_user);
2174 } else {
2175 strcpy(tmp1, "");
2176 }
2177 if (!popup_and_get_string("Network user", "Which user should I use if any ?",tmp1, MAX_STR_LEN-1)) {
2178 log_to_screen("User has chosen not to backup the machine");
2179 finish(1);
2180 }
2181 mr_free(bkpinfo->netfs_user);
2182 if (strcmp(tmp1, "") != 0) {
2183 mr_asprintf(bkpinfo->netfs_user, "%s", tmp1);
2184 }
2185
2186 /* Initiate bkpinfo isodir path from running environment if mount already done */
2187 log_msg(3, "Testing mount for %s", bkpinfo->netfs_mount);
2188 if (is_this_device_mounted(bkpinfo->netfs_mount)) {
2189 mr_free(bkpinfo->isodir);
2190 bkpinfo->isodir = call_program_and_get_last_line_of_output("mount | grep \":\" | cut -d' ' -f3 | head -n1");
2191 } else {
2192 // Why netfsdir ?
2193 mr_asprintf(bkpinfo->isodir, "%s/netfsdir", bkpinfo->tmpdir);
2194 mr_asprintf(command, "mkdir -p %s", bkpinfo->isodir);
2195 run_program_and_log_output(command, 5);
2196 mr_free(command);
2197
2198 if (bkpinfo->restore_data) {
2199 /* mount the FS read-only in restore mode to avoid any erase of whatever */
2200 mr_asprintf(tmpro, "-o ro");
2201 } else {
2202 mr_asprintf(tmpro, "");
2203 }
2204
2205 /* Build the mount string */
2206 if (strstr(bkpinfo->netfs_proto, "smbfs")) {
2207 mr_asprintf(tmp, "mount -t cifs %s %s %s",bkpinfo->netfs_mount, bkpinfo->isodir,tmpro);
2208 if (bkpinfo->netfs_user) {
2209 mr_strcat(tmp, " -o user=%s", bkpinfo->netfs_user);
2210 }
2211 else {
2212 if (strstr(bkpinfo->netfs_proto, "sshfs")) {
2213 mr_asprintf(tmp, "sshfs %s ",tmpro);
2214 } else {
2215 mr_asprintf(tmp, "mount -t %s -o nolock %s ", bkpinfo->netfs_proto,tmpro);
2216 }
2217 if (bkpinfo->netfs_user) {
2218 mr_strcat(tmp, "%s@", bkpinfo->netfs_user);
2219 }
2220 mr_strcat(tmp, "%s %s", bkpinfo->netfs_mount, bkpinfo->isodir);
2221 }
2222 run_program_and_log_output(tmp, 3);
2223 mr_free(tmp);
2224
2225 malloc_string(g_selfmounted_isodir);
2226 strcpy(g_selfmounted_isodir, bkpinfo->isodir);
2227 }
2228 }
2229
2230 log_msg(3, "Testing mount for %s", bkpinfo->netfs_mount);
2231 if (!is_this_device_mounted(bkpinfo->netfs_mount)) {
2232 popup_and_OK("Please mount that partition before you try to backup to or restore from it.");
2233 finish(1);
2234 }
2235 if (bkpinfo->netfs_remote_dir == NULL) {
2236 fatal_error("bkpinfo->netfs_remote_dir should not be NULL");
2237 }
2238 strcpy(tmp1, bkpinfo->netfs_remote_dir);
2239 if (!popup_and_get_string ("Directory", "Which directory within that mountpoint?", tmp1, MAX_STR_LEN-1)) {
2240 log_to_screen("User has chosen not to backup the machine");
2241 finish(1);
2242 }
2243 mr_free(bkpinfo->netfs_remote_dir);
2244 // check whether writable - we better remove surrounding spaces for this
2245 bkpinfo->netfs_remote_dir = mr_strip_spaces(tmp1);
2246
2247 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);
2248 if (tmp == NULL) {
2249 log_to_screen("User has chosen not to backup the machine");
2250 finish(1);
2251 }
2252 mr_free(bkpinfo->prefix);
2253 bkpinfo->prefix = tmp;
2254 log_msg(3, "prefix set to %s", bkpinfo->prefix);
2255 log_msg(3, "Just set netfs_remote_dir to %s", bkpinfo->netfs_remote_dir);
2256 log_msg(3, "isodir is still %s", bkpinfo->isodir);
2257 break;
2258
2259 case iso:
2260 if (!bkpinfo->disaster_recovery) {
2261 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);
2262 if (tmp == NULL) {
2263 log_to_screen("User has chosen not to backup the machine");
2264 finish(1);
2265 } else {
2266 mr_free(bkpinfo->isodir);
2267 bkpinfo->isodir = tmp;
2268 }
2269 if (archiving_to_media) {
2270 sprintf(tmp1, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4482 MB
2271 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)) {
2272 log_to_screen("User has chosen not to backup the machine");
2273 finish(1);
2274 }
2275 bkpinfo->media_size = atoi(tmp1);
2276 } else {
2277 bkpinfo->media_size = 650;
2278 }
2279 }
2280 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);
2281 if (tmp == NULL) {
2282 log_to_screen("User has chosen not to backup the machine");
2283 finish(1);
2284 }
2285 mr_free(bkpinfo->prefix);
2286 bkpinfo->prefix = tmp;
2287 log_msg(3, "prefix set to %s", bkpinfo->prefix);
2288 break;
2289 default:
2290 fatal_error("I, Mojo Jojo, shall defeat those pesky Powerpuff Girls!");
2291 }
2292
2293 if (archiving_to_media) {
2294 /* Needs to be done before calling which_boot_loader */
2295 bkpinfo->boot_type = mr_boot_type();
2296 mr_free(bkpinfo->boot_device);
2297#ifdef __FreeBSD__
2298 bkpinfo->boot_device = call_program_and_get_last_line_of_output("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/\\([0-9]\\).*/\\1/'");
2299#else
2300 bkpinfo->boot_device = call_program_and_get_last_line_of_output("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'");
2301#endif
2302 i = which_boot_loader(bkpinfo->boot_device);
2303 if (i == 'U') // unknown
2304 {
2305
2306#ifdef __FreeBSD__
2307 if (!popup_and_get_string("Boot device", "What is your boot device? (e.g. /dev/ad0)", bkpinfo->boot_device,(MAX_STR_LEN / 4)-1)) {
2308 log_to_screen("User has chosen not to backup the machine");
2309 finish(1);
2310 }
2311 i = which_boot_loader(bkpinfo->boot_device);
2312#else
2313 strcpy(tmp1, bkpinfo->boot_device);
2314 if (!popup_and_get_string("Boot device", "What is your boot device? (e.g. /dev/hda)", tmp1,(MAX_STR_LEN / 4)-1)) {
2315 log_to_screen("User has chosen not to backup the machine");
2316 finish(1);
2317 }
2318 mr_free(bkpinfo->boot_device);
2319 mr_asprintf(bkpinfo->boot_device, "%s", tmp1);
2320
2321 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "ELILO")) {
2322 i = 'E';
2323 } else
2324 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "LILO")) {
2325 i = 'L';
2326 } else
2327 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "GRUB")) {
2328 i = 'G';
2329 } else {
2330 i = 'U';
2331 }
2332#endif
2333 if (i == 'U') {
2334 if (ask_me_yes_or_no("Unidentified boot loader. Shall I restore it byte-for-byte at restore time and hope for the best?")) {
2335 i = 'R'; // raw
2336 } else {
2337 log_to_screen("I cannot find your boot loader. Please run mondoarchive with parameters.");
2338 finish(1);
2339 }
2340 }
2341 }
2342 bkpinfo->boot_loader = i;
2343 /* TODO: Check consistency of boot type and boot loader */
2344
2345 if (bkpinfo->include_paths) {
2346 strcpy(tmp1, bkpinfo->include_paths);
2347 mr_free(bkpinfo->include_paths);
2348 } else {
2349 strcpy(tmp1, "/");
2350 }
2351 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)) {
2352 log_to_screen("User has chosen not to backup the machine");
2353 finish(1);
2354 }
2355 mr_asprintf(bkpinfo->include_paths, "%s", tmp1);
2356
2357 tmp = list_of_NETFS_mounts_only();
2358 if (strlen(tmp) > 2) {
2359 mr_strcat(bkpinfo->exclude_paths, "|%s",tmp);
2360 }
2361 mr_free(tmp);
2362// NTFS
2363 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};'");
2364 strncpy(tmp1, tmp,(MAX_STR_LEN / 4)-1);
2365 mr_free(tmp);
2366 if (strlen(tmp1) > 2) {
2367 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)) {
2368 log_to_screen("User has chosen not to backup the machine");
2369 finish(1);
2370 }
2371 mr_asprintf(bkpinfo->image_devs, "%s", tmp1);
2372 }
2373
2374 if (bkpinfo->exclude_paths != NULL ) {
2375 mr_asprintf(p, "%s", bkpinfo->exclude_paths);
2376 } else {
2377 mr_asprintf(p, "%s", "");
2378 }
2379 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);
2380 mr_free(p);
2381 if (tmp == NULL) {
2382 log_to_screen("User has chosen not to backup the machine");
2383 finish(1);
2384 }
2385 mr_free(bkpinfo->exclude_paths);
2386 bkpinfo->exclude_paths = tmp;
2387
2388 mr_asprintf(oldtmp, "%s", bkpinfo->tmpdir);
2389 if (bkpinfo->tmpdir != NULL ) {
2390 mr_asprintf(p, "%s", bkpinfo->tmpdir);
2391 } else {
2392 mr_asprintf(p, "%s", "");
2393 }
2394 tmp = mr_popup_and_get_string("Temporary directory", "Please enter your temporary directory.", p);
2395 mr_free(p);
2396 if (tmp == NULL) {
2397 mr_free(oldtmp);
2398 log_to_screen("User has chosen not to backup the machine");
2399 finish(1);
2400 }
2401 /* if modified to another path */
2402 if (strcmp(tmp,oldtmp) != 0) {
2403 setup_tmpdir(tmp);
2404 }
2405 mr_free(oldtmp);
2406
2407 mr_asprintf(oldtmp, "%s", bkpinfo->scratchdir);
2408 if (bkpinfo->scratchdir != NULL ) {
2409 mr_asprintf(p, "%s", bkpinfo->scratchdir);
2410 } else {
2411 mr_asprintf(p, "%s", "");
2412 }
2413 tmp = mr_popup_and_get_string("Scratch directory", "Please enter your scratch directory.", p);
2414 mr_free(p);
2415 if (tmp == NULL) {
2416 mr_free(oldtmp);
2417 log_to_screen("User has chosen not to backup the machine");
2418 finish(1);
2419 }
2420 /* if modified to another path */
2421 if (strcmp(tmp,oldtmp) != 0) {
2422 setup_scratchdir(tmp);
2423 }
2424 mr_free(oldtmp);
2425
2426 if (ask_me_yes_or_no("Do you want to backup extended attributes?")) {
2427 mr_free(g_getfattr);
2428 g_getfattr = find_home_of_exe("getfattr");
2429 mr_free(g_getfacl);
2430 g_getfacl = find_home_of_exe("getfacl");
2431 log_it("Backup of extended attributes");
2432 }
2433// Interactive mode:
2434#ifdef __IA64__
2435 bkpinfo->make_cd_use_lilo = TRUE;
2436#else
2437 bkpinfo->make_cd_use_lilo = FALSE;
2438#endif
2439 bkpinfo->backup_data = TRUE;
2440 if (strcmp(compression_type,"lzo") == 0) {
2441 mr_asprintf(bkpinfo->zip_exe, "%s", "lzop");
2442 mr_asprintf(bkpinfo->zip_suffix, "%s", "lzo");
2443 } else if (strcmp(compression_type,"gzip") == 0) {
2444 mr_asprintf(bkpinfo->zip_exe, "%s", "gzip");
2445 mr_asprintf(bkpinfo->zip_suffix, "%s", "gz");
2446 } else if (strcmp(compression_type,"lzma") == 0) {
2447 mr_asprintf(bkpinfo->zip_exe, "%s", "xz");
2448 mr_asprintf(bkpinfo->zip_suffix, "%s", "xz");
2449 } else if (strcmp(compression_type,"bzip2") == 0) {
2450 mr_asprintf(bkpinfo->zip_exe, "%s", "bzip2");
2451 mr_asprintf(bkpinfo->zip_suffix, "%s", "bz2");
2452 } else {
2453 mr_free(bkpinfo->zip_exe);
2454 mr_free(bkpinfo->zip_suffix);
2455 }
2456 mr_free(compression_type);
2457
2458#if __FreeBSD__ == 5
2459 mr_asprintf(bkpinfo->kernel_path, "%s", "/boot/kernel/kernel");
2460#elif __FreeBSD__ == 4
2461 mr_asprintf(bkpinfo->kernel_path, "%s", "/kernel");
2462#elif linux
2463 if (figure_out_kernel_path_interactively_if_necessary(bkpinfo->kernel_path)) {
2464 fatal_error("Kernel not found. Please specify manually with the '-k' switch.");
2465 }
2466#else
2467#error "I don't know about this system!"
2468#endif
2469
2470
2471 bkpinfo->verify_data =
2472 ask_me_yes_or_no
2473 ("Will you want to verify your backups after Mondo has created them?");
2474
2475 if (!ask_me_yes_or_no
2476 ("Are you sure you want to proceed? Hit 'no' to abort.")) {
2477 log_to_screen("User has chosen not to backup the machine");
2478 finish(1);
2479 }
2480 } else {
2481 bkpinfo->restore_data = TRUE; // probably...
2482 }
2483
2484 if (bkpinfo->backup_media_type == iso
2485 || bkpinfo->backup_media_type == netfs) {
2486 g_ISO_restore_mode = TRUE;
2487 }
2488#ifdef __FreeSD__
2489// skip
2490#else
2491 if (bkpinfo->backup_media_type == netfs) {
2492 log_msg(3, "I think the Remote mount is mounted at %s", bkpinfo->isodir);
2493 }
2494 log_it("isodir = %s", bkpinfo->isodir);
2495 if (bkpinfo->netfs_mount) {
2496 log_it("netfs_mount = '%s'", bkpinfo->netfs_mount);
2497 }
2498 if (bkpinfo->netfs_user) {
2499 log_it("netfs_user = '%s'", bkpinfo->netfs_user);
2500 }
2501 if (bkpinfo->netfs_proto) {
2502 log_it("netfs_proto = '%s'", bkpinfo->netfs_proto);
2503 }
2504#endif
2505
2506 log_it("media device = %s", bkpinfo->media_device);
2507 log_it("media size = %ld", bkpinfo->media_size);
2508 log_it("media type = %s", bkptype_to_string(bkpinfo->backup_media_type));
2509 if (bkpinfo->prefix != NULL) {
2510 log_it("prefix = %s", bkpinfo->prefix);
2511 }
2512 log_it("compression = %ld", bkpinfo->compression_level);
2513 log_it("exclude_path = %s", bkpinfo->exclude_paths);
2514 if (bkpinfo->include_paths) {
2515 log_it("include_path = %s", bkpinfo->include_paths);
2516 }
2517
2518 /* Handle devices passed in bkpinfo and print result */
2519 /* the mr_make_devlist_from_pathlist function appends */
2520 /* to the *_paths variables so copy before */
2521 mr_make_devlist_from_pathlist(bkpinfo->exclude_paths, 'E');
2522 mr_make_devlist_from_pathlist(bkpinfo->include_paths, 'I');
2523
2524 log_it("scratchdir = '%s'", bkpinfo->scratchdir);
2525 log_it("tmpdir = '%s'", bkpinfo->tmpdir);
2526 if (bkpinfo->image_devs) {
2527 log_it("image_devs = '%s'", bkpinfo->image_devs);
2528 }
2529 log_it("boot_device = '%s' (loader=%c)", bkpinfo->boot_device, bkpinfo->boot_loader);
2530 if (bkpinfo->media_size < 0) {
2531 if (archiving_to_media) {
2532 fatal_error("Media size is less than zero.");
2533 } else {
2534 log_msg(2, "Warning - media size is less than zero.");
2535 bkpinfo->media_size = 0;
2536 }
2537 }
2538 paranoid_free(sz_size);
2539 paranoid_free(tmp1);
2540 return (0);
2541}
2542
2543
2544/**
2545 * Get a |-separated list of NETFS mounts.
2546 * @return The list created.
2547 * @note The return value points to allocated string that needs to be freed by
2548 * caller
2549 * @bug Even though we only want the mounts, the devices are still checked.
2550 */
2551char *list_of_NETFS_mounts_only(void)
2552{
2553 char *exclude_these_directories = NULL;
2554
2555 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;}'");
2556 log_msg(9,"list_of_NETFS_mounts_only returns %s",exclude_these_directories);
2557 return(exclude_these_directories);
2558}
2559
2560/* @} - end of utilityGroup */
2561
2562
2563
2564
2565
2566/**
2567 * Create a randomly-named FIFO. The format is @p stub "." [random] [random] where
2568 * [random] is a random number between 1 and 32767.
2569 * @param store_name_here Where to store the new filename.
2570 * @param stub A random number will be appended to this to make the FIFO's name.
2571 * @ingroup deviceGroup
2572 */
2573void make_fifo(char *store_name_here, char *stub)
2574{
2575 char *tmp = NULL;
2576
2577 assert_string_is_neither_NULL_nor_zerolength(stub);
2578
2579 sprintf(store_name_here, "%s%d%d", stub, (int) (random() % 32768),
2580 (int) (random() % 32768));
2581 make_hole_for_file(store_name_here);
2582 mkfifo(store_name_here, S_IRWXU | S_IRWXG);
2583 mr_asprintf(tmp, "chmod 770 %s", store_name_here);
2584 paranoid_system(tmp);
2585 mr_free(tmp);
2586}
2587
2588
2589
2590/**
2591 * @addtogroup deviceGroup
2592 * @{
2593 */
2594/**
2595 * If we can read @p dev, set @p output to it.
2596 * If @p dev cannot be read, set @p output to "".
2597 * @param dev The device to check for.
2598 * @param output Set to @p dev if @p dev exists, "" otherwise.
2599 * @return TRUE if @p dev exists, FALSE if it doesn't.
2600 */
2601bool set_dev_to_this_if_rx_OK(char *output, char *dev)
2602{
2603 char *command = NULL;
2604 bool ret=FALSE;
2605
2606 if (!dev || dev[0] == '\0') {
2607 output[0] = '\0';
2608 return(ret);
2609 }
2610// assert_string_is_neither_NULL_nor_zerolength(dev);
2611 if (!bkpinfo->please_dont_eject) {
2612 log_msg(10, "Injecting %s", dev);
2613 inject_device(dev);
2614 }
2615 if (!does_file_exist(dev)) {
2616 log_msg(10, "%s doesn't exist. Returning FALSE.", dev);
2617 return(ret);
2618 }
2619 mr_asprintf(command, "dd bs=%ld count=1 if=%s of=/dev/null &> /dev/null", 512L, dev);
2620 if (!run_program_and_log_output(command, FALSE) && !run_program_and_log_output(command, FALSE)) {
2621 strcpy(output, dev);
2622 log_msg(4, "Found it - %s", dev);
2623 ret = TRUE;
2624 } else {
2625 output[0] = '\0';
2626 log_msg(4, "It's not %s", dev);
2627 }
2628 mr_free(command);
2629 return(ret);
2630}
2631
2632
2633
2634
2635
2636/**
2637 * Find out what number CD is in the drive.
2638 * @param bkpinfo The backup information structure. The @c bkpinfo->media_device field is the only one used.
2639 * @return The current CD number, or -1 if it could not be found.
2640 * @note If the CD is not mounted, it will be mounted
2641 * (and remain mounted after this function returns).
2642 */
2643int what_number_cd_is_this(void) {
2644
2645 int cd_number = -1;
2646 char *mountdev = NULL;
2647 char *tmp = NULL;
2648
2649 assert(bkpinfo != NULL);
2650// log_it("Asking what_number_cd_is_this");
2651 if ((g_ISO_restore_mode) || (g_restoring_live_from_cd)) {
2652 tmp = call_program_and_get_last_line_of_output("mount | grep iso9660 | awk '{print $3;}'");
2653 mr_asprintf(mountdev, "%s%s", tmp, "/archives/THIS-CD-NUMBER");
2654 mr_free(tmp);
2655 cd_number = atoi(last_line_of_file(mountdev));
2656 mr_free(mountdev);
2657 return (cd_number);
2658 }
2659
2660 if ((bkpinfo->media_device == NULL) || !does_file_exist(bkpinfo->media_device)) {
2661 log_it("ERROR: bkpinfo->media_device shoulnd't be unaccessible here\n");
2662 /* trying again ! */
2663 bkpinfo->media_device = find_optical_device();
2664 }
2665 if ((bkpinfo->media_device == NULL) || !does_file_exist(bkpinfo->media_device)) {
2666 fatal_error("ERROR: bkpinfo->media_device shoulnd't really be unaccessible here\n");
2667 }
2668 if (!is_this_device_mounted(bkpinfo->media_device, MNT_CDROM)) {
2669 mount_media(MNT_CDROM);
2670 }
2671
2672 cd_number = atoi(last_line_of_file(MNT_CDROM "/archives/THIS-CD-NUMBER"));
2673 return(cd_number);
2674}
2675
2676
2677/**
2678 * Find out what device is mounted as root (/).
2679 * @return Root device.
2680 * @note The returned string points to storage that needs to be freed by
2681 * caller
2682 * @bug A bit of a misnomer; it's actually finding out the root device.
2683 * The mountpoint (where it's mounted) will obviously be '/'.
2684 */
2685char *where_is_root_mounted() {
2686
2687/*@ buffers **************** */
2688char *tmp = NULL;
2689
2690#ifdef __FreeBSD__
2691 tmp = call_program_and_get_last_line_of_output("mount | grep \" on / \" | cut -d' ' -f1");
2692#else
2693 tmp = call_program_and_get_last_line_of_output("mount | grep \" on / \" | cut -d' ' -f1 | sed s/[0-9]// | sed s/[0-9]//");
2694 if (strstr(tmp, "/dev/cciss/")) {
2695 mr_free(tmp);
2696 tmp = call_program_and_get_last_line_of_output("mount | grep \" on / \" | cut -d' ' -f1 | cut -dp -f1");
2697 }
2698 if (strstr(tmp, "/dev/md")) {
2699 mr_free(tmp);
2700 tmp = call_program_and_get_last_line_of_output("mount | grep \" on / \" | cut -d' ' -f1");
2701 }
2702#endif
2703
2704return (tmp);
2705}
2706
2707
2708/**
2709 * Find out which boot loader is in use.
2710 * @param which_device Device to look for the boot loader on.
2711 * @return 'L' for LILO, 'E'for ELILO, 'G' for GRUB, 'B' or 'D' for FreeBSD boot loaders, or 'U' for Unknown.
2712 * @note Under Linux, all drives are examined, not just @p which_device.
2713 */
2714#ifdef __FreeBSD__
2715char which_boot_loader(char *which_device)
2716{
2717 int count_lilos = 0;
2718 int count_grubs = 0;
2719 int count_boot0s = 0;
2720 int count_dangerouslydedicated = 0;
2721
2722 log_it("looking at drive %s's MBR", which_device);
2723 if (does_string_exist_in_boot_block(which_device, "GRUB")) {
2724 count_grubs++;
2725 }
2726 if (does_string_exist_in_boot_block(which_device, "LILO")) {
2727 count_lilos++;
2728 }
2729 if (does_string_exist_in_boot_block(which_device, "Drive")) {
2730 count_boot0s++;
2731 }
2732 if (does_string_exist_in_first_N_blocks
2733 (which_device, "FreeBSD/i386", 17)) {
2734 count_dangerouslydedicated++;
2735 }
2736 log_it("%d grubs and %d lilos and %d elilos and %d boot0s and %d DD\n",
2737 count_grubs, count_lilos, count_elilos, count_boot0s,
2738 count_dangerouslydedicated);
2739
2740 if (count_grubs && !count_lilos) {
2741 return ('G');
2742 } else if (count_lilos && !count_grubs) {
2743 return ('L');
2744 } else if (count_grubs == 1 && count_lilos == 1) {
2745 log_it("I'll bet you used to use LILO but switched to GRUB...");
2746 return ('G');
2747 } else if (count_boot0s == 1) {
2748 return ('B');
2749 } else if (count_dangerouslydedicated) {
2750 return ('D');
2751 } else {
2752 log_it("Unknown boot loader");
2753 return ('U');
2754 }
2755}
2756
2757#else
2758
2759char which_boot_loader(char *which_device)
2760{
2761 /*@ buffer ***************************************************** */
2762 char *list_drives_cmd = NULL;
2763 char *current_drive;
2764 char *tmp;
2765
2766 /*@ pointers *************************************************** */
2767 FILE *pdrives;
2768
2769 /*@ int ******************************************************** */
2770 int count_lilos = 0;
2771 int count_grubs = 0;
2772
2773 /*@ end vars *************************************************** */
2774
2775#ifdef __IA64__
2776 /* No choice for it */
2777 return ('E');
2778#endif
2779 if (bkpinfo->boot_type == EFI) {
2780 /* No choice for it */
2781 return ('E');
2782 }
2783
2784 if (bkpinfo->boot_type == UEFI) {
2785 /* hardcoded for now. We can for sure do a better job here ! */
2786 /* RHEL, SLES, Mageia, Debian, Ubuntu use grub as boot loader as it seems for UEFI */
2787 return ('G');
2788 }
2789
2790 assert(which_device != NULL);
2791
2792 tmp = where_is_root_mounted();
2793 mr_asprintf(list_drives_cmd, "mr-parted2fdisk -l 2>/dev/null | grep \"/dev/.*:\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/; echo %s", tmp);
2794 log_it("list_drives_cmd = %s", list_drives_cmd);
2795 mr_free(tmp);
2796 if (!(pdrives = popen(list_drives_cmd, "r"))) {
2797 log_OS_error("Unable to open list of drives");
2798 mr_free(list_drives_cmd);
2799 return ('\0');
2800 }
2801 mr_free(list_drives_cmd);
2802
2803 malloc_string(current_drive);
2804 if (bkpinfo->boot_type == BIOS) {
2805 for (tmp = fgets(current_drive, MAX_STR_LEN, pdrives); !feof(pdrives) && (tmp != NULL); tmp = fgets(current_drive, MAX_STR_LEN, pdrives)) {
2806 strip_spaces(current_drive);
2807 log_it("looking at drive %s's MBR", current_drive);
2808 if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
2809 count_grubs++;
2810 strcpy(which_device, current_drive);
2811 break;
2812 }
2813 if (does_string_exist_in_boot_block(current_drive, "LILO")) {
2814 count_lilos++;
2815 strcpy(which_device, current_drive);
2816 break;
2817 }
2818 }
2819 if (pclose(pdrives)) {
2820 log_OS_error("Cannot pclose pdrives");
2821 }
2822 log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
2823 if (count_grubs && !count_lilos) {
2824 paranoid_free(current_drive);
2825 return ('G');
2826 } else if (count_lilos && !count_grubs) {
2827 paranoid_free(current_drive);
2828 return ('L');
2829 } else if (count_grubs == 1 && count_lilos == 1) {
2830 log_it("I'll bet you used to use LILO but switched to GRUB...");
2831 paranoid_free(current_drive);
2832 return ('G');
2833 } else {
2834 // We need to look on each partition then
2835 mr_asprintf(list_drives_cmd, "mr-parted2fdisk -l 2>/dev/null | grep -E \"^/dev/\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/");
2836 log_it("list_drives_cmd = %s", list_drives_cmd);
2837
2838 if (!(pdrives = popen(list_drives_cmd, "r"))) {
2839 log_OS_error("Unable to open list of drives");
2840 mr_free(list_drives_cmd);
2841 paranoid_free(current_drive);
2842 return ('\0');
2843 }
2844 mr_free(list_drives_cmd);
2845
2846 for (tmp = fgets(current_drive, MAX_STR_LEN, pdrives); !feof(pdrives) && (tmp != NULL);
2847 tmp = fgets(current_drive, MAX_STR_LEN, pdrives)) {
2848 strip_spaces(current_drive);
2849 log_it("looking at partition %s's BR", current_drive);
2850 if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
2851 count_grubs++;
2852 strcpy(which_device, current_drive);
2853 break;
2854 }
2855 if (does_string_exist_in_boot_block(current_drive, "LILO")) {
2856 count_lilos++;
2857 strcpy(which_device, current_drive);
2858 break;
2859 }
2860 }
2861 if (pclose(pdrives)) {
2862 log_OS_error("Cannot pclose pdrives");
2863 }
2864 log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
2865 paranoid_free(current_drive);
2866 if (count_grubs && !count_lilos) {
2867 return ('G');
2868 } else if (count_lilos && !count_grubs) {
2869 return ('L');
2870 } else if (count_grubs == 1 && count_lilos == 1) {
2871 log_it("I'll bet you used to use LILO but switched to GRUB...");
2872 return ('G');
2873 } else {
2874 log_it("Unknown boot loader");
2875 return ('U');
2876 }
2877 }
2878 }
2879}
2880#endif
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.