[58] | 1 | /* $Id: libmondo-tools.c 89 2005-10-27 20:45:34Z bcornec $
|
---|
| 2 | misc tools
|
---|
[1] | 3 | */
|
---|
| 4 |
|
---|
| 5 | /**
|
---|
| 6 | * @file
|
---|
| 7 | * Miscellaneous tools that didn't really fit anywhere else.
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | #include "my-stuff.h"
|
---|
| 11 | #include "mondostructures.h"
|
---|
| 12 | #include "lib-common-externs.h"
|
---|
| 13 | #include "libmondo-tools.h"
|
---|
| 14 | #include "libmondo-gui-EXT.h"
|
---|
| 15 | #include "libmondo-files-EXT.h"
|
---|
| 16 | #include "libmondo-fork-EXT.h"
|
---|
| 17 | #include "libmondo-raid-EXT.h"
|
---|
| 18 | #include <sys/socket.h>
|
---|
| 19 | #include <netdb.h>
|
---|
| 20 | #include <netinet/in.h>
|
---|
| 21 | #include <arpa/inet.h>
|
---|
| 22 |
|
---|
| 23 | /*@unused@*/
|
---|
[49] | 24 | //static char cvsid[] = "$Id: libmondo-tools.c 89 2005-10-27 20:45:34Z bcornec $";
|
---|
[1] | 25 |
|
---|
| 26 | extern int g_tape_buffer_size_MB;
|
---|
| 27 | extern char *g_erase_tmpdir_and_scratchdir;
|
---|
| 28 | extern char *g_serial_string;
|
---|
| 29 | extern bool g_text_mode;
|
---|
| 30 | extern int g_currentY;
|
---|
| 31 | extern int g_current_media_number;
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @addtogroup globalGroup
|
---|
| 35 | * @{
|
---|
| 36 | */
|
---|
[49] | 37 | bool g_remount_cdrom_at_end, ///< TRUE if we unmounted the CD-ROM and should remount it when done with the backup.
|
---|
| 38 | g_remount_floppy_at_end; ///< TRUE if we unmounted the floppy and should remount it when done with the backup.
|
---|
| 39 | bool g_cd_recovery; ///< TRUE if we're making an "autonuke" backup.
|
---|
[1] | 40 | double g_kernel_version;
|
---|
| 41 |
|
---|
| 42 | /**
|
---|
| 43 | * The place where /boot is mounted.
|
---|
| 44 | */
|
---|
[49] | 45 | char *g_boot_mountpt = NULL;
|
---|
[1] | 46 |
|
---|
| 47 | /**
|
---|
| 48 | * The location of Mondo's home directory.
|
---|
| 49 | */
|
---|
[49] | 50 | char *g_mondo_home = NULL;
|
---|
[1] | 51 |
|
---|
| 52 | /**
|
---|
| 53 | * The serial string (used to differentiate between backups) of the current backup.
|
---|
| 54 | */
|
---|
[49] | 55 | char *g_serial_string = NULL;
|
---|
[1] | 56 |
|
---|
| 57 | /**
|
---|
| 58 | * The location where tmpfs is mounted, or "" if it's not mounted.
|
---|
| 59 | */
|
---|
[49] | 60 | char *g_tmpfs_mountpt = NULL;
|
---|
| 61 | char *g_magicdev_command = NULL;
|
---|
[1] | 62 |
|
---|
| 63 | /**
|
---|
| 64 | * The default maximum level to log messages at or below.
|
---|
| 65 | */
|
---|
[49] | 66 | int g_loglevel = DEFAULT_DEBUG_LEVEL;
|
---|
[1] | 67 |
|
---|
| 68 | /* @} - end of globalGroup */
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 | extern pid_t g_buffer_pid;
|
---|
| 72 | extern pid_t g_main_pid;
|
---|
| 73 |
|
---|
| 74 | extern t_bkptype g_backup_media_type;
|
---|
| 75 |
|
---|
| 76 | extern bool am_I_in_disaster_recovery_mode(void);
|
---|
| 77 |
|
---|
| 78 |
|
---|
| 79 | /**
|
---|
| 80 | * @addtogroup utilityGroup
|
---|
| 81 | * @{
|
---|
| 82 | */
|
---|
| 83 | /**
|
---|
| 84 | * Assertion handler. Prints a friendly message to the user,
|
---|
| 85 | * offering to ignore all, dump core, break to debugger,
|
---|
| 86 | * exit, or ignore. Intended to be used with an assert() macro.
|
---|
| 87 | *
|
---|
| 88 | * @param file The file in which the assertion triggered.
|
---|
| 89 | * @param function The function (@c __FUNCTION__) in which the assertion triggered.
|
---|
| 90 | * @param line The line number of the assert() statement.
|
---|
| 91 | * @param exp The expression that failed (as a string).
|
---|
| 92 | */
|
---|
[49] | 93 | void _mondo_assert_fail(const char *file,
|
---|
| 94 | const char *function, int line, const char *exp)
|
---|
[1] | 95 | {
|
---|
[49] | 96 | static int ignoring_assertions = 0;
|
---|
| 97 | bool is_valid = TRUE;
|
---|
[1] | 98 |
|
---|
[49] | 99 | log_it("ASSERTION FAILED: `%s' at %s:%d in %s", exp, file, line,
|
---|
| 100 | function);
|
---|
| 101 | if (ignoring_assertions) {
|
---|
| 102 | log_it("Well, the user doesn't care...");
|
---|
| 103 | return;
|
---|
| 104 | }
|
---|
[1] | 105 | #ifndef _XWIN
|
---|
[49] | 106 | if (!g_text_mode)
|
---|
| 107 | newtSuspend();
|
---|
[1] | 108 | #endif
|
---|
[49] | 109 | printf("ASSERTION FAILED: `%s'\n", exp);
|
---|
| 110 | printf("\tat %s:%d in %s\n\n", file, line, function);
|
---|
| 111 | printf("(I)gnore, ignore (A)ll, (D)ebug, a(B)ort, or (E)xit? ");
|
---|
| 112 | do {
|
---|
| 113 | is_valid = TRUE;
|
---|
| 114 | switch (toupper(getchar())) {
|
---|
| 115 | case 'A': // ignore (A)ll
|
---|
| 116 | ignoring_assertions = 1;
|
---|
| 117 | break;
|
---|
| 118 | case 'B': // a(B)ort
|
---|
| 119 | signal(SIGABRT, SIG_DFL); /* prevent SIGABRT handler from running */
|
---|
| 120 | raise(SIGABRT);
|
---|
| 121 | break; /* "can't get here" */
|
---|
| 122 | case 'D': // (D)ebug, aka asm("int 3")
|
---|
[1] | 123 | #ifdef __IA32__
|
---|
[49] | 124 | __asm__ __volatile__("int $3"); // break to debugger
|
---|
[1] | 125 | #endif
|
---|
[49] | 126 | break;
|
---|
| 127 | case 'E': // (E)xit
|
---|
| 128 | fatal_error("Failed assertion -- see above for details");
|
---|
| 129 | break; /* "can't get here" */
|
---|
| 130 | case 'I': // (I)gnore
|
---|
| 131 | break;
|
---|
| 132 | /* These next two work as follows:
|
---|
| 133 | the `default' catches the user's invalid choice and says so;
|
---|
| 134 | the '\n' catches the newline on the end and prints the prompt again.
|
---|
| 135 | */
|
---|
| 136 | case '\n':
|
---|
| 137 | printf
|
---|
| 138 | ("(I)gnore, ignore (A)ll, (D)ebug, a(B)ort, or (E)xit? ");
|
---|
| 139 | break;
|
---|
| 140 | default:
|
---|
| 141 | is_valid = FALSE;
|
---|
| 142 | printf("Invalid choice.\n");
|
---|
| 143 | break;
|
---|
| 144 | }
|
---|
| 145 | } while (!is_valid);
|
---|
| 146 |
|
---|
| 147 | if (ignoring_assertions) {
|
---|
| 148 | log_it("Ignoring ALL assertions from now on.");
|
---|
| 149 | } else {
|
---|
| 150 | log_it("Ignoring assertion: %s", exp);
|
---|
[1] | 151 | }
|
---|
| 152 |
|
---|
[49] | 153 | getchar(); // skip \n
|
---|
[1] | 154 |
|
---|
| 155 | #ifndef _XWIN
|
---|
[49] | 156 | if (!g_text_mode)
|
---|
| 157 | newtResume();
|
---|
[1] | 158 | #endif
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | /**
|
---|
| 162 | * Clean's up users' KDE desktops.
|
---|
| 163 | * @bug Details about this function are unknown.
|
---|
| 164 | */
|
---|
| 165 | void clean_up_KDE_desktop_if_necessary(void)
|
---|
| 166 | {
|
---|
[49] | 167 | char *tmp;
|
---|
[1] | 168 |
|
---|
[49] | 169 | malloc_string(tmp);
|
---|
| 170 | strcpy(tmp,
|
---|
| 171 | "for i in `find /root /home -type d -name Desktop -maxdepth 2`; do \
|
---|
[1] | 172 | file=$i/.directory; if [ -f \"$file\" ] ; then mv -f $file $file.old ; \
|
---|
| 173 | cat $file.old | awk '{if (index($0, \"rootimagesmindi\")) { while (length($0)>2) { getline;} ; } \
|
---|
| 174 | else { print $0;};}' > $file ; fi ; done");
|
---|
[49] | 175 | run_program_and_log_output(tmp, 5);
|
---|
| 176 | paranoid_free(tmp);
|
---|
[1] | 177 | }
|
---|
| 178 |
|
---|
| 179 |
|
---|
| 180 | /**
|
---|
| 181 | * Locate mondoarchive's home directory. Searches in /usr/local/mondo, /usr/share/mondo,
|
---|
| 182 | * /usr/local/share/mondo, /opt, or if all else fails, search /usr.
|
---|
| 183 | *
|
---|
| 184 | * @param home_sz String to store the home directory ("" if it could not be found).
|
---|
| 185 | * @return 0 for success, nonzero for failure.
|
---|
| 186 | */
|
---|
| 187 | int find_and_store_mondoarchives_home(char *home_sz)
|
---|
| 188 | {
|
---|
[49] | 189 | assert(home_sz != NULL);
|
---|
| 190 | strcpy(home_sz,
|
---|
| 191 | call_program_and_get_last_line_of_output
|
---|
| 192 | ("find /usr/lib/ /usr/local/ /usr/share/ /usr/local/share/ /opt/ /ramdisk/usr/share/ -type d -maxdepth 2 -name include -prune -o -type d -maxdepth 2 -path '*/mondo/restore-scripts' -printf '%h\n' 2> /dev/null"));
|
---|
[1] | 193 |
|
---|
[49] | 194 | if (home_sz[0] == '\0') {
|
---|
| 195 | strcpy(home_sz,
|
---|
| 196 | call_program_and_get_last_line_of_output
|
---|
| 197 | ("find /usr -type d -path '*/mondo/restore-scripts' -follow -maxdepth 3 -printf '%h\n' 2> /dev/null"));
|
---|
| 198 | }
|
---|
| 199 | if (home_sz[0] == '\0') {
|
---|
| 200 | return (1);
|
---|
| 201 | } else {
|
---|
| 202 | return (0);
|
---|
| 203 | }
|
---|
[1] | 204 | }
|
---|
| 205 |
|
---|
| 206 |
|
---|
| 207 | char *get_architecture()
|
---|
| 208 | {
|
---|
| 209 | #ifdef __IA32__
|
---|
[49] | 210 | return ("i386");
|
---|
[1] | 211 | #endif
|
---|
| 212 | #ifdef __IA64__
|
---|
[49] | 213 | return ("ia64");
|
---|
[1] | 214 | #endif
|
---|
[49] | 215 | return ("unknown");
|
---|
[1] | 216 | }
|
---|
| 217 |
|
---|
| 218 |
|
---|
| 219 |
|
---|
| 220 | double get_kernel_version()
|
---|
| 221 | {
|
---|
[49] | 222 | char *p, tmp[200];
|
---|
| 223 | double d;
|
---|
[1] | 224 | #ifdef __FreeBSD__
|
---|
[49] | 225 | // JOSH - FIXME :)
|
---|
| 226 | d = 5.2; // :-)
|
---|
[1] | 227 | #else
|
---|
[49] | 228 | strcpy(tmp, call_program_and_get_last_line_of_output("uname -r"));
|
---|
| 229 | p = strchr(tmp, '.');
|
---|
| 230 | if (p) {
|
---|
| 231 | p = strchr(++p, '.');
|
---|
| 232 | if (p) {
|
---|
| 233 | while (*p) {
|
---|
| 234 | *p = *(p + 1);
|
---|
| 235 | p++;
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
[1] | 239 | // log_msg(1, "tmp = '%s'", tmp);
|
---|
[49] | 240 | d = atof(tmp);
|
---|
[1] | 241 | #endif
|
---|
[49] | 242 | log_msg(1, "g_kernel_version = %f", d);
|
---|
| 243 | return (d);
|
---|
[1] | 244 | }
|
---|
| 245 |
|
---|
| 246 |
|
---|
| 247 |
|
---|
| 248 |
|
---|
| 249 |
|
---|
| 250 | /**
|
---|
| 251 | * Get the current time.
|
---|
| 252 | * @return number of seconds since the epoch.
|
---|
| 253 | */
|
---|
[49] | 254 | long get_time()
|
---|
[1] | 255 | {
|
---|
| 256 |
|
---|
| 257 | #if 0
|
---|
| 258 |
|
---|
[49] | 259 | /*@ pointers **************************************************** */
|
---|
| 260 | FILE *fin;
|
---|
[1] | 261 |
|
---|
[49] | 262 | /*@ buffers ***************************************************** */
|
---|
| 263 | char incoming[MAX_STR_LEN];
|
---|
[1] | 264 |
|
---|
[49] | 265 | /*@ end vars **************************************************** */
|
---|
[1] | 266 |
|
---|
[49] | 267 | if (!(fin = popen("date +%s", "r"))) {
|
---|
| 268 | log_OS_error("Cannot popen date");
|
---|
| 269 | return (0);
|
---|
| 270 | }
|
---|
| 271 | fgets(incoming, MAX_STR_LEN - 1, fin);
|
---|
| 272 | paranoid_pclose(fin);
|
---|
| 273 | return (atol(incoming));
|
---|
[1] | 274 | #else
|
---|
[49] | 275 | return (long) time((void *) 0);
|
---|
[1] | 276 | #endif
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 |
|
---|
| 280 |
|
---|
| 281 |
|
---|
| 282 |
|
---|
| 283 |
|
---|
| 284 |
|
---|
| 285 | /**
|
---|
| 286 | * Initialize a RAID volume structure, setting fields to zero. The
|
---|
| 287 | * actual hard drive is unaffected.
|
---|
| 288 | *
|
---|
| 289 | * @param raidrec The RAID volume structure to initialize.
|
---|
| 290 | * @note This function is system dependent.
|
---|
| 291 | */
|
---|
| 292 | #ifdef __FreeBSD__
|
---|
[49] | 293 | void initialize_raidrec(struct vinum_volume *raidrec)
|
---|
[1] | 294 | {
|
---|
[49] | 295 | int i, j;
|
---|
| 296 | raidrec->volname[0] = '\0';
|
---|
| 297 | raidrec->plexes = 0;
|
---|
| 298 | for (i = 0; i < 9; ++i) {
|
---|
| 299 | raidrec->plex[i].raidlevel = -1;
|
---|
| 300 | raidrec->plex[i].stripesize = 0;
|
---|
| 301 | raidrec->plex[i].subdisks = 0;
|
---|
| 302 | for (j = 0; j < 9; ++j) {
|
---|
| 303 | strcpy(raidrec->plex[i].sd[j].which_device, "");
|
---|
| 304 | }
|
---|
| 305 | }
|
---|
[1] | 306 | }
|
---|
| 307 | #else
|
---|
[49] | 308 | void initialize_raidrec(struct raid_device_record *raidrec)
|
---|
[1] | 309 | {
|
---|
[49] | 310 | assert(raidrec != NULL);
|
---|
| 311 | raidrec->raid_device[0] = '\0';
|
---|
| 312 | raidrec->raid_level = 0;
|
---|
| 313 | raidrec->chunk_size = 4;
|
---|
| 314 | raidrec->persistent_superblock = 1;
|
---|
| 315 | raidrec->data_disks.entries = 0;
|
---|
| 316 | raidrec->spare_disks.entries = 0;
|
---|
| 317 | raidrec->parity_disks.entries = 0;
|
---|
| 318 | raidrec->failed_disks.entries = 0;
|
---|
| 319 | raidrec->additional_vars.entries = 0;
|
---|
[1] | 320 | }
|
---|
| 321 | #endif
|
---|
| 322 |
|
---|
| 323 |
|
---|
| 324 |
|
---|
| 325 |
|
---|
| 326 | /**
|
---|
| 327 | * Insert modules that Mondo requires.
|
---|
| 328 | * Currently inserts @c dos, @c fat, @c vfat, and @c osst for Linux;
|
---|
| 329 | * @c msdosfs and @c ext2fs for FreeBSD.
|
---|
| 330 | */
|
---|
| 331 | void insmod_crucial_modules(void)
|
---|
| 332 | {
|
---|
| 333 | #ifdef __FreeBSD__
|
---|
[49] | 334 | system("kldstat | grep msdosfs || kldload msdosfs 2> /dev/null");
|
---|
| 335 | system("kldstat | grep ext2fs || kldload ext2fs 2> /dev/null");
|
---|
[1] | 336 | #else
|
---|
[49] | 337 | system("modprobe dos &> /dev/null");
|
---|
| 338 | system("modprobe fat &> /dev/null");
|
---|
| 339 | system("modprobe vfat &> /dev/null");
|
---|
| 340 | // system("modprobe osst &> /dev/null");
|
---|
[1] | 341 | #endif
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 |
|
---|
| 345 | /**
|
---|
| 346 | * Log a trace message to the trace file.
|
---|
| 347 | * @bug This function seems orphaned. Please remove.
|
---|
| 348 | */
|
---|
[49] | 349 | void log_trace(char *o)
|
---|
[1] | 350 | {
|
---|
[49] | 351 | /*@ pointers **************************************************** */
|
---|
| 352 | FILE *fout;
|
---|
[1] | 353 |
|
---|
[49] | 354 | /*@ buffers ***************************************************** */
|
---|
| 355 | char output[MAX_STR_LEN];
|
---|
[1] | 356 |
|
---|
[49] | 357 | /*@ int ****************************************************** */
|
---|
| 358 | int i;
|
---|
[1] | 359 |
|
---|
[49] | 360 | /*@ end vars *************************************************** */
|
---|
[1] | 361 |
|
---|
[49] | 362 | if (o[0] == '\0') {
|
---|
| 363 | return;
|
---|
| 364 | }
|
---|
| 365 | strcpy(output, o);
|
---|
| 366 | i = (int) strlen(output);
|
---|
| 367 | if (i <= 0) {
|
---|
| 368 | return;
|
---|
| 369 | }
|
---|
| 370 | if (output[i - 1] < 32) {
|
---|
| 371 | output[i - 1] = '\0';
|
---|
| 372 | }
|
---|
| 373 | if (g_text_mode
|
---|
| 374 | /* && !strstr(last_line_of_file(MONDO_LOGFILE),output) */ ) {
|
---|
| 375 | printf("%s\n", output);
|
---|
| 376 | }
|
---|
[1] | 377 |
|
---|
[49] | 378 | fout = fopen(MONDO_TRACEFILE, "a");
|
---|
| 379 | if (fout) {
|
---|
| 380 | fprintf(fout, "%s\n", output);
|
---|
| 381 | paranoid_fclose(fout);
|
---|
| 382 | } else {
|
---|
| 383 | log_OS_error("Cannot write to tracefile");
|
---|
| 384 | }
|
---|
[1] | 385 | }
|
---|
| 386 |
|
---|
| 387 |
|
---|
| 388 |
|
---|
| 389 |
|
---|
| 390 |
|
---|
| 391 | /**
|
---|
| 392 | * Finish configuring the backup information structure. Call this function
|
---|
| 393 | * to set the parameters that depend on those that can be given on the command
|
---|
| 394 | * line.
|
---|
| 395 | *
|
---|
| 396 | * @param bkpinfo The backup information structure. Fields modified/used:
|
---|
| 397 | * - Used: @c bkpinfo->backup_data
|
---|
| 398 | * - Used: @c bkpinfo->backup_media_type
|
---|
| 399 | * - Used: @c bkpinfo->cdrw_speed
|
---|
| 400 | * - Used: @c bkpinfo->compression_level
|
---|
| 401 | * - Used: @c bkpinfo->include_paths
|
---|
[20] | 402 | * - Used: @c bkpinfo->prefix
|
---|
[1] | 403 | * - Used: @c bkpinfo->isodir
|
---|
| 404 | * - Used: @c bkpinfo->manual_cd_tray
|
---|
| 405 | * - Used: @c bkpinfo->make_cd_use_lilo
|
---|
| 406 | * - Used: @c bkpinfo->media_device
|
---|
| 407 | * - Used: @c bkpinfo->nfs_mount
|
---|
| 408 | * - Used: @c bkpinfo->nonbootable_backup
|
---|
| 409 | * - Used: @c bkpinfo->scratchdir
|
---|
| 410 | * - Used: @c bkpinfo->tmpdir
|
---|
| 411 | * - Used: @c bkpinfo->use_lzo
|
---|
| 412 | * - Modified: @c bkpinfo->call_before_iso
|
---|
| 413 | * - Modified: @c bkpinfo->call_make_iso
|
---|
| 414 | * - Modified: @c bkpinfo->optimal_set_size
|
---|
| 415 | * - Modified: @c bkpinfo->zip_exe
|
---|
| 416 | * - Modified: @c bkpinfo->zip_suffix
|
---|
| 417 | *
|
---|
| 418 | * @return number of errors, or 0 for success.
|
---|
| 419 | * @note Also creates directories that are specified in the @c bkpinfo structure but
|
---|
| 420 | * do not exist.
|
---|
| 421 | */
|
---|
[49] | 422 | int post_param_configuration(struct s_bkpinfo *bkpinfo)
|
---|
[1] | 423 | {
|
---|
[49] | 424 | char *extra_cdrom_params;
|
---|
| 425 | char *mondo_mkisofs_sz;
|
---|
| 426 | char *command;
|
---|
| 427 | char *mtpt;
|
---|
| 428 | char *hostname, *ip_address;
|
---|
| 429 | int retval = 0;
|
---|
| 430 | long avm = 0;
|
---|
| 431 | char *colon;
|
---|
| 432 | char *cdr_exe;
|
---|
| 433 | char *tmp;
|
---|
| 434 | int rdsiz_MB;
|
---|
| 435 | char *iso_dev;
|
---|
| 436 | char *iso_mnt;
|
---|
| 437 | char *iso_tmp;
|
---|
| 438 | char *iso_path;
|
---|
[1] | 439 |
|
---|
[49] | 440 | assert(bkpinfo != NULL);
|
---|
| 441 | malloc_string(extra_cdrom_params);
|
---|
| 442 | malloc_string(mondo_mkisofs_sz);
|
---|
| 443 | malloc_string(command);
|
---|
| 444 | malloc_string(mtpt);
|
---|
| 445 | malloc_string(hostname);
|
---|
| 446 | malloc_string(ip_address);
|
---|
| 447 | malloc_string(cdr_exe);
|
---|
| 448 | malloc_string(tmp);
|
---|
| 449 | malloc_string(iso_dev);
|
---|
| 450 | malloc_string(iso_mnt);
|
---|
| 451 | malloc_string(iso_tmp);
|
---|
| 452 | malloc_string(iso_path);
|
---|
| 453 | bkpinfo->optimal_set_size =
|
---|
| 454 | (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type) ? 4 : 8) *
|
---|
| 455 | 1024;
|
---|
[1] | 456 |
|
---|
[49] | 457 | log_msg(1, "Foo");
|
---|
| 458 | if (bkpinfo->backup_media_type == tape) {
|
---|
| 459 | log_msg(1, "Bar");
|
---|
| 460 | sprintf(tmp, "mt -f %s status", bkpinfo->media_device);
|
---|
| 461 | log_msg(1, "tmp = '%s'", tmp);
|
---|
| 462 | if (run_program_and_log_output(tmp, 3)) {
|
---|
| 463 | fatal_error
|
---|
| 464 | ("Unable to open tape device. If you haven't specified it with -d, do so. If you already have, check your parameter. I think it's wrong.");
|
---|
| 465 | }
|
---|
[1] | 466 | }
|
---|
[49] | 467 | make_hole_for_dir(bkpinfo->scratchdir);
|
---|
| 468 | make_hole_for_dir(bkpinfo->tmpdir);
|
---|
| 469 | if (bkpinfo->backup_media_type == iso)
|
---|
| 470 | make_hole_for_dir(bkpinfo->isodir);
|
---|
[1] | 471 |
|
---|
[49] | 472 | run_program_and_log_output("uname -a", 5);
|
---|
| 473 | run_program_and_log_output("cat /etc/*issue*", 5);
|
---|
| 474 | sprintf(g_tmpfs_mountpt, "%s/tmpfs", bkpinfo->tmpdir);
|
---|
| 475 | sprintf(command, "mkdir -p %s", g_tmpfs_mountpt);
|
---|
| 476 | paranoid_system(command);
|
---|
| 477 | rdsiz_MB = PPCFG_RAMDISK_SIZE + g_tape_buffer_size_MB;
|
---|
[1] | 478 | #ifdef __FreeBSD__
|
---|
[49] | 479 | strcpy(tmp,
|
---|
| 480 | call_program_and_get_last_line_of_output
|
---|
| 481 | ("vmstat | tail -1 | tr -s ' ' | cut -d' ' -f6"));
|
---|
| 482 | avm += atol(tmp);
|
---|
| 483 | strcpy(tmp,
|
---|
| 484 | call_program_and_get_last_line_of_output
|
---|
| 485 | ("swapinfo | grep -v Device | tr -s ' ' | cut -d' ' -f4 | tr '\n' '+' | sed 's/+$//' | bc"));
|
---|
| 486 | avm += atol(tmp);
|
---|
| 487 | sprintf(command, "mdmfs -s %d%c md9 %s", rdsiz_MB, 'm',
|
---|
| 488 | g_tmpfs_mountpt);
|
---|
[1] | 489 | #else
|
---|
[49] | 490 | strcpy(tmp,
|
---|
| 491 | call_program_and_get_last_line_of_output
|
---|
| 492 | ("free | grep \":\" | tr -s ' ' '\t' | cut -f2 | head -n1"));
|
---|
| 493 | avm += atol(tmp);
|
---|
| 494 | sprintf(command, "mount /dev/shm -t tmpfs %s -o size=%d%c",
|
---|
| 495 | g_tmpfs_mountpt, rdsiz_MB, 'm');
|
---|
| 496 | run_program_and_log_output("cat /proc/cpuinfo", 5);
|
---|
| 497 | run_program_and_log_output
|
---|
| 498 | ("rpm -q newt newt-devel slang slang-devel ncurses ncurses-devel gcc",
|
---|
| 499 | 5);
|
---|
[1] | 500 | #endif
|
---|
[49] | 501 | if (avm / 1024 > rdsiz_MB * 3) {
|
---|
| 502 | if (run_program_and_log_output(command, 5)) {
|
---|
| 503 | g_tmpfs_mountpt[0] = '\0';
|
---|
| 504 | log_it("Failed to mount tmpfs");
|
---|
| 505 | } else {
|
---|
| 506 | log_it("Tmpfs mounted OK - %d MB", rdsiz_MB);
|
---|
| 507 | }
|
---|
| 508 | } else {
|
---|
| 509 | g_tmpfs_mountpt[0] = '\0';
|
---|
| 510 | log_it("It doesn't seem you have enough swap to use tmpfs. Fine.");
|
---|
[1] | 511 | }
|
---|
[49] | 512 |
|
---|
| 513 | if (bkpinfo->use_lzo) {
|
---|
| 514 | strcpy(bkpinfo->zip_exe, "lzop");
|
---|
| 515 | strcpy(bkpinfo->zip_suffix, "lzo");
|
---|
| 516 | } else if (bkpinfo->compression_level != 0) {
|
---|
| 517 | strcpy(bkpinfo->zip_exe, "bzip2");
|
---|
| 518 | strcpy(bkpinfo->zip_suffix, "bz2");
|
---|
| 519 | } else {
|
---|
| 520 | bkpinfo->zip_exe[0] = bkpinfo->zip_suffix[0] = '\0';
|
---|
[1] | 521 | }
|
---|
| 522 |
|
---|
| 523 | // DVD
|
---|
| 524 |
|
---|
[49] | 525 | if (bkpinfo->backup_media_type == dvd) {
|
---|
| 526 | extra_cdrom_params[0] = '\0';
|
---|
| 527 | mondo_mkisofs_sz[0] = '\0';
|
---|
| 528 | if (find_home_of_exe("growisofs")) {
|
---|
| 529 | strcpy(cdr_exe, "growisofs");
|
---|
| 530 | } // unlikely to be used
|
---|
| 531 | else {
|
---|
| 532 | fatal_error("Please install growisofs.");
|
---|
| 533 | }
|
---|
| 534 | if (bkpinfo->nonbootable_backup) {
|
---|
| 535 | strcat(mondo_mkisofs_sz, MONDO_GROWISOFS_NONBOOT);
|
---|
| 536 | } else if
|
---|
[1] | 537 | #ifdef __FreeBSD__
|
---|
[49] | 538 | (TRUE)
|
---|
[1] | 539 | #else
|
---|
[49] | 540 | (bkpinfo->make_cd_use_lilo)
|
---|
[1] | 541 | #endif
|
---|
| 542 | #ifdef __IA64__
|
---|
[49] | 543 | {
|
---|
| 544 | strcat(mondo_mkisofs_sz, MONDO_GROWISOFS_REGULAR_ELILO);
|
---|
| 545 | }
|
---|
[1] | 546 | #else
|
---|
| 547 | {
|
---|
[49] | 548 | strcat(mondo_mkisofs_sz, MONDO_GROWISOFS_REGULAR_LILO);
|
---|
[1] | 549 | }
|
---|
[49] | 550 | #endif
|
---|
| 551 | else
|
---|
| 552 | {
|
---|
| 553 | strcat(mondo_mkisofs_sz, MONDO_GROWISOFS_REGULAR_SYSLINUX);
|
---|
| 554 | }
|
---|
| 555 | if (bkpinfo->manual_cd_tray) {
|
---|
| 556 | fatal_error("Manual CD tray + DVD not supported yet.");
|
---|
| 557 | // -m isn't supported by growisofs, BTW...
|
---|
| 558 | } else {
|
---|
| 559 | sprintf(bkpinfo->call_make_iso,
|
---|
| 560 | "%s %s -Z %s . 2>> _ERR_",
|
---|
| 561 | mondo_mkisofs_sz,
|
---|
| 562 | extra_cdrom_params, bkpinfo->media_device);
|
---|
| 563 | }
|
---|
[89] | 564 | if (getenv("SUDO_COMMAND")) {
|
---|
| 565 | fatal_error
|
---|
| 566 | ("Can't write DVDs as sudo because growisofs doesn't support this - please see the growisofs manpage for details.");
|
---|
[72] | 567 | }
|
---|
[49] | 568 | log_msg(2, "call_make_iso (DVD res) is ... %s",
|
---|
| 569 | bkpinfo->call_make_iso);
|
---|
| 570 | } // end of DVD code
|
---|
[1] | 571 |
|
---|
| 572 | // CD-R or CD-RW
|
---|
[49] | 573 | if (bkpinfo->backup_media_type == cdrw
|
---|
| 574 | || bkpinfo->backup_media_type == cdr) {
|
---|
| 575 | extra_cdrom_params[0] = '\0';
|
---|
| 576 | if (!bkpinfo->manual_cd_tray) {
|
---|
| 577 | strcat(extra_cdrom_params, "-waiti ");
|
---|
| 578 | }
|
---|
| 579 | if (bkpinfo->backup_media_type == cdrw) {
|
---|
| 580 | strcat(extra_cdrom_params, "blank=fast ");
|
---|
| 581 | }
|
---|
| 582 | if (find_home_of_exe("cdrecord")) {
|
---|
| 583 | strcpy(cdr_exe, "cdrecord");
|
---|
| 584 | } else if (find_home_of_exe("dvdrecord")) {
|
---|
| 585 | strcpy(cdr_exe, "dvdrecord");
|
---|
| 586 | } else {
|
---|
| 587 | fatal_error("Please install either cdrecord or dvdrecord.");
|
---|
| 588 | }
|
---|
| 589 | if (bkpinfo->nonbootable_backup) {
|
---|
| 590 | strcpy(mondo_mkisofs_sz, MONDO_MKISOFS_NONBOOT);
|
---|
| 591 | } else if
|
---|
[1] | 592 | #ifdef __FreeBSD__
|
---|
[49] | 593 | (TRUE)
|
---|
[1] | 594 | #else
|
---|
[49] | 595 | (bkpinfo->make_cd_use_lilo)
|
---|
[1] | 596 | #endif
|
---|
| 597 | #ifdef __IA64__
|
---|
| 598 | {
|
---|
[49] | 599 | strcat(mondo_mkisofs_sz, MONDO_MKISOFS_REGULAR_ELILO);
|
---|
[1] | 600 | }
|
---|
[49] | 601 | #else
|
---|
[1] | 602 | {
|
---|
[49] | 603 | strcpy(mondo_mkisofs_sz, MONDO_MKISOFS_REGULAR_LILO);
|
---|
[1] | 604 | }
|
---|
[49] | 605 | #endif
|
---|
| 606 | else
|
---|
| 607 | {
|
---|
| 608 | strcpy(mondo_mkisofs_sz, MONDO_MKISOFS_REGULAR_SYSLINUX);
|
---|
| 609 | }
|
---|
| 610 | if (bkpinfo->manual_cd_tray) {
|
---|
| 611 | sprintf(bkpinfo->call_before_iso,
|
---|
| 612 | "%s -o %s/temporary.iso . 2>> _ERR_", mondo_mkisofs_sz,
|
---|
| 613 | bkpinfo->tmpdir);
|
---|
| 614 | sprintf(bkpinfo->call_make_iso,
|
---|
| 615 | "%s %s -v %s fs=4m dev=%s speed=%d %s/temporary.iso",
|
---|
| 616 | cdr_exe, (bkpinfo->please_dont_eject) ? " " : "-eject",
|
---|
| 617 | extra_cdrom_params, bkpinfo->media_device,
|
---|
| 618 | bkpinfo->cdrw_speed, bkpinfo->tmpdir);
|
---|
| 619 | } else {
|
---|
| 620 | sprintf(bkpinfo->call_make_iso,
|
---|
| 621 | "%s . 2>> _ERR_ | %s %s %s fs=4m dev=%s speed=%d -",
|
---|
| 622 | mondo_mkisofs_sz, cdr_exe,
|
---|
| 623 | (bkpinfo->please_dont_eject) ? " " : "-eject",
|
---|
| 624 | extra_cdrom_params, bkpinfo->media_device,
|
---|
| 625 | bkpinfo->cdrw_speed);
|
---|
| 626 | }
|
---|
| 627 | } // end of CD code
|
---|
[1] | 628 |
|
---|
[49] | 629 | /*
|
---|
| 630 | if (bkpinfo->backup_data && bkpinfo->backup_media_type == tape)
|
---|
| 631 | {
|
---|
| 632 | sprintf (tmp,
|
---|
| 633 | "dd if=/dev/zero of=%s bs=%ld count=32 2> /dev/null",
|
---|
| 634 | bkpinfo->media_device, bkpinfo->internal_tape_block_size);
|
---|
| 635 | if (system(tmp))
|
---|
| 636 | {
|
---|
| 637 | retval++;
|
---|
| 638 | fprintf (stderr,
|
---|
| 639 | "Cannot write to tape device. Is the tape set read-only?\n");
|
---|
| 640 | }
|
---|
| 641 | } // end of tape code
|
---|
| 642 | */
|
---|
[1] | 643 |
|
---|
| 644 |
|
---|
[49] | 645 | if (bkpinfo->backup_media_type == iso) {
|
---|
| 646 |
|
---|
[1] | 647 | /* Patch by Conor Daly <conor.daly@met.ie>
|
---|
| 648 | * 23-june-2004
|
---|
| 649 | * Break up isodir into iso_mnt and iso_path
|
---|
| 650 | * These will be used along with iso-dev at restore time
|
---|
| 651 | * to locate the ISOs where ever they're mounted
|
---|
| 652 | */
|
---|
| 653 |
|
---|
[49] | 654 | log_it("isodir = %s", bkpinfo->isodir);
|
---|
| 655 | sprintf(command, "df %s | tail -n1 | cut -d' ' -f1",
|
---|
| 656 | bkpinfo->isodir);
|
---|
| 657 | log_it("command = %s", command);
|
---|
| 658 | log_it("res of it = %s",
|
---|
| 659 | call_program_and_get_last_line_of_output(command));
|
---|
| 660 | sprintf(iso_dev, "%s",
|
---|
| 661 | call_program_and_get_last_line_of_output(command));
|
---|
| 662 | sprintf(tmp, "%s/ISO-DEV", bkpinfo->tmpdir);
|
---|
| 663 | write_one_liner_data_file(tmp,
|
---|
| 664 | call_program_and_get_last_line_of_output
|
---|
| 665 | (command));
|
---|
[1] | 666 |
|
---|
[49] | 667 | sprintf(command, "mount | grep -w %s | tail -n1 | cut -d' ' -f3",
|
---|
| 668 | iso_dev);
|
---|
| 669 | log_it("command = %s", command);
|
---|
| 670 | log_it("res of it = %s",
|
---|
| 671 | call_program_and_get_last_line_of_output(command));
|
---|
| 672 | sprintf(iso_mnt, "%s",
|
---|
| 673 | call_program_and_get_last_line_of_output(command));
|
---|
| 674 | sprintf(tmp, "%s/ISO-MNT", bkpinfo->tmpdir);
|
---|
| 675 | write_one_liner_data_file(tmp,
|
---|
| 676 | call_program_and_get_last_line_of_output
|
---|
| 677 | (command));
|
---|
| 678 | log_it("isomnt: %s, %d", iso_mnt, strlen(iso_mnt));
|
---|
| 679 | sprintf(iso_tmp, "%s", bkpinfo->isodir);
|
---|
| 680 | if (strlen(iso_tmp) < strlen(iso_mnt)) {
|
---|
| 681 | iso_path[0] = '\0';
|
---|
| 682 | } else {
|
---|
| 683 | sprintf(iso_path, "%s", iso_tmp + strlen(iso_mnt));
|
---|
| 684 | }
|
---|
| 685 | sprintf(tmp, "%s/ISODIR", bkpinfo->tmpdir);
|
---|
| 686 | write_one_liner_data_file(tmp, iso_path);
|
---|
| 687 | log_it("isodir: %s", iso_path);
|
---|
[1] | 688 |
|
---|
| 689 | /* End patch */
|
---|
[49] | 690 | } // end of iso code
|
---|
[1] | 691 |
|
---|
[49] | 692 | if (bkpinfo->backup_media_type == nfs) {
|
---|
| 693 | strcpy(hostname, bkpinfo->nfs_mount);
|
---|
| 694 | colon = strchr(hostname, ':');
|
---|
| 695 | if (!colon) {
|
---|
| 696 | log_it("nfs mount doesn't have a colon in it");
|
---|
| 697 | retval++;
|
---|
| 698 | } else {
|
---|
| 699 | struct hostent *hent;
|
---|
[1] | 700 |
|
---|
[49] | 701 | *colon = '\0';
|
---|
| 702 | hent = gethostbyname(hostname);
|
---|
| 703 | if (!hent) {
|
---|
| 704 | log_it("Can't resolve NFS mount (%s): %s", hostname,
|
---|
| 705 | hstrerror(h_errno));
|
---|
| 706 | retval++;
|
---|
| 707 | } else {
|
---|
[59] | 708 | strcpy(ip_address, inet_ntoa((struct in_addr)
|
---|
| 709 | *((struct in_addr *) hent->
|
---|
| 710 | h_addr)));
|
---|
[49] | 711 | strcat(ip_address, strchr(bkpinfo->nfs_mount, ':'));
|
---|
| 712 | strcpy(bkpinfo->nfs_mount, ip_address);
|
---|
| 713 | }
|
---|
| 714 | }
|
---|
[89] | 715 | store_nfs_config(bkpinfo);
|
---|
[49] | 716 | }
|
---|
[1] | 717 |
|
---|
[49] | 718 | log_it("Finished processing incoming params");
|
---|
| 719 | if (retval) {
|
---|
| 720 | fprintf(stderr, "Type 'man mondoarchive' for help.\n");
|
---|
| 721 | }
|
---|
[87] | 722 | sprintf(tmp, "%s", MONDO_TMPISOS);
|
---|
[49] | 723 | if (does_file_exist(tmp)) {
|
---|
| 724 | unlink(tmp);
|
---|
| 725 | }
|
---|
| 726 | if (strlen(bkpinfo->tmpdir) < 2 || strlen(bkpinfo->scratchdir) < 2) {
|
---|
| 727 | log_it("tmpdir or scratchdir are blank/missing");
|
---|
| 728 | retval++;
|
---|
| 729 | }
|
---|
| 730 | if (bkpinfo->include_paths[0] == '\0') {
|
---|
| 731 | // fatal_error ("Why no backup path?");
|
---|
| 732 | strcpy(bkpinfo->include_paths, "/");
|
---|
| 733 | }
|
---|
| 734 | chmod(bkpinfo->scratchdir, 0700);
|
---|
| 735 | chmod(bkpinfo->tmpdir, 0700);
|
---|
| 736 | g_backup_media_type = bkpinfo->backup_media_type;
|
---|
| 737 | paranoid_free(mtpt);
|
---|
| 738 | paranoid_free(extra_cdrom_params);
|
---|
| 739 | paranoid_free(mondo_mkisofs_sz);
|
---|
| 740 | paranoid_free(command);
|
---|
| 741 | paranoid_free(hostname);
|
---|
| 742 | paranoid_free(ip_address);
|
---|
| 743 | paranoid_free(cdr_exe);
|
---|
| 744 | paranoid_free(tmp);
|
---|
| 745 | paranoid_free(iso_dev);
|
---|
| 746 | paranoid_free(iso_mnt);
|
---|
| 747 | paranoid_free(iso_tmp);
|
---|
| 748 | paranoid_free(iso_path);
|
---|
| 749 | return (retval);
|
---|
[1] | 750 | }
|
---|
| 751 |
|
---|
| 752 |
|
---|
| 753 |
|
---|
| 754 | /**
|
---|
| 755 | * Do some miscellaneous setup tasks to be performed before filling @c bkpinfo.
|
---|
| 756 | * Seeds the random-number generator, loads important modules, checks the sanity
|
---|
| 757 | * of the user's Linux distribution, and deletes logfile.
|
---|
| 758 | * @param bkpinfo The backup information structure. Will be initialized.
|
---|
| 759 | * @return number of errors (0 for success)
|
---|
| 760 | */
|
---|
| 761 | int pre_param_configuration(struct s_bkpinfo *bkpinfo)
|
---|
| 762 | {
|
---|
[49] | 763 | int res = 0;
|
---|
[1] | 764 |
|
---|
[49] | 765 | make_hole_for_dir(MNT_CDROM);
|
---|
| 766 | assert(bkpinfo != NULL);
|
---|
| 767 | srandom((unsigned long) (time(NULL)));
|
---|
| 768 | insmod_crucial_modules();
|
---|
| 769 | reset_bkpinfo(bkpinfo); // also sets defaults ('/'=backup path, 3=compression level)
|
---|
| 770 | if (bkpinfo->disaster_recovery) {
|
---|
| 771 | if (!does_nonMS_partition_exist()) {
|
---|
| 772 | fatal_error
|
---|
| 773 | ("I am in disaster recovery mode\nPlease don't run mondoarchive.");
|
---|
| 774 | }
|
---|
| 775 | }
|
---|
[1] | 776 |
|
---|
[49] | 777 | unlink(MONDO_TRACEFILE);
|
---|
| 778 | run_program_and_log_output("rm -Rf /tmp/changed.files*", FALSE);
|
---|
| 779 | if (find_and_store_mondoarchives_home(g_mondo_home)) {
|
---|
| 780 | fprintf(stderr,
|
---|
| 781 | "Cannot find Mondo's homedir. I think you have >1 'mondo' directory on your hard disk. Please delete the superfluous 'mondo' directories and try again\n");
|
---|
| 782 | res++;
|
---|
| 783 | return (res);
|
---|
| 784 | }
|
---|
| 785 | res += some_basic_system_sanity_checks();
|
---|
| 786 | if (res) {
|
---|
| 787 | log_it("Your distribution did not pass Mondo's sanity test.");
|
---|
| 788 | }
|
---|
| 789 | g_current_media_number = 1;
|
---|
| 790 | bkpinfo->postnuke_tarball[0] = bkpinfo->nfs_mount[0] = '\0';
|
---|
| 791 | return (res);
|
---|
[1] | 792 | }
|
---|
| 793 |
|
---|
| 794 |
|
---|
| 795 |
|
---|
| 796 |
|
---|
| 797 | /**
|
---|
| 798 | * Reset all fields of the backup information structure to a sensible default.
|
---|
| 799 | * @param bkpinfo The @c bkpinfo to reset.
|
---|
| 800 | */
|
---|
[49] | 801 | void reset_bkpinfo(struct s_bkpinfo *bkpinfo)
|
---|
[1] | 802 | {
|
---|
[49] | 803 | int i;
|
---|
[1] | 804 |
|
---|
[49] | 805 | log_msg(1, "Hi");
|
---|
| 806 | assert(bkpinfo != NULL);
|
---|
| 807 | memset((void *) bkpinfo, 0, sizeof(struct s_bkpinfo));
|
---|
| 808 | bkpinfo->manual_cd_tray = FALSE;
|
---|
| 809 | bkpinfo->internal_tape_block_size = DEFAULT_INTERNAL_TAPE_BLOCK_SIZE;
|
---|
| 810 | bkpinfo->media_device[0] = '\0';
|
---|
| 811 | for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
|
---|
| 812 | bkpinfo->media_size[i] = -1;
|
---|
| 813 | }
|
---|
| 814 | bkpinfo->boot_loader = '\0';
|
---|
| 815 | bkpinfo->boot_device[0] = '\0';
|
---|
| 816 | bkpinfo->zip_exe[0] = '\0';
|
---|
| 817 | bkpinfo->zip_suffix[0] = '\0';
|
---|
| 818 | bkpinfo->restore_path[0] = '\0';
|
---|
| 819 | bkpinfo->use_lzo = FALSE;
|
---|
| 820 | bkpinfo->do_not_compress_these[0] = '\0';
|
---|
| 821 | bkpinfo->verify_data = FALSE;
|
---|
| 822 | bkpinfo->backup_data = FALSE;
|
---|
| 823 | bkpinfo->restore_data = FALSE;
|
---|
| 824 | bkpinfo->disaster_recovery =
|
---|
| 825 | (am_I_in_disaster_recovery_mode()? TRUE : FALSE);
|
---|
| 826 | if (bkpinfo->disaster_recovery) {
|
---|
| 827 | strcpy(bkpinfo->isodir, "/");
|
---|
| 828 | } else {
|
---|
| 829 | strcpy(bkpinfo->isodir, "/root/images/mondo");
|
---|
| 830 | }
|
---|
| 831 | strcpy(bkpinfo->prefix, "mondorescue");
|
---|
[1] | 832 |
|
---|
[49] | 833 | bkpinfo->scratchdir[0] = '\0';
|
---|
| 834 | bkpinfo->make_filelist = TRUE; // unless -J supplied to mondoarchive
|
---|
| 835 | sprintf(bkpinfo->tmpdir, "/tmp/tmpfs/mondo.tmp.%d", (int) (random() % 32768)); // for mondorestore
|
---|
| 836 | bkpinfo->optimal_set_size = 0;
|
---|
| 837 | bkpinfo->backup_media_type = none;
|
---|
| 838 | strcpy(bkpinfo->include_paths, "/");
|
---|
| 839 | bkpinfo->exclude_paths[0] = '\0';
|
---|
| 840 | bkpinfo->call_before_iso[0] = '\0';
|
---|
| 841 | bkpinfo->call_make_iso[0] = '\0';
|
---|
| 842 | bkpinfo->call_burn_iso[0] = '\0';
|
---|
| 843 | bkpinfo->call_after_iso[0] = '\0';
|
---|
| 844 | bkpinfo->image_devs[0] = '\0';
|
---|
| 845 | bkpinfo->postnuke_tarball[0] = '\0';
|
---|
| 846 | bkpinfo->kernel_path[0] = '\0';
|
---|
| 847 | bkpinfo->nfs_mount[0] = '\0';
|
---|
| 848 | bkpinfo->nfs_remote_dir[0] = '\0';
|
---|
| 849 | bkpinfo->wipe_media_first = FALSE;
|
---|
| 850 | bkpinfo->differential = FALSE;
|
---|
| 851 | bkpinfo->cdrw_speed = 0;
|
---|
[1] | 852 | // patch by Herman Kuster
|
---|
[49] | 853 | bkpinfo->differential = 0;
|
---|
[1] | 854 | // patch end
|
---|
[49] | 855 | bkpinfo->compression_level = 3;
|
---|
[1] | 856 | }
|
---|
| 857 |
|
---|
| 858 |
|
---|
| 859 |
|
---|
| 860 |
|
---|
| 861 | /**
|
---|
| 862 | * Get the remaining free space (in MB) on @p partition.
|
---|
| 863 | * @param partition The partition to check free space on (either a device or a mountpoint).
|
---|
| 864 | * @return The free space on @p partition, in MB.
|
---|
| 865 | */
|
---|
[49] | 866 | long free_space_on_given_partition(char *partition)
|
---|
[1] | 867 | {
|
---|
[49] | 868 | char command[MAX_STR_LEN], out_sz[MAX_STR_LEN];
|
---|
| 869 | long res;
|
---|
[1] | 870 |
|
---|
[49] | 871 | assert_string_is_neither_NULL_nor_zerolength(partition);
|
---|
[1] | 872 |
|
---|
[49] | 873 | sprintf(command, "df -m %s &> /dev/null", partition);
|
---|
| 874 | if (system(command)) {
|
---|
| 875 | return (-1);
|
---|
| 876 | } // partition does not exist
|
---|
| 877 | sprintf(command, "df -m %s | tail -n1 | tr -s ' ' '\t' | cut -f4",
|
---|
| 878 | partition);
|
---|
| 879 | strcpy(out_sz, call_program_and_get_last_line_of_output(command));
|
---|
| 880 | if (strlen(out_sz) == 0) {
|
---|
| 881 | return (-1);
|
---|
| 882 | } // error within df, probably
|
---|
| 883 | res = atol(out_sz);
|
---|
| 884 | return (res);
|
---|
[1] | 885 | }
|
---|
| 886 |
|
---|
| 887 |
|
---|
| 888 |
|
---|
| 889 | /**
|
---|
| 890 | * Check the user's system for sanity. Checks performed:
|
---|
| 891 | * - make sure user has enough RAM (32mb required, 64mb recommended)
|
---|
| 892 | * - make sure user has enough free space in @c /
|
---|
| 893 | * - check kernel for ramdisk support
|
---|
| 894 | * - make sure afio, cdrecord, mkisofs, bzip2, awk, md5sum, strings, mindi, and buffer exist
|
---|
| 895 | * - make sure CD-ROM is unmounted
|
---|
| 896 | * - make sure /etc/modules.conf exists
|
---|
| 897 | * - make sure user's mountlist is OK by running <tt>mindi --makemountlist</tt>
|
---|
| 898 | *
|
---|
| 899 | * @return number of problems with the user's setup (0 for success)
|
---|
| 900 | */
|
---|
[49] | 901 | int some_basic_system_sanity_checks()
|
---|
[1] | 902 | {
|
---|
| 903 |
|
---|
[49] | 904 | /*@ buffers ************ */
|
---|
| 905 | char tmp[MAX_STR_LEN];
|
---|
| 906 | // char command[MAX_STR_LEN];
|
---|
[1] | 907 |
|
---|
[49] | 908 | /*@ int's *************** */
|
---|
| 909 | int retval = 0;
|
---|
| 910 | long Lres;
|
---|
[1] | 911 |
|
---|
| 912 |
|
---|
[49] | 913 | mvaddstr_and_log_it(g_currentY, 0,
|
---|
| 914 | "Checking sanity of your Linux distribution");
|
---|
[1] | 915 | #ifndef __FreeBSD__
|
---|
[49] | 916 | if (system("which mkfs.vfat &> /dev/null")
|
---|
| 917 | && !system("which mkfs.msdos &> /dev/null")) {
|
---|
| 918 | log_it
|
---|
| 919 | ("OK, you've got mkfs.msdos but not mkfs.vfat; time for the fairy to wave her magic wand...");
|
---|
| 920 | run_program_and_log_output
|
---|
| 921 | ("ln -sf `which mkfs.msdos` /sbin/mkfs.vfat", FALSE);
|
---|
| 922 | }
|
---|
| 923 | strcpy(tmp,
|
---|
| 924 | call_program_and_get_last_line_of_output
|
---|
| 925 | ("free | grep Mem | head -n1 | tr -s ' ' '\t' | cut -f2"));
|
---|
| 926 | if (atol(tmp) < 35000) {
|
---|
| 927 | retval++;
|
---|
| 928 | log_to_screen("You must have at least 32MB of RAM to use Mondo.");
|
---|
| 929 | }
|
---|
| 930 | if (atol(tmp) < 66000) {
|
---|
| 931 | log_to_screen
|
---|
| 932 | ("WARNING! You have very little RAM. Please upgrade to 64MB or more.");
|
---|
| 933 | }
|
---|
[1] | 934 | #endif
|
---|
| 935 |
|
---|
[49] | 936 | if ((Lres = free_space_on_given_partition("/root")) == -1) {
|
---|
| 937 | Lres = free_space_on_given_partition("/");
|
---|
| 938 | }
|
---|
| 939 | log_it("Free space on given partition = %ld MB", Lres);
|
---|
[1] | 940 |
|
---|
[49] | 941 | if (Lres < 50) {
|
---|
| 942 | run_program_and_log_output
|
---|
| 943 | ("rm -Rf /root/images/mindi; mkdir -p /home/root/images/mindi; mkdir -p /root/images; ln -sf /home/root/images/mindi /root/images/mindi",
|
---|
| 944 | 3);
|
---|
| 945 | // fatal_error("Your / (or /root) partition has <50MB free. Please adjust your partition table to something saner.");
|
---|
| 946 | }
|
---|
[1] | 947 |
|
---|
[49] | 948 | if (system("which " MKE2FS_OR_NEWFS " > /dev/null 2> /dev/null")) {
|
---|
| 949 | retval++;
|
---|
| 950 | log_to_screen
|
---|
| 951 | ("Unable to find " MKE2FS_OR_NEWFS " in system path.");
|
---|
| 952 | fatal_error
|
---|
| 953 | ("Please use \"su -\", not \"su\" to become root. OK? ...and please don't e-mail the mailing list or me about this. Just read the message. :)");
|
---|
| 954 | }
|
---|
[1] | 955 | #ifndef __FreeBSD__
|
---|
[49] | 956 | if (run_program_and_log_output
|
---|
| 957 | ("cat /proc/devices | grep ramdisk", FALSE)) {
|
---|
| 958 | if (!ask_me_yes_or_no
|
---|
| 959 | ("Your kernel has no ramdisk support. That's mind-numbingly stupid but I'll allow it if you're planning to use a failsafe kernel. Are you?"))
|
---|
| 960 | {
|
---|
| 961 | // retval++;
|
---|
| 962 | log_to_screen
|
---|
| 963 | ("It looks as if your kernel lacks ramdisk and initrd support.");
|
---|
| 964 | log_to_screen
|
---|
| 965 | ("I'll allow you to proceed but FYI, if I'm right, your kernel is broken.");
|
---|
| 966 | }
|
---|
| 967 | }
|
---|
[1] | 968 | #endif
|
---|
[49] | 969 | retval += whine_if_not_found(MKE2FS_OR_NEWFS);
|
---|
| 970 | retval += whine_if_not_found("mkisofs");
|
---|
| 971 | if (system("which dvdrecord > /dev/null 2> /dev/null")) {
|
---|
| 972 | retval += whine_if_not_found("cdrecord");
|
---|
| 973 | }
|
---|
| 974 | retval += whine_if_not_found("bzip2");
|
---|
| 975 | retval += whine_if_not_found("awk");
|
---|
| 976 | retval += whine_if_not_found("md5sum");
|
---|
| 977 | retval += whine_if_not_found("strings");
|
---|
| 978 | retval += whine_if_not_found("mindi");
|
---|
| 979 | retval += whine_if_not_found("buffer");
|
---|
[1] | 980 |
|
---|
[49] | 981 | // abort if Windows partition but no ms-sys and parted
|
---|
| 982 | if (!run_program_and_log_output
|
---|
| 983 | ("mount | grep -w vfat | grep -v /dev/fd | grep -v nexdisk", 0)
|
---|
| 984 | ||
|
---|
| 985 | !run_program_and_log_output
|
---|
| 986 | ("mount | grep -w dos | grep -v /dev/fd | grep -v nexdisk", 0)) {
|
---|
| 987 | log_to_screen("I think you have a Windows 9x partition.");
|
---|
| 988 | retval += whine_if_not_found("parted");
|
---|
[1] | 989 | #ifndef __IA64__
|
---|
[49] | 990 | /* IA64 always has one vfat partition for EFI even without Windows */
|
---|
| 991 | // retval +=
|
---|
| 992 | if (!find_home_of_exe("ms-sys")) {
|
---|
| 993 | log_to_screen("Please install ms-sys just in case.");
|
---|
| 994 | }
|
---|
| 995 | #endif
|
---|
[1] | 996 | }
|
---|
| 997 |
|
---|
[49] | 998 | if (!find_home_of_exe("cmp")) {
|
---|
| 999 | if (!find_home_of_exe("true")) {
|
---|
| 1000 | whine_if_not_found("cmp");
|
---|
| 1001 | } else {
|
---|
| 1002 | log_to_screen
|
---|
| 1003 | ("Your system lacks the 'cmp' binary. I'll create a dummy cmp for you.");
|
---|
| 1004 | if (run_program_and_log_output
|
---|
| 1005 | ("cp -f `which true` /usr/bin/cmp", 0)) {
|
---|
| 1006 | fatal_error("Failed to create dummy 'cmp' file.");
|
---|
| 1007 | }
|
---|
| 1008 | }
|
---|
[1] | 1009 | }
|
---|
[49] | 1010 | run_program_and_log_output
|
---|
| 1011 | ("umount `mount | grep cdr | cut -d' ' -f3 | tr '\n' ' '`", 5);
|
---|
| 1012 | strcpy(tmp,
|
---|
| 1013 | call_program_and_get_last_line_of_output
|
---|
| 1014 | ("mount | grep -E \"cdr(om|w)\""));
|
---|
| 1015 | if (strcmp("", tmp)) {
|
---|
| 1016 | if (strstr(tmp, "autofs")) {
|
---|
| 1017 | log_to_screen
|
---|
| 1018 | ("Your CD-ROM is mounted via autofs. I therefore cannot tell");
|
---|
| 1019 | log_to_screen
|
---|
| 1020 | ("if a CD actually is inserted. If a CD is inserted, please");
|
---|
| 1021 | log_to_screen("eject it. Thank you.");
|
---|
| 1022 | log_it
|
---|
| 1023 | ("Ignoring autofs CD-ROM 'mount' since we hope nothing's in it.");
|
---|
| 1024 | } else
|
---|
| 1025 | if (run_program_and_log_output("uname -a | grep Knoppix", 5)) {
|
---|
| 1026 | retval++;
|
---|
| 1027 | fatal_error
|
---|
| 1028 | ("Your CD-ROM drive is mounted. Please unmount it.");
|
---|
| 1029 | }
|
---|
| 1030 | }
|
---|
[1] | 1031 | #ifndef __FreeBSD__
|
---|
[49] | 1032 | if (!does_file_exist("/etc/modules.conf")) {
|
---|
| 1033 | if (does_file_exist("/etc/conf.modules")) {
|
---|
| 1034 | log_it("Linking /etc/modules.conf to /etc/conf.modules");
|
---|
| 1035 | run_program_and_log_output
|
---|
| 1036 | ("ln -sf /etc/conf.modules /etc/modules.conf", 5);
|
---|
| 1037 | } else if (does_file_exist("/etc/modprobe.d")) {
|
---|
| 1038 | log_it
|
---|
| 1039 | ("Directory /etc/modprobe.d found. mindi will use its contents.");
|
---|
| 1040 | } else if (does_file_exist("/etc/modprobe.conf")) {
|
---|
| 1041 | log_it("Linking /etc/modules.conf to /etc/modprobe.conf");
|
---|
| 1042 | run_program_and_log_output
|
---|
| 1043 | ("ln -sf /etc/modprobe.conf /etc/modules.conf", 5);
|
---|
| 1044 | } else {
|
---|
| 1045 | retval++;
|
---|
| 1046 | log_to_screen
|
---|
| 1047 | ("Please find out what happened to /etc/modules.conf");
|
---|
| 1048 | }
|
---|
[1] | 1049 | }
|
---|
| 1050 | #endif
|
---|
| 1051 |
|
---|
[49] | 1052 | run_program_and_log_output("cat /etc/fstab", 5);
|
---|
[1] | 1053 | #ifdef __FreeBSD__
|
---|
[49] | 1054 | run_program_and_log_output("vinum printconfig", 5);
|
---|
[1] | 1055 | #else
|
---|
[49] | 1056 | run_program_and_log_output("cat /etc/raidtab", 5);
|
---|
[1] | 1057 | #endif
|
---|
| 1058 |
|
---|
[49] | 1059 | if (run_program_and_log_output("mindi -V", 1)) {
|
---|
| 1060 | log_to_screen("Could not ascertain mindi's version number.");
|
---|
| 1061 | log_to_screen
|
---|
| 1062 | ("You have not installed Mondo and/or Mindi properly.");
|
---|
| 1063 | log_to_screen("Please uninstall and reinstall them both.");
|
---|
| 1064 | fatal_error("Please reinstall Mondo and Mindi.");
|
---|
| 1065 | }
|
---|
| 1066 | if (run_program_and_log_output
|
---|
| 1067 | ("mindi --makemountlist /tmp/mountlist.txt.test", 5)) {
|
---|
| 1068 | log_to_screen
|
---|
| 1069 | ("Mindi --makemountlist /tmp/mountlist.txt.test failed for some reason.");
|
---|
| 1070 | log_to_screen
|
---|
| 1071 | ("Please run that command by hand and examine /var/log/mindi.log");
|
---|
| 1072 | log_to_screen
|
---|
| 1073 | ("for more information. Perhaps your /etc/fstab file is insane.");
|
---|
| 1074 | log_to_screen
|
---|
| 1075 | ("Perhaps Mindi's MakeMountlist() subroutine has a bug. We'll see.");
|
---|
| 1076 | retval++;
|
---|
| 1077 | }
|
---|
[1] | 1078 |
|
---|
[49] | 1079 | if (!run_program_and_log_output("fdisk -l | grep -i raid", 1)
|
---|
| 1080 | && !does_file_exist("/etc/raidtab")) {
|
---|
| 1081 | log_to_screen
|
---|
| 1082 | ("You have RAID partitions but no /etc/raidtab - creating one from /proc/mdstat");
|
---|
| 1083 | create_raidtab_from_mdstat("/etc/raidtab", "/proc/mdstat");
|
---|
| 1084 | }
|
---|
| 1085 |
|
---|
| 1086 | if (retval) {
|
---|
| 1087 | mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
|
---|
| 1088 | } else {
|
---|
| 1089 | mvaddstr_and_log_it(g_currentY++, 74, "Done.");
|
---|
| 1090 | }
|
---|
| 1091 | return (retval);
|
---|
[1] | 1092 | }
|
---|
| 1093 |
|
---|
| 1094 | /**
|
---|
| 1095 | * Retrieve the line containing @p label from the config file.
|
---|
| 1096 | * @param config_file The file to read from, usually @c /tmp/mondo-restore.cfg.
|
---|
| 1097 | * @param label What to read from the file.
|
---|
| 1098 | * @param value Where to put it.
|
---|
| 1099 | * @return 0 for success, 1 for failure.
|
---|
| 1100 | */
|
---|
[49] | 1101 | int read_cfg_var(char *config_file, char *label, char *value)
|
---|
[1] | 1102 | {
|
---|
[49] | 1103 | /*@ buffer ****************************************************** */
|
---|
| 1104 | char command[MAX_STR_LEN * 2];
|
---|
| 1105 | char tmp[MAX_STR_LEN];
|
---|
[1] | 1106 |
|
---|
[49] | 1107 | /*@ end vars *************************************************** */
|
---|
[1] | 1108 |
|
---|
[49] | 1109 | assert_string_is_neither_NULL_nor_zerolength(config_file);
|
---|
| 1110 | assert_string_is_neither_NULL_nor_zerolength(label);
|
---|
| 1111 | if (!does_file_exist(config_file)) {
|
---|
| 1112 | sprintf(tmp, "(read_cfg_var) Cannot find %s config file",
|
---|
| 1113 | config_file);
|
---|
| 1114 | log_to_screen(tmp);
|
---|
| 1115 | value[0] = '\0';
|
---|
| 1116 | return (1);
|
---|
| 1117 | } else if (strstr(value, "/dev/") && strstr(value, "t0")
|
---|
| 1118 | && !strcmp(label, "media-dev")) {
|
---|
| 1119 | log_msg(2, "FYI, I shan't read new value for %s - already got %s",
|
---|
| 1120 | label, value);
|
---|
| 1121 | return (0);
|
---|
| 1122 | } else {
|
---|
| 1123 | sprintf(command, "cat %s | grep \"%s .*\" | cut -d' ' -f2,3,4,5",
|
---|
| 1124 | config_file, label);
|
---|
| 1125 | strcpy(value, call_program_and_get_last_line_of_output(command));
|
---|
| 1126 | if (strlen(value) == 0) {
|
---|
| 1127 | return (1);
|
---|
| 1128 | } else {
|
---|
| 1129 | return (0);
|
---|
| 1130 | }
|
---|
[1] | 1131 | }
|
---|
| 1132 | }
|
---|
| 1133 |
|
---|
| 1134 |
|
---|
| 1135 |
|
---|
| 1136 | /**
|
---|
| 1137 | * Remount @c supermount if it was unmounted earlier.
|
---|
| 1138 | */
|
---|
| 1139 | void remount_supermounts_if_necessary()
|
---|
| 1140 | {
|
---|
[49] | 1141 | if (g_remount_cdrom_at_end) {
|
---|
| 1142 | run_program_and_log_output("mount " MNT_CDROM, FALSE);
|
---|
| 1143 | }
|
---|
| 1144 | if (g_remount_floppy_at_end) {
|
---|
| 1145 | run_program_and_log_output("mount " MNT_FLOPPY, FALSE);
|
---|
| 1146 | }
|
---|
[1] | 1147 | }
|
---|
| 1148 |
|
---|
| 1149 | /**
|
---|
| 1150 | * Unmount @c supermount if it's mounted.
|
---|
| 1151 | */
|
---|
| 1152 | void unmount_supermounts_if_necessary()
|
---|
| 1153 | {
|
---|
[49] | 1154 | if (run_program_and_log_output
|
---|
| 1155 | ("mount | grep cdrom | grep super", FALSE) == 0) {
|
---|
| 1156 | g_remount_cdrom_at_end = TRUE;
|
---|
| 1157 | run_program_and_log_output("umount " MNT_CDROM, FALSE);
|
---|
| 1158 | }
|
---|
| 1159 | if (run_program_and_log_output
|
---|
| 1160 | ("mount | grep floppy | grep super", FALSE) == 0) {
|
---|
| 1161 | g_remount_floppy_at_end = TRUE;
|
---|
| 1162 | run_program_and_log_output("umount " MNT_FLOPPY, FALSE);
|
---|
| 1163 | }
|
---|
[1] | 1164 | }
|
---|
| 1165 |
|
---|
| 1166 | /**
|
---|
| 1167 | * Whether we had to stop autofs (if so, restart it at end).
|
---|
| 1168 | */
|
---|
[49] | 1169 | bool g_autofs_stopped = FALSE;
|
---|
[1] | 1170 |
|
---|
| 1171 | /**
|
---|
| 1172 | * Path to the autofs initscript ("" if none exists).
|
---|
| 1173 | */
|
---|
| 1174 | char g_autofs_exe[MAX_STR_LEN];
|
---|
| 1175 |
|
---|
| 1176 | /**
|
---|
| 1177 | * Autofs initscript in Xandros Linux distribution.
|
---|
| 1178 | */
|
---|
| 1179 | #define XANDROS_AUTOFS_FNAME "/etc/init.d/xandros-autofs"
|
---|
| 1180 |
|
---|
| 1181 | /**
|
---|
| 1182 | * Autofs initscript in most Linux distributions.
|
---|
| 1183 | */
|
---|
| 1184 | #define STOCK_AUTOFS_FNAME "/etc/rc.d/init.d/autofs"
|
---|
| 1185 |
|
---|
| 1186 | /**
|
---|
| 1187 | * If autofs is mounted, stop it (restart at end).
|
---|
| 1188 | */
|
---|
| 1189 | void stop_autofs_if_necessary()
|
---|
| 1190 | {
|
---|
[49] | 1191 | char tmp[MAX_STR_LEN];
|
---|
| 1192 |
|
---|
| 1193 | g_autofs_exe[0] = '\0';
|
---|
| 1194 | if (does_file_exist(XANDROS_AUTOFS_FNAME)) {
|
---|
| 1195 | strcpy(g_autofs_exe, XANDROS_AUTOFS_FNAME);
|
---|
| 1196 | } else if (does_file_exist(STOCK_AUTOFS_FNAME)) {
|
---|
| 1197 | strcpy(g_autofs_exe, STOCK_AUTOFS_FNAME);
|
---|
| 1198 | }
|
---|
| 1199 |
|
---|
| 1200 | if (!g_autofs_exe[0]) {
|
---|
| 1201 | log_msg(3, "No autofs detected.");
|
---|
| 1202 | } else {
|
---|
| 1203 | log_msg(3, "%s --- autofs detected", g_autofs_exe);
|
---|
[1] | 1204 | // FIXME -- only disable it if it's running --- sprintf(tmp, "%s status", autofs_exe);
|
---|
[49] | 1205 | sprintf(tmp, "%s stop", g_autofs_exe);
|
---|
| 1206 | if (run_program_and_log_output(tmp, 2)) {
|
---|
| 1207 | log_it("Failed to stop autofs - I assume it wasn't running");
|
---|
| 1208 | } else {
|
---|
| 1209 | g_autofs_stopped = TRUE;
|
---|
| 1210 | log_it("Stopped autofs OK");
|
---|
| 1211 | }
|
---|
| 1212 | }
|
---|
[1] | 1213 | }
|
---|
| 1214 |
|
---|
| 1215 | /**
|
---|
| 1216 | * If autofs was stopped earlier, restart it.
|
---|
| 1217 | */
|
---|
| 1218 | void restart_autofs_if_necessary()
|
---|
| 1219 | {
|
---|
[49] | 1220 | char tmp[MAX_STR_LEN];
|
---|
| 1221 |
|
---|
| 1222 | if (!g_autofs_stopped || !g_autofs_exe[0]) {
|
---|
| 1223 | log_msg(3, "No autofs detected.");
|
---|
| 1224 | return;
|
---|
| 1225 | }
|
---|
| 1226 | sprintf(tmp, "%s start", g_autofs_exe);
|
---|
| 1227 | if (run_program_and_log_output(tmp, 2)) {
|
---|
| 1228 | log_it("Failed to start autofs");
|
---|
| 1229 | } else {
|
---|
| 1230 | g_autofs_stopped = FALSE;
|
---|
| 1231 | log_it("Started autofs OK");
|
---|
| 1232 | }
|
---|
[1] | 1233 | }
|
---|
| 1234 |
|
---|
| 1235 |
|
---|
| 1236 | /**
|
---|
| 1237 | * If this is a distribution like Gentoo that doesn't keep /boot mounted, mount it.
|
---|
| 1238 | */
|
---|
| 1239 | void mount_boot_if_necessary()
|
---|
| 1240 | {
|
---|
[49] | 1241 | char tmp[MAX_STR_LEN];
|
---|
| 1242 | char command[MAX_STR_LEN];
|
---|
[1] | 1243 |
|
---|
[49] | 1244 | log_msg(1, "Started sub");
|
---|
| 1245 | log_msg(4, "About to set g_boot_mountpt[0] to '\\0'");
|
---|
| 1246 | g_boot_mountpt[0] = '\0';
|
---|
| 1247 | log_msg(4, "Done. Great. Seeting command to something");
|
---|
| 1248 | strcpy(command,
|
---|
| 1249 | "cat /etc/fstab | grep -v \":\" | grep -vx \"#.*\" | grep -w \"/boot\" | tr -s ' ' '\t' | cut -f1 | head -n1");
|
---|
| 1250 | log_msg(4, "Cool. Command = '%s'", command);
|
---|
| 1251 | strcpy(tmp, call_program_and_get_last_line_of_output(command));
|
---|
| 1252 | log_msg(4, "tmp = '%s'", tmp);
|
---|
| 1253 | if (tmp[0]) {
|
---|
| 1254 | log_it("/boot is at %s according to /etc/fstab", tmp);
|
---|
| 1255 | if (strstr(tmp, "LABEL=")) {
|
---|
| 1256 | if (!run_program_and_log_output("mount /boot", 5)) {
|
---|
| 1257 | strcpy(g_boot_mountpt, "/boot");
|
---|
| 1258 | log_msg(1, "Mounted /boot");
|
---|
| 1259 | } else {
|
---|
| 1260 | log_it("...ignored cos it's a label :-)");
|
---|
| 1261 | }
|
---|
| 1262 | } else {
|
---|
| 1263 | sprintf(command, "mount | grep -w \"%s\"", tmp);
|
---|
| 1264 | log_msg(3, "command = %s", command);
|
---|
| 1265 | if (run_program_and_log_output(command, 5)) {
|
---|
| 1266 | strcpy(g_boot_mountpt, tmp);
|
---|
| 1267 | sprintf(tmp,
|
---|
| 1268 | "%s (your /boot partition) is not mounted. I'll mount it before backing up",
|
---|
| 1269 | g_boot_mountpt);
|
---|
| 1270 | log_it(tmp);
|
---|
| 1271 | sprintf(tmp, "mount %s", g_boot_mountpt);
|
---|
| 1272 | if (run_program_and_log_output(tmp, 5)) {
|
---|
| 1273 | g_boot_mountpt[0] = '\0';
|
---|
| 1274 | log_msg(1, "Plan B");
|
---|
| 1275 | if (!run_program_and_log_output("mount /boot", 5)) {
|
---|
| 1276 | strcpy(g_boot_mountpt, "/boot");
|
---|
| 1277 | log_msg(1, "Plan B worked");
|
---|
| 1278 | } else {
|
---|
| 1279 | log_msg(1,
|
---|
| 1280 | "Plan B failed. Unable to mount /boot for backup purposes. This probably means /boot is mounted already, or doesn't have its own partition.");
|
---|
| 1281 | }
|
---|
| 1282 | }
|
---|
| 1283 | }
|
---|
| 1284 | }
|
---|
[1] | 1285 | }
|
---|
[49] | 1286 | log_msg(1, "Ended sub");
|
---|
[1] | 1287 | }
|
---|
| 1288 |
|
---|
| 1289 |
|
---|
| 1290 | /**
|
---|
| 1291 | * If we mounted /boot earlier, unmount it.
|
---|
| 1292 | */
|
---|
| 1293 | void unmount_boot_if_necessary()
|
---|
| 1294 | {
|
---|
[49] | 1295 | char tmp[MAX_STR_LEN];
|
---|
[1] | 1296 |
|
---|
[49] | 1297 | log_msg(3, "starting");
|
---|
| 1298 | if (g_boot_mountpt[0]) {
|
---|
| 1299 | sprintf(tmp, "umount %s", g_boot_mountpt);
|
---|
| 1300 | if (run_program_and_log_output(tmp, 5)) {
|
---|
| 1301 | log_it("WARNING - unable to unmount /boot");
|
---|
| 1302 | }
|
---|
| 1303 | }
|
---|
| 1304 | log_msg(3, "leaving");
|
---|
[1] | 1305 | }
|
---|
| 1306 |
|
---|
| 1307 |
|
---|
| 1308 |
|
---|
| 1309 | /**
|
---|
| 1310 | * Write a line to a configuration file. Writes a line of the form,
|
---|
| 1311 | * @c label @c value.
|
---|
| 1312 | * @param config_file The file to write to. Usually @c mondo-restore.cfg.
|
---|
| 1313 | * @param label What to call this bit of data you're writing.
|
---|
| 1314 | * @param value The bit of data you're writing.
|
---|
| 1315 | * @return 0 for success, 1 for failure.
|
---|
| 1316 | */
|
---|
[49] | 1317 | int write_cfg_var(char *config_file, char *label, char *value)
|
---|
[1] | 1318 | {
|
---|
[49] | 1319 | /*@ buffers ***************************************************** */
|
---|
| 1320 | char command[MAX_STR_LEN * 2];
|
---|
| 1321 | char tempfile[MAX_STR_LEN];
|
---|
| 1322 | char tmp[MAX_STR_LEN];
|
---|
[1] | 1323 |
|
---|
| 1324 |
|
---|
[49] | 1325 | /*@ end vars *************************************************** */
|
---|
| 1326 | assert_string_is_neither_NULL_nor_zerolength(config_file);
|
---|
| 1327 | assert_string_is_neither_NULL_nor_zerolength(label);
|
---|
| 1328 | assert(value != NULL);
|
---|
| 1329 | if (!does_file_exist(config_file)) {
|
---|
| 1330 | sprintf(tmp, "(write_cfg_file) Cannot find %s config file",
|
---|
| 1331 | config_file);
|
---|
| 1332 | log_to_screen(tmp);
|
---|
| 1333 | return (1);
|
---|
| 1334 | }
|
---|
| 1335 | strcpy(tempfile,
|
---|
| 1336 | call_program_and_get_last_line_of_output
|
---|
| 1337 | ("mktemp -q /tmp/mojo-jojo.blah.XXXXXX"));
|
---|
| 1338 | if (does_file_exist(config_file)) {
|
---|
| 1339 | sprintf(command, "cat %s | grep -vx \"%s .*\" > %s", config_file,
|
---|
| 1340 | label, tempfile);
|
---|
| 1341 | paranoid_system(command);
|
---|
| 1342 | }
|
---|
| 1343 | sprintf(command, "echo \"%s %s\" >> %s", label, value, tempfile);
|
---|
| 1344 | paranoid_system(command);
|
---|
| 1345 | sprintf(command, "mv -f %s %s", tempfile, config_file);
|
---|
| 1346 | paranoid_system(command);
|
---|
| 1347 | unlink(tempfile);
|
---|
| 1348 | return (0);
|
---|
[1] | 1349 | }
|
---|
| 1350 |
|
---|
| 1351 |
|
---|
| 1352 | /**
|
---|
| 1353 | * The standard log_debug_msg() (log_msg() also due to a macro). Writes some describing
|
---|
| 1354 | * information to the logfile.
|
---|
| 1355 | */
|
---|
| 1356 | void standard_log_debug_msg(int debug_level, const char *szFile,
|
---|
[49] | 1357 | const char *szFunction, int nLine,
|
---|
| 1358 | const char *fmt, ...)
|
---|
[1] | 1359 | {
|
---|
[49] | 1360 | va_list args;
|
---|
| 1361 | int i;
|
---|
| 1362 | static int depth = 0;
|
---|
| 1363 | char *tmp;
|
---|
| 1364 | FILE *fout;
|
---|
[1] | 1365 |
|
---|
[49] | 1366 | if (depth > 5) {
|
---|
| 1367 | depth--;
|
---|
| 1368 | return;
|
---|
| 1369 | }
|
---|
| 1370 | depth++;
|
---|
[1] | 1371 |
|
---|
[49] | 1372 | malloc_string(tmp);
|
---|
[1] | 1373 |
|
---|
[49] | 1374 | if (debug_level <= g_loglevel) {
|
---|
| 1375 | va_start(args, fmt);
|
---|
| 1376 | if (!(fout = fopen(MONDO_LOGFILE, "a"))) {
|
---|
| 1377 | return;
|
---|
| 1378 | } // fatal_error("Failed to openout to logfile - sheesh..."); }
|
---|
[1] | 1379 |
|
---|
[49] | 1380 | // add tabs to distinguish log levels
|
---|
| 1381 | if (debug_level > 0) {
|
---|
| 1382 | for (i = 1; i < debug_level; i++)
|
---|
| 1383 | fprintf(fout, "\t");
|
---|
| 1384 | if (getpid() == g_main_pid)
|
---|
| 1385 | fprintf(fout, "[Main] %s->%s#%d: ", szFile, szFunction,
|
---|
| 1386 | nLine);
|
---|
| 1387 | else if (getpid() == g_buffer_pid && g_buffer_pid > 0)
|
---|
| 1388 | fprintf(fout, "[Buff] %s->%s#%d: ", szFile, szFunction,
|
---|
| 1389 | nLine);
|
---|
| 1390 | else
|
---|
| 1391 | fprintf(fout, "[TH=%d] %s->%s#%d: ", getpid(), szFile,
|
---|
| 1392 | szFunction, nLine);
|
---|
| 1393 | }
|
---|
| 1394 | vfprintf(fout, fmt, args);
|
---|
[1] | 1395 |
|
---|
[49] | 1396 | // do not slow down the progran if standard debug level
|
---|
| 1397 | // must be enabled: if no flush, the log won't be up-to-date if there
|
---|
| 1398 | // is a segfault
|
---|
| 1399 | //if (g_dwDebugLevel != 1)
|
---|
[1] | 1400 |
|
---|
[49] | 1401 | va_end(args);
|
---|
| 1402 | fprintf(fout, "\n");
|
---|
| 1403 | paranoid_fclose(fout);
|
---|
| 1404 | }
|
---|
| 1405 | depth--;
|
---|
| 1406 | paranoid_free(tmp);
|
---|
[1] | 1407 | }
|
---|
| 1408 |
|
---|
| 1409 | /**
|
---|
| 1410 | * Function pointer to the @c log_debug_msg function to use. Points to standard_log_debug_msg() by default.
|
---|
| 1411 | */
|
---|
[49] | 1412 | void (*log_debug_msg) (int, const char *, const char *, int, const char *,
|
---|
| 1413 | ...) = standard_log_debug_msg;
|
---|
[1] | 1414 |
|
---|
| 1415 |
|
---|
| 1416 | /**
|
---|
| 1417 | * If @p y, malloc @p x, else free @p x.
|
---|
| 1418 | * @bug This function seems orphaned. Please remove.
|
---|
| 1419 | */
|
---|
| 1420 | #define do_alloc_or_free_depending(x,y) { if(y) {x=malloc(MAX_STR_LEN);} else {paranoid_free(x);} }
|
---|
| 1421 |
|
---|
| 1422 | /**
|
---|
| 1423 | * Allocate or free important globals, depending on @p mal.
|
---|
| 1424 | * @param mal If TRUE, malloc; if FALSE, free.
|
---|
| 1425 | */
|
---|
| 1426 | void do_libmondo_global_strings_thing(int mal)
|
---|
| 1427 | {
|
---|
[49] | 1428 | if (mal) {
|
---|
| 1429 | iamhere("Malloc'ing globals");
|
---|
| 1430 | malloc_string(g_boot_mountpt);
|
---|
| 1431 | malloc_string(g_mondo_home);
|
---|
| 1432 | malloc_string(g_tmpfs_mountpt);
|
---|
| 1433 | malloc_string(g_erase_tmpdir_and_scratchdir);
|
---|
| 1434 | malloc_string(g_serial_string);
|
---|
| 1435 | malloc_string(g_magicdev_command);
|
---|
| 1436 | } else {
|
---|
| 1437 | iamhere("Freeing globals");
|
---|
| 1438 | paranoid_free(g_boot_mountpt);
|
---|
| 1439 | paranoid_free(g_mondo_home);
|
---|
| 1440 | paranoid_free(g_tmpfs_mountpt);
|
---|
| 1441 | paranoid_free(g_erase_tmpdir_and_scratchdir);
|
---|
| 1442 | paranoid_free(g_serial_string);
|
---|
| 1443 | paranoid_free(g_magicdev_command);
|
---|
| 1444 | }
|
---|
[1] | 1445 |
|
---|
[49] | 1446 | /*
|
---|
| 1447 | char**list_of_arrays[] = {
|
---|
| 1448 | &g_boot_mountpt,
|
---|
| 1449 | &g_mondo_home,
|
---|
| 1450 | &g_tmpfs_mountpt,
|
---|
| 1451 | &g_erase_tmpdir_and_scratchdir,
|
---|
| 1452 | &g_serial_string,
|
---|
| 1453 | &g_magicdev_command,
|
---|
| 1454 | NULL};
|
---|
[1] | 1455 |
|
---|
[49] | 1456 | char**ppcurr;
|
---|
| 1457 | int i;
|
---|
| 1458 |
|
---|
| 1459 | for(i=0;list_of_arrays[i];i++)
|
---|
| 1460 | {
|
---|
| 1461 | log_msg(5, "Allocating %d", i);
|
---|
| 1462 | ppcurr = list_of_arrays[i];
|
---|
| 1463 | if (mal)
|
---|
| 1464 | { *ppcurr = malloc(MAX_STR_LEN); }
|
---|
| 1465 | else
|
---|
| 1466 | {
|
---|
| 1467 | if (*ppcurr)
|
---|
| 1468 | {
|
---|
| 1469 | free(*ppcurr);
|
---|
| 1470 | }
|
---|
| 1471 | }
|
---|
| 1472 | }
|
---|
| 1473 | log_msg(5, "Returning");
|
---|
| 1474 | */
|
---|
[1] | 1475 | }
|
---|
| 1476 |
|
---|
| 1477 | /**
|
---|
| 1478 | * Allocate important globals.
|
---|
| 1479 | * @see do_libmondo_global_strings_thing
|
---|
| 1480 | */
|
---|
| 1481 | void malloc_libmondo_global_strings(void)
|
---|
| 1482 | {
|
---|
[49] | 1483 | do_libmondo_global_strings_thing(1);
|
---|
[1] | 1484 | }
|
---|
| 1485 |
|
---|
| 1486 | /**
|
---|
| 1487 | * Free important globals.
|
---|
| 1488 | * @see do_libmondo_global_strings_thing
|
---|
| 1489 | */
|
---|
| 1490 | void free_libmondo_global_strings(void)
|
---|
| 1491 | {
|
---|
[49] | 1492 | do_libmondo_global_strings_thing(0);
|
---|
[1] | 1493 | }
|
---|
| 1494 |
|
---|
| 1495 |
|
---|
| 1496 |
|
---|
| 1497 | /**
|
---|
| 1498 | * Stop @c magicdev if it's running.
|
---|
| 1499 | * The command used to start it is saved in @p g_magicdev_command.
|
---|
| 1500 | */
|
---|
| 1501 | void stop_magicdev_if_necessary()
|
---|
| 1502 | {
|
---|
[49] | 1503 | strcpy(g_magicdev_command,
|
---|
| 1504 | call_program_and_get_last_line_of_output
|
---|
| 1505 | ("ps ax | grep -w magicdev | grep -v grep | tr -s '\t' ' '| cut -d' ' -f6-99"));
|
---|
| 1506 | if (g_magicdev_command[0]) {
|
---|
| 1507 | log_msg(1, "g_magicdev_command = '%s'", g_magicdev_command);
|
---|
| 1508 | paranoid_system("killall magicdev");
|
---|
| 1509 | }
|
---|
[1] | 1510 | }
|
---|
| 1511 |
|
---|
| 1512 |
|
---|
| 1513 | /**
|
---|
| 1514 | * Restart magicdev if it was stopped.
|
---|
| 1515 | */
|
---|
| 1516 | void restart_magicdev_if_necessary()
|
---|
| 1517 | {
|
---|
[49] | 1518 | char *tmp;
|
---|
| 1519 |
|
---|
| 1520 | malloc_string(tmp);
|
---|
| 1521 | if (g_magicdev_command && g_magicdev_command[0]) {
|
---|
| 1522 | sprintf(tmp, "%s &", g_magicdev_command);
|
---|
| 1523 | paranoid_system(tmp);
|
---|
| 1524 | }
|
---|
| 1525 | paranoid_free(tmp);
|
---|
[1] | 1526 | }
|
---|
| 1527 |
|
---|
| 1528 | /* @} - end of utilityGroup */
|
---|