source: MondoRescue/branches/3.3/mondo/src/common/libmondo-devices.c@ 3870

Last change on this file since 3870 was 3870, checked in by Bruno Cornec, 4 months ago

Rewrite find_tape_device ans start for other find_*dev functions

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