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

Last change on this file since 3880 was 3879, checked in by Bruno Cornec, 18 months ago

Fix all remaining compiler errors

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