source: MondoRescue/branches/stable/mondo/src/mondorestore/mondo-rstr-tools.c@ 1548

Last change on this file since 1548 was 1548, checked in by Bruno Cornec, 17 years ago
  • Add the possibiilty to edit in interactive mode mtab and device.map for grub
  • Remove blkid cache files after restore to avoid problems in cloning mode
  • Fix what seems to appear a huge number of bugs in hack-fstab (illustration of 1 LOC = 1 bug :-)
  • Especially improve LABEL and UUID support.
  • Should fix #185
  • Exclude_path should be 4*MAX_STR_LEN everywhere. Fixed now.
  • Increasing that value will allow to having larger exclude paths.
  • Should solve bug #137 (and maybe #3 as well)
  • Adds support for fedora 7

(merge -r 1533:1547 $SVN_M/branches/2.2.5)

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