source: MondoRescue/branches/2.2.5/mondo/src/common/libmondo-devices.c@ 1819

Last change on this file since 1819 was 1819, checked in by Bruno Cornec, 16 years ago
  • removes bkphw dir before potential creation
  • render mount command more vebose at restore time
  • Property svn:keywords set to Id
File size: 82.1 KB
Line 
1/* libmondo-devices.c Subroutines for handling devices
2 $Id: libmondo-devices.c 1819 2007-11-21 10:07:32Z bruno $
3.
4
5
601/07/2005
7- sensibly_set_tmpdir_and_scratchdir() --- exclude smb and smbfs and (new) cifs
8
910/23
10- if root mounted at '/dev/rd/' then say FALSE, not mounted on ramdisk
11 just to please Fred Beondo :-p
12
1307/25
14- updated get_phys_size_of_drive() to support newer, larger drives
15
1607/18
17- better support of users who boot from LVM CD and nuke-restore non-LVM backups
18
1907/08
20- BLKGETSIZE64 instead of BLKGETIO..whatever (Joshua)
21- amended to use BLKGETSIZE if BLKGETSIZE64 missing (Hugo)
22- resort to HDIO_GETGEO if BLKGETSIZE and BLKGETSIZE63 fail
23
24
2506/25
26- added Mandrake 9.2 support to which_boot_loader()
27- cleaned up find_cdrom_device()
28
2906/23
30- rewrote resolve_softlinks_to_get_to_actual_device_file() in C
31
3206/17
33- don't use star automatically if SELINUX detected; let user do that
34
3505/07
36- usage of parted2fdisk instead of fdisk alone (ia32/ia64 compatibility)
37 BCO
38
3904/17
40- replaced INTERNAL_TAPE_BLK_SIZE with bkpinfo->internal_tape_block_size
41
4204/13
43- if kernel >= 2.6 then ask for /dev entry, not SCSI node
44
4504/12
46- in mount_CDROM_...whatever, don't use bkpinfo_DONTUSETHIS to see if
47 mounting a real CD device; look for /dev/ at start of $device instead
48
4904/04
50- when testing for tape drive, use INTERNAL_TAPE_BLK_SIZE, not TAPE_BLOCK_SIZE
51
5204/03
53- don't eject/retract drive if ISO
54
5502/23/2004
56- changed strstr() to strncmp() in a few places
57
5812/11/2003
59- if we can't find the DVD using dvdrecord then try it w/cdrecord
60
6111/15
62- changed a few []s to char*s
63- better support of multiple CD-ROM drives
64
6511/14
66- better find_cdrom_device(), to cope w/ multiple CD writers
67
6810/26
69- call 'mt -f %s offline' to eject tape drive in eject_device()
70
7110/25
72- ask user to confirm which NFS share to mount
73- improved which_boot_loader()
74
7510/22
76- tweaked find_cdrom_device() and find_cdrw_device()
77- created find_dvd_device()
78
7910/21
80- changed "/mnt/cdrom" to MNT_CDROM
81
8209/28
83- interactive gui no longer asks dvd's speed
84
8509/26
86- interactive gui now supports dvd
87
8809/23
89- malloc/free global strings in new subroutines - malloc_libmondo_global_strings()
90 and free_libmondo_global_strings() - which are in libmondo-tools.c
91
9209/21
93- ask for CD size when backing up interactively
94- offer to exclude NFS shares if they're present
95
9609/20
97- exclude /dev/shm from sensibly_*()
98
9909/18
100- less verbose logging when insisting on CD #n
101
10209/09
103- added resolve_softlinks_to_get_to_actual_device_file()
104
10509/05
106- rewrote inisist_on_this_cd_number() and what_number_cd_is_this()
107
10809/02
109- is_this_a_valid_disk_format() -- treat ntfs (a.k.a. type 7) as recognized format
110 just in case user's backup includes a partimagehack-imaged drive
111
11208/01 - 08/31
113- better boot-time tape detection
114- remove backup-time, erroneous, "remove floppy" msg
115- working on am_I_in_disaster_recovery_mode()
116- interactively_obtain_...() - pause & ask user to remove CD/floppy
117
11806/01 - 07/31
119- in which_boot_loader(), also search /dev/cciss for boot loader
120- fixed calls to popup_and_get_string()
121- fixed bug in get_phys_size_of_drive()
122- fixed bug in where_is_root_mounted()
123- commented out a spurious assert()
124
12505/01 - 05/31
126- superior get_phys_size_of_drive() (Joshua Oreman)
127- fixed call to inject_device() --- Andree L.
128- find_cdrom_device() now may, if asked to, mount drive if possible,
129 to test that it has a CD in it; it then unmounts
130- mount_CDROM_here() now calls retract_cd_and_defeat_autorun() as well
131- fixed support for subdir-within-NFS-mount
132- cleaned up some FreeBSD-specific stuff
133- added Joshua Oreman's FreeBSD patches
134- mount_CDROM_here() --- mkdir -p mountpt
135- add older drive support to get_phys_size_of_drive()
136
13704/01 - 04/30
138- made which_boot_loader() more friendly towards Red Hat 9
139- find_and_mount_actual_cd() retracts all CD trays
140- added lots of log_OS_error()'s and assert()'s
141- fix ISO support in interactively_restore_...()
142- made find_cdrom_device() skip dmesg if second-stage string is empty
143- in find_cdrw_device(), find cdrecord before dvdrecord
144- fixed am_i_in_disaster_recovery_mode()
145- fixed insist_on_this_cd_number()
146
14703/01 - 03/31
148- more work on find_cdrom_device()
149- find_cdrom_device() --- if nonexistent/not found then
150 make sure to return '' as dev str and 1 as res
151- cleaner find_cdrom_device(), to find DVD instead of CD-ROM
152 as last resort
153- fix insist_on_this_cd_number()
154- add user the speed of CD writer, if writing
155- if root is /dev/root then assume not a ramdisk
156
15701/07 - 02/28
158- fixed find_cdrom_device (Stan Benoit)
159- if root is at /dev/root/root then LVM (not ramdisk)
160- fix am_I_in_disaster_recovery_mode() to recognize that /dev/rd/cp*
161 entries are not ramdisks but RAID disks
162- find_cdrw_device() no longer complains to stderr if no cdrecord
163
16401/02/2003
165- sensibly_set_tmpdir_and_scratchdir() --- exclude smb and smbfs
166
16712/01/2002
168- don't ask for isodir path if in disaster recovery mode
169
17011/01 - 11/30
171- added g_restoring_live_from_cd;
172- handle_incoming_parameters() will sensibly set tmpdir and scratchdir
173- properly wipe spurious scratchdir and tmpdir
174- modified sensibly_...() to use tmp.mondo.* and mondo.scratch.*
175 instead of mondo.tmpdir.* and mondo.scratchdir.*
176- line 861,912: set media_size[0] to 1999 as well as 1 thru N
177- chmod 700, not 770, in make_fifo()
178- sensibly_set_tmpdir_and_scratchdir() will now prefix a '/' to the
179 strings if it is missing
180
18110/01 - 10/31
182- changed find_cdrom_device() to ignore DVD drives in the hd[a-d] search;
183 consequently, the subroutine will try scd0, sr0, etc. afterwards
184- commented code
185- fixed obscure bug in find_and_mount_actual_cd()
186- code which sensibly configures tmpdir,scratchdir is now sep. sub.
187- changed sort -s to sort -n <-- sensibly...()
188
18909/01 - 09/30
190- added list_of_NFS_devices_and_mounts()
191- try /dev/st0 if no tape specified at all
192- change '64k' to TAPE_BLOCK_SIZE
193- improved find_cdrom_device()
194- if cdrecord not found then find_cdrom_device() returns 1
195- if disaster recovery mode then don't ask for CD-ROM's /dev entry;
196 assume /dev/cdrom
197- if restoring data then ask for CD-ROM's /dev entry (not its SCSI node)
198- run_program_and_log_output() now takes boolean operator to specify
199 whether it will log its activities in the event of _success_
200- changed sort -n +4 to sort -s +4 (Troff)
201- ask if user's kernel is sane; if 'no' then use FAILSAFE
202- ask user to confirm the tape/CD device name
203- if restoring (not archiving) then don't try to find CD-ROM's SCSI
204 node; try to find its /dev entry instead
205- better detection of biggest partition
206
20708/01 - 08/31
208- fixed verify bug --- CD#1 was being verified again & again & ...
209- detect boot loader + device; if not detectible then _ask_
210- if run interactively, assume tape size is irrelevant & don't ask
211 for it; Mondo should handle end-of-tape gracefully now
212- worked on handling unknowng media size
213- added interactively_obtain_media_parameters_from_user()
214- when trying to mount CD-ROM, find_and_mount_actual_cd() will
215 now call find_cdrom_device() to try to find the /dev entry first
216- fixed serious bug in zero_out_a_device()
217- cleaned up some log_it() calls
218- fixed minor bug in find_cdrom_device()
219- better at distinguishing between live filesystem and ramdisk
220
22107/24/2002
222- created
223*/
224
225/**
226 * @file
227 * Functions to handle interactions with backup devices.
228 */
229
230#include "my-stuff.h"
231#include "mondostructures.h"
232#include "libmondo-files-EXT.h"
233#include "libmondo-devices.h"
234#include "lib-common-externs.h"
235#include "libmondo-string-EXT.h"
236#include "libmondo-tools-EXT.h"
237#include "libmondo-gui-EXT.h"
238#include "libmondo-fork-EXT.h"
239#include "libmondo-stream-EXT.h"
240
241#include <sys/types.h>
242#ifdef __FreeBSD__
243#define DKTYPENAMES
244#define FSTYPENAMES
245#include <sys/disklabel.h>
246#include <sys/disk.h>
247#elif linux
248#define u64 unsigned long long
249#include <linux/fs.h> /* for BLKGETSIZE64 */
250#include <linux/hdreg.h>
251#endif
252
253/*@unused@*/
254//static char cvsid[] = "$Id: libmondo-devices.c 1819 2007-11-21 10:07:32Z bruno $";
255
256extern int g_current_media_number;
257extern double g_kernel_version;
258
259extern bool g_ISO_restore_mode;
260extern char *g_selfmounted_isodir;
261extern char *MONDO_LOGFILE;
262
263extern void setup_tmpdir(char *path);
264
265static char g_cdrw_drive_is_here[MAX_STR_LEN / 4] = "";
266static char g_cdrom_drive_is_here[MAX_STR_LEN / 4] = "";
267static char g_dvd_drive_is_here[MAX_STR_LEN / 4] = "";
268
269
270/**
271 * ????? @bug ?????
272 * @ingroup globalGroup
273 */
274bool g_restoring_live_from_cd = FALSE;
275
276extern t_bkptype g_backup_media_type; // set by main()
277
278/* Reference to global bkpinfo */
279extern struct s_bkpinfo *bkpinfo;
280
281
282
283
284void set_g_cdrom_and_g_dvd_to_bkpinfo_value()
285{
286 strcpy(g_cdrom_drive_is_here, bkpinfo->media_device); // just in case
287 strcpy(g_dvd_drive_is_here, bkpinfo->media_device); // just in case
288}
289
290
291
292/**
293 * Retract all CD trays and wait for autorun to complete.
294 * @ingroup deviceGroup
295 */
296void retract_CD_tray_and_defeat_autorun(void)
297{
298// log_it("rctada: Retracting all CD trays", __LINE__);
299 if (strlen(g_cdrom_drive_is_here) > 0) {
300 inject_device(g_cdrom_drive_is_here);
301 }
302 if (strlen(g_dvd_drive_is_here) > 0) {
303 inject_device(g_dvd_drive_is_here);
304 }
305 if (strlen(g_cdrw_drive_is_here) > 0) {
306 inject_device(g_cdrw_drive_is_here);
307 }
308// log_it("rctada: killing autorun");
309// run_program_and_log_output("killall autorun", TRUE);
310 if (!run_program_and_log_output("ps | grep autorun | grep -v grep", 5)) {
311 log_it("autorun detected; sleeping for 2 seconds");
312 sleep(2);
313 }
314 log_it("rctada: Unmounting all CD drives", __LINE__);
315 run_program_and_log_output("umount /dev/cdr* /dev/dvd*", 5);
316}
317
318
319
320/**
321 * Determine whether we're booted off a ramdisk.
322 * @return @c TRUE (we are) or @c FALSE (we aren't).
323 * @ingroup utilityGroup
324 */
325bool am_I_in_disaster_recovery_mode(void)
326{
327 char *tmp, *comment;
328 bool is_this_a_ramdisk = FALSE;
329
330 malloc_string(tmp);
331 malloc_string(comment);
332 strcpy(tmp, where_is_root_mounted());
333 sprintf(comment, "root is mounted at %s\n", tmp);
334 log_msg(0, comment);
335 log_msg(0,
336 "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().",
337 tmp);
338
339#ifdef __FreeBSD__
340 if (strstr(tmp, "/dev/md")) {
341 is_this_a_ramdisk = TRUE;
342 }
343#else
344 if (!strncmp(tmp, "/dev/ram", 8)
345 || (!strncmp(tmp, "/dev/rd", 7) && !strcmp(tmp, "/dev/rd/")
346 && strncmp(tmp, "/dev/rd/cd", 10)) || strstr(tmp, "rootfs")
347 || !strcmp(tmp, "/dev/root")) {
348 is_this_a_ramdisk = TRUE;
349 } else {
350 is_this_a_ramdisk = FALSE;
351 }
352#endif
353
354 if (is_this_a_ramdisk) {
355 if (!does_file_exist("/THIS-IS-A-RAMDISK")
356 && !does_file_exist("/tmp/mountlist.txt.sample")) {
357 log_to_screen
358 ("Using /dev/root is stupid of you but I'll forgive you.");
359 is_this_a_ramdisk = FALSE;
360 }
361 }
362 if (does_file_exist("/THIS-IS-A-RAMDISK")) {
363 is_this_a_ramdisk = TRUE;
364 }
365 paranoid_free(tmp);
366 paranoid_free(comment);
367 log_msg(1, "Is this a ramdisk? result = %d", is_this_a_ramdisk);
368 return (is_this_a_ramdisk);
369}
370
371
372
373
374
375/**
376 * Turn @c bkpinfo->backup_media_type into a human-readable string.
377 * @return The human readable string (e.g. @c cdr becomes <tt>"cdr"</tt>).
378 * @note The returned string points to static storage that will be overwritten with each call.
379 * @ingroup stringGroup
380 */
381static char *bkptype_to_string(t_bkptype bt)
382{
383 static char output[MAX_STR_LEN / 4];
384 switch (bt) {
385 case none:
386 strcpy(output, "none");
387 break;
388 case iso:
389 strcpy(output, "iso");
390 break;
391 case cdr:
392 strcpy(output, "cdr");
393 break;
394 case cdrw:
395 strcpy(output, "cdrw");
396 break;
397 case cdstream:
398 strcpy(output, "cdstream");
399 break;
400 case nfs:
401 strcpy(output, "nfs");
402 break;
403 case tape:
404 strcpy(output, "tape");
405 break;
406 case udev:
407 strcpy(output, "udev");
408 break;
409 case usb:
410 strcpy(output, "usb");
411 break;
412 default:
413 strcpy(output, "default");
414 }
415 return (output);
416}
417
418
419
420/**
421 * @addtogroup deviceGroup
422 * @{
423 */
424/**
425 * Eject the tray of the specified CD device.
426 * @param dev The device to eject.
427 * @return the return value of the @c eject command. (0=success, nonzero=failure)
428 */
429int eject_device(char *dev)
430{
431 char *command;
432 int res1 = 0, res2 = 0;
433
434 malloc_string(command);
435
436 if (IS_THIS_A_STREAMING_BACKUP(g_backup_media_type)
437 && g_backup_media_type != udev) {
438 sprintf(command, "mt -f %s offline", dev);
439 res1 = run_program_and_log_output(command, 1);
440 } else {
441 res1 = 0;
442 }
443
444#ifdef __FreeBSD__
445 if (strstr(dev, "acd")) {
446 sprintf(command, "cdcontrol -f %s eject", dev);
447 } else {
448 sprintf(command, "camcontrol eject `echo %s | sed 's|/dev/||'`",
449 dev);
450 }
451#else
452 sprintf(command, "eject %s", dev);
453#endif
454
455 log_msg(3, "Ejecting %s", dev);
456 res2 = run_program_and_log_output(command, 1);
457 paranoid_free(command);
458 if (res1 && res2) {
459 return (1);
460 } else {
461 return (0);
462 }
463}
464
465/**
466 * Load (inject) the tray of the specified CD device.
467 * @param dev The device to load/inject.
468 * @return 0 for success, nonzero for failure.
469 */
470int inject_device(char *dev)
471{
472 char *command;
473 int i;
474
475 malloc_string(command);
476
477
478#ifdef __FreeBSD__
479 if (strstr(dev, "acd")) {
480 sprintf(command, "cdcontrol -f %s close", dev);
481 } else {
482 sprintf(command, "camcontrol load `echo %s | sed 's|/dev/||'`",
483 dev);
484 }
485#else
486 sprintf(command, "eject -t %s", dev);
487#endif
488 i = run_program_and_log_output(command, FALSE);
489 paranoid_free(command);
490 return (i);
491}
492
493
494/**
495 * Determine whether the specified @p device (really, you can use any file)
496 * exists.
497 * @return TRUE if it exists, FALSE if it doesn't.
498 */
499bool does_device_exist(char *device)
500{
501
502 /*@ buffers *********************************************************** */
503 char *tmp;
504 bool ret;
505
506 malloc_string(tmp);
507 assert_string_is_neither_NULL_nor_zerolength(device);
508
509 sprintf(tmp, "ls %s > /dev/null 2> /dev/null", device);
510
511 if (system(tmp)) {
512 ret = FALSE;
513 } else {
514 ret = TRUE;
515 }
516 paranoid_free(tmp);
517 return (ret);
518}
519
520
521/**
522 * Determine whether a non-Microsoft partition exists on any connected hard drive.
523 * @return TRUE (there's a Linux/FreeBSD partition) or FALSE (Microsoft has taken over yet another innocent PC).
524 */
525bool does_nonMS_partition_exist(void)
526{
527#if __FreeBSD__
528 return
529 !system
530 ("for drive in /dev/ad? /dev/da?; do fdisk $drive | grep -q FreeBSD && exit 0; done; false");
531#else
532 return
533 !system
534 ("parted2fdisk -l 2>/dev/null | grep '^/dev/' | grep -Eqv '(MS|DOS|FAT|NTFS)'");
535#endif
536}
537
538/**
539 * Determine whether the specified @p partno exists on the specified @p drive.
540 * @param drive The drive to search for the partition in.
541 * @param partno The partition number to look for.
542 * @return 0 if it exists, nonzero otherwise.
543 */
544int does_partition_exist(const char *drive, int partno)
545{
546 /*@ buffers **************************************************** */
547 char *program;
548 char *incoming;
549 char *searchstr;
550 char *tmp;
551
552 /*@ ints ******************************************************* */
553 int res = 0;
554
555 /*@ pointers *************************************************** */
556 FILE *fin;
557
558
559 /*@ end vars *************************************************** */
560 assert_string_is_neither_NULL_nor_zerolength(drive);
561 assert(partno >= 0 && partno < 999);
562
563 malloc_string(program);
564 malloc_string(incoming);
565 malloc_string(searchstr);
566 malloc_string(tmp);
567
568#ifdef __FreeBSD__
569 // We assume here that this is running from mondorestore. (It is.)
570 sprintf(program, "ls %s %s >/dev/null 2>&1", drive,
571 build_partition_name(tmp, drive, partno));
572 return system(program);
573#else
574 tmp[0] = '\0';
575#endif
576
577 sprintf(program, "parted2fdisk -l %s 2> /dev/null", drive);
578 fin = popen(program, "r");
579 if (!fin) {
580 log_it("program=%s", program);
581 log_OS_error("Cannot popen-in program");
582 return (0);
583 }
584 (void) build_partition_name(searchstr, drive, partno);
585 strcat(searchstr, " ");
586 for (res = 0; !res && fgets(incoming, MAX_STR_LEN - 1, fin);) {
587 if (strstr(incoming, searchstr)) {
588 res = 1;
589 }
590 }
591 if (pclose(fin)) {
592 log_OS_error("Cannot pclose fin");
593 }
594 paranoid_free(program);
595 paranoid_free(incoming);
596 paranoid_free(searchstr);
597 paranoid_free(tmp);
598 return (res);
599}
600
601
602
603
604
605/**
606 * Determine whether given NULL-terminated @p str exists in the MBR of @p dev.
607 * @param dev The device to look in.
608 * @param str The string to look for.
609 * @return TRUE if it exists, FALSE if it doesn't.
610 */
611bool does_string_exist_in_boot_block(char *dev, char *str)
612{
613 /*@ buffers **************************************************** */
614 char *command;
615
616 /*@ end vars *************************************************** */
617 int i;
618
619 assert_string_is_neither_NULL_nor_zerolength(dev);
620 assert_string_is_neither_NULL_nor_zerolength(str);
621
622 malloc_string(command);
623 sprintf(command,
624 "dd if=%s bs=446 count=1 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null",
625 dev, str);
626 i = system(command);
627 paranoid_free(command);
628 if (i) {
629 return (FALSE);
630 } else {
631 return (TRUE);
632 }
633}
634
635/**
636 * Determine whether specified @p str exists in the first @p n sectors of
637 * @p dev.
638 * @param dev The device to look in.
639 * @param str The string to look for.
640 * @param n The number of 512-byte sectors to search.
641 */
642bool does_string_exist_in_first_N_blocks(char *dev, char *str, int n)
643{
644 /*@ buffers **************************************************** */
645 char *command;
646 /*@ end vars *************************************************** */
647 int i;
648
649 malloc_string(command);
650 sprintf(command,
651 "dd if=%s bs=512 count=%i 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null",
652 dev, n, str);
653 i = system(command);
654 paranoid_free(command);
655 if (i) {
656 return (FALSE);
657 } else {
658 return (TRUE);
659 }
660}
661
662
663
664/**
665 * Try to mount CD-ROM at @p mountpoint. If the CD-ROM is not found or has
666 * not been specified, call find_cdrom_device() to find it.
667 * @param bkpinfo The backup information structure. The only field used is @c bkpinfo->media_device.
668 * @param mountpoint Where to mount the CD-ROM.
669 * @return 0 for success, nonzero for failure.
670 * @see mount_CDROM_here
671 */
672int find_and_mount_actual_cd(char *mountpoint)
673{
674 /*@ buffers ***************************************************** */
675
676 /*@ int's ****************************************************** */
677 int res;
678 char *dev;
679
680 /*@ end vars **************************************************** */
681
682 malloc_string(dev);
683 assert(bkpinfo != NULL);
684 assert_string_is_neither_NULL_nor_zerolength(mountpoint);
685
686 if (g_backup_media_type == dvd) {
687 strcpy(dev, g_dvd_drive_is_here);
688 if (!dev[0]) {
689 find_dvd_device(dev, FALSE);
690 }
691 } else {
692 strcpy(dev, g_cdrom_drive_is_here);
693 if (!dev[0]) {
694 find_cdrom_device(dev, FALSE);
695 }
696 }
697
698 if (bkpinfo->backup_media_type != iso) {
699 retract_CD_tray_and_defeat_autorun();
700 }
701
702 if (!dev[0] || (res = mount_CDROM_here(dev, mountpoint))) {
703 if (!popup_and_get_string
704 ("CD-ROM device", "Please enter your CD-ROM's /dev device",
705 dev, MAX_STR_LEN / 4)) {
706 res = 1;
707 } else {
708 res = mount_CDROM_here(dev, mountpoint);
709 }
710 }
711 if (res) {
712 log_msg(1, "mount failed");
713 } else {
714 log_msg(1, "mount succeeded with %s", dev);
715 }
716 paranoid_free(dev);
717 return (res);
718}
719
720
721
722
723
724
725/**
726 * Locate a CD-R/W writer's SCSI node.
727 * @param cdrw_device SCSI node will be placed here.
728 * @return 0 for success, nonzero for failure.
729 */
730int find_cdrw_device(char *cdrw_device)
731{
732 /*@ buffers ************************ */
733 char *comment;
734 char *tmp;
735 char *cdr_exe;
736 char *command;
737
738 malloc_string(comment);
739 malloc_string(tmp);
740 malloc_string(cdr_exe);
741 malloc_string(command);
742 if (g_cdrw_drive_is_here[0]) {
743 strcpy(cdrw_device, g_cdrw_drive_is_here);
744 log_msg(3, "Been there, done that. Returning %s", cdrw_device);
745 paranoid_free(comment);
746 paranoid_free(tmp);
747 paranoid_free(cdr_exe);
748 paranoid_free(command);
749 return (0);
750 }
751 if (g_backup_media_type == dvd) {
752 log_msg(1,
753 "This is dumb. You're calling find_cdrw_device() but you're backing up to DVD. WTF?");
754 paranoid_free(comment);
755 paranoid_free(tmp);
756 paranoid_free(cdr_exe);
757 paranoid_free(command);
758 return (1);
759 }
760 run_program_and_log_output("insmod ide-scsi", -1);
761 if (find_home_of_exe("cdrecord")) {
762 strcpy(cdr_exe, "cdrecord");
763 } else {
764 strcpy(cdr_exe, "dvdrecord");
765 }
766 tmp[0] = '\0';
767 if (find_home_of_exe(cdr_exe)) {
768 sprintf(command,
769 "%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",
770 cdr_exe);
771 strcpy(tmp, call_program_and_get_last_line_of_output(command));
772 }
773 if (strlen(tmp) < 2) {
774 paranoid_free(comment);
775 paranoid_free(tmp);
776 paranoid_free(cdr_exe);
777 paranoid_free(command);
778 return 1;
779 } else {
780 strcpy(cdrw_device, tmp);
781 sprintf(comment, "Found CDRW device - %s", cdrw_device);
782 log_it(comment);
783 strcpy(g_cdrw_drive_is_here, cdrw_device);
784 paranoid_free(comment);
785 paranoid_free(tmp);
786 paranoid_free(cdr_exe);
787 paranoid_free(command);
788 return (0);
789 }
790}
791
792
793
794
795/**
796 * Attempt to locate a CD-ROM device's /dev entry.
797 * Several different methods may be used to find the device, including
798 * calling @c cdrecord, searching @c dmesg, and trial-and-error.
799 * @param output Where to put the located /dev entry.
800 * @param try_to_mount Whether to mount the CD as part of the test; if mount
801 * fails then return failure.
802 * @return 0 for success, nonzero for failure.
803 */
804int find_cdrom_device(char *output, bool try_to_mount)
805{
806 /*@ pointers **************************************************** */
807 FILE *fin;
808 char *p;
809 char *q;
810 char *r;
811 int retval = 0;
812
813 /*@ bool's ****************************************************** */
814 bool found_it = FALSE;
815
816 /*@ buffers ***************************************************** */
817 char *tmp;
818 char *cdr_exe;
819 char *phrase_one;
820 char *phrase_two;
821 char *command;
822 char *dvd_last_resort;
823 char *mountpoint;
824 static char the_last_place_i_found_it[MAX_STR_LEN] = "";
825
826 /*@ intialize *************************************************** */
827 malloc_string(tmp);
828 malloc_string(cdr_exe);
829 malloc_string(phrase_one);
830 malloc_string(phrase_two);
831 malloc_string(command);
832 malloc_string(dvd_last_resort);
833 malloc_string(mountpoint);
834
835 output[0] = '\0';
836 phrase_one[0] = '\0';
837 phrase_two[0] = '\0';
838 dvd_last_resort[0] = '\0';
839
840 /*@ end vars **************************************************** */
841
842 if (g_cdrom_drive_is_here[0] && !isdigit(g_cdrom_drive_is_here[0])) {
843 strcpy(output, g_cdrom_drive_is_here);
844 log_msg(3, "Been there, done that. Returning %s", output);
845 retval = 0;
846 goto end_of_find_cdrom_device;
847 }
848 if (the_last_place_i_found_it[0] != '\0' && !try_to_mount) {
849 strcpy(output, the_last_place_i_found_it);
850 log_msg(3,
851 "find_cdrom_device() --- returning last found location - '%s'",
852 output);
853 retval = 0;
854 goto end_of_find_cdrom_device;
855 }
856
857 sprintf(mountpoint, "%s/cd.mnt", bkpinfo->tmpdir);
858 make_hole_for_dir(mountpoint);
859
860 if (find_home_of_exe("cdrecord")) {
861 strcpy(cdr_exe, "cdrecord");
862 } else {
863 strcpy(cdr_exe, "dvdrecord");
864 }
865 tmp[0] = '\0';
866 if (!find_home_of_exe(cdr_exe)) {
867 strcpy(output, "/dev/cdrom");
868 log_msg(4, "Can't find cdrecord; assuming %s", output);
869 if (!does_device_exist(output)) {
870 log_msg(4, "That didn't work. Sorry.");
871 retval = 1;
872 goto end_of_find_cdrom_device;
873 } else {
874 retval = 0;
875 goto end_of_find_cdrom_device;
876 }
877 }
878
879 sprintf(command, "%s -scanbus 2> /dev/null", cdr_exe);
880 fin = popen(command, "r");
881 if (!fin) {
882 log_msg(4, "command=%s", command);
883 log_OS_error("Cannot popen command");
884 return (1);
885 }
886 for (fgets(tmp, MAX_STR_LEN, fin); !feof(fin);
887 fgets(tmp, MAX_STR_LEN, fin)) {
888 p = strchr(tmp, '\'');
889 if (p) {
890 q = strchr(++p, '\'');
891 if (q) {
892 for (r = q; *(r - 1) == ' '; r--);
893 *r = '\0';
894 strcpy(phrase_one, p);
895 p = strchr(++q, '\'');
896 if (p) {
897 q = strchr(++p, '\'');
898 if (q) {
899 while (*(q - 1) == ' ') {
900 q--;
901 }
902 *q = '\0';
903 strcpy(phrase_two, p);
904 }
905 }
906 }
907 }
908 }
909 paranoid_pclose(fin);
910
911#ifndef __FreeBSD__
912 if (strlen(phrase_two) == 0) {
913 log_msg(4, "Not running phase two. String is empty.");
914 } else {
915 sprintf(command, "dmesg | grep \"%s\" 2> /dev/null", phrase_two);
916 fin = popen(command, "r");
917 if (!fin) {
918 log_msg(4, "Cannot run 2nd command - non-fatal, fortunately");
919 } else {
920 for (fgets(tmp, MAX_STR_LEN, fin); !feof(fin);
921 fgets(tmp, MAX_STR_LEN, fin)) {
922 log_msg(5, "--> '%s'", tmp);
923 if (tmp[0] != ' ' && tmp[1] != ' ') {
924 p = strchr(tmp, ':');
925 if (p) {
926 *p = '\0';
927 if (strstr(tmp, "DVD")) {
928 sprintf(dvd_last_resort, "/dev/%s", tmp);
929 log_msg(4,
930 "Ignoring '%s' because it's a DVD drive",
931 tmp);
932 } else {
933 sprintf(output, "/dev/%s", tmp);
934 found_it = TRUE;
935 }
936 }
937 }
938 }
939 paranoid_pclose(fin);
940 }
941 }
942
943#endif
944#ifdef __FreeBSD__
945 if (!found_it) {
946 log_msg(4, "OK, approach 2");
947 if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/cdrom"))) {
948 if (!
949 (found_it =
950 set_dev_to_this_if_rx_OK(output, "/dev/cdrom1"))) {
951 if (!
952 (found_it =
953 set_dev_to_this_if_rx_OK(output, "/dev/dvd"))) {
954 if (!
955 (found_it =
956 set_dev_to_this_if_rx_OK(output, "/dev/acd0"))) {
957 if (!
958 (found_it =
959 set_dev_to_this_if_rx_OK(output,
960 "/dev/cd01"))) {
961 if (!
962 (found_it =
963 set_dev_to_this_if_rx_OK(output,
964 "/dev/acd1"))) {
965 if (!
966 (found_it =
967 set_dev_to_this_if_rx_OK(output,
968 "/dev/cd1")))
969 {
970 retval = 1;
971 goto end_of_find_cdrom_device;
972 }
973 }
974 }
975 }
976 }
977 }
978 }
979 }
980#else
981 if (!found_it && strlen(dvd_last_resort) > 0) {
982 log_msg(4, "Well, I'll use the DVD - %s - as a last resort",
983 dvd_last_resort);
984 strcpy(output, dvd_last_resort);
985 found_it = TRUE;
986 }
987 if (found_it) {
988 sprintf(tmp, "grep \"%s=ide-scsi\" /proc/cmdline &> /dev/null",
989 strrchr(output, '/') + 1);
990 if (system(tmp) == 0) {
991 log_msg(4,
992 "%s is not right. It's being SCSI-emulated. Continuing.",
993 output);
994 found_it = FALSE;
995 output[0] = '\0';
996 }
997 }
998
999 if (found_it) {
1000 log_msg(4, "(find_cdrom_device) --> '%s'", output);
1001 if (!does_device_exist(output)) {
1002 found_it = FALSE;
1003 log_msg(4, "OK, I was wrong, I haven't found it... yet.");
1004 }
1005 }
1006
1007 if (!found_it) {
1008 log_msg(4, "OK, approach 2");
1009 if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/scd0"))) {
1010 if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/sr0"))) {
1011 if (!
1012 (found_it =
1013 set_dev_to_this_if_rx_OK(output, "/dev/cdrom"))) {
1014 if (!
1015 (found_it =
1016 set_dev_to_this_if_rx_OK(output,
1017 "/dev/cdrom0"))) {
1018 if (!
1019 (found_it =
1020 set_dev_to_this_if_rx_OK(output,
1021 "/dev/cdrom1"))) {
1022 if (!
1023 (found_it =
1024 set_dev_to_this_if_rx_OK(output,
1025 "/dev/sr1"))) {
1026 if (!
1027 (found_it =
1028 set_dev_to_this_if_rx_OK(output,
1029 "/dev/dvd")))
1030 {
1031 if (!
1032 (found_it =
1033 set_dev_to_this_if_rx_OK(output,
1034 g_cdrw_drive_is_here)))
1035 {
1036 retval = 1;
1037 goto end_of_find_cdrom_device;
1038 }
1039 }
1040 }
1041 }
1042 }
1043 }
1044 }
1045 }
1046 }
1047#endif
1048
1049 if (found_it && try_to_mount) {
1050 if (mount_CDROM_here(output, mountpoint)) {
1051 log_msg(4, "[Cardigans] I've changed my mind");
1052 found_it = FALSE;
1053 } else {
1054 sprintf(tmp, "%s/archives", mountpoint);
1055 if (!does_file_exist(tmp)) {
1056 log_msg(4, "[Cardigans] I'll take it back");
1057 found_it = FALSE;
1058 } else {
1059 sprintf(command, "umount %s", output);
1060 paranoid_system(command);
1061 log_msg(4, "I'm confident the Mondo CD is in %s", output);
1062 }
1063 }
1064 }
1065 unlink(mountpoint);
1066
1067 if (found_it) {
1068 if (!does_file_exist(output)) {
1069 log_msg(3, "I still haven't found it.");
1070 return (1);
1071 }
1072 log_msg(3, "(find_cdrom_device) --> '%s'", output);
1073 strcpy(the_last_place_i_found_it, output);
1074 strcpy(g_cdrom_drive_is_here, output);
1075 retval = 0;
1076 goto end_of_find_cdrom_device;
1077 }
1078
1079 sprintf(command,
1080 "%s -scanbus | grep \"[0-9],[0-9],[0-9]\" | grep \"[D|C][V|D]\" | grep -n \"\" | grep \"%s\" | cut -d':' -f2",
1081 cdr_exe, g_cdrw_drive_is_here);
1082 log_msg(1, "command=%s", command);
1083 strcpy(tmp, call_program_and_get_last_line_of_output(command));
1084 if (tmp[0]) {
1085 strcpy(output, tmp);
1086 log_msg(4, "Finally found it at %s", output);
1087 retval = 0;
1088 goto end_of_find_cdrom_device;
1089 } else {
1090 log_msg(4, "Still couldn't find it.");
1091 retval = 1;
1092 goto end_of_find_cdrom_device;
1093 }
1094 end_of_find_cdrom_device:
1095 paranoid_free(tmp);
1096 paranoid_free(cdr_exe);
1097 paranoid_free(phrase_one);
1098 paranoid_free(phrase_two);
1099 paranoid_free(command);
1100 paranoid_free(dvd_last_resort);
1101 paranoid_free(mountpoint);
1102 return (retval);
1103}
1104
1105
1106
1107
1108
1109int find_dvd_device(char *output, bool try_to_mount)
1110{
1111 char *command;
1112 char *tmp;
1113 int retval = 0, devno = -1;
1114
1115 malloc_string(command);
1116 malloc_string(tmp);
1117
1118 if (g_dvd_drive_is_here[0]) {
1119 strcpy(output, g_dvd_drive_is_here);
1120 log_msg(3, "Been there, done that. Returning %s", output);
1121 return (0);
1122 }
1123
1124 sprintf(tmp, call_program_and_get_last_line_of_output
1125 ("dvdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1")
1126 );
1127 log_msg(5, "tmp = '%s'", tmp);
1128 if (!tmp[0])
1129 sprintf(tmp, call_program_and_get_last_line_of_output
1130 ("cdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1")
1131 );
1132 if (tmp[0]) {
1133 devno = atoi(tmp) - 1;
1134 }
1135 if (devno >= 0) {
1136 retval = 0;
1137 sprintf(output, "/dev/scd%d", devno);
1138 strcpy(g_dvd_drive_is_here, output);
1139 log_msg(2, "I think DVD is at %s", output);
1140 } else {
1141 log_msg(2, "I cannot find DVD");
1142 retval = 1;
1143 }
1144
1145 if (try_to_mount) {
1146 log_msg(1, "Ignoring the fact that try_to_mount==TRUE");
1147 }
1148 return (retval);
1149}
1150
1151
1152
1153
1154
1155#include <sys/ioctl.h>
1156
1157/**
1158 * Find the size of the specified @p drive, in megabytes. Uses @c ioctl calls
1159 * and @c dmesg.
1160 * @param drive The device to find the size of.
1161 * @return size in megabytes.
1162 */
1163long get_phys_size_of_drive(char *drive)
1164{
1165 int fd;
1166#if linux
1167 unsigned long long s = 0;
1168 int fileid, cylinders = 0, cylindersleft = 0;
1169 int cylindersize = 0;
1170 int gotgeo = 0;
1171
1172
1173 struct hd_geometry hdgeo;
1174#elif __FreeBSD__
1175 off_t s;
1176#endif
1177
1178 long outvalA = -1;
1179 long outvalB = -1;
1180 long outvalC = -1;
1181
1182 if ((fd = open(drive, O_RDONLY)) != -1) {
1183 if (ioctl(fd,
1184#if linux
1185#ifdef BLKGETSIZE64
1186 BLKGETSIZE64,
1187#else
1188 BLKGETSIZE,
1189#endif
1190#elif __FreeBSD__
1191 DIOCGMEDIASIZE,
1192#endif
1193 &s) != -1) {
1194 close(fd);
1195 // s>>11 works for older disks but not for newer ones
1196 outvalB =
1197#if linux
1198#ifdef BLKGETSIZE64
1199 s >> 20
1200#else
1201 s >> 11
1202#endif
1203#else
1204 s >> 20
1205#endif
1206 ;
1207 }
1208 }
1209
1210 if (outvalB <= 0) {
1211 log_msg(1, "Error getting size of %s: %s", drive, strerror(errno));
1212#if linux
1213 fileid = open(drive, O_RDONLY);
1214 if (fileid) {
1215 if (ioctl(fileid, HDIO_GETGEO, &hdgeo) != -1) {
1216 if (hdgeo.cylinders && hdgeo.heads && hdgeo.sectors) {
1217 cylindersleft = cylinders = hdgeo.cylinders;
1218 cylindersize = hdgeo.heads * hdgeo.sectors / 2;
1219 outvalA = cylindersize * cylinders / 1024;
1220 log_msg(2, "Got Harddisk geometry, C:%d, H:%d, S:%d",
1221 hdgeo.cylinders, hdgeo.heads, hdgeo.sectors);
1222 gotgeo = 1;
1223 } else {
1224 log_msg(1, "Harddisk geometry wrong");
1225 }
1226 } else {
1227 log_msg(1,
1228 "Error in ioctl() getting new hard disk geometry (%s), resizing in unsafe mode",
1229 strerror(errno));
1230 }
1231 close(fileid);
1232 } else {
1233 log_msg(1, "Failed to open %s for reading: %s", drive,
1234 strerror(errno));
1235 }
1236 if (!gotgeo) {
1237 log_msg(1, "Failed to get harddisk geometry, using old mode");
1238 }
1239/*
1240 if ((fd = open (drive, O_RDONLY)) != -1) {
1241 if (ioctl (fd, HDIO_GETGEO, &hdgeo) != -1) {
1242 close (fd);
1243 log_msg (2, "Geometry of drive %s is C:%d, H:%d, S%d, its size is %d MB", drive, hdgeo.cylinders, hdgeo.heads, hdgeo.sectors, (hdgeo.cylinders * hdgeo.heads * hdgeo.sectors / 2 / 1024));
1244 if ( hdgeo.cylinders && hdgeo.heads && hdgeo.sectors ) {
1245 outvalB = ((long) (hdgeo.cylinders * hdgeo.heads * hdgeo.sectors / 2 / 1024));
1246 }
1247 }
1248 close (fd);
1249 */
1250#endif
1251 }
1252// OLDER DISKS will give ridiculously low value for outvalB (so outvalA is returned) :)
1253// NEWER DISKS will give sane value for outvalB (close to outvalA, in other words) :)
1254
1255 outvalC = (outvalA > outvalB) ? outvalA : outvalB;
1256
1257// log_msg (5, "drive = %s, error = %s", drive, strerror (errno));
1258// fatal_error ("GPSOD: Unable to get size of drive");
1259 log_msg(1, "%s --> %ld or %ld --> %ld", drive, outvalA, outvalB,
1260 outvalC);
1261
1262 return (outvalC);
1263}
1264
1265/* The old version */
1266#if 0
1267long get_phys_size_of_drive(char *drive)
1268{
1269 /*@ pointers **************************************************** */
1270#if linux
1271 FILE *fin;
1272 char *p;
1273 char *q;
1274 char *r;
1275 /*@ buffers ***************************************************** */
1276 char *tmp;
1277 char *command;
1278
1279 /*@ long ******************************************************** */
1280 long outL;
1281 long tempLa;
1282 long tempLb;
1283 long tempLc;
1284
1285#endif
1286
1287 struct hd_geometry hdgeo;
1288 int fd;
1289
1290#ifdef __FreeBSD__
1291 off_t o;
1292
1293 if ((fd = open(drive, O_RDONLY)) != -1) {
1294 if (ioctl(fd, DIOCGMEDIASIZE, &o) != -1) {
1295 close(fd);
1296 return (long) (o / (off_t) (1024 * 1024));
1297 }
1298 close(fd);
1299 }
1300 log_msg(4, "drive = %s, error = %s", drive, strerror(errno));
1301 fatal_error("GPSOD: Unable to get size of drive");
1302#else
1303
1304 malloc_string(tmp);
1305 malloc_string(command);
1306
1307 if ((fd = open(drive, O_RDONLY)) != -1) {
1308 if (ioctl(fd, HDIO_GETGEO, &hdgeo) != -1) {
1309 close(fd);
1310 log_msg(2,
1311 "Geometry of drive %s is C:%d, H:%d, S%d, its size is %d MB",
1312 drive, hdgeo.cylinders, hdgeo.heads, hdgeo.sectors,
1313 (hdgeo.cylinders * hdgeo.heads * hdgeo.sectors / 2 /
1314 1024));
1315 if (hdgeo.cylinders && hdgeo.heads && hdgeo.sectors) {
1316 return ((long)
1317 (hdgeo.cylinders * hdgeo.heads * hdgeo.sectors /
1318 2 / 1024));
1319 }
1320 }
1321 close(fd);
1322 }
1323
1324 assert_string_is_neither_NULL_nor_zerolength(drive);
1325
1326 sprintf(command,
1327 "parted2fdisk -l %s | head -n4 | tr -s '\n' '\t' | tr -s ' ' '\t' | cut -f8,14,16",
1328 drive);
1329 strcpy(tmp, call_program_and_get_last_line_of_output(command));
1330 if (tmp[0]) {
1331 p = tmp;
1332 q = strchr(p, ' ');
1333 if (q) {
1334 *(q++) = '\0';
1335 r = strchr(q, ' ');
1336 if (r) {
1337 *(r++) = '\0';
1338 tempLa = atol(p);
1339 tempLb = atol(q);
1340 tempLc = atol(r);
1341 outL = tempLa * tempLb / 1024 * tempLc / 1024;
1342 if (outL > 100) {
1343 paranoid_free(tmp);
1344 paranoid_free(command);
1345 return (outL);
1346 }
1347 }
1348 }
1349 }
1350
1351 /* try to grep for 'Drive xxxx: yyy MB' */
1352 sprintf(command,
1353 "parted2fdisk -l %s | grep MB | tr -s ' ' '\t' | cut -f3",
1354 drive);
1355 strcpy(tmp, call_program_and_get_last_line_of_output(command));
1356 if (atol(tmp) > 0) {
1357 paranoid_free(tmp);
1358 paranoid_free(command);
1359 return (atol(tmp));
1360 }
1361
1362 /* else, do it the old-fashioned way */
1363 p = strrchr(drive, (int) '/');
1364 if (p) {
1365 strcpy(tmp, p + 1);
1366 } else {
1367 paranoid_free(tmp);
1368 paranoid_free(command);
1369 return (-1);
1370 }
1371 sprintf(command, "dmesg | grep %s 2> /dev/null", tmp);
1372 if (!(fin = popen(command, "r"))) {
1373 log_OS_error("Cannot popen dmesg command");
1374 } else {
1375 fgets(tmp, MAX_STR_LEN - 1, fin);
1376 while (!feof(fin) && !strstr(tmp, "GB") && !strstr(tmp, "MB")) {
1377 fgets(tmp, MAX_STR_LEN - 1, fin);
1378 }
1379 if (pclose(fin)) {
1380 log_OS_error("Cannot pclose dmesg fin");
1381 }
1382 }
1383 if (!(p = strstr(tmp, "GB")) && !(p = strstr(tmp, "MB"))) {
1384 log_msg(3, "Cannot find %s's size: dmesg isn't helping either.",
1385 drive);
1386 paranoid_free(tmp);
1387 paranoid_free(command);
1388 return (-1);
1389 }
1390 for (; !isdigit(*(p - 1)); p--);
1391 *p = '\0';
1392 for (p--; isdigit(*(p - 1)); p--);
1393 outL = atol(p);
1394 if (outL <= 0) {
1395 paranoid_free(tmp);
1396 paranoid_free(command);
1397 return (-1);
1398 }
1399 if (strstr(tmp, "GB")) {
1400 outL = outL * 1024;
1401 }
1402 paranoid_free(tmp);
1403 paranoid_free(command);
1404 return (outL * 19 / 20);
1405#endif
1406}
1407#endif /* 0 */
1408
1409
1410
1411
1412
1413/**
1414 * Determine whether @p format is supported by the kernel. Uses /proc/filesystems
1415 * under Linux and @c lsvfs under FreeBSD.
1416 * @param format The format to test.
1417 * @return TRUE if the format is supported, FALSE if not.
1418 */
1419bool is_this_a_valid_disk_format(char *format)
1420{
1421 char *good_formats;
1422 char *command;
1423 char *format_sz;
1424
1425 FILE *pin;
1426 int retval;
1427 malloc_string(good_formats);
1428 malloc_string(command);
1429 malloc_string(format_sz);
1430
1431 assert_string_is_neither_NULL_nor_zerolength(format);
1432
1433 sprintf(format_sz, "%s ", format);
1434
1435#ifdef __FreeBSD__
1436 sprintf(command,
1437 "lsvfs | tr -s '\t' ' ' | grep -v Filesys | grep -v -- -- | cut -d' ' -f1 | tr -s '\n' ' '");
1438#else
1439 sprintf(command,
1440 "grep -v nodev /proc/filesystems | tr -s '\t' ' ' | cut -d' ' -f2 | tr -s '\n' ' '");
1441#endif
1442
1443 pin = popen(command, "r");
1444 if (!pin) {
1445 log_OS_error("Unable to read good formats");
1446 retval = 0;
1447 } else {
1448 strcpy(good_formats, " ");
1449 (void) fgets(good_formats + 1, MAX_STR_LEN, pin);
1450 if (pclose(pin)) {
1451 log_OS_error("Cannot pclose good formats");
1452 }
1453 strip_spaces(good_formats);
1454 strcat(good_formats, " swap lvm raid ntfs 7 "); // " ntfs 7 " -- um, cheating much? :)
1455 if (strstr(good_formats, format_sz)) {
1456 retval = 1;
1457 } else {
1458 retval = 0;
1459 }
1460 }
1461 paranoid_free(good_formats);
1462 paranoid_free(command);
1463 paranoid_free(format_sz);
1464 return (retval);
1465}
1466
1467
1468/** @def SWAPLIST_COMMAND The command to list the swap files/partitions in use. */
1469
1470/**
1471 * Determine whether @p device_raw is currently mounted.
1472 * @param device_raw The device to check.
1473 * @return TRUE if it's mounted, FALSE if not.
1474 */
1475bool is_this_device_mounted(char *device_raw)
1476{
1477
1478 /*@ pointers **************************************************** */
1479 FILE *fin;
1480
1481 /*@ buffers ***************************************************** */
1482 char *incoming;
1483 char *device_with_tab;
1484 char *device_with_space;
1485 char *tmp;
1486 int retval = 0;
1487
1488#ifdef __FreeBSD__
1489#define SWAPLIST_COMMAND "swapinfo"
1490#else
1491#define SWAPLIST_COMMAND "cat /proc/swaps"
1492#endif
1493
1494 /*@ end vars **************************************************** */
1495
1496 malloc_string(incoming);
1497 malloc_string(device_with_tab);
1498 malloc_string(device_with_space);
1499 malloc_string(tmp);
1500 assert(device_raw != NULL);
1501// assert_string_is_neither_NULL_nor_zerolength(device_raw);
1502 if (device_raw[0] != '/' && !strstr(device_raw, ":/")) {
1503 log_msg(1, "%s needs to have a '/' prefixed - I'll do it",
1504 device_raw);
1505 sprintf(tmp, "/%s", device_raw);
1506 } else {
1507 strcpy(tmp, device_raw);
1508 }
1509 log_msg(1, "Is %s mounted?", tmp);
1510 if (!strcmp(tmp, "/proc") || !strcmp(tmp, "proc")) {
1511 log_msg(1,
1512 "I don't know how the heck /proc made it into the mountlist. I'll ignore it.");
1513 return (0);
1514 }
1515 sprintf(device_with_tab, "%s\t", tmp);
1516 sprintf(device_with_space, "%s ", tmp);
1517
1518 if (!(fin = popen("mount", "r"))) {
1519 log_OS_error("Cannot popen 'mount'");
1520 return (FALSE);
1521 }
1522 for (fgets(incoming, MAX_STR_LEN - 1, fin); !feof(fin);
1523 fgets(incoming, MAX_STR_LEN - 1, fin)) {
1524 if (strstr(incoming, device_with_space) //> incoming
1525 || strstr(incoming, device_with_tab)) // > incoming)
1526 {
1527 paranoid_pclose(fin);
1528 retval = 1;
1529 goto end_of_func;
1530 }
1531 }
1532 paranoid_pclose(fin);
1533 sprintf(tmp, "%s | grep -E \"^%s\" > /dev/null 2> /dev/null",
1534 SWAPLIST_COMMAND, device_with_space);
1535 log_msg(4, "tmp (command) = '%s'", tmp);
1536 if (!system(tmp)) {
1537 retval = 1;
1538 goto end_of_func;
1539 }
1540 end_of_func:
1541 paranoid_free(incoming);
1542 paranoid_free(device_with_tab);
1543 paranoid_free(device_with_space);
1544 paranoid_free(tmp);
1545 return (retval);
1546}
1547
1548#ifdef __FreeBSD__
1549// CODE IS FREEBSD-SPECIFIC
1550/**
1551 * Create a loopback device for specified @p fname.
1552 * @param fname The file to associate with a device.
1553 * @return /dev entry for the device, or NULL if it couldn't be allocated.
1554 */
1555char *make_vn(char *fname)
1556{
1557 char *device = (char *) malloc(MAX_STR_LEN);
1558 char *mddevice = (char *) malloc(32);
1559 char command[MAX_STR_LEN];
1560 int vndev = 2;
1561 if (atoi
1562 (call_program_and_get_last_line_of_output
1563 ("/sbin/sysctl -n kern.osreldate")) < 500000) {
1564 do {
1565 sprintf(mddevice, "vn%ic", vndev++);
1566 sprintf(command, "vnconfig %s %s", mddevice, fname);
1567 if (vndev > 10) {
1568 return NULL;
1569 }
1570 }
1571 while (system(command));
1572 } else {
1573 sprintf(command, "mdconfig -a -t vnode -f %s", fname);
1574 mddevice = call_program_and_get_last_line_of_output(command);
1575 if (!strstr(mddevice, "md")) {
1576 return NULL;
1577 }
1578 }
1579 sprintf(device, "/dev/%s", mddevice);
1580 return device;
1581}
1582
1583
1584
1585// CODE IS FREEBSD-SPECIFIC
1586/**
1587 * Deallocate specified @p dname.
1588 * This should be called when you are done with the device created by make_vn(),
1589 * so the system does not run out of @c vn devices.
1590 * @param dname The device to deallocate.
1591 * @return 0 for success, nonzero for failure.
1592 */
1593int kick_vn(char *dname)
1594{
1595 char command[MAX_STR_LEN];
1596
1597 if (strncmp(dname, "/dev/", 5) == 0) {
1598 dname += 5;
1599 }
1600
1601 if (atoi
1602 (call_program_and_get_last_line_of_output
1603 ("/sbin/sysctl -n kern.osreldate")) < 500000) {
1604 sprintf(command, "vnconfig -d %s", dname);
1605 return system(command);
1606 } else {
1607 sprintf(command, "mdconfig -d -u %s", dname);
1608 return system(command);
1609 }
1610 /*NOTREACHED*/ return 255;
1611}
1612#endif
1613
1614
1615/**
1616 * Mount the CD-ROM at @p mountpoint.
1617 * @param device The device (or file if g_ISO_restore_mode) to mount.
1618 * @param mountpoint The place to mount it.
1619 * @return 0 for success, nonzero for failure.
1620 */
1621int mount_USB_here(char *device, char *mountpoint)
1622{
1623 /*@ buffer ****************************************************** */
1624 char *command;
1625 char *dev;
1626 int retval;
1627
1628 malloc_string(command);
1629 malloc_string(dev);
1630 assert_string_is_neither_NULL_nor_zerolength(device);
1631 assert_string_is_neither_NULL_nor_zerolength(mountpoint);
1632
1633 make_hole_for_dir(mountpoint);
1634 if (isdigit(device[0])) {
1635 return(1);
1636 } else {
1637 strcpy(dev, device);
1638 }
1639 log_msg(4, "(mount_USB_here --- device=%s, mountpoint=%s", device,
1640 mountpoint);
1641
1642#ifdef __FreeBSD__
1643 sprintf(command, "mount_vfat %s %s 2>> %s",
1644 device, mountpoint, MONDO_LOGFILE);
1645
1646#else
1647 sprintf(command, "mount %s -t vfat %s 2>> %s",
1648 device, mountpoint, MONDO_LOGFILE);
1649#endif
1650
1651 log_msg(4, command);
1652 retval = system(command);
1653 log_msg(1, "system(%s) returned %d", command, retval);
1654
1655 paranoid_free(command);
1656 paranoid_free(dev);
1657 return (retval);
1658}
1659
1660/**
1661 * Mount the CD-ROM at @p mountpoint.
1662 * @param device The device (or file if g_ISO_restore_mode) to mount.
1663 * @param mountpoint The place to mount it.
1664 * @return 0 for success, nonzero for failure.
1665 */
1666int mount_CDROM_here(char *device, char *mountpoint)
1667{
1668 /*@ buffer ****************************************************** */
1669 char *command;
1670 char *dev;
1671 char *options;
1672 int retval;
1673
1674 malloc_string(command);
1675 malloc_string(dev);
1676 malloc_string(options);
1677 assert_string_is_neither_NULL_nor_zerolength(device);
1678 assert_string_is_neither_NULL_nor_zerolength(mountpoint);
1679
1680 make_hole_for_dir(mountpoint);
1681 strcpy(options, "ro");
1682 if (isdigit(device[0])) {
1683 find_cdrom_device(device, FALSE);
1684 } else {
1685 strcpy(dev, device);
1686 }
1687 if (g_ISO_restore_mode) {
1688
1689#ifdef __FreeBSD__
1690 strcpy(dev, make_vn(device));
1691 if (!dev) {
1692 sprintf(command, "Unable to mount ISO (make_vn(%s) failed)",
1693 device);
1694 fatal_error(command);
1695 }
1696 strcpy(device, dev);
1697#else
1698 strcat(options, ",loop");
1699#endif
1700
1701 }
1702 log_msg(4, "(mount_CDROM_here --- device=%s, mountpoint=%s", device,
1703 mountpoint);
1704 /*@ end vars *************************************************** */
1705
1706#ifdef __FreeBSD__
1707 sprintf(command, "mount_cd9660 -r %s %s 2>> %s",
1708 device, mountpoint, MONDO_LOGFILE);
1709
1710#else
1711 sprintf(command, "mount %s -o %s -t iso9660 %s 2>> %s",
1712 device, options, mountpoint, MONDO_LOGFILE);
1713#endif
1714
1715 log_msg(4, command);
1716 if (strncmp(device, "/dev/", 5) == 0) {
1717 retract_CD_tray_and_defeat_autorun();
1718 }
1719 retval = system(command);
1720 log_msg(1, "system(%s) returned %d", command, retval);
1721
1722 paranoid_free(command);
1723 paranoid_free(dev);
1724 paranoid_free(options);
1725 return (retval);
1726}
1727
1728
1729
1730
1731
1732
1733/**
1734 * Ask the user for CD number @p cd_number_i_want.
1735 * Sets g_current_media_number once the correct CD is inserted.
1736 * @param bkpinfo The backup information structure. Fields used:
1737 * - @c bkpinfo->backup_media_type
1738 * - @c bkpinfo->prefix
1739 * - @c bkpinfo->isodir
1740 * - @c bkpinfo->media_device
1741 * - @c bkpinfo->please_dont_eject_when_restoring
1742 * @param cd_number_i_want The CD number to ask for.
1743 */
1744void
1745insist_on_this_cd_number(int cd_number_i_want)
1746{
1747
1748 /*@ int ************************************************************* */
1749 int res = 0;
1750
1751
1752 /*@ buffers ********************************************************* */
1753 char *tmp;
1754 char *request;
1755
1756 assert(bkpinfo != NULL);
1757 assert(cd_number_i_want > 0);
1758
1759// log_msg(3, "Insisting on CD number %d", cd_number_i_want);
1760
1761 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1762 log_msg(3,
1763 "No need to insist_on_this_cd_number when the backup type isn't CD-R(W) or NFS or ISO");
1764 return;
1765 }
1766 malloc_string(tmp);
1767 malloc_string(request);
1768 sprintf(tmp, "mkdir -p " MNT_CDROM);
1769 run_program_and_log_output(tmp, 5);
1770 if (g_ISO_restore_mode || bkpinfo->backup_media_type == iso
1771 || bkpinfo->backup_media_type == nfs) {
1772 log_msg(3, "Remounting CD");
1773 g_ISO_restore_mode = TRUE;
1774// FIXME --- I'm tempted to do something about this...
1775// Why unmount and remount again and again?
1776 if (is_this_device_mounted(MNT_CDROM)) {
1777 run_program_and_log_output("umount " MNT_CDROM, 5);
1778 }
1779 sprintf(tmp, "mkdir -p %s/isodir &> /dev/null", bkpinfo->tmpdir);
1780 system(tmp);
1781 sprintf(tmp, "%s/%s/%s-%d.iso", bkpinfo->isodir,
1782 bkpinfo->nfs_remote_dir, bkpinfo->prefix,
1783 cd_number_i_want);
1784 if (!does_file_exist(tmp)) {
1785 sprintf(tmp, "%s/isodir/%s/%s-%d.iso", bkpinfo->tmpdir,
1786 bkpinfo->nfs_remote_dir, bkpinfo->prefix,
1787 cd_number_i_want);
1788 if (does_file_exist(tmp)) {
1789 log_msg(1,
1790 "FIXME - hacking bkpinfo->isodir from '%s' to %s/isodir",
1791 bkpinfo->isodir, bkpinfo->tmpdir);
1792 sprintf(bkpinfo->isodir, "%s/isodir", bkpinfo->tmpdir);
1793 }
1794 }
1795 log_msg(3, "Mounting %s at %s", tmp, MNT_CDROM);
1796 if (mount_CDROM_here(tmp, MNT_CDROM)) {
1797 fatal_error("Mommy!");
1798 }
1799// g_current_media_number = cd_number_i_want;
1800// return;
1801 }
1802 if ((res = what_number_cd_is_this()) != cd_number_i_want) {
1803 log_msg(3, "Currently, we hold %d but we want %d", res,
1804 cd_number_i_want);
1805 sprintf(tmp, "Insisting on %s #%d",
1806 media_descriptor_string(bkpinfo->backup_media_type),
1807 cd_number_i_want);
1808 sprintf(request, "Please insert %s #%d and press Enter.",
1809 media_descriptor_string(bkpinfo->backup_media_type),
1810 cd_number_i_want);
1811 log_msg(3, tmp);
1812 while (what_number_cd_is_this() != cd_number_i_want) {
1813 paranoid_system("sync");
1814 if (is_this_device_mounted(MNT_CDROM)) {
1815 res =
1816 run_program_and_log_output("umount " MNT_CDROM, FALSE);
1817 } else {
1818 res = 0;
1819 }
1820 if (res) {
1821 log_to_screen("WARNING - failed to unmount CD-ROM drive");
1822 }
1823 if (!bkpinfo->please_dont_eject) {
1824 res = eject_device(bkpinfo->media_device);
1825 } else {
1826 res = 0;
1827 }
1828 if (res) {
1829 log_to_screen("WARNING - failed to eject CD-ROM disk");
1830 }
1831 popup_and_OK(request);
1832 if (!bkpinfo->please_dont_eject) {
1833 inject_device(bkpinfo->media_device);
1834 }
1835 paranoid_system("sync");
1836 }
1837 log_msg(1, "Thankyou. Proceeding...");
1838 g_current_media_number = cd_number_i_want;
1839 }
1840 paranoid_free(tmp);
1841 paranoid_free(request);
1842}
1843
1844/* @} - end of deviceGroup */
1845
1846
1847
1848
1849
1850
1851/**
1852 * Ask user for details of backup/restore information.
1853 * Called when @c mondoarchive doesn't get any parameters.
1854 * @param bkpinfo The backup information structure to fill out with the user's data.
1855 * @param archiving_to_media TRUE if archiving, FALSE if restoring.
1856 * @return 0, always.
1857 * @bug No point of `int' return value.
1858 * @ingroup archiveGroup
1859 */
1860int interactively_obtain_media_parameters_from_user(bool archiving_to_media)
1861// archiving_to_media is TRUE if I'm being called by mondoarchive
1862// archiving_to_media is FALSE if I'm being called by mondorestore
1863{
1864 char *tmp;
1865 char *tmp1 = NULL;
1866 char *sz_size;
1867 char *command;
1868 char *comment;
1869 char *prompt;
1870 int i;
1871 FILE *fin;
1872
1873 malloc_string(tmp);
1874 malloc_string(sz_size);
1875 malloc_string(command);
1876 malloc_string(comment);
1877 malloc_string(prompt);
1878 assert(bkpinfo != NULL);
1879 sz_size[0] = '\0';
1880 bkpinfo->nonbootable_backup = FALSE;
1881
1882// Tape, CD, NFS, ...?
1883 srandom(getpid());
1884 bkpinfo->backup_media_type =
1885 (g_restoring_live_from_cd) ? cdr :
1886 which_backup_media_type(bkpinfo->restore_data);
1887 if (bkpinfo->backup_media_type == none) {
1888 log_to_screen("User has chosen not to backup the PC");
1889 finish(1);
1890 }
1891 if (bkpinfo->backup_media_type == tape && bkpinfo->restore_data) {
1892 popup_and_OK("Please remove CD/floppy from drive(s)");
1893 }
1894 log_msg(3, "media type = %s",
1895 bkptype_to_string(bkpinfo->backup_media_type));
1896 if (archiving_to_media) {
1897 sensibly_set_tmpdir_and_scratchdir();
1898 }
1899 bkpinfo->cdrw_speed = (bkpinfo->backup_media_type == cdstream) ? 2 : 4;
1900 bkpinfo->compression_level =
1901 (bkpinfo->backup_media_type == cdstream) ? 1 : 5;
1902 bkpinfo->use_lzo =
1903 (bkpinfo->backup_media_type == cdstream) ? TRUE : FALSE;
1904 mvaddstr_and_log_it(2, 0, " ");
1905
1906// Find device's /dev (or SCSI) entry
1907 switch (bkpinfo->backup_media_type) {
1908 case cdr:
1909 case cdrw:
1910 case dvd:
1911 case usb:
1912 /* Never try to eject a USB device */
1913 if (bkpinfo->backup_media_type == usb) {
1914 bkpinfo->please_dont_eject = TRUE;
1915 }
1916 if (archiving_to_media) {
1917 if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) {
1918 if (ask_me_yes_or_no
1919 ("Is your computer a laptop, or does the CD writer incorporate BurnProof technology?"))
1920 {
1921 bkpinfo->manual_cd_tray = TRUE;
1922 }
1923 }
1924 if ((bkpinfo->compression_level =
1925 which_compression_level()) == -1) {
1926 log_to_screen("User has chosen not to backup the PC");
1927 finish(1);
1928 }
1929 sprintf(comment, "What speed is your %s (re)writer?",
1930 media_descriptor_string(bkpinfo->backup_media_type));
1931 if (bkpinfo->backup_media_type == dvd) {
1932 find_dvd_device(bkpinfo->media_device, FALSE);
1933 strcpy(tmp, "1");
1934 sprintf(sz_size, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4482 MB
1935 log_msg(1, "Setting to DVD defaults");
1936 } else {
1937 strcpy(bkpinfo->media_device, VANILLA_SCSI_CDROM);
1938 strcpy(tmp, "4");
1939 strcpy(sz_size, "650");
1940 log_msg(1, "Setting to CD defaults");
1941 }
1942 if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) {
1943 if (!popup_and_get_string("Speed", comment, tmp, 4)) {
1944 log_to_screen("User has chosen not to backup the PC");
1945 finish(1);
1946 }
1947 }
1948 bkpinfo->cdrw_speed = atoi(tmp); // if DVD then this shouldn't ever be used anyway :)
1949 sprintf(comment,
1950 "How much data (in Megabytes) will each %s store?",
1951 media_descriptor_string(bkpinfo->backup_media_type));
1952 if (!popup_and_get_string("Size", comment, sz_size, 5)) {
1953 log_to_screen("User has chosen not to backup the PC");
1954 finish(1);
1955 }
1956 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1957 bkpinfo->media_size[i] = atoi(sz_size);
1958 }
1959 if (bkpinfo->media_size[0] <= 0) {
1960 log_to_screen("User has chosen not to backup the PC");
1961 finish(1);
1962 }
1963 }
1964 /* No break because we continue even for usb */
1965 case cdstream:
1966 if ((bkpinfo->disaster_recovery) && (bkpinfo->backup_media_type != usb)) {
1967 strcpy(bkpinfo->media_device, "/dev/cdrom");
1968 log_msg(2, "CD-ROM device assumed to be at %s",
1969 bkpinfo->media_device);
1970 } else if ((bkpinfo->restore_data && (bkpinfo->backup_media_type != usb))
1971 || bkpinfo->backup_media_type == dvd) {
1972 if (!bkpinfo->media_device[0]) {
1973 strcpy(bkpinfo->media_device, "/dev/cdrom");
1974 } // just for the heck of it :)
1975 log_msg(1, "bkpinfo->media_device = %s",
1976 bkpinfo->media_device);
1977 if (bkpinfo->backup_media_type == dvd
1978 || find_cdrom_device(bkpinfo->media_device, FALSE)) {
1979 log_msg(1, "bkpinfo->media_device = %s",
1980 bkpinfo->media_device);
1981 sprintf(comment,
1982 "Please specify your %s drive's /dev entry",
1983 media_descriptor_string(bkpinfo->backup_media_type));
1984 if (!popup_and_get_string
1985 ("Device?", comment, bkpinfo->media_device,
1986 MAX_STR_LEN / 4)) {
1987 log_to_screen("User has chosen not to backup the PC");
1988 finish(1);
1989 }
1990 }
1991 log_msg(2, "%s device found at %s",
1992 media_descriptor_string(bkpinfo->backup_media_type),
1993 bkpinfo->media_device);
1994 } else {
1995 if ((find_cdrw_device(bkpinfo->media_device)) && (bkpinfo->backup_media_type != usb)) {
1996 bkpinfo->media_device[0] = '\0';
1997 }
1998 if (bkpinfo->media_device[0]) {
1999 if (bkpinfo->backup_media_type == usb) {
2000 sprintf(tmp,
2001 "I think your %s media corresponds to %s. Is this correct?",
2002 media_descriptor_string(bkpinfo->backup_media_type),
2003 bkpinfo->media_device);
2004 } else {
2005 sprintf(tmp,
2006 "I think I've found your %s burner at SCSI node %s. Is this correct? (Say no if you have an IDE burner and you are running a 2.6 kernel. You will then be prompted for further details.)",
2007 media_descriptor_string(bkpinfo->backup_media_type),
2008 bkpinfo->media_device);
2009 }
2010 if (!ask_me_yes_or_no(tmp)) {
2011 bkpinfo->media_device[0] = '\0';
2012 }
2013 }
2014 if (!bkpinfo->media_device[0]) {
2015 if (bkpinfo->backup_media_type == usb) {
2016 i = popup_and_get_string("/dev entry?",
2017 "What is the /dev entry of your USB Disk/Key, please?",
2018 bkpinfo->media_device,
2019 MAX_STR_LEN / 4);
2020 } else {
2021 if (g_kernel_version < 2.6) {
2022 i = popup_and_get_string("Device node?",
2023 "What is the SCSI node of your CD (re)writer, please?",
2024 bkpinfo->media_device,
2025 MAX_STR_LEN / 4);
2026 } else {
2027 i = popup_and_get_string("/dev entry?",
2028 "What is the /dev entry of your CD (re)writer, please?",
2029 bkpinfo->media_device,
2030 MAX_STR_LEN / 4);
2031 }
2032 }
2033 if (!i) {
2034 log_to_screen("User has chosen not to backup the PC");
2035 finish(1);
2036 }
2037 }
2038 }
2039 if (bkpinfo->backup_media_type == cdstream) {
2040 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
2041 bkpinfo->media_size[i] = 650;
2042 }
2043 }
2044 break;
2045 case udev:
2046 if (!ask_me_yes_or_no
2047 ("This option is for advanced users only. Are you sure?")) {
2048 log_to_screen("User has chosen not to backup the PC");
2049 finish(1);
2050 }
2051 case tape:
2052
2053 if (find_tape_device_and_size(bkpinfo->media_device, sz_size)) {
2054 log_msg(3, "Ok, using vanilla scsi tape.");
2055 strcpy(bkpinfo->media_device, VANILLA_SCSI_TAPE);
2056 if ((fin = fopen(bkpinfo->media_device, "r"))) {
2057 paranoid_fclose(fin);
2058 } else {
2059 strcpy(bkpinfo->media_device, "/dev/osst0");
2060 }
2061 }
2062 if (bkpinfo->media_device[0]) {
2063 if ((fin = fopen(bkpinfo->media_device, "r"))) {
2064 paranoid_fclose(fin);
2065 } else {
2066 if (does_file_exist("/tmp/mondo-restore.cfg")) {
2067 read_cfg_var("/tmp/mondo-restore.cfg", "media-dev",
2068 bkpinfo->media_device);
2069 }
2070 }
2071 sprintf(tmp,
2072 "I think I've found your tape streamer at %s; am I right on the money?",
2073 bkpinfo->media_device);
2074 }
2075 if (bkpinfo->media_device[0]) {
2076 sprintf(tmp,
2077 "I think I've found your tape streamer at %s; am I right on the money?",
2078 bkpinfo->media_device);
2079 if (!ask_me_yes_or_no(tmp)) {
2080 bkpinfo->media_device[0] = '\0';
2081 }
2082 }
2083 if (!bkpinfo->media_device[0]) {
2084 if (!popup_and_get_string
2085 ("Device name?",
2086 "What is the /dev entry of your tape streamer?",
2087 bkpinfo->media_device, MAX_STR_LEN / 4)) {
2088 log_to_screen("User has chosen not to backup the PC");
2089 finish(1);
2090 }
2091 }
2092 sprintf(tmp, "ls -l %s", bkpinfo->media_device);
2093 if (run_program_and_log_output(tmp, FALSE)) {
2094 log_to_screen("User has not specified a valid /dev entry");
2095 finish(1);
2096 }
2097 log_msg(4, "sz_size = %s", sz_size);
2098 sz_size[0] = '\0';
2099/*
2100 if ((size_sz[0]=='\0' || atol(size_sz)==0) && archiving_to_media)
2101 {
2102 if (!popup_and_get_string("Tape size", "How much COMPRESSED data will one of your tape cartridges hold? (e.g. 4GB for 4 gigabytes)", size_sz, 16))
2103 { log_to_screen("User has chosen not to backup the PC"); finish(1); }
2104 }
2105*/
2106 if (sz_size[0] == '\0') {
2107 bkpinfo->media_size[0] = 0;
2108 } else {
2109 bkpinfo->media_size[0] =
2110 friendly_sizestr_to_sizelong(sz_size) / 2 - 50;
2111 }
2112 log_msg(4, "media_size[0] = %ld", bkpinfo->media_size[0]);
2113 if (bkpinfo->media_size[0] <= 0) {
2114 bkpinfo->media_size[0] = 0;
2115 }
2116 for (i = 1; i <= MAX_NOOF_MEDIA; i++) {
2117 bkpinfo->media_size[i] = bkpinfo->media_size[0];
2118 }
2119 if (archiving_to_media) {
2120 if ((bkpinfo->compression_level =
2121 which_compression_level()) == -1) {
2122 log_to_screen("User has chosen not to backup the PC");
2123 finish(1);
2124 }
2125 }
2126 break;
2127
2128
2129
2130 case nfs:
2131 if (!bkpinfo->nfs_mount[0]) {
2132 strcpy(bkpinfo->nfs_mount,
2133 call_program_and_get_last_line_of_output
2134 ("mount | grep \":\" | cut -d' ' -f1 | head -n1"));
2135 }
2136#ifdef __FreeBSD__
2137 if (TRUE)
2138#else
2139 if (!bkpinfo->disaster_recovery)
2140#endif
2141 {
2142 if (!popup_and_get_string
2143 ("NFS dir.",
2144 "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.)",
2145 bkpinfo->nfs_mount, MAX_STR_LEN / 4)) {
2146 log_to_screen("User has chosen not to backup the PC");
2147 finish(1);
2148 }
2149 if (!bkpinfo->restore_data) {
2150 if ((bkpinfo->compression_level =
2151 which_compression_level()) == -1) {
2152 log_to_screen("User has chosen not to backup the PC");
2153 finish(1);
2154 }
2155 }
2156 // check whether already mounted - we better remove
2157 // surrounding spaces and trailing '/' for this
2158 strip_spaces(bkpinfo->nfs_mount);
2159 if (bkpinfo->nfs_mount[strlen(bkpinfo->nfs_mount) - 1] == '/')
2160 bkpinfo->nfs_mount[strlen(bkpinfo->nfs_mount) - 1] = '\0';
2161 sprintf(command, "mount | grep \"%s \" | cut -d' ' -f3",
2162 bkpinfo->nfs_mount);
2163 strcpy(bkpinfo->isodir,
2164 call_program_and_get_last_line_of_output(command));
2165
2166 sprintf(comment,
2167 "How much data (in Megabytes) will each media store?");
2168 if (!popup_and_get_string("Size", comment, sz_size, 5)) {
2169 log_to_screen("User has chosen not to backup the PC");
2170 finish(1);
2171 }
2172 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
2173 bkpinfo->media_size[i] = atoi(sz_size);
2174 }
2175 if (bkpinfo->media_size[0] <= 0) {
2176 log_to_screen("User has chosen not to backup the PC");
2177 finish(1);
2178 }
2179 }
2180 if (bkpinfo->disaster_recovery) {
2181 sprintf(command ,"umount %s/isodir 2> /dev/null", bkpinfo->tmpdir);
2182 system(command);
2183 if (!popup_and_get_string
2184 ("NFS share", "Which remote NFS share should I mount?",
2185 bkpinfo->nfs_mount, MAX_STR_LEN)) {
2186 log_to_screen("User has chosen not to backup the PC");
2187 finish(1);
2188 }
2189 }
2190 if (!is_this_device_mounted(bkpinfo->nfs_mount)) {
2191 sprintf(bkpinfo->isodir, "%s/nfsdir", bkpinfo->tmpdir);
2192 sprintf(command, "mkdir -p %s", bkpinfo->isodir);
2193 run_program_and_log_output(command, 5);
2194 sprintf(tmp, "mount -t nfs -o nolock %s %s", bkpinfo->nfs_mount,
2195 bkpinfo->isodir);
2196 run_program_and_log_output(tmp, 3);
2197 malloc_string(g_selfmounted_isodir);
2198 strcpy(g_selfmounted_isodir, bkpinfo->isodir);
2199 }
2200 if (!is_this_device_mounted(bkpinfo->nfs_mount)) {
2201 popup_and_OK
2202 ("Please mount that partition before you try to backup to or restore from it.");
2203 finish(1);
2204 }
2205 strcpy(tmp, bkpinfo->nfs_remote_dir);
2206 if (!popup_and_get_string
2207 ("Directory", "Which directory within that mountpoint?", tmp,
2208 MAX_STR_LEN)) {
2209 log_to_screen("User has chosen not to backup the PC");
2210 finish(1);
2211 }
2212 strcpy(bkpinfo->nfs_remote_dir, tmp);
2213 // check whether writable - we better remove surrounding spaces for this
2214 strip_spaces(bkpinfo->nfs_remote_dir);
2215 asprintf(&tmp1,"%s/%s/.dummy.txt", bkpinfo->isodir,bkpinfo->nfs_remote_dir);
2216 sprintf(command, "echo hi > %s", tmp1);
2217 while (run_program_and_log_output(command, FALSE)) {
2218 strcpy(tmp, bkpinfo->nfs_remote_dir);
2219 sprintf(prompt,
2220 "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.",
2221 bkpinfo->nfs_remote_dir, bkpinfo->isodir);
2222 if (!popup_and_get_string
2223 ("Directory", prompt, tmp, MAX_STR_LEN)) {
2224 log_to_screen("User has chosen not to backup the PC");
2225 finish(1);
2226 }
2227 strcpy(bkpinfo->nfs_remote_dir, tmp);
2228 // check whether writable - we better remove surrounding space s for this
2229 strip_spaces(bkpinfo->nfs_remote_dir);
2230 paranoid_free(tmp1);
2231 asprintf(&tmp1,"%s/%s/.dummy.txt", bkpinfo->isodir,bkpinfo->nfs_remote_dir);
2232 sprintf(command, "echo hi > %s", tmp1);
2233 }
2234 unlink(tmp1);
2235 paranoid_free(tmp1);
2236
2237 if (!popup_and_get_string
2238 ("Prefix.",
2239 "Please enter the prefix that will be prepended to your ISO filename. Example: machine1 to obtain machine1-[1-9]*.iso files",
2240 bkpinfo->prefix, MAX_STR_LEN / 4)) {
2241 log_to_screen("User has chosen not to backup the PC");
2242 finish(1);
2243 }
2244 log_msg(3, "prefix set to %s", bkpinfo->prefix);
2245
2246 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
2247 bkpinfo->media_size[i] = 650;
2248 }
2249 log_msg(3, "Just set nfs_remote_dir to %s",
2250 bkpinfo->nfs_remote_dir);
2251 log_msg(3, "isodir is still %s", bkpinfo->isodir);
2252 break;
2253
2254 case iso:
2255 if (!bkpinfo->disaster_recovery) {
2256 if (!popup_and_get_string
2257 ("Storage dir.",
2258 "Please enter the full path name to the directory for your ISO images. Example: /mnt/raid0_0",
2259 bkpinfo->isodir, MAX_STR_LEN / 4)) {
2260 log_to_screen("User has chosen not to backup the PC");
2261 finish(1);
2262 }
2263 if (archiving_to_media) {
2264 if ((bkpinfo->compression_level =
2265 which_compression_level()) == -1) {
2266 log_to_screen("User has chosen not to backup the PC");
2267 finish(1);
2268 }
2269 if (!popup_and_get_string
2270 ("ISO size.",
2271 "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.",
2272 sz_size, 16)) {
2273 log_to_screen("User has chosen not to backup the PC");
2274 finish(1);
2275 }
2276 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
2277 bkpinfo->media_size[i] = atoi(sz_size);
2278 }
2279 } else {
2280 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
2281 bkpinfo->media_size[i] = 650;
2282 }
2283 }
2284 }
2285 if (!popup_and_get_string
2286 ("Prefix.",
2287 "Please enter the prefix that will be prepended to your ISO filename. Example: machine1 to obtain machine1-[1-9]*.iso files",
2288 bkpinfo->prefix, MAX_STR_LEN / 4)) {
2289 log_to_screen("User has chosen not to backup the PC");
2290 finish(1);
2291 }
2292 log_msg(3, "prefix set to %s", bkpinfo->prefix);
2293 break;
2294 default:
2295 fatal_error
2296 ("I, Mojo Jojo, shall defeat those pesky Powerpuff Girls!");
2297 }
2298
2299 if (archiving_to_media) {
2300
2301#ifdef __FreeBSD__
2302 strcpy(bkpinfo->boot_device,
2303 call_program_and_get_last_line_of_output
2304 ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/\\([0-9]\\).*/\\1/'"));
2305#else
2306 strcpy(bkpinfo->boot_device,
2307 call_program_and_get_last_line_of_output
2308 ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'"));
2309#endif
2310 i = which_boot_loader(bkpinfo->boot_device);
2311 if (i == 'U') // unknown
2312 {
2313
2314#ifdef __FreeBSD__
2315 if (!popup_and_get_string
2316 ("Boot device",
2317 "What is your boot device? (e.g. /dev/ad0)",
2318 bkpinfo->boot_device, MAX_STR_LEN / 4)) {
2319 log_to_screen("User has chosen not to backup the PC");
2320 finish(1);
2321 }
2322 i = which_boot_loader(bkpinfo->boot_device);
2323#else
2324 if (!popup_and_get_string
2325 ("Boot device",
2326 "What is your boot device? (e.g. /dev/hda)",
2327 bkpinfo->boot_device, MAX_STR_LEN / 4)) {
2328 log_to_screen("User has chosen not to backup the PC");
2329 finish(1);
2330 }
2331 if (does_string_exist_in_boot_block
2332 (bkpinfo->boot_device, "LILO")) {
2333 i = 'L';
2334 } else
2335 if (does_string_exist_in_boot_block
2336 (bkpinfo->boot_device, "ELILO")) {
2337 i = 'E';
2338 } else
2339 if (does_string_exist_in_boot_block
2340 (bkpinfo->boot_device, "GRUB")) {
2341 i = 'G';
2342 } else {
2343 i = 'U';
2344 }
2345#endif
2346 if (i == 'U') {
2347 if (ask_me_yes_or_no
2348 ("Unidentified boot loader. Shall I restore it byte-for-byte at restore time and hope for the best?"))
2349 {
2350 i = 'R'; // raw
2351 } else {
2352 log_to_screen
2353 ("I cannot find your boot loader. Please run mondoarchive with parameters.");
2354 finish(1);
2355 }
2356 }
2357 }
2358 bkpinfo->boot_loader = i;
2359 strcpy(bkpinfo->include_paths, "/");
2360 if (!popup_and_get_string
2361 ("Backup paths",
2362 "Please enter paths which you want me to backup. The default is '/' (i.e. everything).",
2363 bkpinfo->include_paths, MAX_STR_LEN)) {
2364 log_to_screen("User has chosen not to backup the PC");
2365 finish(1);
2366 }
2367 strcpy(tmp, list_of_NFS_mounts_only());
2368 if (strlen(tmp) > 2) {
2369 if (bkpinfo->exclude_paths[0]) {
2370 strcat(bkpinfo->exclude_paths, " ");
2371 }
2372 strncpy(bkpinfo->exclude_paths, tmp, MAX_STR_LEN);
2373 }
2374// NTFS
2375 strcpy(tmp,
2376 call_program_and_get_last_line_of_output
2377 ("parted2fdisk -l | grep -i ntfs | awk '{ print $1};' | tr -s '\\n' ' ' | awk '{ print $0};'"));
2378 if (strlen(tmp) > 2) {
2379 if (!popup_and_get_string
2380 ("NTFS partitions",
2381 "Please enter/confirm the NTFS partitions you wish to backup as well.",
2382 tmp, MAX_STR_LEN / 4)) {
2383 log_to_screen("User has chosen not to backup the PC");
2384 finish(1);
2385 }
2386 strncpy(bkpinfo->image_devs, tmp, MAX_STR_LEN / 4);
2387 }
2388
2389
2390 if (!popup_and_get_string
2391 ("Exclude paths",
2392 "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.",
2393 bkpinfo->exclude_paths, (4*MAX_STR_LEN)-1)) {
2394 log_to_screen("User has chosen not to backup the PC");
2395 finish(1);
2396 }
2397// Interactive mode:
2398#ifdef __IA64__
2399 bkpinfo->make_cd_use_lilo = TRUE;
2400#else
2401 bkpinfo->make_cd_use_lilo = FALSE;
2402#endif
2403 bkpinfo->backup_data = TRUE;
2404 bkpinfo->verify_data =
2405 ask_me_yes_or_no
2406 ("Will you want to verify your backups after Mondo has created them?");
2407
2408#ifndef __FreeBSD__
2409 if (!ask_me_yes_or_no
2410 ("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."))
2411#endif
2412 {
2413 strcpy(bkpinfo->kernel_path, "FAILSAFE");
2414 }
2415
2416 if (!ask_me_yes_or_no
2417 ("Are you sure you want to proceed? Hit 'no' to abort.")) {
2418 log_to_screen("User has chosen not to backup the PC");
2419 finish(1);
2420 }
2421 } else {
2422 bkpinfo->restore_data = TRUE; // probably...
2423 }
2424
2425 if (bkpinfo->backup_media_type == iso
2426 || bkpinfo->backup_media_type == nfs) {
2427 g_ISO_restore_mode = TRUE;
2428 }
2429#ifdef __FreeSD__
2430// skip
2431#else
2432 if (bkpinfo->backup_media_type == nfs) {
2433 sprintf(tmp, "mount | grep \"%s\" | cut -d' ' -f3",
2434 bkpinfo->nfs_mount);
2435// strcpy(bkpinfo->isodir, call_program_and_get_last_line_of_output(tmp));
2436 log_msg(3, "I think the NFS mount is mounted at %s",
2437 bkpinfo->isodir);
2438 }
2439 log_it("isodir = %s", bkpinfo->isodir);
2440 log_it("nfs_mount = '%s'", bkpinfo->nfs_mount);
2441#endif
2442
2443 log_it("media device = %s", bkpinfo->media_device);
2444 log_it("media size = %ld", bkpinfo->media_size[1]);
2445 log_it("media type = %s",
2446 bkptype_to_string(bkpinfo->backup_media_type));
2447 log_it("prefix = %s", bkpinfo->prefix);
2448 log_it("compression = %ld", bkpinfo->compression_level);
2449 log_it("include_paths = '%s'", bkpinfo->include_paths);
2450 log_it("exclude_paths = '%s'", bkpinfo->exclude_paths);
2451 log_it("scratchdir = '%s'", bkpinfo->scratchdir);
2452 log_it("tmpdir = '%s'", bkpinfo->tmpdir);
2453 log_it("image_devs = '%s'", bkpinfo->image_devs);
2454 log_it("boot_device = '%s' (loader=%c)", bkpinfo->boot_device,
2455 bkpinfo->boot_loader);
2456 if (bkpinfo->media_size[0] < 0) {
2457 if (archiving_to_media) {
2458 fatal_error("Media size is less than zero.");
2459 } else {
2460 log_msg(2, "Warning - media size is less than zero.");
2461 bkpinfo->media_size[0] = 0;
2462 }
2463 }
2464 paranoid_free(tmp);
2465 paranoid_free(sz_size);
2466 paranoid_free(command);
2467 paranoid_free(comment);
2468 paranoid_free(prompt);
2469 return (0);
2470}
2471
2472
2473
2474
2475/**
2476 * @addtogroup utilityGroup
2477 * @{
2478 */
2479/**
2480 * Get a space-separated list of NFS devices and mounts.
2481 * @return The list created.
2482 * @note The return value points to static data that will be overwritten with each call.
2483 */
2484char *list_of_NFS_devices_and_mounts(void)
2485{
2486 char *exclude_these_devices;
2487 char *exclude_these_directories;
2488 static char result_sz[512];
2489
2490 malloc_string(exclude_these_devices);
2491 malloc_string(exclude_these_directories);
2492 strcpy(exclude_these_directories,
2493 call_program_and_get_last_line_of_output
2494 ("mount -t coda,ncpfs,nfs,smbfs,cifs,afs,ocfs,ocfs2,mvfs | tr -s '\t' ' ' | cut -d' ' -f3 | tr -s '\n' ' ' | awk '{print $0;}'"));
2495 strcpy(exclude_these_devices,
2496 call_program_and_get_last_line_of_output
2497 ("tr -s '\t' ' ' < /etc/fstab | grep -E '( (coda|ncpfs|nfs|smbfs|cifs|afs|ocfs|ocfs2|mvfs) )' | cut -d' ' -f1 | tr -s '\n' ' ' | awk '{print $0;}'"));
2498 sprintf(result_sz, "%s %s", exclude_these_directories,
2499 exclude_these_devices);
2500 paranoid_free(exclude_these_devices);
2501 paranoid_free(exclude_these_directories);
2502 return (result_sz);
2503}
2504
2505
2506
2507
2508/**
2509 * Get a space-separated list of NFS mounts.
2510 * @return The list created.
2511 * @note The return value points to static data that will be overwritten with each call.
2512 * @bug Even though we only want the mounts, the devices are still checked.
2513 */
2514char *list_of_NFS_mounts_only(void)
2515{
2516 char *exclude_these_devices;
2517 char *exclude_these_directories;
2518 static char result_sz[512];
2519
2520 malloc_string(exclude_these_devices);
2521 malloc_string(exclude_these_directories);
2522 strcpy(exclude_these_directories,
2523 call_program_and_get_last_line_of_output
2524 ("mount -t coda,ncpfs,nfs,smbfs,cifs,afs,ocfs,ocfs2,mvfs | tr -s '\t' ' ' | cut -d' ' -f3 | tr -s '\n' ' ' | awk '{print $0;}'"));
2525 strcpy(exclude_these_devices,
2526 call_program_and_get_last_line_of_output
2527 ("tr -s '\t' ' ' < /etc/fstab | grep -E '( (coda|ncpfs|nfs|smbfs|cifs|afs|ocfs|ocfs2|mvfs) )' | cut -d' ' -f1 | tr -s '\n' ' ' | awk '{print $0;}'"));
2528 sprintf(result_sz, "%s", exclude_these_directories);
2529 paranoid_free(exclude_these_devices);
2530 paranoid_free(exclude_these_directories);
2531 return (result_sz);
2532}
2533
2534/* @} - end of utilityGroup */
2535
2536
2537
2538
2539
2540/**
2541 * Create a randomly-named FIFO. The format is @p stub "." [random] [random] where
2542 * [random] is a random number between 1 and 32767.
2543 * @param store_name_here Where to store the new filename.
2544 * @param stub A random number will be appended to this to make the FIFO's name.
2545 * @ingroup deviceGroup
2546 */
2547void make_fifo(char *store_name_here, char *stub)
2548{
2549 char *tmp;
2550
2551 malloc_string(tmp);
2552 assert_string_is_neither_NULL_nor_zerolength(stub);
2553
2554 sprintf(store_name_here, "%s%d%d", stub, (int) (random() % 32768),
2555 (int) (random() % 32768));
2556 make_hole_for_file(store_name_here);
2557 mkfifo(store_name_here, S_IRWXU | S_IRWXG);
2558 sprintf(tmp, "chmod 770 %s", store_name_here);
2559 paranoid_system(tmp);
2560 paranoid_free(tmp);
2561}
2562
2563
2564
2565
2566
2567
2568/**
2569 * Set the tmpdir and scratchdir to reside on the partition with the most free space.
2570 * Automatically excludes DOS, NTFS, SMB, and NFS filesystems.
2571 * @param bkpinfo The backup information structure. @c bkpinfo->tmpdir and @c bkpinfo->scratchdir will be set.
2572 * @ingroup utilityGroup
2573 */
2574void sensibly_set_tmpdir_and_scratchdir()
2575{
2576 char *tmp, *command, *sz;
2577
2578 malloc_string(tmp);
2579 malloc_string(command);
2580 malloc_string(sz);
2581 assert(bkpinfo != NULL);
2582
2583#ifdef __FreeBSD__
2584 strcpy(tmp,
2585 call_program_and_get_last_line_of_output
2586 ("LANGUAGE=C df -m -P -t nonfs,msdosfs,ntfs,smbfs,smb,cifs,afs,ocfs,ocfs2,mvfs | tr -s '\t' ' ' | grep -vE \"none|Filesystem\" | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'"));
2587#else
2588 strcpy(tmp,
2589 call_program_and_get_last_line_of_output
2590 ("LANGUAGE=C df -m -P -x nfs -x vfat -x ntfs -x smbfs -x smb -x cifs -x afs -x ocfs -x ocfs2 -x mvfs | sed 's/ /devdev/' | tr -s '\t' ' ' | grep -vE \"none|Filesystem|/dev/shm\" | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'"));
2591#endif
2592
2593 if (tmp[0] != '/') {
2594 strcpy(sz, tmp);
2595 strcpy(tmp, "/");
2596 strcat(tmp, sz);
2597 }
2598 if (!tmp[0]) {
2599 fatal_error("I couldn't figure out the tempdir!");
2600 }
2601 setup_tmpdir(tmp);
2602 log_it("bkpinfo->tmpdir is being set to %s", bkpinfo->tmpdir);
2603
2604 sprintf(bkpinfo->scratchdir, "%s/mondo.scratch.%d", tmp,
2605 (int) (random() % 32768));
2606 log_it("bkpinfo->scratchdir is being set to %s", bkpinfo->scratchdir);
2607
2608 sprintf(command, "rm -Rf %s/tmp.mondo.* %s/mondo.scratch.*", tmp, tmp);
2609 paranoid_system(command);
2610 paranoid_free(tmp);
2611 paranoid_free(command);
2612 paranoid_free(sz);
2613}
2614
2615
2616
2617
2618
2619
2620/**
2621 * @addtogroup deviceGroup
2622 * @{
2623 */
2624/**
2625 * If we can read @p dev, set @p output to it.
2626 * If @p dev cannot be read, set @p output to "".
2627 * @param dev The device to check for.
2628 * @param output Set to @p dev if @p dev exists, "" otherwise.
2629 * @return TRUE if @p dev exists, FALSE if it doesn't.
2630 */
2631bool set_dev_to_this_if_rx_OK(char *output, char *dev)
2632{
2633 char *command;
2634
2635 malloc_string(command);
2636 if (!dev || dev[0] == '\0') {
2637 output[0] = '\0';
2638 return (FALSE);
2639 }
2640// assert_string_is_neither_NULL_nor_zerolength(dev);
2641 log_msg(10, "Injecting %s", dev);
2642 inject_device(dev);
2643 if (!does_file_exist(dev)) {
2644 log_msg(10, "%s doesn't exist. Returning FALSE.", dev);
2645 return (FALSE);
2646 }
2647 sprintf(command, "dd bs=%ld count=1 if=%s of=/dev/null &> /dev/null",
2648 512L, dev);
2649 if (!run_program_and_log_output(command, FALSE)
2650 && !run_program_and_log_output(command, FALSE)) {
2651 strcpy(output, dev);
2652 log_msg(4, "Found it - %s", dev);
2653 return (TRUE);
2654 } else {
2655 output[0] = '\0';
2656 log_msg(4, "It's not %s", dev);
2657 return (FALSE);
2658 }
2659}
2660
2661
2662
2663
2664
2665/**
2666 * Find out what number CD is in the drive.
2667 * @param bkpinfo The backup information structure. The @c bkpinfo->media_device field is the only one used.
2668 * @return The current CD number, or -1 if it could not be found.
2669 * @note If the CD is not mounted, it will be mounted
2670 * (and remain mounted after this function returns).
2671 */
2672int what_number_cd_is_this()
2673{
2674 int cd_number = -1;
2675 char *mountdev;
2676 char *tmp;
2677
2678 malloc_string(mountdev);
2679 malloc_string(tmp);
2680 assert(bkpinfo != NULL);
2681// log_it("Asking what_number_cd_is_this");
2682 if (g_ISO_restore_mode) {
2683 sprintf(tmp, "mount | grep iso9660 | awk '{print $3;}'");
2684// log_it("tmp = %s", tmp);
2685
2686 strcpy(mountdev, call_program_and_get_last_line_of_output(tmp));
2687 strcat(mountdev, "/archives/THIS-CD-NUMBER");
2688// log_it("mountdev = %s", mountdev);
2689 cd_number = atoi(last_line_of_file(mountdev));
2690// log_it("cd_number = %d", cd_number);
2691 paranoid_free(mountdev);
2692 paranoid_free(tmp);
2693 return (cd_number);
2694 }
2695
2696 strcpy(mountdev, bkpinfo->media_device);
2697 if (!mountdev[0]) {
2698 log_it
2699 ("(what_number_cd_is_this) Warning - media_device unknown. Finding out...");
2700 find_cdrom_device(bkpinfo->media_device, FALSE);
2701 }
2702 if (!is_this_device_mounted(MNT_CDROM)) {
2703 if (bkpinfo->backup_media_type == usb) {
2704 mount_USB_here(mountdev, MNT_CDROM);
2705 } else {
2706 mount_CDROM_here(mountdev, MNT_CDROM);
2707 }
2708 }
2709 cd_number =
2710 atoi(last_line_of_file(MNT_CDROM "/archives/THIS-CD-NUMBER"));
2711// log_it("cd_number..later.. = %d", cd_number);
2712 paranoid_free(mountdev);
2713 paranoid_free(tmp);
2714 return (cd_number);
2715}
2716
2717
2718
2719
2720
2721
2722
2723/**
2724 * Find out what device is mounted as root (/).
2725 * @return Root device.
2726 * @note The returned string points to static storage and will be overwritten with every call.
2727 * @bug A bit of a misnomer; it's actually finding out the root device.
2728 * The mountpoint (where it's mounted) will obviously be '/'.
2729 */
2730char *where_is_root_mounted()
2731{
2732 /*@ buffers **************** */
2733 static char tmp[MAX_STR_LEN];
2734
2735
2736#ifdef __FreeBSD__
2737 strcpy(tmp, call_program_and_get_last_line_of_output
2738 ("mount | grep \" on / \" | cut -d' ' -f1"));
2739#else
2740 strcpy(tmp, call_program_and_get_last_line_of_output
2741 ("mount | grep \" on / \" | cut -d' ' -f1 | sed s/[0-9]// | sed s/[0-9]//"));
2742 if (strstr(tmp, "/dev/cciss/")) {
2743 strcpy(tmp, call_program_and_get_last_line_of_output
2744 ("mount | grep \" on / \" | cut -d' ' -f1 | cut -dp -f1"));
2745 }
2746 if (strstr(tmp, "/dev/md")) {
2747 strcpy(tmp,
2748 call_program_and_get_last_line_of_output
2749 ("mount | grep \" on / \" | cut -d' ' -f1"));
2750 }
2751#endif
2752
2753 return (tmp);
2754}
2755
2756
2757/**
2758 * Find out which boot loader is in use.
2759 * @param which_device Device to look for the boot loader on.
2760 * @return 'L' for LILO, 'E'for ELILO, 'G' for GRUB, 'B' or 'D' for FreeBSD boot loaders, or 'U' for Unknown.
2761 * @note Under Linux, all drives are examined, not just @p which_device.
2762 */
2763#ifdef __FreeBSD__
2764char which_boot_loader(char *which_device)
2765{
2766 int count_lilos = 0;
2767 int count_grubs = 0;
2768 int count_boot0s = 0;
2769 int count_dangerouslydedicated = 0;
2770
2771 log_it("looking at drive %s's MBR", which_device);
2772 if (does_string_exist_in_boot_block(which_device, "GRUB")) {
2773 count_grubs++;
2774 }
2775 if (does_string_exist_in_boot_block(which_device, "LILO")) {
2776 count_lilos++;
2777 }
2778 if (does_string_exist_in_boot_block(which_device, "Drive")) {
2779 count_boot0s++;
2780 }
2781 if (does_string_exist_in_first_N_blocks
2782 (which_device, "FreeBSD/i386", 17)) {
2783 count_dangerouslydedicated++;
2784 }
2785 log_it("%d grubs and %d lilos and %d elilos and %d boot0s and %d DD\n",
2786 count_grubs, count_lilos, count_elilos, count_boot0s,
2787 count_dangerouslydedicated);
2788
2789 if (count_grubs && !count_lilos) {
2790 return ('G');
2791 } else if (count_lilos && !count_grubs) {
2792 return ('L');
2793 } else if (count_grubs == 1 && count_lilos == 1) {
2794 log_it("I'll bet you used to use LILO but switched to GRUB...");
2795 return ('G');
2796 } else if (count_boot0s == 1) {
2797 return ('B');
2798 } else if (count_dangerouslydedicated) {
2799 return ('D');
2800 } else {
2801 log_it("Unknown boot loader");
2802 return ('U');
2803 }
2804}
2805
2806#else
2807
2808char which_boot_loader(char *which_device)
2809{
2810 /*@ buffer ***************************************************** */
2811 char *list_drives_cmd;
2812 char *current_drive;
2813
2814 /*@ pointers *************************************************** */
2815 FILE *pdrives;
2816
2817 /*@ int ******************************************************** */
2818 int count_lilos = 0;
2819 int count_grubs = 0;
2820
2821 /*@ end vars *************************************************** */
2822
2823 malloc_string(list_drives_cmd);
2824 malloc_string(current_drive);
2825
2826#ifdef __IA64__
2827 /* No choice for it */
2828 return ('E');
2829#endif
2830 assert(which_device != NULL);
2831 // sprintf (list_drives_cmd,
2832 // "fdisk -l | grep /dev | grep cyl | tr ':' ' ' | cut -d' ' -f2");
2833
2834 sprintf(list_drives_cmd,
2835 "parted2fdisk -l 2>/dev/null | grep \"/dev/.*:\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/; echo %s",
2836 where_is_root_mounted());
2837 log_it("list_drives_cmd = %s", list_drives_cmd);
2838
2839 if (!(pdrives = popen(list_drives_cmd, "r"))) {
2840 log_OS_error("Unable to open list of drives");
2841 paranoid_free(list_drives_cmd);
2842 paranoid_free(current_drive);
2843 return ('\0');
2844 }
2845 for (fgets(current_drive, MAX_STR_LEN, pdrives); !feof(pdrives);
2846 fgets(current_drive, MAX_STR_LEN, pdrives)) {
2847 strip_spaces(current_drive);
2848 log_it("looking at drive %s's MBR", current_drive);
2849 if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
2850 count_grubs++;
2851 strcpy(which_device, current_drive);
2852 break;
2853 }
2854 if (does_string_exist_in_boot_block(current_drive, "LILO")) {
2855 count_lilos++;
2856 strcpy(which_device, current_drive);
2857 break;
2858 }
2859 }
2860 if (pclose(pdrives)) {
2861 log_OS_error("Cannot pclose pdrives");
2862 }
2863 log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
2864 if (count_grubs && !count_lilos) {
2865 return ('G');
2866 } else if (count_lilos && !count_grubs) {
2867 return ('L');
2868 } else if (count_grubs == 1 && count_lilos == 1) {
2869 log_it("I'll bet you used to use LILO but switched to GRUB...");
2870 return ('G');
2871 } else {
2872 // We need to look on each partition then
2873 sprintf(list_drives_cmd,
2874 "parted2fdisk -l 2>/dev/null | grep -E \"^/dev/\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/");
2875 log_it("list_drives_cmd = %s", list_drives_cmd);
2876
2877 if (!(pdrives = popen(list_drives_cmd, "r"))) {
2878 log_OS_error("Unable to open list of drives");
2879 paranoid_free(list_drives_cmd);
2880 paranoid_free(current_drive);
2881 return ('\0');
2882 }
2883 for (fgets(current_drive, MAX_STR_LEN, pdrives); !feof(pdrives);
2884 fgets(current_drive, MAX_STR_LEN, pdrives)) {
2885 strip_spaces(current_drive);
2886 log_it("looking at partition %s's BR", current_drive);
2887 if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
2888 count_grubs++;
2889 strcpy(which_device, current_drive);
2890 break;
2891 }
2892 if (does_string_exist_in_boot_block(current_drive, "LILO")) {
2893 count_lilos++;
2894 strcpy(which_device, current_drive);
2895 break;
2896 }
2897 }
2898 if (pclose(pdrives)) {
2899 log_OS_error("Cannot pclose pdrives");
2900 }
2901 log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
2902 paranoid_free(list_drives_cmd);
2903 paranoid_free(current_drive);
2904 if (count_grubs && !count_lilos) {
2905 return ('G');
2906 } else if (count_lilos && !count_grubs) {
2907 return ('L');
2908 } else if (count_grubs == 1 && count_lilos == 1) {
2909 log_it("I'll bet you used to use LILO but switched to GRUB...");
2910 return ('G');
2911 } else {
2912 log_it("Unknown boot loader");
2913 return ('U');
2914 }
2915 }
2916}
2917#endif
2918
2919
2920
2921
2922/**
2923 * Write zeroes over the first 16K of @p device.
2924 * @param device The device to zero.
2925 * @return 0 for success, 1 for failure.
2926 */
2927int zero_out_a_device(char *device)
2928{
2929 FILE *fout;
2930 int i;
2931
2932 assert_string_is_neither_NULL_nor_zerolength(device);
2933
2934 log_it("Zeroing drive %s", device);
2935 if (!(fout = fopen(device, "w"))) {
2936 log_OS_error("Unable to open/write to device");
2937 return (1);
2938 }
2939 for (i = 0; i < 16384; i++) {
2940 fputc('\0', fout);
2941 }
2942 paranoid_fclose(fout);
2943 log_it("Device successfully zeroed.");
2944 return (0);
2945}
2946
2947/**
2948 * Return the device pointed to by @p incoming.
2949 * @param incoming The device to resolve symlinks for.
2950 * @return The path to the real device file.
2951 * @note The returned string points to static storage that will be overwritten with each call.
2952 * @bug Won't work with file v4.0; needs to be written in C.
2953 */
2954char *resolve_softlinks_to_get_to_actual_device_file(char *incoming)
2955{
2956 static char output[MAX_STR_LEN];
2957 char *command;
2958 char *curr_fname;
2959 char *scratch;
2960 char *tmp;
2961 char *p;
2962
2963 struct stat statbuf;
2964 command = malloc(1000);
2965 malloc_string(tmp);
2966 malloc_string(scratch);
2967 malloc_string(curr_fname);
2968 if (!does_file_exist(incoming)) {
2969 log_it
2970 ("resolve_softlinks_to_get_to_actual_device_file --- device not found");
2971 strcpy(output, incoming);
2972 } else {
2973 strcpy(curr_fname, incoming);
2974 lstat(curr_fname, &statbuf);
2975 while (S_ISLNK(statbuf.st_mode)) {
2976 log_msg(1, "curr_fname = %s", curr_fname);
2977 sprintf(command, "file %s", curr_fname);
2978 strcpy(tmp, call_program_and_get_last_line_of_output(command));
2979 for (p = tmp + strlen(tmp); p != tmp && *p != '`' && *p != ' ';
2980 p--);
2981 p++;
2982 strcpy(scratch, p);
2983 for (p = scratch; *p != '\0' && *p != '\''; p++);
2984 *p = '\0';
2985 log_msg(0, "curr_fname %s --> '%s' --> %s", curr_fname, tmp,
2986 scratch);
2987 if (scratch[0] == '/') {
2988 strcpy(curr_fname, scratch); // copy whole thing because it's an absolute softlink
2989 } else { // copy over the basename cos it's a relative softlink
2990 p = curr_fname + strlen(curr_fname);
2991 while (p != curr_fname && *p != '/') {
2992 p--;
2993 }
2994 if (*p == '/') {
2995 p++;
2996 }
2997 strcpy(p, scratch);
2998 }
2999 lstat(curr_fname, &statbuf);
3000 }
3001 strcpy(output, curr_fname);
3002 log_it("resolved %s to %s", incoming, output);
3003 }
3004 paranoid_free(command);
3005 paranoid_free(curr_fname);
3006 paranoid_free(tmp);
3007 return (output);
3008}
3009
3010/* @} - end of deviceGroup */
3011
3012
3013/**
3014 * Return the type of partition format (GPT or MBR)
3015 */
3016char *which_partition_format(const char *drive)
3017{
3018 static char output[4];
3019 char *tmp;
3020 char *command;
3021 char *fdisk;
3022#ifdef __IA64__
3023 struct stat buf;
3024#endif
3025 malloc_string(tmp);
3026 malloc_string(command);
3027 malloc_string(fdisk);
3028 log_msg(0, "Looking for partition table format type");
3029 sprintf(fdisk, "/sbin/parted2fdisk");
3030 log_msg(1, "Using %s", fdisk);
3031 sprintf(command, "%s -l %s | grep 'EFI GPT'", fdisk, drive);
3032 strcpy(tmp, call_program_and_get_last_line_of_output(command));
3033 if (strstr(tmp, "GPT") == NULL) {
3034 strcpy(output, "MBR");
3035 } else {
3036 strcpy(output, "GPT");
3037 }
3038 log_msg(0, "Found %s partition table format type", output);
3039 paranoid_free(command);
3040 paranoid_free(tmp);
3041 paranoid_free(fdisk);
3042 return (output);
3043}
3044
3045/* @} - end of deviceGroup */
Note: See TracBrowser for help on using the repository browser.