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

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

merge -r489:506 $SVN_M/branches/stable

  • Property svn:keywords set to Id
File size: 66.2 KB
Line 
1/* $Id: libmondo-devices.c 507 2006-04-30 00:04:16Z 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 507 2006-04-30 00:04:16Z 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/' | egrep -qv '(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 (ask_me_yes_or_no
1468 (_("Is your computer a laptop, or does the CD writer incorporate BurnProof technology?")))
1469 {
1470 bkpinfo->manual_cd_tray = TRUE;
1471 }
1472 if ((bkpinfo->compression_level =
1473 which_compression_level()) == -1) {
1474 log_to_screen(_("User has chosen not to backup the PC"));
1475 finish(1);
1476 }
1477 asprintf(&comment, _("What speed is your %s (re)writer?"),
1478 media_descriptor_string(bkpinfo->backup_media_type));
1479 if (bkpinfo->backup_media_type == dvd) {
1480 paranoid_free(bkpinfo->media_device);
1481 bkpinfo->media_device = find_dvd_device();
1482 asprintf(&tmp, "1");
1483 asprintf(&sz_size, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4582 MB
1484 log_msg(1, "Setting to DVD defaults");
1485 } else {
1486 paranoid_alloc(bkpinfo->media_device,VANILLA_SCSI_CDROM );
1487 asprintf(&tmp, "4");
1488 asprintf(&sz_size, "650");
1489 log_msg(1, "Setting to CD defaults");
1490 }
1491 if (bkpinfo->backup_media_type != dvd) {
1492 if (!popup_and_get_string(_("Speed"), comment, tmp, 4)) {
1493 log_to_screen(_("User has chosen not to backup the PC"));
1494 finish(1);
1495 }
1496 }
1497 paranoid_free(comment);
1498
1499 bkpinfo->cdrw_speed = atoi(tmp); // if DVD then this shouldn't ever be used anyway :)
1500 paranoid_free(tmp);
1501
1502 asprintf(&comment,
1503 _("How much data (in Megabytes) will each %s store?"),
1504 media_descriptor_string(bkpinfo->backup_media_type));
1505
1506 if (!popup_and_get_string("Size", comment, sz_size, 5)) {
1507 log_to_screen(_("User has chosen not to backup the PC"));
1508 finish(1);
1509 }
1510 paranoid_free(comment);
1511
1512 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1513 bkpinfo->media_size[i] = atoi(sz_size);
1514 }
1515 if (bkpinfo->media_size[0] <= 0) {
1516 log_to_screen(_("User has chosen not to backup the PC"));
1517 finish(1);
1518 }
1519 paranoid_free(sz_size);
1520 }
1521 case cdstream:
1522 if (bkpinfo->disaster_recovery) {
1523 paranoid_alloc(bkpinfo->media_device, "/dev/cdrom");
1524 log_msg(2, "CD-ROM device assumed to be at %s",
1525 bkpinfo->media_device);
1526 } else if (bkpinfo->restore_data
1527 || bkpinfo->backup_media_type == dvd) {
1528 if (bkpinfo->media_device == NULL) {
1529 paranoid_alloc(bkpinfo->media_device, "/dev/cdrom");
1530 } // just for the heck of it :)
1531 log_msg(1, "bkpinfo->media_device = %s",
1532 bkpinfo->media_device);
1533 if ((bkpinfo->backup_media_type == dvd)
1534 || ((tmp = find_cdrom_device(FALSE)) != NULL)) {
1535 paranoid_free(bkpinfo->media_device);
1536 bkpinfo->media_device = tmp;
1537 log_msg(1, "bkpinfo->media_device = %s",
1538 bkpinfo->media_device);
1539 asprintf(&comment,
1540 _("Please specify your %s drive's /dev entry"),
1541 media_descriptor_string(bkpinfo->
1542 backup_media_type));
1543 if (!popup_and_get_string
1544 (_("Device?"), comment, bkpinfo->media_device,
1545 MAX_STR_LEN / 4)) {
1546 log_to_screen(_("User has chosen not to backup the PC"));
1547 finish(1);
1548 }
1549 paranoid_free(comment);
1550 }
1551 log_msg(2, "%s device found at %s",
1552 media_descriptor_string(bkpinfo->backup_media_type),
1553 bkpinfo->media_device);
1554 } else {
1555 paranoid_free(bkpinfo->media_device);
1556 bkpinfo->media_device = find_cdrw_device();
1557 if (bkpinfo->media_device != NULL) {
1558 asprintf(&tmp,
1559 _("I think I've found your %s burner at SCSI node %s; am I right on the money?"),
1560 media_descriptor_string(bkpinfo->
1561 backup_media_type),
1562 bkpinfo->media_device);
1563 if (!ask_me_yes_or_no(tmp)) {
1564 paranoid_free(bkpinfo->media_device);
1565 }
1566 paranoid_free(tmp);
1567 } else {
1568 if (g_kernel_version < 2.6) {
1569 i = popup_and_get_string(_("Device node?"),
1570 _("What is the SCSI node of your CD (re)writer, please?"),
1571 bkpinfo->media_device,
1572 MAX_STR_LEN / 4);
1573 } else {
1574 i = popup_and_get_string(_("/dev entry?"),
1575 _("What is the /dev entry of your CD (re)writer, please?"),
1576 bkpinfo->media_device,
1577 MAX_STR_LEN / 4);
1578 }
1579 if (!i) {
1580 log_to_screen(_("User has chosen not to backup the PC"));
1581 finish(1);
1582 }
1583 }
1584 }
1585 if (bkpinfo->backup_media_type == cdstream) {
1586 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1587 bkpinfo->media_size[i] = 650;
1588 }
1589 }
1590 break;
1591 case udev:
1592 if (!ask_me_yes_or_no
1593 (_("This option is for advanced users only. Are you sure?"))) {
1594 log_to_screen(_("User has chosen not to backup the PC"));
1595 finish(1);
1596 }
1597 case tape:
1598
1599 paranoid_free(bkpinfo->media_device);
1600 if (find_tape_device_and_size(bkpinfo->media_device, sz_size)) {
1601 log_msg(3, _("Ok, using vanilla scsi tape."));
1602 paranoid_alloc(bkpinfo->media_device,VANILLA_SCSI_TAPE );
1603 if ((fin = fopen(bkpinfo->media_device, "r"))) {
1604 paranoid_fclose(fin);
1605 } else {
1606 paranoid_alloc(bkpinfo->media_device,"/dev/osst0");
1607 }
1608 }
1609 if (bkpinfo->media_device != NULL) {
1610 if ((fin = fopen(bkpinfo->media_device, "r"))) {
1611 paranoid_fclose(fin);
1612 } else {
1613 if (does_file_exist("/tmp/mondo-restore.cfg")) {
1614 read_cfg_var("/tmp/mondo-restore.cfg", "media-dev",
1615 bkpinfo->media_device);
1616 }
1617 }
1618 asprintf(&tmp,
1619 _("I think I've found your tape streamer at %s; am I right on the money?"),
1620 bkpinfo->media_device);
1621 if (!ask_me_yes_or_no(tmp)) {
1622 paranoid_free(bkpinfo->media_device);
1623 }
1624 paranoid_free(tmp);
1625 }
1626 if (bkpinfo->media_device == NULL) {
1627 if (!popup_and_get_string
1628 (_("Device name?"),
1629 _("What is the /dev entry of your tape streamer?"),
1630 bkpinfo->media_device, MAX_STR_LEN / 4)) {
1631 log_to_screen(_("User has chosen not to backup the PC"));
1632 finish(1);
1633 }
1634 }
1635 asprintf(&tmp, "ls -l %s", bkpinfo->media_device);
1636 if (run_program_and_log_output(tmp, FALSE)) {
1637 log_to_screen(_("User has not specified a valid /dev entry"));
1638 finish(1);
1639 }
1640 paranoid_free(tmp);
1641 log_msg(4, "sz_size = %s", sz_size);
1642 sz_size[0] = '\0';
1643 bkpinfo->media_size[0] = 0;
1644 log_msg(4, "media_size[0] = %ld", bkpinfo->media_size[0]);
1645 if (bkpinfo->media_size[0] <= 0) {
1646 bkpinfo->media_size[0] = 0;
1647 }
1648 for (i = 1; i <= MAX_NOOF_MEDIA; i++) {
1649 bkpinfo->media_size[i] = bkpinfo->media_size[0];
1650 }
1651 if (archiving_to_media) {
1652 if ((bkpinfo->compression_level =
1653 which_compression_level()) == -1) {
1654 log_to_screen(_("User has chosen not to backup the PC"));
1655 finish(1);
1656 }
1657 }
1658 break;
1659
1660
1661
1662 case nfs:
1663 if (!bkpinfo->nfs_mount[0]) {
1664 strcpy(bkpinfo->nfs_mount,
1665 call_program_and_get_last_line_of_output
1666 ("mount | grep \":\" | cut -d' ' -f1 | head -n1"));
1667 }
1668#ifdef __FreeBSD__
1669 if (TRUE)
1670#else
1671 if (!bkpinfo->disaster_recovery)
1672#endif
1673 {
1674 if (!popup_and_get_string
1675 (_("NFS dir."),
1676 _("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.)"),
1677 bkpinfo->nfs_mount, MAX_STR_LEN / 4)) {
1678 log_to_screen(_("User has chosen not to backup the PC"));
1679 finish(1);
1680 }
1681 if (!bkpinfo->restore_data) {
1682 if ((bkpinfo->compression_level =
1683 which_compression_level()) == -1) {
1684 log_to_screen(_("User has chosen not to backup the PC"));
1685 finish(1);
1686 }
1687 }
1688 // check whether already mounted - we better remove
1689 // surrounding spaces and trailing '/' for this
1690 strip_spaces(bkpinfo->nfs_mount);
1691 if (bkpinfo->nfs_mount[strlen(bkpinfo->nfs_mount) - 1] == '/')
1692 bkpinfo->nfs_mount[strlen(bkpinfo->nfs_mount) - 1] = '\0';
1693 asprintf(&command, "mount | grep \"%s \" | cut -d' ' -f3",
1694 bkpinfo->nfs_mount);
1695 strcpy(bkpinfo->isodir,
1696 call_program_and_get_last_line_of_output(command));
1697 paranoid_free(command);
1698
1699 asprintf(&comment,
1700 _("How much data (in Megabytes) will each media store?"));
1701 if (!popup_and_get_string(_("Size"), comment, sz_size, 5)) {
1702 log_to_screen(_("User has chosen not to backup the PC"));
1703 finish(1);
1704 }
1705 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1706 bkpinfo->media_size[i] = atoi(sz_size);
1707 }
1708 if (bkpinfo->media_size[0] <= 0) {
1709 log_to_screen(_("User has chosen not to backup the PC"));
1710 finish(1);
1711 }
1712 paranoid_free(comment);
1713 }
1714 if (bkpinfo->disaster_recovery) {
1715 system("umount /tmp/isodir 2> /dev/null");
1716 if (!popup_and_get_string
1717 (_("NFS share"), _("Which remote NFS share should I mount?"),
1718 bkpinfo->nfs_mount, MAX_STR_LEN)) {
1719 log_to_screen(_("User has chosen not to backup the PC"));
1720 finish(1);
1721 }
1722 }
1723 if (!is_this_device_mounted(bkpinfo->nfs_mount)) {
1724 sprintf(bkpinfo->isodir, "/tmp/isodir.mondo.%d",
1725 (int) (random() % 32768));
1726 asprintf(&command, "mkdir -p %s", bkpinfo->isodir);
1727 run_program_and_log_output(command, 5);
1728 paranoid_free(command);
1729
1730 asprintf(&tmp, "mount -t nfs -o nolock %s %s", bkpinfo->nfs_mount,
1731 bkpinfo->isodir);
1732 run_program_and_log_output(tmp, 5);
1733 paranoid_free(tmp);
1734 malloc_string(g_selfmounted_isodir);
1735 strcpy(g_selfmounted_isodir, bkpinfo->isodir);
1736 }
1737 if (!is_this_device_mounted(bkpinfo->nfs_mount)) {
1738 popup_and_OK
1739 (_("Please mount that partition before you try to backup to or restore from it."));
1740 finish(1);
1741 }
1742 asprintf(&tmp, bkpinfo->nfs_remote_dir);
1743 if (!popup_and_get_string
1744 (_("Directory"), _("Which directory within that mountpoint?"), tmp,
1745 MAX_STR_LEN)) {
1746 log_to_screen(_("User has chosen not to backup the PC"));
1747 finish(1);
1748 }
1749 strcpy(bkpinfo->nfs_remote_dir, tmp);
1750 paranoid_free(tmp);
1751
1752 // check whether writable - we better remove surrounding spaces for this
1753 strip_spaces(bkpinfo->nfs_remote_dir);
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 paranoid_free(tmp);
1758 paranoid_free(prompt);
1759 asprintf(&tmp, bkpinfo->nfs_remote_dir);
1760 asprintf(&prompt,
1761 _("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."),
1762 bkpinfo->nfs_remote_dir, bkpinfo->isodir);
1763 if (!popup_and_get_string
1764 (_("Directory"), prompt, tmp, MAX_STR_LEN)) {
1765 log_to_screen(_("User has chosen not to backup the PC"));
1766 finish(1);
1767 }
1768 strcpy(bkpinfo->nfs_remote_dir, tmp);
1769 // check whether writable - we better remove surrounding spaces for this */
1770 strip_spaces(bkpinfo->nfs_remote_dir);
1771 asprintf(&command, "echo hi > %s/%s/.dummy.txt",
1772 bkpinfo->isodir, bkpinfo->nfs_remote_dir);
1773 }
1774 paranoid_free(command);
1775 paranoid_free(tmp);
1776 paranoid_free(prompt);
1777
1778 if (!popup_and_get_string
1779 (_("Prefix."),
1780 _("Please enter the prefix that will be prepended to your ISO filename. Example: machine1 to obtain machine1-[1-9]*.iso files"),
1781 bkpinfo->prefix, MAX_STR_LEN / 4)) {
1782 log_to_screen(_("User has chosen not to backup the PC"));
1783 finish(1);
1784 }
1785 log_msg(3, "prefix set to %s", bkpinfo->prefix);
1786
1787 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1788 bkpinfo->media_size[i] = 650;
1789 }
1790 log_msg(3, "Just set nfs_remote_dir to %s",
1791 bkpinfo->nfs_remote_dir);
1792 log_msg(3, "isodir is still %s", bkpinfo->isodir);
1793 break;
1794
1795 case iso:
1796 if (!bkpinfo->disaster_recovery) {
1797 if (!popup_and_get_string
1798 (_("Storage dir."),
1799 _("Please enter the full path that contains your ISO images. Example: /mnt/raid0_0"),
1800 bkpinfo->isodir, MAX_STR_LEN / 4)) {
1801 log_to_screen(_("User has chosen not to backup the PC"));
1802 finish(1);
1803 }
1804 if (archiving_to_media) {
1805 if ((bkpinfo->compression_level =
1806 which_compression_level()) == -1) {
1807 log_to_screen(_("User has chosen not to backup the PC"));
1808 finish(1);
1809 }
1810 if (!popup_and_get_string
1811 (_("ISO size."),
1812 _("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."),
1813 sz_size, 16)) {
1814 log_to_screen(_("User has chosen not to backup the PC"));
1815 finish(1);
1816 }
1817 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1818 bkpinfo->media_size[i] = atoi(sz_size);
1819 }
1820 paranoid_free(sz_size);
1821
1822 if (!popup_and_get_string
1823 (_("Prefix."),
1824 _("Please enter the prefix that will be prepended to your ISO filename. Example: machine1 to obtain machine1-[1-9]*.iso files"),
1825 bkpinfo->prefix, MAX_STR_LEN / 4)) {
1826 log_to_screen("User has chosen not to backup the PC");
1827 finish(1);
1828 }
1829 log_msg(3, "prefix set to %s", bkpinfo->prefix);
1830 } else {
1831 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1832 bkpinfo->media_size[i] = 650;
1833 }
1834 }
1835 }
1836 break;
1837 default:
1838 fatal_error
1839 ("I, Mojo Jojo, shall defeat those pesky Powerpuff Girls!");
1840 }
1841 if (archiving_to_media) {
1842
1843#ifdef __FreeBSD__
1844 strcpy(bkpinfo->boot_device,
1845 call_program_and_get_last_line_of_output
1846 ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/\\([0-9]\\).*/\\1/'"));
1847#else
1848 strcpy(bkpinfo->boot_device,
1849 call_program_and_get_last_line_of_output
1850 ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'"));
1851#endif
1852 i = which_boot_loader(bkpinfo->boot_device);
1853 if (i == 'U') // unknown
1854 {
1855
1856#ifdef __FreeBSD__
1857 if (!popup_and_get_string
1858 (_("Boot device"),
1859 _("What is your boot device? (e.g. /dev/ad0)"),
1860 bkpinfo->boot_device, MAX_STR_LEN / 4)) {
1861 log_to_screen(_("User has chosen not to backup the PC"));
1862 finish(1);
1863 }
1864 i = which_boot_loader(bkpinfo->boot_device);
1865#else
1866 if (!popup_and_get_string
1867 (_("Boot device"),
1868 _("What is your boot device? (e.g. /dev/hda)"),
1869 bkpinfo->boot_device, MAX_STR_LEN / 4)) {
1870 log_to_screen(_("User has chosen not to backup the PC"));
1871 finish(1);
1872 }
1873 if (does_string_exist_in_boot_block
1874 (bkpinfo->boot_device, "LILO")) {
1875 i = 'L';
1876 } else
1877 if (does_string_exist_in_boot_block
1878 (bkpinfo->boot_device, "ELILO")) {
1879 i = 'E';
1880 } else
1881 if (does_string_exist_in_boot_block
1882 (bkpinfo->boot_device, "GRUB")) {
1883 i = 'G';
1884 } else {
1885 i = 'U';
1886 }
1887#endif
1888 if (i == 'U') {
1889 if (ask_me_yes_or_no
1890 (_("Unidentified boot loader. Shall I restore it byte-for-byte at restore time and hope for the best?")))
1891 {
1892 i = 'R'; // raw
1893 } else {
1894 log_to_screen
1895 (_("I cannot find your boot loader. Please run mondoarchive with parameters."));
1896 finish(1);
1897 }
1898 }
1899 }
1900 bkpinfo->boot_loader = i;
1901 strcpy(bkpinfo->include_paths, "/");
1902 if (!popup_and_get_string
1903 (_("Backup paths"),
1904 _("Please enter paths which you want me to backup. The default is '/' (i.e. everything)."),
1905 bkpinfo->include_paths, MAX_STR_LEN)) {
1906 log_to_screen(_("User has chosen not to backup the PC"));
1907 finish(1);
1908 }
1909 tmp = list_of_NFS_mounts_only();
1910 if (strlen(tmp) > 2) {
1911 if (bkpinfo->exclude_paths[0]) {
1912 strcat(bkpinfo->exclude_paths, " ");
1913 }
1914 strncpy(bkpinfo->exclude_paths, tmp, MAX_STR_LEN);
1915 }
1916 paranoid_free(tmp);
1917// NTFS
1918 asprintf(&tmp,
1919 call_program_and_get_last_line_of_output
1920 ("parted2fdisk -l | grep -i ntfs | awk '{ print $1};' | tr -s '\\n' ' ' | awk '{ print $0};'"));
1921 if (strlen(tmp) > 2) {
1922 if (!popup_and_get_string
1923 (_("NTFS partitions"),
1924 _("Please enter/confirm the NTFS partitions you wish to backup as well."),
1925 tmp, MAX_STR_LEN / 4)) {
1926 log_to_screen(_("User has chosen not to backup the PC"));
1927 finish(1);
1928 }
1929 strncpy(bkpinfo->image_devs, tmp, MAX_STR_LEN / 4);
1930 }
1931 paranoid_free(tmp);
1932
1933 if (!popup_and_get_string
1934 (_("Exclude paths"),
1935 _("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."),
1936 bkpinfo->exclude_paths, MAX_STR_LEN)) {
1937 log_to_screen(_("User has chosen not to backup the PC"));
1938 finish(1);
1939 }
1940 bkpinfo->make_cd_use_lilo = FALSE;
1941 bkpinfo->backup_data = TRUE;
1942 bkpinfo->verify_data =
1943 ask_me_yes_or_no
1944 (_("Will you want to verify your backups after Mondo has created them?"));
1945
1946#ifndef __FreeBSD__
1947 if (!ask_me_yes_or_no
1948 (_("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."))){
1949 paranoid_alloc(bkpinfo->kernel_path, "FAILSAFE");
1950 }
1951#endif
1952
1953 if (!ask_me_yes_or_no
1954 (_("Are you sure you want to proceed? Hit 'no' to abort."))) {
1955 log_to_screen(_("User has chosen not to backup the PC"));
1956 finish(1);
1957 }
1958 } else {
1959 bkpinfo->restore_data = TRUE; // probably...
1960 }
1961
1962 if (bkpinfo->backup_media_type == iso
1963 || bkpinfo->backup_media_type == nfs) {
1964 g_ISO_restore_mode = TRUE;
1965 }
1966#ifdef __FreeSD__
1967// skip
1968#else
1969 if (bkpinfo->backup_media_type == nfs) {
1970 log_msg(3, "I think the NFS mount is mounted at %s",
1971 bkpinfo->isodir);
1972 }
1973 log_it("isodir = %s", bkpinfo->isodir);
1974 log_it("nfs_mount = '%s'", bkpinfo->nfs_mount);
1975#endif
1976
1977 log_it("media device = %s", bkpinfo->media_device);
1978 log_it("media size = %ld", bkpinfo->media_size[1]);
1979 log_it("media type = %s",
1980 bkptype_to_string(bkpinfo->backup_media_type));
1981 log_it("prefix = %s", bkpinfo->prefix);
1982 log_it("compression = %ld", bkpinfo->compression_level);
1983 log_it("include_paths = '%s'", bkpinfo->include_paths);
1984 log_it("exclude_paths = '%s'", bkpinfo->exclude_paths);
1985 log_it("scratchdir = '%s'", bkpinfo->scratchdir);
1986 log_it("tmpdir = '%s'", bkpinfo->tmpdir);
1987 log_it("boot_device = '%s' (loader=%c)", bkpinfo->boot_device,
1988 bkpinfo->boot_loader);
1989 log_it("prefix = %s", bkpinfo->prefix);
1990 if (bkpinfo->media_size[0] < 0) {
1991 if (archiving_to_media) {
1992 fatal_error("Media size is less than zero.");
1993 } else {
1994 log_msg(2, "Warning - media size is less than zero.");
1995 bkpinfo->media_size[0] = 0;
1996 }
1997 }
1998 return (0);
1999}
2000
2001
2002/**
2003 * Get a space-separated list of NFS mounts.
2004 * @return The list created.
2005 * @note The return value points to static data that will be overwritten with each call.
2006 * @bug Even though we only want the mounts, the devices are still checked.
2007 */
2008char *list_of_NFS_mounts_only(void)
2009{
2010 char *exclude_these_devices;
2011 char *exclude_these_directories;
2012 char *result_sz;
2013
2014 asprintf(&exclude_these_directories,
2015 call_program_and_get_last_line_of_output
2016 ("mount -t coda,ncpfs,nfs,smbfs,cifs | tr -s '\t' ' ' | cut -d' ' -f3 | tr -s '\n' ' ' | awk '{print $0;}'"));
2017 asprintf(&exclude_these_devices,
2018 call_program_and_get_last_line_of_output
2019 ("tr -s '\t' ' ' < /etc/fstab | grep -E '( (coda|ncpfs|nfs|smbfs|cifs) )' | cut -d' ' -f1 | tr -s '\n' ' ' | awk '{print $0;}'"));
2020 asprintf(&result_sz, "%s", exclude_these_directories);
2021 paranoid_free(exclude_these_devices);
2022 paranoid_free(exclude_these_directories);
2023 return (result_sz);
2024}
2025
2026
2027/* @} - end of utilityGroup */
2028
2029/**
2030 * Create a randomly-named FIFO. The format is @p stub "." [random] [random] where
2031 * [random] is a random number between 1 and 32767.
2032 * @param store_name_here Where to store the new filename.
2033 * @param stub A random number will be appended to this to make the FIFO's name.
2034 * @ingroup deviceGroup
2035 */
2036void make_fifo(char *store_name_here, char *stub)
2037{
2038 char *tmp;
2039
2040 assert_string_is_neither_NULL_nor_zerolength(stub);
2041
2042 sprintf(store_name_here, "%s%d%d", stub, (int) (random() % 32768),
2043 (int) (random() % 32768));
2044 make_hole_for_file(store_name_here);
2045 mkfifo(store_name_here, S_IRWXU | S_IRWXG);
2046 asprintf(&tmp, "chmod 770 %s", store_name_here);
2047 paranoid_system(tmp);
2048 paranoid_free(tmp);
2049}
2050
2051
2052/**
2053 * Set the tmpdir and scratchdir to reside on the partition with the most free space.
2054 * Automatically excludes DOS, NTFS, SMB, and NFS filesystems.
2055 * @param bkpinfo The backup information structure. @c bkpinfo->tmpdir and @c bkpinfo->scratchdir will be set.
2056 * @ingroup utilityGroup
2057 */
2058void sensibly_set_tmpdir_and_scratchdir(struct s_bkpinfo *bkpinfo)
2059{
2060 char *tmp, *command, *sz;
2061
2062 assert(bkpinfo != NULL);
2063
2064#ifdef __FreeBSD__
2065 asprintf(&tmp,
2066 call_program_and_get_last_line_of_output
2067 ("df -m -P -t nonfs,msdosfs,ntfs,smbfs,smb,cifs | tr -s '\t' ' ' | grep -v none | grep -v Filesystem | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'"));
2068#else
2069 asprintf(&tmp,
2070 call_program_and_get_last_line_of_output
2071 ("df -m -P -x nfs -x vfat -x ntfs -x smbfs -x smb -x cifs | sed 's/ /devdev/' | tr -s '\t' ' ' | grep -v none | grep -v Filesystem | grep -v /dev/shm | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'"));
2072#endif
2073
2074 if (tmp[0] != '/') {
2075 asprintf(&sz, "/%s", tmp);
2076 paranoid_free(tmp);
2077 tmp = sz;
2078 }
2079 if (!tmp[0]) {
2080 fatal_error("I couldn't figure out the tempdir!");
2081 }
2082 sprintf(bkpinfo->tmpdir, "%s/tmp.mondo.%d", tmp,
2083 (int) (random() % 32768));
2084 log_it("bkpinfo->tmpdir is being set to %s", bkpinfo->tmpdir);
2085
2086 sprintf(bkpinfo->scratchdir, "%s/mondo.scratch.%d", tmp,
2087 (int) (random() % 32768));
2088 log_it("bkpinfo->scratchdir is being set to %s", bkpinfo->scratchdir);
2089
2090 sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
2091 bkpinfo->scratchdir);
2092
2093 asprintf(&command, "rm -Rf %s/tmp.mondo.* %s/mondo.scratch.*", tmp, tmp);
2094 paranoid_free(tmp);
2095
2096 paranoid_system(command);
2097 paranoid_free(command);
2098}
2099
2100
2101/**
2102 * @addtogroup deviceGroup
2103 * @{
2104 */
2105/**
2106 * If we can read @p dev, set @p output to it.
2107 * If @p dev cannot be read, set @p output to "".
2108 * @param dev The device to check for.
2109 * @param output Set to @p dev if @p dev exists, NULL otherwise. Needs to be freed by caller
2110 * @return TRUE if @p dev exists, FALSE if it doesn't.
2111 */
2112bool set_dev_to_this_if_rx_OK(char *output, char *dev)
2113{
2114 char *command;
2115
2116 paranoid_free(output);
2117 if (!dev || dev[0] == '\0') {
2118 return (FALSE);
2119 }
2120 log_msg(10, "Injecting %s", dev);
2121 inject_device(dev);
2122 if (!does_file_exist(dev)) {
2123 log_msg(10, "%s doesn't exist. Returning FALSE.", dev);
2124 return (FALSE);
2125 }
2126 asprintf(&command, "dd bs=%ld count=1 if=%s of=/dev/null &> /dev/null",
2127 512L, dev);
2128 if (!run_program_and_log_output(command, FALSE)
2129 && !run_program_and_log_output(command, FALSE)) {
2130 asprintf(&output, dev);
2131 log_msg(4, "Found it - %s", dev);
2132 paranoid_free(command);
2133 return (TRUE);
2134 } else {
2135 log_msg(4, "It's not %s", dev);
2136 paranoid_free(command);
2137 return (FALSE);
2138 }
2139}
2140
2141
2142/**
2143 * Find out what number CD is in the drive.
2144 * @param bkpinfo The backup information structure. The @c bkpinfo->media_device field is the only one used.
2145 * @return The current CD number, or -1 if it could not be found.
2146 * @note If the CD is not mounted, it will be mounted
2147 * (and remain mounted after this function returns).
2148 */
2149int what_number_cd_is_this(struct s_bkpinfo *bkpinfo)
2150{
2151 int cd_number = -1;
2152 char *mountdev;
2153 char *tmp;
2154
2155 assert(bkpinfo != NULL);
2156 if (g_ISO_restore_mode) {
2157 asprintf(&tmp, "mount | grep iso9660 | awk '{print $3;}'");
2158
2159 asprintf(&mountdev, "%s/archives/THIS-CD-NUMBER", call_program_and_get_last_line_of_output(tmp));
2160 paranoid_free(tmp);
2161
2162 cd_number = atoi(last_line_of_file(mountdev));
2163 paranoid_free(mountdev);
2164
2165 return (cd_number);
2166 }
2167
2168 if (bkpinfo->media_device == NULL) {
2169 log_it
2170 ("(what_number_cd_is_this) Warning - media_device unknown. Finding out...");
2171 bkpinfo->media_device = find_cdrom_device(FALSE);
2172 }
2173 if (!is_this_device_mounted(MNT_CDROM)) {
2174 (void)mount_CDROM_here(bkpinfo->media_device, MNT_CDROM);
2175 }
2176 cd_number =
2177 atoi(last_line_of_file(MNT_CDROM "/archives/THIS-CD-NUMBER"));
2178 return (cd_number);
2179}
2180
2181
2182/**
2183 * Find out what device is mounted as root (/).
2184 * @return Root device.
2185 * @note The returned string points to static storage and will be overwritten with every call.
2186 * @bug A bit of a misnomer; it's actually finding out the root device.
2187 * The mountpoint (where it's mounted) will obviously be '/'.
2188 */
2189char *where_is_root_mounted()
2190{
2191 /*@ buffers **************** */
2192 char *tmp;
2193
2194
2195#ifdef __FreeBSD__
2196 asprintf(&tmp, call_program_and_get_last_line_of_output
2197 ("mount | grep \" on / \" | cut -d' ' -f1"));
2198#else
2199 asprintf(&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 asprintf(&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 asprintf(&tmp,
2209 call_program_and_get_last_line_of_output
2210 ("mount | grep \" on / \" | cut -d' ' -f1"));
2211 }
2212#endif
2213
2214 return (tmp);
2215}
2216
2217
2218/**
2219 * Find out which boot loader is in use.
2220 * @param which_device Device to look for the boot loader on.
2221 * @return 'L' for LILO, 'E'for ELILO, 'G' for GRUB, 'B' or 'D' for FreeBSD boot loaders, or 'U' for Unknown.
2222 * @note Under Linux, all drives are examined, not just @p which_device.
2223 */
2224#ifdef __FreeBSD__
2225char which_boot_loader(char *which_device)
2226{
2227 int count_lilos = 0;
2228 int count_grubs = 0;
2229 int count_boot0s = 0;
2230 int count_dangerouslydedicated = 0;
2231
2232 log_it("looking at drive %s's MBR", which_device);
2233 if (does_string_exist_in_boot_block(which_device, "GRUB")) {
2234 count_grubs++;
2235 }
2236 if (does_string_exist_in_boot_block(which_device, "LILO")) {
2237 count_lilos++;
2238 }
2239 if (does_string_exist_in_boot_block(which_device, "Drive")) {
2240 count_boot0s++;
2241 }
2242 if (does_string_exist_in_first_N_blocks
2243 (which_device, "FreeBSD/i386", 17)) {
2244 count_dangerouslydedicated++;
2245 }
2246 log_it("%d grubs and %d lilos and %d elilos and %d boot0s and %d DD\n",
2247 count_grubs, count_lilos, count_elilos, count_boot0s,
2248 count_dangerouslydedicated);
2249
2250 if (count_grubs && !count_lilos) {
2251 return ('G');
2252 } else if (count_lilos && !count_grubs) {
2253 return ('L');
2254 } else if (count_grubs == 1 && count_lilos == 1) {
2255 log_it("I'll bet you used to use LILO but switched to GRUB...");
2256 return ('G');
2257 } else if (count_boot0s == 1) {
2258 return ('B');
2259 } else if (count_dangerouslydedicated) {
2260 return ('D');
2261 } else {
2262 log_it("Unknown boot loader");
2263 return ('U');
2264 }
2265}
2266
2267#else
2268
2269char which_boot_loader(char *which_device)
2270{
2271 /*@ buffer ***************************************************** */
2272 char *list_drives_cmd;
2273 char *current_drive = NULL;
2274
2275 /*@ pointers *************************************************** */
2276 FILE *pdrives;
2277
2278 /*@ int ******************************************************** */
2279 int count_lilos = 0;
2280 int count_grubs = 0;
2281 size_t n = 0;
2282
2283 /*@ end vars *************************************************** */
2284
2285#ifdef __IA64__
2286 /* No choice for it */
2287 return ('E');
2288#endif
2289 assert(which_device != NULL);
2290 asprintf(&list_drives_cmd,
2291 "parted2fdisk -l 2>/dev/null | grep \"/dev/.*:\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/; echo %s",
2292 where_is_root_mounted());
2293 log_it("list_drives_cmd = %s", list_drives_cmd);
2294
2295 if (!(pdrives = popen(list_drives_cmd, "r"))) {
2296 log_OS_error("Unable to open list of drives");
2297 paranoid_free(list_drives_cmd);
2298 return ('\0');
2299 }
2300 paranoid_free(list_drives_cmd);
2301
2302 for (getline(&current_drive, &n, pdrives); !feof(pdrives);
2303 getline(&current_drive, &n, pdrives)) {
2304 strip_spaces(current_drive);
2305 log_it("looking at drive %s's MBR", current_drive);
2306 if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
2307 count_grubs++;
2308 strcpy(which_device, current_drive);
2309 break;
2310 }
2311 if (does_string_exist_in_boot_block(current_drive, "LILO")) {
2312 count_lilos++;
2313 strcpy(which_device, current_drive);
2314 break;
2315 }
2316 }
2317 paranoid_free(current_drive);
2318
2319 if (pclose(pdrives)) {
2320 log_OS_error("Cannot pclose pdrives");
2321 }
2322 log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
2323 if (count_grubs && !count_lilos) {
2324 return ('G');
2325 } else if (count_lilos && !count_grubs) {
2326 return ('L');
2327 } else if (count_grubs == 1 && count_lilos == 1) {
2328 log_it("I'll bet you used to use LILO but switched to GRUB...");
2329 return ('G');
2330 } else {
2331 log_it("Unknown boot loader");
2332 return ('U');
2333 }
2334}
2335#endif
2336
2337
2338/**
2339 * Write zeroes over the first 16K of @p device.
2340 * @param device The device to zero.
2341 * @return 0 for success, 1 for failure.
2342 */
2343int zero_out_a_device(char *device)
2344{
2345 FILE *fout;
2346 int i;
2347
2348 assert_string_is_neither_NULL_nor_zerolength(device);
2349
2350 log_it("Zeroing drive %s", device);
2351 if (!(fout = fopen(device, "w"))) {
2352 log_OS_error("Unable to open/write to device");
2353 return (1);
2354 }
2355 for (i = 0; i < 16384; i++) {
2356 fputc('\0', fout);
2357 }
2358 paranoid_fclose(fout);
2359 log_it("Device successfully zeroed.");
2360 return (0);
2361}
2362
2363
2364/**
2365 * Return the device pointed to by @p incoming.
2366 * @param incoming The device to resolve symlinks for.
2367 * @return The path to the real device file.
2368 * @note The returned string points to a storage that need to be freed by the caller
2369 * @bug Won't work with file v4.0; needs to be written in C.
2370 */
2371char *resolve_softlinks_to_get_to_actual_device_file(char *incoming)
2372{
2373 char *output;
2374 char *command;
2375 char *curr_fname;
2376 char *scratch;
2377 char *tmp;
2378 char *p;
2379
2380 struct stat statbuf;
2381 if (!does_file_exist(incoming)) {
2382 log_it
2383 ("resolve_softlinks_to_get_to_actual_device_file --- device not found");
2384 asprintf(&output, incoming);
2385 } else {
2386 asprintf(&curr_fname, incoming);
2387 lstat(curr_fname, &statbuf);
2388 while (S_ISLNK(statbuf.st_mode)) {
2389 log_msg(1, "curr_fname = %s", curr_fname);
2390 asprintf(&command, "file %s", curr_fname);
2391 asprintf(&tmp, call_program_and_get_last_line_of_output(command));
2392 paranoid_free(command);
2393
2394 for (p = tmp + strlen(tmp); p != tmp && *p != '`' && *p != ' ';
2395 p--);
2396 p++;
2397 asprintf(&scratch, p);
2398 for (p = scratch; *p != '\0' && *p != '\''; p++);
2399 *p = '\0';
2400 log_msg(0, "curr_fname %s --> '%s' --> %s", curr_fname, tmp,
2401 scratch);
2402 paranoid_free(tmp);
2403
2404 if (scratch[0] == '/') {
2405 paranoid_free(curr_fname);
2406 asprintf(&curr_fname, scratch); // copy whole thing because it's an absolute softlink
2407 } else { // copy over the basename cos it's a relative softlink
2408 p = curr_fname + strlen(curr_fname);
2409 while (p != curr_fname && *p != '/') {
2410 p--;
2411 }
2412 if (*p == '/') {
2413 p++;
2414 }
2415 *p = '\0';
2416 asprintf(&tmp, "%s%s", curr_fname, scratch);
2417 paranoid_free(curr_fname);
2418 curr_fname = tmp;
2419 }
2420 paranoid_free(scratch);
2421 lstat(curr_fname, &statbuf);
2422 }
2423 asprintf(&output, curr_fname);
2424 log_it("resolved %s to %s", incoming, output);
2425 paranoid_free(curr_fname);
2426 }
2427 return (output);
2428}
2429
2430/* @} - end of deviceGroup */
2431
2432
2433/**
2434 * Return the type of partition format (GPT or MBR)
2435 * Caller needs to free the return value
2436 */
2437char *which_partition_format(const char *drive)
2438{
2439 char *output;
2440 char *tmp;
2441 char *command;
2442 char *fdisk;
2443
2444 log_msg(0, "Looking for partition table format type");
2445 asprintf(&fdisk, "/sbin/parted2fdisk");
2446 log_msg(1, "Using %s", fdisk);
2447 asprintf(&command, "%s -l %s | grep 'EFI GPT'", fdisk, drive);
2448 paranoid_free(fdisk);
2449
2450 asprintf(&tmp, call_program_and_get_last_line_of_output(command));
2451 paranoid_free(command);
2452
2453 if (strstr(tmp, "GPT") == NULL) {
2454 asprintf(&output, "MBR");
2455 } else {
2456 asprintf(&output, "GPT");
2457 }
2458 paranoid_free(tmp);
2459 log_msg(0, "Found %s partition table format type", output);
2460 return (output);
2461}
2462
2463/* @} - end of deviceGroup */
Note: See TracBrowser for help on using the repository browser.