source: MondoRescue/trunk/mondo/src/common/libmondo-tools.c@ 914

Last change on this file since 914 was 914, checked in by Bruno Cornec, 18 years ago

merge -r902:913 $SVN_M/branches/stable

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