source: MondoRescue/branches/stable/mondo/src/mondoarchive/mondoarchive.c@ 1378

Last change on this file since 1378 was 1378, checked in by Bruno Cornec, 17 years ago

Fixes for merges

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