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