source: MondoRescue/trunk/mondo/src/common/libmondo-devices.c@ 1161

Last change on this file since 1161 was 1161, checked in by Bruno Cornec, 17 years ago

Update trunk with the latest feedback from stable (obtained with meld) - First pass

  • Property svn:keywords set to Id
File size: 64.5 KB
Line 
1/* libmondo-devices.c Subroutines for handling devices
2 $Id: libmondo-devices.c 1161 2007-02-14 12:13:49Z bruno $
3*/
4
5/**
6 * @file
7 * Functions to handle interactions with backup devices.
8 */
9
10#include "my-stuff.h"
11#include "mondostructures.h"
12#include "libmondo-files-EXT.h"
13#include "libmondo-devices.h"
14#include "libmondo-string-EXT.h"
15#include "libmondo-tools-EXT.h"
16#include "newt-specific-EXT.h"
17#include "libmondo-fork-EXT.h"
18#include "libmondo-stream-EXT.h"
19#include "mr_mem.h"
20#include "mr_msg.h"
21
22#include <sys/ioctl.h>
23#include <sys/types.h>
24#include <unistd.h>
25#ifdef __FreeBSD__
26#define DKTYPENAMES
27#define FSTYPENAMES
28#include <sys/disklabel.h>
29#include <sys/disk.h>
30#elif linux
31#define u64 unsigned long long
32#include <linux/fs.h> /* for BLKGETSIZE64 */
33#include <linux/hdreg.h>
34#endif
35
36/*@unused@*/
37//static char cvsid[] = "$Id: libmondo-devices.c 1161 2007-02-14 12:13:49Z bruno $";
38
39extern int g_current_media_number;
40extern double g_kernel_version;
41
42extern bool g_ISO_restore_mode;
43extern struct s_bkpinfo *g_bkpinfo_DONTUSETHIS;
44extern char *g_erase_tmpdir_and_scratchdir;
45extern char *g_selfmounted_isodir;
46
47static char *g_cdrw_drive_is_here = NULL;
48static char *g_cdrom_drive_is_here = NULL;
49static char *g_dvd_drive_is_here = NULL;
50
51
52/**
53 * ????? @bug ?????
54 * @ingroup globalGroup
55 */
56bool g_restoring_live_from_cd = FALSE;
57
58extern t_bkptype g_backup_media_type; // set by main()
59
60
61
62
63void set_g_cdrom_and_g_dvd_to_bkpinfo_value(struct s_bkpinfo *bkpinfo)
64{
65 if (bkpinfo->media_device != NULL) {
66 mr_allocstr(g_cdrom_drive_is_here, bkpinfo->media_device); // just in case
67 mr_allocstr(g_dvd_drive_is_here, bkpinfo->media_device); // just in case
68 }
69}
70
71
72
73/**
74 * Retract all CD trays and wait for autorun to complete.
75 * @ingroup deviceGroup
76 */
77void retract_CD_tray_and_defeat_autorun(void)
78{
79// log_it("rctada: Retracting all CD trays", __LINE__);
80 if (strlen(g_cdrom_drive_is_here) > 0) {
81 inject_device(g_cdrom_drive_is_here);
82 }
83 if (strlen(g_dvd_drive_is_here) > 0) {
84 inject_device(g_dvd_drive_is_here);
85 }
86 if (strlen(g_cdrw_drive_is_here) > 0) {
87 inject_device(g_cdrw_drive_is_here);
88 }
89// log_it("rctada: killing autorun");
90// run_program_and_log_output("killall autorun", TRUE);
91 if (!run_program_and_log_output("ps | grep autorun | grep -v grep", 5)) {
92 log_it("autorun detected; sleeping for 2 seconds");
93 sleep(2);
94 }
95 log_it("rctada: Unmounting all CD drives", __LINE__);
96 run_program_and_log_output("umount /dev/cdr* /dev/dvd*", 5);
97}
98
99
100/**
101 * Mount the CD-ROM at @p mountpoint.
102 * @param device The device (or file if g_ISO_restore_mode) to mount.
103 * @param mountpoint The place to mount it.
104 * @return TRUE for success, FALSE for failure.
105 */
106bool mount_CDROM_here(char *device, char *mountpoint)
107{
108 /*@ buffer ****************************************************** */
109 char *command = NULL;
110 int retval = 0;
111
112 assert_string_is_neither_NULL_nor_zerolength(device);
113 assert_string_is_neither_NULL_nor_zerolength(mountpoint);
114
115 make_hole_for_dir(mountpoint);
116 if (isdigit(device[0])) {
117 mr_free(device);
118 device = find_cdrom_device(FALSE);
119 }
120 if (g_ISO_restore_mode) {
121
122#ifdef __FreeBSD__
123 char *dev = NULL;
124
125 dev = make_vn(device));
126 if (!dev) {
127 mr_asprintf(&command, "Unable to mount ISO (make_vn(%s) failed)",
128 device);
129 fatal_error(command);
130 }
131 mr_free(device);
132 device = dev;
133#endif
134 }
135
136 mr_msg(4, "(mount_CDROM_here --- device=%s, mountpoint=%s", device,
137 mountpoint);
138 /*@ end vars *************************************************** */
139
140#ifdef __FreeBSD__
141 mr_asprintf(&command, "mount_cd9660 -r %s %s 2>> %s",
142 device, mountpoint, MONDO_LOGFILE);
143#else
144 mr_asprintf(&command, "mount %s -o ro,loop -t iso9660 %s 2>> %s",
145 device, mountpoint, MONDO_LOGFILE);
146#endif
147
148 mr_msg(4, command);
149 if (strncmp(device, "/dev/", 5) == 0) {
150 retract_CD_tray_and_defeat_autorun();
151 }
152 retval = system(command);
153 mr_msg(1, "system(%s) returned %d", command, retval);
154 mr_free(command);
155
156 if (retval == 0) {
157 return(TRUE);
158 } else {
159 return(FALSE);
160 }
161}
162
163
164
165/**
166 * Determine whether we're booted off a ramdisk.
167 * @return @c TRUE (we are) or @c FALSE (we aren't).
168 * @ingroup utilityGroup
169 */
170bool am_I_in_disaster_recovery_mode(void)
171{
172 char *tmp = NULL;
173 char *comment = NULL;
174 bool is_this_a_ramdisk = FALSE;
175
176 mr_asprintf(&tmp, where_is_root_mounted());
177 mr_asprintf(&comment, "root is mounted at %s\n", tmp);
178 mr_msg(0, comment);
179 mr_free(comment);
180
181 mr_msg(0,
182 "No, Schlomo, 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().",
183 tmp);
184
185#ifdef __FreeBSD__
186 if (strstr(tmp, "/dev/md")) {
187 is_this_a_ramdisk = TRUE;
188 }
189#else
190 if (!strncmp(tmp, "/dev/ram", 8)
191 || (!strncmp(tmp, "/dev/rd", 7) && !strcmp(tmp, "/dev/rd/")
192 && strncmp(tmp, "/dev/rd/cd", 10)) || strstr(tmp, "rootfs")
193 || !strcmp(tmp, "/dev/root")) {
194 is_this_a_ramdisk = TRUE;
195 } else {
196 is_this_a_ramdisk = FALSE;
197 }
198#endif
199 mr_free(tmp);
200
201 if (is_this_a_ramdisk) {
202 if (!does_file_exist("/THIS-IS-A-RAMDISK")
203 && !does_file_exist("/tmp/mountlist.txt.sample")) {
204 log_to_screen
205 (_("Using /dev/root is stupid of you but I'll forgive you."));
206 is_this_a_ramdisk = FALSE;
207 }
208 }
209 if (does_file_exist("/THIS-IS-A-RAMDISK")) {
210 is_this_a_ramdisk = TRUE;
211 }
212 mr_msg(1, "Is this a ramdisk? result = %d", is_this_a_ramdisk);
213 return (is_this_a_ramdisk);
214}
215
216
217/**
218 * Turn @c bkpinfo->backup_media_type into a human-readable string.
219 * @return The human readable string (e.g. @c cdr becomes <tt>"cdr"</tt>).
220 * @note The returned string points to static storage that will be overwritten with each call.
221 * @ingroup stringGroup
222 */
223static char *bkptype_to_string(t_bkptype bt)
224{
225 char *output = NULL;
226
227 switch (bt) {
228 case none:
229 mr_asprintf(&output, _("none"));
230 break;
231 case iso:
232 mr_asprintf(&output, _("iso"));
233 break;
234 case cdr:
235 mr_asprintf(&output, _("cdr"));
236 break;
237 case cdrw:
238 mr_asprintf(&output, _("cdrw"));
239 break;
240 case cdstream:
241 mr_asprintf(&output, _("cdstream"));
242 break;
243 case nfs:
244 mr_asprintf(&output, _("nfs"));
245 break;
246 case tape:
247 mr_asprintf(&output, _("tape"));
248 break;
249 case udev:
250 mr_asprintf(&output, _("udev"));
251 break;
252 case usb:
253 mr_asprintf(&output, _("usb"));
254 break;
255 default:
256 mr_asprintf(&output, _("default"));
257 }
258 return (output);
259}
260
261
262/**
263 * @addtogroup deviceGroup
264 * @{
265 */
266/**
267 * Eject the tray of the specified CD device.
268 * @param dev The device to eject.
269 * @return the return value of the @c eject command. (0=success, nonzero=failure)
270 */
271int eject_device(char *dev)
272{
273 char *command = NULL;
274 int res1 = 0, res2 = 0;
275
276 if (IS_THIS_A_STREAMING_BACKUP(g_backup_media_type)
277 && g_backup_media_type != udev) {
278 mr_asprintf(&command, "mt -f %s offline", dev);
279 res1 = run_program_and_log_output(command, 1);
280 mr_free(command);
281 } else {
282 res1 = 0;
283 }
284
285#ifdef __FreeBSD__
286 if (strstr(dev, "acd")) {
287 mr_asprintf(&command, "cdcontrol -f %s eject", dev);
288 } else {
289 mr_asprintf(&command, "camcontrol eject `echo %s | sed 's|/dev/||'`",
290 dev);
291 }
292#else
293 mr_asprintf(&command, "eject %s", dev);
294#endif
295
296 mr_msg(3, "Ejecting %s", dev);
297 res2 = run_program_and_log_output(command, 1);
298 mr_free(command);
299 if (res1 && res2) {
300 return (1);
301 } else {
302 return (0);
303 }
304}
305
306
307/**
308 * Load (inject) the tray of the specified CD device.
309 * @param dev The device to load/inject.
310 * @return 0 for success, nonzero for failure.
311 */
312int inject_device(char *dev)
313{
314 char *command = NULL;
315 int i;
316
317#ifdef __FreeBSD__
318 if (strstr(dev, "acd")) {
319 mr_asprintf(&command, "cdcontrol -f %s close", dev);
320 } else {
321 mr_asprintf(&command, "camcontrol load `echo %s | sed 's|/dev/||'`",
322 dev);
323 }
324#else
325 mr_asprintf(&command, "eject -t %s", dev);
326#endif
327 i = run_program_and_log_output(command, FALSE);
328 mr_free(command);
329 return (i);
330}
331
332
333/**
334 * Determine whether the specified @p device (really, you can use any file)
335 * exists.
336 * @return TRUE if it exists, FALSE if it doesn't.
337 */
338bool does_device_exist(char *device)
339{
340
341 /*@ buffers *********************************************************** */
342 char *tmp = NULL;
343 bool ret = FALSE;
344
345 assert_string_is_neither_NULL_nor_zerolength(device);
346
347 mr_asprintf(&tmp, "ls %s > /dev/null 2> /dev/null", device);
348
349 if (system(tmp)) {
350 ret = FALSE;
351 } else {
352 ret = TRUE;
353 }
354 mr_free(tmp);
355 return(ret);
356}
357
358
359/**
360 * Determine whether a non-Microsoft partition exists on any connected hard drive.
361 * @return TRUE (there's a Linux/FreeBSD partition) or FALSE (Microsoft has taken over yet another innocent PC).
362 */
363bool does_nonMS_partition_exist(void)
364{
365#if __FreeBSD__
366 return
367 !system
368 ("for drive in /dev/ad? /dev/da?; do fdisk $drive | grep -q FreeBSD && exit 0; done; false");
369#else
370 return
371 !system
372 ("parted2fdisk -l 2>/dev/null | grep '^/dev/' | grep -Eqv '(MS|DOS|FAT|NTFS)'");
373#endif
374}
375
376/**
377 * Determine whether the specified @p partno exists on the specified @p drive.
378 * @param drive The drive to search for the partition in.
379 * @param partno The partition number to look for.
380 * @return 0 if it exists, nonzero otherwise.
381 */
382int does_partition_exist(const char *drive, int partno)
383{
384 /*@ buffers **************************************************** */
385 char *program = NULL;
386 char *incoming = NULL;
387 char *searchstr = NULL;
388 char *tmp = NULL;
389
390 /*@ ints ******************************************************* */
391 int res = 0;
392 size_t n = 0;
393
394 /*@ pointers *************************************************** */
395 FILE *fin;
396
397
398 /*@ end vars *************************************************** */
399 assert_string_is_neither_NULL_nor_zerolength(drive);
400 assert(partno >= 0 && partno < 999);
401
402#ifdef __FreeBSD__
403 // We assume here that this is running from mondorestore. (It is.)
404 mr_asprintf(&program, "ls %s >/dev/null 2>&1", drive);
405 res = system(program);
406 mr_free(program);
407 return(res);
408#endif
409
410 mr_asprintf(&program, "parted2fdisk -l %s 2> /dev/null", drive);
411 fin = popen(program, "r");
412 if (!fin) {
413 log_it("program=%s", program);
414 log_OS_error("Cannot popen-in program");
415 mr_free(program);
416 return (0);
417 }
418 mr_free(program);
419
420 searchstr = build_partition_name(drive, partno);
421 mr_asprintf(&tmp, "%s ", searchstr);
422 mr_free(searchstr);
423
424 for (res = 0; !res ;) {
425 mr_getline(&incoming, &n, fin);
426 if (strstr(incoming, tmp)) {
427 res = 1;
428 }
429 }
430 mr_free(incoming);
431
432 if (pclose(fin)) {
433 log_OS_error("Cannot pclose fin");
434 }
435 mr_free(tmp);
436 return (res);
437}
438
439
440/**
441 * Determine whether given NULL-terminated @p str exists in the MBR of @p dev.
442 * @param dev The device to look in.
443 * @param str The string to look for.
444 * @return TRUE if it exists, FALSE if it doesn't.
445 */
446bool does_string_exist_in_boot_block(char *dev, char *str)
447{
448 /*@ buffers **************************************************** */
449 char *command = NULL;
450
451 /*@ end vars *************************************************** */
452 int ret;
453
454 assert_string_is_neither_NULL_nor_zerolength(dev);
455 assert_string_is_neither_NULL_nor_zerolength(str);
456
457 mr_asprintf(&command,
458 "dd if=%s bs=446 count=1 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null",
459 dev, str);
460 ret = system(command);
461 mr_free(command);
462 if (ret) {
463 return (FALSE);
464 } else {
465 return (TRUE);
466 }
467}
468
469
470/**
471 * Determine whether specified @p str exists in the first @p n sectors of
472 * @p dev.
473 * @param dev The device to look in.
474 * @param str The string to look for.
475 * @param n The number of 512-byte sectors to search.
476 */
477bool does_string_exist_in_first_N_blocks(char *dev, char *str, int n)
478{
479 /*@ buffers **************************************************** */
480 char *command = NULL;
481 /*@ end vars *************************************************** */
482 int ret;
483
484 mr_asprintf(&command,
485 "dd if=%s bs=512 count=%i 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null",
486 dev, n, str);
487 ret = system(command);
488 mr_free(command);
489
490 if (ret) {
491 return (FALSE);
492 } else {
493 return (TRUE);
494 }
495}
496
497
498/**
499 * Try to mount CD-ROM at @p mountpoint. If the CD-ROM is not found or has
500 * not been specified, call find_cdrom_device() to find it.
501 * @param bkpinfo The backup information structure. The only field used is @c bkpinfo->media_device.
502 * @param mountpoint Where to mount the CD-ROM.
503 * @return 0 for success, nonzero for failure.
504 * @see mount_CDROM_here
505 */
506bool find_and_mount_actual_cd(struct s_bkpinfo *bkpinfo, char *mountpoint)
507{
508 /*@ buffers ***************************************************** */
509
510 /*@ int's ****************************************************** */
511 bool res = TRUE;
512 char *dev = NULL;
513
514 /*@ end vars **************************************************** */
515
516 assert(bkpinfo != NULL);
517 assert_string_is_neither_NULL_nor_zerolength(mountpoint);
518
519 if (g_backup_media_type == dvd) {
520 if (g_dvd_drive_is_here != NULL) {
521 mr_asprintf(&dev, g_dvd_drive_is_here);
522 } else {
523 dev = find_dvd_device();
524 }
525 } else {
526 if (g_cdrom_drive_is_here != NULL) {
527 mr_asprintf(&dev, g_cdrom_drive_is_here);
528 } else {
529 // find_cdrom_device allocates the string
530 dev = find_cdrom_device(FALSE);
531 }
532 }
533
534 if (bkpinfo->backup_media_type != iso) {
535 retract_CD_tray_and_defeat_autorun();
536 }
537
538 if ((dev == NULL) || (! mount_CDROM_here(dev, mountpoint))) {
539 mr_free(dev);
540 if (!popup_and_get_string
541 (_("CD-ROM device"), _("Please enter your CD-ROM's /dev device"), dev)) {
542 res = FALSE;
543 } else {
544 res = mount_CDROM_here(dev, mountpoint);
545 }
546 }
547 if (res) {
548 mr_msg(1, _("mount failed"));
549 } else {
550 mr_msg(1, _("mount succeeded with %s"), dev);
551 }
552 mr_free(dev);
553 return(res);
554}
555
556
557/**
558 * Locate a CD-R/W writer's SCSI node.
559 * @param cdrw_device SCSI node will be placed here. Caller needs to free it.
560 * @return the cdrw device or NULL if not found
561 */
562char *find_cdrw_device(void)
563{
564 char *comment = NULL;
565 char *tmp = NULL;
566 char *tmp1 = NULL;
567 char *cdr_exe = NULL;
568 char *command = NULL;
569 char *cdrw_device = NULL;
570
571 if (g_cdrw_drive_is_here != NULL) {
572 mr_asprintf(&cdrw_device, g_cdrw_drive_is_here);
573 mr_msg(3, "Been there, done that. Returning %s", cdrw_device);
574 return(cdrw_device);
575 }
576 if (g_backup_media_type == dvd) {
577 mr_msg(1,
578 "This is dumb. You're calling find_cdrw_device() but you're backing up to DVD. WTF?");
579 return(NULL);
580 }
581 run_program_and_log_output("insmod ide-scsi", -1);
582 tmp = find_home_of_exe("cdrecord");
583 if (tmp) {
584 mr_asprintf(&cdr_exe, "cdrecord");
585 } else {
586 mr_asprintf(&cdr_exe, "dvdrecord");
587 }
588 mr_free(tmp);
589
590 tmp1 = find_home_of_exe(cdr_exe);
591 if (tmp1) {
592 mr_asprintf(&command,
593 "%s -scanbus 2> /dev/null | tr -s '\t' ' ' | grep \"[0-9]*,[0-9]*,[0-9]*\" | grep -v \"[0-9]*) \\*\" | grep CD | cut -d' ' -f2 | head -n1",
594 cdr_exe);
595 mr_asprintf(&tmp, call_program_and_get_last_line_of_output(command));
596 mr_free(command);
597 } else {
598 mr_asprintf(&tmp, " ");
599 }
600 mr_free(tmp1);
601 mr_free(cdr_exe);
602
603 if (strlen(tmp) < 2) {
604 mr_free(tmp);
605 return(NULL);
606 } else {
607 cdrw_device = tmp;
608
609 mr_asprintf(&comment, "Found CDRW device - %s", cdrw_device);
610 log_it(comment);
611 mr_free(comment);
612
613 mr_free(g_cdrw_drive_is_here);
614 mr_asprintf(&g_cdrw_drive_is_here, cdrw_device);
615 return(cdrw_device);
616 }
617}
618
619
620/**
621 * Attempt to locate a CD-ROM device's /dev entry.
622 * Several different methods may be used to find the device, including
623 * calling @c cdrecord, searching @c dmesg, and trial-and-error.
624 * @param output Where to put the located /dev entry. Needs to be freed by the caller.
625 * @param try_to_mount Whether to mount the CD as part of the test; if mount
626 * fails then return failure.
627 * @return output if success or NULL otherwise.
628 */
629char *find_cdrom_device(bool try_to_mount)
630{
631 /*@ pointers **************************************************** */
632 FILE *fin = NULL;
633 char *p = NULL;
634 char *q = NULL;
635 char *r = NULL;
636 char *output = NULL;
637 size_t n = 0;
638
639 /*@ bool's ****************************************************** */
640 bool found_it = FALSE;
641
642 /*@ buffers ***************************************************** */
643 char *tmp = NULL;
644 char *cdr_exe = NULL;
645#ifndef __FreeBSD__
646 char *phrase_two = NULL;
647 char *dvd_last_resort = NULL;
648#endif
649 char *command = NULL;
650 char *mountpoint = NULL;
651 static char *the_last_place_i_found_it = NULL;
652
653 /*@ end vars **************************************************** */
654
655 if ((g_cdrom_drive_is_here != NULL) && !isdigit(g_cdrom_drive_is_here[0])) {
656 mr_asprintf(&output, g_cdrom_drive_is_here);
657 mr_msg(3, "Been there, done that. Returning %s", output);
658 return(output);
659 }
660 if ((the_last_place_i_found_it != NULL) && !try_to_mount) {
661 mr_asprintf(&output, the_last_place_i_found_it);
662 mr_msg(3,
663 "find_cdrom_device() --- returning last found location - '%s'",
664 output);
665 return(output);
666 }
667
668 tmp = find_home_of_exe("cdrecord");
669 if (tmp) {
670 mr_asprintf(&cdr_exe, "cdrecord");
671 } else {
672 mr_asprintf(&cdr_exe, "dvdrecord");
673 }
674 mr_free(tmp);
675
676 tmp = find_home_of_exe(cdr_exe);
677 if (!tmp) {
678 mr_asprintf(&output, "/dev/cdrom");
679 mr_msg(4, "Can't find cdrecord; assuming %s", output);
680 if (!does_device_exist(output)) {
681 mr_msg(4, "That didn't work. Sorry.");
682 mr_free(cdr_exe);
683 mr_free(output);
684 return(NULL);
685 } else {
686 mr_free(cdr_exe);
687 return(output);
688 }
689 }
690 mr_free(tmp);
691
692 mr_asprintf(&command, "%s -scanbus 2> /dev/null", cdr_exe);
693 fin = popen(command, "r");
694 if (!fin) {
695 mr_msg(4, "command=%s", command);
696 log_OS_error("Cannot popen command");
697 mr_free(cdr_exe);
698 mr_free(command);
699 return (NULL);
700 }
701 mr_free(command);
702
703 for (mr_getline(&tmp, &n, fin); !feof(fin);
704 mr_getline(&tmp, &n, fin)) {
705 p = strchr(tmp, '\'');
706 if (p) {
707 q = strchr(++p, '\'');
708 if (q) {
709 for (r = q; *(r - 1) == ' '; r--);
710 *r = '\0';
711 p = strchr(++q, '\'');
712 if (p) {
713 q = strchr(++p, '\'');
714 if (q) {
715 while (*(q - 1) == ' ') {
716 q--;
717 }
718 *q = '\0';
719#ifndef __FreeBSD__
720 mr_free(phrase_two);
721 mr_asprintf(&phrase_two, p);
722#endif
723 }
724 }
725 }
726 }
727 }
728 paranoid_pclose(fin);
729 mr_free(tmp);
730 tmp = NULL;
731 n = 0;
732
733#ifndef __FreeBSD__
734 if (strlen(phrase_two) == 0) {
735 mr_msg(4, "Not running phase two. String is empty.");
736 } else {
737 mr_asprintf(&command, "dmesg | grep \"%s\" 2> /dev/null", phrase_two);
738 fin = popen(command, "r");
739 if (!fin) {
740 mr_msg(4, "Cannot run 2nd command - non-fatal, fortunately");
741 } else {
742 for (mr_getline(&tmp, &n, fin); !feof(fin);
743 mr_getline(&tmp, &n, fin)) {
744 mr_msg(5, "--> '%s'", tmp);
745 if (tmp[0] != ' ' && tmp[1] != ' ') {
746 p = strchr(tmp, ':');
747 if (p) {
748 *p = '\0';
749 if (strstr(tmp, "DVD")) {
750 mr_free(dvd_last_resort);
751 mr_asprintf(&dvd_last_resort, "/dev/%s", tmp);
752 mr_msg(4,
753 "Ignoring '%s' because it's a DVD drive",
754 tmp);
755 } else {
756 mr_asprintf(&output, "/dev/%s", tmp);
757 found_it = TRUE;
758 }
759 }
760 }
761 }
762 mr_free(tmp);
763 paranoid_pclose(fin);
764 }
765 mr_free(command);
766 }
767 mr_free(phrase_two);
768
769#endif
770#ifdef __FreeBSD__
771 if (!found_it) {
772 mr_msg(4, "OK, approach 2");
773 if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/cdrom"))) {
774 if (!
775 (found_it =
776 set_dev_to_this_if_rx_OK(output, "/dev/cdrom1"))) {
777 if (!
778 (found_it =
779 set_dev_to_this_if_rx_OK(output, "/dev/dvd"))) {
780 if (!
781 (found_it =
782 set_dev_to_this_if_rx_OK(output, "/dev/acd0"))) {
783 if (!
784 (found_it =
785 set_dev_to_this_if_rx_OK(output,
786 "/dev/cd01"))) {
787 if (!
788 (found_it =
789 set_dev_to_this_if_rx_OK(output,
790 "/dev/acd1"))) {
791 if (!
792 (found_it =
793 set_dev_to_this_if_rx_OK(output,
794 "/dev/cd1")))
795 {
796 mr_free(cdr_exe);
797 return(NULL);
798 }
799 }
800 }
801 }
802 }
803 }
804 }
805 }
806#else
807 if (dvd_last_resort != NULL) {
808 if (!found_it && strlen(dvd_last_resort) > 0) {
809 mr_msg(4, "Well, I'll use the DVD - %s - as a last resort",
810 dvd_last_resort);
811 mr_free(output);
812 mr_asprintf(&output, dvd_last_resort);
813 found_it = TRUE;
814 }
815 }
816 mr_free(dvd_last_resort);
817
818 if (found_it) {
819 mr_asprintf(&tmp, "grep \"%s=ide-scsi\" /proc/cmdline &> /dev/null",
820 strrchr(output, '/') + 1);
821 if (system(tmp) == 0) {
822 mr_msg(4,
823 "%s is not right. It's being SCSI-emulated. Continuing.",
824 output);
825 found_it = FALSE;
826 mr_free(output);
827 }
828 mr_free(tmp);
829 }
830
831 if (found_it) {
832 mr_msg(4, "(find_cdrom_device) --> '%s'", output);
833 if (!does_device_exist(output)) {
834 mr_msg(4, "OK, I was wrong, I haven't found it... yet.");
835 found_it = FALSE;
836 mr_free(output);
837 }
838 }
839
840 if (!found_it) {
841 mr_msg(4, "OK, approach 2");
842 if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/scd0"))) {
843 if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/sr0"))) {
844 if (!
845 (found_it =
846 set_dev_to_this_if_rx_OK(output, "/dev/cdrom"))) {
847 if (!
848 (found_it =
849 set_dev_to_this_if_rx_OK(output,
850 "/dev/cdrom0"))) {
851 if (!
852 (found_it =
853 set_dev_to_this_if_rx_OK(output,
854 "/dev/cdrom1"))) {
855 if (!
856 (found_it =
857 set_dev_to_this_if_rx_OK(output,
858 "/dev/sr1"))) {
859 if (!
860 (found_it =
861 set_dev_to_this_if_rx_OK(output,
862 "/dev/dvd")))
863 {
864 if (!
865 (found_it =
866 set_dev_to_this_if_rx_OK(output,
867 g_cdrw_drive_is_here)))
868 {
869 mr_free(cdr_exe);
870 return(NULL);
871 }
872 }
873 }
874 }
875 }
876 }
877 }
878 }
879 }
880#endif
881
882 mr_asprintf(&mountpoint, "/tmp/cd.%d", (int) (random() % 32767));
883 make_hole_for_dir(mountpoint);
884
885 if (found_it && try_to_mount) {
886 if (! mount_CDROM_here(output, mountpoint)) {
887 mr_msg(4, "[Cardigans] I've changed my mind");
888 found_it = FALSE;
889 mr_free(output);
890 } else {
891 mr_asprintf(&tmp, "%s/archives", mountpoint);
892 if (!does_file_exist(tmp)) {
893 mr_msg(4, "[Cardigans] I'll take it back");
894 found_it = FALSE;
895 mr_free(output);
896 } else {
897 mr_asprintf(&command, "umount %s", output);
898 paranoid_system(command);
899 mr_free(command);
900 mr_msg(4, "I'm confident the Mondo CD is in %s", output);
901 }
902 mr_free(tmp);
903 }
904 }
905 unlink(mountpoint);
906 mr_free(mountpoint);
907
908 if (found_it) {
909 if (!does_file_exist(output)) {
910 mr_msg(3, "I still haven't found it.");
911 mr_free(output);
912 return(NULL);
913 }
914 mr_msg(3, "(find_cdrom_device) --> '%s'", output);
915 mr_asprintf(&the_last_place_i_found_it, output);
916 mr_free(g_cdrom_drive_is_here);
917 mr_asprintf(&g_cdrom_drive_is_here, output);
918 return(output);
919 }
920
921 mr_asprintf(&command,
922 "%s -scanbus | grep \"[0-9],[0-9],[0-9]\" | grep \"[D|C][V|D]\" | grep -n \"\" | grep \"%s\" | cut -d':' -f2",
923 cdr_exe, g_cdrw_drive_is_here);
924 mr_free(cdr_exe);
925
926 mr_msg(1, "command=%s", command);
927 mr_asprintf(&tmp, call_program_and_get_last_line_of_output(command));
928 mr_free(command);
929
930 if (tmp[0]) {
931 output = tmp;
932 mr_msg(4, "Finally found it at %s", output);
933 } else {
934 mr_free(tmp);
935 mr_free(output);
936 mr_msg(4, "Still couldn't find it.");
937 }
938 return(output);
939}
940
941
942char *find_dvd_device()
943{
944 char *tmp = NULL;
945 int retval = 0, devno = -1;
946 char *output = NULL;
947
948 if (g_dvd_drive_is_here != NULL) {
949 mr_asprintf(&output, g_dvd_drive_is_here);
950 mr_msg(3, "Been there, done that. Returning %s", output);
951 return (output);
952 }
953
954 mr_asprintf(&tmp, call_program_and_get_last_line_of_output
955 ("dvdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1")
956 );
957 mr_msg(5, "tmp = '%s'", tmp);
958 if (!tmp[0]) {
959 mr_free(tmp);
960 mr_asprintf(&tmp, call_program_and_get_last_line_of_output
961 ("cdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1")
962 );
963 }
964 if (tmp[0]) {
965 devno = atoi(tmp) - 1;
966 }
967 mr_free(tmp);
968
969 if (devno >= 0) {
970 retval = 0;
971 mr_asprintf(&output, "/dev/scd%d", devno);
972 mr_free(g_dvd_drive_is_here);
973 mr_asprintf(&g_dvd_drive_is_here, output);
974 mr_msg(2, "I think DVD is at %s", output);
975 } else {
976 mr_msg(2, "I cannot find DVD");
977 }
978
979 return(output);
980}
981
982
983/**
984 * Find the size of the specified @p drive, in megabytes. Uses @c ioctl calls
985 * and @c dmesg.
986 * @param drive The device to find the size of.
987 * @return size in megabytes.
988 */
989long get_phys_size_of_drive(char *drive)
990{
991 int fd;
992#if linux
993 unsigned long long s = 0;
994 int fileid, cylinders = 0, cylindersleft = 0;
995 int cylindersize = 0;
996 int gotgeo = 0;
997
998
999 struct hd_geometry hdgeo;
1000#elif __FreeBSD__
1001 off_t s;
1002#endif
1003
1004 long outvalA = -1;
1005 long outvalB = -1;
1006 long outvalC = -1;
1007
1008 if ((fd = open(drive, O_RDONLY)) != -1) {
1009 if (ioctl(fd,
1010#if linux
1011#ifdef BLKGETSIZE64
1012 BLKGETSIZE64,
1013#else
1014 BLKGETSIZE,
1015#endif
1016#elif __FreeBSD__
1017 DIOCGMEDIASIZE,
1018#endif
1019 &s) != -1) {
1020 close(fd);
1021 // s>>11 works for older disks but not for newer ones
1022 outvalB =
1023#if linux
1024#ifdef BLKGETSIZE64
1025 s >> 20
1026#else
1027 s >> 11
1028#endif
1029#else
1030 s >> 20
1031#endif
1032 ;
1033 }
1034 }
1035
1036 if (outvalB <= 0) {
1037 mr_msg(1, "Error getting size of %s: %s", drive, strerror(errno));
1038#if linux
1039 fileid = open(drive, O_RDONLY);
1040 if (fileid) {
1041 if (ioctl(fileid, HDIO_GETGEO, &hdgeo) != -1) {
1042 if (hdgeo.cylinders && hdgeo.heads && hdgeo.sectors) {
1043 cylindersleft = cylinders = hdgeo.cylinders;
1044 cylindersize = hdgeo.heads * hdgeo.sectors / 2;
1045 outvalA = cylindersize * cylinders / 1024;
1046 mr_msg(2, "Got Harddisk geometry, C:%d, H:%d, S:%d",
1047 hdgeo.cylinders, hdgeo.heads, hdgeo.sectors);
1048 gotgeo = 1;
1049 } else {
1050 mr_msg(1, "Harddisk geometry wrong");
1051 }
1052 } else {
1053 mr_msg(1,
1054 "Error in ioctl() getting new hard disk geometry (%s), resizing in unsafe mode",
1055 strerror(errno));
1056 }
1057 close(fileid);
1058 } else {
1059 mr_msg(1, "Failed to open %s for reading: %s", drive,
1060 strerror(errno));
1061 }
1062 if (!gotgeo) {
1063 mr_msg(1, "Failed to get harddisk geometry, using old mode");
1064 }
1065#endif
1066 }
1067// OLDER DISKS will give ridiculously low value for outvalB (so outvalA is returned) :)
1068// NEWER DISKS will give sane value for outvalB (close to outvalA, in other words) :)
1069
1070 outvalC = (outvalA > outvalB) ? outvalA : outvalB;
1071
1072// mr_msg (5, "drive = %s, error = %s", drive, strerror (errno));
1073// fatal_error ("GPSOD: Unable to get size of drive");
1074 mr_msg(1, "%s --> %ld or %ld --> %ld", drive, outvalA, outvalB,
1075 outvalC);
1076
1077 return (outvalC);
1078}
1079
1080
1081/**
1082 * Determine whether @p format is supported by the kernel. Uses /proc/filesystems
1083 * under Linux and @c lsvfs under FreeBSD.
1084 * @param format The format to test.
1085 * @return TRUE if the format is supported, FALSE if not.
1086 */
1087bool is_this_a_valid_disk_format(char *format)
1088{
1089 char *good_formats = NULL;
1090 char *command = NULL;
1091 char *tmp = NULL;
1092 char *format_sz = NULL;
1093
1094 FILE *pin = NULL;
1095 bool retval;
1096 size_t n = 0;
1097 assert_string_is_neither_NULL_nor_zerolength(format);
1098
1099 mr_asprintf(&format_sz, "%s ", format);
1100
1101#ifdef __FreeBSD__
1102 mr_asprintf(&command,
1103 "lsvfs | tr -s '\t' ' ' | grep -v Filesys | grep -v -- -- | cut -d' ' -f1 | tr -s '\n' ' '");
1104#else
1105 mr_asprintf(&command,
1106 "grep -v nodev /proc/filesystems | tr -s '\t' ' ' | cut -d' ' -f2 | tr -s '\n' ' '");
1107#endif
1108
1109 pin = popen(command, "r");
1110 mr_free(command);
1111
1112 if (!pin) {
1113 log_OS_error("Unable to read good formats");
1114 retval = FALSE;
1115 } else {
1116 mr_getline(&good_formats, &n , pin);
1117 if (pclose(pin)) {
1118 log_OS_error("Cannot pclose good formats");
1119 }
1120 strip_spaces(good_formats);
1121 // " ntfs 7 " -- um, cheating much? :)
1122 mr_asprintf(&tmp, " %s swap lvm raid ntfs 7 ",good_formats);
1123 mr_free(good_formats);
1124 good_formats = tmp;
1125
1126 if (strstr(good_formats, format_sz)) {
1127 retval = TRUE;
1128 } else {
1129 retval = FALSE;
1130 }
1131 mr_free(good_formats);
1132 }
1133 mr_free(format_sz);
1134 return (retval);
1135}
1136
1137
1138/** @def SWAPLIST_COMMAND The command to list the swap files/partitions in use. */
1139
1140/**
1141 * Determine whether @p device_raw is currently mounted.
1142 * @param device_raw The device to check.
1143 * @return TRUE if it's mounted, FALSE if not.
1144 */
1145bool is_this_device_mounted(char *device_raw)
1146{
1147
1148 /*@ pointers **************************************************** */
1149 FILE *fin;
1150
1151 /*@ buffers ***************************************************** */
1152 char *incoming = NULL;
1153 char *device_with_tab = NULL;
1154 char *device_with_space = NULL;
1155 char *tmp = NULL;
1156 size_t n = 0;
1157
1158#ifdef __FreeBSD__
1159#define SWAPLIST_COMMAND "swapinfo"
1160#else
1161#define SWAPLIST_COMMAND "cat /proc/swaps"
1162#endif
1163
1164 /*@ end vars **************************************************** */
1165
1166 assert(device_raw != NULL);
1167// assert_string_is_neither_NULL_nor_zerolength(device_raw);
1168 if (device_raw[0] != '/' && !strstr(device_raw, ":/")) {
1169 mr_msg(1, "%s needs to have a '/' prefixed - I'll do it",
1170 device_raw);
1171 mr_asprintf(&tmp, "/%s", device_raw);
1172 } else {
1173 mr_asprintf(&tmp, device_raw);
1174 }
1175 mr_msg(1, "Is %s mounted?", tmp);
1176 if (!strcmp(tmp, "/proc") || !strcmp(tmp, "proc")) {
1177 mr_msg(1,
1178 "I don't know how the heck /proc made it into the mountlist. I'll ignore it.");
1179 return (FALSE);
1180 }
1181 mr_asprintf(&device_with_tab, "%s\t", tmp);
1182 mr_asprintf(&device_with_space, "%s ", tmp);
1183 mr_free(tmp);
1184
1185 if (!(fin = popen("mount", "r"))) {
1186 log_OS_error("Cannot popen 'mount'");
1187 return (FALSE);
1188 }
1189 for (mr_getline(&incoming, &n, fin); !feof(fin);
1190 mr_getline(&incoming, &n, fin)) {
1191 if (strstr(incoming, device_with_space) //> incoming
1192 || strstr(incoming, device_with_tab)) // > incoming)
1193 {
1194 paranoid_pclose(fin);
1195 mr_free(incoming);
1196 return(TRUE);
1197 }
1198 }
1199 mr_free(incoming);
1200 mr_free(device_with_tab);
1201 paranoid_pclose(fin);
1202
1203 mr_asprintf(&tmp, "%s | grep -E \"^%s\" > /dev/null 2> /dev/null",
1204 SWAPLIST_COMMAND, device_with_space);
1205 mr_free(device_with_space);
1206
1207 mr_msg(4, "tmp (command) = '%s'", tmp);
1208 if (!system(tmp)) {
1209 mr_free(tmp);
1210 return(TRUE);
1211 }
1212 mr_free(tmp);
1213 return (FALSE);
1214}
1215
1216
1217#ifdef __FreeBSD__
1218// CODE IS FREEBSD-SPECIFIC
1219/**
1220 * Create a loopback device for specified @p fname.
1221 * @param fname The file to associate with a device.
1222 * @return /dev entry for the device (to be freed by the caller),
1223 * or NULL if it couldn't be allocated.
1224 */
1225char *make_vn(char *fname)
1226{
1227 char *device = NULL;
1228 char *mddevice = NULL;
1229 char *command = NULL;
1230 int vndev = 2;
1231
1232 if (atoi
1233 (call_program_and_get_last_line_of_output
1234 ("/sbin/sysctl -n kern.osreldate")) < 500000) {
1235 do {
1236 mr_free(mddevice);
1237 mr_asprintf(&mddevice, "vn%ic", vndev++);
1238 mr_free(command);
1239 mr_asprintf(&command, "vnconfig %s %s", mddevice, fname);
1240
1241 if (vndev > 10) {
1242 mr_free(command);
1243 mr_free(mddevice);
1244 return NULL;
1245 }
1246 }
1247 while (system(command));
1248 mr_free(command);
1249 } else {
1250 mr_asprintf(&command, "mdconfig -a -t vnode -f %s", fname);
1251 mr_asprintf(&mddevice, call_program_and_get_last_line_of_output(command));
1252 mr_free(command);
1253
1254 if (!strstr(mddevice, "md")) {
1255 mr_free(mddevice);
1256 return NULL;
1257 }
1258 }
1259 mr_asprintf(&device, "/dev/%s", mddevice);
1260 mr_free(mddevice);
1261 return(device);
1262}
1263
1264
1265// CODE IS FREEBSD-SPECIFIC
1266/**
1267 * Deallocate specified @p dname.
1268 * This should be called when you are done with the device created by make_vn(),
1269 * so the system does not run out of @c vn devices.
1270 * @param dname The device to deallocate.
1271 * @return 0 for success, nonzero for failure.
1272 */
1273int kick_vn(char *dname)
1274{
1275 char *command;
1276 int ret = 0;
1277
1278 if (strncmp(dname, "/dev/", 5) == 0) {
1279 dname += 5;
1280 }
1281
1282 if (atoi
1283 (call_program_and_get_last_line_of_output
1284 ("/sbin/sysctl -n kern.osreldate")) < 500000) {
1285 mr_asprintf(&command, "vnconfig -d %s", dname);
1286 } else {
1287 mr_asprintf(&command, "mdconfig -d -u %s", dname);
1288 }
1289 ret = system(command);
1290 mr_free(command);
1291 return(ret);
1292}
1293#endif
1294
1295
1296/**
1297 * Ask the user for CD number @p cd_number_i_want.
1298 * Sets g_current_media_number once the correct CD is inserted.
1299 * @param bkpinfo The backup information structure. Fields used:
1300 * - @c bkpinfo->backup_media_type
1301 * - @c bkpinfo->prefix
1302 * - @c bkpinfo->isodir
1303 * - @c bkpinfo->media_device
1304 * - @c bkpinfo->please_dont_eject_when_restoring
1305 * @param cd_number_i_want The CD number to ask for.
1306 */
1307void
1308insist_on_this_cd_number(struct s_bkpinfo *bkpinfo, int cd_number_i_want)
1309{
1310
1311 /*@ int ************************************************************* */
1312 int res = 0;
1313
1314
1315 /*@ buffers ********************************************************* */
1316 char *tmp = NULL;
1317 char *request = NULL;
1318
1319 assert(bkpinfo != NULL);
1320 assert(cd_number_i_want > 0);
1321
1322// mr_msg(3, "Insisting on CD number %d", cd_number_i_want);
1323
1324 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1325 mr_msg(3,
1326 "No need to insist_on_this_cd_number when the backup type isn't CD-R(W) or NFS or ISO");
1327 return;
1328 }
1329 mr_asprintf(&tmp, "mkdir -p " MNT_CDROM);
1330 run_program_and_log_output(tmp, 5);
1331 mr_free(tmp);
1332
1333 if (g_ISO_restore_mode || bkpinfo->backup_media_type == iso
1334 || bkpinfo->backup_media_type == nfs) {
1335 mr_msg(3, "Remounting CD");
1336 g_ISO_restore_mode = TRUE;
1337// FIXME --- I'm tempted to do something about this...
1338// Why unmount and remount again and again?
1339 if (is_this_device_mounted(MNT_CDROM)) {
1340 run_program_and_log_output("umount " MNT_CDROM, 5);
1341 }
1342 system("mkdir -p /tmp/isodir &> /dev/null");
1343 mr_asprintf(&tmp, "%s/%s/%s-%d.iso", bkpinfo->isodir,
1344 bkpinfo->nfs_remote_dir, bkpinfo->prefix,
1345 cd_number_i_want);
1346 if (!does_file_exist(tmp)) {
1347 mr_free(tmp);
1348 mr_asprintf(&tmp, "/tmp/isodir/%s/%s-%d.iso",
1349 bkpinfo->nfs_remote_dir, bkpinfo->prefix,
1350 cd_number_i_want);
1351 if (does_file_exist(tmp)) {
1352 mr_msg(1,
1353 "FIXME - hacking bkpinfo->isodir from '%s' to /tmp/isodir",
1354 bkpinfo->isodir);
1355 mr_allocstr(bkpinfo->isodir, "/tmp/isodir");
1356 }
1357 }
1358 mr_msg(3, "Mounting %s at %s", tmp, MNT_CDROM);
1359 if (! mount_CDROM_here(tmp, MNT_CDROM)) {
1360 fatal_error("Mommy!");
1361 }
1362 mr_free(tmp);
1363 }
1364 if ((res = what_number_cd_is_this(bkpinfo)) != cd_number_i_want) {
1365 mr_msg(3, "Currently, we hold %d but we want %d", res,
1366 cd_number_i_want);
1367 mr_asprintf(&tmp, "Insisting on %s #%d",
1368 bkpinfo->backup_media_string,
1369 cd_number_i_want);
1370 mr_asprintf(&request, "Please insert %s #%d and press Enter.",
1371 bkpinfo->backup_media_string,
1372 cd_number_i_want);
1373 mr_msg(3, tmp);
1374 mr_free(tmp);
1375
1376 while (what_number_cd_is_this(bkpinfo) != cd_number_i_want) {
1377 sync();
1378 if (is_this_device_mounted(MNT_CDROM)) {
1379 res =
1380 run_program_and_log_output("umount " MNT_CDROM, FALSE);
1381 } else {
1382 res = 0;
1383 }
1384 if (res) {
1385 log_to_screen(_("WARNING - failed to unmount CD-ROM drive"));
1386 }
1387 if (!bkpinfo->please_dont_eject) {
1388 res = eject_device(bkpinfo->media_device);
1389 } else {
1390 res = 0;
1391 }
1392 if (res) {
1393 log_to_screen(_("WARNING - failed to eject CD-ROM disk"));
1394 }
1395 popup_and_OK(request);
1396 if (!bkpinfo->please_dont_eject) {
1397 inject_device(bkpinfo->media_device);
1398 }
1399 sync();
1400 }
1401 mr_free(request);
1402
1403 mr_msg(1, "Thankyou. Proceeding...");
1404 g_current_media_number = cd_number_i_want;
1405 }
1406}
1407/* @} - end of deviceGroup */
1408
1409
1410/**
1411 * Ask user for details of backup/restore information.
1412 * Called when @c mondoarchive doesn't get any parameters.
1413 * @param bkpinfo The backup information structure to fill out with the user's data.
1414 * @param archiving_to_media TRUE if archiving, FALSE if restoring.
1415 * @return 0, always.
1416 * @bug No point of `int' return value.
1417 * @ingroup archiveGroup
1418 */
1419int interactively_obtain_media_parameters_from_user(struct s_bkpinfo
1420 *bkpinfo,
1421 bool
1422 archiving_to_media)
1423// archiving_to_media is TRUE if I'm being called by mondoarchive
1424// archiving_to_media is FALSE if I'm being called by mondorestore
1425{
1426 char *tmp = NULL;
1427 char *tmp1 = NULL;
1428 char *tmp2 = NULL;
1429 char *sz_size = NULL;
1430 char *command = NULL;
1431 char *comment = NULL;
1432 char *prompt = NULL;
1433 int i = 0;
1434 FILE *fin = NULL;
1435
1436 assert(bkpinfo != NULL);
1437 bkpinfo->nonbootable_backup = FALSE;
1438
1439// Tape, CD, NFS, ...?
1440 srandom(getpid());
1441 bkpinfo->backup_media_type =
1442 (g_restoring_live_from_cd) ? cdr :
1443 which_backup_media_type(bkpinfo->restore_data);
1444 if (bkpinfo->backup_media_type == none) {
1445 log_to_screen(_("User has chosen not to backup the PC"));
1446 finish(1);
1447 }
1448 if (bkpinfo->backup_media_type == tape && bkpinfo->restore_data) {
1449 popup_and_OK(_("Please remove CD/floppy from drive(s)"));
1450 }
1451 bkpinfo->backup_media_string = bkptype_to_string(bkpinfo->backup_media_type);
1452 mr_msg(3, "media type = %s",bkpinfo->backup_media_string);
1453
1454 if (archiving_to_media) {
1455 sensibly_set_tmpdir_and_scratchdir(bkpinfo);
1456 }
1457 bkpinfo->cdrw_speed = (bkpinfo->backup_media_type == cdstream) ? 2 : 4;
1458 bkpinfo->compression_level =
1459 (bkpinfo->backup_media_type == cdstream) ? 1 : 5;
1460 bkpinfo->use_lzo =
1461 (bkpinfo->backup_media_type == cdstream) ? TRUE : FALSE;
1462 mvaddstr_and_log_it(2, 0, " ");
1463
1464// Find device's /dev (or SCSI) entry
1465 switch (bkpinfo->backup_media_type) {
1466 case cdr:
1467 case cdrw:
1468 case dvd:
1469 case usb:
1470 if (archiving_to_media) {
1471 if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) {
1472 if (ask_me_yes_or_no
1473 (_("Is your computer a laptop, or does the CD writer incorporate BurnProof technology?")))
1474 {
1475 bkpinfo->manual_cd_tray = TRUE;
1476 }
1477 }
1478 if ((bkpinfo->compression_level =
1479 which_compression_level()) == -1) {
1480 log_to_screen(_("User has chosen not to backup the PC"));
1481 finish(1);
1482 }
1483 mr_asprintf(&comment, _("What speed is your %s (re)writer?"),
1484 bkpinfo->backup_media_string);
1485 if (bkpinfo->backup_media_type == dvd) {
1486 mr_free(bkpinfo->media_device);
1487 bkpinfo->media_device = find_dvd_device();
1488 mr_asprintf(&tmp, "1");
1489 mr_asprintf(&sz_size, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4582 MB
1490 mr_msg(1, "Setting to DVD defaults");
1491 } else if (bkpinfo->backup_media_type == usb) {
1492 strcpy(bkpinfo->media_device, VANILLA_USB_DEVICE);
1493 strcpy(sz_size, "512");
1494 } else {
1495 mr_allocstr(bkpinfo->media_device,VANILLA_SCSI_CDROM );
1496 mr_asprintf(&tmp, "4");
1497 mr_asprintf(&sz_size, "650");
1498 mr_msg(1, "Setting to CD defaults");
1499 }
1500 if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) {
1501 if (!popup_and_get_string(_("Speed"), comment, tmp)) {
1502 log_to_screen(_("User has chosen not to backup the PC"));
1503 finish(1);
1504 }
1505 }
1506 mr_free(comment);
1507
1508 bkpinfo->cdrw_speed = atoi(tmp); // if DVD then this shouldn't ever be used anyway :)
1509 mr_free(tmp);
1510
1511 mr_asprintf(&comment,
1512 _("How much data (in Megabytes) will each %s store?"),
1513 bkpinfo->backup_media_string);
1514
1515 if (!popup_and_get_string("Size", comment, sz_size)) {
1516 log_to_screen(_("User has chosen not to backup the PC"));
1517 finish(1);
1518 }
1519 mr_free(comment);
1520
1521 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1522 bkpinfo->media_size[i] = atoi(sz_size);
1523 }
1524 mr_free(sz_size);
1525
1526 if (bkpinfo->media_size[0] <= 0) {
1527 log_to_screen(_("User has chosen not to backup the PC"));
1528 finish(1);
1529 }
1530 }
1531 case cdstream:
1532 if (bkpinfo->disaster_recovery) {
1533 mr_allocstr(bkpinfo->media_device, "/dev/cdrom");
1534 mr_msg(2, "CD-ROM device assumed to be at %s",
1535 bkpinfo->media_device);
1536 } else if (bkpinfo->restore_data
1537 || bkpinfo->backup_media_type == dvd) {
1538 if (bkpinfo->media_device == NULL) {
1539 mr_asprintf(&bkpinfo->media_device, "/dev/cdrom");
1540 } // just for the heck of it :)
1541 mr_msg(1, "bkpinfo->media_device = %s",
1542 bkpinfo->media_device);
1543 tmp = find_cdrom_device(FALSE);
1544 if ((bkpinfo->backup_media_type == dvd)
1545 || (tmp != NULL)) {
1546 mr_allocstr(bkpinfo->media_device, tmp);
1547 mr_msg(1, "bkpinfo->media_device = %s",
1548 bkpinfo->media_device);
1549 mr_asprintf(&comment,
1550 _("Please specify your %s drive's /dev entry"),
1551 bkpinfo->backup_media_string);
1552 if (!popup_and_get_string
1553 (_("Device?"), comment, bkpinfo->media_device)) {
1554 log_to_screen(_("User has chosen not to backup the PC"));
1555 finish(1);
1556 }
1557 mr_free(comment);
1558 }
1559 mr_free(tmp);
1560
1561 mr_msg(2, "%s device found at %s",
1562 bkpinfo->backup_media_string,
1563 bkpinfo->media_device);
1564 } else {
1565 bkpinfo->media_device = find_cdrw_device();
1566 if (bkpinfo->media_device != NULL) {
1567 mr_asprintf(&tmp,
1568 _("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."),
1569 bkpinfo->backup_media_string,
1570 bkpinfo->media_device);
1571 if (!ask_me_yes_or_no(tmp)) {
1572 mr_free(bkpinfo->media_device);
1573 }
1574 mr_free(tmp);
1575 } else {
1576 if (g_kernel_version < 2.6) {
1577 i = popup_and_get_string(_("Device node?"),
1578 _("What is the SCSI node of your CD (re)writer, please?"),
1579 bkpinfo->media_device);
1580 } else {
1581 i = popup_and_get_string(_("/dev entry?"),
1582 _("What is the /dev entry of your CD (re)writer, please?"),
1583 bkpinfo->media_device);
1584 }
1585 if (!i) {
1586 log_to_screen(_("User has chosen not to backup the PC"));
1587 finish(1);
1588 }
1589 }
1590 }
1591 if (bkpinfo->backup_media_type == cdstream) {
1592 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1593 bkpinfo->media_size[i] = 650;
1594 }
1595 }
1596 break;
1597 case udev:
1598 if (!ask_me_yes_or_no
1599 (_("This option is for advanced users only. Are you sure?"))) {
1600 log_to_screen(_("User has chosen not to backup the PC"));
1601 finish(1);
1602 }
1603 case tape:
1604
1605 mr_free(bkpinfo->media_device);
1606 if (find_tape_device_and_size(bkpinfo->media_device, sz_size)) {
1607 mr_msg(3, _("Ok, using vanilla scsi tape."));
1608 mr_allocstr(bkpinfo->media_device,VANILLA_SCSI_TAPE"0" );
1609 if ((fin = fopen(bkpinfo->media_device, "r"))) {
1610 paranoid_fclose(fin);
1611 } else {
1612 mr_allocstr(bkpinfo->media_device,"/dev/osst0");
1613 }
1614 }
1615 if (bkpinfo->media_device != NULL) {
1616 if ((fin = fopen(bkpinfo->media_device, "r"))) {
1617 paranoid_fclose(fin);
1618 } else {
1619 if (does_file_exist("/tmp/mondo-restore.cfg")) {
1620 mr_free(bkpinfo->media_device);
1621 read_cfg_var("/tmp/mondo-restore.cfg", "media-dev",
1622 bkpinfo->media_device);
1623 }
1624 }
1625 mr_asprintf(&tmp,
1626 _("I think I've found your tape streamer at %s; am I right on the money?"),
1627 bkpinfo->media_device);
1628 if (!ask_me_yes_or_no(tmp)) {
1629 mr_free(bkpinfo->media_device);
1630 }
1631 mr_free(tmp);
1632 } else {
1633 if (!popup_and_get_string
1634 (_("Device name?"),
1635 _("What is the /dev entry of your tape streamer?"),
1636 bkpinfo->media_device)) {
1637 log_to_screen(_("User has chosen not to backup the PC"));
1638 finish(1);
1639 }
1640 }
1641 mr_asprintf(&tmp, "ls -l %s", bkpinfo->media_device);
1642 if (run_program_and_log_output(tmp, FALSE)) {
1643 log_to_screen(_("User has not specified a valid /dev entry"));
1644 finish(1);
1645 }
1646 mr_free(tmp);
1647 mr_msg(4, "sz_size = %s", sz_size);
1648 mr_free(sz_size);
1649 bkpinfo->media_size[0] = 0;
1650 mr_msg(4, "media_size[0] = %ld", bkpinfo->media_size[0]);
1651 if (bkpinfo->media_size[0] <= 0) {
1652 bkpinfo->media_size[0] = 0;
1653 }
1654 for (i = 1; i <= MAX_NOOF_MEDIA; i++) {
1655 bkpinfo->media_size[i] = bkpinfo->media_size[0];
1656 }
1657 if (archiving_to_media) {
1658 if ((bkpinfo->compression_level =
1659 which_compression_level()) == -1) {
1660 log_to_screen(_("User has chosen not to backup the PC"));
1661 finish(1);
1662 }
1663 }
1664 break;
1665
1666
1667
1668 case nfs:
1669 if (bkpinfo->nfs_mount == NULL) {
1670 bkpinfo->nfs_mount = call_program_and_get_last_line_of_output
1671 ("mount | grep \":\" | cut -d' ' -f1 | head -n1");
1672 }
1673#ifdef __FreeBSD__
1674 if (TRUE)
1675#else
1676 if (!bkpinfo->disaster_recovery)
1677#endif
1678 {
1679 if (!popup_and_get_string
1680 (_("NFS dir."),
1681 _("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.)"),
1682 bkpinfo->nfs_mount)) {
1683 log_to_screen(_("User has chosen not to backup the PC"));
1684 finish(1);
1685 }
1686 if (!bkpinfo->restore_data) {
1687 if ((bkpinfo->compression_level =
1688 which_compression_level()) == -1) {
1689 log_to_screen(_("User has chosen not to backup the PC"));
1690 finish(1);
1691 }
1692 }
1693 // check whether already mounted - we better remove
1694 // surrounding spaces and trailing '/' for this
1695 /* BERLIOS: Useless
1696 strip_spaces(bkpinfo->nfs_mount);
1697 */
1698 if (bkpinfo->nfs_mount[strlen(bkpinfo->nfs_mount) - 1] == '/')
1699 bkpinfo->nfs_mount[strlen(bkpinfo->nfs_mount) - 1] = '\0';
1700 mr_asprintf(&command, "mount | grep \"%s \" | cut -d' ' -f3",
1701 bkpinfo->nfs_mount);
1702 mr_free(bkpinfo->isodir);
1703 bkpinfo->isodir = call_program_and_get_last_line_of_output(command);
1704 mr_free(command);
1705
1706 mr_asprintf(&comment,
1707 _("How much data (in Megabytes) will each media store?"));
1708 if (!popup_and_get_string(_("Size"), comment, sz_size, 5)) {
1709 log_to_screen(_("User has chosen not to backup the PC"));
1710 finish(1);
1711 }
1712 mr_free(comment);
1713 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1714 bkpinfo->media_size[i] = atoi(sz_size);
1715 }
1716 if (bkpinfo->media_size[0] <= 0) {
1717 log_to_screen(_("User has chosen not to backup the PC"));
1718 finish(1);
1719 }
1720 }
1721 if (bkpinfo->disaster_recovery) {
1722 system("umount /tmp/isodir 2> /dev/null");
1723 if (!popup_and_get_string
1724 (_("NFS share"), _("Which remote NFS share should I mount?"),
1725 bkpinfo->nfs_mount)) {
1726 log_to_screen(_("User has chosen not to backup the PC"));
1727 finish(1);
1728 }
1729 }
1730 if (!is_this_device_mounted(bkpinfo->nfs_mount)) {
1731 mr_free(bkpinfo->isodir);
1732 mr_asprintf(&bkpinfo->isodir, "/tmp/isodir.mondo.%d",
1733 (int) (random() % 32768));
1734 mr_asprintf(&command, "mkdir -p %s", bkpinfo->isodir);
1735 run_program_and_log_output(command, 5);
1736 mr_free(command);
1737
1738 mr_asprintf(&tmp, "mount -t nfs -o nolock %s %s", bkpinfo->nfs_mount,
1739 bkpinfo->isodir);
1740 run_program_and_log_output(tmp, 5);
1741 mr_free(tmp);
1742 mr_asprintf(&g_selfmounted_isodir, bkpinfo->isodir);
1743 }
1744 if (!is_this_device_mounted(bkpinfo->nfs_mount)) {
1745 popup_and_OK
1746 (_("Please mount that partition before you try to backup to or restore from it."));
1747 finish(1);
1748 }
1749 if (!popup_and_get_string
1750 (_("Directory"), _("Which directory within that mountpoint?"),bkpinfo->nfs_remote_dir)) {
1751 log_to_screen(_("User has chosen not to backup the PC"));
1752 finish(1);
1753 }
1754
1755 mr_asprintf(&command, "echo hi > '%s/%s/.dummy.txt'", bkpinfo->isodir,
1756 bkpinfo->nfs_remote_dir);
1757 while (run_program_and_log_output(command, FALSE)) {
1758 mr_asprintf(&prompt,
1759 _("Directory '%s' under mountpoint '%s' does not exist or is not writable. You can fix this or change the directory and retry or cancel the backup."),
1760 bkpinfo->nfs_remote_dir, bkpinfo->isodir);
1761 if (!popup_and_get_string
1762 (_("Directory"), prompt, bkpinfo->nfs_remote_dir)) {
1763 log_to_screen(_("User has chosen not to backup the PC"));
1764 finish(1);
1765 }
1766 mr_free(prompt);
1767
1768 mr_free(command);
1769 mr_asprintf(&command, "echo hi > '%s/%s/.dummy.txt'", bkpinfo->isodir,
1770 bkpinfo->nfs_remote_dir);
1771 }
1772 mr_free(command);
1773
1774 if (!popup_and_get_string
1775 (_("Prefix."),
1776 _("Please enter the prefix that will be prepended to your ISO filename. Example: machine1 to obtain machine1-[1-9]*.iso files"),
1777 bkpinfo->prefix)) {
1778 log_to_screen(_("User has chosen not to backup the PC"));
1779 finish(1);
1780 }
1781 mr_msg(3, "prefix set to %s", bkpinfo->prefix);
1782
1783 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1784 bkpinfo->media_size[i] = 650;
1785 }
1786 mr_msg(3, "Just set nfs_remote_dir to %s",
1787 bkpinfo->nfs_remote_dir);
1788 mr_msg(3, "isodir is still %s", bkpinfo->isodir);
1789 break;
1790
1791 case iso:
1792 if (!bkpinfo->disaster_recovery) {
1793 if (!popup_and_get_string
1794 (_("Storage dir."),
1795 _("Please enter the full path that contains your ISO images. Example: /mnt/raid0_0"),
1796 bkpinfo->isodir)) {
1797 log_to_screen(_("User has chosen not to backup the PC"));
1798 finish(1);
1799 }
1800 if (archiving_to_media) {
1801 if ((bkpinfo->compression_level =
1802 which_compression_level()) == -1) {
1803 log_to_screen(_("User has chosen not to backup the PC"));
1804 finish(1);
1805 }
1806 if (!popup_and_get_string
1807 (_("ISO size."),
1808 _("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 or DVD's you plan to backup to."), sz_size)) {
1809 log_to_screen(_("User has chosen not to backup the PC"));
1810 finish(1);
1811 }
1812 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1813 bkpinfo->media_size[i] = atoi(sz_size);
1814 }
1815 } else {
1816 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1817 bkpinfo->media_size[i] = 650;
1818 }
1819 }
1820 }
1821 if (!popup_and_get_string
1822 (_("Prefix."),
1823 _("Please enter the prefix that will be prepended to your ISO filename. Example: machine1 to obtain machine1-[1-9]*.iso files"),
1824 bkpinfo->prefix)) {
1825 log_to_screen("User has chosen not to backup the PC");
1826 finish(1);
1827 }
1828 mr_msg(3, "prefix set to %s", bkpinfo->prefix);
1829 break;
1830 default:
1831 fatal_error
1832 ("I, Mojo Jojo, shall defeat those pesky Powerpuff Girls!");
1833 }
1834 if (archiving_to_media) {
1835
1836 if (bkpinfo->boot_device) {
1837 mr_free(bkpinfo->boot_device);
1838 }
1839#ifdef __FreeBSD__
1840 bkpinfo->boot_device = call_program_and_get_last_line_of_output
1841 ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/\\([0-9]\\).*/\\1/'");
1842#else
1843 bkpinfo->boot_device = call_program_and_get_last_line_of_output
1844 ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'");
1845#endif
1846 i = which_boot_loader(bkpinfo->boot_device);
1847 if (i == 'U') // unknown
1848 {
1849
1850#ifdef __FreeBSD__
1851 if (!popup_and_get_string
1852 (_("Boot device"),
1853 _("What is your boot device? (e.g. /dev/ad0)"),
1854 bkpinfo->boot_device)) {
1855 log_to_screen(_("User has chosen not to backup the PC"));
1856 finish(1);
1857 }
1858 i = which_boot_loader(bkpinfo->boot_device);
1859#else
1860 if (!popup_and_get_string
1861 (_("Boot device"),
1862 _("What is your boot device? (e.g. /dev/hda)"),
1863 bkpinfo->boot_device)) {
1864 log_to_screen(_("User has chosen not to backup the PC"));
1865 finish(1);
1866 }
1867 if (does_string_exist_in_boot_block
1868 (bkpinfo->boot_device, "LILO")) {
1869 i = 'L';
1870 } else
1871 if (does_string_exist_in_boot_block
1872 (bkpinfo->boot_device, "ELILO")) {
1873 i = 'E';
1874 } else
1875 if (does_string_exist_in_boot_block
1876 (bkpinfo->boot_device, "GRUB")) {
1877 i = 'G';
1878 } else {
1879 i = 'U';
1880 }
1881#endif
1882 if (i == 'U') {
1883 if (ask_me_yes_or_no
1884 (_("Unidentified boot loader. Shall I restore it byte-for-byte at restore time and hope for the best?")))
1885 {
1886 i = 'R'; // raw
1887 } else {
1888 log_to_screen
1889 (_("I cannot find your boot loader. Please run mondoarchive with parameters."));
1890 finish(1);
1891 }
1892 }
1893 }
1894 bkpinfo->boot_loader = i;
1895 mr_asprintf(&bkpinfo->include_paths, "/");
1896 if (!popup_and_get_string
1897 (_("Backup paths"),
1898 _("Please enter paths which you want me to backup. The default is '/' (i.e. everything)."),
1899 bkpinfo->include_paths)) {
1900 log_to_screen(_("User has chosen not to backup the PC"));
1901 finish(1);
1902 }
1903
1904 tmp = list_of_NFS_mounts_only();
1905 if (strlen(tmp) > 2) {
1906 if (bkpinfo->exclude_paths != NULL) {
1907 tmp1 = bkpinfo->exclude_paths;
1908 mr_asprintf(&bkpinfo->exclude_paths, "%s %s", tmp1, tmp);
1909 mr_free(tmp1);
1910 } else {
1911 bkpinfo->exclude_paths = tmp;
1912 }
1913 }
1914 mr_free(tmp);
1915// NTFS
1916 tmp = call_program_and_get_last_line_of_output
1917 ("parted2fdisk -l | grep -i ntfs | awk '{ print $1};' | tr -s '\\n' ' ' | awk '{ print $0};'");
1918 if (strlen(tmp) > 2) {
1919 if (!popup_and_get_string
1920 (_("NTFS partitions"),
1921 _("Please enter/confirm the NTFS partitions you wish to backup as well."),
1922 tmp1)) {
1923 log_to_screen(_("User has chosen not to backup the PC"));
1924 finish(1);
1925 }
1926 if (bkpinfo->image_devs != NULL) {
1927 tmp2 = bkpinfo->image_devs;
1928 mr_asprintf(&bkpinfo->image_devs, "%s %s", tmp2, tmp1);
1929 mr_free(tmp2);
1930 } else {
1931 bkpinfo->image_devs = tmp1;
1932 }
1933 mr_free(tmp1);
1934 }
1935 mr_free(tmp);
1936
1937 if (!popup_and_get_string
1938 (_("Exclude paths"),
1939 _("Please enter paths which you do NOT want to backup. Separate them with spaces. NB: /tmp and /proc are always excluded. :-) Just hit 'Enter' if you want to do a full system backup."),
1940 bkpinfo->exclude_paths)) {
1941 log_to_screen(_("User has chosen not to backup the PC"));
1942 finish(1);
1943 }
1944 bkpinfo->make_cd_use_lilo = FALSE;
1945 bkpinfo->backup_data = TRUE;
1946 bkpinfo->verify_data =
1947 ask_me_yes_or_no
1948 (_("Will you want to verify your backups after Mondo has created them?"));
1949
1950#ifndef __FreeBSD__
1951 if (!ask_me_yes_or_no
1952 (_("Are you confident that your kernel is a sane, sensible, standard Linux kernel? Say 'no' if you are using a Gentoo <1.4 or Debian <3.0, please."))){
1953 mr_allocstr(bkpinfo->kernel_path, "FAILSAFE");
1954 }
1955#endif
1956
1957 if (!ask_me_yes_or_no
1958 (_("Are you sure you want to proceed? Hit 'no' to abort."))) {
1959 log_to_screen(_("User has chosen not to backup the PC"));
1960 finish(1);
1961 }
1962 } else {
1963 bkpinfo->restore_data = TRUE; // probably...
1964 }
1965
1966 if (bkpinfo->backup_media_type == iso
1967 || bkpinfo->backup_media_type == nfs) {
1968 g_ISO_restore_mode = TRUE;
1969 }
1970#ifdef __FreeSD__
1971// skip
1972#else
1973 if (bkpinfo->backup_media_type == nfs) {
1974 mr_msg(3, "I think the NFS mount is mounted at %s",
1975 bkpinfo->isodir);
1976 }
1977 log_it("isodir = %s", bkpinfo->isodir);
1978 log_it("nfs_mount = '%s'", bkpinfo->nfs_mount);
1979#endif
1980
1981 log_it("media device = %s", bkpinfo->media_device);
1982 log_it("media size = %ld", bkpinfo->media_size[1]);
1983 log_it("media type = %s", bkpinfo->backup_media_string);
1984 log_it("prefix = %s", bkpinfo->prefix);
1985 log_it("compression = %ld", bkpinfo->compression_level);
1986 log_it("include_paths = '%s'", bkpinfo->include_paths);
1987 log_it("exclude_paths = '%s'", bkpinfo->exclude_paths);
1988 log_it("scratchdir = '%s'", bkpinfo->scratchdir);
1989 log_it("tmpdir = '%s'", bkpinfo->tmpdir);
1990 log_it("boot_device = '%s' (loader=%c)", bkpinfo->boot_device,
1991 bkpinfo->boot_loader);
1992 if (bkpinfo->media_size[0] < 0) {
1993 if (archiving_to_media) {
1994 fatal_error("Media size is less than zero.");
1995 } else {
1996 mr_msg(2, "Warning - media size is less than zero.");
1997 bkpinfo->media_size[0] = 0;
1998 }
1999 }
2000 return (0);
2001}
2002
2003
2004/**
2005 * Get a space-separated list of NFS mounts.
2006 * @return The list created.
2007 * @note The return value should be freed by caller
2008 * @bug Even though we only want the mounts, the devices are still checked.
2009 */
2010char *list_of_NFS_mounts_only(void)
2011{
2012 return(call_program_and_get_last_line_of_output
2013 ("mount -t coda,ncpfs,nfs,smbfs,cifs,afs,mvfs | tr -s '\t' ' ' | cut -d' ' -f3 | tr -s '\n' ' ' | awk '{print $0;}'"));
2014 /* BERLIOS : Useless
2015 mr_asprintf(&exclude_these_devices,
2016 call_program_and_get_last_line_of_output
2017 ("tr -s '\t' ' ' < /etc/fstab | grep -E '( (coda|ncpfs|nfs|smbfs|cifs|mvfs) )' | cut -d' ' -f1 | tr -s '\n' ' ' | awk '{print $0;}'"));
2018 */
2019}
2020/* @} - end of utilityGroup */
2021
2022
2023/**
2024 * Set the tmpdir and scratchdir to reside on the partition with the most free space.
2025 * Automatically excludes DOS, NTFS, SMB, and NFS filesystems.
2026 * @param bkpinfo The backup information structure. @c bkpinfo->tmpdir and @c bkpinfo->scratchdir will be set.
2027 * @ingroup utilityGroup
2028 */
2029void sensibly_set_tmpdir_and_scratchdir(struct s_bkpinfo *bkpinfo)
2030{
2031 char *tmp = NULL;
2032 char *command = NULL;
2033 char *sz = NULL;
2034 int i = 0;
2035
2036 assert(bkpinfo != NULL);
2037
2038#ifdef __FreeBSD__
2039 tmp = call_program_and_get_last_line_of_output
2040 ("df -m -P -t nonfs,msdosfs,ntfs,smbfs,smb,cifs,afs,mvfs | tr -s '\t' ' ' | grep -vE \"none|Filesystem\" | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'");
2041#else
2042 tmp = call_program_and_get_last_line_of_output
2043 ("df -m -P -x nfs -x vfat -x ntfs -x smbfs -x smb -x cifs -x afs -x mvfs | sed 's/ /devdev/' | tr -s '\t' ' ' | grep -vE \"none|Filesystem|/dev/shm\" | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'");
2044#endif
2045
2046 if (tmp[0] != '/') {
2047 mr_asprintf(&sz, "/%s", tmp);
2048 mr_free(tmp);
2049 tmp = sz;
2050 }
2051 if (!tmp[0]) {
2052 fatal_error("I couldn't figure out the tempdir!");
2053 }
2054 i = (int) (random() % 32768);
2055 mr_free(bkpinfo->tmpdir);
2056 mr_asprintf(&bkpinfo->tmpdir, "%s/mondo.tmp.%d", tmp, i);
2057 log_it("bkpinfo->tmpdir is being set to %s", bkpinfo->tmpdir);
2058
2059 mr_free(bkpinfo->scratchdir);
2060 mr_asprintf(&bkpinfo->scratchdir, "%s/mondo.scratch.%d", tmp, i);
2061 log_it("bkpinfo->scratchdir is being set to %s", bkpinfo->scratchdir);
2062
2063 mr_asprintf(&command, "rm -Rf %s/mondo.tmp.* %s/mondo.scratch.*", tmp, tmp);
2064 mr_free(tmp);
2065
2066 paranoid_system(command);
2067 mr_free(command);
2068}
2069
2070
2071/**
2072 * @addtogroup deviceGroup
2073 * @{
2074 */
2075/**
2076 * If we can read @p dev, set @p output to it.
2077 * If @p dev cannot be read, set @p output to "".
2078 * @param dev The device to check for.
2079 * @param output Set to @p dev if @p dev exists, NULL otherwise. Needs to be freed by caller
2080 * @return TRUE if @p dev exists, FALSE if it doesn't.
2081 */
2082bool set_dev_to_this_if_rx_OK(char *output, char *dev)
2083{
2084 char *command = NULL;
2085
2086 mr_free(output);
2087 if (!dev || dev[0] == '\0') {
2088 return (FALSE);
2089 }
2090 mr_msg(10, "Injecting %s", dev);
2091 inject_device(dev);
2092 if (!does_file_exist(dev)) {
2093 mr_msg(10, "%s doesn't exist. Returning FALSE.", dev);
2094 return (FALSE);
2095 }
2096 mr_asprintf(&command, "dd bs=%ld count=1 if=%s of=/dev/null &> /dev/null",
2097 512L, dev);
2098 if (!run_program_and_log_output(command, FALSE)
2099 && !run_program_and_log_output(command, FALSE)) {
2100 mr_asprintf(&output, dev);
2101 mr_msg(4, "Found it - %s", dev);
2102 mr_free(command);
2103 return (TRUE);
2104 } else {
2105 mr_msg(4, "It's not %s", dev);
2106 mr_free(command);
2107 return (FALSE);
2108 }
2109}
2110
2111
2112/**
2113 * Find out what number CD is in the drive.
2114 * @param bkpinfo The backup information structure. The @c bkpinfo->media_device field is the only one used.
2115 * @return The current CD number, or -1 if it could not be found.
2116 * @note If the CD is not mounted, it will be mounted
2117 * (and remain mounted after this function returns).
2118 */
2119int what_number_cd_is_this(struct s_bkpinfo *bkpinfo)
2120{
2121 int cd_number = -1;
2122 char *mountdev = NULL;
2123 char *tmp = NULL;
2124 char *tmp1 = NULL;
2125
2126 assert(bkpinfo != NULL);
2127 if (g_ISO_restore_mode) {
2128 mr_asprintf(&tmp, "mount | grep iso9660 | awk '{print $3;}'");
2129
2130 tmp1 = call_program_and_get_last_line_of_output(tmp);
2131 mr_free(tmp);
2132
2133 mr_asprintf(&mountdev, "%s/archives/THIS-CD-NUMBER", tmp1);
2134 mr_free(tmp1);
2135
2136 tmp = last_line_of_file(mountdev);
2137 cd_number = atoi(tmp);
2138 mr_free(mountdev);
2139 mr_free(tmp);
2140 return (cd_number);
2141 }
2142
2143 if (bkpinfo->media_device == NULL) {
2144 log_it
2145 ("(what_number_cd_is_this) Warning - media_device unknown. Finding out...");
2146 bkpinfo->media_device = find_cdrom_device(FALSE);
2147 }
2148 if (!is_this_device_mounted(MNT_CDROM)) {
2149 (void)mount_CDROM_here(bkpinfo->media_device, MNT_CDROM);
2150 }
2151 tmp = last_line_of_file(MNT_CDROM "/archives/THIS-CD-NUMBER");
2152 cd_number = atoi(tmp);
2153 mr_free(tmp);
2154 return (cd_number);
2155}
2156
2157
2158/**
2159 * Find out what device is mounted as root (/).
2160 * @return Root device.
2161 * @note The returned string points to static storage and will be overwritten with every call.
2162 * @bug A bit of a misnomer; it's actually finding out the root device.
2163 * The mountpoint (where it's mounted) will obviously be '/'.
2164 */
2165char *where_is_root_mounted()
2166{
2167 char *tmp;
2168
2169#ifdef __FreeBSD__
2170 tmp = call_program_and_get_last_line_of_output
2171 ("mount | grep \" on / \" | cut -d' ' -f1");
2172#else
2173 tmp = call_program_and_get_last_line_of_output
2174 ("mount | grep \" on / \" | cut -d' ' -f1 | sed s/[0-9]// | sed s/[0-9]//");
2175 if (strstr(tmp, "/dev/cciss/")) {
2176 mr_free(tmp);
2177 tmp = call_program_and_get_last_line_of_output
2178 ("mount | grep \" on / \" | cut -d' ' -f1 | cut -dp -f1");
2179 }
2180 if (strstr(tmp, "/dev/md")) {
2181 mr_free(tmp);
2182 tmp = call_program_and_get_last_line_of_output
2183 ("mount | grep \" on / \" | cut -d' ' -f1");
2184 }
2185#endif
2186
2187 return (tmp);
2188}
2189
2190
2191/**
2192 * Find out which boot loader is in use.
2193 * @param which_device Device to look for the boot loader on.
2194 * @return 'L' for LILO, 'E'for ELILO, 'G' for GRUB, 'B' or 'D' for FreeBSD boot loaders, or 'U' for Unknown.
2195 * @note Under Linux, all drives are examined, not just @p which_device.
2196 */
2197char which_boot_loader(char *which_device)
2198{
2199#ifdef __FreeBSD__
2200 int count_lilos = 0;
2201 int count_grubs = 0;
2202 int count_boot0s = 0;
2203 int count_dangerouslydedicated = 0;
2204
2205 log_it("looking at drive %s's MBR", which_device);
2206 if (does_string_exist_in_boot_block(which_device, "GRUB")) {
2207 count_grubs++;
2208 }
2209 if (does_string_exist_in_boot_block(which_device, "LILO")) {
2210 count_lilos++;
2211 }
2212 if (does_string_exist_in_boot_block(which_device, "Drive")) {
2213 count_boot0s++;
2214 }
2215 if (does_string_exist_in_first_N_blocks
2216 (which_device, "FreeBSD/i386", 17)) {
2217 count_dangerouslydedicated++;
2218 }
2219 log_it("%d grubs and %d lilos and %d elilos and %d boot0s and %d DD\n",
2220 count_grubs, count_lilos, count_elilos, count_boot0s,
2221 count_dangerouslydedicated);
2222
2223 if (count_grubs && !count_lilos) {
2224 return ('G');
2225 } else if (count_lilos && !count_grubs) {
2226 return ('L');
2227 } else if (count_grubs == 1 && count_lilos == 1) {
2228 log_it("I'll bet you used to use LILO but switched to GRUB...");
2229 return ('G');
2230 } else if (count_boot0s == 1) {
2231 return ('B');
2232 } else if (count_dangerouslydedicated) {
2233 return ('D');
2234 } else {
2235 log_it("Unknown boot loader");
2236 return ('U');
2237 }
2238#else
2239 /*@ buffer ***************************************************** */
2240 char *list_drives_cmd;
2241 char *current_drive = NULL;
2242
2243 /*@ pointers *************************************************** */
2244 FILE *pdrives;
2245
2246 /*@ int ******************************************************** */
2247 int count_lilos = 0;
2248 int count_grubs = 0;
2249 size_t n = 0;
2250
2251 /*@ end vars *************************************************** */
2252
2253#ifdef __IA64__
2254 /* No choice for it */
2255 return ('E');
2256#endif
2257 assert(which_device != NULL);
2258 mr_asprintf(&list_drives_cmd,
2259 "parted2fdisk -l 2>/dev/null | grep \"/dev/.*:\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/; echo %s",
2260 where_is_root_mounted());
2261 log_it("list_drives_cmd = %s", list_drives_cmd);
2262
2263 if (!(pdrives = popen(list_drives_cmd, "r"))) {
2264 log_OS_error("Unable to open list of drives");
2265 mr_free(list_drives_cmd);
2266 return ('\0');
2267 }
2268 mr_free(list_drives_cmd);
2269
2270 for (mr_getline(&current_drive, &n, pdrives); !feof(pdrives);
2271 mr_getline(&current_drive, &n, pdrives)) {
2272 strip_spaces(current_drive);
2273 log_it("looking at drive %s's MBR", current_drive);
2274 if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
2275 count_grubs++;
2276 mr_allocstr(which_device,current_drive);
2277 break;
2278 }
2279 if (does_string_exist_in_boot_block(current_drive, "LILO")) {
2280 count_lilos++;
2281 mr_allocstr(which_device,current_drive);
2282 break;
2283 }
2284 }
2285
2286 if (pclose(pdrives)) {
2287 log_OS_error("Cannot pclose pdrives");
2288 }
2289 log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
2290 if (count_grubs && !count_lilos) {
2291 return ('G');
2292 } else if (count_lilos && !count_grubs) {
2293 return ('L');
2294 } else if (count_grubs == 1 && count_lilos == 1) {
2295 log_it("I'll bet you used to use LILO but switched to GRUB...");
2296 return ('G');
2297 } else {
2298 log_it("Unknown boot loader");
2299 return ('U');
2300 }
2301#endif
2302}
2303
2304
2305/**
2306 * Write zeroes over the first 16K of @p device.
2307 * @param device The device to zero.
2308 * @return 0 for success, 1 for failure.
2309 */
2310int zero_out_a_device(char *device)
2311{
2312 FILE *fout;
2313 int i;
2314
2315 assert_string_is_neither_NULL_nor_zerolength(device);
2316
2317 log_it("Zeroing drive %s", device);
2318 if (!(fout = fopen(device, "w"))) {
2319 log_OS_error("Unable to open/write to device");
2320 return (1);
2321 }
2322 for (i = 0; i < 16384; i++) {
2323 fputc('\0', fout);
2324 }
2325 paranoid_fclose(fout);
2326 log_it("Device successfully zeroed.");
2327 return (0);
2328}
2329
2330
2331/**
2332 * Return the device pointed to by @p incoming.
2333 * @param incoming The device to resolve symlinks for.
2334 * @return The path to the real device file.
2335 * @note The returned string points to a storage that need to be freed by the caller
2336 * @bug Won't work with file v4.0; needs to be written in C.
2337 */
2338char *resolve_softlinks_to_get_to_actual_device_file(char *incoming)
2339{
2340 char *output = NULL;
2341 char *command = NULL;
2342 char *curr_fname = NULL;
2343 char *scratch = NULL;
2344 char *tmp = NULL;
2345 char *p = NULL;
2346
2347 struct stat statbuf;
2348 if (!does_file_exist(incoming)) {
2349 log_it
2350 ("resolve_softlinks_to_get_to_actual_device_file --- device not found");
2351 mr_asprintf(&output, incoming);
2352 } else {
2353 mr_asprintf(&curr_fname, incoming);
2354 lstat(curr_fname, &statbuf);
2355 while (S_ISLNK(statbuf.st_mode)) {
2356 mr_msg(1, "curr_fname = %s", curr_fname);
2357 mr_asprintf(&command, "file %s", curr_fname);
2358 tmp = call_program_and_get_last_line_of_output(command);
2359 mr_free(command);
2360
2361 for (p = tmp + strlen(tmp); p != tmp && *p != '`' && *p != ' ';
2362 p--);
2363 p++;
2364 mr_asprintf(&scratch, p);
2365 for (p = scratch; *p != '\0' && *p != '\''; p++);
2366 *p = '\0';
2367 mr_msg(0, "curr_fname %s --> '%s' --> %s", curr_fname, tmp, scratch);
2368 mr_free(tmp);
2369
2370 if (scratch[0] == '/') {
2371 mr_free(curr_fname);
2372 mr_asprintf(&curr_fname, scratch); // copy whole thing because it's an absolute softlink
2373 } else { // copy over the basename cos it's a relative softlink
2374 p = curr_fname + strlen(curr_fname);
2375 while (p != curr_fname && *p != '/') {
2376 p--;
2377 }
2378 if (*p == '/') {
2379 p++;
2380 }
2381 *p = '\0';
2382 mr_asprintf(&tmp, "%s%s", curr_fname, scratch);
2383 mr_free(curr_fname);
2384 curr_fname = tmp;
2385 }
2386 mr_free(scratch);
2387 lstat(curr_fname, &statbuf);
2388 }
2389 mr_asprintf(&output, curr_fname);
2390 log_it("resolved %s to %s", incoming, output);
2391 mr_free(curr_fname);
2392 }
2393 return (output);
2394}
2395
2396/* @} - end of deviceGroup */
2397
2398
2399/**
2400 * Return the type of partition format (GPT or MBR)
2401 * Caller needs to free the return value
2402 */
2403char *which_partition_format(const char *drive)
2404{
2405 char *output = NULL;
2406 char *tmp = NULL;
2407 char *command = NULL;
2408 char *fdisk = NULL;
2409
2410 mr_msg(0, "Looking for partition table format type");
2411 mr_asprintf(&fdisk, "/sbin/parted2fdisk");
2412 mr_msg(1, "Using %s", fdisk);
2413 mr_asprintf(&command, "%s -l %s | grep 'EFI GPT'", fdisk, drive);
2414 mr_free(fdisk);
2415
2416 tmp = call_program_and_get_last_line_of_output(command);
2417 mr_free(command);
2418
2419 if (strstr(tmp, "GPT") == NULL) {
2420 mr_asprintf(&output, "MBR");
2421 } else {
2422 mr_asprintf(&output, "GPT");
2423 }
2424 mr_free(tmp);
2425 mr_msg(0, "Found %s partition table format type", output);
2426 return(output);
2427}
2428/* @} - end of deviceGroup */
Note: See TracBrowser for help on using the repository browser.