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

Last change on this file since 254 was 254, checked in by bcornec, 18 years ago

merge -r251:253 $SVN_M/branches/2.05

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