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

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

find_dvd|cdrom_device merged into a rewritten find_optical_device - adds find_usb_device and find_device - mount_CDROM_here moved to mount_media (ongoing)

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