source: MondoRescue/branches/3.3/mondo/src/common/libmondo-tools.c

Last change on this file was 3898, checked in by Bruno Cornec, 7 months ago

create alloc_bkpinfo function and fix reset_bkpinfo proto

  • Property svn:keywords set to Id
File size: 39.7 KB
RevLine 
[2030]1/*
[99]2 $Id: libmondo-tools.c 3898 2024-12-07 02:21:00Z bruno $
[1]3*/
4
5
6/**
7 * @file
8 * Miscellaneous tools that didn't really fit anywhere else.
9 */
10
11#include "my-stuff.h"
[2211]12#include "mr_mem.h"
[3500]13#include "mr_sys.h"
[1]14#include "mondostructures.h"
[3871]15
[541]16#include "libmondo-gui-EXT.h"
[1]17#include "libmondo-files-EXT.h"
18#include "libmondo-fork-EXT.h"
19#include "libmondo-raid-EXT.h"
[1917]20#include "libmondo-devices-EXT.h"
[1]21#include <sys/socket.h>
22#include <netdb.h>
[1653]23#include <stdlib.h>
[1]24#include <netinet/in.h>
25#include <arpa/inet.h>
[1375]26#include <sys/utsname.h>
[1]27
28/*@unused@*/
[99]29//static char cvsid[] = "$Id: libmondo-tools.c 3898 2024-12-07 02:21:00Z bruno $";
[1]30
31extern int g_tape_buffer_size_MB;
32extern bool g_text_mode;
33extern int g_currentY;
34extern int g_current_media_number;
[1316]35extern char *MONDO_LOGFILE;
[1]36
[1645]37/* Reference to global bkpinfo */
38extern struct s_bkpinfo *bkpinfo;
39
[1]40/**
41 * @addtogroup globalGroup
42 * @{
43 */
[3756]44bool g_remount_cdrom_at_end; ///< TRUE if we unmounted the CD-ROM and should remount it when done with the backup.
[1]45double g_kernel_version;
46
47/**
48 * The place where /boot is mounted.
49 */
[128]50char *g_boot_mountpt = NULL;
[1]51
52/**
53 * The location of Mondo's home directory.
54 */
[3790]55extern char *g_mondo_home;
[1]56
57/**
58 * The location where tmpfs is mounted, or "" if it's not mounted.
[2030]59char *g_tmpfs_mountpt = NULL;
[1]60 */
[128]61char *g_magicdev_command = NULL;
[1]62
63/**
64 * The default maximum level to log messages at or below.
65 */
[128]66int g_loglevel = DEFAULT_DEBUG_LEVEL;
[1]67
68/* @} - end of globalGroup */
69
70
71extern pid_t g_buffer_pid;
72extern pid_t g_main_pid;
73
74extern t_bkptype g_backup_media_type;
75
[1917]76/*-----------------------------------------------------------*/
77
78
[1]79/**
80 * @addtogroup utilityGroup
81 * @{
82 */
83/**
84 * Assertion handler. Prints a friendly message to the user,
85 * offering to ignore all, dump core, break to debugger,
86 * exit, or ignore. Intended to be used with an assert() macro.
87 *
88 * @param file The file in which the assertion triggered.
89 * @param function The function (@c __FUNCTION__) in which the assertion triggered.
90 * @param line The line number of the assert() statement.
91 * @param exp The expression that failed (as a string).
92 */
[128]93void _mondo_assert_fail(const char *file,
94 const char *function, int line, const char *exp)
[1]95{
[128]96 static int ignoring_assertions = 0;
97 bool is_valid = TRUE;
[1]98
[3613]99 log_it("ASSERTION FAILED: `%s' at %s:%d in %s", exp, file, line, function);
[128]100 if (ignoring_assertions) {
101 log_it("Well, the user doesn't care...");
102 return;
103 }
104 if (!g_text_mode)
105 newtSuspend();
[541]106 printf("ASSERTION FAILED: `%s'\n", exp);
107 printf("\tat %s:%d in %s\n\n", file, line, function);
108 printf("(I)gnore, ignore (A)ll, (D)ebug, a(B)ort, or (E)xit? ");
[128]109 do {
110 is_valid = TRUE;
111 switch (toupper(getchar())) {
112 case 'A': // ignore (A)ll
113 ignoring_assertions = 1;
114 break;
115 case 'B': // a(B)ort
116 signal(SIGABRT, SIG_DFL); /* prevent SIGABRT handler from running */
117 raise(SIGABRT);
118 break; /* "can't get here" */
119 case 'D': // (D)ebug, aka asm("int 3")
[1]120#ifdef __IA32__
[128]121 __asm__ __volatile__("int $3"); // break to debugger
[1]122#endif
[128]123 break;
124 case 'E': // (E)xit
125 fatal_error("Failed assertion -- see above for details");
126 break; /* "can't get here" */
127 case 'I': // (I)gnore
128 break;
129 /* These next two work as follows:
130 the `default' catches the user's invalid choice and says so;
131 the '\n' catches the newline on the end and prints the prompt again.
132 */
133 case '\n':
[3613]134 printf("(I)gnore, ignore (A)ll, (D)ebug, a(B)ort, or (E)xit? ");
[128]135 break;
136 default:
137 is_valid = FALSE;
[541]138 printf("Invalid choice.\n");
[128]139 break;
140 }
141 } while (!is_valid);
142
143 if (ignoring_assertions) {
144 log_it("Ignoring ALL assertions from now on.");
145 } else {
146 log_it("Ignoring assertion: %s", exp);
[1]147 }
148
[128]149 getchar(); // skip \n
[1]150
[128]151 if (!g_text_mode)
152 newtResume();
[1]153}
154
155/**
156 * Clean's up users' KDE desktops.
157 * @bug Details about this function are unknown.
158 */
159void clean_up_KDE_desktop_if_necessary(void)
160{
[3191]161 char *tmp = NULL;
[1]162
[3191]163 mr_asprintf(tmp, "for i in `find /root /home -maxdepth 2 -type d -name Desktop -maxdepth 2`; do file=$i/.directory; if [ -f \"$file\" ] ; then mv -f $file $file.old ; awk '{if (index($0, \"rootimagesmindi\")) { while (length($0)>2) { getline;} ; } else { print $0;};}' $file.old > $file ; fi ; done");
[128]164 run_program_and_log_output(tmp, 5);
[3191]165 mr_free(tmp);
[1]166}
167
168
169/**
170 * Locate mondoarchive's home directory. Searches in /usr/local/mondo, /usr/share/mondo,
171 * /usr/local/share/mondo, /opt, or if all else fails, search /usr.
172 *
173 * @param home_sz String to store the home directory ("" if it could not be found).
174 * @return 0 for success, nonzero for failure.
175 */
176int find_and_store_mondoarchives_home(char *home_sz)
177{
[128]178 assert(home_sz != NULL);
[424]179 strcpy(home_sz, MONDO_SHARE);
[182]180 return (0);
[1]181}
182
183
[1375]184char *get_architecture(void) {
[1]185#ifdef __IA32__
[1375]186# ifdef __X86_64__
187 return ("x86_64");
188# else
189 return ("i386");
190# endif
[1]191#endif
[128]192 return ("unknown");
[1]193}
194
195
[1375]196char *get_uname_m(void) {
[1]197
[1375]198 struct utsname utsn;
199 char *tmp = NULL;
200
201 uname(&utsn);
[3185]202 mr_asprintf(tmp, "%s", utsn.machine);
[1375]203 return (tmp);
204}
205
206
207
208double get_kernel_version(void)
[1]209{
[3191]210 char *p = NULL;
211 char *tmp = NULL;
[128]212 double d;
[1]213#ifdef __FreeBSD__
[128]214 // JOSH - FIXME :)
215 d = 5.2; // :-)
[1]216#else
[3610]217 tmp = call_program_and_get_last_line_of_output("uname -r");
[128]218 p = strchr(tmp, '.');
219 if (p) {
220 p = strchr(++p, '.');
221 if (p) {
222 while (*p) {
223 *p = *(p + 1);
224 p++;
225 }
226 }
227 }
228 d = atof(tmp);
[3191]229 mr_free(tmp);
[1]230#endif
[128]231 log_msg(1, "g_kernel_version = %f", d);
[3871]232
233 if (g_kernel_version < 2.6) {
234 fatal_error("Please use MondoRescue < 3.3.0 to support that old kernel or upgrade your distribution :-)");
235 }
236 return(d);
[1]237}
238
239
240/**
241 * Get the current time.
242 * @return number of seconds since the epoch.
243 */
[128]244long get_time()
[1]245{
[128]246 return (long) time((void *) 0);
[1]247}
248
249
250/**
251 * Initialize a RAID volume structure, setting fields to zero. The
252 * actual hard drive is unaffected.
253 *
254 * @param raidrec The RAID volume structure to initialize.
255 * @note This function is system dependent.
256 */
257#ifdef __FreeBSD__
[128]258void initialize_raidrec(struct vinum_volume *raidrec)
[1]259{
[128]260 int i, j;
261 raidrec->volname[0] = '\0';
262 raidrec->plexes = 0;
263 for (i = 0; i < 9; ++i) {
264 raidrec->plex[i].raidlevel = -1;
265 raidrec->plex[i].stripesize = 0;
266 raidrec->plex[i].subdisks = 0;
267 for (j = 0; j < 9; ++j) {
268 strcpy(raidrec->plex[i].sd[j].which_device, "");
269 }
270 }
[1]271}
272#else
[128]273void initialize_raidrec(struct raid_device_record *raidrec)
[1]274{
[128]275 assert(raidrec != NULL);
276 raidrec->raid_device[0] = '\0';
[558]277 raidrec->raid_level = -9;
[128]278 raidrec->persistent_superblock = 1;
[558]279 raidrec->chunk_size = 64;
280 raidrec->parity = -1;
[128]281 raidrec->data_disks.entries = 0;
282 raidrec->spare_disks.entries = 0;
283 raidrec->parity_disks.entries = 0;
284 raidrec->failed_disks.entries = 0;
285 raidrec->additional_vars.entries = 0;
[1]286}
287#endif
288
289
290
291
292/**
293 * Insert modules that Mondo requires.
[1236]294 * Currently inserts @c msdos, @c vfat, and @c loop for Linux;
[1]295 * @c msdosfs and @c ext2fs for FreeBSD.
296 */
297void insmod_crucial_modules(void)
298{
299#ifdef __FreeBSD__
[3060]300 paranoid_system("kldstat | grep msdosfs || kldload msdosfs 2> /dev/null");
301 paranoid_system("kldstat | grep ext2fs || kldload ext2fs 2> /dev/null");
[1]302#else
[3060]303 paranoid_system("modprobe -a msdos vfat loop &> /dev/null");
[1]304#endif
305}
306
[3794]307/* compute the final options for bootable media, considering the command and the boot type */
[3665]308char *mr_compute_uefi_string(char *isofs_cmd) {
309
310 char *uefistr = NULL;
311 char *mondo_mkisofs_sz = NULL;
312
313 /* real media */
314 if (strstr(isofs_cmd,"growisofs")) {
315 mr_asprintf(mondo_mkisofs_sz, MONDO_GROWISOFS_REGULAR_SYSLINUX);
316 } else {
317 /* iso image */
318 if (bkpinfo->boot_type == UEFI) {
319 if (strstr(isofs_cmd,"xorriso")) {
320 /* xorriso needs another '-' before efi-boot */
321 mr_asprintf(uefistr, "%s -%s", MONDO_UEFI_PREFIX, MONDO_MKISOFS_UEFI);
322 } else {
323 mr_asprintf(uefistr, "%s %s", MONDO_UEFI_PREFIX, MONDO_MKISOFS_UEFI);
324 }
325 } else {
326 mr_asprintf(uefistr, "%s",MONDO_MKISOFS_CMS);
327 }
328 mr_asprintf(mondo_mkisofs_sz, "%s%s%s ", isofs_cmd, MONDO_MKISOFS_REGULAR_SYSLINUX, uefistr);
329 }
330
331 return(mondo_mkisofs_sz);
332}
333
[1]334/**
335 * Finish configuring the backup information structure. Call this function
336 * to set the parameters that depend on those that can be given on the command
337 * line.
338 *
339 * @param bkpinfo The backup information structure. Fields modified/used:
340 * - Used: @c bkpinfo->backup_data
341 * - Used: @c bkpinfo->backup_media_type
342 * - Used: @c bkpinfo->compression_level
343 * - Used: @c bkpinfo->include_paths
[20]344 * - Used: @c bkpinfo->prefix
[1]345 * - Used: @c bkpinfo->isodir
346 * - Used: @c bkpinfo->manual_cd_tray
347 * - Used: @c bkpinfo->media_device
[2380]348 * - Used: @c bkpinfo->netfs_mount
[1]349 * - Used: @c bkpinfo->scratchdir
350 * - Used: @c bkpinfo->tmpdir
351 * - Used: @c bkpinfo->use_lzo
352 * - Modified: @c bkpinfo->call_before_iso
353 * - Modified: @c bkpinfo->call_make_iso
354 * - Modified: @c bkpinfo->optimal_set_size
355 * - Modified: @c bkpinfo->zip_exe
356 * - Modified: @c bkpinfo->zip_suffix
357 *
358 * @return number of errors, or 0 for success.
359 * @note Also creates directories that are specified in the @c bkpinfo structure but
360 * do not exist.
361 */
[1645]362int post_param_configuration()
[1]363{
[2211]364 char *extra_cdrom_params = NULL;
365 char *mondo_mkisofs_sz = NULL;
[2518]366 char *command = NULL;
[2848]367 char *isofs_cmd = NULL;
[128]368 int retval = 0;
[3191]369 char *cdr_exe = NULL;
[2518]370 char *tmp = NULL;
[3191]371 char *call_before_iso_user = NULL;
[2518]372 char *iso_dev = NULL;
373 char *iso_mnt = NULL;
374 char *iso_tmp = NULL;
[3278]375 char *iso_dir = NULL;
[1]376
[128]377 assert(bkpinfo != NULL);
[3191]378
[3318]379 bkpinfo->optimal_set_size = (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type) ? 16 : 16) * 1024L;
[1]380
[2658]381 log_msg(1, "Post-param");
[128]382 if (bkpinfo->backup_media_type == tape) {
[2926]383 if (whine_if_not_found("mt") == 1) {
384 fatal_error("Please install the mt command");
385 }
[2658]386 log_msg(1, "Tape");
[3191]387 if (bkpinfo->media_device == NULL) {
388 return(1);
389 }
[3185]390 mr_asprintf(tmp, "mt -f %s status", bkpinfo->media_device);
[128]391 log_msg(1, "tmp = '%s'", tmp);
392 if (run_program_and_log_output(tmp, 3)) {
[2518]393 mr_free(tmp);
[3205]394 fatal_error("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.");
[128]395 }
[2518]396 mr_free(tmp);
[1]397 }
[128]398 if (bkpinfo->backup_media_type == iso)
399 make_hole_for_dir(bkpinfo->isodir);
[1]400
[128]401 run_program_and_log_output("uname -a", 5);
[678]402 run_program_and_log_output("cat /etc/*-release", 5);
[128]403 run_program_and_log_output("cat /etc/*issue*", 5);
[1]404#ifdef __FreeBSD__
405#else
[128]406 run_program_and_log_output("cat /proc/cpuinfo", 5);
[3292]407 /*
408 run_program_and_log_output("rpm -q newt newt-devel slang slang-devel ncurses ncurses-devel gcc", 5);
409 */
[1]410#endif
[128]411
412 if (bkpinfo->use_lzo) {
[3830]413 mr_asprintf(bkpinfo->zip_exe, "%s", "lzop");
414 mr_asprintf(bkpinfo->zip_suffix, "%s", "lzo");
[998]415 } else if (bkpinfo->use_gzip) {
[3830]416 mr_asprintf(bkpinfo->zip_exe, "%s", "gzip");
417 mr_asprintf(bkpinfo->zip_suffix, "%s", "gz");
[3191]418 } else if (bkpinfo->use_lzma) {
[3830]419 mr_asprintf(bkpinfo->zip_exe, "%s", "xz");
420 mr_asprintf(bkpinfo->zip_suffix, "%s", "xz");
[128]421 } else if (bkpinfo->compression_level != 0) {
[3830]422 mr_asprintf(bkpinfo->zip_exe, "%s", "bzip2");
423 mr_asprintf(bkpinfo->zip_suffix, "%s", "bz2");
[128]424 } else {
[3830]425 mr_free(bkpinfo->zip_exe);
426 mr_free(bkpinfo->zip_suffix);
[1]427 }
428
[3882]429 if (bkpinfo->backup_media_type == optical) {
[3867]430 if ((cdr_exe = find_home_of_exe("growisofs")) == NULL) {
[3888]431 mr_free(cdr_exe);
[128]432 fatal_error("Please install growisofs.");
433 }
[1]434#ifdef __FreeBSD__
[3888]435 mr_asprintf(mondo_mkisofs_sz, MONDO_GROWISOFS_REGULAR_LILO);
[1]436#else
[3888]437 mondo_mkisofs_sz = mr_compute_uefi_string(cdr_exe);
[1]438#endif
[3867]439 mr_free(cdr_exe);
440
[128]441 if (bkpinfo->manual_cd_tray) {
442 fatal_error("Manual CD tray + DVD not supported yet.");
443 // -m isn't supported by growisofs, BTW...
444 } else {
[3827]445 mr_asprintf(bkpinfo->call_make_iso, "%s %s -Z %s . 2>> _ERR_", mondo_mkisofs_sz, "", bkpinfo->media_device);
[128]446 }
[3191]447 mr_free(mondo_mkisofs_sz);
[153]448 if (getenv ("SUDO_COMMAND")) {
[3185]449 mr_asprintf(command, "strings `which growisofs` | grep -c SUDO_COMMAND");
[3610]450 tmp = call_program_and_get_last_line_of_output(command);
451 mr_free(command);
452 if (!strcmp(tmp, "1")) {
453 mr_free(tmp);
[679]454 popup_and_OK("Fatal Error: Can't write DVDs as sudo because growisofs doesn't support this - see the growisofs manpage for details.");
455 fatal_error("Can't write DVDs as sudo because growisofs doesn't support this - see the growisofs manpage for details.");
456 }
[3610]457 mr_free(tmp);
[153]458 }
[3191]459 log_msg(2, "call_make_iso (DVD res) is ... %s", bkpinfo->call_make_iso);
[128]460 } // end of DVD code
[1]461
462// CD-R or CD-RW
[3881]463 if (bkpinfo->backup_media_type == optical) {
[128]464 if (!bkpinfo->manual_cd_tray) {
[3185]465 mr_asprintf(extra_cdrom_params, "-waiti ");
[128]466 }
[3880]467 if (bkpinfo->wipe_media_first) {
468 mr_asprintf(extra_cdrom_params, "%s", "blank=fast ");
469 }
[3867]470 if ((cdr_exe = find_home_of_exe("cdrecord")) == NULL) {
471 if ((cdr_exe = find_home_of_exe("wodim")) == NULL) {
[3882]472 fatal_error("Please install either cdrecord or wodim.");
[3867]473 }
[128]474 }
[3867]475 if ((tmp = find_home_of_exe("xorriso")) != NULL) {
476 mr_asprintf(isofs_cmd, "%s %s", tmp, MONDO_XORRISO_OPT);
477 } else if ((tmp = find_home_of_exe("genisoimage")) != NULL) {
478 mr_asprintf(isofs_cmd, "%s %s", tmp, MONDO_GENISOIMAGE_OPT);
479 } else if ((tmp = find_home_of_exe("mkisofs")) != NULL) {
480 mr_asprintf(isofs_cmd, "%s %s", tmp, MONDO_MKISOFS_OPT);
481 } else if ((tmp = find_home_of_exe("wodim")) != NULL) {
482 mr_asprintf(isofs_cmd, "%s %s", tmp, MONDO_WODIM_OPT);
[2848]483 } else {
[3867]484 fatal_error("Unable to find a command to create ISO among xorriso, genisoimage, mksiofs or wodim, please install one");
[2848]485 }
[3867]486 mr_free(tmp);
487
[1]488#ifdef __FreeBSD__
[3888]489 mr_asprintf(mondo_mkisofs_sz, "%s%s ", isofs_cmd, MONDO_MKISOFS_REGULAR_LILO);
[1]490#else
[3888]491 mondo_mkisofs_sz = mr_compute_uefi_string(isofs_cmd);
[1]492#endif
[2848]493 mr_free(isofs_cmd);
494
[128]495 if (bkpinfo->manual_cd_tray) {
[3823]496 if (bkpinfo->call_before_iso == NULL) {
497 mr_asprintf(bkpinfo->call_before_iso, "%s -o %s/"MONDO_TMPISOS" . 2>> _ERR_", mondo_mkisofs_sz, bkpinfo->tmpdir);
[3205]498 } else {
[3191]499 mr_asprintf(call_before_iso_user, "%s", bkpinfo->call_before_iso);
[3823]500 mr_free(bkpinfo->call_before_iso);
501 mr_asprintf(bkpinfo->call_before_iso, "( %s -o %s/"MONDO_TMPISOS" . 2>> _ERR_ ; %s )", mondo_mkisofs_sz, bkpinfo->tmpdir, call_before_iso_user);
[3191]502 mr_free(call_before_iso_user);
[163]503 }
504 log_it("bkpinfo->call_before_iso = %s", bkpinfo->call_before_iso);
[3882]505 mr_asprintf(bkpinfo->call_make_iso, "%s %s -v %s fs=4m dev=%s %s/"MONDO_TMPISOS, cdr_exe, (bkpinfo->please_dont_eject) ? " " : "-eject", extra_cdrom_params, bkpinfo->media_device, bkpinfo->tmpdir);
[128]506 } else {
[3882]507 mr_asprintf(bkpinfo->call_make_iso, "%s . 2>> _ERR_ | %s %s %s fs=4m dev=%s -", mondo_mkisofs_sz, cdr_exe, (bkpinfo->please_dont_eject) ? " " : "-eject", extra_cdrom_params, bkpinfo->media_device);
[128]508 }
[3191]509 mr_free(cdr_exe);
510 mr_free(mondo_mkisofs_sz);
511 mr_free(extra_cdrom_params);
[128]512 } // end of CD code
[1]513
[128]514 if (bkpinfo->backup_media_type == iso) {
515
[3278]516/* Break up isodir into iso_mnt and iso_dir
[1]517 * These will be used along with iso-dev at restore time
518 * to locate the ISOs where ever they're mounted
519 */
520
[128]521 log_it("isodir = %s", bkpinfo->isodir);
[3191]522 mr_asprintf(command, "df -P %s | tail -n1 | cut -d' ' -f1", bkpinfo->isodir);
[128]523 log_it("command = %s", command);
[3610]524 iso_dev = call_program_and_get_last_line_of_output(command);
[3191]525 log_it("res of it = %s", iso_dev);
[3185]526 mr_asprintf(tmp, "%s/ISO-DEV", bkpinfo->tmpdir);
[3191]527 write_one_liner_data_file(tmp, iso_dev);
[2518]528 mr_free(tmp);
529 mr_free(command);
[1]530
[3185]531 mr_asprintf(command, "mount | grep -w %s | tail -n1 | cut -d' ' -f3", iso_dev);
[2518]532 mr_free(iso_dev);
533
[128]534 log_it("command = %s", command);
[3610]535 iso_mnt = call_program_and_get_last_line_of_output(command);
[3191]536 log_it("res of it = %s", iso_mnt);
[3185]537 mr_asprintf(tmp, "%s/ISO-MNT", bkpinfo->tmpdir);
[3191]538 write_one_liner_data_file(tmp, iso_mnt);
[2518]539 mr_free(tmp);
540 mr_free(command);
541
[3278]542 log_it("iso_mnt: %s, %d", iso_mnt, strlen(iso_mnt));
[3185]543 mr_asprintf(iso_tmp, "%s", bkpinfo->isodir);
[128]544 if (strlen(iso_tmp) < strlen(iso_mnt)) {
[3278]545 mr_asprintf(iso_dir, "%s", "");
[128]546 } else {
[3278]547 // If iso_mnt is only / then iso_dir is the full dir
[2522]548 // (the formula bellow doesn't work in this case)
549 if (strcmp(iso_mnt, "/") == 0) {
[3278]550 mr_asprintf(iso_dir, "%s", iso_tmp);
[2522]551 // else it's a part of iso_tmp
552 } else {
[3278]553 mr_asprintf(iso_dir, "%s", iso_tmp + strlen(iso_mnt));
[2522]554 }
[128]555 }
[3191]556 mr_free(iso_mnt);
[2518]557 mr_free(iso_tmp);
558
[3278]559 mr_asprintf(tmp, "%s/ISO-DIR", bkpinfo->tmpdir);
560 write_one_liner_data_file(tmp, iso_dir);
[2518]561 mr_free(tmp);
562
[3278]563 log_it("iso-dir: %s", iso_dir);
564 mr_free(iso_dir);
[2518]565
[3185]566 mr_asprintf(tmp, "%s/ISO-PREFIX", bkpinfo->tmpdir);
[148]567 write_one_liner_data_file(tmp, bkpinfo->prefix);
[2518]568 mr_free(tmp);
569
[148]570 log_it("iso-prefix: %s", bkpinfo->prefix);
[1]571
572/* End patch */
[128]573 } // end of iso code
[1]574
[2380]575 if (bkpinfo->backup_media_type == netfs) {
[3268]576 mr_asprintf(tmp, "%s/ISO-PREFIX", bkpinfo->tmpdir);
577 write_one_liner_data_file(tmp, bkpinfo->prefix);
578 mr_free(tmp);
579
[3278]580 mr_asprintf(tmp, "%s/ISO-MNT", bkpinfo->tmpdir);
581 write_one_liner_data_file(tmp, bkpinfo->isodir);
582 mr_free(tmp);
583
584 mr_asprintf(tmp, "%s/ISO-DIR", bkpinfo->tmpdir);
585 write_one_liner_data_file(tmp, bkpinfo->netfs_remote_dir);
586 mr_free(tmp);
587
[3191]588 if (bkpinfo->netfs_mount) {
[3268]589 mr_asprintf(tmp, "%s/NETFS-SERVER-MOUNT", bkpinfo->tmpdir);
590 write_one_liner_data_file(tmp, bkpinfo->netfs_mount);
591 mr_free(tmp);
[3191]592 } else {
593 log_it("netfs_mount is NULL");
594 retval++;
595 }
[128]596 }
[1]597
[128]598 log_it("Finished processing incoming params");
599 if (retval) {
600 fprintf(stderr, "Type 'man mondoarchive' for help.\n");
601 }
[3191]602 if (bkpinfo->include_paths == NULL) {
603 mr_asprintf(bkpinfo->include_paths, "/");
[128]604 }
605 g_backup_media_type = bkpinfo->backup_media_type;
606 return (retval);
[1]607}
608
609
610
611/**
[3877]612 * Check the user's system for sanity. Checks performed:
613 * - make sure user has enough RAM (32mb required, 64mb recommended)
614 * - make sure user has enough free space in @c /
615 * - check kernel for ramdisk support
616 * - make sure afio, cdrecord, mkisofs, bzip2, awk, md5sum, strings, mindi, and buffer exist
617 * - make sure CD-ROM is unmounted
618 * - make sure user's mountlist is OK by running <tt>mindi --makemountlist</tt>
619 *
620 * @return number of problems with the user's setup (0 for success)
621 */
622int some_basic_system_sanity_checks()
623{
624
625 /*@ buffers ************ */
626 char *tmp = NULL;
627
628 /*@ int's *************** */
629 int retval = 0;
630
631 mvaddstr_and_log_it(g_currentY, 0, "Checking sanity of your Linux distribution");
632#ifndef __FreeBSD__
633 if (system("which mkfs.vfat 2> /dev/null 1> /dev/null")
634 && !system("which mkfs.msdos 2> /dev/null 1> /dev/null")) {
635 log_it("OK, you've got mkfs.msdos but not mkfs.vfat; time for the fairy to wave her magic wand...");
636 run_program_and_log_output("ln -sf `which mkfs.msdos` /sbin/mkfs.vfat", FALSE);
637 }
638 tmp = call_program_and_get_last_line_of_output("free | grep Mem | head -n1 | tr -s ' ' '\t' | cut -f2");
639 if (atol(tmp) < 35000) {
640 retval++;
641 log_to_screen("You must have at least 32MB of RAM to use Mondo.");
642 }
643 if (atol(tmp) < 66000) {
644 log_to_screen("WARNING! You have very little RAM. Please upgrade to 64MB or more.");
645 }
646 mr_free(tmp);
647#endif
648
649 if (system("which " MKE2FS_OR_NEWFS " > /dev/null 2> /dev/null")) {
650 retval++;
651 log_to_screen("Unable to find " MKE2FS_OR_NEWFS " in system path.");
652 fatal_error("Please use \"su -\", not \"su\" to become root. OK?\n...and please don't e-mail the mailing list about this. Just read the message. :)");
653 }
654#ifndef __FreeBSD__
655 if (run_program_and_log_output("grep ramdisk /proc/devices", FALSE)) {
656 /* Some SuSE have ramdisk as modules, so insert it first, then test again */
657 run_program_and_log_output("modprobe brd 2> /dev/null > /dev/null",FALSE);
658 if (run_program_and_log_output("grep ramdisk /proc/devices", FALSE)) {
659 if (!ask_me_yes_or_no("Your kernel has no ramdisk support. That's mind-numbingly stupid but I'll allow it if you're planning to use another kernel. Are you?")) {
660 // retval++;
661 log_to_screen("It looks as if your kernel lacks ramdisk and initrd support.");
662 log_to_screen("I'll allow you to proceed but FYI, if I'm right, your kernel is broken.");
663 }
664 }
665 }
666#endif
667 retval += whine_if_not_found(MKE2FS_OR_NEWFS);
668 if (system("which xorriso > /dev/null 2> /dev/null")) {
669 if (system("which genisoimage > /dev/null 2> /dev/null")) {
670 retval += whine_if_not_found("mkisofs");
671 }
672 }
673 if (system("which wodim > /dev/null 2> /dev/null")) {
674 retval += whine_if_not_found("cdrecord");
675 }
676 retval += whine_if_not_found("bzip2");
677 retval += whine_if_not_found("gzip");
678 retval += whine_if_not_found("cmp");
679 retval += whine_if_not_found("awk");
680 retval += whine_if_not_found("md5sum");
681 retval += whine_if_not_found("strings");
682 retval += whine_if_not_found("mindi");
683 retval += whine_if_not_found("buffer");
684
685 // abort if Windows partition but no ms-sys and parted
686 if (!run_program_and_log_output("mount | grep -Ew 'vfat|fat|dos' | grep -vE \"/dev/fd|nexdisk\"", 0)) {
687 if (!run_program_and_log_output("mount | grep -Ew 'vfat|fat|dos' | grep -Ew efi", 0)) {
[3889]688 log_to_screen("I think you have a UEFI partition.");
[3877]689 } else {
690 log_to_screen("I think you have a Windows 9x partition.");
691 }
692 retval += whine_if_not_found("parted");
693 }
694
695 run_program_and_log_output("umount `mount | grep cdr | cut -d' ' -f3 | tr '\n' ' '`", 5);
696 tmp = call_program_and_get_last_line_of_output("mount | grep -E 'cdr(om|w)'");
697 if (strcmp("", tmp)) {
698 if (strstr(tmp, "autofs")) {
699 log_to_screen("Your CD-ROM is mounted via autofs. I therefore cannot tell");
700 log_to_screen("if a CD actually is inserted. If a CD is inserted, please");
701 log_to_screen("eject it. Thank you.");
702 log_it("Ignoring autofs CD-ROM 'mount' since we hope nothing's in it.");
703 } else
704 if (run_program_and_log_output("uname -a | grep Knoppix", 5)) {
705 retval++;
706 mr_free(tmp);
707 fatal_error("Your CD-ROM drive is mounted. Please unmount it.");
708 }
709 }
710 mr_free(tmp);
711
712 run_program_and_log_output("cat /etc/fstab", 5);
713#ifdef __FreeBSD__
714 run_program_and_log_output("vinum printconfig", 5);
715#else
716 run_program_and_log_output("cat /etc/raidtab", 5);
717#endif
718
719 if (run_program_and_log_output("mindi -V", 1)) {
720 log_to_screen("Could not ascertain mindi's version number.");
721 log_to_screen("You have not installed Mondo and/or Mindi properly.");
722 log_to_screen("Please uninstall and reinstall them both.");
723 fatal_error("Please reinstall Mondo and Mindi.");
724 }
725 mr_asprintf(tmp, "mindi --makemountlist %s/mountlist.txt.test", bkpinfo->tmpdir);
726 if (run_program_and_log_output(tmp, 5)) {
727 log_to_screen("%s failed for some reason.", tmp);
728 mr_free(tmp);
729 log_to_screen("Please run that command by hand and examine "MINDI_LOGFILE);
730 log_to_screen("for more information. Perhaps your /etc/fstab file is insane.");
731 log_to_screen("Perhaps Mindi's MakeMountlist() subroutine has a bug. We'll see.");
732 retval++;
733 }
734 mr_free(tmp);
735
736 if (!run_program_and_log_output("mr-parted2fdisk -l 2>/dev/null | grep -i raid", 1) && !does_file_exist("/etc/raidtab")) {
[3892]737 log_to_screen("You have RAID partitions but no /etc/raidtab - creating one from " MDSTAT_FILE);
[3877]738 create_raidtab_from_mdstat(MDSTAT_FILE,"/etc/raidtab");
739 }
740
741 if (retval) {
742 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
743 } else {
744 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
745 }
746 return (retval);
747}
748
749
750
751/**
[1]752 * Do some miscellaneous setup tasks to be performed before filling @c bkpinfo.
753 * Seeds the random-number generator, loads important modules, checks the sanity
754 * of the user's Linux distribution, and deletes logfile.
755 * @param bkpinfo The backup information structure. Will be initialized.
756 * @return number of errors (0 for success)
757 */
[1645]758int pre_param_configuration()
[1]759{
[128]760 int res = 0;
[1652]761 char *tmp = NULL;
[1]762
[128]763 make_hole_for_dir(MNT_CDROM);
764 assert(bkpinfo != NULL);
765 srandom((unsigned long) (time(NULL)));
766 insmod_crucial_modules();
767 if (bkpinfo->disaster_recovery) {
768 if (!does_nonMS_partition_exist()) {
[3191]769 fatal_error("I am in disaster recovery mode\nPlease don't run mondoarchive.");
[128]770 }
771 }
[1]772
[3185]773 mr_asprintf(tmp,"rm -Rf %s/changed.files*",MONDO_CACHE);
[1652]774 run_program_and_log_output(tmp, FALSE);
775 paranoid_free(tmp);
[128]776 if (find_and_store_mondoarchives_home(g_mondo_home)) {
[3191]777 fprintf(stderr, "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");
[128]778 res++;
779 return (res);
780 }
781 res += some_basic_system_sanity_checks();
782 if (res) {
783 log_it("Your distribution did not pass Mondo's sanity test.");
784 }
785 g_current_media_number = 1;
786 return (res);
[1]787}
788
[3043]789/* From busybox under GPLv2 */
790#ifndef HAVE_MKDTEMP
791/* This is now actually part of POSIX.1, but was only added in 2008 */
792char* mkdtemp(char *template)
793{
[3514]794 if (mkstemp(template) == -1) {
[3043]795 return NULL;
[3507]796 }
797 unlink(template);
798 if (mkdir(template, 0700) != 0) {
799 return NULL;
800 }
[3043]801 return template;
802}
803#endif
804
[1655]805void setup_tmpdir(char *path) {
[1967]806
[1653]807 char *tmp = NULL;
[1]808
[3833]809 if (bkpinfo->tmpdir != NULL) {
[1655]810 /* purging a potential old tmpdir */
[3060]811 if (chdir("/tmp")) {
812 // FIXME
813 }
[3154]814 if (strstr(bkpinfo->tmpdir,"mondo.tmp.") != NULL) {
815 log_it("Purging old tmpdir %s", bkpinfo->tmpdir);
[3185]816 mr_asprintf(tmp,"rm -Rf %s",bkpinfo->tmpdir);
[3154]817 } else {
818 log_it("Purging old tmpdir %s/mondo.tmp.*", bkpinfo->tmpdir);
[3185]819 mr_asprintf(tmp,"rm -Rf %s/mondo.tmp.*",bkpinfo->tmpdir);
[3154]820 }
[3060]821 paranoid_system(tmp);
[3154]822 mr_free(tmp);
[1655]823 }
[1967]824
[3154]825 /* Always take in account arg first, then env, then default */
826 if (path != NULL) {
[3503]827 mr_system("mkdir -p %s",path);
828 log_it("Created temporary directory %s", path);
[3185]829 mr_asprintf(tmp, "%s/mondo.tmp.XXXXXX", path);
[3154]830 } else if (getenv("TMPDIR")) {
[3500]831 mr_system("mkdir -p %s",getenv("TMPDIR"));
[3503]832 log_it("Created temporary directory %s", getenv("TMPDIR"));
[3185]833 mr_asprintf(tmp, "%s/mondo.tmp.XXXXXX", getenv("TMPDIR"));
[3154]834 } else if (getenv("MRTMP")) {
[3500]835 mr_system("mkdir -p %s",getenv("MRTMP"));
[3503]836 log_it("Created temporary directory %s", getenv("MRTMP"));
[3185]837 mr_asprintf(tmp, "%s/mondo.tmp.XXXXXX", getenv("MRTMP"));
[1653]838 } else {
[3185]839 mr_asprintf(tmp, "/tmp/mondo.tmp.XXXXXX");
[1653]840 }
[3834]841 mr_free(bkpinfo->tmpdir);
[3833]842 bkpinfo->tmpdir = mkdtemp(tmp);
843 if (bkpinfo->tmpdir == NULL) {
[3195]844 printf("Failed to create global tmp directory %s for Mondo.",tmp);
[3191]845 mr_free(tmp);
[1653]846 finish(-1);
847 }
[3191]848 log_it("bkpinfo->tmpdir is being set to %s", bkpinfo->tmpdir);
[3507]849 mr_system("mkdir -p %s/tmpfs", bkpinfo->tmpdir);
[1653]850}
[1]851
[3154]852void setup_scratchdir(char *path) {
[1]853
[3154]854 char *tmp = NULL;
855
856 if (bkpinfo->scratchdir != NULL) {
857 /* purging a potential old scratchdir */
858 if (chdir("/tmp")) {
859 // FIXME
860 }
861 if (strstr(bkpinfo->scratchdir,"mondo.scratch.") != NULL) {
862 log_it("Purging old scratchdir %s", bkpinfo->scratchdir);
[3185]863 mr_asprintf(tmp,"rm -Rf %s",bkpinfo->scratchdir);
[3154]864 } else {
865 log_it("Purging old scratchdir %s/mondo.scratch.*", bkpinfo->scratchdir);
[3185]866 mr_asprintf(tmp,"rm -Rf %s/mondo.scratch.*",bkpinfo->scratchdir);
[3154]867 }
868 paranoid_system(tmp);
869 mr_free(tmp);
870 }
871
872 /* Always take in account arg first, then env, then default */
873 if (path != NULL) {
[3500]874 mr_system("mkdir -p %s",path);
[3503]875 log_it("Created scratch directory %s", path);
[3185]876 mr_asprintf(tmp, "%s/mondo.scratch.XXXXXX", path);
[3154]877 } else if (getenv("MRSCRATCH")) {
[3500]878 mr_system("mkdir -p %s",getenv("MRSCRATCH"));
[3503]879 log_it("Created scratch directory %s", getenv("MRSCRATCH"));
[3185]880 mr_asprintf(tmp, "%s/mondo.scratch.XXXXXX", getenv("MRSCRATCH"));
[3154]881 } else {
[3185]882 mr_asprintf(tmp, "/tmp/mondo.scratch.XXXXXX");
[3154]883 }
[3834]884 mr_free(bkpinfo->scratchdir);
885 bkpinfo->scratchdir = mkdtemp(tmp);
886 if (bkpinfo->scratchdir == NULL) {
[3154]887 log_it("Failed to create global scratch directory %s for Mondo.",tmp);
[3834]888 mr_free(tmp);
[3154]889 finish(-1);
890 }
[3191]891 log_it("bkpinfo->scratchdir is being set to %s", bkpinfo->scratchdir);
[3834]892 mr_system("mkdir -p %s", bkpinfo->scratchdir);
[3154]893}
894
[3898]895/**
896 * Reset all fields of the backup information structure to a sensible default.
897 * @param bkpinfo The @c bkpinfo to reset.
898 */
899void alloc_bkpinfo(void) {
[3154]900
[3898]901if (! (bkpinfo = (struct s_bkpinfo *)malloc(sizeof(struct s_bkpinfo)))) {
902 fatal_error("Cannot malloc bkpinfo");
903 }
904}
905
[1]906/**
907 * Reset all fields of the backup information structure to a sensible default.
908 * @param bkpinfo The @c bkpinfo to reset.
909 */
[3898]910void reset_bkpinfo(void) {
911
[128]912 assert(bkpinfo != NULL);
913 memset((void *) bkpinfo, 0, sizeof(struct s_bkpinfo));
[1653]914
[3822]915 bkpinfo->media_device = NULL;
[3150]916 bkpinfo->media_size = -1;
[128]917 bkpinfo->boot_loader = '\0';
[3373]918 bkpinfo->boot_device = NULL;
[3830]919 bkpinfo->zip_exe = NULL;
920 bkpinfo->zip_suffix = NULL;
[3821]921 bkpinfo->image_devs = NULL;
[1966]922 bkpinfo->compression_level = 3;
[128]923 bkpinfo->use_lzo = FALSE;
[998]924 bkpinfo->use_gzip = FALSE;
[3191]925 bkpinfo->use_lzma = FALSE;
[128]926 bkpinfo->verify_data = FALSE;
927 bkpinfo->backup_data = FALSE;
928 bkpinfo->restore_data = FALSE;
[1966]929 bkpinfo->use_star = FALSE;
930 bkpinfo->internal_tape_block_size = DEFAULT_INTERNAL_TAPE_BLOCK_SIZE;
[3191]931
[3194]932 /* We need tmpdir as early as possible for further function calls */
[3834]933 bkpinfo->tmpdir = NULL; // Really setup just after
934 bkpinfo->scratchdir = NULL; // Really setup just after
[3191]935 setup_tmpdir(NULL);
936 setup_scratchdir(NULL);
937
938 bkpinfo->disaster_recovery = am_I_in_disaster_recovery_mode();
[128]939 if (bkpinfo->disaster_recovery) {
[3836]940 mr_asprintf(bkpinfo->isodir, "%s", "/");
[128]941 } else {
[3836]942 mr_asprintf(bkpinfo->isodir, "%s", MONDO_CACHE);
[128]943 }
[3836]944 mr_asprintf(bkpinfo->prefix, "%s", STD_PREFIX);
[3318]945 bkpinfo->optimal_set_size = 0L;
[3191]946 bkpinfo->backup_media_type = none;
[1966]947 bkpinfo->make_filelist = TRUE; // unless -J supplied to mondoarchive
[3191]948 bkpinfo->exclude_paths = NULL;
949 bkpinfo->include_paths = NULL;
[2424]950 bkpinfo->exclude_devs = NULL;
[3828]951 bkpinfo->restore_path = NULL;
[3823]952 bkpinfo->call_before_iso = NULL;
[3827]953 bkpinfo->call_make_iso = NULL;
954 bkpinfo->call_burn_iso = NULL;
955 bkpinfo->call_after_iso = NULL;
[3829]956 bkpinfo->kernel_path = NULL;
[3194]957 bkpinfo->netfs_mount = NULL;
[2380]958 bkpinfo->netfs_proto = NULL;
959 bkpinfo->netfs_user = NULL;
[3194]960 bkpinfo->netfs_remote_dir = NULL;
[3826]961 bkpinfo->postnuke_tarball = NULL;
[2769]962 bkpinfo->subdir = NULL;
[128]963 bkpinfo->wipe_media_first = FALSE;
[1966]964 bkpinfo->differential = 0;
965 bkpinfo->please_dont_eject = FALSE;
966 bkpinfo->manual_cd_tray = FALSE;
967 bkpinfo->use_obdr = FALSE;
[1967]968 bkpinfo->restore_mode = interactive;
[3375]969 bkpinfo->boot_type = BIOS;
[1]970}
971
972
973/**
974 * Get the remaining free space (in MB) on @p partition.
975 * @param partition The partition to check free space on (either a device or a mountpoint).
976 * @return The free space on @p partition, in MB.
977 */
[128]978long free_space_on_given_partition(char *partition)
[1]979{
[2518]980 char *out_sz = NULL;
981 char *command = NULL;
[128]982 long res;
[1]983
[128]984 assert_string_is_neither_NULL_nor_zerolength(partition);
[1]985
[3185]986 mr_asprintf(command, "df -m -P %s 1> /dev/null 2> /dev/null", partition);
[128]987 if (system(command)) {
[2518]988 mr_free(command);
[128]989 return (-1);
990 } // partition does not exist
[2518]991 mr_free(command);
992
[3191]993 mr_asprintf(command, "df -m -P %s | tail -n1 | tr -s ' ' '\t' | cut -f4", partition);
[3610]994 out_sz = call_program_and_get_last_line_of_output(command);
[2518]995 mr_free(command);
996
[128]997 if (strlen(out_sz) == 0) {
[2518]998 mr_free(out_sz);
[128]999 return (-1);
1000 } // error within df, probably
1001 res = atol(out_sz);
[2518]1002 mr_free(out_sz);
[128]1003 return (res);
[1]1004}
1005
1006/**
1007 * Retrieve the line containing @p label from the config file.
[3268]1008 * @param config_file The file to read from, usually @c /tmp/mondorestore.cfg.
[1]1009 * @param label What to read from the file.
1010 * @param value Where to put it.
1011 * @return 0 for success, 1 for failure.
1012 */
[128]1013int read_cfg_var(char *config_file, char *label, char *value)
[1]1014{
[128]1015 /*@ buffer ****************************************************** */
[2518]1016 char *command = NULL;
[3610]1017 char *tmp = NULL;
[1]1018
[128]1019 /*@ end vars *************************************************** */
[1]1020
[128]1021 assert_string_is_neither_NULL_nor_zerolength(config_file);
1022 assert_string_is_neither_NULL_nor_zerolength(label);
1023 if (!does_file_exist(config_file)) {
[3191]1024 log_to_screen("(read_cfg_var) Cannot find %s config file", config_file);
[128]1025 value[0] = '\0';
1026 return (1);
[1279]1027 } else if ((value != NULL) && (strstr(value, "/dev/") && strstr(value, "t0") && !strcmp(label, "media-dev"))) {
1028 log_msg(2, "FYI, I can't read new value for %s - already got %s", label, value);
[128]1029 return (0);
1030 } else {
[3205]1031 mr_asprintf(command, "grep '%s .*' %s| cut -d' ' -f2,3,4,5", label, config_file);
[3610]1032 tmp = call_program_and_get_last_line_of_output(command);
1033 strcpy(value, tmp);
[2518]1034 mr_free(command);
1035
[3191]1036 log_msg(4, "Configuration item %s is %s", label, value);
[128]1037 if (strlen(value) == 0) {
1038 return (1);
1039 } else {
1040 return (0);
1041 }
[1]1042 }
1043}
1044
1045
1046
1047/**
1048 * Remount @c supermount if it was unmounted earlier.
1049 */
1050void remount_supermounts_if_necessary()
1051{
[128]1052 if (g_remount_cdrom_at_end) {
1053 run_program_and_log_output("mount " MNT_CDROM, FALSE);
1054 }
[1]1055}
1056
1057/**
1058 * Unmount @c supermount if it's mounted.
1059 */
1060void unmount_supermounts_if_necessary()
1061{
[128]1062 if (run_program_and_log_output
1063 ("mount | grep cdrom | grep super", FALSE) == 0) {
1064 g_remount_cdrom_at_end = TRUE;
1065 run_program_and_log_output("umount " MNT_CDROM, FALSE);
1066 }
[1]1067}
1068
1069/**
1070 * Whether we had to stop autofs (if so, restart it at end).
1071 */
[128]1072bool g_autofs_stopped = FALSE;
[1]1073
1074/**
1075 * Path to the autofs initscript ("" if none exists).
1076 */
1077char g_autofs_exe[MAX_STR_LEN];
1078
1079/**
1080 * Autofs initscript in Xandros Linux distribution.
1081 */
1082#define XANDROS_AUTOFS_FNAME "/etc/init.d/xandros-autofs"
1083
1084/**
1085 * Autofs initscript in most Linux distributions.
1086 */
1087#define STOCK_AUTOFS_FNAME "/etc/rc.d/init.d/autofs"
1088
1089/**
1090 * If autofs is mounted, stop it (restart at end).
1091 */
1092void stop_autofs_if_necessary()
1093{
[2518]1094 char *tmp = NULL;
[128]1095
1096 g_autofs_exe[0] = '\0';
1097 if (does_file_exist(XANDROS_AUTOFS_FNAME)) {
1098 strcpy(g_autofs_exe, XANDROS_AUTOFS_FNAME);
1099 } else if (does_file_exist(STOCK_AUTOFS_FNAME)) {
1100 strcpy(g_autofs_exe, STOCK_AUTOFS_FNAME);
1101 }
1102
1103 if (!g_autofs_exe[0]) {
1104 log_msg(3, "No autofs detected.");
1105 } else {
1106 log_msg(3, "%s --- autofs detected", g_autofs_exe);
[2518]1107 // FIXME -- only disable it if it's running --- sprintf(tmp, "%s status", autofs_exe);
[3185]1108 mr_asprintf(tmp, "%s stop", g_autofs_exe);
[128]1109 if (run_program_and_log_output(tmp, 2)) {
1110 log_it("Failed to stop autofs - I assume it wasn't running");
1111 } else {
1112 g_autofs_stopped = TRUE;
1113 log_it("Stopped autofs OK");
1114 }
[2518]1115 mr_free(tmp);
[128]1116 }
[1]1117}
1118
1119/**
1120 * If autofs was stopped earlier, restart it.
1121 */
1122void restart_autofs_if_necessary()
1123{
[2518]1124 char *tmp = NULL;
[128]1125
1126 if (!g_autofs_stopped || !g_autofs_exe[0]) {
1127 log_msg(3, "No autofs detected.");
1128 return;
1129 }
[3185]1130 mr_asprintf(tmp, "%s start", g_autofs_exe);
[128]1131 if (run_program_and_log_output(tmp, 2)) {
1132 log_it("Failed to start autofs");
1133 } else {
1134 g_autofs_stopped = FALSE;
1135 log_it("Started autofs OK");
1136 }
[2518]1137 mr_free(tmp);
[1]1138}
1139
1140
1141/**
1142 * If this is a distribution like Gentoo that doesn't keep /boot mounted, mount it.
1143 */
[3191]1144void mount_boot_if_necessary() {
[2518]1145 char *tmp = NULL;
1146 char *command = NULL;
[1]1147
[128]1148 log_msg(1, "Started sub");
1149 log_msg(4, "About to set g_boot_mountpt[0] to '\\0'");
1150 g_boot_mountpt[0] = '\0';
[3654]1151 log_msg(4, "Done. Great. Setting command to something");
[3191]1152 mr_asprintf(command, "%s", "grep -v \":\" /etc/fstab | grep -vE '^#.*$' | grep -E \"[ ]/boot[ ]\" | tr -s ' ' '\t' | cut -f1 | head -n1");
[128]1153 log_msg(4, "Cool. Command = '%s'", command);
[3610]1154 tmp = call_program_and_get_last_line_of_output(command);
[2518]1155 mr_free(command);
1156
[128]1157 log_msg(4, "tmp = '%s'", tmp);
[3191]1158 log_it("/boot is at %s according to /etc/fstab", tmp);
1159 mr_asprintf(command, "mount | grep -Ew '/boot'");
1160 mr_free(tmp);
[2518]1161
[3610]1162 tmp = call_program_and_get_last_line_of_output(command);
[3191]1163 mr_free(command);
1164
1165 if (!strcmp(tmp,"")) {
1166 if ((strstr(tmp, "LABEL=") != NULL) || (strstr(tmp,"UUID=") != NULL)) {
1167 if (!run_program_and_log_output("mount /boot", 5)) {
1168 strcpy(g_boot_mountpt, "/boot");
1169 log_msg(1, "Mounted /boot");
[128]1170 } else {
[3191]1171 log_it("...ignored cos it's a label or uuid :-)");
1172 }
1173 } else {
1174 mr_asprintf(command, "mount | grep -E '^%s'", tmp);
1175 log_msg(3, "command = %s", command);
1176 if (run_program_and_log_output(command, 5)) {
1177 strcpy(g_boot_mountpt, tmp);
1178 mr_free(tmp);
1179 log_it("%s (your /boot partition) is not mounted. I'll mount it before backing up", g_boot_mountpt);
[2518]1180
[3191]1181 mr_asprintf(tmp, "mount %s", g_boot_mountpt);
1182 if (run_program_and_log_output(tmp, 5)) {
1183 g_boot_mountpt[0] = '\0';
1184 log_msg(1, "Plan B");
1185 if (!run_program_and_log_output("mount /boot", 5)) {
1186 strcpy(g_boot_mountpt, "/boot");
1187 log_msg(1, "Plan B worked");
1188 } else {
1189 log_msg(1,
1190 "Plan B failed. Unable to mount /boot for backup purposes. This probably means /boot is mounted already, or doesn't have its own partition.");
[128]1191 }
1192 }
1193 }
[3191]1194 mr_free(command);
[128]1195 }
[1]1196 }
[3191]1197 mr_free(tmp);
[128]1198 log_msg(1, "Ended sub");
[1]1199}
1200
1201
1202/**
1203 * If we mounted /boot earlier, unmount it.
1204 */
1205void unmount_boot_if_necessary()
1206{
[2518]1207 char *tmp = NULL;
[1]1208
[128]1209 log_msg(3, "starting");
1210 if (g_boot_mountpt[0]) {
[3185]1211 mr_asprintf(tmp, "umount %s", g_boot_mountpt);
[128]1212 if (run_program_and_log_output(tmp, 5)) {
1213 log_it("WARNING - unable to unmount /boot");
1214 }
[2518]1215 mr_free(tmp);
[128]1216 }
1217 log_msg(3, "leaving");
[1]1218}
1219
1220
1221
1222/**
1223 * Write a line to a configuration file. Writes a line of the form,
1224 * @c label @c value.
[3268]1225 * @param config_file The file to write to. Usually @c mondorestore.cfg.
[1]1226 * @param label What to call this bit of data you're writing.
1227 * @param value The bit of data you're writing.
1228 * @return 0 for success, 1 for failure.
1229 */
[128]1230int write_cfg_var(char *config_file, char *label, char *value)
[1]1231{
[128]1232 /*@ buffers ***************************************************** */
[2518]1233 char *command = NULL;
1234 char *tempfile = NULL;
[1]1235
1236
[128]1237 /*@ end vars *************************************************** */
1238 assert_string_is_neither_NULL_nor_zerolength(config_file);
1239 assert_string_is_neither_NULL_nor_zerolength(label);
1240 assert(value != NULL);
1241 if (!does_file_exist(config_file)) {
[3191]1242 log_to_screen("(write_cfg_file) Cannot find %s config file", config_file);
[128]1243 return (1);
1244 }
[3185]1245 mr_asprintf(tempfile, "%s/mojo-jojo.blah", bkpinfo->tmpdir);
[128]1246 if (does_file_exist(config_file)) {
[3191]1247 mr_asprintf(command, "grep -vE '^%s .*$' %s > %s", label, config_file, tempfile);
[128]1248 paranoid_system(command);
[2518]1249 mr_free(command);
[128]1250 }
[3185]1251 mr_asprintf(command, "echo \"%s %s\" >> %s", label, value, tempfile);
[128]1252 paranoid_system(command);
[2518]1253 mr_free(command);
1254
[3185]1255 mr_asprintf(command, "mv -f %s %s", tempfile, config_file);
[128]1256 paranoid_system(command);
[2518]1257 mr_free(command);
[128]1258 unlink(tempfile);
[2518]1259 mr_free(tempfile);
[128]1260 return (0);
[1]1261}
1262
[541]1263
[1]1264/**
[541]1265 * The standard log_debug_msg() (log_msg() also due to a macro). Writes some describing
1266 * information to the logfile.
1267 */
[3191]1268void standard_log_debug_msg(int debug_level, const char *szFile, const char *szFunction, int nLine, const char *fmt, ...) {
1269
[541]1270 va_list args;
1271 static int depth = 0;
1272 FILE *fout;
1273
1274 if (depth > 5) {
1275 depth--;
1276 return;
1277 }
1278 depth++;
1279
1280 if (debug_level <= g_loglevel) {
1281 if (!(fout = fopen(MONDO_LOGFILE, "a"))) {
1282 return;
[3191]1283 }
[541]1284
1285 // add tabs to distinguish log levels
1286 if (debug_level > 0) {
[3191]1287 fprintf(fout, "DBG%d: ", debug_level);
[541]1288 if (getpid() == g_main_pid)
[2205]1289 fprintf(fout, "[Main] %s->%s#%d: ", szFile, szFunction, nLine);
[541]1290 else if (getpid() == g_buffer_pid && g_buffer_pid > 0)
[2205]1291 fprintf(fout, "[Buff] %s->%s#%d: ", szFile, szFunction, nLine);
[541]1292 else
[2205]1293 fprintf(fout, "[TH=%d] %s->%s#%d: ", getpid(), szFile, szFunction, nLine);
[3191]1294 } else {
1295 fprintf(fout, "INFO: ");
[541]1296 }
[2230]1297 va_start(args, fmt);
[541]1298 vfprintf(fout, fmt, args);
[2230]1299 va_end(args);
[541]1300
1301 fprintf(fout, "\n");
1302 paranoid_fclose(fout);
1303 }
1304 depth--;
1305}
1306
1307/**
1308 * Function pointer to the @c log_debug_msg function to use. Points to standard_log_debug_msg() by default.
1309 */
[3194]1310void (*log_debug_msg) (int, const char *, const char *, int, const char *, ...) = standard_log_debug_msg;
[541]1311
1312
1313/**
[1]1314 * Allocate or free important globals, depending on @p mal.
1315 * @param mal If TRUE, malloc; if FALSE, free.
1316 */
1317void do_libmondo_global_strings_thing(int mal)
1318{
[128]1319 if (mal) {
1320 malloc_string(g_boot_mountpt);
1321 malloc_string(g_mondo_home);
1322 malloc_string(g_magicdev_command);
1323 } else {
1324 paranoid_free(g_boot_mountpt);
1325 paranoid_free(g_mondo_home);
1326 paranoid_free(g_magicdev_command);
1327 }
[1]1328}
1329
1330/**
1331 * Allocate important globals.
1332 * @see do_libmondo_global_strings_thing
1333 */
1334void malloc_libmondo_global_strings(void)
1335{
[128]1336 do_libmondo_global_strings_thing(1);
[1]1337}
1338
1339/**
1340 * Free important globals.
1341 * @see do_libmondo_global_strings_thing
1342 */
1343void free_libmondo_global_strings(void)
1344{
[128]1345 do_libmondo_global_strings_thing(0);
[1]1346}
1347
1348
1349
1350/**
1351 * Stop @c magicdev if it's running.
1352 * The command used to start it is saved in @p g_magicdev_command.
1353 */
1354void stop_magicdev_if_necessary()
1355{
[3610]1356 char *tmp = NULL;
1357
1358 tmp = call_program_and_get_last_line_of_output("ps ax | grep -w magicdev | grep -v grep | tr -s '\t' ' '| cut -d' ' -f6-99");
1359 strcpy(g_magicdev_command, tmp);
[128]1360 if (g_magicdev_command[0]) {
1361 log_msg(1, "g_magicdev_command = '%s'", g_magicdev_command);
1362 paranoid_system("killall magicdev");
1363 }
[3610]1364 mr_free(tmp);
[1]1365}
1366
1367
1368/**
1369 * Restart magicdev if it was stopped.
1370 */
1371void restart_magicdev_if_necessary()
1372{
[2518]1373 char *tmp = NULL;
[128]1374
1375 if (g_magicdev_command && g_magicdev_command[0]) {
[3185]1376 mr_asprintf(tmp, "%s &", g_magicdev_command);
[128]1377 paranoid_system(tmp);
[2518]1378 mr_free(tmp);
[128]1379 }
[1]1380}
1381
1382/* @} - end of utilityGroup */
Note: See TracBrowser for help on using the repository browser.