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

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

Some merges from stable (synchro for mem. mngt)

  • Property svn:keywords set to Id
File size: 64.4 KB
Line 
1/* libmondo-devices.c Subroutines for handling devices
2 $Id: libmondo-devices.c 1176 2007-02-16 13:16:14Z 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#include "mr_str.h"
22
23#include <sys/ioctl.h>
24#include <sys/types.h>
25#include <unistd.h>
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 1176 2007-02-16 13:16:14Z bruno $";
39
40extern int g_current_media_number;
41extern double g_kernel_version;
42
43extern bool g_ISO_restore_mode;
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 = FALSE;
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 } else {
1115 mr_getline(&good_formats, &n , pin);
1116 if (pclose(pin)) {
1117 log_OS_error("Cannot pclose good formats");
1118 }
1119 mr_strip_spaces(good_formats);
1120 // " ntfs 7 " -- um, cheating much? :)
1121 mr_asprintf(&tmp, " %s swap lvm raid ntfs 7 ",good_formats);
1122 mr_free(good_formats);
1123 good_formats = tmp;
1124
1125 if (strstr(good_formats, format_sz)) {
1126 retval = TRUE;
1127 }
1128 mr_free(good_formats);
1129 }
1130 mr_free(format_sz);
1131 return (retval);
1132}
1133
1134
1135/** @def SWAPLIST_COMMAND The command to list the swap files/partitions in use. */
1136
1137/**
1138 * Determine whether @p device_raw is currently mounted.
1139 * @param device_raw The device to check.
1140 * @return TRUE if it's mounted, FALSE if not.
1141 */
1142bool is_this_device_mounted(char *device_raw)
1143{
1144
1145 /*@ pointers **************************************************** */
1146 FILE *fin;
1147
1148 /*@ buffers ***************************************************** */
1149 char *incoming = NULL;
1150 char *device_with_tab = NULL;
1151 char *device_with_space = NULL;
1152 char *tmp = NULL;
1153 size_t n = 0;
1154
1155#ifdef __FreeBSD__
1156#define SWAPLIST_COMMAND "swapinfo"
1157#else
1158#define SWAPLIST_COMMAND "cat /proc/swaps"
1159#endif
1160
1161 /*@ end vars **************************************************** */
1162
1163 assert(device_raw != NULL);
1164// assert_string_is_neither_NULL_nor_zerolength(device_raw);
1165 if (device_raw[0] != '/' && !strstr(device_raw, ":/")) {
1166 mr_msg(1, "%s needs to have a '/' prefixed - I'll do it",
1167 device_raw);
1168 mr_asprintf(&tmp, "/%s", device_raw);
1169 } else {
1170 mr_asprintf(&tmp, device_raw);
1171 }
1172 mr_msg(1, "Is %s mounted?", tmp);
1173 if (!strcmp(tmp, "/proc") || !strcmp(tmp, "proc")) {
1174 mr_msg(1,
1175 "I don't know how the heck /proc made it into the mountlist. I'll ignore it.");
1176 return (FALSE);
1177 }
1178 mr_asprintf(&device_with_tab, "%s\t", tmp);
1179 mr_asprintf(&device_with_space, "%s ", tmp);
1180 mr_free(tmp);
1181
1182 if (!(fin = popen("mount", "r"))) {
1183 log_OS_error("Cannot popen 'mount'");
1184 return (FALSE);
1185 }
1186 for (mr_getline(&incoming, &n, fin); !feof(fin);
1187 mr_getline(&incoming, &n, fin)) {
1188 if (strstr(incoming, device_with_space) //> incoming
1189 || strstr(incoming, device_with_tab)) // > incoming)
1190 {
1191 paranoid_pclose(fin);
1192 mr_free(incoming);
1193 return(TRUE);
1194 }
1195 }
1196 mr_free(incoming);
1197 mr_free(device_with_tab);
1198 paranoid_pclose(fin);
1199
1200 mr_asprintf(&tmp, "%s | grep -E \"^%s\" > /dev/null 2> /dev/null",
1201 SWAPLIST_COMMAND, device_with_space);
1202 mr_free(device_with_space);
1203
1204 mr_msg(4, "tmp (command) = '%s'", tmp);
1205 if (!system(tmp)) {
1206 mr_free(tmp);
1207 return(TRUE);
1208 }
1209 mr_free(tmp);
1210 return (FALSE);
1211}
1212
1213
1214#ifdef __FreeBSD__
1215// CODE IS FREEBSD-SPECIFIC
1216/**
1217 * Create a loopback device for specified @p fname.
1218 * @param fname The file to associate with a device.
1219 * @return /dev entry for the device (to be freed by the caller),
1220 * or NULL if it couldn't be allocated.
1221 */
1222char *make_vn(char *fname)
1223{
1224 char *device = NULL;
1225 char *mddevice = NULL;
1226 char *command = NULL;
1227 int vndev = 2;
1228
1229 if (atoi
1230 (call_program_and_get_last_line_of_output
1231 ("/sbin/sysctl -n kern.osreldate")) < 500000) {
1232 do {
1233 mr_free(mddevice);
1234 mr_asprintf(&mddevice, "vn%ic", vndev++);
1235 mr_free(command);
1236 mr_asprintf(&command, "vnconfig %s %s", mddevice, fname);
1237
1238 if (vndev > 10) {
1239 mr_free(command);
1240 mr_free(mddevice);
1241 return NULL;
1242 }
1243 }
1244 while (system(command));
1245 mr_free(command);
1246 } else {
1247 mr_asprintf(&command, "mdconfig -a -t vnode -f %s", fname);
1248 mr_asprintf(&mddevice, call_program_and_get_last_line_of_output(command));
1249 mr_free(command);
1250
1251 if (!strstr(mddevice, "md")) {
1252 mr_free(mddevice);
1253 return NULL;
1254 }
1255 }
1256 mr_asprintf(&device, "/dev/%s", mddevice);
1257 mr_free(mddevice);
1258 return(device);
1259}
1260
1261
1262// CODE IS FREEBSD-SPECIFIC
1263/**
1264 * Deallocate specified @p dname.
1265 * This should be called when you are done with the device created by make_vn(),
1266 * so the system does not run out of @c vn devices.
1267 * @param dname The device to deallocate.
1268 * @return 0 for success, nonzero for failure.
1269 */
1270int kick_vn(char *dname)
1271{
1272 char *command;
1273 int ret = 0;
1274
1275 if (strncmp(dname, "/dev/", 5) == 0) {
1276 dname += 5;
1277 }
1278
1279 if (atoi
1280 (call_program_and_get_last_line_of_output
1281 ("/sbin/sysctl -n kern.osreldate")) < 500000) {
1282 mr_asprintf(&command, "vnconfig -d %s", dname);
1283 } else {
1284 mr_asprintf(&command, "mdconfig -d -u %s", dname);
1285 }
1286 ret = system(command);
1287 mr_free(command);
1288 return(ret);
1289}
1290#endif
1291
1292
1293/**
1294 * Ask the user for CD number @p cd_number_i_want.
1295 * Sets g_current_media_number once the correct CD is inserted.
1296 * @param bkpinfo The backup information structure. Fields used:
1297 * - @c bkpinfo->backup_media_type
1298 * - @c bkpinfo->prefix
1299 * - @c bkpinfo->isodir
1300 * - @c bkpinfo->media_device
1301 * - @c bkpinfo->please_dont_eject_when_restoring
1302 * @param cd_number_i_want The CD number to ask for.
1303 */
1304void
1305insist_on_this_cd_number(struct s_bkpinfo *bkpinfo, int cd_number_i_want)
1306{
1307
1308 /*@ int ************************************************************* */
1309 int res = 0;
1310
1311
1312 /*@ buffers ********************************************************* */
1313 char *tmp = NULL;
1314 char *request = NULL;
1315
1316 assert(bkpinfo != NULL);
1317 assert(cd_number_i_want > 0);
1318
1319// mr_msg(3, "Insisting on CD number %d", cd_number_i_want);
1320
1321 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1322 mr_msg(3,
1323 "No need to insist_on_this_cd_number when the backup type isn't CD-R(W) or NFS or ISO");
1324 return;
1325 }
1326 mr_asprintf(&tmp, "mkdir -p " MNT_CDROM);
1327 run_program_and_log_output(tmp, 5);
1328 mr_free(tmp);
1329
1330 if (g_ISO_restore_mode || bkpinfo->backup_media_type == iso
1331 || bkpinfo->backup_media_type == nfs) {
1332 mr_msg(3, "Remounting CD");
1333 g_ISO_restore_mode = TRUE;
1334// FIXME --- I'm tempted to do something about this...
1335// Why unmount and remount again and again?
1336 if (is_this_device_mounted(MNT_CDROM)) {
1337 run_program_and_log_output("umount " MNT_CDROM, 5);
1338 }
1339 system("mkdir -p /tmp/isodir &> /dev/null");
1340 mr_asprintf(&tmp, "%s/%s/%s-%d.iso", bkpinfo->isodir,
1341 bkpinfo->nfs_remote_dir, bkpinfo->prefix,
1342 cd_number_i_want);
1343 if (!does_file_exist(tmp)) {
1344 mr_free(tmp);
1345 mr_asprintf(&tmp, "/tmp/isodir/%s/%s-%d.iso",
1346 bkpinfo->nfs_remote_dir, bkpinfo->prefix,
1347 cd_number_i_want);
1348 if (does_file_exist(tmp)) {
1349 mr_msg(1,
1350 "FIXME - hacking bkpinfo->isodir from '%s' to /tmp/isodir",
1351 bkpinfo->isodir);
1352 mr_allocstr(bkpinfo->isodir, "/tmp/isodir");
1353 }
1354 }
1355 mr_msg(3, "Mounting %s at %s", tmp, MNT_CDROM);
1356 if (! mount_CDROM_here(tmp, MNT_CDROM)) {
1357 fatal_error("Mommy!");
1358 }
1359 mr_free(tmp);
1360 }
1361 if ((res = what_number_cd_is_this(bkpinfo)) != cd_number_i_want) {
1362 mr_msg(3, "Currently, we hold %d but we want %d", res,
1363 cd_number_i_want);
1364 mr_asprintf(&tmp, "Insisting on %s #%d",
1365 bkpinfo->backup_media_string,
1366 cd_number_i_want);
1367 mr_asprintf(&request, "Please insert %s #%d and press Enter.",
1368 bkpinfo->backup_media_string,
1369 cd_number_i_want);
1370 mr_msg(3, tmp);
1371 mr_free(tmp);
1372
1373 while (what_number_cd_is_this(bkpinfo) != cd_number_i_want) {
1374 sync();
1375 if (is_this_device_mounted(MNT_CDROM)) {
1376 res =
1377 run_program_and_log_output("umount " MNT_CDROM, FALSE);
1378 } else {
1379 res = 0;
1380 }
1381 if (res) {
1382 log_to_screen(_("WARNING - failed to unmount CD-ROM drive"));
1383 }
1384 if (!bkpinfo->please_dont_eject) {
1385 res = eject_device(bkpinfo->media_device);
1386 } else {
1387 res = 0;
1388 }
1389 if (res) {
1390 log_to_screen(_("WARNING - failed to eject CD-ROM disk"));
1391 }
1392 popup_and_OK(request);
1393 if (!bkpinfo->please_dont_eject) {
1394 inject_device(bkpinfo->media_device);
1395 }
1396 sync();
1397 }
1398 mr_free(request);
1399
1400 mr_msg(1, "Thankyou. Proceeding...");
1401 g_current_media_number = cd_number_i_want;
1402 }
1403}
1404/* @} - end of deviceGroup */
1405
1406
1407/**
1408 * Ask user for details of backup/restore information.
1409 * Called when @c mondoarchive doesn't get any parameters.
1410 * @param bkpinfo The backup information structure to fill out with the user's data.
1411 * @param archiving_to_media TRUE if archiving, FALSE if restoring.
1412 * @return 0, always.
1413 * @bug No point of `int' return value.
1414 * @ingroup archiveGroup
1415 */
1416int interactively_obtain_media_parameters_from_user(struct s_bkpinfo
1417 *bkpinfo,
1418 bool
1419 archiving_to_media)
1420// archiving_to_media is TRUE if I'm being called by mondoarchive
1421// archiving_to_media is FALSE if I'm being called by mondorestore
1422{
1423 char *tmp = NULL;
1424 char *tmp1 = NULL;
1425 char *tmp2 = NULL;
1426 char *sz_size = NULL;
1427 char *command = NULL;
1428 char *comment = NULL;
1429 char *prompt = NULL;
1430 int i = 0;
1431 FILE *fin = NULL;
1432
1433 assert(bkpinfo != NULL);
1434 bkpinfo->nonbootable_backup = FALSE;
1435
1436// Tape, CD, NFS, ...?
1437 srandom(getpid());
1438 bkpinfo->backup_media_type =
1439 (g_restoring_live_from_cd) ? cdr :
1440 which_backup_media_type(bkpinfo->restore_data);
1441 if (bkpinfo->backup_media_type == none) {
1442 log_to_screen(_("User has chosen not to backup the PC"));
1443 finish(1);
1444 }
1445 if (bkpinfo->backup_media_type == tape && bkpinfo->restore_data) {
1446 popup_and_OK(_("Please remove CD/floppy from drive(s)"));
1447 }
1448 bkpinfo->backup_media_string = bkptype_to_string(bkpinfo->backup_media_type);
1449 mr_msg(3, "media type = %s",bkpinfo->backup_media_string);
1450
1451 if (archiving_to_media) {
1452 sensibly_set_tmpdir_and_scratchdir(bkpinfo);
1453 }
1454 bkpinfo->cdrw_speed = (bkpinfo->backup_media_type == cdstream) ? 2 : 4;
1455 bkpinfo->compression_level =
1456 (bkpinfo->backup_media_type == cdstream) ? 1 : 5;
1457 bkpinfo->use_lzo =
1458 (bkpinfo->backup_media_type == cdstream) ? TRUE : FALSE;
1459 mvaddstr_and_log_it(2, 0, " ");
1460
1461// Find device's /dev (or SCSI) entry
1462 switch (bkpinfo->backup_media_type) {
1463 case cdr:
1464 case cdrw:
1465 case dvd:
1466 case usb:
1467 if (archiving_to_media) {
1468 if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) {
1469 if (ask_me_yes_or_no
1470 (_("Is your computer a laptop, or does the CD writer incorporate BurnProof technology?")))
1471 {
1472 bkpinfo->manual_cd_tray = TRUE;
1473 }
1474 }
1475 if ((bkpinfo->compression_level =
1476 which_compression_level()) == -1) {
1477 log_to_screen(_("User has chosen not to backup the PC"));
1478 finish(1);
1479 }
1480 mr_asprintf(&comment, _("What speed is your %s (re)writer?"),
1481 bkpinfo->backup_media_string);
1482 if (bkpinfo->backup_media_type == dvd) {
1483 mr_free(bkpinfo->media_device);
1484 bkpinfo->media_device = find_dvd_device();
1485 mr_asprintf(&tmp, "1");
1486 mr_asprintf(&sz_size, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4582 MB
1487 mr_msg(1, "Setting to DVD defaults");
1488 } else if (bkpinfo->backup_media_type == usb) {
1489 strcpy(bkpinfo->media_device, VANILLA_USB_DEVICE);
1490 strcpy(sz_size, "512");
1491 } else {
1492 mr_allocstr(bkpinfo->media_device,VANILLA_SCSI_CDROM );
1493 mr_asprintf(&tmp, "4");
1494 mr_asprintf(&sz_size, "650");
1495 mr_msg(1, "Setting to CD defaults");
1496 }
1497 if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) {
1498 if (!popup_and_get_string(_("Speed"), comment, tmp)) {
1499 log_to_screen(_("User has chosen not to backup the PC"));
1500 finish(1);
1501 }
1502 }
1503 mr_free(comment);
1504
1505 bkpinfo->cdrw_speed = atoi(tmp); // if DVD then this shouldn't ever be used anyway :)
1506 mr_free(tmp);
1507
1508 mr_asprintf(&comment,
1509 _("How much data (in Megabytes) will each %s store?"),
1510 bkpinfo->backup_media_string);
1511
1512 if (!popup_and_get_string("Size", comment, sz_size)) {
1513 log_to_screen(_("User has chosen not to backup the PC"));
1514 finish(1);
1515 }
1516 mr_free(comment);
1517
1518 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1519 bkpinfo->media_size[i] = atoi(sz_size);
1520 }
1521 mr_free(sz_size);
1522
1523 if (bkpinfo->media_size[0] <= 0) {
1524 log_to_screen(_("User has chosen not to backup the PC"));
1525 finish(1);
1526 }
1527 }
1528 case cdstream:
1529 if (bkpinfo->disaster_recovery) {
1530 mr_allocstr(bkpinfo->media_device, "/dev/cdrom");
1531 mr_msg(2, "CD-ROM device assumed to be at %s",
1532 bkpinfo->media_device);
1533 } else if (bkpinfo->restore_data
1534 || bkpinfo->backup_media_type == dvd) {
1535 if (bkpinfo->media_device == NULL) {
1536 mr_asprintf(&bkpinfo->media_device, "/dev/cdrom");
1537 } // just for the heck of it :)
1538 mr_msg(1, "bkpinfo->media_device = %s",
1539 bkpinfo->media_device);
1540 tmp = find_cdrom_device(FALSE);
1541 if ((bkpinfo->backup_media_type == dvd)
1542 || (tmp != NULL)) {
1543 mr_allocstr(bkpinfo->media_device, tmp);
1544 mr_msg(1, "bkpinfo->media_device = %s",
1545 bkpinfo->media_device);
1546 mr_asprintf(&comment,
1547 _("Please specify your %s drive's /dev entry"),
1548 bkpinfo->backup_media_string);
1549 if (!popup_and_get_string
1550 (_("Device?"), comment, bkpinfo->media_device)) {
1551 log_to_screen(_("User has chosen not to backup the PC"));
1552 finish(1);
1553 }
1554 mr_free(comment);
1555 }
1556 mr_free(tmp);
1557
1558 mr_msg(2, "%s device found at %s",
1559 bkpinfo->backup_media_string,
1560 bkpinfo->media_device);
1561 } else {
1562 bkpinfo->media_device = find_cdrw_device();
1563 if (bkpinfo->media_device != NULL) {
1564 mr_asprintf(&tmp,
1565 _("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."),
1566 bkpinfo->backup_media_string,
1567 bkpinfo->media_device);
1568 if (!ask_me_yes_or_no(tmp)) {
1569 mr_free(bkpinfo->media_device);
1570 }
1571 mr_free(tmp);
1572 } else {
1573 if (g_kernel_version < 2.6) {
1574 i = popup_and_get_string(_("Device node?"),
1575 _("What is the SCSI node of your CD (re)writer, please?"),
1576 bkpinfo->media_device);
1577 } else {
1578 i = popup_and_get_string(_("/dev entry?"),
1579 _("What is the /dev entry of your CD (re)writer, please?"),
1580 bkpinfo->media_device);
1581 }
1582 if (!i) {
1583 log_to_screen(_("User has chosen not to backup the PC"));
1584 finish(1);
1585 }
1586 }
1587 }
1588 if (bkpinfo->backup_media_type == cdstream) {
1589 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1590 bkpinfo->media_size[i] = 650;
1591 }
1592 }
1593 break;
1594 case udev:
1595 if (!ask_me_yes_or_no
1596 (_("This option is for advanced users only. Are you sure?"))) {
1597 log_to_screen(_("User has chosen not to backup the PC"));
1598 finish(1);
1599 }
1600 case tape:
1601
1602 mr_free(bkpinfo->media_device);
1603 if (find_tape_device_and_size(bkpinfo->media_device, sz_size)) {
1604 mr_msg(3, _("Ok, using vanilla scsi tape."));
1605 mr_allocstr(bkpinfo->media_device,VANILLA_SCSI_TAPE"0" );
1606 if ((fin = fopen(bkpinfo->media_device, "r"))) {
1607 paranoid_fclose(fin);
1608 } else {
1609 mr_allocstr(bkpinfo->media_device,"/dev/osst0");
1610 }
1611 }
1612 if (bkpinfo->media_device != NULL) {
1613 if ((fin = fopen(bkpinfo->media_device, "r"))) {
1614 paranoid_fclose(fin);
1615 } else {
1616 if (does_file_exist("/tmp/mondo-restore.cfg")) {
1617 mr_free(bkpinfo->media_device);
1618 read_cfg_var("/tmp/mondo-restore.cfg", "media-dev",
1619 bkpinfo->media_device);
1620 }
1621 }
1622 mr_asprintf(&tmp,
1623 _("I think I've found your tape streamer at %s; am I right on the money?"),
1624 bkpinfo->media_device);
1625 if (!ask_me_yes_or_no(tmp)) {
1626 mr_free(bkpinfo->media_device);
1627 }
1628 mr_free(tmp);
1629 } else {
1630 if (!popup_and_get_string
1631 (_("Device name?"),
1632 _("What is the /dev entry of your tape streamer?"),
1633 bkpinfo->media_device)) {
1634 log_to_screen(_("User has chosen not to backup the PC"));
1635 finish(1);
1636 }
1637 }
1638 mr_asprintf(&tmp, "ls -l %s", bkpinfo->media_device);
1639 if (run_program_and_log_output(tmp, FALSE)) {
1640 log_to_screen(_("User has not specified a valid /dev entry"));
1641 finish(1);
1642 }
1643 mr_free(tmp);
1644 mr_msg(4, "sz_size = %s", sz_size);
1645 mr_free(sz_size);
1646 bkpinfo->media_size[0] = 0;
1647 mr_msg(4, "media_size[0] = %ld", bkpinfo->media_size[0]);
1648 if (bkpinfo->media_size[0] <= 0) {
1649 bkpinfo->media_size[0] = 0;
1650 }
1651 for (i = 1; i <= MAX_NOOF_MEDIA; i++) {
1652 bkpinfo->media_size[i] = bkpinfo->media_size[0];
1653 }
1654 if (archiving_to_media) {
1655 if ((bkpinfo->compression_level =
1656 which_compression_level()) == -1) {
1657 log_to_screen(_("User has chosen not to backup the PC"));
1658 finish(1);
1659 }
1660 }
1661 break;
1662
1663
1664
1665 case nfs:
1666 if (bkpinfo->nfs_mount == NULL) {
1667 bkpinfo->nfs_mount = call_program_and_get_last_line_of_output
1668 ("mount | grep \":\" | cut -d' ' -f1 | head -n1");
1669 }
1670#ifdef __FreeBSD__
1671 if (TRUE)
1672#else
1673 if (!bkpinfo->disaster_recovery)
1674#endif
1675 {
1676 if (!popup_and_get_string
1677 (_("NFS dir."),
1678 _("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.)"),
1679 bkpinfo->nfs_mount)) {
1680 log_to_screen(_("User has chosen not to backup the PC"));
1681 finish(1);
1682 }
1683 if (!bkpinfo->restore_data) {
1684 if ((bkpinfo->compression_level =
1685 which_compression_level()) == -1) {
1686 log_to_screen(_("User has chosen not to backup the PC"));
1687 finish(1);
1688 }
1689 }
1690 // check whether already mounted - we better remove
1691 // surrounding spaces and trailing '/' for this
1692 mr_strip_spaces(bkpinfo->nfs_mount);
1693 if (bkpinfo->nfs_mount[strlen(bkpinfo->nfs_mount) - 1] == '/')
1694 bkpinfo->nfs_mount[strlen(bkpinfo->nfs_mount) - 1] = '\0';
1695 mr_asprintf(&command, "mount | grep \"%s \" | cut -d' ' -f3",
1696 bkpinfo->nfs_mount);
1697 mr_free(bkpinfo->isodir);
1698 bkpinfo->isodir = call_program_and_get_last_line_of_output(command);
1699 mr_free(command);
1700
1701 mr_asprintf(&comment,
1702 _("How much data (in Megabytes) will each media store?"));
1703 if (!popup_and_get_string(_("Size"), comment, sz_size, 5)) {
1704 log_to_screen(_("User has chosen not to backup the PC"));
1705 finish(1);
1706 }
1707 mr_free(comment);
1708 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1709 bkpinfo->media_size[i] = atoi(sz_size);
1710 }
1711 if (bkpinfo->media_size[0] <= 0) {
1712 log_to_screen(_("User has chosen not to backup the PC"));
1713 finish(1);
1714 }
1715 }
1716 if (bkpinfo->disaster_recovery) {
1717 system("umount /tmp/isodir 2> /dev/null");
1718 if (!popup_and_get_string
1719 (_("NFS share"), _("Which remote NFS share should I mount?"),
1720 bkpinfo->nfs_mount)) {
1721 log_to_screen(_("User has chosen not to backup the PC"));
1722 finish(1);
1723 }
1724 }
1725 if (!is_this_device_mounted(bkpinfo->nfs_mount)) {
1726 mr_free(bkpinfo->isodir);
1727 mr_asprintf(&bkpinfo->isodir, "/tmp/isodir.mondo.%d",
1728 (int) (random() % 32768));
1729 mr_asprintf(&command, "mkdir -p %s", bkpinfo->isodir);
1730 run_program_and_log_output(command, 5);
1731 mr_free(command);
1732
1733 mr_asprintf(&tmp, "mount -t nfs -o nolock %s %s", bkpinfo->nfs_mount,
1734 bkpinfo->isodir);
1735 run_program_and_log_output(tmp, 5);
1736 mr_free(tmp);
1737 mr_asprintf(&g_selfmounted_isodir, bkpinfo->isodir);
1738 }
1739 if (!is_this_device_mounted(bkpinfo->nfs_mount)) {
1740 popup_and_OK
1741 (_("Please mount that partition before you try to backup to or restore from it."));
1742 finish(1);
1743 }
1744 if (!popup_and_get_string
1745 (_("Directory"), _("Which directory within that mountpoint?"),bkpinfo->nfs_remote_dir)) {
1746 log_to_screen(_("User has chosen not to backup the PC"));
1747 finish(1);
1748 }
1749
1750 mr_asprintf(&command, "echo hi > '%s/%s/.dummy.txt'", bkpinfo->isodir,
1751 bkpinfo->nfs_remote_dir);
1752 while (run_program_and_log_output(command, FALSE)) {
1753 mr_asprintf(&prompt,
1754 _("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."),
1755 bkpinfo->nfs_remote_dir, bkpinfo->isodir);
1756 if (!popup_and_get_string
1757 (_("Directory"), prompt, bkpinfo->nfs_remote_dir)) {
1758 log_to_screen(_("User has chosen not to backup the PC"));
1759 finish(1);
1760 }
1761 mr_free(prompt);
1762
1763 mr_free(command);
1764 mr_asprintf(&command, "echo hi > '%s/%s/.dummy.txt'", bkpinfo->isodir,
1765 bkpinfo->nfs_remote_dir);
1766 }
1767 mr_free(command);
1768
1769 if (!popup_and_get_string
1770 (_("Prefix."),
1771 _("Please enter the prefix that will be prepended to your ISO filename. Example: machine1 to obtain machine1-[1-9]*.iso files"),
1772 bkpinfo->prefix)) {
1773 log_to_screen(_("User has chosen not to backup the PC"));
1774 finish(1);
1775 }
1776 mr_msg(3, "prefix set to %s", bkpinfo->prefix);
1777
1778 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1779 bkpinfo->media_size[i] = 650;
1780 }
1781 mr_msg(3, "Just set nfs_remote_dir to %s",
1782 bkpinfo->nfs_remote_dir);
1783 mr_msg(3, "isodir is still %s", bkpinfo->isodir);
1784 break;
1785
1786 case iso:
1787 if (!bkpinfo->disaster_recovery) {
1788 if (!popup_and_get_string
1789 (_("Storage dir."),
1790 _("Please enter the full path that contains your ISO images. Example: /mnt/raid0_0"),
1791 bkpinfo->isodir)) {
1792 log_to_screen(_("User has chosen not to backup the PC"));
1793 finish(1);
1794 }
1795 if (archiving_to_media) {
1796 if ((bkpinfo->compression_level =
1797 which_compression_level()) == -1) {
1798 log_to_screen(_("User has chosen not to backup the PC"));
1799 finish(1);
1800 }
1801 if (!popup_and_get_string
1802 (_("ISO size."),
1803 _("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)) {
1804 log_to_screen(_("User has chosen not to backup the PC"));
1805 finish(1);
1806 }
1807 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1808 bkpinfo->media_size[i] = atoi(sz_size);
1809 }
1810 } else {
1811 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1812 bkpinfo->media_size[i] = 650;
1813 }
1814 }
1815 }
1816 if (!popup_and_get_string
1817 (_("Prefix."),
1818 _("Please enter the prefix that will be prepended to your ISO filename. Example: machine1 to obtain machine1-[1-9]*.iso files"),
1819 bkpinfo->prefix)) {
1820 log_to_screen("User has chosen not to backup the PC");
1821 finish(1);
1822 }
1823 mr_msg(3, "prefix set to %s", bkpinfo->prefix);
1824 break;
1825 default:
1826 fatal_error
1827 ("I, Mojo Jojo, shall defeat those pesky Powerpuff Girls!");
1828 }
1829 if (archiving_to_media) {
1830
1831 if (bkpinfo->boot_device) {
1832 mr_free(bkpinfo->boot_device);
1833 }
1834#ifdef __FreeBSD__
1835 bkpinfo->boot_device = call_program_and_get_last_line_of_output
1836 ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/\\([0-9]\\).*/\\1/'");
1837#else
1838 bkpinfo->boot_device = call_program_and_get_last_line_of_output
1839 ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'");
1840#endif
1841 i = which_boot_loader(bkpinfo->boot_device);
1842 if (i == 'U') // unknown
1843 {
1844
1845#ifdef __FreeBSD__
1846 if (!popup_and_get_string
1847 (_("Boot device"),
1848 _("What is your boot device? (e.g. /dev/ad0)"),
1849 bkpinfo->boot_device)) {
1850 log_to_screen(_("User has chosen not to backup the PC"));
1851 finish(1);
1852 }
1853 i = which_boot_loader(bkpinfo->boot_device);
1854#else
1855 if (!popup_and_get_string
1856 (_("Boot device"),
1857 _("What is your boot device? (e.g. /dev/hda)"),
1858 bkpinfo->boot_device)) {
1859 log_to_screen(_("User has chosen not to backup the PC"));
1860 finish(1);
1861 }
1862 if (does_string_exist_in_boot_block
1863 (bkpinfo->boot_device, "LILO")) {
1864 i = 'L';
1865 } else
1866 if (does_string_exist_in_boot_block
1867 (bkpinfo->boot_device, "ELILO")) {
1868 i = 'E';
1869 } else
1870 if (does_string_exist_in_boot_block
1871 (bkpinfo->boot_device, "GRUB")) {
1872 i = 'G';
1873 } else {
1874 i = 'U';
1875 }
1876#endif
1877 if (i == 'U') {
1878 if (ask_me_yes_or_no
1879 (_("Unidentified boot loader. Shall I restore it byte-for-byte at restore time and hope for the best?")))
1880 {
1881 i = 'R'; // raw
1882 } else {
1883 log_to_screen
1884 (_("I cannot find your boot loader. Please run mondoarchive with parameters."));
1885 finish(1);
1886 }
1887 }
1888 }
1889 bkpinfo->boot_loader = i;
1890 mr_asprintf(&bkpinfo->include_paths, "/");
1891 if (!popup_and_get_string
1892 (_("Backup paths"),
1893 _("Please enter paths which you want me to backup. The default is '/' (i.e. everything)."),
1894 bkpinfo->include_paths)) {
1895 log_to_screen(_("User has chosen not to backup the PC"));
1896 finish(1);
1897 }
1898
1899 tmp = list_of_NFS_mounts_only();
1900 if (strlen(tmp) > 2) {
1901 if (bkpinfo->exclude_paths != NULL) {
1902 tmp1 = bkpinfo->exclude_paths;
1903 mr_asprintf(&bkpinfo->exclude_paths, "%s %s", tmp1, tmp);
1904 mr_free(tmp1);
1905 } else {
1906 bkpinfo->exclude_paths = tmp;
1907 }
1908 }
1909 mr_free(tmp);
1910// NTFS
1911 tmp = call_program_and_get_last_line_of_output
1912 ("parted2fdisk -l | grep -i ntfs | awk '{ print $1};' | tr -s '\\n' ' ' | awk '{ print $0};'");
1913 if (strlen(tmp) > 2) {
1914 if (!popup_and_get_string
1915 (_("NTFS partitions"),
1916 _("Please enter/confirm the NTFS partitions you wish to backup as well."),
1917 tmp1)) {
1918 log_to_screen(_("User has chosen not to backup the PC"));
1919 finish(1);
1920 }
1921 if (bkpinfo->image_devs != NULL) {
1922 tmp2 = bkpinfo->image_devs;
1923 mr_asprintf(&bkpinfo->image_devs, "%s %s", tmp2, tmp1);
1924 mr_free(tmp2);
1925 } else {
1926 bkpinfo->image_devs = tmp1;
1927 }
1928 mr_free(tmp1);
1929 }
1930 mr_free(tmp);
1931
1932 if (!popup_and_get_string
1933 (_("Exclude paths"),
1934 _("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."),
1935 bkpinfo->exclude_paths)) {
1936 log_to_screen(_("User has chosen not to backup the PC"));
1937 finish(1);
1938 }
1939 bkpinfo->make_cd_use_lilo = FALSE;
1940 bkpinfo->backup_data = TRUE;
1941 bkpinfo->verify_data =
1942 ask_me_yes_or_no
1943 (_("Will you want to verify your backups after Mondo has created them?"));
1944
1945#ifndef __FreeBSD__
1946 if (!ask_me_yes_or_no
1947 (_("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."))){
1948 mr_allocstr(bkpinfo->kernel_path, "FAILSAFE");
1949 }
1950#endif
1951
1952 if (!ask_me_yes_or_no
1953 (_("Are you sure you want to proceed? Hit 'no' to abort."))) {
1954 log_to_screen(_("User has chosen not to backup the PC"));
1955 finish(1);
1956 }
1957 } else {
1958 bkpinfo->restore_data = TRUE; // probably...
1959 }
1960
1961 if (bkpinfo->backup_media_type == iso
1962 || bkpinfo->backup_media_type == nfs) {
1963 g_ISO_restore_mode = TRUE;
1964 }
1965#ifdef __FreeSD__
1966// skip
1967#else
1968 if (bkpinfo->backup_media_type == nfs) {
1969 mr_msg(3, "I think the NFS mount is mounted at %s",
1970 bkpinfo->isodir);
1971 }
1972 log_it("isodir = %s", bkpinfo->isodir);
1973 log_it("nfs_mount = '%s'", bkpinfo->nfs_mount);
1974#endif
1975
1976 log_it("media device = %s", bkpinfo->media_device);
1977 log_it("media size = %ld", bkpinfo->media_size[1]);
1978 log_it("media type = %s", bkpinfo->backup_media_string);
1979 log_it("prefix = %s", bkpinfo->prefix);
1980 log_it("compression = %ld", bkpinfo->compression_level);
1981 log_it("include_paths = '%s'", bkpinfo->include_paths);
1982 log_it("exclude_paths = '%s'", bkpinfo->exclude_paths);
1983 log_it("scratchdir = '%s'", bkpinfo->scratchdir);
1984 log_it("tmpdir = '%s'", bkpinfo->tmpdir);
1985 log_it("boot_device = '%s' (loader=%c)", bkpinfo->boot_device,
1986 bkpinfo->boot_loader);
1987 if (bkpinfo->media_size[0] < 0) {
1988 if (archiving_to_media) {
1989 fatal_error("Media size is less than zero.");
1990 } else {
1991 mr_msg(2, "Warning - media size is less than zero.");
1992 bkpinfo->media_size[0] = 0;
1993 }
1994 }
1995 return (0);
1996}
1997
1998
1999/**
2000 * Get a space-separated list of NFS mounts.
2001 * @return The list created.
2002 * @note The return value should be freed by caller
2003 * @bug Even though we only want the mounts, the devices are still checked.
2004 */
2005char *list_of_NFS_mounts_only(void)
2006{
2007 return(call_program_and_get_last_line_of_output
2008 ("mount -t coda,ncpfs,nfs,smbfs,cifs,afs,mvfs | tr -s '\t' ' ' | cut -d' ' -f3 | tr -s '\n' ' ' | awk '{print $0;}'"));
2009 /* BERLIOS : Useless
2010 mr_asprintf(&exclude_these_devices,
2011 call_program_and_get_last_line_of_output
2012 ("tr -s '\t' ' ' < /etc/fstab | grep -E '( (coda|ncpfs|nfs|smbfs|cifs|mvfs) )' | cut -d' ' -f1 | tr -s '\n' ' ' | awk '{print $0;}'"));
2013 */
2014}
2015/* @} - end of utilityGroup */
2016
2017
2018/**
2019 * Set the tmpdir and scratchdir to reside on the partition with the most free space.
2020 * Automatically excludes DOS, NTFS, SMB, and NFS filesystems.
2021 * @param bkpinfo The backup information structure. @c bkpinfo->tmpdir and @c bkpinfo->scratchdir will be set.
2022 * @ingroup utilityGroup
2023 */
2024void sensibly_set_tmpdir_and_scratchdir(struct s_bkpinfo *bkpinfo)
2025{
2026 char *tmp = NULL;
2027 char *command = NULL;
2028 char *sz = NULL;
2029 int i = 0;
2030
2031 assert(bkpinfo != NULL);
2032
2033#ifdef __FreeBSD__
2034 tmp = call_program_and_get_last_line_of_output
2035 ("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;}'");
2036#else
2037 tmp = call_program_and_get_last_line_of_output
2038 ("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;}'");
2039#endif
2040
2041 if (tmp[0] != '/') {
2042 mr_asprintf(&sz, "/%s", tmp);
2043 mr_free(tmp);
2044 tmp = sz;
2045 }
2046 if (!tmp[0]) {
2047 fatal_error("I couldn't figure out the tempdir!");
2048 }
2049 i = (int) (random() % 32768);
2050 mr_free(bkpinfo->tmpdir);
2051 mr_asprintf(&bkpinfo->tmpdir, "%s/mondo.tmp.%d", tmp, i);
2052 log_it("bkpinfo->tmpdir is being set to %s", bkpinfo->tmpdir);
2053
2054 mr_free(bkpinfo->scratchdir);
2055 mr_asprintf(&bkpinfo->scratchdir, "%s/mondo.scratch.%d", tmp, i);
2056 log_it("bkpinfo->scratchdir is being set to %s", bkpinfo->scratchdir);
2057
2058 mr_asprintf(&command, "rm -Rf %s/mondo.tmp.* %s/mondo.scratch.*", tmp, tmp);
2059 mr_free(tmp);
2060
2061 paranoid_system(command);
2062 mr_free(command);
2063}
2064
2065
2066/**
2067 * @addtogroup deviceGroup
2068 * @{
2069 */
2070/**
2071 * If we can read @p dev, set @p output to it.
2072 * If @p dev cannot be read, set @p output to "".
2073 * @param dev The device to check for.
2074 * @param output Set to @p dev if @p dev exists, NULL otherwise. Needs to be freed by caller
2075 * @return TRUE if @p dev exists, FALSE if it doesn't.
2076 */
2077bool set_dev_to_this_if_rx_OK(char *output, char *dev)
2078{
2079 char *command = NULL;
2080
2081 mr_free(output);
2082 if (!dev || dev[0] == '\0') {
2083 return (FALSE);
2084 }
2085 mr_msg(10, "Injecting %s", dev);
2086 inject_device(dev);
2087 if (!does_file_exist(dev)) {
2088 mr_msg(10, "%s doesn't exist. Returning FALSE.", dev);
2089 return (FALSE);
2090 }
2091 mr_asprintf(&command, "dd bs=%ld count=1 if=%s of=/dev/null &> /dev/null",
2092 512L, dev);
2093 if (!run_program_and_log_output(command, FALSE)
2094 && !run_program_and_log_output(command, FALSE)) {
2095 mr_asprintf(&output, dev);
2096 mr_msg(4, "Found it - %s", dev);
2097 mr_free(command);
2098 return (TRUE);
2099 } else {
2100 mr_msg(4, "It's not %s", dev);
2101 mr_free(command);
2102 return (FALSE);
2103 }
2104}
2105
2106
2107/**
2108 * Find out what number CD is in the drive.
2109 * @param bkpinfo The backup information structure. The @c bkpinfo->media_device field is the only one used.
2110 * @return The current CD number, or -1 if it could not be found.
2111 * @note If the CD is not mounted, it will be mounted
2112 * (and remain mounted after this function returns).
2113 */
2114int what_number_cd_is_this(struct s_bkpinfo *bkpinfo)
2115{
2116 int cd_number = -1;
2117 char *mountdev = NULL;
2118 char *tmp = NULL;
2119 char *tmp1 = NULL;
2120
2121 assert(bkpinfo != NULL);
2122 if (g_ISO_restore_mode) {
2123 mr_asprintf(&tmp, "mount | grep iso9660 | awk '{print $3;}'");
2124
2125 tmp1 = call_program_and_get_last_line_of_output(tmp);
2126 mr_free(tmp);
2127
2128 mr_asprintf(&mountdev, "%s/archives/THIS-CD-NUMBER", tmp1);
2129 mr_free(tmp1);
2130
2131 tmp = last_line_of_file(mountdev);
2132 cd_number = atoi(tmp);
2133 mr_free(mountdev);
2134 mr_free(tmp);
2135 return (cd_number);
2136 }
2137
2138 if (bkpinfo->media_device == NULL) {
2139 log_it
2140 ("(what_number_cd_is_this) Warning - media_device unknown. Finding out...");
2141 bkpinfo->media_device = find_cdrom_device(FALSE);
2142 }
2143 if (!is_this_device_mounted(MNT_CDROM)) {
2144 (void)mount_CDROM_here(bkpinfo->media_device, MNT_CDROM);
2145 }
2146 tmp = last_line_of_file(MNT_CDROM "/archives/THIS-CD-NUMBER");
2147 cd_number = atoi(tmp);
2148 mr_free(tmp);
2149 return (cd_number);
2150}
2151
2152
2153/**
2154 * Find out what device is mounted as root (/).
2155 * @return Root device.
2156 * @note The returned string points to static storage and will be overwritten with every call.
2157 * @bug A bit of a misnomer; it's actually finding out the root device.
2158 * The mountpoint (where it's mounted) will obviously be '/'.
2159 */
2160char *where_is_root_mounted()
2161{
2162 char *tmp;
2163
2164#ifdef __FreeBSD__
2165 tmp = call_program_and_get_last_line_of_output
2166 ("mount | grep \" on / \" | cut -d' ' -f1");
2167#else
2168 tmp = call_program_and_get_last_line_of_output
2169 ("mount | grep \" on / \" | cut -d' ' -f1 | sed s/[0-9]// | sed s/[0-9]//");
2170 if (strstr(tmp, "/dev/cciss/")) {
2171 mr_free(tmp);
2172 tmp = call_program_and_get_last_line_of_output
2173 ("mount | grep \" on / \" | cut -d' ' -f1 | cut -dp -f1");
2174 }
2175 if (strstr(tmp, "/dev/md")) {
2176 mr_free(tmp);
2177 tmp = call_program_and_get_last_line_of_output
2178 ("mount | grep \" on / \" | cut -d' ' -f1");
2179 }
2180#endif
2181
2182 return (tmp);
2183}
2184
2185
2186/**
2187 * Find out which boot loader is in use.
2188 * @param which_device Device to look for the boot loader on.
2189 * @return 'L' for LILO, 'E'for ELILO, 'G' for GRUB, 'B' or 'D' for FreeBSD boot loaders, or 'U' for Unknown.
2190 * @note Under Linux, all drives are examined, not just @p which_device.
2191 */
2192char which_boot_loader(char *which_device)
2193{
2194#ifdef __FreeBSD__
2195 int count_lilos = 0;
2196 int count_grubs = 0;
2197 int count_boot0s = 0;
2198 int count_dangerouslydedicated = 0;
2199
2200 log_it("looking at drive %s's MBR", which_device);
2201 if (does_string_exist_in_boot_block(which_device, "GRUB")) {
2202 count_grubs++;
2203 }
2204 if (does_string_exist_in_boot_block(which_device, "LILO")) {
2205 count_lilos++;
2206 }
2207 if (does_string_exist_in_boot_block(which_device, "Drive")) {
2208 count_boot0s++;
2209 }
2210 if (does_string_exist_in_first_N_blocks
2211 (which_device, "FreeBSD/i386", 17)) {
2212 count_dangerouslydedicated++;
2213 }
2214 log_it("%d grubs and %d lilos and %d elilos and %d boot0s and %d DD\n",
2215 count_grubs, count_lilos, count_elilos, count_boot0s,
2216 count_dangerouslydedicated);
2217
2218 if (count_grubs && !count_lilos) {
2219 return ('G');
2220 } else if (count_lilos && !count_grubs) {
2221 return ('L');
2222 } else if (count_grubs == 1 && count_lilos == 1) {
2223 log_it("I'll bet you used to use LILO but switched to GRUB...");
2224 return ('G');
2225 } else if (count_boot0s == 1) {
2226 return ('B');
2227 } else if (count_dangerouslydedicated) {
2228 return ('D');
2229 } else {
2230 log_it("Unknown boot loader");
2231 return ('U');
2232 }
2233#else
2234 /*@ buffer ***************************************************** */
2235 char *list_drives_cmd;
2236 char *current_drive = NULL;
2237
2238 /*@ pointers *************************************************** */
2239 FILE *pdrives;
2240
2241 /*@ int ******************************************************** */
2242 int count_lilos = 0;
2243 int count_grubs = 0;
2244 size_t n = 0;
2245
2246 /*@ end vars *************************************************** */
2247
2248#ifdef __IA64__
2249 /* No choice for it */
2250 return ('E');
2251#endif
2252 assert(which_device != NULL);
2253 mr_asprintf(&list_drives_cmd,
2254 "parted2fdisk -l 2>/dev/null | grep \"/dev/.*:\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/; echo %s",
2255 where_is_root_mounted());
2256 log_it("list_drives_cmd = %s", list_drives_cmd);
2257
2258 if (!(pdrives = popen(list_drives_cmd, "r"))) {
2259 log_OS_error("Unable to open list of drives");
2260 mr_free(list_drives_cmd);
2261 return ('\0');
2262 }
2263 mr_free(list_drives_cmd);
2264
2265 for (mr_getline(&current_drive, &n, pdrives); !feof(pdrives);
2266 mr_getline(&current_drive, &n, pdrives)) {
2267 mr_strip_spaces(current_drive);
2268 log_it("looking at drive %s's MBR", current_drive);
2269 if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
2270 count_grubs++;
2271 mr_allocstr(which_device,current_drive);
2272 break;
2273 }
2274 if (does_string_exist_in_boot_block(current_drive, "LILO")) {
2275 count_lilos++;
2276 mr_allocstr(which_device,current_drive);
2277 break;
2278 }
2279 }
2280
2281 if (pclose(pdrives)) {
2282 log_OS_error("Cannot pclose pdrives");
2283 }
2284 log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
2285 if (count_grubs && !count_lilos) {
2286 return ('G');
2287 } else if (count_lilos && !count_grubs) {
2288 return ('L');
2289 } else if (count_grubs == 1 && count_lilos == 1) {
2290 log_it("I'll bet you used to use LILO but switched to GRUB...");
2291 return ('G');
2292 } else {
2293 log_it("Unknown boot loader");
2294 return ('U');
2295 }
2296#endif
2297}
2298
2299
2300/**
2301 * Write zeroes over the first 16K of @p device.
2302 * @param device The device to zero.
2303 * @return 0 for success, 1 for failure.
2304 */
2305int zero_out_a_device(char *device)
2306{
2307 FILE *fout;
2308 int i;
2309
2310 assert_string_is_neither_NULL_nor_zerolength(device);
2311
2312 log_it("Zeroing drive %s", device);
2313 if (!(fout = fopen(device, "w"))) {
2314 log_OS_error("Unable to open/write to device");
2315 return (1);
2316 }
2317 for (i = 0; i < 16384; i++) {
2318 fputc('\0', fout);
2319 }
2320 paranoid_fclose(fout);
2321 log_it("Device successfully zeroed.");
2322 return (0);
2323}
2324
2325
2326/**
2327 * Return the device pointed to by @p incoming.
2328 * @param incoming The device to resolve symlinks for.
2329 * @return The path to the real device file.
2330 * @note The returned string points to a storage that need to be freed by the caller
2331 * @bug Won't work with file v4.0; needs to be written in C.
2332 */
2333char *resolve_softlinks_to_get_to_actual_device_file(char *incoming)
2334{
2335 char *output = NULL;
2336 char *command = NULL;
2337 char *curr_fname = NULL;
2338 char *scratch = NULL;
2339 char *tmp = NULL;
2340 char *p = NULL;
2341
2342 struct stat statbuf;
2343 if (!does_file_exist(incoming)) {
2344 log_it
2345 ("resolve_softlinks_to_get_to_actual_device_file --- device not found");
2346 mr_asprintf(&output, incoming);
2347 } else {
2348 mr_asprintf(&curr_fname, incoming);
2349 lstat(curr_fname, &statbuf);
2350 while (S_ISLNK(statbuf.st_mode)) {
2351 mr_msg(1, "curr_fname = %s", curr_fname);
2352 mr_asprintf(&command, "file %s", curr_fname);
2353 tmp = call_program_and_get_last_line_of_output(command);
2354 mr_free(command);
2355
2356 for (p = tmp + strlen(tmp); p != tmp && *p != '`' && *p != ' ';
2357 p--);
2358 p++;
2359 mr_asprintf(&scratch, p);
2360 for (p = scratch; *p != '\0' && *p != '\''; p++);
2361 *p = '\0';
2362 mr_msg(0, "curr_fname %s --> '%s' --> %s", curr_fname, tmp, scratch);
2363 mr_free(tmp);
2364
2365 if (scratch[0] == '/') {
2366 mr_free(curr_fname);
2367 mr_asprintf(&curr_fname, scratch); // copy whole thing because it's an absolute softlink
2368 } else { // copy over the basename cos it's a relative softlink
2369 p = curr_fname + strlen(curr_fname);
2370 while (p != curr_fname && *p != '/') {
2371 p--;
2372 }
2373 if (*p == '/') {
2374 p++;
2375 }
2376 *p = '\0';
2377 mr_asprintf(&tmp, "%s%s", curr_fname, scratch);
2378 mr_free(curr_fname);
2379 curr_fname = tmp;
2380 }
2381 mr_free(scratch);
2382 lstat(curr_fname, &statbuf);
2383 }
2384 mr_asprintf(&output, curr_fname);
2385 log_it("resolved %s to %s", incoming, output);
2386 mr_free(curr_fname);
2387 }
2388 return (output);
2389}
2390
2391/* @} - end of deviceGroup */
2392
2393
2394/**
2395 * Return the type of partition format (GPT or MBR)
2396 * Caller needs to free the return value
2397 */
2398char *which_partition_format(const char *drive)
2399{
2400 char *output = NULL;
2401 char *tmp = NULL;
2402 char *command = NULL;
2403 char *fdisk = NULL;
2404
2405 mr_msg(0, "Looking for partition table format type");
2406 mr_asprintf(&fdisk, "/sbin/parted2fdisk");
2407 mr_msg(1, "Using %s", fdisk);
2408 mr_asprintf(&command, "%s -l %s | grep 'EFI GPT'", fdisk, drive);
2409 mr_free(fdisk);
2410
2411 tmp = call_program_and_get_last_line_of_output(command);
2412 mr_free(command);
2413
2414 if (strstr(tmp, "GPT") == NULL) {
2415 mr_asprintf(&output, "MBR");
2416 } else {
2417 mr_asprintf(&output, "GPT");
2418 }
2419 mr_free(tmp);
2420 mr_msg(0, "Found %s partition table format type", output);
2421 return(output);
2422}
2423/* @} - end of deviceGroup */
Note: See TracBrowser for help on using the repository browser.