source: branches/stable/mondo/src/mondoarchive/mondoarchive.c @ 1592

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