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

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