root/branches/stable/mondo/src/mondorestore/mondo-rstr-tools.c

Revision 909, 80.0 kB (checked in by bruno, 4 years ago)

Remove calls to grep -w when use in mondorestore as busybox doesn't support that option. (report from Alfred Chua)

  • Property svn:keywords set to Id
Line 
1 /***************************************************************************
2 mondo-rstr-tools.c  -  description
3 -----------------
4
5 begin: Sun Sep 21 16:40:35 EDT 2003
6 copyright : (C) 2002 Mondo  Hugo Rabson
7 email     : Hugo Rabson <hugorabson@msn.com>
8 edited by : by Stan Benoit ?/2003
9 email     : troff@nakedsoul.org
10 cvsid     : $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
24 07/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
29 07/20
30 - use backup's i-want-my-lvm file
31 - be sure to use archives' raidtab when restoring
32
33 07/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
37 07/12
38 - bugfix to allow user to burn ISOs to CDs and restore from CDs (not original src)
39
40 06/29
41 - mount ext3 partitions as ext2, just in case :)
42
43 06/26
44 - delete make_relevant_partition_bootable()
45
46 06/19
47 - futzed with the call to mount floppy, to stop it from locking up on my AMD64 system
48
49 06/14
50 - shell out to /mnt/RESTORING chroot in order to let user install GRUB
51   manually if automatic GRUB installation fails
52
53 06/15
54 - Added check for different 'isodir' chosen by user than stored in the archive
55   Conor Daly <conor.daly@met.ie>
56
57 04/17
58 - replaced INTERNAL_TAPE_BLK_SIZE with bkpinfo->internal_tape_block_size
59
60 04/09
61 - don't try to mount CD if tape bkp
62
63 04/03
64 - trying to copy tmp/mondo-restore.cfg to itself - silly! - fixed
65
66 04/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
70 02/21
71 - don't use 'mv -v' cos Busybox won't support it
72
73 02/09
74 - make hole for cfg file before moving it (line 2094 or so)
75
76 02/03
77 - changed a couple of refs to filelist.full, to filelist.full.gz
78
79 01/16/2004
80 - instead of copying filelist, use 'ln -sf' to link to original;
81   saves space
82
83 11/20/2003
84 - also retrieve /tmp/mountlist.txt if user wants
85
86 11/16
87 - fixed NFS path bug affecting the extractions of filelist/biggielist
88   during selective restore
89
90 11/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
94 10/17
95 - run_grub() uses MNT_RESTORING instead of "/mnt/RESTORING"
96
97 10/26
98 - cleaned up run_grub()
99
100 10/25
101 - fixed mount_cdrom() to run properly w/ nfs restores
102
103 10/21
104 - mount_device() returns 0 if swap mount fails cos swap isn't crucial
105
106 10/15
107 - run_grub() now uses its initiative instead
108   of calling grub-install
109
110 10/10
111 - don't leave copies of filelist.full lying around, clogging up
112   the ramdisk, there's a good fellow :-)
113
114 10/02
115 - added 'dvd' to the range of media types I'll understand
116 - fixed iso->cdr problem (thanks, Stan Benoit & Fred Beondo)
117
118 09/24
119 - try lots of tape devs if /dev/st0 fails
120
121 09/23/2003
122 - first incarnation
123 */
124
125
126 #include <pthread.h>
127 #include "../common/my-stuff.h"
128 #include "../common/mondostructures.h"
129 #include "../common/libmondo.h"
130 #include "mr-externs.h"
131 //#include "mondo-restore.h"
132 //#include "mondo-rstr-compare-EXT.h"
133 #include "mondo-rstr-tools.h"
134
135 extern bool g_sigpipe_caught;
136 extern bool g_ISO_restore_mode; /* are we in Iso Mode? */
137 extern bool g_I_have_just_nuked;
138 extern char *g_tmpfs_mountpt;
139 extern char *g_isodir_device;
140 extern char *g_isodir_format;
141 extern long g_current_progress, g_maximum_progress;
142 extern char *g_biggielist_txt;  // where 'biggielist.txt' is stored, on ramdisk / tempdir;
143                                                   // biggielist.txt is the list of big files stored on the
144                                                   // backup media set in question
145 extern char *g_filelist_full;   // filelist.full.gz is the list of all regular files
146                                                   // (excluding big files) stored on the backup media set
147 extern char *g_biggielist_pot;  // list of big files which _could_ be restored, if the
148                                                   // user chooses them
149 extern char *g_filelist_imagedevs;      // list of devices (e.g. /dev/hda1, /dev/sda5) which
150                                                          // were archived as images, not just /dev entries
151                                                          // ... e.g. NTFS, BeOS partitions
152 extern char *g_imagedevs_restthese;     // of the imagedevs listed in FILELIST_IMAGEDEVS,
153                                                           // restore only these
154 extern char *g_mondo_cfg_file;  // where m*ndo-restore.cfg (the config file) is stored
155 extern char *g_mountlist_fname; // where mountlist.txt (the mountlist file) is stored
156 extern char *g_mondo_home;              // homedir of Mondo; usually /usr/local/share/mondo
157 extern struct s_bkpinfo *g_bkpinfo_DONTUSETHIS;
158
159 extern t_bkptype g_backup_media_type;
160
161 extern int g_partition_table_locked_up;
162
163 /**
164  * @addtogroup restoreUtilityGroup
165  * @{
166  */
167 /**
168  * Free the malloc()s for the filename variables.
169  */
170 void free_MR_global_filenames()
171 {
172         paranoid_free(g_biggielist_txt);
173         paranoid_free(g_filelist_full);
174         paranoid_free(g_filelist_imagedevs);
175 //  paranoid_free (g_imagedevs_pot );
176         paranoid_free(g_imagedevs_restthese);
177         paranoid_free(g_mondo_cfg_file);
178         paranoid_free(g_mountlist_fname);
179         paranoid_free(g_mondo_home);
180         paranoid_free(g_tmpfs_mountpt);
181         paranoid_free(g_isodir_device);
182         paranoid_free(g_isodir_format);
183
184 }
185
186
187
188 /**
189  * Ask the user which imagedevs from the list contained in @p infname should
190  * actually be restored.
191  * @param infname The file containing a list of all imagedevs.
192  * @param outfname The location of the output file containing the imagedevs the user wanted to restore.
193  * @ingroup restoreUtilityGroup
194  */
195 void ask_about_these_imagedevs(char *infname, char *outfname)
196 {
197         FILE *fin;
198         FILE *fout;
199   /************************************************************************
200    * allocate memory regions. test and set  -sab 16 feb 2003              *
201    ************************************************************************/
202         char *incoming_ptr;
203         char *question_ptr;
204
205         char incoming[MAX_STR_LEN] = "\0";
206         char question[MAX_STR_LEN];
207
208         assert_string_is_neither_NULL_nor_zerolength(infname);
209         assert_string_is_neither_NULL_nor_zerolength(outfname);
210
211         incoming_ptr = malloc(sizeof(incoming));
212         if (incoming_ptr == NULL) {
213                 fprintf(stderr, "Out of Memory\n");
214                 exit(EXIT_FAILURE);
215         }
216
217         question_ptr = malloc(sizeof(question));
218         if (question_ptr == NULL) {
219                 fprintf(stderr, "Out of Memory\n");
220                 exit(EXIT_FAILURE);
221         }
222
223         memset(incoming_ptr, '\0', sizeof(incoming));
224         memset(question_ptr, '\0', sizeof(question));
225
226
227
228         if (!(fin = fopen(infname, "r"))) {
229                 fatal_error("Cannot openin infname");
230         }
231         if (!(fout = fopen(outfname, "w"))) {
232                 fatal_error("Cannot openin outfname");
233         }
234         for (fgets(incoming_ptr, MAX_STR_LEN, fin);
235                  !feof(fin); fgets(incoming_ptr, MAX_STR_LEN, fin)) {
236                 strip_spaces(incoming_ptr);
237
238                 if (incoming[0] == '\0') {
239                         continue;
240                 }
241
242                 sprintf(question_ptr,
243                                 "Should I restore the image of %s ?", incoming_ptr);
244
245                 if (ask_me_yes_or_no(question_ptr)) {
246                         fprintf(fout, "%s\n", incoming_ptr);
247                 }
248         }
249
250   /*** free memory ***********/
251         paranoid_free(incoming_ptr);
252         incoming_ptr = NULL;
253         paranoid_free(question_ptr);
254         question_ptr = NULL;
255
256
257         paranoid_fclose(fout);
258         paranoid_fclose(fin);
259 }
260
261 /**************************************************************************
262  *ASK_ABOUT_THESE_IMAGEDEVS                                               *
263  **************************************************************************/
264
265
266
267
268
269
270
271
272 /**
273  * Extract @c mondo-restore.cfg and @c mountlist.txt from @p ramdisk_fname.
274  * @param bkpinfo The backup information structure. @c tmpdir is the only field used.
275  * @param ramdisk_fname The filename of the @b compressed ramdisk to look in.
276  * @param output_cfg_file Where to put the configuration file extracted.
277  * @param output_mountlist_file Where to put the mountlist file extracted.
278  * @return 0 for success, nonzero for failure.
279  * @ingroup restoreUtilityGroup
280  */
281 int
282 extract_config_file_from_ramdisk(struct s_bkpinfo *bkpinfo,
283                                                                  char *ramdisk_fname,
284                                                                  char *output_cfg_file,
285                                                                  char *output_mountlist_file)
286 {
287         char *mountpt;
288         char *command;
289         char *orig_fname;
290         int retval = 0;
291
292         assert(bkpinfo != NULL);
293         malloc_string(mountpt);
294         malloc_string(command);
295         malloc_string(orig_fname);
296         assert_string_is_neither_NULL_nor_zerolength(ramdisk_fname);
297         assert_string_is_neither_NULL_nor_zerolength(output_cfg_file);
298         assert_string_is_neither_NULL_nor_zerolength(output_mountlist_file);
299         sprintf(mountpt, "%s/mount.bootdisk", bkpinfo->tmpdir);
300         sprintf(command, "mkdir -p %s", mountpt);
301         run_program_and_log_output(command, FALSE);
302         sprintf(command, "gzip -dc %s > %s/mindi.rd 2> /dev/null",
303                         ramdisk_fname, bkpinfo->tmpdir);
304
305         run_program_and_log_output(command, FALSE);
306         sprintf(command, "umount %s", mountpt);
307
308         run_program_and_log_output(command, FALSE);
309
310         sprintf(command, "mount -o loop %s/mindi.rd -t ext2 %s",
311                         bkpinfo->tmpdir, mountpt);
312
313         run_program_and_log_output(command, FALSE);
314
315         sprintf(command, "mkdir -p %s/tmp", bkpinfo->tmpdir);
316
317         run_program_and_log_output(command, FALSE);
318
319         sprintf(command, "cp -f %s/%s %s",      // %s/%s becomes {mountpt}/tmp/m*ndo-restore.cfg
320                         mountpt, g_mondo_cfg_file, output_cfg_file);
321         run_program_and_log_output(command, FALSE);
322
323         sprintf(orig_fname, "%s/%s", mountpt, g_mountlist_fname);
324         if (does_file_exist(orig_fname)) {
325                 sprintf(command, "cp -f %s %s", orig_fname, output_mountlist_file);
326                 run_program_and_log_output(command, FALSE);
327         }
328         sprintf(command, "umount %s", mountpt);
329         run_program_and_log_output(command, FALSE);
330         if (!does_file_exist(output_cfg_file)
331                 || (!does_file_exist(output_mountlist_file)
332                         && does_file_exist(orig_fname))) {
333                 log_msg(2, "Failed to extract %s and/or %s from ramdisk",
334                                 output_cfg_file, output_mountlist_file);
335                 retval = 1;
336         } else {
337                 retval = 0;
338         }
339         paranoid_free(mountpt);
340         paranoid_free(command);
341         paranoid_free(orig_fname);
342         return (retval);
343
344 }
345
346
347
348
349 /**
350  * Keep trying to get mondo-restore.cfg from the archive, until the user gives up.
351  * @param bkpinfo The backup information structure.
352  */
353 void get_cfg_file_from_archive_or_bust(struct s_bkpinfo *bkpinfo)
354 {
355         while (get_cfg_file_from_archive(bkpinfo)) {
356                 if (!ask_me_yes_or_no
357                         ("Failed to find config file/archives. Choose another source?"))
358                 {
359                         fatal_error("Could not find config file/archives. Aborting.");
360                 }
361                 interactively_obtain_media_parameters_from_user(bkpinfo, FALSE);
362         }
363 }
364
365
366 /**
367  * Determine whether @p list_fname contains a line containing @p f.
368  * @param f The line to search for.
369  * @param list_fname The file to search in.
370  * @param preamble Ignore this beginning part of @p f ("" to disable).
371  * @return TRUE if it's in the list, FALSE if it's not.
372  */
373 bool is_file_in_list(char *f, char *list_fname, char *preamble)
374 {
375
376   /** needs malloc **/
377         char *command;
378         char *file;
379         char *tmp;
380         int res;
381
382         malloc_string(command);
383         malloc_string(file);
384         malloc_string(tmp);
385         assert_string_is_neither_NULL_nor_zerolength(f);
386         assert_string_is_neither_NULL_nor_zerolength(list_fname);
387         assert(preamble != NULL);
388
389         if (strncmp(preamble, f, strlen(preamble)) == 0) {
390                 strcpy(file, f + strlen(preamble));
391         } else {
392                 strcpy(file, f);
393         }
394         if (file[0] == '/' && file[1] == '/') {
395                 strcpy(tmp, file);
396                 strcpy(file, tmp + 1);
397         }
398         sprintf(tmp,
399                         "Checking to see if f=%s, file=%s, is in the list of biggiefiles",
400                         f, file);
401         log_msg(2, tmp);
402         sprintf(command, "grep -x \"%s\" %s", file, list_fname);
403         res = run_program_and_log_output(command, FALSE);
404         paranoid_free(command);
405         paranoid_free(file);
406         paranoid_free(tmp);
407         if (res) {
408                 return (FALSE);
409         } else {
410                 return (TRUE);
411         }
412 }
413
414 /**************************************************************************
415  *END_IS_FILE_IN_LIST                                                     *
416  **************************************************************************/
417
418
419
420 /**
421  * Set up an ISO backup.
422  * @param bkpinfo The backup information structure. Fields used:
423  * - @c bkpinfo->backup_media_type
424  * - @c bkpinfo->disaster_recovery
425  * - @c bkpinfo->isodir
426  * @param nuke_me_please If TRUE, we're in nuke mode; if FALSE we're in interactive mode.
427  * @return 0 for success, nonzero for failure.
428  */
429 int iso_fiddly_bits(struct s_bkpinfo *bkpinfo, bool nuke_me_please)
430 {
431         char *mount_isodir_command, *tmp, *command;
432         int retval = 0, i;
433         bool already_mounted = FALSE;
434
435         assert(bkpinfo != NULL);
436         malloc_string(mount_isodir_command);
437         malloc_string(tmp);
438         malloc_string(command);
439         g_ISO_restore_mode = TRUE;
440         read_cfg_var(g_mondo_cfg_file, "iso-dev", g_isodir_device);
441         if (bkpinfo->disaster_recovery) {
442 /* Patch Conor Daly 26-june-2004
443  * Don't let this clobber an existing bkpinfo->isodir */
444                 if (!bkpinfo->isodir[0]) {
445                         strcpy(bkpinfo->isodir, "/tmp/isodir");
446                 }
447 /* End patch */
448                 sprintf(command, "mkdir -p %s", bkpinfo->isodir);
449                 run_program_and_log_output(command, 5);
450                 log_msg(2, "Setting isodir to %s", bkpinfo->isodir);
451         }
452
453         if (!get_isodir_info
454                 (g_isodir_device, g_isodir_format, bkpinfo->isodir,
455                  nuke_me_please)) {
456                 return (1);
457         }
458         paranoid_system("umount " MNT_CDROM " 2> /dev/null");   /* just in case */
459
460         if (is_this_device_mounted(g_isodir_device)) {
461                 log_to_screen("WARNING - isodir is already mounted");
462                 already_mounted = TRUE;
463         } else {
464                 sprintf(mount_isodir_command, "mount %s", g_isodir_device);
465                 if (strlen(g_isodir_format) > 1) {
466                         sprintf(mount_isodir_command + strlen(mount_isodir_command),
467                                         " -t %s", g_isodir_format);
468                 }
469                 strcat(mount_isodir_command, " -o ro ");
470                 strcat(mount_isodir_command, bkpinfo->isodir);
471                 run_program_and_log_output("df -m", FALSE);
472                 sprintf(tmp,
473                                 "The 'mount' command is '%s'. PLEASE report this command to be if you have problems, ok?",
474                                 mount_isodir_command);
475                 log_msg(1, tmp);
476                 if (run_program_and_log_output(mount_isodir_command, FALSE)) {
477                         popup_and_OK
478                                 ("Cannot mount the device where the ISO files are stored.");
479                         return (1);
480                 }
481                 log_to_screen
482                         ("I have mounted the device where the ISO files are stored.");
483         }
484         if (!IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
485                 mount_cdrom(bkpinfo);
486         }
487         i = what_number_cd_is_this(bkpinfo);    /* has the side-effect of calling mount_cdrom() */
488         sprintf(tmp, "%s #%d has been mounted via loopback mount",
489                         media_descriptor_string(bkpinfo->backup_media_type), i);
490         log_msg(1, tmp);
491         if (i < 0) {
492                 popup_and_OK
493                         ("Cannot find ISO images in the directory you specified.");
494                 retval = 1;
495         }
496         log_msg(2, "%ld: bkpinfo->isodir is now %s", __LINE__,
497                         bkpinfo->isodir);
498         paranoid_free(mount_isodir_command);
499         paranoid_free(tmp);
500         paranoid_free(command);
501         return (retval);
502 }
503
504
505
506
507 /**
508  * Kill all Petris processes.
509  */
510 void kill_petris(void)
511 {
512         char *command;
513         malloc_string(command);
514         sprintf(command,
515                         "kill `ps 2> /dev/null | grep petris 2> /dev/null | grep -v grep | cut -d' ' -f2` 2> /dev/null");
516         paranoid_system(command);
517         paranoid_free(command);
518 }
519
520 /**************************************************************************
521  *END_KILL_PETRIS                                                         *
522  **************************************************************************/
523
524
525 /**
526  * (Disabled) Modify rc.local to fix some things on first boot.
527  * This function currently doesn't do anything except make sure /tmp has the
528  * right permissions.
529  * @param path The path to /etc on the user's filesystem.
530  * @return 0 for success, nonzero for failure.
531  */
532 int modify_rclocal_one_time(char *path)
533 {
534   /** malloc **/
535         char *rclocal_fname;
536         char *newfile_fname;
537         char *tmp;
538
539         malloc_string(rclocal_fname);
540         malloc_string(newfile_fname);
541         malloc_string(tmp);
542         assert_string_is_neither_NULL_nor_zerolength(path);
543
544         sprintf(rclocal_fname, "%s/rc.local", path);
545
546 //  sprintf(tmp, "chmod 1777 %s/tmp", MNT_RESTORING);
547 //  run_program_and_log_output( tmp, FALSE );
548         return (0);                                     /* remove this line to open the floodgates... */
549
550         if (!does_file_exist(rclocal_fname)) {
551                 sprintf(rclocal_fname, "%s/rc.d/rc.local", path);
552         }
553         if (!does_file_exist(rclocal_fname)) {
554                 paranoid_free(rclocal_fname);
555                 paranoid_free(newfile_fname);
556                 paranoid_free(tmp);
557                 return (1);
558         }
559         sprintf(newfile_fname, "%s/rc.local.mondorescue", path);
560         sprintf(tmp, "grep mondorescue %s > /dev/null 2> /dev/null",
561                         rclocal_fname);
562         if (system(tmp)) {
563                 sprintf(tmp, "echo \"[ -e %s ] && %s\n\" >> %s",
564                                 newfile_fname, newfile_fname, rclocal_fname);
565
566                 paranoid_system(tmp);
567         }
568         sprintf(tmp, "echo -en \"#!/bin/sh\
569 \\n\
570 \\n\
571 grep -v mondorescue %s > %s\\n\
572 rm -f /var/lock/subsys/*xfs*\\n\
573 rm -f /var/run/xfs.*\\n\
574 killall xfs\\n\
575 service xfs start\\n\
576 yes | rm -f %s\\n\
577 \" > %s", rclocal_fname, rclocal_fname, newfile_fname, newfile_fname);
578         sprintf(tmp, "chmod +x \"%s\"", newfile_fname);
579         run_program_and_log_output(tmp, FALSE);
580         paranoid_free(rclocal_fname);
581         paranoid_free(newfile_fname);
582         paranoid_free(tmp);
583         return (0);
584 }
585
586 /**************************************************************************
587  *END_ MODIFY_RCLOCAL_ONE_TIME                                            *
588  **************************************************************************/
589
590
591
592
593
594 /**
595  * Mount all devices in @p p_external_copy_of_mountlist on @p MNT_RESTORING.
596  * @param p_external_copy_of_mountlist The mountlist containing devices to be mounted.
597  * @param writeable If TRUE, then mount read-write; if FALSE mount read-only.
598  * @return The number of errors encountered (0 for success).
599  */
600 int mount_all_devices(struct mountlist_itself
601                                           *p_external_copy_of_mountlist, bool writeable)
602 {
603         int retval = 0, lino, res;
604         char *tmp, *these_failed, *format;
605         struct mountlist_itself *mountlist;
606
607         malloc_string(tmp);
608         malloc_string(format);
609         malloc_string(these_failed);
610         assert(p_external_copy_of_mountlist != NULL);
611         mountlist = malloc(sizeof(struct mountlist_itself));
612         memcpy((void *) mountlist, (void *) p_external_copy_of_mountlist,
613                    sizeof(struct mountlist_itself));
614         sort_mountlist_by_mountpoint(mountlist, 0);
615
616   /** menset **/
617         these_failed[0] = '\0';
618
619         mvaddstr_and_log_it(g_currentY, 0, "Mounting devices         ");
620         open_progress_form("Mounting devices",
621                                            "I am now mounting all the drives.",
622                                            "This should not take long.",
623                                            "", mountlist->entries);
624
625         for (lino = 0; lino < mountlist->entries; lino++) {
626                 if (!strcmp(mountlist->el[lino].device, "/proc")) {
627                         log_msg(1,
628                                         "Again with the /proc - why is this in your mountlist?");
629                 } else if (is_this_device_mounted(mountlist->el[lino].device)) {
630                         sprintf(tmp, "%s is already mounted",
631                                         mountlist->el[lino].device);
632                         log_to_screen(tmp);
633                 } else if (strcmp(mountlist->el[lino].mountpoint, "none")
634                                    && strcmp(mountlist->el[lino].mountpoint, "lvm")
635                                    && strcmp(mountlist->el[lino].mountpoint, "raid")
636                                    && strcmp(mountlist->el[lino].mountpoint, "image")) {
637                         sprintf(tmp, "Mounting %s", mountlist->el[lino].device);
638                         update_progress_form(tmp);
639                         strcpy(format, mountlist->el[lino].format);
640                         if (!strcmp(format, "ext3")) {
641                                 strcpy(format, "ext2");
642                         }
643                         res = mount_device(mountlist->el[lino].device,
644                                                            mountlist->el[lino].mountpoint,
645                                                            format, writeable);
646                         retval += res;
647                         if (res) {
648                                 strcat(these_failed, mountlist->el[lino].device);
649                                 strcat(these_failed, " ");
650                         }
651                 }
652                 g_current_progress++;
653         }
654         close_progress_form();
655         run_program_and_log_output("df -m", TRUE);
656         if (retval) {
657                 if (g_partition_table_locked_up > 0) {
658                         log_to_screen
659                                 ("fdisk's ictol() call to refresh its copy of the partition table causes the kernel to");
660                         log_to_screen
661                                 ("lock up the partition table. You might have to reboot and use Interactive Mode to");
662                         log_to_screen
663                                 ("format and restore *without* partitioning first. Sorry for the inconvenience.");
664                 }
665                 sprintf(tmp, "Could not mount devices %s- shall I abort?",
666                                 these_failed);
667                 if (!ask_me_yes_or_no(tmp)) {
668                         retval = 0;
669                         log_to_screen
670                                 ("Continuing, although some devices failed to be mounted");
671                         mvaddstr_and_log_it(g_currentY++, 74, "Done.");
672                 } else {
673                         mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
674                         log_to_screen
675                                 ("Unable to mount some or all of your partitions.");
676                 }
677         } else {
678                 log_to_screen("All partitions were mounted OK.");
679                 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
680         }
681         run_program_and_log_output("df -m", 3);
682         paranoid_free(mountlist);
683         paranoid_free(tmp);
684         paranoid_free(format);
685         paranoid_free(these_failed);
686         return (retval);
687 }
688
689  /**************************************************************************
690   *END_MOUNT_ALL_DEVICES                                                   *
691   **************************************************************************/
692
693
694 /**
695  * Mount the CD-ROM device at /mnt/cdrom.
696  * @param bkpinfo The backup information structure. Fields used:
697  * - @c bkpinfo->backup_media_type
698  * - @c bkpinfo->disaster_recovery
699  * - @c bkpinfo->isodir
700  * - @c bkpinfo->media_device
701  * @return 0 for success, nonzero for failure.
702  */
703 int mount_cdrom(struct s_bkpinfo *bkpinfo)
704 {
705         char *mount_cmd;
706         int i, res;
707 #ifdef __FreeBSD__
708         char mdd[32];
709         char *mddev = mdd;
710 #endif
711
712         malloc_string(mount_cmd);
713         assert(bkpinfo != NULL);
714
715         if (bkpinfo->backup_media_type == tape
716                 || bkpinfo->backup_media_type == udev) {
717                 log_msg(8, "Tape/udev. Therefore, no need to mount CDROM.");
718                 paranoid_free(mount_cmd);
719                 return 0;
720         }
721
722         if (!run_program_and_log_output("mount | grep -F " MNT_CDROM, FALSE)) {
723                 log_msg(2, "mount_cdrom() - CD already mounted. Fair enough.");
724                 paranoid_free(mount_cmd);
725                 return (0);
726         }
727
728         if (bkpinfo->backup_media_type == nfs) {
729                 log_msg(2, "Mounting for NFS thingy");
730                 log_msg(2, "isodir = %s", bkpinfo->isodir);
731                 if ((!bkpinfo->isodir[0] || !strcmp(bkpinfo->isodir, "/"))
732                         && am_I_in_disaster_recovery_mode()) {
733                         strcpy(bkpinfo->isodir, "/tmp/isodir");
734                         log_msg(1, "isodir is being set to %s", bkpinfo->isodir);
735                 }
736 #ifdef __FreeBSD__
737                 sprintf(mount_cmd, "/mnt/isodir/%s/%s/%s-%d.iso", bkpinfo->isodir,
738                                 bkpinfo->nfs_remote_dir, bkpinfo->prefix, g_current_media_number);
739                 mddev = make_vn(mount_cmd);
740                 sprintf(mount_cmd, "mount_cd9660 -r %s " MNT_CDROM, mddev);
741 #else
742                 sprintf(mount_cmd, "mount %s/%s/%s-%d.iso -t iso9660 -o loop,ro %s",
743                                 bkpinfo->isodir, bkpinfo->nfs_remote_dir,
744                                 bkpinfo->prefix, g_current_media_number, MNT_CDROM);
745 #endif
746
747         } else
748          if (bkpinfo->backup_media_type == iso) {
749 #ifdef __FreeBSD__
750                 sprintf(mount_cmd, "%s/%s-%d.iso", bkpinfo->isodir,
751                                 bkpinfo->prefix, g_current_media_number);
752                 mddev = make_vn(mount_cmd);
753                 sprintf(mount_cmd, "mount_cd9660 -r %s %s", mddev, MNT_CDROM);
754 #else
755                 sprintf(mount_cmd, "mount %s/%s-%d.iso -t iso9660 -o loop,ro %s",
756                                 bkpinfo->isodir, bkpinfo->prefix, g_current_media_number, MNT_CDROM);
757 #endif
758         } else if (strstr(bkpinfo->media_device, "/dev/"))
759 #ifdef __FreeBSD__
760         {
761                 sprintf(mount_cmd, "mount_cd9660 -r %s %s", bkpinfo->media_device,
762                                 MNT_CDROM);
763         }
764 #else
765         {
766                 sprintf(mount_cmd, "mount %s -t iso9660 -o ro %s",
767                                 bkpinfo->media_device, MNT_CDROM);
768         }
769 #endif
770
771         else {
772                 if (bkpinfo->disaster_recovery
773                         && does_file_exist("/tmp/CDROM-LIVES-HERE")) {
774                         strcpy(bkpinfo->media_device,
775                                    last_line_of_file("/tmp/CDROM-LIVES-HERE"));
776                 } else {
777                         find_cdrom_device(bkpinfo->media_device, TRUE);
778                 }
779
780 #ifdef __FreeBSD__
781                 sprintf(mount_cmd, "mount_cd9660 -r %s %s", bkpinfo->media_device,
782                                 MNT_CDROM);
783 #else
784                 sprintf(mount_cmd, "mount %s -t iso9660 -o ro %s",
785                                 bkpinfo->media_device, MNT_CDROM);
786 #endif
787
788         }
789         log_msg(2, "(mount_cdrom) --- command = %s", mount_cmd);
790         for (i = 0; i < 2; i++) {
791                 res = run_program_and_log_output(mount_cmd, FALSE);
792                 if (!res) {
793                         break;
794                 } else {
795                         log_msg(2, "Failed to mount CD-ROM drive.");
796                         sleep(5);
797                         run_program_and_log_output("sync", FALSE);
798                 }
799         }
800         if (res) {
801                 log_msg(2, "Failed, despite %d attempts", i);
802         } else {
803                 log_msg(2, "Mounted CD-ROM drive OK");
804         }
805         paranoid_free(mount_cmd);
806         return (res);
807 }
808
809
810
811
812
813 /**************************************************************************
814  *END_MOUNT_CDROM                                                         *
815  **************************************************************************/
816
817
818 /**
819  * Mount @p device at @p mpt as @p format.
820  * @param device The device (/dev entry) to mount.
821  * @param mpt The directory to mount it on.
822  * @param format The filesystem type of @p device.
823  * @param writeable If TRUE, mount read-write; if FALSE, mount read-only.
824  * @return 0 for success, nonzero for failure.
825  */
826 int mount_device(char *device, char *mpt, char *format, bool writeable)
827 {
828         int res = 0;
829
830   /** malloc **/
831         char *tmp, *command, *mountdir, *mountpoint, *additional_parameters;
832
833         assert_string_is_neither_NULL_nor_zerolength(device);
834         assert_string_is_neither_NULL_nor_zerolength(mpt);
835         assert(format != NULL);
836         malloc_string(tmp);
837         malloc_string(command);
838         malloc_string(mountdir);
839         malloc_string(mountpoint);
840         malloc_string(additional_parameters);
841
842         if (!strcmp(mpt, "/1")) {
843                 strcpy(mountpoint, "/");
844                 log_msg(3, "Mommm! SME is being a dildo!");
845         } else {
846                 strcpy(mountpoint, mpt);
847         }
848
849         if (!strcmp(mountpoint, "lvm")) {
850                 return (0);
851         }
852         if (!strcmp(mountpoint, "image")) {
853                 return (0);
854         }
855         sprintf(tmp, "Mounting device %s   ", device);
856         log_msg(1, tmp);
857         if (writeable) {
858                 strcpy(additional_parameters, "-o rw");
859         } else {
860                 strcpy(additional_parameters, "-o ro");
861         }
862         if (find_home_of_exe("setfattr")) {
863                 strcat(additional_parameters, ",user_xattr");
864         }
865         if (find_home_of_exe("setfacl")) {
866                 strcat(additional_parameters, ",acl");
867         }
868
869         if (!strcmp(mountpoint, "swap")) {
870                 sprintf(command, "swapon %s", device);
871         } else {
872                 if (!strcmp(mountpoint, "/")) {
873                         strcpy(mountdir, MNT_RESTORING);
874                 } else {
875                         sprintf(mountdir, "%s%s", MNT_RESTORING, mountpoint);
876                 }
877                 sprintf(command, "mkdir -p %s", mountdir);
878                 run_program_and_log_output(command, FALSE);
879                 sprintf(command, "mount -t %s %s %s %s 2>> %s", format, device,
880                                 additional_parameters, mountdir, MONDO_LOGFILE);
881                 log_msg(2, "command='%s'", command);
882         }
883         res = run_program_and_log_output(command, TRUE);
884         if (res && (strstr(command, "xattr") || strstr(command, "acl"))) {
885                 log_msg(1, "Re-trying without the fancy extra parameters");
886                 sprintf(command, "mount -t %s %s %s 2>> %s", format, device,
887                                 mountdir, MONDO_LOGFILE);
888                 res = run_program_and_log_output(command, TRUE);
889         }
890         if (res) {
891                 log_msg(1, "Unable to mount device %s (type %s) at %s", device,
892                                 format, mountdir);
893                 log_msg(1, "command was '%s'", command);
894                 if (!strcmp(mountpoint, "swap")) {
895                         log_to_screen(tmp);
896                 } else {
897                         log_msg(2, "Retrying w/o the '-t' switch");
898                         sprintf(command, "mount %s %s 2>> %s", device, mountdir,
899                                         MONDO_LOGFILE);
900                         log_msg(2, "2nd command = '%s'", command);
901                         res = run_program_and_log_output(command, TRUE);
902                         if (res == 0) {
903                                 log_msg(1,
904                                                 "That's OK. I called mount w/o a filesystem type and it worked fine in the end.");
905                         } else {
906                                 log_to_screen(tmp);
907                         }
908                 }
909         }
910         if (res && !strcmp(mountpoint, "swap")) {
911                 log_msg(2, "That's ok. It's just a swap partition.");
912                 log_msg(2, "Non-fatal error. Returning 0.");
913                 res = 0;
914         }
915
916         paranoid_free(tmp);
917         paranoid_free(command);
918         paranoid_free(mountdir);
919         paranoid_free(mountpoint);
920         paranoid_free(additional_parameters);
921
922         return (res);
923 }
924
925 /**************************************************************************
926  *END_MOUNT_DEVICE                                                        *
927  **************************************************************************/
928
929
930
931 /**
932  * Fix some miscellaneous things in the filesystem so the system will come
933  * up correctly on the first boot.
934  */
935 void protect_against_braindead_sysadmins()
936 {
937         run_program_and_log_output("touch " MNT_RESTORING "/var/log/pacct",
938                                                            FALSE);
939         run_program_and_log_output("touch " MNT_RESTORING "/var/account/pacct",
940                                                            FALSE);
941         if (run_program_and_log_output("ls " MNT_RESTORING " /tmp", FALSE)) {
942                 run_program_and_log_output("chmod 1777 " MNT_RESTORING "/tmp",
943                                                                    FALSE);
944         }
945         run_program_and_log_output("mkdir -p " MNT_RESTORING
946                                                            "/var/run/console", FALSE);
947         run_program_and_log_output("chmod 777 " MNT_RESTORING "/dev/null",
948                                                            FALSE);
949         run_program_and_log_output("cd " MNT_RESTORING
950                                                            "; 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",
951                                                            TRUE);
952         run_program_and_log_output("rm -f " MNT_RESTORING "/var/run/*.pid",
953                                                            TRUE);
954         run_program_and_log_output("rm -f " MNT_RESTORING "/var/lock/subsys/*",
955                                                            TRUE);
956 }
957
958 /**************************************************************************
959  *END_PROTECT_AGAINST_BRAINDEAD_SYSADMINS                                 *
960  **************************************************************************/
961
962
963
964
965 /**
966  * Fill out @p bkpinfo based on @p cfg_file.
967  * @param cfg_file The mondo-restore.cfg file to read into @p bkpinfo.
968  * @param bkpinfo The backup information structure to fill out with information
969  * from @p cfg_file.
970  * @return 0 for success, nonzero for failure.
971  */
972 int read_cfg_file_into_bkpinfo(char *cfgf, struct s_bkpinfo *bkpinfo)
973 {
974   /** add mallocs **/
975         char *value = NULL;
976         char *tmp = NULL;
977         char *envtmp1 = NULL;
978         char *envtmp2 = NULL;
979         char *command = NULL;
980         char *iso_mnt = NULL;
981         char *iso_path = NULL;
982         char *old_isodir = NULL;
983         char cfg_file[100];
984         t_bkptype media_specified_by_user;
985
986         malloc_string(command);
987         malloc_string(iso_mnt);
988         malloc_string(iso_path);
989         malloc_string(old_isodir);
990         malloc_string(value);
991         malloc_string(tmp);
992 //  assert_string_is_neither_NULL_nor_zerolength(cfg_file);
993         assert(bkpinfo != NULL);
994
995         if (!cfgf) {
996                 strcpy(cfg_file, g_mondo_cfg_file);
997         } else {
998                 strcpy(cfg_file, cfgf);
999         }
1000
1001         media_specified_by_user = bkpinfo->backup_media_type;   // or 'none', if not specified
1002
1003         if (0 == read_cfg_var(cfg_file, "backup-media-type", value)) {
1004                 if (!strcmp(value, "cdstream")) {
1005                         bkpinfo->backup_media_type = cdstream;
1006                 } else if (!strcmp(value, "cdr")) {
1007                         bkpinfo->backup_media_type = cdr;
1008                 } else if (!strcmp(value, "cdrw")) {
1009                         bkpinfo->backup_media_type = cdrw;
1010                 } else if (!strcmp(value, "dvd")) {
1011                         bkpinfo->backup_media_type = dvd;
1012                 } else if (!strcmp(value, "iso")) {
1013 /*
1014           if (am_I_in_disaster_recovery_mode()
1015           && !run_program_and_log_output("mount /dev/cdrom "MNT_CDROM, 1)
1016           && does_file_exist(MNT_CDROM"/archives/filelist.0"))
1017 */
1018
1019 // Patch by Conor Daly - 2004/07/12
1020                         bkpinfo->backup_media_type = iso;
1021                         if (am_I_in_disaster_recovery_mode()) {
1022                                 /* Check to see if CD is already mounted before mounting it... */
1023                                 if (!is_this_device_mounted("/dev/cdrom")) {
1024                                         log_msg(2,
1025                                                         "NB: CDROM device not mounted, mounting...");
1026                                         run_program_and_log_output("mount /dev/cdrom "
1027                                                                                            MNT_CDROM, 1);
1028                                 }
1029                                 if (does_file_exist(MNT_CDROM "/archives/filelist.0")) {
1030                                         bkpinfo->backup_media_type = cdr;
1031                                         run_program_and_log_output("umount " MNT_CDROM, 1);
1032                                         log_it
1033                                                 ("Re-jigging configuration AGAIN. CD-R, not ISO.");
1034                                 }
1035                         }
1036                         if (read_cfg_var(cfg_file, "iso-prefix", value) == 0) {
1037                                         strcpy(bkpinfo->prefix,value);
1038                         } else {
1039                                         strcpy(bkpinfo->prefix,STD_PREFIX);
1040                         }
1041                 } else if (!strcmp(value, "nfs")) {
1042                         bkpinfo->backup_media_type = nfs;
1043                         if (read_cfg_var(cfg_file, "iso-prefix", value) == 0) {
1044                                         strcpy(bkpinfo->prefix,value);
1045                         } else {
1046                                         strcpy(bkpinfo->prefix,STD_PREFIX);
1047                         }
1048                         /* We need to override prefix value in PXE mode as it's
1049                          * already done in start-nfs */
1050                         envtmp1 = getenv("imgname");
1051                         if (envtmp1 == NULL) {
1052                                 fatal_error("no imgname variable in environment");
1053                         }
1054                         if (strstr(call_program_and_get_last_line_of_output
1055                            ("cat /proc/cmdline"), "pxe")) {
1056                                         strcpy(bkpinfo->prefix,envtmp1);
1057                         }
1058
1059                 } else if (!strcmp(value, "tape")) {
1060                         bkpinfo->backup_media_type = tape;
1061                 } else if (!strcmp(value, "udev")) {
1062                         bkpinfo->backup_media_type = udev;
1063                 } else {
1064                         fatal_error("UNKNOWN bkp-media-type");
1065                 }
1066         } else {
1067                 fatal_error("backup-media-type not specified!");
1068         }
1069         if (bkpinfo->disaster_recovery) {
1070                 if (bkpinfo->backup_media_type == cdstream) {
1071                         sprintf(bkpinfo->media_device, "/dev/cdrom");
1072 //          bkpinfo->media_size[0] = -1;
1073                         bkpinfo->media_size[0] = 1999 * 1024;
1074                         bkpinfo->media_size[1] = 650;   /* good guess */
1075                 } else if (bkpinfo->backup_media_type == tape
1076                                    || bkpinfo->backup_media_type == udev) {
1077                         if (read_cfg_var(cfg_file, "media-dev", value)) {
1078                                 fatal_error("Cannot get tape device name from cfg file");
1079                         }
1080                         strcpy(bkpinfo->media_device, value);
1081                         read_cfg_var(cfg_file, "media-size", value);
1082                         bkpinfo->media_size[1] = atol(value);
1083                         sprintf(tmp, "Backup medium is TAPE --- dev=%s",
1084                                         bkpinfo->media_device);
1085                         log_msg(2, tmp);
1086                 } else {
1087                         strcpy(bkpinfo->media_device, "/dev/cdrom");    /* we don't really need this var */
1088                         bkpinfo->media_size[0] = 1999 * 1024;   /* 650, probably, but we don't need this var anyway */
1089                         bkpinfo->media_size[1] = 1999 * 1024;   /* 650, probably, but we don't need this var anyway */
1090                         log_msg(2, "Backup medium is CD-R[W]");
1091                 }
1092         } else {
1093                 log_msg(2,
1094                                 "Not in Disaster Recovery Mode. No need to derive device name from config file.");
1095         }
1096
1097         read_cfg_var(cfg_file, "use-star", value);
1098         if (strstr(value, "yes")) {
1099                 bkpinfo->use_star = TRUE;
1100                 log_msg(1, "Goody! ... bkpinfo->use_star is now true.");
1101         }
1102
1103         if (0 == read_cfg_var(cfg_file, "internal-tape-block-size", value)) {
1104                 bkpinfo->internal_tape_block_size = atol(value);
1105                 log_msg(1, "Internal tape block size has been custom-set to %ld",
1106                                 bkpinfo->internal_tape_block_size);
1107         } else {
1108                 bkpinfo->internal_tape_block_size =
1109                         DEFAULT_INTERNAL_TAPE_BLOCK_SIZE;
1110                 log_msg(1, "Internal tape block size = default (%ld)",
1111                                 DEFAULT_INTERNAL_TAPE_BLOCK_SIZE);
1112         }
1113
1114         read_cfg_var(cfg_file, "use-lzo", value);
1115         if (strstr(value, "yes")) {
1116                 bkpinfo->use_lzo = TRUE;
1117                 strcpy(bkpinfo->zip_exe, "lzop");
1118                 strcpy(bkpinfo->zip_suffix, "lzo");
1119         } else {
1120                 read_cfg_var(cfg_file, "use-comp", value);
1121                 if (strstr(value, "yes")) {
1122                         bkpinfo->use_lzo = FALSE;
1123                         strcpy(bkpinfo->zip_exe, "bzip2");
1124                         strcpy(bkpinfo->zip_suffix, "bz2");
1125                 } else {
1126                         bkpinfo->zip_exe[0] = bkpinfo->zip_suffix[0] = '\0';
1127                 }
1128         }
1129
1130         value[0] = '\0';
1131         read_cfg_var(cfg_file, "differential", value);
1132         if (!strcmp(value, "yes") || !strcmp(value, "1")) {
1133                 bkpinfo->differential = TRUE;
1134         }
1135         log_msg(2, "differential var = '%s'", value);
1136         if (bkpinfo->differential) {
1137                 log_msg(2, "THIS IS A DIFFERENTIAL BACKUP");
1138         } else {
1139                 log_msg(2, "This is a regular (full) backup");
1140         }
1141
1142         read_cfg_var(g_mondo_cfg_file, "please-dont-eject", tmp);
1143         if (tmp[0]
1144                 ||
1145                 strstr(call_program_and_get_last_line_of_output
1146                            ("cat /proc/cmdline"), "donteject")) {
1147                 bkpinfo->please_dont_eject = TRUE;
1148                 log_msg(2, "Ok, I shan't eject when restoring! Groovy.");
1149         }
1150
1151         if (bkpinfo->backup_media_type == nfs) {
1152                 if (!cfgf) {
1153                         log_msg(2, "nfs_mount remains %s", bkpinfo->nfs_mount);
1154                         log_msg(2, "nfs_remote_dir remains %s",
1155                                         bkpinfo->nfs_remote_dir);
1156                         log_msg(2,
1157                                         "...cos it wouldn't make sense to abandon the values that GOT ME to this config file in the first place");
1158                 } else {
1159                         read_cfg_var(g_mondo_cfg_file, "nfs-server-mount",
1160                                                  bkpinfo->nfs_mount);
1161                         read_cfg_var(g_mondo_cfg_file, "nfs-server-path",
1162                                                  bkpinfo->nfs_remote_dir);
1163                         log_msg(2, "nfs_mount is %s", bkpinfo->nfs_mount);
1164                         log_msg(2, "nfs_remote_dir is %s", bkpinfo->nfs_remote_dir);
1165                 }
1166                 /* We need to override values in PXE mode as it's
1167                  * already done in start-nfs */
1168                 envtmp1 = getenv("nfsmount");
1169                 if (envtmp1 == NULL) {
1170                         fatal_error("no nfsmount variable in environment");
1171                 }
1172                 envtmp2 = getenv("dirimg");
1173                 if (envtmp2 == NULL) {
1174                         fatal_error("no dirimg variable in environment");
1175                 }
1176                 if (strstr(call_program_and_get_last_line_of_output
1177                    ("cat /proc/cmdline"), "pxe")) {
1178                                 strcpy(bkpinfo->nfs_mount,envtmp1);
1179                                 strcpy(bkpinfo->nfs_remote_dir,envtmp2);
1180                         }
1181         } else if (bkpinfo->backup_media_type == iso) {
1182                 /* Patch by Conor Daly 23-june-2004
1183                  * to correctly mount iso-dev and set a sensible
1184                  * isodir in disaster recovery mode
1185                  */
1186                 strcpy(old_isodir, bkpinfo->isodir);
1187                 read_cfg_var(g_mondo_cfg_file, "iso-mnt", iso_mnt);
1188                 read_cfg_var(g_mondo_cfg_file, "isodir", iso_path);
1189                 sprintf(bkpinfo->isodir, "%s%s", iso_mnt, iso_path);
1190                 if (!bkpinfo->isodir[0]) {
1191                         strcpy(bkpinfo->isodir, old_isodir);
1192                 }
1193                 if (!bkpinfo->disaster_recovery) {
1194                         if (strcmp(old_isodir, bkpinfo->isodir)) {
1195                                 log_it
1196                                         ("user nominated isodir differs from archive, keeping user's choice: %s %s\n",
1197                                          old_isodir, bkpinfo->isodir);
1198                                 strcpy(bkpinfo->isodir, old_isodir);
1199                         }
1200                 }
1201                 read_cfg_var(g_mondo_cfg_file, "iso-dev", g_isodir_device);
1202                 log_msg(2, "isodir=%s; iso-dev=%s", bkpinfo->isodir,
1203                                 g_isodir_device);
1204                 if (bkpinfo->disaster_recovery) {
1205                         if (is_this_device_mounted(g_isodir_device)) {
1206                                 log_msg(2, "NB: isodir is already mounted");
1207                                 /* Find out where it's mounted */
1208                                 sprintf(command,
1209                                                 "mount | grep -E '^%s' | tail -n1 | cut -d' ' -f3",
1210                                                 g_isodir_device);
1211                                 log_it("command = %s", command);
1212                                 log_it("res of it = %s",
1213                                            call_program_and_get_last_line_of_output(command));
1214                                 sprintf(iso_mnt, "%s",
1215                                                 call_program_and_get_last_line_of_output(command));
1216                         } else {
1217                                 sprintf(iso_mnt, "/tmp/isodir");
1218                                 sprintf(tmp, "mkdir -p %s", iso_mnt);
1219                                 run_program_and_log_output(tmp, 5);
1220                                 sprintf(tmp, "mount %s %s", g_isodir_device, iso_mnt);
1221                                 if (run_program_and_log_output(tmp, 3)) {
1222                                         log_msg(1,
1223                                                         "Unable to mount isodir. Perhaps this is really a CD backup?");
1224                                         bkpinfo->backup_media_type = cdr;
1225                                         strcpy(bkpinfo->media_device, "/dev/cdrom");    /* superfluous */
1226                                         bkpinfo->isodir[0] = iso_mnt[0] = iso_path[0] = '\0';
1227                                         if (mount_cdrom(bkpinfo)) {
1228                                                 fatal_error
1229                                                         ("Unable to mount isodir. Failed to mount CD-ROM as well.");
1230                                         } else {
1231                                                 log_msg(1,
1232                                                                 "You backed up to disk, then burned some CDs.");
1233                                         }
1234                                 }
1235                         }
1236                         /* bkpinfo->isodir should now be the true path to prefix-1.iso etc... */
1237                         if (bkpinfo->backup_media_type == iso) {
1238                                 sprintf(bkpinfo->isodir, "%s%s", iso_mnt, iso_path);
1239                         }
1240                 }
1241         }
1242
1243         if (media_specified_by_user != none) {
1244                 if (g_restoring_live_from_cd) {
1245                         if (bkpinfo->backup_media_type != media_specified_by_user) {
1246                                 log_msg(2,
1247                                                 "bkpinfo->backup_media_type != media_specified_by_user, so I'd better ask :)");
1248                                 interactively_obtain_media_parameters_from_user(bkpinfo,
1249                                                                                                                                 FALSE);
1250                                 media_specified_by_user = bkpinfo->backup_media_type;
1251                                 get_cfg_file_from_archive(bkpinfo);
1252 /*
1253               if (media_specified_by_user != cdr && media_specified_by_user == cdrw)
1254                 { g_restoring_live_from_cd = FALSE; }
1255 */
1256                         }
1257                 }
1258                 bkpinfo->backup_media_type = media_specified_by_user;
1259         }
1260         g_backup_media_type = bkpinfo->backup_media_type;
1261         paranoid_free(value);
1262         paranoid_free(tmp);
1263         paranoid_free(command);
1264         paranoid_free(iso_mnt);
1265         paranoid_free(iso_path);
1266         paranoid_free(old_isodir);
1267         return (0);
1268
1269 }
1270
1271 /**************************************************************************
1272  *END_READ_CFG_FILE_INTO_BKPINFO                                          *
1273  **************************************************************************/
1274
1275
1276
1277
1278 /**
1279  * Allow the user to edit the filelist and biggielist.
1280  * The filelist is unlinked after it is read.
1281  * @param bkpinfo The backup information structure. Fields used:
1282  * - @c bkpinfo->backup_media_type
1283  * - @c bkpinfo->isodir
1284  * - @c bkpinfo->media_device
1285  * - @c bkpinfo->tmpdir
1286  * @return The filelist structure containing the information read from disk.
1287  */
1288 struct
1289 s_node *process_filelist_and_biggielist(struct s_bkpinfo *bkpinfo)
1290 {
1291         struct s_node *filelist;
1292
1293   /** add mallocs**/
1294         char *command;
1295         char *tmp;
1296         int res = 0;
1297         pid_t pid;
1298
1299         assert(bkpinfo != NULL);
1300         malloc_string(command);
1301         malloc_string(tmp);
1302
1303         if (does_file_exist(g_filelist_full)
1304                 && does_file_exist(g_biggielist_txt)) {
1305                 log_msg(1, "%s exists", g_filelist_full);
1306                 log_msg(1, "%s exists", g_biggielist_txt);
1307                 log_msg(2,
1308                                 "Filelist and biggielist already recovered from media. Yay!");
1309         } else {
1310                 getcwd(tmp, MAX_STR_LEN);
1311                 chdir(bkpinfo->tmpdir);
1312                 log_msg(1, "chdir(%s)", bkpinfo->tmpdir);
1313                 log_to_screen("Extracting filelist and biggielist from media...");
1314                 unlink("/tmp/filelist.full");
1315                 unlink("/" FILELIST_FULL_STUB);
1316                 unlink("/tmp/i-want-my-lvm");
1317                 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1318                         sprintf(command,
1319                                         "tar -zxf %s %s %s %s %s %s",
1320                                         bkpinfo->media_device,
1321                                         MOUNTLIST_FNAME_STUB,
1322                                         BIGGIELIST_TXT_STUB,
1323                                         FILELIST_FULL_STUB,
1324                                         "tmp/i-want-my-lvm", MONDO_CFG_FILE_STUB);
1325                         log_msg(1, "tarcommand = %s", command);
1326                         run_program_and_log_output(command, 1);
1327                 } else {
1328                         log_msg(2,
1329                                         "Calling insist_on_this_cd_number; bkpinfo->isodir=%s",
1330                                         bkpinfo->isodir);
1331                         insist_on_this_cd_number(bkpinfo, 1);
1332                         log_msg(2, "Back from iotcn");
1333                         run_program_and_log_output("mount", 1);
1334                         sprintf(command,
1335                                         "tar -zxf %s/images/all.tar.gz %s %s %s %s %s",
1336                                         MNT_CDROM,
1337                                         MOUNTLIST_FNAME_STUB,
1338                                         BIGGIELIST_TXT_STUB,
1339                                         FILELIST_FULL_STUB,
1340                                         "tmp/i-want-my-lvm", MONDO_CFG_FILE_STUB);
1341
1342                         log_msg(1, "tarcommand = %s", command);
1343                         run_program_and_log_output(command, 1);
1344 //    popup_and_OK("Press ENTER to continue");
1345                         if (!does_file_exist(BIGGIELIST_TXT_STUB)) {
1346                                 fatal_error
1347                                         ("all.tar.gz did not include tmp/biggielist.txt");
1348                         }
1349                         if (!does_file_exist(FILELIST_FULL_STUB)) {
1350                                 fatal_error
1351                                         ("all.tar.gz did not include tmp/filelist.full.gz");
1352                         }
1353                 }
1354                 sprintf(command, "cp -f %s %s", MONDO_CFG_FILE_STUB,
1355                                 g_mondo_cfg_file);
1356                 run_program_and_log_output(command, FALSE);
1357
1358                 sprintf(command, "cp -f %s/%s %s", bkpinfo->tmpdir,
1359                                 BIGGIELIST_TXT_STUB, g_biggielist_txt);
1360                 log_msg(1, "command = %s", command);
1361                 paranoid_system(command);
1362                 sprintf(command, "ln -sf %s/%s %s", bkpinfo->tmpdir,
1363                                 FILELIST_FULL_STUB, g_filelist_full);
1364                 log_msg(1, "command = %s", command);
1365                 paranoid_system(command);
1366         }
1367
1368         if (am_I_in_disaster_recovery_mode()
1369                 &&
1370                 ask_me_yes_or_no("Do you want to retrieve the mountlist as well?"))
1371         {
1372 //      sprintf(command, "cp -f tmp/mountlist.txt /tmp");
1373                 sprintf(command, "ln -sf %s/%s /tmp", MOUNTLIST_FNAME_STUB,
1374                                 bkpinfo->tmpdir);
1375                 paranoid_system(command);
1376         }
1377
1378         chdir(tmp);
1379
1380         if (!does_file_exist(g_biggielist_txt)) {
1381                 log_msg(1, "Warning - %s not found", g_biggielist_txt);
1382         }
1383         if (!does_file_exist(g_filelist_full)) {
1384                 log_msg(1, "Warning - %s does not exist", g_filelist_full);
1385         }
1386 //  popup_and_OK("Wonderful.");
1387
1388         log_msg(2, "Forking");
1389         pid = fork();
1390         switch (pid) {
1391         case -1:
1392                 fatal_error("Forking error");
1393                 break;
1394
1395         case 0:
1396                 log_to_screen("Pre-processing filelist");
1397                 if (!does_file_exist(g_biggielist_txt)) {
1398                         sprintf(command, "> %s", g_biggielist_txt);
1399                         paranoid_system(command);
1400                 }
1401                 sprintf(command, "grep  -x \"/dev/.*\" %s > %s",
1402                                 g_biggielist_txt, g_filelist_imagedevs);
1403                 paranoid_system(command);
1404                 exit(0);
1405                 break;
1406
1407         default:
1408                 open_evalcall_form("Pre-processing filelist");
1409                 while (!waitpid(pid, (int *) 0, WNOHANG)) {
1410                         usleep(100000);
1411                         update_evalcall_form(0);
1412                 }
1413         }
1414         close_evalcall_form();
1415
1416         log_msg(3, "loading filelist");
1417         filelist = load_filelist(g_filelist_full);
1418         log_msg(3, "deleting original filelist");
1419         unlink(g_filelist_full);
1420         if (g_text_mode) {
1421                 printf("Restore which directory? --> ");
1422                 fgets(tmp, sizeof(tmp), stdin);
1423                 toggle_path_selection(filelist, tmp, TRUE);
1424                 if (strlen(tmp) == 0) {
1425                         res = 1;
1426                 } else {
1427                         res = 0;
1428                 }
1429         } else {
1430                 res = edit_filelist(filelist);
1431         }
1432         if (res) {
1433                 log_msg(2, "User hit 'cancel'. Freeing filelist and aborting.");
1434                 free_filelist(filelist);
1435                 return (NULL);
1436         }
1437         ask_about_these_imagedevs(g_filelist_imagedevs, g_imagedevs_restthese);
1438         close_evalcall_form();
1439
1440         // NB: It's not necessary to add g_biggielist_txt to the filelist.full
1441         // file. The filelist.full file already contains the filename of EVERY
1442         // file backed up - regular and biggie files.
1443
1444         // However, we do want to make sure the imagedevs selected by the user
1445         // are flagged for restoring.
1446         if (length_of_file(g_imagedevs_restthese) > 2) {
1447                 add_list_of_files_to_filelist(filelist, g_imagedevs_restthese,
1448                                                                           TRUE);
1449         }
1450
1451         paranoid_free(command);
1452         paranoid_free(tmp);
1453         return (filelist);
1454 }
1455
1456 /**************************************************************************
1457  *END_ PROCESS_FILELIST_AND_BIGGIELIST                                    *
1458  **************************************************************************/
1459
1460
1461
1462
1463 /**
1464  * Make a backup copy of <tt>path_root</tt>/<tt>filename</tt>.
1465  * The backup filename is the filename of the original with ".pristine" added.
1466  * @param path_root The place where the filesystem is mounted (e.g. MNT_RESTORING).
1467  * @param filename The filename (absolute path) within @p path_root.
1468  * @return 0 for success, nonzero for failure.
1469  */
1470 int backup_crucial_file(char *path_root, char *filename)
1471 {
1472         char *tmp;
1473         char *command;
1474         int res;
1475
1476         malloc_string(tmp);
1477         malloc_string(command);
1478         assert(path_root != NULL);
1479         assert_string_is_neither_NULL_nor_zerolength(filename);
1480
1481         sprintf(tmp, "%s/%s", path_root, filename);
1482         sprintf(command, "cp -f %s %s.pristine", tmp, tmp);
1483
1484         res = run_program_and_log_output(command, 5);
1485         paranoid_free(tmp);
1486         paranoid_free(command);
1487         return (res);
1488 }
1489
1490
1491 /**
1492  * Install the user's boot loader in the MBR.
1493  * Currently LILO, ELILO, GRUB, RAW (dd of MBR), and the FreeBSD bootloader are supported.
1494  * @param offer_to_hack_scripts If TRUE, then offer to hack the user's fstab for them.
1495  * @return 0 for success, nonzero for failure.
1496  */
1497 int run_boot_loader(bool offer_to_hack_scripts)
1498 {
1499         int res;
1500         int retval = 0;
1501
1502   /** malloc *******/
1503         char *device;
1504         char *tmp;
1505         char *name;
1506
1507         malloc_string(device);
1508         malloc_string(tmp);
1509         malloc_string(name);
1510         backup_crucial_file(MNT_RESTORING, "/etc/fstab");
1511         backup_crucial_file(MNT_RESTORING, "/etc/grub.conf");
1512         backup_crucial_file(MNT_RESTORING, "/etc/lilo.conf");
1513         backup_crucial_file(MNT_RESTORING, "/etc/elilo.conf");
1514         read_cfg_var(g_mondo_cfg_file, "bootloader.device", device);
1515         read_cfg_var(g_mondo_cfg_file, "bootloader.name", name);
1516         sprintf(tmp, "run_boot_loader: device='%s', name='%s'", device, name);
1517         log_msg(2, tmp);
1518         system("sync");
1519         if (!strcmp(name, "LILO")) {
1520                 res = run_lilo(offer_to_hack_scripts);
1521         } else if (!strcmp(name, "ELILO")) {
1522                 res = run_elilo(offer_to_hack_scripts);
1523         } else if (!strcmp(name, "GRUB")) {
1524 //      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?")))
1525 //        {
1526                 res = run_grub(offer_to_hack_scripts, device);
1527 //    unlink(DO_MBR_PLEASE);
1528 //  }
1529 //      else
1530 //        {
1531 //    log_msg(1, "Not running run_grub(). Was a bad idea anyway.");
1532 //    res = 1;
1533 //  }
1534         } else if (!strcmp(name, "RAW")) {
1535                 res = run_raw_mbr(offer_to_hack_scripts, device);
1536         }
1537 #ifdef __FreeBSD__
1538         else if (!strcmp(name, "BOOT0")) {
1539                 sprintf(tmp, "boot0cfg -B %s", device);
1540                 res = run_program_and_log_output(tmp, FALSE);
1541         } else {
1542                 sprintf(tmp, "ls /dev | grep -xq %ss[1-4].*", device);
1543                 if (!system(tmp)) {
1544                         sprintf(tmp, MNT_RESTORING "/sbin/fdisk -B %s", device);
1545                         res = run_program_and_log_output(tmp, 3);
1546                 } else {
1547                         log_msg(1,
1548                                         "I'm not running any boot loader. You have a DD boot drive. It's already loaded up.");
1549                 }
1550         }
1551 #else
1552         else {
1553                 log_to_screen
1554                         ("Unable to determine type of boot loader. Defaulting to LILO.");
1555                 res = run_lilo(offer_to_hack_scripts);
1556         }
1557 #endif
1558         retval += res;
1559         if (res) {
1560                 log_to_screen("Your boot loader returned an error");
1561         } else {
1562                 log_to_screen("Your boot loader ran OK");
1563         }
1564         paranoid_free(device);
1565         paranoid_free(tmp);
1566         paranoid_free(name);
1567         return (retval);
1568 }
1569
1570 /**************************************************************************
1571  *END_ RUN_BOOT_LOADER                                                    *
1572  **************************************************************************/
1573
1574
1575
1576 /**
1577  * Attempt to find the user's editor.
1578  * @return The editor found ("vi" if none could be found).
1579  * @note The returned string points to static storage that will be overwritten with each call.
1580  */
1581 char *find_my_editor(void)
1582 {
1583         static char output[MAX_STR_LEN];
1584         if (find_home_of_exe("pico")) {
1585                 strcpy(output, "pico");
1586         } else if (find_home_of_exe("nano")) {
1587                 strcpy(output, "nano");
1588         } else if (find_home_of_exe("e3em")) {
1589                 strcpy(output, "e3em");
1590         } else if (find_home_of_exe("e3vi")) {
1591                 strcpy(output, "e3vi");
1592         } else {
1593                 strcpy(output, "vi");
1594         }
1595         if (!find_home_of_exe(output)) {
1596                 log_msg(2, " (find_my_editor) --- warning - %s not found", output);
1597         }
1598         return (output);
1599 }
1600
1601
1602 /**
1603  * Install GRUB on @p bd.
1604  * @param offer_to_run_stabgrub If TRUE, then offer to hack the user's fstab for them.
1605  * @param bd The boot device where GRUB is installed.
1606  * @return 0 for success, nonzero for failure.
1607  */
1608 int run_grub(bool offer_to_run_stabgrub, char *bd)
1609 {
1610   /** malloc **/
1611         char *command;
1612         char *boot_device;
1613         char *rootdev;
1614         char *rootdrive;
1615         char *conffile;
1616         char *tmp;
1617         char *editor;
1618
1619         int res;
1620         int done;
1621
1622         malloc_string(command);
1623         malloc_string(boot_device);
1624         malloc_string(tmp);
1625         malloc_string(editor);
1626         malloc_string(rootdev);
1627         malloc_string(rootdrive);
1628         malloc_string(conffile);
1629         assert_string_is_neither_NULL_nor_zerolength(bd);
1630         strcpy(editor, "vi");           // find_my_editor() );
1631         strcpy(boot_device, bd);
1632
1633         if (!run_program_and_log_output("which grub-MR", FALSE)) {
1634                 log_msg(1, "Yay! grub-MR found...");
1635                 sprintf(command, "grub-MR %s /tmp/mountlist.txt", boot_device);
1636                 log_msg(1, "command = %s", command);
1637         } else {
1638                 sprintf(command, "chroot " MNT_RESTORING " grub-install %s",
1639                                 boot_device);
1640                 log_msg(1, "WARNING - grub-MR not found; using grub-install");
1641         }
1642         if (offer_to_run_stabgrub
1643                 && ask_me_yes_or_no("Did you change the mountlist?"))
1644                 /* interactive mode */
1645         {
1646                 mvaddstr_and_log_it(g_currentY,
1647                                                         0,
1648                                                         "Modifying fstab and grub.conf, and running GRUB...                             ");
1649                 for (done = FALSE; !done;) {
1650                         popup_and_get_string("Boot device",
1651                                                                  "Please confirm/enter the boot device. If in doubt, try /dev/hda",
1652                                                                  boot_device, MAX_STR_LEN / 4);
1653                         sprintf(command, "stabgrub-me %s", boot_device);
1654                         res = run_program_and_log_output(command, 1);
1655                         if (res) {
1656                                 popup_and_OK
1657                                         ("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.");
1658                                 newtSuspend();
1659                                 system("chroot " MNT_RESTORING);
1660                                 newtResume();
1661                                 popup_and_OK("Thank you.");
1662                         } else {
1663                                 done = TRUE;
1664                         }
1665                         popup_and_OK("You will now edit fstab and grub.conf");
1666                         if (!g_text_mode) {
1667                                 newtSuspend();
1668                         }
1669                         sprintf(tmp, "%s " MNT_RESTORING "/etc/fstab", editor);
1670                         paranoid_system(tmp);
1671                         sprintf(tmp, "%s " MNT_RESTORING "/etc/grub.conf", editor);
1672                         paranoid_system(tmp);
1673                         if (!g_text_mode) {
1674                                 newtResume();
1675                         }
1676                 }
1677         } else
1678                 /* nuke mode */
1679         {
1680                 mvaddstr_and_log_it(g_currentY,
1681                                                         0,
1682                                                         "Running GRUB...                                                 ");
1683                 iamhere(command);
1684                 res = run_program_and_log_output(command, 1);
1685                 if (res) {
1686                         popup_and_OK
1687                                 ("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.");
1688                         newtSuspend();
1689                         system("chroot " MNT_RESTORING);
1690                         newtResume();
1691                         popup_and_OK("Thank you.");
1692                 }
1693         }
1694         if (res) {
1695                 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
1696                 log_to_screen
1697                         ("GRUB ran w/error(s). See /tmp/mondo-restore.log for more info.");
1698                 log_msg(1, "Type:-");
1699                 log_msg(1, "    mount-me");
1700                 log_msg(1, "    chroot " MNT_RESTORING);
1701                 log_msg(1, "    mount /boot");
1702                 log_msg(1, "    grub-install '(hd0)'");
1703                 log_msg(1, "    exit");
1704                 log_msg(1, "    unmount-me");
1705                 log_msg(1,
1706                                 "If you're really stuck, please e-mail the mailing list.");
1707         } else {
1708                 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1709         }
1710         paranoid_free(rootdev);
1711         paranoid_free(rootdrive);
1712         paranoid_free(conffile);
1713         paranoid_free(command);
1714         paranoid_free(boot_device);
1715         paranoid_free(tmp);
1716         paranoid_free(editor);
1717
1718         return (res);
1719 }
1720
1721 /**************************************************************************
1722  *END_RUN_GRUB                                                            *
1723  **************************************************************************/
1724
1725
1726 /**
1727  * Install ELILO on the user's boot drive (determined by elilo.conf).
1728  * @param offer_to_run_stabelilo If TRUE, then offer to hack the user's fstab for them.
1729  * @return 0 for success, nonzero for failure.
1730  */
1731 int run_elilo(bool offer_to_run_stabelilo)
1732 {
1733   /** malloc **/
1734         char *command;
1735         char *tmp;
1736         char *editor;
1737
1738         int res;
1739         int done;
1740
1741         malloc_string(command);
1742         malloc_string(tmp);
1743         malloc_string(editor);
1744         strcpy(editor, find_my_editor());
1745         if (offer_to_run_stabelilo
1746                 && ask_me_yes_or_no("Did you change the mountlist?"))
1747
1748                 /* interactive mode */
1749         {
1750                 mvaddstr_and_log_it(g_currentY,
1751                                                         0,
1752                                                         "Modifying fstab and elilo.conf...                             ");
1753                 sprintf(command, "stabelilo-me");
1754                 res = run_program_and_log_output(command, 3);
1755                 if (res) {
1756                         popup_and_OK
1757                                 ("You will now edit fstab and elilo.conf, to make sure they match your new mountlist.");
1758                         for (done = FALSE; !done;) {
1759                                 if (!g_text_mode) {
1760                                         newtSuspend();
1761                                 }
1762                                 sprintf(tmp, "%s " MNT_RESTORING "/etc/fstab", editor);
1763                                 paranoid_system(tmp);
1764                                 sprintf(tmp, "%s " MNT_RESTORING "/etc/elilo.conf",
1765                                                 editor);
1766                                 paranoid_system(tmp);
1767                                 if (!g_text_mode) {
1768                                         newtResume();
1769                                 }
1770 //              newtCls();
1771                                 if (ask_me_yes_or_no("Edit them again?")) {
1772                                         continue;
1773                                 }
1774                                 done = TRUE;
1775                         }
1776                 } else {
1777                         log_to_screen("elilo.conf and fstab were modified OK");
1778                 }
1779         } else
1780                 /* nuke mode */
1781         {
1782                 res = TRUE;
1783         }
1784         paranoid_free(command);
1785         paranoid_free(tmp);
1786         paranoid_free(editor);
1787         return (res);
1788 }
1789
1790 /**************************************************************************
1791  *END_RUN_ELILO                                                            *
1792  **************************************************************************/
1793
1794
1795 /**
1796  * Install LILO on the user's boot drive (determined by /etc/lilo.conf).
1797  * @param offer_to_run_stablilo If TRUE, then offer to hack the user's fstab for them.
1798  * @return 0 for success, nonzero for failure.
1799  */
1800 int run_lilo(bool offer_to_run_stablilo)
1801 {
1802   /** malloc **/
1803         char *command;
1804         char *tmp;
1805         char *editor;
1806
1807         int res;
1808         int done;
1809         bool run_lilo_M = FALSE;
1810         malloc_string(command);
1811         malloc_string(tmp);
1812         malloc_string(editor);
1813
1814         if (!run_program_and_log_output
1815                 ("grep \"boot.*=.*/dev/md\" " MNT_RESTORING "/etc/lilo.conf", 1)) {
1816                 run_lilo_M = TRUE;
1817         }
1818
1819         strcpy(editor, find_my_editor());
1820         if (offer_to_run_stablilo
1821                 && ask_me_yes_or_no("Did you change the mountlist?"))
1822
1823                 /* interactive mode */
1824         {
1825                 mvaddstr_and_log_it(g_currentY,
1826                                                         0,
1827                                                         "Modifying fstab and lilo.conf, and running LILO...                             ");
1828                 sprintf(command, "stablilo-me");
1829                 res = run_program_and_log_output(command, 3);
1830                 if (res) {
1831                         popup_and_OK
1832                                 ("You will now edit fstab and lilo.conf, to make sure they match your new mountlist.");
1833                         for (done = FALSE; !done;) {
1834                                 if (!g_text_mode) {
1835                                         newtSuspend();
1836                                 }
1837                                 sprintf(tmp, "%s " MNT_RESTORING "/etc/fstab", editor);
1838                                 paranoid_system(tmp);
1839                                 sprintf(tmp, "%s " MNT_RESTORING "/etc/lilo.conf", editor);
1840                                 paranoid_system(tmp);
1841                                 if (!g_text_mode) {
1842                                         newtResume();
1843                                 }
1844 //              newtCls();
1845                                 if (ask_me_yes_or_no("Edit them again?")) {
1846                                         continue;
1847                                 }
1848                                 res =
1849                                         run_program_and_log_output("chroot " MNT_RESTORING
1850                                                                                            " lilo -L", 3);
1851                                 if (res) {
1852                                         res =
1853                                                 run_program_and_log_output("chroot " MNT_RESTORING
1854                                                                                                    " lilo", 3);
1855                                 }
1856                                 if (res) {
1857                                         done =
1858                                                 ask_me_yes_or_no
1859                                                 ("LILO failed. Re-edit system files?");
1860                                 } else {
1861                                         done = TRUE;
1862                                 }
1863                         }
1864                 } else {
1865                         log_to_screen("lilo.conf and fstab were modified OK");
1866                 }
1867         } else
1868                 /* nuke mode */
1869         {
1870                 mvaddstr_and_log_it(g_currentY,
1871                                                         0,
1872                                                         "Running LILO...                                                 ");
1873                 res =
1874                         run_program_and_log_output("chroot " MNT_RESTORING " lilo -L",
1875                                                                            3);
1876                 if (res) {
1877                         res =
1878                                 run_program_and_log_output("chroot " MNT_RESTORING " lilo",
1879                                                                                    3);
1880                 }
1881                 if (res) {
1882                         mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
1883                         log_to_screen
1884                                 ("Failed to re-jig fstab and/or lilo. Edit/run manually, please.");
1885                 } else {
1886                         mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1887                 }
1888         }
1889         if (run_lilo_M) {
1890                 run_program_and_log_output("chroot " MNT_RESTORING
1891                                                                    " lilo -M /dev/hda", 3);
1892                 run_program_and_log_output("chroot " MNT_RESTORING
1893                                                                    " lilo -M /dev/sda", 3);
1894         }
1895         paranoid_free(command);
1896         paranoid_free(tmp);
1897         paranoid_free(editor);
1898         return (res);
1899 }
1900
1901 /**************************************************************************
1902  *END_RUN_LILO                                                            *
1903  **************************************************************************/
1904
1905
1906 /**
1907  * Install a raw MBR onto @p bd.
1908  * @param offer_to_hack_scripts If TRUE, then offer to hack the user's fstab for them.
1909  * @param bd The device to copy the stored MBR to.
1910  * @return 0 for success, nonzero for failure.
1911  */
1912 int run_raw_mbr(bool offer_to_hack_scripts, char *bd)
1913 {
1914   /** malloc **/
1915         char *command;
1916         char *boot_device;
1917         char *tmp;
1918         char *editor;
1919         int res;
1920         int done;
1921
1922         malloc_string(command);
1923         malloc_string(boot_device);
1924         malloc_string(tmp);
1925         malloc_string(editor);
1926         assert_string_is_neither_NULL_nor_zerolength(bd);
1927
1928         strcpy(editor, find_my_editor());
1929         strcpy(boot_device, bd);
1930         sprintf(command, "raw-MR %s /tmp/mountlist.txt", boot_device);
1931         log_msg(2, "run_raw_mbr() --- command='%s'", command);
1932
1933         if (offer_to_hack_scripts
1934                 && ask_me_yes_or_no("Did you change the mountlist?"))
1935                 /* interactive mode */
1936         {
1937                 mvaddstr_and_log_it(g_currentY, 0,
1938                                                         "Modifying fstab and restoring MBR...                           ");
1939                 for (done = FALSE; !done;) {
1940                         if (!run_program_and_log_output("which vi", FALSE)) {
1941                                 popup_and_OK("You will now edit fstab");
1942                                 if (!g_text_mode) {
1943                                         newtSuspend();
1944                                 }
1945                                 sprintf(tmp, "%s " MNT_RESTORING "/etc/fstab", editor);
1946                                 paranoid_system(tmp);
1947                                 if (!g_text_mode) {
1948                                         newtResume();
1949                                 }
1950 //              newtCls();
1951                         }
1952                         popup_and_get_string("Boot device",
1953                                                                  "Please confirm/enter the boot device. If in doubt, try /dev/hda",
1954                                                                  boot_device, MAX_STR_LEN / 4);
1955                         sprintf(command, "stabraw-me %s", boot_device);
1956                         res = run_program_and_log_output(command, 3);
1957                         if (res) {
1958                                 done = ask_me_yes_or_no("Modifications failed. Re-try?");
1959                         } else {
1960                                 done = TRUE;
1961                         }
1962                 }
1963         } else
1964                 /* nuke mode */
1965         {
1966                 mvaddstr_and_log_it(g_currentY, 0,
1967                                                         "Restoring MBR...                                               ");
1968                 res = run_program_and_log_output(command, 3);
1969         }
1970         if (res) {
1971                 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
1972                 log_to_screen
1973                         ("MBR+fstab processed w/error(s). See /tmp/mondo-restore.log for more info.");
1974         } else {
1975                 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1976         }
1977         paranoid_free(command);
1978         paranoid_free(boot_device);
1979         paranoid_free(tmp);
1980         paranoid_free(editor);
1981         return (res);
1982 }
1983
1984 /**************************************************************************
1985  *END_RUN_RAW_MBR                                                         *
1986  **************************************************************************/
1987
1988
1989
1990
1991
1992 /**
1993  * Turn signal trapping on or off.
1994  * @param on If TRUE, then do full cleanup when we receive a signal; if FALSE, then
1995  * print a message and exit immediately.
1996  */
1997 void set_signals(int on)
1998 {
1999         int signals[] =
2000                 { SIGKILL, SIGPIPE, SIGTERM, SIGHUP, SIGTRAP, SIGABRT, SIGINT,
2001 SIGSTOP, 0 };
2002         int i;
2003         for (i = 0; signals[i]; i++) {
2004                 if (on) {
2005                         signal(signals[i], terminate_daemon);
2006                 } else {
2007                         signal(signals[i], termination_in_progress);
2008                 }
2009         }
2010 }
2011
2012 /**************************************************************************
2013  *END_SET_SIGNALS                                                         *
2014  **************************************************************************/
2015
2016
2017 /**
2018  * malloc() and set sensible defaults for the mondorestore filename variables.
2019  * @param bkpinfo The backup information structure. Fields used:
2020  * - @c bkpinfo->tmpdir
2021  * - @c bkpinfo->disaster_recovery
2022  */
2023 void setup_MR_global_filenames(struct s_bkpinfo *