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