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

Last change on this file since 3859 was 3859, checked in by Bruno Cornec, 3 months ago

Fix build_partition_name to allocate dynamically and change proto accordingly

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