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