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

Last change on this file since 3642 was 3642, checked in by Bruno Cornec, 7 years ago

Add logging of internal_tape_block_size ("David C. Partridge" <david.partridge_at_perdrix.co.uk>)

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