source: branches/2.2.8/mondo/src/mondorestore/mondo-rstr-tools.c @ 2087

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