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

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

r3327@localhost: bruno | 2009-08-04 00:17:46 +0200
popup_and_get_string and build_partition_name now return an allocated string

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