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

Last change on this file since 2324 was 2324, checked in by Bruno Cornec, 16 years ago

r3335@localhost: bruno | 2009-08-08 23:04:12 +0200

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