source: MondoRescue/branches/2.05/mondo/mondo/common/libmondo-devices.c@ 123

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

Fix a memory free problem in libmondo-devices.c

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