source: MondoRescue/branches/stable/mondo/mondo/common/libmondo-devices.c@ 787

Last change on this file since 787 was 787, checked in by Bruno Cornec, 18 years ago

AFS support added

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