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

Last change on this file since 3877 was 3876, checked in by Bruno Cornec, 2 years ago

Fix mount_media calls

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