source: MondoRescue/branches/3.0/mondo/src/mondorestore/mondo-rstr-tools.c@ 3076

Last change on this file since 3076 was 3076, checked in by Bruno Cornec, 11 years ago
  • Avoids again some additional warnings at compilation level
  • Property svn:keywords set to Id
File size: 73.5 KB
Line 
1/***************************************************************************
2$Id: mondo-rstr-tools.c 3076 2012-11-24 19:23:58Z bruno $
3***************************************************************************/
4
5#include <pthread.h>
6#include <linux/fd.h>
7#include "my-stuff.h"
8#include "mr_mem.h"
9#include "../common/mondostructures.h"
10#include "../common/libmondo.h"
11#include "mr-externs.h"
12#include "mondo-rstr-tools.h"
13
14/**
15 * The biggielist stub (appended to the directory where all.tar.gz was unpacked).
16 */
17#define BIGGIELIST_TXT_STUB "tmp/biggielist.txt"
18
19/**
20 * The filelist stub (appended to the directory where all.tar.gz was unpacked).
21 */
22#define FILELIST_FULL_STUB "tmp/filelist.full.gz"
23
24/**
25 * The mountlist stub (appended to the directory where all.tar.gz was unpacked).
26 */
27#define MOUNTLIST_FNAME_STUB "tmp/mountlist.txt"
28
29/**
30 * The mondo-restore.cfg stub (appended to the directory where all.tar.gz was unpacked).
31 */
32#define MONDO_CFG_FILE_STUB "tmp/mondo-restore.cfg"
33/**
34 * The i-want-my-lvm stub
35 */
36#define IWANTMYLVM_STUB "tmp/i-want-my-lvm"
37
38extern bool g_ISO_restore_mode; /* are we in Iso Mode? */
39extern bool g_I_have_just_nuked;
40/*
41extern char *g_tmpfs_mountpt;
42*/
43extern char *g_isodir_device;
44extern char *g_isodir_format;
45extern long g_current_progress, g_maximum_progress;
46extern char *g_biggielist_txt; // where 'biggielist.txt' is stored, on ramdisk / tempdir;
47 // biggielist.txt is the list of big files stored on the
48 // backup media set in question
49extern char *g_filelist_full; // filelist.full.gz is the list of all regular files
50 // (excluding big files) stored on the backup media set
51extern char *g_biggielist_pot; // list of big files which _could_ be restored, if the
52 // user chooses them
53extern char *g_filelist_imagedevs; // list of devices (e.g. /dev/hda1, /dev/sda5) which
54 // were archived as images, not just /dev entries
55 // ... e.g. NTFS, BeOS partitions
56extern char *g_imagedevs_restthese; // of the imagedevs listed in FILELIST_IMAGEDEVS,
57 // restore only these
58extern char *g_mondo_cfg_file; // where m*ndo-restore.cfg (the config file) is stored
59extern char *g_mountlist_fname; // where mountlist.txt (the mountlist file) is stored
60extern char *g_mondo_home; // homedir of Mondo; usually /usr/local/share/mondo
61
62extern t_bkptype g_backup_media_type;
63
64extern int g_partition_table_locked_up;
65extern char *MONDO_LOGFILE;
66
67/* Reference to global bkpinfo */
68extern struct s_bkpinfo *bkpinfo;
69
70/* Should we use or not extended attributes and acl when restoring */
71char *g_getfattr = NULL;
72char *g_getfacl = NULL;
73
74extern void kill_anything_like_this(char *str);
75extern int skip_obdr(void);
76extern int mount_media();
77extern int set_tape_block_size_with_mt(long internal_tape_block_size);
78
79/**
80* @addtogroup restoreUtilityGroup
81* @{
82*/
83/**
84* Free the malloc()s for the filename variables.
85*/
86void free_MR_global_filenames(void)
87{
88paranoid_free(g_biggielist_txt);
89paranoid_free(g_filelist_full);
90paranoid_free(g_filelist_imagedevs);
91paranoid_free(g_imagedevs_restthese);
92paranoid_free(g_mondo_cfg_file);
93paranoid_free(g_mountlist_fname);
94paranoid_free(g_mondo_home);
95/*
96paranoid_free(g_tmpfs_mountpt);
97*/
98paranoid_free(g_isodir_device);
99paranoid_free(g_isodir_format);
100
101}
102
103
104
105/**
106* Ask the user which imagedevs from the list contained in @p infname should
107* actually be restored.
108* @param infname The file containing a list of all imagedevs.
109* @param outfname The location of the output file containing the imagedevs the user wanted to restore.
110* @ingroup restoreUtilityGroup
111*/
112void ask_about_these_imagedevs(char *infname, char *outfname)
113{
114FILE *fin;
115FILE *fout;
116/************************************************************************
117* allocate memory regions. test and set -sab 16 feb 2003 *
118************************************************************************/
119char *incoming_ptr;
120char *question_ptr;
121char *q;
122
123char incoming[MAX_STR_LEN] = "\0";
124char question[MAX_STR_LEN];
125
126assert_string_is_neither_NULL_nor_zerolength(infname);
127assert_string_is_neither_NULL_nor_zerolength(outfname);
128
129incoming_ptr = malloc(sizeof(incoming));
130if (incoming_ptr == NULL) {
131 fprintf(stderr, "Out of Memory\n");
132 exit(EXIT_FAILURE);
133}
134
135question_ptr = malloc(sizeof(question));
136if (question_ptr == NULL) {
137 fprintf(stderr, "Out of Memory\n");
138 exit(EXIT_FAILURE);
139}
140
141memset(incoming_ptr, '\0', sizeof(incoming));
142memset(question_ptr, '\0', sizeof(question));
143
144
145
146if (!(fin = fopen(infname, "r"))) {
147 fatal_error("Cannot openin infname");
148}
149if (!(fout = fopen(outfname, "w"))) {
150 fatal_error("Cannot openin outfname");
151}
152for (q = fgets(incoming_ptr, MAX_STR_LEN, fin); !feof(fin) && (q != NULL); q = fgets(incoming_ptr, MAX_STR_LEN, fin)) {
153 strip_spaces(incoming_ptr);
154
155 if (incoming[0] == '\0') {
156 continue;
157 }
158
159 sprintf(question_ptr, "Should I restore the image of %s ?", incoming_ptr);
160
161 if (ask_me_yes_or_no(question_ptr)) {
162 fprintf(fout, "%s\n", incoming_ptr);
163 }
164}
165
166/*** free memory ***********/
167paranoid_free(incoming_ptr);
168incoming_ptr = NULL;
169paranoid_free(question_ptr);
170question_ptr = NULL;
171
172
173paranoid_fclose(fout);
174paranoid_fclose(fin);
175}
176
177/**************************************************************************
178*ASK_ABOUT_THESE_IMAGEDEVS *
179**************************************************************************/
180
181
182
183
184
185
186
187
188/**
189* Extract @c mondo-restore.cfg and @c mountlist.txt from @p ramdisk_fname.
190* @param bkpinfo The backup information structure. @c tmpdir is the only field used.
191* @param ramdisk_fname The filename of the @b compressed ramdisk to look in.
192* @param output_cfg_file Where to put the configuration file extracted.
193* @param output_mountlist_file Where to put the mountlist file extracted.
194* @return 0 for success, nonzero for failure.
195* @ingroup restoreUtilityGroup
196*/
197
198/**
199* Keep trying to get mondo-restore.cfg from the archive, until the user gives up.
200*/
201void get_cfg_file_from_archive_or_bust()
202{
203while (get_cfg_file_from_archive()) {
204if (!ask_me_yes_or_no
205 ("Failed to find config file/archives. Choose another source?"))
206{
207 fatal_error("Could not find config file/archives. Aborting.");
208}
209interactively_obtain_media_parameters_from_user(FALSE);
210}
211}
212
213
214/**
215* Determine whether @p list_fname contains a line containing @p f.
216* @param f The line to search for.
217* @param list_fname The file to search in.
218* @param preamble Ignore this beginning part of @p f ("" to disable).
219* @return TRUE if it's in the list, FALSE if it's not.
220*/
221bool is_file_in_list(char *f, char *list_fname, char *preamble)
222{
223
224/** needs malloc **/
225char *command;
226char *file;
227char *tmp;
228int res;
229
230malloc_string(command);
231malloc_string(file);
232malloc_string(tmp);
233assert_string_is_neither_NULL_nor_zerolength(f);
234assert_string_is_neither_NULL_nor_zerolength(list_fname);
235assert(preamble != NULL);
236
237if (strncmp(preamble, f, strlen(preamble)) == 0) {
238strcpy(file, f + strlen(preamble));
239} else {
240strcpy(file, f);
241}
242if (file[0] == '/' && file[1] == '/') {
243strcpy(tmp, file);
244strcpy(file, tmp + 1);
245}
246sprintf(tmp,
247 "Checking to see if f=%s, file=%s, is in the list of biggiefiles",
248 f, file);
249log_msg(2, tmp);
250sprintf(command, "grep -E '^%s$' %s", file, list_fname);
251res = run_program_and_log_output(command, FALSE);
252paranoid_free(command);
253paranoid_free(file);
254paranoid_free(tmp);
255if (res) {
256return (FALSE);
257} else {
258return (TRUE);
259}
260}
261
262/**************************************************************************
263*END_IS_FILE_IN_LIST *
264**************************************************************************/
265
266
267
268/**
269* Set up an ISO backup.
270* @param bkpinfo The backup information structure. Fields used:
271* - @c bkpinfo->backup_media_type
272* - @c bkpinfo->disaster_recovery
273* - @c bkpinfo->isodir
274* @param nuke_me_please If TRUE, we're in nuke mode; if FALSE we're in interactive mode.
275* @return 0 for success, nonzero for failure.
276*/
277int iso_fiddly_bits(bool nuke_me_please)
278{
279char *mount_isodir_command = NULL;
280char *tmp, *command;
281char *mds = NULL;
282int retval = 0, i;
283bool already_mounted = FALSE;
284
285assert(bkpinfo != NULL);
286malloc_string(tmp);
287malloc_string(command);
288g_ISO_restore_mode = TRUE;
289read_cfg_var(g_mondo_cfg_file, "iso-dev", g_isodir_device);
290if (bkpinfo->disaster_recovery) {
291 /* Patch Conor Daly 26-june-2004
292 * Don't let this clobber an existing bkpinfo->isodir */
293 if (!bkpinfo->isodir[0]) {
294 strcpy(bkpinfo->isodir, "/tmp/isodir");
295 }
296 /* End patch */
297 sprintf(command, "mkdir -p %s", bkpinfo->isodir);
298 run_program_and_log_output(command, 5);
299 log_msg(2, "Setting isodir to %s", bkpinfo->isodir);
300}
301
302if (!get_isodir_info(g_isodir_device, g_isodir_format, bkpinfo->isodir, bkpinfo->subdir, nuke_me_please)) {
303 return (1);
304}
305paranoid_system("umount -d " MNT_CDROM " 2> /dev/null"); /* just in case */
306
307if (is_this_device_mounted(g_isodir_device)) {
308 log_to_screen("WARNING - isodir is already mounted");
309 already_mounted = TRUE;
310} else {
311 mr_asprintf(&mount_isodir_command, "mount %s", g_isodir_device);
312 if (strlen(g_isodir_format) > 1) {
313 mr_strcat(mount_isodir_command, " -t %s", g_isodir_format);
314 }
315 mr_strcat(mount_isodir_command, " -o ro %s", bkpinfo->isodir);
316 run_program_and_log_output("df -m", FALSE);
317 sprintf(tmp,
318 "The 'mount' command is '%s'. PLEASE report this command to be if you have problems, ok?",
319 mount_isodir_command);
320 log_msg(1, tmp);
321 if (run_program_and_log_output(mount_isodir_command, FALSE)) {
322 popup_and_OK
323 ("Cannot mount the device where the ISO files are stored.");
324 return (1);
325 }
326 paranoid_free(mount_isodir_command);
327 log_to_screen
328 ("I have mounted the device where the ISO files are stored.");
329}
330if (!IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
331 mount_media();
332}
333i = what_number_cd_is_this(); /* has the side-effect of calling mount_media() */
334mds = media_descriptor_string(bkpinfo->backup_media_type);
335sprintf(tmp, "%s #%d has been mounted via loopback mount", mds, i);
336mr_free(mds);
337
338log_msg(1, tmp);
339if (i < 0) {
340popup_and_OK
341 ("Cannot find ISO images in the directory you specified.");
342retval = 1;
343}
344log_msg(2, "%ld: bkpinfo->isodir is now %s", __LINE__,
345 bkpinfo->isodir);
346paranoid_free(tmp);
347paranoid_free(command);
348return (retval);
349}
350
351
352
353
354/**
355* Kill all Petris processes.
356*/
357void kill_petris(void) {
358 kill_anything_like_this("petris");
359}
360
361/**************************************************************************
362*END_KILL_PETRIS *
363**************************************************************************/
364
365
366/**
367 * Mount @p device at @p mpt as @p format.
368 * @param device The device (/dev entry) to mount.
369 * @param mpt The directory to mount it on.
370 * @param format The filesystem type of @p device.
371 * @param writeable If TRUE, mount read-write; if FALSE, mount read-only.
372 * @return 0 for success, nonzero for failure.
373 */
374int mount_device(char *device, char *mpt, char *format, bool writeable)
375{
376int res = 0;
377
378/** malloc **/
379char *tmp, *command, *mountdir, *mountpoint;
380char *additional_parameters = NULL;
381
382assert_string_is_neither_NULL_nor_zerolength(device);
383assert_string_is_neither_NULL_nor_zerolength(mpt);
384assert(format != NULL);
385malloc_string(tmp);
386malloc_string(command);
387malloc_string(mountdir);
388malloc_string(mountpoint);
389
390 if (!strcmp(mpt, "/1")) {
391 strcpy(mountpoint, "/");
392 log_msg(3, "Mommm! SME is being a dildo!");
393 } else {
394 strcpy(mountpoint, mpt);
395 }
396
397 if (!strcmp(mountpoint, "lvm")) {
398 return (0);
399 }
400 if (!strcmp(mountpoint, "image")) {
401 return (0);
402 }
403 sprintf(tmp, "Mounting device %s ", device);
404 log_msg(1, tmp);
405 /* Deal with additional params only if not /proc or /sys */
406 mr_asprintf(&additional_parameters, "");
407 if (strcmp(format, "proc") && strcmp(format, "sys")) {
408 if (writeable) {
409 mr_strcat(additional_parameters, "-o rw");
410 } else {
411 mr_strcat(additional_parameters, "-o ro");
412 }
413 if (find_home_of_exe("setfattr")) {
414 mr_strcat(additional_parameters, ",user_xattr");
415 }
416 if (find_home_of_exe("setfacl")) {
417 mr_strcat(additional_parameters, ",acl");
418 }
419 }
420
421 if (!strcmp(mountpoint, "swap")) {
422 sprintf(command, "swapon %s", device);
423 } else {
424 if (!strcmp(mountpoint, "/")) {
425 strcpy(mountdir, MNT_RESTORING);
426 } else {
427 sprintf(mountdir, "%s%s", MNT_RESTORING, mountpoint);
428 }
429 sprintf(command, "mkdir -p %s", mountdir);
430 run_program_and_log_output(command, FALSE);
431 sprintf(command, "mount -t %s %s %s %s 2>> %s", format, device,
432 additional_parameters, mountdir, MONDO_LOGFILE);
433 log_msg(2, "command='%s'", command);
434 }
435 paranoid_free(additional_parameters);
436
437 res = run_program_and_log_output(command, TRUE);
438 if (res && (strstr(command, "xattr") || strstr(command, "acl"))) {
439 log_msg(1, "Re-trying without the fancy extra parameters");
440 sprintf(command, "mount -t %s %s %s 2>> %s", format, device,
441 mountdir, MONDO_LOGFILE);
442 res = run_program_and_log_output(command, TRUE);
443 }
444 if (res) {
445 log_msg(1, "Unable to mount device %s (type %s) at %s", device,
446 format, mountdir);
447 log_msg(1, "command was '%s'", command);
448 if (!strcmp(mountpoint, "swap")) {
449 log_to_screen(tmp);
450 } else {
451 log_msg(2, "Retrying w/o the '-t' switch");
452 sprintf(command, "mount %s %s 2>> %s", device, mountdir,
453 MONDO_LOGFILE);
454 log_msg(2, "2nd command = '%s'", command);
455 res = run_program_and_log_output(command, TRUE);
456 if (res == 0) {
457 log_msg(1,
458 "That's OK. I called mount w/o a filesystem type and it worked fine in the end.");
459 } else {
460 log_to_screen(tmp);
461 }
462 }
463 }
464
465 if (res && !strcmp(mountpoint, "swap")) {
466 log_msg(2, "That's ok. It's just a swap partition.");
467 log_msg(2, "Non-fatal error. Returning 0.");
468 res = 0;
469 }
470
471paranoid_free(tmp);
472paranoid_free(command);
473paranoid_free(mountdir);
474paranoid_free(mountpoint);
475
476 return (res);
477}
478/**************************************************************************
479 *END_MOUNT_DEVICE *
480**************************************************************************/
481
482
483/**
484 * Mount all devices in @p p_external_copy_of_mountlist on @p MNT_RESTORING.
485 * @param p_external_copy_of_mountlist The mountlist containing devices to be mounted.
486 * @param writeable If TRUE, then mount read-write; if FALSE mount read-only.
487 * @return The number of errors encountered (0 for success).
488 */
489int mount_all_devices(struct mountlist_itself
490 *p_external_copy_of_mountlist, bool writeable)
491{
492int retval = 0, lino, res;
493char *tmp;
494char *these_failed = NULL;
495char *format;
496struct mountlist_itself *mountlist = NULL;
497
498malloc_string(tmp);
499malloc_string(format);
500
501assert(p_external_copy_of_mountlist != NULL);
502mountlist = malloc(sizeof(struct mountlist_itself));
503memcpy((void *) mountlist, (void *) p_external_copy_of_mountlist,
504 sizeof(struct mountlist_itself));
505 sort_mountlist_by_mountpoint(mountlist, 0);
506
507
508 mvaddstr_and_log_it(g_currentY, 0, "Mounting devices ");
509 open_progress_form("Mounting devices",
510 "I am now mounting all the drives.",
511 "This should not take long.",
512 "", mountlist->entries);
513
514 mr_asprintf(&these_failed, "");
515 for (lino = 0; lino < mountlist->entries; lino++) {
516 if (!strcmp(mountlist->el[lino].device, "/proc")) {
517 log_msg(1,
518 "Again with the /proc - why is this in your mountlist?");
519 } else if (is_this_device_mounted(mountlist->el[lino].device)) {
520 sprintf(tmp, "%s is already mounted",
521 mountlist->el[lino].device);
522 log_to_screen(tmp);
523 } else if (strcmp(mountlist->el[lino].mountpoint, "none")
524 && strcmp(mountlist->el[lino].mountpoint, "lvm")
525 && strcmp(mountlist->el[lino].mountpoint, "raid")
526 && strcmp(mountlist->el[lino].mountpoint, "image")) {
527 sprintf(tmp, "Mounting %s", mountlist->el[lino].device);
528 update_progress_form(tmp);
529 strcpy(format, mountlist->el[lino].format);
530 res = mount_device(mountlist->el[lino].device,
531 mountlist->el[lino].mountpoint,
532 format, writeable);
533 retval += res;
534 if (res) {
535 mr_strcat(these_failed, "%s ",mountlist->el[lino].device);
536 }
537 }
538 g_current_progress++;
539 }
540 close_progress_form();
541 if (retval) {
542 if (g_partition_table_locked_up > 0) {
543 log_to_screen
544 ("fdisk's ictol() call to refresh its copy of the partition table causes the kernel to");
545 log_to_screen
546 ("lock up the partition table. You might have to reboot and use Interactive Mode to");
547 log_to_screen
548 ("format and restore *without* partitioning first. Sorry for the inconvenience.");
549 }
550 sprintf(tmp, "Could not mount device(s) %s- shall I abort?", these_failed);
551
552 if (!ask_me_yes_or_no(tmp)) {
553 retval = 0;
554 log_to_screen
555 ("Continuing, although some device(s) failed to be mounted");
556 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
557 } else {
558 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
559 log_to_screen
560 ("Unable to mount some or all of your partitions.");
561 }
562 } else {
563 log_to_screen("All partitions were mounted OK.");
564 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
565 }
566 paranoid_free(these_failed);
567
568 /* Also mounting under MNT_RESTORING special FS */
569 (void)mount_device("/proc","/proc","proc",TRUE);
570 (void)mount_device("/sys","/sys","sysfs",TRUE);
571 run_program_and_log_output("df -m", 3);
572 paranoid_free(mountlist);
573 paranoid_free(tmp);
574 paranoid_free(format);
575 return (retval);
576}
577/**************************************************************************
578*END_MOUNT_ALL_DEVICES *
579**************************************************************************/
580
581
582
583
584/**
585* Fix some miscellaneous things in the filesystem so the system will come
586* up correctly on the first boot.
587*/
588void protect_against_braindead_sysadmins()
589{
590run_program_and_log_output("touch " MNT_RESTORING "/var/log/pacct",
591 FALSE);
592run_program_and_log_output("touch " MNT_RESTORING "/var/account/pacct",
593 FALSE);
594if (run_program_and_log_output("ls " MNT_RESTORING " /tmp", FALSE)) {
595run_program_and_log_output("chmod 1777 " MNT_RESTORING "/tmp",
596 FALSE);
597}
598run_program_and_log_output("mkdir -p " MNT_RESTORING
599 "/var/run/console", FALSE);
600run_program_and_log_output("chmod 777 " MNT_RESTORING "/dev/null",
601 FALSE);
602run_program_and_log_output("cd " MNT_RESTORING
603 "; for i in `ls home/`; do echo \"Moving $i's spurious files to $i/.disabled\"; mkdir \"$i\"/.disabled ; mv -f \"$i\"/.DCOP* \"$i\"/.MCOP* \"$i\"/.*authority \"$i\"/.kde/tmp* \"$i\"/.kde/socket* \"$i\"/.disabled/ ; done",
604 TRUE);
605run_program_and_log_output("rm -f " MNT_RESTORING "/var/run/*.pid",
606 TRUE);
607run_program_and_log_output("rm -f " MNT_RESTORING "/var/lock/subsys/*",
608 TRUE);
609}
610
611/**************************************************************************
612*END_PROTECT_AGAINST_BRAINDEAD_SYSADMINS *
613**************************************************************************/
614
615
616
617
618/**
619* Fill out @p bkpinfo based on @p cfg_file.
620* @param cfg_file The mondo-restore.cfg file to read into @p bkpinfo.
621* @param bkpinfo The backup information structure to fill out with information
622* from @p cfg_file.
623* @return 0 for success, nonzero for failure.
624*/
625int read_cfg_file_into_bkpinfo(char *cfgf)
626{
627/** add mallocs **/
628char *value = NULL;
629char *tmp = NULL;
630char *envtmp1 = NULL;
631char *envtmp2 = NULL;
632char *command = NULL;
633char *iso_mnt = NULL;
634char *iso_path = NULL;
635char *old_isodir = NULL;
636char cfg_file[100];
637t_bkptype media_specified_by_user;
638
639malloc_string(command);
640malloc_string(iso_mnt);
641malloc_string(iso_path);
642malloc_string(old_isodir);
643malloc_string(value);
644malloc_string(tmp);
645// assert_string_is_neither_NULL_nor_zerolength(cfg_file);
646assert(bkpinfo != NULL);
647
648if (!cfgf) {
649 strcpy(cfg_file, g_mondo_cfg_file);
650} else {
651 strcpy(cfg_file, cfgf);
652}
653
654media_specified_by_user = bkpinfo->backup_media_type; // or 'none', if not specified
655
656if (0 == read_cfg_var(cfg_file, "backup-media-type", value)) {
657if (!strcmp(value, "cdstream")) {
658 bkpinfo->backup_media_type = cdstream;
659} else if (!strcmp(value, "cdr")) {
660 bkpinfo->backup_media_type = cdr;
661} else if (!strcmp(value, "cdrw")) {
662 bkpinfo->backup_media_type = cdrw;
663} else if (!strcmp(value, "dvd")) {
664 bkpinfo->backup_media_type = dvd;
665} else if (!strcmp(value, "usb")) {
666 bkpinfo->backup_media_type = usb;
667 bkpinfo->please_dont_eject = TRUE;
668} else if (!strcmp(value, "iso")) {
669
670// Patch by Conor Daly - 2004/07/12
671 bkpinfo->backup_media_type = iso;
672 if (am_I_in_disaster_recovery_mode()) {
673 /* Check to see if CD is already mounted before mounting it... */
674 if (!is_this_device_mounted("/dev/cdrom")) {
675 log_msg(2,
676 "NB: CDROM device not mounted, mounting...");
677 run_program_and_log_output("mount /dev/cdrom "
678 MNT_CDROM, 1);
679 }
680 if (does_file_exist(MNT_CDROM "/archives/filelist.0")) {
681 bkpinfo->backup_media_type = cdr;
682 run_program_and_log_output("umount -d " MNT_CDROM, 1);
683 log_it
684 ("Re-jigging configuration AGAIN. CD-R, not ISO.");
685 }
686 }
687 if (read_cfg_var(cfg_file, "iso-prefix", value) == 0) {
688 strcpy(bkpinfo->prefix,value);
689 } else {
690 strcpy(bkpinfo->prefix,STD_PREFIX);
691 }
692} else if ((!strcmp(value, "netfs")) || (!strcmp(value, "nfs"))) {
693 /* Stay compatible with previous versions by allowing nfs as an entry here */
694 bkpinfo->backup_media_type = netfs;
695 bkpinfo->please_dont_eject = TRUE;
696 if (read_cfg_var(cfg_file, "netfs-proto", value) == 0) {
697 mr_asprintf(&(bkpinfo->netfs_proto),"%s", value);
698 } else {
699 /* For compatibility, force protocol in old nfs case to be transparent */
700 mr_asprintf(&(bkpinfo->netfs_proto), "nfs");
701 }
702 if (read_cfg_var(cfg_file, "netfs-server-user", value) == 0) {
703 mr_asprintf(&(bkpinfo->netfs_user),"%s", value);
704 }
705 if (read_cfg_var(cfg_file, "iso-prefix", value) == 0) {
706 strcpy(bkpinfo->prefix,value);
707 } else {
708 strcpy(bkpinfo->prefix,STD_PREFIX);
709 }
710 if (strstr(call_program_and_get_last_line_of_output
711 ("cat /proc/cmdline"), "pxe")) {
712 /* We need to override prefix value in PXE mode as it's
713 * already done in start-netfs */
714 envtmp1 = getenv("imgname");
715 if (envtmp1 == NULL) {
716 fatal_error("no imgname variable in environment");
717 }
718 strcpy(bkpinfo->prefix,envtmp1);
719 }
720
721} else if (!strcmp(value, "tape")) {
722 bkpinfo->backup_media_type = tape;
723} else if (!strcmp(value, "udev")) {
724 bkpinfo->backup_media_type = udev;
725} else {
726 fatal_error("UNKNOWN bkp-media-type");
727}
728} else {
729fatal_error("backup-media-type not specified!");
730}
731if (bkpinfo->disaster_recovery) {
732 if (bkpinfo->backup_media_type == cdstream) {
733 sprintf(bkpinfo->media_device, "/dev/cdrom");
734// bkpinfo->media_size[0] = -1;
735 bkpinfo->media_size[0] = 1999 * 1024;
736 bkpinfo->media_size[1] = 650; /* good guess */
737 } else if (bkpinfo->backup_media_type == usb) {
738 envtmp1 = getenv("MRUSBDEV");
739 if (envtmp1 == NULL) {
740 if (read_cfg_var(cfg_file, "usb-dev", value)) {
741 fatal_error("Cannot get USB device name from cfg file");
742 }
743 } else {
744 strcpy(value,envtmp1);
745 }
746 sprintf(bkpinfo->media_device, "%s1", value);
747 sprintf(tmp, "Backup medium is USB --- dev=%s", bkpinfo->media_device);
748 log_msg(2, tmp);
749 } else if (bkpinfo->backup_media_type == tape
750 || bkpinfo->backup_media_type == udev) {
751 if (read_cfg_var(cfg_file, "media-dev", value)) {
752 fatal_error("Cannot get tape device name from cfg file");
753 }
754 strcpy(bkpinfo->media_device, value);
755 read_cfg_var(cfg_file, "media-size", value);
756 bkpinfo->media_size[1] = atol(value);
757 sprintf(tmp, "Backup medium is TAPE --- dev=%s",
758 bkpinfo->media_device);
759 log_msg(2, tmp);
760 } else {
761 strcpy(bkpinfo->media_device, "/dev/cdrom"); /* we don't really need this var */
762 bkpinfo->media_size[0] = 1999 * 1024; /* 650, probably, but we don't need this var anyway */
763 bkpinfo->media_size[1] = 1999 * 1024; /* 650, probably, but we don't need this var anyway */
764 log_msg(2, "Backup medium is CD-R[W]");
765 }
766} else {
767 log_msg(2,
768 "Not in Disaster Recovery Mode. No need to derive device name from config file.");
769}
770
771read_cfg_var(cfg_file, "use-star", value);
772if (strstr(value, "yes")) {
773 bkpinfo->use_star = TRUE;
774 log_msg(1, "Goody! ... bkpinfo->use_star is now true.");
775}
776
777read_cfg_var(cfg_file, "obdr", value);
778if (strstr(value, "TRUE")) {
779 bkpinfo->use_obdr = TRUE;
780 log_msg(1, "OBDR mode activated");
781}
782
783read_cfg_var(cfg_file, "acl", value);
784if (strstr(value, "TRUE")) {
785 mr_asprintf(&g_getfacl,"setfacl");
786 log_msg(1, "We will restore ACLs");
787 if (! find_home_of_exe("setfacl")) {
788 log_msg(1, "Unable to restore ACLs as no setfacl found");
789 }
790}
791read_cfg_var(cfg_file, "xattr", value);
792if (strstr(value, "TRUE")) {
793 mr_asprintf(&g_getfattr,"setfattr");
794 log_msg(1, "We will restore XATTRs");
795 if (! find_home_of_exe("setfattr")) {
796 log_msg(1, "Unable to restore XATTRs as no setfattr found");
797 }
798}
799
800if (0 == read_cfg_var(cfg_file, "internal-tape-block-size", value)) {
801bkpinfo->internal_tape_block_size = atol(value);
802log_msg(1, "Internal tape block size has been custom-set to %ld",
803 bkpinfo->internal_tape_block_size);
804} else {
805bkpinfo->internal_tape_block_size =
806 DEFAULT_INTERNAL_TAPE_BLOCK_SIZE;
807log_msg(1, "Internal tape block size = default (%ld)",
808 DEFAULT_INTERNAL_TAPE_BLOCK_SIZE);
809}
810
811read_cfg_var(cfg_file, "use-lzo", value);
812if (strstr(value, "yes")) {
813bkpinfo->use_lzo = TRUE;
814bkpinfo->use_gzip = FALSE;
815strcpy(bkpinfo->zip_exe, "lzop");
816strcpy(bkpinfo->zip_suffix, "lzo");
817} else {
818read_cfg_var(cfg_file, "use-gzip", value);
819if (strstr(value, "yes")) {
820 bkpinfo->use_lzo = FALSE;
821 bkpinfo->use_gzip = TRUE;
822 strcpy(bkpinfo->zip_exe, "gzip");
823 strcpy(bkpinfo->zip_suffix, "gz");
824} else {
825 read_cfg_var(cfg_file, "use-comp", value);
826 if (strstr(value, "yes")) {
827 bkpinfo->use_lzo = FALSE;
828 bkpinfo->use_gzip = FALSE;
829 strcpy(bkpinfo->zip_exe, "bzip2");
830 strcpy(bkpinfo->zip_suffix, "bz2");
831 } else {
832 bkpinfo->zip_exe[0] = bkpinfo->zip_suffix[0] = '\0';
833 }
834}
835}
836
837value[0] = '\0';
838read_cfg_var(cfg_file, "differential", value);
839if (!strcmp(value, "yes") || !strcmp(value, "1")) {
840bkpinfo->differential = TRUE;
841}
842log_msg(2, "differential var = '%s'", value);
843if (bkpinfo->differential) {
844log_msg(2, "THIS IS A DIFFERENTIAL BACKUP");
845} else {
846log_msg(2, "This is a regular (full) backup");
847}
848
849read_cfg_var(g_mondo_cfg_file, "please-dont-eject", tmp);
850if (tmp[0]
851||
852strstr(call_program_and_get_last_line_of_output
853 ("cat /proc/cmdline"), "donteject")) {
854bkpinfo->please_dont_eject = TRUE;
855log_msg(2, "Ok, I shan't eject when restoring! Groovy.");
856}
857
858if (bkpinfo->backup_media_type == netfs) {
859 if (!cfgf) {
860 log_msg(2, "netfs_mount remains %s", bkpinfo->netfs_mount);
861 log_msg(2, "netfs_remote_dir remains %s",
862 bkpinfo->netfs_remote_dir);
863 log_msg(2,
864 "...cos it wouldn't make sense to abandon the values that GOT ME to this config file in the first place");
865 } else {
866 read_cfg_var(g_mondo_cfg_file, "netfs-server-mount",
867 bkpinfo->netfs_mount);
868 read_cfg_var(g_mondo_cfg_file, "netfs-server-path",
869 bkpinfo->netfs_remote_dir);
870 log_msg(2, "netfs_mount is %s", bkpinfo->netfs_mount);
871 log_msg(2, "netfs_proto is %s", bkpinfo->netfs_proto);
872 if (bkpinfo->netfs_user) {
873 log_msg(2, "netfs_user is %s", bkpinfo->netfs_user);
874 }
875 log_msg(2, "netfs_remote_dir is %s", bkpinfo->netfs_remote_dir);
876 }
877 if (strstr(call_program_and_get_last_line_of_output
878 ("cat /proc/cmdline"), "pxe")) {
879 /* We need to override values in PXE mode as it's
880 * already done in start-netfs */
881 envtmp1 = getenv("netfsmount");
882 if (envtmp1 == NULL) {
883 fatal_error("no netfsmount variable in environment");
884 }
885 envtmp2 = getenv("dirimg");
886 if (envtmp2 == NULL) {
887 fatal_error("no dirimg variable in environment");
888 }
889 strcpy(bkpinfo->netfs_mount,envtmp1);
890 strcpy(bkpinfo->netfs_remote_dir,envtmp2);
891 }
892} else if (bkpinfo->backup_media_type == iso) {
893 /* Patch by Conor Daly 23-june-2004
894 * to correctly mount iso-dev and set a sensible
895 * isodir in disaster recovery mode
896 */
897 strcpy(old_isodir, bkpinfo->isodir);
898 read_cfg_var(g_mondo_cfg_file, "iso-mnt", iso_mnt);
899 read_cfg_var(g_mondo_cfg_file, "isodir", iso_path);
900 sprintf(bkpinfo->isodir, "%s%s", iso_mnt, iso_path);
901 if (!bkpinfo->isodir[0]) {
902 strcpy(bkpinfo->isodir, old_isodir);
903 }
904 if (!bkpinfo->disaster_recovery) {
905 if (strcmp(old_isodir, bkpinfo->isodir)) {
906 log_it
907 ("user nominated isodir %s differs from archive %s, keeping user's choice\n",
908 old_isodir, bkpinfo->isodir);
909 strcpy(bkpinfo->isodir, old_isodir);
910 }
911 }
912 read_cfg_var(g_mondo_cfg_file, "iso-dev", g_isodir_device);
913 log_msg(2, "isodir=%s; iso-dev=%s", bkpinfo->isodir,
914 g_isodir_device);
915 if (bkpinfo->disaster_recovery) {
916 if (is_this_device_mounted(g_isodir_device)) {
917 log_msg(2, "NB: isodir is already mounted");
918 /* Find out where it's mounted */
919 sprintf(command,
920 "mount | grep -E '^%s' | tail -n1 | cut -d' ' -f3",
921 g_isodir_device);
922 log_it("command = %s", command);
923 log_it("res of it = %s",
924 call_program_and_get_last_line_of_output(command));
925 sprintf(iso_mnt, "%s",
926 call_program_and_get_last_line_of_output(command));
927 } else {
928 sprintf(iso_mnt, "/tmp/isodir");
929 sprintf(tmp, "mkdir -p %s", iso_mnt);
930 run_program_and_log_output(tmp, 5);
931 sprintf(tmp, "mount %s %s", g_isodir_device, iso_mnt);
932 if (run_program_and_log_output(tmp, 3)) {
933 log_msg(1,
934 "Unable to mount isodir. Perhaps this is really a CD backup?");
935 bkpinfo->backup_media_type = cdr;
936 strcpy(bkpinfo->media_device, "/dev/cdrom"); /* superfluous */
937 bkpinfo->isodir[0] = iso_mnt[0] = iso_path[0] = '\0';
938 if (mount_media()) {
939 fatal_error
940 ("Unable to mount isodir. Failed to mount CD-ROM as well.");
941 } else {
942 log_msg(1,
943 "You backed up to disk, then burned some CDs.");
944 }
945 }
946 }
947 /* bkpinfo->isodir should now be the true path to prefix-1.iso etc... */
948 if (bkpinfo->backup_media_type == iso) {
949 sprintf(bkpinfo->isodir, "%s%s", iso_mnt, iso_path);
950 }
951 }
952}
953
954if (media_specified_by_user != none) {
955 if (g_restoring_live_from_cd) {
956 if (bkpinfo->backup_media_type != media_specified_by_user) {
957 log_msg(2,
958 "bkpinfo->backup_media_type != media_specified_by_user, so I'd better ask :)");
959 interactively_obtain_media_parameters_from_user(FALSE);
960 media_specified_by_user = bkpinfo->backup_media_type;
961 get_cfg_file_from_archive();
962 /*
963 if (media_specified_by_user != cdr && media_specified_by_user == cdrw)
964 { g_restoring_live_from_cd = FALSE; }
965 */
966 }
967 }
968 bkpinfo->backup_media_type = media_specified_by_user;
969}
970g_backup_media_type = bkpinfo->backup_media_type;
971paranoid_free(value);
972paranoid_free(tmp);
973paranoid_free(command);
974paranoid_free(iso_mnt);
975paranoid_free(iso_path);
976paranoid_free(old_isodir);
977return (0);
978
979}
980
981/**************************************************************************
982*END_READ_CFG_FILE_INTO_BKPINFO *
983**************************************************************************/
984
985
986
987
988/**
989 * Allow the user to edit the filelist and biggielist.
990 * The filelist is unlinked after it is read.
991 * @param bkpinfo The backup information structure. Fields used:
992 * - @c bkpinfo->backup_media_type
993 * - @c bkpinfo->isodir
994 * - @c bkpinfo->media_device
995 * - @c bkpinfo->tmpdir
996 * @return The filelist structure containing the information read from disk.
997 */
998struct
999s_node *process_filelist_and_biggielist()
1000{
1001struct s_node *filelist;
1002
1003/** add mallocs**/
1004char *command;
1005char *tmp;
1006char *q;
1007int res = 0;
1008pid_t pid;
1009bool extract_mountlist_stub = FALSE;
1010
1011assert(bkpinfo != NULL);
1012malloc_string(command);
1013malloc_string(tmp);
1014
1015/* If those files already exist, do not overwrite them later on */
1016if (does_file_exist("/"MOUNTLIST_FNAME_STUB)) {
1017 extract_mountlist_stub = FALSE;
1018} else {
1019 extract_mountlist_stub = TRUE;
1020}
1021
1022if (does_file_exist(g_filelist_full)
1023&& does_file_exist(g_biggielist_txt)) {
1024 log_msg(1, "%s exists", g_filelist_full);
1025 log_msg(1, "%s exists", g_biggielist_txt);
1026 log_msg(2,
1027 "Filelist and biggielist already recovered from media. Yay!");
1028} else {
1029 getcwd(tmp, MAX_STR_LEN);
1030 if (chdir(bkpinfo->tmpdir)) {
1031 // FIXME
1032 }
1033 log_msg(1, "chdir(%s)", bkpinfo->tmpdir);
1034 log_to_screen("Extracting filelist and biggielist from media...");
1035 unlink("/tmp/filelist.full");
1036 unlink(FILELIST_FULL_STUB);
1037 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1038 sprintf(command,
1039 "tar -b %ld -zxf %s ./%s ./%s ./%s ./%s ./%s",
1040 bkpinfo->internal_tape_block_size,
1041 bkpinfo->media_device,
1042 MOUNTLIST_FNAME_STUB,
1043 BIGGIELIST_TXT_STUB,
1044 FILELIST_FULL_STUB,
1045 IWANTMYLVM_STUB,
1046 MONDO_CFG_FILE_STUB);
1047 log_msg(1, "tarcommand = %s", command);
1048 run_program_and_log_output(command, 1);
1049 if (!does_file_exist(FILELIST_FULL_STUB)) {
1050 /* Doing that allow us to remain compatible with pre-2.2.5 versions */
1051 log_msg(2, "pre-2.2.4 compatible mode on");
1052 sprintf(command,
1053 "tar -b %ld -zxf %s %s %s %s %s %s",
1054 bkpinfo->internal_tape_block_size,
1055 bkpinfo->media_device,
1056 MOUNTLIST_FNAME_STUB,
1057 BIGGIELIST_TXT_STUB,
1058 FILELIST_FULL_STUB,
1059 IWANTMYLVM_STUB,
1060 MONDO_CFG_FILE_STUB);
1061 log_msg(1, "tarcommand = %s", command);
1062 run_program_and_log_output(command, 1);
1063 }
1064 } else {
1065 log_msg(2,
1066 "Calling insist_on_this_cd_number; bkpinfo->isodir=%s",
1067 bkpinfo->isodir);
1068 insist_on_this_cd_number(1);
1069 log_msg(2, "Back from iotcn");
1070 run_program_and_log_output("mount", 1);
1071 sprintf(command,
1072 "tar -zxf %s/images/all.tar.gz ./%s ./%s ./%s ./%s ./%s",
1073 MNT_CDROM,
1074 MOUNTLIST_FNAME_STUB,
1075 BIGGIELIST_TXT_STUB,
1076 FILELIST_FULL_STUB,
1077 IWANTMYLVM_STUB,
1078 MONDO_CFG_FILE_STUB);
1079
1080 log_msg(1, "tarcommand = %s", command);
1081 run_program_and_log_output(command, 1);
1082 if (!does_file_exist(FILELIST_FULL_STUB)) {
1083 /* Doing that allow us to remain compatible with pre-2.2.5 versions */
1084 log_msg(2, "pre-2.2.4 compatible mode on");
1085 sprintf(command,
1086 "tar -zxf %s/images/all.tar.gz %s %s %s %s %s",
1087 MNT_CDROM,
1088 MOUNTLIST_FNAME_STUB,
1089 BIGGIELIST_TXT_STUB,
1090 FILELIST_FULL_STUB,
1091 IWANTMYLVM_STUB,
1092 MONDO_CFG_FILE_STUB);
1093
1094 log_msg(1, "tarcommand = %s", command);
1095 run_program_and_log_output(command, 1);
1096 }
1097 if (!does_file_exist(BIGGIELIST_TXT_STUB)) {
1098 fatal_error
1099 ("all.tar.gz did not include " BIGGIELIST_TXT_STUB);
1100 }
1101 if (!does_file_exist(FILELIST_FULL_STUB)) {
1102 fatal_error
1103 ("all.tar.gz did not include " FILELIST_FULL_STUB);
1104 }
1105 }
1106 sprintf(command, "cp -f %s %s", MONDO_CFG_FILE_STUB,
1107 g_mondo_cfg_file);
1108 run_program_and_log_output(command, FALSE);
1109
1110 sprintf(command, "cp -f %s/%s %s", bkpinfo->tmpdir,
1111 BIGGIELIST_TXT_STUB, g_biggielist_txt);
1112 log_msg(1, "command = %s", command);
1113 paranoid_system(command);
1114 sprintf(command, "ln -sf %s/%s %s", bkpinfo->tmpdir,
1115 FILELIST_FULL_STUB, g_filelist_full);
1116 log_msg(1, "command = %s", command);
1117 paranoid_system(command);
1118 }
1119
1120 if (am_I_in_disaster_recovery_mode()
1121 &&
1122 /* If it was there, do not overwrite it */
1123 (extract_mountlist_stub)
1124 &&
1125 ask_me_yes_or_no("Do you want to retrieve the mountlist as well?"))
1126 {
1127 sprintf(command, "ln -sf %s/%s /tmp", MOUNTLIST_FNAME_STUB,
1128 bkpinfo->tmpdir);
1129 paranoid_system(command);
1130 }
1131
1132 if (chdir(tmp)) {
1133 // FIXME
1134 }
1135
1136 if (!does_file_exist(g_biggielist_txt)) {
1137 log_msg(1, "Warning - %s not found", g_biggielist_txt);
1138 }
1139 if (!does_file_exist(g_filelist_full)) {
1140 log_msg(1, "Warning - %s does not exist", g_filelist_full);
1141 }
1142// popup_and_OK("Wonderful.");
1143
1144 log_msg(2, "Forking");
1145 pid = fork();
1146 switch (pid) {
1147 case -1:
1148 fatal_error("Forking error");
1149 break;
1150
1151 case 0:
1152 log_to_screen("Pre-processing filelist");
1153 if (!does_file_exist(g_biggielist_txt)) {
1154 sprintf(command, "echo -n > %s", g_biggielist_txt);
1155 paranoid_system(command);
1156 }
1157 sprintf(command, "grep -E '^/dev/.*' %s > %s",
1158 g_biggielist_txt, g_filelist_imagedevs);
1159 paranoid_system(command);
1160 exit(0);
1161 break;
1162
1163 default:
1164 open_evalcall_form("Pre-processing filelist");
1165 while (!waitpid(pid, (int *) 0, WNOHANG)) {
1166 usleep(100000);
1167 update_evalcall_form(0);
1168 }
1169 }
1170 close_evalcall_form();
1171
1172 log_msg(3, "loading filelist");
1173 filelist = load_filelist(g_filelist_full);
1174 log_msg(3, "deleting original filelist");
1175 unlink(g_filelist_full);
1176 if (g_text_mode) {
1177 q = NULL;
1178 while (q == NULL) {
1179 printf("Restore which directory? --> ");
1180 q = fgets(tmp, sizeof(tmp), stdin);
1181 }
1182 toggle_path_selection(filelist, tmp, TRUE);
1183 if (strlen(tmp) == 0) {
1184 res = 1;
1185 } else {
1186 res = 0;
1187 }
1188 } else {
1189 res = edit_filelist(filelist);
1190 }
1191 if (res) {
1192 log_msg(2, "User hit 'cancel'. Freeing filelist and aborting.");
1193 free_filelist(filelist);
1194 return (NULL);
1195 }
1196 ask_about_these_imagedevs(g_filelist_imagedevs, g_imagedevs_restthese);
1197 close_evalcall_form();
1198
1199 // NB: It's not necessary to add g_biggielist_txt to the filelist.full
1200 // file. The filelist.full file already contains the filename of EVERY
1201 // file backed up - regular and biggie files.
1202
1203 // However, we do want to make sure the imagedevs selected by the user
1204 // are flagged for restoring.
1205 if (length_of_file(g_imagedevs_restthese) > 2) {
1206 add_list_of_files_to_filelist(filelist, g_imagedevs_restthese,
1207 TRUE);
1208 }
1209
1210 paranoid_free(command);
1211 paranoid_free(tmp);
1212 return (filelist);
1213}
1214
1215/**************************************************************************
1216 *END_ PROCESS_FILELIST_AND_BIGGIELIST *
1217 **************************************************************************/
1218
1219
1220
1221
1222/**
1223 * Make a backup copy of <tt>path_root</tt>/<tt>filename</tt>.
1224 * The backup filename is the filename of the original with ".pristine" added.
1225 * @param path_root The place where the filesystem is mounted (e.g. MNT_RESTORING).
1226 * @param filename The filename (absolute path) within @p path_root.
1227 * @return 0 for success, nonzero for failure.
1228 */
1229int backup_crucial_file(char *path_root, char *filename)
1230{
1231 char *tmp;
1232 char *command;
1233 int res;
1234
1235 malloc_string(tmp);
1236 malloc_string(command);
1237 assert(path_root != NULL);
1238 assert_string_is_neither_NULL_nor_zerolength(filename);
1239
1240 sprintf(tmp, "%s/%s", path_root, filename);
1241 sprintf(command, "cp -f %s %s.pristine", tmp, tmp);
1242
1243 res = run_program_and_log_output(command, 5);
1244 paranoid_free(tmp);
1245 paranoid_free(command);
1246 return (res);
1247}
1248
1249void offer_to_make_initrd() {
1250
1251if (bkpinfo->restore_mode != nuke) {
1252 if (ask_me_yes_or_no
1253 ("You will now be able to re-generate your initrd.\nThis is especially useful if you changed of hardware configuration, cloned, made P2V, used multipath...\nDo you need to do it ?")) {
1254 log_msg(1,"Launching shell for manual initrd recreation");
1255 if (does_file_exist(MNT_RESTORING"/etc/arch-release")) {
1256 popup_and_OK("You'll now be chrooted under your future / partition.\nEdit /etc/mkinitcpio.conf if needed and rebuild your initrd with the kernel preset name e.g.\nmkinitcpio -p kernel26\nThen type exit to finish.\n");
1257 } else {
1258 popup_and_OK("You'll now be chrooted under your future / partition.\nGo under /boot and rebuild your initrd with\nmkinitrd -f -v initrd-2.x.y.img 2.x.y e.g.\nThen type exit to finish.");
1259 }
1260 mvaddstr_and_log_it(g_currentY, 0, "Modifying initrd...");
1261 if (!g_text_mode) {
1262 newtSuspend();
1263 }
1264 paranoid_system("chroot " MNT_RESTORING);
1265 if (!g_text_mode) {
1266 newtResume();
1267 }
1268 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1269 } else {
1270 return;
1271 }
1272} else {
1273 log_to_screen("Non-interactive mode: no way to give you the keyboard so that you re-generate your initrd. Hope it's OK");
1274 log_msg(1,"Non-interactive mode: no way to give you the keyboard so that you re-generate your initrd. Hope it's OK");
1275}
1276}
1277
1278
1279/**
1280 * Install the user's boot loader in the MBR.
1281 * Currently LILO, ELILO, GRUB, RAW (dd of MBR), and the FreeBSD bootloader are supported.
1282 * @param offer_to_hack_scripts If TRUE, then offer to hack the user's fstab for them.
1283 * @return 0 for success, nonzero for failure.
1284 */
1285int run_boot_loader(bool offer_to_hack_scripts)
1286{
1287 int res;
1288 int retval = 0;
1289
1290 /** malloc *******/
1291 char *device;
1292 char *tmp = NULL;
1293 char *name;
1294 char *cmd = NULL;
1295
1296 malloc_string(device);
1297 malloc_string(name);
1298
1299 /* In order to have a working bootloader, we need to have all devices
1300 * ready in the chroot. If they are not there (udev) then copy them from
1301 * the current /dev location
1302 */
1303 mr_asprintf(&cmd,"tar cf - /dev | ( cd %s ; tar xf - )",MNT_RESTORING);
1304 run_program_and_log_output(cmd, 3);
1305 paranoid_free(cmd);
1306
1307 backup_crucial_file(MNT_RESTORING, "/etc/fstab");
1308 backup_crucial_file(MNT_RESTORING, "/boot/grub/menu.lst");
1309 backup_crucial_file(MNT_RESTORING, "/boot/grub/grub.cfg");
1310 backup_crucial_file(MNT_RESTORING, "/boot/grub2/grub.cfg");
1311 backup_crucial_file(MNT_RESTORING, "/etc/lilo.conf");
1312 backup_crucial_file(MNT_RESTORING, "/etc/elilo.conf");
1313 backup_crucial_file(MNT_RESTORING, "/boot/grub/device.map");
1314 backup_crucial_file(MNT_RESTORING, "/boot/grub2/device.map");
1315 backup_crucial_file(MNT_RESTORING, "/etc/mtab");
1316 read_cfg_var(g_mondo_cfg_file, "bootloader.device", device);
1317 read_cfg_var(g_mondo_cfg_file, "bootloader.name", name);
1318 mr_asprintf(&tmp, "run_boot_loader: device='%s', name='%s'", device, name);
1319 log_msg(2, tmp);
1320 paranoid_free(tmp);
1321 paranoid_system("sync");
1322
1323 offer_to_make_initrd();
1324 if (!strcmp(name, "LILO")) {
1325 res = run_lilo(offer_to_hack_scripts);
1326 } else if (!strcmp(name, "ELILO")) {
1327 res = run_elilo(offer_to_hack_scripts);
1328 } else if (!strcmp(name, "GRUB")) {
1329 res = run_grub(offer_to_hack_scripts, device);
1330 } else if (!strcmp(name, "RAW")) {
1331 res = run_raw_mbr(offer_to_hack_scripts, device);
1332 }
1333#ifdef __FreeBSD__
1334 else if (!strcmp(name, "BOOT0")) {
1335 mr_asprintf(&tmp, "boot0cfg -B %s", device);
1336 res = run_program_and_log_output(tmp, FALSE);
1337 paranoid_free(tmp);
1338 } else {
1339 mr_asprintf(&tmp, "ls /dev | grep -Eq '^%ss[1-4].*'", device);
1340 if (!system(tmp)) {
1341 paranoid_free(tmp);
1342 mr_asprintf(&tmp, MNT_RESTORING "/sbin/fdisk -B %s", device);
1343 res = run_program_and_log_output(tmp, 3);
1344 } else {
1345 log_msg(1,
1346 "I'm not running any boot loader. You have a DD boot drive. It's already loaded up.");
1347 }
1348 paranoid_free(tmp);
1349 }
1350#else
1351 else {
1352 log_to_screen
1353 ("Unable to determine type of boot loader. Defaulting to LILO.");
1354 res = run_lilo(offer_to_hack_scripts);
1355 }
1356#endif
1357 retval += res;
1358 if (res) {
1359 log_to_screen("Your boot loader returned an error");
1360 } else {
1361 log_to_screen("Your boot loader ran OK");
1362 }
1363 paranoid_free(device);
1364 paranoid_free(name);
1365 return (retval);
1366}
1367
1368/**************************************************************************
1369 *END_ RUN_BOOT_LOADER *
1370 **************************************************************************/
1371
1372
1373
1374/**
1375 * Attempt to find the user's editor.
1376 * @return The editor found ("vi" if none could be found).
1377 * @note The returned string points to static storage that will be overwritten with each call.
1378 */
1379char *find_my_editor(void)
1380{
1381 static char output[MAX_STR_LEN];
1382 if (find_home_of_exe("pico")) {
1383 strcpy(output, "pico");
1384 } else if (find_home_of_exe("nano")) {
1385 strcpy(output, "nano");
1386 } else if (find_home_of_exe("e3em")) {
1387 strcpy(output, "e3em");
1388 } else if (find_home_of_exe("e3vi")) {
1389 strcpy(output, "e3vi");
1390 } else {
1391 strcpy(output, "vi");
1392 }
1393 if (!find_home_of_exe(output)) {
1394 log_msg(2, " (find_my_editor) --- warning - %s not found", output);
1395 }
1396 return (output);
1397}
1398
1399
1400/**
1401 * Install GRUB on @p bd.
1402 * @param offer_to_run_stabgrub If TRUE, then offer to hack the user's fstab for them.
1403 * @param bd The boot device where GRUB is installed.
1404 * @return 0 for success, nonzero for failure.
1405 */
1406int run_grub(bool offer_to_run_stabgrub, char *bd)
1407{
1408 /** malloc **/
1409 char *command;
1410 char *boot_device;
1411 char *rootdev;
1412 char *rootdrive;
1413 char *conffile;
1414 char *tmp;
1415 char *editor;
1416
1417 int res = 0; /* FALSE */
1418 int done;
1419 bool mntlistchg = FALSE;
1420 FILE *fin = NULL;
1421
1422 malloc_string(command);
1423 malloc_string(boot_device);
1424 malloc_string(tmp);
1425 malloc_string(editor);
1426 malloc_string(rootdev);
1427 malloc_string(rootdrive);
1428 malloc_string(conffile);
1429 assert_string_is_neither_NULL_nor_zerolength(bd);
1430 strcpy(editor, find_my_editor());
1431 strcpy(boot_device, bd);
1432
1433 if (!run_program_and_log_output("which grub-MR", FALSE)) {
1434 log_msg(1, "Yay! grub-MR found...");
1435 sprintf(command, "grub-MR %s /tmp/mountlist.txt", boot_device);
1436 log_msg(1, "command = %s", command);
1437 } else {
1438 sprintf(command, "chroot " MNT_RESTORING " grub-install %s",
1439 boot_device);
1440 log_msg(1, "WARNING - grub-MR not found; using grub-install");
1441 }
1442 if (offer_to_run_stabgrub
1443 && ask_me_yes_or_no("Did you change the mountlist or cloned the system ?"))
1444 /* interactive mode */
1445 {
1446 mvaddstr_and_log_it(g_currentY,
1447 0,
1448 "Modifying fstab, mtab, device.map and menu.lst/grub.cfg, and running GRUB... ");
1449 /* Did we changed the mountlist ? If yes, then force editing conf files */
1450 if ((fin = fopen(MONDO_MNTLISTCHG, "r")) != NULL) {
1451 mntlistchg = TRUE;
1452 }
1453 for (done = FALSE; !done;) {
1454 popup_and_get_string("Boot device",
1455 "Please confirm/enter the boot device. If in doubt, try /dev/hda",
1456 boot_device, MAX_STR_LEN / 4);
1457 /* Only try to adapt grub here first if the mountlist wasn't changed before */
1458 if (! mntlistchg) {
1459 sprintf(command, "stabgrub-me %s", boot_device);
1460 res = run_program_and_log_output(command, 1);
1461 }
1462 if ((res) || (mntlistchg)){
1463 if (res) {
1464 popup_and_OK("GRUB installation failed. You will now edit fstab, mtab, device.map and menu.lst/grub.cfg in order to fix grub install");
1465 } else {
1466 popup_and_OK("The mountlist was changed. You will now edit fstab, mtab, device.map and menu.lst/grub.cfg in order to fix grub install");
1467 }
1468 if (!g_text_mode) {
1469 newtSuspend();
1470 }
1471 sprintf(tmp, "chroot %s %s /etc/fstab", MNT_RESTORING, editor);
1472 paranoid_system(tmp);
1473 sprintf(tmp, "chroot %s %s /etc/mtab", MNT_RESTORING, editor);
1474 paranoid_system(tmp);
1475 if (does_file_exist(MNT_RESTORING"/boot/grub/menu.lst")) {
1476 sprintf(tmp, "chroot %s %s /boot/grub/menu.lst", MNT_RESTORING, editor);
1477 } else if (does_file_exist(MNT_RESTORING"/boot/grub/grub.cfg")) {
1478 sprintf(tmp, "chroot %s %s /boot/grub/grub.cfg", MNT_RESTORING, editor);
1479 } else if (does_file_exist(MNT_RESTORING"/boot/grub2/grub.cfg")) {
1480 sprintf(tmp, "chroot %s %s /boot/grub2/grub.cfg", MNT_RESTORING, editor);
1481 }
1482 paranoid_system(tmp);
1483 if (does_file_exist(MNT_RESTORING"/boot/grub/device.map")) {
1484 sprintf(tmp, "chroot %s %s /boot/grub/device.map", MNT_RESTORING, editor);
1485 } else if (does_file_exist(MNT_RESTORING"/boot/grub2/device.map")) {
1486 sprintf(tmp, "chroot %s %s /boot/grub2/device.map", MNT_RESTORING, editor);
1487 }
1488 paranoid_system(tmp);
1489 if (!g_text_mode) {
1490 newtResume();
1491 }
1492 sprintf(command, "stabgrub-me %s", boot_device);
1493 res = run_program_and_log_output(command, 1);
1494 if (res) {
1495 popup_and_OK
1496 ("GRUB installation failed. Please fix the conf files so that a manual install using 'grub-install' or similar command works. You are now chroot()'ed to your restored system. Please type 'exit' when you are done.");
1497 newtSuspend();
1498 paranoid_system("chroot " MNT_RESTORING);
1499 newtResume();
1500 popup_and_OK("Thank you.");
1501 } else {
1502 popup_and_OK("GRUB is now installed correctly");
1503 done = TRUE;
1504 }
1505 } else {
1506 done = TRUE;
1507 }
1508 }
1509 } else
1510 /* nuke mode */
1511 {
1512 mvaddstr_and_log_it(g_currentY,
1513 0,
1514 "Running GRUB... ");
1515 log_it("%s",command);
1516 res = run_program_and_log_output(command, 1);
1517 if (res) {
1518 popup_and_OK
1519 ("Because of bugs in GRUB's own installer, GRUB was not installed properly. Please install the boot loader manually now, using this chroot()'ed shell prompt. Type 'exit' when you have finished.");
1520 newtSuspend();
1521 paranoid_system("chroot " MNT_RESTORING);
1522 newtResume();
1523 popup_and_OK("Thank you.");
1524 }
1525 }
1526 if (res) {
1527 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
1528 log_to_screen
1529 ("GRUB ran w/error(s). See %s for more info.", MONDO_LOGFILE);
1530 log_msg(1, "Type:-");
1531 log_msg(1, " mount-me");
1532 log_msg(1, " chroot " MNT_RESTORING);
1533 log_msg(1, " mount /boot");
1534 log_msg(1, " grub-install '(hd0)'");
1535 log_msg(1, " exit");
1536 log_msg(1, " unmount-me");
1537 log_msg(1,
1538 "If you're really stuck, please e-mail the mailing list.");
1539 } else {
1540 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1541 }
1542 paranoid_free(rootdev);
1543 paranoid_free(rootdrive);
1544 paranoid_free(conffile);
1545 paranoid_free(command);
1546 paranoid_free(boot_device);
1547 paranoid_free(tmp);
1548 paranoid_free(editor);
1549
1550 return (res);
1551}
1552
1553/**************************************************************************
1554 *END_RUN_GRUB *
1555 **************************************************************************/
1556
1557
1558/**
1559 * Install ELILO on the user's boot drive (determined by elilo.conf).
1560 * @param offer_to_run_stabelilo If TRUE, then offer to hack the user's fstab for them.
1561 * @return 0 for success, nonzero for failure.
1562 */
1563int run_elilo(bool offer_to_run_stabelilo)
1564{
1565 /** malloc **/
1566 char *command;
1567 char *tmp;
1568 char *editor;
1569
1570 int res;
1571 int done;
1572
1573 malloc_string(command);
1574 malloc_string(tmp);
1575 malloc_string(editor);
1576 strcpy(editor, find_my_editor());
1577 if (offer_to_run_stabelilo
1578 && ask_me_yes_or_no("Did you change the mountlist or cloned the system ?"))
1579
1580 /* interactive mode */
1581 {
1582 mvaddstr_and_log_it(g_currentY,
1583 0,
1584 "Modifying fstab and elilo.conf... ");
1585 sprintf(command, "stabelilo-me");
1586 res = run_program_and_log_output(command, 3);
1587 if (res) {
1588 popup_and_OK
1589 ("You will now edit fstab and elilo.conf, to make sure they match your new mountlist.");
1590 for (done = FALSE; !done;) {
1591 if (!g_text_mode) {
1592 newtSuspend();
1593 }
1594 sprintf(tmp, "chroot %s %s /etc/fstab", MNT_RESTORING, editor);
1595 paranoid_system(tmp);
1596 sprintf(tmp, "chroot %s %s /etc/elilo.conf", MNT_RESTORING, editor);
1597 paranoid_system(tmp);
1598 if (!g_text_mode) {
1599 newtResume();
1600 }
1601// newtCls();
1602 if (ask_me_yes_or_no("Edit them again?")) {
1603 continue;
1604 }
1605 done = TRUE;
1606 }
1607 } else {
1608 log_to_screen("elilo.conf and fstab were modified OK");
1609 }
1610 } else
1611 /* nuke mode */
1612 {
1613 res = TRUE;
1614 }
1615 paranoid_free(command);
1616 paranoid_free(tmp);
1617 paranoid_free(editor);
1618 return (res);
1619}
1620
1621/**************************************************************************
1622 *END_RUN_ELILO *
1623 **************************************************************************/
1624
1625
1626/**
1627 * Install LILO on the user's boot drive (determined by /etc/lilo.conf).
1628 * @param offer_to_run_stablilo If TRUE, then offer to hack the user's fstab for them.
1629 * @return 0 for success, nonzero for failure.
1630 */
1631int run_lilo(bool offer_to_run_stablilo)
1632{
1633 /** malloc **/
1634 char *command;
1635 char *tmp;
1636 char *editor;
1637
1638 int res;
1639 int done;
1640 bool run_lilo_M = FALSE;
1641 malloc_string(command);
1642 malloc_string(tmp);
1643 malloc_string(editor);
1644
1645 if (!run_program_and_log_output
1646 ("grep \"boot.*=.*/dev/md\" " MNT_RESTORING "/etc/lilo.conf", 1)) {
1647 run_lilo_M = TRUE;
1648 }
1649
1650 strcpy(editor, find_my_editor());
1651 if (offer_to_run_stablilo
1652 && ask_me_yes_or_no("Did you change the mountlist or cloned the system ?"))
1653
1654 /* interactive mode */
1655 {
1656 mvaddstr_and_log_it(g_currentY,
1657 0,
1658 "Modifying fstab and lilo.conf, and running LILO... ");
1659 sprintf(command, "stablilo-me");
1660 res = run_program_and_log_output(command, 3);
1661 if (res) {
1662 popup_and_OK
1663 ("You will now edit fstab and lilo.conf, to make sure they match your new mountlist.");
1664 for (done = FALSE; !done;) {
1665 if (!g_text_mode) {
1666 newtSuspend();
1667 }
1668 sprintf(tmp, "%s " MNT_RESTORING "/etc/fstab", editor);
1669 paranoid_system(tmp);
1670 sprintf(tmp, "%s " MNT_RESTORING "/etc/lilo.conf", editor);
1671 paranoid_system(tmp);
1672 if (!g_text_mode) {
1673 newtResume();
1674 }
1675// newtCls();
1676 if (ask_me_yes_or_no("Edit them again?")) {
1677 continue;
1678 }
1679 res =
1680 run_program_and_log_output("chroot " MNT_RESTORING
1681 " lilo -L", 3);
1682 if (res) {
1683 res =
1684 run_program_and_log_output("chroot " MNT_RESTORING
1685 " lilo", 3);
1686 }
1687 if (res) {
1688 done =
1689 ask_me_yes_or_no
1690 ("LILO failed. Re-edit system files?");
1691 } else {
1692 done = TRUE;
1693 }
1694 }
1695 } else {
1696 log_to_screen("lilo.conf and fstab were modified OK");
1697 }
1698 } else
1699 /* nuke mode */
1700 {
1701 mvaddstr_and_log_it(g_currentY,
1702 0,
1703 "Running LILO... ");
1704 res =
1705 run_program_and_log_output("chroot " MNT_RESTORING " lilo -L",
1706 3);
1707 if (res) {
1708 res =
1709 run_program_and_log_output("chroot " MNT_RESTORING " lilo",
1710 3);
1711 }
1712 if (res) {
1713 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
1714 log_to_screen
1715 ("Failed to re-jig fstab and/or lilo. Edit/run manually, please.");
1716 } else {
1717 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1718 }
1719 }
1720 if (run_lilo_M) {
1721 run_program_and_log_output("chroot " MNT_RESTORING
1722 " lilo -M /dev/hda", 3);
1723 run_program_and_log_output("chroot " MNT_RESTORING
1724 " lilo -M /dev/sda", 3);
1725 }
1726 paranoid_free(command);
1727 paranoid_free(tmp);
1728 paranoid_free(editor);
1729 return (res);
1730}
1731
1732/**************************************************************************
1733 *END_RUN_LILO *
1734 **************************************************************************/
1735
1736
1737/**
1738 * Install a raw MBR onto @p bd.
1739 * @param offer_to_hack_scripts If TRUE, then offer to hack the user's fstab for them.
1740 * @param bd The device to copy the stored MBR to.
1741 * @return 0 for success, nonzero for failure.
1742 */
1743int run_raw_mbr(bool offer_to_hack_scripts, char *bd)
1744{
1745 /** malloc **/
1746 char *command;
1747 char *boot_device;
1748 char *tmp;
1749 char *editor;
1750 int res;
1751 int done;
1752
1753 malloc_string(command);
1754 malloc_string(boot_device);
1755 malloc_string(tmp);
1756 malloc_string(editor);
1757 assert_string_is_neither_NULL_nor_zerolength(bd);
1758
1759 strcpy(editor, find_my_editor());
1760 strcpy(boot_device, bd);
1761 sprintf(command, "raw-MR %s /tmp/mountlist.txt", boot_device);
1762 log_msg(2, "run_raw_mbr() --- command='%s'", command);
1763
1764 if (offer_to_hack_scripts
1765 && ask_me_yes_or_no("Did you change the mountlist or cloned the system ?"))
1766 /* interactive mode */
1767 {
1768 mvaddstr_and_log_it(g_currentY, 0,
1769 "Modifying fstab and restoring MBR... ");
1770 for (done = FALSE; !done;) {
1771 if (!run_program_and_log_output("which vi", FALSE)) {
1772 popup_and_OK("You will now edit fstab");
1773 if (!g_text_mode) {
1774 newtSuspend();
1775 }
1776 sprintf(tmp, "%s " MNT_RESTORING "/etc/fstab", editor);
1777 paranoid_system(tmp);
1778 if (!g_text_mode) {
1779 newtResume();
1780 }
1781// newtCls();
1782 }
1783 popup_and_get_string("Boot device",
1784 "Please confirm/enter the boot device. If in doubt, try /dev/hda",
1785 boot_device, MAX_STR_LEN / 4);
1786 sprintf(command, "stabraw-me %s", boot_device);
1787 res = run_program_and_log_output(command, 3);
1788 if (res) {
1789 done = ask_me_yes_or_no("Modifications failed. Re-try?");
1790 } else {
1791 done = TRUE;
1792 }
1793 }
1794 } else
1795 /* nuke mode */
1796 {
1797 mvaddstr_and_log_it(g_currentY, 0,
1798 "Restoring MBR... ");
1799 res = run_program_and_log_output(command, 3);
1800 }
1801 if (res) {
1802 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
1803 log_to_screen
1804 ("MBR+fstab processed w/error(s). See %s for more info.", MONDO_LOGFILE);
1805 } else {
1806 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1807 }
1808 paranoid_free(command);
1809 paranoid_free(boot_device);
1810 paranoid_free(tmp);
1811 paranoid_free(editor);
1812 return (res);
1813}
1814
1815/**************************************************************************
1816 *END_RUN_RAW_MBR *
1817 **************************************************************************/
1818
1819
1820
1821/**
1822 * malloc() and set sensible defaults for the mondorestore filename variables.
1823 * @param bkpinfo The backup information structure. Fields used:
1824 * - @c bkpinfo->tmpdir
1825 * - @c bkpinfo->disaster_recovery
1826 */
1827void setup_MR_global_filenames()
1828{
1829 char *temppath;
1830
1831 assert(bkpinfo != NULL);
1832
1833 malloc_string(g_biggielist_txt);
1834 malloc_string(g_filelist_full);
1835 malloc_string(g_filelist_imagedevs);
1836 malloc_string(g_imagedevs_restthese);
1837 malloc_string(g_mondo_cfg_file);
1838 malloc_string(g_mountlist_fname);
1839 malloc_string(g_mondo_home);
1840 /*
1841 malloc_string(g_tmpfs_mountpt);
1842 */
1843 malloc_string(g_isodir_device);
1844 malloc_string(g_isodir_format);
1845
1846 temppath = bkpinfo->tmpdir;
1847
1848 sprintf(g_biggielist_txt, "%s/%s", temppath, BIGGIELIST_TXT_STUB);
1849 sprintf(g_filelist_full, "%s/%s", temppath, FILELIST_FULL_STUB);
1850 sprintf(g_filelist_imagedevs, "%s/tmp/filelist.imagedevs", temppath);
1851// sprintf(g_imagedevs_pot, "%s/tmp/imagedevs.pot", temppath);
1852 sprintf(g_imagedevs_restthese, "%s/tmp/imagedevs.restore-these",
1853 temppath);
1854 if (bkpinfo->disaster_recovery) {
1855 sprintf(g_mondo_cfg_file, "/%s", MONDO_CFG_FILE_STUB);
1856 sprintf(g_mountlist_fname, "/%s", MOUNTLIST_FNAME_STUB);
1857 } else {
1858 sprintf(g_mondo_cfg_file, "%s/%s", temppath, MONDO_CFG_FILE_STUB);
1859 sprintf(g_mountlist_fname, "%s/%s", temppath,
1860 MOUNTLIST_FNAME_STUB);
1861 }
1862}
1863
1864/**************************************************************************
1865 *END_SET_GLOBAL_FILENAME *
1866 **************************************************************************/
1867
1868
1869/**
1870 * Copy @p input_file (containing the result of a compare) to @p output_file,
1871 * deleting spurious "changes" along the way.
1872 * @param output_file The output file to write with spurious changes removed.
1873 * @param input_file The input file, a list of changed files created by a compare.
1874 */
1875void streamline_changes_file(char *output_file, char *input_file)
1876{
1877 FILE *fin;
1878 FILE *fout;
1879 /** malloc **/
1880 char *incoming;
1881 char *q;
1882
1883 assert_string_is_neither_NULL_nor_zerolength(output_file);
1884 assert_string_is_neither_NULL_nor_zerolength(input_file);
1885 malloc_string(incoming);
1886
1887 if (!(fin = fopen(input_file, "r"))) {
1888 log_OS_error(input_file);
1889 return;
1890 }
1891 if (!(fout = fopen(output_file, "w"))) {
1892 fatal_error("cannot open output_file");
1893 }
1894 for (q = fgets(incoming, MAX_STR_LEN - 1, fin); !feof(fin) && (q != NULL);
1895 q = fgets(incoming, MAX_STR_LEN - 1, fin)) {
1896 if (strncmp(incoming, "etc/adjtime", 11)
1897 && strncmp(incoming, "etc/mtab", 8)
1898 && strncmp(incoming, "tmp/", 4)
1899 && strncmp(incoming, "boot/map", 8)
1900 && !strstr(incoming, "incheckentry")
1901 && strncmp(incoming, "etc/mail/statistics", 19)
1902 && strncmp(incoming, "var/", 4))
1903 fprintf(fout, "%s", incoming); /* don't need \n here, for some reason.. */
1904 }
1905 paranoid_fclose(fout);
1906 paranoid_fclose(fin);
1907 paranoid_free(incoming);
1908}
1909
1910/**************************************************************************
1911 *END_STREAMLINE_CHANGES_FILE *
1912 **************************************************************************/
1913
1914
1915/**
1916 * Give the user twenty seconds to press Ctrl-Alt-Del before we nuke their drives.
1917 */
1918void twenty_seconds_til_yikes()
1919{
1920 int i;
1921 /* MALLOC * */
1922 char *tmp;
1923
1924 malloc_string(tmp);
1925 if (does_file_exist("/tmp/NOPAUSE")) {
1926 return;
1927 }
1928 open_progress_form("CAUTION",
1929 "Be advised: I am about to ERASE your hard disk(s)!",
1930 "You may press Ctrl+Alt+Del to abort safely.",
1931 "", 20);
1932 for (i = 0; i < 20; i++) {
1933 g_current_progress = i;
1934 sprintf(tmp, "You have %d seconds left to abort.", 20 - i);
1935 update_progress_form(tmp);
1936 sleep(1);
1937 }
1938 close_progress_form();
1939 paranoid_free(tmp);
1940}
1941
1942/**************************************************************************
1943 *END_TWENTY_SECONDS_TIL_YIKES *
1944 **************************************************************************/
1945
1946
1947/**
1948 * Unmount all devices in @p p_external_copy_of_mountlist.
1949 * @param p_external_copy_of_mountlist The mountlist to guide the devices to unmount.
1950 * @return 0 for success, nonzero for failure.
1951 */
1952int unmount_all_devices(struct mountlist_itself
1953 *p_external_copy_of_mountlist)
1954{
1955 struct mountlist_itself *mountlist;
1956 int retval = 0, lino, res = 0, i;
1957 char *command;
1958 char *tmp = NULL;
1959
1960 malloc_string(command);
1961 assert(p_external_copy_of_mountlist != NULL);
1962
1963 mountlist = malloc(sizeof(struct mountlist_itself));
1964 memcpy((void *) mountlist, (void *) p_external_copy_of_mountlist,
1965 sizeof(struct mountlist_itself));
1966 sort_mountlist_by_mountpoint(mountlist, 0);
1967
1968 run_program_and_log_output("df -m", 3);
1969 mvaddstr_and_log_it(g_currentY, 0, "Unmounting devices ");
1970 open_progress_form("Unmounting devices",
1971 "Unmounting all devices that were mounted,",
1972 "in preparation for the post-restoration reboot.",
1973 "", mountlist->entries);
1974 if (chdir("/")) {
1975 // FIXME
1976 }
1977 for (i = 0;
1978 i < 10
1979 &&
1980 run_program_and_log_output
1981 ("ps | grep buffer | grep -v \"grep buffer\"", TRUE) == 0;
1982 i++) {
1983 sleep(1);
1984 log_msg(2, "Waiting for buffer() to finish");
1985 }
1986
1987 paranoid_system("sync");
1988
1989 mr_asprintf(&tmp, "cp -f %s " MNT_RESTORING "/var/log", MONDO_LOGFILE);
1990 if (run_program_and_log_output(tmp, FALSE)) {
1991 log_msg(1,
1992 "Error. Failed to copy log to PC's /var/log dir. (Mounted read-only?)");
1993 }
1994 paranoid_free(tmp);
1995 if (does_file_exist("/tmp/DUMBASS-GENTOO")) {
1996 run_program_and_log_output("mkdir -p " MNT_RESTORING
1997 "/mnt/.boot.d", 5);
1998 }
1999
2000 /* Unmounting the local /proc and /sys first */
2001 run_program_and_log_output("umount " MNT_RESTORING "/proc",3);
2002 run_program_and_log_output("umount " MNT_RESTORING "/sys",3);
2003
2004 for (lino = mountlist->entries - 1; lino >= 0; lino--) {
2005 if (!strcmp(mountlist->el[lino].mountpoint, "lvm")) {
2006 continue;
2007 }
2008 mr_asprintf(&tmp, "Unmounting device %s ", mountlist->el[lino].device);
2009
2010 update_progress_form(tmp);
2011
2012 if (is_this_device_mounted(mountlist->el[lino].device)) {
2013 if (!strcmp(mountlist->el[lino].mountpoint, "swap")) {
2014 sprintf(command, "swapoff %s", mountlist->el[lino].device);
2015 } else {
2016 if (!strcmp(mountlist->el[lino].mountpoint, "/1")) {
2017 sprintf(command, "umount %s/", MNT_RESTORING);
2018 log_msg(3,
2019 "Well, I know a certain kitty-kitty who'll be sleeping with Mommy tonight...");
2020 } else {
2021 sprintf(command, "umount " MNT_RESTORING "%s",
2022 mountlist->el[lino].mountpoint);
2023
2024 /* To support latest Ubuntu where /var is a separate FS
2025 * Cf: http://linux.derkeiler.com/Mailing-Lists/Ubuntu/2007-04/msg01319.html
2026 * we need to create some dirs under the real / before unmounting it */
2027 if (!strcmp(mountlist->el[lino].mountpoint, "/")) {
2028 run_program_and_log_output("mkdir -p " MNT_RESTORING "/var/lock", FALSE);
2029 run_program_and_log_output("mkdir -p " MNT_RESTORING "/var/run", FALSE);
2030 }
2031 }
2032 }
2033 log_msg(10, "The 'umount' command is '%s'", command);
2034 res = run_program_and_log_output(command, 3);
2035 } else {
2036 mr_strcat(tmp, "...not mounted anyway :-) OK");
2037 res = 0;
2038 }
2039 g_current_progress++;
2040 if (res) {
2041 mr_strcat(tmp, "...Failed");
2042 retval++;
2043 log_to_screen(tmp);
2044 } else {
2045 log_msg(2, tmp);
2046 }
2047 paranoid_free(tmp);
2048 }
2049 close_progress_form();
2050 if (retval) {
2051 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
2052 } else {
2053 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
2054 }
2055 if (retval) {
2056 log_to_screen("Unable to unmount some of your partitions.");
2057 } else {
2058 log_to_screen("All partitions were unmounted OK.");
2059 }
2060 free(mountlist);
2061 paranoid_free(command);
2062 return (retval);
2063}
2064
2065/**************************************************************************
2066 *END_UNMOUNT_ALL_DEVICES *
2067 **************************************************************************/
2068
2069
2070
2071/**
2072 * Extract mondo-restore.cfg and the mountlist from the tape inserted
2073 * to the ./tmp/ directory.
2074 * @param dev The tape device to read from.
2075 * @return 0 for success, nonzero for failure.
2076 */
2077int extract_cfg_file_and_mountlist_from_tape_dev(char *dev)
2078{
2079 char *command;
2080 int res = 0;
2081
2082 malloc_string(command);
2083
2084 if (bkpinfo->use_obdr) {
2085 skip_obdr();
2086 } else {
2087 // BCO: below 32KB seems to block at least on RHAS 2.1 and MDK 10.0
2088 set_tape_block_size_with_mt(bkpinfo->internal_tape_block_size);
2089 }
2090
2091 sprintf(command,
2092 "dd if=%s bs=%ld count=%ld 2> /dev/null | tar -zx ./%s ./%s ./%s ./%s ./%s",
2093 dev,
2094 bkpinfo->internal_tape_block_size,
2095 1024L * 1024 * 32 / bkpinfo->internal_tape_block_size,
2096 MOUNTLIST_FNAME_STUB, MONDO_CFG_FILE_STUB,
2097 BIGGIELIST_TXT_STUB, FILELIST_FULL_STUB, IWANTMYLVM_STUB);
2098 log_msg(2, "command = '%s'", command);
2099 res = run_program_and_log_output(command, -1);
2100 if (res != 0) {
2101 if (does_file_exist(MONDO_CFG_FILE_STUB)) {
2102 res = 0;
2103 } else {
2104 /* Doing that allow us to remain compatible with pre-2.2.5 versions */
2105 log_msg(2, "pre-2.2.4 compatible mode on");
2106 sprintf(command,
2107 "dd if=%s bs=%ld count=%ld 2> /dev/null | tar -zx %s %s %s %s %s",
2108 dev,
2109 bkpinfo->internal_tape_block_size,
2110 1024L * 1024 * 32 / bkpinfo->internal_tape_block_size,
2111 MOUNTLIST_FNAME_STUB, MONDO_CFG_FILE_STUB,
2112 BIGGIELIST_TXT_STUB, FILELIST_FULL_STUB, IWANTMYLVM_STUB);
2113 log_msg(2, "command = '%s'", command);
2114 res = run_program_and_log_output(command, -1);
2115 if ((res != 0) && (does_file_exist(MONDO_CFG_FILE_STUB))) {
2116 res = 0;
2117 }
2118 }
2119 }
2120 paranoid_free(command);
2121 return (res);
2122}
2123
2124
2125
2126/**
2127 * Get the configuration file from the floppy, tape, or CD.
2128 * @param bkpinfo The backup information structure. Fields used:
2129 * - @c bkpinfo->backup_media_type
2130 * - @c bkpinfo->media_device
2131 * - @c bkpinfo->tmpdir
2132 * @return 0 for success, nonzero for failure.
2133 */
2134int get_cfg_file_from_archive()
2135{
2136 int retval = 0;
2137
2138 /** malloc *****/
2139 char *device;
2140 char *command;
2141 char *cfg_file;
2142 char *mounted_cfgf_path;
2143 char *tmp;
2144 char *mountpt;
2145 char *ramdisk_fname;
2146 char *mountlist_file;
2147 bool extract_mountlist_stub = FALSE;
2148 bool extract_i_want_my_lvm = FALSE;
2149
2150 bool try_plan_B;
2151
2152 assert(bkpinfo != NULL);
2153 malloc_string(cfg_file);
2154 malloc_string(mounted_cfgf_path);
2155 malloc_string(mountpt);
2156 malloc_string(ramdisk_fname);
2157 malloc_string(mountlist_file);
2158 malloc_string(device);
2159 malloc_string(command);
2160 malloc_string(tmp);
2161 log_msg(2, "gcffa --- starting");
2162 log_to_screen("I'm thinking...");
2163 sprintf(mountpt, "%s/mount.bootdisk", bkpinfo->tmpdir);
2164 device[0] = '\0';
2165 if (chdir(bkpinfo->tmpdir)) {
2166 // FIXME
2167 }
2168 strcpy(cfg_file, MONDO_CFG_FILE_STUB);
2169 unlink(cfg_file); // cfg_file[] is missing the '/' at the start, FYI, by intent
2170 unlink(FILELIST_FULL_STUB);
2171 unlink(BIGGIELIST_TXT_STUB);
2172 sprintf(command, "mkdir -p %s", mountpt);
2173 run_program_and_log_output(command, FALSE);
2174
2175 sprintf(cfg_file, "%s/%s", bkpinfo->tmpdir, MONDO_CFG_FILE_STUB);
2176 sprintf(mountlist_file, "%s/%s", bkpinfo->tmpdir, MOUNTLIST_FNAME_STUB);
2177 // make_hole_for_file( cfg_file );
2178 // make_hole_for_file( mountlist_file);
2179 log_msg(2, "mountpt = %s; cfg_file=%s", mountpt, cfg_file);
2180
2181 if (!does_file_exist(cfg_file)) {
2182 log_msg(2, "gcffa --- we don't have cfg file yet.");
2183 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
2184 try_plan_B = TRUE;
2185 } else {
2186 log_msg(2, "gcffa --- calling mount_media now :)");
2187 if (!mount_media()) {
2188 log_msg(2,
2189 "gcffa --- managed to mount CD; so, no need for Plan B");
2190 try_plan_B = FALSE;
2191 } else {
2192 try_plan_B = TRUE;
2193 }
2194 if (what_number_cd_is_this() > 1) {
2195 insist_on_this_cd_number((g_current_media_number = 1));
2196 }
2197 }
2198 if (try_plan_B) {
2199 log_msg(2, "gcffa --- OK, switching to Plan B");
2200 if (chdir(bkpinfo->tmpdir)) {
2201 // FIXME
2202 }
2203 run_program_and_log_output("mkdir -p tmp", FALSE);
2204
2205 if (strlen(bkpinfo->media_device) == 0) {
2206 strcpy(bkpinfo->media_device, "/dev/st0");
2207 log_msg(2, "media_device is blank; assuming %s");
2208 }
2209 strcpy(tmp, bkpinfo->media_device);
2210 if (extract_cfg_file_and_mountlist_from_tape_dev
2211 (bkpinfo->media_device)) {
2212 strcpy(bkpinfo->media_device, "/dev/st0");
2213 if (extract_cfg_file_and_mountlist_from_tape_dev
2214 (bkpinfo->media_device)) {
2215 strcpy(bkpinfo->media_device, "/dev/osst0");
2216 if (extract_cfg_file_and_mountlist_from_tape_dev
2217 (bkpinfo->media_device)) {
2218 strcpy(bkpinfo->media_device, "/dev/ht0");
2219 if (extract_cfg_file_and_mountlist_from_tape_dev
2220 (bkpinfo->media_device)) {
2221 log_msg(3,
2222 "I tried lots of devices but none worked.");
2223 strcpy(bkpinfo->media_device, tmp);
2224 }
2225 }
2226 }
2227 }
2228
2229 if (!does_file_exist("tmp/mondo-restore.cfg")) {
2230 log_to_screen("Cannot find config info on media");
2231 return (1);
2232 }
2233 } else {
2234 if (does_file_exist("/"MOUNTLIST_FNAME_STUB)) {
2235 extract_mountlist_stub = FALSE;
2236 } else {
2237 extract_mountlist_stub = TRUE;
2238 }
2239 if (does_file_exist("/"IWANTMYLVM_STUB)) {
2240 extract_i_want_my_lvm = FALSE;
2241 } else {
2242 extract_i_want_my_lvm = TRUE;
2243 }
2244
2245 log_msg(2,
2246 "gcffa --- Plan B, a.k.a. untarring some file from all.tar.gz");
2247 sprintf(command, "tar -zxvf " MNT_CDROM "/images/all.tar.gz ./%s ./%s ./%s ./%s ./%s", MOUNTLIST_FNAME_STUB, MONDO_CFG_FILE_STUB, BIGGIELIST_TXT_STUB, FILELIST_FULL_STUB, IWANTMYLVM_STUB); // add -b TAPE_BLOCK_SIZE if you _really_ think it's necessary
2248 run_program_and_log_output(command, TRUE);
2249 if (!does_file_exist(MONDO_CFG_FILE_STUB)) {
2250 /* Doing that allow us to remain compatible with pre-2.2.5 versions */
2251 log_msg(2, "pre-2.2.4 compatible mode on");
2252 sprintf(command, "tar -zxvf " MNT_CDROM "/images/all.tar.gz %s %s %s %s %s", MOUNTLIST_FNAME_STUB, MONDO_CFG_FILE_STUB, BIGGIELIST_TXT_STUB, FILELIST_FULL_STUB, IWANTMYLVM_STUB); // add -b TAPE_BLOCK_SIZE if you _really_ think it's necessary
2253 run_program_and_log_output(command, TRUE);
2254 if (!does_file_exist(MONDO_CFG_FILE_STUB)) {
2255 fatal_error
2256 ("Please reinsert the disk/CD and try again.");
2257 }
2258 }
2259 }
2260 }
2261 if (does_file_exist(MONDO_CFG_FILE_STUB)) {
2262 log_msg(1, "gcffa --- great! We've got the config file");
2263 sprintf(tmp, "%s/%s",
2264 call_program_and_get_last_line_of_output("pwd"),
2265 MONDO_CFG_FILE_STUB);
2266 sprintf(command, "cp -f %s %s", tmp, cfg_file);
2267 log_it("%s",command);
2268 if (strcmp(tmp, cfg_file)
2269 && run_program_and_log_output(command, 1)) {
2270 log_msg(1,
2271 "... but an error occurred when I tried to move it to %s",
2272 cfg_file);
2273 } else {
2274 log_msg(1, "... and I moved it successfully to %s", cfg_file);
2275 }
2276 sprintf(command, "cp -f %s/%s %s",
2277 call_program_and_get_last_line_of_output("pwd"),
2278 MOUNTLIST_FNAME_STUB, mountlist_file);
2279 log_it("%s",command);
2280 if (extract_mountlist_stub) {
2281 if (strcmp(tmp, cfg_file)
2282 && run_program_and_log_output(command, 1)) {
2283 log_msg(1, "Failed to get mountlist");
2284 } else {
2285 log_msg(1, "Got mountlist too");
2286 sprintf(command, "cp -f %s %s", mountlist_file,
2287 g_mountlist_fname);
2288 if (run_program_and_log_output(command, 1)) {
2289 log_msg(1, "Failed to copy mountlist to /tmp");
2290 } else {
2291 log_msg(1, "Copied mountlist to /tmp as well OK");
2292 sprintf(command, "cp -f %s /tmp/",IWANTMYLVM_STUB);
2293 run_program_and_log_output(command, 1);
2294 }
2295 }
2296 }
2297 }
2298 run_program_and_log_output("umount -d " MNT_CDROM, FALSE);
2299 if (!does_file_exist(cfg_file)) {
2300 log_it("%s",cfg_file);
2301 log_msg(1, "%s not found", cfg_file);
2302 log_to_screen
2303 ("Oh dear. Unable to recover configuration file from boot disk");
2304 return (1);
2305 }
2306
2307 log_to_screen("Recovered mondo-restore.cfg");
2308 if (!does_file_exist(MOUNTLIST_FNAME_STUB)) {
2309 log_to_screen("...but not mountlist.txt - a pity, really...");
2310 }
2311 else {
2312 /* Is this code really useful ??? */
2313 if (extract_mountlist_stub) {
2314 sprintf(command, "cp -f %s %s/%s", MOUNTLIST_FNAME_STUB,
2315 bkpinfo->tmpdir, MOUNTLIST_FNAME_STUB);
2316 run_program_and_log_output(command, FALSE);
2317 }
2318 }
2319
2320 sprintf(command, "cp -f %s /%s", cfg_file, MONDO_CFG_FILE_STUB);
2321 run_program_and_log_output(command, FALSE);
2322 if (extract_mountlist_stub) {
2323 sprintf(command, "cp -f %s /%s", mountlist_file, MOUNTLIST_FNAME_STUB);
2324 run_program_and_log_output(command, FALSE);
2325 }
2326 sprintf(command, "cp -f etc/raidtab /etc/");
2327 run_program_and_log_output(command, FALSE);
2328 if (extract_i_want_my_lvm) {
2329 sprintf(command, "cp -f %s /tmp/",IWANTMYLVM_STUB);
2330 run_program_and_log_output(command, FALSE);
2331 }
2332 g_backup_media_type = bkpinfo->backup_media_type;
2333 paranoid_free(device);
2334 paranoid_free(command);
2335 paranoid_free(tmp);
2336 paranoid_free(cfg_file);
2337 paranoid_free(mounted_cfgf_path);
2338 paranoid_free(mountpt);
2339 paranoid_free(ramdisk_fname);
2340 paranoid_free(mountlist_file);
2341 return (retval);
2342}
2343
2344/**************************************************************************
2345 *END_GET_CFG_FILE_FROM_ARCHIVE *
2346 **************************************************************************/
2347
2348/* @} - end restoreUtilityGroup */
2349
2350
2351/***************************************************************************
2352 * F@ *
2353 * () -- Hugo Rabson *
2354 * *
2355 * Purpose: *
2356 * *
2357 * Called by: *
2358 * Params: - - *
2359 * Returns: 0=success; nonzero=failure *
2360 ***************************************************************************/
2361
2362
2363
2364void wait_until_software_raids_are_prepped(char *mdstat_file, int wait_for_percentage)
2365{
2366 struct raidlist_itself *raidlist;
2367 int unfinished_mdstat_devices = 9999;
2368 int i = 0;
2369 char *screen_message;
2370
2371 malloc_string(screen_message);
2372 raidlist = malloc(sizeof(struct raidlist_itself));
2373
2374 assert(wait_for_percentage <= 100);
2375 log_it("wait_until_software_raids_are_prepped");
2376 while (unfinished_mdstat_devices > 0) {
2377 // FIXME: Prefix '/dev/' should really be dynamic!
2378 if (parse_mdstat(MDSTAT_FILE, raidlist, "/dev/")) {
2379 log_to_screen("Sorry, cannot read %s", MDSTAT_FILE);
2380 log_msg(1,"Sorry, cannot read %s", MDSTAT_FILE);
2381 return;
2382 }
2383 for (unfinished_mdstat_devices = 0; i <= raidlist->entries; i++) {
2384 if (raidlist->el[i].progress < wait_for_percentage) {
2385 unfinished_mdstat_devices++;
2386 if (raidlist->el[i].progress == -1) // delayed while another partition inits
2387 {
2388 continue;
2389 }
2390 log_msg(1,"Sync'ing %s (i=%d)", raidlist->el[i].raid_device, i);
2391 sprintf(screen_message, "Sync'ing %s",
2392 raidlist->el[i].raid_device);
2393 open_evalcall_form(screen_message);
2394 while (raidlist->el[i].progress < wait_for_percentage) {
2395 log_msg(1,"Percentage sync'ed: %d", raidlist->el[i].progress);
2396 update_evalcall_form(raidlist->el[i].progress);
2397 sleep(2);
2398 // FIXME: Prefix '/dev/' should really be dynamic!
2399 if (parse_mdstat(MDSTAT_FILE, raidlist, "/dev/")) {
2400 break;
2401 }
2402 }
2403 close_evalcall_form();
2404 }
2405 }
2406 }
2407 paranoid_free(screen_message);
2408 paranoid_free(raidlist);
2409}
2410
2411
Note: See TracBrowser for help on using the repository browser.