source: MondoRescue/branches/3.2/mondo/src/mondoarchive/mondoarchive.c@ 3527

Last change on this file since 3527 was 3527, checked in by Bruno Cornec, 8 years ago

Improve mondoarchive/mindi interface

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