source: MondoRescue/branches/3.1/mondo/src/common/libmondo-devices.c@ 3147

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