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

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