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

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

Fix compiler errors

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