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

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

r3341@localhost: bruno | 2009-08-13 22:36:24 +0200

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