[688] | 1 | /*
|
---|
| 2 | * $Id: libmondo-devices.c 979 2006-11-26 11:35:32Z bruno $
|
---|
[171] | 3 | * Subroutines for handling devices
|
---|
| 4 | */
|
---|
[1] | 5 | /**
|
---|
| 6 | * @file
|
---|
| 7 | * Functions to handle interactions with backup devices.
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | #include "my-stuff.h"
|
---|
| 11 | #include "mondostructures.h"
|
---|
| 12 | #include "libmondo-files-EXT.h"
|
---|
| 13 | #include "libmondo-devices.h"
|
---|
| 14 | #include "libmondo-string-EXT.h"
|
---|
| 15 | #include "libmondo-tools-EXT.h"
|
---|
[507] | 16 | #include "newt-specific-EXT.h"
|
---|
[1] | 17 | #include "libmondo-fork-EXT.h"
|
---|
| 18 | #include "libmondo-stream-EXT.h"
|
---|
[900] | 19 | #include "mr_mem.h"
|
---|
[1] | 20 |
|
---|
[146] | 21 | #include <sys/ioctl.h>
|
---|
[1] | 22 | #include <sys/types.h>
|
---|
[688] | 23 | #include <unistd.h>
|
---|
[1] | 24 | #ifdef __FreeBSD__
|
---|
| 25 | #define DKTYPENAMES
|
---|
| 26 | #define FSTYPENAMES
|
---|
| 27 | #include <sys/disklabel.h>
|
---|
| 28 | #include <sys/disk.h>
|
---|
| 29 | #elif linux
|
---|
| 30 | #define u64 unsigned long long
|
---|
[59] | 31 | #include <linux/fs.h> /* for BLKGETSIZE64 */
|
---|
[1] | 32 | #include <linux/hdreg.h>
|
---|
| 33 | #endif
|
---|
| 34 |
|
---|
| 35 | /*@unused@*/
|
---|
[59] | 36 | //static char cvsid[] = "$Id: libmondo-devices.c 979 2006-11-26 11:35:32Z bruno $";
|
---|
[1] | 37 |
|
---|
| 38 | extern int g_current_media_number;
|
---|
| 39 | extern double g_kernel_version;
|
---|
| 40 |
|
---|
| 41 | extern bool g_ISO_restore_mode;
|
---|
| 42 | extern struct s_bkpinfo *g_bkpinfo_DONTUSETHIS;
|
---|
| 43 | extern char *g_erase_tmpdir_and_scratchdir;
|
---|
[75] | 44 | extern char *g_selfmounted_isodir;
|
---|
[1] | 45 |
|
---|
[171] | 46 | static char *g_cdrw_drive_is_here = NULL;
|
---|
| 47 | static char *g_cdrom_drive_is_here = NULL;
|
---|
| 48 | static char *g_dvd_drive_is_here = NULL;
|
---|
[1] | 49 |
|
---|
| 50 |
|
---|
| 51 | /**
|
---|
| 52 | * ????? @bug ?????
|
---|
| 53 | * @ingroup globalGroup
|
---|
| 54 | */
|
---|
[59] | 55 | bool g_restoring_live_from_cd = FALSE;
|
---|
[1] | 56 |
|
---|
[59] | 57 | extern t_bkptype g_backup_media_type; // set by main()
|
---|
[1] | 58 |
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 |
|
---|
| 62 | void set_g_cdrom_and_g_dvd_to_bkpinfo_value(struct s_bkpinfo *bkpinfo)
|
---|
| 63 | {
|
---|
[171] | 64 | if (bkpinfo->media_device != NULL) {
|
---|
[900] | 65 | mr_allocstr(g_cdrom_drive_is_here, bkpinfo->media_device); // just in case
|
---|
| 66 | mr_allocstr(g_dvd_drive_is_here, bkpinfo->media_device); // just in case
|
---|
[171] | 67 | }
|
---|
[1] | 68 | }
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 |
|
---|
| 72 | /**
|
---|
| 73 | * Retract all CD trays and wait for autorun to complete.
|
---|
| 74 | * @ingroup deviceGroup
|
---|
| 75 | */
|
---|
| 76 | void retract_CD_tray_and_defeat_autorun(void)
|
---|
| 77 | {
|
---|
| 78 | // log_it("rctada: Retracting all CD trays", __LINE__);
|
---|
[59] | 79 | if (strlen(g_cdrom_drive_is_here) > 0) {
|
---|
| 80 | inject_device(g_cdrom_drive_is_here);
|
---|
| 81 | }
|
---|
| 82 | if (strlen(g_dvd_drive_is_here) > 0) {
|
---|
| 83 | inject_device(g_dvd_drive_is_here);
|
---|
| 84 | }
|
---|
| 85 | if (strlen(g_cdrw_drive_is_here) > 0) {
|
---|
| 86 | inject_device(g_cdrw_drive_is_here);
|
---|
| 87 | }
|
---|
[1] | 88 | // log_it("rctada: killing autorun");
|
---|
| 89 | // run_program_and_log_output("killall autorun", TRUE);
|
---|
[59] | 90 | if (!run_program_and_log_output("ps | grep autorun | grep -v grep", 5)) {
|
---|
| 91 | log_it("autorun detected; sleeping for 2 seconds");
|
---|
| 92 | sleep(2);
|
---|
| 93 | }
|
---|
| 94 | log_it("rctada: Unmounting all CD drives", __LINE__);
|
---|
| 95 | run_program_and_log_output("umount /dev/cdr* /dev/dvd*", 5);
|
---|
[1] | 96 | }
|
---|
| 97 |
|
---|
| 98 |
|
---|
[783] | 99 | /**
|
---|
| 100 | * Mount the CD-ROM at @p mountpoint.
|
---|
| 101 | * @param device The device (or file if g_ISO_restore_mode) to mount.
|
---|
| 102 | * @param mountpoint The place to mount it.
|
---|
| 103 | * @return TRUE for success, FALSE for failure.
|
---|
| 104 | */
|
---|
| 105 | bool mount_CDROM_here(char *device, char *mountpoint)
|
---|
| 106 | {
|
---|
| 107 | /*@ buffer ****************************************************** */
|
---|
| 108 | char *command = NULL;
|
---|
| 109 | int retval = 0;
|
---|
[1] | 110 |
|
---|
[783] | 111 | assert_string_is_neither_NULL_nor_zerolength(device);
|
---|
| 112 | assert_string_is_neither_NULL_nor_zerolength(mountpoint);
|
---|
| 113 |
|
---|
| 114 | make_hole_for_dir(mountpoint);
|
---|
| 115 | if (isdigit(device[0])) {
|
---|
[900] | 116 | mr_free(device);
|
---|
[783] | 117 | device = find_cdrom_device(FALSE);
|
---|
| 118 | }
|
---|
| 119 | if (g_ISO_restore_mode) {
|
---|
| 120 |
|
---|
| 121 | #ifdef __FreeBSD__
|
---|
| 122 | char *dev = NULL;
|
---|
| 123 |
|
---|
| 124 | dev = make_vn(device));
|
---|
| 125 | if (!dev) {
|
---|
[900] | 126 | mr_asprintf(&command, "Unable to mount ISO (make_vn(%s) failed)",
|
---|
[783] | 127 | device);
|
---|
| 128 | fatal_error(command);
|
---|
| 129 | }
|
---|
[900] | 130 | mr_free(device);
|
---|
[783] | 131 | device = dev;
|
---|
| 132 | #endif
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | log_msg(4, "(mount_CDROM_here --- device=%s, mountpoint=%s", device,
|
---|
| 136 | mountpoint);
|
---|
| 137 | /*@ end vars *************************************************** */
|
---|
| 138 |
|
---|
| 139 | #ifdef __FreeBSD__
|
---|
[900] | 140 | mr_asprintf(&command, "mount_cd9660 -r %s %s 2>> %s",
|
---|
[783] | 141 | device, mountpoint, MONDO_LOGFILE);
|
---|
| 142 | #else
|
---|
[900] | 143 | mr_asprintf(&command, "mount %s -o ro,loop -t iso9660 %s 2>> %s",
|
---|
[783] | 144 | device, mountpoint, MONDO_LOGFILE);
|
---|
| 145 | #endif
|
---|
| 146 |
|
---|
| 147 | log_msg(4, command);
|
---|
| 148 | if (strncmp(device, "/dev/", 5) == 0) {
|
---|
| 149 | retract_CD_tray_and_defeat_autorun();
|
---|
| 150 | }
|
---|
| 151 | retval = system(command);
|
---|
| 152 | log_msg(1, "system(%s) returned %d", command, retval);
|
---|
[900] | 153 | mr_free(command);
|
---|
[783] | 154 |
|
---|
| 155 | if (retval == 0) {
|
---|
| 156 | return(TRUE);
|
---|
| 157 | } else {
|
---|
| 158 | return(FALSE);
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 |
|
---|
| 163 |
|
---|
[1] | 164 | /**
|
---|
| 165 | * Determine whether we're booted off a ramdisk.
|
---|
| 166 | * @return @c TRUE (we are) or @c FALSE (we aren't).
|
---|
| 167 | * @ingroup utilityGroup
|
---|
| 168 | */
|
---|
[59] | 169 | bool am_I_in_disaster_recovery_mode(void)
|
---|
[1] | 170 | {
|
---|
[59] | 171 | char *tmp, *comment;
|
---|
| 172 | bool is_this_a_ramdisk = FALSE;
|
---|
[1] | 173 |
|
---|
[900] | 174 | mr_asprintf(&tmp, where_is_root_mounted());
|
---|
| 175 | mr_asprintf(&comment, "root is mounted at %s\n", tmp);
|
---|
[59] | 176 | log_msg(0, comment);
|
---|
[900] | 177 | mr_free(comment);
|
---|
[146] | 178 |
|
---|
[59] | 179 | log_msg(0,
|
---|
| 180 | "No, Schlomo, that doesn't mean %s is the root partition. It's just a debugging message. Relax. It's part of am_I_in_disaster_recovery_mode().",
|
---|
| 181 | tmp);
|
---|
[1] | 182 |
|
---|
| 183 | #ifdef __FreeBSD__
|
---|
[59] | 184 | if (strstr(tmp, "/dev/md")) {
|
---|
| 185 | is_this_a_ramdisk = TRUE;
|
---|
| 186 | }
|
---|
[1] | 187 | #else
|
---|
[59] | 188 | if (!strncmp(tmp, "/dev/ram", 8)
|
---|
| 189 | || (!strncmp(tmp, "/dev/rd", 7) && !strcmp(tmp, "/dev/rd/")
|
---|
| 190 | && strncmp(tmp, "/dev/rd/cd", 10)) || strstr(tmp, "rootfs")
|
---|
| 191 | || !strcmp(tmp, "/dev/root")) {
|
---|
| 192 | is_this_a_ramdisk = TRUE;
|
---|
| 193 | } else {
|
---|
| 194 | is_this_a_ramdisk = FALSE;
|
---|
| 195 | }
|
---|
[1] | 196 | #endif
|
---|
[900] | 197 | mr_free(tmp);
|
---|
[1] | 198 |
|
---|
[59] | 199 | if (is_this_a_ramdisk) {
|
---|
| 200 | if (!does_file_exist("/THIS-IS-A-RAMDISK")
|
---|
| 201 | && !does_file_exist("/tmp/mountlist.txt.sample")) {
|
---|
| 202 | log_to_screen
|
---|
[507] | 203 | (_("Using /dev/root is stupid of you but I'll forgive you."));
|
---|
[59] | 204 | is_this_a_ramdisk = FALSE;
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 | if (does_file_exist("/THIS-IS-A-RAMDISK")) {
|
---|
| 208 | is_this_a_ramdisk = TRUE;
|
---|
| 209 | }
|
---|
| 210 | log_msg(1, "Is this a ramdisk? result = %d", is_this_a_ramdisk);
|
---|
| 211 | return (is_this_a_ramdisk);
|
---|
[1] | 212 | }
|
---|
| 213 |
|
---|
| 214 |
|
---|
| 215 | /**
|
---|
| 216 | * Turn @c bkpinfo->backup_media_type into a human-readable string.
|
---|
| 217 | * @return The human readable string (e.g. @c cdr becomes <tt>"cdr"</tt>).
|
---|
| 218 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
| 219 | * @ingroup stringGroup
|
---|
| 220 | */
|
---|
[171] | 221 | char *bkptype_to_string(t_bkptype bt)
|
---|
[1] | 222 | {
|
---|
[171] | 223 | char *output = NULL;
|
---|
[146] | 224 |
|
---|
[900] | 225 | mr_free(output);
|
---|
[146] | 226 |
|
---|
[59] | 227 | switch (bt) {
|
---|
| 228 | case none:
|
---|
[900] | 229 | mr_asprintf(&output, _("none"));
|
---|
[1] | 230 | break;
|
---|
[59] | 231 | case iso:
|
---|
[900] | 232 | mr_asprintf(&output, _("iso"));
|
---|
[1] | 233 | break;
|
---|
[59] | 234 | case cdr:
|
---|
[900] | 235 | mr_asprintf(&output, _("cdr"));
|
---|
[1] | 236 | break;
|
---|
[59] | 237 | case cdrw:
|
---|
[900] | 238 | mr_asprintf(&output, _("cdrw"));
|
---|
[1] | 239 | break;
|
---|
[59] | 240 | case cdstream:
|
---|
[900] | 241 | mr_asprintf(&output, _("cdstream"));
|
---|
[1] | 242 | break;
|
---|
[59] | 243 | case nfs:
|
---|
[900] | 244 | mr_asprintf(&output, _("nfs"));
|
---|
[1] | 245 | break;
|
---|
[59] | 246 | case tape:
|
---|
[900] | 247 | mr_asprintf(&output, _("tape"));
|
---|
[1] | 248 | break;
|
---|
[59] | 249 | case udev:
|
---|
[900] | 250 | mr_asprintf(&output, _("udev"));
|
---|
[1] | 251 | break;
|
---|
[59] | 252 | default:
|
---|
[900] | 253 | mr_asprintf(&output, _("default"));
|
---|
[59] | 254 | }
|
---|
[147] | 255 | return (output);
|
---|
[1] | 256 | }
|
---|
| 257 |
|
---|
| 258 |
|
---|
| 259 | /**
|
---|
| 260 | * @addtogroup deviceGroup
|
---|
| 261 | * @{
|
---|
| 262 | */
|
---|
| 263 | /**
|
---|
| 264 | * Eject the tray of the specified CD device.
|
---|
| 265 | * @param dev The device to eject.
|
---|
| 266 | * @return the return value of the @c eject command. (0=success, nonzero=failure)
|
---|
| 267 | */
|
---|
[59] | 268 | int eject_device(char *dev)
|
---|
[1] | 269 | {
|
---|
[59] | 270 | char *command;
|
---|
| 271 | int res1 = 0, res2 = 0;
|
---|
[1] | 272 |
|
---|
[59] | 273 | if (IS_THIS_A_STREAMING_BACKUP(g_backup_media_type)
|
---|
| 274 | && g_backup_media_type != udev) {
|
---|
[900] | 275 | mr_asprintf(&command, "mt -f %s offline", dev);
|
---|
[59] | 276 | res1 = run_program_and_log_output(command, 1);
|
---|
[900] | 277 | mr_free(command);
|
---|
[59] | 278 | } else {
|
---|
| 279 | res1 = 0;
|
---|
| 280 | }
|
---|
[1] | 281 |
|
---|
| 282 | #ifdef __FreeBSD__
|
---|
[59] | 283 | if (strstr(dev, "acd")) {
|
---|
[900] | 284 | mr_asprintf(&command, "cdcontrol -f %s eject", dev);
|
---|
[59] | 285 | } else {
|
---|
[900] | 286 | mr_asprintf(&command, "camcontrol eject `echo %s | sed 's|/dev/||'`",
|
---|
[59] | 287 | dev);
|
---|
| 288 | }
|
---|
[1] | 289 | #else
|
---|
[900] | 290 | mr_asprintf(&command, "eject %s", dev);
|
---|
[1] | 291 | #endif
|
---|
| 292 |
|
---|
[59] | 293 | log_msg(3, "Ejecting %s", dev);
|
---|
| 294 | res2 = run_program_and_log_output(command, 1);
|
---|
[900] | 295 | mr_free(command);
|
---|
[59] | 296 | if (res1 && res2) {
|
---|
| 297 | return (1);
|
---|
| 298 | } else {
|
---|
| 299 | return (0);
|
---|
| 300 | }
|
---|
[1] | 301 | }
|
---|
| 302 |
|
---|
[146] | 303 |
|
---|
[1] | 304 | /**
|
---|
| 305 | * Load (inject) the tray of the specified CD device.
|
---|
| 306 | * @param dev The device to load/inject.
|
---|
| 307 | * @return 0 for success, nonzero for failure.
|
---|
| 308 | */
|
---|
[59] | 309 | int inject_device(char *dev)
|
---|
[1] | 310 | {
|
---|
[59] | 311 | char *command;
|
---|
| 312 | int i;
|
---|
[1] | 313 |
|
---|
| 314 | #ifdef __FreeBSD__
|
---|
[59] | 315 | if (strstr(dev, "acd")) {
|
---|
[900] | 316 | mr_asprintf(&command, "cdcontrol -f %s close", dev);
|
---|
[59] | 317 | } else {
|
---|
[900] | 318 | mr_asprintf(&command, "camcontrol load `echo %s | sed 's|/dev/||'`",
|
---|
[59] | 319 | dev);
|
---|
| 320 | }
|
---|
[1] | 321 | #else
|
---|
[900] | 322 | mr_asprintf(&command, "eject -t %s", dev);
|
---|
[1] | 323 | #endif
|
---|
[59] | 324 | i = run_program_and_log_output(command, FALSE);
|
---|
[900] | 325 | mr_free(command);
|
---|
[59] | 326 | return (i);
|
---|
[1] | 327 | }
|
---|
| 328 |
|
---|
| 329 |
|
---|
| 330 | /**
|
---|
| 331 | * Determine whether the specified @p device (really, you can use any file)
|
---|
| 332 | * exists.
|
---|
| 333 | * @return TRUE if it exists, FALSE if it doesn't.
|
---|
| 334 | */
|
---|
[59] | 335 | bool does_device_exist(char *device)
|
---|
[1] | 336 | {
|
---|
| 337 |
|
---|
[59] | 338 | /*@ buffers *********************************************************** */
|
---|
| 339 | char *tmp;
|
---|
[146] | 340 | bool ret;
|
---|
[1] | 341 |
|
---|
[59] | 342 | assert_string_is_neither_NULL_nor_zerolength(device);
|
---|
[1] | 343 |
|
---|
[900] | 344 | mr_asprintf(&tmp, "ls %s > /dev/null 2> /dev/null", device);
|
---|
[59] | 345 |
|
---|
| 346 | if (system(tmp)) {
|
---|
[146] | 347 | ret = FALSE;
|
---|
[59] | 348 | } else {
|
---|
[146] | 349 | ret = TRUE;
|
---|
[59] | 350 | }
|
---|
[900] | 351 | mr_free(tmp);
|
---|
[146] | 352 | return(ret);
|
---|
[1] | 353 | }
|
---|
| 354 |
|
---|
| 355 |
|
---|
| 356 | /**
|
---|
| 357 | * Determine whether a non-Microsoft partition exists on any connected hard drive.
|
---|
| 358 | * @return TRUE (there's a Linux/FreeBSD partition) or FALSE (Microsoft has taken over yet another innocent PC).
|
---|
| 359 | */
|
---|
[59] | 360 | bool does_nonMS_partition_exist(void)
|
---|
[1] | 361 | {
|
---|
| 362 | #if __FreeBSD__
|
---|
[59] | 363 | return
|
---|
| 364 | !system
|
---|
| 365 | ("for drive in /dev/ad? /dev/da?; do fdisk $drive | grep -q FreeBSD && exit 0; done; false");
|
---|
[1] | 366 | #else
|
---|
[59] | 367 | return
|
---|
| 368 | !system
|
---|
[687] | 369 | ("parted2fdisk -l 2>/dev/null | grep '^/dev/' | grep -Eqv '(MS|DOS|FAT|NTFS)'");
|
---|
[1] | 370 | #endif
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | /**
|
---|
| 374 | * Determine whether the specified @p partno exists on the specified @p drive.
|
---|
| 375 | * @param drive The drive to search for the partition in.
|
---|
| 376 | * @param partno The partition number to look for.
|
---|
| 377 | * @return 0 if it exists, nonzero otherwise.
|
---|
| 378 | */
|
---|
[59] | 379 | int does_partition_exist(const char *drive, int partno)
|
---|
[1] | 380 | {
|
---|
[59] | 381 | /*@ buffers **************************************************** */
|
---|
| 382 | char *program;
|
---|
[146] | 383 | char *incoming = NULL;
|
---|
[59] | 384 | char *searchstr;
|
---|
[688] | 385 | char *tmp;
|
---|
[1] | 386 |
|
---|
[59] | 387 | /*@ ints ******************************************************* */
|
---|
| 388 | int res = 0;
|
---|
[171] | 389 | size_t n = 0;
|
---|
[1] | 390 |
|
---|
[59] | 391 | /*@ pointers *************************************************** */
|
---|
| 392 | FILE *fin;
|
---|
[1] | 393 |
|
---|
| 394 |
|
---|
[59] | 395 | /*@ end vars *************************************************** */
|
---|
| 396 | assert_string_is_neither_NULL_nor_zerolength(drive);
|
---|
| 397 | assert(partno >= 0 && partno < 999);
|
---|
[1] | 398 |
|
---|
| 399 | #ifdef __FreeBSD__
|
---|
[59] | 400 | // We assume here that this is running from mondorestore. (It is.)
|
---|
[900] | 401 | mr_asprintf(&program, "ls %s >/dev/null 2>&1", drive);
|
---|
[146] | 402 | res = system(program);
|
---|
[900] | 403 | mr_free(program);
|
---|
[146] | 404 | return(res);
|
---|
[1] | 405 | #endif
|
---|
| 406 |
|
---|
[900] | 407 | mr_asprintf(&program, "parted2fdisk -l %s 2> /dev/null", drive);
|
---|
[59] | 408 | fin = popen(program, "r");
|
---|
| 409 | if (!fin) {
|
---|
| 410 | log_it("program=%s", program);
|
---|
| 411 | log_OS_error("Cannot popen-in program");
|
---|
[900] | 412 | mr_free(program);
|
---|
[59] | 413 | return (0);
|
---|
[1] | 414 | }
|
---|
[900] | 415 | mr_free(program);
|
---|
[146] | 416 |
|
---|
[688] | 417 | searchstr = build_partition_name(drive, partno);
|
---|
[900] | 418 | mr_asprintf(&tmp, "%s ", searchstr);
|
---|
| 419 | mr_free(searchstr);
|
---|
[688] | 420 |
|
---|
[900] | 421 | for (res = 0; !res ;) {
|
---|
| 422 | mr_getline(&incoming, &n, fin);
|
---|
[688] | 423 | if (strstr(incoming, tmp)) {
|
---|
[59] | 424 | res = 1;
|
---|
| 425 | }
|
---|
| 426 | }
|
---|
[900] | 427 | mr_free(incoming);
|
---|
[146] | 428 |
|
---|
[59] | 429 | if (pclose(fin)) {
|
---|
| 430 | log_OS_error("Cannot pclose fin");
|
---|
| 431 | }
|
---|
[900] | 432 | mr_free(tmp);
|
---|
[59] | 433 | return (res);
|
---|
[1] | 434 | }
|
---|
| 435 |
|
---|
| 436 |
|
---|
| 437 | /**
|
---|
| 438 | * Determine whether given NULL-terminated @p str exists in the MBR of @p dev.
|
---|
| 439 | * @param dev The device to look in.
|
---|
| 440 | * @param str The string to look for.
|
---|
| 441 | * @return TRUE if it exists, FALSE if it doesn't.
|
---|
| 442 | */
|
---|
[59] | 443 | bool does_string_exist_in_boot_block(char *dev, char *str)
|
---|
[1] | 444 | {
|
---|
[59] | 445 | /*@ buffers **************************************************** */
|
---|
| 446 | char *command;
|
---|
[1] | 447 |
|
---|
[59] | 448 | /*@ end vars *************************************************** */
|
---|
[146] | 449 | int ret;
|
---|
[1] | 450 |
|
---|
[59] | 451 | assert_string_is_neither_NULL_nor_zerolength(dev);
|
---|
| 452 | assert_string_is_neither_NULL_nor_zerolength(str);
|
---|
[1] | 453 |
|
---|
[900] | 454 | mr_asprintf(&command,
|
---|
[59] | 455 | "dd if=%s bs=446 count=1 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null",
|
---|
| 456 | dev, str);
|
---|
[146] | 457 | ret = system(command);
|
---|
[900] | 458 | mr_free(command);
|
---|
[146] | 459 | if (ret) {
|
---|
[59] | 460 | return (FALSE);
|
---|
| 461 | } else {
|
---|
| 462 | return (TRUE);
|
---|
| 463 | }
|
---|
[1] | 464 | }
|
---|
| 465 |
|
---|
[146] | 466 |
|
---|
[1] | 467 | /**
|
---|
| 468 | * Determine whether specified @p str exists in the first @p n sectors of
|
---|
| 469 | * @p dev.
|
---|
| 470 | * @param dev The device to look in.
|
---|
| 471 | * @param str The string to look for.
|
---|
| 472 | * @param n The number of 512-byte sectors to search.
|
---|
| 473 | */
|
---|
[59] | 474 | bool does_string_exist_in_first_N_blocks(char *dev, char *str, int n)
|
---|
[1] | 475 | {
|
---|
[59] | 476 | /*@ buffers **************************************************** */
|
---|
| 477 | char *command;
|
---|
| 478 | /*@ end vars *************************************************** */
|
---|
[146] | 479 | int ret;
|
---|
[1] | 480 |
|
---|
[900] | 481 | mr_asprintf(&command,
|
---|
[59] | 482 | "dd if=%s bs=512 count=%i 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null",
|
---|
| 483 | dev, n, str);
|
---|
[146] | 484 | ret = system(command);
|
---|
[900] | 485 | mr_free(command);
|
---|
[146] | 486 |
|
---|
| 487 | if (ret) {
|
---|
[59] | 488 | return (FALSE);
|
---|
| 489 | } else {
|
---|
| 490 | return (TRUE);
|
---|
| 491 | }
|
---|
[1] | 492 | }
|
---|
| 493 |
|
---|
| 494 |
|
---|
| 495 | /**
|
---|
| 496 | * Try to mount CD-ROM at @p mountpoint. If the CD-ROM is not found or has
|
---|
| 497 | * not been specified, call find_cdrom_device() to find it.
|
---|
| 498 | * @param bkpinfo The backup information structure. The only field used is @c bkpinfo->media_device.
|
---|
| 499 | * @param mountpoint Where to mount the CD-ROM.
|
---|
| 500 | * @return 0 for success, nonzero for failure.
|
---|
| 501 | * @see mount_CDROM_here
|
---|
| 502 | */
|
---|
[171] | 503 | bool find_and_mount_actual_cd(struct s_bkpinfo *bkpinfo, char *mountpoint)
|
---|
[1] | 504 | {
|
---|
[59] | 505 | /*@ buffers ***************************************************** */
|
---|
[1] | 506 |
|
---|
[59] | 507 | /*@ int's ****************************************************** */
|
---|
[171] | 508 | bool res = TRUE;
|
---|
| 509 | char *dev = NULL;
|
---|
[1] | 510 |
|
---|
[59] | 511 | /*@ end vars **************************************************** */
|
---|
[1] | 512 |
|
---|
[59] | 513 | assert(bkpinfo != NULL);
|
---|
| 514 | assert_string_is_neither_NULL_nor_zerolength(mountpoint);
|
---|
[1] | 515 |
|
---|
[59] | 516 | if (g_backup_media_type == dvd) {
|
---|
[171] | 517 | if (g_dvd_drive_is_here != NULL) {
|
---|
[900] | 518 | mr_asprintf(&dev, g_dvd_drive_is_here);
|
---|
[171] | 519 | } else {
|
---|
| 520 | dev = find_dvd_device();
|
---|
[59] | 521 | }
|
---|
| 522 | } else {
|
---|
[171] | 523 | if (g_cdrom_drive_is_here != NULL) {
|
---|
[900] | 524 | mr_asprintf(&dev, g_cdrom_drive_is_here);
|
---|
[171] | 525 | } else {
|
---|
[688] | 526 | // find_cdrom_device allocates the string
|
---|
[171] | 527 | dev = find_cdrom_device(FALSE);
|
---|
[59] | 528 | }
|
---|
| 529 | }
|
---|
[1] | 530 |
|
---|
[59] | 531 | if (bkpinfo->backup_media_type != iso) {
|
---|
| 532 | retract_CD_tray_and_defeat_autorun();
|
---|
| 533 | }
|
---|
[1] | 534 |
|
---|
[171] | 535 | if ((dev == NULL) || (! mount_CDROM_here(dev, mountpoint))) {
|
---|
[900] | 536 | mr_free(dev);
|
---|
[59] | 537 | if (!popup_and_get_string
|
---|
[688] | 538 | (_("CD-ROM device"), _("Please enter your CD-ROM's /dev device"), dev)) {
|
---|
[171] | 539 | res = FALSE;
|
---|
[59] | 540 | } else {
|
---|
| 541 | res = mount_CDROM_here(dev, mountpoint);
|
---|
| 542 | }
|
---|
| 543 | }
|
---|
| 544 | if (res) {
|
---|
[507] | 545 | log_msg(1, _("mount failed"));
|
---|
[59] | 546 | } else {
|
---|
[507] | 547 | log_msg(1, _("mount succeeded with %s"), dev);
|
---|
[59] | 548 | }
|
---|
[900] | 549 | mr_free(dev);
|
---|
[171] | 550 | return(res);
|
---|
[1] | 551 | }
|
---|
| 552 |
|
---|
| 553 |
|
---|
| 554 | /**
|
---|
| 555 | * Locate a CD-R/W writer's SCSI node.
|
---|
[171] | 556 | * @param cdrw_device SCSI node will be placed here. Caller needs to free it.
|
---|
| 557 | * @return the cdrw device or NULL if not found
|
---|
[1] | 558 | */
|
---|
[171] | 559 | char *find_cdrw_device(void)
|
---|
[1] | 560 | {
|
---|
[689] | 561 | char *comment = NULL;
|
---|
| 562 | char *tmp = NULL;
|
---|
| 563 | char *tmp1 = NULL;
|
---|
| 564 | char *cdr_exe = NULL;
|
---|
| 565 | char *command = NULL;
|
---|
| 566 | char *cdrw_device = NULL;
|
---|
[1] | 567 |
|
---|
[171] | 568 | if (g_cdrw_drive_is_here != NULL) {
|
---|
[900] | 569 | mr_asprintf(&cdrw_device, g_cdrw_drive_is_here);
|
---|
[59] | 570 | log_msg(3, "Been there, done that. Returning %s", cdrw_device);
|
---|
[171] | 571 | return(cdrw_device);
|
---|
[59] | 572 | }
|
---|
| 573 | if (g_backup_media_type == dvd) {
|
---|
| 574 | log_msg(1,
|
---|
| 575 | "This is dumb. You're calling find_cdrw_device() but you're backing up to DVD. WTF?");
|
---|
[171] | 576 | return(NULL);
|
---|
[59] | 577 | }
|
---|
| 578 | run_program_and_log_output("insmod ide-scsi", -1);
|
---|
[689] | 579 | tmp = find_home_of_exe("cdrecord");
|
---|
| 580 | if (tmp) {
|
---|
[900] | 581 | mr_asprintf(&cdr_exe, "cdrecord");
|
---|
[59] | 582 | } else {
|
---|
[900] | 583 | mr_asprintf(&cdr_exe, "dvdrecord");
|
---|
[59] | 584 | }
|
---|
[900] | 585 | mr_free(tmp);
|
---|
[689] | 586 |
|
---|
| 587 | tmp1 = find_home_of_exe(cdr_exe);
|
---|
| 588 | if (tmp1) {
|
---|
[900] | 589 | mr_asprintf(&command,
|
---|
[59] | 590 | "%s -scanbus 2> /dev/null | tr -s '\t' ' ' | grep \"[0-9]*,[0-9]*,[0-9]*\" | grep -v \"[0-9]*) \\*\" | grep CD | cut -d' ' -f2 | head -n1",
|
---|
| 591 | cdr_exe);
|
---|
[900] | 592 | mr_asprintf(&tmp, call_program_and_get_last_line_of_output(command));
|
---|
| 593 | mr_free(command);
|
---|
[146] | 594 | } else {
|
---|
[900] | 595 | mr_asprintf(&tmp, " ");
|
---|
[59] | 596 | }
|
---|
[900] | 597 | mr_free(tmp1);
|
---|
| 598 | mr_free(cdr_exe);
|
---|
[146] | 599 |
|
---|
[59] | 600 | if (strlen(tmp) < 2) {
|
---|
[900] | 601 | mr_free(tmp);
|
---|
[171] | 602 | return(NULL);
|
---|
[59] | 603 | } else {
|
---|
[171] | 604 | cdrw_device = tmp;
|
---|
[146] | 605 |
|
---|
[900] | 606 | mr_asprintf(&comment, "Found CDRW device - %s", cdrw_device);
|
---|
[59] | 607 | log_it(comment);
|
---|
[900] | 608 | mr_free(comment);
|
---|
[146] | 609 |
|
---|
[900] | 610 | mr_free(g_cdrw_drive_is_here);
|
---|
| 611 | mr_asprintf(&g_cdrw_drive_is_here, cdrw_device);
|
---|
[171] | 612 | return(cdrw_device);
|
---|
[59] | 613 | }
|
---|
[1] | 614 | }
|
---|
| 615 |
|
---|
| 616 |
|
---|
| 617 | /**
|
---|
| 618 | * Attempt to locate a CD-ROM device's /dev entry.
|
---|
| 619 | * Several different methods may be used to find the device, including
|
---|
| 620 | * calling @c cdrecord, searching @c dmesg, and trial-and-error.
|
---|
[171] | 621 | * @param output Where to put the located /dev entry. Needs to be freed by the caller.
|
---|
[1] | 622 | * @param try_to_mount Whether to mount the CD as part of the test; if mount
|
---|
| 623 | * fails then return failure.
|
---|
[171] | 624 | * @return output if success or NULL otherwise.
|
---|
[1] | 625 | */
|
---|
[171] | 626 | char *find_cdrom_device(bool try_to_mount)
|
---|
[1] | 627 | {
|
---|
[59] | 628 | /*@ pointers **************************************************** */
|
---|
[688] | 629 | FILE *fin = NULL;
|
---|
| 630 | char *p = NULL;
|
---|
| 631 | char *q = NULL;
|
---|
| 632 | char *r = NULL;
|
---|
[171] | 633 | char *output = NULL;
|
---|
| 634 | size_t n = 0;
|
---|
[1] | 635 |
|
---|
[59] | 636 | /*@ bool's ****************************************************** */
|
---|
| 637 | bool found_it = FALSE;
|
---|
[1] | 638 |
|
---|
[59] | 639 | /*@ buffers ***************************************************** */
|
---|
[146] | 640 | char *tmp = NULL;
|
---|
[688] | 641 | char *cdr_exe = NULL;
|
---|
[146] | 642 | #ifndef __FreeBSD__
|
---|
| 643 | char *phrase_two = NULL;
|
---|
| 644 | char *dvd_last_resort = NULL;
|
---|
| 645 | #endif
|
---|
[688] | 646 | char *command = NULL;
|
---|
| 647 | char *mountpoint = NULL;
|
---|
| 648 | static char *the_last_place_i_found_it = NULL;
|
---|
[1] | 649 |
|
---|
[59] | 650 | /*@ end vars **************************************************** */
|
---|
[1] | 651 |
|
---|
[171] | 652 | if ((g_cdrom_drive_is_here != NULL) && !isdigit(g_cdrom_drive_is_here[0])) {
|
---|
[900] | 653 | mr_asprintf(&output, g_cdrom_drive_is_here);
|
---|
[59] | 654 | log_msg(3, "Been there, done that. Returning %s", output);
|
---|
[171] | 655 | return(output);
|
---|
[59] | 656 | }
|
---|
[688] | 657 | if ((the_last_place_i_found_it != NULL) && !try_to_mount) {
|
---|
[900] | 658 | mr_asprintf(&output, the_last_place_i_found_it);
|
---|
[59] | 659 | log_msg(3,
|
---|
| 660 | "find_cdrom_device() --- returning last found location - '%s'",
|
---|
| 661 | output);
|
---|
[171] | 662 | return(output);
|
---|
[59] | 663 | }
|
---|
[1] | 664 |
|
---|
[689] | 665 | tmp = find_home_of_exe("cdrecord");
|
---|
| 666 | if (tmp) {
|
---|
[900] | 667 | mr_asprintf(&cdr_exe, "cdrecord");
|
---|
[59] | 668 | } else {
|
---|
[900] | 669 | mr_asprintf(&cdr_exe, "dvdrecord");
|
---|
[59] | 670 | }
|
---|
[900] | 671 | mr_free(tmp);
|
---|
[689] | 672 |
|
---|
| 673 | tmp = find_home_of_exe(cdr_exe);
|
---|
| 674 | if (!tmp) {
|
---|
[900] | 675 | mr_asprintf(&output, "/dev/cdrom");
|
---|
[59] | 676 | log_msg(4, "Can't find cdrecord; assuming %s", output);
|
---|
| 677 | if (!does_device_exist(output)) {
|
---|
| 678 | log_msg(4, "That didn't work. Sorry.");
|
---|
[900] | 679 | mr_free(cdr_exe);
|
---|
| 680 | mr_free(output);
|
---|
[171] | 681 | return(NULL);
|
---|
[59] | 682 | } else {
|
---|
[900] | 683 | mr_free(cdr_exe);
|
---|
[171] | 684 | return(output);
|
---|
[59] | 685 | }
|
---|
| 686 | }
|
---|
[900] | 687 | mr_free(tmp);
|
---|
[59] | 688 |
|
---|
[900] | 689 | mr_asprintf(&command, "%s -scanbus 2> /dev/null", cdr_exe);
|
---|
[59] | 690 | fin = popen(command, "r");
|
---|
| 691 | if (!fin) {
|
---|
| 692 | log_msg(4, "command=%s", command);
|
---|
| 693 | log_OS_error("Cannot popen command");
|
---|
[900] | 694 | mr_free(cdr_exe);
|
---|
| 695 | mr_free(command);
|
---|
[171] | 696 | return (NULL);
|
---|
[59] | 697 | }
|
---|
[900] | 698 | mr_free(command);
|
---|
[146] | 699 |
|
---|
[900] | 700 | for (mr_getline(&tmp, &n, fin); !feof(fin);
|
---|
| 701 | mr_getline(&tmp, &n, fin)) {
|
---|
[59] | 702 | p = strchr(tmp, '\'');
|
---|
| 703 | if (p) {
|
---|
| 704 | q = strchr(++p, '\'');
|
---|
| 705 | if (q) {
|
---|
| 706 | for (r = q; *(r - 1) == ' '; r--);
|
---|
| 707 | *r = '\0';
|
---|
| 708 | p = strchr(++q, '\'');
|
---|
| 709 | if (p) {
|
---|
| 710 | q = strchr(++p, '\'');
|
---|
| 711 | if (q) {
|
---|
| 712 | while (*(q - 1) == ' ') {
|
---|
| 713 | q--;
|
---|
| 714 | }
|
---|
| 715 | *q = '\0';
|
---|
[146] | 716 | #ifndef __FreeBSD__
|
---|
[900] | 717 | mr_free(phrase_two);
|
---|
| 718 | mr_asprintf(&phrase_two, p);
|
---|
[146] | 719 | #endif
|
---|
[59] | 720 | }
|
---|
| 721 | }
|
---|
[1] | 722 | }
|
---|
| 723 | }
|
---|
| 724 | }
|
---|
[59] | 725 | paranoid_pclose(fin);
|
---|
[900] | 726 | mr_free(tmp);
|
---|
[146] | 727 | tmp = NULL;
|
---|
| 728 | n = 0;
|
---|
[1] | 729 |
|
---|
| 730 | #ifndef __FreeBSD__
|
---|
[59] | 731 | if (strlen(phrase_two) == 0) {
|
---|
| 732 | log_msg(4, "Not running phase two. String is empty.");
|
---|
| 733 | } else {
|
---|
[900] | 734 | mr_asprintf(&command, "dmesg | grep \"%s\" 2> /dev/null", phrase_two);
|
---|
[59] | 735 | fin = popen(command, "r");
|
---|
| 736 | if (!fin) {
|
---|
| 737 | log_msg(4, "Cannot run 2nd command - non-fatal, fortunately");
|
---|
| 738 | } else {
|
---|
[900] | 739 | for (mr_getline(&tmp, &n, fin); !feof(fin);
|
---|
| 740 | mr_getline(&tmp, &n, fin)) {
|
---|
[59] | 741 | log_msg(5, "--> '%s'", tmp);
|
---|
| 742 | if (tmp[0] != ' ' && tmp[1] != ' ') {
|
---|
| 743 | p = strchr(tmp, ':');
|
---|
| 744 | if (p) {
|
---|
| 745 | *p = '\0';
|
---|
| 746 | if (strstr(tmp, "DVD")) {
|
---|
[900] | 747 | mr_free(dvd_last_resort);
|
---|
| 748 | mr_asprintf(&dvd_last_resort, "/dev/%s", tmp);
|
---|
[59] | 749 | log_msg(4,
|
---|
| 750 | "Ignoring '%s' because it's a DVD drive",
|
---|
| 751 | tmp);
|
---|
| 752 | } else {
|
---|
[900] | 753 | mr_asprintf(&output, "/dev/%s", tmp);
|
---|
[59] | 754 | found_it = TRUE;
|
---|
| 755 | }
|
---|
| 756 | }
|
---|
| 757 | }
|
---|
| 758 | }
|
---|
[900] | 759 | mr_free(tmp);
|
---|
[59] | 760 | paranoid_pclose(fin);
|
---|
| 761 | }
|
---|
[900] | 762 | mr_free(command);
|
---|
[59] | 763 | }
|
---|
[900] | 764 | mr_free(phrase_two);
|
---|
[1] | 765 |
|
---|
| 766 | #endif
|
---|
| 767 | #ifdef __FreeBSD__
|
---|
[59] | 768 | if (!found_it) {
|
---|
| 769 | log_msg(4, "OK, approach 2");
|
---|
| 770 | if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/cdrom"))) {
|
---|
| 771 | if (!
|
---|
| 772 | (found_it =
|
---|
| 773 | set_dev_to_this_if_rx_OK(output, "/dev/cdrom1"))) {
|
---|
| 774 | if (!
|
---|
| 775 | (found_it =
|
---|
| 776 | set_dev_to_this_if_rx_OK(output, "/dev/dvd"))) {
|
---|
| 777 | if (!
|
---|
| 778 | (found_it =
|
---|
| 779 | set_dev_to_this_if_rx_OK(output, "/dev/acd0"))) {
|
---|
| 780 | if (!
|
---|
| 781 | (found_it =
|
---|
| 782 | set_dev_to_this_if_rx_OK(output,
|
---|
| 783 | "/dev/cd01"))) {
|
---|
| 784 | if (!
|
---|
| 785 | (found_it =
|
---|
| 786 | set_dev_to_this_if_rx_OK(output,
|
---|
| 787 | "/dev/acd1"))) {
|
---|
| 788 | if (!
|
---|
| 789 | (found_it =
|
---|
| 790 | set_dev_to_this_if_rx_OK(output,
|
---|
| 791 | "/dev/cd1")))
|
---|
| 792 | {
|
---|
[900] | 793 | mr_free(cdr_exe);
|
---|
[171] | 794 | return(NULL);
|
---|
[59] | 795 | }
|
---|
| 796 | }
|
---|
| 797 | }
|
---|
| 798 | }
|
---|
| 799 | }
|
---|
| 800 | }
|
---|
| 801 | }
|
---|
| 802 | }
|
---|
[1] | 803 | #else
|
---|
[146] | 804 | if (dvd_last_resort != NULL) {
|
---|
| 805 | if (!found_it && strlen(dvd_last_resort) > 0) {
|
---|
| 806 | log_msg(4, "Well, I'll use the DVD - %s - as a last resort",
|
---|
| 807 | dvd_last_resort);
|
---|
[900] | 808 | mr_free(output);
|
---|
| 809 | mr_asprintf(&output, dvd_last_resort);
|
---|
[146] | 810 | found_it = TRUE;
|
---|
| 811 | }
|
---|
[1] | 812 | }
|
---|
[900] | 813 | mr_free(dvd_last_resort);
|
---|
[146] | 814 |
|
---|
[59] | 815 | if (found_it) {
|
---|
[900] | 816 | mr_asprintf(&tmp, "grep \"%s=ide-scsi\" /proc/cmdline &> /dev/null",
|
---|
[59] | 817 | strrchr(output, '/') + 1);
|
---|
| 818 | if (system(tmp) == 0) {
|
---|
| 819 | log_msg(4,
|
---|
| 820 | "%s is not right. It's being SCSI-emulated. Continuing.",
|
---|
| 821 | output);
|
---|
| 822 | found_it = FALSE;
|
---|
[900] | 823 | mr_free(output);
|
---|
[59] | 824 | }
|
---|
[900] | 825 | mr_free(tmp);
|
---|
[59] | 826 | }
|
---|
[1] | 827 |
|
---|
[59] | 828 | if (found_it) {
|
---|
| 829 | log_msg(4, "(find_cdrom_device) --> '%s'", output);
|
---|
| 830 | if (!does_device_exist(output)) {
|
---|
[171] | 831 | log_msg(4, "OK, I was wrong, I haven't found it... yet.");
|
---|
[59] | 832 | found_it = FALSE;
|
---|
[900] | 833 | mr_free(output);
|
---|
[59] | 834 | }
|
---|
| 835 | }
|
---|
[1] | 836 |
|
---|
[59] | 837 | if (!found_it) {
|
---|
| 838 | log_msg(4, "OK, approach 2");
|
---|
| 839 | if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/scd0"))) {
|
---|
| 840 | if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/sr0"))) {
|
---|
| 841 | if (!
|
---|
| 842 | (found_it =
|
---|
| 843 | set_dev_to_this_if_rx_OK(output, "/dev/cdrom"))) {
|
---|
| 844 | if (!
|
---|
| 845 | (found_it =
|
---|
| 846 | set_dev_to_this_if_rx_OK(output,
|
---|
| 847 | "/dev/cdrom0"))) {
|
---|
| 848 | if (!
|
---|
| 849 | (found_it =
|
---|
| 850 | set_dev_to_this_if_rx_OK(output,
|
---|
| 851 | "/dev/cdrom1"))) {
|
---|
| 852 | if (!
|
---|
| 853 | (found_it =
|
---|
| 854 | set_dev_to_this_if_rx_OK(output,
|
---|
| 855 | "/dev/sr1"))) {
|
---|
| 856 | if (!
|
---|
| 857 | (found_it =
|
---|
| 858 | set_dev_to_this_if_rx_OK(output,
|
---|
| 859 | "/dev/dvd")))
|
---|
| 860 | {
|
---|
| 861 | if (!
|
---|
| 862 | (found_it =
|
---|
| 863 | set_dev_to_this_if_rx_OK(output,
|
---|
| 864 | g_cdrw_drive_is_here)))
|
---|
| 865 | {
|
---|
[900] | 866 | mr_free(cdr_exe);
|
---|
[171] | 867 | return(NULL);
|
---|
[59] | 868 | }
|
---|
| 869 | }
|
---|
| 870 | }
|
---|
| 871 | }
|
---|
| 872 | }
|
---|
| 873 | }
|
---|
| 874 | }
|
---|
| 875 | }
|
---|
| 876 | }
|
---|
[1] | 877 | #endif
|
---|
| 878 |
|
---|
[900] | 879 | mr_asprintf(&mountpoint, "/tmp/cd.%d", (int) (random() % 32767));
|
---|
[146] | 880 | make_hole_for_dir(mountpoint);
|
---|
| 881 |
|
---|
[59] | 882 | if (found_it && try_to_mount) {
|
---|
[171] | 883 | if (! mount_CDROM_here(output, mountpoint)) {
|
---|
[59] | 884 | log_msg(4, "[Cardigans] I've changed my mind");
|
---|
| 885 | found_it = FALSE;
|
---|
[900] | 886 | mr_free(output);
|
---|
[59] | 887 | } else {
|
---|
[900] | 888 | mr_asprintf(&tmp, "%s/archives", mountpoint);
|
---|
[59] | 889 | if (!does_file_exist(tmp)) {
|
---|
| 890 | log_msg(4, "[Cardigans] I'll take it back");
|
---|
| 891 | found_it = FALSE;
|
---|
[900] | 892 | mr_free(output);
|
---|
[59] | 893 | } else {
|
---|
[900] | 894 | mr_asprintf(&command, "umount %s", output);
|
---|
[59] | 895 | paranoid_system(command);
|
---|
[900] | 896 | mr_free(command);
|
---|
[59] | 897 | log_msg(4, "I'm confident the Mondo CD is in %s", output);
|
---|
| 898 | }
|
---|
[900] | 899 | mr_free(tmp);
|
---|
[59] | 900 | }
|
---|
| 901 | }
|
---|
| 902 | unlink(mountpoint);
|
---|
[900] | 903 | mr_free(mountpoint);
|
---|
[1] | 904 |
|
---|
[59] | 905 | if (found_it) {
|
---|
| 906 | if (!does_file_exist(output)) {
|
---|
| 907 | log_msg(3, "I still haven't found it.");
|
---|
[900] | 908 | mr_free(output);
|
---|
[171] | 909 | return(NULL);
|
---|
[59] | 910 | }
|
---|
| 911 | log_msg(3, "(find_cdrom_device) --> '%s'", output);
|
---|
[900] | 912 | mr_asprintf(&the_last_place_i_found_it, output);
|
---|
| 913 | mr_free(g_cdrom_drive_is_here);
|
---|
| 914 | mr_asprintf(&g_cdrom_drive_is_here, output);
|
---|
[171] | 915 | return(output);
|
---|
[59] | 916 | }
|
---|
[1] | 917 |
|
---|
[900] | 918 | mr_asprintf(&command,
|
---|
[59] | 919 | "%s -scanbus | grep \"[0-9],[0-9],[0-9]\" | grep \"[D|C][V|D]\" | grep -n \"\" | grep \"%s\" | cut -d':' -f2",
|
---|
| 920 | cdr_exe, g_cdrw_drive_is_here);
|
---|
[900] | 921 | mr_free(cdr_exe);
|
---|
[171] | 922 |
|
---|
[59] | 923 | log_msg(1, "command=%s", command);
|
---|
[900] | 924 | mr_asprintf(&tmp, call_program_and_get_last_line_of_output(command));
|
---|
| 925 | mr_free(command);
|
---|
[146] | 926 |
|
---|
[59] | 927 | if (tmp[0]) {
|
---|
[171] | 928 | output = tmp;
|
---|
[59] | 929 | log_msg(4, "Finally found it at %s", output);
|
---|
| 930 | } else {
|
---|
[900] | 931 | mr_free(tmp);
|
---|
| 932 | mr_free(output);
|
---|
[59] | 933 | log_msg(4, "Still couldn't find it.");
|
---|
| 934 | }
|
---|
[171] | 935 | return(output);
|
---|
[1] | 936 | }
|
---|
| 937 |
|
---|
| 938 |
|
---|
[171] | 939 | char *find_dvd_device()
|
---|
[1] | 940 | {
|
---|
[688] | 941 | char *tmp = NULL;
|
---|
[59] | 942 | int retval = 0, devno = -1;
|
---|
[171] | 943 | char *output = NULL;
|
---|
[1] | 944 |
|
---|
[171] | 945 | if (g_dvd_drive_is_here != NULL) {
|
---|
[900] | 946 | mr_asprintf(&output, g_dvd_drive_is_here);
|
---|
[59] | 947 | log_msg(3, "Been there, done that. Returning %s", output);
|
---|
[171] | 948 | return (output);
|
---|
[59] | 949 | }
|
---|
[1] | 950 |
|
---|
[900] | 951 | mr_asprintf(&tmp, call_program_and_get_last_line_of_output
|
---|
[59] | 952 | ("dvdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1")
|
---|
[1] | 953 | );
|
---|
[59] | 954 | log_msg(5, "tmp = '%s'", tmp);
|
---|
[146] | 955 | if (!tmp[0]) {
|
---|
[900] | 956 | mr_free(tmp);
|
---|
| 957 | mr_asprintf(&tmp, call_program_and_get_last_line_of_output
|
---|
[59] | 958 | ("cdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1")
|
---|
| 959 | );
|
---|
[146] | 960 | }
|
---|
[59] | 961 | if (tmp[0]) {
|
---|
| 962 | devno = atoi(tmp) - 1;
|
---|
| 963 | }
|
---|
[900] | 964 | mr_free(tmp);
|
---|
[146] | 965 |
|
---|
[59] | 966 | if (devno >= 0) {
|
---|
| 967 | retval = 0;
|
---|
[900] | 968 | mr_asprintf(&output, "/dev/scd%d", devno);
|
---|
| 969 | mr_free(g_dvd_drive_is_here);
|
---|
| 970 | mr_asprintf(&g_dvd_drive_is_here, output);
|
---|
[59] | 971 | log_msg(2, "I think DVD is at %s", output);
|
---|
| 972 | } else {
|
---|
| 973 | log_msg(2, "I cannot find DVD");
|
---|
| 974 | }
|
---|
[1] | 975 |
|
---|
[171] | 976 | return(output);
|
---|
[1] | 977 | }
|
---|
| 978 |
|
---|
| 979 |
|
---|
| 980 | /**
|
---|
| 981 | * Find the size of the specified @p drive, in megabytes. Uses @c ioctl calls
|
---|
| 982 | * and @c dmesg.
|
---|
| 983 | * @param drive The device to find the size of.
|
---|
| 984 | * @return size in megabytes.
|
---|
| 985 | */
|
---|
[59] | 986 | long get_phys_size_of_drive(char *drive)
|
---|
[1] | 987 | {
|
---|
[59] | 988 | int fd;
|
---|
[1] | 989 | #if linux
|
---|
[59] | 990 | unsigned long long s = 0;
|
---|
| 991 | int fileid, cylinders = 0, cylindersleft = 0;
|
---|
| 992 | int cylindersize = 0;
|
---|
[1] | 993 | int gotgeo = 0;
|
---|
| 994 |
|
---|
[59] | 995 |
|
---|
| 996 | struct hd_geometry hdgeo;
|
---|
[1] | 997 | #elif __FreeBSD__
|
---|
[59] | 998 | off_t s;
|
---|
[1] | 999 | #endif
|
---|
| 1000 |
|
---|
[59] | 1001 | long outvalA = -1;
|
---|
| 1002 | long outvalB = -1;
|
---|
| 1003 | long outvalC = -1;
|
---|
[1] | 1004 |
|
---|
[59] | 1005 | if ((fd = open(drive, O_RDONLY)) != -1) {
|
---|
| 1006 | if (ioctl(fd,
|
---|
[1] | 1007 | #if linux
|
---|
[59] | 1008 | #ifdef BLKGETSIZE64
|
---|
| 1009 | BLKGETSIZE64,
|
---|
| 1010 | #else
|
---|
| 1011 | BLKGETSIZE,
|
---|
| 1012 | #endif
|
---|
[1] | 1013 | #elif __FreeBSD__
|
---|
[59] | 1014 | DIOCGMEDIASIZE,
|
---|
[1] | 1015 | #endif
|
---|
[59] | 1016 | &s) != -1) {
|
---|
| 1017 | close(fd);
|
---|
| 1018 | // s>>11 works for older disks but not for newer ones
|
---|
| 1019 | outvalB =
|
---|
[1] | 1020 | #if linux
|
---|
[59] | 1021 | #ifdef BLKGETSIZE64
|
---|
| 1022 | s >> 20
|
---|
[1] | 1023 | #else
|
---|
[59] | 1024 | s >> 11
|
---|
[1] | 1025 | #endif
|
---|
[59] | 1026 | #else
|
---|
| 1027 | s >> 20
|
---|
| 1028 | #endif
|
---|
| 1029 | ;
|
---|
| 1030 | }
|
---|
[1] | 1031 | }
|
---|
| 1032 |
|
---|
[59] | 1033 | if (outvalB <= 0) {
|
---|
| 1034 | log_msg(1, "Error getting size of %s: %s", drive, strerror(errno));
|
---|
[1] | 1035 | #if linux
|
---|
[59] | 1036 | fileid = open(drive, O_RDONLY);
|
---|
[1] | 1037 | if (fileid) {
|
---|
[59] | 1038 | if (ioctl(fileid, HDIO_GETGEO, &hdgeo) != -1) {
|
---|
[1] | 1039 | if (hdgeo.cylinders && hdgeo.heads && hdgeo.sectors) {
|
---|
| 1040 | cylindersleft = cylinders = hdgeo.cylinders;
|
---|
| 1041 | cylindersize = hdgeo.heads * hdgeo.sectors / 2;
|
---|
| 1042 | outvalA = cylindersize * cylinders / 1024;
|
---|
[59] | 1043 | log_msg(2, "Got Harddisk geometry, C:%d, H:%d, S:%d",
|
---|
| 1044 | hdgeo.cylinders, hdgeo.heads, hdgeo.sectors);
|
---|
[1] | 1045 | gotgeo = 1;
|
---|
| 1046 | } else {
|
---|
[59] | 1047 | log_msg(1, "Harddisk geometry wrong");
|
---|
[1] | 1048 | }
|
---|
| 1049 | } else {
|
---|
[59] | 1050 | log_msg(1,
|
---|
| 1051 | "Error in ioctl() getting new hard disk geometry (%s), resizing in unsafe mode",
|
---|
| 1052 | strerror(errno));
|
---|
[1] | 1053 | }
|
---|
| 1054 | close(fileid);
|
---|
| 1055 | } else {
|
---|
[59] | 1056 | log_msg(1, "Failed to open %s for reading: %s", drive,
|
---|
| 1057 | strerror(errno));
|
---|
[1] | 1058 | }
|
---|
| 1059 | if (!gotgeo) {
|
---|
[59] | 1060 | log_msg(1, "Failed to get harddisk geometry, using old mode");
|
---|
[1] | 1061 | }
|
---|
| 1062 | #endif
|
---|
[162] | 1063 | }
|
---|
[1] | 1064 | // OLDER DISKS will give ridiculously low value for outvalB (so outvalA is returned) :)
|
---|
| 1065 | // NEWER DISKS will give sane value for outvalB (close to outvalA, in other words) :)
|
---|
| 1066 |
|
---|
[59] | 1067 | outvalC = (outvalA > outvalB) ? outvalA : outvalB;
|
---|
| 1068 |
|
---|
[1] | 1069 | // log_msg (5, "drive = %s, error = %s", drive, strerror (errno));
|
---|
| 1070 | // fatal_error ("GPSOD: Unable to get size of drive");
|
---|
[59] | 1071 | log_msg(1, "%s --> %ld or %ld --> %ld", drive, outvalA, outvalB,
|
---|
| 1072 | outvalC);
|
---|
| 1073 |
|
---|
| 1074 | return (outvalC);
|
---|
[1] | 1075 | }
|
---|
[59] | 1076 |
|
---|
[1] | 1077 |
|
---|
| 1078 | /**
|
---|
| 1079 | * Determine whether @p format is supported by the kernel. Uses /proc/filesystems
|
---|
| 1080 | * under Linux and @c lsvfs under FreeBSD.
|
---|
| 1081 | * @param format The format to test.
|
---|
| 1082 | * @return TRUE if the format is supported, FALSE if not.
|
---|
| 1083 | */
|
---|
[59] | 1084 | bool is_this_a_valid_disk_format(char *format)
|
---|
[1] | 1085 | {
|
---|
[688] | 1086 | char *good_formats = NULL;
|
---|
| 1087 | char *command = NULL;
|
---|
| 1088 | char *tmp = NULL;
|
---|
| 1089 | char *format_sz = NULL;
|
---|
[1] | 1090 |
|
---|
[688] | 1091 | FILE *pin = NULL;
|
---|
[146] | 1092 | bool retval;
|
---|
[688] | 1093 | size_t n = 0;
|
---|
[59] | 1094 | assert_string_is_neither_NULL_nor_zerolength(format);
|
---|
[1] | 1095 |
|
---|
[900] | 1096 | mr_asprintf(&format_sz, "%s ", format);
|
---|
[1] | 1097 |
|
---|
| 1098 | #ifdef __FreeBSD__
|
---|
[900] | 1099 | mr_asprintf(&command,
|
---|
[59] | 1100 | "lsvfs | tr -s '\t' ' ' | grep -v Filesys | grep -v -- -- | cut -d' ' -f1 | tr -s '\n' ' '");
|
---|
[1] | 1101 | #else
|
---|
[900] | 1102 | mr_asprintf(&command,
|
---|
[274] | 1103 | "grep -v nodev /proc/filesystems | tr -s '\t' ' ' | cut -d' ' -f2 | tr -s '\n' ' '");
|
---|
[1] | 1104 | #endif
|
---|
| 1105 |
|
---|
[59] | 1106 | pin = popen(command, "r");
|
---|
[900] | 1107 | mr_free(command);
|
---|
[146] | 1108 |
|
---|
[59] | 1109 | if (!pin) {
|
---|
| 1110 | log_OS_error("Unable to read good formats");
|
---|
[146] | 1111 | retval = FALSE;
|
---|
[59] | 1112 | } else {
|
---|
[900] | 1113 | mr_getline(&good_formats, &n , pin);
|
---|
[59] | 1114 | if (pclose(pin)) {
|
---|
| 1115 | log_OS_error("Cannot pclose good formats");
|
---|
| 1116 | }
|
---|
| 1117 | strip_spaces(good_formats);
|
---|
[688] | 1118 | // " ntfs 7 " -- um, cheating much? :)
|
---|
[900] | 1119 | mr_asprintf(&tmp, " %s swap lvm raid ntfs 7 ",good_formats);
|
---|
| 1120 | mr_free(good_formats);
|
---|
[688] | 1121 | good_formats = tmp;
|
---|
| 1122 |
|
---|
[59] | 1123 | if (strstr(good_formats, format_sz)) {
|
---|
[146] | 1124 | retval = TRUE;
|
---|
[59] | 1125 | } else {
|
---|
[146] | 1126 | retval = FALSE;
|
---|
[59] | 1127 | }
|
---|
[900] | 1128 | mr_free(good_formats);
|
---|
[59] | 1129 | }
|
---|
[900] | 1130 | mr_free(format_sz);
|
---|
[59] | 1131 | return (retval);
|
---|
[1] | 1132 | }
|
---|
| 1133 |
|
---|
| 1134 |
|
---|
| 1135 | /** @def SWAPLIST_COMMAND The command to list the swap files/partitions in use. */
|
---|
| 1136 |
|
---|
| 1137 | /**
|
---|
| 1138 | * Determine whether @p device_raw is currently mounted.
|
---|
| 1139 | * @param device_raw The device to check.
|
---|
| 1140 | * @return TRUE if it's mounted, FALSE if not.
|
---|
| 1141 | */
|
---|
[59] | 1142 | bool is_this_device_mounted(char *device_raw)
|
---|
[1] | 1143 | {
|
---|
| 1144 |
|
---|
[59] | 1145 | /*@ pointers **************************************************** */
|
---|
| 1146 | FILE *fin;
|
---|
[1] | 1147 |
|
---|
[59] | 1148 | /*@ buffers ***************************************************** */
|
---|
[146] | 1149 | char *incoming = NULL;
|
---|
[59] | 1150 | char *device_with_tab;
|
---|
| 1151 | char *device_with_space;
|
---|
| 1152 | char *tmp;
|
---|
[171] | 1153 | size_t n = 0;
|
---|
[59] | 1154 |
|
---|
[1] | 1155 | #ifdef __FreeBSD__
|
---|
[59] | 1156 | #define SWAPLIST_COMMAND "swapinfo"
|
---|
[1] | 1157 | #else
|
---|
[59] | 1158 | #define SWAPLIST_COMMAND "cat /proc/swaps"
|
---|
[1] | 1159 | #endif
|
---|
| 1160 |
|
---|
[59] | 1161 | /*@ end vars **************************************************** */
|
---|
[1] | 1162 |
|
---|
[59] | 1163 | assert(device_raw != NULL);
|
---|
[1] | 1164 | // assert_string_is_neither_NULL_nor_zerolength(device_raw);
|
---|
[59] | 1165 | if (device_raw[0] != '/' && !strstr(device_raw, ":/")) {
|
---|
| 1166 | log_msg(1, "%s needs to have a '/' prefixed - I'll do it",
|
---|
| 1167 | device_raw);
|
---|
[900] | 1168 | mr_asprintf(&tmp, "/%s", device_raw);
|
---|
[59] | 1169 | } else {
|
---|
[900] | 1170 | mr_asprintf(&tmp, device_raw);
|
---|
[59] | 1171 | }
|
---|
| 1172 | log_msg(1, "Is %s mounted?", tmp);
|
---|
| 1173 | if (!strcmp(tmp, "/proc") || !strcmp(tmp, "proc")) {
|
---|
| 1174 | log_msg(1,
|
---|
| 1175 | "I don't know how the heck /proc made it into the mountlist. I'll ignore it.");
|
---|
[146] | 1176 | return (FALSE);
|
---|
[59] | 1177 | }
|
---|
[900] | 1178 | mr_asprintf(&device_with_tab, "%s\t", tmp);
|
---|
| 1179 | mr_asprintf(&device_with_space, "%s ", tmp);
|
---|
| 1180 | mr_free(tmp);
|
---|
[1] | 1181 |
|
---|
[59] | 1182 | if (!(fin = popen("mount", "r"))) {
|
---|
| 1183 | log_OS_error("Cannot popen 'mount'");
|
---|
| 1184 | return (FALSE);
|
---|
[1] | 1185 | }
|
---|
[900] | 1186 | for (mr_getline(&incoming, &n, fin); !feof(fin);
|
---|
| 1187 | mr_getline(&incoming, &n, fin)) {
|
---|
[59] | 1188 | if (strstr(incoming, device_with_space) //> incoming
|
---|
| 1189 | || strstr(incoming, device_with_tab)) // > incoming)
|
---|
| 1190 | {
|
---|
| 1191 | paranoid_pclose(fin);
|
---|
[146] | 1192 | return(TRUE);
|
---|
[59] | 1193 | }
|
---|
| 1194 | }
|
---|
[900] | 1195 | mr_free(incoming);
|
---|
| 1196 | mr_free(device_with_tab);
|
---|
[59] | 1197 | paranoid_pclose(fin);
|
---|
[146] | 1198 |
|
---|
[914] | 1199 | mr_asprintf(&tmp, "%s | grep -E \"^%s\" > /dev/null 2> /dev/null",
|
---|
[59] | 1200 | SWAPLIST_COMMAND, device_with_space);
|
---|
[900] | 1201 | mr_free(device_with_space);
|
---|
[146] | 1202 |
|
---|
[59] | 1203 | log_msg(4, "tmp (command) = '%s'", tmp);
|
---|
| 1204 | if (!system(tmp)) {
|
---|
[900] | 1205 | mr_free(tmp);
|
---|
[146] | 1206 | return(TRUE);
|
---|
[59] | 1207 | }
|
---|
[900] | 1208 | mr_free(tmp);
|
---|
[146] | 1209 | return (FALSE);
|
---|
[1] | 1210 | }
|
---|
| 1211 |
|
---|
[146] | 1212 |
|
---|
[1] | 1213 | #ifdef __FreeBSD__
|
---|
| 1214 | // CODE IS FREEBSD-SPECIFIC
|
---|
| 1215 | /**
|
---|
| 1216 | * Create a loopback device for specified @p fname.
|
---|
| 1217 | * @param fname The file to associate with a device.
|
---|
[146] | 1218 | * @return /dev entry for the device (to be freed by the caller),
|
---|
| 1219 | * or NULL if it couldn't be allocated.
|
---|
[1] | 1220 | */
|
---|
[59] | 1221 | char *make_vn(char *fname)
|
---|
[1] | 1222 | {
|
---|
[688] | 1223 | char *device = NULL;
|
---|
[146] | 1224 | char *mddevice = NULL;
|
---|
| 1225 | char *command = NULL;
|
---|
[59] | 1226 | int vndev = 2;
|
---|
[146] | 1227 |
|
---|
[59] | 1228 | if (atoi
|
---|
| 1229 | (call_program_and_get_last_line_of_output
|
---|
| 1230 | ("/sbin/sysctl -n kern.osreldate")) < 500000) {
|
---|
| 1231 | do {
|
---|
[900] | 1232 | mr_free(mddevice);
|
---|
| 1233 | mr_asprintf(&mddevice, "vn%ic", vndev++);
|
---|
[146] | 1234 |
|
---|
[900] | 1235 | mr_free(command);
|
---|
| 1236 | mr_asprintf(&command, "vnconfig %s %s", mddevice, fname);
|
---|
[146] | 1237 |
|
---|
[59] | 1238 | if (vndev > 10) {
|
---|
[900] | 1239 | mr_free(command);
|
---|
| 1240 | mr_free(mddevice);
|
---|
[59] | 1241 | return NULL;
|
---|
| 1242 | }
|
---|
| 1243 | }
|
---|
| 1244 | while (system(command));
|
---|
[900] | 1245 | mr_free(command);
|
---|
[59] | 1246 | } else {
|
---|
[900] | 1247 | mr_asprintf(&command, "mdconfig -a -t vnode -f %s", fname);
|
---|
| 1248 | mr_asprintf(&mddevice, call_program_and_get_last_line_of_output(command));
|
---|
| 1249 | mr_free(command);
|
---|
[146] | 1250 |
|
---|
[59] | 1251 | if (!strstr(mddevice, "md")) {
|
---|
[900] | 1252 | mr_free(mddevice);
|
---|
[59] | 1253 | return NULL;
|
---|
| 1254 | }
|
---|
| 1255 | }
|
---|
[900] | 1256 | mr_asprintf(&device, "/dev/%s", mddevice);
|
---|
| 1257 | mr_free(mddevice);
|
---|
[146] | 1258 | return(device);
|
---|
[1] | 1259 | }
|
---|
| 1260 |
|
---|
| 1261 |
|
---|
| 1262 | // CODE IS FREEBSD-SPECIFIC
|
---|
| 1263 | /**
|
---|
| 1264 | * Deallocate specified @p dname.
|
---|
| 1265 | * This should be called when you are done with the device created by make_vn(),
|
---|
| 1266 | * so the system does not run out of @c vn devices.
|
---|
| 1267 | * @param dname The device to deallocate.
|
---|
| 1268 | * @return 0 for success, nonzero for failure.
|
---|
| 1269 | */
|
---|
[59] | 1270 | int kick_vn(char *dname)
|
---|
[1] | 1271 | {
|
---|
[146] | 1272 | char *command;
|
---|
| 1273 | int ret = 0;
|
---|
[1] | 1274 |
|
---|
[59] | 1275 | if (strncmp(dname, "/dev/", 5) == 0) {
|
---|
| 1276 | dname += 5;
|
---|
| 1277 | }
|
---|
[1] | 1278 |
|
---|
[59] | 1279 | if (atoi
|
---|
| 1280 | (call_program_and_get_last_line_of_output
|
---|
| 1281 | ("/sbin/sysctl -n kern.osreldate")) < 500000) {
|
---|
[900] | 1282 | mr_asprintf(&command, "vnconfig -d %s", dname);
|
---|
[146] | 1283 | ret = system(command);
|
---|
[900] | 1284 | mr_free(command);
|
---|
[146] | 1285 | return(ret);
|
---|
[59] | 1286 | } else {
|
---|
[900] | 1287 | mr_asprintf(&command, "mdconfig -d -u %s", dname);
|
---|
[146] | 1288 | ret = system(command);
|
---|
[900] | 1289 | mr_free(command);
|
---|
[146] | 1290 | return(ret);
|
---|
[59] | 1291 | }
|
---|
| 1292 | /*NOTREACHED*/ return 255;
|
---|
[1] | 1293 | }
|
---|
| 1294 | #endif
|
---|
| 1295 |
|
---|
| 1296 |
|
---|
| 1297 | /**
|
---|
| 1298 | * Ask the user for CD number @p cd_number_i_want.
|
---|
| 1299 | * Sets g_current_media_number once the correct CD is inserted.
|
---|
| 1300 | * @param bkpinfo The backup information structure. Fields used:
|
---|
| 1301 | * - @c bkpinfo->backup_media_type
|
---|
[20] | 1302 | * - @c bkpinfo->prefix
|
---|
[1] | 1303 | * - @c bkpinfo->isodir
|
---|
| 1304 | * - @c bkpinfo->media_device
|
---|
| 1305 | * - @c bkpinfo->please_dont_eject_when_restoring
|
---|
| 1306 | * @param cd_number_i_want The CD number to ask for.
|
---|
| 1307 | */
|
---|
| 1308 | void
|
---|
[59] | 1309 | insist_on_this_cd_number(struct s_bkpinfo *bkpinfo, int cd_number_i_want)
|
---|
[1] | 1310 | {
|
---|
| 1311 |
|
---|
[59] | 1312 | /*@ int ************************************************************* */
|
---|
| 1313 | int res = 0;
|
---|
[1] | 1314 |
|
---|
| 1315 |
|
---|
[59] | 1316 | /*@ buffers ********************************************************* */
|
---|
| 1317 | char *tmp;
|
---|
| 1318 | char *request;
|
---|
[1] | 1319 |
|
---|
[59] | 1320 | assert(bkpinfo != NULL);
|
---|
| 1321 | assert(cd_number_i_want > 0);
|
---|
[1] | 1322 |
|
---|
| 1323 | // log_msg(3, "Insisting on CD number %d", cd_number_i_want);
|
---|
| 1324 |
|
---|
[59] | 1325 | if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
|
---|
| 1326 | log_msg(3,
|
---|
| 1327 | "No need to insist_on_this_cd_number when the backup type isn't CD-R(W) or NFS or ISO");
|
---|
| 1328 | return;
|
---|
| 1329 | }
|
---|
[146] | 1330 |
|
---|
[900] | 1331 | mr_asprintf(&tmp, "mkdir -p " MNT_CDROM);
|
---|
[59] | 1332 | run_program_and_log_output(tmp, 5);
|
---|
[900] | 1333 | mr_free(tmp);
|
---|
[146] | 1334 |
|
---|
[59] | 1335 | if (g_ISO_restore_mode || bkpinfo->backup_media_type == iso
|
---|
| 1336 | || bkpinfo->backup_media_type == nfs) {
|
---|
| 1337 | log_msg(3, "Remounting CD");
|
---|
| 1338 | g_ISO_restore_mode = TRUE;
|
---|
[1] | 1339 | // FIXME --- I'm tempted to do something about this...
|
---|
| 1340 | // Why unmount and remount again and again?
|
---|
[59] | 1341 | if (is_this_device_mounted(MNT_CDROM)) {
|
---|
| 1342 | run_program_and_log_output("umount " MNT_CDROM, 5);
|
---|
| 1343 | }
|
---|
| 1344 | system("mkdir -p /tmp/isodir &> /dev/null");
|
---|
[900] | 1345 | mr_asprintf(&tmp, "%s/%s/%s-%d.iso", bkpinfo->isodir,
|
---|
[59] | 1346 | bkpinfo->nfs_remote_dir, bkpinfo->prefix,
|
---|
| 1347 | cd_number_i_want);
|
---|
| 1348 | if (!does_file_exist(tmp)) {
|
---|
[900] | 1349 | mr_free(tmp);
|
---|
| 1350 | mr_asprintf(&tmp, "/tmp/isodir/%s/%s-%d.iso",
|
---|
[59] | 1351 | bkpinfo->nfs_remote_dir, bkpinfo->prefix,
|
---|
| 1352 | cd_number_i_want);
|
---|
| 1353 | if (does_file_exist(tmp)) {
|
---|
| 1354 | log_msg(1,
|
---|
| 1355 | "FIXME - hacking bkpinfo->isodir from '%s' to /tmp/isodir",
|
---|
| 1356 | bkpinfo->isodir);
|
---|
[900] | 1357 | mr_allocstr(bkpinfo->isodir, "/tmp/isodir");
|
---|
[59] | 1358 | }
|
---|
| 1359 | }
|
---|
| 1360 | log_msg(3, "Mounting %s at %s", tmp, MNT_CDROM);
|
---|
[171] | 1361 | if (! mount_CDROM_here(tmp, MNT_CDROM)) {
|
---|
[59] | 1362 | fatal_error("Mommy!");
|
---|
| 1363 | }
|
---|
[900] | 1364 | mr_free(tmp);
|
---|
[1] | 1365 | }
|
---|
[59] | 1366 | if ((res = what_number_cd_is_this(bkpinfo)) != cd_number_i_want) {
|
---|
| 1367 | log_msg(3, "Currently, we hold %d but we want %d", res,
|
---|
| 1368 | cd_number_i_want);
|
---|
[900] | 1369 | mr_asprintf(&tmp, "Insisting on %s #%d",
|
---|
[783] | 1370 | bkpinfo->backup_media_string,
|
---|
[59] | 1371 | cd_number_i_want);
|
---|
[900] | 1372 | mr_asprintf(&request, "Please insert %s #%d and press Enter.",
|
---|
[783] | 1373 | bkpinfo->backup_media_string,
|
---|
[59] | 1374 | cd_number_i_want);
|
---|
| 1375 | log_msg(3, tmp);
|
---|
[900] | 1376 | mr_free(tmp);
|
---|
[146] | 1377 |
|
---|
[59] | 1378 | while (what_number_cd_is_this(bkpinfo) != cd_number_i_want) {
|
---|
[688] | 1379 | sync();
|
---|
[59] | 1380 | if (is_this_device_mounted(MNT_CDROM)) {
|
---|
| 1381 | res =
|
---|
| 1382 | run_program_and_log_output("umount " MNT_CDROM, FALSE);
|
---|
| 1383 | } else {
|
---|
| 1384 | res = 0;
|
---|
| 1385 | }
|
---|
| 1386 | if (res) {
|
---|
[507] | 1387 | log_to_screen(_("WARNING - failed to unmount CD-ROM drive"));
|
---|
[59] | 1388 | }
|
---|
| 1389 | if (!bkpinfo->please_dont_eject) {
|
---|
| 1390 | res = eject_device(bkpinfo->media_device);
|
---|
| 1391 | } else {
|
---|
| 1392 | res = 0;
|
---|
| 1393 | }
|
---|
| 1394 | if (res) {
|
---|
[507] | 1395 | log_to_screen(_("WARNING - failed to eject CD-ROM disk"));
|
---|
[59] | 1396 | }
|
---|
| 1397 | popup_and_OK(request);
|
---|
| 1398 | if (!bkpinfo->please_dont_eject) {
|
---|
| 1399 | inject_device(bkpinfo->media_device);
|
---|
| 1400 | }
|
---|
[688] | 1401 | sync();
|
---|
[59] | 1402 | }
|
---|
[900] | 1403 | mr_free(request);
|
---|
[146] | 1404 |
|
---|
[59] | 1405 | log_msg(1, "Thankyou. Proceeding...");
|
---|
| 1406 | g_current_media_number = cd_number_i_want;
|
---|
| 1407 | }
|
---|
[1] | 1408 | }
|
---|
| 1409 | /* @} - end of deviceGroup */
|
---|
| 1410 |
|
---|
| 1411 |
|
---|
| 1412 | /**
|
---|
| 1413 | * Ask user for details of backup/restore information.
|
---|
| 1414 | * Called when @c mondoarchive doesn't get any parameters.
|
---|
| 1415 | * @param bkpinfo The backup information structure to fill out with the user's data.
|
---|
| 1416 | * @param archiving_to_media TRUE if archiving, FALSE if restoring.
|
---|
| 1417 | * @return 0, always.
|
---|
| 1418 | * @bug No point of `int' return value.
|
---|
| 1419 | * @ingroup archiveGroup
|
---|
| 1420 | */
|
---|
[59] | 1421 | int interactively_obtain_media_parameters_from_user(struct s_bkpinfo
|
---|
| 1422 | *bkpinfo,
|
---|
| 1423 | bool
|
---|
| 1424 | archiving_to_media)
|
---|
[1] | 1425 | // archiving_to_media is TRUE if I'm being called by mondoarchive
|
---|
| 1426 | // archiving_to_media is FALSE if I'm being called by mondorestore
|
---|
| 1427 | {
|
---|
[688] | 1428 | char *tmp = NULL;
|
---|
| 1429 | char *tmp1 = NULL;
|
---|
| 1430 | char *tmp2 = NULL;
|
---|
[146] | 1431 | char *sz_size = NULL;
|
---|
[688] | 1432 | char *command = NULL;
|
---|
| 1433 | char *comment = NULL;
|
---|
[146] | 1434 | char *prompt = NULL;
|
---|
[688] | 1435 | int i = 0;
|
---|
| 1436 | FILE *fin = NULL;
|
---|
[1] | 1437 |
|
---|
[59] | 1438 | assert(bkpinfo != NULL);
|
---|
| 1439 | bkpinfo->nonbootable_backup = FALSE;
|
---|
[1] | 1440 |
|
---|
| 1441 | // Tape, CD, NFS, ...?
|
---|
[59] | 1442 | srandom(getpid());
|
---|
| 1443 | bkpinfo->backup_media_type =
|
---|
| 1444 | (g_restoring_live_from_cd) ? cdr :
|
---|
| 1445 | which_backup_media_type(bkpinfo->restore_data);
|
---|
| 1446 | if (bkpinfo->backup_media_type == none) {
|
---|
[507] | 1447 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[59] | 1448 | finish(1);
|
---|
| 1449 | }
|
---|
| 1450 | if (bkpinfo->backup_media_type == tape && bkpinfo->restore_data) {
|
---|
[507] | 1451 | popup_and_OK(_("Please remove CD/floppy from drive(s)"));
|
---|
[59] | 1452 | }
|
---|
[783] | 1453 | bkpinfo->backup_media_string = bkptype_to_string(bkpinfo->backup_media_type);
|
---|
| 1454 | log_msg(3, "media type = %s",bkpinfo->backup_media_string);
|
---|
| 1455 |
|
---|
[59] | 1456 | if (archiving_to_media) {
|
---|
| 1457 | sensibly_set_tmpdir_and_scratchdir(bkpinfo);
|
---|
| 1458 | }
|
---|
| 1459 | bkpinfo->cdrw_speed = (bkpinfo->backup_media_type == cdstream) ? 2 : 4;
|
---|
| 1460 | bkpinfo->compression_level =
|
---|
| 1461 | (bkpinfo->backup_media_type == cdstream) ? 1 : 5;
|
---|
| 1462 | bkpinfo->use_lzo =
|
---|
| 1463 | (bkpinfo->backup_media_type == cdstream) ? TRUE : FALSE;
|
---|
| 1464 | mvaddstr_and_log_it(2, 0, " ");
|
---|
[1] | 1465 |
|
---|
| 1466 | // Find device's /dev (or SCSI) entry
|
---|
[59] | 1467 | switch (bkpinfo->backup_media_type) {
|
---|
| 1468 | case cdr:
|
---|
| 1469 | case cdrw:
|
---|
| 1470 | case dvd:
|
---|
| 1471 | if (archiving_to_media) {
|
---|
[687] | 1472 | if (bkpinfo->backup_media_type != dvd) {
|
---|
| 1473 | if (ask_me_yes_or_no
|
---|
| 1474 | (_("Is your computer a laptop, or does the CD writer incorporate BurnProof technology?")))
|
---|
| 1475 | {
|
---|
| 1476 | bkpinfo->manual_cd_tray = TRUE;
|
---|
| 1477 | }
|
---|
[59] | 1478 | }
|
---|
| 1479 | if ((bkpinfo->compression_level =
|
---|
| 1480 | which_compression_level()) == -1) {
|
---|
[507] | 1481 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[59] | 1482 | finish(1);
|
---|
| 1483 | }
|
---|
[900] | 1484 | mr_asprintf(&comment, _("What speed is your %s (re)writer?"),
|
---|
[783] | 1485 | bkpinfo->backup_media_string);
|
---|
[59] | 1486 | if (bkpinfo->backup_media_type == dvd) {
|
---|
[900] | 1487 | mr_free(bkpinfo->media_device);
|
---|
[171] | 1488 | bkpinfo->media_device = find_dvd_device();
|
---|
[900] | 1489 | mr_asprintf(&tmp, "1");
|
---|
| 1490 | mr_asprintf(&sz_size, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4582 MB
|
---|
[59] | 1491 | log_msg(1, "Setting to DVD defaults");
|
---|
| 1492 | } else {
|
---|
[900] | 1493 | mr_allocstr(bkpinfo->media_device,VANILLA_SCSI_CDROM );
|
---|
| 1494 | mr_asprintf(&tmp, "4");
|
---|
| 1495 | mr_asprintf(&sz_size, "650");
|
---|
[59] | 1496 | log_msg(1, "Setting to CD defaults");
|
---|
| 1497 | }
|
---|
| 1498 | if (bkpinfo->backup_media_type != dvd) {
|
---|
[688] | 1499 | if (!popup_and_get_string(_("Speed"), comment, tmp)) {
|
---|
[507] | 1500 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[59] | 1501 | finish(1);
|
---|
| 1502 | }
|
---|
| 1503 | }
|
---|
[900] | 1504 | mr_free(comment);
|
---|
[146] | 1505 |
|
---|
[59] | 1506 | bkpinfo->cdrw_speed = atoi(tmp); // if DVD then this shouldn't ever be used anyway :)
|
---|
[900] | 1507 | mr_free(tmp);
|
---|
[146] | 1508 |
|
---|
[900] | 1509 | mr_asprintf(&comment,
|
---|
[507] | 1510 | _("How much data (in Megabytes) will each %s store?"),
|
---|
[783] | 1511 | bkpinfo->backup_media_string);
|
---|
[146] | 1512 |
|
---|
[688] | 1513 | if (!popup_and_get_string("Size", comment, sz_size)) {
|
---|
[507] | 1514 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[59] | 1515 | finish(1);
|
---|
| 1516 | }
|
---|
[900] | 1517 | mr_free(comment);
|
---|
[146] | 1518 |
|
---|
[59] | 1519 | for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
|
---|
| 1520 | bkpinfo->media_size[i] = atoi(sz_size);
|
---|
| 1521 | }
|
---|
[900] | 1522 | mr_free(sz_size);
|
---|
[688] | 1523 |
|
---|
[59] | 1524 | if (bkpinfo->media_size[0] <= 0) {
|
---|
[507] | 1525 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[59] | 1526 | finish(1);
|
---|
| 1527 | }
|
---|
| 1528 | }
|
---|
| 1529 | case cdstream:
|
---|
| 1530 | if (bkpinfo->disaster_recovery) {
|
---|
[900] | 1531 | mr_allocstr(bkpinfo->media_device, "/dev/cdrom");
|
---|
[59] | 1532 | log_msg(2, "CD-ROM device assumed to be at %s",
|
---|
| 1533 | bkpinfo->media_device);
|
---|
| 1534 | } else if (bkpinfo->restore_data
|
---|
| 1535 | || bkpinfo->backup_media_type == dvd) {
|
---|
[171] | 1536 | if (bkpinfo->media_device == NULL) {
|
---|
[900] | 1537 | mr_asprintf(&bkpinfo->media_device, "/dev/cdrom");
|
---|
[59] | 1538 | } // just for the heck of it :)
|
---|
| 1539 | log_msg(1, "bkpinfo->media_device = %s",
|
---|
| 1540 | bkpinfo->media_device);
|
---|
[783] | 1541 | tmp = find_cdrom_device(FALSE);
|
---|
[171] | 1542 | if ((bkpinfo->backup_media_type == dvd)
|
---|
[783] | 1543 | || (tmp != NULL)) {
|
---|
[900] | 1544 | mr_allocstr(bkpinfo->media_device, tmp);
|
---|
[59] | 1545 | log_msg(1, "bkpinfo->media_device = %s",
|
---|
| 1546 | bkpinfo->media_device);
|
---|
[900] | 1547 | mr_asprintf(&comment,
|
---|
[507] | 1548 | _("Please specify your %s drive's /dev entry"),
|
---|
[783] | 1549 | bkpinfo->backup_media_string);
|
---|
[59] | 1550 | if (!popup_and_get_string
|
---|
[688] | 1551 | (_("Device?"), comment, bkpinfo->media_device)) {
|
---|
[507] | 1552 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[59] | 1553 | finish(1);
|
---|
| 1554 | }
|
---|
[900] | 1555 | mr_free(comment);
|
---|
[59] | 1556 | }
|
---|
[900] | 1557 | mr_free(tmp);
|
---|
[783] | 1558 |
|
---|
[59] | 1559 | log_msg(2, "%s device found at %s",
|
---|
[783] | 1560 | bkpinfo->backup_media_string,
|
---|
[59] | 1561 | bkpinfo->media_device);
|
---|
| 1562 | } else {
|
---|
[171] | 1563 | bkpinfo->media_device = find_cdrw_device();
|
---|
| 1564 | if (bkpinfo->media_device != NULL) {
|
---|
[900] | 1565 | mr_asprintf(&tmp,
|
---|
[903] | 1566 | _("I think I've found your %s burner at SCSI node %s; Is this correct ? (say no if you have an IDE burner and you are running a 2.6 kernel. You will then be prompted for further details."),
|
---|
[783] | 1567 | bkpinfo->backup_media_string,
|
---|
[59] | 1568 | bkpinfo->media_device);
|
---|
| 1569 | if (!ask_me_yes_or_no(tmp)) {
|
---|
[900] | 1570 | mr_free(bkpinfo->media_device);
|
---|
[59] | 1571 | }
|
---|
[900] | 1572 | mr_free(tmp);
|
---|
[171] | 1573 | } else {
|
---|
[59] | 1574 | if (g_kernel_version < 2.6) {
|
---|
[507] | 1575 | i = popup_and_get_string(_("Device node?"),
|
---|
| 1576 | _("What is the SCSI node of your CD (re)writer, please?"),
|
---|
[688] | 1577 | bkpinfo->media_device);
|
---|
[59] | 1578 | } else {
|
---|
[507] | 1579 | i = popup_and_get_string(_("/dev entry?"),
|
---|
| 1580 | _("What is the /dev entry of your CD (re)writer, please?"),
|
---|
[688] | 1581 | bkpinfo->media_device);
|
---|
[59] | 1582 | }
|
---|
| 1583 | if (!i) {
|
---|
[507] | 1584 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[59] | 1585 | finish(1);
|
---|
| 1586 | }
|
---|
| 1587 | }
|
---|
| 1588 | }
|
---|
| 1589 | if (bkpinfo->backup_media_type == cdstream) {
|
---|
| 1590 | for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
|
---|
| 1591 | bkpinfo->media_size[i] = 650;
|
---|
| 1592 | }
|
---|
| 1593 | }
|
---|
| 1594 | break;
|
---|
| 1595 | case udev:
|
---|
| 1596 | if (!ask_me_yes_or_no
|
---|
[507] | 1597 | (_("This option is for advanced users only. Are you sure?"))) {
|
---|
| 1598 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[59] | 1599 | finish(1);
|
---|
| 1600 | }
|
---|
| 1601 | case tape:
|
---|
[1] | 1602 |
|
---|
[900] | 1603 | mr_free(bkpinfo->media_device);
|
---|
[59] | 1604 | if (find_tape_device_and_size(bkpinfo->media_device, sz_size)) {
|
---|
[507] | 1605 | log_msg(3, _("Ok, using vanilla scsi tape."));
|
---|
[900] | 1606 | mr_allocstr(bkpinfo->media_device,VANILLA_SCSI_TAPE"0" );
|
---|
[59] | 1607 | if ((fin = fopen(bkpinfo->media_device, "r"))) {
|
---|
| 1608 | paranoid_fclose(fin);
|
---|
| 1609 | } else {
|
---|
[900] | 1610 | mr_allocstr(bkpinfo->media_device,"/dev/osst0");
|
---|
[59] | 1611 | }
|
---|
| 1612 | }
|
---|
[171] | 1613 | if (bkpinfo->media_device != NULL) {
|
---|
[59] | 1614 | if ((fin = fopen(bkpinfo->media_device, "r"))) {
|
---|
| 1615 | paranoid_fclose(fin);
|
---|
| 1616 | } else {
|
---|
| 1617 | if (does_file_exist("/tmp/mondo-restore.cfg")) {
|
---|
[900] | 1618 | mr_free(bkpinfo->media_device);
|
---|
[59] | 1619 | read_cfg_var("/tmp/mondo-restore.cfg", "media-dev",
|
---|
| 1620 | bkpinfo->media_device);
|
---|
| 1621 | }
|
---|
| 1622 | }
|
---|
[900] | 1623 | mr_asprintf(&tmp,
|
---|
[507] | 1624 | _("I think I've found your tape streamer at %s; am I right on the money?"),
|
---|
[59] | 1625 | bkpinfo->media_device);
|
---|
| 1626 | if (!ask_me_yes_or_no(tmp)) {
|
---|
[900] | 1627 | mr_free(bkpinfo->media_device);
|
---|
[59] | 1628 | }
|
---|
[900] | 1629 | mr_free(tmp);
|
---|
[783] | 1630 | } else {
|
---|
[59] | 1631 | if (!popup_and_get_string
|
---|
[507] | 1632 | (_("Device name?"),
|
---|
| 1633 | _("What is the /dev entry of your tape streamer?"),
|
---|
[688] | 1634 | bkpinfo->media_device)) {
|
---|
[507] | 1635 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[59] | 1636 | finish(1);
|
---|
| 1637 | }
|
---|
| 1638 | }
|
---|
[900] | 1639 | mr_asprintf(&tmp, "ls -l %s", bkpinfo->media_device);
|
---|
[59] | 1640 | if (run_program_and_log_output(tmp, FALSE)) {
|
---|
[507] | 1641 | log_to_screen(_("User has not specified a valid /dev entry"));
|
---|
[59] | 1642 | finish(1);
|
---|
| 1643 | }
|
---|
[900] | 1644 | mr_free(tmp);
|
---|
[59] | 1645 | log_msg(4, "sz_size = %s", sz_size);
|
---|
[900] | 1646 | mr_free(sz_size);
|
---|
[146] | 1647 | bkpinfo->media_size[0] = 0;
|
---|
[59] | 1648 | log_msg(4, "media_size[0] = %ld", bkpinfo->media_size[0]);
|
---|
| 1649 | if (bkpinfo->media_size[0] <= 0) {
|
---|
| 1650 | bkpinfo->media_size[0] = 0;
|
---|
| 1651 | }
|
---|
| 1652 | for (i = 1; i <= MAX_NOOF_MEDIA; i++) {
|
---|
| 1653 | bkpinfo->media_size[i] = bkpinfo->media_size[0];
|
---|
| 1654 | }
|
---|
| 1655 | if (archiving_to_media) {
|
---|
| 1656 | if ((bkpinfo->compression_level =
|
---|
| 1657 | which_compression_level()) == -1) {
|
---|
[507] | 1658 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[59] | 1659 | finish(1);
|
---|
| 1660 | }
|
---|
| 1661 | }
|
---|
| 1662 | break;
|
---|
[1] | 1663 |
|
---|
| 1664 |
|
---|
| 1665 |
|
---|
[59] | 1666 | case nfs:
|
---|
[783] | 1667 | if (bkpinfo->nfs_mount == NULL) {
|
---|
| 1668 | bkpinfo->nfs_mount = call_program_and_get_last_line_of_output
|
---|
| 1669 | ("mount | grep \":\" | cut -d' ' -f1 | head -n1");
|
---|
[59] | 1670 | }
|
---|
[1] | 1671 | #ifdef __FreeBSD__
|
---|
[59] | 1672 | if (TRUE)
|
---|
[1] | 1673 | #else
|
---|
[59] | 1674 | if (!bkpinfo->disaster_recovery)
|
---|
[1] | 1675 | #endif
|
---|
[59] | 1676 | {
|
---|
| 1677 | if (!popup_and_get_string
|
---|
[507] | 1678 | (_("NFS dir."),
|
---|
| 1679 | _("Please enter path and directory where archives are stored remotely. (Mondo has taken a guess at the correct value. If it is incorrect, delete it and type the correct one.)"),
|
---|
[688] | 1680 | bkpinfo->nfs_mount)) {
|
---|
[507] | 1681 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[59] | 1682 | finish(1);
|
---|
| 1683 | }
|
---|
| 1684 | if (!bkpinfo->restore_data) {
|
---|
| 1685 | if ((bkpinfo->compression_level =
|
---|
| 1686 | which_compression_level()) == -1) {
|
---|
[507] | 1687 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[59] | 1688 | finish(1);
|
---|
| 1689 | }
|
---|
| 1690 | }
|
---|
[75] | 1691 | // check whether already mounted - we better remove
|
---|
[89] | 1692 | // surrounding spaces and trailing '/' for this
|
---|
[783] | 1693 | /* BERLIOS: Useless
|
---|
[89] | 1694 | strip_spaces(bkpinfo->nfs_mount);
|
---|
[783] | 1695 | */
|
---|
[75] | 1696 | if (bkpinfo->nfs_mount[strlen(bkpinfo->nfs_mount) - 1] == '/')
|
---|
[89] | 1697 | bkpinfo->nfs_mount[strlen(bkpinfo->nfs_mount) - 1] = '\0';
|
---|
[900] | 1698 | mr_asprintf(&command, "mount | grep \"%s \" | cut -d' ' -f3",
|
---|
[89] | 1699 | bkpinfo->nfs_mount);
|
---|
[900] | 1700 | mr_free(bkpinfo->isodir);
|
---|
[783] | 1701 | bkpinfo->isodir = call_program_and_get_last_line_of_output(command);
|
---|
[900] | 1702 | mr_free(command);
|
---|
[262] | 1703 |
|
---|
[900] | 1704 | mr_asprintf(&comment,
|
---|
[507] | 1705 | _("How much data (in Megabytes) will each media store?"));
|
---|
[688] | 1706 | if (!popup_and_get_string(_("Size"), comment, sz_size)) {
|
---|
[507] | 1707 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[262] | 1708 | finish(1);
|
---|
| 1709 | }
|
---|
| 1710 | for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
|
---|
| 1711 | bkpinfo->media_size[i] = atoi(sz_size);
|
---|
| 1712 | }
|
---|
| 1713 | if (bkpinfo->media_size[0] <= 0) {
|
---|
[507] | 1714 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[262] | 1715 | finish(1);
|
---|
| 1716 | }
|
---|
[900] | 1717 | mr_free(comment);
|
---|
[59] | 1718 | }
|
---|
| 1719 | if (bkpinfo->disaster_recovery) {
|
---|
| 1720 | system("umount /tmp/isodir 2> /dev/null");
|
---|
| 1721 | if (!popup_and_get_string
|
---|
[507] | 1722 | (_("NFS share"), _("Which remote NFS share should I mount?"),
|
---|
[688] | 1723 | bkpinfo->nfs_mount)) {
|
---|
[507] | 1724 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[59] | 1725 | finish(1);
|
---|
| 1726 | }
|
---|
| 1727 | }
|
---|
| 1728 | if (!is_this_device_mounted(bkpinfo->nfs_mount)) {
|
---|
[900] | 1729 | mr_free(bkpinfo->isodir);
|
---|
| 1730 | mr_asprintf(&bkpinfo->isodir, "/tmp/isodir.mondo.%d",
|
---|
[89] | 1731 | (int) (random() % 32768));
|
---|
[900] | 1732 | mr_asprintf(&command, "mkdir -p %s", bkpinfo->isodir);
|
---|
[89] | 1733 | run_program_and_log_output(command, 5);
|
---|
[900] | 1734 | mr_free(command);
|
---|
[146] | 1735 |
|
---|
[900] | 1736 | mr_asprintf(&tmp, "mount -t nfs -o nolock %s %s", bkpinfo->nfs_mount,
|
---|
[89] | 1737 | bkpinfo->isodir);
|
---|
| 1738 | run_program_and_log_output(tmp, 5);
|
---|
[900] | 1739 | mr_free(tmp);
|
---|
| 1740 | mr_asprintf(&g_selfmounted_isodir, bkpinfo->isodir);
|
---|
[59] | 1741 | }
|
---|
| 1742 | if (!is_this_device_mounted(bkpinfo->nfs_mount)) {
|
---|
| 1743 | popup_and_OK
|
---|
[507] | 1744 | (_("Please mount that partition before you try to backup to or restore from it."));
|
---|
[59] | 1745 | finish(1);
|
---|
| 1746 | }
|
---|
| 1747 | if (!popup_and_get_string
|
---|
[688] | 1748 | (_("Directory"), _("Which directory within that mountpoint?"),bkpinfo->nfs_remote_dir)) {
|
---|
[507] | 1749 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[59] | 1750 | finish(1);
|
---|
| 1751 | }
|
---|
[146] | 1752 |
|
---|
[900] | 1753 | mr_asprintf(&command, "echo hi > '%s/%s/.dummy.txt'", bkpinfo->isodir,
|
---|
[89] | 1754 | bkpinfo->nfs_remote_dir);
|
---|
| 1755 | while (run_program_and_log_output(command, FALSE)) {
|
---|
[900] | 1756 | mr_asprintf(&prompt,
|
---|
[507] | 1757 | _("Directory '%s' under mountpoint '%s' does not exist or is not writable. You can fix this or change the directory and retry or cancel the backup."),
|
---|
[89] | 1758 | bkpinfo->nfs_remote_dir, bkpinfo->isodir);
|
---|
| 1759 | if (!popup_and_get_string
|
---|
[688] | 1760 | (_("Directory"), prompt, bkpinfo->nfs_remote_dir)) {
|
---|
[507] | 1761 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[89] | 1762 | finish(1);
|
---|
| 1763 | }
|
---|
[900] | 1764 | mr_free(prompt);
|
---|
[688] | 1765 |
|
---|
[900] | 1766 | mr_free(command);
|
---|
| 1767 | mr_asprintf(&command, "echo hi > %s/%s/.dummy.txt",
|
---|
[89] | 1768 | bkpinfo->isodir, bkpinfo->nfs_remote_dir);
|
---|
[75] | 1769 | }
|
---|
[900] | 1770 | mr_free(command);
|
---|
[146] | 1771 |
|
---|
[252] | 1772 | if (!popup_and_get_string
|
---|
[507] | 1773 | (_("Prefix."),
|
---|
| 1774 | _("Please enter the prefix that will be prepended to your ISO filename. Example: machine1 to obtain machine1-[1-9]*.iso files"),
|
---|
[688] | 1775 | bkpinfo->prefix)) {
|
---|
[507] | 1776 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[252] | 1777 | finish(1);
|
---|
| 1778 | }
|
---|
| 1779 | log_msg(3, "prefix set to %s", bkpinfo->prefix);
|
---|
| 1780 |
|
---|
[59] | 1781 | for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
|
---|
| 1782 | bkpinfo->media_size[i] = 650;
|
---|
| 1783 | }
|
---|
| 1784 | log_msg(3, "Just set nfs_remote_dir to %s",
|
---|
| 1785 | bkpinfo->nfs_remote_dir);
|
---|
| 1786 | log_msg(3, "isodir is still %s", bkpinfo->isodir);
|
---|
| 1787 | break;
|
---|
[1] | 1788 |
|
---|
[59] | 1789 | case iso:
|
---|
| 1790 | if (!bkpinfo->disaster_recovery) {
|
---|
| 1791 | if (!popup_and_get_string
|
---|
[507] | 1792 | (_("Storage dir."),
|
---|
| 1793 | _("Please enter the full path that contains your ISO images. Example: /mnt/raid0_0"),
|
---|
[688] | 1794 | bkpinfo->isodir)) {
|
---|
[507] | 1795 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[59] | 1796 | finish(1);
|
---|
| 1797 | }
|
---|
| 1798 | if (archiving_to_media) {
|
---|
| 1799 | if ((bkpinfo->compression_level =
|
---|
| 1800 | which_compression_level()) == -1) {
|
---|
[507] | 1801 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[59] | 1802 | finish(1);
|
---|
| 1803 | }
|
---|
| 1804 | if (!popup_and_get_string
|
---|
[507] | 1805 | (_("ISO size."),
|
---|
[688] | 1806 | _("Please enter how big you want each ISO image to be (in megabytes). This should be less than or equal to the size of the CD-R[W]'s or DVD's you plan to backup to."), sz_size)) {
|
---|
[507] | 1807 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[59] | 1808 | finish(1);
|
---|
| 1809 | }
|
---|
| 1810 | for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
|
---|
| 1811 | bkpinfo->media_size[i] = atoi(sz_size);
|
---|
| 1812 | }
|
---|
| 1813 | } else {
|
---|
| 1814 | for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
|
---|
| 1815 | bkpinfo->media_size[i] = 650;
|
---|
| 1816 | }
|
---|
| 1817 | }
|
---|
| 1818 | }
|
---|
[808] | 1819 | if (!popup_and_get_string
|
---|
[835] | 1820 | (_("Prefix."),
|
---|
| 1821 | _("Please enter the prefix that will be prepended to your ISO filename. Example: machine1 to obtain machine1-[1-9]*.iso files"),
|
---|
| 1822 | bkpinfo->prefix)) {
|
---|
[808] | 1823 | log_to_screen("User has chosen not to backup the PC");
|
---|
| 1824 | finish(1);
|
---|
| 1825 | }
|
---|
| 1826 | log_msg(3, "prefix set to %s", bkpinfo->prefix);
|
---|
[59] | 1827 | break;
|
---|
| 1828 | default:
|
---|
| 1829 | fatal_error
|
---|
| 1830 | ("I, Mojo Jojo, shall defeat those pesky Powerpuff Girls!");
|
---|
| 1831 | }
|
---|
| 1832 | if (archiving_to_media) {
|
---|
[1] | 1833 |
|
---|
[783] | 1834 | if (bkpinfo->boot_device) {
|
---|
[900] | 1835 | mr_free(bkpinfo->boot_device);
|
---|
[783] | 1836 | }
|
---|
[1] | 1837 | #ifdef __FreeBSD__
|
---|
[688] | 1838 | bkpinfo->boot_device = call_program_and_get_last_line_of_output
|
---|
[783] | 1839 | ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/\\([0-9]\\).*/\\1/'");
|
---|
[1] | 1840 | #else
|
---|
[688] | 1841 | bkpinfo->boot_device = call_program_and_get_last_line_of_output
|
---|
[783] | 1842 | ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'");
|
---|
[1] | 1843 | #endif
|
---|
[59] | 1844 | i = which_boot_loader(bkpinfo->boot_device);
|
---|
| 1845 | if (i == 'U') // unknown
|
---|
| 1846 | {
|
---|
[1] | 1847 |
|
---|
| 1848 | #ifdef __FreeBSD__
|
---|
[59] | 1849 | if (!popup_and_get_string
|
---|
[507] | 1850 | (_("Boot device"),
|
---|
| 1851 | _("What is your boot device? (e.g. /dev/ad0)"),
|
---|
[688] | 1852 | bkpinfo->boot_device)) {
|
---|
[507] | 1853 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[59] | 1854 | finish(1);
|
---|
| 1855 | }
|
---|
| 1856 | i = which_boot_loader(bkpinfo->boot_device);
|
---|
[1] | 1857 | #else
|
---|
[59] | 1858 | if (!popup_and_get_string
|
---|
[507] | 1859 | (_("Boot device"),
|
---|
| 1860 | _("What is your boot device? (e.g. /dev/hda)"),
|
---|
[688] | 1861 | bkpinfo->boot_device)) {
|
---|
[507] | 1862 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[59] | 1863 | finish(1);
|
---|
| 1864 | }
|
---|
| 1865 | if (does_string_exist_in_boot_block
|
---|
| 1866 | (bkpinfo->boot_device, "LILO")) {
|
---|
| 1867 | i = 'L';
|
---|
| 1868 | } else
|
---|
| 1869 | if (does_string_exist_in_boot_block
|
---|
| 1870 | (bkpinfo->boot_device, "ELILO")) {
|
---|
| 1871 | i = 'E';
|
---|
| 1872 | } else
|
---|
| 1873 | if (does_string_exist_in_boot_block
|
---|
| 1874 | (bkpinfo->boot_device, "GRUB")) {
|
---|
| 1875 | i = 'G';
|
---|
| 1876 | } else {
|
---|
| 1877 | i = 'U';
|
---|
| 1878 | }
|
---|
[1] | 1879 | #endif
|
---|
[59] | 1880 | if (i == 'U') {
|
---|
| 1881 | if (ask_me_yes_or_no
|
---|
[507] | 1882 | (_("Unidentified boot loader. Shall I restore it byte-for-byte at restore time and hope for the best?")))
|
---|
[59] | 1883 | {
|
---|
| 1884 | i = 'R'; // raw
|
---|
| 1885 | } else {
|
---|
| 1886 | log_to_screen
|
---|
[507] | 1887 | (_("I cannot find your boot loader. Please run mondoarchive with parameters."));
|
---|
[59] | 1888 | finish(1);
|
---|
| 1889 | }
|
---|
| 1890 | }
|
---|
[1] | 1891 | }
|
---|
[59] | 1892 | bkpinfo->boot_loader = i;
|
---|
[900] | 1893 | mr_asprintf(&bkpinfo->include_paths, "/");
|
---|
[59] | 1894 | if (!popup_and_get_string
|
---|
[507] | 1895 | (_("Backup paths"),
|
---|
| 1896 | _("Please enter paths which you want me to backup. The default is '/' (i.e. everything)."),
|
---|
[688] | 1897 | bkpinfo->include_paths)) {
|
---|
[507] | 1898 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[59] | 1899 | finish(1);
|
---|
[1] | 1900 | }
|
---|
[688] | 1901 |
|
---|
[146] | 1902 | tmp = list_of_NFS_mounts_only();
|
---|
[59] | 1903 | if (strlen(tmp) > 2) {
|
---|
[688] | 1904 | if (bkpinfo->exclude_paths != NULL) {
|
---|
| 1905 | tmp1 = bkpinfo->exclude_paths;
|
---|
[900] | 1906 | mr_asprintf(&bkpinfo->exclude_paths, "%s %s", tmp1, tmp);
|
---|
| 1907 | mr_free(tmp1);
|
---|
[688] | 1908 | } else {
|
---|
| 1909 | bkpinfo->exclude_paths = tmp;
|
---|
[59] | 1910 | }
|
---|
| 1911 | }
|
---|
[900] | 1912 | mr_free(tmp);
|
---|
[1] | 1913 | // NTFS
|
---|
[688] | 1914 | tmp = call_program_and_get_last_line_of_output
|
---|
| 1915 | ("parted2fdisk -l | grep -i ntfs | awk '{ print $1};' | tr -s '\\n' ' ' | awk '{ print $0};'");
|
---|
[59] | 1916 | if (strlen(tmp) > 2) {
|
---|
| 1917 | if (!popup_and_get_string
|
---|
[507] | 1918 | (_("NTFS partitions"),
|
---|
| 1919 | _("Please enter/confirm the NTFS partitions you wish to backup as well."),
|
---|
[688] | 1920 | tmp1)) {
|
---|
[507] | 1921 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[59] | 1922 | finish(1);
|
---|
| 1923 | }
|
---|
[688] | 1924 | if (bkpinfo->image_devs != NULL) {
|
---|
| 1925 | tmp2 = bkpinfo->image_devs;
|
---|
[900] | 1926 | mr_asprintf(&bkpinfo->image_devs, "%s %s", tmp2, tmp1);
|
---|
| 1927 | mr_free(tmp2);
|
---|
[688] | 1928 | } else {
|
---|
| 1929 | bkpinfo->image_devs = tmp1;
|
---|
| 1930 | }
|
---|
[900] | 1931 | mr_free(tmp1);
|
---|
[59] | 1932 | }
|
---|
[900] | 1933 | mr_free(tmp);
|
---|
[1] | 1934 |
|
---|
[59] | 1935 | if (!popup_and_get_string
|
---|
[507] | 1936 | (_("Exclude paths"),
|
---|
| 1937 | _("Please enter paths which you do NOT want to backup. Separate them with spaces. NB: /tmp and /proc are always excluded. :-) Just hit 'Enter' if you want to do a full system backup."),
|
---|
[688] | 1938 | bkpinfo->exclude_paths)) {
|
---|
[507] | 1939 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[59] | 1940 | finish(1);
|
---|
| 1941 | }
|
---|
| 1942 | bkpinfo->make_cd_use_lilo = FALSE;
|
---|
| 1943 | bkpinfo->backup_data = TRUE;
|
---|
| 1944 | bkpinfo->verify_data =
|
---|
| 1945 | ask_me_yes_or_no
|
---|
[507] | 1946 | (_("Will you want to verify your backups after Mondo has created them?"));
|
---|
[1] | 1947 |
|
---|
| 1948 | #ifndef __FreeBSD__
|
---|
[59] | 1949 | if (!ask_me_yes_or_no
|
---|
[507] | 1950 | (_("Are you confident that your kernel is a sane, sensible, standard Linux kernel? Say 'no' if you are using a Gentoo <1.4 or Debian <3.0, please."))){
|
---|
[900] | 1951 | mr_allocstr(bkpinfo->kernel_path, "FAILSAFE");
|
---|
[171] | 1952 | }
|
---|
[1] | 1953 | #endif
|
---|
| 1954 |
|
---|
[59] | 1955 | if (!ask_me_yes_or_no
|
---|
[507] | 1956 | (_("Are you sure you want to proceed? Hit 'no' to abort."))) {
|
---|
| 1957 | log_to_screen(_("User has chosen not to backup the PC"));
|
---|
[59] | 1958 | finish(1);
|
---|
| 1959 | }
|
---|
| 1960 | } else {
|
---|
| 1961 | bkpinfo->restore_data = TRUE; // probably...
|
---|
| 1962 | }
|
---|
[1] | 1963 |
|
---|
[59] | 1964 | if (bkpinfo->backup_media_type == iso
|
---|
| 1965 | || bkpinfo->backup_media_type == nfs) {
|
---|
| 1966 | g_ISO_restore_mode = TRUE;
|
---|
| 1967 | }
|
---|
[1] | 1968 | #ifdef __FreeSD__
|
---|
| 1969 | // skip
|
---|
| 1970 | #else
|
---|
[59] | 1971 | if (bkpinfo->backup_media_type == nfs) {
|
---|
| 1972 | log_msg(3, "I think the NFS mount is mounted at %s",
|
---|
| 1973 | bkpinfo->isodir);
|
---|
| 1974 | }
|
---|
| 1975 | log_it("isodir = %s", bkpinfo->isodir);
|
---|
| 1976 | log_it("nfs_mount = '%s'", bkpinfo->nfs_mount);
|
---|
[1] | 1977 | #endif
|
---|
| 1978 |
|
---|
[59] | 1979 | log_it("media device = %s", bkpinfo->media_device);
|
---|
| 1980 | log_it("media size = %ld", bkpinfo->media_size[1]);
|
---|
[783] | 1981 | log_it("media type = %s", bkpinfo->backup_media_string);
|
---|
[900] | 1982 | mr_free(tmp);
|
---|
[252] | 1983 | log_it("prefix = %s", bkpinfo->prefix);
|
---|
[59] | 1984 | log_it("compression = %ld", bkpinfo->compression_level);
|
---|
| 1985 | log_it("include_paths = '%s'", bkpinfo->include_paths);
|
---|
| 1986 | log_it("exclude_paths = '%s'", bkpinfo->exclude_paths);
|
---|
| 1987 | log_it("scratchdir = '%s'", bkpinfo->scratchdir);
|
---|
| 1988 | log_it("tmpdir = '%s'", bkpinfo->tmpdir);
|
---|
| 1989 | log_it("boot_device = '%s' (loader=%c)", bkpinfo->boot_device,
|
---|
| 1990 | bkpinfo->boot_loader);
|
---|
| 1991 | if (bkpinfo->media_size[0] < 0) {
|
---|
| 1992 | if (archiving_to_media) {
|
---|
| 1993 | fatal_error("Media size is less than zero.");
|
---|
| 1994 | } else {
|
---|
| 1995 | log_msg(2, "Warning - media size is less than zero.");
|
---|
| 1996 | bkpinfo->media_size[0] = 0;
|
---|
| 1997 | }
|
---|
[1] | 1998 | }
|
---|
[59] | 1999 | return (0);
|
---|
[1] | 2000 | }
|
---|
| 2001 |
|
---|
| 2002 |
|
---|
| 2003 | /**
|
---|
| 2004 | * Get a space-separated list of NFS mounts.
|
---|
| 2005 | * @return The list created.
|
---|
[688] | 2006 | * @note The return value should be freed by caller
|
---|
[1] | 2007 | * @bug Even though we only want the mounts, the devices are still checked.
|
---|
| 2008 | */
|
---|
| 2009 | char *list_of_NFS_mounts_only(void)
|
---|
| 2010 | {
|
---|
[688] | 2011 | return(call_program_and_get_last_line_of_output
|
---|
[794] | 2012 | ("mount -t coda,ncpfs,nfs,smbfs,cifs,afs,mvfs | tr -s '\t' ' ' | cut -d' ' -f3 | tr -s '\n' ' ' | awk '{print $0;}'"));
|
---|
[688] | 2013 | /* BERLIOS : Useless
|
---|
[900] | 2014 | mr_asprintf(&exclude_these_devices,
|
---|
[59] | 2015 | call_program_and_get_last_line_of_output
|
---|
[839] | 2016 | ("tr -s '\t' ' ' < /etc/fstab | grep -E '( (coda|ncpfs|nfs|smbfs|cifs|mvfs) )' | cut -d' ' -f1 | tr -s '\n' ' ' | awk '{print $0;}'"));
|
---|
[688] | 2017 | */
|
---|
[1] | 2018 | }
|
---|
| 2019 |
|
---|
[146] | 2020 |
|
---|
[1] | 2021 | /* @} - end of utilityGroup */
|
---|
| 2022 |
|
---|
| 2023 | /**
|
---|
| 2024 | * Set the tmpdir and scratchdir to reside on the partition with the most free space.
|
---|
| 2025 | * Automatically excludes DOS, NTFS, SMB, and NFS filesystems.
|
---|
| 2026 | * @param bkpinfo The backup information structure. @c bkpinfo->tmpdir and @c bkpinfo->scratchdir will be set.
|
---|
| 2027 | * @ingroup utilityGroup
|
---|
| 2028 | */
|
---|
| 2029 | void sensibly_set_tmpdir_and_scratchdir(struct s_bkpinfo *bkpinfo)
|
---|
| 2030 | {
|
---|
[688] | 2031 | char *tmp = NULL;
|
---|
| 2032 | char *command = NULL;
|
---|
| 2033 | char *sz = NULL;
|
---|
| 2034 | int i = 0;
|
---|
[1] | 2035 |
|
---|
[59] | 2036 | assert(bkpinfo != NULL);
|
---|
[1] | 2037 |
|
---|
| 2038 | #ifdef __FreeBSD__
|
---|
[688] | 2039 | tmp = call_program_and_get_last_line_of_output
|
---|
[794] | 2040 | ("df -m -P -t nonfs,msdosfs,ntfs,smbfs,smb,cifs,afs,mvfs | tr -s '\t' ' ' | grep -vE \"none|Filesystem\" | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'");
|
---|
[1] | 2041 | #else
|
---|
[688] | 2042 | tmp = call_program_and_get_last_line_of_output
|
---|
[794] | 2043 | ("df -m -P -x nfs -x vfat -x ntfs -x smbfs -x smb -x cifs -x afs -x mvfs | sed 's/ /devdev/' | tr -s '\t' ' ' | grep -vE \"none|Filesystem|/dev/shm\" | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'");
|
---|
[1] | 2044 | #endif
|
---|
| 2045 |
|
---|
[59] | 2046 | if (tmp[0] != '/') {
|
---|
[900] | 2047 | mr_asprintf(&sz, "/%s", tmp);
|
---|
| 2048 | mr_free(tmp);
|
---|
[146] | 2049 | tmp = sz;
|
---|
[59] | 2050 | }
|
---|
| 2051 | if (!tmp[0]) {
|
---|
| 2052 | fatal_error("I couldn't figure out the tempdir!");
|
---|
| 2053 | }
|
---|
[688] | 2054 | i = (int) (random() % 32768);
|
---|
[900] | 2055 | mr_free(bkpinfo->tmpdir);
|
---|
| 2056 | mr_asprintf(&bkpinfo->tmpdir, "%s/mondo.tmp.%d", tmp, i);
|
---|
[59] | 2057 | log_it("bkpinfo->tmpdir is being set to %s", bkpinfo->tmpdir);
|
---|
[1] | 2058 |
|
---|
[900] | 2059 | mr_free(bkpinfo->scratchdir);
|
---|
| 2060 | mr_asprintf(&bkpinfo->scratchdir, "%s/mondo.scratch.%d", tmp, i);
|
---|
[59] | 2061 | log_it("bkpinfo->scratchdir is being set to %s", bkpinfo->scratchdir);
|
---|
[1] | 2062 |
|
---|
[900] | 2063 | mr_asprintf(&g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
|
---|
[59] | 2064 | bkpinfo->scratchdir);
|
---|
| 2065 |
|
---|
[900] | 2066 | mr_asprintf(&command, "rm -Rf %s/mondo.tmp.* %s/mondo.scratch.*", tmp, tmp);
|
---|
| 2067 | mr_free(tmp);
|
---|
[146] | 2068 |
|
---|
[59] | 2069 | paranoid_system(command);
|
---|
[900] | 2070 | mr_free(command);
|
---|
[1] | 2071 | }
|
---|
| 2072 |
|
---|
| 2073 |
|
---|
| 2074 | /**
|
---|
| 2075 | * @addtogroup deviceGroup
|
---|
| 2076 | * @{
|
---|
| 2077 | */
|
---|
| 2078 | /**
|
---|
| 2079 | * If we can read @p dev, set @p output to it.
|
---|
| 2080 | * If @p dev cannot be read, set @p output to "".
|
---|
| 2081 | * @param dev The device to check for.
|
---|
[171] | 2082 | * @param output Set to @p dev if @p dev exists, NULL otherwise. Needs to be freed by caller
|
---|
[1] | 2083 | * @return TRUE if @p dev exists, FALSE if it doesn't.
|
---|
| 2084 | */
|
---|
[59] | 2085 | bool set_dev_to_this_if_rx_OK(char *output, char *dev)
|
---|
[1] | 2086 | {
|
---|
[59] | 2087 | char *command;
|
---|
[1] | 2088 |
|
---|
[900] | 2089 | mr_free(output);
|
---|
[59] | 2090 | if (!dev || dev[0] == '\0') {
|
---|
| 2091 | return (FALSE);
|
---|
| 2092 | }
|
---|
| 2093 | log_msg(10, "Injecting %s", dev);
|
---|
| 2094 | inject_device(dev);
|
---|
| 2095 | if (!does_file_exist(dev)) {
|
---|
| 2096 | log_msg(10, "%s doesn't exist. Returning FALSE.", dev);
|
---|
| 2097 | return (FALSE);
|
---|
| 2098 | }
|
---|
[900] | 2099 | mr_asprintf(&command, "dd bs=%ld count=1 if=%s of=/dev/null &> /dev/null",
|
---|
[59] | 2100 | 512L, dev);
|
---|
| 2101 | if (!run_program_and_log_output(command, FALSE)
|
---|
| 2102 | && !run_program_and_log_output(command, FALSE)) {
|
---|
[900] | 2103 | mr_asprintf(&output, dev);
|
---|
[59] | 2104 | log_msg(4, "Found it - %s", dev);
|
---|
[900] | 2105 | mr_free(command);
|
---|
[59] | 2106 | return (TRUE);
|
---|
| 2107 | } else {
|
---|
| 2108 | log_msg(4, "It's not %s", dev);
|
---|
[900] | 2109 | mr_free(command);
|
---|
[59] | 2110 | return (FALSE);
|
---|
| 2111 | }
|
---|
[1] | 2112 | }
|
---|
| 2113 |
|
---|
| 2114 |
|
---|
| 2115 | /**
|
---|
| 2116 | * Find out what number CD is in the drive.
|
---|
| 2117 | * @param bkpinfo The backup information structure. The @c bkpinfo->media_device field is the only one used.
|
---|
| 2118 | * @return The current CD number, or -1 if it could not be found.
|
---|
| 2119 | * @note If the CD is not mounted, it will be mounted
|
---|
| 2120 | * (and remain mounted after this function returns).
|
---|
| 2121 | */
|
---|
| 2122 | int what_number_cd_is_this(struct s_bkpinfo *bkpinfo)
|
---|
| 2123 | {
|
---|
[59] | 2124 | int cd_number = -1;
|
---|
[688] | 2125 | char *mountdev = NULL;
|
---|
| 2126 | char *tmp = NULL;
|
---|
| 2127 | char *tmp1 = NULL;
|
---|
[1] | 2128 |
|
---|
[59] | 2129 | assert(bkpinfo != NULL);
|
---|
| 2130 | if (g_ISO_restore_mode) {
|
---|
[900] | 2131 | mr_asprintf(&tmp, "mount | grep iso9660 | awk '{print $3;}'");
|
---|
[1] | 2132 |
|
---|
[688] | 2133 | tmp1 = call_program_and_get_last_line_of_output(tmp);
|
---|
[900] | 2134 | mr_free(tmp);
|
---|
[146] | 2135 |
|
---|
[900] | 2136 | mr_asprintf(&mountdev, "%s/archives/THIS-CD-NUMBER", tmp1);
|
---|
| 2137 | mr_free(tmp1);
|
---|
[688] | 2138 |
|
---|
| 2139 | tmp = last_line_of_file(mountdev);
|
---|
| 2140 | cd_number = atoi(tmp);
|
---|
[900] | 2141 | mr_free(mountdev);
|
---|
| 2142 | mr_free(tmp);
|
---|
[146] | 2143 |
|
---|
[59] | 2144 | return (cd_number);
|
---|
| 2145 | }
|
---|
[1] | 2146 |
|
---|
[171] | 2147 | if (bkpinfo->media_device == NULL) {
|
---|
[59] | 2148 | log_it
|
---|
| 2149 | ("(what_number_cd_is_this) Warning - media_device unknown. Finding out...");
|
---|
[171] | 2150 | bkpinfo->media_device = find_cdrom_device(FALSE);
|
---|
[59] | 2151 | }
|
---|
| 2152 | if (!is_this_device_mounted(MNT_CDROM)) {
|
---|
[171] | 2153 | (void)mount_CDROM_here(bkpinfo->media_device, MNT_CDROM);
|
---|
[59] | 2154 | }
|
---|
[688] | 2155 | tmp = last_line_of_file(MNT_CDROM "/archives/THIS-CD-NUMBER");
|
---|
| 2156 | cd_number = atoi(tmp);
|
---|
[900] | 2157 | mr_free(tmp);
|
---|
[59] | 2158 | return (cd_number);
|
---|
[1] | 2159 | }
|
---|
| 2160 |
|
---|
| 2161 |
|
---|
| 2162 | /**
|
---|
| 2163 | * Find out what device is mounted as root (/).
|
---|
| 2164 | * @return Root device.
|
---|
| 2165 | * @note The returned string points to static storage and will be overwritten with every call.
|
---|
| 2166 | * @bug A bit of a misnomer; it's actually finding out the root device.
|
---|
| 2167 | * The mountpoint (where it's mounted) will obviously be '/'.
|
---|
| 2168 | */
|
---|
[59] | 2169 | char *where_is_root_mounted()
|
---|
[1] | 2170 | {
|
---|
[146] | 2171 | char *tmp;
|
---|
[1] | 2172 |
|
---|
| 2173 | #ifdef __FreeBSD__
|
---|
[688] | 2174 | tmp = call_program_and_get_last_line_of_output
|
---|
| 2175 | ("mount | grep \" on / \" | cut -d' ' -f1");
|
---|
[1] | 2176 | #else
|
---|
[688] | 2177 | tmp = call_program_and_get_last_line_of_output
|
---|
| 2178 | ("mount | grep \" on / \" | cut -d' ' -f1 | sed s/[0-9]// | sed s/[0-9]//");
|
---|
[59] | 2179 | if (strstr(tmp, "/dev/cciss/")) {
|
---|
[900] | 2180 | mr_free(tmp);
|
---|
[688] | 2181 | tmp = call_program_and_get_last_line_of_output
|
---|
| 2182 | ("mount | grep \" on / \" | cut -d' ' -f1 | cut -dp -f1");
|
---|
[59] | 2183 | }
|
---|
| 2184 | if (strstr(tmp, "/dev/md")) {
|
---|
[900] | 2185 | mr_free(tmp);
|
---|
[688] | 2186 | tmp = call_program_and_get_last_line_of_output
|
---|
| 2187 | ("mount | grep \" on / \" | cut -d' ' -f1");
|
---|
[59] | 2188 | }
|
---|
[1] | 2189 | #endif
|
---|
| 2190 |
|
---|
[59] | 2191 | return (tmp);
|
---|
[1] | 2192 | }
|
---|
| 2193 |
|
---|
| 2194 |
|
---|
| 2195 | /**
|
---|
| 2196 | * Find out which boot loader is in use.
|
---|
| 2197 | * @param which_device Device to look for the boot loader on.
|
---|
| 2198 | * @return 'L' for LILO, 'E'for ELILO, 'G' for GRUB, 'B' or 'D' for FreeBSD boot loaders, or 'U' for Unknown.
|
---|
| 2199 | * @note Under Linux, all drives are examined, not just @p which_device.
|
---|
| 2200 | */
|
---|
| 2201 | #ifdef __FreeBSD__
|
---|
[59] | 2202 | char which_boot_loader(char *which_device)
|
---|
[1] | 2203 | {
|
---|
[59] | 2204 | int count_lilos = 0;
|
---|
| 2205 | int count_grubs = 0;
|
---|
| 2206 | int count_boot0s = 0;
|
---|
| 2207 | int count_dangerouslydedicated = 0;
|
---|
[1] | 2208 |
|
---|
[59] | 2209 | log_it("looking at drive %s's MBR", which_device);
|
---|
| 2210 | if (does_string_exist_in_boot_block(which_device, "GRUB")) {
|
---|
| 2211 | count_grubs++;
|
---|
| 2212 | }
|
---|
| 2213 | if (does_string_exist_in_boot_block(which_device, "LILO")) {
|
---|
| 2214 | count_lilos++;
|
---|
| 2215 | }
|
---|
| 2216 | if (does_string_exist_in_boot_block(which_device, "Drive")) {
|
---|
| 2217 | count_boot0s++;
|
---|
| 2218 | }
|
---|
| 2219 | if (does_string_exist_in_first_N_blocks
|
---|
| 2220 | (which_device, "FreeBSD/i386", 17)) {
|
---|
| 2221 | count_dangerouslydedicated++;
|
---|
| 2222 | }
|
---|
| 2223 | log_it("%d grubs and %d lilos and %d elilos and %d boot0s and %d DD\n",
|
---|
| 2224 | count_grubs, count_lilos, count_elilos, count_boot0s,
|
---|
| 2225 | count_dangerouslydedicated);
|
---|
[1] | 2226 |
|
---|
[59] | 2227 | if (count_grubs && !count_lilos) {
|
---|
| 2228 | return ('G');
|
---|
| 2229 | } else if (count_lilos && !count_grubs) {
|
---|
| 2230 | return ('L');
|
---|
| 2231 | } else if (count_grubs == 1 && count_lilos == 1) {
|
---|
| 2232 | log_it("I'll bet you used to use LILO but switched to GRUB...");
|
---|
| 2233 | return ('G');
|
---|
| 2234 | } else if (count_boot0s == 1) {
|
---|
| 2235 | return ('B');
|
---|
| 2236 | } else if (count_dangerouslydedicated) {
|
---|
| 2237 | return ('D');
|
---|
| 2238 | } else {
|
---|
| 2239 | log_it("Unknown boot loader");
|
---|
| 2240 | return ('U');
|
---|
| 2241 | }
|
---|
[1] | 2242 | }
|
---|
| 2243 |
|
---|
| 2244 | #else
|
---|
| 2245 |
|
---|
[59] | 2246 | char which_boot_loader(char *which_device)
|
---|
[783] | 2247 | /* which_device is allocated here and needs to be freed by caller */
|
---|
[1] | 2248 | {
|
---|
[59] | 2249 | /*@ buffer ***************************************************** */
|
---|
| 2250 | char *list_drives_cmd;
|
---|
[146] | 2251 | char *current_drive = NULL;
|
---|
[1] | 2252 |
|
---|
[59] | 2253 | /*@ pointers *************************************************** */
|
---|
| 2254 | FILE *pdrives;
|
---|
[1] | 2255 |
|
---|
[59] | 2256 | /*@ int ******************************************************** */
|
---|
| 2257 | int count_lilos = 0;
|
---|
| 2258 | int count_grubs = 0;
|
---|
[171] | 2259 | size_t n = 0;
|
---|
[1] | 2260 |
|
---|
[59] | 2261 | /*@ end vars *************************************************** */
|
---|
[1] | 2262 |
|
---|
| 2263 | #ifdef __IA64__
|
---|
[59] | 2264 | /* No choice for it */
|
---|
| 2265 | return ('E');
|
---|
[1] | 2266 | #endif
|
---|
[59] | 2267 | assert(which_device != NULL);
|
---|
[900] | 2268 | mr_asprintf(&list_drives_cmd,
|
---|
[197] | 2269 | "parted2fdisk -l 2>/dev/null | grep \"/dev/.*:\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/; echo %s",
|
---|
[59] | 2270 | where_is_root_mounted());
|
---|
| 2271 | log_it("list_drives_cmd = %s", list_drives_cmd);
|
---|
[1] | 2272 |
|
---|
[59] | 2273 | if (!(pdrives = popen(list_drives_cmd, "r"))) {
|
---|
| 2274 | log_OS_error("Unable to open list of drives");
|
---|
[900] | 2275 | mr_free(list_drives_cmd);
|
---|
[59] | 2276 | return ('\0');
|
---|
[1] | 2277 | }
|
---|
[900] | 2278 | mr_free(list_drives_cmd);
|
---|
[146] | 2279 |
|
---|
[900] | 2280 | for (mr_getline(¤t_drive, &n, pdrives); !feof(pdrives);
|
---|
| 2281 | mr_getline(¤t_drive, &n, pdrives)) {
|
---|
[59] | 2282 | strip_spaces(current_drive);
|
---|
| 2283 | log_it("looking at drive %s's MBR", current_drive);
|
---|
| 2284 | if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
|
---|
| 2285 | count_grubs++;
|
---|
[900] | 2286 | mr_allocstr(which_device,current_drive);
|
---|
[59] | 2287 | break;
|
---|
| 2288 | }
|
---|
| 2289 | if (does_string_exist_in_boot_block(current_drive, "LILO")) {
|
---|
| 2290 | count_lilos++;
|
---|
[900] | 2291 | mr_allocstr(which_device,current_drive);
|
---|
[59] | 2292 | break;
|
---|
| 2293 | }
|
---|
[1] | 2294 | }
|
---|
[146] | 2295 |
|
---|
[59] | 2296 | if (pclose(pdrives)) {
|
---|
| 2297 | log_OS_error("Cannot pclose pdrives");
|
---|
| 2298 | }
|
---|
| 2299 | log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
|
---|
| 2300 | if (count_grubs && !count_lilos) {
|
---|
| 2301 | return ('G');
|
---|
| 2302 | } else if (count_lilos && !count_grubs) {
|
---|
| 2303 | return ('L');
|
---|
| 2304 | } else if (count_grubs == 1 && count_lilos == 1) {
|
---|
| 2305 | log_it("I'll bet you used to use LILO but switched to GRUB...");
|
---|
| 2306 | return ('G');
|
---|
| 2307 | } else {
|
---|
| 2308 | log_it("Unknown boot loader");
|
---|
| 2309 | return ('U');
|
---|
| 2310 | }
|
---|
[1] | 2311 | }
|
---|
| 2312 | #endif
|
---|
| 2313 |
|
---|
| 2314 |
|
---|
| 2315 | /**
|
---|
| 2316 | * Write zeroes over the first 16K of @p device.
|
---|
| 2317 | * @param device The device to zero.
|
---|
| 2318 | * @return 0 for success, 1 for failure.
|
---|
| 2319 | */
|
---|
[59] | 2320 | int zero_out_a_device(char *device)
|
---|
[1] | 2321 | {
|
---|
[59] | 2322 | FILE *fout;
|
---|
| 2323 | int i;
|
---|
[1] | 2324 |
|
---|
[59] | 2325 | assert_string_is_neither_NULL_nor_zerolength(device);
|
---|
[1] | 2326 |
|
---|
[59] | 2327 | log_it("Zeroing drive %s", device);
|
---|
| 2328 | if (!(fout = fopen(device, "w"))) {
|
---|
| 2329 | log_OS_error("Unable to open/write to device");
|
---|
| 2330 | return (1);
|
---|
| 2331 | }
|
---|
| 2332 | for (i = 0; i < 16384; i++) {
|
---|
| 2333 | fputc('\0', fout);
|
---|
| 2334 | }
|
---|
| 2335 | paranoid_fclose(fout);
|
---|
| 2336 | log_it("Device successfully zeroed.");
|
---|
| 2337 | return (0);
|
---|
[1] | 2338 | }
|
---|
| 2339 |
|
---|
[146] | 2340 |
|
---|
[1] | 2341 | /**
|
---|
| 2342 | * Return the device pointed to by @p incoming.
|
---|
| 2343 | * @param incoming The device to resolve symlinks for.
|
---|
| 2344 | * @return The path to the real device file.
|
---|
[146] | 2345 | * @note The returned string points to a storage that need to be freed by the caller
|
---|
[1] | 2346 | * @bug Won't work with file v4.0; needs to be written in C.
|
---|
| 2347 | */
|
---|
[59] | 2348 | char *resolve_softlinks_to_get_to_actual_device_file(char *incoming)
|
---|
[1] | 2349 | {
|
---|
[688] | 2350 | char *output = NULL;
|
---|
| 2351 | char *command = NULL;
|
---|
| 2352 | char *curr_fname = NULL;
|
---|
| 2353 | char *scratch = NULL;
|
---|
| 2354 | char *tmp = NULL;
|
---|
| 2355 | char *p = NULL;
|
---|
[59] | 2356 |
|
---|
| 2357 | struct stat statbuf;
|
---|
| 2358 | if (!does_file_exist(incoming)) {
|
---|
| 2359 | log_it
|
---|
| 2360 | ("resolve_softlinks_to_get_to_actual_device_file --- device not found");
|
---|
[900] | 2361 | mr_asprintf(&output, incoming);
|
---|
[59] | 2362 | } else {
|
---|
[900] | 2363 | mr_asprintf(&curr_fname, incoming);
|
---|
[59] | 2364 | lstat(curr_fname, &statbuf);
|
---|
| 2365 | while (S_ISLNK(statbuf.st_mode)) {
|
---|
| 2366 | log_msg(1, "curr_fname = %s", curr_fname);
|
---|
[900] | 2367 | mr_asprintf(&command, "file %s", curr_fname);
|
---|
[688] | 2368 | tmp = call_program_and_get_last_line_of_output(command);
|
---|
[900] | 2369 | mr_free(command);
|
---|
[146] | 2370 |
|
---|
[59] | 2371 | for (p = tmp + strlen(tmp); p != tmp && *p != '`' && *p != ' ';
|
---|
| 2372 | p--);
|
---|
| 2373 | p++;
|
---|
[900] | 2374 | mr_asprintf(&scratch, p);
|
---|
[59] | 2375 | for (p = scratch; *p != '\0' && *p != '\''; p++);
|
---|
| 2376 | *p = '\0';
|
---|
[688] | 2377 | log_msg(0, "curr_fname %s --> '%s' --> %s", curr_fname, tmp, scratch);
|
---|
[900] | 2378 | mr_free(tmp);
|
---|
[146] | 2379 |
|
---|
[59] | 2380 | if (scratch[0] == '/') {
|
---|
[900] | 2381 | mr_free(curr_fname);
|
---|
| 2382 | mr_asprintf(&curr_fname, scratch); // copy whole thing because it's an absolute softlink
|
---|
[59] | 2383 | } else { // copy over the basename cos it's a relative softlink
|
---|
| 2384 | p = curr_fname + strlen(curr_fname);
|
---|
| 2385 | while (p != curr_fname && *p != '/') {
|
---|
| 2386 | p--;
|
---|
| 2387 | }
|
---|
| 2388 | if (*p == '/') {
|
---|
| 2389 | p++;
|
---|
| 2390 | }
|
---|
[146] | 2391 | *p = '\0';
|
---|
[900] | 2392 | mr_asprintf(&tmp, "%s%s", curr_fname, scratch);
|
---|
| 2393 | mr_free(curr_fname);
|
---|
[146] | 2394 | curr_fname = tmp;
|
---|
[59] | 2395 | }
|
---|
[900] | 2396 | mr_free(scratch);
|
---|
[59] | 2397 | lstat(curr_fname, &statbuf);
|
---|
| 2398 | }
|
---|
[900] | 2399 | mr_asprintf(&output, curr_fname);
|
---|
[59] | 2400 | log_it("resolved %s to %s", incoming, output);
|
---|
[900] | 2401 | mr_free(curr_fname);
|
---|
[1] | 2402 | }
|
---|
[59] | 2403 | return (output);
|
---|
[1] | 2404 | }
|
---|
| 2405 |
|
---|
| 2406 | /* @} - end of deviceGroup */
|
---|
| 2407 |
|
---|
| 2408 |
|
---|
| 2409 | /**
|
---|
| 2410 | * Return the type of partition format (GPT or MBR)
|
---|
[146] | 2411 | * Caller needs to free the return value
|
---|
[1] | 2412 | */
|
---|
| 2413 | char *which_partition_format(const char *drive)
|
---|
| 2414 | {
|
---|
[688] | 2415 | char *output = NULL;
|
---|
| 2416 | char *tmp = NULL;
|
---|
| 2417 | char *command = NULL;
|
---|
| 2418 | char *fdisk = NULL;
|
---|
[59] | 2419 |
|
---|
| 2420 | log_msg(0, "Looking for partition table format type");
|
---|
[900] | 2421 | mr_asprintf(&fdisk, "/sbin/parted2fdisk");
|
---|
[89] | 2422 | log_msg(1, "Using %s", fdisk);
|
---|
[900] | 2423 | mr_asprintf(&command, "%s -l %s | grep 'EFI GPT'", fdisk, drive);
|
---|
| 2424 | mr_free(fdisk);
|
---|
[146] | 2425 |
|
---|
[688] | 2426 | tmp = call_program_and_get_last_line_of_output(command);
|
---|
[900] | 2427 | mr_free(command);
|
---|
[146] | 2428 |
|
---|
[59] | 2429 | if (strstr(tmp, "GPT") == NULL) {
|
---|
[900] | 2430 | mr_asprintf(&output, "MBR");
|
---|
[59] | 2431 | } else {
|
---|
[900] | 2432 | mr_asprintf(&output, "GPT");
|
---|
[59] | 2433 | }
|
---|
[900] | 2434 | mr_free(tmp);
|
---|
[59] | 2435 | log_msg(0, "Found %s partition table format type", output);
|
---|
| 2436 | return (output);
|
---|
[1] | 2437 | }
|
---|
| 2438 |
|
---|
| 2439 | /* @} - end of deviceGroup */
|
---|