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

Last change on this file since 1438 was 1438, checked in by Bruno Cornec, 13 years ago

Merge Andree's patch (2.2.4 rev [1437] around MINDI_CACHE define)
Announce for LinuxDay? added

  • Property svn:keywords set to Id
File size: 16.9 KB
Line 
1/***************************************************************************
2* $Id: mondoarchive.c 1438 2007-05-11 23:55:13Z 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 1438 2007-05-11 23:55:13Z 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
139    mr_asprintf(&p, mr_conf_sread("mondo_iso_creation_cmd"));
140    mr_cnf->iso_creation_cmd = p;
141    p = NULL;
142
143    mr_asprintf(&p, mr_conf_sread("mondo_iso_creation_options"));
144    mr_cnf->iso_creation_options = p;
145    p = NULL;
146
147    mr_asprintf(&p, mr_conf_sread("mondo_iso_burning_cmd"));
148    mr_cnf->iso_burning_cmd = p;
149    p = NULL;
150
151    mr_asprintf(&p, mr_conf_sread("mondo_iso_burning_options"));
152    mr_cnf->iso_burning_options = p;
153    p = NULL;
154
155    mr_cnf->iso_burning_speed = mr_conf_iread("mondo_iso_burning_speed");
156    mr_cnf->media_size = mr_conf_iread("mondo_media_size");
157
158    mr_asprintf(&p, mr_conf_sread("mondo_media_device"));
159    mr_cnf->media_device = p;
160    p = NULL;
161
162    mr_cnf->manual_tray = mr_conf_bread("mondo_manual_tray");
163    mr_cnf->log_level = mr_conf_iread("mondo_log_level");
164
165    mr_asprintf(&p, mr_conf_sread("mondo_prefix"));
166    mr_cnf->prefix = p;
167    p = NULL;
168
169    mr_cnf->external_tape_blocksize = mr_conf_iread("mondo_external_tape_blocksize");
170    mr_cnf->internal_tape_blocksize = mr_conf_iread("mondo_internal_tape_blocksize");
171    mr_cnf->slice_size = mr_conf_iread("mondo_slice_size");
172
173    mr_asprintf(&p, mr_conf_sread("mondo_deplist_file"));
174    mr_cnf->deplist_file = p;
175    p = NULL;
176
177    mr_cnf->write_boot_floppy = mr_conf_bread("mondo_write_boot_floppy");
178    mr_cnf->create_mindi_cd = mr_conf_bread("mondo_create_mindi_cd");
179
180    mr_asprintf(&p, mr_conf_sread("mondo_kernel"));
181    mr_cnf->kernel = p;
182    p = NULL;
183
184    mr_asprintf(&p, mr_conf_sread("mondo_additional_modules"));
185    mr_cnf->additional_modules = p;
186    p = NULL;
187
188    mr_asprintf(&p, mr_conf_sread("mondo_boot_loader"));
189    mr_cnf->boot_loader = p;
190    p = NULL;
191
192    mr_cnf->differential = mr_conf_bread("mondo_differential");
193
194    mr_asprintf(&p, mr_conf_sread("mondo_compression_tool"));
195    mr_cnf->compression_tool = p;
196    p = NULL;
197
198    mr_cnf->compression_level = mr_conf_iread("mondo_compression_level");
199
200    mr_asprintf(&p, mr_conf_sread("mondo_exclude_paths"));
201    mr_cnf->exclude_paths = p;
202    p = NULL;
203
204    mr_asprintf(&p, mr_conf_sread("mondo_include_paths"));
205    mr_cnf->include_paths = p;
206    p = NULL;
207
208    mr_asprintf(&p, mr_conf_sread("mondo_ui_mode"));
209    mr_cnf->ui_mode = p;
210    p = NULL;
211
212    mr_cnf->automatic_restore = mr_conf_bread("mondo_automatic_restore");
213
214    mr_asprintf(&p, mr_conf_sread("mondo_scratch_dir"));
215    mr_cnf->scratch_dir = p;
216    p = NULL;
217
218    mr_asprintf(&p, mr_conf_sread("mondo_tmp_dir"));
219    mr_cnf->tmp_dir = p;
220    p = NULL;
221
222    mr_asprintf(&p, mr_conf_sread("mondo_images_dir"));
223    mr_cnf->images_dir = p;
224    p = NULL;
225    mr_msg(5, "Directory for images is %s", mr_cnf->images_dir);
226}
227
228/* destroy the mr_ar_conf structure's content */
229static void mr_ar_clean_conf(struct mr_ar_conf *mr_cnf) {
230   
231    if (mr_cnf == NULL) {
232        return;
233    }
234    mr_free(mr_cnf->iso_creation_cmd);
235    mr_free(mr_cnf->iso_creation_options);
236    mr_free(mr_cnf->iso_burning_cmd);
237    mr_free(mr_cnf->iso_burning_options);
238    mr_free(mr_cnf->media_device);
239    mr_free(mr_cnf->prefix);
240    mr_free(mr_cnf->deplist_file);
241    mr_free(mr_cnf->kernel);
242    mr_free(mr_cnf->additional_modules);
243    mr_free(mr_cnf->boot_loader);
244    mr_free(mr_cnf->compression_tool);
245    mr_free(mr_cnf->exclude_paths);
246    mr_free(mr_cnf->include_paths);
247    mr_free(mr_cnf->ui_mode);
248    mr_free(mr_cnf->scratch_dir);
249    mr_free(mr_cnf->tmp_dir);
250    mr_free(mr_cnf->images_dir);
251}
252
253/* Create the pointer to the function called in mr_exit */
254void (*mr_cleanup)(void) = NULL;
255
256/* Just for init */
257void mr_ar_cleanup_empty(void) {
258}
259
260/* Cleanup all memory allocated in various structures */
261void mr_ar_cleanup(void) {
262    /* Highly incomplete function for the moment */
263    /* We have to free all allocated memory */
264    mr_ar_clean_conf(&mr_conf);
265    /* We have to remove all temporary files */
266    /* We have to unmount what has been mounted */
267    /* We have to properly end newt */
268    /* We have to remind people of log files */
269
270    mr_msg_close();
271}
272
273/*-----------------------------------------------------------*/
274
275
276
277/**
278 * Backup/verify the user's data.
279 * What did you think it did, anyway? :-)
280 */
281int main(int argc, char *argv[])
282{
283    struct s_bkpinfo *bkpinfo = NULL;
284    struct stat stbuf;
285    char *tmp = NULL;
286    int res = 0;
287    int retval = 0;
288    char *say_at_end = NULL;
289    char *say_at_end2 = NULL;
290
291#ifdef ENABLE_NLS
292    setlocale(LC_ALL, "");
293    (void) textdomain("mondo");
294#endif
295    printf(_("Initializing..."));
296
297    /* Reference a dummy cleanup function for mr_exit temporarily */
298    mr_cleanup = &mr_ar_cleanup_empty;
299
300    /* initialize log file with time stamp */
301    /* We start with a loglevel of 4 - Adapted later on */
302    /* It's mandatory to set this up first as all mr_ functions rely on it */
303    unlink(MONDO_LOGFILE);
304    mr_msg_init(MONDO_LOGFILE,4);
305    mr_msg(0, _("Time started: %s"), mr_date());
306
307    /* Make sure I'm root; abort if not */
308    if (getuid() != 0) {
309        mr_log_exit(127, _("Please run as root."));
310    }
311
312    /* If -V, -v or --version then echo version no. and quit */
313    if (argc == 2
314        && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
315            || !strcmp(argv[argc - 1], "--version"))) {
316        printf(_("mondoarchive v%s\nSee man page for help\n"), PACKAGE_VERSION);
317        mr_exit(0, NULL);
318    }
319
320    /* Conf file management */
321    /* Check md5 sum before */
322    /* Get content */
323    if (mr_conf_open(MONDO_CONF_DIR"/mondo.conf.dist") != 0) {
324            mr_log_exit(-1, "Unable to open "MONDO_CONF_DIR"/mondo.conf.dist");
325    }
326    mr_ar_store_conf(&mr_conf);
327    /* Reference the right cleanup function for mr_exit now it's allocated */
328    mr_cleanup = &mr_ar_cleanup;
329
330    mr_conf_close();
331
332    /* Add MONDO_SHARE + other environment variables for mindi */
333    setenv_mondo_var();
334
335    /* Initialize variables */
336    malloc_libmondo_global_strings();
337    diffs = 0;
338    bkpinfo = mr_malloc(sizeof(struct s_bkpinfo));
339    if (stat(MONDO_CACHE, &stbuf) != 0) {
340        mr_mkdir(MONDO_CACHE,0x755);
341    }
342
343    /* BERLIOS: Hardcoded to be improved */
344    unlink(MONDO_CACHE"/mindi.conf");
345    unlink(MONDORESTORECFG);
346
347    /* Configure the bkpinfo structure, global file paths, etc. */
348    g_main_pid = getpid();
349    mr_msg(9, "This");
350
351    register_pid(g_main_pid, "mondo");
352    set_signals(TRUE);          // catch SIGTERM, etc.
353    run_program_and_log_output("dmesg -n1", TRUE);
354
355    mr_msg(9, "Next");
356    welcome_to_mondoarchive();
357    distro_specific_kludges_at_start_of_mondoarchive();
358    g_kernel_version = get_kernel_version();
359
360    if (argc == 4 && !strcmp(argv[1], "getfattr")) {
361        g_loglevel = 10;
362        g_text_mode = TRUE;
363        setup_newt_stuff();
364        if (!strstr(argv[2], "filelist")) {
365            mr_msg(1,_("Sorry - filelist goes first\n"));
366            finish(1);
367        } else {
368            finish(get_fattr_list(argv[2], argv[3]));
369        }
370        finish(0);
371    }
372    if (argc == 4 && !strcmp(argv[1], "setfattr")) {
373        g_loglevel = 10;
374        g_text_mode = TRUE;
375        setup_newt_stuff();
376        finish(set_fattr_list(argv[2], argv[3]));
377    }
378
379    if (argc == 3 && !strcmp(argv[1], "wildcards")) {
380        g_loglevel = 10;
381        g_text_mode = TRUE;
382        setup_newt_stuff();
383        tmp = mr_stresc(argv[2], WILDCHARS, BACKSLASH);
384        printf("in=%s; out=%s\n", argv[2], tmp);
385        mr_free(tmp);
386        finish(1);
387    }
388
389    if (argc == 4 && !strcmp(argv[1], "getfacl")) {
390        g_loglevel = 10;
391        g_text_mode = TRUE;
392        setup_newt_stuff();
393        if (!strstr(argv[2], "filelist")) {
394            mr_msg(1,_("Sorry - filelist goes first\n"));
395            finish(1);
396        } else {
397            finish(get_acl_list(argv[2], argv[3]));
398        }
399        finish(0);
400    }
401    if (argc == 4 && !strcmp(argv[1], "setfacl")) {
402        g_loglevel = 10;
403        g_text_mode = TRUE;
404        setup_newt_stuff();
405        finish(set_acl_list(argv[2], argv[3]));
406    }
407
408    if (argc > 2 && !strcmp(argv[1], "find-cd")) {
409        g_loglevel = 10;
410        g_text_mode = TRUE;
411        setup_newt_stuff();
412        malloc_string(tmp);
413        if (find_cdrw_device(tmp)) {
414            mr_msg(1,_("Failed to find CDR-RW drive\n"));
415        } else {
416            mr_msg(1,_("CD-RW is at %s\n"), tmp);
417        }
418        tmp[0] = '\0';
419        if (find_cdrom_device(tmp, atoi(argv[2]))) {
420            mr_msg(1,_("Failed to find CD-ROM drive\n"));
421        } else {
422            mr_msg(1,_("CD-ROM is at %s\n"), tmp);
423        }
424        mr_free(tmp);
425        finish(0);
426    }
427
428    if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
429        g_loglevel = 10;
430        g_text_mode = TRUE;
431        setup_newt_stuff();
432        malloc_string(tmp);
433        if (find_dvd_device(tmp, atoi(argv[2]))) {
434            mr_msg(1,_("Failed to find DVD drive\n"));
435        } else {
436            mr_msg(1,_("DVD is at %s\n"), tmp);
437        }
438        mr_free(tmp);
439        finish(0);
440    }
441
442    if (argc > 2 && !strcmp(argv[1], "disksize")) {
443        printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
444        finish(0);
445    }
446    if (argc > 2 && !strcmp(argv[1], "test-dev")) {
447        if (is_dev_an_NTFS_dev(argv[2])) {
448            mr_msg(1,_("%s is indeed an NTFS dev\n"), argv[2]);
449        } else {
450            mr_msg(1,_("%s is _not_ an NTFS dev\n"), argv[2]);
451        }
452        finish(0);
453    }
454
455    if (pre_param_configuration(bkpinfo)) {
456        fatal_error
457            ("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
458    }
459    sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
460            bkpinfo->scratchdir);
461
462    /* Process command line, if there is one. If not, ask user for info. */
463    if (argc == 1) {
464        g_text_mode = FALSE;
465        setup_newt_stuff();
466        res = interactively_obtain_media_parameters_from_user(bkpinfo, TRUE);   /* yes, archiving */
467        if (res) {
468            fatal_error
469                ("Syntax error. Please review the parameters you have supplied and try again.");
470        }
471    } else {
472        res = handle_incoming_parameters(argc, argv, bkpinfo);
473        if (res) {
474            mr_msg(1,
475                _("Errors were detected in the command line you supplied.\n"));
476            mr_msg(1,_("Please review the log file - %s \n"),MONDO_LOGFILE);
477            mr_msg(1, "Mondoarchive will now exit.");
478            finish(1);
479        }
480        setup_newt_stuff();
481    }
482
483/* Finish configuring global structures */
484    if (post_param_configuration(bkpinfo)) {
485        fatal_error
486            ("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
487    }
488
489    log_to_screen
490        (_("BusyBox's sources are available from http://www.busybox.net"));
491
492    sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
493            bkpinfo->scratchdir);
494
495    /* If we're meant to backup then backup */
496    if (bkpinfo->backup_data) {
497        res = backup_data(bkpinfo);
498        retval += res;
499        if (res) {
500            mr_strcat(say_at_end,
501                   _("Data archived. Please check the logs, just as a precaution. "));
502        } else {
503            mr_strcat(say_at_end, _("Data archived OK. "));
504        }
505    }
506
507    /* If we're meant to verify then verify */
508    if (bkpinfo->verify_data) {
509        res = verify_data(bkpinfo);
510        if (res < 0) {
511            mr_asprintf(&say_at_end2, _("%d difference%c found."), -res,
512                    (-res != 1) ? 's' : ' ');
513            res = 0;
514        }
515        retval += res;
516    }
517
518    /* Offer to write floppy disk images to physical disks */
519    if (bkpinfo->backup_data && !g_skip_floppies) {
520        res = offer_to_write_boot_floppies_to_physical_disks(bkpinfo);
521        retval += res;
522    }
523
524    /* Report result of entire operation (success? errors?) */
525    if (!retval) {
526        mvaddstr_and_log_it(g_currentY++, 0,
527                            _("Backup and/or verify ran to completion. Everything appears to be fine."));
528    } else {
529        mvaddstr_and_log_it(g_currentY++, 0,
530                            _("Backup and/or verify ran to completion. However, errors did occur."));
531    }
532
533    if (does_file_exist(MINDI_CACHE"/mondorescue.iso")) {
534        log_to_screen
535            (_(MINDI_CACHE"/mondorescue.iso, a boot/utility CD, is available if you want it."));
536    }
537
538
539    if (length_of_file("/tmp/changed.files") > 2) {
540        if (g_text_mode) {
541            log_to_screen
542                (_("Type 'less /tmp/changed.files' to see which files don't match the archives"));
543        } else {
544            mr_msg(1,
545                    _("Type 'less /tmp/changed.files' to see which files don't match the archives"));
546            mr_msg(2, "Calling popup_changelist_from_file()");
547            popup_changelist_from_file("/tmp/changed.files");
548            mr_msg(2, "Returned from popup_changelist_from_file()");
549        }
550    } else {
551        unlink("/tmp/changed.files");
552    }
553    log_to_screen(say_at_end);
554    mr_free(say_at_end);
555    if (say_at_end2 != NULL) {
556        log_to_screen(say_at_end2);
557        mr_free(say_at_end2);
558    }
559
560    mr_asprintf(&tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
561    run_program_and_log_output(tmp, TRUE);
562    mr_free(tmp);
563    run_program_and_log_output(g_erase_tmpdir_and_scratchdir, TRUE);
564
565    run_program_and_log_output("mount", 2);
566
567    system("rm -f /var/cache/mondo-archive/last-backup.aborted");
568    system("rm -Rf /tmp.mondo.* /mondo.scratch.*");
569    if (!retval) {
570        printf(_("Mondoarchive ran OK.\n"));
571    } else {
572        printf(_("Errors occurred during backup. Please check logfile.\n"));
573    }
574    distro_specific_kludges_at_end_of_mondoarchive();
575    register_pid(0, "mondo");
576    set_signals(FALSE);
577    chdir("/tmp");              // just in case there's something wrong with g_erase_tmpdir_and_scratchdir
578    system(g_erase_tmpdir_and_scratchdir);
579    free_libmondo_global_strings();
580    mr_free(bkpinfo);
581
582    unlink("/tmp/filelist.full");
583    unlink("/tmp/filelist.full.gz");
584
585    if (!g_cdrom_drive_is_here) {
586        mr_msg(10, "FYI, g_cdrom_drive_is_here was never used");
587    }
588    if (!g_dvd_drive_is_here) {
589        mr_msg(10, "FYI, g_dvd_drive_is_here was never used");
590    }
591
592    /* finalize log file with time stamp */
593    mr_msg(1, "Time finished: %s", mr_date());
594    mr_msg_close();
595
596    if (!g_text_mode) {
597        popup_and_OK
598            (_("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt."));
599        log_to_screen(_("See %s for details of backup run."), MONDO_LOGFILE);
600        finish(retval);
601    } else {
602        printf(_("See %s for details of backup run.\n"), MONDO_LOGFILE);
603        mr_exit(retval, NULL);
604    }
605
606    return EXIT_SUCCESS;
607}
Note: See TracBrowser for help on using the repository browser.