source: MondoRescue/trunk/mondo/src/common/libmondo-devices.c@ 1161

Last change on this file since 1161 was 1161, checked in by Bruno Cornec, 17 years ago

Update trunk with the latest feedback from stable (obtained with meld) - First pass

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