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