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