source: branches/2.2.7/mondo/src/mondoarchive/mondoarchive.c @ 2030

Last change on this file since 2030 was 2030, checked in by Bruno Cornec, 12 years ago
  • Fix #252
  • Improve OCFS2 support (ocfs2 is a SANE_FORMAT)
  • Property svn:keywords set to Id
File size: 13.6 KB
Line 
1/***************************************************************************
2                          main.c  -  description
3                             -------------------
4    begin                : Fri Apr 19 16:40:35 EDT 2002
5    copyright            : (C) 2002 by Stan Benoit
6    email                : troff@nakedsoul.org
7    cvsid                : $Id: mondoarchive.c 2030 2008-10-02 23:50:44Z bruno $
8 ***************************************************************************/
9
10/***************************************************************************
11 *                                                                         *
12 *   This program is free software; you can redistribute it and/or modify  *
13 *   it under the terms of the GNU General Public License as published by  *
14 *   the Free Software Foundation; either version 2 of the License, or     *
15 *   (at your option) any later version.                                   *
16 *                                                                         *
17 ***************************************************************************/
18
19/**
20 * @file
21 * The main file for mondoarchive.
22 */
23
24/************************* #include statements *************************/
25#include <pthread.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include "my-stuff.h"
29#include "../common/mondostructures.h"
30#include "../common/libmondo.h"
31#include "../common/libmondo-cli-EXT.h"
32#include "../common/libmondo-tools-EXT.h"
33#include "mondoarchive.h"
34
35// for CVS
36//static char cvsid[] = "$Id: mondoarchive.c 2030 2008-10-02 23:50:44Z bruno $";
37
38/************************* external variables *************************/
39extern void set_signals(int);
40extern int g_current_media_number;
41extern void register_pid(pid_t, char *);
42extern int g_currentY;
43extern bool g_text_mode;
44extern char *g_boot_mountpt;
45extern bool g_remount_floppy_at_end;
46extern char *g_cdrw_drive_is_here;
47static char *g_cdrom_drive_is_here = NULL;
48static char *g_dvd_drive_is_here = NULL;
49extern double g_kernel_version;
50
51/***************** global vars, used only by main.c ******************/
52long diffs;
53
54extern t_bkptype g_backup_media_type;
55extern int g_loglevel;
56
57/**
58 * Whether we're restoring from ISOs. Obviously not, since this is the
59 * backup program.
60 * @note You @b MUST declare this variable somewhere in your program if
61 * you use libmondo. Otherwise the link will fail.
62 * @ingroup globalGroup
63 */
64bool g_ISO_restore_mode = FALSE;
65
66/* Do we use extended attributes and acl ?
67 *  * By default no, use --acl & --attr options to force their usage */
68char *g_getfacl = NULL;
69char *g_getfattr = NULL;
70
71/* Reference to global bkpinfo */
72struct s_bkpinfo *bkpinfo;
73
74/* To be coded */
75void free_MR_global_filenames(void) {
76}
77
78/****************** subroutines used only by main.c ******************/
79
80
81/**
82 * Print a "don't panic" message to the log and a message about the logfile to the screen.
83 */
84void welcome_to_mondoarchive(void)
85{
86    char *tmp = NULL;
87
88    log_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org",
89            PACKAGE_VERSION);
90    log_msg(0, "running %s binaries", get_architecture());
91    tmp = get_uname_m();
92    log_msg(0, "running on %s architecture", tmp);
93    free(tmp);
94    log_msg(0,
95            "-----------------------------------------------------------");
96    log_msg(0,
97            "NB: Mondo logs almost everything, so don't panic if you see");
98    log_msg(0,
99            "some error messages.  Please read them carefully before you");
100    log_msg(0,
101            "decide to break out in a cold sweat.    Despite (or perhaps");
102    log_msg(0,
103            "because of) the wealth of messages. some users are inclined");
104    log_msg(0,
105            "to stop reading this log. If Mondo stopped for some reason,");
106    log_msg(0,
107            "chances are it's detailed here.  More than likely there's a");
108    log_msg(0,
109            "message at the very end of this log that will tell you what");
110    log_msg(0,
111            "is wrong. Please read it!                          -Devteam");
112    log_msg(0,
113            "-----------------------------------------------------------");
114
115    log_msg(0, "Zero...");
116    log_msg(1, "One...");
117    log_msg(2, "Two...");
118    log_msg(3, "Three...");
119    log_msg(4, "Four...");
120    log_msg(5, "Five...");
121    log_msg(6, "Six...");
122    log_msg(7, "Seven...");
123    log_msg(8, "Eight...");
124    printf("See %s for details of backup run.\n", MONDO_LOGFILE);
125}
126
127
128extern char *g_magicdev_command;
129
130/**
131 * Do whatever is necessary to insure a successful backup on the Linux distribution
132 * of the day.
133 */
134void distro_specific_kludges_at_start_of_mondoarchive(void)
135{
136    log_msg(2, "Unmounting old ramdisks if necessary");
137    stop_magicdev_if_necessary();   // for RH+Gnome users
138    /*
139    run_program_and_log_output
140        ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
141        */
142    unmount_supermounts_if_necessary(); // for Mandrake users whose CD-ROMs are supermounted
143    //  stop_autofs_if_necessary(); // for Xandros users
144    mount_boot_if_necessary();  // for Gentoo users with non-mounted /boot partitions
145    clean_up_KDE_desktop_if_necessary();    // delete various misc ~/.* files that get in the way
146}
147
148
149
150/**
151 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
152 */
153void distro_specific_kludges_at_end_of_mondoarchive(void)
154{
155//  char tmp[500];
156    log_msg(2, "Restarting magicdev if necessary");
157    sync();
158    restart_magicdev_if_necessary();    // for RH+Gnome users
159
160    log_msg(2, "Restarting autofs if necessary");
161    sync();
162    //  restart_autofs_if_necessary(); // for Xandros users
163
164    log_msg(2, "Restarting supermounts if necessary");
165    sync();
166    remount_supermounts_if_necessary(); // for Mandrake users
167
168    log_msg(2, "Unmounting /boot if necessary");
169    sync();
170    unmount_boot_if_necessary();    // for Gentoo users
171
172//  log_msg( 2, "Cleaning up KDE desktop");
173//  clean_up_KDE_desktop_if_necessary();
174}
175
176
177/**
178 * Backup/verify the user's data.
179 * What did you think it did, anyway? :-)
180 */
181int main(int argc, char *argv[])
182{
183    char *tmp;
184    int res, retval;
185    char *say_at_end;
186
187/* Make sure I'm root; abort if not */
188    if (getuid() != 0) {
189        fprintf(stderr, "Please run as root.\r\n");
190        exit(127);
191    }
192
193/* If -V, -v or --version then echo version no. and quit */
194    if (argc == 2
195        && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
196            || !strcmp(argv[argc - 1], "--version"))) {
197        printf("mondoarchive v%s\nSee man page for help\n", PACKAGE_VERSION);
198        exit(0);
199    }
200
201/* Initialize variables */
202
203    printf("Initializing...\n");
204    if (!(bkpinfo = malloc(sizeof(struct s_bkpinfo)))) {
205        fatal_error("Cannot malloc bkpinfo");
206    }
207    reset_bkpinfo();
208
209    res = 0;
210    retval = 0;
211    diffs = 0;
212    malloc_string(tmp);
213    malloc_string(say_at_end);
214    say_at_end[0] = '\0';
215    malloc_libmondo_global_strings();
216
217    /* initialize log file with time stamp */
218    unlink(MONDO_LOGFILE);
219    log_msg(0, "Time started: %s", mr_date());
220
221    /* make sure PATH environmental variable allows access to mkfs, fdisk, etc. */
222    strncpy(tmp, getenv("PATH"), MAX_STR_LEN - 1);
223    tmp[MAX_STR_LEN - 1] = '\0';
224    if (strlen(tmp) >= MAX_STR_LEN - 33) {
225        fatal_error
226            ("Your PATH environmental variable is too long. Please shorten it.");
227    }
228    strcat(tmp, ":/sbin:/usr/sbin:/usr/local/sbin");
229    setenv("PATH", tmp, 1);
230
231    /* Add the ARCH environment variable for ia64 purposes */
232    strncpy(tmp, get_architecture(), MAX_STR_LEN - 1);
233    tmp[MAX_STR_LEN - 1] = '\0';
234    setenv("ARCH", tmp, 1);
235
236    /* Add MONDO_SHARE environment variable for mindi */
237    setenv_mondo_share();
238
239    /* Configure the bkpinfo structure, global file paths, etc. */
240    g_main_pid = getpid();
241    log_msg(9, "This");
242
243    register_pid(g_main_pid, "mondo");
244    set_signals(TRUE);          // catch SIGTERM, etc.
245    run_program_and_log_output("dmesg -n1", TRUE);
246
247    log_msg(9, "Next");
248    make_hole_for_dir(MONDO_CACHE);
249
250    welcome_to_mondoarchive();
251    distro_specific_kludges_at_start_of_mondoarchive();
252    g_kernel_version = get_kernel_version();
253
254    if (argc == 4 && !strcmp(argv[1], "getfattr")) {
255        g_loglevel = 10;
256        g_text_mode = TRUE;
257        setup_newt_stuff();
258        if (!strstr(argv[2], "filelist")) {
259            printf("Sorry - filelist goes first\n");
260            finish(1);
261        } else {
262            finish(get_fattr_list(argv[2], argv[3]));
263        }
264        finish(0);
265    }
266    if (argc == 4 && !strcmp(argv[1], "setfattr")) {
267        g_loglevel = 10;
268//      chdir("/tmp");
269        g_text_mode = TRUE;
270        setup_newt_stuff();
271        finish(set_fattr_list(argv[2], argv[3]));
272    }
273
274    if (argc == 3 && !strcmp(argv[1], "wildcards")) {
275        g_loglevel = 10;
276        g_text_mode = TRUE;
277        setup_newt_stuff();
278        turn_wildcard_chars_into_literal_chars(tmp, argv[2]);
279        printf("in=%s; out=%s\n", argv[2], tmp);
280        finish(1);
281    }
282
283    if (argc == 4 && !strcmp(argv[1], "getfacl")) {
284        g_loglevel = 10;
285        g_text_mode = TRUE;
286        setup_newt_stuff();
287        if (!strstr(argv[2], "filelist")) {
288            printf("Sorry - filelist goes first\n");
289            finish(1);
290        } else {
291            finish(get_acl_list(argv[2], argv[3]));
292        }
293        finish(0);
294    }
295    if (argc == 4 && !strcmp(argv[1], "setfacl")) {
296        g_loglevel = 10;
297//      chdir("/tmp");
298        g_text_mode = TRUE;
299        setup_newt_stuff();
300        finish(set_acl_list(argv[2], argv[3]));
301    }
302
303    if (argc > 2 && !strcmp(argv[1], "find-cd")) {
304        g_loglevel = 10;
305        g_text_mode = TRUE;
306        setup_newt_stuff();
307        if (find_cdrw_device(tmp)) {
308            printf("Failed to find CDR-RW drive\n");
309        } else {
310            printf("CD-RW is at %s\n", tmp);
311        }
312        tmp[0] = '\0';
313        if (find_cdrom_device(tmp, atoi(argv[2]))) {
314            printf("Failed to find CD-ROM drive\n");
315        } else {
316            printf("CD-ROM is at %s\n", tmp);
317        }
318        finish(0);
319    }
320
321    if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
322        g_loglevel = 10;
323        g_text_mode = TRUE;
324        setup_newt_stuff();
325        if (find_dvd_device(tmp, atoi(argv[2]))) {
326            printf("Failed to find DVD drive\n");
327        } else {
328            printf("DVD is at %s\n", tmp);
329        }
330        finish(0);
331    }
332
333    if (argc > 2 && !strcmp(argv[1], "disksize")) {
334        printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
335        finish(0);
336    }
337    if (argc > 2 && !strcmp(argv[1], "test-dev")) {
338        if (is_dev_an_NTFS_dev(argv[2])) {
339            printf("%s is indeed an NTFS dev\n", argv[2]);
340        } else {
341            printf("%s is _not_ an NTFS dev\n", argv[2]);
342        }
343        finish(0);
344    }
345
346    if (pre_param_configuration()) {
347        fatal_error
348            ("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
349    }
350
351/* Process command line, if there is one. If not, ask user for info. */
352    if (argc == 1) {
353        g_text_mode = FALSE;
354        setup_newt_stuff();
355        res = interactively_obtain_media_parameters_from_user(TRUE);    /* yes, archiving */
356        if (res) {
357            fatal_error
358                ("Syntax error. Please review the parameters you have supplied and try again.");
359        }
360    } else {
361        res = handle_incoming_parameters(argc, argv);
362        if (res) {
363            printf
364                ("Errors were detected in the command line you supplied.\n");
365            printf("Please review the log file - %s\n", MONDO_LOGFILE );
366            log_msg(1, "Mondoarchive will now exit.");
367            finish(1);
368        }
369        setup_newt_stuff();
370    }
371
372/* Finish configuring global structures */
373    if (post_param_configuration()) {
374        fatal_error
375            ("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
376    }
377
378    log_to_screen
379        ("BusyBox's sources are available from http://www.busybox.net");
380
381    /* If we're meant to backup then backup */
382    if (bkpinfo->backup_data) {
383        res = backup_data();
384        retval += res;
385        if (res) {
386            strcat(say_at_end,
387                   "Data archived. Please check the logs, just as a precaution. ");
388        } else {
389            strcat(say_at_end, "Data archived OK. ");
390        }
391    }
392
393/* If we're meant to verify then verify */
394    if (bkpinfo->verify_data) {
395        res = verify_data();
396        if (res < 0) {
397            sprintf(tmp, "%d difference%c found.", -res,
398                    (-res != 1) ? 's' : ' ');
399            strcat(say_at_end, tmp);
400            log_to_screen(tmp);
401            res = 0;
402        }
403        retval += res;
404    }
405
406/* Report result of entire operation (success? errors?) */
407    if (!retval) {
408        mvaddstr_and_log_it(g_currentY++, 0,
409                            "Backup and/or verify ran to completion. Everything appears to be fine.");
410    } else {
411        mvaddstr_and_log_it(g_currentY++, 0,
412                            "Backup and/or verify ran to completion. However, errors did occur.");
413    }
414
415    if (does_file_exist(MINDI_CACHE"/mondorescue.iso")) {
416        log_to_screen
417            (MINDI_CACHE"/mondorescue.iso, a boot/utility CD, is available if you want it.");
418    }
419
420    if (length_of_file(MONDO_CACHE"/changed.files") > 2) {
421        if (g_text_mode) {
422            log_to_screen("Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
423        } else {
424            log_msg(1, "Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
425            log_msg(2, "Calling popup_changelist_from_file()");
426            popup_changelist_from_file(MONDO_CACHE"/changed.files");
427            log_msg(2, "Returned from popup_changelist_from_file()");
428        }
429    } else {
430        unlink(MONDO_CACHE"/changed.files");
431    }
432    log_to_screen(say_at_end);
433    sprintf(tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
434    run_program_and_log_output(tmp, TRUE);
435    if (bkpinfo->backup_media_type == usb) {
436        log_msg(1, "Unmounting USB device.");
437        sprintf(tmp, "umount %s1", bkpinfo->media_device);
438        run_program_and_log_output(tmp, TRUE);
439    }
440
441    run_program_and_log_output("mount", 2);
442
443    system("rm -f "MONDO_CACHE"/last-backup.aborted");
444    if (!retval) {
445        printf("Mondoarchive ran OK.\n");
446    } else {
447        printf("Errors occurred during backup. Please check logfile.\n");
448    }
449    distro_specific_kludges_at_end_of_mondoarchive();
450    register_pid(0, "mondo");
451    set_signals(FALSE);
452
453    free_libmondo_global_strings();
454    paranoid_free(say_at_end);
455   
456
457    if (!g_cdrom_drive_is_here) {
458        log_msg(10, "FYI, g_cdrom_drive_is_here was never used");
459    }
460    if (!g_dvd_drive_is_here) {
461        log_msg(10, "FYI, g_dvd_drive_is_here was never used");
462    }
463
464    /* finalize log file with time stamp */
465    log_msg(0, "Time finished: %s", mr_date());
466
467    chdir("/tmp");
468
469    if (!g_text_mode) {
470        popup_and_OK
471            ("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt.");
472        log_to_screen("See %s for details of backup run.", MONDO_LOGFILE);
473    } else {
474        printf("See %s for details of backup run.\n", MONDO_LOGFILE);
475    }
476    finish(retval);
477
478    return EXIT_SUCCESS;
479}
Note: See TracBrowser for help on using the repository browser.