source: MondoRescue/branches/3.2/mondo/src/common/libmondo-devices.c@ 3337

Last change on this file since 3337 was 3337, checked in by Bruno Cornec, 9 years ago
  • Fix some memory allocation which were not freed correctly (valgrind report)
  • Property svn:keywords set to Id
File size: 96.9 KB
RevLine 
[1]1/* libmondo-devices.c Subroutines for handling devices
[85]2 $Id: libmondo-devices.c 3337 2015-01-26 11:25:07Z 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"
[2434]12#include "mr_str.h"
[1]13#include "mondostructures.h"
14#include "libmondo-files-EXT.h"
15#include "libmondo-devices.h"
[541]16#include "lib-common-externs.h"
[1]17#include "libmondo-string-EXT.h"
18#include "libmondo-tools-EXT.h"
[541]19#include "libmondo-gui-EXT.h"
[1]20#include "libmondo-fork-EXT.h"
21#include "libmondo-stream-EXT.h"
22
[3191]23extern void mr_strip_spaces(char *);
24
[1]25#include <sys/types.h>
26#ifdef __FreeBSD__
27#define DKTYPENAMES
28#define FSTYPENAMES
29#include <sys/disklabel.h>
30#include <sys/disk.h>
31#elif linux
32#define u64 unsigned long long
[128]33#include <linux/fs.h> /* for BLKGETSIZE64 */
[1]34#include <linux/hdreg.h>
35#endif
36
37/*@unused@*/
[85]38//static char cvsid[] = "$Id: libmondo-devices.c 3337 2015-01-26 11:25:07Z bruno $";
[2771]39//
[1]40
[2771]41extern char *which_compression_type();
[2772]42/* Do we use extended attributes and acl ? */
43extern char *g_getfacl;
44extern char *g_getfattr;
[2771]45
[1]46extern int g_current_media_number;
47extern double g_kernel_version;
48
49extern bool g_ISO_restore_mode;
[99]50extern char *g_selfmounted_isodir;
[1316]51extern char *MONDO_LOGFILE;
[1]52
[1656]53extern void setup_tmpdir(char *path);
[3154]54extern void setup_scratchdir(char *path);
[1656]55
[128]56static char g_cdrw_drive_is_here[MAX_STR_LEN / 4] = "";
57static char g_cdrom_drive_is_here[MAX_STR_LEN / 4] = "";
58static char g_dvd_drive_is_here[MAX_STR_LEN / 4] = "";
[1]59
60
61/**
62 * ????? @bug ?????
63 * @ingroup globalGroup
64 */
[128]65bool g_restoring_live_from_cd = FALSE;
[2380]66bool g_restoring_live_from_netfs = FALSE;
[1]67
[128]68extern t_bkptype g_backup_media_type; // set by main()
[1]69
[1645]70/* Reference to global bkpinfo */
71extern struct s_bkpinfo *bkpinfo;
[1]72
[2424]73/* Stuff that handles the -I and -E option when a whole disk DSF is used */
74typedef struct mounted_fs_struct {
75 char device[MAX_STR_LEN]; /* The name of the device */
76 char mount_point[MAX_STR_LEN]; /* The devices mount point */
77 unsigned char check; /* 1 == included on DSF */
78 struct mounted_fs_struct *next;
79} MOUNTED_FS_STRUCT;
[1]80
[2424]81static MOUNTED_FS_STRUCT *DSF_Head = NULL; /* Points to the first entry of mounted_fs_struct list */
82static MOUNTED_FS_STRUCT *DSF_Tail = NULL; /* Points to the last entry of mounted_fs_struct list */
[1]83
[1645]84
85void set_g_cdrom_and_g_dvd_to_bkpinfo_value()
[1]86{
[128]87 strcpy(g_cdrom_drive_is_here, bkpinfo->media_device); // just in case
88 strcpy(g_dvd_drive_is_here, bkpinfo->media_device); // just in case
[1]89}
90
91
92
93/**
94 * Retract all CD trays and wait for autorun to complete.
95 * @ingroup deviceGroup
96 */
97void retract_CD_tray_and_defeat_autorun(void)
98{
99// log_it("rctada: Retracting all CD trays", __LINE__);
[2931]100 if (!bkpinfo->please_dont_eject) {
101 if (strlen(g_cdrom_drive_is_here) > 0) {
102 inject_device(g_cdrom_drive_is_here);
103 }
104 if (strlen(g_dvd_drive_is_here) > 0) {
105 inject_device(g_dvd_drive_is_here);
106 }
107 if (strlen(g_cdrw_drive_is_here) > 0) {
108 inject_device(g_cdrw_drive_is_here);
109 }
[128]110 }
[1]111// log_it("rctada: killing autorun");
112// run_program_and_log_output("killall autorun", TRUE);
[128]113 if (!run_program_and_log_output("ps | grep autorun | grep -v grep", 5)) {
114 log_it("autorun detected; sleeping for 2 seconds");
115 sleep(2);
116 }
117 log_it("rctada: Unmounting all CD drives", __LINE__);
118 run_program_and_log_output("umount /dev/cdr* /dev/dvd*", 5);
[1]119}
120
121
122
123/**
124 * Determine whether we're booted off a ramdisk.
125 * @return @c TRUE (we are) or @c FALSE (we aren't).
126 * @ingroup utilityGroup
127 */
[128]128bool am_I_in_disaster_recovery_mode(void)
[1]129{
[2214]130 char *tmp = NULL;
[128]131 bool is_this_a_ramdisk = FALSE;
[1]132
[3185]133 mr_asprintf(tmp, "%s", where_is_root_mounted());
[3191]134 log_msg(0, "root is mounted at %s\n", tmp);
135 log_msg(0, "That doesn't mean %s is the root partition. It's just a debugging message. Relax. It's part of am_I_in_disaster_recovery_mode().", tmp);
[1]136
137#ifdef __FreeBSD__
[128]138 if (strstr(tmp, "/dev/md")) {
139 is_this_a_ramdisk = TRUE;
140 }
[1]141#else
[128]142 if (!strncmp(tmp, "/dev/ram", 8)
143 || (!strncmp(tmp, "/dev/rd", 7) && !strcmp(tmp, "/dev/rd/")
144 && strncmp(tmp, "/dev/rd/cd", 10)) || strstr(tmp, "rootfs")
145 || !strcmp(tmp, "/dev/root")) {
146 is_this_a_ramdisk = TRUE;
147 } else {
148 is_this_a_ramdisk = FALSE;
149 }
[1]150#endif
[2214]151 mr_free(tmp);
[1]152
[128]153 if (is_this_a_ramdisk) {
[1983]154 if (!does_file_exist("/THIS-IS-A-RAMDISK")) {
[3191]155 log_to_screen("Using /dev/root is stupid of you but I'll forgive you.");
[128]156 is_this_a_ramdisk = FALSE;
157 }
158 }
159 if (does_file_exist("/THIS-IS-A-RAMDISK")) {
160 is_this_a_ramdisk = TRUE;
161 }
[3191]162
163 log_msg(1, "Is this a ramdisk? result = %s", (is_this_a_ramdisk) ? "TRUE" : "FALSE");
[128]164 return (is_this_a_ramdisk);
[1]165}
166
167
168
169
170
171/**
172 * Turn @c bkpinfo->backup_media_type into a human-readable string.
173 * @return The human readable string (e.g. @c cdr becomes <tt>"cdr"</tt>).
174 * @note The returned string points to static storage that will be overwritten with each call.
175 * @ingroup stringGroup
176 */
177static char *bkptype_to_string(t_bkptype bt)
178{
[128]179 static char output[MAX_STR_LEN / 4];
180 switch (bt) {
181 case none:
[541]182 strcpy(output, "none");
[1]183 break;
[128]184 case iso:
[541]185 strcpy(output, "iso");
[1]186 break;
[128]187 case cdr:
[541]188 strcpy(output, "cdr");
[1]189 break;
[128]190 case cdrw:
[541]191 strcpy(output, "cdrw");
[1]192 break;
[128]193 case cdstream:
[541]194 strcpy(output, "cdstream");
[1]195 break;
[2380]196 case netfs:
197 strcpy(output, "netfs");
[1]198 break;
[128]199 case tape:
[541]200 strcpy(output, "tape");
[1]201 break;
[128]202 case udev:
[541]203 strcpy(output, "udev");
[1]204 break;
[1687]205 case usb:
206 strcpy(output, "usb");
207 break;
[128]208 default:
[541]209 strcpy(output, "default");
[128]210 }
211 return (output);
[1]212}
213
214
215
216/**
217 * @addtogroup deviceGroup
218 * @{
219 */
220/**
221 * Eject the tray of the specified CD device.
222 * @param dev The device to eject.
223 * @return the return value of the @c eject command. (0=success, nonzero=failure)
224 */
[128]225int eject_device(char *dev)
[1]226{
[3191]227 char *command = NULL;
[128]228 int res1 = 0, res2 = 0;
[1]229
[3191]230 if (dev == NULL) {
231 return (1);
232 }
[1]233
[128]234 if (IS_THIS_A_STREAMING_BACKUP(g_backup_media_type)
235 && g_backup_media_type != udev) {
[3191]236 mr_asprintf(command, "mt -f %s offline", dev);
[128]237 res1 = run_program_and_log_output(command, 1);
[3191]238 mr_free(command);
[128]239 } else {
240 res1 = 0;
241 }
[1]242
243#ifdef __FreeBSD__
[128]244 if (strstr(dev, "acd")) {
[3191]245 mr_asprintf(command, "cdcontrol -f %s eject", dev);
[128]246 } else {
[3191]247 mr_asprintf(command, "camcontrol eject `echo %s | sed 's|/dev/||'`", dev);
[128]248 }
[1]249#else
[3191]250 mr_asprintf(command, "eject %s", dev);
[1]251#endif
252
[128]253 log_msg(3, "Ejecting %s", dev);
254 res2 = run_program_and_log_output(command, 1);
[3191]255 mr_free(command);
[128]256 if (res1 && res2) {
257 return (1);
258 } else {
259 return (0);
260 }
[1]261}
262
263/**
264 * Load (inject) the tray of the specified CD device.
265 * @param dev The device to load/inject.
266 * @return 0 for success, nonzero for failure.
267 */
[128]268int inject_device(char *dev)
[1]269{
[3191]270 char *command = NULL;
[128]271 int i;
[1]272
[3191]273 if (dev == NULL) {
274 return (1);
275 }
[1]276
277#ifdef __FreeBSD__
[128]278 if (strstr(dev, "acd")) {
[3191]279 mr_asprintf(command, "cdcontrol -f %s close", dev);
[128]280 } else {
[3191]281 mr_asprintf(command, "camcontrol load `echo %s | sed 's|/dev/||'`", dev);
[128]282 }
[1]283#else
[3191]284 mr_asprintf(command, "eject -t %s", dev);
[1]285#endif
[128]286 i = run_program_and_log_output(command, FALSE);
[3191]287 mr_free(command);
[128]288 return (i);
[1]289}
290
291
292/**
293 * Determine whether the specified @p device (really, you can use any file)
294 * exists.
295 * @return TRUE if it exists, FALSE if it doesn't.
296 */
[128]297bool does_device_exist(char *device)
[1]298{
299
[128]300 /*@ buffers *********************************************************** */
[3191]301 char *tmp = NULL;
[128]302 bool ret;
[1]303
[128]304 assert_string_is_neither_NULL_nor_zerolength(device);
[1]305
[3191]306 mr_asprintf(tmp, "ls %s > /dev/null 2> /dev/null", device);
[128]307
308 if (system(tmp)) {
309 ret = FALSE;
310 } else {
311 ret = TRUE;
312 }
[3191]313 mr_free(tmp);
[128]314 return (ret);
[1]315}
316
317
318/**
319 * Determine whether a non-Microsoft partition exists on any connected hard drive.
320 * @return TRUE (there's a Linux/FreeBSD partition) or FALSE (Microsoft has taken over yet another innocent PC).
321 */
[128]322bool does_nonMS_partition_exist(void)
[1]323{
324#if __FreeBSD__
[128]325 return
326 !system
327 ("for drive in /dev/ad? /dev/da?; do fdisk $drive | grep -q FreeBSD && exit 0; done; false");
[1]328#else
[128]329 return
330 !system
[681]331 ("parted2fdisk -l 2>/dev/null | grep '^/dev/' | grep -Eqv '(MS|DOS|FAT|NTFS)'");
[1]332#endif
333}
334
335/**
336 * Determine whether the specified @p partno exists on the specified @p drive.
337 * @param drive The drive to search for the partition in.
338 * @param partno The partition number to look for.
339 * @return 0 if it exists, nonzero otherwise.
340 */
[128]341int does_partition_exist(const char *drive, int partno)
[1]342{
[128]343 /*@ buffers **************************************************** */
[3191]344 char *program = NULL;
345 char *incoming = NULL;
[2211]346 char *searchstr = NULL;
[1]347
[128]348 /*@ ints ******************************************************* */
349 int res = 0;
[1]350
[128]351 /*@ pointers *************************************************** */
352 FILE *fin;
[1]353
[128]354 /*@ end vars *************************************************** */
355 assert_string_is_neither_NULL_nor_zerolength(drive);
356 assert(partno >= 0 && partno < 999);
[1]357
[128]358 malloc_string(searchstr);
[1]359
360#ifdef __FreeBSD__
[128]361 // We assume here that this is running from mondorestore. (It is.)
[3191]362 mr_asprintf(program, "ls %s %s >/dev/null 2>&1", drive, build_partition_name(tmp, drive, partno));
363 res = system(program);
364 mr_free(program);
365 return(res);
[1]366#endif
367
[3191]368 mr_asprintf(program, "parted2fdisk -l %s 2> /dev/null", drive);
[128]369 fin = popen(program, "r");
370 if (!fin) {
371 log_it("program=%s", program);
372 log_OS_error("Cannot popen-in program");
[3191]373 mr_free(program);
[128]374 return (0);
[1]375 }
[3191]376 mr_free(program);
377
[128]378 (void) build_partition_name(searchstr, drive, partno);
379 strcat(searchstr, " ");
[3191]380 for (res = 0, mr_getline(incoming, fin); !res && !feof(fin) ; mr_getline(incoming, fin)) {
[128]381 if (strstr(incoming, searchstr)) {
382 res = 1;
383 }
[3191]384 mr_free(incoming);
[128]385 }
[3191]386 mr_free(incoming);
387
[128]388 if (pclose(fin)) {
389 log_OS_error("Cannot pclose fin");
390 }
391 paranoid_free(searchstr);
392 return (res);
[1]393}
394
395
396
397
398
399/**
400 * Determine whether given NULL-terminated @p str exists in the MBR of @p dev.
401 * @param dev The device to look in.
402 * @param str The string to look for.
403 * @return TRUE if it exists, FALSE if it doesn't.
404 */
[128]405bool does_string_exist_in_boot_block(char *dev, char *str)
[1]406{
[128]407 /*@ buffers **************************************************** */
[3191]408 char *command = NULL;
[1]409
[128]410 /*@ end vars *************************************************** */
411 int i;
[1]412
[128]413 assert_string_is_neither_NULL_nor_zerolength(dev);
414 assert_string_is_neither_NULL_nor_zerolength(str);
[1]415
[3191]416 /* For UEFI detection, this should be extended to count=2 */
417 mr_asprintf(command, "dd if=%s bs=446 count=1 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null", dev, str);
[128]418 i = system(command);
[3191]419 mr_free(command);
[128]420 if (i) {
421 return (FALSE);
422 } else {
423 return (TRUE);
424 }
[1]425}
426
427/**
428 * Determine whether specified @p str exists in the first @p n sectors of
429 * @p dev.
430 * @param dev The device to look in.
431 * @param str The string to look for.
432 * @param n The number of 512-byte sectors to search.
433 */
[128]434bool does_string_exist_in_first_N_blocks(char *dev, char *str, int n)
[1]435{
[128]436 /*@ buffers **************************************************** */
[3191]437 char *command = NULL;
[128]438 /*@ end vars *************************************************** */
439 int i;
[1]440
[3191]441 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]442 i = system(command);
[3191]443 mr_free(command);
[128]444 if (i) {
445 return (FALSE);
446 } else {
447 return (TRUE);
448 }
[1]449}
450
451
452
453/**
454 * Try to mount CD-ROM at @p mountpoint. If the CD-ROM is not found or has
455 * not been specified, call find_cdrom_device() to find it.
456 * @param mountpoint Where to mount the CD-ROM.
457 * @return 0 for success, nonzero for failure.
458 * @see mount_CDROM_here
459 */
[3191]460int find_and_mount_actual_cd(char *mountpoint) {
461
[128]462 /*@ buffers ***************************************************** */
[1]463
[128]464 /*@ int's ****************************************************** */
465 int res;
[3191]466 char *dev = NULL;
[1]467
[128]468 /*@ end vars **************************************************** */
[1]469
[128]470 malloc_string(dev);
471 assert(bkpinfo != NULL);
472 assert_string_is_neither_NULL_nor_zerolength(mountpoint);
[1]473
[128]474 if (g_backup_media_type == dvd) {
475 strcpy(dev, g_dvd_drive_is_here);
476 if (!dev[0]) {
477 find_dvd_device(dev, FALSE);
478 }
479 } else {
480 strcpy(dev, g_cdrom_drive_is_here);
481 if (!dev[0]) {
482 find_cdrom_device(dev, FALSE);
483 }
484 }
[1]485
[128]486 if (bkpinfo->backup_media_type != iso) {
487 retract_CD_tray_and_defeat_autorun();
488 }
[1]489
[128]490 if (!dev[0] || (res = mount_CDROM_here(dev, mountpoint))) {
[3212]491 if (!popup_and_get_string("CD-ROM device", "Please enter your CD-ROM's /dev device", dev, MAX_STR_LEN / 4)) {
[128]492 res = 1;
493 } else {
494 res = mount_CDROM_here(dev, mountpoint);
495 }
496 }
497 if (res) {
[541]498 log_msg(1, "mount failed");
[128]499 } else {
[541]500 log_msg(1, "mount succeeded with %s", dev);
[128]501 }
502 paranoid_free(dev);
503 return (res);
[1]504}
505
506
507
508
509
510
511/**
512 * Locate a CD-R/W writer's SCSI node.
513 * @param cdrw_device SCSI node will be placed here.
514 * @return 0 for success, nonzero for failure.
515 */
[128]516int find_cdrw_device(char *cdrw_device)
[1]517{
[128]518 /*@ buffers ************************ */
[2214]519 char *tmp = NULL;
520 char *cdr_exe = NULL;
[3191]521 char *command = NULL;
[1]522
[128]523 if (g_cdrw_drive_is_here[0]) {
524 strcpy(cdrw_device, g_cdrw_drive_is_here);
525 log_msg(3, "Been there, done that. Returning %s", cdrw_device);
526 return (0);
527 }
528 if (g_backup_media_type == dvd) {
[3191]529 log_msg(1, "This is dumb. You're calling find_cdrw_device() but you're backing up to DVD. WTF?");
[128]530 return (1);
531 }
532 run_program_and_log_output("insmod ide-scsi", -1);
533 if (find_home_of_exe("cdrecord")) {
[3185]534 mr_asprintf(cdr_exe, "cdrecord");
[128]535 } else {
[3185]536 mr_asprintf(cdr_exe, "dvdrecord");
[128]537 }
538 if (find_home_of_exe(cdr_exe)) {
[3191]539 mr_asprintf(command, "%s -scanbus 2> /dev/null | tr -s '\t' ' ' | grep \"[0-9]*,[0-9]*,[0-9]*\" | grep -v \"[0-9]*) \\*\" | grep -E '[D|C][V|D]' | cut -d' ' -f2 | head -n1", cdr_exe);
[3185]540 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
[3191]541 mr_free(command);
[128]542 }
[2214]543 if ((tmp == NULL) || (strlen(tmp) < 2)) {
544 mr_free(tmp);
545 mr_free(cdr_exe);
[128]546 return 1;
547 } else {
548 strcpy(cdrw_device, tmp);
[3191]549 log_it("Found CDRW device - %s", cdrw_device);
[128]550 strcpy(g_cdrw_drive_is_here, cdrw_device);
[2214]551 mr_free(tmp);
552 mr_free(cdr_exe);
[128]553 return (0);
554 }
[1]555}
556
557
558/**
559 * Attempt to locate a CD-ROM device's /dev entry.
560 * Several different methods may be used to find the device, including
561 * calling @c cdrecord, searching @c dmesg, and trial-and-error.
562 * @param output Where to put the located /dev entry.
563 * @param try_to_mount Whether to mount the CD as part of the test; if mount
564 * fails then return failure.
565 * @return 0 for success, nonzero for failure.
566 */
[128]567int find_cdrom_device(char *output, bool try_to_mount)
[1]568{
[128]569 /*@ pointers **************************************************** */
570 FILE *fin;
571 char *p;
572 char *q;
573 char *r;
574 int retval = 0;
[1]575
[128]576 /*@ bool's ****************************************************** */
577 bool found_it = FALSE;
[1]578
[128]579 /*@ buffers ***************************************************** */
[3191]580 char *tmp = NULL;
[2214]581 char *tmp1 = NULL;
582 char *cdr_exe = NULL;
[128]583 char *phrase_one;
[3191]584 char *phrase_two = NULL;
585 char *command = NULL;
586#ifndef __FreeBSD__
587 char *dvd_last_resort = NULL;
588#endif
589 char *mountpoint = NULL;
[128]590 static char the_last_place_i_found_it[MAX_STR_LEN] = "";
[1]591
[128]592 /*@ intialize *************************************************** */
593 malloc_string(tmp);
594 malloc_string(phrase_one);
595 malloc_string(mountpoint);
[1]596
[128]597 output[0] = '\0';
598 phrase_one[0] = '\0';
[1]599
[128]600 /*@ end vars **************************************************** */
[1]601
[128]602 if (g_cdrom_drive_is_here[0] && !isdigit(g_cdrom_drive_is_here[0])) {
603 strcpy(output, g_cdrom_drive_is_here);
604 log_msg(3, "Been there, done that. Returning %s", output);
605 retval = 0;
606 goto end_of_find_cdrom_device;
607 }
608 if (the_last_place_i_found_it[0] != '\0' && !try_to_mount) {
609 strcpy(output, the_last_place_i_found_it);
[3191]610 log_msg(3, "find_cdrom_device() --- returning last found location - '%s'", output);
[128]611 retval = 0;
612 goto end_of_find_cdrom_device;
613 }
[1]614
[1644]615 sprintf(mountpoint, "%s/cd.mnt", bkpinfo->tmpdir);
[128]616 make_hole_for_dir(mountpoint);
[1]617
[128]618 if (find_home_of_exe("cdrecord")) {
[3185]619 mr_asprintf(cdr_exe, "cdrecord");
[128]620 } else {
[3185]621 mr_asprintf(cdr_exe, "dvdrecord");
[128]622 }
623 tmp[0] = '\0';
624 if (!find_home_of_exe(cdr_exe)) {
625 strcpy(output, "/dev/cdrom");
626 log_msg(4, "Can't find cdrecord; assuming %s", output);
627 if (!does_device_exist(output)) {
628 log_msg(4, "That didn't work. Sorry.");
629 retval = 1;
630 goto end_of_find_cdrom_device;
631 } else {
632 retval = 0;
633 goto end_of_find_cdrom_device;
634 }
635 }
636
[3191]637 mr_asprintf(command, "%s -scanbus 2> /dev/null", cdr_exe);
[128]638 fin = popen(command, "r");
639 if (!fin) {
640 log_msg(4, "command=%s", command);
641 log_OS_error("Cannot popen command");
[2214]642 mr_free(cdr_exe);
[3191]643 mr_free(command);
[128]644 return (1);
645 }
[3191]646 mr_free(command);
647
[3060]648 for (tmp1 = fgets(tmp, MAX_STR_LEN, fin); !feof(fin) && (tmp1 != NULL);
649 tmp1 = fgets(tmp, MAX_STR_LEN, fin)) {
[128]650 p = strchr(tmp, '\'');
651 if (p) {
652 q = strchr(++p, '\'');
653 if (q) {
654 for (r = q; *(r - 1) == ' '; r--);
655 *r = '\0';
656 strcpy(phrase_one, p);
657 p = strchr(++q, '\'');
658 if (p) {
659 q = strchr(++p, '\'');
660 if (q) {
661 while (*(q - 1) == ' ') {
662 q--;
663 }
664 *q = '\0';
[3191]665 mr_asprintf(phrase_two, "%s", p);
[128]666 }
667 }
[1]668 }
669 }
670 }
[128]671 paranoid_pclose(fin);
[1]672
673#ifndef __FreeBSD__
[3191]674 if (!phrase_two || strlen(phrase_two) == 0) {
[128]675 log_msg(4, "Not running phase two. String is empty.");
676 } else {
[3191]677 mr_asprintf(command, "dmesg | grep \"%s\" 2> /dev/null", phrase_two);
[128]678 fin = popen(command, "r");
[3191]679 mr_free(command);
680
[128]681 if (!fin) {
682 log_msg(4, "Cannot run 2nd command - non-fatal, fortunately");
683 } else {
[3060]684 for (tmp1 = fgets(tmp, MAX_STR_LEN, fin); !feof(fin) && (tmp1 != NULL);
685 tmp1 = fgets(tmp, MAX_STR_LEN, fin)) {
[128]686 log_msg(5, "--> '%s'", tmp);
687 if (tmp[0] != ' ' && tmp[1] != ' ') {
688 p = strchr(tmp, ':');
689 if (p) {
690 *p = '\0';
691 if (strstr(tmp, "DVD")) {
[3191]692 mr_free(dvd_last_resort);
693 mr_asprintf(dvd_last_resort, "/dev/%s", tmp);
694 log_msg(4, "Ignoring '%s' because it's a DVD drive", tmp);
[128]695 } else {
696 sprintf(output, "/dev/%s", tmp);
697 found_it = TRUE;
698 }
699 }
700 }
701 }
702 paranoid_pclose(fin);
703 }
704 }
[1]705
706#endif
707#ifdef __FreeBSD__
[128]708 if (!found_it) {
709 log_msg(4, "OK, approach 2");
710 if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/cdrom"))) {
711 if (!
712 (found_it =
713 set_dev_to_this_if_rx_OK(output, "/dev/cdrom1"))) {
714 if (!
715 (found_it =
716 set_dev_to_this_if_rx_OK(output, "/dev/dvd"))) {
717 if (!
718 (found_it =
719 set_dev_to_this_if_rx_OK(output, "/dev/acd0"))) {
720 if (!
721 (found_it =
722 set_dev_to_this_if_rx_OK(output,
723 "/dev/cd01"))) {
724 if (!
725 (found_it =
726 set_dev_to_this_if_rx_OK(output,
727 "/dev/acd1"))) {
728 if (!
729 (found_it =
730 set_dev_to_this_if_rx_OK(output,
731 "/dev/cd1")))
732 {
733 retval = 1;
734 goto end_of_find_cdrom_device;
735 }
736 }
737 }
738 }
739 }
740 }
741 }
742 }
[1]743#else
[128]744 if (!found_it && strlen(dvd_last_resort) > 0) {
[3191]745 log_msg(4, "Well, I'll use the DVD - %s - as a last resort", dvd_last_resort);
[128]746 strcpy(output, dvd_last_resort);
747 found_it = TRUE;
[1]748 }
[128]749 if (found_it) {
[3191]750 sprintf(tmp, "grep \"%s=ide-scsi\" /proc/cmdline &> /dev/null", strrchr(output, '/') + 1);
[128]751 if (system(tmp) == 0) {
[3191]752 log_msg(4, "%s is not right. It's being SCSI-emulated. Continuing.", output);
[128]753 found_it = FALSE;
754 output[0] = '\0';
755 }
756 }
[1]757
[128]758 if (found_it) {
759 log_msg(4, "(find_cdrom_device) --> '%s'", output);
760 if (!does_device_exist(output)) {
761 found_it = FALSE;
762 log_msg(4, "OK, I was wrong, I haven't found it... yet.");
763 }
764 }
[1]765
[128]766 if (!found_it) {
767 log_msg(4, "OK, approach 2");
768 if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/scd0"))) {
769 if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/sr0"))) {
770 if (!
771 (found_it =
772 set_dev_to_this_if_rx_OK(output, "/dev/cdrom"))) {
773 if (!
774 (found_it =
775 set_dev_to_this_if_rx_OK(output,
776 "/dev/cdrom0"))) {
777 if (!
778 (found_it =
779 set_dev_to_this_if_rx_OK(output,
780 "/dev/cdrom1"))) {
781 if (!
782 (found_it =
783 set_dev_to_this_if_rx_OK(output,
784 "/dev/sr1"))) {
785 if (!
786 (found_it =
787 set_dev_to_this_if_rx_OK(output,
788 "/dev/dvd")))
789 {
790 if (!
791 (found_it =
792 set_dev_to_this_if_rx_OK(output,
793 g_cdrw_drive_is_here)))
794 {
795 retval = 1;
796 goto end_of_find_cdrom_device;
797 }
798 }
799 }
800 }
801 }
802 }
803 }
804 }
805 }
[1]806#endif
807
[128]808 if (found_it && try_to_mount) {
809 if (mount_CDROM_here(output, mountpoint)) {
810 log_msg(4, "[Cardigans] I've changed my mind");
811 found_it = FALSE;
812 } else {
813 sprintf(tmp, "%s/archives", mountpoint);
814 if (!does_file_exist(tmp)) {
815 log_msg(4, "[Cardigans] I'll take it back");
816 found_it = FALSE;
817 } else {
[3191]818 mr_asprintf(command, "umount %s", output);
[128]819 paranoid_system(command);
[3191]820 mr_free(command);
821
[128]822 log_msg(4, "I'm confident the Mondo CD is in %s", output);
823 }
824 }
825 }
826 unlink(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
[3191]840 mr_asprintf(command, "%s -scanbus | grep \"[0-9],[0-9],[0-9]\" | grep -E \"[D|C][V|D]\" | grep -n \"\" | grep \"%s\" | cut -d':' -f2", cdr_exe, g_cdrw_drive_is_here);
841
[128]842 log_msg(1, "command=%s", command);
[3185]843 mr_asprintf(tmp1, "%s", call_program_and_get_last_line_of_output(command));
[3191]844 mr_free(command);
845
[2214]846 if (strlen(tmp1) > 0) {
847 strcpy(output, tmp1);
[128]848 log_msg(4, "Finally found it at %s", output);
849 retval = 0;
850 } else {
851 log_msg(4, "Still couldn't find it.");
852 retval = 1;
853 }
[2214]854 mr_free(tmp1);
855
[128]856 end_of_find_cdrom_device:
[3191]857 mr_free(cdr_exe);
858 mr_free(phrase_two);
859 mr_free(dvd_last_resort);
860
[128]861 paranoid_free(tmp);
862 paranoid_free(phrase_one);
863 paranoid_free(mountpoint);
864 return (retval);
[1]865}
866
867
[128]868int find_dvd_device(char *output, bool try_to_mount)
[1]869{
[128]870 char *tmp;
871 int retval = 0, devno = -1;
[1]872
[128]873 if (g_dvd_drive_is_here[0]) {
874 strcpy(output, g_dvd_drive_is_here);
875 log_msg(3, "Been there, done that. Returning %s", output);
876 return (0);
877 }
[1]878
[3191]879 malloc_string(tmp);
880 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output("dvdrecord -scanbus 2> /dev/null | grep -E '\)\ \'' | grep -n '' | grep -E '[D|C][V|D]' | cut -d':' -f1"));
[128]881 log_msg(5, "tmp = '%s'", tmp);
[3191]882 if (!tmp[0]) {
883 mr_free(tmp);
[3194]884 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output("cdrecord -scanbus 2> /dev/null | grep -E '\)\ \'' | grep -n '' | grep -E '[D|C][V|D]' | cut -d':' -f1"));
[3191]885 }
[128]886 if (tmp[0]) {
887 devno = atoi(tmp) - 1;
888 }
[3191]889 mr_free(tmp);
890
[128]891 if (devno >= 0) {
892 retval = 0;
893 sprintf(output, "/dev/scd%d", devno);
894 strcpy(g_dvd_drive_is_here, output);
895 log_msg(2, "I think DVD is at %s", output);
896 } else {
897 log_msg(2, "I cannot find DVD");
898 retval = 1;
899 }
[1]900
[128]901 return (retval);
[1]902}
903
904
905
906
907
908#include <sys/ioctl.h>
909
910/**
911 * Find the size of the specified @p drive, in megabytes. Uses @c ioctl calls
912 * and @c dmesg.
913 * @param drive The device to find the size of.
914 * @return size in megabytes.
915 */
[128]916long get_phys_size_of_drive(char *drive)
[1]917{
[128]918 int fd;
[1]919#if linux
[128]920 unsigned long long s = 0;
[3060]921 int fileid, cylinders = 0;
[128]922 int cylindersize = 0;
[1]923 int gotgeo = 0;
924
[128]925
926 struct hd_geometry hdgeo;
[1]927#elif __FreeBSD__
[128]928 off_t s;
[1]929#endif
930
[128]931 long outvalA = -1;
932 long outvalB = -1;
933 long outvalC = -1;
[1]934
[128]935 if ((fd = open(drive, O_RDONLY)) != -1) {
936 if (ioctl(fd,
[1]937#if linux
[128]938#ifdef BLKGETSIZE64
939 BLKGETSIZE64,
940#else
941 BLKGETSIZE,
942#endif
[1]943#elif __FreeBSD__
[128]944 DIOCGMEDIASIZE,
[1]945#endif
[128]946 &s) != -1) {
947 close(fd);
948 // s>>11 works for older disks but not for newer ones
949 outvalB =
[1]950#if linux
[128]951#ifdef BLKGETSIZE64
952 s >> 20
[1]953#else
[128]954 s >> 11
[1]955#endif
[128]956#else
957 s >> 20
958#endif
959 ;
960 }
[1]961 }
962
[128]963 if (outvalB <= 0) {
964 log_msg(1, "Error getting size of %s: %s", drive, strerror(errno));
[1]965#if linux
[161]966 fileid = open(drive, O_RDONLY);
[2205]967 if (fileid != -1) {
[161]968 if (ioctl(fileid, HDIO_GETGEO, &hdgeo) != -1) {
969 if (hdgeo.cylinders && hdgeo.heads && hdgeo.sectors) {
970 cylindersize = hdgeo.heads * hdgeo.sectors / 2;
971 outvalA = cylindersize * cylinders / 1024;
972 log_msg(2, "Got Harddisk geometry, C:%d, H:%d, S:%d",
973 hdgeo.cylinders, hdgeo.heads, hdgeo.sectors);
974 gotgeo = 1;
975 } else {
976 log_msg(1, "Harddisk geometry wrong");
977 }
[1]978 } else {
[161]979 log_msg(1,
980 "Error in ioctl() getting new hard disk geometry (%s), resizing in unsafe mode",
981 strerror(errno));
[1]982 }
[161]983 close(fileid);
[1]984 } else {
[161]985 log_msg(1, "Failed to open %s for reading: %s", drive,
[128]986 strerror(errno));
[1]987 }
[161]988 if (!gotgeo) {
989 log_msg(1, "Failed to get harddisk geometry, using old mode");
990 }
[1]991#endif
[161]992 }
[1]993// OLDER DISKS will give ridiculously low value for outvalB (so outvalA is returned) :)
994// NEWER DISKS will give sane value for outvalB (close to outvalA, in other words) :)
995
[128]996 outvalC = (outvalA > outvalB) ? outvalA : outvalB;
997
[1]998// log_msg (5, "drive = %s, error = %s", drive, strerror (errno));
999// fatal_error ("GPSOD: Unable to get size of drive");
[128]1000 log_msg(1, "%s --> %ld or %ld --> %ld", drive, outvalA, outvalB,
1001 outvalC);
1002
1003 return (outvalC);
[1]1004}
[128]1005
[1]1006/**
1007 * Determine whether @p format is supported by the kernel. Uses /proc/filesystems
1008 * under Linux and @c lsvfs under FreeBSD.
1009 * @param format The format to test.
1010 * @return TRUE if the format is supported, FALSE if not.
1011 */
[128]1012bool is_this_a_valid_disk_format(char *format)
[1]1013{
[2211]1014 char *good_formats = NULL;
[3191]1015 char *command = NULL;
1016 char *format_sz = NULL;
[3291]1017 char *p = NULL;
[1]1018
[128]1019 FILE *pin;
1020 int retval;
[1]1021
[128]1022 assert_string_is_neither_NULL_nor_zerolength(format);
[1]1023
[3191]1024 mr_asprintf(format_sz, "%s ", format);
[1]1025
1026#ifdef __FreeBSD__
[3191]1027 mr_asprintf(command, "lsvfs | tr -s '\t' ' ' | grep -v Filesys | grep -v -- -- | cut -d' ' -f1 | tr -s '\n' ' '");
[1]1028#else
[3191]1029 mr_asprintf(command, "grep -v nodev /proc/filesystems | tr -s '\t' ' ' | cut -d' ' -f2 | tr -s '\n' ' '");
[1]1030#endif
1031
[128]1032 pin = popen(command, "r");
[3191]1033 mr_free(command);
1034
[128]1035 if (!pin) {
1036 log_OS_error("Unable to read good formats");
1037 retval = 0;
1038 } else {
[3291]1039 mr_getline(p, pin);
[3337]1040 mr_free(p);
[3291]1041 mr_asprintf(good_formats, " %s",p);
1042 (void)pclose(pin);
[3309]1043 mr_strip_spaces(good_formats);
[3291]1044 mr_strcat(good_formats, " swap lvm raid ntfs-3g ntfs 7 "); // " ntfs 7 " -- um, cheating much? :)
[128]1045 if (strstr(good_formats, format_sz)) {
1046 retval = 1;
1047 } else {
1048 retval = 0;
1049 }
1050 }
[3291]1051 mr_free(good_formats);
[3191]1052 mr_free(format_sz);
1053
[128]1054 return (retval);
[1]1055}
1056
1057
1058/** @def SWAPLIST_COMMAND The command to list the swap files/partitions in use. */
1059
1060/**
1061 * Determine whether @p device_raw is currently mounted.
1062 * @param device_raw The device to check.
1063 * @return TRUE if it's mounted, FALSE if not.
1064 */
[128]1065bool is_this_device_mounted(char *device_raw)
[1]1066{
1067
[128]1068 /*@ pointers **************************************************** */
1069 FILE *fin;
[1]1070
[128]1071 /*@ buffers ***************************************************** */
[3191]1072 char *incoming = NULL;
[2214]1073 char *device_with_tab = NULL;
1074 char *device_with_space = NULL;
1075 char *tmp = NULL;
1076 bool retval = FALSE;
[128]1077
[1]1078#ifdef __FreeBSD__
[128]1079#define SWAPLIST_COMMAND "swapinfo"
[1]1080#else
[128]1081#define SWAPLIST_COMMAND "cat /proc/swaps"
[1]1082#endif
1083
[128]1084 /*@ end vars **************************************************** */
[1]1085
[3191]1086 if (device_raw == NULL) {
1087 return(FALSE);
1088 }
1089
[128]1090 if (device_raw[0] != '/' && !strstr(device_raw, ":/")) {
1091 log_msg(1, "%s needs to have a '/' prefixed - I'll do it",
1092 device_raw);
[3185]1093 mr_asprintf(tmp, "/%s", device_raw);
[128]1094 } else {
[3185]1095 mr_asprintf(tmp, "%s", device_raw);
[128]1096 }
1097 log_msg(1, "Is %s mounted?", tmp);
1098 if (!strcmp(tmp, "/proc") || !strcmp(tmp, "proc")) {
1099 log_msg(1,
1100 "I don't know how the heck /proc made it into the mountlist. I'll ignore it.");
[2214]1101 mr_free(tmp);
1102 return(FALSE);
[128]1103 }
[3185]1104 mr_asprintf(device_with_tab, "%s\t", tmp);
1105 mr_asprintf(device_with_space, "%s ", tmp);
[2214]1106 mr_free(tmp);
[1]1107
[128]1108 if (!(fin = popen("mount", "r"))) {
1109 log_OS_error("Cannot popen 'mount'");
[2214]1110 return(FALSE);
[1]1111 }
[3191]1112
1113 for (mr_getline(incoming, fin); !feof(fin); mr_getline(incoming, fin)) {
1114 if (strstr(incoming, device_with_space) || strstr(incoming, device_with_tab)) {
[128]1115 paranoid_pclose(fin);
[3191]1116 mr_free(incoming);
[2214]1117 return(TRUE);
[128]1118 }
[3191]1119 mr_free(incoming);
[128]1120 }
[3191]1121 mr_free(incoming);
[2214]1122 mr_free(device_with_tab);
[128]1123 paranoid_pclose(fin);
[3185]1124 mr_asprintf(tmp, "%s | grep -E \"^%s\" > /dev/null 2> /dev/null", SWAPLIST_COMMAND, device_with_space);
[2423]1125 mr_free(device_with_space);
[128]1126 log_msg(4, "tmp (command) = '%s'", tmp);
1127 if (!system(tmp)) {
[2214]1128 retval = TRUE;
[128]1129 }
[2214]1130 mr_free(tmp);
1131 return(retval);
[1]1132}
1133
1134#ifdef __FreeBSD__
1135// CODE IS FREEBSD-SPECIFIC
1136/**
1137 * Create a loopback device for specified @p fname.
1138 * @param fname The file to associate with a device.
1139 * @return /dev entry for the device, or NULL if it couldn't be allocated.
1140 */
[128]1141char *make_vn(char *fname)
[1]1142{
[128]1143 char *device = (char *) malloc(MAX_STR_LEN);
1144 char *mddevice = (char *) malloc(32);
1145 char command[MAX_STR_LEN];
1146 int vndev = 2;
[3191]1147 if (atoi(call_program_and_get_last_line_of_output("/sbin/sysctl -n kern.osreldate")) < 500000) {
[128]1148 do {
1149 sprintf(mddevice, "vn%ic", vndev++);
1150 sprintf(command, "vnconfig %s %s", mddevice, fname);
1151 if (vndev > 10) {
1152 return NULL;
1153 }
1154 }
1155 while (system(command));
1156 } else {
1157 sprintf(command, "mdconfig -a -t vnode -f %s", fname);
1158 mddevice = call_program_and_get_last_line_of_output(command);
1159 if (!strstr(mddevice, "md")) {
1160 return NULL;
1161 }
1162 }
1163 sprintf(device, "/dev/%s", mddevice);
1164 return device;
[1]1165}
1166
1167
1168
1169// CODE IS FREEBSD-SPECIFIC
1170/**
1171 * Deallocate specified @p dname.
1172 * This should be called when you are done with the device created by make_vn(),
1173 * so the system does not run out of @c vn devices.
1174 * @param dname The device to deallocate.
1175 * @return 0 for success, nonzero for failure.
1176 */
[128]1177int kick_vn(char *dname)
[1]1178{
[3191]1179 char *command = NULL;
1180 int res = 0;
[1]1181
[128]1182 if (strncmp(dname, "/dev/", 5) == 0) {
1183 dname += 5;
1184 }
[1]1185
[3191]1186 if (atoi(call_program_and_get_last_line_of_output("/sbin/sysctl -n kern.osreldate")) < 500000) {
1187 mr_asprintf(command, "vnconfig -d %s", dname);
[128]1188 } else {
[3191]1189 mr_asprintf(command, "mdconfig -d -u %s", dname);
[128]1190 }
[3191]1191 res = system(command);
1192 mr_free(command);
1193 return(res);
[1]1194}
1195#endif
1196
1197
1198/**
1199 * Mount the CD-ROM at @p mountpoint.
1200 * @param device The device (or file if g_ISO_restore_mode) to mount.
1201 * @param mountpoint The place to mount it.
1202 * @return 0 for success, nonzero for failure.
1203 */
[1741]1204int mount_USB_here(char *device, char *mountpoint)
1205{
1206 /*@ buffer ****************************************************** */
[3191]1207 char *command = NULL;
[1741]1208 int retval;
1209
1210 assert_string_is_neither_NULL_nor_zerolength(device);
1211 assert_string_is_neither_NULL_nor_zerolength(mountpoint);
1212
1213 make_hole_for_dir(mountpoint);
1214 if (isdigit(device[0])) {
1215 return(1);
1216 }
[3191]1217 log_msg(4, "(mount_USB_here --- device=%s, mountpoint=%s", device, mountpoint);
[1741]1218
1219#ifdef __FreeBSD__
[3191]1220 mr_asprintf(command, "mount_vfat %s %s 2>> %s", device, mountpoint, MONDO_LOGFILE);
[1741]1221
1222#else
[3191]1223 mr_asprintf(command, "mount %s -t vfat %s 2>> %s", device, mountpoint, MONDO_LOGFILE);
[1741]1224#endif
1225
1226 log_msg(4, command);
1227 retval = system(command);
1228 log_msg(1, "system(%s) returned %d", command, retval);
[3191]1229 mr_free(command);
[1741]1230
1231 return (retval);
1232}
1233
1234/**
1235 * Mount the CD-ROM at @p mountpoint.
1236 * @param device The device (or file if g_ISO_restore_mode) to mount.
1237 * @param mountpoint The place to mount it.
1238 * @return 0 for success, nonzero for failure.
1239 */
[3191]1240int mount_CDROM_here(char *device, const char *mountpoint)
[1]1241{
[128]1242 /*@ buffer ****************************************************** */
[2211]1243 char *command = NULL;
[128]1244 int retval;
[2257]1245#ifdef __FreeBSD__
1246 char *dev = NULL;
1247#else
1248 char *options = NULL;
1249#endif
[1]1250
[128]1251 assert_string_is_neither_NULL_nor_zerolength(device);
1252 assert_string_is_neither_NULL_nor_zerolength(mountpoint);
[1]1253
[128]1254 make_hole_for_dir(mountpoint);
[2211]1255
[2257]1256 if (isdigit(device[0])) {
1257 find_cdrom_device(device, FALSE);
1258 }
1259#ifndef __FreeBSD__
[3185]1260 mr_asprintf(options, "ro");
[2211]1261#endif
1262
[128]1263 if (g_ISO_restore_mode) {
[1]1264
1265#ifdef __FreeBSD__
[3185]1266 mr_asprintf(dev, "%s", make_vn(device));
[128]1267 if (!dev) {
[3185]1268 mr_asprintf(command, "Unable to mount ISO (make_vn(%s) failed)", device);
[128]1269 fatal_error(command);
1270 }
1271 strcpy(device, dev);
[2211]1272 paranoid_free(dev);
[1]1273#else
[2211]1274 mr_strcat(options, ",loop");
[1]1275#endif
1276
[128]1277 }
[3191]1278 log_msg(4, "(mount_CDROM_here --- device=%s, mountpoint=%s", device, mountpoint);
[128]1279 /*@ end vars *************************************************** */
[1]1280
1281#ifdef __FreeBSD__
[3185]1282 mr_asprintf(command, "mount_cd9660 -r %s %s 2>> %s", device, mountpoint, MONDO_LOGFILE);
[1]1283
1284#else
[3185]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);
[3191]1295 mr_free(command);
[1]1296
[128]1297 return (retval);
[1]1298}
1299
1300
[2682]1301/**
1302* Mount the CD-ROM or USB device at /mnt/cdrom.
1303* @param bkpinfo The backup information structure. Fields used:
1304* - @c bkpinfo->backup_media_type
1305* - @c bkpinfo->disaster_recovery
1306* - @c bkpinfo->isodir
1307* - @c bkpinfo->media_device
1308* @return 0 for success, nonzero for failure.
1309*/
1310int mount_media()
1311{
[3191]1312char *mount_cmd = NULL;
[2769]1313char *mountdir = NULL;
[2682]1314int i, res;
1315#ifdef __FreeBSD__
[3191]1316 char mdd[32];
1317 char *mddev = mdd;
[2682]1318#endif
[1]1319
[3191]1320 if (bkpinfo->backup_media_type == tape || bkpinfo->backup_media_type == udev) {
[2682]1321 log_msg(8, "Tape/udev. Therefore, no need to mount a media.");
1322 return 0;
1323 }
[1]1324
[2682]1325 if (!run_program_and_log_output("mount | grep -F " MNT_CDROM, FALSE)) {
1326 log_msg(2, "mount_media() - media already mounted. Fair enough.");
1327 return (0);
1328 }
[1]1329
[2682]1330 if (bkpinfo->backup_media_type == netfs) {
1331 log_msg(2, "Mounting for Network thingy");
1332 log_msg(2, "isodir = %s", bkpinfo->isodir);
[3191]1333 if ((!bkpinfo->isodir[0] || !strcmp(bkpinfo->isodir, "/")) && am_I_in_disaster_recovery_mode()) {
[2682]1334 strcpy(bkpinfo->isodir, "/tmp/isodir");
1335 log_msg(1, "isodir is being set to %s", bkpinfo->isodir);
1336 }
1337#ifdef __FreeBSD__
[3207]1338 if (bkpinfo->netfs_remote_dir != NULL) {
[3196]1339 // NETFS
1340 mr_asprintf(mount_cmd, "/mnt/isodir/%s/%s/%s-%d.iso", bkpinfo->isodir, bkpinfo->netfs_remote_dir, bkpinfo->prefix, g_current_media_number);
1341 } else {
1342 // ISO
1343 mr_asprintf(mount_cmd, "/mnt/isodir/%s/%s-%d.iso", bkpinfo->isodir, bkpinfo->prefix, g_current_media_number);
1344 }
[2682]1345 mddev = make_vn(mount_cmd);
[3191]1346 mr_free(mount_cmd);
1347
1348 mr_asprintf(mount_cmd, "mount_cd9660 -r %s " MNT_CDROM, mddev);
[2682]1349#else
[3207]1350 if (bkpinfo->netfs_remote_dir != NULL) {
[3196]1351 // NETFS
1352 mr_asprintf(mount_cmd, "mount %s/%s/%s-%d.iso -t iso9660 -o loop,ro %s", bkpinfo->isodir, bkpinfo->netfs_remote_dir, bkpinfo->prefix, g_current_media_number, MNT_CDROM);
1353 } else {
1354 // ISO
1355 mr_asprintf(mount_cmd, "mount %s/%s-%d.iso -t iso9660 -o loop,ro %s", bkpinfo->isodir, bkpinfo->prefix, g_current_media_number, MNT_CDROM);
1356 }
[2682]1357#endif
1358
1359 } else if (bkpinfo->backup_media_type == iso) {
[2769]1360 if (bkpinfo->subdir) {
[3185]1361 mr_asprintf(mountdir, "%s/%s", bkpinfo->isodir, bkpinfo->subdir);
[2769]1362 } else {
[3185]1363 mr_asprintf(mountdir, "%s", bkpinfo->isodir);
[2769]1364 }
[2682]1365#ifdef __FreeBSD__
[3191]1366 mr_asprintf(mount_cmd, "%s/%s-%d.iso", mountdir, bkpinfo->prefix, g_current_media_number);
[2682]1367 mddev = make_vn(mount_cmd);
[3191]1368 mr_free(mount_cmd);
1369
1370 mr_asprintf(mount_cmd, "mount_cd9660 -r %s %s", mddev, MNT_CDROM);
[2682]1371#else
[3191]1372 mr_asprintf(mount_cmd, "mount %s/%s-%d.iso -t iso9660 -o loop,ro %s", mountdir, bkpinfo->prefix, g_current_media_number, MNT_CDROM);
[2682]1373#endif
[2774]1374 mr_free(mountdir);
[2682]1375 } else if (bkpinfo->backup_media_type == usb) {
[3191]1376 mr_asprintf(mount_cmd, "mount -t vfat %s %s", bkpinfo->media_device, MNT_CDROM);
[2682]1377 } else if (strstr(bkpinfo->media_device, "/dev/")) {
1378#ifdef __FreeBSD__
[3191]1379 mr_asprintf(mount_cmd, "mount_cd9660 -r %s %s", bkpinfo->media_device, MNT_CDROM);
[2682]1380#else
[3191]1381 mr_asprintf(mount_cmd, "mount %s -t iso9660 -o ro %s", bkpinfo->media_device, MNT_CDROM);
[2682]1382#endif
1383 } else {
1384 if (bkpinfo->disaster_recovery
1385 && does_file_exist("/tmp/CDROM-LIVES-HERE")) {
1386 strcpy(bkpinfo->media_device,
1387 last_line_of_file("/tmp/CDROM-LIVES-HERE"));
1388 } else {
1389 find_cdrom_device(bkpinfo->media_device, TRUE);
1390 }
1391
1392#ifdef __FreeBSD__
[3191]1393 mr_asprintf(mount_cmd, "mount_cd9660 -r %s %s", bkpinfo->media_device, MNT_CDROM);
[2682]1394#else
[3191]1395 mr_asprintf(mount_cmd, "mount %s -t iso9660 -o ro %s", bkpinfo->media_device, MNT_CDROM);
[2682]1396#endif
1397 }
1398
1399 log_msg(2, "(mount_media) --- command = %s", mount_cmd);
1400 for (i = 0; i < 2; i++) {
1401 res = run_program_and_log_output(mount_cmd, FALSE);
1402 if (!res) {
1403 break;
1404 } else {
1405 log_msg(2, "Failed to mount device.");
1406 sleep(5);
[3191]1407 sync();
[2682]1408 }
1409 }
[3191]1410 mr_free(mount_cmd);
[2682]1411
1412 if (res) {
1413 log_msg(2, "Failed, despite %d attempts", i);
1414 } else {
1415 log_msg(2, "Mounted media drive OK");
1416 }
1417 return (res);
1418}
1419/**************************************************************************
1420*END_MOUNT_CDROM *
1421**************************************************************************/
1422
1423
1424
1425
[1]1426/**
1427 * Ask the user for CD number @p cd_number_i_want.
1428 * Sets g_current_media_number once the correct CD is inserted.
1429 * @param bkpinfo The backup information structure. Fields used:
1430 * - @c bkpinfo->backup_media_type
[20]1431 * - @c bkpinfo->prefix
[1]1432 * - @c bkpinfo->isodir
1433 * - @c bkpinfo->media_device
1434 * - @c bkpinfo->please_dont_eject_when_restoring
1435 * @param cd_number_i_want The CD number to ask for.
1436 */
1437void
[1645]1438insist_on_this_cd_number(int cd_number_i_want)
[1]1439{
1440
[128]1441 /*@ int ************************************************************* */
1442 int res = 0;
[1]1443
1444
[128]1445 /*@ buffers ********************************************************* */
[3191]1446 char *tmp = NULL;
[2242]1447 char *mds = NULL;
[3191]1448 char *request = NULL;
[1]1449
[128]1450 assert(bkpinfo != NULL);
1451 assert(cd_number_i_want > 0);
[1]1452
1453// log_msg(3, "Insisting on CD number %d", cd_number_i_want);
1454
[128]1455 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1456 log_msg(3,
1457 "No need to insist_on_this_cd_number when the backup type isn't CD-R(W) or NFS or ISO");
1458 return;
1459 }
[3191]1460 mr_asprintf(tmp, "mkdir -p " MNT_CDROM);
1461 run_program_and_log_output(tmp, 5);
1462 mr_free(tmp);
1463
1464 if (g_ISO_restore_mode || bkpinfo->backup_media_type == iso || bkpinfo->backup_media_type == netfs) {
[2866]1465 g_ISO_restore_mode = TRUE;
1466 }
[1645]1467 if ((res = what_number_cd_is_this()) != cd_number_i_want) {
[3191]1468 log_msg(3, "Currently, we hold %d but we want %d", res, cd_number_i_want);
[2866]1469
1470 /* Now we need to umount the current media to have the next mounted after */
[2878]1471 run_program_and_log_output("umount -d " MNT_CDROM, FALSE);
[2866]1472 log_msg(3, "Mounting next media %d",cd_number_i_want);
1473 g_current_media_number = cd_number_i_want;
1474 mount_media();
1475
[2242]1476 mds = media_descriptor_string(bkpinfo->backup_media_type);
[3191]1477 log_msg(3, "Insisting on %s #%d", mds, cd_number_i_want);
1478 mr_asprintf(request, "Please insert %s #%d and press Enter.", mds, cd_number_i_want);
[2242]1479 mr_free(mds);
[3191]1480
[1645]1481 while (what_number_cd_is_this() != cd_number_i_want) {
[3191]1482 sync();
[128]1483 if (is_this_device_mounted(MNT_CDROM)) {
1484 res =
[2878]1485 run_program_and_log_output("umount -d " MNT_CDROM, FALSE);
[128]1486 } else {
1487 res = 0;
1488 }
1489 if (res) {
[541]1490 log_to_screen("WARNING - failed to unmount CD-ROM drive");
[128]1491 }
1492 if (!bkpinfo->please_dont_eject) {
1493 res = eject_device(bkpinfo->media_device);
1494 } else {
1495 res = 0;
1496 }
1497 if (res) {
[541]1498 log_to_screen("WARNING - failed to eject CD-ROM disk");
[128]1499 }
1500 popup_and_OK(request);
1501 if (!bkpinfo->please_dont_eject) {
1502 inject_device(bkpinfo->media_device);
1503 }
[3191]1504 sync();
[128]1505 }
[3191]1506 mr_free(request);
1507
[128]1508 log_msg(1, "Thankyou. Proceeding...");
1509 g_current_media_number = cd_number_i_want;
1510 }
[1]1511}
1512
1513/* @} - end of deviceGroup */
1514
1515
1516
[2434]1517/**
1518 * Creates a singly linked list of all of the non-NETFS mounted file systems.
1519 * @param DSFptr A pointer to the structure MOUNTED_FS_STRUCT used to hold
1520 * the list of mounted file systems.
1521 * @return None.
1522 */
1523static void add_mounted_fs_struct (MOUNTED_FS_STRUCT *DSFptr)
1524{
1525 assert (DSFptr);
1526 if (DSF_Head == NULL) {
1527 DSF_Head = DSFptr;
1528 } else {
1529 DSF_Tail->next = DSFptr;
1530 }
1531 DSFptr->next = NULL;
1532 DSF_Tail = DSFptr;
1533}
[1]1534
[2434]1535/**
1536 * Find the structure, in the singly linked list of all of the non-NETFS
1537 * mounted file systems, that contains the specified device.
1538 * @param device The device to find
1539 * @return NULL if it didn't find the device, a pointer to the
1540 * structure if it did.
1541 */
1542static MOUNTED_FS_STRUCT *find_device_in_list (char *device)
1543{
1544 MOUNTED_FS_STRUCT *DSFptr = NULL;
[1]1545
[2434]1546 DSFptr = DSF_Head;
1547 while (DSFptr != NULL) {
1548 if (!strcmp(DSFptr->device, device)) {
1549 break;
1550 }
1551 DSFptr = DSFptr->next;
1552 }
1553 return (DSFptr);
1554}
[1]1555
1556/**
[2434]1557 * Find the structure, in the singly linked list of all of the non-NETFS
1558 * mounted file systems, that contains the specified mount point.
1559 * @param mount_point The mount point to find
1560 * @return NULL is it didn't find the mount point, a pointer to the
1561 * structure if it did.
1562 */
1563static MOUNTED_FS_STRUCT *find_mount_point_in_list (char *mount_point)
1564{
1565 MOUNTED_FS_STRUCT *DSFptr = NULL;
1566
1567 DSFptr = DSF_Head;
1568 while (DSFptr != NULL) {
1569 if (!strcmp(DSFptr->mount_point, mount_point)) {
1570 break;
1571 }
1572 DSFptr = DSFptr->next;
1573 }
1574 return (DSFptr);
1575}
1576
1577/**
1578 * Frees the memory for all of the structures on the linked list of
1579 * all of the non-NETFS mounted file systems.
1580 */
1581static void free_mounted_fs_list (void) {
1582 MOUNTED_FS_STRUCT *DSFptr = NULL;
1583 MOUNTED_FS_STRUCT *DSFnext = NULL;
1584
1585 DSFptr = DSF_Head;
1586 while (DSFptr != NULL) {
1587 DSFnext = DSFptr->next;
1588 paranoid_free(DSFptr);
1589 DSFptr = DSFnext;
1590 }
1591 DSF_Head = NULL;
1592 DSF_Tail = NULL;
1593}
1594
1595
1596/**
1597 * Creates a linked list of all of the non-NETFS mounted file systems.
1598 * We use a linked list because we don't know how many mounted file
1599 * there are (and there can be a lot).
1600 * @return 0 on success and greated than 0 on failure.
1601 */
1602static int create_list_of_non_NETFS_mounted_file_systems (void)
1603{
1604 int i = 0;
1605 int mount_cnt = 0;
[2436]1606 int lastpos = 0;
[2434]1607 char *mounted_file_system = NULL;
1608 char *command = NULL;
1609 char *token = NULL;
1610 char token_chars[] =" :\t\r\f\a\0";
1611 MOUNTED_FS_STRUCT *DSFptr = NULL;
1612
1613 free_mounted_fs_list();
1614 /********
1615 * Find the number of mounted file system entries and their respective mount points.
1616 * I can't return all of the entries as one string because it's length can be longer
1617 * than MAX_STR_LEN which is used in call_program_and_get_last_line_of_output().
1618 * So start looping and get the number of mounted file systems and query them one by one.
1619 ********/
1620 /* Get the number of mounted file systems ((those that start with "/dev/" */
[3185]1621 mr_asprintf(command, "mount 2>/dev/null | awk '{if($1 ~ \"^/dev/\"){print $0}}'|wc -l");
[2434]1622 log_msg(5, "Running: %s", command);
[3185]1623 mr_asprintf(mounted_file_system, "%s", call_program_and_get_last_line_of_output(command));
[3191]1624 mr_free(command);
[2434]1625
1626 mount_cnt = atoi(mounted_file_system);
1627 log_msg (5, "mount_cnt: %d", mount_cnt);
[3191]1628 mr_free(mounted_file_system);
[2434]1629
1630 for (i=mount_cnt; i > 0; i--) {
[3185]1631 mr_asprintf(command, "mount 2>/dev/null | awk '{if($1 ~ \"^/dev/\"){print $1,$3}}'|head -n %d", i);
[2434]1632 log_msg(5, "Running: %s", command);
[3185]1633 mr_asprintf(mounted_file_system, "%s", call_program_and_get_last_line_of_output(command));
[3191]1634 mr_free(command);
[2434]1635
1636 log_msg (5, "mounted_file_system: %s", mounted_file_system);
[2436]1637 if ((token = mr_strtok(mounted_file_system, token_chars, &lastpos)) == NULL) {
[2434]1638 log_msg (4, "Could not get the list of mounted file systems");
[3191]1639 mr_free(mounted_file_system);
[2434]1640 mr_free(token);
1641 return (1);
1642 }
1643 if (token) {
1644 log_msg (5, "token: %s", token);
1645 }
1646 while (token != NULL) {
1647 log_msg (5, "token: %s", token);
1648 if ((DSFptr = (MOUNTED_FS_STRUCT *) calloc(1, sizeof(MOUNTED_FS_STRUCT))) == NULL) {
1649 fatal_error ("Cannot allocate memory");
1650 }
1651 add_mounted_fs_struct(DSFptr);
1652 strcpy(DSFptr->device, token);
1653 mr_free(token);
[2436]1654 if ((token = mr_strtok(mounted_file_system, token_chars, &lastpos)) == NULL) {
[2434]1655 log_msg (5, "Ran out of entries on the mounted file systems list");
[2436]1656 mr_free(mounted_file_system);
[2434]1657 mr_free(token);
1658 return (1);
1659 }
1660 log_msg (5, "token: %s", token);
1661 strcpy(DSFptr->mount_point, token);
1662 mr_free(token);
[2436]1663 token = mr_strtok(mounted_file_system, token_chars, &lastpos);
[2434]1664 }
[2436]1665 mr_free(mounted_file_system);
[2434]1666 }
1667 return (0);
1668}
1669
1670
1671
1672/**
1673 * Given a whole disk device special file, determine which mounted file systems
1674 * are on the dsf's partitions and which mounted file systems are not.
1675 * @param dsf The whole disk device special file.
1676 * @param included_dsf_list A char pointer used to hold the list of mount points
1677 * that are on the dsf. Memory for the array will be allocated within the function.
1678 * @param excluded_dsf_list A char pointer used to hold the list of mount points
1679 * that are not on the dsf. Memory for the array will be allocated within the function.
1680 * @return 0 on success, -1 if no device special file was passed in, -2 if a device
1681 * special file was passed in but it has no partitions on it, or 1 on failure
1682 */
1683static int get_dsf_mount_list (const char *dsf, char **included_dsf_list, char **excluded_dsf_list) {
1684 int i = 0;
1685 int c = 0;
1686 int lastpos = 0;
[3288]1687 char *VG = NULL;
[2434]1688 char *tmp = NULL;
1689 char *command = NULL;
1690 char *partition_list = NULL;
1691 char partitions[64][MAX_STR_LEN];
1692 char *mount_list = NULL;
1693 char *token = NULL;
[2436]1694 char *ndsf = NULL;
[2434]1695 char token_chars[] =" \t\r\f\a\0";
1696 MOUNTED_FS_STRUCT *DSFptr = NULL;
1697
1698 memset((char *)partitions, 0, sizeof(partitions));
1699
1700 log_msg(5, "dsf: %s", dsf);
1701
1702 /********
1703 * See if a device special file was passed in (i.e. it must start with /dev/
1704 ********/
1705 if (strncmp(dsf, "/dev/", 5)) {
[2527]1706 log_msg (4, "%s does not start with /dev/ and (probably) is not a device special file", dsf);
[2434]1707 return (-1);
1708 }
[2527]1709 log_msg(4, " %s looks like a device special file", dsf);
[2434]1710 /* Verify that the dsf exists */
[3185]1711 mr_asprintf(command, "ls -al %s 2>/dev/null | wc -l", dsf);
[2434]1712 log_msg(5, " Executing: %s", command);
[3185]1713 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
[3191]1714 mr_free(command);
[2434]1715
1716 log_msg(5, " Return value: %s", tmp);
1717 c = atoi(tmp);
[3191]1718 mr_free(tmp);
[2434]1719
1720 if (!c) {
1721 log_to_screen("Cannot find device special file %s", dsf);
1722 return (1);
1723 }
[2527]1724 log_msg(4, " %s device special file exists", dsf);
[2434]1725
1726 /* Get a list of the mounted file systems */
1727 if (create_list_of_non_NETFS_mounted_file_systems()) {
1728 log_to_screen ("Could not get the list of mounted file systems");
1729 return (1);
1730 }
1731 log_msg (5, "Processing dsf: %s", dsf);
1732 /********
1733 * Get a list of the dsf's partitions. There could be no partitions on the disk
1734 * or a dsf of a partition was passed in (e.g. /dev/sda1 instead of /dev/sda).
1735 * Either way, it's an error.
1736 ********/
[3185]1737 mr_asprintf(command, "parted2fdisk -l %s 2>/dev/null|grep -E \"^/dev/\"|awk '{printf(\"%%s \", $1)}END{print \"\"}'", dsf);
[2527]1738 log_msg(5, "Executing: %s", command);
[3185]1739 mr_asprintf(partition_list, "%s", call_program_and_get_last_line_of_output(command));
[3191]1740 mr_free(command);
[2434]1741 log_msg(4, "Partition list for %s: %s", dsf, partition_list);
1742 if (!strlen(partition_list)) {
1743 /* There were no partitions on the disk */
[2436]1744 log_msg(4, "No partitions on device special file %s", dsf);
[2453]1745 log_msg(4, "I guess it's a partition itself");
[2436]1746 strcpy(partitions[0], dsf);
1747 ndsf = truncate_to_drive_name(dsf);
1748 } else {
1749 /* Fill the partition list */
1750 i = 0;
1751 lastpos = 0;
1752 while ((token = mr_strtok(partition_list, token_chars, &lastpos)) != NULL) {
[2527]1753 log_msg (4, "Found partition: %s", token);
[2436]1754 strcpy(partitions[i++], token);
1755 mr_free(token);
1756 }
[3185]1757 mr_asprintf(ndsf, "%s", dsf);
[2434]1758 }
[2436]1759 mr_free(partition_list);
[2527]1760
1761 /* In any case we want to exclude the dsf itself from all MondRescue activities
1762 * at restore time (LVM, fdisk, ...) so we want it in our exclude_dev list */
[2528]1763 if ((DSFptr = (MOUNTED_FS_STRUCT *) calloc(1, sizeof(MOUNTED_FS_STRUCT))) == NULL) {
1764 fatal_error ("Cannot allocate memory");
[2527]1765 }
[2528]1766 add_mounted_fs_struct(DSFptr);
1767 strcpy(DSFptr->device, dsf);
[2527]1768 DSFptr->check = 1;
1769
[2436]1770 /* For the rest ndsf is the new dsf to deal with */
[2434]1771 /********
1772 * At this point, we have a list of all of the partitions on the dsf. Now try to
1773 * see which partitions have a file system on them.
1774 *
1775 * Loop through each partition on the disk and:
1776 *
1777 * - If the partition is swap, it ignores it.
1778 *
1779 * - If the partition is mounted (e.g. /dev/sda1 is mounted on /boot), it adds an entry
1780 * to the linked list, copies to it the device name and mount point, and sets check == 1.
1781 *
1782 * - If the partition is part of a Volume Group that has Logical Volumes mounted, it adds
1783 * an entry to the linked list for each mounted Logical Volume in that Volume Group, copying
1784 * to it the device name and mount point, and sets check == 1. Note that if the Volume Group
1785 * contains more than one disk, it will still add the entry even if the Logical Volume's
1786 * extents are not on the dsf that was passed in to the function. For example, Volume Group
1787 * VolGroup00 contains the disks /dev/sda1 and /dev/sdb1, and the Logical Volumes LogVol01,
1788 * which is mounted on /var and has all of its extents on /dev/sda1, and LogVol02, which is
1789 * mounted as /usr and has all of its extents on /dev/sdb1. If you pass /dev/sda into the
1790 * function, both /var and /usr will be archived even though /usr is actually on/dev/sdb.
1791 *
1792 * - If the partition is part of a Volume Group that has Logical Volumes used in a mounted
1793 * software raid device, it adds an entry to the linked list, copies to it the software raid
1794 * device name and mount point, and sets check == 1.
1795 *
1796 * - If the partition is part of a mounted software raid device, it adds an entry to the linked
1797 * list, copies to it the software raid device name and mount point, and sets check == 1.
1798 *
1799 ********/
1800 for (i=0; strlen(partitions[i]); i++) {
1801 log_msg(4, "Processing partition: %s", partitions[i]);
1802 /* See if it's swap. If it is, ignore it. */
[3191]1803 mr_asprintf(command, "parted2fdisk -l %s 2>/dev/null | awk '{if(($1==\"%s\")&&(toupper($0) ~ \"SWAP\")){print $1;exit}}'", ndsf, partitions[i]);
[2527]1804 log_msg(5, " Running: %s", command);
[3185]1805 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
[3191]1806 mr_free(command);
1807
[2527]1808 log_msg(5, " Return value: %s", tmp);
[2434]1809 c = strlen(tmp);
[3191]1810 mr_free(tmp);
1811
[2434]1812 if (c) {
1813 log_msg(4, "It's swap. Ignoring partition %s", partitions[i]);
1814 continue;
1815 }
[3191]1816
[2434]1817 /* It's not swap. See if we can find the mount point from the mount command. */
[3185]1818 mr_asprintf(command, "mount 2>/dev/null | awk '{if((NF>0)&&($1==\"%s\")){print $3}}'", partitions[i]);
1819 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
[3191]1820 mr_free(command);
1821
[2434]1822 if (strlen(tmp)) {
1823 log_msg(4, " %s is mounted: %s", partitions[i], tmp);
1824 if ((DSFptr = find_mount_point_in_list(tmp)) == NULL) {
1825 log_msg (4, "Can't find mount point %s in mounted file systems list", tmp);
[3191]1826 mr_free(tmp);
[2434]1827 return (1);
1828 }
1829 DSFptr->check = 1;
[3191]1830 mr_free(tmp);
[2434]1831 continue;
1832 }
[3191]1833 mr_free(tmp);
1834
[2434]1835 /* It's not swap and it's not mounted. See if it's LVM */
1836 log_msg(4, " It's not mounted. Checking to see if it's LVM...");
[3191]1837
[2527]1838 /* Check for LVM */
[3185]1839 mr_asprintf(command, "pvdisplay -c %s 2> /dev/null", partitions[i]);
[2527]1840 log_msg(5, " Running: %s", command);
[3185]1841 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
[3191]1842 mr_free(command);
1843
[2434]1844 if (strlen(tmp)) {
[2527]1845 log_msg(4, "Found an LVM partition at %s. Find the VG it's in...", partitions[i]);
1846 /* It's LVM: Find the VG it's in */
[3185]1847 mr_asprintf(command, "pvdisplay -v %s 2>/dev/null|grep \"VG Name\"|awk '{print $NF}'", partitions[i]);
[2527]1848 log_msg(5, " Running: %s", command);
[3288]1849 mr_asprintf(VG, "%s", call_program_and_get_last_line_of_output(command));
[3191]1850 mr_free(command);
1851
[2527]1852 log_msg(4, " Volume Group: %s", VG);
1853 if (strlen(VG)) {
1854 /* Found the Volume Group. Now find all of the VG's mount points */
1855 log_msg(4, " Found the Volume Group. Now find all of the VG's mount points");
[3185]1856 mr_asprintf(command, "mount 2>/dev/null|grep -E \"/dev/mapper/%s|/dev/%s/\"|awk '{printf(\"%%s \",$3)}END{print \"\"}'", VG, VG);
[2527]1857 log_msg(5, " Running: %s", command);
[3185]1858 mr_asprintf(mount_list, "%s", call_program_and_get_last_line_of_output(command));
[3191]1859 mr_free(command);
1860
[2527]1861 log_msg(4, " VG %s mount_list: %s", VG, mount_list);
1862 lastpos = 0;
1863 while ((token = mr_strtok(mount_list, token_chars, &lastpos)) != NULL) {
1864 log_msg (5, "mount point token: %s", token);
1865 if ((DSFptr = find_mount_point_in_list(token)) == NULL) {
1866 log_msg (4, "Can't find mount point %s in mounted file systems list", token);
[3191]1867 mr_free(tmp);
[2527]1868 mr_free(token);
1869 return (1);
1870 }
1871 DSFptr->check = 1;
1872 mr_free(token);
1873 }
1874 /********
1875 * Now we want to see if there are any software raid devices using
1876 * any of the Logical Volumes on the Volume Group.
1877 *******/
[3191]1878 mr_free(mount_list);
[2527]1879
[3185]1880 mr_asprintf(command, "%s", "cat /proc/mdstat|grep -iv Personal|awk '{if($0~\"^.*[ ]+:\"){printf(\"/dev/%s \", $1)}}END{print \"\"}'");
[2527]1881 log_msg (5, "Running: %s", command);
[3185]1882 mr_asprintf(mount_list, "%s", call_program_and_get_last_line_of_output(command));
[3191]1883 mr_free(command);
[2527]1884 log_msg(4, " Software raid device list: %s", mount_list);
1885 lastpos = 0;
1886 while ((token = mr_strtok(mount_list, token_chars, &lastpos)) != NULL) {
[3185]1887 mr_asprintf(command, "mdadm --detail %s 2>/dev/null | grep -c %s", token, VG);
[2527]1888 log_msg (5, "Running: %s", command);
[3191]1889 mr_free(tmp);
[3185]1890 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
[3191]1891 mr_free(command);
[2527]1892 log_msg(4, "Number of Software raid device: %s", tmp);
1893 if (atoi(tmp)) {
1894 /* This device is on our disk */
1895 if ((DSFptr = find_device_in_list(token)) == NULL) {
1896 log_msg (4, "Can't find device %s in mounted file systems list", token);
[3191]1897 mr_free(tmp);
[2434]1898 mr_free(token);
1899 return (1);
1900 }
1901 DSFptr->check = 1;
1902 }
1903 }
[2527]1904 mr_free(token);
1905 paranoid_free(mount_list);
1906 } else {
1907 log_msg (4, "Error finding Volume Group for partition %s", partitions[i]);
[3191]1908 mr_free(tmp);
[2527]1909 return (1);
[2434]1910 }
[3191]1911 mr_free(tmp);
[3288]1912 mr_free(VG);
[2527]1913 continue;
[2434]1914 } else {
1915 log_msg (4, "Error finding partition type for the partition %s", partitions[i]);
1916 }
[3191]1917 mr_free(tmp);
1918
[2434]1919 /********
1920 * It's not swap, mounted, or LVM. See if it's used in a software raid device.
1921 ********/
1922 log_msg (5, "It's not swap, mounted, or LVM. See if it's used in a software raid device.");
[3185]1923 mr_asprintf(command, "mdadm --examine %s 2>/dev/null | awk '{if($1 == \"UUID\"){print $3}}'", partitions[i]);
[2434]1924 log_msg(4, " Running: %s", command);
[3185]1925 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
[3191]1926 mr_free(command);
1927
[2434]1928 if (!strlen(tmp)) {
1929 log_msg(4, " Partition %s is not used in a non-LVM software raid device", partitions[i]);
[3191]1930 mr_free(tmp);
[2434]1931 continue;
1932 }
1933 log_msg (5, " UUID: %s", tmp);
[3191]1934
[2434]1935 /* Get the Software raid device list */
[3185]1936 mr_asprintf(command, "%s", "cat /proc/mdstat|grep -iv Personal|awk '{if($0~\"^.*[ ]+:\"){printf(\"/dev/%s \", $1)}}END{print \"\"}'");
[2434]1937 log_msg (5, " Running: %s", command);
[3185]1938 mr_asprintf(mount_list, "%s", call_program_and_get_last_line_of_output(command));
[3191]1939 mr_free(command);
1940
[2434]1941 log_msg(4, " Software raid device list: %s", mount_list);
1942 /* Loop through the software raid device list to see if we can find the partition */
1943 lastpos = 0;
1944 while ((token = mr_strtok(mount_list, token_chars, &lastpos)) != NULL) {
[3185]1945 mr_asprintf(command, "mdadm --detail %s 2>/dev/null | grep -c %s", token, tmp);
[2434]1946 log_msg(4, " Running: %s", command);
[3191]1947 mr_free(tmp);
[3185]1948 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
[3191]1949 mr_free(command);
1950
[2434]1951 if (!atoi(tmp)) {
1952 log_msg (4," Didn't find partition %s in software raid device %s", partitions[i], token);
1953 } else {
1954 if ((DSFptr = find_device_in_list(token)) == NULL) {
1955 log_msg (4, "Can't find device %s in mounted file systems list", token);
[3191]1956 mr_free(tmp);
[2434]1957 mr_free(token);
1958 return (1);
1959 }
1960 DSFptr->check = 1;
1961 break;
1962 }
1963 mr_free(token);
1964 }
[3191]1965 mr_free(tmp);
1966 mr_free(mount_list);
[2434]1967 }
1968
1969 /* Determine how much memory to allocate for included_dsf_list and excluded_dsf_list */
1970 i = 0;
1971 DSFptr= DSF_Head;
1972 while (DSFptr != NULL) {
1973 i += strlen(DSFptr->mount_point) + 1;
1974 DSFptr = DSFptr->next;
1975 }
1976 log_msg (5, "i: %d", i);
1977 if ((*included_dsf_list = (char *) calloc(i+100, sizeof(char))) == NULL) {
1978 fatal_error ("Cannot allocate memory");
1979 }
1980 if ((*excluded_dsf_list = (char *) calloc(i+100, sizeof(char))) == NULL) {
1981 fatal_error ("Cannot allocate memory");
1982 }
1983 DSFptr= DSF_Head;
1984 while (DSFptr != NULL) {
1985 if (DSFptr->check) {
[2632]1986 log_msg (4, "%s is mounted on %s and is on disk %s", DSFptr->device, DSFptr->mount_point, ndsf);
[2434]1987 strcat(*included_dsf_list, DSFptr->mount_point);
[2713]1988 strcat(*included_dsf_list, "|");
[2434]1989 } else {
[2632]1990 log_msg (4, "%s is mounted on %s and is NOT on disk %s", DSFptr->device, DSFptr->mount_point, ndsf);
[2434]1991 strcat(*excluded_dsf_list, DSFptr->mount_point);
[2713]1992 strcat(*excluded_dsf_list, "|");
[2434]1993 }
1994 DSFptr = DSFptr->next;
1995 }
[2436]1996 mr_free(ndsf);
1997
[2434]1998 log_msg (5, "included_dsf_list: %s", *included_dsf_list);
1999 log_msg (5, "excluded_dsf_list: %s", *excluded_dsf_list);
2000 return (0);
2001}
2002
2003
2004/* Update the bkpinfo structure for exclude & include paths
2005 * in order to handle correctly paths corresponding to devices */
2006void mr_make_devlist_from_pathlist(char *pathlist, char mode) {
2007
2008char *token = NULL;
2009int lastpos = 0;
2010char *mounted_on_dsf = NULL;
2011char *not_mounted_on_dsf = NULL;
[2713]2012char token_chars[] ="|\t\r\f\a\0\n";
[2525]2013char *tmp = NULL;
2014char *tmp1 = NULL;
[2934]2015char *tmp2 = NULL;
[2434]2016
[2745]2017if (pathlist == NULL) {
2018 return;
2019}
[2434]2020while ((token = mr_strtok(pathlist, token_chars, &lastpos)) != NULL) {
2021 switch (get_dsf_mount_list(token, &mounted_on_dsf, &not_mounted_on_dsf)) {
2022 case 1:
2023 if (mode == 'E') {
2024 log_msg(1, "WARNING ! %s doesn't exist in -E option", token);
2025 } else {
[2453]2026 log_msg(1, "ERROR ! %s doesn't exist in -I option", token);
[2434]2027 fatal_error("Error processing -I option");
2028 }
2029 break;
2030 /* Everything is OK; proceed to archive data */
2031 case 0:
2032 if (mode == 'E') {
2033 if (strlen(mounted_on_dsf)) {
[2632]2034 log_to_screen("Excluding the following file systems on %s:", token);
2035 log_to_screen("==> %s", mounted_on_dsf);
[2434]2036 log_msg (5, "Adding to bkpinfo->exclude_paths due to -E option: %s", mounted_on_dsf);
[2713]2037 if (bkpinfo->exclude_paths) {
2038 mr_strcat(bkpinfo->exclude_paths,"|%s",mounted_on_dsf);
2039 } else {
[3185]2040 mr_asprintf(bkpinfo->exclude_paths,"%s",mounted_on_dsf);
[2713]2041 }
2042 if (bkpinfo->exclude_devs) {
2043 mr_strcat(bkpinfo->exclude_devs,"|%s",token);
2044 } else {
[3185]2045 mr_asprintf(bkpinfo->exclude_devs,"%s",token);
[2713]2046 }
[2434]2047 }
[2453]2048 } else {
[2632]2049 log_to_screen("Archiving only the following file systems on %s:", token);
[2636]2050 log_to_screen("==> %s", mounted_on_dsf);
[3191]2051 mr_free(bkpinfo->include_paths);
2052 mr_asprintf(bkpinfo->include_paths, "%s", "/");
[2453]2053 if (strlen(not_mounted_on_dsf)) {
2054 log_msg (5, "Adding to bkpinfo->exclude_paths due to -I option: %s", not_mounted_on_dsf);
[2632]2055 log_to_screen("Not archiving the following file systems:");
2056 log_to_screen("==> %s", not_mounted_on_dsf);
[2713]2057 if (bkpinfo->exclude_paths) {
2058 mr_strcat(bkpinfo->exclude_paths, "|%s",not_mounted_on_dsf);
2059 } else {
[3185]2060 mr_asprintf(bkpinfo->exclude_paths,"%s",not_mounted_on_dsf);
[2713]2061 }
[2453]2062 }
[2434]2063 }
2064 break;
2065 /* It's a dsf but not a whole disk dsf */
2066 case -2:
2067 log_to_screen("Could %s be a partition instead of a whole disk device special file?\nIgnored.", token);
2068 break;
2069 /* A device special file was not passed in. Process it as a path. */
2070 case -1:
[2713]2071 /* Adds a | to ensure correct detection even at both ends */
[3185]2072 mr_asprintf(tmp1,"|%s",token);
2073 mr_asprintf(tmp2,"|%s|",token);
[2434]2074 if (mode == 'E') {
[2473]2075 /* Add the token if not already in the list */
[3185]2076 mr_asprintf(tmp,"|%s|",bkpinfo->exclude_paths);
[2934]2077 if (strstr(tmp,tmp2) == NULL) {
[2713]2078 if (bkpinfo->exclude_paths) {
2079 mr_strcat(bkpinfo->exclude_paths,tmp1);
2080 mr_free(tmp1);
2081 } else {
2082 bkpinfo->exclude_paths = tmp1;
2083 }
[2473]2084 }
[2434]2085 } else {
[2473]2086 /* Add the token if not already in the list */
[3185]2087 mr_asprintf(tmp,"|%s|",bkpinfo->include_paths);
[2934]2088 if (strstr(tmp,tmp2) == NULL) {
[3191]2089 mr_strcat(bkpinfo->include_paths, "%s", tmp1);
[2473]2090 }
[2713]2091 mr_free(tmp1);
[2434]2092 }
[2525]2093 mr_free(tmp);
[2934]2094 mr_free(tmp2);
[2434]2095 break;
2096 }
2097 mr_free(token);
2098
2099 if (bkpinfo->include_paths != NULL) {
2100 log_msg(1, "include_paths is now '%s'", bkpinfo->include_paths);
2101 }
2102 if (bkpinfo->exclude_paths != NULL) {
2103 log_msg(1, "exclude_paths is now '%s'", bkpinfo->exclude_paths);
2104 }
2105 if (bkpinfo->exclude_devs != NULL) {
2106 log_msg(1, "exclude_devs is now '%s'", bkpinfo->exclude_devs);
2107 }
2108}
2109}
2110
2111
2112
2113
2114/**
[1]2115 * Ask user for details of backup/restore information.
2116 * Called when @c mondoarchive doesn't get any parameters.
2117 * @param bkpinfo The backup information structure to fill out with the user's data.
2118 * @param archiving_to_media TRUE if archiving, FALSE if restoring.
2119 * @return 0, always.
2120 * @bug No point of `int' return value.
2121 * @ingroup archiveGroup
2122 */
[1645]2123int interactively_obtain_media_parameters_from_user(bool archiving_to_media)
[1]2124// archiving_to_media is TRUE if I'm being called by mondoarchive
2125// archiving_to_media is FALSE if I'm being called by mondorestore
2126{
[2214]2127 char *tmp = NULL;
[3154]2128 char *sz = NULL;
[3111]2129 char *tmpro = NULL;
[2304]2130 char *tmp1 = NULL;
[2242]2131 char *mds = NULL;
[3212]2132 char *oldtmp = NULL;
[2847]2133 char *q = NULL;
[3062]2134 char p[16*MAX_STR_LEN];
[3191]2135 char *sz_size = NULL;
2136 char *command = NULL;
[2771]2137 char *compression_type = NULL;
[3191]2138 char *comment = NULL;
[128]2139 int i;
2140 FILE *fin;
[1]2141
[2304]2142 malloc_string(tmp1);
[128]2143 assert(bkpinfo != NULL);
2144 bkpinfo->nonbootable_backup = FALSE;
[1]2145
[2380]2146 // Tape, CD, NETFS, ...?
[128]2147 srandom(getpid());
2148 bkpinfo->backup_media_type =
2149 (g_restoring_live_from_cd) ? cdr :
2150 which_backup_media_type(bkpinfo->restore_data);
2151 if (bkpinfo->backup_media_type == none) {
[541]2152 log_to_screen("User has chosen not to backup the PC");
[128]2153 finish(1);
2154 }
[1934]2155 /* Why asking to remove the media with tape ?
[128]2156 if (bkpinfo->backup_media_type == tape && bkpinfo->restore_data) {
[1885]2157 popup_and_OK("Please remove media from drive(s)");
[128]2158 }
[1934]2159 */
[3154]2160 if (archiving_to_media) {
2161 setup_tmpdir(NULL);
2162 /*
2163 * Set the scratchdir to reside on the partition with the most free space.
2164 * Automatically excludes DOS, NTFS, SMB, and NFS filesystems.
2165 */
2166#ifdef __FreeBSD__
[3185]2167 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output("LANGUAGE=C df -m -P -t nonfs,msdosfs,ntfs,ntfs-3g,vmhgfs,smbfs,smb,cifs,afs,gfs,ocfs,ocfs2,mvfs,nsspool,nssvol | grep -vE \"none|Filesystem\" | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -nr | awk '{print $NF;}' | while read x ; do test -w $x && echo $x && break ; done"));
[3154]2168#else
[3185]2169 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output("LANGUAGE=C df -m -P -x nfs -x nfs4 -x fuse.sshfs -x fuse -x vfat -x ntfs -x ntfs-3g -x vmhgfs -x smbfs -x smb -x cifs -x afs -x gfs -x ocfs -x ocfs2 -x mvfs -x nsspool -x nssvol -x iso9660 | grep -vE \"none|Filesystem|/dev/shm\" | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -nr | awk '{print $NF;}' | while read x ; do test -w $x && echo $x && break ; done"));
[3154]2170#endif
2171
2172 if (tmp[0] != '/') {
[3185]2173 mr_asprintf(sz, "%s", tmp);
[3154]2174 mr_free(tmp);
[3185]2175 mr_asprintf(tmp, "/%s", sz);
[3154]2176 mr_free(sz);
2177 }
2178 setup_scratchdir(tmp);
[3292]2179 mr_free(tmp);
[3154]2180 }
[3191]2181 log_msg(3, "media type = %s", bkptype_to_string(bkpinfo->backup_media_type));
[128]2182 bkpinfo->cdrw_speed = (bkpinfo->backup_media_type == cdstream) ? 2 : 4;
[3191]2183 bkpinfo->compression_level = (bkpinfo->backup_media_type == cdstream) ? 1 : 5;
2184 bkpinfo->use_lzo = (bkpinfo->backup_media_type == cdstream) ? TRUE : FALSE;
[128]2185 mvaddstr_and_log_it(2, 0, " ");
[1]2186
[1934]2187 // Find device's /dev (or SCSI) entry
[128]2188 switch (bkpinfo->backup_media_type) {
2189 case cdr:
2190 case cdrw:
2191 case dvd:
[1687]2192 case usb:
[1745]2193 /* Never try to eject a USB device */
2194 if (bkpinfo->backup_media_type == usb) {
[1746]2195 bkpinfo->please_dont_eject = TRUE;
[1745]2196 }
[128]2197 if (archiving_to_media) {
[1687]2198 if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) {
[3191]2199 if (ask_me_yes_or_no("Is your computer a laptop, or does the CD writer incorporate BurnProof technology?")) {
[685]2200 bkpinfo->manual_cd_tray = TRUE;
2201 }
[128]2202 }
[2771]2203 if ((compression_type = which_compression_type()) == NULL) {
2204 log_to_screen("User has chosen not to backup the PC");
2205 finish(1);
2206 }
[3191]2207
2208 if ((bkpinfo->compression_level = which_compression_level()) == -1) {
[541]2209 log_to_screen("User has chosen not to backup the PC");
[128]2210 finish(1);
2211 }
[2242]2212 mds = media_descriptor_string(bkpinfo->backup_media_type);
[3191]2213 mr_asprintf(comment, "What speed is your %s (re)writer?", mds);
[128]2214 if (bkpinfo->backup_media_type == dvd) {
2215 find_dvd_device(bkpinfo->media_device, FALSE);
[2304]2216 strcpy(tmp1, "1");
[3191]2217 mr_asprintf(sz_size, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4482 MB
[128]2218 log_msg(1, "Setting to DVD defaults");
2219 } else {
2220 strcpy(bkpinfo->media_device, VANILLA_SCSI_CDROM);
[2304]2221 strcpy(tmp1, "4");
[3191]2222 mr_asprintf(sz_size, "%d", 650);
[128]2223 log_msg(1, "Setting to CD defaults");
2224 }
[1687]2225 if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) {
[2304]2226 if (!popup_and_get_string("Speed", comment, tmp1, 4)) {
[541]2227 log_to_screen("User has chosen not to backup the PC");
[3191]2228 mr_free(comment);
[128]2229 finish(1);
2230 }
2231 }
[3191]2232 mr_free(comment);
[2304]2233 bkpinfo->cdrw_speed = atoi(tmp1); // if DVD then this shouldn't ever be used anyway :)
[2214]2234
[3191]2235 strcpy(tmp1, sz_size);
2236 mr_asprintf(comment, "How much data (in Megabytes) will each %s store?", mds);
[2242]2237 mr_free(mds);
[3191]2238 if (!popup_and_get_string("Size", comment, tmp1, 5)) {
[541]2239 log_to_screen("User has chosen not to backup the PC");
[128]2240 finish(1);
2241 }
[3191]2242 mr_asprintf(sz_size, "%s", tmp1);
[3150]2243 bkpinfo->media_size = atoi(sz_size);
[3191]2244
[3150]2245 if (bkpinfo->media_size <= 0) {
[541]2246 log_to_screen("User has chosen not to backup the PC");
[128]2247 finish(1);
2248 }
2249 }
[1737]2250 /* No break because we continue even for usb */
[128]2251 case cdstream:
[2242]2252 mds = media_descriptor_string(bkpinfo->backup_media_type);
2253
[1737]2254 if ((bkpinfo->disaster_recovery) && (bkpinfo->backup_media_type != usb)) {
[128]2255 strcpy(bkpinfo->media_device, "/dev/cdrom");
[3191]2256 log_msg(2, "CD-ROM device assumed to be at %s", bkpinfo->media_device);
2257 } else if ((bkpinfo->restore_data && (bkpinfo->backup_media_type != usb)) || bkpinfo->backup_media_type == dvd) {
[128]2258 if (!bkpinfo->media_device[0]) {
2259 strcpy(bkpinfo->media_device, "/dev/cdrom");
2260 } // just for the heck of it :)
[3191]2261 log_msg(1, "bkpinfo->media_device = %s", bkpinfo->media_device);
2262 if (bkpinfo->backup_media_type == dvd || find_cdrom_device(bkpinfo->media_device, FALSE)) {
2263 log_msg(1, "bkpinfo->media_device = %s", bkpinfo->media_device);
2264 mr_asprintf(comment, "Please specify your %s drive's /dev entry", mds);
2265 if (!popup_and_get_string("Device?", comment, bkpinfo->media_device, MAX_STR_LEN / 4)) {
[541]2266 log_to_screen("User has chosen not to backup the PC");
[128]2267 finish(1);
2268 }
2269 }
[2242]2270 log_msg(2, "%s device found at %s", mds, bkpinfo->media_device);
[128]2271 } else {
[1737]2272 if ((find_cdrw_device(bkpinfo->media_device)) && (bkpinfo->backup_media_type != usb)) {
[128]2273 bkpinfo->media_device[0] = '\0';
2274 }
2275 if (bkpinfo->media_device[0]) {
[1738]2276 if (bkpinfo->backup_media_type == usb) {
[3185]2277 mr_asprintf(tmp, "I think your %s media corresponds to %s. Is this correct?", mds, bkpinfo->media_device);
[1738]2278 } else {
[3185]2279 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]2280 }
[128]2281 if (!ask_me_yes_or_no(tmp)) {
2282 bkpinfo->media_device[0] = '\0';
2283 }
[2214]2284 mr_free(tmp);
[128]2285 }
2286 if (!bkpinfo->media_device[0]) {
[1737]2287 if (bkpinfo->backup_media_type == usb) {
[3212]2288 i = popup_and_get_string("/dev entry?", "What is the /dev entry of your USB Disk/Key, please?", bkpinfo->media_device, MAX_STR_LEN / 4);
[1737]2289 } else {
2290 if (g_kernel_version < 2.6) {
[3212]2291 i = popup_and_get_string("Device node?", "What is the SCSI node of your CD (re)writer, please?", bkpinfo->media_device, MAX_STR_LEN / 4);
[1709]2292 } else {
[3212]2293 i = popup_and_get_string("/dev entry?", "What is the /dev entry of your CD (re)writer, please?", bkpinfo->media_device, MAX_STR_LEN / 4);
[1709]2294 }
[128]2295 }
2296 if (!i) {
[541]2297 log_to_screen("User has chosen not to backup the PC");
[128]2298 finish(1);
2299 }
2300 }
2301 }
[2242]2302 mr_free(mds);
2303
[128]2304 if (bkpinfo->backup_media_type == cdstream) {
[3150]2305 bkpinfo->media_size = 650;
[128]2306 }
2307 break;
2308 case udev:
2309 if (!ask_me_yes_or_no
[541]2310 ("This option is for advanced users only. Are you sure?")) {
2311 log_to_screen("User has chosen not to backup the PC");
[128]2312 finish(1);
2313 }
2314 case tape:
[1]2315
[1969]2316 if ((!bkpinfo->restore_mode) && (find_tape_device_and_size(bkpinfo->media_device, sz_size))) {
[541]2317 log_msg(3, "Ok, using vanilla scsi tape.");
[128]2318 strcpy(bkpinfo->media_device, VANILLA_SCSI_TAPE);
2319 if ((fin = fopen(bkpinfo->media_device, "r"))) {
2320 paranoid_fclose(fin);
2321 } else {
2322 strcpy(bkpinfo->media_device, "/dev/osst0");
2323 }
2324 }
2325 if (bkpinfo->media_device[0]) {
2326 if ((fin = fopen(bkpinfo->media_device, "r"))) {
2327 paranoid_fclose(fin);
2328 } else {
[3271]2329 if (does_file_exist("/tmp/mondorestore.cfg")) {
2330 read_cfg_var("/tmp/mondorestore.cfg", "media-dev", bkpinfo->media_device);
[128]2331 }
2332 }
2333 }
2334 if (bkpinfo->media_device[0]) {
[3185]2335 mr_asprintf(tmp, "I think I've found your tape streamer at %s; am I right on the money?", bkpinfo->media_device);
[128]2336 if (!ask_me_yes_or_no(tmp)) {
2337 bkpinfo->media_device[0] = '\0';
2338 }
[2214]2339 mr_free(tmp);
[128]2340 }
2341 if (!bkpinfo->media_device[0]) {
[3212]2342 if (!popup_and_get_string("Device name?", "What is the /dev entry of your tape streamer?", bkpinfo->media_device, MAX_STR_LEN / 4)) {
[541]2343 log_to_screen("User has chosen not to backup the PC");
[128]2344 finish(1);
2345 }
2346 }
[3185]2347 mr_asprintf(tmp, "ls -l %s", bkpinfo->media_device);
[128]2348 if (run_program_and_log_output(tmp, FALSE)) {
[541]2349 log_to_screen("User has not specified a valid /dev entry");
[128]2350 finish(1);
2351 }
[2214]2352 mr_free(tmp);
2353
[3212]2354 if (!popup_and_get_string("Tape block size?", "What is the block size of your tape streamer?", sz_size, 15)) {
[3169]2355 log_to_screen("User has chosen not to backup the PC");
2356 finish(1);
[128]2357 }
[3169]2358 bkpinfo->internal_tape_block_size = atol(sz_size);
2359 if (bkpinfo->internal_tape_block_size <= 0) {
2360 log_to_screen("User has chosen not to backup the PC");
2361 finish(1);
2362 }
2363
2364 bkpinfo->media_size = 0;
[3150]2365 log_msg(4, "media_size = %ld", bkpinfo->media_size);
[3169]2366
[2993]2367 bkpinfo->use_obdr = ask_me_yes_or_no
2368 ("Do you want to activate OBDR support for your tapes ?");
2369 if (bkpinfo->use_obdr) {
2370 log_msg(4, "obdr mode = TRUE");
2371 } else {
2372 log_msg(4, "obdr mode = FALSE");
2373 }
[128]2374 if (archiving_to_media) {
[2771]2375 if ((compression_type = which_compression_type()) == NULL) {
2376 log_to_screen("User has chosen not to backup the PC");
2377 finish(1);
2378 }
[3191]2379 if ((bkpinfo->compression_level = which_compression_level()) == -1) {
[541]2380 log_to_screen("User has chosen not to backup the PC");
[128]2381 finish(1);
2382 }
2383 }
2384 break;
[1]2385
2386
2387
[2380]2388 case netfs:
2389 /* Never try to eject a NETFS device */
[1848]2390 bkpinfo->please_dont_eject = TRUE;
[3191]2391 /* Force NFS to be the protocol by default */
2392 if (bkpinfo->netfs_proto == NULL) {
2393 mr_asprintf(bkpinfo->netfs_proto, "nfs");
2394 }
[1848]2395
[2380]2396 /* Initiate bkpinfo netfs_mount path from running environment if not already done */
[3194]2397 if (bkpinfo->netfs_mount == NULL) {
2398 mr_asprintf(bkpinfo->netfs_mount, "%s", call_program_and_get_last_line_of_output("mount | grep \":\" | cut -d' ' -f1 | head -n1"));
[128]2399 }
[1]2400#ifdef __FreeBSD__
[128]2401 if (TRUE)
[1]2402#else
[128]2403 if (!bkpinfo->disaster_recovery)
[1]2404#endif
[128]2405 {
[3212]2406 if (!popup_and_get_string("Network shared 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.)", p, MAX_STR_LEN / 4)) {
[541]2407 log_to_screen("User has chosen not to backup the PC");
[128]2408 finish(1);
2409 }
[3191]2410 mr_free(bkpinfo->netfs_mount);
[3194]2411 mr_asprintf(bkpinfo->netfs_mount, "%s", p);
[128]2412 if (!bkpinfo->restore_data) {
[2771]2413 if ((compression_type = which_compression_type()) == NULL) {
2414 log_to_screen("User has chosen not to backup the PC");
2415 finish(1);
2416 }
[3191]2417
2418 if ((bkpinfo->compression_level = which_compression_level()) == -1) {
[541]2419 log_to_screen("User has chosen not to backup the PC");
[128]2420 finish(1);
2421 }
2422 }
2423 // check whether already mounted - we better remove
2424 // surrounding spaces and trailing '/' for this
[3191]2425 mr_strip_spaces(bkpinfo->netfs_mount);
[2380]2426 if (bkpinfo->netfs_mount[strlen(bkpinfo->netfs_mount) - 1] == '/')
2427 bkpinfo->netfs_mount[strlen(bkpinfo->netfs_mount) - 1] = '\0';
[2847]2428 q = strchr(bkpinfo->netfs_mount, '@');
2429 if (q != NULL) {
2430 /* User found. Store the 2 values */
2431 q++;
2432 /* new netfs mount */
[3013]2433 strcpy(tmp1,q);
[2847]2434 } else {
[3013]2435 strcpy(tmp1,bkpinfo->netfs_mount);
[2847]2436 }
[3191]2437 mr_asprintf(command, "mount | grep \"%s \" | cut -d' ' -f3", tmp1);
[3013]2438 strcpy(bkpinfo->isodir, call_program_and_get_last_line_of_output(command));
[3191]2439 mr_free(command);
[256]2440
[1847]2441 if (!bkpinfo->restore_data) {
[3194]2442 mr_asprintf(sz_size, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4482 MB
[3191]2443 mr_asprintf(comment, "How much data (in Megabytes) will each media store?");
2444 strcpy(tmp1, sz_size);
2445 mr_free(sz_size);
2446 if (!popup_and_get_string("Size", comment, tmp1, 5)) {
[1847]2447 log_to_screen("User has chosen not to backup the PC");
2448 finish(1);
2449 }
[3191]2450 mr_free(comment);
2451 mr_asprintf(sz_size, "%s", tmp1);
[1847]2452 } else {
[3191]2453 mr_asprintf(sz_size, "0");
[256]2454 }
[3150]2455 bkpinfo->media_size = atoi(sz_size);
[3191]2456 mr_free(sz_size);
2457
[3150]2458 if (bkpinfo->media_size < 0) {
[541]2459 log_to_screen("User has chosen not to backup the PC");
[256]2460 finish(1);
2461 }
[128]2462 }
2463 if (bkpinfo->disaster_recovery) {
[3191]2464 mr_asprintf(command ,"umount %s/isodir 2> /dev/null", bkpinfo->tmpdir);
[3060]2465 paranoid_system(command);
[3191]2466 mr_free(command);
2467
[128]2468 }
[2628]2469 strcpy(tmp1, bkpinfo->netfs_proto);
[3191]2470 if (!popup_and_get_string("Network protocol", "Which protocol should I use (nfs/sshfs/smbfs) ?",tmp1, MAX_STR_LEN)) {
[2628]2471 log_to_screen("User has chosen not to backup the PC");
2472 finish(1);
2473 }
2474 mr_free(bkpinfo->netfs_proto);
[3185]2475 mr_asprintf(bkpinfo->netfs_proto, "%s", tmp1);
[3191]2476
2477 strcpy(tmp1, bkpinfo->netfs_mount);
2478 if (!popup_and_get_string("Network share", "Which remote share should I mount?", tmp1, MAX_STR_LEN)) {
[2628]2479 log_to_screen("User has chosen not to backup the PC");
2480 finish(1);
2481 }
[3191]2482 mr_free(bkpinfo->netfs_mount);
2483 mr_asprintf(bkpinfo->netfs_mount, "%s", tmp1);
[2628]2484
[2847]2485 if (bkpinfo->netfs_user) {
2486 strcpy(tmp1, bkpinfo->netfs_user);
2487 } else {
2488 strcpy(tmp1, "");
2489 }
[3194]2490 if (!popup_and_get_string("Network user", "Which user should I use if any ?",tmp1, MAX_STR_LEN)) {
[2847]2491 log_to_screen("User has chosen not to backup the PC");
2492 finish(1);
2493 }
2494 mr_free(bkpinfo->netfs_user);
2495 if (strcmp(tmp1, "") != 0) {
[3185]2496 mr_asprintf(bkpinfo->netfs_user, "%s", tmp1);
[2847]2497 }
2498
[1858]2499 /* Initiate bkpinfo isodir path from running environment if mount already done */
[2380]2500 if (is_this_device_mounted(bkpinfo->netfs_mount)) {
[3191]2501 strcpy(bkpinfo->isodir, call_program_and_get_last_line_of_output("mount | grep \":\" | cut -d' ' -f3 | head -n1"));
[1858]2502 } else {
[3278]2503 // Why netfsdir ?
[2380]2504 sprintf(bkpinfo->isodir, "%s/netfsdir", bkpinfo->tmpdir);
[3191]2505 mr_asprintf(command, "mkdir -p %s", bkpinfo->isodir);
[128]2506 run_program_and_log_output(command, 5);
[3191]2507 mr_free(command);
[3111]2508
[2361]2509 if (bkpinfo->restore_data) {
[3111]2510 /* mount th FS read-only in restore mode to avoid any erase of whatever */
[3185]2511 mr_asprintf(tmpro, "-o ro");
[3111]2512 } else {
[3185]2513 mr_asprintf(tmpro, "");
[3111]2514 }
2515
2516 /* Build the mount string */
2517 if (strstr(bkpinfo->netfs_proto, "smbfs")) {
[3185]2518 mr_asprintf(tmp, "mount -t cifs %s %s %s",bkpinfo->netfs_mount, bkpinfo->isodir,tmpro);
[3111]2519 if (bkpinfo->netfs_user) {
2520 mr_strcat(tmp, " -o user=%s", bkpinfo->netfs_user);
[3107]2521 }
[3111]2522 else {
[2380]2523 if (strstr(bkpinfo->netfs_proto, "sshfs")) {
[3185]2524 mr_asprintf(tmp, "sshfs %s ",tmpro);
[2380]2525 } else {
[3185]2526 mr_asprintf(tmp, "mount -t %s -o nolock %s ", bkpinfo->netfs_proto,tmpro);
[2380]2527 }
[3111]2528 if (bkpinfo->netfs_user) {
2529 mr_strcat(tmp, "%s@", bkpinfo->netfs_user);
2530 }
2531 mr_strcat(tmp, "%s %s", bkpinfo->netfs_mount, bkpinfo->isodir);
[2361]2532 }
[1819]2533 run_program_and_log_output(tmp, 3);
[2214]2534 mr_free(tmp);
2535
[128]2536 malloc_string(g_selfmounted_isodir);
2537 strcpy(g_selfmounted_isodir, bkpinfo->isodir);
[3111]2538 }
[128]2539 }
[2380]2540 if (!is_this_device_mounted(bkpinfo->netfs_mount)) {
[3191]2541 popup_and_OK("Please mount that partition before you try to backup to or restore from it.");
[128]2542 finish(1);
2543 }
[3196]2544 if (bkpinfo->netfs_remote_dir == NULL) {
[3205]2545 fatal_error("bkpinfo->netfs_remote_dir should not be NULL");
[3196]2546 }
[2380]2547 strcpy(tmp1, bkpinfo->netfs_remote_dir);
[3212]2548 if (!popup_and_get_string ("Directory", "Which directory within that mountpoint?", tmp1, MAX_STR_LEN)) {
[541]2549 log_to_screen("User has chosen not to backup the PC");
[128]2550 finish(1);
2551 }
[3196]2552 mr_free(bkpinfo->netfs_remote_dir);
[3194]2553 mr_asprintf(bkpinfo->netfs_remote_dir, "%s", tmp1);
[2214]2554
[128]2555 // check whether writable - we better remove surrounding spaces for this
[3194]2556 mr_strip_spaces(bkpinfo->netfs_remote_dir);
[1767]2557
[3212]2558 if (!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, MAX_STR_LEN / 4)) {
[541]2559 log_to_screen("User has chosen not to backup the PC");
[251]2560 finish(1);
2561 }
2562 log_msg(3, "prefix set to %s", bkpinfo->prefix);
[3191]2563 log_msg(3, "Just set netfs_remote_dir to %s", bkpinfo->netfs_remote_dir);
[128]2564 log_msg(3, "isodir is still %s", bkpinfo->isodir);
2565 break;
[1]2566
[128]2567 case iso:
2568 if (!bkpinfo->disaster_recovery) {
[3212]2569 if (!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, MAX_STR_LEN / 4)) {
[541]2570 log_to_screen("User has chosen not to backup the PC");
[128]2571 finish(1);
2572 }
2573 if (archiving_to_media) {
[2771]2574 if ((compression_type = which_compression_type()) == NULL) {
2575 log_to_screen("User has chosen not to backup the PC");
2576 finish(1);
2577 }
[3191]2578 if ((bkpinfo->compression_level = which_compression_level()) == -1) {
[541]2579 log_to_screen("User has chosen not to backup the PC");
[128]2580 finish(1);
2581 }
[3191]2582 sprintf(tmp1, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4482 MB
[3212]2583 if (!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 (700) or DVD's (4480) you plan to backup to.", tmp1, 16)) {
[541]2584 log_to_screen("User has chosen not to backup the PC");
[128]2585 finish(1);
2586 }
[3191]2587 bkpinfo->media_size = atoi(tmp1);
[128]2588 } else {
[3150]2589 bkpinfo->media_size = 650;
[128]2590 }
2591 }
[3212]2592 if (!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, MAX_STR_LEN / 4)) {
[804]2593 log_to_screen("User has chosen not to backup the PC");
2594 finish(1);
2595 }
2596 log_msg(3, "prefix set to %s", bkpinfo->prefix);
[128]2597 break;
2598 default:
[3196]2599 fatal_error("I, Mojo Jojo, shall defeat those pesky Powerpuff Girls!");
[128]2600 }
[1]2601
[128]2602 if (archiving_to_media) {
[1]2603
2604#ifdef __FreeBSD__
[128]2605 strcpy(bkpinfo->boot_device,
2606 call_program_and_get_last_line_of_output
2607 ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/\\([0-9]\\).*/\\1/'"));
[1]2608#else
[128]2609 strcpy(bkpinfo->boot_device,
2610 call_program_and_get_last_line_of_output
2611 ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'"));
[1]2612#endif
[128]2613 i = which_boot_loader(bkpinfo->boot_device);
2614 if (i == 'U') // unknown
2615 {
[1]2616
2617#ifdef __FreeBSD__
[3212]2618 if (!popup_and_get_string("Boot device", "What is your boot device? (e.g. /dev/ad0)", bkpinfo->boot_device, MAX_STR_LEN / 4)) {
[541]2619 log_to_screen("User has chosen not to backup the PC");
[128]2620 finish(1);
2621 }
2622 i = which_boot_loader(bkpinfo->boot_device);
[1]2623#else
[3212]2624 if (!popup_and_get_string("Boot device", "What is your boot device? (e.g. /dev/hda)", bkpinfo->boot_device, MAX_STR_LEN / 4)) {
[541]2625 log_to_screen("User has chosen not to backup the PC");
[128]2626 finish(1);
2627 }
[3191]2628 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "LILO")) {
[128]2629 i = 'L';
2630 } else
[3191]2631 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "ELILO")) {
[128]2632 i = 'E';
2633 } else
[3191]2634 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "GRUB")) {
[128]2635 i = 'G';
2636 } else {
2637 i = 'U';
2638 }
[1]2639#endif
[128]2640 if (i == 'U') {
[3191]2641 if (ask_me_yes_or_no("Unidentified boot loader. Shall I restore it byte-for-byte at restore time and hope for the best?")) {
[128]2642 i = 'R'; // raw
2643 } else {
[3191]2644 log_to_screen("I cannot find your boot loader. Please run mondoarchive with parameters.");
[128]2645 finish(1);
2646 }
2647 }
[1]2648 }
[128]2649 bkpinfo->boot_loader = i;
[3191]2650
[3205]2651 if (bkpinfo->include_paths) {
2652 strcpy(tmp1, bkpinfo->include_paths);
2653 mr_free(bkpinfo->include_paths);
2654 } else {
2655 strcpy(tmp1, "/");
2656 }
[3212]2657 if (!popup_and_get_string("Backup paths", "Please enter paths (separated by '|') which you want me to backup. The default is '/' (i.e. everything).", tmp1, MAX_STR_LEN)) {
[541]2658 log_to_screen("User has chosen not to backup the PC");
[128]2659 finish(1);
[1]2660 }
[3191]2661 mr_asprintf(bkpinfo->include_paths, "%s", tmp1);
2662
[2713]2663 tmp = list_of_NETFS_mounts_only();
[128]2664 if (strlen(tmp) > 2) {
[2713]2665 mr_strcat(bkpinfo->exclude_paths, "|%s",tmp);
[128]2666 }
[2214]2667 mr_free(tmp);
[1]2668// NTFS
[3318]2669 strcpy(tmp1, call_program_and_get_last_line_of_output("parted2fdisk -l 2>/dev/null | grep -i ntfs | awk '{ print $1};' | tr -s '\\n' ' ' | awk '{ print $0};'"));
[2304]2670 if (strlen(tmp1) > 2) {
[3212]2671 if (!popup_and_get_string("NTFS partitions", "Please enter/confirm the NTFS partitions you wish to backup as well.", tmp1, MAX_STR_LEN / 4)) {
[541]2672 log_to_screen("User has chosen not to backup the PC");
[128]2673 finish(1);
2674 }
[2304]2675 strncpy(bkpinfo->image_devs, tmp1, MAX_STR_LEN / 4);
[128]2676 }
[1]2677
[2743]2678 if (bkpinfo->exclude_paths != NULL ) {
[3062]2679 strncpy(p,bkpinfo->exclude_paths,(16*MAX_STR_LEN)-1);
[2743]2680 } else {
2681 p[0] = '\0';
2682 }
[3062]2683 popup_and_get_string("Exclude paths", "Please enter paths which you do NOT want to backup. Separate them with '|'. NB: /tmp and /proc are always excluded. :-) Just hit 'Enter' if you want to do a full system backup.", p, (16*MAX_STR_LEN)-1);
[2697]2684 if (p == NULL) {
[541]2685 log_to_screen("User has chosen not to backup the PC");
[128]2686 finish(1);
2687 }
[2697]2688 mr_free(bkpinfo->exclude_paths);
[3185]2689 mr_asprintf(tmp, "%s", p);
[2697]2690 bkpinfo->exclude_paths = tmp;
2691
[3212]2692 mr_asprintf(oldtmp, "%s", bkpinfo->tmpdir);
2693 if (bkpinfo->tmpdir != NULL ) {
2694 strncpy(p,bkpinfo->tmpdir,(16*MAX_STR_LEN)-1);
2695 } else {
2696 p[0] = '\0';
2697 }
2698 if (!popup_and_get_string("Temporary directory", "Please enter your temporary directory.", p, (4*MAX_STR_LEN)-1)) {
[2220]2699 log_to_screen("User has chosen not to backup the PC");
2700 finish(1);
2701 }
[3154]2702 /* if modified to another path */
[3212]2703 if (strcmp(p,oldtmp) != 0) {
2704 setup_tmpdir(p);
[3154]2705 }
[3212]2706 mr_free(oldtmp);
2707
2708 mr_asprintf(oldtmp, "%s", bkpinfo->scratchdir);
2709 if (bkpinfo->scratchdir != NULL ) {
2710 strncpy(p,bkpinfo->scratchdir,(16*MAX_STR_LEN)-1);
2711 } else {
2712 p[0] = '\0';
2713 }
2714 if (!popup_and_get_string("Scratch directory", "Please enter your scratch directory.", p, (4*MAX_STR_LEN)-1)) {
[2220]2715 log_to_screen("User has chosen not to backup the PC");
2716 finish(1);
2717 }
[3154]2718 /* if modified to another path */
[3212]2719 if (strcmp(p,oldtmp) != 0) {
2720 setup_scratchdir(p);
[3154]2721 }
[3212]2722 mr_free(oldtmp);
2723
[2772]2724 if (ask_me_yes_or_no("Do you want to backup extended attributes?")) {
2725 if (find_home_of_exe("getfattr")) {
[3191]2726 mr_free(g_getfattr);
[3185]2727 mr_asprintf(g_getfattr,"getfattr");
[2772]2728 }
2729 if (find_home_of_exe("getfacl")) {
[3191]2730 mr_free(g_getfacl);
[3185]2731 mr_asprintf(g_getfacl,"getfacl");
[2772]2732 }
[3171]2733 log_it("Backup of extended attributes");
[2772]2734 }
[1575]2735// Interactive mode:
2736#ifdef __IA64__
2737 bkpinfo->make_cd_use_lilo = TRUE;
2738#else
[128]2739 bkpinfo->make_cd_use_lilo = FALSE;
[1575]2740#endif
[128]2741 bkpinfo->backup_data = TRUE;
[2771]2742 if (strcmp(compression_type,"lzo") == 0) {
2743 strcpy(bkpinfo->zip_exe, "lzop");
2744 strcpy(bkpinfo->zip_suffix, "lzo");
2745 } else if (strcmp(compression_type,"gzip") == 0) {
2746 strcpy(bkpinfo->zip_exe, "gzip");
2747 strcpy(bkpinfo->zip_suffix, "gz");
[3191]2748 } else if (strcmp(compression_type,"lzma") == 0) {
[2771]2749 //strcpy(bkpinfo->zip_exe, "xy");
2750 //strcpy(bkpinfo->zip_suffix, "xy");
2751 } else if (strcmp(compression_type,"bzip2") == 0) {
2752 strcpy(bkpinfo->zip_exe, "bzip2");
2753 strcpy(bkpinfo->zip_suffix, "bz2");
2754 } else {
2755 bkpinfo->zip_exe[0] = bkpinfo->zip_suffix[0] = '\0';
2756 }
[2774]2757
[128]2758 bkpinfo->verify_data =
2759 ask_me_yes_or_no
[541]2760 ("Will you want to verify your backups after Mondo has created them?");
[1]2761
[128]2762 if (!ask_me_yes_or_no
[541]2763 ("Are you sure you want to proceed? Hit 'no' to abort.")) {
2764 log_to_screen("User has chosen not to backup the PC");
[128]2765 finish(1);
2766 }
2767 } else {
2768 bkpinfo->restore_data = TRUE; // probably...
2769 }
[2774]2770 mr_free(compression_type);
[1]2771
[128]2772 if (bkpinfo->backup_media_type == iso
[2380]2773 || bkpinfo->backup_media_type == netfs) {
[128]2774 g_ISO_restore_mode = TRUE;
2775 }
[1]2776#ifdef __FreeSD__
2777// skip
2778#else
[2380]2779 if (bkpinfo->backup_media_type == netfs) {
[3191]2780 log_msg(3, "I think the Remote mount is mounted at %s", bkpinfo->isodir);
[128]2781 }
2782 log_it("isodir = %s", bkpinfo->isodir);
[3191]2783 if (bkpinfo->netfs_mount) {
2784 log_it("netfs_mount = '%s'", bkpinfo->netfs_mount);
[2682]2785 }
[2380]2786 if (bkpinfo->netfs_user) {
2787 log_it("netfs_user = '%s'", bkpinfo->netfs_user);
[2224]2788 }
[3191]2789 if (bkpinfo->netfs_proto) {
2790 log_it("netfs_proto = '%s'", bkpinfo->netfs_proto);
2791 }
[1]2792#endif
2793
[128]2794 log_it("media device = %s", bkpinfo->media_device);
[3150]2795 log_it("media size = %ld", bkpinfo->media_size);
[3015]2796 log_it("media type = %s", bkptype_to_string(bkpinfo->backup_media_type));
[3191]2797 if (bkpinfo->prefix) {
2798 log_it("prefix = %s", bkpinfo->prefix);
2799 }
[128]2800 log_it("compression = %ld", bkpinfo->compression_level);
[2473]2801 log_it("exclude_path = %s", bkpinfo->exclude_paths);
[3205]2802 if (bkpinfo->include_paths) {
2803 log_it("include_path = %s", bkpinfo->include_paths);
2804 }
[2424]2805
2806 /* Handle devices passed in bkpinfo and print result */
[3194]2807 /* the mr_make_devlist_from_pathlist function appends */
[3191]2808 /* to the *_paths variables so copy before */
[2713]2809 mr_make_devlist_from_pathlist(bkpinfo->exclude_paths, 'E');
2810 mr_make_devlist_from_pathlist(bkpinfo->include_paths, 'I');
[2424]2811
[128]2812 log_it("scratchdir = '%s'", bkpinfo->scratchdir);
2813 log_it("tmpdir = '%s'", bkpinfo->tmpdir);
[3191]2814 if (bkpinfo->image_devs) {
2815 log_it("image_devs = '%s'", bkpinfo->image_devs);
2816 }
2817 log_it("boot_device = '%s' (loader=%c)", bkpinfo->boot_device, bkpinfo->boot_loader);
[3150]2818 if (bkpinfo->media_size < 0) {
[128]2819 if (archiving_to_media) {
2820 fatal_error("Media size is less than zero.");
2821 } else {
2822 log_msg(2, "Warning - media size is less than zero.");
[3150]2823 bkpinfo->media_size = 0;
[128]2824 }
[1]2825 }
[128]2826 paranoid_free(sz_size);
[2305]2827 paranoid_free(tmp1);
[128]2828 return (0);
[1]2829}
2830
2831
2832/**
[2713]2833 * Get a |-separated list of NETFS mounts.
[1]2834 * @return The list created.
[2713]2835 * @note The return value points to allocated string that needs to be freed
[1]2836 * @bug Even though we only want the mounts, the devices are still checked.
2837 */
[2380]2838char *list_of_NETFS_mounts_only(void)
[1]2839{
[2214]2840 char *exclude_these_directories = NULL;
[1]2841
[3185]2842 mr_asprintf(exclude_these_directories,"%s", call_program_and_get_last_line_of_output("mount -t coda,ncpfs,fuse.sshfs,nfs,nfs4,vmhgfs,smbfs,cifs,afs,gfs,ocfs,ocfs2,mvfs,nsspool,nssvol | tr -s '\t' ' ' | cut -d' ' -f3 | tr -s '\n' '|' | awk '{print $0;}'"));
[2713]2843 log_msg(9,"list_of_NETFS_mounts_only returns %s\n",exclude_these_directories);
2844 return(exclude_these_directories);
[1]2845}
2846
2847/* @} - end of utilityGroup */
2848
2849
2850
2851
2852
2853/**
2854 * Create a randomly-named FIFO. The format is @p stub "." [random] [random] where
2855 * [random] is a random number between 1 and 32767.
2856 * @param store_name_here Where to store the new filename.
2857 * @param stub A random number will be appended to this to make the FIFO's name.
2858 * @ingroup deviceGroup
2859 */
[128]2860void make_fifo(char *store_name_here, char *stub)
[1]2861{
[128]2862 char *tmp;
[1]2863
[128]2864 malloc_string(tmp);
2865 assert_string_is_neither_NULL_nor_zerolength(stub);
[1]2866
[128]2867 sprintf(store_name_here, "%s%d%d", stub, (int) (random() % 32768),
2868 (int) (random() % 32768));
2869 make_hole_for_file(store_name_here);
2870 mkfifo(store_name_here, S_IRWXU | S_IRWXG);
2871 sprintf(tmp, "chmod 770 %s", store_name_here);
2872 paranoid_system(tmp);
2873 paranoid_free(tmp);
[1]2874}
2875
2876
2877
2878/**
2879 * @addtogroup deviceGroup
2880 * @{
2881 */
2882/**
2883 * If we can read @p dev, set @p output to it.
2884 * If @p dev cannot be read, set @p output to "".
2885 * @param dev The device to check for.
2886 * @param output Set to @p dev if @p dev exists, "" otherwise.
2887 * @return TRUE if @p dev exists, FALSE if it doesn't.
2888 */
[128]2889bool set_dev_to_this_if_rx_OK(char *output, char *dev)
[1]2890{
[128]2891 char *command;
[1]2892
[128]2893 malloc_string(command);
2894 if (!dev || dev[0] == '\0') {
2895 output[0] = '\0';
2896 return (FALSE);
2897 }
[1]2898// assert_string_is_neither_NULL_nor_zerolength(dev);
[2931]2899 if (!bkpinfo->please_dont_eject) {
2900 log_msg(10, "Injecting %s", dev);
2901 inject_device(dev);
2902 }
[128]2903 if (!does_file_exist(dev)) {
2904 log_msg(10, "%s doesn't exist. Returning FALSE.", dev);
2905 return (FALSE);
2906 }
2907 sprintf(command, "dd bs=%ld count=1 if=%s of=/dev/null &> /dev/null",
2908 512L, dev);
2909 if (!run_program_and_log_output(command, FALSE)
2910 && !run_program_and_log_output(command, FALSE)) {
2911 strcpy(output, dev);
2912 log_msg(4, "Found it - %s", dev);
2913 return (TRUE);
2914 } else {
2915 output[0] = '\0';
2916 log_msg(4, "It's not %s", dev);
2917 return (FALSE);
2918 }
[1]2919}
2920
2921
2922
2923
2924
2925/**
2926 * Find out what number CD is in the drive.
2927 * @param bkpinfo The backup information structure. The @c bkpinfo->media_device field is the only one used.
2928 * @return The current CD number, or -1 if it could not be found.
2929 * @note If the CD is not mounted, it will be mounted
2930 * (and remain mounted after this function returns).
2931 */
[1645]2932int what_number_cd_is_this()
[1]2933{
[128]2934 int cd_number = -1;
[2211]2935 char *mountdev = NULL;
2936 char *tmp = NULL;
[1]2937
[128]2938 assert(bkpinfo != NULL);
[1]2939// log_it("Asking what_number_cd_is_this");
[128]2940 if (g_ISO_restore_mode) {
[3185]2941 mr_asprintf(tmp, "mount | grep iso9660 | awk '{print $3;}'");
[1]2942
[3185]2943 mr_asprintf(mountdev, "%s%s", call_program_and_get_last_line_of_output(tmp), "/archives/THIS-CD-NUMBER");
[128]2944 cd_number = atoi(last_line_of_file(mountdev));
2945 paranoid_free(mountdev);
2946 paranoid_free(tmp);
[2211]2947
[128]2948 return (cd_number);
2949 }
[1]2950
[3185]2951 mr_asprintf(mountdev, "%s", bkpinfo->media_device);
[128]2952 if (!mountdev[0]) {
2953 log_it
2954 ("(what_number_cd_is_this) Warning - media_device unknown. Finding out...");
2955 find_cdrom_device(bkpinfo->media_device, FALSE);
2956 }
2957 if (!is_this_device_mounted(MNT_CDROM)) {
[1741]2958 if (bkpinfo->backup_media_type == usb) {
2959 mount_USB_here(mountdev, MNT_CDROM);
2960 } else {
2961 mount_CDROM_here(mountdev, MNT_CDROM);
2962 }
[128]2963 }
2964 paranoid_free(mountdev);
[2211]2965
2966 cd_number = atoi(last_line_of_file(MNT_CDROM "/archives/THIS-CD-NUMBER"));
[128]2967 return (cd_number);
[1]2968}
2969
2970
2971
2972
2973
2974
2975
2976/**
2977 * Find out what device is mounted as root (/).
2978 * @return Root device.
2979 * @note The returned string points to static storage and will be overwritten with every call.
2980 * @bug A bit of a misnomer; it's actually finding out the root device.
2981 * The mountpoint (where it's mounted) will obviously be '/'.
2982 */
[3205]2983char *where_is_root_mounted() {
[1]2984
[3205]2985/*@ buffers **************** */
2986static char tmp[MAX_STR_LEN];
[1]2987
[3205]2988
[1]2989#ifdef __FreeBSD__
[3205]2990 strcpy(tmp, call_program_and_get_last_line_of_output("mount | grep \" on / \" | cut -d' ' -f1"));
[1]2991#else
[3205]2992 strcpy(tmp, call_program_and_get_last_line_of_output("mount | grep \" on / \" | cut -d' ' -f1 | sed s/[0-9]// | sed s/[0-9]//"));
[128]2993 if (strstr(tmp, "/dev/cciss/")) {
[3205]2994 strcpy(tmp, call_program_and_get_last_line_of_output("mount | grep \" on / \" | cut -d' ' -f1 | cut -dp -f1"));
[128]2995 }
2996 if (strstr(tmp, "/dev/md")) {
[3205]2997 strcpy(tmp, call_program_and_get_last_line_of_output("mount | grep \" on / \" | cut -d' ' -f1"));
[128]2998 }
[1]2999#endif
3000
[3205]3001return (tmp);
[1]3002}
3003
3004
3005/**
3006 * Find out which boot loader is in use.
3007 * @param which_device Device to look for the boot loader on.
3008 * @return 'L' for LILO, 'E'for ELILO, 'G' for GRUB, 'B' or 'D' for FreeBSD boot loaders, or 'U' for Unknown.
3009 * @note Under Linux, all drives are examined, not just @p which_device.
3010 */
3011#ifdef __FreeBSD__
[128]3012char which_boot_loader(char *which_device)
[1]3013{
[128]3014 int count_lilos = 0;
3015 int count_grubs = 0;
3016 int count_boot0s = 0;
3017 int count_dangerouslydedicated = 0;
[1]3018
[128]3019 log_it("looking at drive %s's MBR", which_device);
3020 if (does_string_exist_in_boot_block(which_device, "GRUB")) {
3021 count_grubs++;
3022 }
3023 if (does_string_exist_in_boot_block(which_device, "LILO")) {
3024 count_lilos++;
3025 }
3026 if (does_string_exist_in_boot_block(which_device, "Drive")) {
3027 count_boot0s++;
3028 }
3029 if (does_string_exist_in_first_N_blocks
3030 (which_device, "FreeBSD/i386", 17)) {
3031 count_dangerouslydedicated++;
3032 }
3033 log_it("%d grubs and %d lilos and %d elilos and %d boot0s and %d DD\n",
3034 count_grubs, count_lilos, count_elilos, count_boot0s,
3035 count_dangerouslydedicated);
[1]3036
[128]3037 if (count_grubs && !count_lilos) {
3038 return ('G');
3039 } else if (count_lilos && !count_grubs) {
3040 return ('L');
3041 } else if (count_grubs == 1 && count_lilos == 1) {
3042 log_it("I'll bet you used to use LILO but switched to GRUB...");
3043 return ('G');
3044 } else if (count_boot0s == 1) {
3045 return ('B');
3046 } else if (count_dangerouslydedicated) {
3047 return ('D');
3048 } else {
3049 log_it("Unknown boot loader");
3050 return ('U');
3051 }
[1]3052}
3053
3054#else
3055
[128]3056char which_boot_loader(char *which_device)
[1]3057{
[128]3058 /*@ buffer ***************************************************** */
[2241]3059 char *list_drives_cmd = NULL;
[128]3060 char *current_drive;
[3060]3061 char *tmp;
[1]3062
[128]3063 /*@ pointers *************************************************** */
3064 FILE *pdrives;
[1]3065
[128]3066 /*@ int ******************************************************** */
3067 int count_lilos = 0;
3068 int count_grubs = 0;
[1]3069
[128]3070 /*@ end vars *************************************************** */
[1]3071
[128]3072 malloc_string(current_drive);
[1]3073
[3191]3074 /* UEFI is not supported here - but should be managed as a BIOS/UEFI option not a Boot Loader one per se */
3075
[1]3076#ifdef __IA64__
[128]3077 /* No choice for it */
3078 return ('E');
[1]3079#endif
[128]3080 assert(which_device != NULL);
[1]3081
[3185]3082 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]3083 log_it("list_drives_cmd = %s", list_drives_cmd);
[1]3084
[128]3085 if (!(pdrives = popen(list_drives_cmd, "r"))) {
3086 log_OS_error("Unable to open list of drives");
[2241]3087 mr_free(list_drives_cmd);
[128]3088 paranoid_free(current_drive);
3089 return ('\0');
[1]3090 }
[2241]3091 mr_free(list_drives_cmd);
3092
[3060]3093 for (tmp = fgets(current_drive, MAX_STR_LEN, pdrives); !feof(pdrives) && (tmp != NULL);
3094 tmp = fgets(current_drive, MAX_STR_LEN, pdrives)) {
[128]3095 strip_spaces(current_drive);
3096 log_it("looking at drive %s's MBR", current_drive);
3097 if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
3098 count_grubs++;
3099 strcpy(which_device, current_drive);
3100 break;
3101 }
3102 if (does_string_exist_in_boot_block(current_drive, "LILO")) {
3103 count_lilos++;
3104 strcpy(which_device, current_drive);
3105 break;
3106 }
[1]3107 }
[128]3108 if (pclose(pdrives)) {
3109 log_OS_error("Cannot pclose pdrives");
3110 }
3111 log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
3112 if (count_grubs && !count_lilos) {
[2241]3113 paranoid_free(current_drive);
[128]3114 return ('G');
3115 } else if (count_lilos && !count_grubs) {
[2241]3116 paranoid_free(current_drive);
[128]3117 return ('L');
3118 } else if (count_grubs == 1 && count_lilos == 1) {
3119 log_it("I'll bet you used to use LILO but switched to GRUB...");
[2241]3120 paranoid_free(current_drive);
[128]3121 return ('G');
3122 } else {
[1451]3123 // We need to look on each partition then
[3185]3124 mr_asprintf(list_drives_cmd, "parted2fdisk -l 2>/dev/null | grep -E \"^/dev/\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/");
[1451]3125 log_it("list_drives_cmd = %s", list_drives_cmd);
3126
3127 if (!(pdrives = popen(list_drives_cmd, "r"))) {
3128 log_OS_error("Unable to open list of drives");
[2241]3129 mr_free(list_drives_cmd);
[1451]3130 paranoid_free(current_drive);
3131 return ('\0');
3132 }
[2241]3133 mr_free(list_drives_cmd);
3134
[3060]3135 for (tmp = fgets(current_drive, MAX_STR_LEN, pdrives); !feof(pdrives) && (tmp != NULL);
3136 tmp = fgets(current_drive, MAX_STR_LEN, pdrives)) {
[1451]3137 strip_spaces(current_drive);
3138 log_it("looking at partition %s's BR", current_drive);
3139 if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
3140 count_grubs++;
3141 strcpy(which_device, current_drive);
3142 break;
3143 }
3144 if (does_string_exist_in_boot_block(current_drive, "LILO")) {
3145 count_lilos++;
3146 strcpy(which_device, current_drive);
3147 break;
3148 }
3149 }
3150 if (pclose(pdrives)) {
3151 log_OS_error("Cannot pclose pdrives");
3152 }
3153 log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
3154 paranoid_free(current_drive);
3155 if (count_grubs && !count_lilos) {
3156 return ('G');
3157 } else if (count_lilos && !count_grubs) {
3158 return ('L');
3159 } else if (count_grubs == 1 && count_lilos == 1) {
3160 log_it("I'll bet you used to use LILO but switched to GRUB...");
3161 return ('G');
3162 } else {
3163 log_it("Unknown boot loader");
3164 return ('U');
3165 }
[128]3166 }
[1]3167}
3168#endif
3169
3170
3171
3172
3173/**
3174 * Write zeroes over the first 16K of @p device.
3175 * @param device The device to zero.
3176 * @return 0 for success, 1 for failure.
3177 */
[128]3178int zero_out_a_device(char *device)
[1]3179{
[128]3180 FILE *fout;
3181 int i;
[1]3182
[128]3183 assert_string_is_neither_NULL_nor_zerolength(device);
[1]3184
[128]3185 log_it("Zeroing drive %s", device);
3186 if (!(fout = fopen(device, "w"))) {
3187 log_OS_error("Unable to open/write to device");
3188 return (1);
3189 }
3190 for (i = 0; i < 16384; i++) {
3191 fputc('\0', fout);
3192 }
3193 paranoid_fclose(fout);
3194 log_it("Device successfully zeroed.");
3195 return (0);
[1]3196}
3197
3198/**
3199 * Return the device pointed to by @p incoming.
3200 * @param incoming The device to resolve symlinks for.
3201 * @return The path to the real device file.
3202 * @note The returned string points to static storage that will be overwritten with each call.
3203 * @bug Won't work with file v4.0; needs to be written in C.
3204 */
[128]3205char *resolve_softlinks_to_get_to_actual_device_file(char *incoming)
[1]3206{
[128]3207 static char output[MAX_STR_LEN];
3208 char *command;
3209 char *curr_fname;
[2214]3210 char *scratch = NULL;
3211 char *tmp = NULL;
[128]3212 char *p;
3213
3214 struct stat statbuf;
3215 command = malloc(1000);
3216 malloc_string(curr_fname);
3217 if (!does_file_exist(incoming)) {
3218 log_it
3219 ("resolve_softlinks_to_get_to_actual_device_file --- device not found");
3220 strcpy(output, incoming);
3221 } else {
3222 strcpy(curr_fname, incoming);
3223 lstat(curr_fname, &statbuf);
3224 while (S_ISLNK(statbuf.st_mode)) {
3225 log_msg(1, "curr_fname = %s", curr_fname);
3226 sprintf(command, "file %s", curr_fname);
[3185]3227 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
[128]3228 for (p = tmp + strlen(tmp); p != tmp && *p != '`' && *p != ' ';
3229 p--);
3230 p++;
[3185]3231 mr_asprintf(scratch, "%s", p);
[128]3232 for (p = scratch; *p != '\0' && *p != '\''; p++);
3233 *p = '\0';
[2214]3234 log_msg(0, "curr_fname %s --> '%s' --> %s", curr_fname, tmp, scratch);
3235 mr_free(tmp);
3236
[128]3237 if (scratch[0] == '/') {
3238 strcpy(curr_fname, scratch); // copy whole thing because it's an absolute softlink
3239 } else { // copy over the basename cos it's a relative softlink
3240 p = curr_fname + strlen(curr_fname);
3241 while (p != curr_fname && *p != '/') {
3242 p--;
3243 }
3244 if (*p == '/') {
3245 p++;
3246 }
3247 strcpy(p, scratch);
3248 }
[2214]3249 mr_free(scratch);
[128]3250 lstat(curr_fname, &statbuf);
3251 }
3252 strcpy(output, curr_fname);
3253 log_it("resolved %s to %s", incoming, output);
[1]3254 }
[128]3255 paranoid_free(command);
3256 paranoid_free(curr_fname);
3257 return (output);
[1]3258}
3259
3260/* @} - end of deviceGroup */
3261
3262/**
3263 * Return the type of partition format (GPT or MBR)
3264 */
3265char *which_partition_format(const char *drive)
3266{
[3237]3267 char *output = NULL;
[2214]3268 char *tmp = NULL;
[128]3269 char *command;
3270 char *fdisk;
[152]3271#ifdef __IA64__
[128]3272 struct stat buf;
[152]3273#endif
[128]3274 malloc_string(command);
3275 malloc_string(fdisk);
[196]3276 sprintf(fdisk, "/sbin/parted2fdisk");
[3318]3277 sprintf(command, "%s -l %s 2>/dev/null | grep 'EFI GPT'", fdisk, drive);
[3185]3278 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
[128]3279 if (strstr(tmp, "GPT") == NULL) {
[3237]3280 mr_asprintf(output, "%s", "MBR");
[128]3281 } else {
[3237]3282 mr_asprintf(output, "%s", "GPT");
[128]3283 }
[2214]3284 mr_free(tmp);
3285
[3263]3286 log_msg(0, "Found %s partition table format type on %s", output, drive);
[128]3287 paranoid_free(command);
3288 paranoid_free(fdisk);
3289 return (output);
[1]3290}
3291/* @} - end of deviceGroup */
Note: See TracBrowser for help on using the repository browser.