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

Last change on this file since 3373 was 3373, checked in by Bruno Cornec, 9 years ago

Change the bkpinfo->boot_device from statically allocated to dynamically allocated

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