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

Last change on this file since 3911 was 3901, checked in by Bruno Cornec, 15 months ago

mount_media done in what_number_cd_is_this

  • Property svn:keywords set to Id
File size: 74.6 KB
RevLine 
[1]1/***************************************************************************
[2029]2$Id: mondo-rstr-tools.c 3901 2024-12-17 22:28:41Z 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}
[1736]633i = what_number_cd_is_this(); /* has the side-effect of calling mount_media() */
[2242]634mds = media_descriptor_string(bkpinfo->backup_media_type);
635mr_free(mds);
636
[3193]637log_msg(1, "%s #%d has been mounted via loopback mount", mds, i);
[1242]638if (i < 0) {
[3193]639 popup_and_OK("Cannot find ISO images in the directory you specified.");
640 retval = 1;
[1242]641}
[3193]642log_msg(2, "bkpinfo->isodir is now %s", bkpinfo->isodir);
[1242]643return (retval);
644}
[1]645
646
647
648
649/**
[1242]650* Kill all Petris processes.
651*/
[1850]652void kill_petris(void) {
[1852]653 kill_anything_like_this("petris");
[1]654}
[128]655
[1]656/**************************************************************************
[1242]657*END_KILL_PETRIS *
658**************************************************************************/
[1]659
660
661/**
[1843]662 * Mount @p device at @p mpt as @p format.
663 * @param device The device (/dev entry) to mount.
664 * @param mpt The directory to mount it on.
665 * @param format The filesystem type of @p device.
666 * @param writeable If TRUE, mount read-write; if FALSE, mount read-only.
667 * @return 0 for success, nonzero for failure.
668 */
[1771]669int mount_device(char *device, char *mpt, char *format, bool writeable)
670{
671int res = 0;
672
673/** malloc **/
[3193]674char *tmp = NULL;
675char *command = NULL;
676char *mountdir = NULL;
677char *mountpoint = NULL;
[2211]678char *additional_parameters = NULL;
[1771]679
680assert_string_is_neither_NULL_nor_zerolength(device);
681assert_string_is_neither_NULL_nor_zerolength(mpt);
682assert(format != NULL);
683
[1843]684 if (!strcmp(mpt, "/1")) {
[3193]685 mr_asprintf(mountpoint, "/");
[1843]686 log_msg(3, "Mommm! SME is being a dildo!");
687 } else {
[3193]688 mr_asprintf(mountpoint, "%s", mpt);
[1843]689 }
[1771]690
[1843]691 if (!strcmp(mountpoint, "lvm")) {
692 return (0);
693 }
694 if (!strcmp(mountpoint, "image")) {
695 return (0);
696 }
[3193]697 mr_asprintf(tmp, "Mounting device %s ", device);
[1843]698 log_msg(1, tmp);
[1937]699 /* Deal with additional params only if not /proc or /sys */
[3185]700 mr_asprintf(additional_parameters, "");
[1937]701 if (strcmp(format, "proc") && strcmp(format, "sys")) {
702 if (writeable) {
[2211]703 mr_strcat(additional_parameters, "-o rw");
[1937]704 } else {
[2211]705 mr_strcat(additional_parameters, "-o ro");
[1937]706 }
[3866]707 if ((tmp = find_home_of_exe("setfattr")) != NULL) {
[2211]708 mr_strcat(additional_parameters, ",user_xattr");
[1937]709 }
[3866]710 mr_free(tmp);
711 if ((tmp = find_home_of_exe("setfacl")) != NULL) {
[2211]712 mr_strcat(additional_parameters, ",acl");
[1937]713 }
[3866]714 mr_free(tmp);
[1843]715 }
[1771]716
717 if (!strcmp(mountpoint, "swap")) {
[3193]718 mr_asprintf(command, "swapon %s", device);
719 mr_asprintf(mountdir, "swap");
[1771]720 } else {
[1843]721 if (!strcmp(mountpoint, "/")) {
[3193]722 mr_asprintf(mountdir, "%s", MNT_RESTORING);
[1843]723 } else {
[3193]724 mr_asprintf(mountdir, "%s%s", MNT_RESTORING, mountpoint);
[1843]725 }
[3193]726 mr_asprintf(command, "mkdir -p %s", mountdir);
[1843]727 run_program_and_log_output(command, FALSE);
[3193]728 mr_free(command);
729
730 mr_asprintf(command, "mount -t %s %s %s %s 2>> %s", format, device, additional_parameters, mountdir, MONDO_LOGFILE);
[1843]731 log_msg(2, "command='%s'", command);
732 }
[3193]733 mr_free(additional_parameters);
[1843]734
735 res = run_program_and_log_output(command, TRUE);
736 if (res && (strstr(command, "xattr") || strstr(command, "acl"))) {
737 log_msg(1, "Re-trying without the fancy extra parameters");
[3193]738 mr_free(command);
739 mr_asprintf(command, "mount -t %s %s %s 2>> %s", format, device, mountdir, MONDO_LOGFILE);
[1771]740 res = run_program_and_log_output(command, TRUE);
[1843]741 }
742 if (res) {
[3193]743 log_msg(1, "Unable to mount device %s (type %s) at %s", device, format, mountdir);
[1843]744 log_msg(1, "command was '%s'", command);
745 if (!strcmp(mountpoint, "swap")) {
746 log_to_screen(tmp);
[1771]747 } else {
[1843]748 log_msg(2, "Retrying w/o the '-t' switch");
[3193]749 mr_free(command);
750 mr_asprintf(command, "mount %s %s 2>> %s", device, mountdir, MONDO_LOGFILE);
[1843]751 log_msg(2, "2nd command = '%s'", command);
752 res = run_program_and_log_output(command, TRUE);
753 if (res == 0) {
754 log_msg(1,
755 "That's OK. I called mount w/o a filesystem type and it worked fine in the end.");
756 } else {
757 log_to_screen(tmp);
758 }
[1771]759 }
760 }
761
[1843]762 if (res && !strcmp(mountpoint, "swap")) {
763 log_msg(2, "That's ok. It's just a swap partition.");
764 log_msg(2, "Non-fatal error. Returning 0.");
765 res = 0;
766 }
767
[3193]768mr_free(tmp);
769mr_free(command);
770mr_free(mountdir);
771mr_free(mountpoint);
[1771]772
[3193]773return (res);
[1771]774}
775/**************************************************************************
[1843]776 *END_MOUNT_DEVICE *
[1771]777**************************************************************************/
778
779
780/**
[1843]781 * Mount all devices in @p p_external_copy_of_mountlist on @p MNT_RESTORING.
782 * @param p_external_copy_of_mountlist The mountlist containing devices to be mounted.
783 * @param writeable If TRUE, then mount read-write; if FALSE mount read-only.
784 * @return The number of errors encountered (0 for success).
785 */
[3193]786int mount_all_devices(struct mountlist_itself *p_external_copy_of_mountlist, bool writeable)
[1]787{
[1242]788int retval = 0, lino, res;
[3193]789char *tmp = NULL;
[2211]790char *these_failed = NULL;
791struct mountlist_itself *mountlist = NULL;
[1]792
[1242]793assert(p_external_copy_of_mountlist != NULL);
794mountlist = malloc(sizeof(struct mountlist_itself));
795memcpy((void *) mountlist, (void *) p_external_copy_of_mountlist,
796 sizeof(struct mountlist_itself));
[1843]797 sort_mountlist_by_mountpoint(mountlist, 0);
[1]798
799
[1843]800 mvaddstr_and_log_it(g_currentY, 0, "Mounting devices ");
[3193]801 open_progress_form("Mounting devices", "I am now mounting all the drives.", "This should not take long.", "", mountlist->entries);
[128]802
[3185]803 mr_asprintf(these_failed, "");
[1843]804 for (lino = 0; lino < mountlist->entries; lino++) {
805 if (!strcmp(mountlist->el[lino].device, "/proc")) {
[3193]806 log_msg(1, "Again with the /proc - why is this in your mountlist?");
[1843]807 } else if (is_this_device_mounted(mountlist->el[lino].device)) {
[3193]808 log_to_screen("%s is already mounted", mountlist->el[lino].device);
[1843]809 } else if (strcmp(mountlist->el[lino].mountpoint, "none")
[1242]810 && strcmp(mountlist->el[lino].mountpoint, "lvm")
811 && strcmp(mountlist->el[lino].mountpoint, "raid")
812 && strcmp(mountlist->el[lino].mountpoint, "image")) {
[3193]813 mr_asprintf(tmp, "Mounting %s", mountlist->el[lino].device);
[1843]814 update_progress_form(tmp);
[3193]815 mr_free(tmp);
816 res = mount_device(mountlist->el[lino].device, mountlist->el[lino].mountpoint, mountlist->el[lino].format, writeable);
[1843]817 retval += res;
818 if (res) {
[2211]819 mr_strcat(these_failed, "%s ",mountlist->el[lino].device);
[1843]820 }
821 }
822 g_current_progress++;
[1]823 }
[1843]824 close_progress_form();
825 if (retval) {
826 if (g_partition_table_locked_up > 0) {
[3193]827 log_to_screen("fdisk's ictol() call to refresh its copy of the partition table causes the kernel to");
828 log_to_screen("lock up the partition table. You might have to reboot and use Interactive Mode to");
829 log_to_screen("format and restore *without* partitioning first. Sorry for the inconvenience.");
[1843]830 }
[3193]831 mr_asprintf(tmp, "Could not mount device(s) %s- shall I abort?", these_failed);
[1843]832
833 if (!ask_me_yes_or_no(tmp)) {
834 retval = 0;
[3193]835 log_to_screen("Continuing, although some device(s) failed to be mounted");
[1843]836 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
837 } else {
838 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
[3193]839 log_to_screen("Unable to mount some or all of your partitions.");
[1843]840 }
[3193]841 mr_free(tmp);
[1843]842 } else {
843 log_to_screen("All partitions were mounted OK.");
844 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
845 }
[2211]846 paranoid_free(these_failed);
847
[1937]848 /* Also mounting under MNT_RESTORING special FS */
849 (void)mount_device("/proc","/proc","proc",TRUE);
850 (void)mount_device("/sys","/sys","sysfs",TRUE);
[3743]851 /* BR#852: we need to create BTRFS subvols now */
852 /* Creating potential btrfs subvolumes */
853 if (does_file_exist("/tmp/create-btrfs-subvol")) {
854 run_program_and_log_output("/tmp/create-btrfs-subvol",3);
855 }
856 /* Mounting potential btrfs subvolumes */
857 if (does_file_exist("/tmp/mount-btrfs-subvol")) {
858 run_program_and_log_output("/tmp/mount-btrfs-subvol",3);
859 }
[3600]860 run_program_and_log_output("df -m -P -T", 3);
[1843]861 paranoid_free(mountlist);
862 return (retval);
[1242]863}
864/**************************************************************************
865*END_MOUNT_ALL_DEVICES *
866**************************************************************************/
[1]867
868
869/**
[1242]870* Fix some miscellaneous things in the filesystem so the system will come
871* up correctly on the first boot.
872*/
[3301]873void protect_against_braindead_sysadmins() {
874
[3601]875run_program_and_log_output("touch " MNT_RESTORING "/var/log/pacct", 0);
876run_program_and_log_output("touch " MNT_RESTORING "/var/account/pacct", 0);
877
878/* Log the status of the boot area */
879run_program_and_log_output("ls -alR " MNT_RESTORING "/boot", 0);
880
881if (run_program_and_log_output("ls -al " MNT_RESTORING " /tmp", 0)) {
882 run_program_and_log_output("chmod 1777 " MNT_RESTORING "/tmp", 0);
[1]883}
[3601]884run_program_and_log_output("mkdir -p " MNT_RESTORING "/var/run/console", 0);
[3634]885run_program_and_log_output("chmod 666 " MNT_RESTORING "/dev/null", 0);
[3635]886/* This doesn't work as the mkdir should be done in home ! Maybe time to remove
887 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);
888 */
[3601]889run_program_and_log_output("rm -f " MNT_RESTORING "/var/run/*.pid", 1);
890run_program_and_log_output("rm -f " MNT_RESTORING "/var/lock/subsys/*", 1);
[1242]891}
[128]892
[1]893/**************************************************************************
[1242]894*END_PROTECT_AGAINST_BRAINDEAD_SYSADMINS *
895**************************************************************************/
[1]896
897
898
899
900/**
[1242]901* Fill out @p bkpinfo based on @p cfg_file.
[3271]902* @param cfg_file The mondorestore.cfg file to read into @p bkpinfo.
[1242]903* @param bkpinfo The backup information structure to fill out with information
904* from @p cfg_file.
905* @return 0 for success, nonzero for failure.
906*/
[1645]907int read_cfg_file_into_bkpinfo(char *cfgf)
[1]908{
[1242]909/** add mallocs **/
[3193]910char value[MAX_STR_LEN];
[3879]911char *tmp = NULL;
[3193]912char *tmp1 = NULL;
[1242]913char *envtmp1 = NULL;
914char *envtmp2 = NULL;
915char *command = NULL;
[3193]916char iso_mnt[MAX_STR_LEN];
917char iso_path[MAX_STR_LEN];
[1242]918char *old_isodir = NULL;
[3193]919char *cfg_file = NULL;
[1242]920t_bkptype media_specified_by_user;
[1]921
922// assert_string_is_neither_NULL_nor_zerolength(cfg_file);
[1242]923assert(bkpinfo != NULL);
[3193]924assert(cfgf != NULL);
[3158]925log_it("Entering read_cfg_file_into_bkpinfo");
[1]926
[1242]927if (!cfgf) {
[3211]928 mr_asprintf(cfg_file, "%s", g_mondo_cfg_file);
[1242]929} else {
[3211]930 mr_asprintf(cfg_file, "%s", cfgf);
[1242]931}
[1]932
[1242]933media_specified_by_user = bkpinfo->backup_media_type; // or 'none', if not specified
[1]934
[1242]935if (0 == read_cfg_var(cfg_file, "backup-media-type", value)) {
[3882]936 if (!strcmp(value, "optical")) {
[3881]937 bkpinfo->backup_media_type = optical;
[3193]938 } else if (!strcmp(value, "usb")) {
939 bkpinfo->backup_media_type = usb;
940 bkpinfo->please_dont_eject = TRUE;
941 } else if (!strcmp(value, "iso")) {
942 bkpinfo->backup_media_type = iso;
943 if (am_I_in_disaster_recovery_mode()) {
944 /* Check to see if CD is already mounted before mounting it... */
945 if (!is_this_device_mounted("/dev/cdrom")) {
946 log_msg(2, "NB: CDROM device not mounted, mounting...");
947 run_program_and_log_output("mount /dev/cdrom "MNT_CDROM, 1);
948 }
[3879]949 if (does_file_exist(ARCHIVES_PATH "/filelist.0")) {
[3881]950 bkpinfo->backup_media_type = optical;
[3193]951 run_program_and_log_output("umount -d "MNT_CDROM, 1);
[3881]952 log_it("Re-jigging configuration AGAIN. Optical, not ISO.");
[3193]953 }
[128]954 }
[3193]955 if (read_cfg_var(cfg_file, "iso-prefix", value) == 0) {
[3835]956 mr_asprintf(bkpinfo->prefix, "%s", value);
[3193]957 } else {
[3835]958 mr_asprintf(bkpinfo->prefix, "%s", STD_PREFIX);
[949]959 }
[3193]960 log_it("Setting Prefix to %s", bkpinfo->prefix);
961 } else if ((!strcmp(value, "netfs")) || (!strcmp(value, "nfs"))) {
962 /* Stay compatible with previous versions by allowing nfs as an entry here */
963 bkpinfo->backup_media_type = netfs;
964 bkpinfo->please_dont_eject = TRUE;
965 if (read_cfg_var(cfg_file, "netfs-proto", value) == 0) {
966 mr_asprintf(bkpinfo->netfs_proto,"%s", value);
967 } else {
968 /* For compatibility, force protocol in old nfs case to be transparent */
969 mr_asprintf(bkpinfo->netfs_proto, "nfs");
970 }
971 if (read_cfg_var(cfg_file, "netfs-server-user", value) == 0) {
972 mr_asprintf(bkpinfo->netfs_user,"%s", value);
973 }
974 if (read_cfg_var(cfg_file, "iso-prefix", value) == 0) {
[3835]975 mr_asprintf(bkpinfo->prefix, "%s", value);
[3193]976 } else {
[3835]977 mr_asprintf(bkpinfo->prefix, "%s", STD_PREFIX);
[3193]978 }
[3610]979 tmp1 = call_program_and_get_last_line_of_output("cat "CMDLINE);
980 if (strstr(tmp1, "pxe")) {
[3193]981 /* We need to override prefix value in PXE mode as it's
982 * already done in start-netfs */
983 envtmp1 = getenv("imgname");
984 if (envtmp1 == NULL) {
985 fatal_error("no imgname variable in environment");
986 }
[3835]987 mr_asprintf(bkpinfo->prefix, "%s", envtmp1);
[3193]988 }
[3610]989 mr_free(tmp1);
[3193]990 } else if (!strcmp(value, "tape")) {
991 bkpinfo->backup_media_type = tape;
992 } else if (!strcmp(value, "udev")) {
993 bkpinfo->backup_media_type = udev;
994 } else {
995 fatal_error("UNKNOWN bkp-media-type");
[949]996 }
[1242]997} else {
[3193]998 fatal_error("backup-media-type not specified!");
[1242]999}
[3193]1000
[1242]1001if (bkpinfo->disaster_recovery) {
[3882]1002 if (bkpinfo->backup_media_type == usb) {
[2127]1003 envtmp1 = getenv("MRUSBDEV");
1004 if (envtmp1 == NULL) {
1005 if (read_cfg_var(cfg_file, "usb-dev", value)) {
1006 fatal_error("Cannot get USB device name from cfg file");
1007 }
1008 } else {
1009 strcpy(value,envtmp1);
[1736]1010 }
[3822]1011 mr_asprintf(bkpinfo->media_device, "%s1", value);
[3193]1012 log_msg(2, "Backup medium is USB --- dev=%s", bkpinfo->media_device);
1013 } else if (bkpinfo->backup_media_type == tape || bkpinfo->backup_media_type == udev) {
[1736]1014 if (read_cfg_var(cfg_file, "media-dev", value)) {
1015 fatal_error("Cannot get tape device name from cfg file");
1016 }
[3822]1017 mr_asprintf(bkpinfo->media_device, "%s", value);
[1736]1018 read_cfg_var(cfg_file, "media-size", value);
[3150]1019 bkpinfo->media_size = atol(value);
[3193]1020 log_msg(2, "Backup medium is TAPE --- dev=%s", bkpinfo->media_device);
[1736]1021 } else {
[3822]1022 mr_asprintf(bkpinfo->media_device, "%s", "/dev/cdrom"); /* we don't really need this var */
[3150]1023 bkpinfo->media_size = 1999 * 1024; /* 650, probably, but we don't need this var anyway */
[3158]1024 log_msg(2, "Backup medium is similar to CD-R[W]");
[1]1025 }
[1242]1026} else {
[3193]1027 log_msg(2, "Not in Disaster Recovery Mode. No need to derive device name from config file.");
[1242]1028}
[128]1029
[1242]1030read_cfg_var(cfg_file, "use-star", value);
1031if (strstr(value, "yes")) {
[1948]1032 bkpinfo->use_star = TRUE;
1033 log_msg(1, "Goody! ... bkpinfo->use_star is now true.");
[1242]1034}
1035
[1948]1036read_cfg_var(cfg_file, "obdr", value);
1037if (strstr(value, "TRUE")) {
1038 bkpinfo->use_obdr = TRUE;
1039 log_msg(1, "OBDR mode activated");
1040}
1041
[1242]1042read_cfg_var(cfg_file, "acl", value);
1043if (strstr(value, "TRUE")) {
[3866]1044 if ((tmp = find_home_of_exe("setfacl")) == NULL) {
[1948]1045 log_msg(1, "Unable to restore ACLs as no setfacl found");
[3866]1046 } else {
1047 mr_asprintf(g_getfacl,"%s", tmp);
1048 log_msg(1, "We will restore ACLs");
[1948]1049 }
[3866]1050 mr_free(tmp);
[1242]1051}
1052read_cfg_var(cfg_file, "xattr", value);
1053if (strstr(value, "TRUE")) {
[3866]1054 if ((tmp = find_home_of_exe("setfattr")) == NULL) {
[1948]1055 log_msg(1, "Unable to restore XATTRs as no setfattr found");
[3866]1056 } else {
1057 mr_asprintf(g_getfattr,"%s", tmp);
1058 log_msg(1, "We will restore XATTRs");
[1948]1059 }
[3866]1060 mr_free(tmp);
[1242]1061}
1062
1063if (0 == read_cfg_var(cfg_file, "internal-tape-block-size", value)) {
[3193]1064 bkpinfo->internal_tape_block_size = atol(value);
[1242]1065} else {
[3193]1066 bkpinfo->internal_tape_block_size = DEFAULT_INTERNAL_TAPE_BLOCK_SIZE;
[1242]1067}
[3193]1068log_msg(1, "Internal tape block size set to %ld", bkpinfo->internal_tape_block_size);
[1242]1069
[3193]1070read_cfg_var(cfg_file, "use-lzma", value);
1071if (strstr(value, "yes")) {
1072 bkpinfo->use_lzma = TRUE;
1073 bkpinfo->use_lzo = FALSE;
1074 bkpinfo->use_gzip = FALSE;
[3830]1075 mr_asprintf(bkpinfo->zip_exe, "%s", "lzma");
1076 mr_asprintf(bkpinfo->zip_suffix, "%s", "lzma");
[3193]1077}
1078
[1242]1079read_cfg_var(cfg_file, "use-lzo", value);
1080if (strstr(value, "yes")) {
[3193]1081 bkpinfo->use_lzma = FALSE;
1082 bkpinfo->use_lzo = TRUE;
1083 bkpinfo->use_gzip = FALSE;
[3830]1084 mr_asprintf(bkpinfo->zip_exe, "%s", "lzop");
1085 mr_asprintf(bkpinfo->zip_suffix, "%s", "lzo");
[3193]1086}
1087
[1242]1088read_cfg_var(cfg_file, "use-gzip", value);
1089if (strstr(value, "yes")) {
[3193]1090 bkpinfo->use_lzma = FALSE;
[1242]1091 bkpinfo->use_lzo = FALSE;
1092 bkpinfo->use_gzip = TRUE;
[3830]1093 mr_asprintf(bkpinfo->zip_exe, "%s", "gzip");
1094 mr_asprintf(bkpinfo->zip_suffix, "%s", "gz");
[1242]1095}
[3193]1096
1097read_cfg_var(cfg_file, "use-comp", value);
1098if (strstr(value, "yes")) {
1099 bkpinfo->use_lzma = FALSE;
1100 bkpinfo->use_lzo = FALSE;
1101 bkpinfo->use_gzip = FALSE;
[3830]1102 mr_asprintf(bkpinfo->zip_exe, "%s", "xz");
1103 mr_asprintf(bkpinfo->zip_suffix, "%s", "xz");
[1242]1104}
[1]1105
[1242]1106value[0] = '\0';
1107read_cfg_var(cfg_file, "differential", value);
1108if (!strcmp(value, "yes") || !strcmp(value, "1")) {
[3193]1109 bkpinfo->differential = TRUE;
[1242]1110}
1111log_msg(2, "differential var = '%s'", value);
1112if (bkpinfo->differential) {
[3193]1113 log_msg(2, "THIS IS A DIFFERENTIAL BACKUP");
[1242]1114} else {
[3193]1115 log_msg(2, "This is a regular (full) backup");
[1242]1116}
1117
[3211]1118read_cfg_var(cfg_file, "please-dont-eject", value);
[3610]1119tmp1 = call_program_and_get_last_line_of_output("cat "CMDLINE);
1120if (value[0] || strstr(tmp1, "donteject")) {
[3158]1121 bkpinfo->please_dont_eject = TRUE;
1122 log_msg(2, "Ok, I shan't eject when restoring! Groovy.");
[1242]1123}
[3610]1124mr_free(tmp1);
[1242]1125
[3430]1126/* TODO: Read here the boot_* variables */
[3397]1127read_cfg_var(cfg_file, "boot-type", value);
1128if (!strcmp(value, "BIOS")) {
1129 bkpinfo->boot_type = BIOS;
1130} else if (!strcmp(value, "UEFI")) {
1131 bkpinfo->boot_type = UEFI;
1132} else {
1133 log_msg(1, "No known boot type found");
1134}
1135log_msg(1, "Found %s boot type",value);
1136
[2380]1137if (bkpinfo->backup_media_type == netfs) {
[1736]1138 if (!cfgf) {
[3193]1139 if (bkpinfo->netfs_mount) {
1140 log_msg(2, "netfs_mount remains %s", bkpinfo->netfs_mount);
1141 }
[3207]1142 if (bkpinfo->netfs_remote_dir != NULL) {
[3193]1143 log_msg(2, "netfs_remote_dir remains %s", bkpinfo->netfs_remote_dir);
1144 }
1145 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]1146 } else {
[3211]1147 read_cfg_var(cfg_file, "netfs-server-mount", value);
[3193]1148 mr_free(bkpinfo->netfs_mount);
1149 mr_asprintf(bkpinfo->netfs_mount, "%s", value);
[3278]1150 if (bkpinfo->netfs_mount != NULL) {
1151 log_msg(2, "netfs_mount is %s", bkpinfo->netfs_mount);
1152 }
[3193]1153
[3278]1154 read_cfg_var(cfg_file, "iso-dir", value);
[3193]1155 mr_free(bkpinfo->netfs_remote_dir);
1156 mr_asprintf(bkpinfo->netfs_remote_dir, "%s", value);
1157 if (bkpinfo->netfs_remote_dir != NULL) {
1158 log_msg(2, "netfs_remote_dir is %s", bkpinfo->netfs_remote_dir);
1159 }
[3278]1160
[3193]1161 if (bkpinfo->netfs_proto != NULL) {
1162 log_msg(2, "netfs_proto is %s", bkpinfo->netfs_proto);
1163 }
1164 if (bkpinfo->netfs_user != NULL) {
[2847]1165 log_msg(2, "netfs_user is %s", bkpinfo->netfs_user);
1166 }
[1]1167 }
[3610]1168 tmp1 = call_program_and_get_last_line_of_output("cat "CMDLINE);
1169 if (strstr(tmp1, "pxe")) {
[1968]1170 /* We need to override values in PXE mode as it's
[2380]1171 * already done in start-netfs */
1172 envtmp1 = getenv("netfsmount");
[1736]1173 if (envtmp1 == NULL) {
[2380]1174 fatal_error("no netfsmount variable in environment");
[1736]1175 }
1176 envtmp2 = getenv("dirimg");
1177 if (envtmp2 == NULL) {
1178 fatal_error("no dirimg variable in environment");
1179 }
[3193]1180 mr_free(bkpinfo->netfs_mount);
1181 mr_asprintf(bkpinfo->netfs_mount, "%s", envtmp1);
1182
1183 mr_free(bkpinfo->netfs_remote_dir);
1184 mr_asprintf(bkpinfo->netfs_remote_dir, "%s", envtmp2);
[1]1185 }
[3610]1186 mr_free(tmp1);
[1242]1187} else if (bkpinfo->backup_media_type == iso) {
[1736]1188 /* Patch by Conor Daly 23-june-2004
1189 * to correctly mount iso-dev and set a sensible
1190 * isodir in disaster recovery mode
1191 */
[3193]1192 mr_asprintf(old_isodir, "%s", bkpinfo->isodir);
[3836]1193 if (read_cfg_var(cfg_file, "iso-mnt", iso_mnt) != 0) {
1194 iso_mnt[0] = '\0';
[128]1195 }
[3836]1196 if (read_cfg_var(cfg_file, "iso-dir", iso_path) != 0) {
1197 iso_path[0] = '\0';
1198 }
1199 mr_asprintf(bkpinfo->isodir, "%s%s", iso_mnt, iso_path);
[3839]1200 if (!strcmp(bkpinfo->isodir,"")) {
[3836]1201 mr_asprintf(bkpinfo->isodir, "%s", old_isodir);
1202 }
1203
[3158]1204 if ((!bkpinfo->disaster_recovery) || (g_isodir_device[0] != '\0')) {
[1736]1205 if (strcmp(old_isodir, bkpinfo->isodir)) {
[3193]1206 log_it("user nominated isodir %s differs from archive, keeping user's choice: %s\n", bkpinfo->isodir, old_isodir );
[3836]1207 mr_asprintf(bkpinfo->isodir, "%s", old_isodir);
[1736]1208 }
1209 }
[3211]1210 mr_free(old_isodir);
1211
[3158]1212 if (g_isodir_device[0] == '\0') {
1213 /* We don't want to loose our conf made earlier in iso_fiddly_bits
1214 * so go here only if job not done already */
[3211]1215 read_cfg_var(cfg_file, "iso-dev", g_isodir_device);
[3158]1216 }
1217
1218 log_msg(2, "isodir=%s; iso-dev=%s", bkpinfo->isodir, g_isodir_device);
[3193]1219
[1736]1220 if (bkpinfo->disaster_recovery) {
1221 if (is_this_device_mounted(g_isodir_device)) {
1222 log_msg(2, "NB: isodir is already mounted");
1223 /* Find out where it's mounted */
[3193]1224 mr_asprintf(command, "mount | grep -E '^%s' | tail -n1 | cut -d' ' -f3", g_isodir_device);
[1736]1225 log_it("command = %s", command);
[3610]1226 tmp1 = call_program_and_get_last_line_of_output(command);
1227 log_it("res of it = %s", tmp1);
1228 sprintf(iso_mnt, "%s", tmp1);
[3193]1229 mr_free(command);
[3610]1230 mr_free(tmp1);
[1736]1231 } else {
[3194]1232 sprintf(iso_mnt, "/tmp/isodir");
[3193]1233 mr_asprintf(tmp1, "mkdir -p %s", iso_mnt);
1234 run_program_and_log_output(tmp1, 5);
1235 mr_free(tmp1);
1236
1237 mr_asprintf(tmp1, "mount %s %s", g_isodir_device, iso_mnt);
1238 if (run_program_and_log_output(tmp1, 3)) {
[3881]1239 log_msg(1, "Unable to mount isodir. Perhaps this is really an optical based backup?");
1240 bkpinfo->backup_media_type = optical;
[3822]1241 mr_asprintf(bkpinfo->media_device, "%s", "/dev/cdrom"); /* superfluous */
[3836]1242 mr_free(bkpinfo->isodir);
1243 iso_mnt[0] = iso_path[0] = '\0';
[3876]1244 if (mount_media(MNT_CDROM)) {
[3193]1245 mr_free(tmp1);
1246 fatal_error("Unable to mount isodir. Failed to mount CD-ROM as well.");
[1736]1247 } else {
[3193]1248 log_msg(1, "You backed up to disk, then burned some CDs.");
[1736]1249 }
[128]1250 }
[3193]1251 mr_free(tmp1);
[128]1252 }
[1736]1253 /* bkpinfo->isodir should now be the true path to prefix-1.iso etc... */
[3193]1254 /* except when already done in iso_fiddly_bits */
[3158]1255 if ((bkpinfo->backup_media_type == iso) && (g_isodir_device[0] == '\0')) {
[3836]1256 mr_asprintf(bkpinfo->isodir, "%s%s", iso_mnt, iso_path);
[1736]1257 }
[1]1258 }
[1242]1259}
[3211]1260mr_free(cfg_file);
[1]1261
[1242]1262if (media_specified_by_user != none) {
[1736]1263 if (g_restoring_live_from_cd) {
1264 if (bkpinfo->backup_media_type != media_specified_by_user) {
[3158]1265 log_msg(2, "bkpinfo->backup_media_type != media_specified_by_user, so I'd better ask :)");
[1736]1266 interactively_obtain_media_parameters_from_user(FALSE);
1267 media_specified_by_user = bkpinfo->backup_media_type;
1268 get_cfg_file_from_archive();
1269 }
[128]1270 }
[1736]1271 bkpinfo->backup_media_type = media_specified_by_user;
[1242]1272}
1273g_backup_media_type = bkpinfo->backup_media_type;
1274return (0);
[1]1275
1276}
[128]1277
[1]1278/**************************************************************************
[1242]1279*END_READ_CFG_FILE_INTO_BKPINFO *
1280**************************************************************************/
[1]1281
1282
1283
[128]1284
[1]1285/**
[1843]1286 * Allow the user to edit the filelist and biggielist.
1287 * The filelist is unlinked after it is read.
1288 * @param bkpinfo The backup information structure. Fields used:
1289 * - @c bkpinfo->backup_media_type
1290 * - @c bkpinfo->isodir
1291 * - @c bkpinfo->media_device
1292 * - @c bkpinfo->tmpdir
1293 * @return The filelist structure containing the information read from disk.
1294 */
[1]1295struct
[1645]1296s_node *process_filelist_and_biggielist()
[1]1297{
[1242]1298struct s_node *filelist;
[1]1299
[3193]1300char *command = NULL;
[3613]1301char *old_pwd = NULL;
[3076]1302char *q;
[1242]1303int res = 0;
1304pid_t pid;
[2049]1305bool extract_mountlist_stub = FALSE;
[1]1306
[1242]1307assert(bkpinfo != NULL);
[1]1308
[2049]1309/* If those files already exist, do not overwrite them later on */
1310if (does_file_exist("/"MOUNTLIST_FNAME_STUB)) {
1311 extract_mountlist_stub = FALSE;
1312} else {
1313 extract_mountlist_stub = TRUE;
1314}
1315
[1242]1316if (does_file_exist(g_filelist_full)
1317&& does_file_exist(g_biggielist_txt)) {
[1987]1318 log_msg(1, "%s exists", g_filelist_full);
1319 log_msg(1, "%s exists", g_biggielist_txt);
1320 log_msg(2,
[1242]1321 "Filelist and biggielist already recovered from media. Yay!");
1322} else {
[3613]1323 old_pwd = mr_getcwd();
[3076]1324 if (chdir(bkpinfo->tmpdir)) {
1325 // FIXME
1326 }
[1987]1327 log_msg(1, "chdir(%s)", bkpinfo->tmpdir);
1328 log_to_screen("Extracting filelist and biggielist from media...");
1329 unlink("/tmp/filelist.full");
[2029]1330 unlink(FILELIST_FULL_STUB);
[1987]1331 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
[3193]1332 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]1333 log_msg(1, "tarcommand = %s", command);
1334 run_program_and_log_output(command, 1);
[3193]1335 mr_free(command);
1336
[2029]1337 if (!does_file_exist(FILELIST_FULL_STUB)) {
1338 /* Doing that allow us to remain compatible with pre-2.2.5 versions */
1339 log_msg(2, "pre-2.2.4 compatible mode on");
[3193]1340 mr_asprintf(command, "tar -b %ld -zxf %s %s %s %s %s %s", bkpinfo->internal_tape_block_size, bkpinfo->media_device, MOUNTLIST_FNAME_STUB, BIGGIELIST_TXT_STUB, FILELIST_FULL_STUB, IWANTMYLVM_STUB, MONDO_CFG_FILE_STUB);
[2029]1341 log_msg(1, "tarcommand = %s", command);
1342 run_program_and_log_output(command, 1);
[3193]1343 mr_free(command);
[2029]1344 }
[1987]1345 } else {
[3193]1346 log_msg(2, "Calling insist_on_this_cd_number; bkpinfo->isodir=%s", bkpinfo->isodir);
[1987]1347 insist_on_this_cd_number(1);
1348 log_msg(2, "Back from iotcn");
1349 run_program_and_log_output("mount", 1);
[3193]1350 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]1351
[1987]1352 log_msg(1, "tarcommand = %s", command);
1353 run_program_and_log_output(command, 1);
[3193]1354 mr_free(command);
1355
[2029]1356 if (!does_file_exist(FILELIST_FULL_STUB)) {
1357 /* Doing that allow us to remain compatible with pre-2.2.5 versions */
1358 log_msg(2, "pre-2.2.4 compatible mode on");
[3193]1359 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]1360
1361 log_msg(1, "tarcommand = %s", command);
1362 run_program_and_log_output(command, 1);
[3193]1363 mr_free(command);
[2029]1364 }
[1987]1365 if (!does_file_exist(BIGGIELIST_TXT_STUB)) {
[3193]1366 fatal_error("all.tar.gz did not include " BIGGIELIST_TXT_STUB);
[1987]1367 }
1368 if (!does_file_exist(FILELIST_FULL_STUB)) {
[3193]1369 fatal_error("all.tar.gz did not include " FILELIST_FULL_STUB);
[1987]1370 }
[128]1371 }
[3193]1372 mr_asprintf(command, "cp -f %s %s", MONDO_CFG_FILE_STUB, g_mondo_cfg_file);
[1987]1373 run_program_and_log_output(command, FALSE);
[3193]1374 mr_free(command);
[1]1375
[3193]1376 mr_asprintf(command, "cp -f %s/%s %s", bkpinfo->tmpdir, BIGGIELIST_TXT_STUB, g_biggielist_txt);
[1987]1377 log_msg(1, "command = %s", command);
1378 paranoid_system(command);
[3193]1379 mr_free(command);
1380
1381 mr_asprintf(command, "ln -sf %s/%s %s", bkpinfo->tmpdir, FILELIST_FULL_STUB, g_filelist_full);
[1987]1382 log_msg(1, "command = %s", command);
1383 paranoid_system(command);
[3193]1384 mr_free(command);
[1987]1385 }
[1242]1386
[1987]1387 if (am_I_in_disaster_recovery_mode()
1388 &&
[2049]1389 /* If it was there, do not overwrite it */
1390 (extract_mountlist_stub)
1391 &&
[1987]1392 ask_me_yes_or_no("Do you want to retrieve the mountlist as well?"))
1393 {
[3193]1394 mr_asprintf(command, "ln -sf %s/%s /tmp", MOUNTLIST_FNAME_STUB, bkpinfo->tmpdir);
1395 paranoid_system(command);
1396 mr_free(command);
[1987]1397 }
[1]1398
[3613]1399 if (chdir(old_pwd)) {
[3076]1400 // FIXME
1401 }
[3613]1402 mr_free(old_pwd);
[2049]1403
[1987]1404 if (!does_file_exist(g_biggielist_txt)) {
1405 log_msg(1, "Warning - %s not found", g_biggielist_txt);
1406 }
1407 if (!does_file_exist(g_filelist_full)) {
1408 log_msg(1, "Warning - %s does not exist", g_filelist_full);
1409 }
[1]1410
[1987]1411 log_msg(2, "Forking");
1412 pid = fork();
1413 switch (pid) {
1414 case -1:
1415 fatal_error("Forking error");
1416 break;
[1]1417
[1987]1418 case 0:
1419 log_to_screen("Pre-processing filelist");
1420 if (!does_file_exist(g_biggielist_txt)) {
[3193]1421 mr_asprintf(command, "echo -n > %s", g_biggielist_txt);
[128]1422 paranoid_system(command);
[3193]1423 mr_free(command);
[128]1424 }
[3193]1425 mr_asprintf(command, "grep -E '^/dev/.*' %s > %s", g_biggielist_txt, g_filelist_imagedevs);
[128]1426 paranoid_system(command);
[3193]1427 mr_free(command);
[128]1428 exit(0);
1429 break;
1430
1431 default:
[541]1432 open_evalcall_form("Pre-processing filelist");
[128]1433 while (!waitpid(pid, (int *) 0, WNOHANG)) {
1434 usleep(100000);
1435 update_evalcall_form(0);
1436 }
[3893]1437 break;
[1]1438 }
[128]1439 close_evalcall_form();
[1]1440
[128]1441 log_msg(3, "loading filelist");
1442 filelist = load_filelist(g_filelist_full);
1443 log_msg(3, "deleting original filelist");
1444 unlink(g_filelist_full);
1445 if (g_text_mode) {
[3076]1446 q = NULL;
1447 while (q == NULL) {
1448 printf("Restore which directory? --> ");
[3193]1449 mr_getline(q, stdin);
[3076]1450 }
[3193]1451 toggle_path_selection(filelist, q, TRUE);
1452 if (strlen(q) == 0) {
[128]1453 res = 1;
1454 } else {
1455 res = 0;
1456 }
[3193]1457 mr_free(q);
[128]1458 } else {
1459 res = edit_filelist(filelist);
[1]1460 }
[128]1461 if (res) {
1462 log_msg(2, "User hit 'cancel'. Freeing filelist and aborting.");
1463 free_filelist(filelist);
1464 return (NULL);
1465 }
1466 ask_about_these_imagedevs(g_filelist_imagedevs, g_imagedevs_restthese);
1467 close_evalcall_form();
[1]1468
[128]1469 // NB: It's not necessary to add g_biggielist_txt to the filelist.full
1470 // file. The filelist.full file already contains the filename of EVERY
1471 // file backed up - regular and biggie files.
[1]1472
[128]1473 // However, we do want to make sure the imagedevs selected by the user
1474 // are flagged for restoring.
1475 if (length_of_file(g_imagedevs_restthese) > 2) {
1476 add_list_of_files_to_filelist(filelist, g_imagedevs_restthese,
1477 TRUE);
1478 }
[1]1479
[128]1480 return (filelist);
1481}
[1]1482
1483/**************************************************************************
1484 *END_ PROCESS_FILELIST_AND_BIGGIELIST *
1485 **************************************************************************/
1486
1487
1488
1489
1490/**
1491 * Make a backup copy of <tt>path_root</tt>/<tt>filename</tt>.
1492 * The backup filename is the filename of the original with ".pristine" added.
1493 * @param path_root The place where the filesystem is mounted (e.g. MNT_RESTORING).
1494 * @param filename The filename (absolute path) within @p path_root.
1495 * @return 0 for success, nonzero for failure.
1496 */
[128]1497int backup_crucial_file(char *path_root, char *filename)
[1]1498{
[3193]1499 char *tmp = NULL;
1500 char *command = NULL;
[128]1501 int res;
[1]1502
[128]1503 assert(path_root != NULL);
1504 assert_string_is_neither_NULL_nor_zerolength(filename);
1505
[3193]1506 mr_asprintf(tmp, "%s/%s", path_root, filename);
1507 mr_asprintf(command, "cp -f %s %s.pristine", tmp, tmp);
1508 mr_free(tmp);
[128]1509
1510 res = run_program_and_log_output(command, 5);
[3193]1511 mr_free(command);
[128]1512 return (res);
[1]1513}
1514
[2094]1515void offer_to_make_initrd() {
[1]1516
[2094]1517if (bkpinfo->restore_mode != nuke) {
[2110]1518 if (ask_me_yes_or_no
1519 ("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]1520 log_msg(1,"Launching shell for manual initrd recreation");
[2758]1521 if (does_file_exist(MNT_RESTORING"/etc/arch-release")) {
1522 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");
1523 } else {
[3157]1524 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]1525 }
[2110]1526 mvaddstr_and_log_it(g_currentY, 0, "Modifying initrd...");
1527 if (!g_text_mode) {
1528 newtSuspend();
1529 }
[3076]1530 paranoid_system("chroot " MNT_RESTORING);
[2110]1531 if (!g_text_mode) {
1532 newtResume();
1533 }
1534 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1535 } else {
[2094]1536 return;
1537 }
1538} else {
1539 log_to_screen("Non-interactive mode: no way to give you the keyboard so that you re-generate your initrd. Hope it's OK");
1540 log_msg(1,"Non-interactive mode: no way to give you the keyboard so that you re-generate your initrd. Hope it's OK");
1541}
1542}
1543
1544
[1]1545/**
[3879]1546 * Attempt to find the user's editor.
1547 * @return The editor found ("vi" if none could be found).
1548 * @note The returned string points to an allocated storage that needs to be freed by caller
1549 */
1550char *find_my_editor(void) {
1551
1552 char *output = NULL ;
1553
1554 if ((output = find_home_of_exe("pico")) == NULL) {
1555 if ((output = find_home_of_exe("nano")) == NULL) {
1556 if ((output = find_home_of_exe("e3em")) == NULL) {
1557 if ((output = find_home_of_exe("e3vi")) == NULL) {
1558 if ((output = find_home_of_exe("vim")) == NULL) {
1559 output = find_home_of_exe("vi");
1560 }
1561 }
1562 }
1563 }
1564 }
1565 if (output == NULL) {
1566 log_msg(2, " (find_my_editor) --- warning - no editor found, even vi");
1567 }
1568 return (output);
1569}
1570
1571
1572
1573
1574/**
1575 * Install LILO on the user's boot drive (determined by /etc/lilo.conf).
1576 * @param offer_to_run_stablilo If TRUE, then offer to hack the user's fstab for them.
[1]1577 * @return 0 for success, nonzero for failure.
1578 */
[3879]1579int run_lilo(bool offer_to_run_stablilo)
[1]1580{
[3879]1581 /** malloc **/
1582 char *command = NULL;
1583 char *tmp = NULL;
1584 char *editor = NULL;
1585
[128]1586 int res;
[3879]1587 int done;
1588 bool run_lilo_M = FALSE;
[1]1589
[3879]1590 if (!run_program_and_log_output
1591 ("grep \"boot.*=.*/dev/md\" " MNT_RESTORING "/etc/lilo.conf", 1)) {
1592 run_lilo_M = TRUE;
1593 }
[1]1594
[3879]1595 if (offer_to_run_stablilo
1596 && ask_me_yes_or_no("Did you change the mountlist or cloned the system ?"))
[1845]1597
[3879]1598 /* interactive mode */
1599 {
1600 mvaddstr_and_log_it(g_currentY,
1601 0,
1602 "Modifying fstab and lilo.conf, and running LILO... ");
1603 mr_asprintf(command, "mr-stablilo-me");
1604 res = run_program_and_log_output(command, 3);
1605 mr_free(command);
[1845]1606
[3879]1607 if (res) {
1608 popup_and_OK
1609 ("You will now edit fstab and lilo.conf, to make sure they match your new mountlist.");
1610 for (done = FALSE; !done;) {
1611 editor = find_my_editor();
1612 if (editor == NULL) {
1613 popup_and_OK("No editor found. You won't be able to edit conf files");
1614 } else {
1615 if (!g_text_mode) {
1616 newtSuspend();
1617 }
1618 mr_asprintf(tmp, "%s " MNT_RESTORING "/etc/fstab", editor);
1619 paranoid_system(tmp);
1620 mr_free(tmp);
1621
1622 mr_asprintf(tmp, "%s " MNT_RESTORING "/etc/lilo.conf", editor);
1623 paranoid_system(tmp);
1624 mr_free(tmp);
1625 mr_free(editor);
1626
1627 if (!g_text_mode) {
1628 newtResume();
1629 }
1630// newtCls();
1631 if (ask_me_yes_or_no("Edit them again?")) {
1632 continue;
1633 }
1634 }
1635 mr_free(editor);
[2094]1636
[3879]1637 res = run_program_and_log_output("chroot " MNT_RESTORING " lilo -L", 3);
1638 if (res) {
1639 res = run_program_and_log_output("chroot " MNT_RESTORING " lilo", 3);
1640 }
1641 if (res) {
1642 done = ask_me_yes_or_no("LILO failed. Re-edit system files?");
1643 } else {
1644 done = TRUE;
1645 }
1646 }
[128]1647 } else {
[3879]1648 log_to_screen("lilo.conf and fstab were modified OK");
[128]1649 }
[3879]1650 } else
1651 /* nuke mode */
1652 {
1653 mvaddstr_and_log_it(g_currentY,
1654 0,
1655 "Running LILO... ");
1656 res =
1657 run_program_and_log_output("chroot " MNT_RESTORING " lilo -L",
1658 3);
1659 if (res) {
1660 res =
1661 run_program_and_log_output("chroot " MNT_RESTORING " lilo",
1662 3);
1663 }
1664 if (res) {
1665 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
1666 log_to_screen
1667 ("Failed to re-jig fstab and/or lilo. Edit/run manually, please.");
1668 } else {
1669 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1670 }
[128]1671 }
[3879]1672 if (run_lilo_M) {
1673 run_program_and_log_output("chroot " MNT_RESTORING
1674 " lilo -M /dev/hda", 3);
1675 run_program_and_log_output("chroot " MNT_RESTORING
1676 " lilo -M /dev/sda", 3);
[128]1677 }
[3879]1678 return (res);
[1]1679}
[128]1680
[1]1681/**************************************************************************
[3879]1682 *END_RUN_LILO *
[1]1683 **************************************************************************/
1684
1685
1686/**
[3879]1687 * Install a raw MBR onto @p bd.
1688 * @param offer_to_hack_scripts If TRUE, then offer to hack the user's fstab for them.
1689 * @param bd The device to copy the stored MBR to.
1690 * @return 0 for success, nonzero for failure.
[1]1691 */
[3879]1692int run_raw_mbr(bool offer_to_hack_scripts, char *bd)
1693{
1694 /** malloc **/
1695 char *command = NULL;
1696 char *boot_device = NULL;
1697 char *tmp = NULL;
1698 char *editor;
1699 int res;
1700 int done;
[3193]1701
[3879]1702 malloc_string(boot_device);
1703 assert_string_is_neither_NULL_nor_zerolength(bd);
[3866]1704
[3879]1705 strcpy(boot_device, bd);
1706
1707 if (offer_to_hack_scripts
1708 && ask_me_yes_or_no("Did you change the mountlist or cloned the system ?")) {
1709 /* interactive mode */
1710 mvaddstr_and_log_it(g_currentY, 0, "Modifying fstab and restoring MBR... ");
1711 for (done = FALSE; !done;) {
1712 popup_and_OK("You will now edit fstab");
1713 editor = find_my_editor();
1714 if (editor == NULL) {
1715 popup_and_OK("No editor found. You won't be able to edit conf files");
1716 } else {
1717 if (!g_text_mode) {
1718 newtSuspend();
[3866]1719 }
[3879]1720 mr_asprintf(tmp, "%s " MNT_RESTORING "/etc/fstab", editor);
1721
1722 paranoid_system(tmp);
1723 mr_free(tmp);
1724 if (!g_text_mode) {
1725 newtResume();
1726 }
[3866]1727 }
[3879]1728 mr_free(editor);
1729
1730 popup_and_get_string("Boot device", "Please confirm/enter the boot device. If in doubt, try /dev/hda", boot_device, MAX_STR_LEN / 4);
1731 mr_asprintf(command, "mr-stabraw-me %s", boot_device);
1732 res = run_program_and_log_output(command, 3);
1733 mr_free(command);
1734
1735 if (res) {
1736 done = ask_me_yes_or_no("Modifications failed. Re-try?");
1737 } else {
1738 done = TRUE;
1739 }
[3866]1740 }
[3879]1741 } else {
1742 /* nuke mode */
1743 mr_asprintf(command, "mr-raw %s /"MOUNTLIST_FNAME_STUB, boot_device);
1744 log_msg(2, "run_raw_mbr() --- command='%s'", command);
1745
1746 mvaddstr_and_log_it(g_currentY, 0, "Restoring MBR... ");
1747 res = run_program_and_log_output(command, 3);
1748 mr_free(command);
[128]1749 }
[3879]1750 if (res) {
1751 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
1752 log_to_screen("MBR+fstab processed w/error(s). See %s for more info.", MONDO_LOGFILE);
1753 } else {
1754 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
[128]1755 }
[3879]1756 paranoid_free(boot_device);
1757 return (res);
[1]1758}
1759
[3879]1760/**************************************************************************
1761 *END_RUN_RAW_MBR *
1762 **************************************************************************/
[1]1763
[3879]1764
[1]1765/**
1766 * Install GRUB on @p bd.
1767 * @param offer_to_run_stabgrub If TRUE, then offer to hack the user's fstab for them.
1768 * @param bd The boot device where GRUB is installed.
1769 * @return 0 for success, nonzero for failure.
1770 */
[128]1771int run_grub(bool offer_to_run_stabgrub, char *bd)
[1]1772{
1773 /** malloc **/
[3193]1774 char *command = NULL;
1775 char *boot_device = NULL;
1776 char *tmp = NULL;
1777 char *editor = NULL;
[1]1778
[3193]1779 bool done;
[2950]1780 int res = 0; /* FALSE */
1781 bool mntlistchg = FALSE;
1782 FILE *fin = NULL;
[1]1783
[128]1784 malloc_string(boot_device);
[3193]1785
[128]1786 assert_string_is_neither_NULL_nor_zerolength(bd);
1787 strcpy(boot_device, bd);
1788
[3297]1789 if (offer_to_run_stabgrub && ask_me_yes_or_no("Did you change the mountlist or cloned the system ?")) {
[128]1790 /* interactive mode */
[3297]1791 mvaddstr_and_log_it(g_currentY, 0, "Modifying fstab, mtab, device.map and menu.lst/grub.cfg, and running GRUB...");
[2950]1792 /* Did we changed the mountlist ? If yes, then force editing conf files */
1793 if ((fin = fopen(MONDO_MNTLISTCHG, "r")) != NULL) {
1794 mntlistchg = TRUE;
1795 }
[128]1796 for (done = FALSE; !done;) {
[3297]1797 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]1798 /* Only try to adapt grub here first if the mountlist wasn't changed before */
1799 if (! mntlistchg) {
[3542]1800 mr_asprintf(command, "mr-stabgrub-me %s", boot_device);
[2950]1801 res = run_program_and_log_output(command, 1);
[3193]1802 mr_free(command);
[2950]1803 }
[3193]1804
1805 if ((res) || (mntlistchg)) {
[2950]1806 if (res) {
1807 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");
1808 } else {
1809 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");
1810 }
[3879]1811 editor = find_my_editor();
[3866]1812 if (editor == NULL) {
1813 popup_and_OK("No editor found. You won't be able to edit conf files");
1814 } else {
1815 if (!g_text_mode) {
1816 newtSuspend();
1817 }
1818 mr_asprintf(tmp, "chroot %s %s /etc/fstab", MNT_RESTORING, editor);
1819 paranoid_system(tmp);
1820 mr_free(tmp);
[3193]1821
[3866]1822 mr_asprintf(tmp, "chroot %s %s /etc/mtab", MNT_RESTORING, editor);
1823 paranoid_system(tmp);
1824 mr_free(tmp);
1825
1826 if (does_file_exist(MNT_RESTORING"/boot/grub/menu.lst")) {
1827 mr_asprintf(tmp, "chroot %s %s /boot/grub/menu.lst", MNT_RESTORING, editor);
1828 } else if (does_file_exist(MNT_RESTORING"/boot/grub/grub.cfg")) {
1829 mr_asprintf(tmp, "chroot %s %s /boot/grub/grub.cfg", MNT_RESTORING, editor);
1830 } else if (does_file_exist(MNT_RESTORING"/boot/grub2/grub.cfg")) {
1831 mr_asprintf(tmp, "chroot %s %s /boot/grub2/grub.cfg", MNT_RESTORING, editor);
1832 }
1833 paranoid_system(tmp);
1834 mr_free(tmp);
1835
1836 if (does_file_exist(MNT_RESTORING"/boot/grub/device.map")) {
1837 mr_asprintf(tmp, "chroot %s %s /boot/grub/device.map", MNT_RESTORING, editor);
1838 } else if (does_file_exist(MNT_RESTORING"/boot/grub2/device.map")) {
1839 mr_asprintf(tmp, "chroot %s %s /boot/grub2/device.map", MNT_RESTORING, editor);
1840 }
1841 paranoid_system(tmp);
1842 mr_free(tmp);
1843 if (!g_text_mode) {
1844 newtResume();
1845 }
[2904]1846 }
[3193]1847 mr_free(editor);
1848
[3542]1849 mr_asprintf(command, "mr-stabgrub-me %s", boot_device);
[2904]1850 res = run_program_and_log_output(command, 1);
[3193]1851 mr_free(command);
1852
[2904]1853 if (res) {
[3193]1854 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]1855 newtSuspend();
[3076]1856 paranoid_system("chroot " MNT_RESTORING);
[2904]1857 newtResume();
1858 } else {
1859 popup_and_OK("GRUB is now installed correctly");
1860 done = TRUE;
1861 }
[128]1862 } else {
1863 done = TRUE;
1864 }
1865 }
[3193]1866 } else {
[128]1867 /* nuke mode */
[3542]1868 if (!run_program_and_log_output("which mr-grub", FALSE)) {
1869 log_msg(1, "Yay! mr-grub found...");
1870 mr_asprintf(command, "mr-grub %s /"MOUNTLIST_FNAME_STUB, boot_device);
[3193]1871 log_msg(1, "command = %s", command);
1872 } else {
[3297]1873 // Or grub2-install
[3193]1874 mr_asprintf(command, "chroot " MNT_RESTORING " grub-install %s", boot_device);
[3542]1875 log_msg(1, "WARNING - mr-grub not found; using grub-install");
[3193]1876 }
1877 mvaddstr_and_log_it(g_currentY, 0, "Running GRUB... ");
[2227]1878 log_it("%s",command);
[128]1879 res = run_program_and_log_output(command, 1);
[3193]1880 mr_free(command);
1881
[128]1882 if (res) {
[3299]1883 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]1884 newtSuspend();
[3076]1885 paranoid_system("chroot " MNT_RESTORING);
[128]1886 newtResume();
1887 }
[1]1888 }
[128]1889 if (res) {
[541]1890 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
[128]1891 log_to_screen
[1315]1892 ("GRUB ran w/error(s). See %s for more info.", MONDO_LOGFILE);
[128]1893 log_msg(1, "Type:-");
[3542]1894 log_msg(1, " mr-mount-me");
[128]1895 log_msg(1, " chroot " MNT_RESTORING);
[3707]1896 log_msg(1, " mount /boot if needed");
[128]1897 log_msg(1, " grub-install '(hd0)'");
1898 log_msg(1, " exit");
[3542]1899 log_msg(1, " mr-unmount-me");
[128]1900 log_msg(1,
1901 "If you're really stuck, please e-mail the mailing list.");
1902 } else {
[541]1903 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
[128]1904 }
1905 paranoid_free(boot_device);
[1]1906
[128]1907 return (res);
[1]1908}
[128]1909
[1]1910/**************************************************************************
1911 *END_RUN_GRUB *
1912 **************************************************************************/
1913
1914
1915/**
[3879]1916 * Install the user's boot loader in the MBR.
[3885]1917 * Currently LILO, GRUB, RAW (dd of MBR), and the FreeBSD bootloader are supported.
[1]1918 * @param offer_to_hack_scripts If TRUE, then offer to hack the user's fstab for them.
1919 * @return 0 for success, nonzero for failure.
1920 */
[3879]1921int run_boot_loader(bool offer_to_hack_scripts)
[1]1922{
[128]1923 int res;
[3879]1924 int retval = 0;
[1]1925
[3879]1926 /** malloc *******/
1927 char *device = NULL;
1928 char *disk = NULL;
1929 char *name = NULL;
1930 char *type = NULL;
1931 char *cmd = NULL;
[1]1932
[3879]1933 malloc_string(device);
1934 malloc_string(name);
1935 malloc_string(type);
[1]1936
[3879]1937 /* In order to have a working bootloader, we need to have all devices
1938 * ready in the chroot. If they are not there (udev) then copy them from
1939 * the current /dev location
1940 */
1941 mr_asprintf(cmd,"tar cf - /dev | ( cd %s ; tar xf - )",MNT_RESTORING);
1942 run_program_and_log_output(cmd, 3);
1943 mr_free(cmd);
[3193]1944
[3879]1945 backup_crucial_file(MNT_RESTORING, "/etc/fstab");
1946 backup_crucial_file(MNT_RESTORING, "/boot/grub/menu.lst");
1947 backup_crucial_file(MNT_RESTORING, "/boot/grub/grub.cfg");
1948 backup_crucial_file(MNT_RESTORING, "/boot/grub2/grub.cfg");
1949 backup_crucial_file(MNT_RESTORING, "/etc/lilo.conf");
1950 backup_crucial_file(MNT_RESTORING, "/boot/grub/device.map");
1951 backup_crucial_file(MNT_RESTORING, "/boot/grub2/device.map");
1952 backup_crucial_file(MNT_RESTORING, "/etc/mtab");
1953 read_cfg_var(g_mondo_cfg_file, "bootloader.device", device);
1954 read_cfg_var(g_mondo_cfg_file, "bootloader.name", name);
1955 read_cfg_var(g_mondo_cfg_file, "boot-type", type);
1956 log_msg(2, "run_boot_loader: device='%s', name='%s', type='%s'", device, name, type);
1957 sync();
[3866]1958
[3879]1959 offer_to_make_initrd();
[3193]1960
[3879]1961 disk = truncate_to_drive_name(device);
1962 if (strcmp(type,"BIOS") == 0) {
1963 // Force installation of a MBR bootloader brought by mindi on the disk
1964 // in case none was available and we then start from a partition
1965 log_msg(2, "Reinstalling mbr.bin on %s", disk);
1966 mr_system("dd bs=440 count=1 conv=notrunc if=/tmp/mbr.bin of=%s &> /dev/null",disk);
[3193]1967 } else {
[3879]1968 // Same for GPT
1969 log_msg(2, "Reinstalling gptmbr.bin on %s", disk);
1970 mr_system("dd bs=440 count=1 conv=notrunc if=/tmp/gptmbr.bin of=%s &> /dev/null",disk);
1971 }
[3193]1972
[3879]1973 // Now reinstall bootloader
1974 if (!strcmp(name, "LILO")) {
1975 res = run_lilo(offer_to_hack_scripts);
1976 } else if (!strcmp(name, "GRUB")) {
1977 res = run_grub(offer_to_hack_scripts, device);
1978 } else if (!strcmp(name, "RAW")) {
1979 res = run_raw_mbr(offer_to_hack_scripts, device);
[1]1980 }
[3879]1981#ifdef __FreeBSD__
1982 else if (!strcmp(name, "BOOT0")) {
1983 mr_asprintf(tmp, "boot0cfg -B %s", device);
1984 res = run_program_and_log_output(tmp, FALSE);
1985 paranoid_free(tmp);
1986 } else {
1987 mr_asprintf(tmp, "ls /dev | grep -Eq '^%ss[1-4].*'", device);
1988 if (!system(tmp)) {
1989 mr_free(tmp);
1990 mr_asprintf(tmp, MNT_RESTORING "/sbin/fdisk -B %s", device);
1991 res = run_program_and_log_output(tmp, 3);
1992 } else {
1993 log_msg(1, "I'm not running any boot loader. You have a DD boot drive. It's already loaded up.");
1994 }
1995 mr_free(tmp);
1996 }
1997#else
1998 else {
1999 log_to_screen
2000 ("Unable to determine type of boot loader. Defaulting to LILO.");
2001 res = run_lilo(offer_to_hack_scripts);
2002 }
2003#endif
2004 retval += res;
[128]2005 if (res) {
[3879]2006 log_to_screen("Your boot loader returned an error");
[128]2007 } else {
[3879]2008 log_to_screen("Your boot loader ran OK");
[128]2009 }
[3879]2010 paranoid_free(device);
2011 paranoid_free(name);
2012 return (retval);
[1]2013}
[128]2014
[1]2015/**************************************************************************
[3879]2016 *END_ RUN_BOOT_LOADER *
[1]2017 **************************************************************************/
2018
2019
2020
2021/**
2022 * malloc() and set sensible defaults for the mondorestore filename variables.
2023 * @param bkpinfo The backup information structure. Fields used:
2024 * - @c bkpinfo->tmpdir
2025 * - @c bkpinfo->disaster_recovery
2026 */
[1645]2027void setup_MR_global_filenames()
[1]2028{
[128]2029 assert(bkpinfo != NULL);
[1]2030
[128]2031 malloc_string(g_biggielist_txt);
2032 malloc_string(g_filelist_full);
2033 malloc_string(g_filelist_imagedevs);
2034 malloc_string(g_imagedevs_restthese);
2035 malloc_string(g_mondo_cfg_file);
2036 malloc_string(g_mountlist_fname);
2037 malloc_string(g_mondo_home);
2038 malloc_string(g_isodir_device);
2039 malloc_string(g_isodir_format);
[1]2040
[3833]2041 sprintf(g_biggielist_txt, "%s/%s", bkpinfo->tmpdir, BIGGIELIST_TXT_STUB);
2042 sprintf(g_filelist_full, "%s/%s", bkpinfo->tmpdir, FILELIST_FULL_STUB);
2043 sprintf(g_filelist_imagedevs, "%s/tmp/filelist.imagedevs", bkpinfo->tmpdir);
2044// sprintf(g_imagedevs_pot, "%s/tmp/imagedevs.pot", bkpinfo->tmpdir);
[128]2045 sprintf(g_imagedevs_restthese, "%s/tmp/imagedevs.restore-these",
[3833]2046 bkpinfo->tmpdir);
[128]2047 if (bkpinfo->disaster_recovery) {
2048 sprintf(g_mondo_cfg_file, "/%s", MONDO_CFG_FILE_STUB);
2049 sprintf(g_mountlist_fname, "/%s", MOUNTLIST_FNAME_STUB);
2050 } else {
[3833]2051 sprintf(g_mondo_cfg_file, "%s/%s", bkpinfo->tmpdir, MONDO_CFG_FILE_STUB);
2052 sprintf(g_mountlist_fname, "%s/%s", bkpinfo->tmpdir, MOUNTLIST_FNAME_STUB);
[128]2053 }
[1]2054}
[128]2055
[1]2056/**************************************************************************
2057 *END_SET_GLOBAL_FILENAME *
2058 **************************************************************************/
2059
2060
2061/**
2062 * Copy @p input_file (containing the result of a compare) to @p output_file,
2063 * deleting spurious "changes" along the way.
2064 * @param output_file The output file to write with spurious changes removed.
2065 * @param input_file The input file, a list of changed files created by a compare.
2066 */
[128]2067void streamline_changes_file(char *output_file, char *input_file)
[1]2068{
[128]2069 FILE *fin;
2070 FILE *fout;
[3193]2071 char *incoming = NULL;
[1]2072
[128]2073 assert_string_is_neither_NULL_nor_zerolength(output_file);
2074 assert_string_is_neither_NULL_nor_zerolength(input_file);
2075
2076 if (!(fin = fopen(input_file, "r"))) {
2077 log_OS_error(input_file);
2078 return;
2079 }
2080 if (!(fout = fopen(output_file, "w"))) {
2081 fatal_error("cannot open output_file");
2082 }
[3193]2083 for (mr_getline(incoming, fin); !feof(fin); mr_getline(incoming, fin)) {
[128]2084 if (strncmp(incoming, "etc/adjtime", 11)
2085 && strncmp(incoming, "etc/mtab", 8)
2086 && strncmp(incoming, "tmp/", 4)
2087 && strncmp(incoming, "boot/map", 8)
2088 && !strstr(incoming, "incheckentry")
2089 && strncmp(incoming, "etc/mail/statistics", 19)
2090 && strncmp(incoming, "var/", 4))
2091 fprintf(fout, "%s", incoming); /* don't need \n here, for some reason.. */
[3193]2092 mr_free(incoming);
[128]2093 }
[3193]2094 mr_free(incoming);
[128]2095 paranoid_fclose(fout);
2096 paranoid_fclose(fin);
[1]2097}
[128]2098
[1]2099/**************************************************************************
2100 *END_STREAMLINE_CHANGES_FILE *
2101 **************************************************************************/
2102
2103
2104/**
2105 * Give the user twenty seconds to press Ctrl-Alt-Del before we nuke their drives.
2106 */
[128]2107void twenty_seconds_til_yikes()
[1]2108{
[128]2109 int i;
2110 /* MALLOC * */
[3193]2111 char *tmp = NULL;
[1]2112
[128]2113 if (does_file_exist("/tmp/NOPAUSE")) {
2114 return;
2115 }
[541]2116 open_progress_form("CAUTION",
2117 "Be advised: I am about to ERASE your hard disk(s)!",
2118 "You may press Ctrl+Alt+Del to abort safely.",
[128]2119 "", 20);
2120 for (i = 0; i < 20; i++) {
2121 g_current_progress = i;
[3193]2122 mr_asprintf(tmp, "You have %d seconds left to abort.", 20 - i);
[128]2123 update_progress_form(tmp);
[3193]2124 mr_free(tmp);
[128]2125 sleep(1);
2126 }
2127 close_progress_form();
[1]2128}
[128]2129
[1]2130/**************************************************************************
2131 *END_TWENTY_SECONDS_TIL_YIKES *
2132 **************************************************************************/
2133
2134
2135/**
2136 * Unmount all devices in @p p_external_copy_of_mountlist.
2137 * @param p_external_copy_of_mountlist The mountlist to guide the devices to unmount.
2138 * @return 0 for success, nonzero for failure.
2139 */
[128]2140int unmount_all_devices(struct mountlist_itself
2141 *p_external_copy_of_mountlist)
[1]2142{
[128]2143 struct mountlist_itself *mountlist;
2144 int retval = 0, lino, res = 0, i;
[3193]2145 char *command = NULL;
[2211]2146 char *tmp = NULL;
[1]2147
[128]2148 assert(p_external_copy_of_mountlist != NULL);
[1]2149
[128]2150 mountlist = malloc(sizeof(struct mountlist_itself));
[3301]2151 memcpy((void *) mountlist, (void *) p_external_copy_of_mountlist, sizeof(struct mountlist_itself));
[128]2152 sort_mountlist_by_mountpoint(mountlist, 0);
[1]2153
[3600]2154 run_program_and_log_output("df -m -P -T", 3);
[541]2155 mvaddstr_and_log_it(g_currentY, 0, "Unmounting devices ");
[3301]2156 open_progress_form("Unmounting devices", "Unmounting all devices that were mounted,", "in preparation for the post-restoration reboot.", "", mountlist->entries);
[3076]2157 if (chdir("/")) {
2158 // FIXME
2159 }
[3301]2160 for (i = 0; i < 10 && run_program_and_log_output("ps | grep buffer | grep -v \"grep buffer\"", TRUE) == 0; i++) {
[128]2161 sleep(1);
2162 log_msg(2, "Waiting for buffer() to finish");
2163 }
[1]2164
[3193]2165 sync();
[1]2166
[3544]2167 /* after that what is logged is not copied on disk, typically the result of labelling */
[3185]2168 mr_asprintf(tmp, "cp -f %s " MNT_RESTORING "/var/log", MONDO_LOGFILE);
[1322]2169 if (run_program_and_log_output(tmp, FALSE)) {
[3653]2170 log_msg(1, "Error. Failed to copy log to the machine's /var/log dir. (Mounted read-only?)");
[1]2171 }
[2211]2172 paranoid_free(tmp);
[128]2173 if (does_file_exist("/tmp/DUMBASS-GENTOO")) {
[3301]2174 run_program_and_log_output("mkdir -p " MNT_RESTORING "/mnt/.boot.d", 5);
[1]2175 }
[1971]2176
2177 /* Unmounting the local /proc and /sys first */
[3193]2178 run_program_and_log_output("umount -d " MNT_RESTORING "/proc",3);
2179 run_program_and_log_output("umount -d " MNT_RESTORING "/sys",3);
[1971]2180
[3734]2181 /* Unmounting potential btrfs subvolumes */
2182 if (does_file_exist("/tmp/umount-btrfs-subvol")) {
2183 run_program_and_log_output("/tmp/umount-btrfs-subvol",3);
2184 }
2185
[128]2186 for (lino = mountlist->entries - 1; lino >= 0; lino--) {
2187 if (!strcmp(mountlist->el[lino].mountpoint, "lvm")) {
2188 continue;
2189 }
[3185]2190 mr_asprintf(tmp, "Unmounting device %s ", mountlist->el[lino].device);
[128]2191 update_progress_form(tmp);
[2211]2192
[3439]2193 //TODO: this should be done in a certain order normally if there are cascading mount
[128]2194 if (is_this_device_mounted(mountlist->el[lino].device)) {
2195 if (!strcmp(mountlist->el[lino].mountpoint, "swap")) {
[3193]2196 mr_asprintf(command, "swapoff %s", mountlist->el[lino].device);
[128]2197 } else {
2198 if (!strcmp(mountlist->el[lino].mountpoint, "/1")) {
[3193]2199 mr_asprintf(command, "umount -d %s/", MNT_RESTORING);
[128]2200 log_msg(3,
2201 "Well, I know a certain kitty-kitty who'll be sleeping with Mommy tonight...");
2202 } else {
[3193]2203 mr_asprintf(command, "umount -d " MNT_RESTORING "%s", mountlist->el[lino].mountpoint);
[2144]2204
2205 /* To support latest Ubuntu where /var is a separate FS
2206 * Cf: http://linux.derkeiler.com/Mailing-Lists/Ubuntu/2007-04/msg01319.html
2207 * we need to create some dirs under the real / before unmounting it */
2208 if (!strcmp(mountlist->el[lino].mountpoint, "/")) {
2209 run_program_and_log_output("mkdir -p " MNT_RESTORING "/var/lock", FALSE);
2210 run_program_and_log_output("mkdir -p " MNT_RESTORING "/var/run", FALSE);
2211 }
[128]2212 }
2213 }
2214 log_msg(10, "The 'umount' command is '%s'", command);
2215 res = run_program_and_log_output(command, 3);
[3193]2216 mr_free(command);
[128]2217 } else {
[2211]2218 mr_strcat(tmp, "...not mounted anyway :-) OK");
[128]2219 res = 0;
2220 }
2221 g_current_progress++;
2222 if (res) {
[2211]2223 mr_strcat(tmp, "...Failed");
[128]2224 retval++;
2225 log_to_screen(tmp);
2226 } else {
2227 log_msg(2, tmp);
2228 }
[2211]2229 paranoid_free(tmp);
[128]2230 }
2231 close_progress_form();
2232 if (retval) {
[541]2233 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
[128]2234 } else {
[541]2235 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
[128]2236 }
2237 if (retval) {
[541]2238 log_to_screen("Unable to unmount some of your partitions.");
[128]2239 } else {
[541]2240 log_to_screen("All partitions were unmounted OK.");
[128]2241 }
[3439]2242 mr_asprintf(command, "mount");
2243 log_msg(4, "mount result after unmounting all FS", command);
2244 (void)run_program_and_log_output(command, 3);
2245 mr_free(command);
[128]2246 free(mountlist);
2247 return (retval);
[1]2248}
[128]2249
[1]2250/**************************************************************************
2251 *END_UNMOUNT_ALL_DEVICES *
2252 **************************************************************************/
2253/* @} - end restoreUtilityGroup */
2254
[3892]2255void wait_until_software_raids_are_prepped(int wait_for_percentage)
[1]2256{
[558]2257 struct raidlist_itself *raidlist;
[2982]2258 int unfinished_mdstat_devices = 9999;
2259 int i = 0;
[3193]2260 char *screen_message = NULL;
[1]2261
[558]2262 raidlist = malloc(sizeof(struct raidlist_itself));
[128]2263
2264 assert(wait_for_percentage <= 100);
[2230]2265 log_it("wait_until_software_raids_are_prepped");
[128]2266 while (unfinished_mdstat_devices > 0) {
[558]2267 // FIXME: Prefix '/dev/' should really be dynamic!
[2982]2268 if (parse_mdstat(MDSTAT_FILE, raidlist, "/dev/")) {
[558]2269 log_to_screen("Sorry, cannot read %s", MDSTAT_FILE);
2270 log_msg(1,"Sorry, cannot read %s", MDSTAT_FILE);
[128]2271 return;
[1]2272 }
[2982]2273 for (unfinished_mdstat_devices = 0; i <= raidlist->entries; i++) {
[558]2274 if (raidlist->el[i].progress < wait_for_percentage) {
[128]2275 unfinished_mdstat_devices++;
[704]2276 if (raidlist->el[i].progress == -1) // delayed while another partition inits
2277 {
2278 continue;
2279 }
[558]2280 log_msg(1,"Sync'ing %s (i=%d)", raidlist->el[i].raid_device, i);
[3193]2281 mr_asprintf(screen_message, "Sync'ing %s", raidlist->el[i].raid_device);
[128]2282 open_evalcall_form(screen_message);
[3193]2283 mr_free(screen_message);
2284
[558]2285 while (raidlist->el[i].progress < wait_for_percentage) {
2286 log_msg(1,"Percentage sync'ed: %d", raidlist->el[i].progress);
2287 update_evalcall_form(raidlist->el[i].progress);
[128]2288 sleep(2);
[558]2289 // FIXME: Prefix '/dev/' should really be dynamic!
[2982]2290 if (parse_mdstat(MDSTAT_FILE, raidlist, "/dev/")) {
[128]2291 break;
2292 }
2293 }
2294 close_evalcall_form();
2295 }
[1]2296 }
[128]2297 }
[558]2298 paranoid_free(raidlist);
[3194]2299}
Note: See TracBrowser for help on using the repository browser.