source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-devices.c@ 2364

Last change on this file since 2364 was 2364, checked in by Bruno Cornec, 15 years ago
  • More verbose at installation of additional tools
  • Fix a case where the NFS dir in restore mode was mounted rw (linked to bug #291 could create a huge problem)

(backport from 2.2.9)

  • Property svn:keywords set to Id
File size: 70.1 KB
RevLine 
[1]1/* libmondo-devices.c Subroutines for handling devices
[85]2 $Id: libmondo-devices.c 2364 2009-09-03 12:48:51Z bruno $
[1]3*/
4
5/**
6 * @file
7 * Functions to handle interactions with backup devices.
8 */
9
10#include "my-stuff.h"
[2211]11#include "mr_mem.h"
[1]12#include "mondostructures.h"
13#include "libmondo-files-EXT.h"
14#include "libmondo-devices.h"
[541]15#include "lib-common-externs.h"
[1]16#include "libmondo-string-EXT.h"
17#include "libmondo-tools-EXT.h"
[541]18#include "libmondo-gui-EXT.h"
[1]19#include "libmondo-fork-EXT.h"
20#include "libmondo-stream-EXT.h"
21
[2322]22extern void mr_strip_spaces(char *);
23
[1]24#include <sys/types.h>
25#ifdef __FreeBSD__
26#define DKTYPENAMES
27#define FSTYPENAMES
28#include <sys/disklabel.h>
29#include <sys/disk.h>
30#elif linux
31#define u64 unsigned long long
[128]32#include <linux/fs.h> /* for BLKGETSIZE64 */
[1]33#include <linux/hdreg.h>
34#endif
35
36/*@unused@*/
[85]37//static char cvsid[] = "$Id: libmondo-devices.c 2364 2009-09-03 12:48:51Z bruno $";
[1]38
39extern int g_current_media_number;
40extern double g_kernel_version;
41
42extern bool g_ISO_restore_mode;
[99]43extern char *g_selfmounted_isodir;
[1316]44extern char *MONDO_LOGFILE;
[1]45
[128]46static char g_cdrw_drive_is_here[MAX_STR_LEN / 4] = "";
47static char g_cdrom_drive_is_here[MAX_STR_LEN / 4] = "";
48static char g_dvd_drive_is_here[MAX_STR_LEN / 4] = "";
[1]49
50
51/**
52 * ????? @bug ?????
53 * @ingroup globalGroup
54 */
[128]55bool g_restoring_live_from_cd = FALSE;
[1916]56bool g_restoring_live_from_nfs = FALSE;
[1]57
[128]58extern t_bkptype g_backup_media_type; // set by main()
[1]59
[1645]60/* Reference to global bkpinfo */
61extern struct s_bkpinfo *bkpinfo;
[1]62
63
64
[1645]65
[2325]66void set_g_cdrom_and_g_dvd_to_bkpinfo_value() {
67
68 if (bkpinfo->media_device) {
69 strcpy(g_cdrom_drive_is_here, bkpinfo->media_device); // just in case
70 strcpy(g_dvd_drive_is_here, bkpinfo->media_device); // just in case
71 }
[1]72}
73
74
75
76/**
77 * Retract all CD trays and wait for autorun to complete.
78 * @ingroup deviceGroup
79 */
80void retract_CD_tray_and_defeat_autorun(void)
81{
82// log_it("rctada: Retracting all CD trays", __LINE__);
[128]83 if (strlen(g_cdrom_drive_is_here) > 0) {
84 inject_device(g_cdrom_drive_is_here);
85 }
86 if (strlen(g_dvd_drive_is_here) > 0) {
87 inject_device(g_dvd_drive_is_here);
88 }
89 if (strlen(g_cdrw_drive_is_here) > 0) {
90 inject_device(g_cdrw_drive_is_here);
91 }
[1]92// log_it("rctada: killing autorun");
93// run_program_and_log_output("killall autorun", TRUE);
[128]94 if (!run_program_and_log_output("ps | grep autorun | grep -v grep", 5)) {
95 log_it("autorun detected; sleeping for 2 seconds");
96 sleep(2);
97 }
98 log_it("rctada: Unmounting all CD drives", __LINE__);
99 run_program_and_log_output("umount /dev/cdr* /dev/dvd*", 5);
[1]100}
101
102
103
104/**
105 * Determine whether we're booted off a ramdisk.
106 * @return @c TRUE (we are) or @c FALSE (we aren't).
107 * @ingroup utilityGroup
108 */
[128]109bool am_I_in_disaster_recovery_mode(void)
[1]110{
[2214]111 char *tmp = NULL;
[128]112 bool is_this_a_ramdisk = FALSE;
[1]113
[2330]114 tmp = where_is_root_mounted();
[2324]115 log_msg(0, "root is currently mounted at %s\n", tmp);
[1]116
117#ifdef __FreeBSD__
[128]118 if (strstr(tmp, "/dev/md")) {
119 is_this_a_ramdisk = TRUE;
120 }
[1]121#else
[128]122 if (!strncmp(tmp, "/dev/ram", 8)
123 || (!strncmp(tmp, "/dev/rd", 7) && !strcmp(tmp, "/dev/rd/")
124 && strncmp(tmp, "/dev/rd/cd", 10)) || strstr(tmp, "rootfs")
125 || !strcmp(tmp, "/dev/root")) {
126 is_this_a_ramdisk = TRUE;
127 } else {
128 is_this_a_ramdisk = FALSE;
129 }
[1]130#endif
[2214]131 mr_free(tmp);
[1]132
[2321]133 log_msg(1, "Is this a ramdisk? result = %s", (is_this_a_ramdisk) ? "TRUE" : "FALSE");
[128]134 return (is_this_a_ramdisk);
[1]135}
136
137
138
139
140
141/**
142 * Turn @c bkpinfo->backup_media_type into a human-readable string.
143 * @return The human readable string (e.g. @c cdr becomes <tt>"cdr"</tt>).
144 * @note The returned string points to static storage that will be overwritten with each call.
145 * @ingroup stringGroup
146 */
147static char *bkptype_to_string(t_bkptype bt)
148{
[2330]149 char *output;
[128]150 switch (bt) {
151 case none:
[2330]152 mr_asprintf(output, "none");
[1]153 break;
[128]154 case iso:
[2330]155 mr_asprintf(output, "iso");
[1]156 break;
[128]157 case cdr:
[2330]158 mr_asprintf(output, "cdr");
[1]159 break;
[128]160 case cdrw:
[2330]161 mr_asprintf(output, "cdrw");
[1]162 break;
[128]163 case cdstream:
[2330]164 mr_asprintf(output, "cdstream");
[1]165 break;
[128]166 case nfs:
[2330]167 mr_asprintf(output, "nfs");
[1]168 break;
[128]169 case tape:
[2330]170 mr_asprintf(output, "tape");
[1]171 break;
[128]172 case udev:
[2330]173 mr_asprintf(output, "udev");
[1]174 break;
[1687]175 case usb:
[2330]176 mr_asprintf(output, "usb");
[1687]177 break;
[128]178 default:
[2330]179 mr_asprintf(output, "default");
[128]180 }
181 return (output);
[1]182}
183
184
185
186/**
187 * @addtogroup deviceGroup
188 * @{
189 */
190/**
191 * Eject the tray of the specified CD device.
192 * @param dev The device to eject.
193 * @return the return value of the @c eject command. (0=success, nonzero=failure)
194 */
[128]195int eject_device(char *dev)
[1]196{
[2266]197 char *command = NULL;
[128]198 int res1 = 0, res2 = 0;
[1]199
[2325]200 if (dev == NULL) {
201 return (1);
202 }
203
[128]204 if (IS_THIS_A_STREAMING_BACKUP(g_backup_media_type)
205 && g_backup_media_type != udev) {
[2323]206 mr_asprintf(command, "mt -f %s offline", dev);
[128]207 res1 = run_program_and_log_output(command, 1);
[2266]208 mr_free(command);
[128]209 } else {
210 res1 = 0;
211 }
[1]212
213#ifdef __FreeBSD__
[128]214 if (strstr(dev, "acd")) {
[2323]215 mr_asprintf(command, "cdcontrol -f %s eject", dev);
[128]216 } else {
[2324]217 mr_asprintf(command, "camcontrol eject `echo %s | sed 's|/dev/||'`", dev);
[128]218 }
[1]219#else
[2323]220 mr_asprintf(command, "eject %s", dev);
[1]221#endif
222
[128]223 log_msg(3, "Ejecting %s", dev);
224 res2 = run_program_and_log_output(command, 1);
[2266]225 mr_free(command);
[128]226 if (res1 && res2) {
227 return (1);
228 } else {
229 return (0);
230 }
[1]231}
232
233/**
234 * Load (inject) the tray of the specified CD device.
235 * @param dev The device to load/inject.
236 * @return 0 for success, nonzero for failure.
237 */
[128]238int inject_device(char *dev)
[1]239{
[2266]240 char *command = NULL;
[128]241 int i;
[1]242
[2325]243 if (dev == NULL) {
244 return (1);
245 }
246
[1]247#ifdef __FreeBSD__
[128]248 if (strstr(dev, "acd")) {
[2323]249 mr_asprintf(command, "cdcontrol -f %s close", dev);
[128]250 } else {
[2323]251 mr_asprintf(command, "camcontrol load `echo %s | sed 's|/dev/||'`", dev);
[128]252 }
[1]253#else
[2323]254 mr_asprintf(command, "eject -t %s", dev);
[1]255#endif
[128]256 i = run_program_and_log_output(command, FALSE);
[2266]257 mr_free(command);
[128]258 return (i);
[1]259}
260
261
262/**
263 * Determine whether the specified @p device (really, you can use any file)
264 * exists.
265 * @return TRUE if it exists, FALSE if it doesn't.
266 */
[128]267bool does_device_exist(char *device)
[1]268{
269
[128]270 /*@ buffers *********************************************************** */
[2266]271 char *tmp = NULL;
[128]272 bool ret;
[1]273
[128]274 assert_string_is_neither_NULL_nor_zerolength(device);
[1]275
[2323]276 mr_asprintf(tmp, "ls %s > /dev/null 2> /dev/null", device);
[128]277
278 if (system(tmp)) {
279 ret = FALSE;
280 } else {
281 ret = TRUE;
282 }
[2266]283 mr_free(tmp);
[128]284 return (ret);
[1]285}
286
287
288/**
289 * Determine whether a non-Microsoft partition exists on any connected hard drive.
290 * @return TRUE (there's a Linux/FreeBSD partition) or FALSE (Microsoft has taken over yet another innocent PC).
291 */
[128]292bool does_nonMS_partition_exist(void)
[1]293{
294#if __FreeBSD__
[128]295 return
296 !system
297 ("for drive in /dev/ad? /dev/da?; do fdisk $drive | grep -q FreeBSD && exit 0; done; false");
[1]298#else
[128]299 return
300 !system
[681]301 ("parted2fdisk -l 2>/dev/null | grep '^/dev/' | grep -Eqv '(MS|DOS|FAT|NTFS)'");
[1]302#endif
303}
304
305/**
306 * Determine whether the specified @p partno exists on the specified @p drive.
307 * @param drive The drive to search for the partition in.
308 * @param partno The partition number to look for.
309 * @return 0 if it exists, nonzero otherwise.
310 */
[128]311int does_partition_exist(const char *drive, int partno)
[1]312{
[128]313 /*@ buffers **************************************************** */
[2266]314 char *program = NULL;
[2351]315 char *incoming = NULL;
[2211]316 char *searchstr = NULL;
[2316]317 char *tmp = NULL;
[1]318
[128]319 /*@ ints ******************************************************* */
320 int res = 0;
[1]321
[128]322 /*@ pointers *************************************************** */
323 FILE *fin;
[1]324
325
[128]326 /*@ end vars *************************************************** */
327 assert_string_is_neither_NULL_nor_zerolength(drive);
328 assert(partno >= 0 && partno < 999);
[1]329
330#ifdef __FreeBSD__
[128]331 // We assume here that this is running from mondorestore. (It is.)
[2316]332 tmp = build_partition_name(drive, partno);
[2323]333 mr_asprintf(program, "ls %s %s >/dev/null 2>&1", drive, tmp);
[2316]334 mr_free(tmp);
335
[2266]336 res = system(program);
337 mr_free(program);
338 return (res);
[1]339#else
[2316]340 /* To avoid compiler warnings */
341 tmp = NULL;
[1]342#endif
343
[2323]344 mr_asprintf(program, "parted2fdisk -l %s 2> /dev/null", drive);
[128]345 fin = popen(program, "r");
346 if (!fin) {
347 log_it("program=%s", program);
348 log_OS_error("Cannot popen-in program");
[2266]349 mr_free(program);
[128]350 return (0);
[1]351 }
[2266]352 mr_free(program);
353
[2316]354 searchstr = build_partition_name(drive, partno);
355 mr_strcat(searchstr, " ");
[2351]356 for (res = 0, mr_getline(incoming, fin); !res && !feof(fin) ; mr_getline(incoming, fin)) {
[128]357 if (strstr(incoming, searchstr)) {
358 res = 1;
359 }
[2351]360 mr_free(incoming);
[128]361 }
[2351]362 mr_free(incoming);
[2316]363 mr_free(searchstr);
364
[128]365 if (pclose(fin)) {
366 log_OS_error("Cannot pclose fin");
367 }
368 return (res);
[1]369}
370
371
372
373
374
375/**
376 * Determine whether given NULL-terminated @p str exists in the MBR of @p dev.
377 * @param dev The device to look in.
378 * @param str The string to look for.
379 * @return TRUE if it exists, FALSE if it doesn't.
380 */
[128]381bool does_string_exist_in_boot_block(char *dev, char *str)
[1]382{
[128]383 /*@ buffers **************************************************** */
[2266]384 char *command = NULL;
[1]385
[128]386 /*@ end vars *************************************************** */
387 int i;
[1]388
[128]389 assert_string_is_neither_NULL_nor_zerolength(dev);
390 assert_string_is_neither_NULL_nor_zerolength(str);
[1]391
[2323]392 mr_asprintf(command, "dd if=%s bs=446 count=1 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null", dev, str);
[128]393 i = system(command);
[2266]394 mr_free(command);
[128]395 if (i) {
396 return (FALSE);
397 } else {
398 return (TRUE);
399 }
[1]400}
401
402/**
403 * Determine whether specified @p str exists in the first @p n sectors of
404 * @p dev.
405 * @param dev The device to look in.
406 * @param str The string to look for.
407 * @param n The number of 512-byte sectors to search.
408 */
[128]409bool does_string_exist_in_first_N_blocks(char *dev, char *str, int n)
[1]410{
[128]411 /*@ buffers **************************************************** */
[2266]412 char *command = NULL;
[128]413 /*@ end vars *************************************************** */
414 int i;
[1]415
[2323]416 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]417 i = system(command);
[2266]418 mr_free(command);
[128]419 if (i) {
420 return (FALSE);
421 } else {
422 return (TRUE);
423 }
[1]424}
425
426
427
428/**
429 * Try to mount CD-ROM at @p mountpoint. If the CD-ROM is not found or has
430 * not been specified, call find_cdrom_device() to find it.
431 * @param mountpoint Where to mount the CD-ROM.
432 * @return 0 for success, nonzero for failure.
433 * @see mount_CDROM_here
434 */
[2325]435int find_and_mount_actual_cd(char *mountpoint) {
436
[128]437 /*@ buffers ***************************************************** */
[1]438
[128]439 /*@ int's ****************************************************** */
440 int res;
[2325]441 char *dev = NULL;
[2316]442 char *p = NULL;
[1]443
[128]444 /*@ end vars **************************************************** */
[1]445
[128]446 assert(bkpinfo != NULL);
447 assert_string_is_neither_NULL_nor_zerolength(mountpoint);
[1]448
[128]449 if (g_backup_media_type == dvd) {
[2325]450 if (g_dvd_drive_is_here[0]) {
451 mr_asprintf(dev, "%s", g_dvd_drive_is_here);
[128]452 }
[2325]453 if (dev == NULL) {
454 dev = find_dvd_device();
455 }
[128]456 } else {
[2325]457 if (g_cdrom_drive_is_here[0]) {
458 mr_asprintf(dev, "%s", g_cdrom_drive_is_here);
[128]459 }
[2325]460 if (dev == NULL) {
461 dev = find_cdrom_device(FALSE);
462 }
[128]463 }
[1]464
[128]465 if (bkpinfo->backup_media_type != iso) {
466 retract_CD_tray_and_defeat_autorun();
467 }
[1]468
[2325]469 if ((dev == NULL) || (res = mount_CDROM_here(dev, mountpoint))) {
[2316]470 p = popup_and_get_string ("CD-ROM device", "Please enter your CD-ROM's /dev device",dev);
471 if (p == NULL) {
[128]472 res = 1;
473 } else {
[2316]474 res = mount_CDROM_here(p, mountpoint);
[128]475 }
[2316]476 mr_free(p);
[128]477 }
478 if (res) {
[541]479 log_msg(1, "mount failed");
[128]480 } else {
[541]481 log_msg(1, "mount succeeded with %s", dev);
[128]482 }
[2325]483 mr_free(dev);
[128]484 return (res);
[1]485}
486
487
488
489
490
491
492/**
493 * Locate a CD-R/W writer's SCSI node.
494 * @param cdrw_device SCSI node will be placed here.
495 * @return 0 for success, nonzero for failure.
496 */
[2325]497char *find_cdrw_device()
[1]498{
[128]499 /*@ buffers ************************ */
[2214]500 char *tmp = NULL;
[2331]501 char *tmp1 = NULL;
[2214]502 char *cdr_exe = NULL;
[2266]503 char *command = NULL;
[2325]504 char *cdrw_device = NULL;
[1]505
[128]506 if (g_cdrw_drive_is_here[0]) {
[2334]507 mr_asprintf(cdrw_device, "%s", g_cdrw_drive_is_here);
[128]508 log_msg(3, "Been there, done that. Returning %s", cdrw_device);
[2325]509 return(cdrw_device);
[128]510 }
511 if (g_backup_media_type == dvd) {
[2325]512 log_msg(1, "This is dumb. You're calling find_cdrw_device() but you're backing up to DVD. WTF?");
513 return(NULL);
[128]514 }
515 run_program_and_log_output("insmod ide-scsi", -1);
[2331]516 tmp = find_home_of_exe("cdrecord");
517 if (tmp) {
[2323]518 mr_asprintf(cdr_exe, "cdrecord");
[128]519 } else {
[2323]520 mr_asprintf(cdr_exe, "dvdrecord");
[128]521 }
[2331]522 mr_free(tmp);
523
524 tmp1 = find_home_of_exe(cdr_exe);
525 if (tmp1) {
[2323]526 mr_asprintf(command, "%s -scanbus 2> /dev/null | tr -s '\t' ' ' | grep \"[0-9]*,[0-9]*,[0-9]*\" | grep -v \"[0-9]*) \\*\" | grep CD | cut -d' ' -f2 | head -n1", cdr_exe);
527 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
[2266]528 mr_free(command);
[128]529 }
[2331]530 mr_free(tmp1);
531
[2214]532 if ((tmp == NULL) || (strlen(tmp) < 2)) {
533 mr_free(tmp);
534 mr_free(cdr_exe);
[2325]535 return(NULL);
[128]536 } else {
[2325]537 cdrw_device = tmp;
[2324]538 log_it("Found CDRW device - %s", cdrw_device);
[128]539 strcpy(g_cdrw_drive_is_here, cdrw_device);
[2214]540 mr_free(cdr_exe);
[2325]541 return (cdrw_device);
[128]542 }
[1]543}
544
545
546/**
547 * Attempt to locate a CD-ROM device's /dev entry.
548 * Several different methods may be used to find the device, including
549 * calling @c cdrecord, searching @c dmesg, and trial-and-error.
550 * @param output Where to put the located /dev entry.
551 * @param try_to_mount Whether to mount the CD as part of the test; if mount
552 * fails then return failure.
553 * @return 0 for success, nonzero for failure.
554 */
[2325]555char *find_cdrom_device(bool try_to_mount)
[1]556{
[128]557 /*@ pointers **************************************************** */
558 FILE *fin;
559 char *p;
560 char *q;
561 char *r;
[1]562
[128]563 /*@ bool's ****************************************************** */
564 bool found_it = FALSE;
[1]565
[128]566 /*@ buffers ***************************************************** */
[2325]567 char *tmp = NULL;
568 char *output = NULL;
[2214]569 char *cdr_exe = NULL;
[2325]570 char *phrase_two = NULL;
[2266]571 char *command = NULL;
572#ifndef __FreeBSD__
573 char *dvd_last_resort = NULL;
574#endif
575 char *mountpoint = NULL;
[128]576 static char the_last_place_i_found_it[MAX_STR_LEN] = "";
[1]577
[128]578 /*@ intialize *************************************************** */
[1]579
[2325]580 output = NULL;
[2266]581#ifndef __FreeBSD__
[2323]582 mr_asprintf(dvd_last_resort, "%s", "");;
[2266]583#endif
[1]584
[128]585 /*@ end vars **************************************************** */
[1]586
[128]587 if (g_cdrom_drive_is_here[0] && !isdigit(g_cdrom_drive_is_here[0])) {
[2325]588 mr_asprintf(output, "%s", g_cdrom_drive_is_here);
[128]589 log_msg(3, "Been there, done that. Returning %s", output);
[2325]590 return(output);
[128]591 }
592 if (the_last_place_i_found_it[0] != '\0' && !try_to_mount) {
[2325]593 mr_asprintf(output, "%s", the_last_place_i_found_it);
594 log_msg(3, "find_cdrom_device() --- returning last found location - '%s'", output);
595 return(output);
[128]596 }
[1]597
[2323]598 mr_asprintf(mountpoint, "%s/cd.mnt", bkpinfo->tmpdir);
[128]599 make_hole_for_dir(mountpoint);
[1]600
[2331]601 tmp = find_home_of_exe("cdrecord");
602 if (tmp) {
[2323]603 mr_asprintf(cdr_exe, "cdrecord");
[128]604 } else {
[2323]605 mr_asprintf(cdr_exe, "dvdrecord");
[128]606 }
[2331]607 mr_free(tmp);
[2325]608
[2331]609 tmp = find_home_of_exe(cdr_exe);
610 if (!tmp) {
[2325]611 mr_asprintf(output, "/dev/cdrom");
[128]612 log_msg(4, "Can't find cdrecord; assuming %s", output);
[2325]613 mr_free(cdr_exe);
[128]614 if (!does_device_exist(output)) {
615 log_msg(4, "That didn't work. Sorry.");
[2325]616 mr_free(output);
[128]617 }
[2331]618 mr_free(tmp);
619 return(output);
[128]620 }
[2331]621 mr_free(tmp);
[128]622
[2323]623 mr_asprintf(command, "%s -scanbus 2> /dev/null", cdr_exe);
[128]624 fin = popen(command, "r");
[2266]625 log_msg(4, "command=%s", command);
626 mr_free(command);
627
[128]628 if (!fin) {
629 log_OS_error("Cannot popen command");
[2214]630 mr_free(cdr_exe);
[2325]631 return(NULL);
[128]632 }
[2266]633
[2351]634 for (mr_getline(tmp, fin); !feof(fin); mr_getline(tmp, fin)) {
[128]635 p = strchr(tmp, '\'');
636 if (p) {
637 q = strchr(++p, '\'');
638 if (q) {
639 for (r = q; *(r - 1) == ' '; r--);
640 *r = '\0';
641 p = strchr(++q, '\'');
642 if (p) {
643 q = strchr(++p, '\'');
644 if (q) {
645 while (*(q - 1) == ' ') {
646 q--;
647 }
648 *q = '\0';
[2330]649 mr_asprintf(phrase_two, "%s", p);
[128]650 }
651 }
[1]652 }
653 }
[2351]654 mr_free(tmp);
[1]655 }
[2351]656 mr_free(tmp);
[128]657 paranoid_pclose(fin);
[1]658
659#ifndef __FreeBSD__
[2330]660 if (!phrase_two || strlen(phrase_two) == 0) {
[128]661 log_msg(4, "Not running phase two. String is empty.");
662 } else {
[2323]663 mr_asprintf(command, "dmesg | grep \"%s\" 2> /dev/null", phrase_two);
[128]664 fin = popen(command, "r");
[2266]665 mr_free(command);
666
[128]667 if (!fin) {
668 log_msg(4, "Cannot run 2nd command - non-fatal, fortunately");
669 } else {
[2351]670 for (mr_getline(tmp, fin); !feof(fin); mr_getline(tmp, fin)) {
[128]671 log_msg(5, "--> '%s'", tmp);
672 if (tmp[0] != ' ' && tmp[1] != ' ') {
673 p = strchr(tmp, ':');
674 if (p) {
675 *p = '\0';
676 if (strstr(tmp, "DVD")) {
[2266]677 mr_free(dvd_last_resort);
[2323]678 mr_asprintf(dvd_last_resort, "/dev/%s", tmp);
[2325]679 log_msg(4, "Ignoring '%s' because it's a DVD drive", tmp);
[128]680 } else {
[2325]681 mr_asprintf(output, "/dev/%s", tmp);
[128]682 found_it = TRUE;
683 }
684 }
685 }
[2351]686 mr_free(tmp);
[128]687 }
688 paranoid_pclose(fin);
[2351]689 mr_free(tmp);
[128]690 }
691 }
[2330]692 mr_free(phrase_two);
[1]693
694#endif
695#ifdef __FreeBSD__
[128]696 if (!found_it) {
697 log_msg(4, "OK, approach 2");
[2325]698 if (!(output = set_dev_to_this_if_rx_OK("/dev/cdrom"))) {
699 if (!(output = set_dev_to_this_if_rx_OK("/dev/cdrom1"))) {
700 if (!(output = set_dev_to_this_if_rx_OK("/dev/dvd"))) {
701 if (!(output = set_dev_to_this_if_rx_OK("/dev/acd0"))) {
702 if (!(output = set_dev_to_this_if_rx_OK("/dev/cd01"))) {
703 if (!(output = set_dev_to_this_if_rx_OK("/dev/acd1"))) {
704 if (!(output = set_dev_to_this_if_rx_OK("/dev/cd1"))) {
705 mr_free(cdr_exe);
706 return(NULL);
[128]707 }
708 }
709 }
710 }
711 }
712 }
713 }
714 }
[1]715#else
[128]716 if (!found_it && strlen(dvd_last_resort) > 0) {
[2266]717 log_msg(4, "Well, I'll use the DVD - %s - as a last resort", dvd_last_resort);
[2325]718 mr_asprintf(output, "%s", dvd_last_resort);
[128]719 found_it = TRUE;
[1]720 }
[2266]721 mr_free(dvd_last_resort);
[2325]722
[128]723 if (found_it) {
[2351]724 mr_asprintf(tmp, "grep \"%s=ide-scsi\" " CMDLINE " &> /dev/null", strrchr(output, '/') + 1);
725 if (system(tmp) == 0) {
[2325]726 log_msg(4, "%s is not right. It's being SCSI-emulated. Continuing.", output);
[128]727 found_it = FALSE;
[2325]728 mr_free(output);
[128]729 }
[2351]730 mr_free(tmp);
[128]731 }
[1]732
[128]733 if (found_it) {
734 log_msg(4, "(find_cdrom_device) --> '%s'", output);
735 if (!does_device_exist(output)) {
736 found_it = FALSE;
737 log_msg(4, "OK, I was wrong, I haven't found it... yet.");
[2325]738 mr_free(output);
[128]739 }
740 }
[1]741
[128]742 if (!found_it) {
743 log_msg(4, "OK, approach 2");
[2325]744 if (!(output = set_dev_to_this_if_rx_OK("/dev/scd0"))) {
745 if (!(output = set_dev_to_this_if_rx_OK("/dev/sr0"))) {
746 if (!(output = set_dev_to_this_if_rx_OK("/dev/cdrom"))) {
747 if (!(output = set_dev_to_this_if_rx_OK("/dev/cdrom0"))) {
748 if (!(output = set_dev_to_this_if_rx_OK("/dev/cdrom1"))) {
749 if (!(output = set_dev_to_this_if_rx_OK("/dev/sr1"))) {
750 if (!(output = set_dev_to_this_if_rx_OK("/dev/dvd"))) {
751 if (!(output = set_dev_to_this_if_rx_OK(g_cdrw_drive_is_here))) {
752 mr_free(cdr_exe);
753 return(NULL);
[128]754 }
755 }
756 }
757 }
758 }
759 }
760 }
761 }
762 }
[1]763#endif
764
[128]765 if (found_it && try_to_mount) {
766 if (mount_CDROM_here(output, mountpoint)) {
767 log_msg(4, "[Cardigans] I've changed my mind");
768 found_it = FALSE;
[2325]769 mr_free(output);
[128]770 } else {
[2351]771 mr_asprintf(tmp, "%s/archives", mountpoint);
[128]772 if (!does_file_exist(tmp)) {
773 log_msg(4, "[Cardigans] I'll take it back");
774 found_it = FALSE;
[2325]775 mr_free(output);
[128]776 } else {
[2323]777 mr_asprintf(command, "umount %s", output);
[128]778 paranoid_system(command);
[2266]779 mr_free(command);
780
[128]781 log_msg(4, "I'm confident the Mondo CD is in %s", output);
782 }
[2351]783 mr_free(tmp);
[128]784 }
785 }
786 unlink(mountpoint);
[2266]787 mr_free(mountpoint);
[1]788
[128]789 if (found_it) {
790 if (!does_file_exist(output)) {
791 log_msg(3, "I still haven't found it.");
[2325]792 mr_free(cdr_exe);
793 mr_free(output);
794 return (NULL);
[128]795 }
796 log_msg(3, "(find_cdrom_device) --> '%s'", output);
797 strcpy(the_last_place_i_found_it, output);
798 strcpy(g_cdrom_drive_is_here, output);
[2325]799 mr_free(cdr_exe);
800 return (output);
[128]801 }
[1]802
[2323]803 mr_asprintf(command, "%s -scanbus | grep \"[0-9],[0-9],[0-9]\" | grep \"[D|C][V|D]\" | grep -n \"\" | grep \"%s\" | cut -d':' -f2", cdr_exe, g_cdrw_drive_is_here);
[2325]804 mr_free(cdr_exe);
805
[128]806 log_msg(1, "command=%s", command);
[2351]807 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
[2266]808 mr_free(command);
809
[2325]810 mr_free(output);
[2351]811 if (strlen(tmp) > 0) {
812 mr_asprintf(output, "%s", tmp);
[128]813 log_msg(4, "Finally found it at %s", output);
814 } else {
815 log_msg(4, "Still couldn't find it.");
816 }
[2351]817 mr_free(tmp);
[2214]818
[2325]819 return (output);
[1]820}
821
822
[2325]823char *find_dvd_device()
[1]824{
[2266]825 char *tmp = NULL;
[2325]826 int devno = -1;
827 char *output = NULL;
[1]828
[128]829 if (g_dvd_drive_is_here[0]) {
[2334]830 mr_asprintf(output, "%s", g_dvd_drive_is_here);
[128]831 log_msg(3, "Been there, done that. Returning %s", output);
[2325]832 return (output);
[128]833 }
[1]834
[2325]835 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output("dvdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1"));
[128]836 log_msg(5, "tmp = '%s'", tmp);
837 if (!tmp[0])
[2266]838 mr_free(tmp);
[2324]839 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output ("cdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1")
[128]840 );
841 if (tmp[0]) {
842 devno = atoi(tmp) - 1;
843 }
[2266]844 mr_free(tmp);
845
[128]846 if (devno >= 0) {
[2325]847 mr_asprintf(output, "/dev/scd%d", devno);
[128]848 strcpy(g_dvd_drive_is_here, output);
849 log_msg(2, "I think DVD is at %s", output);
850 } else {
851 log_msg(2, "I cannot find DVD");
852 }
[1]853
[2325]854 return (output);
[1]855}
856
857
858
859
860
861#include <sys/ioctl.h>
862
863/**
864 * Find the size of the specified @p drive, in megabytes. Uses @c ioctl calls
865 * and @c dmesg.
866 * @param drive The device to find the size of.
867 * @return size in megabytes.
868 */
[128]869long get_phys_size_of_drive(char *drive)
[1]870{
[128]871 int fd;
[1]872#if linux
[128]873 unsigned long long s = 0;
874 int fileid, cylinders = 0, cylindersleft = 0;
875 int cylindersize = 0;
[1]876 int gotgeo = 0;
877
[128]878
879 struct hd_geometry hdgeo;
[1]880#elif __FreeBSD__
[128]881 off_t s;
[1]882#endif
883
[128]884 long outvalA = -1;
885 long outvalB = -1;
886 long outvalC = -1;
[1]887
[128]888 if ((fd = open(drive, O_RDONLY)) != -1) {
889 if (ioctl(fd,
[1]890#if linux
[128]891#ifdef BLKGETSIZE64
892 BLKGETSIZE64,
893#else
894 BLKGETSIZE,
895#endif
[1]896#elif __FreeBSD__
[128]897 DIOCGMEDIASIZE,
[1]898#endif
[128]899 &s) != -1) {
900 close(fd);
901 // s>>11 works for older disks but not for newer ones
902 outvalB =
[1]903#if linux
[128]904#ifdef BLKGETSIZE64
905 s >> 20
[1]906#else
[128]907 s >> 11
[1]908#endif
[128]909#else
910 s >> 20
911#endif
912 ;
913 }
[1]914 }
915
[128]916 if (outvalB <= 0) {
917 log_msg(1, "Error getting size of %s: %s", drive, strerror(errno));
[1]918#if linux
[161]919 fileid = open(drive, O_RDONLY);
[2205]920 if (fileid != -1) {
[161]921 if (ioctl(fileid, HDIO_GETGEO, &hdgeo) != -1) {
922 if (hdgeo.cylinders && hdgeo.heads && hdgeo.sectors) {
923 cylindersleft = cylinders = hdgeo.cylinders;
924 cylindersize = hdgeo.heads * hdgeo.sectors / 2;
925 outvalA = cylindersize * cylinders / 1024;
926 log_msg(2, "Got Harddisk geometry, C:%d, H:%d, S:%d",
927 hdgeo.cylinders, hdgeo.heads, hdgeo.sectors);
928 gotgeo = 1;
929 } else {
930 log_msg(1, "Harddisk geometry wrong");
931 }
[1]932 } else {
[161]933 log_msg(1,
934 "Error in ioctl() getting new hard disk geometry (%s), resizing in unsafe mode",
935 strerror(errno));
[1]936 }
[161]937 close(fileid);
[1]938 } else {
[161]939 log_msg(1, "Failed to open %s for reading: %s", drive,
[128]940 strerror(errno));
[1]941 }
[161]942 if (!gotgeo) {
943 log_msg(1, "Failed to get harddisk geometry, using old mode");
944 }
[1]945#endif
[161]946 }
[1]947// OLDER DISKS will give ridiculously low value for outvalB (so outvalA is returned) :)
948// NEWER DISKS will give sane value for outvalB (close to outvalA, in other words) :)
949
[128]950 outvalC = (outvalA > outvalB) ? outvalA : outvalB;
951
[1]952// log_msg (5, "drive = %s, error = %s", drive, strerror (errno));
953// fatal_error ("GPSOD: Unable to get size of drive");
[128]954 log_msg(1, "%s --> %ld or %ld --> %ld", drive, outvalA, outvalB,
955 outvalC);
956
957 return (outvalC);
[1]958}
[128]959
[1]960/**
961 * Determine whether @p format is supported by the kernel. Uses /proc/filesystems
962 * under Linux and @c lsvfs under FreeBSD.
963 * @param format The format to test.
964 * @return TRUE if the format is supported, FALSE if not.
965 */
[128]966bool is_this_a_valid_disk_format(char *format)
[1]967{
[2211]968 char *good_formats = NULL;
[2266]969 char *command = NULL;
970 char *format_sz = NULL;
[1]971
[128]972 FILE *pin;
973 int retval;
[1]974
[128]975 assert_string_is_neither_NULL_nor_zerolength(format);
[1]976
[2323]977 mr_asprintf(format_sz, "%s ", format);
[1]978
979#ifdef __FreeBSD__
[2323]980 mr_asprintf(command, "lsvfs | tr -s '\t' ' ' | grep -v Filesys | grep -v -- -- | cut -d' ' -f1 | tr -s '\n' ' '");
[1]981#else
[2323]982 mr_asprintf(command, "grep -v nodev /proc/filesystems | tr -s '\t' ' ' | cut -d' ' -f2 | tr -s '\n' ' '");
[1]983#endif
984
[128]985 pin = popen(command, "r");
[2266]986 mr_free(command);
987
[128]988 if (!pin) {
989 log_OS_error("Unable to read good formats");
990 retval = 0;
991 } else {
[2331]992 mr_getline(good_formats, pin);
[128]993 if (pclose(pin)) {
994 log_OS_error("Cannot pclose good formats");
995 }
[2316]996 mr_strip_spaces(good_formats);
997 mr_strcat(good_formats, " swap lvm raid ntfs-3g ntfs 7 "); // " ntfs 7 " -- um, cheating much? :)
[128]998 if (strstr(good_formats, format_sz)) {
999 retval = 1;
1000 } else {
1001 retval = 0;
1002 }
[2316]1003 mr_free(good_formats);
[128]1004 }
[2266]1005 mr_free(format_sz);
1006
[128]1007 return (retval);
[1]1008}
1009
1010
1011/** @def SWAPLIST_COMMAND The command to list the swap files/partitions in use. */
1012
1013/**
1014 * Determine whether @p device_raw is currently mounted.
1015 * @param device_raw The device to check.
1016 * @return TRUE if it's mounted, FALSE if not.
1017 */
[128]1018bool is_this_device_mounted(char *device_raw)
[1]1019{
1020
[128]1021 /*@ pointers **************************************************** */
1022 FILE *fin;
[1]1023
[128]1024 /*@ buffers ***************************************************** */
[2351]1025 char *incoming = NULL;
[2214]1026 char *device_with_tab = NULL;
1027 char *device_with_space = NULL;
1028 char *tmp = NULL;
1029 bool retval = FALSE;
[128]1030
[1]1031#ifdef __FreeBSD__
[128]1032#define SWAPLIST_COMMAND "swapinfo"
[1]1033#else
[128]1034#define SWAPLIST_COMMAND "cat /proc/swaps"
[1]1035#endif
1036
[128]1037 /*@ end vars **************************************************** */
[1]1038
[2325]1039 if (device_raw == NULL) {
1040 return(FALSE);
1041 }
1042
[128]1043 if (device_raw[0] != '/' && !strstr(device_raw, ":/")) {
1044 log_msg(1, "%s needs to have a '/' prefixed - I'll do it",
1045 device_raw);
[2323]1046 mr_asprintf(tmp, "/%s", device_raw);
[128]1047 } else {
[2323]1048 mr_asprintf(tmp, "%s", device_raw);
[128]1049 }
1050 log_msg(1, "Is %s mounted?", tmp);
1051 if (!strcmp(tmp, "/proc") || !strcmp(tmp, "proc")) {
1052 log_msg(1,
1053 "I don't know how the heck /proc made it into the mountlist. I'll ignore it.");
[2214]1054 mr_free(tmp);
1055 return(FALSE);
[128]1056 }
[2323]1057 mr_asprintf(device_with_tab, "%s\t", tmp);
1058 mr_asprintf(device_with_space, "%s ", tmp);
[2214]1059 mr_free(tmp);
[1]1060
[128]1061 if (!(fin = popen("mount", "r"))) {
1062 log_OS_error("Cannot popen 'mount'");
[2214]1063 return(FALSE);
[1]1064 }
[2351]1065
1066 for (mr_getline(incoming, fin); !feof(fin); mr_getline(incoming, fin)) {
1067 if (strstr(incoming, device_with_space) || strstr(incoming, device_with_tab)) {
[128]1068 paranoid_pclose(fin);
[2351]1069 mr_free(incoming);
[2214]1070 return(TRUE);
[128]1071 }
[2351]1072 mr_free(incoming);
[128]1073 }
[2351]1074 mr_free(incoming);
[2214]1075 mr_free(device_with_tab);
1076 mr_free(device_with_space);
[128]1077 paranoid_pclose(fin);
[2323]1078 mr_asprintf(tmp, "%s | grep -E \"^%s\" > /dev/null 2> /dev/null", SWAPLIST_COMMAND, device_with_space);
[128]1079 log_msg(4, "tmp (command) = '%s'", tmp);
1080 if (!system(tmp)) {
[2214]1081 retval = TRUE;
[128]1082 }
[2214]1083 mr_free(tmp);
1084 return(retval);
[1]1085}
1086
1087#ifdef __FreeBSD__
1088// CODE IS FREEBSD-SPECIFIC
1089/**
1090 * Create a loopback device for specified @p fname.
1091 * @param fname The file to associate with a device.
1092 * @return /dev entry for the device, or NULL if it couldn't be allocated.
1093 */
[128]1094char *make_vn(char *fname)
[1]1095{
[128]1096 char *device = (char *) malloc(MAX_STR_LEN);
1097 char *mddevice = (char *) malloc(32);
1098 char command[MAX_STR_LEN];
1099 int vndev = 2;
1100 if (atoi
1101 (call_program_and_get_last_line_of_output
1102 ("/sbin/sysctl -n kern.osreldate")) < 500000) {
1103 do {
1104 sprintf(mddevice, "vn%ic", vndev++);
1105 sprintf(command, "vnconfig %s %s", mddevice, fname);
1106 if (vndev > 10) {
1107 return NULL;
1108 }
1109 }
1110 while (system(command));
1111 } else {
1112 sprintf(command, "mdconfig -a -t vnode -f %s", fname);
1113 mddevice = call_program_and_get_last_line_of_output(command);
1114 if (!strstr(mddevice, "md")) {
1115 return NULL;
1116 }
1117 }
1118 sprintf(device, "/dev/%s", mddevice);
1119 return device;
[1]1120}
1121
1122
1123
1124// CODE IS FREEBSD-SPECIFIC
1125/**
1126 * Deallocate specified @p dname.
1127 * This should be called when you are done with the device created by make_vn(),
1128 * so the system does not run out of @c vn devices.
1129 * @param dname The device to deallocate.
1130 * @return 0 for success, nonzero for failure.
1131 */
[128]1132int kick_vn(char *dname)
[1]1133{
[2266]1134 char *command = NULL;
1135 int res = 0;
[1]1136
[128]1137 if (strncmp(dname, "/dev/", 5) == 0) {
1138 dname += 5;
1139 }
[1]1140
[128]1141 if (atoi
1142 (call_program_and_get_last_line_of_output
1143 ("/sbin/sysctl -n kern.osreldate")) < 500000) {
[2323]1144 mr_asprintf(command, "vnconfig -d %s", dname);
[128]1145 } else {
[2323]1146 mr_asprintf(command, "mdconfig -d -u %s", dname);
[128]1147 }
[2266]1148 res = system(command);
1149 mr_free(command);
1150 return(res);
[1]1151}
1152#endif
1153
1154
1155/**
1156 * Mount the CD-ROM at @p mountpoint.
1157 * @param device The device (or file if g_ISO_restore_mode) to mount.
1158 * @param mountpoint The place to mount it.
1159 * @return 0 for success, nonzero for failure.
1160 */
[1741]1161int mount_USB_here(char *device, char *mountpoint)
1162{
1163 /*@ buffer ****************************************************** */
[2266]1164 char *command = NULL;
[1741]1165 int retval;
1166
1167 assert_string_is_neither_NULL_nor_zerolength(device);
1168 assert_string_is_neither_NULL_nor_zerolength(mountpoint);
1169
1170 make_hole_for_dir(mountpoint);
1171 if (isdigit(device[0])) {
1172 return(1);
1173 }
[2325]1174 log_msg(4, "(mount_USB_here --- device=%s, mountpoint=%s", device, mountpoint);
[1741]1175
1176#ifdef __FreeBSD__
[2323]1177 mr_asprintf(command, "mount_vfat %s %s 2>> %s", device, mountpoint, MONDO_LOGFILE);
[1741]1178
1179#else
[2323]1180 mr_asprintf(command, "mount %s -t vfat %s 2>> %s", device, mountpoint, MONDO_LOGFILE);
[1741]1181#endif
1182
1183 log_msg(4, command);
1184 retval = system(command);
1185 log_msg(1, "system(%s) returned %d", command, retval);
[2266]1186 mr_free(command);
[1741]1187
1188 return (retval);
1189}
1190
1191/**
1192 * Mount the CD-ROM at @p mountpoint.
1193 * @param device The device (or file if g_ISO_restore_mode) to mount.
1194 * @param mountpoint The place to mount it.
1195 * @return 0 for success, nonzero for failure.
1196 */
[2325]1197int mount_CDROM_here(char *device, const char *mountpoint)
[1]1198{
[128]1199 /*@ buffer ****************************************************** */
[2211]1200 char *command = NULL;
[128]1201 int retval;
[2257]1202#ifdef __FreeBSD__
1203 char *dev = NULL;
1204#else
1205 char *options = NULL;
1206#endif
[1]1207
[128]1208 assert_string_is_neither_NULL_nor_zerolength(mountpoint);
[1]1209
[128]1210 make_hole_for_dir(mountpoint);
[2211]1211
[2325]1212 if ((device == NULL) || (isdigit(device[0]))) {
1213 mr_free(device);
1214 device = find_cdrom_device(FALSE);
[2257]1215 }
1216#ifndef __FreeBSD__
[2323]1217 mr_asprintf(options, "ro");
[2211]1218#endif
1219
[128]1220 if (g_ISO_restore_mode) {
[1]1221
1222#ifdef __FreeBSD__
[2323]1223 mr_asprintf(dev, "%s", make_vn(device));
[128]1224 if (!dev) {
[2323]1225 mr_asprintf(command, "Unable to mount ISO (make_vn(%s) failed)", device);
[128]1226 fatal_error(command);
1227 }
[2325]1228 mr_free(device);
1229 device = dev;
[1]1230#else
[2211]1231 mr_strcat(options, ",loop");
[1]1232#endif
1233
[128]1234 }
[2325]1235 log_msg(4, "(mount_CDROM_here --- device=%s, mountpoint=%s", device, mountpoint);
[128]1236 /*@ end vars *************************************************** */
[1]1237
1238#ifdef __FreeBSD__
[2323]1239 mr_asprintf(command, "mount_cd9660 -r %s %s 2>> %s", device, mountpoint, MONDO_LOGFILE);
[1]1240
1241#else
[2323]1242 mr_asprintf(command, "mount %s -o %s -t iso9660 %s 2>> %s", device, options, mountpoint, MONDO_LOGFILE);
[2211]1243 paranoid_free(options);
[1]1244#endif
1245
[128]1246 log_msg(4, command);
1247 if (strncmp(device, "/dev/", 5) == 0) {
1248 retract_CD_tray_and_defeat_autorun();
1249 }
1250 retval = system(command);
1251 log_msg(1, "system(%s) returned %d", command, retval);
[2325]1252 mr_free(command);
[1]1253
[128]1254 return (retval);
[1]1255}
1256
1257
1258
1259
1260
1261
1262/**
1263 * Ask the user for CD number @p cd_number_i_want.
1264 * Sets g_current_media_number once the correct CD is inserted.
1265 * @param bkpinfo The backup information structure. Fields used:
1266 * - @c bkpinfo->backup_media_type
[20]1267 * - @c bkpinfo->prefix
[1]1268 * - @c bkpinfo->isodir
1269 * - @c bkpinfo->media_device
1270 * - @c bkpinfo->please_dont_eject_when_restoring
1271 * @param cd_number_i_want The CD number to ask for.
1272 */
1273void
[1645]1274insist_on_this_cd_number(int cd_number_i_want)
[1]1275{
1276
[128]1277 /*@ int ************************************************************* */
1278 int res = 0;
[1]1279
1280
[128]1281 /*@ buffers ********************************************************* */
[2266]1282 char *tmp = NULL;
[2242]1283 char *mds = NULL;
[2266]1284 char *request = NULL;
[1]1285
[128]1286 assert(bkpinfo != NULL);
1287 assert(cd_number_i_want > 0);
[1]1288
1289// log_msg(3, "Insisting on CD number %d", cd_number_i_want);
1290
[128]1291 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1292 log_msg(3,
1293 "No need to insist_on_this_cd_number when the backup type isn't CD-R(W) or NFS or ISO");
1294 return;
1295 }
[2323]1296 mr_asprintf(tmp, "mkdir -p " MNT_CDROM);
[128]1297 run_program_and_log_output(tmp, 5);
[2266]1298 mr_free(tmp);
1299
[2325]1300 if (g_ISO_restore_mode || bkpinfo->backup_media_type == iso || bkpinfo->backup_media_type == nfs) {
1301 // BERLIOS --- I'm tempted to do something about this...
1302 // Why unmount and remount again and again?
[128]1303 log_msg(3, "Remounting CD");
1304 g_ISO_restore_mode = TRUE;
1305 if (is_this_device_mounted(MNT_CDROM)) {
1306 run_program_and_log_output("umount " MNT_CDROM, 5);
1307 }
[2323]1308 mr_asprintf(tmp, "mkdir -p %s/isodir &> /dev/null", bkpinfo->tmpdir);
[2202]1309 (void)system(tmp);
[2266]1310 mr_free(tmp);
1311
[2325]1312 if (((bkpinfo->isodir == NULL) && (bkpinfo->nfs_remote_dir == NULL)) || (bkpinfo->prefix == NULL)) {
1313 fatal_error("Unable to prepare ISO file name. Please report to dev team");
1314 }
1315 if (bkpinfo->nfs_remote_dir) {
1316 // NFS
1317 mr_asprintf(tmp, "%s/%s/%s-%d.iso", bkpinfo->isodir, bkpinfo->nfs_remote_dir, bkpinfo->prefix, cd_number_i_want);
1318 } else {
1319 // ISO
1320 mr_asprintf(tmp, "%s/%s-%d.iso", bkpinfo->isodir, bkpinfo->prefix, cd_number_i_want);
1321 }
[128]1322 if (!does_file_exist(tmp)) {
[2266]1323 mr_free(tmp);
[2325]1324 if (bkpinfo->nfs_remote_dir) {
1325 // NFS
1326 mr_asprintf(tmp, "%s/isodir/%s/%s-%d.iso", bkpinfo->tmpdir, bkpinfo->nfs_remote_dir, bkpinfo->prefix, cd_number_i_want);
1327 } else {
1328 // ISO
1329 mr_asprintf(tmp, "%s/isodir/%s-%d.iso", bkpinfo->tmpdir, bkpinfo->prefix, cd_number_i_want);
1330 }
[128]1331 if (does_file_exist(tmp)) {
[2321]1332 log_msg(1, "FIXME - hacking bkpinfo->isodir from '%s' to %s/isodir", bkpinfo->isodir, bkpinfo->tmpdir);
[2323]1333 mr_free(bkpinfo->isodir);
[2324]1334 mr_asprintf(bkpinfo->isodir, "%s/isodir", bkpinfo->tmpdir);
[128]1335 }
1336 }
1337 log_msg(3, "Mounting %s at %s", tmp, MNT_CDROM);
1338 if (mount_CDROM_here(tmp, MNT_CDROM)) {
[2266]1339 mr_free(tmp);
[128]1340 fatal_error("Mommy!");
1341 }
[2266]1342 mr_free(tmp);
[1]1343 }
[1645]1344 if ((res = what_number_cd_is_this()) != cd_number_i_want) {
[2325]1345 log_msg(3, "Currently, we hold %d but we want %d", res, cd_number_i_want);
[2242]1346 mds = media_descriptor_string(bkpinfo->backup_media_type);
[2324]1347 log_msg(3, "Insisting on %s #%d", mds, cd_number_i_want);
[2323]1348 mr_asprintf(request, "Please insert %s #%d and press Enter.", mds, cd_number_i_want);
[2242]1349 mr_free(mds);
[2266]1350
[1645]1351 while (what_number_cd_is_this() != cd_number_i_want) {
[128]1352 paranoid_system("sync");
1353 if (is_this_device_mounted(MNT_CDROM)) {
1354 res =
1355 run_program_and_log_output("umount " MNT_CDROM, FALSE);
1356 } else {
1357 res = 0;
1358 }
1359 if (res) {
[541]1360 log_to_screen("WARNING - failed to unmount CD-ROM drive");
[128]1361 }
1362 if (!bkpinfo->please_dont_eject) {
1363 res = eject_device(bkpinfo->media_device);
1364 } else {
1365 res = 0;
1366 }
1367 if (res) {
[541]1368 log_to_screen("WARNING - failed to eject CD-ROM disk");
[128]1369 }
1370 popup_and_OK(request);
1371 if (!bkpinfo->please_dont_eject) {
1372 inject_device(bkpinfo->media_device);
1373 }
1374 paranoid_system("sync");
1375 }
[2266]1376 mr_free(request);
1377
[128]1378 log_msg(1, "Thankyou. Proceeding...");
1379 g_current_media_number = cd_number_i_want;
1380 }
[1]1381}
1382
1383/* @} - end of deviceGroup */
1384
1385
1386/**
1387 * Ask user for details of backup/restore information.
1388 * Called when @c mondoarchive doesn't get any parameters.
1389 * @param bkpinfo The backup information structure to fill out with the user's data.
1390 * @param archiving_to_media TRUE if archiving, FALSE if restoring.
1391 * @return 0, always.
1392 * @bug No point of `int' return value.
1393 * @ingroup archiveGroup
1394 */
[1645]1395int interactively_obtain_media_parameters_from_user(bool archiving_to_media)
[1]1396// archiving_to_media is TRUE if I'm being called by mondoarchive
1397// archiving_to_media is FALSE if I'm being called by mondorestore
1398{
[2214]1399 char *tmp = NULL;
[2316]1400 char *p = NULL;
[2332]1401 char *tmp1 = NULL;
[2242]1402 char *mds = NULL;
[2266]1403 char *sz_size = NULL;
1404 char *command = NULL;
1405 char *comment = NULL;
[128]1406 char *prompt;
1407 int i;
1408 FILE *fin;
[1]1409
[2266]1410 malloc_string(prompt);
[2306]1411 malloc_string(tmp1);
[128]1412 assert(bkpinfo != NULL);
1413 bkpinfo->nonbootable_backup = FALSE;
[1]1414
[1934]1415 // Tape, CD, NFS, ...?
[128]1416 srandom(getpid());
1417 bkpinfo->backup_media_type =
1418 (g_restoring_live_from_cd) ? cdr :
1419 which_backup_media_type(bkpinfo->restore_data);
1420 if (bkpinfo->backup_media_type == none) {
[541]1421 log_to_screen("User has chosen not to backup the PC");
[128]1422 finish(1);
1423 }
[1934]1424 /* Why asking to remove the media with tape ?
[128]1425 if (bkpinfo->backup_media_type == tape && bkpinfo->restore_data) {
[1885]1426 popup_and_OK("Please remove media from drive(s)");
[128]1427 }
[1934]1428 */
[2330]1429 tmp = bkptype_to_string(bkpinfo->backup_media_type);
1430 log_msg(3, "media type = %s", tmp);
1431 mr_free(tmp);
1432
[128]1433 bkpinfo->cdrw_speed = (bkpinfo->backup_media_type == cdstream) ? 2 : 4;
[2330]1434 bkpinfo->compression_level = (bkpinfo->backup_media_type == cdstream) ? 1 : 5;
1435 bkpinfo->use_lzo = (bkpinfo->backup_media_type == cdstream) ? TRUE : FALSE;
[128]1436 mvaddstr_and_log_it(2, 0, " ");
[1]1437
[1934]1438 // Find device's /dev (or SCSI) entry
[128]1439 switch (bkpinfo->backup_media_type) {
1440 case cdr:
1441 case cdrw:
1442 case dvd:
[1687]1443 case usb:
[1745]1444 /* Never try to eject a USB device */
1445 if (bkpinfo->backup_media_type == usb) {
[1746]1446 bkpinfo->please_dont_eject = TRUE;
[1745]1447 }
[128]1448 if (archiving_to_media) {
[1687]1449 if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) {
[685]1450 if (ask_me_yes_or_no
1451 ("Is your computer a laptop, or does the CD writer incorporate BurnProof technology?"))
1452 {
1453 bkpinfo->manual_cd_tray = TRUE;
1454 }
[128]1455 }
1456 if ((bkpinfo->compression_level =
1457 which_compression_level()) == -1) {
[541]1458 log_to_screen("User has chosen not to backup the PC");
[128]1459 finish(1);
1460 }
[2242]1461 mds = media_descriptor_string(bkpinfo->backup_media_type);
[2323]1462 mr_asprintf(comment, "What speed is your %s (re)writer?", mds);
[2325]1463 mr_free(bkpinfo->media_device);
[128]1464 if (bkpinfo->backup_media_type == dvd) {
[2325]1465 bkpinfo->media_device = find_dvd_device();
[2323]1466 mr_asprintf(tmp, "1");
1467 mr_asprintf(sz_size, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4482 MB
[128]1468 log_msg(1, "Setting to DVD defaults");
1469 } else {
[2325]1470 mr_asprintf(bkpinfo->media_device, "%s", VANILLA_SCSI_CDROM);
[2323]1471 mr_asprintf(tmp, "4");
1472 mr_asprintf(sz_size, "%d", 650);
[128]1473 log_msg(1, "Setting to CD defaults");
1474 }
[1687]1475 if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) {
[2316]1476 p = popup_and_get_string("Speed", comment, tmp);
1477 mr_free(tmp);
1478
1479 if (p == NULL) {
[541]1480 log_to_screen("User has chosen not to backup the PC");
[2266]1481 mr_free(comment);
[128]1482 finish(1);
1483 }
[2316]1484 /* tmp now has the new value given by the user */
1485 tmp = p;
[128]1486 }
[2266]1487 mr_free(comment);
1488
[2306]1489 bkpinfo->cdrw_speed = atoi(tmp1); // if DVD then this shouldn't ever be used anyway :)
[2214]1490
[2323]1491 mr_asprintf(comment, "How much data (in Megabytes) will each %s store?", mds);
[2242]1492 mr_free(mds);
[2316]1493 p = popup_and_get_string("Size", comment, sz_size);
1494 mr_free(sz_size);
1495 mr_free(comment);
1496
1497 if (p == NULL) {
[541]1498 log_to_screen("User has chosen not to backup the PC");
[128]1499 finish(1);
1500 }
[2316]1501 sz_size = p;
[2266]1502
[128]1503 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1504 bkpinfo->media_size[i] = atoi(sz_size);
1505 }
[2266]1506
[128]1507 if (bkpinfo->media_size[0] <= 0) {
[541]1508 log_to_screen("User has chosen not to backup the PC");
[128]1509 finish(1);
1510 }
1511 }
[1737]1512 /* No break because we continue even for usb */
[128]1513 case cdstream:
[2242]1514 mds = media_descriptor_string(bkpinfo->backup_media_type);
1515
[2325]1516 mr_free(bkpinfo->media_device);
[1737]1517 if ((bkpinfo->disaster_recovery) && (bkpinfo->backup_media_type != usb)) {
[2325]1518 mr_asprintf(bkpinfo->media_device, "/dev/cdrom");
1519 log_msg(2, "CD-ROM device assumed to be at %s", bkpinfo->media_device);
1520 } else if ((bkpinfo->restore_data && (bkpinfo->backup_media_type != usb)) || bkpinfo->backup_media_type == dvd) {
1521 if ((bkpinfo->backup_media_type == dvd) || ((bkpinfo->media_device = find_cdrom_device(FALSE)) != NULL)) {
[2323]1522 mr_asprintf(comment, "Please specify your %s drive's /dev entry", mds);
[2316]1523 p = popup_and_get_string("Device?", comment, bkpinfo->media_device);
1524 mr_free(comment);
1525
1526 if (p == NULL) {
[541]1527 log_to_screen("User has chosen not to backup the PC");
[128]1528 finish(1);
1529 }
[2325]1530 mr_free(bkpinfo->media_device);
1531 bkpinfo->media_device = p;
[128]1532 }
[2325]1533 if (bkpinfo->media_device != NULL) {
1534 log_msg(2, "%s device found at %s", mds, bkpinfo->media_device);
1535 } else {
1536 log_msg(2, "%s device not found (bkpinfo->media_device is NULL)", mds);
1537 }
[128]1538 } else {
[2325]1539 if (((bkpinfo->media_device = find_cdrw_device()) != NULL) && (bkpinfo->backup_media_type != usb)) {
1540 mr_free(bkpinfo->media_device);
[128]1541 }
[2325]1542 if (bkpinfo->media_device != NULL) {
[1738]1543 if (bkpinfo->backup_media_type == usb) {
[2323]1544 mr_asprintf(tmp, "I think your %s media corresponds to %s. Is this correct?", mds, bkpinfo->media_device);
[1738]1545 } else {
[2323]1546 mr_asprintf(tmp, "I think I've found your %s burner at SCSI node %s. Is this correct? (Say no if you have an IDE burner and you are running a 2.6 kernel. You will then be prompted for further details.)", mds, bkpinfo->media_device);
[1739]1547 }
[128]1548 if (!ask_me_yes_or_no(tmp)) {
[2325]1549 mr_free(bkpinfo->media_device);
[128]1550 }
[2214]1551 mr_free(tmp);
[128]1552 }
[2325]1553 if (!bkpinfo->media_device) {
[1737]1554 if (bkpinfo->backup_media_type == usb) {
[2325]1555 p = popup_and_get_string("/dev entry?", "What is the /dev entry of your USB Disk/Key, please?", NULL);
[1737]1556 } else {
1557 if (g_kernel_version < 2.6) {
[2325]1558 p = popup_and_get_string("Device node?", "What is the SCSI node of your CD (re)writer, please?", NULL);
[1709]1559 } else {
[2325]1560 p = popup_and_get_string("/dev entry?", "What is the /dev entry of your CD (re)writer, please?", NULL);
[1709]1561 }
[128]1562 }
[2316]1563 if (p == NULL) {
[541]1564 log_to_screen("User has chosen not to backup the PC");
[128]1565 finish(1);
1566 }
[2325]1567 bkpinfo->media_device = p;
[128]1568 }
1569 }
[2242]1570 mr_free(mds);
1571
[128]1572 if (bkpinfo->backup_media_type == cdstream) {
1573 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1574 bkpinfo->media_size[i] = 650;
1575 }
1576 }
1577 break;
1578 case udev:
1579 if (!ask_me_yes_or_no
[541]1580 ("This option is for advanced users only. Are you sure?")) {
1581 log_to_screen("User has chosen not to backup the PC");
[128]1582 finish(1);
1583 }
1584 case tape:
[1]1585
[2325]1586 mr_free(bkpinfo->media_device);
[2334]1587 if ((!bkpinfo->restore_mode) && ((bkpinfo->media_device = mr_find_tape_device()) == NULL)) {
[541]1588 log_msg(3, "Ok, using vanilla scsi tape.");
[2325]1589 mr_asprintf(bkpinfo->media_device, "%s", VANILLA_SCSI_TAPE);
[128]1590 if ((fin = fopen(bkpinfo->media_device, "r"))) {
1591 paranoid_fclose(fin);
1592 } else {
[2325]1593 mr_free(bkpinfo->media_device);
1594 mr_asprintf(bkpinfo->media_device, "/dev/osst0");
[128]1595 }
1596 }
[2325]1597 if (bkpinfo->media_device != NULL) {
[128]1598 if ((fin = fopen(bkpinfo->media_device, "r"))) {
1599 paranoid_fclose(fin);
1600 } else {
1601 if (does_file_exist("/tmp/mondo-restore.cfg")) {
[2325]1602 mr_free(bkpinfo->media_device);
1603 bkpinfo->media_device = read_cfg_var("/tmp/mondo-restore.cfg", "media-dev");
[128]1604 }
1605 }
1606 }
[2325]1607 if (bkpinfo->media_device != NULL) {
[2323]1608 mr_asprintf(tmp, "I think I've found your tape streamer at %s; am I right on the money?", bkpinfo->media_device);
[128]1609 if (!ask_me_yes_or_no(tmp)) {
[2325]1610 mr_free(bkpinfo->media_device);
[128]1611 }
[2214]1612 mr_free(tmp);
[128]1613 }
[2325]1614 if (bkpinfo->media_device == NULL) {
[2316]1615 p = popup_and_get_string("Device name?", "What is the /dev entry of your tape streamer?", bkpinfo->media_device);
1616 if (p == NULL) {
[541]1617 log_to_screen("User has chosen not to backup the PC");
[128]1618 finish(1);
1619 }
[2325]1620 bkpinfo->media_device = p;
[128]1621 }
[2323]1622 mr_asprintf(tmp, "ls -l %s", bkpinfo->media_device);
[128]1623 if (run_program_and_log_output(tmp, FALSE)) {
[541]1624 log_to_screen("User has not specified a valid /dev entry");
[128]1625 finish(1);
1626 }
[2214]1627 mr_free(tmp);
1628
[2334]1629 bkpinfo->use_obdr = ask_me_yes_or_no("Do you want to activate OBDR support for your tapes ?");
[2266]1630 bkpinfo->media_size[0] = 0;
[128]1631 log_msg(4, "media_size[0] = %ld", bkpinfo->media_size[0]);
1632 if (bkpinfo->media_size[0] <= 0) {
1633 bkpinfo->media_size[0] = 0;
1634 }
1635 for (i = 1; i <= MAX_NOOF_MEDIA; i++) {
1636 bkpinfo->media_size[i] = bkpinfo->media_size[0];
1637 }
1638 if (archiving_to_media) {
1639 if ((bkpinfo->compression_level =
1640 which_compression_level()) == -1) {
[541]1641 log_to_screen("User has chosen not to backup the PC");
[128]1642 finish(1);
1643 }
1644 }
1645 break;
[1]1646
1647
1648
[128]1649 case nfs:
[1848]1650 /* Never try to eject a NFS device */
1651 bkpinfo->please_dont_eject = TRUE;
1652
[1858]1653 /* Initiate bkpinfo nfs_mount path from running environment if not already done */
[2325]1654 if (bkpinfo->nfs_mount == NULL) {
1655 mr_asprintf(bkpinfo->nfs_mount, "%s", call_program_and_get_last_line_of_output("mount | grep \":\" | cut -d' ' -f1 | head -n1"));
[128]1656 }
[1]1657#ifdef __FreeBSD__
[128]1658 if (TRUE)
[1]1659#else
[128]1660 if (!bkpinfo->disaster_recovery)
[1]1661#endif
[128]1662 {
[2316]1663 p = popup_and_get_string("NFS dir.", "Please enter path and directory where archives are stored remotely. (Mondo has taken a guess at the correct value. If it is incorrect, delete it and type the correct one.)", bkpinfo->nfs_mount);
1664 if (p == NULL) {
[541]1665 log_to_screen("User has chosen not to backup the PC");
[128]1666 finish(1);
1667 }
[2325]1668 mr_free(bkpinfo->nfs_mount);
1669 bkpinfo->nfs_mount = p;
[128]1670 if (!bkpinfo->restore_data) {
1671 if ((bkpinfo->compression_level =
1672 which_compression_level()) == -1) {
[541]1673 log_to_screen("User has chosen not to backup the PC");
[128]1674 finish(1);
1675 }
1676 }
1677 // check whether already mounted - we better remove
1678 // surrounding spaces and trailing '/' for this
[2325]1679 mr_strip_spaces(bkpinfo->nfs_mount);
[128]1680 if (bkpinfo->nfs_mount[strlen(bkpinfo->nfs_mount) - 1] == '/')
1681 bkpinfo->nfs_mount[strlen(bkpinfo->nfs_mount) - 1] = '\0';
[2323]1682 mr_asprintf(command, "mount | grep \"%s \" | cut -d' ' -f3", bkpinfo->nfs_mount);
1683 mr_free(bkpinfo->isodir);
[2324]1684 mr_asprintf(bkpinfo->isodir, "%s", call_program_and_get_last_line_of_output(command));
[2266]1685 mr_free(command);
[256]1686
[1847]1687 if (!bkpinfo->restore_data) {
[2323]1688 mr_asprintf(comment, "How much data (in Megabytes) will each media store?");
[2316]1689 // BERLIOS: 4480 shouldn't be hardcoded here
1690 sz_size = popup_and_get_string("Size", comment, "4480");
1691 mr_free(comment);
1692 if (sz_size == NULL) {
[1847]1693 log_to_screen("User has chosen not to backup the PC");
1694 finish(1);
1695 }
1696 } else {
[2323]1697 mr_asprintf(sz_size, "0");
[256]1698 }
1699 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1700 bkpinfo->media_size[i] = atoi(sz_size);
1701 }
[2316]1702 mr_free(sz_size);
[1917]1703 if (bkpinfo->media_size[0] < 0) {
[541]1704 log_to_screen("User has chosen not to backup the PC");
[256]1705 finish(1);
1706 }
[128]1707 }
1708 if (bkpinfo->disaster_recovery) {
[2323]1709 mr_asprintf(command ,"umount %s/isodir 2> /dev/null", bkpinfo->tmpdir);
[2202]1710 (void)system(command);
[2266]1711 mr_free(command);
1712
[2316]1713 p = popup_and_get_string("NFS share", "Which remote NFS share should I mount?", bkpinfo->nfs_mount);
1714 if (p == NULL) {
[541]1715 log_to_screen("User has chosen not to backup the PC");
[128]1716 finish(1);
1717 }
[2325]1718 mr_free(bkpinfo->nfs_mount);
1719 bkpinfo->nfs_mount = p;
[128]1720 }
[1858]1721 /* Initiate bkpinfo isodir path from running environment if mount already done */
[2323]1722 mr_free(bkpinfo->isodir);
[1858]1723 if (is_this_device_mounted(bkpinfo->nfs_mount)) {
[2324]1724 mr_asprintf(bkpinfo->isodir, "%s", call_program_and_get_last_line_of_output("mount | grep \":\" | cut -d' ' -f3 | head -n1"));
[2323]1725
[1858]1726 } else {
[2324]1727 mr_asprintf(bkpinfo->isodir, "%s/nfsdir", bkpinfo->tmpdir);
[2323]1728 mr_asprintf(command, "mkdir -p %s", bkpinfo->isodir);
[128]1729 run_program_and_log_output(command, 5);
[2266]1730 mr_free(command);
1731
[2364]1732 if (bkpinfo->restore_data) {
1733 mr_asprintf(tmp, "mount -t nfs -o nolock,ro %s %s", bkpinfo->nfs_mount, bkpinfo->isodir);
1734 else {
1735 mr_asprintf(tmp, "mount -t nfs -o nolock %s %s", bkpinfo->nfs_mount, bkpinfo->isodir);
1736 }
[1819]1737 run_program_and_log_output(tmp, 3);
[2214]1738 mr_free(tmp);
1739
[128]1740 malloc_string(g_selfmounted_isodir);
1741 strcpy(g_selfmounted_isodir, bkpinfo->isodir);
1742 }
1743 if (!is_this_device_mounted(bkpinfo->nfs_mount)) {
[2324]1744 popup_and_OK("Please mount that partition before you try to backup to or restore from it.");
[128]1745 finish(1);
1746 }
[2316]1747 p = popup_and_get_string("Directory", "Which directory within that mountpoint?", bkpinfo->nfs_remote_dir);
1748 if (p == NULL) {
[541]1749 log_to_screen("User has chosen not to backup the PC");
[128]1750 finish(1);
1751 }
[2325]1752 mr_free(bkpinfo->nfs_remote_dir);
1753 bkpinfo->nfs_remote_dir = p;
[2214]1754
[128]1755 // check whether writable - we better remove surrounding spaces for this
[2325]1756 mr_strip_spaces(bkpinfo->nfs_remote_dir);
[1767]1757
[2316]1758 p = popup_and_get_string("Prefix.", "Please enter the prefix that will be prepended to your ISO filename. Example: machine1 to obtain machine1-[1-9]*.iso files", bkpinfo->prefix);
1759 if (p == NULL) {
[541]1760 log_to_screen("User has chosen not to backup the PC");
[251]1761 finish(1);
1762 }
[2322]1763 mr_free(bkpinfo->prefix);
1764 bkpinfo->prefix = p;
[251]1765 log_msg(3, "prefix set to %s", bkpinfo->prefix);
[99]1766
[2316]1767 log_msg(3, "Just set nfs_remote_dir to %s", bkpinfo->nfs_remote_dir);
[128]1768 log_msg(3, "isodir is still %s", bkpinfo->isodir);
1769 break;
[1]1770
[128]1771 case iso:
1772 if (!bkpinfo->disaster_recovery) {
[2316]1773 p = popup_and_get_string("Storage dir.", "Please enter the full path name to the directory for your ISO images. Example: /mnt/raid0_0", bkpinfo->isodir);
1774 if (p == NULL) {
[541]1775 log_to_screen("User has chosen not to backup the PC");
[128]1776 finish(1);
1777 }
[2323]1778 bkpinfo->isodir = p;
[2316]1779
[128]1780 if (archiving_to_media) {
1781 if ((bkpinfo->compression_level =
1782 which_compression_level()) == -1) {
[541]1783 log_to_screen("User has chosen not to backup the PC");
[128]1784 finish(1);
1785 }
[2316]1786 p = popup_and_get_string("ISO size.", "Please enter how big you want each ISO image to be (in megabytes). This should be less than or equal to the size of the CD-R[W]'s or DVD's you plan to backup to.", sz_size);
1787 if (p == NULL) {
[541]1788 log_to_screen("User has chosen not to backup the PC");
[128]1789 finish(1);
1790 }
1791 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
[2316]1792 bkpinfo->media_size[i] = atoi(p);
[128]1793 }
[2316]1794 mr_free(p);
[128]1795 } else {
1796 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1797 bkpinfo->media_size[i] = 650;
1798 }
1799 }
1800 }
[2316]1801 p = popup_and_get_string("Prefix.", "Please enter the prefix that will be prepended to your ISO filename. Example: machine1 to obtain machine1-[1-9]*.iso files", bkpinfo->prefix);
1802 if (p == NULL) {
[804]1803 log_to_screen("User has chosen not to backup the PC");
1804 finish(1);
1805 }
[2322]1806 mr_free(bkpinfo->prefix);
1807 bkpinfo->prefix = p;
[804]1808 log_msg(3, "prefix set to %s", bkpinfo->prefix);
[128]1809 break;
[2322]1810
[128]1811 default:
1812 fatal_error
1813 ("I, Mojo Jojo, shall defeat those pesky Powerpuff Girls!");
1814 }
[1]1815
[128]1816 if (archiving_to_media) {
[1]1817
[2326]1818 mr_free(bkpinfo->boot_device);
[1]1819#ifdef __FreeBSD__
[2326]1820#define EXAMPLEBD "/dev/ad0"
1821 mr_asprintf(bkpinfo->boot_device, "%s", call_program_and_get_last_line_of_output("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/\\([0-9]\\).*/\\1/'"));
[1]1822#else
[2326]1823#define EXAMPLEBD "/dev/hda"
1824 mr_asprintf(bkpinfo->boot_device, "%s", call_program_and_get_last_line_of_output("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'"));
[1]1825#endif
[128]1826 i = which_boot_loader(bkpinfo->boot_device);
1827 if (i == 'U') // unknown
1828 {
[1]1829
[2326]1830 p = popup_and_get_string("Boot device", "What is your boot device? (e.g. "EXAMPLEBD")", bkpinfo->boot_device);
1831#undef EXAMPLEBD
1832 if (p == NULL) {
[541]1833 log_to_screen("User has chosen not to backup the PC");
[128]1834 finish(1);
1835 }
[2326]1836 mr_free(bkpinfo->boot_device);
1837 bkpinfo->boot_device = p;
1838#ifdef __FreeBSD__
[128]1839 i = which_boot_loader(bkpinfo->boot_device);
[1]1840#else
[2316]1841 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "LILO")) {
[128]1842 i = 'L';
1843 } else
[2316]1844 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "ELILO")) {
[128]1845 i = 'E';
1846 } else
[2316]1847 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "GRUB")) {
[128]1848 i = 'G';
1849 } else {
1850 i = 'U';
1851 }
[1]1852#endif
[128]1853 if (i == 'U') {
[2326]1854 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]1855 i = 'R'; // raw
1856 } else {
[2326]1857 log_to_screen("I cannot find your boot loader. Please run mondoarchive with parameters.");
[128]1858 finish(1);
1859 }
1860 }
[1]1861 }
[128]1862 bkpinfo->boot_loader = i;
[2319]1863 mr_free(bkpinfo->include_paths);
[2323]1864 mr_asprintf(p, "/");
[2319]1865 bkpinfo->include_paths = p;
1866
[2316]1867 p = popup_and_get_string("Backup paths", "Please enter paths which you want me to backup. The default is '/' (i.e. everything).", bkpinfo->include_paths);
1868 if (p == NULL) {
[541]1869 log_to_screen("User has chosen not to backup the PC");
[128]1870 finish(1);
[1]1871 }
[2319]1872 mr_free(bkpinfo->include_paths);
1873 bkpinfo->include_paths = p;
[2316]1874
[2323]1875 mr_asprintf(tmp, "%s", list_of_NFS_mounts_only());
[128]1876 if (strlen(tmp) > 2) {
[2318]1877 mr_strcat(bkpinfo->exclude_paths, " %s",tmp);
[128]1878 }
[2214]1879 mr_free(tmp);
[1]1880// NTFS
[2323]1881 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output("parted2fdisk -l | grep -i ntfs | awk '{ print $1};' | tr -s '\\n' ' ' | awk '{ print $0};'"));
1882 if (strlen(tmp) > 2) {
[2316]1883 p = popup_and_get_string("NTFS partitions", "Please enter/confirm the NTFS partitions you wish to backup as well.", tmp);
1884
1885 if (p == NULL) {
[541]1886 log_to_screen("User has chosen not to backup the PC");
[128]1887 finish(1);
1888 }
[2327]1889 mr_free(bkpinfo->image_devs);
1890 bkpinfo->image_devs = p;
[128]1891 }
[1]1892
1893
[2316]1894 p = popup_and_get_string("Exclude paths", "Please enter paths which you do NOT want to backup. Separate them with spaces. NB: /tmp and /proc are always excluded. :-) Just hit 'Enter' if you want to do a full system backup.", bkpinfo->exclude_paths);
1895 if (p == NULL) {
[541]1896 log_to_screen("User has chosen not to backup the PC");
[128]1897 finish(1);
1898 }
[2318]1899 mr_free(bkpinfo->exclude_paths);
1900 bkpinfo->exclude_paths = p;
[2316]1901
1902 p = popup_and_get_string("Temporary directory", "Please enter your temporary directory.", bkpinfo->tmpdir);
1903 if (p == NULL) {
[2220]1904 log_to_screen("User has chosen not to backup the PC");
1905 finish(1);
1906 }
[2321]1907 mr_free(bkpinfo->tmpdir);
1908 bkpinfo->tmpdir = p;
[2316]1909
1910 p = popup_and_get_string("Scratch directory", "Please enter your scratch directory.", bkpinfo->scratchdir);
1911 if (p == NULL) {
[2220]1912 log_to_screen("User has chosen not to backup the PC");
1913 finish(1);
1914 }
[2320]1915 mr_free(bkpinfo->scratchdir);
1916 bkpinfo->scratchdir = p;
[2316]1917
[1575]1918// Interactive mode:
1919#ifdef __IA64__
1920 bkpinfo->make_cd_use_lilo = TRUE;
1921#else
[128]1922 bkpinfo->make_cd_use_lilo = FALSE;
[1575]1923#endif
[128]1924 bkpinfo->backup_data = TRUE;
1925 bkpinfo->verify_data =
1926 ask_me_yes_or_no
[541]1927 ("Will you want to verify your backups after Mondo has created them?");
[1]1928
1929#ifndef __FreeBSD__
[128]1930 if (!ask_me_yes_or_no
[541]1931 ("Are you confident that your kernel is a sane, sensible, standard Linux kernel? Say 'no' if you are using a Gentoo <1.4 or Debian <3.0, please."))
[1]1932#endif
[128]1933 {
[2328]1934 mr_free(bkpinfo->kernel_path);
1935 mr_asprintf(bkpinfo->kernel_path, "FAILSAFE");
[128]1936 }
[1]1937
[128]1938 if (!ask_me_yes_or_no
[541]1939 ("Are you sure you want to proceed? Hit 'no' to abort.")) {
1940 log_to_screen("User has chosen not to backup the PC");
[128]1941 finish(1);
1942 }
1943 } else {
1944 bkpinfo->restore_data = TRUE; // probably...
1945 }
[1]1946
[128]1947 if (bkpinfo->backup_media_type == iso
1948 || bkpinfo->backup_media_type == nfs) {
1949 g_ISO_restore_mode = TRUE;
1950 }
[1]1951#ifdef __FreeSD__
1952// skip
1953#else
[128]1954 if (bkpinfo->backup_media_type == nfs) {
[2323]1955 log_msg(3, "I think the NFS mount is mounted at %s", bkpinfo->isodir);
[128]1956 }
1957 log_it("isodir = %s", bkpinfo->isodir);
[2325]1958 if (bkpinfo->nfs_mount) {
1959 log_it("nfs_mount = '%s'", bkpinfo->nfs_mount);
1960 }
[2226]1961 if (bkpinfo->nfs_user) {
[2224]1962 log_it("nfs_user = '%s'", bkpinfo->nfs_user);
1963 }
[1]1964#endif
1965
[128]1966 log_it("media device = %s", bkpinfo->media_device);
1967 log_it("media size = %ld", bkpinfo->media_size[1]);
[2330]1968 tmp = bkptype_to_string(bkpinfo->backup_media_type);
1969 log_it("media type = %s", tmp);
1970 mr_free(tmp);
1971
[2327]1972 if (bkpinfo->prefix) {
1973 log_it("prefix = %s", bkpinfo->prefix);
1974 }
[128]1975 log_it("compression = %ld", bkpinfo->compression_level);
[2327]1976 if (bkpinfo->include_paths) {
1977 log_it("include_paths = '%s'", bkpinfo->include_paths);
1978 }
1979 if (bkpinfo->exclude_paths) {
1980 log_it("exclude_paths = '%s'", bkpinfo->exclude_paths);
1981 }
[128]1982 log_it("scratchdir = '%s'", bkpinfo->scratchdir);
1983 log_it("tmpdir = '%s'", bkpinfo->tmpdir);
[2327]1984 if (bkpinfo->image_devs) {
1985 log_it("image_devs = '%s'", bkpinfo->image_devs);
1986 }
[2322]1987 log_it("boot_device = '%s' (loader=%c)", bkpinfo->boot_device, bkpinfo->boot_loader);
[128]1988 if (bkpinfo->media_size[0] < 0) {
1989 if (archiving_to_media) {
1990 fatal_error("Media size is less than zero.");
1991 } else {
1992 log_msg(2, "Warning - media size is less than zero.");
1993 bkpinfo->media_size[0] = 0;
1994 }
[1]1995 }
[128]1996 paranoid_free(sz_size);
[2306]1997 paranoid_free(tmp1);
[128]1998 paranoid_free(prompt);
1999 return (0);
[1]2000}
2001
2002
2003
2004
2005/**
2006 * @addtogroup utilityGroup
2007 * @{
2008 */
2009/**
2010 * Get a space-separated list of NFS devices and mounts.
2011 * @return The list created.
2012 * @note The return value points to static data that will be overwritten with each call.
2013 */
2014char *list_of_NFS_devices_and_mounts(void)
2015{
[2214]2016 char *exclude_these_devices = NULL;
2017 char *exclude_these_directories = NULL;
[2187]2018 static char result_sz[1024];
[1]2019
[2323]2020 mr_asprintf(exclude_these_directories,"%s",list_of_NFS_mounts_only());
2021 mr_asprintf(exclude_these_devices,"%s", call_program_and_get_last_line_of_output("tr -s '\t' ' ' < /etc/fstab | grep -E '( (coda|ncpfs|nfs|nfs4|smbfs|cifs|afs|gfs|ocfs|ocfs2|mvfs|nsspool|nsvol) )' | cut -d' ' -f1 | tr -s '\n' ' ' | awk '{print $0;}'"));
[2224]2022 snprintf(result_sz, 1023, "%s %s", exclude_these_directories, exclude_these_devices);
[2214]2023 mr_free(exclude_these_devices);
2024 mr_free(exclude_these_directories);
[128]2025 return (result_sz);
[1]2026}
2027
2028
2029
2030
2031/**
2032 * Get a space-separated list of NFS mounts.
2033 * @return The list created.
2034 * @note The return value points to static data that will be overwritten with each call.
2035 * @bug Even though we only want the mounts, the devices are still checked.
2036 */
2037char *list_of_NFS_mounts_only(void)
2038{
[2214]2039 char *exclude_these_directories = NULL;
[128]2040 static char result_sz[512];
[1]2041
[2323]2042 mr_asprintf(exclude_these_directories,"%s", call_program_and_get_last_line_of_output("mount -t coda,ncpfs,nfs,nfs4,smbfs,cifs,afs,gfs,ocfs,ocfs2,mvfs,nsspool,nssvol | tr -s '\t' ' ' | cut -d' ' -f3 | tr -s '\n' ' ' | awk '{print $0;}'"));
[2224]2043 snprintf(result_sz, 511, "%s", exclude_these_directories);
[2214]2044 mr_free(exclude_these_directories);
[128]2045 return (result_sz);
[1]2046}
2047
2048/* @} - end of utilityGroup */
2049
2050
2051
2052
2053
2054/**
2055 * Create a randomly-named FIFO. The format is @p stub "." [random] [random] where
2056 * [random] is a random number between 1 and 32767.
2057 * @param store_name_here Where to store the new filename.
2058 * @param stub A random number will be appended to this to make the FIFO's name.
2059 * @ingroup deviceGroup
2060 */
[128]2061void make_fifo(char *store_name_here, char *stub)
[1]2062{
[2266]2063 char *tmp = NULL;
[1]2064
[128]2065 assert_string_is_neither_NULL_nor_zerolength(stub);
[1]2066
[128]2067 sprintf(store_name_here, "%s%d%d", stub, (int) (random() % 32768),
2068 (int) (random() % 32768));
2069 make_hole_for_file(store_name_here);
2070 mkfifo(store_name_here, S_IRWXU | S_IRWXG);
[2323]2071 mr_asprintf(tmp, "chmod 770 %s", store_name_here);
[128]2072 paranoid_system(tmp);
[2266]2073 mr_free(tmp);
[1]2074}
2075
2076
2077
2078
2079
2080
2081/**
2082 * Set the tmpdir and scratchdir to reside on the partition with the most free space.
2083 * Automatically excludes DOS, NTFS, SMB, and NFS filesystems.
2084 * @param bkpinfo The backup information structure. @c bkpinfo->tmpdir and @c bkpinfo->scratchdir will be set.
2085 * @ingroup utilityGroup
2086 */
[2321]2087void sensibly_set_scratchdir()
[1]2088{
[2211]2089 char *tmp = NULL;
[2214]2090 char *command = NULL;
2091 char *sz = NULL;
[1]2092
2093#ifdef __FreeBSD__
[2349]2094 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output("LANGUAGE=C df -m -P -t nonfs,msdosfs,ntfs,ntfs-3g,smbfs,smb,cifs,afs,gfs,ocfs,ocfs2,mvfs,nsspool,nssvol | grep -vE \"none|Filesystem\" | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'"));
[1]2095#else
[2349]2096 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output("LANGUAGE=C df -m -P -x nfs -x nfs4 -x vfat -x ntfs -x ntfs-3g -x smbfs -x smb -x cifs -x afs -x gfs -x ocfs -x ocfs2 -x mvfs -x nsspool -x nssvol -x iso9660 | grep -vE \"none|Filesystem|/dev/shm\" | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'"));
[1]2097#endif
2098
[128]2099 if (tmp[0] != '/') {
[2323]2100 mr_asprintf(sz, "%s", tmp);
[2320]2101 mr_free(tmp);
[2323]2102 mr_asprintf(tmp, "/%s", sz);
[2214]2103 mr_free(sz);
[128]2104 }
2105 if (!tmp[0]) {
[2321]2106 fatal_error("I couldn't figure out the scratchdir!");
[128]2107 }
[2325]2108
2109 /* Cleaning a potential previous scratchdir */
2110 if (bkpinfo->scratchdir) {
2111 mr_asprintf(command, "rm -Rf %s", bkpinfo->scratchdir);
2112 paranoid_system(command);
2113 mr_free(command);
2114 }
[2320]2115 mr_free(bkpinfo->scratchdir);
[2325]2116
[2324]2117 mr_asprintf(bkpinfo->scratchdir , "%s/mondo.scratch.%d", tmp, (int) (random() % 32768));
[128]2118 log_it("bkpinfo->scratchdir is being set to %s", bkpinfo->scratchdir);
[1]2119
[2325]2120 /* Cleaning potential previous scratchdir */
[2323]2121 mr_asprintf(command, "rm -Rf %s/mondo.scratch.*", tmp);
[2320]2122 mr_free(tmp);
[2211]2123
[128]2124 paranoid_system(command);
[2214]2125 mr_free(command);
[1]2126}
2127
2128
2129
2130
2131
2132
2133/**
2134 * @addtogroup deviceGroup
2135 * @{
2136 */
2137/**
[2325]2138 * If we can read @p dev, set @return output to it.
2139 * If @p dev cannot be read, set @p output to NULL.
[1]2140 * @param dev The device to check for.
[2325]2141 * @return output Set to @p dev if @p dev exists, NULL otherwise.
[1]2142 */
[2325]2143char *set_dev_to_this_if_rx_OK(char *dev)
[1]2144{
[2266]2145 char *command = NULL;
[2325]2146 char *output = NULL;
[1]2147
[128]2148 if (!dev || dev[0] == '\0') {
[2325]2149 return(NULL);
[128]2150 }
2151 log_msg(10, "Injecting %s", dev);
2152 inject_device(dev);
2153 if (!does_file_exist(dev)) {
2154 log_msg(10, "%s doesn't exist. Returning FALSE.", dev);
[2325]2155 return(NULL);
[128]2156 }
[2323]2157 mr_asprintf(command, "dd bs=%ld count=1 if=%s of=/dev/null &> /dev/null", 512L, dev);
[128]2158 if (!run_program_and_log_output(command, FALSE)
2159 && !run_program_and_log_output(command, FALSE)) {
[2325]2160 mr_asprintf(output, "%s", dev);
[128]2161 log_msg(4, "Found it - %s", dev);
2162 } else {
2163 log_msg(4, "It's not %s", dev);
2164 }
[2266]2165 mr_free(command);
[2325]2166 return(output);
[1]2167}
2168
2169
2170
2171
2172
2173/**
2174 * Find out what number CD is in the drive.
2175 * @param bkpinfo The backup information structure. The @c bkpinfo->media_device field is the only one used.
2176 * @return The current CD number, or -1 if it could not be found.
2177 * @note If the CD is not mounted, it will be mounted
2178 * (and remain mounted after this function returns).
2179 */
[1645]2180int what_number_cd_is_this()
[1]2181{
[128]2182 int cd_number = -1;
[2211]2183 char *mountdev = NULL;
2184 char *tmp = NULL;
[1]2185
[128]2186 assert(bkpinfo != NULL);
[2324]2187 // log_it("Asking what_number_cd_is_this");
[128]2188 if (g_ISO_restore_mode) {
[2323]2189 mr_asprintf(tmp, "mount | grep iso9660 | awk '{print $3;}'");
2190 mr_asprintf(mountdev, "%s%s", call_program_and_get_last_line_of_output(tmp), "/archives/THIS-CD-NUMBER");
[2324]2191 mr_free(tmp);
[2211]2192
[2357]2193 tmp = last_line_of_file(mountdev);
2194 cd_number = atoi(tmp);
2195 mr_free(tmp);
2196 mr_free(mountdev);
[128]2197 return (cd_number);
2198 }
[1]2199
[2325]2200 if (bkpinfo->media_device) {
2201 mr_asprintf(mountdev, "%s", bkpinfo->media_device);
2202 }
2203 if (!mountdev) {
[2324]2204 log_it("(what_number_cd_is_this) Warning - media_device unknown. Finding out...");
[2325]2205 mr_free(bkpinfo->media_device);
2206 bkpinfo->media_device = find_cdrom_device(FALSE);
2207 mr_asprintf(mountdev, "%s", bkpinfo->media_device);
[128]2208 }
2209 if (!is_this_device_mounted(MNT_CDROM)) {
[1741]2210 if (bkpinfo->backup_media_type == usb) {
2211 mount_USB_here(mountdev, MNT_CDROM);
2212 } else {
2213 mount_CDROM_here(mountdev, MNT_CDROM);
2214 }
[128]2215 }
[2324]2216 mr_free(mountdev);
[2211]2217
[2357]2218 tmp = last_line_of_file(MNT_CDROM "/archives/THIS-CD-NUMBER");
2219 cd_number = atoi(tmp);
2220 mr_free(tmp);
[128]2221 return (cd_number);
[1]2222}
2223
2224
2225
2226
2227
2228
2229
2230/**
2231 * Find out what device is mounted as root (/).
2232 * @return Root device.
2233 * @note The returned string points to static storage and will be overwritten with every call.
2234 * @bug A bit of a misnomer; it's actually finding out the root device.
2235 * The mountpoint (where it's mounted) will obviously be '/'.
2236 */
[2330]2237char *where_is_root_mounted() {
[128]2238 /*@ buffers **************** */
[2330]2239 char *output;
[1]2240
2241
2242#ifdef __FreeBSD__
[2330]2243 mr_asprintf(output, call_program_and_get_last_line_of_output("mount | grep \" on / \" | cut -d' ' -f1"));
[1]2244#else
[2330]2245 mr_asprintf(output, call_program_and_get_last_line_of_output("mount | grep \" on / \" | cut -d' ' -f1 | sed s/[0-9]// | sed s/[0-9]//"));
2246 if (strstr(output, "/dev/cciss/")) {
2247 mr_free(output);
2248 mr_asprintf(output, call_program_and_get_last_line_of_output("mount | grep \" on / \" | cut -d' ' -f1 | cut -dp -f1"));
[128]2249 }
[2330]2250 if (strstr(output, "/dev/md")) {
2251 mr_free(output);
2252 mr_asprintf(output, call_program_and_get_last_line_of_output("mount | grep \" on / \" | cut -d' ' -f1"));
[128]2253 }
[1]2254#endif
2255
[2330]2256 return (output);
[1]2257}
2258
2259
2260/**
2261 * Find out which boot loader is in use.
2262 * @param which_device Device to look for the boot loader on.
2263 * @return 'L' for LILO, 'E'for ELILO, 'G' for GRUB, 'B' or 'D' for FreeBSD boot loaders, or 'U' for Unknown.
2264 * @note Under Linux, all drives are examined, not just @p which_device.
2265 */
2266#ifdef __FreeBSD__
[2330]2267char which_boot_loader(char *which_device) {
[128]2268 int count_lilos = 0;
2269 int count_grubs = 0;
2270 int count_boot0s = 0;
2271 int count_dangerouslydedicated = 0;
[1]2272
[128]2273 log_it("looking at drive %s's MBR", which_device);
2274 if (does_string_exist_in_boot_block(which_device, "GRUB")) {
2275 count_grubs++;
2276 }
2277 if (does_string_exist_in_boot_block(which_device, "LILO")) {
2278 count_lilos++;
2279 }
2280 if (does_string_exist_in_boot_block(which_device, "Drive")) {
2281 count_boot0s++;
2282 }
[2330]2283 if (does_string_exist_in_first_N_blocks(which_device, "FreeBSD/i386", 17)) {
[128]2284 count_dangerouslydedicated++;
2285 }
2286 log_it("%d grubs and %d lilos and %d elilos and %d boot0s and %d DD\n",
2287 count_grubs, count_lilos, count_elilos, count_boot0s,
2288 count_dangerouslydedicated);
[1]2289
[128]2290 if (count_grubs && !count_lilos) {
2291 return ('G');
2292 } else if (count_lilos && !count_grubs) {
2293 return ('L');
2294 } else if (count_grubs == 1 && count_lilos == 1) {
2295 log_it("I'll bet you used to use LILO but switched to GRUB...");
2296 return ('G');
2297 } else if (count_boot0s == 1) {
2298 return ('B');
2299 } else if (count_dangerouslydedicated) {
2300 return ('D');
2301 } else {
2302 log_it("Unknown boot loader");
2303 return ('U');
2304 }
[1]2305}
2306
2307#else
2308
[2330]2309char which_boot_loader(char *which_device) {
[128]2310 /*@ buffer ***************************************************** */
[2241]2311 char *list_drives_cmd = NULL;
[2330]2312 char *tmp = NULL;
[2351]2313 char *current_drive = NULL;
[1]2314
[128]2315 /*@ pointers *************************************************** */
[2351]2316 FILE *pdrives = NULL;
[1]2317
[128]2318 /*@ int ******************************************************** */
2319 int count_lilos = 0;
2320 int count_grubs = 0;
[1]2321
[128]2322 /*@ end vars *************************************************** */
[1]2323
2324
2325#ifdef __IA64__
[128]2326 /* No choice for it */
2327 return ('E');
[1]2328#endif
2329
[2330]2330 tmp = where_is_root_mounted();
2331 mr_asprintf(list_drives_cmd, "parted2fdisk -l 2>/dev/null | grep \"/dev/.*:\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/; echo %s", tmp);
2332 mr_free(tmp);
[128]2333 log_it("list_drives_cmd = %s", list_drives_cmd);
[1]2334
[2330]2335 pdrives = popen(list_drives_cmd, "r");
2336 mr_free(list_drives_cmd);
2337
2338 if (!pdrives) {
[128]2339 log_OS_error("Unable to open list of drives");
2340 return ('\0');
[1]2341 }
[2241]2342
[2351]2343 for (mr_getline(current_drive, pdrives); !feof(pdrives); mr_getline(current_drive, pdrives)) {
2344 mr_strip_spaces(current_drive);
[128]2345 log_it("looking at drive %s's MBR", current_drive);
2346 if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
2347 count_grubs++;
[2331]2348 /* BERLIOS : removed as I don't think it's mandatory here
[2330]2349 mr_free(which_device);
2350 mr_asprintf(which_device, "%s", current_drive);
[2331]2351 */
[2351]2352 mr_free(current_drive);
[128]2353 break;
2354 }
2355 if (does_string_exist_in_boot_block(current_drive, "LILO")) {
2356 count_lilos++;
[2331]2357 /* BERLIOS : removed as I don't think it's mandatory here
[2330]2358 mr_free(which_device);
2359 mr_asprintf(which_device, "%s", current_drive);
[2331]2360 */
[2351]2361 mr_free(current_drive);
[128]2362 break;
2363 }
[2351]2364 mr_free(current_drive);
[1]2365 }
[2351]2366 mr_free(current_drive);
2367
[128]2368 if (pclose(pdrives)) {
2369 log_OS_error("Cannot pclose pdrives");
2370 }
2371 log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
2372 if (count_grubs && !count_lilos) {
2373 return ('G');
2374 } else if (count_lilos && !count_grubs) {
2375 return ('L');
2376 } else if (count_grubs == 1 && count_lilos == 1) {
2377 log_it("I'll bet you used to use LILO but switched to GRUB...");
2378 return ('G');
2379 } else {
[1451]2380 // We need to look on each partition then
[2323]2381 mr_asprintf(list_drives_cmd, "parted2fdisk -l 2>/dev/null | grep -E \"^/dev/\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/");
[1451]2382 log_it("list_drives_cmd = %s", list_drives_cmd);
2383
2384 if (!(pdrives = popen(list_drives_cmd, "r"))) {
2385 log_OS_error("Unable to open list of drives");
[2241]2386 mr_free(list_drives_cmd);
[1451]2387 return ('\0');
2388 }
[2241]2389 mr_free(list_drives_cmd);
2390
[2351]2391 for (mr_getline(current_drive, pdrives); !feof(pdrives); mr_getline(current_drive, pdrives)) {
2392 mr_strip_spaces(current_drive);
[1451]2393 log_it("looking at partition %s's BR", current_drive);
2394 if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
2395 count_grubs++;
[2331]2396 /* BERLIOS : removed as I don't think it's mandatory here
[2330]2397 mr_free(which_device);
2398 mr_asprintf(which_device, "%s", current_drive);
[2331]2399 */
[2351]2400 mr_free(current_drive);
[1451]2401 break;
2402 }
2403 if (does_string_exist_in_boot_block(current_drive, "LILO")) {
2404 count_lilos++;
[2331]2405 /* BERLIOS : removed as I don't think it's mandatory here
[2330]2406 mr_free(which_device);
2407 mr_asprintf(which_device, "%s", current_drive);
[2331]2408 */
[2351]2409 mr_free(current_drive);
[1451]2410 break;
2411 }
[2351]2412 mr_free(current_drive);
[1451]2413 }
[2351]2414 mr_free(current_drive);
2415
[1451]2416 if (pclose(pdrives)) {
2417 log_OS_error("Cannot pclose pdrives");
2418 }
2419 log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
2420 if (count_grubs && !count_lilos) {
2421 return ('G');
2422 } else if (count_lilos && !count_grubs) {
2423 return ('L');
2424 } else if (count_grubs == 1 && count_lilos == 1) {
2425 log_it("I'll bet you used to use LILO but switched to GRUB...");
2426 return ('G');
2427 } else {
2428 log_it("Unknown boot loader");
2429 return ('U');
2430 }
[128]2431 }
[1]2432}
2433#endif
2434
2435
2436
2437
2438/**
2439 * Write zeroes over the first 16K of @p device.
2440 * @param device The device to zero.
2441 * @return 0 for success, 1 for failure.
2442 */
[128]2443int zero_out_a_device(char *device)
[1]2444{
[128]2445 FILE *fout;
2446 int i;
[1]2447
[128]2448 assert_string_is_neither_NULL_nor_zerolength(device);
[1]2449
[128]2450 log_it("Zeroing drive %s", device);
2451 if (!(fout = fopen(device, "w"))) {
2452 log_OS_error("Unable to open/write to device");
2453 return (1);
2454 }
2455 for (i = 0; i < 16384; i++) {
2456 fputc('\0', fout);
2457 }
2458 paranoid_fclose(fout);
2459 log_it("Device successfully zeroed.");
2460 return (0);
[1]2461}
2462
2463/**
2464 * Return the device pointed to by @p incoming.
2465 * @param incoming The device to resolve symlinks for.
2466 * @return The path to the real device file.
2467 * @note The returned string points to static storage that will be overwritten with each call.
2468 * @bug Won't work with file v4.0; needs to be written in C.
2469 */
[128]2470char *resolve_softlinks_to_get_to_actual_device_file(char *incoming)
[1]2471{
[2330]2472 char *output;
[2266]2473 char *command = NULL;
[128]2474 char *curr_fname;
[2214]2475 char *scratch = NULL;
2476 char *tmp = NULL;
[128]2477 char *p;
2478
2479 struct stat statbuf;
2480 malloc_string(curr_fname);
2481 if (!does_file_exist(incoming)) {
[2330]2482 log_it("resolve_softlinks_to_get_to_actual_device_file --- device not found");
2483 mr_asprintf(output, "%s", incoming);
[128]2484 } else {
2485 strcpy(curr_fname, incoming);
2486 lstat(curr_fname, &statbuf);
2487 while (S_ISLNK(statbuf.st_mode)) {
2488 log_msg(1, "curr_fname = %s", curr_fname);
[2323]2489 mr_asprintf(command, "file %s", curr_fname);
2490 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
[2266]2491 mr_free(command);
[2330]2492 for (p = tmp + strlen(tmp); p != tmp && *p != '`' && *p != ' '; p--);
[128]2493 p++;
[2323]2494 mr_asprintf(scratch, "%s", p);
[128]2495 for (p = scratch; *p != '\0' && *p != '\''; p++);
2496 *p = '\0';
[2214]2497 log_msg(0, "curr_fname %s --> '%s' --> %s", curr_fname, tmp, scratch);
2498 mr_free(tmp);
2499
[128]2500 if (scratch[0] == '/') {
2501 strcpy(curr_fname, scratch); // copy whole thing because it's an absolute softlink
2502 } else { // copy over the basename cos it's a relative softlink
2503 p = curr_fname + strlen(curr_fname);
2504 while (p != curr_fname && *p != '/') {
2505 p--;
2506 }
2507 if (*p == '/') {
2508 p++;
2509 }
2510 strcpy(p, scratch);
2511 }
[2214]2512 mr_free(scratch);
[128]2513 lstat(curr_fname, &statbuf);
2514 }
[2330]2515 mr_asprintf(output, "%s", curr_fname);
[128]2516 log_it("resolved %s to %s", incoming, output);
[1]2517 }
[128]2518 paranoid_free(curr_fname);
2519 return (output);
[1]2520}
2521
2522/* @} - end of deviceGroup */
2523
2524
2525/**
2526 * Return the type of partition format (GPT or MBR)
2527 */
2528char *which_partition_format(const char *drive)
2529{
[2330]2530 char *output;
[2214]2531 char *tmp = NULL;
[2266]2532 char *command = NULL;
2533 char *fdisk = NULL;
[152]2534#ifdef __IA64__
[128]2535 struct stat buf;
[152]2536#endif
[128]2537 log_msg(0, "Looking for partition table format type");
[2323]2538 mr_asprintf(fdisk, "/sbin/parted2fdisk");
[128]2539 log_msg(1, "Using %s", fdisk);
[2323]2540 mr_asprintf(command, "%s -l %s | grep 'EFI GPT'", fdisk, drive);
[2266]2541 mr_free(fdisk);
2542
[2323]2543 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
[2266]2544 mr_free(command);
2545
[128]2546 if (strstr(tmp, "GPT") == NULL) {
[2330]2547 mr_asprintf(output, "MBR");
[128]2548 } else {
[2330]2549 mr_asprintf(output, "GPT");
[128]2550 }
[2214]2551 mr_free(tmp);
2552
[128]2553 log_msg(0, "Found %s partition table format type", output);
2554 return (output);
[1]2555}
2556
2557/* @} - end of deviceGroup */
Note: See TracBrowser for help on using the repository browser.