source: MondoRescue/trunk/mondo/src/mondorestore/mondo-rstr-tools.c

Last change on this file was 1994, checked in by Bruno Cornec, 16 years ago

merge content for trunk from old laptop

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