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

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