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

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

Huge memory management patch.
Still not finished but a lot as been done.
What remains is around some functions returning strings, and some structure members.
(Could not finish due to laptop failure !)

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