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

Last change on this file since 3898 was 3893, checked in by Bruno Cornec, 17 months ago

Remove more warnings - switch and size_t/int comparisons

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