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

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

Continue to remove floppy support

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