source: MondoRescue/branches/stable/mondo/src/common/libmondo-tools.c@ 1817

Last change on this file since 1817 was 1817, checked in by Bruno Cornec, 16 years ago

exclude_paths now a configuration item

  • Property svn:keywords set to Id
File size: 35.7 KB
RevLine 
[1178]1/* $Id: libmondo-tools.c 1817 2007-11-20 14:36:00Z bruno $
2misc tools
[1]3*/
4
5/**
6 * @file
7 * Miscellaneous tools that didn't really fit anywhere else.
8 */
9
10#include "my-stuff.h"
[1080]11#include "mr_mem.h"
[1123]12#include "mr_msg.h"
[1087]13#include "mr_file.h"
[1549]14#include "mr_gettext.h"
[1592]15#include "mr_conf.h"
[1095]16
[1]17#include "mondostructures.h"
18#include "libmondo-tools.h"
[1178]19#include "newt-specific-EXT.h"
[1]20#include "libmondo-files-EXT.h"
21#include "libmondo-fork-EXT.h"
22#include "libmondo-raid-EXT.h"
23#include <sys/socket.h>
24#include <netdb.h>
[1663]25#include <stdlib.h>
[1]26#include <netinet/in.h>
27#include <arpa/inet.h>
[1377]28#include <sys/utsname.h>
[1]29
30/*@unused@*/
[99]31//static char cvsid[] = "$Id: libmondo-tools.c 1817 2007-11-20 14:36:00Z bruno $";
[1]32
33extern int g_tape_buffer_size_MB;
34extern char *g_serial_string;
35extern bool g_text_mode;
36extern int g_currentY;
37extern int g_current_media_number;
[1344]38extern char *MONDO_LOGFILE;
[1]39
[1592]40extern struct mr_ar_conf *mr_conf;
41
[1663]42/* Reference to global bkpinfo */
43extern struct s_bkpinfo *bkpinfo;
44
[1]45/**
46 * @addtogroup globalGroup
47 * @{
48 */
[128]49bool g_remount_cdrom_at_end, ///< TRUE if we unmounted the CD-ROM and should remount it when done with the backup.
50 g_remount_floppy_at_end; ///< TRUE if we unmounted the floppy and should remount it when done with the backup.
51bool g_cd_recovery; ///< TRUE if we're making an "autonuke" backup.
[1]52double g_kernel_version;
53
54/**
[1178]55 * The place where /boot is mounted. - Used locally only
[1]56 */
[1178]57static char *g_boot_mountpt = NULL;
[1]58
59/**
60 * The serial string (used to differentiate between backups) of the current backup.
61 */
[128]62char *g_serial_string = NULL;
[1]63
64/**
65 * The location where tmpfs is mounted, or "" if it's not mounted.
66 */
[128]67char *g_tmpfs_mountpt = NULL;
68char *g_magicdev_command = NULL;
[1]69
70/* @} - end of globalGroup */
71
72
73extern pid_t g_buffer_pid;
74extern pid_t g_main_pid;
75
76extern t_bkptype g_backup_media_type;
[1178]77extern char *g_backup_media_string;
[1]78
79extern bool am_I_in_disaster_recovery_mode(void);
80
81
82/**
83 * @addtogroup utilityGroup
84 * @{
85 */
86/**
87 * Assertion handler. Prints a friendly message to the user,
88 * offering to ignore all, dump core, break to debugger,
89 * exit, or ignore. Intended to be used with an assert() macro.
90 *
91 * @param file The file in which the assertion triggered.
92 * @param function The function (@c __FUNCTION__) in which the assertion triggered.
93 * @param line The line number of the assert() statement.
94 * @param exp The expression that failed (as a string).
95 */
[128]96void _mondo_assert_fail(const char *file,
97 const char *function, int line, const char *exp)
[1]98{
[128]99 static int ignoring_assertions = 0;
100 bool is_valid = TRUE;
[1]101
[128]102 log_it("ASSERTION FAILED: `%s' at %s:%d in %s", exp, file, line,
103 function);
104 if (ignoring_assertions) {
105 log_it("Well, the user doesn't care...");
106 return;
107 }
[1]108#ifndef _XWIN
[128]109 if (!g_text_mode)
110 newtSuspend();
[1]111#endif
[1178]112 printf(_("ASSERTION FAILED: `%s'\n"), exp);
113 printf(_("\tat %s:%d in %s\n\n"), file, line, function);
114 printf(_("(I)gnore, ignore (A)ll, (D)ebug, a(B)ort, or (E)xit? "));
[128]115 do {
116 is_valid = TRUE;
117 switch (toupper(getchar())) {
118 case 'A': // ignore (A)ll
119 ignoring_assertions = 1;
120 break;
121 case 'B': // a(B)ort
122 signal(SIGABRT, SIG_DFL); /* prevent SIGABRT handler from running */
123 raise(SIGABRT);
124 break; /* "can't get here" */
125 case 'D': // (D)ebug, aka asm("int 3")
[1]126#ifdef __IA32__
[128]127 __asm__ __volatile__("int $3"); // break to debugger
[1]128#endif
[128]129 break;
130 case 'E': // (E)xit
131 fatal_error("Failed assertion -- see above for details");
132 break; /* "can't get here" */
133 case 'I': // (I)gnore
134 break;
135 /* These next two work as follows:
136 the `default' catches the user's invalid choice and says so;
137 the '\n' catches the newline on the end and prints the prompt again.
138 */
139 case '\n':
140 printf
[1178]141 (_("(I)gnore, ignore (A)ll, (D)ebug, a(B)ort, or (E)xit? "));
[128]142 break;
143 default:
144 is_valid = FALSE;
[1178]145 printf(_("Invalid choice.\n"));
[128]146 break;
147 }
148 } while (!is_valid);
149
150 if (ignoring_assertions) {
151 log_it("Ignoring ALL assertions from now on.");
152 } else {
153 log_it("Ignoring assertion: %s", exp);
[1]154 }
155
[128]156 getchar(); // skip \n
[1]157
158#ifndef _XWIN
[128]159 if (!g_text_mode)
160 newtResume();
[1]161#endif
162}
163
164/**
165 * Clean's up users' KDE desktops.
166 * @bug Details about this function are unknown.
167 */
168void clean_up_KDE_desktop_if_necessary(void)
169{
[128]170 char *tmp;
[1]171
[1178]172 mr_asprintf(&tmp,
173 "for i in `find /root /home -type d -name Desktop -maxdepth 2`; do \
[1]174file=$i/.directory; if [ -f \"$file\" ] ; then mv -f $file $file.old ; \
[273]175awk '{if (index($0, \"rootimagesmindi\")) { while (length($0)>2) { getline;} ; } \
[275]176else { print $0;};}' $file.old > $file ; fi ; done");
[128]177 run_program_and_log_output(tmp, 5);
[1080]178 mr_free(tmp);
[1]179}
180
181
182/**
[1061]183 * Locate mondoarchive's home directory. Searches in /usr/share/mondo,
[1]184 * /usr/local/share/mondo, /opt, or if all else fails, search /usr.
185 *
186 * @param home_sz String to store the home directory ("" if it could not be found).
187 * @return 0 for success, nonzero for failure.
188 */
189int find_and_store_mondoarchives_home(char *home_sz)
190{
[128]191 assert(home_sz != NULL);
[424]192 strcpy(home_sz, MONDO_SHARE);
[182]193 return (0);
[1]194}
195
196
[1377]197char *get_architecture(void) {
[1]198#ifdef __IA32__
[1377]199# ifdef __X86_64__
200 return ("x86_64");
201# else
202 return ("i386");
203# endif
[1]204#endif
[1178]205#ifdef __X86_64__
206 return ("x86_64");
207#endif
[1]208#ifdef __IA64__
[128]209 return ("ia64");
[1]210#endif
[128]211 return ("unknown");
[1]212}
213
214
[1377]215char *get_uname_m(void) {
216
217 struct utsname utsn;
218 char *tmp = NULL;
219
220 uname(&utsn);
221 mr_asprintf(&tmp, utsn.machine);
222 return (tmp);
223}
224
225
226
[1178]227/* BERLIOS: Use uname instead */
[1152]228double get_kernel_version(void)
[1]229{
[128]230 char *p, tmp[200];
231 double d;
[1]232#ifdef __FreeBSD__
[128]233 // JOSH - FIXME :)
234 d = 5.2; // :-)
[1]235#else
[128]236 strcpy(tmp, call_program_and_get_last_line_of_output("uname -r"));
237 p = strchr(tmp, '.');
238 if (p) {
239 p = strchr(++p, '.');
240 if (p) {
241 while (*p) {
242 *p = *(p + 1);
243 p++;
244 }
245 }
246 }
[1107]247// mr_msg(1, "tmp = '%s'", tmp);
[128]248 d = atof(tmp);
[1]249#endif
[1107]250 mr_msg(1, "g_kernel_version = %f", d);
[128]251 return (d);
[1]252}
253
254
255/**
256 * Get the current time.
257 * @return number of seconds since the epoch.
258 */
[1152]259long get_time(void)
[1]260{
[128]261 return (long) time((void *) 0);
[1]262}
263
264
265/**
266 * Initialize a RAID volume structure, setting fields to zero. The
267 * actual hard drive is unaffected.
268 *
269 * @param raidrec The RAID volume structure to initialize.
270 * @note This function is system dependent.
271 */
272#ifdef __FreeBSD__
[128]273void initialize_raidrec(struct vinum_volume *raidrec)
[1]274{
[128]275 int i, j;
276 raidrec->volname[0] = '\0';
277 raidrec->plexes = 0;
278 for (i = 0; i < 9; ++i) {
279 raidrec->plex[i].raidlevel = -1;
280 raidrec->plex[i].stripesize = 0;
281 raidrec->plex[i].subdisks = 0;
282 for (j = 0; j < 9; ++j) {
283 strcpy(raidrec->plex[i].sd[j].which_device, "");
284 }
285 }
[1]286}
287#else
[128]288void initialize_raidrec(struct raid_device_record *raidrec)
[1]289{
[128]290 assert(raidrec != NULL);
291 raidrec->raid_device[0] = '\0';
[558]292 raidrec->raid_level = -9;
[128]293 raidrec->persistent_superblock = 1;
[558]294 raidrec->chunk_size = 64;
295 raidrec->parity = -1;
[128]296 raidrec->data_disks.entries = 0;
297 raidrec->spare_disks.entries = 0;
298 raidrec->parity_disks.entries = 0;
299 raidrec->failed_disks.entries = 0;
300 raidrec->additional_vars.entries = 0;
[1]301}
302#endif
303
304
305/**
306 * Insert modules that Mondo requires.
[1221]307 * Currently inserts @c msdos, @c vfat, and @c loop for Linux;
[1]308 * @c msdosfs and @c ext2fs for FreeBSD.
309 */
310void insmod_crucial_modules(void)
311{
312#ifdef __FreeBSD__
[128]313 system("kldstat | grep msdosfs || kldload msdosfs 2> /dev/null");
314 system("kldstat | grep ext2fs || kldload ext2fs 2> /dev/null");
[1]315#else
[1221]316 system("modprobe -a msdos vfat loop &> /dev/null");
[1]317#endif
318}
319
320
321/**
322 * Finish configuring the backup information structure. Call this function
323 * to set the parameters that depend on those that can be given on the command
324 * line.
325 *
326 * @param bkpinfo The backup information structure. Fields modified/used:
327 * - Used: @c bkpinfo->backup_data
328 * - Used: @c bkpinfo->backup_media_type
[1594]329 * - Used: @c bkpinfo->writer_speed
[1]330 * - Used: @c bkpinfo->include_paths
[20]331 * - Used: @c bkpinfo->prefix
[1]332 * - Used: @c bkpinfo->isodir
[1609]333 * - Used: @c bkpinfo->manual_tray
[1]334 * - Used: @c bkpinfo->make_cd_use_lilo
335 * - Used: @c bkpinfo->media_device
336 * - Used: @c bkpinfo->nfs_mount
337 * - Used: @c bkpinfo->nonbootable_backup
338 * - Used: @c bkpinfo->scratchdir
339 * - Used: @c bkpinfo->tmpdir
340 * - Modified: @c bkpinfo->call_before_iso
341 * - Modified: @c bkpinfo->call_make_iso
342 * - Modified: @c bkpinfo->optimal_set_size
343 *
344 * @return number of errors, or 0 for success.
345 * @note Also creates directories that are specified in the @c bkpinfo structure but
346 * do not exist.
347 */
[1663]348int post_param_configuration()
[1]349{
[1178]350 char *extra_cdrom_params = NULL;
351 char *mondo_mkisofs_sz = NULL;
352 char *command = NULL;
353 char *hostname = NULL;
354 char *ip_address = NULL;
[128]355 int retval = 0;
356 long avm = 0;
[1178]357 char *colon = NULL;
358 char *tmp = NULL;
359 char *call_before_iso_user = NULL;
[128]360 int rdsiz_MB;
[1178]361 char *iso_dev = NULL;
362 char *iso_mnt = NULL;
363 char *iso_tmp = NULL;
364 char *iso_path = NULL;
[1087]365 FILE *fd1 = NULL;
[1]366
[128]367 assert(bkpinfo != NULL);
368 bkpinfo->optimal_set_size =
[686]369 (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type) ? 16 : 16) *
[128]370 1024;
[1]371
[1107]372 mr_msg(1, "Foo");
[128]373 if (bkpinfo->backup_media_type == tape) {
[1107]374 mr_msg(1, "Bar");
[1178]375 mr_asprintf(&tmp, "mt -f %s status", bkpinfo->media_device);
[1107]376 mr_msg(1, "tmp = '%s'", tmp);
[128]377 if (run_program_and_log_output(tmp, 3)) {
378 fatal_error
379 ("Unable to open tape device. If you haven't specified it with -d, do so. If you already have, check your parameter. I think it's wrong.");
380 }
[1178]381 mr_free(tmp);
[1]382 }
[128]383 make_hole_for_dir(bkpinfo->scratchdir);
384 if (bkpinfo->backup_media_type == iso)
385 make_hole_for_dir(bkpinfo->isodir);
[1]386
[128]387 run_program_and_log_output("uname -a", 5);
[678]388 run_program_and_log_output("cat /etc/*-release", 5);
[128]389 sprintf(g_tmpfs_mountpt, "%s/tmpfs", bkpinfo->tmpdir);
[1178]390 mr_asprintf(&command, "mkdir -p %s", g_tmpfs_mountpt);
[128]391 paranoid_system(command);
[1178]392 mr_free(command);
[128]393 rdsiz_MB = PPCFG_RAMDISK_SIZE + g_tape_buffer_size_MB;
[1]394#ifdef __FreeBSD__
[1178]395 mr_asprintf(&tmp,
[128]396 call_program_and_get_last_line_of_output
397 ("vmstat | tail -1 | tr -s ' ' | cut -d' ' -f6"));
398 avm += atol(tmp);
[1178]399 mr_free(tmp);
400 mr_asprintf(&tmp,
[128]401 call_program_and_get_last_line_of_output
402 ("swapinfo | grep -v Device | tr -s ' ' | cut -d' ' -f4 | tr '\n' '+' | sed 's/+$//' | bc"));
403 avm += atol(tmp);
[1178]404 mr_free(tmp);
405 mr_asprintf(&command, "mdmfs -s %d%c md9 %s", rdsiz_MB, 'm',
406 g_tmpfs_mountpt);
[1]407#else
[1178]408 mr_asprintf(&tmp,
[128]409 call_program_and_get_last_line_of_output
[911]410 ("free | grep ':' | tr -s ' ' '\t' | cut -f2 | head -n1"));
[128]411 avm += atol(tmp);
[1178]412 mr_free(tmp);
413 mr_asprintf(&command, "mount /dev/shm -t tmpfs %s -o size=%d%c",
[128]414 g_tmpfs_mountpt, rdsiz_MB, 'm');
415 run_program_and_log_output("cat /proc/cpuinfo", 5);
[1178]416 /* BERLIOS: rpm is not necessarily there ! */
[128]417 run_program_and_log_output
418 ("rpm -q newt newt-devel slang slang-devel ncurses ncurses-devel gcc",
419 5);
[1]420#endif
[128]421 if (avm / 1024 > rdsiz_MB * 3) {
422 if (run_program_and_log_output(command, 5)) {
423 g_tmpfs_mountpt[0] = '\0';
424 log_it("Failed to mount tmpfs");
425 } else {
426 log_it("Tmpfs mounted OK - %d MB", rdsiz_MB);
427 }
428 } else {
429 g_tmpfs_mountpt[0] = '\0';
430 log_it("It doesn't seem you have enough swap to use tmpfs. Fine.");
[1]431 }
[1178]432 mr_free(command);
[128]433
[1592]434// CD-R or CD-RW or DVD
[128]435 if (bkpinfo->backup_media_type == cdrw
[1592]436 || bkpinfo->backup_media_type == dvd
437 || bkpinfo->backup_media_type == iso
438 || bkpinfo->backup_media_type == nfs
[128]439 || bkpinfo->backup_media_type == cdr) {
[1609]440 if (!bkpinfo->manual_tray) {
[1178]441 mr_strcat(extra_cdrom_params, "-waiti ");
[128]442 }
443 if (bkpinfo->backup_media_type == cdrw) {
[1178]444 mr_strcat(extra_cdrom_params, "blank=fast ");
[128]445 }
446 if (bkpinfo->nonbootable_backup) {
[1592]447 mr_asprintf(&mondo_mkisofs_sz, "%s -no-boot", mr_conf->iso_creation_opt);
[128]448 } else if
[1]449#ifdef __FreeBSD__
[128]450 (TRUE)
[1]451#else
[128]452 (bkpinfo->make_cd_use_lilo)
[1]453#endif
454#ifdef __IA64__
455 {
[1592]456 mr_asprintf(&mondo_mkisofs_sz, "%s -b images/mindi-boot.2880.img -c boot.cat", mr_conf->iso_creation_opt);
[1]457 }
[128]458#else
[1]459 {
[1592]460 mr_asprintf(&mondo_mkisofs_sz, "%s -b isolinux.bin -c boot.cat", mr_conf->iso_creation_opt);
[1]461 }
[128]462#endif
[1592]463 else {
464 mr_asprintf(&mondo_mkisofs_sz, "%s -b isolinux.bin -c boot.cat", mr_conf->iso_creation_opt);
[128]465 }
[1609]466 if (bkpinfo->manual_tray) {
[1594]467 if (strstr(mr_conf->iso_burning_cmd,"growisofs") != NULL) {
[1592]468 fatal_error("Unable to use manual CD tray with growisofs");
469 }
[163]470 if (bkpinfo->call_before_iso[0] == '\0') {
[1592]471 sprintf(bkpinfo->call_before_iso,
[1670]472 "%s %s -o %s/"MONDO_TMPISOS" . 2>> %s",
[1592]473 mr_conf->iso_creation_cmd,mondo_mkisofs_sz, bkpinfo->tmpdir, MONDO_LOGFILE);
[1178]474 } else {
475 mr_asprintf(&call_before_iso_user, bkpinfo->call_before_iso);
[163]476 sprintf (bkpinfo->call_before_iso,
[1670]477 "(%s %s -o %s/"MONDO_TMPISOS" . 2>> %s ; %s )",
[1592]478 mr_conf->iso_creation_cmd,mondo_mkisofs_sz, bkpinfo->tmpdir, MONDO_LOGFILE, call_before_iso_user);
[1178]479 mr_free(call_before_iso_user);
[163]480 }
481 log_it("bkpinfo->call_before_iso = %s", bkpinfo->call_before_iso);
[128]482 sprintf(bkpinfo->call_make_iso,
[1670]483 "%s %s %s dev=%s speed=%d %s/"MONDO_TMPISOS,
[1594]484 mr_conf->iso_burning_cmd,
[1592]485 extra_cdrom_params,
[1594]486 mr_conf->iso_burning_opt,
487 bkpinfo->iso_burning_dev,
488 bkpinfo->writer_speed,
[1592]489 bkpinfo->tmpdir);
[128]490 } else {
[1594]491 if (bkpinfo->backup_media_type == iso) {
492 sprintf(bkpinfo->call_make_iso,
493 "%s %s . 2>> %s",
494 mr_conf->iso_creation_cmd,
495 mondo_mkisofs_sz,
496 MONDO_LOGFILE);
497 } else {
498 if (strstr(mr_conf->iso_burning_cmd,"growisofs") != NULL) {
499 if (getenv ("SUDO_COMMAND")) {
500 mr_asprintf(&command, "strings %s | grep -c SUDO_COMMAND", mr_conf->iso_burning_cmd);
501 if (!strcmp(call_program_and_get_last_line_of_output(command), "1")) {
502 fatal_error("Can't write DVDs as sudo because growisofs doesn't support this - see the growisofs manpage for details.");
503 }
504 mr_free(command);
[1592]505 }
[1594]506 sprintf(bkpinfo->call_make_iso,
507 "%s %s %s %s -Z %s -speed=%d . 2>> %s",
508 mr_conf->iso_burning_cmd,
509 mondo_mkisofs_sz,
510 extra_cdrom_params,
511 mr_conf->iso_burning_opt,
512 bkpinfo->iso_burning_dev,
513 bkpinfo->writer_speed,
514 MONDO_LOGFILE);
515 } else {
516 sprintf(bkpinfo->call_make_iso,
517 "%s %s . 2>> %s | %s %s %s dev=%s speed=%d -",
518 mr_conf->iso_creation_cmd,
519 mondo_mkisofs_sz,
520 MONDO_LOGFILE,
521 mr_conf->iso_burning_cmd,
522 extra_cdrom_params,
523 mr_conf->iso_burning_opt,
524 bkpinfo->iso_burning_dev,
525 bkpinfo->writer_speed);
[1592]526 }
[1594]527 mr_free(mondo_mkisofs_sz);
528 mr_free(extra_cdrom_params);
[1592]529 }
[128]530 }
531 } // end of CD code
[1]532
[128]533 if (bkpinfo->backup_media_type == iso) {
534
[1]535/* Patch by Conor Daly <conor.daly@met.ie>
536 * 23-june-2004
537 * Break up isodir into iso_mnt and iso_path
538 * These will be used along with iso-dev at restore time
539 * to locate the ISOs where ever they're mounted
540 */
541
[128]542 log_it("isodir = %s", bkpinfo->isodir);
[1178]543 mr_asprintf(&command, "df -P %s | tail -n1 | cut -d' ' -f1",
544 bkpinfo->isodir);
[128]545 log_it("command = %s", command);
[1178]546 mr_asprintf(&iso_dev, call_program_and_get_last_line_of_output(command));
547 mr_free(command);
[1087]548 log_it("res of it = %s", iso_dev);
[1]549
[1087]550 fd1 = mr_fopen(MONDORESTORECFG, "a");
[1158]551 mr_fprintf(fd1, "iso-dev=%s\n", iso_dev);
[1087]552
[1178]553 mr_asprintf(&command, "mount | grep -w %s | tail -n1 | cut -d' ' -f3",
554 iso_dev);
555 mr_free(iso_dev);
556
[128]557 log_it("command = %s", command);
[1178]558 mr_asprintf(&iso_mnt,call_program_and_get_last_line_of_output(command));
559 mr_free(command);
[1087]560
561 log_it("res of it = %s", iso_mnt);
[1158]562 mr_fprintf(fd1, "iso-mnt=%s\n", iso_mnt);
[128]563 log_it("isomnt: %s, %d", iso_mnt, strlen(iso_mnt));
[1087]564
[1178]565 mr_asprintf(&iso_tmp, "%s", bkpinfo->isodir);
566 if (strlen(iso_tmp) >= strlen(iso_mnt)) {
567 mr_asprintf(&iso_path, "%s", iso_tmp + strlen(iso_mnt));
[128]568 } else {
[1178]569 mr_asprintf(&iso_path, "");
[128]570 }
[1178]571 mr_free(iso_tmp);
572 mr_free(iso_mnt);
573
[128]574 log_it("isodir: %s", iso_path);
[1158]575 mr_fprintf(fd1, "isodir=%s\n", iso_path);
[1178]576 mr_free(iso_path);
[1087]577
[148]578 log_it("iso-prefix: %s", bkpinfo->prefix);
[1158]579 mr_fprintf(fd1, "iso-prefix=%s\n", bkpinfo->prefix);
[1]580
[1095]581 mr_fclose(fd1);
[128]582 } // end of iso code
[1]583
[128]584 if (bkpinfo->backup_media_type == nfs) {
[1178]585 mr_asprintf(&hostname, bkpinfo->nfs_mount);
[128]586 colon = strchr(hostname, ':');
587 if (!colon) {
588 log_it("nfs mount doesn't have a colon in it");
589 retval++;
590 } else {
591 struct hostent *hent;
[1]592
[128]593 *colon = '\0';
594 hent = gethostbyname(hostname);
595 if (!hent) {
596 log_it("Can't resolve NFS mount (%s): %s", hostname,
597 hstrerror(h_errno));
598 retval++;
599 } else {
[1178]600 mr_asprintf(&ip_address, "%s%s", inet_ntoa((struct in_addr)
601 *((struct in_addr *) hent->h_addr)),
602 strchr(bkpinfo->nfs_mount, ':'));
[128]603 strcpy(bkpinfo->nfs_mount, ip_address);
[1178]604 mr_free(ip_address);
[128]605 }
606 }
[1663]607 store_nfs_config();
[1178]608 mr_free(hostname);
[128]609 }
[1]610
[128]611 log_it("Finished processing incoming params");
612 if (retval) {
613 fprintf(stderr, "Type 'man mondoarchive' for help.\n");
614 }
[1178]615
[128]616 if (strlen(bkpinfo->tmpdir) < 2 || strlen(bkpinfo->scratchdir) < 2) {
617 log_it("tmpdir or scratchdir are blank/missing");
618 retval++;
619 }
620 if (bkpinfo->include_paths[0] == '\0') {
621 // fatal_error ("Why no backup path?");
622 strcpy(bkpinfo->include_paths, "/");
623 }
624 chmod(bkpinfo->scratchdir, 0700);
625 g_backup_media_type = bkpinfo->backup_media_type;
[1178]626 g_backup_media_string = bkpinfo->backup_media_string;
[128]627 return (retval);
[1]628}
629
630
631/**
632 * Do some miscellaneous setup tasks to be performed before filling @c bkpinfo.
633 * Seeds the random-number generator, loads important modules, checks the sanity
634 * of the user's Linux distribution, and deletes logfile.
635 * @param bkpinfo The backup information structure. Will be initialized.
636 * @return number of errors (0 for success)
637 */
[1663]638int pre_param_configuration()
[1]639{
[128]640 int res = 0;
[1663]641 char *tmp = NULL;
[1]642
[128]643 make_hole_for_dir(MNT_CDROM);
644 assert(bkpinfo != NULL);
645 srandom((unsigned long) (time(NULL)));
646 insmod_crucial_modules();
647 if (bkpinfo->disaster_recovery) {
648 if (!does_nonMS_partition_exist()) {
649 fatal_error
650 ("I am in disaster recovery mode\nPlease don't run mondoarchive.");
651 }
652 }
[1]653
[1770]654 asprintf(&tmp,"rm -Rf %s/changed.files*",MONDO_CACHE);
[1663]655 run_program_and_log_output(tmp, FALSE);
656 paranoid_free(tmp);
[128]657 res += some_basic_system_sanity_checks();
658 if (res) {
659 log_it("Your distribution did not pass Mondo's sanity test.");
660 }
661 g_current_media_number = 1;
662 bkpinfo->postnuke_tarball[0] = bkpinfo->nfs_mount[0] = '\0';
663 return (res);
[1]664}
665
[1663]666void setup_tmpdir(char *path) {
667
668 char *tmp = NULL;
669 char *p = NULL;
[1]670
[1663]671 if (bkpinfo->tmpdir != NULL) {
672 /* purging a potential old tmpdir */
673 asprintf(&tmp,"rm -Rf %s",bkpinfo->tmpdir);
674 system(tmp);
675 paranoid_free(tmp);
676 }
677
678 if (path != NULL) {
679 asprintf(&tmp, "%s/mondo.tmp.XXXXXX", path);
680 } else if (getenv("TMPDIR")) {
681 asprintf(&tmp, "%s/mondo.tmp.XXXXXX", getenv("TMPDIR"));
682 } else if (getenv("TMP")) {
683 asprintf(&tmp, "%s/mondo.tmp.XXXXXX", getenv("TMP"));
684 } else {
685 asprintf(&tmp, "/tmp/mondo.tmp.XXXXXX");
686 }
687 p = mkdtemp(tmp);
688 if (p == NULL) {
689 log_it("Failed to create global tmp directory %s for Mondo.",tmp);
690 finish(-1);
691 }
692 strcpy(bkpinfo->tmpdir,p);
693 paranoid_free(tmp);
[1]694
[1663]695 //sprintf(bkpinfo->tmpdir, "%s/tmpfs/mondo.tmp.%d", "/tmp", (int) (random() % 32768)); // for mondorestore
696}
[1]697
[1663]698
[1]699/**
700 * Reset all fields of the backup information structure to a sensible default.
701 * @param bkpinfo The @c bkpinfo to reset.
702 */
[1663]703void reset_bkpinfo()
[1]704{
[1594]705 char *tmp = NULL;
706
[1107]707 mr_msg(1, "Hi");
[1178]708
[128]709 assert(bkpinfo != NULL);
[1178]710 /* BERLIOS : Useless ?? */
[128]711 memset((void *) bkpinfo, 0, sizeof(struct s_bkpinfo));
[1178]712
[1663]713 /* special case for tmpdir as used eveywhere after */
714 setup_tmpdir(NULL);
715
[1609]716 bkpinfo->manual_tray = mr_conf->manual_tray;
[1638]717 bkpinfo->internal_tape_block_size = mr_conf->internal_tape_blocksize;
[1769]718 bkpinfo->compression_level = mr_conf->compression_level;
719 mr_asprintf(&tmp,mr_conf->compression_suffix);
720 bkpinfo->compression_suffix = tmp;
721 mr_asprintf(&tmp,mr_conf->compression_tool);
722 bkpinfo->compression_tool = tmp;
[1594]723 mr_asprintf(&tmp,mr_conf->media_device);
724 bkpinfo->media_device = tmp;
725 mr_asprintf(&tmp,mr_conf->iso_burning_dev);
726 bkpinfo->iso_burning_dev = tmp;
727
728 bkpinfo->media_size = mr_conf->iso_burning_speed;
[1669]729 mr_asprintf(&tmp,mr_conf->kernel);
730 bkpinfo->kernel_path = tmp;
731
[1671]732 if (strcmp(mr_conf->boot_loader,"LILO") == 0) {
733 bkpinfo->boot_loader = 'L';
734 } else if (strcmp(mr_conf->boot_loader,"GRUB") == 0) {
735 bkpinfo->boot_loader = 'G';
736 } else if (strcmp(mr_conf->boot_loader,"ELILO") == 0) {
737 bkpinfo->boot_loader = 'E';
738 } else if (strcmp(mr_conf->boot_loader,"RAW") == 0) {
739 bkpinfo->boot_loader = 'R';
740 } else if (strcmp(mr_conf->boot_loader,"DD") == 0) {
741 bkpinfo->boot_loader = 'D';
742 } else if (strcmp(mr_conf->boot_loader,"BOOT0") == 0) {
743 bkpinfo->boot_loader = 'B';
744 } else if (strcmp(mr_conf->boot_loader,"NATIVE") == 0) {
745 bkpinfo->boot_loader = '\0';
746 } else{
747 mr_msg("Boot Loader %s unknown\n",mr_conf->boot_loader);
748 fatal_error("Unknown Boot Loader in conf file");
749 }
[128]750 bkpinfo->boot_device[0] = '\0';
751 bkpinfo->restore_path[0] = '\0';
752 bkpinfo->do_not_compress_these[0] = '\0';
753 bkpinfo->verify_data = FALSE;
754 bkpinfo->backup_data = FALSE;
755 bkpinfo->restore_data = FALSE;
756 bkpinfo->disaster_recovery =
757 (am_I_in_disaster_recovery_mode()? TRUE : FALSE);
758 if (bkpinfo->disaster_recovery) {
759 strcpy(bkpinfo->isodir, "/");
760 } else {
[1178]761 strcpy(bkpinfo->isodir, "/var/cache/mondo/iso");
[128]762 }
[1628]763 mr_asprintf(&tmp,mr_conf->prefix);
764 bkpinfo->prefix = tmp;
[1]765
[128]766 bkpinfo->scratchdir[0] = '\0';
767 bkpinfo->make_filelist = TRUE; // unless -J supplied to mondoarchive
768 bkpinfo->optimal_set_size = 0;
769 bkpinfo->backup_media_type = none;
[1178]770 bkpinfo->backup_media_string[0] = '\0';
[128]771 strcpy(bkpinfo->include_paths, "/");
772 bkpinfo->call_before_iso[0] = '\0';
773 bkpinfo->call_make_iso[0] = '\0';
774 bkpinfo->call_after_iso[0] = '\0';
775 bkpinfo->image_devs[0] = '\0';
776 bkpinfo->postnuke_tarball[0] = '\0';
777 bkpinfo->nfs_mount[0] = '\0';
778 bkpinfo->nfs_remote_dir[0] = '\0';
779 bkpinfo->wipe_media_first = FALSE;
780 bkpinfo->differential = FALSE;
[1594]781 bkpinfo->writer_speed = mr_conf->iso_burning_speed;
[1]782}
783
784
785/**
786 * Get the remaining free space (in MB) on @p partition.
787 * @param partition The partition to check free space on (either a device or a mountpoint).
788 * @return The free space on @p partition, in MB.
789 */
[128]790long free_space_on_given_partition(char *partition)
[1]791{
[1178]792 char *command = NULL;
793 char *out_sz = NULL;
794 long res = 0L;
[1]795
[128]796 assert_string_is_neither_NULL_nor_zerolength(partition);
[1]797
[1770]798 mr_asprintf(&command, "df -m -P %s 1> /dev/null 2> /dev/null", partition);
[128]799 if (system(command)) {
800 return (-1);
801 } // partition does not exist
[1178]802 mr_free(command);
803
804 mr_asprintf(&command, "df -m -P %s | tail -n1 | tr -s ' ' '\t' | cut -f4",
[128]805 partition);
[1178]806 mr_asprintf(&out_sz, call_program_and_get_last_line_of_output(command));
807 mr_free(command);
808
[128]809 if (strlen(out_sz) == 0) {
810 return (-1);
811 } // error within df, probably
812 res = atol(out_sz);
[1178]813 mr_free(out_sz);
[128]814 return (res);
[1]815}
816
817
818/**
819 * Check the user's system for sanity. Checks performed:
820 * - make sure user has enough RAM (32mb required, 64mb recommended)
821 * - make sure user has enough free space in @c /
822 * - check kernel for ramdisk support
[1592]823 * - make sure afio, cdrecord, bzip2, awk, md5sum, strings, mindi, and buffer exist
[1]824 * - make sure CD-ROM is unmounted
825 * - make sure /etc/modules.conf exists
826 * - make sure user's mountlist is OK by running <tt>mindi --makemountlist</tt>
827 *
828 * @return number of problems with the user's setup (0 for success)
829 */
[128]830int some_basic_system_sanity_checks()
[1]831{
832
[128]833 /*@ buffers ************ */
[1178]834 char *tmp = NULL;
[1]835
[128]836 /*@ int's *************** */
837 int retval = 0;
[1]838
[1178]839 mvaddstr_and_log_it(g_currentY, 0,
840 "Checking sanity of your Linux distribution");
[1]841#ifndef __FreeBSD__
[1770]842 if (system("which mkfs.vfat 2> /dev/null 1> /dev/null")
843 && !system("which mkfs.msdos 2> /dev/null 1> /dev/null")) {
[128]844 log_it
845 ("OK, you've got mkfs.msdos but not mkfs.vfat; time for the fairy to wave her magic wand...");
846 run_program_and_log_output
847 ("ln -sf `which mkfs.msdos` /sbin/mkfs.vfat", FALSE);
848 }
[1178]849 mr_asprintf(&tmp,
[128]850 call_program_and_get_last_line_of_output
851 ("free | grep Mem | head -n1 | tr -s ' ' '\t' | cut -f2"));
852 if (atol(tmp) < 35000) {
853 retval++;
[1178]854 log_to_screen(_("You must have at least 32MB of RAM to use Mondo."));
[128]855 }
856 if (atol(tmp) < 66000) {
857 log_to_screen
[1178]858 (_("WARNING! You have very little RAM. Please upgrade to 64MB or more."));
[128]859 }
[1178]860 mr_free(tmp);
[1]861#endif
862
[1438]863 Lres = free_space_on_given_partition(MONDO_CACHE);
[128]864 log_it("Free space on given partition = %ld MB", Lres);
[1]865
[128]866 if (Lres < 50) {
[1438]867 fatal_error("Your "MONDO_CACHE" partition has <50MB free. Please adjust your partition table to something saner or make "MONDO_CACHE" a symbolic link");
[128]868 }
[1]869
[128]870 if (system("which " MKE2FS_OR_NEWFS " > /dev/null 2> /dev/null")) {
871 retval++;
872 log_to_screen
873 ("Unable to find " MKE2FS_OR_NEWFS " in system path.");
874 fatal_error
875 ("Please use \"su -\", not \"su\" to become root. OK? ...and please don't e-mail the mailing list or me about this. Just read the message. :)");
876 }
[1]877#ifndef __FreeBSD__
[128]878 if (run_program_and_log_output
[273]879 ("grep ramdisk /proc/devices", FALSE)) {
[128]880 if (!ask_me_yes_or_no
[1178]881 (_("Your kernel has no ramdisk support. That's mind-numbingly stupid but I'll allow it if you're planning to use a failsafe kernel. Are you?")))
[128]882 {
883 // retval++;
884 log_to_screen
[1178]885 (_("It looks as if your kernel lacks ramdisk and initrd support."));
[128]886 log_to_screen
[1178]887 (_("I'll allow you to proceed but FYI, if I'm right, your kernel is broken."));
[128]888 }
889 }
[1]890#endif
[1594]891 retval += whine_if_not_found(mr_conf->iso_creation_cmd);
892 retval += whine_if_not_found(mr_conf->iso_burning_cmd);
[128]893 retval += whine_if_not_found(MKE2FS_OR_NEWFS);
[1769]894 retval += whine_if_not_found(mr_conf->compresstion_tool);
[998]895 retval += whine_if_not_found("gzip");
[128]896 retval += whine_if_not_found("awk");
897 retval += whine_if_not_found("md5sum");
898 retval += whine_if_not_found("strings");
899 retval += whine_if_not_found("mindi");
900 retval += whine_if_not_found("buffer");
[1]901
[128]902 // abort if Windows partition but no ms-sys and parted
903 if (!run_program_and_log_output
[680]904 ("mount | grep -w vfat | grep -vE \"/dev/fd|nexdisk\"", 0)
[128]905 ||
906 !run_program_and_log_output
[680]907 ("mount | grep -w dos | grep -vE \"/dev/fd|nexdisk\"", 0)) {
[1178]908 log_to_screen(_("I think you have a Windows 9x partition."));
[128]909 retval += whine_if_not_found("parted");
[1]910#ifndef __IA64__
[128]911 /* IA64 always has one vfat partition for EFI even without Windows */
912 // retval +=
913 if (!find_home_of_exe("ms-sys")) {
[541]914 log_to_screen("Please install ms-sys just in case.");
[128]915 }
916#endif
[1]917 }
918
[128]919 if (!find_home_of_exe("cmp")) {
920 if (!find_home_of_exe("true")) {
921 whine_if_not_found("cmp");
922 } else {
923 log_to_screen
[541]924 ("Your system lacks the 'cmp' binary. I'll create a dummy cmp for you.");
[128]925 if (run_program_and_log_output
926 ("cp -f `which true` /usr/bin/cmp", 0)) {
927 fatal_error("Failed to create dummy 'cmp' file.");
928 }
929 }
[1]930 }
[128]931 run_program_and_log_output
932 ("umount `mount | grep cdr | cut -d' ' -f3 | tr '\n' ' '`", 5);
[1178]933 mr_asprintf(&tmp,
[128]934 call_program_and_get_last_line_of_output
935 ("mount | grep -E \"cdr(om|w)\""));
936 if (strcmp("", tmp)) {
937 if (strstr(tmp, "autofs")) {
938 log_to_screen
[1178]939 (_("Your CD-ROM is mounted via autofs. I therefore cannot tell"));
[128]940 log_to_screen
[1178]941 (_("if a CD actually is inserted. If a CD is inserted, please"));
942 log_to_screen(_("eject it. Thank you."));
[128]943 log_it
944 ("Ignoring autofs CD-ROM 'mount' since we hope nothing's in it.");
945 } else
946 if (run_program_and_log_output("uname -a | grep Knoppix", 5)) {
947 retval++;
948 fatal_error
949 ("Your CD-ROM drive is mounted. Please unmount it.");
950 }
951 }
[1178]952 mr_free(tmp);
[1]953#ifndef __FreeBSD__
[128]954 if (!does_file_exist("/etc/modules.conf")) {
955 if (does_file_exist("/etc/conf.modules")) {
956 log_it("Linking /etc/modules.conf to /etc/conf.modules");
957 run_program_and_log_output
958 ("ln -sf /etc/conf.modules /etc/modules.conf", 5);
959 } else if (does_file_exist("/etc/modprobe.d")) {
960 log_it
961 ("Directory /etc/modprobe.d found. mindi will use its contents.");
962 } else if (does_file_exist("/etc/modprobe.conf")) {
963 log_it("Linking /etc/modules.conf to /etc/modprobe.conf");
964 run_program_and_log_output
965 ("ln -sf /etc/modprobe.conf /etc/modules.conf", 5);
966 } else {
967 retval++;
968 log_to_screen
[1178]969 (_("Please find out what happened to /etc/modules.conf"));
[128]970 }
[1]971 }
972#endif
973
[128]974 run_program_and_log_output("cat /etc/fstab", 5);
[1]975#ifdef __FreeBSD__
[128]976 run_program_and_log_output("vinum printconfig", 5);
[1]977#else
[128]978 run_program_and_log_output("cat /etc/raidtab", 5);
[1]979#endif
980
[128]981 if (run_program_and_log_output("mindi -V", 1)) {
[1178]982 log_to_screen(_("Could not ascertain mindi's version number."));
[128]983 log_to_screen
[1178]984 (_("You have not installed Mondo and/or Mindi properly."));
985 log_to_screen(_("Please uninstall and reinstall them both."));
[128]986 fatal_error("Please reinstall Mondo and Mindi.");
987 }
[1663]988 mr_asprintf(&tmp, "mindi --makemountlist %s/mountlist.txt.test", bkpinfo->tmpdir);
989 if (run_program_and_log_output(tmp, 5)) {
990 log_to_screen(tmp);
[128]991 log_to_screen
[1663]992 (_("failed for some reason."));
[128]993 log_to_screen
[1178]994 (_("Please run that command by hand and examine /var/log/mindi.log"));
[128]995 log_to_screen
[1178]996 (_("for more information. Perhaps your /etc/fstab file is insane."));
[128]997 log_to_screen
[1178]998 (_("Perhaps Mindi's MakeMountlist() subroutine has a bug. We'll see."));
[128]999 retval++;
1000 }
[1663]1001 mr_free(tmp);
[1]1002
[196]1003 if (!run_program_and_log_output("parted2fdisk -l | grep -i raid", 1)
[128]1004 && !does_file_exist("/etc/raidtab")) {
1005 log_to_screen
[1252]1006 (_("You have RAID partitions but no /etc/raidtab - creating one from "MDSTAT_FILE));
[558]1007 create_raidtab_from_mdstat("/etc/raidtab");
[128]1008 }
1009
1010 if (retval) {
[1178]1011 mvaddstr_and_log_it(g_currentY++, 74, _("Failed."));
[128]1012 } else {
[1178]1013 mvaddstr_and_log_it(g_currentY++, 74, _("Done."));
[128]1014 }
1015 return (retval);
[1]1016}
1017
1018/**
1019 * Retrieve the line containing @p label from the config file.
1020 * @param config_file The file to read from, usually @c /tmp/mondo-restore.cfg.
1021 * @param label What to read from the file.
1022 * @param value Where to put it.
1023 * @return 0 for success, 1 for failure.
1024 */
[128]1025int read_cfg_var(char *config_file, char *label, char *value)
[1]1026{
[128]1027 /*@ buffer ****************************************************** */
[1178]1028 char *command = NULL;
1029 char *tmp = NULL;
[1]1030
[128]1031 /*@ end vars *************************************************** */
[1]1032
[128]1033 assert_string_is_neither_NULL_nor_zerolength(config_file);
1034 assert_string_is_neither_NULL_nor_zerolength(label);
[1178]1035
[128]1036 if (!does_file_exist(config_file)) {
[1178]1037 mr_asprintf(&tmp, "(read_cfg_var) Cannot find %s config file",
1038 config_file);
[128]1039 log_to_screen(tmp);
[1178]1040 mr_free(tmp);
[128]1041 value[0] = '\0';
1042 return (1);
[1178]1043 /* } BERLIOS: Useless:
1044 else if (strstr(value, "/dev/") && strstr(value, "t0")
[128]1045 && !strcmp(label, "media-dev")) {
[1107]1046 mr_msg(2, "FYI, I shan't read new value for %s - already got %s",
[128]1047 label, value);
1048 return (0);
[1178]1049 */ } else {
1050 mr_asprintf(&command, "grep '%s .*' %s| cut -d'=' -f2,3,4,5",
[148]1051 label, config_file);
[128]1052 strcpy(value, call_program_and_get_last_line_of_output(command));
[1178]1053 mr_free(command);
[128]1054 if (strlen(value) == 0) {
1055 return (1);
1056 } else {
1057 return (0);
1058 }
[1]1059 }
1060}
1061
1062
1063/**
1064 * Remount @c supermount if it was unmounted earlier.
1065 */
1066void remount_supermounts_if_necessary()
1067{
[128]1068 if (g_remount_cdrom_at_end) {
1069 run_program_and_log_output("mount " MNT_CDROM, FALSE);
1070 }
[1]1071}
1072
1073/**
1074 * Unmount @c supermount if it's mounted.
1075 */
1076void unmount_supermounts_if_necessary()
1077{
[128]1078 if (run_program_and_log_output
1079 ("mount | grep cdrom | grep super", FALSE) == 0) {
1080 g_remount_cdrom_at_end = TRUE;
1081 run_program_and_log_output("umount " MNT_CDROM, FALSE);
1082 }
[1]1083}
1084
1085/**
1086 * If this is a distribution like Gentoo that doesn't keep /boot mounted, mount it.
1087 */
1088void mount_boot_if_necessary()
1089{
[1178]1090 char *tmp = NULL;
1091 char *tmp1 = NULL;
1092 char *command = NULL;
[1]1093
[1107]1094 mr_msg(1, "Started sub");
1095 mr_msg(4, "About to set g_boot_mountpt[0] to '\\0'");
[128]1096 g_boot_mountpt[0] = '\0';
[1178]1097 mr_msg(4, "Done. Great. Setting command to something");
1098 mr_asprintf(&command,
1099 "grep -v ':' /etc/fstab | grep -vE '^#.*$' | grep -E \"[ ]/boot[ ]\" | tr -s ' ' '\t' | cut -f1 | head -n1");
[1107]1100 mr_msg(4, "Cool. Command = '%s'", command);
[1178]1101 mr_asprintf(&tmp, call_program_and_get_last_line_of_output(command));
1102 mr_free(command);
1103
[1107]1104 mr_msg(4, "tmp = '%s'", tmp);
[128]1105 if (tmp[0]) {
1106 log_it("/boot is at %s according to /etc/fstab", tmp);
[1373]1107 if ((strstr(tmp, "LABEL=") || strstr(tmp,"UUID="))) {
[128]1108 if (!run_program_and_log_output("mount /boot", 5)) {
1109 strcpy(g_boot_mountpt, "/boot");
[1107]1110 mr_msg(1, "Mounted /boot");
[128]1111 } else {
[1373]1112 log_it("...ignored cos it's a label or uuid :-)");
[128]1113 }
1114 } else {
[1178]1115 mr_asprintf(&command, "mount | grep -E '^%s'", tmp);
[1107]1116 mr_msg(3, "command = %s", command);
[128]1117 if (run_program_and_log_output(command, 5)) {
1118 strcpy(g_boot_mountpt, tmp);
[1178]1119 mr_asprintf(&tmp1,
[128]1120 "%s (your /boot partition) is not mounted. I'll mount it before backing up",
1121 g_boot_mountpt);
[1178]1122 log_it(tmp1);
1123 mr_free(tmp1);
1124
1125 mr_asprintf(&tmp1, "mount %s", g_boot_mountpt);
1126 if (run_program_and_log_output(tmp1, 5)) {
[128]1127 g_boot_mountpt[0] = '\0';
[1107]1128 mr_msg(1, "Plan B");
[128]1129 if (!run_program_and_log_output("mount /boot", 5)) {
1130 strcpy(g_boot_mountpt, "/boot");
[1107]1131 mr_msg(1, "Plan B worked");
[128]1132 } else {
[1107]1133 mr_msg(1,
[128]1134 "Plan B failed. Unable to mount /boot for backup purposes. This probably means /boot is mounted already, or doesn't have its own partition.");
1135 }
1136 }
[1178]1137 mr_free(tmp1);
[128]1138 }
[1178]1139 mr_free(command);
[128]1140 }
[1]1141 }
[1178]1142 mr_free(tmp);
[1107]1143 mr_msg(1, "Ended sub");
[1]1144}
1145
1146
1147/**
1148 * If we mounted /boot earlier, unmount it.
1149 */
1150void unmount_boot_if_necessary()
1151{
[1178]1152 char *tmp;
[1]1153
[1107]1154 mr_msg(3, "starting");
[128]1155 if (g_boot_mountpt[0]) {
[1178]1156 mr_asprintf(&tmp, "umount %s", g_boot_mountpt);
[128]1157 if (run_program_and_log_output(tmp, 5)) {
1158 log_it("WARNING - unable to unmount /boot");
1159 }
[1178]1160 mr_free(tmp);
[128]1161 }
[1107]1162 mr_msg(3, "leaving");
[1]1163}
1164
1165
1166/**
1167 * Write a line to a configuration file. Writes a line of the form,
1168 * @c label @c value.
1169 * @param config_file The file to write to. Usually @c mondo-restore.cfg.
1170 * @param label What to call this bit of data you're writing.
1171 * @param value The bit of data you're writing.
1172 * @return 0 for success, 1 for failure.
1173 */
[128]1174int write_cfg_var(char *config_file, char *label, char *value)
[1]1175{
[128]1176 /*@ buffers ***************************************************** */
[1178]1177 char *command = NULL;
1178 char *tempfile = NULL;
1179 char *tmp = NULL;
[1]1180
1181
[128]1182 /*@ end vars *************************************************** */
1183 assert_string_is_neither_NULL_nor_zerolength(config_file);
1184 assert_string_is_neither_NULL_nor_zerolength(label);
1185 assert(value != NULL);
[1178]1186
[128]1187 if (!does_file_exist(config_file)) {
[1178]1188 mr_asprintf(&tmp, "(write_cfg_file) Cannot find %s config file",
1189 config_file);
[128]1190 log_to_screen(tmp);
[1178]1191 mr_free(tmp);
[128]1192 return (1);
1193 }
[1663]1194 mr_asprintf(&tempfile, "%s/mojo-jojo.blah", bkpinfo->tmpdir);
[128]1195 if (does_file_exist(config_file)) {
[1178]1196 mr_asprintf(&command, "grep -vE '^%s .*$' %s > %s",
[148]1197 label, config_file, tempfile);
[128]1198 paranoid_system(command);
[1178]1199 mr_free(command);
[128]1200 }
[1178]1201 mr_asprintf(&command, "echo \"%s %s\" >> %s", label, value, tempfile);
[128]1202 paranoid_system(command);
[1178]1203 mr_free(command);
1204
1205 mr_asprintf(&command, "mv -f %s %s", tempfile, config_file);
[128]1206 paranoid_system(command);
[1178]1207 mr_free(command);
[128]1208 unlink(tempfile);
[1178]1209 mr_free(tempfile);
[128]1210 return (0);
[1]1211}
1212
[541]1213
[1]1214/**
1215 * Allocate or free important globals, depending on @p mal.
1216 * @param mal If TRUE, malloc; if FALSE, free.
1217 */
1218void do_libmondo_global_strings_thing(int mal)
1219{
[128]1220 if (mal) {
1221 malloc_string(g_boot_mountpt);
1222 malloc_string(g_tmpfs_mountpt);
1223 malloc_string(g_serial_string);
1224 malloc_string(g_magicdev_command);
1225 } else {
[1080]1226 mr_free(g_boot_mountpt);
1227 mr_free(g_tmpfs_mountpt);
1228 mr_free(g_serial_string);
1229 mr_free(g_magicdev_command);
[128]1230 }
[1]1231}
1232
1233/**
1234 * Allocate important globals.
1235 * @see do_libmondo_global_strings_thing
1236 */
1237void malloc_libmondo_global_strings(void)
1238{
[128]1239 do_libmondo_global_strings_thing(1);
[1]1240}
1241
1242/**
1243 * Free important globals.
1244 * @see do_libmondo_global_strings_thing
1245 */
1246void free_libmondo_global_strings(void)
1247{
[128]1248 do_libmondo_global_strings_thing(0);
[1]1249}
1250
1251
1252
1253/**
1254 * Stop @c magicdev if it's running.
1255 * The command used to start it is saved in @p g_magicdev_command.
1256 */
1257void stop_magicdev_if_necessary()
1258{
[128]1259 strcpy(g_magicdev_command,
1260 call_program_and_get_last_line_of_output
1261 ("ps ax | grep -w magicdev | grep -v grep | tr -s '\t' ' '| cut -d' ' -f6-99"));
1262 if (g_magicdev_command[0]) {
[1107]1263 mr_msg(1, "g_magicdev_command = '%s'", g_magicdev_command);
[128]1264 paranoid_system("killall magicdev");
1265 }
[1]1266}
1267
1268
1269/**
1270 * Restart magicdev if it was stopped.
1271 */
1272void restart_magicdev_if_necessary()
1273{
[1178]1274 char *tmp = NULL;
[128]1275
1276 if (g_magicdev_command && g_magicdev_command[0]) {
[1178]1277 mr_asprintf(&tmp, "%s &", g_magicdev_command);
[128]1278 paranoid_system(tmp);
[1178]1279 mr_free(tmp);
[128]1280 }
[1]1281}
1282
1283/* @} - end of utilityGroup */
Note: See TracBrowser for help on using the repository browser.