source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-devices.c@ 2322

Last change on this file since 2322 was 2322, checked in by Bruno Cornec, 15 years ago

r3333@localhost: bruno | 2009-08-08 01:58:31 +0200

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