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

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

More cleanup

  • Remove support for cd stream never really tested/useful/... (-C option removed for mondoarchive)
  • Remove dvdrecord specificities as obsolete
  • Remove types cdr and dvd to keep a single optical
  • Remove cdrw_speed param as obsolete now it's automatically detected
  • mondoarchive -c has no parameter anymore
  • Remove some useless global variables
  • Replaces some remaining /mnt/cdrom values by the define MNT_CDROM
  • Property svn:keywords set to Id
File size: 76.9 KB
Line 
1/***************************************************************************
2$Id: mondo-rstr-tools.c 3882 2024-03-09 23:46:53Z 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, "optical")) {
943 bkpinfo->backup_media_type = optical;
944 } else if (!strcmp(value, "usb")) {
945 bkpinfo->backup_media_type = usb;
946 bkpinfo->please_dont_eject = TRUE;
947 } else if (!strcmp(value, "iso")) {
948 bkpinfo->backup_media_type = iso;
949 if (am_I_in_disaster_recovery_mode()) {
950 /* Check to see if CD is already mounted before mounting it... */
951 if (!is_this_device_mounted("/dev/cdrom")) {
952 log_msg(2, "NB: CDROM device not mounted, mounting...");
953 run_program_and_log_output("mount /dev/cdrom "MNT_CDROM, 1);
954 }
955 if (does_file_exist(ARCHIVES_PATH "/filelist.0")) {
956 bkpinfo->backup_media_type = optical;
957 run_program_and_log_output("umount -d "MNT_CDROM, 1);
958 log_it("Re-jigging configuration AGAIN. Optical, not ISO.");
959 }
960 }
961 if (read_cfg_var(cfg_file, "iso-prefix", value) == 0) {
962 mr_asprintf(bkpinfo->prefix, "%s", value);
963 } else {
964 mr_asprintf(bkpinfo->prefix, "%s", STD_PREFIX);
965 }
966 log_it("Setting Prefix to %s", bkpinfo->prefix);
967 } else if ((!strcmp(value, "netfs")) || (!strcmp(value, "nfs"))) {
968 /* Stay compatible with previous versions by allowing nfs as an entry here */
969 bkpinfo->backup_media_type = netfs;
970 bkpinfo->please_dont_eject = TRUE;
971 if (read_cfg_var(cfg_file, "netfs-proto", value) == 0) {
972 mr_asprintf(bkpinfo->netfs_proto,"%s", value);
973 } else {
974 /* For compatibility, force protocol in old nfs case to be transparent */
975 mr_asprintf(bkpinfo->netfs_proto, "nfs");
976 }
977 if (read_cfg_var(cfg_file, "netfs-server-user", value) == 0) {
978 mr_asprintf(bkpinfo->netfs_user,"%s", value);
979 }
980 if (read_cfg_var(cfg_file, "iso-prefix", value) == 0) {
981 mr_asprintf(bkpinfo->prefix, "%s", value);
982 } else {
983 mr_asprintf(bkpinfo->prefix, "%s", STD_PREFIX);
984 }
985 tmp1 = call_program_and_get_last_line_of_output("cat "CMDLINE);
986 if (strstr(tmp1, "pxe")) {
987 /* We need to override prefix value in PXE mode as it's
988 * already done in start-netfs */
989 envtmp1 = getenv("imgname");
990 if (envtmp1 == NULL) {
991 fatal_error("no imgname variable in environment");
992 }
993 mr_asprintf(bkpinfo->prefix, "%s", envtmp1);
994 }
995 mr_free(tmp1);
996 } else if (!strcmp(value, "tape")) {
997 bkpinfo->backup_media_type = tape;
998 } else if (!strcmp(value, "udev")) {
999 bkpinfo->backup_media_type = udev;
1000 } else {
1001 fatal_error("UNKNOWN bkp-media-type");
1002 }
1003} else {
1004 fatal_error("backup-media-type not specified!");
1005}
1006
1007if (bkpinfo->disaster_recovery) {
1008 if (bkpinfo->backup_media_type == usb) {
1009 envtmp1 = getenv("MRUSBDEV");
1010 if (envtmp1 == NULL) {
1011 if (read_cfg_var(cfg_file, "usb-dev", value)) {
1012 fatal_error("Cannot get USB device name from cfg file");
1013 }
1014 } else {
1015 strcpy(value,envtmp1);
1016 }
1017 mr_asprintf(bkpinfo->media_device, "%s1", value);
1018 log_msg(2, "Backup medium is USB --- dev=%s", bkpinfo->media_device);
1019 } else if (bkpinfo->backup_media_type == tape || bkpinfo->backup_media_type == udev) {
1020 if (read_cfg_var(cfg_file, "media-dev", value)) {
1021 fatal_error("Cannot get tape device name from cfg file");
1022 }
1023 mr_asprintf(bkpinfo->media_device, "%s", value);
1024 read_cfg_var(cfg_file, "media-size", value);
1025 bkpinfo->media_size = atol(value);
1026 log_msg(2, "Backup medium is TAPE --- dev=%s", bkpinfo->media_device);
1027 } else {
1028 mr_asprintf(bkpinfo->media_device, "%s", "/dev/cdrom"); /* we don't really need this var */
1029 bkpinfo->media_size = 1999 * 1024; /* 650, probably, but we don't need this var anyway */
1030 log_msg(2, "Backup medium is similar to CD-R[W]");
1031 }
1032} else {
1033 log_msg(2, "Not in Disaster Recovery Mode. No need to derive device name from config file.");
1034}
1035
1036read_cfg_var(cfg_file, "use-star", value);
1037if (strstr(value, "yes")) {
1038 bkpinfo->use_star = TRUE;
1039 log_msg(1, "Goody! ... bkpinfo->use_star is now true.");
1040}
1041
1042read_cfg_var(cfg_file, "obdr", value);
1043if (strstr(value, "TRUE")) {
1044 bkpinfo->use_obdr = TRUE;
1045 log_msg(1, "OBDR mode activated");
1046}
1047
1048read_cfg_var(cfg_file, "acl", value);
1049if (strstr(value, "TRUE")) {
1050 if ((tmp = find_home_of_exe("setfacl")) == NULL) {
1051 log_msg(1, "Unable to restore ACLs as no setfacl found");
1052 } else {
1053 mr_asprintf(g_getfacl,"%s", tmp);
1054 log_msg(1, "We will restore ACLs");
1055 }
1056 mr_free(tmp);
1057}
1058read_cfg_var(cfg_file, "xattr", value);
1059if (strstr(value, "TRUE")) {
1060 if ((tmp = find_home_of_exe("setfattr")) == NULL) {
1061 log_msg(1, "Unable to restore XATTRs as no setfattr found");
1062 } else {
1063 mr_asprintf(g_getfattr,"%s", tmp);
1064 log_msg(1, "We will restore XATTRs");
1065 }
1066 mr_free(tmp);
1067}
1068
1069if (0 == read_cfg_var(cfg_file, "internal-tape-block-size", value)) {
1070 bkpinfo->internal_tape_block_size = atol(value);
1071} else {
1072 bkpinfo->internal_tape_block_size = DEFAULT_INTERNAL_TAPE_BLOCK_SIZE;
1073}
1074log_msg(1, "Internal tape block size set to %ld", bkpinfo->internal_tape_block_size);
1075
1076read_cfg_var(cfg_file, "use-lzma", value);
1077if (strstr(value, "yes")) {
1078 bkpinfo->use_lzma = TRUE;
1079 bkpinfo->use_lzo = FALSE;
1080 bkpinfo->use_gzip = FALSE;
1081 mr_asprintf(bkpinfo->zip_exe, "%s", "lzma");
1082 mr_asprintf(bkpinfo->zip_suffix, "%s", "lzma");
1083}
1084
1085read_cfg_var(cfg_file, "use-lzo", value);
1086if (strstr(value, "yes")) {
1087 bkpinfo->use_lzma = FALSE;
1088 bkpinfo->use_lzo = TRUE;
1089 bkpinfo->use_gzip = FALSE;
1090 mr_asprintf(bkpinfo->zip_exe, "%s", "lzop");
1091 mr_asprintf(bkpinfo->zip_suffix, "%s", "lzo");
1092}
1093
1094read_cfg_var(cfg_file, "use-gzip", value);
1095if (strstr(value, "yes")) {
1096 bkpinfo->use_lzma = FALSE;
1097 bkpinfo->use_lzo = FALSE;
1098 bkpinfo->use_gzip = TRUE;
1099 mr_asprintf(bkpinfo->zip_exe, "%s", "gzip");
1100 mr_asprintf(bkpinfo->zip_suffix, "%s", "gz");
1101}
1102
1103read_cfg_var(cfg_file, "use-comp", value);
1104if (strstr(value, "yes")) {
1105 bkpinfo->use_lzma = FALSE;
1106 bkpinfo->use_lzo = FALSE;
1107 bkpinfo->use_gzip = FALSE;
1108 mr_asprintf(bkpinfo->zip_exe, "%s", "xz");
1109 mr_asprintf(bkpinfo->zip_suffix, "%s", "xz");
1110}
1111
1112value[0] = '\0';
1113read_cfg_var(cfg_file, "differential", value);
1114if (!strcmp(value, "yes") || !strcmp(value, "1")) {
1115 bkpinfo->differential = TRUE;
1116}
1117log_msg(2, "differential var = '%s'", value);
1118if (bkpinfo->differential) {
1119 log_msg(2, "THIS IS A DIFFERENTIAL BACKUP");
1120} else {
1121 log_msg(2, "This is a regular (full) backup");
1122}
1123
1124read_cfg_var(cfg_file, "please-dont-eject", value);
1125tmp1 = call_program_and_get_last_line_of_output("cat "CMDLINE);
1126if (value[0] || strstr(tmp1, "donteject")) {
1127 bkpinfo->please_dont_eject = TRUE;
1128 log_msg(2, "Ok, I shan't eject when restoring! Groovy.");
1129}
1130mr_free(tmp1);
1131
1132/* TODO: Read here the boot_* variables */
1133read_cfg_var(cfg_file, "boot-type", value);
1134if (!strcmp(value, "BIOS")) {
1135 bkpinfo->boot_type = BIOS;
1136} else if (!strcmp(value, "EFI")) {
1137 bkpinfo->boot_type = EFI;
1138} else if (!strcmp(value, "UEFI")) {
1139 bkpinfo->boot_type = UEFI;
1140} else {
1141 log_msg(1, "No known boot type found");
1142}
1143log_msg(1, "Found %s boot type",value);
1144
1145if (bkpinfo->backup_media_type == netfs) {
1146 if (!cfgf) {
1147 if (bkpinfo->netfs_mount) {
1148 log_msg(2, "netfs_mount remains %s", bkpinfo->netfs_mount);
1149 }
1150 if (bkpinfo->netfs_remote_dir != NULL) {
1151 log_msg(2, "netfs_remote_dir remains %s", bkpinfo->netfs_remote_dir);
1152 }
1153 log_msg(2, "...cos it wouldn't make sense to abandon the values that GOT ME to this config file in the first place");
1154 } else {
1155 read_cfg_var(cfg_file, "netfs-server-mount", value);
1156 mr_free(bkpinfo->netfs_mount);
1157 mr_asprintf(bkpinfo->netfs_mount, "%s", value);
1158 if (bkpinfo->netfs_mount != NULL) {
1159 log_msg(2, "netfs_mount is %s", bkpinfo->netfs_mount);
1160 }
1161
1162 read_cfg_var(cfg_file, "iso-dir", value);
1163 mr_free(bkpinfo->netfs_remote_dir);
1164 mr_asprintf(bkpinfo->netfs_remote_dir, "%s", value);
1165 if (bkpinfo->netfs_remote_dir != NULL) {
1166 log_msg(2, "netfs_remote_dir is %s", bkpinfo->netfs_remote_dir);
1167 }
1168
1169 if (bkpinfo->netfs_proto != NULL) {
1170 log_msg(2, "netfs_proto is %s", bkpinfo->netfs_proto);
1171 }
1172 if (bkpinfo->netfs_user != NULL) {
1173 log_msg(2, "netfs_user is %s", bkpinfo->netfs_user);
1174 }
1175 }
1176 tmp1 = call_program_and_get_last_line_of_output("cat "CMDLINE);
1177 if (strstr(tmp1, "pxe")) {
1178 /* We need to override values in PXE mode as it's
1179 * already done in start-netfs */
1180 envtmp1 = getenv("netfsmount");
1181 if (envtmp1 == NULL) {
1182 fatal_error("no netfsmount variable in environment");
1183 }
1184 envtmp2 = getenv("dirimg");
1185 if (envtmp2 == NULL) {
1186 fatal_error("no dirimg variable in environment");
1187 }
1188 mr_free(bkpinfo->netfs_mount);
1189 mr_asprintf(bkpinfo->netfs_mount, "%s", envtmp1);
1190
1191 mr_free(bkpinfo->netfs_remote_dir);
1192 mr_asprintf(bkpinfo->netfs_remote_dir, "%s", envtmp2);
1193 }
1194 mr_free(tmp1);
1195} else if (bkpinfo->backup_media_type == iso) {
1196 /* Patch by Conor Daly 23-june-2004
1197 * to correctly mount iso-dev and set a sensible
1198 * isodir in disaster recovery mode
1199 */
1200 mr_asprintf(old_isodir, "%s", bkpinfo->isodir);
1201 if (read_cfg_var(cfg_file, "iso-mnt", iso_mnt) != 0) {
1202 iso_mnt[0] = '\0';
1203 }
1204 if (read_cfg_var(cfg_file, "iso-dir", iso_path) != 0) {
1205 iso_path[0] = '\0';
1206 }
1207 mr_asprintf(bkpinfo->isodir, "%s%s", iso_mnt, iso_path);
1208 if (!strcmp(bkpinfo->isodir,"")) {
1209 mr_asprintf(bkpinfo->isodir, "%s", old_isodir);
1210 }
1211
1212 if ((!bkpinfo->disaster_recovery) || (g_isodir_device[0] != '\0')) {
1213 if (strcmp(old_isodir, bkpinfo->isodir)) {
1214 log_it("user nominated isodir %s differs from archive, keeping user's choice: %s\n", bkpinfo->isodir, old_isodir );
1215 mr_asprintf(bkpinfo->isodir, "%s", old_isodir);
1216 }
1217 }
1218 mr_free(old_isodir);
1219
1220 if (g_isodir_device[0] == '\0') {
1221 /* We don't want to loose our conf made earlier in iso_fiddly_bits
1222 * so go here only if job not done already */
1223 read_cfg_var(cfg_file, "iso-dev", g_isodir_device);
1224 }
1225
1226 log_msg(2, "isodir=%s; iso-dev=%s", bkpinfo->isodir, g_isodir_device);
1227
1228 if (bkpinfo->disaster_recovery) {
1229 if (is_this_device_mounted(g_isodir_device)) {
1230 log_msg(2, "NB: isodir is already mounted");
1231 /* Find out where it's mounted */
1232 mr_asprintf(command, "mount | grep -E '^%s' | tail -n1 | cut -d' ' -f3", g_isodir_device);
1233 log_it("command = %s", command);
1234 tmp1 = call_program_and_get_last_line_of_output(command);
1235 log_it("res of it = %s", tmp1);
1236 sprintf(iso_mnt, "%s", tmp1);
1237 mr_free(command);
1238 mr_free(tmp1);
1239 } else {
1240 sprintf(iso_mnt, "/tmp/isodir");
1241 mr_asprintf(tmp1, "mkdir -p %s", iso_mnt);
1242 run_program_and_log_output(tmp1, 5);
1243 mr_free(tmp1);
1244
1245 mr_asprintf(tmp1, "mount %s %s", g_isodir_device, iso_mnt);
1246 if (run_program_and_log_output(tmp1, 3)) {
1247 log_msg(1, "Unable to mount isodir. Perhaps this is really an optical based backup?");
1248 bkpinfo->backup_media_type = optical;
1249 mr_asprintf(bkpinfo->media_device, "%s", "/dev/cdrom"); /* superfluous */
1250 mr_free(bkpinfo->isodir);
1251 iso_mnt[0] = iso_path[0] = '\0';
1252 if (mount_media(MNT_CDROM)) {
1253 mr_free(tmp1);
1254 fatal_error("Unable to mount isodir. Failed to mount CD-ROM as well.");
1255 } else {
1256 log_msg(1, "You backed up to disk, then burned some CDs.");
1257 }
1258 }
1259 mr_free(tmp1);
1260 }
1261 /* bkpinfo->isodir should now be the true path to prefix-1.iso etc... */
1262 /* except when already done in iso_fiddly_bits */
1263 if ((bkpinfo->backup_media_type == iso) && (g_isodir_device[0] == '\0')) {
1264 mr_asprintf(bkpinfo->isodir, "%s%s", iso_mnt, iso_path);
1265 }
1266 }
1267}
1268mr_free(cfg_file);
1269
1270if (media_specified_by_user != none) {
1271 if (g_restoring_live_from_cd) {
1272 if (bkpinfo->backup_media_type != media_specified_by_user) {
1273 log_msg(2, "bkpinfo->backup_media_type != media_specified_by_user, so I'd better ask :)");
1274 interactively_obtain_media_parameters_from_user(FALSE);
1275 media_specified_by_user = bkpinfo->backup_media_type;
1276 get_cfg_file_from_archive();
1277 }
1278 }
1279 bkpinfo->backup_media_type = media_specified_by_user;
1280}
1281g_backup_media_type = bkpinfo->backup_media_type;
1282return (0);
1283
1284}
1285
1286/**************************************************************************
1287*END_READ_CFG_FILE_INTO_BKPINFO *
1288**************************************************************************/
1289
1290
1291
1292
1293/**
1294 * Allow the user to edit the filelist and biggielist.
1295 * The filelist is unlinked after it is read.
1296 * @param bkpinfo The backup information structure. Fields used:
1297 * - @c bkpinfo->backup_media_type
1298 * - @c bkpinfo->isodir
1299 * - @c bkpinfo->media_device
1300 * - @c bkpinfo->tmpdir
1301 * @return The filelist structure containing the information read from disk.
1302 */
1303struct
1304s_node *process_filelist_and_biggielist()
1305{
1306struct s_node *filelist;
1307
1308char *command = NULL;
1309char *old_pwd = NULL;
1310char *q;
1311int res = 0;
1312pid_t pid;
1313bool extract_mountlist_stub = FALSE;
1314
1315assert(bkpinfo != NULL);
1316
1317/* If those files already exist, do not overwrite them later on */
1318if (does_file_exist("/"MOUNTLIST_FNAME_STUB)) {
1319 extract_mountlist_stub = FALSE;
1320} else {
1321 extract_mountlist_stub = TRUE;
1322}
1323
1324if (does_file_exist(g_filelist_full)
1325&& does_file_exist(g_biggielist_txt)) {
1326 log_msg(1, "%s exists", g_filelist_full);
1327 log_msg(1, "%s exists", g_biggielist_txt);
1328 log_msg(2,
1329 "Filelist and biggielist already recovered from media. Yay!");
1330} else {
1331 old_pwd = mr_getcwd();
1332 if (chdir(bkpinfo->tmpdir)) {
1333 // FIXME
1334 }
1335 log_msg(1, "chdir(%s)", bkpinfo->tmpdir);
1336 log_to_screen("Extracting filelist and biggielist from media...");
1337 unlink("/tmp/filelist.full");
1338 unlink(FILELIST_FULL_STUB);
1339 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1340 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);
1341 log_msg(1, "tarcommand = %s", command);
1342 run_program_and_log_output(command, 1);
1343 mr_free(command);
1344
1345 if (!does_file_exist(FILELIST_FULL_STUB)) {
1346 /* Doing that allow us to remain compatible with pre-2.2.5 versions */
1347 log_msg(2, "pre-2.2.4 compatible mode on");
1348 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);
1349 log_msg(1, "tarcommand = %s", command);
1350 run_program_and_log_output(command, 1);
1351 mr_free(command);
1352 }
1353 } else {
1354 log_msg(2, "Calling insist_on_this_cd_number; bkpinfo->isodir=%s", bkpinfo->isodir);
1355 insist_on_this_cd_number(1);
1356 log_msg(2, "Back from iotcn");
1357 run_program_and_log_output("mount", 1);
1358 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);
1359
1360 log_msg(1, "tarcommand = %s", command);
1361 run_program_and_log_output(command, 1);
1362 mr_free(command);
1363
1364 if (!does_file_exist(FILELIST_FULL_STUB)) {
1365 /* Doing that allow us to remain compatible with pre-2.2.5 versions */
1366 log_msg(2, "pre-2.2.4 compatible mode on");
1367 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);
1368
1369 log_msg(1, "tarcommand = %s", command);
1370 run_program_and_log_output(command, 1);
1371 mr_free(command);
1372 }
1373 if (!does_file_exist(BIGGIELIST_TXT_STUB)) {
1374 fatal_error("all.tar.gz did not include " BIGGIELIST_TXT_STUB);
1375 }
1376 if (!does_file_exist(FILELIST_FULL_STUB)) {
1377 fatal_error("all.tar.gz did not include " FILELIST_FULL_STUB);
1378 }
1379 }
1380 mr_asprintf(command, "cp -f %s %s", MONDO_CFG_FILE_STUB, g_mondo_cfg_file);
1381 run_program_and_log_output(command, FALSE);
1382 mr_free(command);
1383
1384 mr_asprintf(command, "cp -f %s/%s %s", bkpinfo->tmpdir, BIGGIELIST_TXT_STUB, g_biggielist_txt);
1385 log_msg(1, "command = %s", command);
1386 paranoid_system(command);
1387 mr_free(command);
1388
1389 mr_asprintf(command, "ln -sf %s/%s %s", bkpinfo->tmpdir, FILELIST_FULL_STUB, g_filelist_full);
1390 log_msg(1, "command = %s", command);
1391 paranoid_system(command);
1392 mr_free(command);
1393 }
1394
1395 if (am_I_in_disaster_recovery_mode()
1396 &&
1397 /* If it was there, do not overwrite it */
1398 (extract_mountlist_stub)
1399 &&
1400 ask_me_yes_or_no("Do you want to retrieve the mountlist as well?"))
1401 {
1402 mr_asprintf(command, "ln -sf %s/%s /tmp", MOUNTLIST_FNAME_STUB, bkpinfo->tmpdir);
1403 paranoid_system(command);
1404 mr_free(command);
1405 }
1406
1407 if (chdir(old_pwd)) {
1408 // FIXME
1409 }
1410 mr_free(old_pwd);
1411
1412 if (!does_file_exist(g_biggielist_txt)) {
1413 log_msg(1, "Warning - %s not found", g_biggielist_txt);
1414 }
1415 if (!does_file_exist(g_filelist_full)) {
1416 log_msg(1, "Warning - %s does not exist", g_filelist_full);
1417 }
1418
1419 log_msg(2, "Forking");
1420 pid = fork();
1421 switch (pid) {
1422 case -1:
1423 fatal_error("Forking error");
1424 break;
1425
1426 case 0:
1427 log_to_screen("Pre-processing filelist");
1428 if (!does_file_exist(g_biggielist_txt)) {
1429 mr_asprintf(command, "echo -n > %s", g_biggielist_txt);
1430 paranoid_system(command);
1431 mr_free(command);
1432 }
1433 mr_asprintf(command, "grep -E '^/dev/.*' %s > %s", g_biggielist_txt, g_filelist_imagedevs);
1434 paranoid_system(command);
1435 mr_free(command);
1436 exit(0);
1437 break;
1438
1439 default:
1440 open_evalcall_form("Pre-processing filelist");
1441 while (!waitpid(pid, (int *) 0, WNOHANG)) {
1442 usleep(100000);
1443 update_evalcall_form(0);
1444 }
1445 }
1446 close_evalcall_form();
1447
1448 log_msg(3, "loading filelist");
1449 filelist = load_filelist(g_filelist_full);
1450 log_msg(3, "deleting original filelist");
1451 unlink(g_filelist_full);
1452 if (g_text_mode) {
1453 q = NULL;
1454 while (q == NULL) {
1455 printf("Restore which directory? --> ");
1456 mr_getline(q, stdin);
1457 }
1458 toggle_path_selection(filelist, q, TRUE);
1459 if (strlen(q) == 0) {
1460 res = 1;
1461 } else {
1462 res = 0;
1463 }
1464 mr_free(q);
1465 } else {
1466 res = edit_filelist(filelist);
1467 }
1468 if (res) {
1469 log_msg(2, "User hit 'cancel'. Freeing filelist and aborting.");
1470 free_filelist(filelist);
1471 return (NULL);
1472 }
1473 ask_about_these_imagedevs(g_filelist_imagedevs, g_imagedevs_restthese);
1474 close_evalcall_form();
1475
1476 // NB: It's not necessary to add g_biggielist_txt to the filelist.full
1477 // file. The filelist.full file already contains the filename of EVERY
1478 // file backed up - regular and biggie files.
1479
1480 // However, we do want to make sure the imagedevs selected by the user
1481 // are flagged for restoring.
1482 if (length_of_file(g_imagedevs_restthese) > 2) {
1483 add_list_of_files_to_filelist(filelist, g_imagedevs_restthese,
1484 TRUE);
1485 }
1486
1487 return (filelist);
1488}
1489
1490/**************************************************************************
1491 *END_ PROCESS_FILELIST_AND_BIGGIELIST *
1492 **************************************************************************/
1493
1494
1495
1496
1497/**
1498 * Make a backup copy of <tt>path_root</tt>/<tt>filename</tt>.
1499 * The backup filename is the filename of the original with ".pristine" added.
1500 * @param path_root The place where the filesystem is mounted (e.g. MNT_RESTORING).
1501 * @param filename The filename (absolute path) within @p path_root.
1502 * @return 0 for success, nonzero for failure.
1503 */
1504int backup_crucial_file(char *path_root, char *filename)
1505{
1506 char *tmp = NULL;
1507 char *command = NULL;
1508 int res;
1509
1510 assert(path_root != NULL);
1511 assert_string_is_neither_NULL_nor_zerolength(filename);
1512
1513 mr_asprintf(tmp, "%s/%s", path_root, filename);
1514 mr_asprintf(command, "cp -f %s %s.pristine", tmp, tmp);
1515 mr_free(tmp);
1516
1517 res = run_program_and_log_output(command, 5);
1518 mr_free(command);
1519 return (res);
1520}
1521
1522void offer_to_make_initrd() {
1523
1524if (bkpinfo->restore_mode != nuke) {
1525 if (ask_me_yes_or_no
1526 ("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 ?")) {
1527 log_msg(1,"Launching shell for manual initrd recreation");
1528 if (does_file_exist(MNT_RESTORING"/etc/arch-release")) {
1529 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");
1530 } else {
1531 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.");
1532 }
1533 mvaddstr_and_log_it(g_currentY, 0, "Modifying initrd...");
1534 if (!g_text_mode) {
1535 newtSuspend();
1536 }
1537 paranoid_system("chroot " MNT_RESTORING);
1538 if (!g_text_mode) {
1539 newtResume();
1540 }
1541 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1542 } else {
1543 return;
1544 }
1545} else {
1546 log_to_screen("Non-interactive mode: no way to give you the keyboard so that you re-generate your initrd. Hope it's OK");
1547 log_msg(1,"Non-interactive mode: no way to give you the keyboard so that you re-generate your initrd. Hope it's OK");
1548}
1549}
1550
1551
1552/**
1553 * Attempt to find the user's editor.
1554 * @return The editor found ("vi" if none could be found).
1555 * @note The returned string points to an allocated storage that needs to be freed by caller
1556 */
1557char *find_my_editor(void) {
1558
1559 char *output = NULL ;
1560
1561 if ((output = find_home_of_exe("pico")) == NULL) {
1562 if ((output = find_home_of_exe("nano")) == NULL) {
1563 if ((output = find_home_of_exe("e3em")) == NULL) {
1564 if ((output = find_home_of_exe("e3vi")) == NULL) {
1565 if ((output = find_home_of_exe("vim")) == NULL) {
1566 output = find_home_of_exe("vi");
1567 }
1568 }
1569 }
1570 }
1571 }
1572 if (output == NULL) {
1573 log_msg(2, " (find_my_editor) --- warning - no editor found, even vi");
1574 }
1575 return (output);
1576}
1577
1578
1579
1580
1581/**
1582 * Install LILO on the user's boot drive (determined by /etc/lilo.conf).
1583 * @param offer_to_run_stablilo If TRUE, then offer to hack the user's fstab for them.
1584 * @return 0 for success, nonzero for failure.
1585 */
1586int run_lilo(bool offer_to_run_stablilo)
1587{
1588 /** malloc **/
1589 char *command = NULL;
1590 char *tmp = NULL;
1591 char *editor = NULL;
1592
1593 int res;
1594 int done;
1595 bool run_lilo_M = FALSE;
1596
1597 if (!run_program_and_log_output
1598 ("grep \"boot.*=.*/dev/md\" " MNT_RESTORING "/etc/lilo.conf", 1)) {
1599 run_lilo_M = TRUE;
1600 }
1601
1602 if (offer_to_run_stablilo
1603 && ask_me_yes_or_no("Did you change the mountlist or cloned the system ?"))
1604
1605 /* interactive mode */
1606 {
1607 mvaddstr_and_log_it(g_currentY,
1608 0,
1609 "Modifying fstab and lilo.conf, and running LILO... ");
1610 mr_asprintf(command, "mr-stablilo-me");
1611 res = run_program_and_log_output(command, 3);
1612 mr_free(command);
1613
1614 if (res) {
1615 popup_and_OK
1616 ("You will now edit fstab and lilo.conf, to make sure they match your new mountlist.");
1617 for (done = FALSE; !done;) {
1618 editor = find_my_editor();
1619 if (editor == NULL) {
1620 popup_and_OK("No editor found. You won't be able to edit conf files");
1621 } else {
1622 if (!g_text_mode) {
1623 newtSuspend();
1624 }
1625 mr_asprintf(tmp, "%s " MNT_RESTORING "/etc/fstab", editor);
1626 paranoid_system(tmp);
1627 mr_free(tmp);
1628
1629 mr_asprintf(tmp, "%s " MNT_RESTORING "/etc/lilo.conf", editor);
1630 paranoid_system(tmp);
1631 mr_free(tmp);
1632 mr_free(editor);
1633
1634 if (!g_text_mode) {
1635 newtResume();
1636 }
1637// newtCls();
1638 if (ask_me_yes_or_no("Edit them again?")) {
1639 continue;
1640 }
1641 }
1642 mr_free(editor);
1643
1644 res = run_program_and_log_output("chroot " MNT_RESTORING " lilo -L", 3);
1645 if (res) {
1646 res = run_program_and_log_output("chroot " MNT_RESTORING " lilo", 3);
1647 }
1648 if (res) {
1649 done = ask_me_yes_or_no("LILO failed. Re-edit system files?");
1650 } else {
1651 done = TRUE;
1652 }
1653 }
1654 } else {
1655 log_to_screen("lilo.conf and fstab were modified OK");
1656 }
1657 } else
1658 /* nuke mode */
1659 {
1660 mvaddstr_and_log_it(g_currentY,
1661 0,
1662 "Running LILO... ");
1663 res =
1664 run_program_and_log_output("chroot " MNT_RESTORING " lilo -L",
1665 3);
1666 if (res) {
1667 res =
1668 run_program_and_log_output("chroot " MNT_RESTORING " lilo",
1669 3);
1670 }
1671 if (res) {
1672 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
1673 log_to_screen
1674 ("Failed to re-jig fstab and/or lilo. Edit/run manually, please.");
1675 } else {
1676 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1677 }
1678 }
1679 if (run_lilo_M) {
1680 run_program_and_log_output("chroot " MNT_RESTORING
1681 " lilo -M /dev/hda", 3);
1682 run_program_and_log_output("chroot " MNT_RESTORING
1683 " lilo -M /dev/sda", 3);
1684 }
1685 return (res);
1686}
1687
1688/**************************************************************************
1689 *END_RUN_LILO *
1690 **************************************************************************/
1691
1692
1693/**
1694 * Install a raw MBR onto @p bd.
1695 * @param offer_to_hack_scripts If TRUE, then offer to hack the user's fstab for them.
1696 * @param bd The device to copy the stored MBR to.
1697 * @return 0 for success, nonzero for failure.
1698 */
1699int run_raw_mbr(bool offer_to_hack_scripts, char *bd)
1700{
1701 /** malloc **/
1702 char *command = NULL;
1703 char *boot_device = NULL;
1704 char *tmp = NULL;
1705 char *editor;
1706 int res;
1707 int done;
1708
1709 malloc_string(boot_device);
1710 assert_string_is_neither_NULL_nor_zerolength(bd);
1711
1712 strcpy(boot_device, bd);
1713
1714 if (offer_to_hack_scripts
1715 && ask_me_yes_or_no("Did you change the mountlist or cloned the system ?")) {
1716 /* interactive mode */
1717 mvaddstr_and_log_it(g_currentY, 0, "Modifying fstab and restoring MBR... ");
1718 for (done = FALSE; !done;) {
1719 popup_and_OK("You will now edit fstab");
1720 editor = find_my_editor();
1721 if (editor == NULL) {
1722 popup_and_OK("No editor found. You won't be able to edit conf files");
1723 } else {
1724 if (!g_text_mode) {
1725 newtSuspend();
1726 }
1727 mr_asprintf(tmp, "%s " MNT_RESTORING "/etc/fstab", editor);
1728
1729 paranoid_system(tmp);
1730 mr_free(tmp);
1731 if (!g_text_mode) {
1732 newtResume();
1733 }
1734 }
1735 mr_free(editor);
1736
1737 popup_and_get_string("Boot device", "Please confirm/enter the boot device. If in doubt, try /dev/hda", boot_device, MAX_STR_LEN / 4);
1738 mr_asprintf(command, "mr-stabraw-me %s", boot_device);
1739 res = run_program_and_log_output(command, 3);
1740 mr_free(command);
1741
1742 if (res) {
1743 done = ask_me_yes_or_no("Modifications failed. Re-try?");
1744 } else {
1745 done = TRUE;
1746 }
1747 }
1748 } else {
1749 /* nuke mode */
1750 mr_asprintf(command, "mr-raw %s /"MOUNTLIST_FNAME_STUB, boot_device);
1751 log_msg(2, "run_raw_mbr() --- command='%s'", command);
1752
1753 mvaddstr_and_log_it(g_currentY, 0, "Restoring MBR... ");
1754 res = run_program_and_log_output(command, 3);
1755 mr_free(command);
1756 }
1757 if (res) {
1758 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
1759 log_to_screen("MBR+fstab processed w/error(s). See %s for more info.", MONDO_LOGFILE);
1760 } else {
1761 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1762 }
1763 paranoid_free(boot_device);
1764 return (res);
1765}
1766
1767/**************************************************************************
1768 *END_RUN_RAW_MBR *
1769 **************************************************************************/
1770
1771
1772/**
1773 * Install GRUB on @p bd.
1774 * @param offer_to_run_stabgrub If TRUE, then offer to hack the user's fstab for them.
1775 * @param bd The boot device where GRUB is installed.
1776 * @return 0 for success, nonzero for failure.
1777 */
1778int run_grub(bool offer_to_run_stabgrub, char *bd)
1779{
1780 /** malloc **/
1781 char *command = NULL;
1782 char *boot_device = NULL;
1783 char *tmp = NULL;
1784 char *editor = NULL;
1785
1786 bool done;
1787 int res = 0; /* FALSE */
1788 bool mntlistchg = FALSE;
1789 FILE *fin = NULL;
1790
1791 malloc_string(boot_device);
1792
1793 assert_string_is_neither_NULL_nor_zerolength(bd);
1794 strcpy(boot_device, bd);
1795
1796 if (offer_to_run_stabgrub && ask_me_yes_or_no("Did you change the mountlist or cloned the system ?")) {
1797 /* interactive mode */
1798 mvaddstr_and_log_it(g_currentY, 0, "Modifying fstab, mtab, device.map and menu.lst/grub.cfg, and running GRUB...");
1799 /* Did we changed the mountlist ? If yes, then force editing conf files */
1800 if ((fin = fopen(MONDO_MNTLISTCHG, "r")) != NULL) {
1801 mntlistchg = TRUE;
1802 }
1803 for (done = FALSE; !done;) {
1804 popup_and_get_string("Boot device", "Please confirm/enter the boot device. If in doubt, try /dev/sda", boot_device, MAX_STR_LEN / 4);
1805 /* Only try to adapt grub here first if the mountlist wasn't changed before */
1806 if (! mntlistchg) {
1807 mr_asprintf(command, "mr-stabgrub-me %s", boot_device);
1808 res = run_program_and_log_output(command, 1);
1809 mr_free(command);
1810 }
1811
1812 if ((res) || (mntlistchg)) {
1813 if (res) {
1814 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");
1815 } else {
1816 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");
1817 }
1818 editor = find_my_editor();
1819 if (editor == NULL) {
1820 popup_and_OK("No editor found. You won't be able to edit conf files");
1821 } else {
1822 if (!g_text_mode) {
1823 newtSuspend();
1824 }
1825 mr_asprintf(tmp, "chroot %s %s /etc/fstab", MNT_RESTORING, editor);
1826 paranoid_system(tmp);
1827 mr_free(tmp);
1828
1829 mr_asprintf(tmp, "chroot %s %s /etc/mtab", MNT_RESTORING, editor);
1830 paranoid_system(tmp);
1831 mr_free(tmp);
1832
1833 if (does_file_exist(MNT_RESTORING"/boot/grub/menu.lst")) {
1834 mr_asprintf(tmp, "chroot %s %s /boot/grub/menu.lst", MNT_RESTORING, editor);
1835 } else if (does_file_exist(MNT_RESTORING"/boot/grub/grub.cfg")) {
1836 mr_asprintf(tmp, "chroot %s %s /boot/grub/grub.cfg", MNT_RESTORING, editor);
1837 } else if (does_file_exist(MNT_RESTORING"/boot/grub2/grub.cfg")) {
1838 mr_asprintf(tmp, "chroot %s %s /boot/grub2/grub.cfg", MNT_RESTORING, editor);
1839 }
1840 paranoid_system(tmp);
1841 mr_free(tmp);
1842
1843 if (does_file_exist(MNT_RESTORING"/boot/grub/device.map")) {
1844 mr_asprintf(tmp, "chroot %s %s /boot/grub/device.map", MNT_RESTORING, editor);
1845 } else if (does_file_exist(MNT_RESTORING"/boot/grub2/device.map")) {
1846 mr_asprintf(tmp, "chroot %s %s /boot/grub2/device.map", MNT_RESTORING, editor);
1847 }
1848 paranoid_system(tmp);
1849 mr_free(tmp);
1850 if (!g_text_mode) {
1851 newtResume();
1852 }
1853 }
1854 mr_free(editor);
1855
1856 mr_asprintf(command, "mr-stabgrub-me %s", boot_device);
1857 res = run_program_and_log_output(command, 1);
1858 mr_free(command);
1859
1860 if (res) {
1861 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.");
1862 newtSuspend();
1863 paranoid_system("chroot " MNT_RESTORING);
1864 newtResume();
1865 } else {
1866 popup_and_OK("GRUB is now installed correctly");
1867 done = TRUE;
1868 }
1869 } else {
1870 done = TRUE;
1871 }
1872 }
1873 } else {
1874 /* nuke mode */
1875 if (!run_program_and_log_output("which mr-grub", FALSE)) {
1876 log_msg(1, "Yay! mr-grub found...");
1877 mr_asprintf(command, "mr-grub %s /"MOUNTLIST_FNAME_STUB, boot_device);
1878 log_msg(1, "command = %s", command);
1879 } else {
1880 // Or grub2-install
1881 mr_asprintf(command, "chroot " MNT_RESTORING " grub-install %s", boot_device);
1882 log_msg(1, "WARNING - mr-grub not found; using grub-install");
1883 }
1884 mvaddstr_and_log_it(g_currentY, 0, "Running GRUB... ");
1885 log_it("%s",command);
1886 res = run_program_and_log_output(command, 1);
1887 mr_free(command);
1888
1889 if (res) {
1890 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.");
1891 newtSuspend();
1892 paranoid_system("chroot " MNT_RESTORING);
1893 newtResume();
1894 }
1895 }
1896 if (res) {
1897 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
1898 log_to_screen
1899 ("GRUB ran w/error(s). See %s for more info.", MONDO_LOGFILE);
1900 log_msg(1, "Type:-");
1901 log_msg(1, " mr-mount-me");
1902 log_msg(1, " chroot " MNT_RESTORING);
1903 log_msg(1, " mount /boot if needed");
1904 log_msg(1, " grub-install '(hd0)'");
1905 log_msg(1, " exit");
1906 log_msg(1, " mr-unmount-me");
1907 log_msg(1,
1908 "If you're really stuck, please e-mail the mailing list.");
1909 } else {
1910 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1911 }
1912 paranoid_free(boot_device);
1913
1914 return (res);
1915}
1916
1917/**************************************************************************
1918 *END_RUN_GRUB *
1919 **************************************************************************/
1920
1921
1922/**
1923 * Install ELILO on the user's boot drive (determined by elilo.conf).
1924 * @param offer_to_run_stabelilo If TRUE, then offer to hack the user's fstab for them.
1925 * @return 0 for success, nonzero for failure.
1926 */
1927int run_elilo(bool offer_to_run_stabelilo)
1928{
1929 /** malloc **/
1930 char *command = NULL;
1931 char *tmp = NULL;
1932 char *editor = NULL;
1933
1934 int res;
1935 int done;
1936
1937 if (offer_to_run_stabelilo
1938 && ask_me_yes_or_no("Did you change the mountlist or cloned the system ?"))
1939
1940 /* interactive mode */
1941 {
1942 mvaddstr_and_log_it(g_currentY,
1943 0,
1944 "Modifying fstab and elilo.conf... ");
1945 mr_asprintf(command, "mr-stabelilo-me");
1946 res = run_program_and_log_output(command, 3);
1947 mr_free(command);
1948
1949 if (res) {
1950 popup_and_OK("You will now edit fstab and elilo.conf, to make sure they match your new mountlist.");
1951 for (done = FALSE; !done;) {
1952 editor = find_my_editor();
1953 if (editor == NULL) {
1954 popup_and_OK("No editor found. You won't be able to edit conf files");
1955 done = TRUE;
1956 } else {
1957
1958 if (!g_text_mode) {
1959 newtSuspend();
1960 }
1961 mr_asprintf(tmp, "chroot %s %s /etc/fstab", MNT_RESTORING, editor);
1962 paranoid_system(tmp);
1963 mr_free(tmp);
1964
1965 mr_asprintf(tmp, "chroot %s %s /etc/elilo.conf", MNT_RESTORING, editor);
1966 paranoid_system(tmp);
1967 mr_free(tmp);
1968 mr_free(editor);
1969
1970 if (!g_text_mode) {
1971 newtResume();
1972 }
1973// newtCls();
1974 if (ask_me_yes_or_no("Edit them again?")) {
1975 continue;
1976 }
1977 done = TRUE;
1978 }
1979 mr_free(editor);
1980 }
1981 } else {
1982 log_to_screen("elilo.conf and fstab were modified OK");
1983 }
1984 } else
1985 /* nuke mode */
1986 {
1987 res = TRUE;
1988 }
1989 return (res);
1990}
1991
1992/**************************************************************************
1993 *END_RUN_ELILO *
1994 **************************************************************************/
1995
1996
1997
1998/**
1999 * Install the user's boot loader in the MBR.
2000 * Currently LILO, ELILO, GRUB, RAW (dd of MBR), and the FreeBSD bootloader are supported.
2001 * @param offer_to_hack_scripts If TRUE, then offer to hack the user's fstab for them.
2002 * @return 0 for success, nonzero for failure.
2003 */
2004int run_boot_loader(bool offer_to_hack_scripts)
2005{
2006 int res;
2007 int retval = 0;
2008
2009 /** malloc *******/
2010 char *device = NULL;
2011 char *disk = NULL;
2012 char *name = NULL;
2013 char *type = NULL;
2014 char *cmd = NULL;
2015
2016 malloc_string(device);
2017 malloc_string(name);
2018 malloc_string(type);
2019
2020 /* In order to have a working bootloader, we need to have all devices
2021 * ready in the chroot. If they are not there (udev) then copy them from
2022 * the current /dev location
2023 */
2024 mr_asprintf(cmd,"tar cf - /dev | ( cd %s ; tar xf - )",MNT_RESTORING);
2025 run_program_and_log_output(cmd, 3);
2026 mr_free(cmd);
2027
2028 backup_crucial_file(MNT_RESTORING, "/etc/fstab");
2029 backup_crucial_file(MNT_RESTORING, "/boot/grub/menu.lst");
2030 backup_crucial_file(MNT_RESTORING, "/boot/grub/grub.cfg");
2031 backup_crucial_file(MNT_RESTORING, "/boot/grub2/grub.cfg");
2032 backup_crucial_file(MNT_RESTORING, "/etc/lilo.conf");
2033 backup_crucial_file(MNT_RESTORING, "/etc/elilo.conf");
2034 backup_crucial_file(MNT_RESTORING, "/boot/grub/device.map");
2035 backup_crucial_file(MNT_RESTORING, "/boot/grub2/device.map");
2036 backup_crucial_file(MNT_RESTORING, "/etc/mtab");
2037 read_cfg_var(g_mondo_cfg_file, "bootloader.device", device);
2038 read_cfg_var(g_mondo_cfg_file, "bootloader.name", name);
2039 read_cfg_var(g_mondo_cfg_file, "boot-type", type);
2040 log_msg(2, "run_boot_loader: device='%s', name='%s', type='%s'", device, name, type);
2041 sync();
2042
2043 offer_to_make_initrd();
2044
2045 disk = truncate_to_drive_name(device);
2046 if (strcmp(type,"BIOS") == 0) {
2047 // Force installation of a MBR bootloader brought by mindi on the disk
2048 // in case none was available and we then start from a partition
2049 log_msg(2, "Reinstalling mbr.bin on %s", disk);
2050 mr_system("dd bs=440 count=1 conv=notrunc if=/tmp/mbr.bin of=%s &> /dev/null",disk);
2051 } else {
2052 // Same for GPT
2053 log_msg(2, "Reinstalling gptmbr.bin on %s", disk);
2054 mr_system("dd bs=440 count=1 conv=notrunc if=/tmp/gptmbr.bin of=%s &> /dev/null",disk);
2055 }
2056
2057 // Now reinstall bootloader
2058 if (!strcmp(name, "LILO")) {
2059 res = run_lilo(offer_to_hack_scripts);
2060 } else if (!strcmp(name, "ELILO")) {
2061 res = run_elilo(offer_to_hack_scripts);
2062 } else if (!strcmp(name, "GRUB")) {
2063 res = run_grub(offer_to_hack_scripts, device);
2064 } else if (!strcmp(name, "RAW")) {
2065 res = run_raw_mbr(offer_to_hack_scripts, device);
2066 }
2067#ifdef __FreeBSD__
2068 else if (!strcmp(name, "BOOT0")) {
2069 mr_asprintf(tmp, "boot0cfg -B %s", device);
2070 res = run_program_and_log_output(tmp, FALSE);
2071 paranoid_free(tmp);
2072 } else {
2073 mr_asprintf(tmp, "ls /dev | grep -Eq '^%ss[1-4].*'", device);
2074 if (!system(tmp)) {
2075 mr_free(tmp);
2076 mr_asprintf(tmp, MNT_RESTORING "/sbin/fdisk -B %s", device);
2077 res = run_program_and_log_output(tmp, 3);
2078 } else {
2079 log_msg(1, "I'm not running any boot loader. You have a DD boot drive. It's already loaded up.");
2080 }
2081 mr_free(tmp);
2082 }
2083#else
2084 else {
2085 log_to_screen
2086 ("Unable to determine type of boot loader. Defaulting to LILO.");
2087 res = run_lilo(offer_to_hack_scripts);
2088 }
2089#endif
2090 retval += res;
2091 if (res) {
2092 log_to_screen("Your boot loader returned an error");
2093 } else {
2094 log_to_screen("Your boot loader ran OK");
2095 }
2096 paranoid_free(device);
2097 paranoid_free(name);
2098 return (retval);
2099}
2100
2101/**************************************************************************
2102 *END_ RUN_BOOT_LOADER *
2103 **************************************************************************/
2104
2105
2106
2107/**
2108 * malloc() and set sensible defaults for the mondorestore filename variables.
2109 * @param bkpinfo The backup information structure. Fields used:
2110 * - @c bkpinfo->tmpdir
2111 * - @c bkpinfo->disaster_recovery
2112 */
2113void setup_MR_global_filenames()
2114{
2115 assert(bkpinfo != NULL);
2116
2117 malloc_string(g_biggielist_txt);
2118 malloc_string(g_filelist_full);
2119 malloc_string(g_filelist_imagedevs);
2120 malloc_string(g_imagedevs_restthese);
2121 malloc_string(g_mondo_cfg_file);
2122 malloc_string(g_mountlist_fname);
2123 malloc_string(g_mondo_home);
2124 malloc_string(g_isodir_device);
2125 malloc_string(g_isodir_format);
2126
2127 sprintf(g_biggielist_txt, "%s/%s", bkpinfo->tmpdir, BIGGIELIST_TXT_STUB);
2128 sprintf(g_filelist_full, "%s/%s", bkpinfo->tmpdir, FILELIST_FULL_STUB);
2129 sprintf(g_filelist_imagedevs, "%s/tmp/filelist.imagedevs", bkpinfo->tmpdir);
2130// sprintf(g_imagedevs_pot, "%s/tmp/imagedevs.pot", bkpinfo->tmpdir);
2131 sprintf(g_imagedevs_restthese, "%s/tmp/imagedevs.restore-these",
2132 bkpinfo->tmpdir);
2133 if (bkpinfo->disaster_recovery) {
2134 sprintf(g_mondo_cfg_file, "/%s", MONDO_CFG_FILE_STUB);
2135 sprintf(g_mountlist_fname, "/%s", MOUNTLIST_FNAME_STUB);
2136 } else {
2137 sprintf(g_mondo_cfg_file, "%s/%s", bkpinfo->tmpdir, MONDO_CFG_FILE_STUB);
2138 sprintf(g_mountlist_fname, "%s/%s", bkpinfo->tmpdir, MOUNTLIST_FNAME_STUB);
2139 }
2140}
2141
2142/**************************************************************************
2143 *END_SET_GLOBAL_FILENAME *
2144 **************************************************************************/
2145
2146
2147/**
2148 * Copy @p input_file (containing the result of a compare) to @p output_file,
2149 * deleting spurious "changes" along the way.
2150 * @param output_file The output file to write with spurious changes removed.
2151 * @param input_file The input file, a list of changed files created by a compare.
2152 */
2153void streamline_changes_file(char *output_file, char *input_file)
2154{
2155 FILE *fin;
2156 FILE *fout;
2157 char *incoming = NULL;
2158
2159 assert_string_is_neither_NULL_nor_zerolength(output_file);
2160 assert_string_is_neither_NULL_nor_zerolength(input_file);
2161
2162 if (!(fin = fopen(input_file, "r"))) {
2163 log_OS_error(input_file);
2164 return;
2165 }
2166 if (!(fout = fopen(output_file, "w"))) {
2167 fatal_error("cannot open output_file");
2168 }
2169 for (mr_getline(incoming, fin); !feof(fin); mr_getline(incoming, fin)) {
2170 if (strncmp(incoming, "etc/adjtime", 11)
2171 && strncmp(incoming, "etc/mtab", 8)
2172 && strncmp(incoming, "tmp/", 4)
2173 && strncmp(incoming, "boot/map", 8)
2174 && !strstr(incoming, "incheckentry")
2175 && strncmp(incoming, "etc/mail/statistics", 19)
2176 && strncmp(incoming, "var/", 4))
2177 fprintf(fout, "%s", incoming); /* don't need \n here, for some reason.. */
2178 mr_free(incoming);
2179 }
2180 mr_free(incoming);
2181 paranoid_fclose(fout);
2182 paranoid_fclose(fin);
2183}
2184
2185/**************************************************************************
2186 *END_STREAMLINE_CHANGES_FILE *
2187 **************************************************************************/
2188
2189
2190/**
2191 * Give the user twenty seconds to press Ctrl-Alt-Del before we nuke their drives.
2192 */
2193void twenty_seconds_til_yikes()
2194{
2195 int i;
2196 /* MALLOC * */
2197 char *tmp = NULL;
2198
2199 if (does_file_exist("/tmp/NOPAUSE")) {
2200 return;
2201 }
2202 open_progress_form("CAUTION",
2203 "Be advised: I am about to ERASE your hard disk(s)!",
2204 "You may press Ctrl+Alt+Del to abort safely.",
2205 "", 20);
2206 for (i = 0; i < 20; i++) {
2207 g_current_progress = i;
2208 mr_asprintf(tmp, "You have %d seconds left to abort.", 20 - i);
2209 update_progress_form(tmp);
2210 mr_free(tmp);
2211 sleep(1);
2212 }
2213 close_progress_form();
2214}
2215
2216/**************************************************************************
2217 *END_TWENTY_SECONDS_TIL_YIKES *
2218 **************************************************************************/
2219
2220
2221/**
2222 * Unmount all devices in @p p_external_copy_of_mountlist.
2223 * @param p_external_copy_of_mountlist The mountlist to guide the devices to unmount.
2224 * @return 0 for success, nonzero for failure.
2225 */
2226int unmount_all_devices(struct mountlist_itself
2227 *p_external_copy_of_mountlist)
2228{
2229 struct mountlist_itself *mountlist;
2230 int retval = 0, lino, res = 0, i;
2231 char *command = NULL;
2232 char *tmp = NULL;
2233
2234 assert(p_external_copy_of_mountlist != NULL);
2235
2236 mountlist = malloc(sizeof(struct mountlist_itself));
2237 memcpy((void *) mountlist, (void *) p_external_copy_of_mountlist, sizeof(struct mountlist_itself));
2238 sort_mountlist_by_mountpoint(mountlist, 0);
2239
2240 run_program_and_log_output("df -m -P -T", 3);
2241 mvaddstr_and_log_it(g_currentY, 0, "Unmounting devices ");
2242 open_progress_form("Unmounting devices", "Unmounting all devices that were mounted,", "in preparation for the post-restoration reboot.", "", mountlist->entries);
2243 if (chdir("/")) {
2244 // FIXME
2245 }
2246 for (i = 0; i < 10 && run_program_and_log_output("ps | grep buffer | grep -v \"grep buffer\"", TRUE) == 0; i++) {
2247 sleep(1);
2248 log_msg(2, "Waiting for buffer() to finish");
2249 }
2250
2251 sync();
2252
2253 /* after that what is logged is not copied on disk, typically the result of labelling */
2254 mr_asprintf(tmp, "cp -f %s " MNT_RESTORING "/var/log", MONDO_LOGFILE);
2255 if (run_program_and_log_output(tmp, FALSE)) {
2256 log_msg(1, "Error. Failed to copy log to the machine's /var/log dir. (Mounted read-only?)");
2257 }
2258 paranoid_free(tmp);
2259 if (does_file_exist("/tmp/DUMBASS-GENTOO")) {
2260 run_program_and_log_output("mkdir -p " MNT_RESTORING "/mnt/.boot.d", 5);
2261 }
2262
2263 /* Unmounting the local /proc and /sys first */
2264 run_program_and_log_output("umount -d " MNT_RESTORING "/proc",3);
2265 run_program_and_log_output("umount -d " MNT_RESTORING "/sys",3);
2266
2267 /* Unmounting potential btrfs subvolumes */
2268 if (does_file_exist("/tmp/umount-btrfs-subvol")) {
2269 run_program_and_log_output("/tmp/umount-btrfs-subvol",3);
2270 }
2271
2272 for (lino = mountlist->entries - 1; lino >= 0; lino--) {
2273 if (!strcmp(mountlist->el[lino].mountpoint, "lvm")) {
2274 continue;
2275 }
2276 mr_asprintf(tmp, "Unmounting device %s ", mountlist->el[lino].device);
2277 update_progress_form(tmp);
2278
2279 //TODO: this should be done in a certain order normally if there are cascading mount
2280 if (is_this_device_mounted(mountlist->el[lino].device)) {
2281 if (!strcmp(mountlist->el[lino].mountpoint, "swap")) {
2282 mr_asprintf(command, "swapoff %s", mountlist->el[lino].device);
2283 } else {
2284 if (!strcmp(mountlist->el[lino].mountpoint, "/1")) {
2285 mr_asprintf(command, "umount -d %s/", MNT_RESTORING);
2286 log_msg(3,
2287 "Well, I know a certain kitty-kitty who'll be sleeping with Mommy tonight...");
2288 } else {
2289 mr_asprintf(command, "umount -d " MNT_RESTORING "%s", mountlist->el[lino].mountpoint);
2290
2291 /* To support latest Ubuntu where /var is a separate FS
2292 * Cf: http://linux.derkeiler.com/Mailing-Lists/Ubuntu/2007-04/msg01319.html
2293 * we need to create some dirs under the real / before unmounting it */
2294 if (!strcmp(mountlist->el[lino].mountpoint, "/")) {
2295 run_program_and_log_output("mkdir -p " MNT_RESTORING "/var/lock", FALSE);
2296 run_program_and_log_output("mkdir -p " MNT_RESTORING "/var/run", FALSE);
2297 }
2298 }
2299 }
2300 log_msg(10, "The 'umount' command is '%s'", command);
2301 res = run_program_and_log_output(command, 3);
2302 mr_free(command);
2303 } else {
2304 mr_strcat(tmp, "...not mounted anyway :-) OK");
2305 res = 0;
2306 }
2307 g_current_progress++;
2308 if (res) {
2309 mr_strcat(tmp, "...Failed");
2310 retval++;
2311 log_to_screen(tmp);
2312 } else {
2313 log_msg(2, tmp);
2314 }
2315 paranoid_free(tmp);
2316 }
2317 close_progress_form();
2318 if (retval) {
2319 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
2320 } else {
2321 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
2322 }
2323 if (retval) {
2324 log_to_screen("Unable to unmount some of your partitions.");
2325 } else {
2326 log_to_screen("All partitions were unmounted OK.");
2327 }
2328 mr_asprintf(command, "mount");
2329 log_msg(4, "mount result after unmounting all FS", command);
2330 (void)run_program_and_log_output(command, 3);
2331 mr_free(command);
2332 free(mountlist);
2333 return (retval);
2334}
2335
2336/**************************************************************************
2337 *END_UNMOUNT_ALL_DEVICES *
2338 **************************************************************************/
2339/* @} - end restoreUtilityGroup */
2340
2341void wait_until_software_raids_are_prepped(char *mdstat_file, int wait_for_percentage)
2342{
2343 struct raidlist_itself *raidlist;
2344 int unfinished_mdstat_devices = 9999;
2345 int i = 0;
2346 char *screen_message = NULL;
2347
2348 raidlist = malloc(sizeof(struct raidlist_itself));
2349
2350 assert(wait_for_percentage <= 100);
2351 log_it("wait_until_software_raids_are_prepped");
2352 while (unfinished_mdstat_devices > 0) {
2353 // FIXME: Prefix '/dev/' should really be dynamic!
2354 if (parse_mdstat(MDSTAT_FILE, raidlist, "/dev/")) {
2355 log_to_screen("Sorry, cannot read %s", MDSTAT_FILE);
2356 log_msg(1,"Sorry, cannot read %s", MDSTAT_FILE);
2357 return;
2358 }
2359 for (unfinished_mdstat_devices = 0; i <= raidlist->entries; i++) {
2360 if (raidlist->el[i].progress < wait_for_percentage) {
2361 unfinished_mdstat_devices++;
2362 if (raidlist->el[i].progress == -1) // delayed while another partition inits
2363 {
2364 continue;
2365 }
2366 log_msg(1,"Sync'ing %s (i=%d)", raidlist->el[i].raid_device, i);
2367 mr_asprintf(screen_message, "Sync'ing %s", raidlist->el[i].raid_device);
2368 open_evalcall_form(screen_message);
2369 mr_free(screen_message);
2370
2371 while (raidlist->el[i].progress < wait_for_percentage) {
2372 log_msg(1,"Percentage sync'ed: %d", raidlist->el[i].progress);
2373 update_evalcall_form(raidlist->el[i].progress);
2374 sleep(2);
2375 // FIXME: Prefix '/dev/' should really be dynamic!
2376 if (parse_mdstat(MDSTAT_FILE, raidlist, "/dev/")) {
2377 break;
2378 }
2379 }
2380 close_evalcall_form();
2381 }
2382 }
2383 }
2384 paranoid_free(raidlist);
2385}
Note: See TracBrowser for help on using the repository browser.