source: MondoRescue/branches/stable/mondo/src/common/libmondo-devices.c@ 1162

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

Merge trunk for libmondo-devices.c finished (first pass)

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