source: MondoRescue/branches/3.3/mondo/src/mondorestore/mondo-rstr-tools.c@ 3871

Last change on this file since 3871 was 3871, checked in by Bruno Cornec, 4 months ago

Fix syntax issues

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