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

Last change on this file since 2030 was 2030, checked in by Bruno Cornec, 16 years ago
  • Fix #252
  • Improve OCFS2 support (ocfs2 is a SANE_FORMAT)
  • Property svn:keywords set to Id
File size: 13.6 KB
RevLine 
[1]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
[128]7 cvsid : $Id: mondoarchive.c 2030 2008-10-02 23:50:44Z bruno $
[1]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>
[1930]28#include "my-stuff.h"
[1]29#include "../common/mondostructures.h"
30#include "../common/libmondo.h"
[1917]31#include "../common/libmondo-cli-EXT.h"
[1383]32#include "../common/libmondo-tools-EXT.h"
[1315]33#include "mondoarchive.h"
[1]34
35// for CVS
[128]36//static char cvsid[] = "$Id: mondoarchive.c 2030 2008-10-02 23:50:44Z bruno $";
[1]37
38/************************* external variables *************************/
39extern void set_signals(int);
40extern int g_current_media_number;
[128]41extern void register_pid(pid_t, char *);
[1]42extern int g_currentY;
43extern bool g_text_mode;
44extern char *g_boot_mountpt;
[1736]45extern bool g_remount_floppy_at_end;
[1]46extern char *g_cdrw_drive_is_here;
[128]47static char *g_cdrom_drive_is_here = NULL;
48static char *g_dvd_drive_is_here = NULL;
[1]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
[1917]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
[1645]71/* Reference to global bkpinfo */
72struct s_bkpinfo *bkpinfo;
73
[1967]74/* To be coded */
75void free_MR_global_filenames(void) {
76}
77
[1]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 */
[1236]84void welcome_to_mondoarchive(void)
[1]85{
[1375]86 char *tmp = NULL;
87
[128]88 log_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org",
[251]89 PACKAGE_VERSION);
[1375]90 log_msg(0, "running %s binaries", get_architecture());
91 tmp = get_uname_m();
92 log_msg(0, "running on %s architecture", tmp);
[1381]93 free(tmp);
[128]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 "-----------------------------------------------------------");
[1]114
[128]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);
[1]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 */
[1236]134void distro_specific_kludges_at_start_of_mondoarchive(void)
[1]135{
[128]136 log_msg(2, "Unmounting old ramdisks if necessary");
137 stop_magicdev_if_necessary(); // for RH+Gnome users
[2030]138 /*
[128]139 run_program_and_log_output
140 ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
[2030]141 */
[128]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
[1]146}
147
148
149
150/**
151 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
152 */
[1236]153void distro_specific_kludges_at_end_of_mondoarchive(void)
[1]154{
155// char tmp[500];
[128]156 log_msg(2, "Restarting magicdev if necessary");
157 sync();
158 restart_magicdev_if_necessary(); // for RH+Gnome users
[1]159
[128]160 log_msg(2, "Restarting autofs if necessary");
161 sync();
162 // restart_autofs_if_necessary(); // for Xandros users
[1]163
[128]164 log_msg(2, "Restarting supermounts if necessary");
165 sync();
166 remount_supermounts_if_necessary(); // for Mandrake users
[1]167
[128]168 log_msg(2, "Unmounting /boot if necessary");
169 sync();
170 unmount_boot_if_necessary(); // for Gentoo users
[1]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 */
[128]181int main(int argc, char *argv[])
[1]182{
[128]183 char *tmp;
184 int res, retval;
185 char *say_at_end;
[1]186
187/* Make sure I'm root; abort if not */
[128]188 if (getuid() != 0) {
[541]189 fprintf(stderr, "Please run as root.\r\n");
[128]190 exit(127);
191 }
[1]192
193/* If -V, -v or --version then echo version no. and quit */
[128]194 if (argc == 2
195 && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
196 || !strcmp(argv[argc - 1], "--version"))) {
[541]197 printf("mondoarchive v%s\nSee man page for help\n", PACKAGE_VERSION);
[128]198 exit(0);
199 }
[1]200
201/* Initialize variables */
202
[1652]203 printf("Initializing...\n");
204 if (!(bkpinfo = malloc(sizeof(struct s_bkpinfo)))) {
205 fatal_error("Cannot malloc bkpinfo");
206 }
207 reset_bkpinfo();
[1]208
[128]209 res = 0;
210 retval = 0;
211 diffs = 0;
[1652]212 malloc_string(tmp);
213 malloc_string(say_at_end);
[128]214 say_at_end[0] = '\0';
[1652]215 malloc_libmondo_global_strings();
[1]216
[810]217 /* initialize log file with time stamp */
218 unlink(MONDO_LOGFILE);
[813]219 log_msg(0, "Time started: %s", mr_date());
[1]220
[807]221 /* make sure PATH environmental variable allows access to mkfs, fdisk, etc. */
[128]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);
[1]230
[807]231 /* Add the ARCH environment variable for ia64 purposes */
[128]232 strncpy(tmp, get_architecture(), MAX_STR_LEN - 1);
233 tmp[MAX_STR_LEN - 1] = '\0';
234 setenv("ARCH", tmp, 1);
[1]235
[424]236 /* Add MONDO_SHARE environment variable for mindi */
237 setenv_mondo_share();
[258]238
[807]239 /* Configure the bkpinfo structure, global file paths, etc. */
[128]240 g_main_pid = getpid();
241 log_msg(9, "This");
[1]242
[128]243 register_pid(g_main_pid, "mondo");
244 set_signals(TRUE); // catch SIGTERM, etc.
245 run_program_and_log_output("dmesg -n1", TRUE);
[1]246
[128]247 log_msg(9, "Next");
[1747]248 make_hole_for_dir(MONDO_CACHE);
249
[128]250 welcome_to_mondoarchive();
251 distro_specific_kludges_at_start_of_mondoarchive();
252 g_kernel_version = get_kernel_version();
[1]253
[128]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")) {
[541]259 printf("Sorry - filelist goes first\n");
[128]260 finish(1);
261 } else {
262 finish(get_fattr_list(argv[2], argv[3]));
263 }
264 finish(0);
[1]265 }
[128]266 if (argc == 4 && !strcmp(argv[1], "setfattr")) {
267 g_loglevel = 10;
[1]268// chdir("/tmp");
[128]269 g_text_mode = TRUE;
270 setup_newt_stuff();
271 finish(set_fattr_list(argv[2], argv[3]));
[1]272 }
[128]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);
[1]281 }
[128]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")) {
[541]288 printf("Sorry - filelist goes first\n");
[128]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;
[1]297// chdir("/tmp");
[128]298 g_text_mode = TRUE;
299 setup_newt_stuff();
300 finish(set_acl_list(argv[2], argv[3]));
301 }
[1]302
[128]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)) {
[541]308 printf("Failed to find CDR-RW drive\n");
[128]309 } else {
[541]310 printf("CD-RW is at %s\n", tmp);
[128]311 }
312 tmp[0] = '\0';
313 if (find_cdrom_device(tmp, atoi(argv[2]))) {
[541]314 printf("Failed to find CD-ROM drive\n");
[128]315 } else {
[541]316 printf("CD-ROM is at %s\n", tmp);
[128]317 }
318 finish(0);
319 }
[1]320
[128]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]))) {
[541]326 printf("Failed to find DVD drive\n");
[128]327 } else {
[541]328 printf("DVD is at %s\n", tmp);
[128]329 }
330 finish(0);
331 }
[1]332
[128]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])) {
[541]339 printf("%s is indeed an NTFS dev\n", argv[2]);
[128]340 } else {
[541]341 printf("%s is _not_ an NTFS dev\n", argv[2]);
[128]342 }
343 finish(0);
344 }
345
[1645]346 if (pre_param_configuration()) {
[128]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
[1]351/* Process command line, if there is one. If not, ask user for info. */
[128]352 if (argc == 1) {
353 g_text_mode = FALSE;
354 setup_newt_stuff();
[1645]355 res = interactively_obtain_media_parameters_from_user(TRUE); /* yes, archiving */
[128]356 if (res) {
357 fatal_error
358 ("Syntax error. Please review the parameters you have supplied and try again.");
359 }
360 } else {
[1645]361 res = handle_incoming_parameters(argc, argv);
[128]362 if (res) {
363 printf
[541]364 ("Errors were detected in the command line you supplied.\n");
[1319]365 printf("Please review the log file - %s\n", MONDO_LOGFILE );
[128]366 log_msg(1, "Mondoarchive will now exit.");
367 finish(1);
368 }
369 setup_newt_stuff();
[1]370 }
371
372/* Finish configuring global structures */
[1645]373 if (post_param_configuration()) {
[128]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 }
[1]377
[128]378 log_to_screen
[541]379 ("BusyBox's sources are available from http://www.busybox.net");
[1]380
[128]381 /* If we're meant to backup then backup */
382 if (bkpinfo->backup_data) {
[1645]383 res = backup_data();
[128]384 retval += res;
385 if (res) {
386 strcat(say_at_end,
[541]387 "Data archived. Please check the logs, just as a precaution. ");
[128]388 } else {
[541]389 strcat(say_at_end, "Data archived OK. ");
[128]390 }
391 }
[1]392
393/* If we're meant to verify then verify */
[128]394 if (bkpinfo->verify_data) {
[1645]395 res = verify_data();
[128]396 if (res < 0) {
[541]397 sprintf(tmp, "%d difference%c found.", -res,
[128]398 (-res != 1) ? 's' : ' ');
399 strcat(say_at_end, tmp);
400 log_to_screen(tmp);
401 res = 0;
402 }
403 retval += res;
404 }
[1]405
406/* Report result of entire operation (success? errors?) */
[128]407 if (!retval) {
408 mvaddstr_and_log_it(g_currentY++, 0,
[541]409 "Backup and/or verify ran to completion. Everything appears to be fine.");
[128]410 } else {
411 mvaddstr_and_log_it(g_currentY++, 0,
[541]412 "Backup and/or verify ran to completion. However, errors did occur.");
[128]413 }
[1]414
[1437]415 if (does_file_exist(MINDI_CACHE"/mondorescue.iso")) {
[128]416 log_to_screen
[1437]417 (MINDI_CACHE"/mondorescue.iso, a boot/utility CD, is available if you want it.");
[128]418 }
[1]419
[1747]420 if (length_of_file(MONDO_CACHE"/changed.files") > 2) {
[128]421 if (g_text_mode) {
[1747]422 log_to_screen("Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
[128]423 } else {
[1747]424 log_msg(1, "Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
[128]425 log_msg(2, "Calling popup_changelist_from_file()");
[1747]426 popup_changelist_from_file(MONDO_CACHE"/changed.files");
[128]427 log_msg(2, "Returned from popup_changelist_from_file()");
428 }
429 } else {
[1747]430 unlink(MONDO_CACHE"/changed.files");
[128]431 }
432 log_to_screen(say_at_end);
433 sprintf(tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
434 run_program_and_log_output(tmp, TRUE);
[1697]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 }
[1]440
[128]441 run_program_and_log_output("mount", 2);
[1]442
[1747]443 system("rm -f "MONDO_CACHE"/last-backup.aborted");
[128]444 if (!retval) {
[541]445 printf("Mondoarchive ran OK.\n");
[128]446 } else {
[541]447 printf("Errors occurred during backup. Please check logfile.\n");
[128]448 }
449 distro_specific_kludges_at_end_of_mondoarchive();
450 register_pid(0, "mondo");
451 set_signals(FALSE);
[1644]452
[128]453 free_libmondo_global_strings();
454 paranoid_free(say_at_end);
[1703]455
[1]456
[128]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 }
[1]463
[810]464 /* finalize log file with time stamp */
[813]465 log_msg(0, "Time finished: %s", mr_date());
[810]466
[1708]467 chdir("/tmp");
[1644]468
[128]469 if (!g_text_mode) {
470 popup_and_OK
[541]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);
[128]473 } else {
[541]474 printf("See %s for details of backup run.\n", MONDO_LOGFILE);
[128]475 }
[1703]476 finish(retval);
[128]477
478 return EXIT_SUCCESS;
[1]479}
Note: See TracBrowser for help on using the repository browser.