source: branches/2.06/mondo/mondo/mondoarchive/main.c @ 296

Last change on this file since 296 was 296, checked in by andree, 15 years ago

Replaced partimagehack with ntfsclone from ntfsprogs package. Replaced
all occurrences of strings 'partimagehack' and 'partimage' with 'ntfsprog'.

  • Property svn:keywords set to Id
File size: 16.2 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: main.c 296 2006-01-11 09:10:31Z andree $
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/** change log ****** MONDO-DEVEL
20
21
2212/10
23- disable stopping/starting of autofs
24
2510/01
26- update g_erase_tmpdir_and_scratchdir to delete user-specified tmpdir, scratchdir
27
2806/19
29- added AUX_VER
30
3106/14/2004
32- use mondorescue.iso, not mindi.iso
33
3402/10/2004
35- tell users where BusyBox's sources are
36
3711/14/2003
38- cleaned up logging at end#
39
4010/23
41- don't try to test-read tape ... That's already
42  handled by post_param_configuration()
43 
4410/19
45- if your PATH var is too long, abort
46
4709/23
48- added some comments
49- malloc/free global strings in new subroutines - malloc_libmondo_global_strings()
50  and free_libmondo_global_strings() - which are in libmondo-tools.c
51- better magicdev support
52
53
5409/16
55- delete /var/log/partimagehack-debug.log at start of main()
56
5709/15
58- added askbootloader
59
6009/09
61- if your tape is weird, I'll pause between backup and verify
62- fixed silly bug in main() - re: say_at_end
63
6401/01 - 08/31
65- call 'dmesg -n1' at start, to shut the kernel logger up
66- moved g_erase_tmpdir_and_scratchdir to common/newt-specific.c
67- added 'don't panic' msg to start of logfile
68- added 'nice(20)' to main()
69- added lots of assert()'s and log_OS_error()'s
70- clean-up (Hugo)
71- make post_param_configuration() setup g_erase_tmpdir_and_scratchdir
72- if --version then print & exit quickly
73- re-run g_erase_tmpdir_and_scratchdir via system() at very end
74
75Year: 2002
76- if user goes root with 'su' instead of 'su -' then
77  workaround it by setting PATH correctly
78- wipe mondoarchive.log at very beginning
79- cleaned up code
80- if changed.files.N exists then copy to changes.files for display
81- run_program_and_log_output() now takes boolean operator to specify
82  whether it will log its activities in the event of _success_
83- added popup list of changed files
84- removed 'beta-quality' warnings
85- if kernel not found and mondo in graphics mode then popup and ask
86  for kernel path+filename
87- fixed tmp[] 'too small' bug
88- unmount and eject CD at end of verify cycle
89- moved interactively_obtain...() to libmondo-stream.c
90- wrote stuff to autodetect tape+cdrw+etc.
91- renamed from main.c to mondo-archive.c
92- fore+after warnings that this code is beta-quality
93- abort if running from ramdisk
94- remount floppy at end & unmount at start if Mandrake
95- took out #debug stuff
96- add 2> /dev/null to 'find' command
97- add support for bkpinfo->nonbootable_backup
98- add main function begin comment and debug conditional
99  compilation - Stan Benoit
100- add debug statements to build a run tree. Stan Benoit
101**** end change log **********/
102
103
104/**
105 * @file
106 * The main file for mondoarchive.
107 */
108
109/************************* #include statements *************************/
110#include <pthread.h>
111//#include <config.h>
112//#include "../../config.h"
113#include <stdio.h>
114#include <stdlib.h>
115#include "../common/my-stuff.h"
116#include "../common/mondostructures.h"
117#include "../common/libmondo.h"
118#include "mondo-cli-EXT.h"
119
120// for CVS
121//static char cvsid[] = "$Id: main.c 296 2006-01-11 09:10:31Z andree $";
122
123/************************* external variables *************************/
124extern void set_signals(int);
125extern int g_current_media_number;
126extern void register_pid(pid_t, char *);
127extern int g_currentY;
128extern bool g_text_mode;
129extern char *g_boot_mountpt;
130extern bool g_remount_cdrom_at_end, g_remount_floppy_at_end;
131extern char *g_mondo_home;
132extern char *g_tmpfs_mountpt;
133extern char *g_erase_tmpdir_and_scratchdir;
134extern char *g_cdrw_drive_is_here;
135static char *g_cdrom_drive_is_here = NULL;
136static char *g_dvd_drive_is_here = NULL;
137extern double g_kernel_version;
138
139/***************** global vars, used only by main.c ******************/
140bool g_skip_floppies;
141long diffs;
142
143extern t_bkptype g_backup_media_type;
144extern int g_loglevel;
145
146/****************** subroutines used only by main.c ******************/
147
148
149/**
150 * Print a "don't panic" message to the log and a message about the logfile to the screen.
151 */
152void welcome_to_mondoarchive()
153{
154    log_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org",
155            PACKAGE_VERSION);
156    log_msg(0, "running on %s architecture", get_architecture());
157    log_msg(0,
158            "-----------------------------------------------------------");
159    log_msg(0,
160            "NB: Mondo logs almost everything, so don't panic if you see");
161    log_msg(0,
162            "some error messages.  Please read them carefully before you");
163    log_msg(0,
164            "decide to break out in a cold sweat.    Despite (or perhaps");
165    log_msg(0,
166            "because of) the wealth of messages. some users are inclined");
167    log_msg(0,
168            "to stop reading this log. If Mondo stopped for some reason,");
169    log_msg(0,
170            "chances are it's detailed here.  More than likely there's a");
171    log_msg(0,
172            "message at the very end of this log that will tell you what");
173    log_msg(0,
174            "is wrong. Please read it!                          -Devteam");
175    log_msg(0,
176            "-----------------------------------------------------------");
177
178    log_msg(0, "Zero...");
179    log_msg(1, "One...");
180    log_msg(2, "Two...");
181    log_msg(3, "Three...");
182    log_msg(4, "Four...");
183    log_msg(5, "Five...");
184    log_msg(6, "Six...");
185    log_msg(7, "Seven...");
186    log_msg(8, "Eight...");
187    printf("See %s for details of backup run.\n", MONDO_LOGFILE);
188}
189
190
191extern char *g_magicdev_command;
192
193/**
194 * Do whatever is necessary to insure a successful backup on the Linux distribution
195 * of the day.
196 */
197void distro_specific_kludges_at_start_of_mondoarchive()
198{
199    log_msg(2, "Unmounting old ramdisks if necessary");
200    stop_magicdev_if_necessary();   // for RH+Gnome users
201    run_program_and_log_output
202        ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
203    unmount_supermounts_if_necessary(); // for Mandrake users whose CD-ROMs are supermounted
204    //  stop_autofs_if_necessary(); // for Xandros users
205    mount_boot_if_necessary();  // for Gentoo users with non-mounted /boot partitions
206    clean_up_KDE_desktop_if_necessary();    // delete various misc ~/.* files that get in the way
207}
208
209
210
211/**
212 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
213 */
214void distro_specific_kludges_at_end_of_mondoarchive()
215{
216//  char tmp[500];
217    log_msg(2, "Restarting magicdev if necessary");
218    sync();
219    restart_magicdev_if_necessary();    // for RH+Gnome users
220
221    log_msg(2, "Restarting autofs if necessary");
222    sync();
223    //  restart_autofs_if_necessary(); // for Xandros users
224
225    log_msg(2, "Restarting supermounts if necessary");
226    sync();
227    remount_supermounts_if_necessary(); // for Mandrake users
228
229    log_msg(2, "Unmounting /boot if necessary");
230    sync();
231    unmount_boot_if_necessary();    // for Gentoo users
232
233//  log_msg( 2, "Cleaning up KDE desktop");
234//  clean_up_KDE_desktop_if_necessary();
235}
236
237
238/*-----------------------------------------------------------*/
239
240
241
242/**
243 * Backup/verify the user's data.
244 * What did you think it did, anyway? :-)
245 */
246int main(int argc, char *argv[])
247{
248    struct s_bkpinfo *bkpinfo;
249    char *tmp;
250    int res, retval;
251    char *say_at_end;
252
253/* Make sure I'm root; abort if not */
254    if (getuid() != 0) {
255        fprintf(stderr, "Please run as root.\r\n");
256        exit(127);
257    }
258
259/* If -V, -v or --version then echo version no. and quit */
260    if (argc == 2
261        && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
262            || !strcmp(argv[argc - 1], "--version"))) {
263        printf("mondoarchive v%s\nSee man page for help\n", PACKAGE_VERSION);
264        exit(0);
265    }
266
267/* Initialize variables */
268
269    malloc_libmondo_global_strings();
270    malloc_string(tmp);
271    malloc_string(say_at_end);
272
273    res = 0;
274    retval = 0;
275    diffs = 0;
276    say_at_end[0] = '\0';
277    printf("Initializing...\n");
278    if (!(bkpinfo = malloc(sizeof(struct s_bkpinfo)))) {
279        fatal_error("Cannot malloc bkpinfo");
280    }
281
282
283/* make sure PATH environmental variable allows access to mkfs, fdisk, etc. */
284    strncpy(tmp, getenv("PATH"), MAX_STR_LEN - 1);
285    tmp[MAX_STR_LEN - 1] = '\0';
286    if (strlen(tmp) >= MAX_STR_LEN - 33) {
287        fatal_error
288            ("Your PATH environmental variable is too long. Please shorten it.");
289    }
290    strcat(tmp, ":/sbin:/usr/sbin:/usr/local/sbin");
291    setenv("PATH", tmp, 1);
292
293/* Add the ARCH environment variable for ia64 purposes */
294    strncpy(tmp, get_architecture(), MAX_STR_LEN - 1);
295    tmp[MAX_STR_LEN - 1] = '\0';
296    setenv("ARCH", tmp, 1);
297
298    /* Add MONDO_LIB environment variable for mindi */
299    setenv_mondo_lib();
300
301    unlink(MONDO_LOGFILE);
302
303/* Configure the bkpinfo structure, global file paths, etc. */
304    g_main_pid = getpid();
305    log_msg(9, "This");
306
307    register_pid(g_main_pid, "mondo");
308    set_signals(TRUE);          // catch SIGTERM, etc.
309    run_program_and_log_output("dmesg -n1", TRUE);
310
311    log_msg(9, "Next");
312    welcome_to_mondoarchive();
313    distro_specific_kludges_at_start_of_mondoarchive();
314    sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
315            bkpinfo->scratchdir);
316    g_kernel_version = get_kernel_version();
317
318    if (argc == 4 && !strcmp(argv[1], "getfattr")) {
319        g_loglevel = 10;
320        g_text_mode = TRUE;
321        setup_newt_stuff();
322        if (!strstr(argv[2], "filelist")) {
323            printf("Sorry - filelist goes first\n");
324            finish(1);
325        } else {
326            finish(get_fattr_list(argv[2], argv[3]));
327        }
328        finish(0);
329    }
330    if (argc == 4 && !strcmp(argv[1], "setfattr")) {
331        g_loglevel = 10;
332//      chdir("/tmp");
333        g_text_mode = TRUE;
334        setup_newt_stuff();
335        finish(set_fattr_list(argv[2], argv[3]));
336    }
337
338    if (argc == 3 && !strcmp(argv[1], "wildcards")) {
339        g_loglevel = 10;
340        g_text_mode = TRUE;
341        setup_newt_stuff();
342        turn_wildcard_chars_into_literal_chars(tmp, argv[2]);
343        printf("in=%s; out=%s\n", argv[2], tmp);
344        finish(1);
345    }
346
347    if (argc == 4 && !strcmp(argv[1], "getfacl")) {
348        g_loglevel = 10;
349        g_text_mode = TRUE;
350        setup_newt_stuff();
351        if (!strstr(argv[2], "filelist")) {
352            printf("Sorry - filelist goes first\n");
353            finish(1);
354        } else {
355            finish(get_acl_list(argv[2], argv[3]));
356        }
357        finish(0);
358    }
359    if (argc == 4 && !strcmp(argv[1], "setfacl")) {
360        g_loglevel = 10;
361//      chdir("/tmp");
362        g_text_mode = TRUE;
363        setup_newt_stuff();
364        finish(set_acl_list(argv[2], argv[3]));
365    }
366
367    if (argc > 2 && !strcmp(argv[1], "find-cd")) {
368        g_loglevel = 10;
369        g_text_mode = TRUE;
370        setup_newt_stuff();
371        if (find_cdrw_device(tmp)) {
372            printf("Failed to find CDR-RW drive\n");
373        } else {
374            printf("CD-RW is at %s\n", tmp);
375        }
376        tmp[0] = '\0';
377        if (find_cdrom_device(tmp, atoi(argv[2]))) {
378            printf("Failed to find CD-ROM drive\n");
379        } else {
380            printf("CD-ROM is at %s\n", tmp);
381        }
382        finish(0);
383    }
384
385    if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
386        g_loglevel = 10;
387        g_text_mode = TRUE;
388        setup_newt_stuff();
389        if (find_dvd_device(tmp, atoi(argv[2]))) {
390            printf("Failed to find DVD drive\n");
391        } else {
392            printf("DVD is at %s\n", tmp);
393        }
394        finish(0);
395    }
396
397    if (argc > 2 && !strcmp(argv[1], "disksize")) {
398        printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
399        finish(0);
400    }
401    if (argc > 2 && !strcmp(argv[1], "test-dev")) {
402        if (is_dev_an_NTFS_dev(argv[2])) {
403            printf("%s is indeed an NTFS dev\n", argv[2]);
404        } else {
405            printf("%s is _not_ an NTFS dev\n", argv[2]);
406        }
407        finish(0);
408    }
409
410    if (pre_param_configuration(bkpinfo)) {
411        fatal_error
412            ("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
413    }
414
415/* Process command line, if there is one. If not, ask user for info. */
416    if (argc == 1) {
417        g_text_mode = FALSE;
418        setup_newt_stuff();
419        res = interactively_obtain_media_parameters_from_user(bkpinfo, TRUE);   /* yes, archiving */
420        if (res) {
421            fatal_error
422                ("Syntax error. Please review the parameters you have supplied and try again.");
423        }
424    } else {
425        res = handle_incoming_parameters(argc, argv, bkpinfo);
426        if (res) {
427            printf
428                ("Errors were detected in the command line you supplied.\n");
429            printf("Please review the log file - " MONDO_LOGFILE "\n");
430            log_msg(1, "Mondoarchive will now exit.");
431            finish(1);
432        }
433        setup_newt_stuff();
434    }
435
436/* Finish configuring global structures */
437    if (post_param_configuration(bkpinfo)) {
438        fatal_error
439            ("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
440    }
441
442    log_to_screen
443        ("BusyBox's sources are available from http://www.busybox.net");
444    sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
445            bkpinfo->scratchdir);
446
447    /* If we're meant to backup then backup */
448    if (bkpinfo->backup_data) {
449/*
450      log_to_screen("INFERNAL PORPOISES");
451      res = archive_this_fileset_with_star(bkpinfo, "/tmp/filelist.0", "/tmp/0.star.bz2", 0);
452      log_to_screen("atfws returned %d", res);
453      finish(0);
454*/
455        res = backup_data(bkpinfo);
456        retval += res;
457        if (res) {
458            strcat(say_at_end,
459                   "Data archived. Please check the logs, just as a precaution. ");
460        } else {
461            strcat(say_at_end, "Data archived OK. ");
462        }
463    }
464
465/* If we're meant to verify then verify */
466    if (bkpinfo->verify_data) {
467        res = verify_data(bkpinfo);
468        if (res < 0) {
469            sprintf(tmp, "%d difference%c found.", -res,
470                    (-res != 1) ? 's' : ' ');
471            strcat(say_at_end, tmp);
472            log_to_screen(tmp);
473            res = 0;
474        }
475        retval += res;
476    }
477
478/* Offer to write floppy disk images to physical disks */
479    if (bkpinfo->backup_data && !g_skip_floppies) {
480        res = offer_to_write_boot_floppies_to_physical_disks(bkpinfo);
481        retval += res;
482//      res = offer_to_write_boot_ISO_to_physical_CD(bkpinfo);
483//      retval += res;
484    }
485
486/* Report result of entire operation (success? errors?) */
487    if (!retval) {
488        mvaddstr_and_log_it(g_currentY++, 0,
489                            "Backup and/or verify ran to completion. Everything appears to be fine.");
490    } else {
491        mvaddstr_and_log_it(g_currentY++, 0,
492                            "Backup and/or verify ran to completion. However, errors did occur.");
493    }
494
495    if (does_file_exist("/root/images/mindi/mondorescue.iso")) {
496        log_to_screen
497            ("/root/images/mindi/mondorescue.iso, a boot/utility CD, is available if you want it.");
498    }
499
500
501    if (length_of_file("/tmp/changed.files") > 2) {
502        if (g_text_mode) {
503            log_to_screen
504                ("Type 'less /tmp/changed.files' to see which files don't match the archives");
505        } else {
506            log_msg(1,
507                    "Type 'less /tmp/changed.files' to see which files don't match the archives");
508            log_msg(2, "Calling popup_changelist_from_file()");
509            popup_changelist_from_file("/tmp/changed.files");
510            log_msg(2, "Returned from popup_changelist_from_file()");
511        }
512    } else {
513        unlink("/tmp/changed.files");
514    }
515    log_to_screen(say_at_end);
516    sprintf(tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
517    run_program_and_log_output(tmp, TRUE);
518    run_program_and_log_output(g_erase_tmpdir_and_scratchdir, TRUE);
519
520    run_program_and_log_output("mount", 2);
521
522    system("rm -f /var/cache/mondo-archive/last-backup.aborted");
523    system("rm -Rf /tmp.mondo.* /mondo.scratch.*");
524    if (!retval) {
525        printf("Mondoarchive ran OK.\n");
526    } else {
527        printf("Errors occurred during backup. Please check logfile.\n");
528    }
529    distro_specific_kludges_at_end_of_mondoarchive();
530    register_pid(0, "mondo");
531    set_signals(FALSE);
532    chdir("/tmp");              // just in case there's something wrong with g_erase_tmpdir_and_scratchdir
533    system(g_erase_tmpdir_and_scratchdir);
534    free_libmondo_global_strings();
535    paranoid_free(say_at_end);
536    paranoid_free(tmp);
537    paranoid_free(bkpinfo);
538
539    unlink("/tmp/filelist.full");
540    unlink("/tmp/filelist.full.gz");
541
542    if (!g_cdrom_drive_is_here) {
543        log_msg(10, "FYI, g_cdrom_drive_is_here was never used");
544    }
545    if (!g_dvd_drive_is_here) {
546        log_msg(10, "FYI, g_dvd_drive_is_here was never used");
547    }
548
549    if (!g_text_mode) {
550        popup_and_OK
551            ("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt.");
552        log_to_screen("See %s for details of backup run.", MONDO_LOGFILE);
553        finish(retval);
554    } else {
555        printf("See %s for details of backup run.\n", MONDO_LOGFILE);
556        exit(retval);
557    }
558
559    return EXIT_SUCCESS;
560}
Note: See TracBrowser for help on using the repository browser.