source: branches/3.0/mondo/src/mondorestore/mondo-rstr-tools.c @ 3150

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