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