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

Last change on this file since 3884 was 3882, checked in by Bruno Cornec, 2 years 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
RevLine 
[1]1/***************************************************************************
[2029]2$Id: mondo-rstr-tools.c 3882 2024-03-09 23:46:53Z bruno $
[1242]3***************************************************************************/
[1]4
5#include <pthread.h>
[1846]6#include <linux/fd.h>
[1930]7#include "my-stuff.h"
[2211]8#include "mr_mem.h"
[3193]9#include "mr_str.h"
[3613]10#include "mr_file.h"
[3746]11#include "mr_sys.h"
[3879]12#include "mondostructures.h"
[3564]13#include "libmondo-mountlist-EXT.h"
[3879]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"
[3873]21#include "newt-specific-EXT.h"
[3879]22// no include for now in mondo-rstr-newt.c
23extern int edit_filelist(struct s_node *);
[1]24
[2029]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/**
[3271]41 * The mondorestore.cfg stub (appended to the directory where all.tar.gz was unpacked).
[2029]42 */
[3271]43#define MONDO_CFG_FILE_STUB "tmp/mondorestore.cfg"
[2029]44/**
45 * The i-want-my-lvm stub
46 */
47#define IWANTMYLVM_STUB "tmp/i-want-my-lvm"
48
[128]49extern bool g_ISO_restore_mode; /* are we in Iso Mode? */
[1]50extern bool g_I_have_just_nuked;
[2030]51/*
[1]52extern char *g_tmpfs_mountpt;
[2030]53*/
[3158]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
[1]66extern long g_current_progress, g_maximum_progress;
[128]67extern char *g_biggielist_txt; // where 'biggielist.txt' is stored, on ramdisk / tempdir;
[1242]68 // biggielist.txt is the list of big files stored on the
69 // backup media set in question
[128]70extern char *g_filelist_full; // filelist.full.gz is the list of all regular files
[1242]71 // (excluding big files) stored on the backup media set
[128]72extern char *g_biggielist_pot; // list of big files which _could_ be restored, if the
[1242]73 // user chooses them
[128]74extern char *g_filelist_imagedevs; // list of devices (e.g. /dev/hda1, /dev/sda5) which
[1242]75 // were archived as images, not just /dev entries
76 // ... e.g. NTFS, BeOS partitions
[128]77extern char *g_imagedevs_restthese; // of the imagedevs listed in FILELIST_IMAGEDEVS,
[1242]78 // restore only these
[3292]79extern char *g_mondo_cfg_file; // where mondorestore.cfg (the config file) is stored
[128]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
[3879]82extern int g_currentY;
83extern bool g_restoring_live_from_cd;
84extern bool g_text_mode;
85extern int g_current_media_number;
[1]86
87extern t_bkptype g_backup_media_type;
88
89extern int g_partition_table_locked_up;
[1316]90extern char *MONDO_LOGFILE;
[1]91
[1645]92/* Reference to global bkpinfo */
93extern struct s_bkpinfo *bkpinfo;
94
[949]95/* Should we use or not extended attributes and acl when restoring */
[3790]96extern char *g_getfattr;
97extern char *g_getfacl;
[949]98
[1850]99extern void kill_anything_like_this(char *str);
[1968]100extern int skip_obdr(void);
[2022]101extern int set_tape_block_size_with_mt(long internal_tape_block_size);
[1850]102
[1]103/**
[1242]104* @addtogroup restoreUtilityGroup
105* @{
106*/
[1]107/**
[1242]108* Free the malloc()s for the filename variables.
109*/
[1967]110void free_MR_global_filenames(void)
[1]111{
[1242]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);
[2030]119/*
[1242]120paranoid_free(g_tmpfs_mountpt);
[2030]121*/
[1242]122paranoid_free(g_isodir_device);
123paranoid_free(g_isodir_format);
[1]124
125}
126
127
128
129/**
[1242]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*/
[3193]136void ask_about_these_imagedevs(char *infname, char *outfname) {
137
[1242]138FILE *fin;
139FILE *fout;
[3193]140char *incoming = NULL;
[3613]141char *p = NULL;
[3193]142char *question = NULL;
[1]143
[1242]144assert_string_is_neither_NULL_nor_zerolength(infname);
145assert_string_is_neither_NULL_nor_zerolength(outfname);
[1]146
[1242]147if (!(fin = fopen(infname, "r"))) {
[3076]148 fatal_error("Cannot openin infname");
[1242]149}
150if (!(fout = fopen(outfname, "w"))) {
[3076]151 fatal_error("Cannot openin outfname");
[1242]152}
[3614]153for (mr_getline(p, fin); !feof(fin) && (p != NULL); mr_getline(p, fin)) {
154 incoming = mr_strip_spaces(p);
155 mr_free(p);
[1]156
[3076]157 if (incoming[0] == '\0') {
[3193]158 mr_free(incoming);
[3076]159 continue;
160 }
[1]161
[3193]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);
[3076]166 }
[3193]167 mr_free(incoming);
[1242]168}
[3193]169mr_free(incoming);
170mr_free(question);
[1242]171paranoid_fclose(fout);
172paranoid_fclose(fin);
[1]173}
174
175/**************************************************************************
[1242]176*ASK_ABOUT_THESE_IMAGEDEVS *
177**************************************************************************/
[1]178
179
180
[3879]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;
[1]191
[3879]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 }
[1]198
[3879]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);
[1]203
[3879]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}
[1]222
223
[3879]224
225
226
[1]227/**
[3879]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/**
[3271]450* Extract @c mondorestore.cfg and @c mountlist.txt from @p ramdisk_fname.
[1242]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*/
[1]458
459/**
[3271]460* Keep trying to get mondorestore.cfg from the archive, until the user gives up.
[1242]461*/
[1645]462void get_cfg_file_from_archive_or_bust()
[1]463{
[1645]464while (get_cfg_file_from_archive()) {
[3263]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 }
[1]470}
471
472
473/**
[1242]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*/
[128]480bool is_file_in_list(char *f, char *list_fname, char *preamble)
[1]481{
482
[3193]483char *command = NULL;
484char *file = NULL;
485char *tmp = NULL;
[1242]486int res;
[1]487
[1242]488assert_string_is_neither_NULL_nor_zerolength(f);
489assert_string_is_neither_NULL_nor_zerolength(list_fname);
490assert(preamble != NULL);
[1]491
[1242]492if (strncmp(preamble, f, strlen(preamble)) == 0) {
[3193]493 mr_asprintf(file, "%s", f + strlen(preamble));
[1242]494} else {
[3193]495 mr_asprintf(file, "%s", f);
[1]496}
[1242]497if (file[0] == '/' && file[1] == '/') {
[3193]498 tmp = file;
499
500 mr_asprintf(file, "%s", tmp + 1);
501 mr_free(tmp);
[1242]502}
[3193]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
[1242]507res = run_program_and_log_output(command, FALSE);
[3193]508mr_free(command);
[1242]509if (res) {
[3193]510 return (FALSE);
[1242]511} else {
[3193]512 return (TRUE);
[1242]513}
514}
[128]515
[1]516/**************************************************************************
[1242]517*END_IS_FILE_IN_LIST *
518**************************************************************************/
[1]519
[3158]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 */
[3836]527bool get_isodir_info(char *isodir_device, char *isodir_format,
[3158]528 struct s_bkpinfo *bkpinfo, bool nuke_me_please)
529{
[1]530
[3158]531 bool HDD = FALSE;
[3836]532 char *tmp = NULL;
[3158]533 /** initialize ********************************************************/
[1]534
[3158]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 }
[3839]544 if (bkpinfo->isodir == NULL) {
[3836]545 mr_asprintf(bkpinfo->isodir, "%s", "/");
[3158]546 }
[3836]547 // TODO: Are we sure it does what it's expected to do here ?
[3278]548 if (does_file_exist("/tmp/NETFS-SERVER-MOUNT")) {
[3158]549 strcpy(isodir_device, last_line_of_file("/tmp/NETFS-SERVER-MOUNT"));
550 strcpy(isodir_format, "netfs");
[3836]551 mr_asprintf(bkpinfo->isodir, "%s", last_line_of_file("/tmp/ISO-DIR"));
[3158]552 }
553 if (nuke_me_please) {
554 return (TRUE);
555 }
556
[3278]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)) {
[3158]559 if (popup_with_buttons("Are you restoring from an external hard drive ?", "Yes", "No")) {
560 HDD = TRUE;
561 }
[3836]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) {
[3158]564 strip_spaces(isodir_device);
565 strip_spaces(isodir_format);
[3836]566 bkpinfo->isodir = mr_strip_spaces(tmp);
567 mr_free(tmp);
[3158]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 */
[3185]571 mr_asprintf(bkpinfo->subdir, "%s", bkpinfo->isodir);
[3836]572 mr_asprintf(bkpinfo->isodir, "%s", "/tmp/isodir");
[3158]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
[1]583/**
[1242]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*/
[3193]592int iso_fiddly_bits(bool nuke_me_please) {
593
[2211]594char *mount_isodir_command = NULL;
[3193]595char *command = NULL;
[2242]596char *mds = NULL;
[1242]597int retval = 0, i;
[1]598
[1242]599g_ISO_restore_mode = TRUE;
600read_cfg_var(g_mondo_cfg_file, "iso-dev", g_isodir_device);
601if (bkpinfo->disaster_recovery) {
[3278]602 /* Don't let this clobber an existing bkpinfo->isodir */
[3836]603 if (bkpinfo->isodir == NULL) {
604 mr_asprintf(bkpinfo->isodir, "%s", "/tmp/isodir");
[2211]605 }
[3193]606 mr_asprintf(command, "mkdir -p %s", bkpinfo->isodir);
[2211]607 run_program_and_log_output(command, 5);
[3193]608 mr_free(command);
[2211]609 log_msg(2, "Setting isodir to %s", bkpinfo->isodir);
[1242]610}
[1]611
[3158]612if (!get_isodir_info(g_isodir_device, g_isodir_format, bkpinfo, nuke_me_please)) {
[2211]613 return (1);
[1242]614}
[2878]615paranoid_system("umount -d " MNT_CDROM " 2> /dev/null"); /* just in case */
[1]616
[1242]617if (is_this_device_mounted(g_isodir_device)) {
[2211]618 log_to_screen("WARNING - isodir is already mounted");
[1242]619} else {
[3185]620 mr_asprintf(mount_isodir_command, "mount %s", g_isodir_device);
[2211]621 if (strlen(g_isodir_format) > 1) {
622 mr_strcat(mount_isodir_command, " -t %s", g_isodir_format);
623 }
[3193]624
[2211]625 mr_strcat(mount_isodir_command, " -o ro %s", bkpinfo->isodir);
[3600]626 run_program_and_log_output("df -m -P -T", FALSE);
[3193]627 log_msg(1, "The 'mount' command is '%s'. PLEASE report this command to be if you have problems, ok?", mount_isodir_command);
[2211]628 if (run_program_and_log_output(mount_isodir_command, FALSE)) {
[3600]629 popup_and_OK("Cannot mount the device where the ISO files are stored.");
[2211]630 return (1);
631 }
632 paranoid_free(mount_isodir_command);
[3600]633 log_to_screen("I have mounted the device where the ISO files are stored.");
[1242]634}
635if (!IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
[3876]636 mount_media(MNT_CDROM);
[1242]637}
[1736]638i = what_number_cd_is_this(); /* has the side-effect of calling mount_media() */
[2242]639mds = media_descriptor_string(bkpinfo->backup_media_type);
640mr_free(mds);
641
[3193]642log_msg(1, "%s #%d has been mounted via loopback mount", mds, i);
[1242]643if (i < 0) {
[3193]644 popup_and_OK("Cannot find ISO images in the directory you specified.");
645 retval = 1;
[1242]646}
[3193]647log_msg(2, "bkpinfo->isodir is now %s", bkpinfo->isodir);
[1242]648return (retval);
649}
[1]650
651
652
653
654/**
[1242]655* Kill all Petris processes.
656*/
[1850]657void kill_petris(void) {
[1852]658 kill_anything_like_this("petris");
[1]659}
[128]660
[1]661/**************************************************************************
[1242]662*END_KILL_PETRIS *
663**************************************************************************/
[1]664
665
666/**
[1843]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 */
[1771]674int mount_device(char *device, char *mpt, char *format, bool writeable)
675{
676int res = 0;
677
678/** malloc **/
[3193]679char *tmp = NULL;
[3866]680char *tmp1 = NULL;
[3193]681char *command = NULL;
682char *mountdir = NULL;
683char *mountpoint = NULL;
[2211]684char *additional_parameters = NULL;
[1771]685
686assert_string_is_neither_NULL_nor_zerolength(device);
687assert_string_is_neither_NULL_nor_zerolength(mpt);
688assert(format != NULL);
689
[1843]690 if (!strcmp(mpt, "/1")) {
[3193]691 mr_asprintf(mountpoint, "/");
[1843]692 log_msg(3, "Mommm! SME is being a dildo!");
693 } else {
[3193]694 mr_asprintf(mountpoint, "%s", mpt);
[1843]695 }
[1771]696
[1843]697 if (!strcmp(mountpoint, "lvm")) {
698 return (0);
699 }
700 if (!strcmp(mountpoint, "image")) {
701 return (0);
702 }
[3193]703 mr_asprintf(tmp, "Mounting device %s ", device);
[1843]704 log_msg(1, tmp);
[1937]705 /* Deal with additional params only if not /proc or /sys */
[3185]706 mr_asprintf(additional_parameters, "");
[1937]707 if (strcmp(format, "proc") && strcmp(format, "sys")) {
708 if (writeable) {
[2211]709 mr_strcat(additional_parameters, "-o rw");
[1937]710 } else {
[2211]711 mr_strcat(additional_parameters, "-o ro");
[1937]712 }
[3866]713 if ((tmp = find_home_of_exe("setfattr")) != NULL) {
[2211]714 mr_strcat(additional_parameters, ",user_xattr");
[1937]715 }
[3866]716 mr_free(tmp);
717 if ((tmp = find_home_of_exe("setfacl")) != NULL) {
[2211]718 mr_strcat(additional_parameters, ",acl");
[1937]719 }
[3866]720 mr_free(tmp);
[1843]721 }
[1771]722
723 if (!strcmp(mountpoint, "swap")) {
[3193]724 mr_asprintf(command, "swapon %s", device);
725 mr_asprintf(mountdir, "swap");
[1771]726 } else {
[1843]727 if (!strcmp(mountpoint, "/")) {
[3193]728 mr_asprintf(mountdir, "%s", MNT_RESTORING);
[1843]729 } else {
[3193]730 mr_asprintf(mountdir, "%s%s", MNT_RESTORING, mountpoint);
[1843]731 }
[3193]732 mr_asprintf(command, "mkdir -p %s", mountdir);
[1843]733 run_program_and_log_output(command, FALSE);
[3193]734 mr_free(command);
735
736 mr_asprintf(command, "mount -t %s %s %s %s 2>> %s", format, device, additional_parameters, mountdir, MONDO_LOGFILE);
[1843]737 log_msg(2, "command='%s'", command);
738 }
[3193]739 mr_free(additional_parameters);
[1843]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");
[3193]744 mr_free(command);
745 mr_asprintf(command, "mount -t %s %s %s 2>> %s", format, device, mountdir, MONDO_LOGFILE);
[1771]746 res = run_program_and_log_output(command, TRUE);
[1843]747 }
748 if (res) {
[3193]749 log_msg(1, "Unable to mount device %s (type %s) at %s", device, format, mountdir);
[1843]750 log_msg(1, "command was '%s'", command);
751 if (!strcmp(mountpoint, "swap")) {
752 log_to_screen(tmp);
[1771]753 } else {
[1843]754 log_msg(2, "Retrying w/o the '-t' switch");
[3193]755 mr_free(command);
756 mr_asprintf(command, "mount %s %s 2>> %s", device, mountdir, MONDO_LOGFILE);
[1843]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 }
[1771]765 }
766 }
767
[1843]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
[3193]774mr_free(tmp);
775mr_free(command);
776mr_free(mountdir);
777mr_free(mountpoint);
[1771]778
[3193]779return (res);
[1771]780}
781/**************************************************************************
[1843]782 *END_MOUNT_DEVICE *
[1771]783**************************************************************************/
784
785
786/**
[1843]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 */
[3193]792int mount_all_devices(struct mountlist_itself *p_external_copy_of_mountlist, bool writeable)
[1]793{
[1242]794int retval = 0, lino, res;
[3193]795char *tmp = NULL;
[2211]796char *these_failed = NULL;
797struct mountlist_itself *mountlist = NULL;
[1]798
[1242]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));
[1843]803 sort_mountlist_by_mountpoint(mountlist, 0);
[1]804
805
[1843]806 mvaddstr_and_log_it(g_currentY, 0, "Mounting devices ");
[3193]807 open_progress_form("Mounting devices", "I am now mounting all the drives.", "This should not take long.", "", mountlist->entries);
[128]808
[3185]809 mr_asprintf(these_failed, "");
[1843]810 for (lino = 0; lino < mountlist->entries; lino++) {
811 if (!strcmp(mountlist->el[lino].device, "/proc")) {
[3193]812 log_msg(1, "Again with the /proc - why is this in your mountlist?");
[1843]813 } else if (is_this_device_mounted(mountlist->el[lino].device)) {
[3193]814 log_to_screen("%s is already mounted", mountlist->el[lino].device);
[1843]815 } else if (strcmp(mountlist->el[lino].mountpoint, "none")
[1242]816 && strcmp(mountlist->el[lino].mountpoint, "lvm")
817 && strcmp(mountlist->el[lino].mountpoint, "raid")
818 && strcmp(mountlist->el[lino].mountpoint, "image")) {
[3193]819 mr_asprintf(tmp, "Mounting %s", mountlist->el[lino].device);
[1843]820 update_progress_form(tmp);
[3193]821 mr_free(tmp);
822 res = mount_device(mountlist->el[lino].device, mountlist->el[lino].mountpoint, mountlist->el[lino].format, writeable);
[1843]823 retval += res;
824 if (res) {
[2211]825 mr_strcat(these_failed, "%s ",mountlist->el[lino].device);
[1843]826 }
827 }
828 g_current_progress++;
[1]829 }
[1843]830 close_progress_form();
831 if (retval) {
832 if (g_partition_table_locked_up > 0) {
[3193]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.");
[1843]836 }
[3193]837 mr_asprintf(tmp, "Could not mount device(s) %s- shall I abort?", these_failed);
[1843]838
839 if (!ask_me_yes_or_no(tmp)) {
840 retval = 0;
[3193]841 log_to_screen("Continuing, although some device(s) failed to be mounted");
[1843]842 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
843 } else {
844 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
[3193]845 log_to_screen("Unable to mount some or all of your partitions.");
[1843]846 }
[3193]847 mr_free(tmp);
[1843]848 } else {
849 log_to_screen("All partitions were mounted OK.");
850 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
851 }
[2211]852 paranoid_free(these_failed);
853
[1937]854 /* Also mounting under MNT_RESTORING special FS */
855 (void)mount_device("/proc","/proc","proc",TRUE);
856 (void)mount_device("/sys","/sys","sysfs",TRUE);
[3743]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 }
[3600]866 run_program_and_log_output("df -m -P -T", 3);
[1843]867 paranoid_free(mountlist);
868 return (retval);
[1242]869}
870/**************************************************************************
871*END_MOUNT_ALL_DEVICES *
872**************************************************************************/
[1]873
874
875/**
[1242]876* Fix some miscellaneous things in the filesystem so the system will come
877* up correctly on the first boot.
878*/
[3301]879void protect_against_braindead_sysadmins() {
880
[3601]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);
[1]889}
[3601]890run_program_and_log_output("mkdir -p " MNT_RESTORING "/var/run/console", 0);
[3634]891run_program_and_log_output("chmod 666 " MNT_RESTORING "/dev/null", 0);
[3635]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 */
[3601]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);
[1242]897}
[128]898
[1]899/**************************************************************************
[1242]900*END_PROTECT_AGAINST_BRAINDEAD_SYSADMINS *
901**************************************************************************/
[1]902
903
904
905
906/**
[1242]907* Fill out @p bkpinfo based on @p cfg_file.
[3271]908* @param cfg_file The mondorestore.cfg file to read into @p bkpinfo.
[1242]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*/
[1645]913int read_cfg_file_into_bkpinfo(char *cfgf)
[1]914{
[1242]915/** add mallocs **/
[3193]916char value[MAX_STR_LEN];
[3879]917char *tmp = NULL;
[3193]918char *tmp1 = NULL;
[1242]919char *envtmp1 = NULL;
920char *envtmp2 = NULL;
921char *command = NULL;
[3193]922char iso_mnt[MAX_STR_LEN];
923char iso_path[MAX_STR_LEN];
[1242]924char *old_isodir = NULL;
[3193]925char *cfg_file = NULL;
[1242]926t_bkptype media_specified_by_user;
[1]927
928// assert_string_is_neither_NULL_nor_zerolength(cfg_file);
[1242]929assert(bkpinfo != NULL);
[3193]930assert(cfgf != NULL);
[3158]931log_it("Entering read_cfg_file_into_bkpinfo");
[1]932
[1242]933if (!cfgf) {
[3211]934 mr_asprintf(cfg_file, "%s", g_mondo_cfg_file);
[1242]935} else {
[3211]936 mr_asprintf(cfg_file, "%s", cfgf);
[1242]937}
[1]938
[1242]939media_specified_by_user = bkpinfo->backup_media_type; // or 'none', if not specified
[1]940
[1242]941if (0 == read_cfg_var(cfg_file, "backup-media-type", value)) {
[3882]942 if (!strcmp(value, "optical")) {
[3881]943 bkpinfo->backup_media_type = optical;
[3193]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 }
[3879]955 if (does_file_exist(ARCHIVES_PATH "/filelist.0")) {
[3881]956 bkpinfo->backup_media_type = optical;
[3193]957 run_program_and_log_output("umount -d "MNT_CDROM, 1);
[3881]958 log_it("Re-jigging configuration AGAIN. Optical, not ISO.");
[3193]959 }
[128]960 }
[3193]961 if (read_cfg_var(cfg_file, "iso-prefix", value) == 0) {
[3835]962 mr_asprintf(bkpinfo->prefix, "%s", value);
[3193]963 } else {
[3835]964 mr_asprintf(bkpinfo->prefix, "%s", STD_PREFIX);
[949]965 }
[3193]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) {
[3835]981 mr_asprintf(bkpinfo->prefix, "%s", value);
[3193]982 } else {
[3835]983 mr_asprintf(bkpinfo->prefix, "%s", STD_PREFIX);
[3193]984 }
[3610]985 tmp1 = call_program_and_get_last_line_of_output("cat "CMDLINE);
986 if (strstr(tmp1, "pxe")) {
[3193]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 }
[3835]993 mr_asprintf(bkpinfo->prefix, "%s", envtmp1);
[3193]994 }
[3610]995 mr_free(tmp1);
[3193]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");
[949]1002 }
[1242]1003} else {
[3193]1004 fatal_error("backup-media-type not specified!");
[1242]1005}
[3193]1006
[1242]1007if (bkpinfo->disaster_recovery) {
[3882]1008 if (bkpinfo->backup_media_type == usb) {
[2127]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);
[1736]1016 }
[3822]1017 mr_asprintf(bkpinfo->media_device, "%s1", value);
[3193]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) {
[1736]1020 if (read_cfg_var(cfg_file, "media-dev", value)) {
1021 fatal_error("Cannot get tape device name from cfg file");
1022 }
[3822]1023 mr_asprintf(bkpinfo->media_device, "%s", value);
[1736]1024 read_cfg_var(cfg_file, "media-size", value);
[3150]1025 bkpinfo->media_size = atol(value);
[3193]1026 log_msg(2, "Backup medium is TAPE --- dev=%s", bkpinfo->media_device);
[1736]1027 } else {
[3822]1028 mr_asprintf(bkpinfo->media_device, "%s", "/dev/cdrom"); /* we don't really need this var */
[3150]1029 bkpinfo->media_size = 1999 * 1024; /* 650, probably, but we don't need this var anyway */
[3158]1030 log_msg(2, "Backup medium is similar to CD-R[W]");
[1]1031 }
[1242]1032} else {
[3193]1033 log_msg(2, "Not in Disaster Recovery Mode. No need to derive device name from config file.");
[1242]1034}
[128]1035
[1242]1036read_cfg_var(cfg_file, "use-star", value);
1037if (strstr(value, "yes")) {
[1948]1038 bkpinfo->use_star = TRUE;
1039 log_msg(1, "Goody! ... bkpinfo->use_star is now true.");
[1242]1040}
1041
[1948]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
[1242]1048read_cfg_var(cfg_file, "acl", value);
1049if (strstr(value, "TRUE")) {
[3866]1050 if ((tmp = find_home_of_exe("setfacl")) == NULL) {
[1948]1051 log_msg(1, "Unable to restore ACLs as no setfacl found");
[3866]1052 } else {
1053 mr_asprintf(g_getfacl,"%s", tmp);
1054 log_msg(1, "We will restore ACLs");
[1948]1055 }
[3866]1056 mr_free(tmp);
[1242]1057}
1058read_cfg_var(cfg_file, "xattr", value);
1059if (strstr(value, "TRUE")) {
[3866]1060 if ((tmp = find_home_of_exe("setfattr")) == NULL) {
[1948]1061 log_msg(1, "Unable to restore XATTRs as no setfattr found");
[3866]1062 } else {
1063 mr_asprintf(g_getfattr,"%s", tmp);
1064 log_msg(1, "We will restore XATTRs");
[1948]1065 }
[3866]1066 mr_free(tmp);
[1242]1067}
1068
1069if (0 == read_cfg_var(cfg_file, "internal-tape-block-size", value)) {
[3193]1070 bkpinfo->internal_tape_block_size = atol(value);
[1242]1071} else {
[3193]1072 bkpinfo->internal_tape_block_size = DEFAULT_INTERNAL_TAPE_BLOCK_SIZE;
[1242]1073}
[3193]1074log_msg(1, "Internal tape block size set to %ld", bkpinfo->internal_tape_block_size);
[1242]1075
[3193]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;
[3830]1081 mr_asprintf(bkpinfo->zip_exe, "%s", "lzma");
1082 mr_asprintf(bkpinfo->zip_suffix, "%s", "lzma");
[3193]1083}
1084
[1242]1085read_cfg_var(cfg_file, "use-lzo", value);
1086if (strstr(value, "yes")) {
[3193]1087 bkpinfo->use_lzma = FALSE;
1088 bkpinfo->use_lzo = TRUE;
1089 bkpinfo->use_gzip = FALSE;
[3830]1090 mr_asprintf(bkpinfo->zip_exe, "%s", "lzop");
1091 mr_asprintf(bkpinfo->zip_suffix, "%s", "lzo");
[3193]1092}
1093
[1242]1094read_cfg_var(cfg_file, "use-gzip", value);
1095if (strstr(value, "yes")) {
[3193]1096 bkpinfo->use_lzma = FALSE;
[1242]1097 bkpinfo->use_lzo = FALSE;
1098 bkpinfo->use_gzip = TRUE;
[3830]1099 mr_asprintf(bkpinfo->zip_exe, "%s", "gzip");
1100 mr_asprintf(bkpinfo->zip_suffix, "%s", "gz");
[1242]1101}
[3193]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;
[3830]1108 mr_asprintf(bkpinfo->zip_exe, "%s", "xz");
1109 mr_asprintf(bkpinfo->zip_suffix, "%s", "xz");
[1242]1110}
[1]1111
[1242]1112value[0] = '\0';
1113read_cfg_var(cfg_file, "differential", value);
1114if (!strcmp(value, "yes") || !strcmp(value, "1")) {
[3193]1115 bkpinfo->differential = TRUE;
[1242]1116}
1117log_msg(2, "differential var = '%s'", value);
1118if (bkpinfo->differential) {
[3193]1119 log_msg(2, "THIS IS A DIFFERENTIAL BACKUP");
[1242]1120} else {
[3193]1121 log_msg(2, "This is a regular (full) backup");
[1242]1122}
1123
[3211]1124read_cfg_var(cfg_file, "please-dont-eject", value);
[3610]1125tmp1 = call_program_and_get_last_line_of_output("cat "CMDLINE);
1126if (value[0] || strstr(tmp1, "donteject")) {
[3158]1127 bkpinfo->please_dont_eject = TRUE;
1128 log_msg(2, "Ok, I shan't eject when restoring! Groovy.");
[1242]1129}
[3610]1130mr_free(tmp1);
[1242]1131
[3430]1132/* TODO: Read here the boot_* variables */
[3397]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
[2380]1145if (bkpinfo->backup_media_type == netfs) {
[1736]1146 if (!cfgf) {
[3193]1147 if (bkpinfo->netfs_mount) {
1148 log_msg(2, "netfs_mount remains %s", bkpinfo->netfs_mount);
1149 }
[3207]1150 if (bkpinfo->netfs_remote_dir != NULL) {
[3193]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");
[1736]1154 } else {
[3211]1155 read_cfg_var(cfg_file, "netfs-server-mount", value);
[3193]1156 mr_free(bkpinfo->netfs_mount);
1157 mr_asprintf(bkpinfo->netfs_mount, "%s", value);
[3278]1158 if (bkpinfo->netfs_mount != NULL) {
1159 log_msg(2, "netfs_mount is %s", bkpinfo->netfs_mount);
1160 }
[3193]1161
[3278]1162 read_cfg_var(cfg_file, "iso-dir", value);
[3193]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 }
[3278]1168
[3193]1169 if (bkpinfo->netfs_proto != NULL) {
1170 log_msg(2, "netfs_proto is %s", bkpinfo->netfs_proto);
1171 }
1172 if (bkpinfo->netfs_user != NULL) {
[2847]1173 log_msg(2, "netfs_user is %s", bkpinfo->netfs_user);
1174 }
[1]1175 }
[3610]1176 tmp1 = call_program_and_get_last_line_of_output("cat "CMDLINE);
1177 if (strstr(tmp1, "pxe")) {
[1968]1178 /* We need to override values in PXE mode as it's
[2380]1179 * already done in start-netfs */
1180 envtmp1 = getenv("netfsmount");
[1736]1181 if (envtmp1 == NULL) {
[2380]1182 fatal_error("no netfsmount variable in environment");
[1736]1183 }
1184 envtmp2 = getenv("dirimg");
1185 if (envtmp2 == NULL) {
1186 fatal_error("no dirimg variable in environment");
1187 }
[3193]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);
[1]1193 }
[3610]1194 mr_free(tmp1);
[1242]1195} else if (bkpinfo->backup_media_type == iso) {
[1736]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 */
[3193]1200 mr_asprintf(old_isodir, "%s", bkpinfo->isodir);
[3836]1201 if (read_cfg_var(cfg_file, "iso-mnt", iso_mnt) != 0) {
1202 iso_mnt[0] = '\0';
[128]1203 }
[3836]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);
[3839]1208 if (!strcmp(bkpinfo->isodir,"")) {
[3836]1209 mr_asprintf(bkpinfo->isodir, "%s", old_isodir);
1210 }
1211
[3158]1212 if ((!bkpinfo->disaster_recovery) || (g_isodir_device[0] != '\0')) {
[1736]1213 if (strcmp(old_isodir, bkpinfo->isodir)) {
[3193]1214 log_it("user nominated isodir %s differs from archive, keeping user's choice: %s\n", bkpinfo->isodir, old_isodir );
[3836]1215 mr_asprintf(bkpinfo->isodir, "%s", old_isodir);
[1736]1216 }
1217 }
[3211]1218 mr_free(old_isodir);
1219
[3158]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 */
[3211]1223 read_cfg_var(cfg_file, "iso-dev", g_isodir_device);
[3158]1224 }
1225
1226 log_msg(2, "isodir=%s; iso-dev=%s", bkpinfo->isodir, g_isodir_device);
[3193]1227
[1736]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 */
[3193]1232 mr_asprintf(command, "mount | grep -E '^%s' | tail -n1 | cut -d' ' -f3", g_isodir_device);
[1736]1233 log_it("command = %s", command);
[3610]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);
[3193]1237 mr_free(command);
[3610]1238 mr_free(tmp1);
[1736]1239 } else {
[3194]1240 sprintf(iso_mnt, "/tmp/isodir");
[3193]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)) {
[3881]1247 log_msg(1, "Unable to mount isodir. Perhaps this is really an optical based backup?");
1248 bkpinfo->backup_media_type = optical;
[3822]1249 mr_asprintf(bkpinfo->media_device, "%s", "/dev/cdrom"); /* superfluous */
[3836]1250 mr_free(bkpinfo->isodir);
1251 iso_mnt[0] = iso_path[0] = '\0';
[3876]1252 if (mount_media(MNT_CDROM)) {
[3193]1253 mr_free(tmp1);
1254 fatal_error("Unable to mount isodir. Failed to mount CD-ROM as well.");
[1736]1255 } else {
[3193]1256 log_msg(1, "You backed up to disk, then burned some CDs.");
[1736]1257 }
[128]1258 }
[3193]1259 mr_free(tmp1);
[128]1260 }
[1736]1261 /* bkpinfo->isodir should now be the true path to prefix-1.iso etc... */
[3193]1262 /* except when already done in iso_fiddly_bits */
[3158]1263 if ((bkpinfo->backup_media_type == iso) && (g_isodir_device[0] == '\0')) {
[3836]1264 mr_asprintf(bkpinfo->isodir, "%s%s", iso_mnt, iso_path);
[1736]1265 }
[1]1266 }
[1242]1267}
[3211]1268mr_free(cfg_file);
[1]1269
[1242]1270if (media_specified_by_user != none) {
[1736]1271 if (g_restoring_live_from_cd) {
1272 if (bkpinfo->backup_media_type != media_specified_by_user) {
[3158]1273 log_msg(2, "bkpinfo->backup_media_type != media_specified_by_user, so I'd better ask :)");
[1736]1274 interactively_obtain_media_parameters_from_user(FALSE);
1275 media_specified_by_user = bkpinfo->backup_media_type;
1276 get_cfg_file_from_archive();
1277 }
[128]1278 }
[1736]1279 bkpinfo->backup_media_type = media_specified_by_user;
[1242]1280}
1281g_backup_media_type = bkpinfo->backup_media_type;
1282return (0);
[1]1283
1284}
[128]1285
[1]1286/**************************************************************************
[1242]1287*END_READ_CFG_FILE_INTO_BKPINFO *
1288**************************************************************************/
[1]1289
1290
1291
[128]1292
[1]1293/**
[1843]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 */
[1]1303struct
[1645]1304s_node *process_filelist_and_biggielist()
[1]1305{
[1242]1306struct s_node *filelist;
[1]1307
[3193]1308char *command = NULL;
[3613]1309char *old_pwd = NULL;
[3076]1310char *q;
[1242]1311int res = 0;
1312pid_t pid;
[2049]1313bool extract_mountlist_stub = FALSE;
[1]1314
[1242]1315assert(bkpinfo != NULL);
[1]1316
[2049]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
[1242]1324if (does_file_exist(g_filelist_full)
1325&& does_file_exist(g_biggielist_txt)) {
[1987]1326 log_msg(1, "%s exists", g_filelist_full);
1327 log_msg(1, "%s exists", g_biggielist_txt);
1328 log_msg(2,
[1242]1329 "Filelist and biggielist already recovered from media. Yay!");
1330} else {
[3613]1331 old_pwd = mr_getcwd();
[3076]1332 if (chdir(bkpinfo->tmpdir)) {
1333 // FIXME
1334 }
[1987]1335 log_msg(1, "chdir(%s)", bkpinfo->tmpdir);
1336 log_to_screen("Extracting filelist and biggielist from media...");
1337 unlink("/tmp/filelist.full");
[2029]1338 unlink(FILELIST_FULL_STUB);
[1987]1339 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
[3193]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);
[1987]1341 log_msg(1, "tarcommand = %s", command);
1342 run_program_and_log_output(command, 1);
[3193]1343 mr_free(command);
1344
[2029]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");
[3193]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);
[2029]1349 log_msg(1, "tarcommand = %s", command);
1350 run_program_and_log_output(command, 1);
[3193]1351 mr_free(command);
[2029]1352 }
[1987]1353 } else {
[3193]1354 log_msg(2, "Calling insist_on_this_cd_number; bkpinfo->isodir=%s", bkpinfo->isodir);
[1987]1355 insist_on_this_cd_number(1);
1356 log_msg(2, "Back from iotcn");
1357 run_program_and_log_output("mount", 1);
[3193]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);
[1]1359
[1987]1360 log_msg(1, "tarcommand = %s", command);
1361 run_program_and_log_output(command, 1);
[3193]1362 mr_free(command);
1363
[2029]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");
[3193]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);
[2029]1368
1369 log_msg(1, "tarcommand = %s", command);
1370 run_program_and_log_output(command, 1);
[3193]1371 mr_free(command);
[2029]1372 }
[1987]1373 if (!does_file_exist(BIGGIELIST_TXT_STUB)) {
[3193]1374 fatal_error("all.tar.gz did not include " BIGGIELIST_TXT_STUB);
[1987]1375 }
1376 if (!does_file_exist(FILELIST_FULL_STUB)) {
[3193]1377 fatal_error("all.tar.gz did not include " FILELIST_FULL_STUB);
[1987]1378 }
[128]1379 }
[3193]1380 mr_asprintf(command, "cp -f %s %s", MONDO_CFG_FILE_STUB, g_mondo_cfg_file);
[1987]1381 run_program_and_log_output(command, FALSE);
[3193]1382 mr_free(command);
[1]1383
[3193]1384 mr_asprintf(command, "cp -f %s/%s %s", bkpinfo->tmpdir, BIGGIELIST_TXT_STUB, g_biggielist_txt);
[1987]1385 log_msg(1, "command = %s", command);
1386 paranoid_system(command);
[3193]1387 mr_free(command);
1388
1389 mr_asprintf(command, "ln -sf %s/%s %s", bkpinfo->tmpdir, FILELIST_FULL_STUB, g_filelist_full);
[1987]1390 log_msg(1, "command = %s", command);
1391 paranoid_system(command);
[3193]1392 mr_free(command);
[1987]1393 }
[1242]1394
[1987]1395 if (am_I_in_disaster_recovery_mode()
1396 &&
[2049]1397 /* If it was there, do not overwrite it */
1398 (extract_mountlist_stub)
1399 &&
[1987]1400 ask_me_yes_or_no("Do you want to retrieve the mountlist as well?"))
1401 {
[3193]1402 mr_asprintf(command, "ln -sf %s/%s /tmp", MOUNTLIST_FNAME_STUB, bkpinfo->tmpdir);
1403 paranoid_system(command);
1404 mr_free(command);
[1987]1405 }
[1]1406
[3613]1407 if (chdir(old_pwd)) {
[3076]1408 // FIXME
1409 }
[3613]1410 mr_free(old_pwd);
[2049]1411
[1987]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 }
[1]1418
[1987]1419 log_msg(2, "Forking");
1420 pid = fork();
1421 switch (pid) {
1422 case -1:
1423 fatal_error("Forking error");
1424 break;
[1]1425
[1987]1426 case 0:
1427 log_to_screen("Pre-processing filelist");
1428 if (!does_file_exist(g_biggielist_txt)) {
[3193]1429 mr_asprintf(command, "echo -n > %s", g_biggielist_txt);
[128]1430 paranoid_system(command);
[3193]1431 mr_free(command);
[128]1432 }
[3193]1433 mr_asprintf(command, "grep -E '^/dev/.*' %s > %s", g_biggielist_txt, g_filelist_imagedevs);
[128]1434 paranoid_system(command);
[3193]1435 mr_free(command);
[128]1436 exit(0);
1437 break;
1438
1439 default:
[541]1440 open_evalcall_form("Pre-processing filelist");
[128]1441 while (!waitpid(pid, (int *) 0, WNOHANG)) {
1442 usleep(100000);
1443 update_evalcall_form(0);
1444 }
[1]1445 }
[128]1446 close_evalcall_form();
[1]1447
[128]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) {
[3076]1453 q = NULL;
1454 while (q == NULL) {
1455 printf("Restore which directory? --> ");
[3193]1456 mr_getline(q, stdin);
[3076]1457 }
[3193]1458 toggle_path_selection(filelist, q, TRUE);
1459 if (strlen(q) == 0) {
[128]1460 res = 1;
1461 } else {
1462 res = 0;
1463 }
[3193]1464 mr_free(q);
[128]1465 } else {
1466 res = edit_filelist(filelist);
[1]1467 }
[128]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();
[1]1475
[128]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.
[1]1479
[128]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 }
[1]1486
[128]1487 return (filelist);
1488}
[1]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 */
[128]1504int backup_crucial_file(char *path_root, char *filename)
[1]1505{
[3193]1506 char *tmp = NULL;
1507 char *command = NULL;
[128]1508 int res;
[1]1509
[128]1510 assert(path_root != NULL);
1511 assert_string_is_neither_NULL_nor_zerolength(filename);
1512
[3193]1513 mr_asprintf(tmp, "%s/%s", path_root, filename);
1514 mr_asprintf(command, "cp -f %s %s.pristine", tmp, tmp);
1515 mr_free(tmp);
[128]1516
1517 res = run_program_and_log_output(command, 5);
[3193]1518 mr_free(command);
[128]1519 return (res);
[1]1520}
1521
[2094]1522void offer_to_make_initrd() {
[1]1523
[2094]1524if (bkpinfo->restore_mode != nuke) {
[2110]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 ?")) {
[2136]1527 log_msg(1,"Launching shell for manual initrd recreation");
[2758]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 {
[3157]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.");
[2758]1532 }
[2110]1533 mvaddstr_and_log_it(g_currentY, 0, "Modifying initrd...");
1534 if (!g_text_mode) {
1535 newtSuspend();
1536 }
[3076]1537 paranoid_system("chroot " MNT_RESTORING);
[2110]1538 if (!g_text_mode) {
1539 newtResume();
1540 }
1541 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1542 } else {
[2094]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
[1]1552/**
[3879]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.
[1]1584 * @return 0 for success, nonzero for failure.
1585 */
[3879]1586int run_lilo(bool offer_to_run_stablilo)
[1]1587{
[3879]1588 /** malloc **/
1589 char *command = NULL;
1590 char *tmp = NULL;
1591 char *editor = NULL;
1592
[128]1593 int res;
[3879]1594 int done;
1595 bool run_lilo_M = FALSE;
[1]1596
[3879]1597 if (!run_program_and_log_output
1598 ("grep \"boot.*=.*/dev/md\" " MNT_RESTORING "/etc/lilo.conf", 1)) {
1599 run_lilo_M = TRUE;
1600 }
[1]1601
[3879]1602 if (offer_to_run_stablilo
1603 && ask_me_yes_or_no("Did you change the mountlist or cloned the system ?"))
[1845]1604
[3879]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);
[1845]1613
[3879]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);
[2094]1643
[3879]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 }
[128]1654 } else {
[3879]1655 log_to_screen("lilo.conf and fstab were modified OK");
[128]1656 }
[3879]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 }
[128]1678 }
[3879]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);
[128]1684 }
[3879]1685 return (res);
[1]1686}
[128]1687
[1]1688/**************************************************************************
[3879]1689 *END_RUN_LILO *
[1]1690 **************************************************************************/
1691
1692
1693/**
[3879]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.
[1]1698 */
[3879]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;
[3193]1708
[3879]1709 malloc_string(boot_device);
1710 assert_string_is_neither_NULL_nor_zerolength(bd);
[3866]1711
[3879]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();
[3866]1726 }
[3879]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 }
[3866]1734 }
[3879]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 }
[3866]1747 }
[3879]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);
[128]1756 }
[3879]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.");
[128]1762 }
[3879]1763 paranoid_free(boot_device);
1764 return (res);
[1]1765}
1766
[3879]1767/**************************************************************************
1768 *END_RUN_RAW_MBR *
1769 **************************************************************************/
[1]1770
[3879]1771
[1]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 */
[128]1778int run_grub(bool offer_to_run_stabgrub, char *bd)
[1]1779{
1780 /** malloc **/
[3193]1781 char *command = NULL;
1782 char *boot_device = NULL;
1783 char *tmp = NULL;
1784 char *editor = NULL;
[1]1785
[3193]1786 bool done;
[2950]1787 int res = 0; /* FALSE */
1788 bool mntlistchg = FALSE;
1789 FILE *fin = NULL;
[1]1790
[128]1791 malloc_string(boot_device);
[3193]1792
[128]1793 assert_string_is_neither_NULL_nor_zerolength(bd);
1794 strcpy(boot_device, bd);
1795
[3297]1796 if (offer_to_run_stabgrub && ask_me_yes_or_no("Did you change the mountlist or cloned the system ?")) {
[128]1797 /* interactive mode */
[3297]1798 mvaddstr_and_log_it(g_currentY, 0, "Modifying fstab, mtab, device.map and menu.lst/grub.cfg, and running GRUB...");
[2950]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 }
[128]1803 for (done = FALSE; !done;) {
[3297]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);
[2950]1805 /* Only try to adapt grub here first if the mountlist wasn't changed before */
1806 if (! mntlistchg) {
[3542]1807 mr_asprintf(command, "mr-stabgrub-me %s", boot_device);
[2950]1808 res = run_program_and_log_output(command, 1);
[3193]1809 mr_free(command);
[2950]1810 }
[3193]1811
1812 if ((res) || (mntlistchg)) {
[2950]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 }
[3879]1818 editor = find_my_editor();
[3866]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);
[3193]1828
[3866]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 }
[2904]1853 }
[3193]1854 mr_free(editor);
1855
[3542]1856 mr_asprintf(command, "mr-stabgrub-me %s", boot_device);
[2904]1857 res = run_program_and_log_output(command, 1);
[3193]1858 mr_free(command);
1859
[2904]1860 if (res) {
[3193]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.");
[2904]1862 newtSuspend();
[3076]1863 paranoid_system("chroot " MNT_RESTORING);
[2904]1864 newtResume();
1865 } else {
1866 popup_and_OK("GRUB is now installed correctly");
1867 done = TRUE;
1868 }
[128]1869 } else {
1870 done = TRUE;
1871 }
1872 }
[3193]1873 } else {
[128]1874 /* nuke mode */
[3542]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);
[3193]1878 log_msg(1, "command = %s", command);
1879 } else {
[3297]1880 // Or grub2-install
[3193]1881 mr_asprintf(command, "chroot " MNT_RESTORING " grub-install %s", boot_device);
[3542]1882 log_msg(1, "WARNING - mr-grub not found; using grub-install");
[3193]1883 }
1884 mvaddstr_and_log_it(g_currentY, 0, "Running GRUB... ");
[2227]1885 log_it("%s",command);
[128]1886 res = run_program_and_log_output(command, 1);
[3193]1887 mr_free(command);
1888
[128]1889 if (res) {
[3299]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.");
[128]1891 newtSuspend();
[3076]1892 paranoid_system("chroot " MNT_RESTORING);
[128]1893 newtResume();
1894 }
[1]1895 }
[128]1896 if (res) {
[541]1897 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
[128]1898 log_to_screen
[1315]1899 ("GRUB ran w/error(s). See %s for more info.", MONDO_LOGFILE);
[128]1900 log_msg(1, "Type:-");
[3542]1901 log_msg(1, " mr-mount-me");
[128]1902 log_msg(1, " chroot " MNT_RESTORING);
[3707]1903 log_msg(1, " mount /boot if needed");
[128]1904 log_msg(1, " grub-install '(hd0)'");
1905 log_msg(1, " exit");
[3542]1906 log_msg(1, " mr-unmount-me");
[128]1907 log_msg(1,
1908 "If you're really stuck, please e-mail the mailing list.");
1909 } else {
[541]1910 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
[128]1911 }
1912 paranoid_free(boot_device);
[1]1913
[128]1914 return (res);
[1]1915}
[128]1916
[1]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 */
[128]1927int run_elilo(bool offer_to_run_stabelilo)
[1]1928{
1929 /** malloc **/
[3193]1930 char *command = NULL;
1931 char *tmp = NULL;
1932 char *editor = NULL;
[1]1933
[128]1934 int res;
1935 int done;
[1]1936
[128]1937 if (offer_to_run_stabelilo
[2094]1938 && ask_me_yes_or_no("Did you change the mountlist or cloned the system ?"))
[1]1939
[128]1940 /* interactive mode */
1941 {
1942 mvaddstr_and_log_it(g_currentY,
1943 0,
[541]1944 "Modifying fstab and elilo.conf... ");
[3542]1945 mr_asprintf(command, "mr-stabelilo-me");
[128]1946 res = run_program_and_log_output(command, 3);
[3193]1947 mr_free(command);
1948
[128]1949 if (res) {
[3866]1950 popup_and_OK("You will now edit fstab and elilo.conf, to make sure they match your new mountlist.");
[128]1951 for (done = FALSE; !done;) {
[3866]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 {
[3193]1957
[3866]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);
[3193]1969
[3866]1970 if (!g_text_mode) {
1971 newtResume();
1972 }
[1]1973// newtCls();
[3866]1974 if (ask_me_yes_or_no("Edit them again?")) {
1975 continue;
1976 }
1977 done = TRUE;
[128]1978 }
[3866]1979 mr_free(editor);
[128]1980 }
1981 } else {
[541]1982 log_to_screen("elilo.conf and fstab were modified OK");
[1]1983 }
[128]1984 } else
1985 /* nuke mode */
[1]1986 {
[128]1987 res = TRUE;
[1]1988 }
[128]1989 return (res);
1990}
[1]1991
1992/**************************************************************************
1993 *END_RUN_ELILO *
1994 **************************************************************************/
1995
1996
1997
1998/**
[3879]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.
[1]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 */
[3879]2004int run_boot_loader(bool offer_to_hack_scripts)
[1]2005{
[128]2006 int res;
[3879]2007 int retval = 0;
[1]2008
[3879]2009 /** malloc *******/
2010 char *device = NULL;
2011 char *disk = NULL;
2012 char *name = NULL;
2013 char *type = NULL;
2014 char *cmd = NULL;
[1]2015
[3879]2016 malloc_string(device);
2017 malloc_string(name);
2018 malloc_string(type);
[1]2019
[3879]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);
[3193]2027
[3879]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();
[3866]2042
[3879]2043 offer_to_make_initrd();
[3193]2044
[3879]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);
[3193]2051 } else {
[3879]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 }
[3193]2056
[3879]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);
[1]2066 }
[3879]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;
[128]2091 if (res) {
[3879]2092 log_to_screen("Your boot loader returned an error");
[128]2093 } else {
[3879]2094 log_to_screen("Your boot loader ran OK");
[128]2095 }
[3879]2096 paranoid_free(device);
2097 paranoid_free(name);
2098 return (retval);
[1]2099}
[128]2100
[1]2101/**************************************************************************
[3879]2102 *END_ RUN_BOOT_LOADER *
[1]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 */
[1645]2113void setup_MR_global_filenames()
[1]2114{
[128]2115 assert(bkpinfo != NULL);
[1]2116
[128]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);
[1]2126
[3833]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);
[128]2131 sprintf(g_imagedevs_restthese, "%s/tmp/imagedevs.restore-these",
[3833]2132 bkpinfo->tmpdir);
[128]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 {
[3833]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);
[128]2139 }
[1]2140}
[128]2141
[1]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 */
[128]2153void streamline_changes_file(char *output_file, char *input_file)
[1]2154{
[128]2155 FILE *fin;
2156 FILE *fout;
[3193]2157 char *incoming = NULL;
[1]2158
[128]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 }
[3193]2169 for (mr_getline(incoming, fin); !feof(fin); mr_getline(incoming, fin)) {
[128]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.. */
[3193]2178 mr_free(incoming);
[128]2179 }
[3193]2180 mr_free(incoming);
[128]2181 paranoid_fclose(fout);
2182 paranoid_fclose(fin);
[1]2183}
[128]2184
[1]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 */
[128]2193void twenty_seconds_til_yikes()
[1]2194{
[128]2195 int i;
2196 /* MALLOC * */
[3193]2197 char *tmp = NULL;
[1]2198
[128]2199 if (does_file_exist("/tmp/NOPAUSE")) {
2200 return;
2201 }
[541]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.",
[128]2205 "", 20);
2206 for (i = 0; i < 20; i++) {
2207 g_current_progress = i;
[3193]2208 mr_asprintf(tmp, "You have %d seconds left to abort.", 20 - i);
[128]2209 update_progress_form(tmp);
[3193]2210 mr_free(tmp);
[128]2211 sleep(1);
2212 }
2213 close_progress_form();
[1]2214}
[128]2215
[1]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 */
[128]2226int unmount_all_devices(struct mountlist_itself
2227 *p_external_copy_of_mountlist)
[1]2228{
[128]2229 struct mountlist_itself *mountlist;
2230 int retval = 0, lino, res = 0, i;
[3193]2231 char *command = NULL;
[2211]2232 char *tmp = NULL;
[1]2233
[128]2234 assert(p_external_copy_of_mountlist != NULL);
[1]2235
[128]2236 mountlist = malloc(sizeof(struct mountlist_itself));
[3301]2237 memcpy((void *) mountlist, (void *) p_external_copy_of_mountlist, sizeof(struct mountlist_itself));
[128]2238 sort_mountlist_by_mountpoint(mountlist, 0);
[1]2239
[3600]2240 run_program_and_log_output("df -m -P -T", 3);
[541]2241 mvaddstr_and_log_it(g_currentY, 0, "Unmounting devices ");
[3301]2242 open_progress_form("Unmounting devices", "Unmounting all devices that were mounted,", "in preparation for the post-restoration reboot.", "", mountlist->entries);
[3076]2243 if (chdir("/")) {
2244 // FIXME
2245 }
[3301]2246 for (i = 0; i < 10 && run_program_and_log_output("ps | grep buffer | grep -v \"grep buffer\"", TRUE) == 0; i++) {
[128]2247 sleep(1);
2248 log_msg(2, "Waiting for buffer() to finish");
2249 }
[1]2250
[3193]2251 sync();
[1]2252
[3544]2253 /* after that what is logged is not copied on disk, typically the result of labelling */
[3185]2254 mr_asprintf(tmp, "cp -f %s " MNT_RESTORING "/var/log", MONDO_LOGFILE);
[1322]2255 if (run_program_and_log_output(tmp, FALSE)) {
[3653]2256 log_msg(1, "Error. Failed to copy log to the machine's /var/log dir. (Mounted read-only?)");
[1]2257 }
[2211]2258 paranoid_free(tmp);
[128]2259 if (does_file_exist("/tmp/DUMBASS-GENTOO")) {
[3301]2260 run_program_and_log_output("mkdir -p " MNT_RESTORING "/mnt/.boot.d", 5);
[1]2261 }
[1971]2262
2263 /* Unmounting the local /proc and /sys first */
[3193]2264 run_program_and_log_output("umount -d " MNT_RESTORING "/proc",3);
2265 run_program_and_log_output("umount -d " MNT_RESTORING "/sys",3);
[1971]2266
[3734]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
[128]2272 for (lino = mountlist->entries - 1; lino >= 0; lino--) {
2273 if (!strcmp(mountlist->el[lino].mountpoint, "lvm")) {
2274 continue;
2275 }
[3185]2276 mr_asprintf(tmp, "Unmounting device %s ", mountlist->el[lino].device);
[128]2277 update_progress_form(tmp);
[2211]2278
[3439]2279 //TODO: this should be done in a certain order normally if there are cascading mount
[128]2280 if (is_this_device_mounted(mountlist->el[lino].device)) {
2281 if (!strcmp(mountlist->el[lino].mountpoint, "swap")) {
[3193]2282 mr_asprintf(command, "swapoff %s", mountlist->el[lino].device);
[128]2283 } else {
2284 if (!strcmp(mountlist->el[lino].mountpoint, "/1")) {
[3193]2285 mr_asprintf(command, "umount -d %s/", MNT_RESTORING);
[128]2286 log_msg(3,
2287 "Well, I know a certain kitty-kitty who'll be sleeping with Mommy tonight...");
2288 } else {
[3193]2289 mr_asprintf(command, "umount -d " MNT_RESTORING "%s", mountlist->el[lino].mountpoint);
[2144]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 }
[128]2298 }
2299 }
2300 log_msg(10, "The 'umount' command is '%s'", command);
2301 res = run_program_and_log_output(command, 3);
[3193]2302 mr_free(command);
[128]2303 } else {
[2211]2304 mr_strcat(tmp, "...not mounted anyway :-) OK");
[128]2305 res = 0;
2306 }
2307 g_current_progress++;
2308 if (res) {
[2211]2309 mr_strcat(tmp, "...Failed");
[128]2310 retval++;
2311 log_to_screen(tmp);
2312 } else {
2313 log_msg(2, tmp);
2314 }
[2211]2315 paranoid_free(tmp);
[128]2316 }
2317 close_progress_form();
2318 if (retval) {
[541]2319 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
[128]2320 } else {
[541]2321 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
[128]2322 }
2323 if (retval) {
[541]2324 log_to_screen("Unable to unmount some of your partitions.");
[128]2325 } else {
[541]2326 log_to_screen("All partitions were unmounted OK.");
[128]2327 }
[3439]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);
[128]2332 free(mountlist);
2333 return (retval);
[1]2334}
[128]2335
[1]2336/**************************************************************************
2337 *END_UNMOUNT_ALL_DEVICES *
2338 **************************************************************************/
2339/* @} - end restoreUtilityGroup */
2340
[2982]2341void wait_until_software_raids_are_prepped(char *mdstat_file, int wait_for_percentage)
[1]2342{
[558]2343 struct raidlist_itself *raidlist;
[2982]2344 int unfinished_mdstat_devices = 9999;
2345 int i = 0;
[3193]2346 char *screen_message = NULL;
[1]2347
[558]2348 raidlist = malloc(sizeof(struct raidlist_itself));
[128]2349
2350 assert(wait_for_percentage <= 100);
[2230]2351 log_it("wait_until_software_raids_are_prepped");
[128]2352 while (unfinished_mdstat_devices > 0) {
[558]2353 // FIXME: Prefix '/dev/' should really be dynamic!
[2982]2354 if (parse_mdstat(MDSTAT_FILE, raidlist, "/dev/")) {
[558]2355 log_to_screen("Sorry, cannot read %s", MDSTAT_FILE);
2356 log_msg(1,"Sorry, cannot read %s", MDSTAT_FILE);
[128]2357 return;
[1]2358 }
[2982]2359 for (unfinished_mdstat_devices = 0; i <= raidlist->entries; i++) {
[558]2360 if (raidlist->el[i].progress < wait_for_percentage) {
[128]2361 unfinished_mdstat_devices++;
[704]2362 if (raidlist->el[i].progress == -1) // delayed while another partition inits
2363 {
2364 continue;
2365 }
[558]2366 log_msg(1,"Sync'ing %s (i=%d)", raidlist->el[i].raid_device, i);
[3193]2367 mr_asprintf(screen_message, "Sync'ing %s", raidlist->el[i].raid_device);
[128]2368 open_evalcall_form(screen_message);
[3193]2369 mr_free(screen_message);
2370
[558]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);
[128]2374 sleep(2);
[558]2375 // FIXME: Prefix '/dev/' should really be dynamic!
[2982]2376 if (parse_mdstat(MDSTAT_FILE, raidlist, "/dev/")) {
[128]2377 break;
2378 }
2379 }
2380 close_evalcall_form();
2381 }
[1]2382 }
[128]2383 }
[558]2384 paranoid_free(raidlist);
[3194]2385}
Note: See TracBrowser for help on using the repository browser.