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

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

Continue on configuration file items (compression)

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