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