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

Last change on this file since 2421 was 2421, checked in by Bruno Cornec, 15 years ago
  • Remove some compiler warnings on main
  • Adds function mr_chomp and mr_strip_char and use them in mr_date and call_program_and_get_last_line_of_output
  • Fix call_program_and_get_last_line_of_output to return the right result using a temp file instead of popen
  • Fix an issue in mr_getline_int in case of eof (returns now an empty string)
  • Sequencing of Init of bkpinfo reviewed
  • Property svn:keywords set to Id
File size: 70.5 KB
RevLine 
[1]1/* libmondo-devices.c Subroutines for handling devices
[85]2 $Id: libmondo-devices.c 2421 2009-09-24 13:50:38Z 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 2421 2009-09-24 13:50:38Z 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;
[2382]56bool g_restoring_live_from_netfs = 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;
[2382]166 case netfs:
167 mr_asprintf(output, "netfs");
[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);
[2383]527 tmp = 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);
[2383]807 tmp = 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
[2383]835 tmp = 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);
[2383]839 tmp = call_program_and_get_last_line_of_output("cdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1");
[128]840 if (tmp[0]) {
841 devno = atoi(tmp) - 1;
842 }
[2266]843 mr_free(tmp);
844
[128]845 if (devno >= 0) {
[2325]846 mr_asprintf(output, "/dev/scd%d", devno);
[128]847 strcpy(g_dvd_drive_is_here, output);
848 log_msg(2, "I think DVD is at %s", output);
849 } else {
850 log_msg(2, "I cannot find DVD");
851 }
[1]852
[2325]853 return (output);
[1]854}
855
856
857
858
859
860#include <sys/ioctl.h>
861
862/**
863 * Find the size of the specified @p drive, in megabytes. Uses @c ioctl calls
864 * and @c dmesg.
865 * @param drive The device to find the size of.
866 * @return size in megabytes.
867 */
[128]868long get_phys_size_of_drive(char *drive)
[1]869{
[128]870 int fd;
[1]871#if linux
[128]872 unsigned long long s = 0;
873 int fileid, cylinders = 0, cylindersleft = 0;
874 int cylindersize = 0;
[1]875 int gotgeo = 0;
876
[128]877
878 struct hd_geometry hdgeo;
[1]879#elif __FreeBSD__
[128]880 off_t s;
[1]881#endif
882
[128]883 long outvalA = -1;
884 long outvalB = -1;
885 long outvalC = -1;
[1]886
[128]887 if ((fd = open(drive, O_RDONLY)) != -1) {
888 if (ioctl(fd,
[1]889#if linux
[128]890#ifdef BLKGETSIZE64
891 BLKGETSIZE64,
892#else
893 BLKGETSIZE,
894#endif
[1]895#elif __FreeBSD__
[128]896 DIOCGMEDIASIZE,
[1]897#endif
[128]898 &s) != -1) {
899 close(fd);
900 // s>>11 works for older disks but not for newer ones
901 outvalB =
[1]902#if linux
[128]903#ifdef BLKGETSIZE64
904 s >> 20
[1]905#else
[128]906 s >> 11
[1]907#endif
[128]908#else
909 s >> 20
910#endif
911 ;
912 }
[1]913 }
914
[128]915 if (outvalB <= 0) {
916 log_msg(1, "Error getting size of %s: %s", drive, strerror(errno));
[1]917#if linux
[161]918 fileid = open(drive, O_RDONLY);
[2205]919 if (fileid != -1) {
[161]920 if (ioctl(fileid, HDIO_GETGEO, &hdgeo) != -1) {
921 if (hdgeo.cylinders && hdgeo.heads && hdgeo.sectors) {
922 cylindersleft = cylinders = hdgeo.cylinders;
923 cylindersize = hdgeo.heads * hdgeo.sectors / 2;
924 outvalA = cylindersize * cylinders / 1024;
925 log_msg(2, "Got Harddisk geometry, C:%d, H:%d, S:%d",
926 hdgeo.cylinders, hdgeo.heads, hdgeo.sectors);
927 gotgeo = 1;
928 } else {
929 log_msg(1, "Harddisk geometry wrong");
930 }
[1]931 } else {
[161]932 log_msg(1,
933 "Error in ioctl() getting new hard disk geometry (%s), resizing in unsafe mode",
934 strerror(errno));
[1]935 }
[161]936 close(fileid);
[1]937 } else {
[161]938 log_msg(1, "Failed to open %s for reading: %s", drive,
[128]939 strerror(errno));
[1]940 }
[161]941 if (!gotgeo) {
942 log_msg(1, "Failed to get harddisk geometry, using old mode");
943 }
[1]944#endif
[161]945 }
[1]946// OLDER DISKS will give ridiculously low value for outvalB (so outvalA is returned) :)
947// NEWER DISKS will give sane value for outvalB (close to outvalA, in other words) :)
948
[128]949 outvalC = (outvalA > outvalB) ? outvalA : outvalB;
950
[1]951// log_msg (5, "drive = %s, error = %s", drive, strerror (errno));
952// fatal_error ("GPSOD: Unable to get size of drive");
[128]953 log_msg(1, "%s --> %ld or %ld --> %ld", drive, outvalA, outvalB,
954 outvalC);
955
956 return (outvalC);
[1]957}
[128]958
[1]959/**
960 * Determine whether @p format is supported by the kernel. Uses /proc/filesystems
961 * under Linux and @c lsvfs under FreeBSD.
962 * @param format The format to test.
963 * @return TRUE if the format is supported, FALSE if not.
964 */
[128]965bool is_this_a_valid_disk_format(char *format)
[1]966{
[2211]967 char *good_formats = NULL;
[2266]968 char *command = NULL;
969 char *format_sz = NULL;
[1]970
[128]971 FILE *pin;
972 int retval;
[1]973
[128]974 assert_string_is_neither_NULL_nor_zerolength(format);
[1]975
[2323]976 mr_asprintf(format_sz, "%s ", format);
[1]977
978#ifdef __FreeBSD__
[2323]979 mr_asprintf(command, "lsvfs | tr -s '\t' ' ' | grep -v Filesys | grep -v -- -- | cut -d' ' -f1 | tr -s '\n' ' '");
[1]980#else
[2323]981 mr_asprintf(command, "grep -v nodev /proc/filesystems | tr -s '\t' ' ' | cut -d' ' -f2 | tr -s '\n' ' '");
[1]982#endif
983
[128]984 pin = popen(command, "r");
[2266]985 mr_free(command);
986
[128]987 if (!pin) {
988 log_OS_error("Unable to read good formats");
989 retval = 0;
990 } else {
[2331]991 mr_getline(good_formats, pin);
[128]992 if (pclose(pin)) {
993 log_OS_error("Cannot pclose good formats");
994 }
[2316]995 mr_strip_spaces(good_formats);
996 mr_strcat(good_formats, " swap lvm raid ntfs-3g ntfs 7 "); // " ntfs 7 " -- um, cheating much? :)
[128]997 if (strstr(good_formats, format_sz)) {
998 retval = 1;
999 } else {
1000 retval = 0;
1001 }
[2316]1002 mr_free(good_formats);
[128]1003 }
[2266]1004 mr_free(format_sz);
1005
[128]1006 return (retval);
[1]1007}
1008
1009
1010/** @def SWAPLIST_COMMAND The command to list the swap files/partitions in use. */
1011
1012/**
1013 * Determine whether @p device_raw is currently mounted.
1014 * @param device_raw The device to check.
1015 * @return TRUE if it's mounted, FALSE if not.
1016 */
[128]1017bool is_this_device_mounted(char *device_raw)
[1]1018{
1019
[128]1020 /*@ pointers **************************************************** */
1021 FILE *fin;
[1]1022
[128]1023 /*@ buffers ***************************************************** */
[2351]1024 char *incoming = NULL;
[2214]1025 char *device_with_tab = NULL;
1026 char *device_with_space = NULL;
1027 char *tmp = NULL;
1028 bool retval = FALSE;
[128]1029
[1]1030#ifdef __FreeBSD__
[128]1031#define SWAPLIST_COMMAND "swapinfo"
[1]1032#else
[128]1033#define SWAPLIST_COMMAND "cat /proc/swaps"
[1]1034#endif
1035
[128]1036 /*@ end vars **************************************************** */
[1]1037
[2325]1038 if (device_raw == NULL) {
1039 return(FALSE);
1040 }
1041
[128]1042 if (device_raw[0] != '/' && !strstr(device_raw, ":/")) {
1043 log_msg(1, "%s needs to have a '/' prefixed - I'll do it",
1044 device_raw);
[2323]1045 mr_asprintf(tmp, "/%s", device_raw);
[128]1046 } else {
[2323]1047 mr_asprintf(tmp, "%s", device_raw);
[128]1048 }
1049 log_msg(1, "Is %s mounted?", tmp);
1050 if (!strcmp(tmp, "/proc") || !strcmp(tmp, "proc")) {
1051 log_msg(1,
1052 "I don't know how the heck /proc made it into the mountlist. I'll ignore it.");
[2214]1053 mr_free(tmp);
1054 return(FALSE);
[128]1055 }
[2323]1056 mr_asprintf(device_with_tab, "%s\t", tmp);
1057 mr_asprintf(device_with_space, "%s ", tmp);
[2214]1058 mr_free(tmp);
[1]1059
[128]1060 if (!(fin = popen("mount", "r"))) {
1061 log_OS_error("Cannot popen 'mount'");
[2214]1062 return(FALSE);
[1]1063 }
[2351]1064
1065 for (mr_getline(incoming, fin); !feof(fin); mr_getline(incoming, fin)) {
1066 if (strstr(incoming, device_with_space) || strstr(incoming, device_with_tab)) {
[128]1067 paranoid_pclose(fin);
[2351]1068 mr_free(incoming);
[2214]1069 return(TRUE);
[128]1070 }
[2351]1071 mr_free(incoming);
[128]1072 }
[2351]1073 mr_free(incoming);
[2214]1074 mr_free(device_with_tab);
1075 mr_free(device_with_space);
[128]1076 paranoid_pclose(fin);
[2323]1077 mr_asprintf(tmp, "%s | grep -E \"^%s\" > /dev/null 2> /dev/null", SWAPLIST_COMMAND, device_with_space);
[128]1078 log_msg(4, "tmp (command) = '%s'", tmp);
1079 if (!system(tmp)) {
[2214]1080 retval = TRUE;
[128]1081 }
[2214]1082 mr_free(tmp);
1083 return(retval);
[1]1084}
1085
1086#ifdef __FreeBSD__
1087// CODE IS FREEBSD-SPECIFIC
1088/**
1089 * Create a loopback device for specified @p fname.
1090 * @param fname The file to associate with a device.
1091 * @return /dev entry for the device, or NULL if it couldn't be allocated.
1092 */
[128]1093char *make_vn(char *fname)
[1]1094{
[128]1095 char *device = (char *) malloc(MAX_STR_LEN);
1096 char *mddevice = (char *) malloc(32);
1097 char command[MAX_STR_LEN];
[2383]1098 char *tmp = NULL;
[128]1099 int vndev = 2;
[2383]1100 int i = 2;
1101
1102 tmp = call_program_and_get_last_line_of_output("/sbin/sysctl -n kern.osreldate"));
1103 i = atoi(tmp);
1104 mr_free(tmp);
1105
1106 if (i < 500000) {
[128]1107 do {
1108 sprintf(mddevice, "vn%ic", vndev++);
1109 sprintf(command, "vnconfig %s %s", mddevice, fname);
1110 if (vndev > 10) {
1111 return NULL;
1112 }
1113 }
1114 while (system(command));
1115 } else {
1116 sprintf(command, "mdconfig -a -t vnode -f %s", fname);
1117 mddevice = call_program_and_get_last_line_of_output(command);
1118 if (!strstr(mddevice, "md")) {
1119 return NULL;
1120 }
1121 }
1122 sprintf(device, "/dev/%s", mddevice);
1123 return device;
[1]1124}
1125
1126
1127
1128// CODE IS FREEBSD-SPECIFIC
1129/**
1130 * Deallocate specified @p dname.
1131 * This should be called when you are done with the device created by make_vn(),
1132 * so the system does not run out of @c vn devices.
1133 * @param dname The device to deallocate.
1134 * @return 0 for success, nonzero for failure.
1135 */
[128]1136int kick_vn(char *dname)
[1]1137{
[2266]1138 char *command = NULL;
[2383]1139 char *tmp = NULL;
[2266]1140 int res = 0;
[2383]1141 int i = 0;
[1]1142
[128]1143 if (strncmp(dname, "/dev/", 5) == 0) {
1144 dname += 5;
1145 }
[1]1146
[2383]1147 tmp = call_program_and_get_last_line_of_output("/sbin/sysctl -n kern.osreldate"));
1148 i = atoi(tmp);
1149 mr_free(tmp);
1150
1151 if (i < 500000) {
[2323]1152 mr_asprintf(command, "vnconfig -d %s", dname);
[128]1153 } else {
[2323]1154 mr_asprintf(command, "mdconfig -d -u %s", dname);
[128]1155 }
[2266]1156 res = system(command);
1157 mr_free(command);
1158 return(res);
[1]1159}
1160#endif
1161
1162
1163/**
1164 * Mount the CD-ROM at @p mountpoint.
1165 * @param device The device (or file if g_ISO_restore_mode) to mount.
1166 * @param mountpoint The place to mount it.
1167 * @return 0 for success, nonzero for failure.
1168 */
[1741]1169int mount_USB_here(char *device, char *mountpoint)
1170{
1171 /*@ buffer ****************************************************** */
[2266]1172 char *command = NULL;
[1741]1173 int retval;
1174
1175 assert_string_is_neither_NULL_nor_zerolength(device);
1176 assert_string_is_neither_NULL_nor_zerolength(mountpoint);
1177
1178 make_hole_for_dir(mountpoint);
1179 if (isdigit(device[0])) {
1180 return(1);
1181 }
[2325]1182 log_msg(4, "(mount_USB_here --- device=%s, mountpoint=%s", device, mountpoint);
[1741]1183
1184#ifdef __FreeBSD__
[2323]1185 mr_asprintf(command, "mount_vfat %s %s 2>> %s", device, mountpoint, MONDO_LOGFILE);
[1741]1186
1187#else
[2323]1188 mr_asprintf(command, "mount %s -t vfat %s 2>> %s", device, mountpoint, MONDO_LOGFILE);
[1741]1189#endif
1190
1191 log_msg(4, command);
1192 retval = system(command);
1193 log_msg(1, "system(%s) returned %d", command, retval);
[2266]1194 mr_free(command);
[1741]1195
1196 return (retval);
1197}
1198
1199/**
1200 * Mount the CD-ROM at @p mountpoint.
1201 * @param device The device (or file if g_ISO_restore_mode) to mount.
1202 * @param mountpoint The place to mount it.
1203 * @return 0 for success, nonzero for failure.
1204 */
[2325]1205int mount_CDROM_here(char *device, const char *mountpoint)
[1]1206{
[128]1207 /*@ buffer ****************************************************** */
[2211]1208 char *command = NULL;
[128]1209 int retval;
[2257]1210#ifdef __FreeBSD__
1211 char *dev = NULL;
1212#else
1213 char *options = NULL;
1214#endif
[1]1215
[128]1216 assert_string_is_neither_NULL_nor_zerolength(mountpoint);
[1]1217
[128]1218 make_hole_for_dir(mountpoint);
[2211]1219
[2325]1220 if ((device == NULL) || (isdigit(device[0]))) {
1221 mr_free(device);
1222 device = find_cdrom_device(FALSE);
[2257]1223 }
1224#ifndef __FreeBSD__
[2323]1225 mr_asprintf(options, "ro");
[2211]1226#endif
1227
[128]1228 if (g_ISO_restore_mode) {
[1]1229
1230#ifdef __FreeBSD__
[2323]1231 mr_asprintf(dev, "%s", make_vn(device));
[128]1232 if (!dev) {
[2323]1233 mr_asprintf(command, "Unable to mount ISO (make_vn(%s) failed)", device);
[128]1234 fatal_error(command);
1235 }
[2325]1236 mr_free(device);
1237 device = dev;
[1]1238#else
[2211]1239 mr_strcat(options, ",loop");
[1]1240#endif
1241
[128]1242 }
[2325]1243 log_msg(4, "(mount_CDROM_here --- device=%s, mountpoint=%s", device, mountpoint);
[128]1244 /*@ end vars *************************************************** */
[1]1245
1246#ifdef __FreeBSD__
[2323]1247 mr_asprintf(command, "mount_cd9660 -r %s %s 2>> %s", device, mountpoint, MONDO_LOGFILE);
[1]1248
1249#else
[2323]1250 mr_asprintf(command, "mount %s -o %s -t iso9660 %s 2>> %s", device, options, mountpoint, MONDO_LOGFILE);
[2211]1251 paranoid_free(options);
[1]1252#endif
1253
[128]1254 log_msg(4, command);
1255 if (strncmp(device, "/dev/", 5) == 0) {
1256 retract_CD_tray_and_defeat_autorun();
1257 }
1258 retval = system(command);
1259 log_msg(1, "system(%s) returned %d", command, retval);
[2325]1260 mr_free(command);
[1]1261
[128]1262 return (retval);
[1]1263}
1264
1265
1266
1267
1268
1269
1270/**
1271 * Ask the user for CD number @p cd_number_i_want.
1272 * Sets g_current_media_number once the correct CD is inserted.
1273 * @param bkpinfo The backup information structure. Fields used:
1274 * - @c bkpinfo->backup_media_type
[20]1275 * - @c bkpinfo->prefix
[1]1276 * - @c bkpinfo->isodir
1277 * - @c bkpinfo->media_device
1278 * - @c bkpinfo->please_dont_eject_when_restoring
1279 * @param cd_number_i_want The CD number to ask for.
1280 */
1281void
[1645]1282insist_on_this_cd_number(int cd_number_i_want)
[1]1283{
1284
[128]1285 /*@ int ************************************************************* */
1286 int res = 0;
[1]1287
1288
[128]1289 /*@ buffers ********************************************************* */
[2266]1290 char *tmp = NULL;
[2242]1291 char *mds = NULL;
[2266]1292 char *request = NULL;
[1]1293
[128]1294 assert(bkpinfo != NULL);
1295 assert(cd_number_i_want > 0);
[1]1296
1297// log_msg(3, "Insisting on CD number %d", cd_number_i_want);
1298
[128]1299 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1300 log_msg(3,
1301 "No need to insist_on_this_cd_number when the backup type isn't CD-R(W) or NFS or ISO");
1302 return;
1303 }
[2323]1304 mr_asprintf(tmp, "mkdir -p " MNT_CDROM);
[128]1305 run_program_and_log_output(tmp, 5);
[2266]1306 mr_free(tmp);
1307
[2382]1308 if (g_ISO_restore_mode || bkpinfo->backup_media_type == iso || bkpinfo->backup_media_type == netfs) {
[2325]1309 // BERLIOS --- I'm tempted to do something about this...
1310 // Why unmount and remount again and again?
[128]1311 log_msg(3, "Remounting CD");
1312 g_ISO_restore_mode = TRUE;
1313 if (is_this_device_mounted(MNT_CDROM)) {
1314 run_program_and_log_output("umount " MNT_CDROM, 5);
1315 }
[2323]1316 mr_asprintf(tmp, "mkdir -p %s/isodir &> /dev/null", bkpinfo->tmpdir);
[2202]1317 (void)system(tmp);
[2266]1318 mr_free(tmp);
1319
[2382]1320 if (((bkpinfo->isodir == NULL) && (bkpinfo->netfs_remote_dir == NULL)) || (bkpinfo->prefix == NULL)) {
[2325]1321 fatal_error("Unable to prepare ISO file name. Please report to dev team");
1322 }
[2382]1323 if (bkpinfo->netfs_remote_dir) {
1324 // NETFS
1325 mr_asprintf(tmp, "%s/%s/%s-%d.iso", bkpinfo->isodir, bkpinfo->netfs_remote_dir, bkpinfo->prefix, cd_number_i_want);
[2325]1326 } else {
1327 // ISO
1328 mr_asprintf(tmp, "%s/%s-%d.iso", bkpinfo->isodir, bkpinfo->prefix, cd_number_i_want);
1329 }
[128]1330 if (!does_file_exist(tmp)) {
[2266]1331 mr_free(tmp);
[2382]1332 if (bkpinfo->netfs_remote_dir) {
1333 // NETFS
1334 mr_asprintf(tmp, "%s/isodir/%s/%s-%d.iso", bkpinfo->tmpdir, bkpinfo->netfs_remote_dir, bkpinfo->prefix, cd_number_i_want);
[2325]1335 } else {
1336 // ISO
1337 mr_asprintf(tmp, "%s/isodir/%s-%d.iso", bkpinfo->tmpdir, bkpinfo->prefix, cd_number_i_want);
1338 }
[128]1339 if (does_file_exist(tmp)) {
[2321]1340 log_msg(1, "FIXME - hacking bkpinfo->isodir from '%s' to %s/isodir", bkpinfo->isodir, bkpinfo->tmpdir);
[2323]1341 mr_free(bkpinfo->isodir);
[2324]1342 mr_asprintf(bkpinfo->isodir, "%s/isodir", bkpinfo->tmpdir);
[128]1343 }
1344 }
1345 log_msg(3, "Mounting %s at %s", tmp, MNT_CDROM);
1346 if (mount_CDROM_here(tmp, MNT_CDROM)) {
[2266]1347 mr_free(tmp);
[128]1348 fatal_error("Mommy!");
1349 }
[2266]1350 mr_free(tmp);
[1]1351 }
[1645]1352 if ((res = what_number_cd_is_this()) != cd_number_i_want) {
[2325]1353 log_msg(3, "Currently, we hold %d but we want %d", res, cd_number_i_want);
[2242]1354 mds = media_descriptor_string(bkpinfo->backup_media_type);
[2324]1355 log_msg(3, "Insisting on %s #%d", mds, cd_number_i_want);
[2323]1356 mr_asprintf(request, "Please insert %s #%d and press Enter.", mds, cd_number_i_want);
[2242]1357 mr_free(mds);
[2266]1358
[1645]1359 while (what_number_cd_is_this() != cd_number_i_want) {
[2376]1360 sync();
[128]1361 if (is_this_device_mounted(MNT_CDROM)) {
1362 res =
1363 run_program_and_log_output("umount " MNT_CDROM, FALSE);
1364 } else {
1365 res = 0;
1366 }
1367 if (res) {
[541]1368 log_to_screen("WARNING - failed to unmount CD-ROM drive");
[128]1369 }
1370 if (!bkpinfo->please_dont_eject) {
1371 res = eject_device(bkpinfo->media_device);
1372 } else {
1373 res = 0;
1374 }
1375 if (res) {
[541]1376 log_to_screen("WARNING - failed to eject CD-ROM disk");
[128]1377 }
1378 popup_and_OK(request);
1379 if (!bkpinfo->please_dont_eject) {
1380 inject_device(bkpinfo->media_device);
1381 }
[2376]1382 sync();
[128]1383 }
[2266]1384 mr_free(request);
1385
[128]1386 log_msg(1, "Thankyou. Proceeding...");
1387 g_current_media_number = cd_number_i_want;
1388 }
[1]1389}
1390
1391/* @} - end of deviceGroup */
1392
1393
1394/**
1395 * Ask user for details of backup/restore information.
1396 * Called when @c mondoarchive doesn't get any parameters.
1397 * @param bkpinfo The backup information structure to fill out with the user's data.
1398 * @param archiving_to_media TRUE if archiving, FALSE if restoring.
1399 * @return 0, always.
1400 * @bug No point of `int' return value.
1401 * @ingroup archiveGroup
1402 */
[1645]1403int interactively_obtain_media_parameters_from_user(bool archiving_to_media)
[1]1404// archiving_to_media is TRUE if I'm being called by mondoarchive
1405// archiving_to_media is FALSE if I'm being called by mondorestore
1406{
[2214]1407 char *tmp = NULL;
[2316]1408 char *p = NULL;
[2242]1409 char *mds = NULL;
[2266]1410 char *sz_size = NULL;
1411 char *command = NULL;
1412 char *comment = NULL;
[128]1413 int i;
1414 FILE *fin;
[1]1415
[128]1416 assert(bkpinfo != NULL);
1417 bkpinfo->nonbootable_backup = FALSE;
[1]1418
[2382]1419 // Tape, CD, NETFS, ...?
[128]1420 srandom(getpid());
1421 bkpinfo->backup_media_type =
1422 (g_restoring_live_from_cd) ? cdr :
1423 which_backup_media_type(bkpinfo->restore_data);
1424 if (bkpinfo->backup_media_type == none) {
[541]1425 log_to_screen("User has chosen not to backup the PC");
[128]1426 finish(1);
1427 }
[1934]1428 /* Why asking to remove the media with tape ?
[128]1429 if (bkpinfo->backup_media_type == tape && bkpinfo->restore_data) {
[1885]1430 popup_and_OK("Please remove media from drive(s)");
[128]1431 }
[1934]1432 */
[2330]1433 tmp = bkptype_to_string(bkpinfo->backup_media_type);
1434 log_msg(3, "media type = %s", tmp);
1435 mr_free(tmp);
1436
[128]1437 bkpinfo->cdrw_speed = (bkpinfo->backup_media_type == cdstream) ? 2 : 4;
[2330]1438 bkpinfo->compression_level = (bkpinfo->backup_media_type == cdstream) ? 1 : 5;
1439 bkpinfo->use_lzo = (bkpinfo->backup_media_type == cdstream) ? TRUE : FALSE;
[128]1440 mvaddstr_and_log_it(2, 0, " ");
[1]1441
[1934]1442 // Find device's /dev (or SCSI) entry
[128]1443 switch (bkpinfo->backup_media_type) {
1444 case cdr:
1445 case cdrw:
1446 case dvd:
[1687]1447 case usb:
[1745]1448 /* Never try to eject a USB device */
1449 if (bkpinfo->backup_media_type == usb) {
[1746]1450 bkpinfo->please_dont_eject = TRUE;
[1745]1451 }
[128]1452 if (archiving_to_media) {
[1687]1453 if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) {
[2405]1454 if (ask_me_yes_or_no("Is your computer a laptop, or does the CD writer incorporate BurnProof technology?")) {
[685]1455 bkpinfo->manual_cd_tray = TRUE;
1456 }
[128]1457 }
[2405]1458 if ((bkpinfo->compression_level = which_compression_level()) == -1) {
[541]1459 log_to_screen("User has chosen not to backup the PC");
[128]1460 finish(1);
1461 }
[2242]1462 mds = media_descriptor_string(bkpinfo->backup_media_type);
[2323]1463 mr_asprintf(comment, "What speed is your %s (re)writer?", mds);
[2325]1464 mr_free(bkpinfo->media_device);
[128]1465 if (bkpinfo->backup_media_type == dvd) {
[2325]1466 bkpinfo->media_device = find_dvd_device();
[2323]1467 mr_asprintf(tmp, "1");
1468 mr_asprintf(sz_size, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4482 MB
[128]1469 log_msg(1, "Setting to DVD defaults");
1470 } else {
[2325]1471 mr_asprintf(bkpinfo->media_device, "%s", VANILLA_SCSI_CDROM);
[2323]1472 mr_asprintf(tmp, "4");
1473 mr_asprintf(sz_size, "%d", 650);
[128]1474 log_msg(1, "Setting to CD defaults");
1475 }
[1687]1476 if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) {
[2316]1477 p = popup_and_get_string("Speed", comment, tmp);
1478 mr_free(tmp);
1479
1480 if (p == NULL) {
[541]1481 log_to_screen("User has chosen not to backup the PC");
[2266]1482 mr_free(comment);
[128]1483 finish(1);
1484 }
[2316]1485 /* tmp now has the new value given by the user */
1486 tmp = p;
[128]1487 }
[2266]1488 mr_free(comment);
1489
[2405]1490 bkpinfo->cdrw_speed = atoi(tmp); // if DVD then this shouldn't ever be used anyway :)
1491 mr_free(tmp);
[2214]1492
[2323]1493 mr_asprintf(comment, "How much data (in Megabytes) will each %s store?", mds);
[2242]1494 mr_free(mds);
[2316]1495 p = popup_and_get_string("Size", comment, sz_size);
1496 mr_free(sz_size);
1497 mr_free(comment);
1498
1499 if (p == NULL) {
[541]1500 log_to_screen("User has chosen not to backup the PC");
[128]1501 finish(1);
1502 }
[2316]1503 sz_size = p;
[2266]1504
[128]1505 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1506 bkpinfo->media_size[i] = atoi(sz_size);
1507 }
[2266]1508
[128]1509 if (bkpinfo->media_size[0] <= 0) {
[541]1510 log_to_screen("User has chosen not to backup the PC");
[128]1511 finish(1);
1512 }
1513 }
[1737]1514 /* No break because we continue even for usb */
[128]1515 case cdstream:
[2242]1516 mds = media_descriptor_string(bkpinfo->backup_media_type);
1517
[2325]1518 mr_free(bkpinfo->media_device);
[1737]1519 if ((bkpinfo->disaster_recovery) && (bkpinfo->backup_media_type != usb)) {
[2325]1520 mr_asprintf(bkpinfo->media_device, "/dev/cdrom");
1521 log_msg(2, "CD-ROM device assumed to be at %s", bkpinfo->media_device);
1522 } else if ((bkpinfo->restore_data && (bkpinfo->backup_media_type != usb)) || bkpinfo->backup_media_type == dvd) {
1523 if ((bkpinfo->backup_media_type == dvd) || ((bkpinfo->media_device = find_cdrom_device(FALSE)) != NULL)) {
[2323]1524 mr_asprintf(comment, "Please specify your %s drive's /dev entry", mds);
[2316]1525 p = popup_and_get_string("Device?", comment, bkpinfo->media_device);
1526 mr_free(comment);
1527
1528 if (p == NULL) {
[541]1529 log_to_screen("User has chosen not to backup the PC");
[128]1530 finish(1);
1531 }
[2325]1532 mr_free(bkpinfo->media_device);
1533 bkpinfo->media_device = p;
[128]1534 }
[2325]1535 if (bkpinfo->media_device != NULL) {
1536 log_msg(2, "%s device found at %s", mds, bkpinfo->media_device);
1537 } else {
1538 log_msg(2, "%s device not found (bkpinfo->media_device is NULL)", mds);
1539 }
[128]1540 } else {
[2325]1541 if (((bkpinfo->media_device = find_cdrw_device()) != NULL) && (bkpinfo->backup_media_type != usb)) {
1542 mr_free(bkpinfo->media_device);
[128]1543 }
[2325]1544 if (bkpinfo->media_device != NULL) {
[1738]1545 if (bkpinfo->backup_media_type == usb) {
[2323]1546 mr_asprintf(tmp, "I think your %s media corresponds to %s. Is this correct?", mds, bkpinfo->media_device);
[1738]1547 } else {
[2323]1548 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]1549 }
[128]1550 if (!ask_me_yes_or_no(tmp)) {
[2325]1551 mr_free(bkpinfo->media_device);
[128]1552 }
[2214]1553 mr_free(tmp);
[128]1554 }
[2325]1555 if (!bkpinfo->media_device) {
[1737]1556 if (bkpinfo->backup_media_type == usb) {
[2325]1557 p = popup_and_get_string("/dev entry?", "What is the /dev entry of your USB Disk/Key, please?", NULL);
[1737]1558 } else {
1559 if (g_kernel_version < 2.6) {
[2325]1560 p = popup_and_get_string("Device node?", "What is the SCSI node of your CD (re)writer, please?", NULL);
[1709]1561 } else {
[2325]1562 p = popup_and_get_string("/dev entry?", "What is the /dev entry of your CD (re)writer, please?", NULL);
[1709]1563 }
[128]1564 }
[2316]1565 if (p == NULL) {
[541]1566 log_to_screen("User has chosen not to backup the PC");
[128]1567 finish(1);
1568 }
[2325]1569 bkpinfo->media_device = p;
[128]1570 }
1571 }
[2242]1572 mr_free(mds);
1573
[128]1574 if (bkpinfo->backup_media_type == cdstream) {
1575 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1576 bkpinfo->media_size[i] = 650;
1577 }
1578 }
1579 break;
1580 case udev:
1581 if (!ask_me_yes_or_no
[541]1582 ("This option is for advanced users only. Are you sure?")) {
1583 log_to_screen("User has chosen not to backup the PC");
[128]1584 finish(1);
1585 }
1586 case tape:
[1]1587
[2325]1588 mr_free(bkpinfo->media_device);
[2334]1589 if ((!bkpinfo->restore_mode) && ((bkpinfo->media_device = mr_find_tape_device()) == NULL)) {
[541]1590 log_msg(3, "Ok, using vanilla scsi tape.");
[2325]1591 mr_asprintf(bkpinfo->media_device, "%s", VANILLA_SCSI_TAPE);
[128]1592 if ((fin = fopen(bkpinfo->media_device, "r"))) {
1593 paranoid_fclose(fin);
1594 } else {
[2325]1595 mr_free(bkpinfo->media_device);
1596 mr_asprintf(bkpinfo->media_device, "/dev/osst0");
[128]1597 }
1598 }
[2325]1599 if (bkpinfo->media_device != NULL) {
[128]1600 if ((fin = fopen(bkpinfo->media_device, "r"))) {
1601 paranoid_fclose(fin);
1602 } else {
1603 if (does_file_exist("/tmp/mondo-restore.cfg")) {
[2325]1604 mr_free(bkpinfo->media_device);
1605 bkpinfo->media_device = read_cfg_var("/tmp/mondo-restore.cfg", "media-dev");
[128]1606 }
1607 }
1608 }
[2325]1609 if (bkpinfo->media_device != NULL) {
[2323]1610 mr_asprintf(tmp, "I think I've found your tape streamer at %s; am I right on the money?", bkpinfo->media_device);
[128]1611 if (!ask_me_yes_or_no(tmp)) {
[2325]1612 mr_free(bkpinfo->media_device);
[128]1613 }
[2214]1614 mr_free(tmp);
[128]1615 }
[2325]1616 if (bkpinfo->media_device == NULL) {
[2316]1617 p = popup_and_get_string("Device name?", "What is the /dev entry of your tape streamer?", bkpinfo->media_device);
1618 if (p == NULL) {
[541]1619 log_to_screen("User has chosen not to backup the PC");
[128]1620 finish(1);
1621 }
[2325]1622 bkpinfo->media_device = p;
[128]1623 }
[2323]1624 mr_asprintf(tmp, "ls -l %s", bkpinfo->media_device);
[128]1625 if (run_program_and_log_output(tmp, FALSE)) {
[541]1626 log_to_screen("User has not specified a valid /dev entry");
[128]1627 finish(1);
1628 }
[2214]1629 mr_free(tmp);
1630
[2334]1631 bkpinfo->use_obdr = ask_me_yes_or_no("Do you want to activate OBDR support for your tapes ?");
[2266]1632 bkpinfo->media_size[0] = 0;
[128]1633 log_msg(4, "media_size[0] = %ld", bkpinfo->media_size[0]);
1634 if (bkpinfo->media_size[0] <= 0) {
1635 bkpinfo->media_size[0] = 0;
1636 }
1637 for (i = 1; i <= MAX_NOOF_MEDIA; i++) {
1638 bkpinfo->media_size[i] = bkpinfo->media_size[0];
1639 }
1640 if (archiving_to_media) {
1641 if ((bkpinfo->compression_level =
1642 which_compression_level()) == -1) {
[541]1643 log_to_screen("User has chosen not to backup the PC");
[128]1644 finish(1);
1645 }
1646 }
1647 break;
[1]1648
1649
1650
[2382]1651 case netfs:
1652 /* Never try to eject a NETFS device */
[1848]1653 bkpinfo->please_dont_eject = TRUE;
1654
[2382]1655 /* Initiate bkpinfo netfs_mount path from running environment if not already done */
1656 if (bkpinfo->netfs_mount == NULL) {
[2383]1657 bkpinfo->netfs_mount = call_program_and_get_last_line_of_output("mount | grep \":\" | cut -d' ' -f1 | head -n1");
[128]1658 }
[1]1659#ifdef __FreeBSD__
[128]1660 if (TRUE)
[1]1661#else
[128]1662 if (!bkpinfo->disaster_recovery)
[1]1663#endif
[128]1664 {
[2382]1665 p = popup_and_get_string("Network shared dir.", "Please enter path and directory where archives are stored remotely. (Mondo has taken a guess at the correct value. If it is incorrect, delete it and type the correct one.)", bkpinfo->netfs_mount);
[2316]1666 if (p == NULL) {
[541]1667 log_to_screen("User has chosen not to backup the PC");
[128]1668 finish(1);
1669 }
[2382]1670 mr_free(bkpinfo->netfs_mount);
1671 bkpinfo->netfs_mount = p;
[128]1672 if (!bkpinfo->restore_data) {
1673 if ((bkpinfo->compression_level =
1674 which_compression_level()) == -1) {
[541]1675 log_to_screen("User has chosen not to backup the PC");
[128]1676 finish(1);
1677 }
1678 }
1679 // check whether already mounted - we better remove
1680 // surrounding spaces and trailing '/' for this
[2382]1681 mr_strip_spaces(bkpinfo->netfs_mount);
1682 if (bkpinfo->netfs_mount[strlen(bkpinfo->netfs_mount) - 1] == '/')
1683 bkpinfo->netfs_mount[strlen(bkpinfo->netfs_mount) - 1] = '\0';
1684 mr_asprintf(command, "mount | grep \"%s \" | cut -d' ' -f3", bkpinfo->netfs_mount);
[2323]1685 mr_free(bkpinfo->isodir);
[2383]1686 bkpinfo->isodir = call_program_and_get_last_line_of_output(command);
[2266]1687 mr_free(command);
[256]1688
[1847]1689 if (!bkpinfo->restore_data) {
[2323]1690 mr_asprintf(comment, "How much data (in Megabytes) will each media store?");
[2316]1691 // BERLIOS: 4480 shouldn't be hardcoded here
1692 sz_size = popup_and_get_string("Size", comment, "4480");
1693 mr_free(comment);
1694 if (sz_size == NULL) {
[1847]1695 log_to_screen("User has chosen not to backup the PC");
1696 finish(1);
1697 }
1698 } else {
[2323]1699 mr_asprintf(sz_size, "0");
[256]1700 }
1701 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1702 bkpinfo->media_size[i] = atoi(sz_size);
1703 }
[2316]1704 mr_free(sz_size);
[1917]1705 if (bkpinfo->media_size[0] < 0) {
[541]1706 log_to_screen("User has chosen not to backup the PC");
[256]1707 finish(1);
1708 }
[128]1709 }
1710 if (bkpinfo->disaster_recovery) {
[2323]1711 mr_asprintf(command ,"umount %s/isodir 2> /dev/null", bkpinfo->tmpdir);
[2202]1712 (void)system(command);
[2266]1713 mr_free(command);
1714
[2400]1715 p = popup_and_get_string("Network protocol", "Which Network protocol should I use?", bkpinfo->netfs_proto);
1716 if (p == NULL) {
1717 log_to_screen("User has chosen not to backup the PC");
1718 finish(1);
1719 }
1720 mr_free(bkpinfo->netfs_proto);
1721 bkpinfo->netfs_proto = p;
1722
[2382]1723 p = popup_and_get_string("Network share", "Which remote Network share should I mount?", bkpinfo->netfs_mount);
[2316]1724 if (p == NULL) {
[541]1725 log_to_screen("User has chosen not to backup the PC");
[128]1726 finish(1);
1727 }
[2382]1728 mr_free(bkpinfo->netfs_mount);
1729 bkpinfo->netfs_mount = p;
[128]1730 }
[1858]1731 /* Initiate bkpinfo isodir path from running environment if mount already done */
[2323]1732 mr_free(bkpinfo->isodir);
[2382]1733 if (is_this_device_mounted(bkpinfo->netfs_mount)) {
[2383]1734 bkpinfo->isodir = call_program_and_get_last_line_of_output("mount | grep \":\" | cut -d' ' -f3 | head -n1");
[2323]1735
[1858]1736 } else {
[2382]1737 mr_asprintf(bkpinfo->isodir, "%s/netfsdir", bkpinfo->tmpdir);
[2323]1738 mr_asprintf(command, "mkdir -p %s", bkpinfo->isodir);
[128]1739 run_program_and_log_output(command, 5);
[2266]1740 mr_free(command);
1741
[2364]1742 if (bkpinfo->restore_data) {
[2382]1743 if (strstr(bkpinfo->netfs_proto, "sshfs")) {
1744 mr_asprintf(tmp, "sshfs -o ro %s %s", bkpinfo->netfs_mount, bkpinfo->isodir);
1745 } else {
1746 mr_asprintf(tmp, "mount -t %s -o nolock,ro %s %s", bkpinfo->netfs_proto, bkpinfo->netfs_mount, bkpinfo->isodir);
1747 }
[2370]1748 } else {
[2382]1749 if (strstr(bkpinfo->netfs_proto, "sshfs")) {
1750 mr_asprintf(tmp, "sshfs %s %s", bkpinfo->netfs_mount, bkpinfo->isodir);
1751 } else {
1752 mr_asprintf(tmp, "mount -t %s -o nolock %s %s", bkpinfo->netfs_proto, bkpinfo->netfs_mount, bkpinfo->isodir);
1753 }
[2364]1754 }
[1819]1755 run_program_and_log_output(tmp, 3);
[2214]1756 mr_free(tmp);
1757
[128]1758 malloc_string(g_selfmounted_isodir);
1759 strcpy(g_selfmounted_isodir, bkpinfo->isodir);
1760 }
[2382]1761 if (!is_this_device_mounted(bkpinfo->netfs_mount)) {
[2324]1762 popup_and_OK("Please mount that partition before you try to backup to or restore from it.");
[128]1763 finish(1);
1764 }
[2382]1765 p = popup_and_get_string("Directory", "Which directory within that mountpoint?", bkpinfo->netfs_remote_dir);
[2316]1766 if (p == NULL) {
[541]1767 log_to_screen("User has chosen not to backup the PC");
[128]1768 finish(1);
1769 }
[2382]1770 mr_free(bkpinfo->netfs_remote_dir);
1771 bkpinfo->netfs_remote_dir = p;
[2214]1772
[128]1773 // check whether writable - we better remove surrounding spaces for this
[2382]1774 mr_strip_spaces(bkpinfo->netfs_remote_dir);
[1767]1775
[2316]1776 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);
1777 if (p == NULL) {
[541]1778 log_to_screen("User has chosen not to backup the PC");
[251]1779 finish(1);
1780 }
[2322]1781 mr_free(bkpinfo->prefix);
1782 bkpinfo->prefix = p;
[251]1783 log_msg(3, "prefix set to %s", bkpinfo->prefix);
[99]1784
[2382]1785 log_msg(3, "Just set netfs_remote_dir to %s", bkpinfo->netfs_remote_dir);
[128]1786 log_msg(3, "isodir is still %s", bkpinfo->isodir);
1787 break;
[1]1788
[128]1789 case iso:
1790 if (!bkpinfo->disaster_recovery) {
[2316]1791 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);
1792 if (p == NULL) {
[541]1793 log_to_screen("User has chosen not to backup the PC");
[128]1794 finish(1);
1795 }
[2323]1796 bkpinfo->isodir = p;
[2316]1797
[128]1798 if (archiving_to_media) {
1799 if ((bkpinfo->compression_level =
1800 which_compression_level()) == -1) {
[541]1801 log_to_screen("User has chosen not to backup the PC");
[128]1802 finish(1);
1803 }
[2316]1804 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);
1805 if (p == NULL) {
[541]1806 log_to_screen("User has chosen not to backup the PC");
[128]1807 finish(1);
1808 }
1809 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
[2316]1810 bkpinfo->media_size[i] = atoi(p);
[128]1811 }
[2316]1812 mr_free(p);
[128]1813 } else {
1814 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1815 bkpinfo->media_size[i] = 650;
1816 }
1817 }
1818 }
[2316]1819 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);
1820 if (p == NULL) {
[804]1821 log_to_screen("User has chosen not to backup the PC");
1822 finish(1);
1823 }
[2322]1824 mr_free(bkpinfo->prefix);
1825 bkpinfo->prefix = p;
[804]1826 log_msg(3, "prefix set to %s", bkpinfo->prefix);
[128]1827 break;
[2322]1828
[128]1829 default:
1830 fatal_error
1831 ("I, Mojo Jojo, shall defeat those pesky Powerpuff Girls!");
1832 }
[1]1833
[128]1834 if (archiving_to_media) {
[1]1835
[2326]1836 mr_free(bkpinfo->boot_device);
[1]1837#ifdef __FreeBSD__
[2326]1838#define EXAMPLEBD "/dev/ad0"
[2383]1839 bkpinfo->boot_device = call_program_and_get_last_line_of_output("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/\\([0-9]\\).*/\\1/'");
[1]1840#else
[2326]1841#define EXAMPLEBD "/dev/hda"
[2383]1842 bkpinfo->boot_device = call_program_and_get_last_line_of_output("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'");
[1]1843#endif
[128]1844 i = which_boot_loader(bkpinfo->boot_device);
1845 if (i == 'U') // unknown
1846 {
[1]1847
[2326]1848 p = popup_and_get_string("Boot device", "What is your boot device? (e.g. "EXAMPLEBD")", bkpinfo->boot_device);
1849#undef EXAMPLEBD
1850 if (p == NULL) {
[541]1851 log_to_screen("User has chosen not to backup the PC");
[128]1852 finish(1);
1853 }
[2326]1854 mr_free(bkpinfo->boot_device);
1855 bkpinfo->boot_device = p;
1856#ifdef __FreeBSD__
[128]1857 i = which_boot_loader(bkpinfo->boot_device);
[1]1858#else
[2316]1859 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "LILO")) {
[128]1860 i = 'L';
1861 } else
[2316]1862 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "ELILO")) {
[128]1863 i = 'E';
1864 } else
[2316]1865 if (does_string_exist_in_boot_block(bkpinfo->boot_device, "GRUB")) {
[128]1866 i = 'G';
1867 } else {
1868 i = 'U';
1869 }
[1]1870#endif
[128]1871 if (i == 'U') {
[2326]1872 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]1873 i = 'R'; // raw
1874 } else {
[2326]1875 log_to_screen("I cannot find your boot loader. Please run mondoarchive with parameters.");
[128]1876 finish(1);
1877 }
1878 }
[1]1879 }
[128]1880 bkpinfo->boot_loader = i;
[2319]1881 mr_free(bkpinfo->include_paths);
[2323]1882 mr_asprintf(p, "/");
[2319]1883 bkpinfo->include_paths = p;
1884
[2316]1885 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);
1886 if (p == NULL) {
[541]1887 log_to_screen("User has chosen not to backup the PC");
[128]1888 finish(1);
[1]1889 }
[2319]1890 mr_free(bkpinfo->include_paths);
1891 bkpinfo->include_paths = p;
[2316]1892
[2382]1893 mr_asprintf(tmp, "%s", list_of_NETFS_mounts_only());
[128]1894 if (strlen(tmp) > 2) {
[2318]1895 mr_strcat(bkpinfo->exclude_paths, " %s",tmp);
[128]1896 }
[2214]1897 mr_free(tmp);
[1]1898// NTFS
[2383]1899 tmp = call_program_and_get_last_line_of_output("parted2fdisk -l | grep -i ntfs | awk '{ print $1};' | tr -s '\\n' ' ' | awk '{ print $0};'");
[2323]1900 if (strlen(tmp) > 2) {
[2316]1901 p = popup_and_get_string("NTFS partitions", "Please enter/confirm the NTFS partitions you wish to backup as well.", tmp);
1902
1903 if (p == NULL) {
[541]1904 log_to_screen("User has chosen not to backup the PC");
[128]1905 finish(1);
1906 }
[2327]1907 mr_free(bkpinfo->image_devs);
1908 bkpinfo->image_devs = p;
[128]1909 }
[1]1910
1911
[2316]1912 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);
1913 if (p == NULL) {
[541]1914 log_to_screen("User has chosen not to backup the PC");
[128]1915 finish(1);
1916 }
[2318]1917 mr_free(bkpinfo->exclude_paths);
1918 bkpinfo->exclude_paths = p;
[2316]1919
1920 p = popup_and_get_string("Temporary directory", "Please enter your temporary directory.", bkpinfo->tmpdir);
1921 if (p == NULL) {
[2220]1922 log_to_screen("User has chosen not to backup the PC");
1923 finish(1);
1924 }
[2321]1925 mr_free(bkpinfo->tmpdir);
1926 bkpinfo->tmpdir = p;
[2316]1927
1928 p = popup_and_get_string("Scratch directory", "Please enter your scratch directory.", bkpinfo->scratchdir);
1929 if (p == NULL) {
[2220]1930 log_to_screen("User has chosen not to backup the PC");
1931 finish(1);
1932 }
[2320]1933 mr_free(bkpinfo->scratchdir);
1934 bkpinfo->scratchdir = p;
[2316]1935
[1575]1936// Interactive mode:
1937#ifdef __IA64__
1938 bkpinfo->make_cd_use_lilo = TRUE;
1939#else
[128]1940 bkpinfo->make_cd_use_lilo = FALSE;
[1575]1941#endif
[128]1942 bkpinfo->backup_data = TRUE;
1943 bkpinfo->verify_data =
1944 ask_me_yes_or_no
[541]1945 ("Will you want to verify your backups after Mondo has created them?");
[1]1946
1947#ifndef __FreeBSD__
[128]1948 if (!ask_me_yes_or_no
[541]1949 ("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]1950#endif
[128]1951 {
[2328]1952 mr_free(bkpinfo->kernel_path);
1953 mr_asprintf(bkpinfo->kernel_path, "FAILSAFE");
[128]1954 }
[1]1955
[128]1956 if (!ask_me_yes_or_no
[541]1957 ("Are you sure you want to proceed? Hit 'no' to abort.")) {
1958 log_to_screen("User has chosen not to backup the PC");
[128]1959 finish(1);
1960 }
1961 } else {
1962 bkpinfo->restore_data = TRUE; // probably...
1963 }
[1]1964
[128]1965 if (bkpinfo->backup_media_type == iso
[2382]1966 || bkpinfo->backup_media_type == netfs) {
[128]1967 g_ISO_restore_mode = TRUE;
1968 }
[1]1969#ifdef __FreeSD__
1970// skip
1971#else
[2382]1972 if (bkpinfo->backup_media_type == netfs) {
1973 log_msg(3, "I think the Remote mount is mounted at %s", bkpinfo->isodir);
[128]1974 }
1975 log_it("isodir = %s", bkpinfo->isodir);
[2382]1976 if (bkpinfo->netfs_mount) {
1977 log_it("netfs_mount = '%s'", bkpinfo->netfs_mount);
[2325]1978 }
[2382]1979 if (bkpinfo->netfs_user) {
1980 log_it("netfs_user = '%s'", bkpinfo->netfs_user);
[2224]1981 }
[2382]1982 if (bkpinfo->netfs_proto) {
1983 log_it("netfs_proto = '%s'", bkpinfo->netfs_proto);
1984 }
[1]1985#endif
1986
[128]1987 log_it("media device = %s", bkpinfo->media_device);
1988 log_it("media size = %ld", bkpinfo->media_size[1]);
[2330]1989 tmp = bkptype_to_string(bkpinfo->backup_media_type);
1990 log_it("media type = %s", tmp);
1991 mr_free(tmp);
1992
[2327]1993 if (bkpinfo->prefix) {
1994 log_it("prefix = %s", bkpinfo->prefix);
1995 }
[128]1996 log_it("compression = %ld", bkpinfo->compression_level);
[2327]1997 if (bkpinfo->include_paths) {
1998 log_it("include_paths = '%s'", bkpinfo->include_paths);
1999 }
2000 if (bkpinfo->exclude_paths) {
2001 log_it("exclude_paths = '%s'", bkpinfo->exclude_paths);
2002 }
[128]2003 log_it("scratchdir = '%s'", bkpinfo->scratchdir);
2004 log_it("tmpdir = '%s'", bkpinfo->tmpdir);
[2327]2005 if (bkpinfo->image_devs) {
2006 log_it("image_devs = '%s'", bkpinfo->image_devs);
2007 }
[2322]2008 log_it("boot_device = '%s' (loader=%c)", bkpinfo->boot_device, bkpinfo->boot_loader);
[128]2009 if (bkpinfo->media_size[0] < 0) {
2010 if (archiving_to_media) {
2011 fatal_error("Media size is less than zero.");
2012 } else {
2013 log_msg(2, "Warning - media size is less than zero.");
2014 bkpinfo->media_size[0] = 0;
2015 }
[1]2016 }
[128]2017 paranoid_free(sz_size);
2018 return (0);
[1]2019}
2020
2021
2022
2023
2024/**
2025 * @addtogroup utilityGroup
2026 * @{
2027 */
2028/**
[2382]2029 * Get a space-separated list of NETFS devices and mounts.
[1]2030 * @return The list created.
2031 * @note The return value points to static data that will be overwritten with each call.
2032 */
[2382]2033char *list_of_NETFS_devices_and_mounts(void)
[1]2034{
[2214]2035 char *exclude_these_devices = NULL;
2036 char *exclude_these_directories = NULL;
[2187]2037 static char result_sz[1024];
[1]2038
[2382]2039 mr_asprintf(exclude_these_directories,"%s",list_of_NETFS_mounts_only());
[2383]2040 exclude_these_devices = call_program_and_get_last_line_of_output("tr -s '\t' ' ' < /etc/fstab | grep -E '( (coda|ncpfs|sshfs|nfs|nfs4|smbfs|cifs|afs|gfs|ocfs|ocfs2|mvfs|nsspool|nsvol) )' | cut -d' ' -f1 | tr -s '\n' ' ' | awk '{print $0;}'");
[2224]2041 snprintf(result_sz, 1023, "%s %s", exclude_these_directories, exclude_these_devices);
[2214]2042 mr_free(exclude_these_devices);
2043 mr_free(exclude_these_directories);
[128]2044 return (result_sz);
[1]2045}
2046
2047
2048
2049
2050/**
[2382]2051 * Get a space-separated list of NETFS mounts.
[1]2052 * @return The list created.
2053 * @note The return value points to static data that will be overwritten with each call.
2054 * @bug Even though we only want the mounts, the devices are still checked.
2055 */
[2382]2056char *list_of_NETFS_mounts_only(void)
[1]2057{
[2214]2058 char *exclude_these_directories = NULL;
[128]2059 static char result_sz[512];
[1]2060
[2383]2061 exclude_these_directories = call_program_and_get_last_line_of_output("mount -t coda,ncpfs,fuse.sshfs,nfs,nfs4,smbfs,cifs,afs,gfs,ocfs,ocfs2,mvfs,nsspool,nssvol | tr -s '\t' ' ' | cut -d' ' -f3 | tr -s '\n' ' ' | awk '{print $0;}'");
[2224]2062 snprintf(result_sz, 511, "%s", exclude_these_directories);
[2214]2063 mr_free(exclude_these_directories);
[128]2064 return (result_sz);
[1]2065}
2066
2067/* @} - end of utilityGroup */
2068
2069
2070
2071
2072
2073/**
2074 * Create a randomly-named FIFO. The format is @p stub "." [random] [random] where
2075 * [random] is a random number between 1 and 32767.
2076 * @param store_name_here Where to store the new filename.
2077 * @param stub A random number will be appended to this to make the FIFO's name.
2078 * @ingroup deviceGroup
2079 */
[128]2080void make_fifo(char *store_name_here, char *stub)
[1]2081{
[2266]2082 char *tmp = NULL;
[1]2083
[128]2084 assert_string_is_neither_NULL_nor_zerolength(stub);
[1]2085
[128]2086 sprintf(store_name_here, "%s%d%d", stub, (int) (random() % 32768),
2087 (int) (random() % 32768));
2088 make_hole_for_file(store_name_here);
2089 mkfifo(store_name_here, S_IRWXU | S_IRWXG);
[2323]2090 mr_asprintf(tmp, "chmod 770 %s", store_name_here);
[128]2091 paranoid_system(tmp);
[2266]2092 mr_free(tmp);
[1]2093}
2094
2095
2096
2097
2098
2099
2100/**
2101 * Set the tmpdir and scratchdir to reside on the partition with the most free space.
2102 * Automatically excludes DOS, NTFS, SMB, and NFS filesystems.
2103 * @param bkpinfo The backup information structure. @c bkpinfo->tmpdir and @c bkpinfo->scratchdir will be set.
2104 * @ingroup utilityGroup
2105 */
[2321]2106void sensibly_set_scratchdir()
[1]2107{
[2211]2108 char *tmp = NULL;
[2214]2109 char *command = NULL;
2110 char *sz = NULL;
[1]2111
2112#ifdef __FreeBSD__
[2383]2113 tmp = call_program_and_get_last_line_of_output("LANGUAGE=C df -m -P -t nonfs,msdosfs,ntfs,ntfs-3g,smbfs,smb,cifs,afs,gfs,ocfs,ocfs2,mvfs,nsspool,nssvol | grep -vE \"none|Filesystem\" | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'");
[1]2114#else
[2383]2115 tmp = call_program_and_get_last_line_of_output("LANGUAGE=C df -m -P -x nfs -x nfs4 -x fuse.sshfs -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]2116#endif
2117
[128]2118 if (tmp[0] != '/') {
[2323]2119 mr_asprintf(sz, "%s", tmp);
[2320]2120 mr_free(tmp);
[2323]2121 mr_asprintf(tmp, "/%s", sz);
[2214]2122 mr_free(sz);
[128]2123 }
2124 if (!tmp[0]) {
[2321]2125 fatal_error("I couldn't figure out the scratchdir!");
[128]2126 }
[2325]2127
2128 /* Cleaning a potential previous scratchdir */
2129 if (bkpinfo->scratchdir) {
2130 mr_asprintf(command, "rm -Rf %s", bkpinfo->scratchdir);
2131 paranoid_system(command);
2132 mr_free(command);
2133 }
[2320]2134 mr_free(bkpinfo->scratchdir);
[2325]2135
[2324]2136 mr_asprintf(bkpinfo->scratchdir , "%s/mondo.scratch.%d", tmp, (int) (random() % 32768));
[128]2137 log_it("bkpinfo->scratchdir is being set to %s", bkpinfo->scratchdir);
[1]2138
[2325]2139 /* Cleaning potential previous scratchdir */
[2323]2140 mr_asprintf(command, "rm -Rf %s/mondo.scratch.*", tmp);
[2320]2141 mr_free(tmp);
[2211]2142
[128]2143 paranoid_system(command);
[2214]2144 mr_free(command);
[1]2145}
2146
2147
2148
2149
2150
2151
2152/**
2153 * @addtogroup deviceGroup
2154 * @{
2155 */
2156/**
[2325]2157 * If we can read @p dev, set @return output to it.
2158 * If @p dev cannot be read, set @p output to NULL.
[1]2159 * @param dev The device to check for.
[2325]2160 * @return output Set to @p dev if @p dev exists, NULL otherwise.
[1]2161 */
[2325]2162char *set_dev_to_this_if_rx_OK(char *dev)
[1]2163{
[2266]2164 char *command = NULL;
[2325]2165 char *output = NULL;
[1]2166
[128]2167 if (!dev || dev[0] == '\0') {
[2325]2168 return(NULL);
[128]2169 }
2170 log_msg(10, "Injecting %s", dev);
2171 inject_device(dev);
2172 if (!does_file_exist(dev)) {
2173 log_msg(10, "%s doesn't exist. Returning FALSE.", dev);
[2325]2174 return(NULL);
[128]2175 }
[2323]2176 mr_asprintf(command, "dd bs=%ld count=1 if=%s of=/dev/null &> /dev/null", 512L, dev);
[128]2177 if (!run_program_and_log_output(command, FALSE)
2178 && !run_program_and_log_output(command, FALSE)) {
[2325]2179 mr_asprintf(output, "%s", dev);
[128]2180 log_msg(4, "Found it - %s", dev);
2181 } else {
2182 log_msg(4, "It's not %s", dev);
2183 }
[2266]2184 mr_free(command);
[2325]2185 return(output);
[1]2186}
2187
2188
2189
2190
2191
2192/**
2193 * Find out what number CD is in the drive.
2194 * @param bkpinfo The backup information structure. The @c bkpinfo->media_device field is the only one used.
2195 * @return The current CD number, or -1 if it could not be found.
2196 * @note If the CD is not mounted, it will be mounted
2197 * (and remain mounted after this function returns).
2198 */
[1645]2199int what_number_cd_is_this()
[1]2200{
[128]2201 int cd_number = -1;
[2211]2202 char *mountdev = NULL;
2203 char *tmp = NULL;
[1]2204
[128]2205 assert(bkpinfo != NULL);
[2324]2206 // log_it("Asking what_number_cd_is_this");
[128]2207 if (g_ISO_restore_mode) {
[2323]2208 mr_asprintf(tmp, "mount | grep iso9660 | awk '{print $3;}'");
[2383]2209 mountdev = call_program_and_get_last_line_of_output(tmp);
2210 mr_strcat(mountdev, "/archives/THIS-CD-NUMBER");
[2324]2211 mr_free(tmp);
[2211]2212
[2357]2213 tmp = last_line_of_file(mountdev);
2214 cd_number = atoi(tmp);
2215 mr_free(tmp);
2216 mr_free(mountdev);
[128]2217 return (cd_number);
2218 }
[1]2219
[2325]2220 if (bkpinfo->media_device) {
2221 mr_asprintf(mountdev, "%s", bkpinfo->media_device);
2222 }
2223 if (!mountdev) {
[2324]2224 log_it("(what_number_cd_is_this) Warning - media_device unknown. Finding out...");
[2325]2225 mr_free(bkpinfo->media_device);
2226 bkpinfo->media_device = find_cdrom_device(FALSE);
2227 mr_asprintf(mountdev, "%s", bkpinfo->media_device);
[128]2228 }
2229 if (!is_this_device_mounted(MNT_CDROM)) {
[1741]2230 if (bkpinfo->backup_media_type == usb) {
2231 mount_USB_here(mountdev, MNT_CDROM);
2232 } else {
2233 mount_CDROM_here(mountdev, MNT_CDROM);
2234 }
[128]2235 }
[2324]2236 mr_free(mountdev);
[2211]2237
[2357]2238 tmp = last_line_of_file(MNT_CDROM "/archives/THIS-CD-NUMBER");
2239 cd_number = atoi(tmp);
2240 mr_free(tmp);
[128]2241 return (cd_number);
[1]2242}
2243
2244
2245
2246
2247
2248
2249
2250/**
2251 * Find out what device is mounted as root (/).
2252 * @return Root device.
2253 * @note The returned string points to static storage and will be overwritten with every call.
2254 * @bug A bit of a misnomer; it's actually finding out the root device.
2255 * The mountpoint (where it's mounted) will obviously be '/'.
2256 */
[2330]2257char *where_is_root_mounted() {
[128]2258 /*@ buffers **************** */
[2421]2259 char *output = NULL;
[1]2260
2261
2262#ifdef __FreeBSD__
[2383]2263 output = call_program_and_get_last_line_of_output("mount | grep ' on / ' | cut -d' ' -f1");
[1]2264#else
[2383]2265 output = call_program_and_get_last_line_of_output("mount | grep ' on / ' | cut -d' ' -f1 | sed 's/[0-9]//' | sed 's/[0-9]//'");
[2330]2266 if (strstr(output, "/dev/cciss/")) {
2267 mr_free(output);
[2383]2268 output = call_program_and_get_last_line_of_output("mount | grep ' on / ' | cut -d' ' -f1 | cut -dp -f1");
[128]2269 }
[2330]2270 if (strstr(output, "/dev/md")) {
2271 mr_free(output);
[2383]2272 output = call_program_and_get_last_line_of_output("mount | grep ' on / ' | cut -d' ' -f1");
[128]2273 }
[1]2274#endif
2275
[2330]2276 return (output);
[1]2277}
2278
2279
2280/**
2281 * Find out which boot loader is in use.
2282 * @param which_device Device to look for the boot loader on.
2283 * @return 'L' for LILO, 'E'for ELILO, 'G' for GRUB, 'B' or 'D' for FreeBSD boot loaders, or 'U' for Unknown.
2284 * @note Under Linux, all drives are examined, not just @p which_device.
2285 */
2286#ifdef __FreeBSD__
[2330]2287char which_boot_loader(char *which_device) {
[128]2288 int count_lilos = 0;
2289 int count_grubs = 0;
2290 int count_boot0s = 0;
2291 int count_dangerouslydedicated = 0;
[1]2292
[128]2293 log_it("looking at drive %s's MBR", which_device);
2294 if (does_string_exist_in_boot_block(which_device, "GRUB")) {
2295 count_grubs++;
2296 }
2297 if (does_string_exist_in_boot_block(which_device, "LILO")) {
2298 count_lilos++;
2299 }
2300 if (does_string_exist_in_boot_block(which_device, "Drive")) {
2301 count_boot0s++;
2302 }
[2330]2303 if (does_string_exist_in_first_N_blocks(which_device, "FreeBSD/i386", 17)) {
[128]2304 count_dangerouslydedicated++;
2305 }
2306 log_it("%d grubs and %d lilos and %d elilos and %d boot0s and %d DD\n",
2307 count_grubs, count_lilos, count_elilos, count_boot0s,
2308 count_dangerouslydedicated);
[1]2309
[128]2310 if (count_grubs && !count_lilos) {
2311 return ('G');
2312 } else if (count_lilos && !count_grubs) {
2313 return ('L');
2314 } else if (count_grubs == 1 && count_lilos == 1) {
2315 log_it("I'll bet you used to use LILO but switched to GRUB...");
2316 return ('G');
2317 } else if (count_boot0s == 1) {
2318 return ('B');
2319 } else if (count_dangerouslydedicated) {
2320 return ('D');
2321 } else {
2322 log_it("Unknown boot loader");
2323 return ('U');
2324 }
[1]2325}
2326
2327#else
2328
[2330]2329char which_boot_loader(char *which_device) {
[128]2330 /*@ buffer ***************************************************** */
[2241]2331 char *list_drives_cmd = NULL;
[2330]2332 char *tmp = NULL;
[2351]2333 char *current_drive = NULL;
[1]2334
[128]2335 /*@ pointers *************************************************** */
[2351]2336 FILE *pdrives = NULL;
[1]2337
[128]2338 /*@ int ******************************************************** */
2339 int count_lilos = 0;
2340 int count_grubs = 0;
[1]2341
[128]2342 /*@ end vars *************************************************** */
[1]2343
2344
2345#ifdef __IA64__
[128]2346 /* No choice for it */
2347 return ('E');
[1]2348#endif
2349
[2330]2350 tmp = where_is_root_mounted();
2351 mr_asprintf(list_drives_cmd, "parted2fdisk -l 2>/dev/null | grep \"/dev/.*:\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/; echo %s", tmp);
2352 mr_free(tmp);
[128]2353 log_it("list_drives_cmd = %s", list_drives_cmd);
[1]2354
[2330]2355 pdrives = popen(list_drives_cmd, "r");
2356 mr_free(list_drives_cmd);
2357
2358 if (!pdrives) {
[128]2359 log_OS_error("Unable to open list of drives");
2360 return ('\0');
[1]2361 }
[2241]2362
[2351]2363 for (mr_getline(current_drive, pdrives); !feof(pdrives); mr_getline(current_drive, pdrives)) {
2364 mr_strip_spaces(current_drive);
[128]2365 log_it("looking at drive %s's MBR", current_drive);
2366 if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
2367 count_grubs++;
[2331]2368 /* BERLIOS : removed as I don't think it's mandatory here
[2330]2369 mr_free(which_device);
2370 mr_asprintf(which_device, "%s", current_drive);
[2331]2371 */
[2351]2372 mr_free(current_drive);
[128]2373 break;
2374 }
2375 if (does_string_exist_in_boot_block(current_drive, "LILO")) {
2376 count_lilos++;
[2331]2377 /* BERLIOS : removed as I don't think it's mandatory here
[2330]2378 mr_free(which_device);
2379 mr_asprintf(which_device, "%s", current_drive);
[2331]2380 */
[2351]2381 mr_free(current_drive);
[128]2382 break;
2383 }
[2351]2384 mr_free(current_drive);
[1]2385 }
[2351]2386 mr_free(current_drive);
2387
[128]2388 if (pclose(pdrives)) {
2389 log_OS_error("Cannot pclose pdrives");
2390 }
2391 log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
2392 if (count_grubs && !count_lilos) {
2393 return ('G');
2394 } else if (count_lilos && !count_grubs) {
2395 return ('L');
2396 } else if (count_grubs == 1 && count_lilos == 1) {
2397 log_it("I'll bet you used to use LILO but switched to GRUB...");
2398 return ('G');
2399 } else {
[1451]2400 // We need to look on each partition then
[2323]2401 mr_asprintf(list_drives_cmd, "parted2fdisk -l 2>/dev/null | grep -E \"^/dev/\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/");
[1451]2402 log_it("list_drives_cmd = %s", list_drives_cmd);
2403
2404 if (!(pdrives = popen(list_drives_cmd, "r"))) {
2405 log_OS_error("Unable to open list of drives");
[2241]2406 mr_free(list_drives_cmd);
[1451]2407 return ('\0');
2408 }
[2241]2409 mr_free(list_drives_cmd);
2410
[2351]2411 for (mr_getline(current_drive, pdrives); !feof(pdrives); mr_getline(current_drive, pdrives)) {
2412 mr_strip_spaces(current_drive);
[1451]2413 log_it("looking at partition %s's BR", current_drive);
2414 if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
2415 count_grubs++;
[2331]2416 /* BERLIOS : removed as I don't think it's mandatory here
[2330]2417 mr_free(which_device);
2418 mr_asprintf(which_device, "%s", current_drive);
[2331]2419 */
[2351]2420 mr_free(current_drive);
[1451]2421 break;
2422 }
2423 if (does_string_exist_in_boot_block(current_drive, "LILO")) {
2424 count_lilos++;
[2331]2425 /* BERLIOS : removed as I don't think it's mandatory here
[2330]2426 mr_free(which_device);
2427 mr_asprintf(which_device, "%s", current_drive);
[2331]2428 */
[2351]2429 mr_free(current_drive);
[1451]2430 break;
2431 }
[2351]2432 mr_free(current_drive);
[1451]2433 }
[2351]2434 mr_free(current_drive);
2435
[1451]2436 if (pclose(pdrives)) {
2437 log_OS_error("Cannot pclose pdrives");
2438 }
2439 log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
2440 if (count_grubs && !count_lilos) {
2441 return ('G');
2442 } else if (count_lilos && !count_grubs) {
2443 return ('L');
2444 } else if (count_grubs == 1 && count_lilos == 1) {
2445 log_it("I'll bet you used to use LILO but switched to GRUB...");
2446 return ('G');
2447 } else {
2448 log_it("Unknown boot loader");
2449 return ('U');
2450 }
[128]2451 }
[1]2452}
2453#endif
2454
2455
2456
2457
2458/**
2459 * Write zeroes over the first 16K of @p device.
2460 * @param device The device to zero.
2461 * @return 0 for success, 1 for failure.
2462 */
[128]2463int zero_out_a_device(char *device)
[1]2464{
[128]2465 FILE *fout;
2466 int i;
[1]2467
[128]2468 assert_string_is_neither_NULL_nor_zerolength(device);
[1]2469
[128]2470 log_it("Zeroing drive %s", device);
2471 if (!(fout = fopen(device, "w"))) {
2472 log_OS_error("Unable to open/write to device");
2473 return (1);
2474 }
2475 for (i = 0; i < 16384; i++) {
2476 fputc('\0', fout);
2477 }
2478 paranoid_fclose(fout);
2479 log_it("Device successfully zeroed.");
2480 return (0);
[1]2481}
2482
2483/**
2484 * Return the device pointed to by @p incoming.
2485 * @param incoming The device to resolve symlinks for.
2486 * @return The path to the real device file.
2487 * @note The returned string points to static storage that will be overwritten with each call.
2488 * @bug Won't work with file v4.0; needs to be written in C.
2489 */
[128]2490char *resolve_softlinks_to_get_to_actual_device_file(char *incoming)
[1]2491{
[2330]2492 char *output;
[2266]2493 char *command = NULL;
[128]2494 char *curr_fname;
[2214]2495 char *scratch = NULL;
2496 char *tmp = NULL;
[128]2497 char *p;
2498
2499 struct stat statbuf;
2500 malloc_string(curr_fname);
2501 if (!does_file_exist(incoming)) {
[2330]2502 log_it("resolve_softlinks_to_get_to_actual_device_file --- device not found");
2503 mr_asprintf(output, "%s", incoming);
[128]2504 } else {
2505 strcpy(curr_fname, incoming);
2506 lstat(curr_fname, &statbuf);
2507 while (S_ISLNK(statbuf.st_mode)) {
2508 log_msg(1, "curr_fname = %s", curr_fname);
[2323]2509 mr_asprintf(command, "file %s", curr_fname);
[2383]2510 tmp = call_program_and_get_last_line_of_output(command);
[2266]2511 mr_free(command);
[2330]2512 for (p = tmp + strlen(tmp); p != tmp && *p != '`' && *p != ' '; p--);
[128]2513 p++;
[2323]2514 mr_asprintf(scratch, "%s", p);
[128]2515 for (p = scratch; *p != '\0' && *p != '\''; p++);
2516 *p = '\0';
[2214]2517 log_msg(0, "curr_fname %s --> '%s' --> %s", curr_fname, tmp, scratch);
2518 mr_free(tmp);
2519
[128]2520 if (scratch[0] == '/') {
2521 strcpy(curr_fname, scratch); // copy whole thing because it's an absolute softlink
2522 } else { // copy over the basename cos it's a relative softlink
2523 p = curr_fname + strlen(curr_fname);
2524 while (p != curr_fname && *p != '/') {
2525 p--;
2526 }
2527 if (*p == '/') {
2528 p++;
2529 }
2530 strcpy(p, scratch);
2531 }
[2214]2532 mr_free(scratch);
[128]2533 lstat(curr_fname, &statbuf);
2534 }
[2330]2535 mr_asprintf(output, "%s", curr_fname);
[128]2536 log_it("resolved %s to %s", incoming, output);
[1]2537 }
[128]2538 paranoid_free(curr_fname);
2539 return (output);
[1]2540}
2541
2542/* @} - end of deviceGroup */
2543
2544
2545/**
2546 * Return the type of partition format (GPT or MBR)
2547 */
2548char *which_partition_format(const char *drive)
2549{
[2330]2550 char *output;
[2214]2551 char *tmp = NULL;
[2266]2552 char *command = NULL;
2553 char *fdisk = NULL;
[152]2554#ifdef __IA64__
[128]2555 struct stat buf;
[152]2556#endif
[2323]2557 mr_asprintf(fdisk, "/sbin/parted2fdisk");
2558 mr_asprintf(command, "%s -l %s | grep 'EFI GPT'", fdisk, drive);
[2266]2559 mr_free(fdisk);
2560
[2383]2561 tmp = call_program_and_get_last_line_of_output(command);
[2266]2562 mr_free(command);
2563
[128]2564 if (strstr(tmp, "GPT") == NULL) {
[2330]2565 mr_asprintf(output, "MBR");
[128]2566 } else {
[2330]2567 mr_asprintf(output, "GPT");
[128]2568 }
[2214]2569 mr_free(tmp);
2570
[128]2571 log_msg(0, "Found %s partition table format type", output);
2572 return (output);
[1]2573}
2574
2575/* @} - end of deviceGroup */
Note: See TracBrowser for help on using the repository browser.