source: MondoRescue/branches/stable/mondo/src/mondoarchive/mondoarchive.c@ 1422

Last change on this file since 1422 was 1422, checked in by Bruno Cornec, 17 years ago

mr_conf now handles # inside strings correctly (before they were considered as a comment which they are not !)

  • Property svn:keywords set to Id
File size: 17.0 KB
Line 
1/***************************************************************************
2* $Id: mondoarchive.c 1422 2007-05-09 09:02:24Z bruno $
3*/
4
5
6/**
7 * @file
8 * The main file for mondoarchive.
9 */
10
11/************************* #include statements *************************/
12#include <pthread.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <unistd.h>
18
19#include "my-stuff.h"
20#include "mondostructures.h"
21#include "libmondo.h"
22#include "mondo-cli-EXT.h"
23
24#include "mondoarchive.h"
25#include "mr_mem.h"
26#include "mr_str.h"
27#include "mr_msg.h"
28#include "mr_file.h"
29#include "mr_err.h"
30#include "mr_conf.h"
31
32// for CVS
33//static char cvsid[] = "$Id: mondoarchive.c 1422 2007-05-09 09:02:24Z bruno $";
34
35/************************* external variables *************************/
36extern void set_signals(int);
37extern int g_current_media_number;
38extern void register_pid(pid_t, char *);
39extern int g_currentY;
40extern bool g_text_mode;
41extern char *g_boot_mountpt;
42extern bool g_remount_cdrom_at_end, g_remount_floppy_at_end;
43extern char *g_tmpfs_mountpt;
44extern char *g_erase_tmpdir_and_scratchdir;
45extern char *g_cdrw_drive_is_here;
46extern double g_kernel_version;
47extern char *g_magicdev_command;
48extern t_bkptype g_backup_media_type;
49extern int g_loglevel;
50
51extern char *get_uname_m();
52
53static char *g_cdrom_drive_is_here = NULL;
54static char *g_dvd_drive_is_here = NULL;
55
56struct mr_ar_conf mr_conf;
57
58/***************** global vars ******************/
59bool g_skip_floppies;
60long diffs;
61
62/****************** subroutines used only here ******************/
63
64
65/**
66 * Print a "don't panic" message to the log and a message about the logfile to the screen.
67 */
68static void welcome_to_mondoarchive(void)
69{
70 char *tmp = NULL;
71
72 mr_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org", PACKAGE_VERSION);
73 mr_msg(0, "running %s binaries", get_architecture());
74 tmp = get_uname_m();
75 mr_msg(0, "running on %s architecture", tmp);
76 mr_free(tmp);
77 mr_msg(0, "-----------------------------------------------------------");
78 mr_msg(0, "NB: Mondo logs almost everything, so don't panic if you see");
79 mr_msg(0, "some error messages. Please read them carefully before you");
80 mr_msg(0, "decide to break out in a cold sweat. Despite (or perhaps");
81 mr_msg(0, "because of) the wealth of messages. some users are inclined");
82 mr_msg(0, "to stop reading this log. If Mondo stopped for some reason,");
83 mr_msg(0, "chances are it's detailed here. More than likely there's a");
84 mr_msg(0, "message at the very end of this log that will tell you what");
85 mr_msg(0, "is wrong. Please read it! -Devteam");
86 mr_msg(0, "-----------------------------------------------------------");
87
88 mr_msg(0, "Zero...");
89 mr_msg(1, "One...");
90 mr_msg(2, "Two...");
91 mr_msg(3, "Three...");
92 mr_msg(4, "Four...");
93 mr_msg(5, "Five...");
94 mr_msg(6, "Six...");
95 mr_msg(7, "Seven...");
96 mr_msg(8, "Eight...");
97 printf("See %s for details of backup run.\n", MONDO_LOGFILE);
98}
99
100/**
101 * Do whatever is necessary to insure a successful backup on the Linux distribution
102 * of the day.
103 */
104static void distro_specific_kludges_at_start_of_mondoarchive(void)
105{
106 mr_msg(2, "Unmounting old ramdisks if necessary");
107 stop_magicdev_if_necessary(); // for RH+Gnome users
108 run_program_and_log_output
109 ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
110 unmount_supermounts_if_necessary(); // for Mandrake users whose CD-ROMs are supermounted
111 mount_boot_if_necessary(); // for Gentoo users with non-mounted /boot partitions
112 clean_up_KDE_desktop_if_necessary(); // delete various misc ~/.* files that get in the way
113}
114
115
116/**
117 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
118 */
119static void distro_specific_kludges_at_end_of_mondoarchive(void)
120{
121 mr_msg(2, "Restarting magicdev if necessary");
122 sync();
123 restart_magicdev_if_necessary(); // for RH+Gnome users
124
125 mr_msg(2, "Restarting supermounts if necessary");
126 sync();
127 remount_supermounts_if_necessary(); // for Mandrake users
128
129 mr_msg(2, "Unmounting /boot if necessary");
130 sync();
131 unmount_boot_if_necessary(); // for Gentoo users
132}
133
134/* create the mr_ar_conf structure from mondo's conf file */
135static void mr_ar_store_conf(struct mr_ar_conf *mr_cnf) {
136
137 char *p = NULL;
138 char *s = NULL;
139
140 s = mr_conf_sread("mondo_iso_creation_cmd");
141 fprintf(stderr,"s: %s\n",s);
142 mr_asprintf(&p, s);
143 mr_cnf->iso_creation_cmd = p;
144 p = NULL;
145
146 mr_asprintf(&p, mr_conf_sread("mondo_iso_creation_options"));
147 mr_cnf->iso_creation_options = p;
148 p = NULL;
149
150 mr_asprintf(&p, mr_conf_sread("mondo_iso_burning_cmd"));
151 mr_cnf->iso_burning_cmd = p;
152 p = NULL;
153
154 mr_asprintf(&p, mr_conf_sread("mondo_iso_burning_options"));
155 mr_cnf->iso_burning_options = p;
156 p = NULL;
157
158 mr_cnf->iso_burning_speed = mr_conf_iread("mondo_iso_burning_speed");
159 mr_cnf->media_size = mr_conf_iread("mondo_media_size");
160
161 mr_asprintf(&p, mr_conf_sread("mondo_media_device"));
162 mr_cnf->media_device = p;
163 p = NULL;
164
165 mr_cnf->manual_tray = mr_conf_bread("mondo_manual_tray");
166 mr_cnf->log_level = mr_conf_iread("mondo_log_level");
167
168 mr_asprintf(&p, mr_conf_sread("mondo_prefix"));
169 mr_cnf->prefix = p;
170 p = NULL;
171
172 mr_cnf->external_tape_blocksize = mr_conf_iread("mondo_external_tape_blocksize");
173 mr_cnf->internal_tape_blocksize = mr_conf_iread("mondo_internal_tape_blocksize");
174 mr_cnf->slice_size = mr_conf_iread("mondo_slice_size");
175
176 mr_asprintf(&p, mr_conf_sread("mondo_deplist_file"));
177 mr_cnf->deplist_file = p;
178 p = NULL;
179
180 mr_cnf->write_boot_floppy = mr_conf_bread("mondo_write_boot_floppy");
181 mr_cnf->create_mindi_cd = mr_conf_bread("mondo_create_mindi_cd");
182
183 mr_asprintf(&p, mr_conf_sread("mondo_kernel"));
184 mr_cnf->kernel = p;
185 p = NULL;
186
187 mr_asprintf(&p, mr_conf_sread("mondo_additional_modules"));
188 mr_cnf->additional_modules = p;
189 p = NULL;
190
191 mr_asprintf(&p, mr_conf_sread("mondo_boot_loader"));
192 mr_cnf->boot_loader = p;
193 p = NULL;
194
195 mr_cnf->differential = mr_conf_bread("mondo_differential");
196
197 mr_asprintf(&p, mr_conf_sread("mondo_compression_tool"));
198 mr_cnf->compression_tool = p;
199 p = NULL;
200
201 mr_cnf->compression_level = mr_conf_iread("mondo_compression_level");
202
203 mr_asprintf(&p, mr_conf_sread("mondo_exclude_paths"));
204 mr_cnf->exclude_paths = p;
205 p = NULL;
206
207 mr_asprintf(&p, mr_conf_sread("mondo_include_paths"));
208 mr_cnf->include_paths = p;
209 p = NULL;
210
211 mr_asprintf(&p, mr_conf_sread("mondo_ui_mode"));
212 mr_cnf->ui_mode = p;
213 p = NULL;
214
215 mr_cnf->automatic_restore = mr_conf_bread("mondo_automatic_restore");
216
217 mr_asprintf(&p, mr_conf_sread("mondo_scratch_dir"));
218 mr_cnf->scratch_dir = p;
219 p = NULL;
220
221 mr_asprintf(&p, mr_conf_sread("mondo_tmp_dir"));
222 mr_cnf->tmp_dir = p;
223 p = NULL;
224
225 mr_asprintf(&p, mr_conf_sread("mondo_images_dir"));
226 mr_cnf->images_dir = p;
227 p = NULL;
228 mr_msg(5, "Directory for images is %s", mr_cnf->images_dir);
229}
230
231/* destroy the mr_ar_conf structure's content */
232static void mr_ar_clean_conf(struct mr_ar_conf *mr_cnf) {
233
234 if (mr_cnf == NULL) {
235 return;
236 }
237 mr_free(mr_cnf->iso_creation_cmd);
238 mr_free(mr_cnf->iso_creation_options);
239 mr_free(mr_cnf->iso_burning_cmd);
240 mr_free(mr_cnf->iso_burning_options);
241 mr_free(mr_cnf->media_device);
242 mr_free(mr_cnf->prefix);
243 mr_free(mr_cnf->deplist_file);
244 mr_free(mr_cnf->kernel);
245 mr_free(mr_cnf->additional_modules);
246 mr_free(mr_cnf->boot_loader);
247 mr_free(mr_cnf->compression_tool);
248 mr_free(mr_cnf->exclude_paths);
249 mr_free(mr_cnf->include_paths);
250 mr_free(mr_cnf->ui_mode);
251 mr_free(mr_cnf->scratch_dir);
252 mr_free(mr_cnf->tmp_dir);
253 mr_free(mr_cnf->images_dir);
254}
255
256/* Create the pointer to the function called in mr_exit */
257void (*mr_cleanup)(void) = NULL;
258
259/* Just for init */
260void mr_ar_cleanup_empty(void) {
261}
262
263/* Cleanup all memory allocated in various structures */
264void mr_ar_cleanup(void) {
265 /* Highly incomplete function for the moment */
266 /* We have to free all allocated memory */
267 mr_ar_clean_conf(&mr_conf);
268 /* We have to remove all temporary files */
269 /* We have to unmount what has been mounted */
270 /* We have to properly end newt */
271 /* We have to remind people of log files */
272
273 mr_msg_close();
274}
275
276/*-----------------------------------------------------------*/
277
278
279
280/**
281 * Backup/verify the user's data.
282 * What did you think it did, anyway? :-)
283 */
284int main(int argc, char *argv[])
285{
286 struct s_bkpinfo *bkpinfo = NULL;
287 struct stat stbuf;
288 char *tmp = NULL;
289 int res = 0;
290 int retval = 0;
291 char *say_at_end = NULL;
292 char *say_at_end2 = NULL;
293
294#ifdef ENABLE_NLS
295 setlocale(LC_ALL, "");
296 (void) textdomain("mondo");
297#endif
298 printf(_("Initializing..."));
299
300 /* Reference a dummy cleanup function for mr_exit temporarily */
301 mr_cleanup = &mr_ar_cleanup_empty;
302
303 /* initialize log file with time stamp */
304 /* We start with a loglevel of 4 - Adapted later on */
305 /* It's mandatory to set this up first as all mr_ functions rely on it */
306 unlink(MONDO_LOGFILE);
307 mr_msg_init(MONDO_LOGFILE,4);
308 mr_msg(0, _("Time started: %s"), mr_date());
309
310 /* Make sure I'm root; abort if not */
311 if (getuid() != 0) {
312 mr_log_exit(127, _("Please run as root."));
313 }
314
315 /* If -V, -v or --version then echo version no. and quit */
316 if (argc == 2
317 && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
318 || !strcmp(argv[argc - 1], "--version"))) {
319 printf(_("mondoarchive v%s\nSee man page for help\n"), PACKAGE_VERSION);
320 mr_exit(0, NULL);
321 }
322
323 /* Conf file management */
324 /* Check md5 sum before */
325 /* Get content */
326 if (mr_conf_open(MONDO_CONF_DIR"/mondo.conf.dist") != 0) {
327 mr_log_exit(-1, "Unable to open "MONDO_CONF_DIR"/mondo.conf.dist");
328 }
329 mr_ar_store_conf(&mr_conf);
330 /* Reference the right cleanup function for mr_exit now it's allocated */
331 mr_cleanup = &mr_ar_cleanup;
332
333 mr_conf_close();
334
335 /* Add MONDO_SHARE + other environment variables for mindi */
336 setenv_mondo_var();
337
338 /* Initialize variables */
339 malloc_libmondo_global_strings();
340 diffs = 0;
341 bkpinfo = mr_malloc(sizeof(struct s_bkpinfo));
342 if (stat(MONDO_CACHE, &stbuf) != 0) {
343 mr_mkdir(MONDO_CACHE,0x755);
344 }
345
346 /* BERLIOS: Hardcoded to be improved */
347 unlink(MONDO_CACHE"/mindi.conf");
348 unlink(MONDORESTORECFG);
349
350 /* Configure the bkpinfo structure, global file paths, etc. */
351 g_main_pid = getpid();
352 mr_msg(9, "This");
353
354 register_pid(g_main_pid, "mondo");
355 set_signals(TRUE); // catch SIGTERM, etc.
356 run_program_and_log_output("dmesg -n1", TRUE);
357
358 mr_msg(9, "Next");
359 welcome_to_mondoarchive();
360 distro_specific_kludges_at_start_of_mondoarchive();
361 g_kernel_version = get_kernel_version();
362
363 if (argc == 4 && !strcmp(argv[1], "getfattr")) {
364 g_loglevel = 10;
365 g_text_mode = TRUE;
366 setup_newt_stuff();
367 if (!strstr(argv[2], "filelist")) {
368 mr_msg(1,_("Sorry - filelist goes first\n"));
369 finish(1);
370 } else {
371 finish(get_fattr_list(argv[2], argv[3]));
372 }
373 finish(0);
374 }
375 if (argc == 4 && !strcmp(argv[1], "setfattr")) {
376 g_loglevel = 10;
377 g_text_mode = TRUE;
378 setup_newt_stuff();
379 finish(set_fattr_list(argv[2], argv[3]));
380 }
381
382 if (argc == 3 && !strcmp(argv[1], "wildcards")) {
383 g_loglevel = 10;
384 g_text_mode = TRUE;
385 setup_newt_stuff();
386 tmp = mr_stresc(argv[2], WILDCHARS, BACKSLASH);
387 printf("in=%s; out=%s\n", argv[2], tmp);
388 mr_free(tmp);
389 finish(1);
390 }
391
392 if (argc == 4 && !strcmp(argv[1], "getfacl")) {
393 g_loglevel = 10;
394 g_text_mode = TRUE;
395 setup_newt_stuff();
396 if (!strstr(argv[2], "filelist")) {
397 mr_msg(1,_("Sorry - filelist goes first\n"));
398 finish(1);
399 } else {
400 finish(get_acl_list(argv[2], argv[3]));
401 }
402 finish(0);
403 }
404 if (argc == 4 && !strcmp(argv[1], "setfacl")) {
405 g_loglevel = 10;
406 g_text_mode = TRUE;
407 setup_newt_stuff();
408 finish(set_acl_list(argv[2], argv[3]));
409 }
410
411 if (argc > 2 && !strcmp(argv[1], "find-cd")) {
412 g_loglevel = 10;
413 g_text_mode = TRUE;
414 setup_newt_stuff();
415 malloc_string(tmp);
416 if (find_cdrw_device(tmp)) {
417 mr_msg(1,_("Failed to find CDR-RW drive\n"));
418 } else {
419 mr_msg(1,_("CD-RW is at %s\n"), tmp);
420 }
421 tmp[0] = '\0';
422 if (find_cdrom_device(tmp, atoi(argv[2]))) {
423 mr_msg(1,_("Failed to find CD-ROM drive\n"));
424 } else {
425 mr_msg(1,_("CD-ROM is at %s\n"), tmp);
426 }
427 mr_free(tmp);
428 finish(0);
429 }
430
431 if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
432 g_loglevel = 10;
433 g_text_mode = TRUE;
434 setup_newt_stuff();
435 malloc_string(tmp);
436 if (find_dvd_device(tmp, atoi(argv[2]))) {
437 mr_msg(1,_("Failed to find DVD drive\n"));
438 } else {
439 mr_msg(1,_("DVD is at %s\n"), tmp);
440 }
441 mr_free(tmp);
442 finish(0);
443 }
444
445 if (argc > 2 && !strcmp(argv[1], "disksize")) {
446 printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
447 finish(0);
448 }
449 if (argc > 2 && !strcmp(argv[1], "test-dev")) {
450 if (is_dev_an_NTFS_dev(argv[2])) {
451 mr_msg(1,_("%s is indeed an NTFS dev\n"), argv[2]);
452 } else {
453 mr_msg(1,_("%s is _not_ an NTFS dev\n"), argv[2]);
454 }
455 finish(0);
456 }
457
458 if (pre_param_configuration(bkpinfo)) {
459 fatal_error
460 ("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
461 }
462 sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
463 bkpinfo->scratchdir);
464
465 /* Process command line, if there is one. If not, ask user for info. */
466 if (argc == 1) {
467 g_text_mode = FALSE;
468 setup_newt_stuff();
469 res = interactively_obtain_media_parameters_from_user(bkpinfo, TRUE); /* yes, archiving */
470 if (res) {
471 fatal_error
472 ("Syntax error. Please review the parameters you have supplied and try again.");
473 }
474 } else {
475 res = handle_incoming_parameters(argc, argv, bkpinfo);
476 if (res) {
477 mr_msg(1,
478 _("Errors were detected in the command line you supplied.\n"));
479 mr_msg(1,_("Please review the log file - %s \n"),MONDO_LOGFILE);
480 mr_msg(1, "Mondoarchive will now exit.");
481 finish(1);
482 }
483 setup_newt_stuff();
484 }
485
486/* Finish configuring global structures */
487 if (post_param_configuration(bkpinfo)) {
488 fatal_error
489 ("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
490 }
491
492 log_to_screen
493 (_("BusyBox's sources are available from http://www.busybox.net"));
494
495 sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
496 bkpinfo->scratchdir);
497
498 /* If we're meant to backup then backup */
499 if (bkpinfo->backup_data) {
500 res = backup_data(bkpinfo);
501 retval += res;
502 if (res) {
503 mr_strcat(say_at_end,
504 _("Data archived. Please check the logs, just as a precaution. "));
505 } else {
506 mr_strcat(say_at_end, _("Data archived OK. "));
507 }
508 }
509
510 /* If we're meant to verify then verify */
511 if (bkpinfo->verify_data) {
512 res = verify_data(bkpinfo);
513 if (res < 0) {
514 mr_asprintf(&say_at_end2, _("%d difference%c found."), -res,
515 (-res != 1) ? 's' : ' ');
516 res = 0;
517 }
518 retval += res;
519 }
520
521 /* Offer to write floppy disk images to physical disks */
522 if (bkpinfo->backup_data && !g_skip_floppies) {
523 res = offer_to_write_boot_floppies_to_physical_disks(bkpinfo);
524 retval += res;
525 }
526
527 /* Report result of entire operation (success? errors?) */
528 if (!retval) {
529 mvaddstr_and_log_it(g_currentY++, 0,
530 _("Backup and/or verify ran to completion. Everything appears to be fine."));
531 } else {
532 mvaddstr_and_log_it(g_currentY++, 0,
533 _("Backup and/or verify ran to completion. However, errors did occur."));
534 }
535
536 if (does_file_exist("/var/cache/mindi/mondorescue.iso")) {
537 log_to_screen
538 (_("/var/cache/mindi/mondorescue.iso, a boot/utility CD, is available if you want it."));
539 }
540
541
542 if (length_of_file("/tmp/changed.files") > 2) {
543 if (g_text_mode) {
544 log_to_screen
545 (_("Type 'less /tmp/changed.files' to see which files don't match the archives"));
546 } else {
547 mr_msg(1,
548 _("Type 'less /tmp/changed.files' to see which files don't match the archives"));
549 mr_msg(2, "Calling popup_changelist_from_file()");
550 popup_changelist_from_file("/tmp/changed.files");
551 mr_msg(2, "Returned from popup_changelist_from_file()");
552 }
553 } else {
554 unlink("/tmp/changed.files");
555 }
556 log_to_screen(say_at_end);
557 mr_free(say_at_end);
558 if (say_at_end2 != NULL) {
559 log_to_screen(say_at_end2);
560 mr_free(say_at_end2);
561 }
562
563 mr_asprintf(&tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
564 run_program_and_log_output(tmp, TRUE);
565 mr_free(tmp);
566 run_program_and_log_output(g_erase_tmpdir_and_scratchdir, TRUE);
567
568 run_program_and_log_output("mount", 2);
569
570 system("rm -f /var/cache/mondo-archive/last-backup.aborted");
571 system("rm -Rf /tmp.mondo.* /mondo.scratch.*");
572 if (!retval) {
573 printf(_("Mondoarchive ran OK.\n"));
574 } else {
575 printf(_("Errors occurred during backup. Please check logfile.\n"));
576 }
577 distro_specific_kludges_at_end_of_mondoarchive();
578 register_pid(0, "mondo");
579 set_signals(FALSE);
580 chdir("/tmp"); // just in case there's something wrong with g_erase_tmpdir_and_scratchdir
581 system(g_erase_tmpdir_and_scratchdir);
582 free_libmondo_global_strings();
583 mr_free(bkpinfo);
584
585 unlink("/tmp/filelist.full");
586 unlink("/tmp/filelist.full.gz");
587
588 if (!g_cdrom_drive_is_here) {
589 mr_msg(10, "FYI, g_cdrom_drive_is_here was never used");
590 }
591 if (!g_dvd_drive_is_here) {
592 mr_msg(10, "FYI, g_dvd_drive_is_here was never used");
593 }
594
595 /* finalize log file with time stamp */
596 mr_msg(1, "Time finished: %s", mr_date());
597 mr_msg_close();
598
599 if (!g_text_mode) {
600 popup_and_OK
601 (_("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt."));
602 log_to_screen(_("See %s for details of backup run."), MONDO_LOGFILE);
603 finish(retval);
604 } else {
605 printf(_("See %s for details of backup run.\n"), MONDO_LOGFILE);
606 mr_exit(retval, NULL);
607 }
608
609 return EXIT_SUCCESS;
610}
Note: See TracBrowser for help on using the repository browser.