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

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