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

Last change on this file since 687 was 687, checked in by bcornec, 18 years ago

merge -r671:686 $SVN_M/branches/stable

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