source: MondoRescue/branches/2.2.2/mondo/src/common/libmondo-devices.c@ 1316

Last change on this file since 1316 was 1316, checked in by Bruno Cornec, 17 years ago

MONDO_LOGFILE is rather a char* exported by each main program (consolidation of those mecanisms in .h files with ps_*)

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