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

Last change on this file since 687 was 687, checked in by bcornec, 18 years ago

merge -r671:686 $SVN_M/branches/stable

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