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

Last change on this file since 2321 was 2321, checked in by Bruno Cornec, 15 years ago

r3332@localhost: bruno | 2009-08-07 23:59:34 +0200

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