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

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

merge -r954:978 $SVN_M/branches/stable

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