[1] | 1 | /* libmondo-devices.c Subroutines for handling devices
|
---|
[85] | 2 | $Id: libmondo-devices.c 2428 2009-09-28 00:29:12Z 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"
|
---|
[2428] | 12 | #include "mr_str.h"
|
---|
[1] | 13 | #include "mondostructures.h"
|
---|
| 14 | #include "libmondo-files-EXT.h"
|
---|
| 15 | #include "libmondo-devices.h"
|
---|
[541] | 16 | #include "lib-common-externs.h"
|
---|
[1] | 17 | #include "libmondo-string-EXT.h"
|
---|
| 18 | #include "libmondo-tools-EXT.h"
|
---|
[541] | 19 | #include "libmondo-gui-EXT.h"
|
---|
[1] | 20 | #include "libmondo-fork-EXT.h"
|
---|
| 21 | #include "libmondo-stream-EXT.h"
|
---|
| 22 |
|
---|
[2322] | 23 | extern void mr_strip_spaces(char *);
|
---|
| 24 |
|
---|
[1] | 25 | #include <sys/types.h>
|
---|
| 26 | #ifdef __FreeBSD__
|
---|
| 27 | #define DKTYPENAMES
|
---|
| 28 | #define FSTYPENAMES
|
---|
| 29 | #include <sys/disklabel.h>
|
---|
| 30 | #include <sys/disk.h>
|
---|
| 31 | #elif linux
|
---|
| 32 | #define u64 unsigned long long
|
---|
[128] | 33 | #include <linux/fs.h> /* for BLKGETSIZE64 */
|
---|
[1] | 34 | #include <linux/hdreg.h>
|
---|
| 35 | #endif
|
---|
| 36 |
|
---|
| 37 | /*@unused@*/
|
---|
[85] | 38 | //static char cvsid[] = "$Id: libmondo-devices.c 2428 2009-09-28 00:29:12Z bruno $";
|
---|
[1] | 39 |
|
---|
| 40 | extern int g_current_media_number;
|
---|
| 41 | extern double g_kernel_version;
|
---|
| 42 |
|
---|
| 43 | extern bool g_ISO_restore_mode;
|
---|
[99] | 44 | extern char *g_selfmounted_isodir;
|
---|
[1316] | 45 | extern char *MONDO_LOGFILE;
|
---|
[1] | 46 |
|
---|
[128] | 47 | static char g_cdrw_drive_is_here[MAX_STR_LEN / 4] = "";
|
---|
| 48 | static char g_cdrom_drive_is_here[MAX_STR_LEN / 4] = "";
|
---|
| 49 | static char g_dvd_drive_is_here[MAX_STR_LEN / 4] = "";
|
---|
[1] | 50 |
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
| 53 | * ????? @bug ?????
|
---|
| 54 | * @ingroup globalGroup
|
---|
| 55 | */
|
---|
[128] | 56 | bool g_restoring_live_from_cd = FALSE;
|
---|
[2382] | 57 | bool g_restoring_live_from_netfs = FALSE;
|
---|
[1] | 58 |
|
---|
[128] | 59 | extern t_bkptype g_backup_media_type; // set by main()
|
---|
[1] | 60 |
|
---|
[1645] | 61 | /* Reference to global bkpinfo */
|
---|
| 62 | extern struct s_bkpinfo *bkpinfo;
|
---|
[1] | 63 |
|
---|
[2428] | 64 | /* Stuff that handles the -I and -E option when a whole disk DSF is used */
|
---|
| 65 | typedef struct mounted_fs_struct {
|
---|
| 66 | char device[MAX_STR_LEN]; /* The name of the device */
|
---|
| 67 | char mount_point[MAX_STR_LEN]; /* The devices mount point */
|
---|
| 68 | unsigned char check; /* 1 == included on DSF */
|
---|
| 69 | struct mounted_fs_struct *next;
|
---|
| 70 | } MOUNTED_FS_STRUCT;
|
---|
[1] | 71 |
|
---|
[2428] | 72 | static MOUNTED_FS_STRUCT *DSF_Head = NULL; /* Points to the first entry of mounted_fs_struct list */
|
---|
| 73 | static MOUNTED_FS_STRUCT *DSF_Tail = NULL; /* Points to the last entry of mounted_fs_struct list */
|
---|
[1] | 74 |
|
---|
[1645] | 75 |
|
---|
[2325] | 76 | void set_g_cdrom_and_g_dvd_to_bkpinfo_value() {
|
---|
| 77 |
|
---|
| 78 | if (bkpinfo->media_device) {
|
---|
| 79 | strcpy(g_cdrom_drive_is_here, bkpinfo->media_device); // just in case
|
---|
| 80 | strcpy(g_dvd_drive_is_here, bkpinfo->media_device); // just in case
|
---|
| 81 | }
|
---|
[1] | 82 | }
|
---|
| 83 |
|
---|
| 84 |
|
---|
| 85 |
|
---|
| 86 | /**
|
---|
| 87 | * Retract all CD trays and wait for autorun to complete.
|
---|
| 88 | * @ingroup deviceGroup
|
---|
| 89 | */
|
---|
| 90 | void retract_CD_tray_and_defeat_autorun(void)
|
---|
| 91 | {
|
---|
| 92 | // log_it("rctada: Retracting all CD trays", __LINE__);
|
---|
[128] | 93 | if (strlen(g_cdrom_drive_is_here) > 0) {
|
---|
| 94 | inject_device(g_cdrom_drive_is_here);
|
---|
| 95 | }
|
---|
| 96 | if (strlen(g_dvd_drive_is_here) > 0) {
|
---|
| 97 | inject_device(g_dvd_drive_is_here);
|
---|
| 98 | }
|
---|
| 99 | if (strlen(g_cdrw_drive_is_here) > 0) {
|
---|
| 100 | inject_device(g_cdrw_drive_is_here);
|
---|
| 101 | }
|
---|
[1] | 102 | // log_it("rctada: killing autorun");
|
---|
| 103 | // run_program_and_log_output("killall autorun", TRUE);
|
---|
[128] | 104 | if (!run_program_and_log_output("ps | grep autorun | grep -v grep", 5)) {
|
---|
| 105 | log_it("autorun detected; sleeping for 2 seconds");
|
---|
| 106 | sleep(2);
|
---|
| 107 | }
|
---|
| 108 | log_it("rctada: Unmounting all CD drives", __LINE__);
|
---|
| 109 | run_program_and_log_output("umount /dev/cdr* /dev/dvd*", 5);
|
---|
[1] | 110 | }
|
---|
| 111 |
|
---|
| 112 |
|
---|
| 113 |
|
---|
| 114 | /**
|
---|
| 115 | * Determine whether we're booted off a ramdisk.
|
---|
| 116 | * @return @c TRUE (we are) or @c FALSE (we aren't).
|
---|
| 117 | * @ingroup utilityGroup
|
---|
| 118 | */
|
---|
[128] | 119 | bool am_I_in_disaster_recovery_mode(void)
|
---|
[1] | 120 | {
|
---|
[2214] | 121 | char *tmp = NULL;
|
---|
[128] | 122 | bool is_this_a_ramdisk = FALSE;
|
---|
[1] | 123 |
|
---|
[2330] | 124 | tmp = where_is_root_mounted();
|
---|
[2324] | 125 | log_msg(0, "root is currently mounted at %s\n", tmp);
|
---|
[1] | 126 |
|
---|
| 127 | #ifdef __FreeBSD__
|
---|
[128] | 128 | if (strstr(tmp, "/dev/md")) {
|
---|
| 129 | is_this_a_ramdisk = TRUE;
|
---|
| 130 | }
|
---|
[1] | 131 | #else
|
---|
[128] | 132 | if (!strncmp(tmp, "/dev/ram", 8)
|
---|
| 133 | || (!strncmp(tmp, "/dev/rd", 7) && !strcmp(tmp, "/dev/rd/")
|
---|
| 134 | && strncmp(tmp, "/dev/rd/cd", 10)) || strstr(tmp, "rootfs")
|
---|
| 135 | || !strcmp(tmp, "/dev/root")) {
|
---|
| 136 | is_this_a_ramdisk = TRUE;
|
---|
| 137 | } else {
|
---|
| 138 | is_this_a_ramdisk = FALSE;
|
---|
| 139 | }
|
---|
[1] | 140 | #endif
|
---|
[2214] | 141 | mr_free(tmp);
|
---|
[1] | 142 |
|
---|
[2321] | 143 | log_msg(1, "Is this a ramdisk? result = %s", (is_this_a_ramdisk) ? "TRUE" : "FALSE");
|
---|
[128] | 144 | return (is_this_a_ramdisk);
|
---|
[1] | 145 | }
|
---|
| 146 |
|
---|
| 147 |
|
---|
| 148 |
|
---|
| 149 |
|
---|
| 150 |
|
---|
| 151 | /**
|
---|
| 152 | * Turn @c bkpinfo->backup_media_type into a human-readable string.
|
---|
| 153 | * @return The human readable string (e.g. @c cdr becomes <tt>"cdr"</tt>).
|
---|
| 154 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
| 155 | * @ingroup stringGroup
|
---|
| 156 | */
|
---|
| 157 | static char *bkptype_to_string(t_bkptype bt)
|
---|
| 158 | {
|
---|
[2330] | 159 | char *output;
|
---|
[128] | 160 | switch (bt) {
|
---|
| 161 | case none:
|
---|
[2330] | 162 | mr_asprintf(output, "none");
|
---|
[1] | 163 | break;
|
---|
[128] | 164 | case iso:
|
---|
[2330] | 165 | mr_asprintf(output, "iso");
|
---|
[1] | 166 | break;
|
---|
[128] | 167 | case cdr:
|
---|
[2330] | 168 | mr_asprintf(output, "cdr");
|
---|
[1] | 169 | break;
|
---|
[128] | 170 | case cdrw:
|
---|
[2330] | 171 | mr_asprintf(output, "cdrw");
|
---|
[1] | 172 | break;
|
---|
[128] | 173 | case cdstream:
|
---|
[2330] | 174 | mr_asprintf(output, "cdstream");
|
---|
[1] | 175 | break;
|
---|
[2382] | 176 | case netfs:
|
---|
| 177 | mr_asprintf(output, "netfs");
|
---|
[1] | 178 | break;
|
---|
[128] | 179 | case tape:
|
---|
[2330] | 180 | mr_asprintf(output, "tape");
|
---|
[1] | 181 | break;
|
---|
[128] | 182 | case udev:
|
---|
[2330] | 183 | mr_asprintf(output, "udev");
|
---|
[1] | 184 | break;
|
---|
[1687] | 185 | case usb:
|
---|
[2330] | 186 | mr_asprintf(output, "usb");
|
---|
[1687] | 187 | break;
|
---|
[128] | 188 | default:
|
---|
[2330] | 189 | mr_asprintf(output, "default");
|
---|
[128] | 190 | }
|
---|
| 191 | return (output);
|
---|
[1] | 192 | }
|
---|
| 193 |
|
---|
| 194 |
|
---|
| 195 |
|
---|
| 196 | /**
|
---|
| 197 | * @addtogroup deviceGroup
|
---|
| 198 | * @{
|
---|
| 199 | */
|
---|
| 200 | /**
|
---|
| 201 | * Eject the tray of the specified CD device.
|
---|
| 202 | * @param dev The device to eject.
|
---|
| 203 | * @return the return value of the @c eject command. (0=success, nonzero=failure)
|
---|
| 204 | */
|
---|
[128] | 205 | int eject_device(char *dev)
|
---|
[1] | 206 | {
|
---|
[2266] | 207 | char *command = NULL;
|
---|
[128] | 208 | int res1 = 0, res2 = 0;
|
---|
[1] | 209 |
|
---|
[2325] | 210 | if (dev == NULL) {
|
---|
| 211 | return (1);
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[128] | 214 | if (IS_THIS_A_STREAMING_BACKUP(g_backup_media_type)
|
---|
| 215 | && g_backup_media_type != udev) {
|
---|
[2323] | 216 | mr_asprintf(command, "mt -f %s offline", dev);
|
---|
[128] | 217 | res1 = run_program_and_log_output(command, 1);
|
---|
[2266] | 218 | mr_free(command);
|
---|
[128] | 219 | } else {
|
---|
| 220 | res1 = 0;
|
---|
| 221 | }
|
---|
[1] | 222 |
|
---|
| 223 | #ifdef __FreeBSD__
|
---|
[128] | 224 | if (strstr(dev, "acd")) {
|
---|
[2323] | 225 | mr_asprintf(command, "cdcontrol -f %s eject", dev);
|
---|
[128] | 226 | } else {
|
---|
[2324] | 227 | mr_asprintf(command, "camcontrol eject `echo %s | sed 's|/dev/||'`", dev);
|
---|
[128] | 228 | }
|
---|
[1] | 229 | #else
|
---|
[2323] | 230 | mr_asprintf(command, "eject %s", dev);
|
---|
[1] | 231 | #endif
|
---|
| 232 |
|
---|
[128] | 233 | log_msg(3, "Ejecting %s", dev);
|
---|
| 234 | res2 = run_program_and_log_output(command, 1);
|
---|
[2266] | 235 | mr_free(command);
|
---|
[128] | 236 | if (res1 && res2) {
|
---|
| 237 | return (1);
|
---|
| 238 | } else {
|
---|
| 239 | return (0);
|
---|
| 240 | }
|
---|
[1] | 241 | }
|
---|
| 242 |
|
---|
| 243 | /**
|
---|
| 244 | * Load (inject) the tray of the specified CD device.
|
---|
| 245 | * @param dev The device to load/inject.
|
---|
| 246 | * @return 0 for success, nonzero for failure.
|
---|
| 247 | */
|
---|
[128] | 248 | int inject_device(char *dev)
|
---|
[1] | 249 | {
|
---|
[2266] | 250 | char *command = NULL;
|
---|
[128] | 251 | int i;
|
---|
[1] | 252 |
|
---|
[2325] | 253 | if (dev == NULL) {
|
---|
| 254 | return (1);
|
---|
| 255 | }
|
---|
| 256 |
|
---|
[1] | 257 | #ifdef __FreeBSD__
|
---|
[128] | 258 | if (strstr(dev, "acd")) {
|
---|
[2323] | 259 | mr_asprintf(command, "cdcontrol -f %s close", dev);
|
---|
[128] | 260 | } else {
|
---|
[2323] | 261 | mr_asprintf(command, "camcontrol load `echo %s | sed 's|/dev/||'`", dev);
|
---|
[128] | 262 | }
|
---|
[1] | 263 | #else
|
---|
[2323] | 264 | mr_asprintf(command, "eject -t %s", dev);
|
---|
[1] | 265 | #endif
|
---|
[128] | 266 | i = run_program_and_log_output(command, FALSE);
|
---|
[2266] | 267 | mr_free(command);
|
---|
[128] | 268 | return (i);
|
---|
[1] | 269 | }
|
---|
| 270 |
|
---|
| 271 |
|
---|
| 272 | /**
|
---|
| 273 | * Determine whether the specified @p device (really, you can use any file)
|
---|
| 274 | * exists.
|
---|
| 275 | * @return TRUE if it exists, FALSE if it doesn't.
|
---|
| 276 | */
|
---|
[128] | 277 | bool does_device_exist(char *device)
|
---|
[1] | 278 | {
|
---|
| 279 |
|
---|
[128] | 280 | /*@ buffers *********************************************************** */
|
---|
[2266] | 281 | char *tmp = NULL;
|
---|
[128] | 282 | bool ret;
|
---|
[1] | 283 |
|
---|
[128] | 284 | assert_string_is_neither_NULL_nor_zerolength(device);
|
---|
[1] | 285 |
|
---|
[2323] | 286 | mr_asprintf(tmp, "ls %s > /dev/null 2> /dev/null", device);
|
---|
[128] | 287 |
|
---|
| 288 | if (system(tmp)) {
|
---|
| 289 | ret = FALSE;
|
---|
| 290 | } else {
|
---|
| 291 | ret = TRUE;
|
---|
| 292 | }
|
---|
[2266] | 293 | mr_free(tmp);
|
---|
[128] | 294 | return (ret);
|
---|
[1] | 295 | }
|
---|
| 296 |
|
---|
| 297 |
|
---|
| 298 | /**
|
---|
| 299 | * Determine whether a non-Microsoft partition exists on any connected hard drive.
|
---|
| 300 | * @return TRUE (there's a Linux/FreeBSD partition) or FALSE (Microsoft has taken over yet another innocent PC).
|
---|
| 301 | */
|
---|
[128] | 302 | bool does_nonMS_partition_exist(void)
|
---|
[1] | 303 | {
|
---|
| 304 | #if __FreeBSD__
|
---|
[128] | 305 | return
|
---|
| 306 | !system
|
---|
| 307 | ("for drive in /dev/ad? /dev/da?; do fdisk $drive | grep -q FreeBSD && exit 0; done; false");
|
---|
[1] | 308 | #else
|
---|
[128] | 309 | return
|
---|
| 310 | !system
|
---|
[681] | 311 | ("parted2fdisk -l 2>/dev/null | grep '^/dev/' | grep -Eqv '(MS|DOS|FAT|NTFS)'");
|
---|
[1] | 312 | #endif
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | /**
|
---|
| 316 | * Determine whether the specified @p partno exists on the specified @p drive.
|
---|
| 317 | * @param drive The drive to search for the partition in.
|
---|
| 318 | * @param partno The partition number to look for.
|
---|
| 319 | * @return 0 if it exists, nonzero otherwise.
|
---|
| 320 | */
|
---|
[128] | 321 | int does_partition_exist(const char *drive, int partno)
|
---|
[1] | 322 | {
|
---|
[128] | 323 | /*@ buffers **************************************************** */
|
---|
[2266] | 324 | char *program = NULL;
|
---|
[2351] | 325 | char *incoming = NULL;
|
---|
[2211] | 326 | char *searchstr = NULL;
|
---|
[2316] | 327 | char *tmp = NULL;
|
---|
[1] | 328 |
|
---|
[128] | 329 | /*@ ints ******************************************************* */
|
---|
| 330 | int res = 0;
|
---|
[1] | 331 |
|
---|
[128] | 332 | /*@ pointers *************************************************** */
|
---|
| 333 | FILE *fin;
|
---|
[1] | 334 |
|
---|
| 335 |
|
---|
[128] | 336 | /*@ end vars *************************************************** */
|
---|
| 337 | assert_string_is_neither_NULL_nor_zerolength(drive);
|
---|
| 338 | assert(partno >= 0 && partno < 999);
|
---|
[1] | 339 |
|
---|
| 340 | #ifdef __FreeBSD__
|
---|
[128] | 341 | // We assume here that this is running from mondorestore. (It is.)
|
---|
[2316] | 342 | tmp = build_partition_name(drive, partno);
|
---|
[2323] | 343 | mr_asprintf(program, "ls %s %s >/dev/null 2>&1", drive, tmp);
|
---|
[2316] | 344 | mr_free(tmp);
|
---|
| 345 |
|
---|
[2266] | 346 | res = system(program);
|
---|
| 347 | mr_free(program);
|
---|
| 348 | return (res);
|
---|
[1] | 349 | #else
|
---|
[2316] | 350 | /* To avoid compiler warnings */
|
---|
| 351 | tmp = NULL;
|
---|
[1] | 352 | #endif
|
---|
| 353 |
|
---|
[2323] | 354 | mr_asprintf(program, "parted2fdisk -l %s 2> /dev/null", drive);
|
---|
[128] | 355 | fin = popen(program, "r");
|
---|
| 356 | if (!fin) {
|
---|
| 357 | log_it("program=%s", program);
|
---|
| 358 | log_OS_error("Cannot popen-in program");
|
---|
[2266] | 359 | mr_free(program);
|
---|
[128] | 360 | return (0);
|
---|
[1] | 361 | }
|
---|
[2266] | 362 | mr_free(program);
|
---|
| 363 |
|
---|
[2316] | 364 | searchstr = build_partition_name(drive, partno);
|
---|
| 365 | mr_strcat(searchstr, " ");
|
---|
[2351] | 366 | for (res = 0, mr_getline(incoming, fin); !res && !feof(fin) ; mr_getline(incoming, fin)) {
|
---|
[128] | 367 | if (strstr(incoming, searchstr)) {
|
---|
| 368 | res = 1;
|
---|
| 369 | }
|
---|
[2351] | 370 | mr_free(incoming);
|
---|
[128] | 371 | }
|
---|
[2351] | 372 | mr_free(incoming);
|
---|
[2316] | 373 | mr_free(searchstr);
|
---|
| 374 |
|
---|
[128] | 375 | if (pclose(fin)) {
|
---|
| 376 | log_OS_error("Cannot pclose fin");
|
---|
| 377 | }
|
---|
| 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]) {
|
---|
[2334] | 517 | mr_asprintf(cdrw_device, "%s", 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);
|
---|
[2383] | 537 | tmp = 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;
|
---|
| 578 | char *output = NULL;
|
---|
[2214] | 579 | char *cdr_exe = NULL;
|
---|
[2325] | 580 | char *phrase_two = NULL;
|
---|
[2266] | 581 | char *command = NULL;
|
---|
| 582 | #ifndef __FreeBSD__
|
---|
| 583 | char *dvd_last_resort = NULL;
|
---|
| 584 | #endif
|
---|
| 585 | char *mountpoint = NULL;
|
---|
[128] | 586 | static char the_last_place_i_found_it[MAX_STR_LEN] = "";
|
---|
[1] | 587 |
|
---|
[128] | 588 | /*@ intialize *************************************************** */
|
---|
[1] | 589 |
|
---|
[2325] | 590 | output = NULL;
|
---|
[2266] | 591 | #ifndef __FreeBSD__
|
---|
[2323] | 592 | mr_asprintf(dvd_last_resort, "%s", "");;
|
---|
[2266] | 593 | #endif
|
---|
[1] | 594 |
|
---|
[128] | 595 | /*@ end vars **************************************************** */
|
---|
[1] | 596 |
|
---|
[128] | 597 | if (g_cdrom_drive_is_here[0] && !isdigit(g_cdrom_drive_is_here[0])) {
|
---|
[2325] | 598 | mr_asprintf(output, "%s", g_cdrom_drive_is_here);
|
---|
[128] | 599 | log_msg(3, "Been there, done that. Returning %s", output);
|
---|
[2325] | 600 | return(output);
|
---|
[128] | 601 | }
|
---|
| 602 | if (the_last_place_i_found_it[0] != '\0' && !try_to_mount) {
|
---|
[2325] | 603 | mr_asprintf(output, "%s", the_last_place_i_found_it);
|
---|
| 604 | log_msg(3, "find_cdrom_device() --- returning last found location - '%s'", output);
|
---|
| 605 | return(output);
|
---|
[128] | 606 | }
|
---|
[1] | 607 |
|
---|
[2323] | 608 | mr_asprintf(mountpoint, "%s/cd.mnt", bkpinfo->tmpdir);
|
---|
[128] | 609 | make_hole_for_dir(mountpoint);
|
---|
[1] | 610 |
|
---|
[2331] | 611 | tmp = find_home_of_exe("cdrecord");
|
---|
| 612 | if (tmp) {
|
---|
[2323] | 613 | mr_asprintf(cdr_exe, "cdrecord");
|
---|
[128] | 614 | } else {
|
---|
[2323] | 615 | mr_asprintf(cdr_exe, "dvdrecord");
|
---|
[128] | 616 | }
|
---|
[2331] | 617 | mr_free(tmp);
|
---|
[2325] | 618 |
|
---|
[2331] | 619 | tmp = find_home_of_exe(cdr_exe);
|
---|
| 620 | if (!tmp) {
|
---|
[2325] | 621 | mr_asprintf(output, "/dev/cdrom");
|
---|
[128] | 622 | log_msg(4, "Can't find cdrecord; assuming %s", output);
|
---|
[2325] | 623 | mr_free(cdr_exe);
|
---|
[128] | 624 | if (!does_device_exist(output)) {
|
---|
| 625 | log_msg(4, "That didn't work. Sorry.");
|
---|
[2325] | 626 | mr_free(output);
|
---|
[128] | 627 | }
|
---|
[2331] | 628 | mr_free(tmp);
|
---|
| 629 | return(output);
|
---|
[128] | 630 | }
|
---|
[2331] | 631 | mr_free(tmp);
|
---|
[128] | 632 |
|
---|
[2323] | 633 | mr_asprintf(command, "%s -scanbus 2> /dev/null", cdr_exe);
|
---|
[128] | 634 | fin = popen(command, "r");
|
---|
[2266] | 635 | log_msg(4, "command=%s", command);
|
---|
| 636 | mr_free(command);
|
---|
| 637 |
|
---|
[128] | 638 | if (!fin) {
|
---|
| 639 | log_OS_error("Cannot popen command");
|
---|
[2214] | 640 | mr_free(cdr_exe);
|
---|
[2325] | 641 | return(NULL);
|
---|
[128] | 642 | }
|
---|
[2266] | 643 |
|
---|
[2351] | 644 | for (mr_getline(tmp, fin); !feof(fin); mr_getline(tmp, fin)) {
|
---|
[128] | 645 | p = strchr(tmp, '\'');
|
---|
| 646 | if (p) {
|
---|
| 647 | q = strchr(++p, '\'');
|
---|
| 648 | if (q) {
|
---|
| 649 | for (r = q; *(r - 1) == ' '; r--);
|
---|
| 650 | *r = '\0';
|
---|
| 651 | p = strchr(++q, '\'');
|
---|
| 652 | if (p) {
|
---|
| 653 | q = strchr(++p, '\'');
|
---|
| 654 | if (q) {
|
---|
| 655 | while (*(q - 1) == ' ') {
|
---|
| 656 | q--;
|
---|
| 657 | }
|
---|
| 658 | *q = '\0';
|
---|
[2330] | 659 | mr_asprintf(phrase_two, "%s", p);
|
---|
[128] | 660 | }
|
---|
| 661 | }
|
---|
[1] | 662 | }
|
---|
| 663 | }
|
---|
[2351] | 664 | mr_free(tmp);
|
---|
[1] | 665 | }
|
---|
[2351] | 666 | mr_free(tmp);
|
---|
[128] | 667 | paranoid_pclose(fin);
|
---|
[1] | 668 |
|
---|
| 669 | #ifndef __FreeBSD__
|
---|
[2330] | 670 | if (!phrase_two || strlen(phrase_two) == 0) {
|
---|
[128] | 671 | log_msg(4, "Not running phase two. String is empty.");
|
---|
| 672 | } else {
|
---|
[2323] | 673 | mr_asprintf(command, "dmesg | grep \"%s\" 2> /dev/null", phrase_two);
|
---|
[128] | 674 | fin = popen(command, "r");
|
---|
[2266] | 675 | mr_free(command);
|
---|
| 676 |
|
---|
[128] | 677 | if (!fin) {
|
---|
| 678 | log_msg(4, "Cannot run 2nd command - non-fatal, fortunately");
|
---|
| 679 | } else {
|
---|
[2351] | 680 | for (mr_getline(tmp, fin); !feof(fin); mr_getline(tmp, fin)) {
|
---|
[128] | 681 | log_msg(5, "--> '%s'", tmp);
|
---|
| 682 | if (tmp[0] != ' ' && tmp[1] != ' ') {
|
---|
| 683 | p = strchr(tmp, ':');
|
---|
| 684 | if (p) {
|
---|
| 685 | *p = '\0';
|
---|
| 686 | if (strstr(tmp, "DVD")) {
|
---|
[2266] | 687 | mr_free(dvd_last_resort);
|
---|
[2323] | 688 | mr_asprintf(dvd_last_resort, "/dev/%s", tmp);
|
---|
[2325] | 689 | log_msg(4, "Ignoring '%s' because it's a DVD drive", tmp);
|
---|
[128] | 690 | } else {
|
---|
[2325] | 691 | mr_asprintf(output, "/dev/%s", tmp);
|
---|
[128] | 692 | found_it = TRUE;
|
---|
| 693 | }
|
---|
| 694 | }
|
---|
| 695 | }
|
---|
[2351] | 696 | mr_free(tmp);
|
---|
[128] | 697 | }
|
---|
| 698 | paranoid_pclose(fin);
|
---|
[2351] | 699 | mr_free(tmp);
|
---|
[128] | 700 | }
|
---|
| 701 | }
|
---|
[2330] | 702 | mr_free(phrase_two);
|
---|
[1] | 703 |
|
---|
| 704 | #endif
|
---|
| 705 | #ifdef __FreeBSD__
|
---|
[128] | 706 | if (!found_it) {
|
---|
| 707 | log_msg(4, "OK, approach 2");
|
---|
[2325] | 708 | if (!(output = set_dev_to_this_if_rx_OK("/dev/cdrom"))) {
|
---|
| 709 | if (!(output = set_dev_to_this_if_rx_OK("/dev/cdrom1"))) {
|
---|
| 710 | if (!(output = set_dev_to_this_if_rx_OK("/dev/dvd"))) {
|
---|
| 711 | if (!(output = set_dev_to_this_if_rx_OK("/dev/acd0"))) {
|
---|
| 712 | if (!(output = set_dev_to_this_if_rx_OK("/dev/cd01"))) {
|
---|
| 713 | if (!(output = set_dev_to_this_if_rx_OK("/dev/acd1"))) {
|
---|
| 714 | if (!(output = set_dev_to_this_if_rx_OK("/dev/cd1"))) {
|
---|
| 715 | mr_free(cdr_exe);
|
---|
| 716 | return(NULL);
|
---|
[128] | 717 | }
|
---|
| 718 | }
|
---|
| 719 | }
|
---|
| 720 | }
|
---|
| 721 | }
|
---|
| 722 | }
|
---|
| 723 | }
|
---|
| 724 | }
|
---|
[1] | 725 | #else
|
---|
[128] | 726 | if (!found_it && strlen(dvd_last_resort) > 0) {
|
---|
[2266] | 727 | log_msg(4, "Well, I'll use the DVD - %s - as a last resort", dvd_last_resort);
|
---|
[2325] | 728 | mr_asprintf(output, "%s", dvd_last_resort);
|
---|
[128] | 729 | found_it = TRUE;
|
---|
[1] | 730 | }
|
---|
[2266] | 731 | mr_free(dvd_last_resort);
|
---|
[2325] | 732 |
|
---|
[128] | 733 | if (found_it) {
|
---|
[2351] | 734 | mr_asprintf(tmp, "grep \"%s=ide-scsi\" " CMDLINE " &> /dev/null", strrchr(output, '/') + 1);
|
---|
| 735 | if (system(tmp) == 0) {
|
---|
[2325] | 736 | log_msg(4, "%s is not right. It's being SCSI-emulated. Continuing.", output);
|
---|
[128] | 737 | found_it = FALSE;
|
---|
[2325] | 738 | mr_free(output);
|
---|
[128] | 739 | }
|
---|
[2351] | 740 | mr_free(tmp);
|
---|
[128] | 741 | }
|
---|
[1] | 742 |
|
---|
[128] | 743 | if (found_it) {
|
---|
| 744 | log_msg(4, "(find_cdrom_device) --> '%s'", output);
|
---|
| 745 | if (!does_device_exist(output)) {
|
---|
| 746 | found_it = FALSE;
|
---|
| 747 | log_msg(4, "OK, I was wrong, I haven't found it... yet.");
|
---|
[2325] | 748 | mr_free(output);
|
---|
[128] | 749 | }
|
---|
| 750 | }
|
---|
[1] | 751 |
|
---|
[128] | 752 | if (!found_it) {
|
---|
| 753 | log_msg(4, "OK, approach 2");
|
---|
[2325] | 754 | if (!(output = set_dev_to_this_if_rx_OK("/dev/scd0"))) {
|
---|
| 755 | if (!(output = set_dev_to_this_if_rx_OK("/dev/sr0"))) {
|
---|
| 756 | if (!(output = set_dev_to_this_if_rx_OK("/dev/cdrom"))) {
|
---|
| 757 | if (!(output = set_dev_to_this_if_rx_OK("/dev/cdrom0"))) {
|
---|
| 758 | if (!(output = set_dev_to_this_if_rx_OK("/dev/cdrom1"))) {
|
---|
| 759 | if (!(output = set_dev_to_this_if_rx_OK("/dev/sr1"))) {
|
---|
| 760 | if (!(output = set_dev_to_this_if_rx_OK("/dev/dvd"))) {
|
---|
| 761 | if (!(output = set_dev_to_this_if_rx_OK(g_cdrw_drive_is_here))) {
|
---|
| 762 | mr_free(cdr_exe);
|
---|
| 763 | return(NULL);
|
---|
[128] | 764 | }
|
---|
| 765 | }
|
---|
| 766 | }
|
---|
| 767 | }
|
---|
| 768 | }
|
---|
| 769 | }
|
---|
| 770 | }
|
---|
| 771 | }
|
---|
| 772 | }
|
---|
[1] | 773 | #endif
|
---|
| 774 |
|
---|
[128] | 775 | if (found_it && try_to_mount) {
|
---|
| 776 | if (mount_CDROM_here(output, mountpoint)) {
|
---|
| 777 | log_msg(4, "[Cardigans] I've changed my mind");
|
---|
| 778 | found_it = FALSE;
|
---|
[2325] | 779 | mr_free(output);
|
---|
[128] | 780 | } else {
|
---|
[2351] | 781 | mr_asprintf(tmp, "%s/archives", mountpoint);
|
---|
[128] | 782 | if (!does_file_exist(tmp)) {
|
---|
| 783 | log_msg(4, "[Cardigans] I'll take it back");
|
---|
| 784 | found_it = FALSE;
|
---|
[2325] | 785 | mr_free(output);
|
---|
[128] | 786 | } else {
|
---|
[2323] | 787 | mr_asprintf(command, "umount %s", output);
|
---|
[128] | 788 | paranoid_system(command);
|
---|
[2266] | 789 | mr_free(command);
|
---|
| 790 |
|
---|
[128] | 791 | log_msg(4, "I'm confident the Mondo CD is in %s", output);
|
---|
| 792 | }
|
---|
[2351] | 793 | mr_free(tmp);
|
---|
[128] | 794 | }
|
---|
| 795 | }
|
---|
| 796 | unlink(mountpoint);
|
---|
[2266] | 797 | mr_free(mountpoint);
|
---|
[1] | 798 |
|
---|
[128] | 799 | if (found_it) {
|
---|
| 800 | if (!does_file_exist(output)) {
|
---|
| 801 | log_msg(3, "I still haven't found it.");
|
---|
[2325] | 802 | mr_free(cdr_exe);
|
---|
| 803 | mr_free(output);
|
---|
| 804 | return (NULL);
|
---|
[128] | 805 | }
|
---|
| 806 | log_msg(3, "(find_cdrom_device) --> '%s'", output);
|
---|
| 807 | strcpy(the_last_place_i_found_it, output);
|
---|
| 808 | strcpy(g_cdrom_drive_is_here, output);
|
---|
[2325] | 809 | mr_free(cdr_exe);
|
---|
| 810 | return (output);
|
---|
[128] | 811 | }
|
---|
[1] | 812 |
|
---|
[2323] | 813 | mr_asprintf(command, "%s -scanbus | grep \"[0-9],[0-9],[0-9]\" | grep \"[D|C][V|D]\" | grep -n \"\" | grep \"%s\" | cut -d':' -f2", cdr_exe, g_cdrw_drive_is_here);
|
---|
[2325] | 814 | mr_free(cdr_exe);
|
---|
| 815 |
|
---|
[128] | 816 | log_msg(1, "command=%s", command);
|
---|
[2383] | 817 | tmp = call_program_and_get_last_line_of_output(command);
|
---|
[2266] | 818 | mr_free(command);
|
---|
| 819 |
|
---|
[2325] | 820 | mr_free(output);
|
---|
[2351] | 821 | if (strlen(tmp) > 0) {
|
---|
| 822 | mr_asprintf(output, "%s", tmp);
|
---|
[128] | 823 | log_msg(4, "Finally found it at %s", output);
|
---|
| 824 | } else {
|
---|
| 825 | log_msg(4, "Still couldn't find it.");
|
---|
| 826 | }
|
---|
[2351] | 827 | mr_free(tmp);
|
---|
[2214] | 828 |
|
---|
[2325] | 829 | return (output);
|
---|
[1] | 830 | }
|
---|
| 831 |
|
---|
| 832 |
|
---|
[2325] | 833 | char *find_dvd_device()
|
---|
[1] | 834 | {
|
---|
[2266] | 835 | char *tmp = NULL;
|
---|
[2325] | 836 | int devno = -1;
|
---|
| 837 | char *output = NULL;
|
---|
[1] | 838 |
|
---|
[128] | 839 | if (g_dvd_drive_is_here[0]) {
|
---|
[2334] | 840 | mr_asprintf(output, "%s", g_dvd_drive_is_here);
|
---|
[128] | 841 | log_msg(3, "Been there, done that. Returning %s", output);
|
---|
[2325] | 842 | return (output);
|
---|
[128] | 843 | }
|
---|
[1] | 844 |
|
---|
[2383] | 845 | tmp = call_program_and_get_last_line_of_output("dvdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1");
|
---|
[128] | 846 | log_msg(5, "tmp = '%s'", tmp);
|
---|
| 847 | if (!tmp[0])
|
---|
[2266] | 848 | mr_free(tmp);
|
---|
[2383] | 849 | tmp = call_program_and_get_last_line_of_output("cdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1");
|
---|
[128] | 850 | if (tmp[0]) {
|
---|
| 851 | devno = atoi(tmp) - 1;
|
---|
| 852 | }
|
---|
[2266] | 853 | mr_free(tmp);
|
---|
| 854 |
|
---|
[128] | 855 | if (devno >= 0) {
|
---|
[2325] | 856 | mr_asprintf(output, "/dev/scd%d", devno);
|
---|
[128] | 857 | strcpy(g_dvd_drive_is_here, output);
|
---|
| 858 | log_msg(2, "I think DVD is at %s", output);
|
---|
| 859 | } else {
|
---|
| 860 | log_msg(2, "I cannot find DVD");
|
---|
| 861 | }
|
---|
[1] | 862 |
|
---|
[2325] | 863 | return (output);
|
---|
[1] | 864 | }
|
---|
| 865 |
|
---|
| 866 |
|
---|
| 867 |
|
---|
| 868 |
|
---|
| 869 |
|
---|
| 870 | #include <sys/ioctl.h>
|
---|
| 871 |
|
---|
| 872 | /**
|
---|
| 873 | * Find the size of the specified @p drive, in megabytes. Uses @c ioctl calls
|
---|
| 874 | * and @c dmesg.
|
---|
| 875 | * @param drive The device to find the size of.
|
---|
| 876 | * @return size in megabytes.
|
---|
| 877 | */
|
---|
[128] | 878 | long get_phys_size_of_drive(char *drive)
|
---|
[1] | 879 | {
|
---|
[128] | 880 | int fd;
|
---|
[1] | 881 | #if linux
|
---|
[128] | 882 | unsigned long long s = 0;
|
---|
| 883 | int fileid, cylinders = 0, cylindersleft = 0;
|
---|
| 884 | int cylindersize = 0;
|
---|
[1] | 885 | int gotgeo = 0;
|
---|
| 886 |
|
---|
[128] | 887 |
|
---|
| 888 | struct hd_geometry hdgeo;
|
---|
[1] | 889 | #elif __FreeBSD__
|
---|
[128] | 890 | off_t s;
|
---|
[1] | 891 | #endif
|
---|
| 892 |
|
---|
[128] | 893 | long outvalA = -1;
|
---|
| 894 | long outvalB = -1;
|
---|
| 895 | long outvalC = -1;
|
---|
[1] | 896 |
|
---|
[128] | 897 | if ((fd = open(drive, O_RDONLY)) != -1) {
|
---|
| 898 | if (ioctl(fd,
|
---|
[1] | 899 | #if linux
|
---|
[128] | 900 | #ifdef BLKGETSIZE64
|
---|
| 901 | BLKGETSIZE64,
|
---|
| 902 | #else
|
---|
| 903 | BLKGETSIZE,
|
---|
| 904 | #endif
|
---|
[1] | 905 | #elif __FreeBSD__
|
---|
[128] | 906 | DIOCGMEDIASIZE,
|
---|
[1] | 907 | #endif
|
---|
[128] | 908 | &s) != -1) {
|
---|
| 909 | close(fd);
|
---|
| 910 | // s>>11 works for older disks but not for newer ones
|
---|
| 911 | outvalB =
|
---|
[1] | 912 | #if linux
|
---|
[128] | 913 | #ifdef BLKGETSIZE64
|
---|
| 914 | s >> 20
|
---|
[1] | 915 | #else
|
---|
[128] | 916 | s >> 11
|
---|
[1] | 917 | #endif
|
---|
[128] | 918 | #else
|
---|
| 919 | s >> 20
|
---|
| 920 | #endif
|
---|
| 921 | ;
|
---|
| 922 | }
|
---|
[1] | 923 | }
|
---|
| 924 |
|
---|
[128] | 925 | if (outvalB <= 0) {
|
---|
| 926 | log_msg(1, "Error getting size of %s: %s", drive, strerror(errno));
|
---|
[1] | 927 | #if linux
|
---|
[161] | 928 | fileid = open(drive, O_RDONLY);
|
---|
[2205] | 929 | if (fileid != -1) {
|
---|
[161] | 930 | if (ioctl(fileid, HDIO_GETGEO, &hdgeo) != -1) {
|
---|
| 931 | if (hdgeo.cylinders && hdgeo.heads && hdgeo.sectors) {
|
---|
| 932 | cylindersleft = cylinders = hdgeo.cylinders;
|
---|
| 933 | cylindersize = hdgeo.heads * hdgeo.sectors / 2;
|
---|
| 934 | outvalA = cylindersize * cylinders / 1024;
|
---|
| 935 | log_msg(2, "Got Harddisk geometry, C:%d, H:%d, S:%d",
|
---|
| 936 | hdgeo.cylinders, hdgeo.heads, hdgeo.sectors);
|
---|
| 937 | gotgeo = 1;
|
---|
| 938 | } else {
|
---|
| 939 | log_msg(1, "Harddisk geometry wrong");
|
---|
| 940 | }
|
---|
[1] | 941 | } else {
|
---|
[161] | 942 | log_msg(1,
|
---|
| 943 | "Error in ioctl() getting new hard disk geometry (%s), resizing in unsafe mode",
|
---|
| 944 | strerror(errno));
|
---|
[1] | 945 | }
|
---|
[161] | 946 | close(fileid);
|
---|
[1] | 947 | } else {
|
---|
[161] | 948 | log_msg(1, "Failed to open %s for reading: %s", drive,
|
---|
[128] | 949 | strerror(errno));
|
---|
[1] | 950 | }
|
---|
[161] | 951 | if (!gotgeo) {
|
---|
| 952 | log_msg(1, "Failed to get harddisk geometry, using old mode");
|
---|
| 953 | }
|
---|
[1] | 954 | #endif
|
---|
[161] | 955 | }
|
---|
[1] | 956 | // OLDER DISKS will give ridiculously low value for outvalB (so outvalA is returned) :)
|
---|
| 957 | // NEWER DISKS will give sane value for outvalB (close to outvalA, in other words) :)
|
---|
| 958 |
|
---|
[128] | 959 | outvalC = (outvalA > outvalB) ? outvalA : outvalB;
|
---|
| 960 |
|
---|
[1] | 961 | // log_msg (5, "drive = %s, error = %s", drive, strerror (errno));
|
---|
| 962 | // fatal_error ("GPSOD: Unable to get size of drive");
|
---|
[128] | 963 | log_msg(1, "%s --> %ld or %ld --> %ld", drive, outvalA, outvalB,
|
---|
| 964 | outvalC);
|
---|
| 965 |
|
---|
| 966 | return (outvalC);
|
---|
[1] | 967 | }
|
---|
[128] | 968 |
|
---|
[1] | 969 | /**
|
---|
| 970 | * Determine whether @p format is supported by the kernel. Uses /proc/filesystems
|
---|
| 971 | * under Linux and @c lsvfs under FreeBSD.
|
---|
| 972 | * @param format The format to test.
|
---|
| 973 | * @return TRUE if the format is supported, FALSE if not.
|
---|
| 974 | */
|
---|
[128] | 975 | bool is_this_a_valid_disk_format(char *format)
|
---|
[1] | 976 | {
|
---|
[2211] | 977 | char *good_formats = NULL;
|
---|
[2266] | 978 | char *command = NULL;
|
---|
| 979 | char *format_sz = NULL;
|
---|
[1] | 980 |
|
---|
[128] | 981 | FILE *pin;
|
---|
| 982 | int retval;
|
---|
[1] | 983 |
|
---|
[128] | 984 | assert_string_is_neither_NULL_nor_zerolength(format);
|
---|
[1] | 985 |
|
---|
[2323] | 986 | mr_asprintf(format_sz, "%s ", format);
|
---|
[1] | 987 |
|
---|
| 988 | #ifdef __FreeBSD__
|
---|
[2323] | 989 | mr_asprintf(command, "lsvfs | tr -s '\t' ' ' | grep -v Filesys | grep -v -- -- | cut -d' ' -f1 | tr -s '\n' ' '");
|
---|
[1] | 990 | #else
|
---|
[2323] | 991 | mr_asprintf(command, "grep -v nodev /proc/filesystems | tr -s '\t' ' ' | cut -d' ' -f2 | tr -s '\n' ' '");
|
---|
[1] | 992 | #endif
|
---|
| 993 |
|
---|
[128] | 994 | pin = popen(command, "r");
|
---|
[2266] | 995 | mr_free(command);
|
---|
| 996 |
|
---|
[128] | 997 | if (!pin) {
|
---|
| 998 | log_OS_error("Unable to read good formats");
|
---|
| 999 | retval = 0;
|
---|
| 1000 | } else {
|
---|
[2331] | 1001 | mr_getline(good_formats, pin);
|
---|
[128] | 1002 | if (pclose(pin)) {
|
---|
| 1003 | log_OS_error("Cannot pclose good formats");
|
---|
| 1004 | }
|
---|
[2316] | 1005 | mr_strip_spaces(good_formats);
|
---|
| 1006 | mr_strcat(good_formats, " swap lvm raid ntfs-3g ntfs 7 "); // " ntfs 7 " -- um, cheating much? :)
|
---|
[128] | 1007 | if (strstr(good_formats, format_sz)) {
|
---|
| 1008 | retval = 1;
|
---|
| 1009 | } else {
|
---|
| 1010 | retval = 0;
|
---|
| 1011 | }
|
---|
[2316] | 1012 | mr_free(good_formats);
|
---|
[128] | 1013 | }
|
---|
[2266] | 1014 | mr_free(format_sz);
|
---|
| 1015 |
|
---|
[128] | 1016 | return (retval);
|
---|
[1] | 1017 | }
|
---|
| 1018 |
|
---|
| 1019 |
|
---|
| 1020 | /** @def SWAPLIST_COMMAND The command to list the swap files/partitions in use. */
|
---|
| 1021 |
|
---|
| 1022 | /**
|
---|
| 1023 | * Determine whether @p device_raw is currently mounted.
|
---|
| 1024 | * @param device_raw The device to check.
|
---|
| 1025 | * @return TRUE if it's mounted, FALSE if not.
|
---|
| 1026 | */
|
---|
[128] | 1027 | bool is_this_device_mounted(char *device_raw)
|
---|
[1] | 1028 | {
|
---|
| 1029 |
|
---|
[128] | 1030 | /*@ pointers **************************************************** */
|
---|
| 1031 | FILE *fin;
|
---|
[1] | 1032 |
|
---|
[128] | 1033 | /*@ buffers ***************************************************** */
|
---|
[2351] | 1034 | char *incoming = NULL;
|
---|
[2214] | 1035 | char *device_with_tab = NULL;
|
---|
| 1036 | char *device_with_space = NULL;
|
---|
| 1037 | char *tmp = NULL;
|
---|
| 1038 | bool retval = FALSE;
|
---|
[128] | 1039 |
|
---|
[1] | 1040 | #ifdef __FreeBSD__
|
---|
[128] | 1041 | #define SWAPLIST_COMMAND "swapinfo"
|
---|
[1] | 1042 | #else
|
---|
[128] | 1043 | #define SWAPLIST_COMMAND "cat /proc/swaps"
|
---|
[1] | 1044 | #endif
|
---|
| 1045 |
|
---|
[128] | 1046 | /*@ end vars **************************************************** */
|
---|
[1] | 1047 |
|
---|
[2325] | 1048 | if (device_raw == NULL) {
|
---|
| 1049 | return(FALSE);
|
---|
| 1050 | }
|
---|
| 1051 |
|
---|
[128] | 1052 | if (device_raw[0] != '/' && !strstr(device_raw, ":/")) {
|
---|
| 1053 | log_msg(1, "%s needs to have a '/' prefixed - I'll do it",
|
---|
| 1054 | device_raw);
|
---|
[2323] | 1055 | mr_asprintf(tmp, "/%s", device_raw);
|
---|
[128] | 1056 | } else {
|
---|
[2323] | 1057 | mr_asprintf(tmp, "%s", device_raw);
|
---|
[128] | 1058 | }
|
---|
| 1059 | log_msg(1, "Is %s mounted?", tmp);
|
---|
| 1060 | if (!strcmp(tmp, "/proc") || !strcmp(tmp, "proc")) {
|
---|
| 1061 | log_msg(1,
|
---|
| 1062 | "I don't know how the heck /proc made it into the mountlist. I'll ignore it.");
|
---|
[2214] | 1063 | mr_free(tmp);
|
---|
| 1064 | return(FALSE);
|
---|
[128] | 1065 | }
|
---|
[2323] | 1066 | mr_asprintf(device_with_tab, "%s\t", tmp);
|
---|
| 1067 | mr_asprintf(device_with_space, "%s ", tmp);
|
---|
[2214] | 1068 | mr_free(tmp);
|
---|
[1] | 1069 |
|
---|
[128] | 1070 | if (!(fin = popen("mount", "r"))) {
|
---|
| 1071 | log_OS_error("Cannot popen 'mount'");
|
---|
[2214] | 1072 | return(FALSE);
|
---|
[1] | 1073 | }
|
---|
[2351] | 1074 |
|
---|
| 1075 | for (mr_getline(incoming, fin); !feof(fin); mr_getline(incoming, fin)) {
|
---|
| 1076 | if (strstr(incoming, device_with_space) || strstr(incoming, device_with_tab)) {
|
---|
[128] | 1077 | paranoid_pclose(fin);
|
---|
[2351] | 1078 | mr_free(incoming);
|
---|
[2214] | 1079 | return(TRUE);
|
---|
[128] | 1080 | }
|
---|
[2351] | 1081 | mr_free(incoming);
|
---|
[128] | 1082 | }
|
---|
[2351] | 1083 | mr_free(incoming);
|
---|
[2214] | 1084 | mr_free(device_with_tab);
|
---|
[128] | 1085 | paranoid_pclose(fin);
|
---|
[2323] | 1086 | mr_asprintf(tmp, "%s | grep -E \"^%s\" > /dev/null 2> /dev/null", SWAPLIST_COMMAND, device_with_space);
|
---|
[2428] | 1087 | mr_free(device_with_space);
|
---|
[128] | 1088 | log_msg(4, "tmp (command) = '%s'", tmp);
|
---|
| 1089 | if (!system(tmp)) {
|
---|
[2214] | 1090 | retval = TRUE;
|
---|
[128] | 1091 | }
|
---|
[2214] | 1092 | mr_free(tmp);
|
---|
| 1093 | return(retval);
|
---|
[1] | 1094 | }
|
---|
| 1095 |
|
---|
| 1096 | #ifdef __FreeBSD__
|
---|
| 1097 | // CODE IS FREEBSD-SPECIFIC
|
---|
| 1098 | /**
|
---|
| 1099 | * Create a loopback device for specified @p fname.
|
---|
| 1100 | * @param fname The file to associate with a device.
|
---|
| 1101 | * @return /dev entry for the device, or NULL if it couldn't be allocated.
|
---|
| 1102 | */
|
---|
[128] | 1103 | char *make_vn(char *fname)
|
---|
[1] | 1104 | {
|
---|
[128] | 1105 | char *device = (char *) malloc(MAX_STR_LEN);
|
---|
| 1106 | char *mddevice = (char *) malloc(32);
|
---|
| 1107 | char command[MAX_STR_LEN];
|
---|
[2383] | 1108 | char *tmp = NULL;
|
---|
[128] | 1109 | int vndev = 2;
|
---|
[2383] | 1110 | int i = 2;
|
---|
| 1111 |
|
---|
[2428] | 1112 | tmp = call_program_and_get_last_line_of_output("/sbin/sysctl -n kern.osreldate");
|
---|
[2383] | 1113 | i = atoi(tmp);
|
---|
| 1114 | mr_free(tmp);
|
---|
| 1115 |
|
---|
| 1116 | if (i < 500000) {
|
---|
[128] | 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;
|
---|
[2383] | 1149 | char *tmp = NULL;
|
---|
[2266] | 1150 | int res = 0;
|
---|
[2383] | 1151 | int i = 0;
|
---|
[1] | 1152 |
|
---|
[128] | 1153 | if (strncmp(dname, "/dev/", 5) == 0) {
|
---|
| 1154 | dname += 5;
|
---|
| 1155 | }
|
---|
[1] | 1156 |
|
---|
[2383] | 1157 | tmp = call_program_and_get_last_line_of_output("/sbin/sysctl -n kern.osreldate"));
|
---|
| 1158 | i = atoi(tmp);
|
---|
| 1159 | mr_free(tmp);
|
---|
| 1160 |
|
---|
| 1161 | if (i < 500000) {
|
---|
[2323] | 1162 | mr_asprintf(command, "vnconfig -d %s", dname);
|
---|
[128] | 1163 | } else {
|
---|
[2323] | 1164 | mr_asprintf(command, "mdconfig -d -u %s", dname);
|
---|
[128] | 1165 | }
|
---|
[2266] | 1166 | res = system(command);
|
---|
| 1167 | mr_free(command);
|
---|
| 1168 | return(res);
|
---|
[1] | 1169 | }
|
---|
| 1170 | #endif
|
---|
| 1171 |
|
---|
| 1172 |
|
---|
| 1173 | /**
|
---|
| 1174 | * Mount the CD-ROM at @p mountpoint.
|
---|
| 1175 | * @param device The device (or file if g_ISO_restore_mode) to mount.
|
---|
| 1176 | * @param mountpoint The place to mount it.
|
---|
| 1177 | * @return 0 for success, nonzero for failure.
|
---|
| 1178 | */
|
---|
[1741] | 1179 | int mount_USB_here(char *device, char *mountpoint)
|
---|
| 1180 | {
|
---|
| 1181 | /*@ buffer ****************************************************** */
|
---|
[2266] | 1182 | char *command = NULL;
|
---|
[1741] | 1183 | int retval;
|
---|
| 1184 |
|
---|
| 1185 | assert_string_is_neither_NULL_nor_zerolength(device);
|
---|
| 1186 | assert_string_is_neither_NULL_nor_zerolength(mountpoint);
|
---|
| 1187 |
|
---|
| 1188 | make_hole_for_dir(mountpoint);
|
---|
| 1189 | if (isdigit(device[0])) {
|
---|
| 1190 | return(1);
|
---|
| 1191 | }
|
---|
[2325] | 1192 | log_msg(4, "(mount_USB_here --- device=%s, mountpoint=%s", device, mountpoint);
|
---|
[1741] | 1193 |
|
---|
| 1194 | #ifdef __FreeBSD__
|
---|
[2323] | 1195 | mr_asprintf(command, "mount_vfat %s %s 2>> %s", device, mountpoint, MONDO_LOGFILE);
|
---|
[1741] | 1196 |
|
---|
| 1197 | #else
|
---|
[2323] | 1198 | mr_asprintf(command, "mount %s -t vfat %s 2>> %s", device, mountpoint, MONDO_LOGFILE);
|
---|
[1741] | 1199 | #endif
|
---|
| 1200 |
|
---|
| 1201 | log_msg(4, command);
|
---|
| 1202 | retval = system(command);
|
---|
| 1203 | log_msg(1, "system(%s) returned %d", command, retval);
|
---|
[2266] | 1204 | mr_free(command);
|
---|
[1741] | 1205 |
|
---|
| 1206 | return (retval);
|
---|
| 1207 | }
|
---|
| 1208 |
|
---|
| 1209 | /**
|
---|
| 1210 | * Mount the CD-ROM at @p mountpoint.
|
---|
| 1211 | * @param device The device (or file if g_ISO_restore_mode) to mount.
|
---|
| 1212 | * @param mountpoint The place to mount it.
|
---|
| 1213 | * @return 0 for success, nonzero for failure.
|
---|
| 1214 | */
|
---|
[2325] | 1215 | int mount_CDROM_here(char *device, const char *mountpoint)
|
---|
[1] | 1216 | {
|
---|
[128] | 1217 | /*@ buffer ****************************************************** */
|
---|
[2211] | 1218 | char *command = NULL;
|
---|
[128] | 1219 | int retval;
|
---|
[2257] | 1220 | #ifdef __FreeBSD__
|
---|
| 1221 | char *dev = NULL;
|
---|
| 1222 | #else
|
---|
| 1223 | char *options = NULL;
|
---|
| 1224 | #endif
|
---|
[1] | 1225 |
|
---|
[128] | 1226 | assert_string_is_neither_NULL_nor_zerolength(mountpoint);
|
---|
[1] | 1227 |
|
---|
[128] | 1228 | make_hole_for_dir(mountpoint);
|
---|
[2211] | 1229 |
|
---|
[2325] | 1230 | if ((device == NULL) || (isdigit(device[0]))) {
|
---|
| 1231 | mr_free(device);
|
---|
| 1232 | device = find_cdrom_device(FALSE);
|
---|
[2257] | 1233 | }
|
---|
| 1234 | #ifndef __FreeBSD__
|
---|
[2323] | 1235 | mr_asprintf(options, "ro");
|
---|
[2211] | 1236 | #endif
|
---|
| 1237 |
|
---|
[128] | 1238 | if (g_ISO_restore_mode) {
|
---|
[1] | 1239 |
|
---|
| 1240 | #ifdef __FreeBSD__
|
---|
[2323] | 1241 | mr_asprintf(dev, "%s", make_vn(device));
|
---|
[128] | 1242 | if (!dev) {
|
---|
[2323] | 1243 | mr_asprintf(command, "Unable to mount ISO (make_vn(%s) failed)", device);
|
---|
[128] | 1244 | fatal_error(command);
|
---|
| 1245 | }
|
---|
[2325] | 1246 | mr_free(device);
|
---|
| 1247 | device = dev;
|
---|
[1] | 1248 | #else
|
---|
[2211] | 1249 | mr_strcat(options, ",loop");
|
---|
[1] | 1250 | #endif
|
---|
| 1251 |
|
---|
[128] | 1252 | }
|
---|
[2325] | 1253 | log_msg(4, "(mount_CDROM_here --- device=%s, mountpoint=%s", device, mountpoint);
|
---|
[128] | 1254 | /*@ end vars *************************************************** */
|
---|
[1] | 1255 |
|
---|
| 1256 | #ifdef __FreeBSD__
|
---|
[2323] | 1257 | mr_asprintf(command, "mount_cd9660 -r %s %s 2>> %s", device, mountpoint, MONDO_LOGFILE);
|
---|
[1] | 1258 |
|
---|
| 1259 | #else
|
---|
[2323] | 1260 | mr_asprintf(command, "mount %s -o %s -t iso9660 %s 2>> %s", device, options, mountpoint, MONDO_LOGFILE);
|
---|
[2211] | 1261 | paranoid_free(options);
|
---|
[1] | 1262 | #endif
|
---|
| 1263 |
|
---|
[128] | 1264 | log_msg(4, command);
|
---|
| 1265 | if (strncmp(device, "/dev/", 5) == 0) {
|
---|
| 1266 | retract_CD_tray_and_defeat_autorun();
|
---|
| 1267 | }
|
---|
| 1268 | retval = system(command);
|
---|
| 1269 | log_msg(1, "system(%s) returned %d", command, retval);
|
---|
[2325] | 1270 | mr_free(command);
|
---|
[1] | 1271 |
|
---|
[128] | 1272 | return (retval);
|
---|
[1] | 1273 | }
|
---|
| 1274 |
|
---|
| 1275 |
|
---|
| 1276 |
|
---|
| 1277 |
|
---|
| 1278 |
|
---|
| 1279 |
|
---|
| 1280 | /**
|
---|
| 1281 | * Ask the user for CD number @p cd_number_i_want.
|
---|
| 1282 | * Sets g_current_media_number once the correct CD is inserted.
|
---|
| 1283 | * @param bkpinfo The backup information structure. Fields used:
|
---|
| 1284 | * - @c bkpinfo->backup_media_type
|
---|
[20] | 1285 | * - @c bkpinfo->prefix
|
---|
[1] | 1286 | * - @c bkpinfo->isodir
|
---|
| 1287 | * - @c bkpinfo->media_device
|
---|
| 1288 | * - @c bkpinfo->please_dont_eject_when_restoring
|
---|
| 1289 | * @param cd_number_i_want The CD number to ask for.
|
---|
| 1290 | */
|
---|
| 1291 | void
|
---|
[1645] | 1292 | insist_on_this_cd_number(int cd_number_i_want)
|
---|
[1] | 1293 | {
|
---|
| 1294 |
|
---|
[128] | 1295 | /*@ int ************************************************************* */
|
---|
| 1296 | int res = 0;
|
---|
[1] | 1297 |
|
---|
| 1298 |
|
---|
[128] | 1299 | /*@ buffers ********************************************************* */
|
---|
[2266] | 1300 | char *tmp = NULL;
|
---|
[2242] | 1301 | char *mds = NULL;
|
---|
[2266] | 1302 | char *request = NULL;
|
---|
[1] | 1303 |
|
---|
[128] | 1304 | assert(bkpinfo != NULL);
|
---|
| 1305 | assert(cd_number_i_want > 0);
|
---|
[1] | 1306 |
|
---|
| 1307 | // log_msg(3, "Insisting on CD number %d", cd_number_i_want);
|
---|
| 1308 |
|
---|
[128] | 1309 | if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
|
---|
| 1310 | log_msg(3,
|
---|
| 1311 | "No need to insist_on_this_cd_number when the backup type isn't CD-R(W) or NFS or ISO");
|
---|
| 1312 | return;
|
---|
| 1313 | }
|
---|
[2323] | 1314 | mr_asprintf(tmp, "mkdir -p " MNT_CDROM);
|
---|
[128] | 1315 | run_program_and_log_output(tmp, 5);
|
---|
[2266] | 1316 | mr_free(tmp);
|
---|
| 1317 |
|
---|
[2382] | 1318 | if (g_ISO_restore_mode || bkpinfo->backup_media_type == iso || bkpinfo->backup_media_type == netfs) {
|
---|
[2325] | 1319 | // BERLIOS --- I'm tempted to do something about this...
|
---|
| 1320 | // Why unmount and remount again and again?
|
---|
[128] | 1321 | log_msg(3, "Remounting CD");
|
---|
| 1322 | g_ISO_restore_mode = TRUE;
|
---|
| 1323 | if (is_this_device_mounted(MNT_CDROM)) {
|
---|
| 1324 | run_program_and_log_output("umount " MNT_CDROM, 5);
|
---|
| 1325 | }
|
---|
[2323] | 1326 | mr_asprintf(tmp, "mkdir -p %s/isodir &> /dev/null", bkpinfo->tmpdir);
|
---|
[2202] | 1327 | (void)system(tmp);
|
---|
[2266] | 1328 | mr_free(tmp);
|
---|
| 1329 |
|
---|
[2382] | 1330 | if (((bkpinfo->isodir == NULL) && (bkpinfo->netfs_remote_dir == NULL)) || (bkpinfo->prefix == NULL)) {
|
---|
[2325] | 1331 | fatal_error("Unable to prepare ISO file name. Please report to dev team");
|
---|
| 1332 | }
|
---|
[2382] | 1333 | if (bkpinfo->netfs_remote_dir) {
|
---|
| 1334 | // NETFS
|
---|
| 1335 | mr_asprintf(tmp, "%s/%s/%s-%d.iso", bkpinfo->isodir, bkpinfo->netfs_remote_dir, bkpinfo->prefix, cd_number_i_want);
|
---|
[2325] | 1336 | } else {
|
---|
| 1337 | // ISO
|
---|
| 1338 | mr_asprintf(tmp, "%s/%s-%d.iso", bkpinfo->isodir, bkpinfo->prefix, cd_number_i_want);
|
---|
| 1339 | }
|
---|
[128] | 1340 | if (!does_file_exist(tmp)) {
|
---|
[2266] | 1341 | mr_free(tmp);
|
---|
[2382] | 1342 | if (bkpinfo->netfs_remote_dir) {
|
---|
| 1343 | // NETFS
|
---|
| 1344 | mr_asprintf(tmp, "%s/isodir/%s/%s-%d.iso", bkpinfo->tmpdir, bkpinfo->netfs_remote_dir, bkpinfo->prefix, cd_number_i_want);
|
---|
[2325] | 1345 | } else {
|
---|
| 1346 | // ISO
|
---|
| 1347 | mr_asprintf(tmp, "%s/isodir/%s-%d.iso", bkpinfo->tmpdir, bkpinfo->prefix, cd_number_i_want);
|
---|
| 1348 | }
|
---|
[128] | 1349 | if (does_file_exist(tmp)) {
|
---|
[2321] | 1350 | log_msg(1, "FIXME - hacking bkpinfo->isodir from '%s' to %s/isodir", bkpinfo->isodir, bkpinfo->tmpdir);
|
---|
[2323] | 1351 | mr_free(bkpinfo->isodir);
|
---|
[2324] | 1352 | mr_asprintf(bkpinfo->isodir, "%s/isodir", bkpinfo->tmpdir);
|
---|
[128] | 1353 | }
|
---|
| 1354 | }
|
---|
| 1355 | log_msg(3, "Mounting %s at %s", tmp, MNT_CDROM);
|
---|
| 1356 | if (mount_CDROM_here(tmp, MNT_CDROM)) {
|
---|
[2266] | 1357 | mr_free(tmp);
|
---|
[128] | 1358 | fatal_error("Mommy!");
|
---|
| 1359 | }
|
---|
[2266] | 1360 | mr_free(tmp);
|
---|
[1] | 1361 | }
|
---|
[1645] | 1362 | if ((res = what_number_cd_is_this()) != cd_number_i_want) {
|
---|
[2325] | 1363 | log_msg(3, "Currently, we hold %d but we want %d", res, cd_number_i_want);
|
---|
[2242] | 1364 | mds = media_descriptor_string(bkpinfo->backup_media_type);
|
---|
[2324] | 1365 | log_msg(3, "Insisting on %s #%d", mds, cd_number_i_want);
|
---|
[2323] | 1366 | mr_asprintf(request, "Please insert %s #%d and press Enter.", mds, cd_number_i_want);
|
---|
[2242] | 1367 | mr_free(mds);
|
---|
[2266] | 1368 |
|
---|
[1645] | 1369 | while (what_number_cd_is_this() != cd_number_i_want) {
|
---|
[2376] | 1370 | sync();
|
---|
[128] | 1371 | if (is_this_device_mounted(MNT_CDROM)) {
|
---|
| 1372 | res =
|
---|
| 1373 | run_program_and_log_output("umount " MNT_CDROM, FALSE);
|
---|
| 1374 | } else {
|
---|
| 1375 | res = 0;
|
---|
| 1376 | }
|
---|
| 1377 | if (res) {
|
---|
[541] | 1378 | log_to_screen("WARNING - failed to unmount CD-ROM drive");
|
---|
[128] | 1379 | }
|
---|
| 1380 | if (!bkpinfo->please_dont_eject) {
|
---|
| 1381 | res = eject_device(bkpinfo->media_device);
|
---|
| 1382 | } else {
|
---|
| 1383 | res = 0;
|
---|
| 1384 | }
|
---|
| 1385 | if (res) {
|
---|
[541] | 1386 | log_to_screen("WARNING - failed to eject CD-ROM disk");
|
---|
[128] | 1387 | }
|
---|
| 1388 | popup_and_OK(request);
|
---|
| 1389 | if (!bkpinfo->please_dont_eject) {
|
---|
| 1390 | inject_device(bkpinfo->media_device);
|
---|
| 1391 | }
|
---|
[2376] | 1392 | sync();
|
---|
[128] | 1393 | }
|
---|
[2266] | 1394 | mr_free(request);
|
---|
| 1395 |
|
---|
[128] | 1396 | log_msg(1, "Thankyou. Proceeding...");
|
---|
| 1397 | g_current_media_number = cd_number_i_want;
|
---|
| 1398 | }
|
---|
[1] | 1399 | }
|
---|
| 1400 |
|
---|
| 1401 | /* @} - end of deviceGroup */
|
---|
| 1402 |
|
---|
| 1403 |
|
---|
[2428] | 1404 |
|
---|
[1] | 1405 | /**
|
---|
[2428] | 1406 | * @addtogroup utilityGroup
|
---|
| 1407 | * @{
|
---|
| 1408 | */
|
---|
| 1409 | /**
|
---|
| 1410 | * Get a space-separated list of NETFS devices and mounts.
|
---|
| 1411 | * @return The list created.
|
---|
| 1412 | * @note The return value points to static data that will be overwritten with each call.
|
---|
| 1413 | */
|
---|
| 1414 | char *list_of_NETFS_devices_and_mounts(void)
|
---|
| 1415 | {
|
---|
| 1416 | char *exclude_these_devices = NULL;
|
---|
| 1417 | char *exclude_these_directories = NULL;
|
---|
| 1418 | static char result_sz[1024];
|
---|
| 1419 |
|
---|
| 1420 | mr_asprintf(exclude_these_directories,"%s",list_of_NETFS_mounts_only());
|
---|
| 1421 | exclude_these_devices = call_program_and_get_last_line_of_output("tr -s '\t' ' ' < /etc/fstab | grep -E '( (coda|ncpfs|sshfs|nfs|nfs4|smbfs|cifs|afs|gfs|ocfs|ocfs2|mvfs|nsspool|nsvol) )' | cut -d' ' -f1 | tr -s '\n' ' ' | awk '{print $0;}'");
|
---|
| 1422 | snprintf(result_sz, 1023, "%s %s", exclude_these_directories, exclude_these_devices);
|
---|
| 1423 | mr_free(exclude_these_devices);
|
---|
| 1424 | mr_free(exclude_these_directories);
|
---|
| 1425 | return (result_sz);
|
---|
| 1426 | }
|
---|
| 1427 |
|
---|
| 1428 |
|
---|
| 1429 |
|
---|
| 1430 |
|
---|
| 1431 | /**
|
---|
| 1432 | * Get a space-separated list of NETFS mounts.
|
---|
| 1433 | * @return The list created.
|
---|
| 1434 | * @note The return value points to static data that will be overwritten with each call.
|
---|
| 1435 | * @bug Even though we only want the mounts, the devices are still checked.
|
---|
| 1436 | */
|
---|
| 1437 | char *list_of_NETFS_mounts_only(void)
|
---|
| 1438 | {
|
---|
| 1439 | char *exclude_these_directories = NULL;
|
---|
| 1440 | static char result_sz[512];
|
---|
| 1441 |
|
---|
| 1442 | exclude_these_directories = call_program_and_get_last_line_of_output("mount -t coda,ncpfs,fuse.sshfs,nfs,nfs4,smbfs,cifs,afs,gfs,ocfs,ocfs2,mvfs,nsspool,nssvol | tr -s '\t' ' ' | cut -d' ' -f3 | tr -s '\n' ' ' | awk '{print $0;}'");
|
---|
| 1443 | snprintf(result_sz, 511, "%s", exclude_these_directories);
|
---|
| 1444 | mr_free(exclude_these_directories);
|
---|
| 1445 | return (result_sz);
|
---|
| 1446 | }
|
---|
| 1447 |
|
---|
| 1448 | /* @} - end of utilityGroup */
|
---|
| 1449 |
|
---|
| 1450 |
|
---|
| 1451 |
|
---|
| 1452 |
|
---|
| 1453 |
|
---|
| 1454 | /**
|
---|
| 1455 | * Create a randomly-named FIFO. The format is @p stub "." [random] [random] where
|
---|
| 1456 | * [random] is a random number between 1 and 32767.
|
---|
| 1457 | * @param store_name_here Where to store the new filename.
|
---|
| 1458 | * @param stub A random number will be appended to this to make the FIFO's name.
|
---|
| 1459 | * @ingroup deviceGroup
|
---|
| 1460 | */
|
---|
| 1461 | void make_fifo(char *store_name_here, char *stub)
|
---|
| 1462 | {
|
---|
| 1463 | char *tmp = NULL;
|
---|
| 1464 |
|
---|
| 1465 | assert_string_is_neither_NULL_nor_zerolength(stub);
|
---|
| 1466 |
|
---|
| 1467 | sprintf(store_name_here, "%s%d%d", stub, (int) (random() % 32768),
|
---|
| 1468 | (int) (random() % 32768));
|
---|
| 1469 | make_hole_for_file(store_name_here);
|
---|
| 1470 | mkfifo(store_name_here, S_IRWXU | S_IRWXG);
|
---|
| 1471 | mr_asprintf(tmp, "chmod 770 %s", store_name_here);
|
---|
| 1472 | paranoid_system(tmp);
|
---|
| 1473 | mr_free(tmp);
|
---|
| 1474 | }
|
---|
| 1475 |
|
---|
| 1476 |
|
---|
| 1477 |
|
---|
| 1478 |
|
---|
| 1479 |
|
---|
| 1480 |
|
---|
| 1481 | /**
|
---|
| 1482 | * Set the tmpdir and scratchdir to reside on the partition with the most free space.
|
---|
| 1483 | * Automatically excludes DOS, NTFS, SMB, and NFS filesystems.
|
---|
| 1484 | * @param bkpinfo The backup information structure. @c bkpinfo->tmpdir and @c bkpinfo->scratchdir will be set.
|
---|
| 1485 | * @ingroup utilityGroup
|
---|
| 1486 | */
|
---|
| 1487 | void sensibly_set_scratchdir()
|
---|
| 1488 | {
|
---|
| 1489 | char *tmp = NULL;
|
---|
| 1490 | char *command = NULL;
|
---|
| 1491 | char *sz = NULL;
|
---|
| 1492 |
|
---|
| 1493 | #ifdef __FreeBSD__
|
---|
| 1494 | tmp = call_program_and_get_last_line_of_output("LANGUAGE=C df -m -P -t nonfs,msdosfs,ntfs,ntfs-3g,smbfs,smb,cifs,afs,gfs,ocfs,ocfs2,mvfs,nsspool,nssvol | grep -vE \"none|Filesystem\" | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'");
|
---|
| 1495 | #else
|
---|
| 1496 | tmp = call_program_and_get_last_line_of_output("LANGUAGE=C df -m -P -x nfs -x nfs4 -x fuse.sshfs -x vfat -x ntfs -x ntfs-3g -x smbfs -x smb -x cifs -x afs -x gfs -x ocfs -x ocfs2 -x mvfs -x nsspool -x nssvol -x iso9660 | grep -vE \"none|Filesystem|/dev/shm\" | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'");
|
---|
| 1497 | #endif
|
---|
| 1498 |
|
---|
| 1499 | if (tmp[0] != '/') {
|
---|
| 1500 | mr_asprintf(sz, "%s", tmp);
|
---|
| 1501 | mr_free(tmp);
|
---|
| 1502 | mr_asprintf(tmp, "/%s", sz);
|
---|
| 1503 | mr_free(sz);
|
---|
| 1504 | }
|
---|
| 1505 | if (!tmp[0]) {
|
---|
| 1506 | fatal_error("I couldn't figure out the scratchdir!");
|
---|
| 1507 | }
|
---|
| 1508 |
|
---|
| 1509 | /* Cleaning a potential previous scratchdir */
|
---|
| 1510 | if (bkpinfo->scratchdir) {
|
---|
| 1511 | mr_asprintf(command, "rm -Rf %s", bkpinfo->scratchdir);
|
---|
| 1512 | paranoid_system(command);
|
---|
| 1513 | mr_free(command);
|
---|
| 1514 | }
|
---|
| 1515 | mr_free(bkpinfo->scratchdir);
|
---|
| 1516 |
|
---|
| 1517 | mr_asprintf(bkpinfo->scratchdir , "%s/mondo.scratch.%d", tmp, (int) (random() % 32768));
|
---|
| 1518 | log_it("bkpinfo->scratchdir is being set to %s", bkpinfo->scratchdir);
|
---|
| 1519 |
|
---|
| 1520 | /* Cleaning potential previous scratchdir */
|
---|
| 1521 | mr_asprintf(command, "rm -Rf %s/mondo.scratch.*", tmp);
|
---|
| 1522 | mr_free(tmp);
|
---|
| 1523 |
|
---|
| 1524 | paranoid_system(command);
|
---|
| 1525 | mr_free(command);
|
---|
| 1526 | }
|
---|
| 1527 |
|
---|
| 1528 |
|
---|
| 1529 |
|
---|
| 1530 |
|
---|
| 1531 |
|
---|
| 1532 |
|
---|
| 1533 | /**
|
---|
| 1534 | * @addtogroup deviceGroup
|
---|
| 1535 | * @{
|
---|
| 1536 | */
|
---|
| 1537 | /**
|
---|
| 1538 | * If we can read @p dev, set @return output to it.
|
---|
| 1539 | * If @p dev cannot be read, set @p output to NULL.
|
---|
| 1540 | * @param dev The device to check for.
|
---|
| 1541 | * @return output Set to @p dev if @p dev exists, NULL otherwise.
|
---|
| 1542 | */
|
---|
| 1543 | char *set_dev_to_this_if_rx_OK(char *dev)
|
---|
| 1544 | {
|
---|
| 1545 | char *command = NULL;
|
---|
| 1546 | char *output = NULL;
|
---|
| 1547 |
|
---|
| 1548 | if (!dev || dev[0] == '\0') {
|
---|
| 1549 | return(NULL);
|
---|
| 1550 | }
|
---|
| 1551 | log_msg(10, "Injecting %s", dev);
|
---|
| 1552 | inject_device(dev);
|
---|
| 1553 | if (!does_file_exist(dev)) {
|
---|
| 1554 | log_msg(10, "%s doesn't exist. Returning FALSE.", dev);
|
---|
| 1555 | return(NULL);
|
---|
| 1556 | }
|
---|
| 1557 | mr_asprintf(command, "dd bs=%ld count=1 if=%s of=/dev/null &> /dev/null", 512L, dev);
|
---|
| 1558 | if (!run_program_and_log_output(command, FALSE)
|
---|
| 1559 | && !run_program_and_log_output(command, FALSE)) {
|
---|
| 1560 | mr_asprintf(output, "%s", dev);
|
---|
| 1561 | log_msg(4, "Found it - %s", dev);
|
---|
| 1562 | } else {
|
---|
| 1563 | log_msg(4, "It's not %s", dev);
|
---|
| 1564 | }
|
---|
| 1565 | mr_free(command);
|
---|
| 1566 | return(output);
|
---|
| 1567 | }
|
---|
| 1568 |
|
---|
| 1569 |
|
---|
| 1570 |
|
---|
| 1571 |
|
---|
| 1572 |
|
---|
| 1573 | /**
|
---|
| 1574 | * Find out what number CD is in the drive.
|
---|
| 1575 | * @param bkpinfo The backup information structure. The @c bkpinfo->media_device field is the only one used.
|
---|
| 1576 | * @return The current CD number, or -1 if it could not be found.
|
---|
| 1577 | * @note If the CD is not mounted, it will be mounted
|
---|
| 1578 | * (and remain mounted after this function returns).
|
---|
| 1579 | */
|
---|
| 1580 | int what_number_cd_is_this()
|
---|
| 1581 | {
|
---|
| 1582 | int cd_number = -1;
|
---|
| 1583 | char *mountdev = NULL;
|
---|
| 1584 | char *tmp = NULL;
|
---|
| 1585 |
|
---|
| 1586 | assert(bkpinfo != NULL);
|
---|
| 1587 | // log_it("Asking what_number_cd_is_this");
|
---|
| 1588 | if (g_ISO_restore_mode) {
|
---|
| 1589 | mr_asprintf(tmp, "mount | grep iso9660 | awk '{print $3;}'");
|
---|
| 1590 | mountdev = call_program_and_get_last_line_of_output(tmp);
|
---|
| 1591 | mr_strcat(mountdev, "/archives/THIS-CD-NUMBER");
|
---|
| 1592 | mr_free(tmp);
|
---|
| 1593 |
|
---|
| 1594 | tmp = last_line_of_file(mountdev);
|
---|
| 1595 | cd_number = atoi(tmp);
|
---|
| 1596 | mr_free(tmp);
|
---|
| 1597 | mr_free(mountdev);
|
---|
| 1598 | return (cd_number);
|
---|
| 1599 | }
|
---|
| 1600 |
|
---|
| 1601 | if (bkpinfo->media_device) {
|
---|
| 1602 | mr_asprintf(mountdev, "%s", bkpinfo->media_device);
|
---|
| 1603 | }
|
---|
| 1604 | if (!mountdev) {
|
---|
| 1605 | log_it("(what_number_cd_is_this) Warning - media_device unknown. Finding out...");
|
---|
| 1606 | mr_free(bkpinfo->media_device);
|
---|
| 1607 | bkpinfo->media_device = find_cdrom_device(FALSE);
|
---|
| 1608 | mr_asprintf(mountdev, "%s", bkpinfo->media_device);
|
---|
| 1609 | }
|
---|
| 1610 | if (!is_this_device_mounted(MNT_CDROM)) {
|
---|
| 1611 | if (bkpinfo->backup_media_type == usb) {
|
---|
| 1612 | mount_USB_here(mountdev, MNT_CDROM);
|
---|
| 1613 | } else {
|
---|
| 1614 | mount_CDROM_here(mountdev, MNT_CDROM);
|
---|
| 1615 | }
|
---|
| 1616 | }
|
---|
| 1617 | mr_free(mountdev);
|
---|
| 1618 |
|
---|
| 1619 | tmp = last_line_of_file(MNT_CDROM "/archives/THIS-CD-NUMBER");
|
---|
| 1620 | cd_number = atoi(tmp);
|
---|
| 1621 | mr_free(tmp);
|
---|
| 1622 | return (cd_number);
|
---|
| 1623 | }
|
---|
| 1624 |
|
---|
| 1625 |
|
---|
| 1626 |
|
---|
| 1627 |
|
---|
| 1628 |
|
---|
| 1629 |
|
---|
| 1630 |
|
---|
| 1631 | /**
|
---|
| 1632 | * Find out what device is mounted as root (/).
|
---|
| 1633 | * @return Root device.
|
---|
| 1634 | * @note The returned string points to static storage and will be overwritten with every call.
|
---|
| 1635 | * @bug A bit of a misnomer; it's actually finding out the root device.
|
---|
| 1636 | * The mountpoint (where it's mounted) will obviously be '/'.
|
---|
| 1637 | */
|
---|
| 1638 | char *where_is_root_mounted() {
|
---|
| 1639 | /*@ buffers **************** */
|
---|
| 1640 | char *output = NULL;
|
---|
| 1641 |
|
---|
| 1642 |
|
---|
| 1643 | #ifdef __FreeBSD__
|
---|
| 1644 | output = call_program_and_get_last_line_of_output("mount | grep ' on / ' | cut -d' ' -f1");
|
---|
| 1645 | #else
|
---|
| 1646 | output = call_program_and_get_last_line_of_output("mount | grep ' on / ' | cut -d' ' -f1 | sed 's/[0-9]//' | sed 's/[0-9]//'");
|
---|
| 1647 | if (strstr(output, "/dev/cciss/")) {
|
---|
| 1648 | mr_free(output);
|
---|
| 1649 | output = call_program_and_get_last_line_of_output("mount | grep ' on / ' | cut -d' ' -f1 | cut -dp -f1");
|
---|
| 1650 | }
|
---|
| 1651 | if (strstr(output, "/dev/md")) {
|
---|
| 1652 | mr_free(output);
|
---|
| 1653 | output = call_program_and_get_last_line_of_output("mount | grep ' on / ' | cut -d' ' -f1");
|
---|
| 1654 | }
|
---|
| 1655 | #endif
|
---|
| 1656 |
|
---|
| 1657 | return (output);
|
---|
| 1658 | }
|
---|
| 1659 |
|
---|
| 1660 |
|
---|
| 1661 | /**
|
---|
| 1662 | * Find out which boot loader is in use.
|
---|
| 1663 | * @param which_device Device to look for the boot loader on.
|
---|
| 1664 | * @return 'L' for LILO, 'E'for ELILO, 'G' for GRUB, 'B' or 'D' for FreeBSD boot loaders, or 'U' for Unknown.
|
---|
| 1665 | * @note Under Linux, all drives are examined, not just @p which_device.
|
---|
| 1666 | */
|
---|
| 1667 | #ifdef __FreeBSD__
|
---|
| 1668 | char which_boot_loader(char *which_device) {
|
---|
| 1669 | int count_lilos = 0;
|
---|
| 1670 | int count_grubs = 0;
|
---|
| 1671 | int count_boot0s = 0;
|
---|
| 1672 | int count_dangerouslydedicated = 0;
|
---|
| 1673 |
|
---|
| 1674 | log_it("looking at drive %s's MBR", which_device);
|
---|
| 1675 | if (does_string_exist_in_boot_block(which_device, "GRUB")) {
|
---|
| 1676 | count_grubs++;
|
---|
| 1677 | }
|
---|
| 1678 | if (does_string_exist_in_boot_block(which_device, "LILO")) {
|
---|
| 1679 | count_lilos++;
|
---|
| 1680 | }
|
---|
| 1681 | if (does_string_exist_in_boot_block(which_device, "Drive")) {
|
---|
| 1682 | count_boot0s++;
|
---|
| 1683 | }
|
---|
| 1684 | if (does_string_exist_in_first_N_blocks(which_device, "FreeBSD/i386", 17)) {
|
---|
| 1685 | count_dangerouslydedicated++;
|
---|
| 1686 | }
|
---|
| 1687 | log_it("%d grubs and %d lilos and %d elilos and %d boot0s and %d DD\n",
|
---|
| 1688 | count_grubs, count_lilos, count_elilos, count_boot0s,
|
---|
| 1689 | count_dangerouslydedicated);
|
---|
| 1690 |
|
---|
| 1691 | if (count_grubs && !count_lilos) {
|
---|
| 1692 | return ('G');
|
---|
| 1693 | } else if (count_lilos && !count_grubs) {
|
---|
| 1694 | return ('L');
|
---|
| 1695 | } else if (count_grubs == 1 && count_lilos == 1) {
|
---|
| 1696 | log_it("I'll bet you used to use LILO but switched to GRUB...");
|
---|
| 1697 | return ('G');
|
---|
| 1698 | } else if (count_boot0s == 1) {
|
---|
| 1699 | return ('B');
|
---|
| 1700 | } else if (count_dangerouslydedicated) {
|
---|
| 1701 | return ('D');
|
---|
| 1702 | } else {
|
---|
| 1703 | log_it("Unknown boot loader");
|
---|
| 1704 | return ('U');
|
---|
| 1705 | }
|
---|
| 1706 | }
|
---|
| 1707 |
|
---|
| 1708 | #else
|
---|
| 1709 |
|
---|
| 1710 | char which_boot_loader(char *which_device) {
|
---|
| 1711 | /*@ buffer ***************************************************** */
|
---|
| 1712 | char *list_drives_cmd = NULL;
|
---|
| 1713 | char *tmp = NULL;
|
---|
| 1714 | char *current_drive = NULL;
|
---|
| 1715 |
|
---|
| 1716 | /*@ pointers *************************************************** */
|
---|
| 1717 | FILE *pdrives = NULL;
|
---|
| 1718 |
|
---|
| 1719 | /*@ int ******************************************************** */
|
---|
| 1720 | int count_lilos = 0;
|
---|
| 1721 | int count_grubs = 0;
|
---|
| 1722 |
|
---|
| 1723 | /*@ end vars *************************************************** */
|
---|
| 1724 |
|
---|
| 1725 |
|
---|
| 1726 | #ifdef __IA64__
|
---|
| 1727 | /* No choice for it */
|
---|
| 1728 | return ('E');
|
---|
| 1729 | #endif
|
---|
| 1730 |
|
---|
| 1731 | tmp = where_is_root_mounted();
|
---|
| 1732 | mr_asprintf(list_drives_cmd, "parted2fdisk -l 2>/dev/null | grep \"/dev/.*:\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/; echo %s", tmp);
|
---|
| 1733 | mr_free(tmp);
|
---|
| 1734 | log_it("list_drives_cmd = %s", list_drives_cmd);
|
---|
| 1735 |
|
---|
| 1736 | pdrives = popen(list_drives_cmd, "r");
|
---|
| 1737 | mr_free(list_drives_cmd);
|
---|
| 1738 |
|
---|
| 1739 | if (!pdrives) {
|
---|
| 1740 | log_OS_error("Unable to open list of drives");
|
---|
| 1741 | return ('\0');
|
---|
| 1742 | }
|
---|
| 1743 |
|
---|
| 1744 | for (mr_getline(current_drive, pdrives); !feof(pdrives); mr_getline(current_drive, pdrives)) {
|
---|
| 1745 | mr_strip_spaces(current_drive);
|
---|
| 1746 | log_it("looking at drive %s's MBR", current_drive);
|
---|
| 1747 | if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
|
---|
| 1748 | count_grubs++;
|
---|
| 1749 | /* BERLIOS : removed as I don't think it's mandatory here
|
---|
| 1750 | mr_free(which_device);
|
---|
| 1751 | mr_asprintf(which_device, "%s", current_drive);
|
---|
| 1752 | */
|
---|
| 1753 | mr_free(current_drive);
|
---|
| 1754 | break;
|
---|
| 1755 | }
|
---|
| 1756 | if (does_string_exist_in_boot_block(current_drive, "LILO")) {
|
---|
| 1757 | count_lilos++;
|
---|
| 1758 | /* BERLIOS : removed as I don't think it's mandatory here
|
---|
| 1759 | mr_free(which_device);
|
---|
| 1760 | mr_asprintf(which_device, "%s", current_drive);
|
---|
| 1761 | */
|
---|
| 1762 | mr_free(current_drive);
|
---|
| 1763 | break;
|
---|
| 1764 | }
|
---|
| 1765 | mr_free(current_drive);
|
---|
| 1766 | }
|
---|
| 1767 | mr_free(current_drive);
|
---|
| 1768 |
|
---|
| 1769 | if (pclose(pdrives)) {
|
---|
| 1770 | log_OS_error("Cannot pclose pdrives");
|
---|
| 1771 | }
|
---|
| 1772 | log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
|
---|
| 1773 | if (count_grubs && !count_lilos) {
|
---|
| 1774 | return ('G');
|
---|
| 1775 | } else if (count_lilos && !count_grubs) {
|
---|
| 1776 | return ('L');
|
---|
| 1777 | } else if (count_grubs == 1 && count_lilos == 1) {
|
---|
| 1778 | log_it("I'll bet you used to use LILO but switched to GRUB...");
|
---|
| 1779 | return ('G');
|
---|
| 1780 | } else {
|
---|
| 1781 | // We need to look on each partition then
|
---|
| 1782 | mr_asprintf(list_drives_cmd, "parted2fdisk -l 2>/dev/null | grep -E \"^/dev/\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/");
|
---|
| 1783 | log_it("list_drives_cmd = %s", list_drives_cmd);
|
---|
| 1784 |
|
---|
| 1785 | if (!(pdrives = popen(list_drives_cmd, "r"))) {
|
---|
| 1786 | log_OS_error("Unable to open list of drives");
|
---|
| 1787 | mr_free(list_drives_cmd);
|
---|
| 1788 | return ('\0');
|
---|
| 1789 | }
|
---|
| 1790 | mr_free(list_drives_cmd);
|
---|
| 1791 |
|
---|
| 1792 | for (mr_getline(current_drive, pdrives); !feof(pdrives); mr_getline(current_drive, pdrives)) {
|
---|
| 1793 | mr_strip_spaces(current_drive);
|
---|
| 1794 | log_it("looking at partition %s's BR", current_drive);
|
---|
| 1795 | if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
|
---|
| 1796 | count_grubs++;
|
---|
| 1797 | /* BERLIOS : removed as I don't think it's mandatory here
|
---|
| 1798 | mr_free(which_device);
|
---|
| 1799 | mr_asprintf(which_device, "%s", current_drive);
|
---|
| 1800 | */
|
---|
| 1801 | mr_free(current_drive);
|
---|
| 1802 | break;
|
---|
| 1803 | }
|
---|
| 1804 | if (does_string_exist_in_boot_block(current_drive, "LILO")) {
|
---|
| 1805 | count_lilos++;
|
---|
| 1806 | /* BERLIOS : removed as I don't think it's mandatory here
|
---|
| 1807 | mr_free(which_device);
|
---|
| 1808 | mr_asprintf(which_device, "%s", current_drive);
|
---|
| 1809 | */
|
---|
| 1810 | mr_free(current_drive);
|
---|
| 1811 | break;
|
---|
| 1812 | }
|
---|
| 1813 | mr_free(current_drive);
|
---|
| 1814 | }
|
---|
| 1815 | mr_free(current_drive);
|
---|
| 1816 |
|
---|
| 1817 | if (pclose(pdrives)) {
|
---|
| 1818 | log_OS_error("Cannot pclose pdrives");
|
---|
| 1819 | }
|
---|
| 1820 | log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
|
---|
| 1821 | if (count_grubs && !count_lilos) {
|
---|
| 1822 | return ('G');
|
---|
| 1823 | } else if (count_lilos && !count_grubs) {
|
---|
| 1824 | return ('L');
|
---|
| 1825 | } else if (count_grubs == 1 && count_lilos == 1) {
|
---|
| 1826 | log_it("I'll bet you used to use LILO but switched to GRUB...");
|
---|
| 1827 | return ('G');
|
---|
| 1828 | } else {
|
---|
| 1829 | log_it("Unknown boot loader");
|
---|
| 1830 | return ('U');
|
---|
| 1831 | }
|
---|
| 1832 | }
|
---|
| 1833 | }
|
---|
| 1834 | #endif
|
---|
| 1835 |
|
---|
| 1836 |
|
---|
| 1837 |
|
---|
| 1838 |
|
---|
| 1839 | /**
|
---|
| 1840 | * Write zeroes over the first 16K of @p device.
|
---|
| 1841 | * @param device The device to zero.
|
---|
| 1842 | * @return 0 for success, 1 for failure.
|
---|
| 1843 | */
|
---|
| 1844 | int zero_out_a_device(char *device)
|
---|
| 1845 | {
|
---|
| 1846 | FILE *fout;
|
---|
| 1847 | int i;
|
---|
| 1848 |
|
---|
| 1849 | assert_string_is_neither_NULL_nor_zerolength(device);
|
---|
| 1850 |
|
---|
| 1851 | log_it("Zeroing drive %s", device);
|
---|
| 1852 | if (!(fout = fopen(device, "w"))) {
|
---|
| 1853 | log_OS_error("Unable to open/write to device");
|
---|
| 1854 | return (1);
|
---|
| 1855 | }
|
---|
| 1856 | for (i = 0; i < 16384; i++) {
|
---|
| 1857 | fputc('\0', fout);
|
---|
| 1858 | }
|
---|
| 1859 | paranoid_fclose(fout);
|
---|
| 1860 | log_it("Device successfully zeroed.");
|
---|
| 1861 | return (0);
|
---|
| 1862 | }
|
---|
| 1863 |
|
---|
| 1864 | /**
|
---|
| 1865 | * Return the device pointed to by @p incoming.
|
---|
| 1866 | * @param incoming The device to resolve symlinks for.
|
---|
| 1867 | * @return The path to the real device file.
|
---|
| 1868 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
| 1869 | * @bug Won't work with file v4.0; needs to be written in C.
|
---|
| 1870 | */
|
---|
| 1871 | char *resolve_softlinks_to_get_to_actual_device_file(char *incoming)
|
---|
| 1872 | {
|
---|
| 1873 | char *output;
|
---|
| 1874 | char *command = NULL;
|
---|
| 1875 | char *curr_fname;
|
---|
| 1876 | char *scratch = NULL;
|
---|
| 1877 | char *tmp = NULL;
|
---|
| 1878 | char *p;
|
---|
| 1879 |
|
---|
| 1880 | struct stat statbuf;
|
---|
| 1881 | malloc_string(curr_fname);
|
---|
| 1882 | if (!does_file_exist(incoming)) {
|
---|
| 1883 | log_it("resolve_softlinks_to_get_to_actual_device_file --- device not found");
|
---|
| 1884 | mr_asprintf(output, "%s", incoming);
|
---|
| 1885 | } else {
|
---|
| 1886 | strcpy(curr_fname, incoming);
|
---|
| 1887 | lstat(curr_fname, &statbuf);
|
---|
| 1888 | while (S_ISLNK(statbuf.st_mode)) {
|
---|
| 1889 | log_msg(1, "curr_fname = %s", curr_fname);
|
---|
| 1890 | mr_asprintf(command, "file %s", curr_fname);
|
---|
| 1891 | tmp = call_program_and_get_last_line_of_output(command);
|
---|
| 1892 | mr_free(command);
|
---|
| 1893 | for (p = tmp + strlen(tmp); p != tmp && *p != '`' && *p != ' '; p--);
|
---|
| 1894 | p++;
|
---|
| 1895 | mr_asprintf(scratch, "%s", p);
|
---|
| 1896 | for (p = scratch; *p != '\0' && *p != '\''; p++);
|
---|
| 1897 | *p = '\0';
|
---|
| 1898 | log_msg(0, "curr_fname %s --> '%s' --> %s", curr_fname, tmp, scratch);
|
---|
| 1899 | mr_free(tmp);
|
---|
| 1900 |
|
---|
| 1901 | if (scratch[0] == '/') {
|
---|
| 1902 | strcpy(curr_fname, scratch); // copy whole thing because it's an absolute softlink
|
---|
| 1903 | } else { // copy over the basename cos it's a relative softlink
|
---|
| 1904 | p = curr_fname + strlen(curr_fname);
|
---|
| 1905 | while (p != curr_fname && *p != '/') {
|
---|
| 1906 | p--;
|
---|
| 1907 | }
|
---|
| 1908 | if (*p == '/') {
|
---|
| 1909 | p++;
|
---|
| 1910 | }
|
---|
| 1911 | strcpy(p, scratch);
|
---|
| 1912 | }
|
---|
| 1913 | mr_free(scratch);
|
---|
| 1914 | lstat(curr_fname, &statbuf);
|
---|
| 1915 | }
|
---|
| 1916 | mr_asprintf(output, "%s", curr_fname);
|
---|
| 1917 | log_it("resolved %s to %s", incoming, output);
|
---|
| 1918 | }
|
---|
| 1919 | paranoid_free(curr_fname);
|
---|
| 1920 | return (output);
|
---|
| 1921 | }
|
---|
| 1922 |
|
---|
| 1923 | /* @} - end of deviceGroup */
|
---|
| 1924 |
|
---|
| 1925 | /**
|
---|
| 1926 | * Return the type of partition format (GPT or MBR)
|
---|
| 1927 | */
|
---|
| 1928 | char *which_partition_format(const char *drive)
|
---|
| 1929 | {
|
---|
| 1930 | char *output;
|
---|
| 1931 | char *tmp = NULL;
|
---|
| 1932 | char *command = NULL;
|
---|
| 1933 | char *fdisk = NULL;
|
---|
| 1934 | #ifdef __IA64__
|
---|
| 1935 | struct stat buf;
|
---|
| 1936 | #endif
|
---|
| 1937 | mr_asprintf(fdisk, "/sbin/parted2fdisk");
|
---|
| 1938 | mr_asprintf(command, "%s -l %s | grep 'EFI GPT'", fdisk, drive);
|
---|
| 1939 | mr_free(fdisk);
|
---|
| 1940 |
|
---|
| 1941 | tmp = call_program_and_get_last_line_of_output(command);
|
---|
| 1942 | mr_free(command);
|
---|
| 1943 |
|
---|
| 1944 | if (strstr(tmp, "GPT") == NULL) {
|
---|
| 1945 | mr_asprintf(output, "MBR");
|
---|
| 1946 | } else {
|
---|
| 1947 | mr_asprintf(output, "GPT");
|
---|
| 1948 | }
|
---|
| 1949 | mr_free(tmp);
|
---|
| 1950 |
|
---|
| 1951 | log_msg(0, "Found %s partition table format type", output);
|
---|
| 1952 | return (output);
|
---|
| 1953 | }
|
---|
| 1954 |
|
---|
| 1955 | /**
|
---|
| 1956 | * Frees the memory for all of the structures on the linked list of
|
---|
| 1957 | * all of the non-NETFS mounted file systems.
|
---|
| 1958 | */
|
---|
| 1959 | static void free_mounted_fs_list (void) {
|
---|
| 1960 | MOUNTED_FS_STRUCT *DSFptr = NULL;
|
---|
| 1961 | MOUNTED_FS_STRUCT *DSFnext = NULL;
|
---|
| 1962 |
|
---|
| 1963 | DSFptr = DSF_Head;
|
---|
| 1964 | while (DSFptr != NULL) {
|
---|
| 1965 | DSFnext = DSFptr->next;
|
---|
| 1966 | paranoid_free(DSFptr);
|
---|
| 1967 | DSFptr = DSFnext;
|
---|
| 1968 | }
|
---|
| 1969 | DSF_Head = NULL;
|
---|
| 1970 | DSF_Tail = NULL;
|
---|
| 1971 | }
|
---|
| 1972 |
|
---|
| 1973 | /**
|
---|
| 1974 | * Creates a singly linked list of all of the non-NETFS mounted file systems.
|
---|
| 1975 | * @param DSFptr A pointer to the structure MOUNTED_FS_STRUCT used to hold
|
---|
| 1976 | * the list of mounted file systems.
|
---|
| 1977 | * @return None.
|
---|
| 1978 | */
|
---|
| 1979 | static void add_mounted_fs_struct (MOUNTED_FS_STRUCT *DSFptr)
|
---|
| 1980 | {
|
---|
| 1981 | assert (DSFptr);
|
---|
| 1982 | if (DSF_Head == NULL) {
|
---|
| 1983 | DSF_Head = DSFptr;
|
---|
| 1984 | } else {
|
---|
| 1985 | DSF_Tail->next = DSFptr;
|
---|
| 1986 | }
|
---|
| 1987 | DSFptr->next = NULL;
|
---|
| 1988 | DSF_Tail = DSFptr;
|
---|
| 1989 | }
|
---|
| 1990 |
|
---|
| 1991 | /**
|
---|
| 1992 | * Find the structure, in the singly linked list of all of the non-NETFS
|
---|
| 1993 | * mounted file systems, that contains the specified device.
|
---|
| 1994 | * @param device The device to find
|
---|
| 1995 | * @return NULL if it didn't find the device, a pointer to the
|
---|
| 1996 | * structure if it did.
|
---|
| 1997 | */
|
---|
| 1998 | static MOUNTED_FS_STRUCT *find_device_in_list (char *device)
|
---|
| 1999 | {
|
---|
| 2000 | MOUNTED_FS_STRUCT *DSFptr = NULL;
|
---|
| 2001 |
|
---|
| 2002 | DSFptr = DSF_Head;
|
---|
| 2003 | while (DSFptr != NULL) {
|
---|
| 2004 | if (!strcmp(DSFptr->device, device)) {
|
---|
| 2005 | break;
|
---|
| 2006 | }
|
---|
| 2007 | DSFptr = DSFptr->next;
|
---|
| 2008 | }
|
---|
| 2009 | return (DSFptr);
|
---|
| 2010 | }
|
---|
| 2011 |
|
---|
| 2012 | /**
|
---|
| 2013 | * Find the structure, in the singly linked list of all of the non-NETFS
|
---|
| 2014 | * mounted file systems, that contains the specified mount point.
|
---|
| 2015 | * @param mount_point The mount point to find
|
---|
| 2016 | * @return NULL is it didn't find the mount point, a pointer to the
|
---|
| 2017 | * structure if it did.
|
---|
| 2018 | */
|
---|
| 2019 | static MOUNTED_FS_STRUCT *find_mount_point_in_list (char *mount_point)
|
---|
| 2020 | {
|
---|
| 2021 | MOUNTED_FS_STRUCT *DSFptr = NULL;
|
---|
| 2022 |
|
---|
| 2023 | DSFptr = DSF_Head;
|
---|
| 2024 | while (DSFptr != NULL) {
|
---|
| 2025 | if (!strcmp(DSFptr->mount_point, mount_point)) {
|
---|
| 2026 | break;
|
---|
| 2027 | }
|
---|
| 2028 | DSFptr = DSFptr->next;
|
---|
| 2029 | }
|
---|
| 2030 | return (DSFptr);
|
---|
| 2031 | }
|
---|
| 2032 |
|
---|
| 2033 | /**
|
---|
| 2034 | * Creates a linked list of all of the non-NETFS mounted file systems.
|
---|
| 2035 | * We use a linked list because we don't know how many mounted file
|
---|
| 2036 | * there are (and there can be a lot).
|
---|
| 2037 | * @return 0 on success and greated than 0 on failure.
|
---|
| 2038 | */
|
---|
| 2039 | static int create_list_of_non_NETFS_mounted_file_systems (void)
|
---|
| 2040 | {
|
---|
| 2041 | int i = 0;
|
---|
| 2042 | int mount_cnt = 0;
|
---|
| 2043 | char *mounted_file_system = NULL;
|
---|
| 2044 | char *command = NULL;
|
---|
| 2045 | char *token = NULL;
|
---|
| 2046 | char token_chars[] =" :\t\r\f\a\0";
|
---|
| 2047 | MOUNTED_FS_STRUCT *DSFptr = NULL;
|
---|
| 2048 |
|
---|
| 2049 | free_mounted_fs_list();
|
---|
| 2050 | /********
|
---|
| 2051 | * Find the number of mounted file system entries and their respective mount points.
|
---|
| 2052 | * I can't return all of the entries as one string because it's length can be longer
|
---|
| 2053 | * than MAX_STR_LEN which is used in call_program_and_get_last_line_of_output().
|
---|
| 2054 | * So start looping and get the number of mounted file systems and query them one by one.
|
---|
| 2055 | ********/
|
---|
| 2056 | /* Get the number of mounted file systems ((those that start with "/dev/" */
|
---|
| 2057 | mr_asprintf(command, "mount 2>/dev/null | awk '{if($1 ~ \"^/dev/\"){print $0}}'|wc -l");
|
---|
| 2058 | log_msg(5, "Running: %s", command);
|
---|
| 2059 | mounted_file_system = call_program_and_get_last_line_of_output(command);
|
---|
| 2060 | mr_free(command);
|
---|
| 2061 |
|
---|
| 2062 | mount_cnt = atoi(mounted_file_system);
|
---|
| 2063 | log_msg (5, "mount_cnt: %d", mount_cnt);
|
---|
| 2064 | mr_free(mounted_file_system);
|
---|
| 2065 |
|
---|
| 2066 | for (i=mount_cnt; i > 0; i--) {
|
---|
| 2067 | mr_asprintf(command, "mount 2>/dev/null | awk '{if($1 ~ \"^/dev/\"){print $1,$3}}'|head -n %d", i);
|
---|
| 2068 | log_msg(5, "Running: %s", command);
|
---|
| 2069 | mounted_file_system = call_program_and_get_last_line_of_output(command);
|
---|
| 2070 | mr_free(command);
|
---|
| 2071 |
|
---|
| 2072 | log_msg (5, "mounted_file_system: %s", mounted_file_system);
|
---|
| 2073 | if ((token = strtok(mounted_file_system, token_chars)) == NULL) {
|
---|
| 2074 | log_msg (4, "Could not get the list of mounted file systems");
|
---|
| 2075 | mr_free(mounted_file_system);
|
---|
| 2076 | mr_free(token);
|
---|
| 2077 | return (1);
|
---|
| 2078 | }
|
---|
| 2079 | if (token) {
|
---|
| 2080 | log_msg (5, "token: %s", token);
|
---|
| 2081 | }
|
---|
| 2082 | while (token != NULL) {
|
---|
| 2083 | log_msg (5, "token: %s", token);
|
---|
| 2084 | if ((DSFptr = (MOUNTED_FS_STRUCT *) calloc(1, sizeof(MOUNTED_FS_STRUCT))) == NULL) {
|
---|
| 2085 | fatal_error ("Cannot allocate memory");
|
---|
| 2086 | }
|
---|
| 2087 | add_mounted_fs_struct(DSFptr);
|
---|
| 2088 | strcpy(DSFptr->device, token);
|
---|
| 2089 | mr_free(token);
|
---|
| 2090 | if ((token = strtok(NULL, token_chars)) == NULL) {
|
---|
| 2091 | log_msg (5, "Ran out of entries on the mounted file systems list");
|
---|
| 2092 | mr_free(mounted_file_system);
|
---|
| 2093 | mr_free(token);
|
---|
| 2094 | return (1);
|
---|
| 2095 | }
|
---|
| 2096 | log_msg (5, "token: %s", token);
|
---|
| 2097 | strcpy(DSFptr->mount_point, token);
|
---|
| 2098 | mr_free(token);
|
---|
| 2099 | token = strtok(NULL, token_chars);
|
---|
| 2100 | }
|
---|
| 2101 | mr_free(mounted_file_system);
|
---|
| 2102 | }
|
---|
| 2103 | return (0);
|
---|
| 2104 | }
|
---|
| 2105 |
|
---|
| 2106 |
|
---|
| 2107 |
|
---|
| 2108 | /**
|
---|
| 2109 | * Given a whole disk device special file, determine which mounted file systems
|
---|
| 2110 | * are on the dsf's partitions and which mounted file systems are not.
|
---|
| 2111 | * @param dsf The whole disk device special file.
|
---|
| 2112 | * @param included_dsf_list A char pointer used to hold the list of mount points
|
---|
| 2113 | * that are on the dsf. Memory for the array will be allocated within the function.
|
---|
| 2114 | * @param excluded_dsf_list A char pointer used to hold the list of mount points
|
---|
| 2115 | * that are not on the dsf. Memory for the array will be allocated within the function.
|
---|
| 2116 | * @return 0 on success, -1 if no device special file was passed in, -2 if a device
|
---|
| 2117 | * special file was passed in but it has no partitions on it, or 1 on failure
|
---|
| 2118 | */
|
---|
| 2119 | static int get_dsf_mount_list (const char *dsf, char **included_dsf_list, char **excluded_dsf_list) {
|
---|
| 2120 | int i = 0;
|
---|
| 2121 | int c = 0;
|
---|
| 2122 | int lastpos = 0;
|
---|
| 2123 | char *VG = NULL;
|
---|
| 2124 | char *tmp = NULL;
|
---|
| 2125 | char *command = NULL;
|
---|
| 2126 | char *partition_list = NULL;
|
---|
| 2127 | char partitions[64][MAX_STR_LEN];
|
---|
| 2128 | char *mount_list = NULL;
|
---|
| 2129 | char *token = NULL;
|
---|
| 2130 | char token_chars[] =" \t\r\f\a\0";
|
---|
| 2131 | MOUNTED_FS_STRUCT *DSFptr = NULL;
|
---|
| 2132 |
|
---|
| 2133 | memset((char *)partitions, 0, sizeof(partitions));
|
---|
| 2134 |
|
---|
| 2135 | log_msg(5, "dsf: %s", dsf);
|
---|
| 2136 |
|
---|
| 2137 | /********
|
---|
| 2138 | * See if a device special file was passed in (i.e. it must start with /dev/
|
---|
| 2139 | ********/
|
---|
| 2140 | if (strncmp(dsf, "/dev/", 5)) {
|
---|
| 2141 | log_msg (5, "%s does not start with /dev/ and (probably) is not a device special file", dsf);
|
---|
| 2142 | return (-1);
|
---|
| 2143 | }
|
---|
| 2144 | log_msg(5, " %s looks like a device special file", dsf);
|
---|
| 2145 | /* Verify that the dsf exists */
|
---|
| 2146 | mr_asprintf(command, "ls -al %s 2>/dev/null | wc -l", dsf);
|
---|
| 2147 | log_msg(5, "Executing: %s", command);
|
---|
| 2148 | tmp = call_program_and_get_last_line_of_output(command);
|
---|
| 2149 | mr_free(command);
|
---|
| 2150 |
|
---|
| 2151 | log_msg(5, " Return value: %s", tmp);
|
---|
| 2152 | c = atoi(tmp);
|
---|
| 2153 | mr_free(tmp);
|
---|
| 2154 |
|
---|
| 2155 | if (!c) {
|
---|
| 2156 | log_to_screen("Cannot find device special file %s", dsf);
|
---|
| 2157 | return (1);
|
---|
| 2158 | }
|
---|
| 2159 | log_msg(5, " %s device special file exists", dsf);
|
---|
| 2160 |
|
---|
| 2161 | /* Get a list of the mounted file systems */
|
---|
| 2162 | if (create_list_of_non_NETFS_mounted_file_systems()) {
|
---|
| 2163 | log_to_screen ("Could not get the list of mounted file systems");
|
---|
| 2164 | return (1);
|
---|
| 2165 | }
|
---|
| 2166 | log_msg (5, "Processing dsf: %s", dsf);
|
---|
| 2167 | /********
|
---|
| 2168 | * Get a list of the dsf's partitions. There could be no partitions on the disk
|
---|
| 2169 | * or a dsf of a partition was passed in (e.g. /dev/sda1 instead of /dev/sda).
|
---|
| 2170 | * Either way, it's an error.
|
---|
| 2171 | ********/
|
---|
| 2172 | mr_asprintf(command, "parted2fdisk -l %s 2>/dev/null|grep -E \"^/dev/\"|awk '{printf(\"%%s \", $1)}END{print \"\"}'", dsf);
|
---|
| 2173 | log_msg(4, "Executing: %s", command);
|
---|
| 2174 | partition_list = call_program_and_get_last_line_of_output(command);
|
---|
| 2175 | mr_free(command);
|
---|
| 2176 | log_msg(4, "Partition list for %s: %s", dsf, partition_list);
|
---|
| 2177 | if (!strlen(partition_list)) {
|
---|
| 2178 | /* There were no partitions on the disk */
|
---|
| 2179 | log_msg(4, "Cannot find any partitions on device special file %s", dsf);
|
---|
| 2180 | return (-2);
|
---|
| 2181 | }
|
---|
| 2182 |
|
---|
| 2183 | /* Fill the partition list */
|
---|
| 2184 | i = 0;
|
---|
| 2185 | lastpos = 0;
|
---|
| 2186 | while ((token = mr_strtok(partition_list, token_chars, &lastpos)) != NULL) {
|
---|
| 2187 | log_msg (5, "Found partition: %s", token);
|
---|
| 2188 | strcpy(partitions[i++], token);
|
---|
| 2189 | mr_free(token);
|
---|
| 2190 | }
|
---|
| 2191 | paranoid_free(partition_list);
|
---|
| 2192 |
|
---|
| 2193 | /********
|
---|
| 2194 | * At this point, we have a list of all of the partitions on the dsf. Now try to
|
---|
| 2195 | * see which partitions have a file system on them.
|
---|
| 2196 | *
|
---|
| 2197 | * Loop through each partition on the disk and:
|
---|
| 2198 | *
|
---|
| 2199 | * - If the partition is swap, it ignores it.
|
---|
| 2200 | *
|
---|
| 2201 | * - If the partition is mounted (e.g. /dev/sda1 is mounted on /boot), it adds an entry
|
---|
| 2202 | * to the linked list, copies to it the device name and mount point, and sets check == 1.
|
---|
| 2203 | *
|
---|
| 2204 | * - If the partition is part of a Volume Group that has Logical Volumes mounted, it adds
|
---|
| 2205 | * an entry to the linked list for each mounted Logical Volume in that Volume Group, copying
|
---|
| 2206 | * to it the device name and mount point, and sets check == 1. Note that if the Volume Group
|
---|
| 2207 | * contains more than one disk, it will still add the entry even if the Logical Volume's
|
---|
| 2208 | * extents are not on the dsf that was passed in to the function. For example, Volume Group
|
---|
| 2209 | * VolGroup00 contains the disks /dev/sda1 and /dev/sdb1, and the Logical Volumes LogVol01,
|
---|
| 2210 | * which is mounted on /var and has all of its extents on /dev/sda1, and LogVol02, which is
|
---|
| 2211 | * mounted as /usr and has all of its extents on /dev/sdb1. If you pass /dev/sda into the
|
---|
| 2212 | * function, both /var and /usr will be archived even though /usr is actually on/dev/sdb.
|
---|
| 2213 | *
|
---|
| 2214 | * - If the partition is part of a Volume Group that has Logical Volumes used in a mounted
|
---|
| 2215 | * software raid device, it adds an entry to the linked list, copies to it the software raid
|
---|
| 2216 | * device name and mount point, and sets check == 1.
|
---|
| 2217 | *
|
---|
| 2218 | * - If the partition is part of a mounted software raid device, it adds an entry to the linked
|
---|
| 2219 | * list, copies to it the software raid device name and mount point, and sets check == 1.
|
---|
| 2220 | *
|
---|
| 2221 | ********/
|
---|
| 2222 | for (i=0; strlen(partitions[i]); i++) {
|
---|
| 2223 | log_msg(4, "Processing partition: %s", partitions[i]);
|
---|
| 2224 | /* See if it's swap. If it is, ignore it. */
|
---|
| 2225 | mr_asprintf(command, "parted2fdisk -l %s 2>/dev/null | awk '{if(($1==\"%s\")&&(toupper($0) ~ \"SWAP\")){print $1;exit}}'", dsf, partitions[i]);
|
---|
| 2226 | log_msg(4, " Running: %s", command);
|
---|
| 2227 | tmp = call_program_and_get_last_line_of_output(command);
|
---|
| 2228 | mr_free(command);
|
---|
| 2229 |
|
---|
| 2230 | log_msg(4, " Return value: %s", tmp);
|
---|
| 2231 | c = strlen(tmp);
|
---|
| 2232 | mr_free(tmp);
|
---|
| 2233 |
|
---|
| 2234 | if (c) {
|
---|
| 2235 | log_msg(4, "It's swap. Ignoring partition %s", partitions[i]);
|
---|
| 2236 | continue;
|
---|
| 2237 | }
|
---|
| 2238 | /* It's not swap. See if we can find the mount point from the mount command. */
|
---|
| 2239 | mr_asprintf(command, "mount 2>/dev/null | awk '{if((NF>0)&&($1==\"%s\")){print $3}}'", partitions[i]);
|
---|
| 2240 | tmp = call_program_and_get_last_line_of_output(command);
|
---|
| 2241 | mr_free(command);
|
---|
| 2242 |
|
---|
| 2243 | c = strlen(tmp);
|
---|
| 2244 | mr_free(tmp);
|
---|
| 2245 | if (c) {
|
---|
| 2246 | log_msg(4, " %s is mounted: %s", partitions[i], tmp);
|
---|
| 2247 | if ((DSFptr = find_mount_point_in_list(tmp)) == NULL) {
|
---|
| 2248 | log_msg (4, "Can't find mount point %s in mounted file systems list", tmp);
|
---|
| 2249 | return (1);
|
---|
| 2250 | }
|
---|
| 2251 | DSFptr->check = 1;
|
---|
| 2252 | continue;
|
---|
| 2253 | }
|
---|
| 2254 |
|
---|
| 2255 | /* It's not swap and it's not mounted. See if it's LVM */
|
---|
| 2256 | log_msg(4, " It's not mounted. Checking to see if it's LVM...");
|
---|
| 2257 |
|
---|
| 2258 | /* Get the partition ID; 8e for LVM */
|
---|
| 2259 | mr_asprintf(command, "parted2fdisk -l %s |awk '{if($1 ~ \"^%s\"){print $5}}'", dsf, partitions[i]);
|
---|
| 2260 | log_msg(4, " Running: %s", command);
|
---|
| 2261 | tmp = call_program_and_get_last_line_of_output(command);
|
---|
| 2262 | mr_free(command);
|
---|
| 2263 |
|
---|
| 2264 | c = strlen(tmp);
|
---|
| 2265 | mr_free(tmp);
|
---|
| 2266 | if (c) {
|
---|
| 2267 | log_msg(4, " Partition ID: %s", tmp);
|
---|
| 2268 | if (!strcasecmp(tmp, "8e")) {
|
---|
| 2269 | /* It's LVM: Find the VG it's in */
|
---|
| 2270 | log_msg(4, " It's LVM: Find the VG it's in...");
|
---|
| 2271 | mr_asprintf(command, "pvdisplay -v %s 2>/dev/null|grep \"VG Name\"|awk '{print $NF}'", partitions[i]);
|
---|
| 2272 | log_msg(4, " Running: %s", command);
|
---|
| 2273 | VG = call_program_and_get_last_line_of_output(command);
|
---|
| 2274 | mr_free(command);
|
---|
| 2275 |
|
---|
| 2276 | log_msg(4, " Volume Group: %s", VG);
|
---|
| 2277 | if (strlen(VG)) {
|
---|
| 2278 | /* Found the Volume Group. Now find all of the VG's mount points */
|
---|
| 2279 | log_msg(4, " Found the Volume Group. Now find all of the VG's mount points");
|
---|
| 2280 | mr_asprintf(command, "mount 2>/dev/null|grep -E \"/dev/mapper/%s-|/dev/%s/\"|awk '{printf(\"%%s \",$3)}END{print \"\"}'", VG, VG);
|
---|
| 2281 | log_msg(4, " Running: %s", command);
|
---|
| 2282 | mount_list = call_program_and_get_last_line_of_output(command);
|
---|
| 2283 | mr_free(command);
|
---|
| 2284 |
|
---|
| 2285 | log_msg(4, " VG %s mount_list: %s", VG, mount_list);
|
---|
| 2286 | lastpos = 0;
|
---|
| 2287 | while ((token = mr_strtok(mount_list, token_chars, &lastpos)) != NULL) {
|
---|
| 2288 | log_msg (5, "mount point token: %s", token);
|
---|
| 2289 | if ((DSFptr = find_mount_point_in_list(token)) == NULL) {
|
---|
| 2290 | log_msg (4, "Can't find mount point %s in mounted file systems list", token);
|
---|
| 2291 | mr_free(token);
|
---|
| 2292 | mr_free(VG);
|
---|
| 2293 | return (1);
|
---|
| 2294 | }
|
---|
| 2295 | DSFptr->check = 1;
|
---|
| 2296 | mr_free(token);
|
---|
| 2297 | }
|
---|
| 2298 | /********
|
---|
| 2299 | * Now we want to see if there are any software raid devices using
|
---|
| 2300 | * any of the Logical Volumes on the Volume Group.
|
---|
| 2301 | *******/
|
---|
| 2302 | mr_free(mount_list);
|
---|
| 2303 | mr_asprintf(command, "%s", "cat /proc/mdstat|grep -iv Personal|awk '{if($0~\"^.*[ ]+:\"){printf(\"/dev/%s \", $1)}}END{print \"\"}'");
|
---|
| 2304 | log_msg (5, "Running: %s", command);
|
---|
| 2305 | mount_list = call_program_and_get_last_line_of_output(command);
|
---|
| 2306 | mr_free(command);
|
---|
| 2307 |
|
---|
| 2308 | log_msg(4, " Software raid device list: %s", mount_list);
|
---|
| 2309 | lastpos = 0;
|
---|
| 2310 | while ((token = mr_strtok(mount_list, token_chars, &lastpos)) != NULL) {
|
---|
| 2311 | mr_asprintf(command, "mdadm --detail %s 2>/dev/null | grep -c %s", token, VG);
|
---|
| 2312 | log_msg (5, "Running: %s", command);
|
---|
| 2313 |
|
---|
| 2314 | tmp = call_program_and_get_last_line_of_output(command);
|
---|
| 2315 | paranoid_free(command);
|
---|
| 2316 | log_msg(4, "Number of Software raid device: %s", tmp);
|
---|
| 2317 | c = atoi(tmp);
|
---|
| 2318 | mr_free(tmp);
|
---|
| 2319 |
|
---|
| 2320 | if (c) {
|
---|
| 2321 | /* This device is on our disk */
|
---|
| 2322 | if ((DSFptr = find_device_in_list(token)) == NULL) {
|
---|
| 2323 | log_msg (4, "Can't find device %s in mounted file systems list", token);
|
---|
| 2324 | mr_free(token);
|
---|
| 2325 | return (1);
|
---|
| 2326 | }
|
---|
| 2327 | DSFptr->check = 1;
|
---|
| 2328 | }
|
---|
| 2329 | mr_free(token);
|
---|
| 2330 | }
|
---|
| 2331 | mr_free(token);
|
---|
| 2332 | mr_free(mount_list);
|
---|
| 2333 | } else {
|
---|
| 2334 | log_msg (4, "Error finding Volume Group for partition %s", partitions[i]);
|
---|
| 2335 | return (1);
|
---|
| 2336 | }
|
---|
| 2337 | continue;
|
---|
| 2338 | }
|
---|
| 2339 | } else {
|
---|
| 2340 | log_msg (4, "Error finding partition type for the partition %s", partitions[i]);
|
---|
| 2341 | }
|
---|
| 2342 | paranoid_free(tmp);
|
---|
| 2343 | /********
|
---|
| 2344 | * It's not swap, mounted, or LVM. See if it's used in a software raid device.
|
---|
| 2345 | ********/
|
---|
| 2346 | log_msg (5, "It's not swap, mounted, or LVM. See if it's used in a software raid device.");
|
---|
| 2347 | mr_asprintf(command, "mdadm --examine %s 2>/dev/null | awk '{if($1 == \"UUID\"){print $3}}'", partitions[i]);
|
---|
| 2348 | log_msg(4, " Running: %s", command);
|
---|
| 2349 | tmp = call_program_and_get_last_line_of_output(command);
|
---|
| 2350 | mr_free(command);
|
---|
| 2351 |
|
---|
| 2352 | c = strlen(tmp);
|
---|
| 2353 | mr_free(tmp);
|
---|
| 2354 | if (!c) {
|
---|
| 2355 | log_msg(4, " Partition %s is not used in a non-LVM software raid device", partitions[i]);
|
---|
| 2356 | continue;
|
---|
| 2357 | }
|
---|
| 2358 | log_msg (5, " UUID: %s", tmp);
|
---|
| 2359 | /* Get the Software raid device list */
|
---|
| 2360 | mr_asprintf(command, "%s", "cat /proc/mdstat|grep -iv Personal|awk '{if($0~\"^.*[ ]+:\"){printf(\"/dev/%s \", $1)}}END{print \"\"}'");
|
---|
| 2361 | log_msg (5, " Running: %s", command);
|
---|
| 2362 | mount_list = call_program_and_get_last_line_of_output(command);
|
---|
| 2363 | mr_free(command);
|
---|
| 2364 |
|
---|
| 2365 | log_msg(4, " Software raid device list: %s", mount_list);
|
---|
| 2366 | /* Loop through the software raid device list to see if we can find the partition */
|
---|
| 2367 | lastpos = 0;
|
---|
| 2368 | while ((token = mr_strtok(mount_list, token_chars, &lastpos)) != NULL) {
|
---|
| 2369 | mr_asprintf(command, "mdadm --detail %s 2>/dev/null | grep -c %s", token, tmp);
|
---|
| 2370 | log_msg(4, " Running: %s", command);
|
---|
| 2371 | tmp = call_program_and_get_last_line_of_output(command);
|
---|
| 2372 | mr_free(command);
|
---|
| 2373 | c = atoi(tmp);
|
---|
| 2374 | mr_free(tmp);
|
---|
| 2375 |
|
---|
| 2376 | if (!c) {
|
---|
| 2377 | log_msg (4," Didn't find partition %s in software raid device %s", partitions[i], token);
|
---|
| 2378 | } else {
|
---|
| 2379 | if ((DSFptr = find_device_in_list(token)) == NULL) {
|
---|
| 2380 | log_msg (4, "Can't find device %s in mounted file systems list", token);
|
---|
| 2381 | mr_free(token);
|
---|
| 2382 | return (1);
|
---|
| 2383 | }
|
---|
| 2384 | DSFptr->check = 1;
|
---|
| 2385 | break;
|
---|
| 2386 | }
|
---|
| 2387 | mr_free(token);
|
---|
| 2388 | }
|
---|
| 2389 | }
|
---|
| 2390 | mr_free(partition_list);
|
---|
| 2391 | mr_free(mount_list);
|
---|
| 2392 |
|
---|
| 2393 | /* Determine how much memory to allocate for included_dsf_list and excluded_dsf_list */
|
---|
| 2394 | i = 0;
|
---|
| 2395 | DSFptr= DSF_Head;
|
---|
| 2396 | while (DSFptr != NULL) {
|
---|
| 2397 | i += strlen(DSFptr->mount_point) + 1;
|
---|
| 2398 | DSFptr = DSFptr->next;
|
---|
| 2399 | }
|
---|
| 2400 | log_msg (5, "i: %d", i);
|
---|
| 2401 | if ((*included_dsf_list = (char *) calloc(i+100, sizeof(char))) == NULL) {
|
---|
| 2402 | fatal_error ("Cannot allocate memory");
|
---|
| 2403 | }
|
---|
| 2404 | if ((*excluded_dsf_list = (char *) calloc(i+100, sizeof(char))) == NULL) {
|
---|
| 2405 | fatal_error ("Cannot allocate memory");
|
---|
| 2406 | }
|
---|
| 2407 | DSFptr= DSF_Head;
|
---|
| 2408 | while (DSFptr != NULL) {
|
---|
| 2409 | if (DSFptr->check) {
|
---|
| 2410 | log_msg (5, "%s is mounted on %s and is on disk %s\n", DSFptr->device, DSFptr->mount_point, dsf);
|
---|
| 2411 | strcat(*included_dsf_list, DSFptr->mount_point);
|
---|
| 2412 | strcat(*included_dsf_list, " ");
|
---|
| 2413 | } else {
|
---|
| 2414 | log_msg (4, "%s is mounted on %s and is NOT on disk %s\n", DSFptr->device, DSFptr->mount_point, dsf);
|
---|
| 2415 | strcat(*excluded_dsf_list, DSFptr->mount_point);
|
---|
| 2416 | strcat(*excluded_dsf_list, " ");
|
---|
| 2417 | }
|
---|
| 2418 | DSFptr = DSFptr->next;
|
---|
| 2419 | }
|
---|
| 2420 | log_msg (5, "included_dsf_list: %s", *included_dsf_list);
|
---|
| 2421 | log_msg (5, "excluded_dsf_list: %s", *excluded_dsf_list);
|
---|
| 2422 | return (0);
|
---|
| 2423 | }
|
---|
| 2424 |
|
---|
| 2425 |
|
---|
| 2426 | /* Update the bkpinfo structure for exclude & include paths
|
---|
| 2427 | * in order to handle correctly paths corresponding to devices */
|
---|
| 2428 | void mr_make_devlist_from_pathlist(char *pathlist, char mode) {
|
---|
| 2429 |
|
---|
| 2430 | char *token = NULL;
|
---|
| 2431 | int lastpos = 0;
|
---|
| 2432 | char *mounted_on_dsf = NULL;
|
---|
| 2433 | char *not_mounted_on_dsf = NULL;
|
---|
| 2434 | char token_chars[] =" \t\r\f\a\0";
|
---|
| 2435 |
|
---|
| 2436 | while ((token = mr_strtok(pathlist, token_chars, &lastpos)) != NULL) {
|
---|
| 2437 | switch (get_dsf_mount_list(token, &mounted_on_dsf, ¬_mounted_on_dsf)) {
|
---|
| 2438 | case 1:
|
---|
| 2439 | if (mode == 'E') {
|
---|
| 2440 | log_msg(1, "WARNING ! %s doesn't exist in -E option", token);
|
---|
| 2441 | } else {
|
---|
| 2442 | fatal_error("Error processing -I option");
|
---|
| 2443 | }
|
---|
| 2444 | break;
|
---|
| 2445 | /* Everything is OK; proceed to archive data */
|
---|
| 2446 | case 0:
|
---|
| 2447 | if (mode == 'E') {
|
---|
| 2448 | if (strlen(mounted_on_dsf)) {
|
---|
| 2449 | log_to_screen("Excluding the following file systems on %s:\n", token);
|
---|
| 2450 | log_to_screen(" %s\n", mounted_on_dsf);
|
---|
| 2451 | log_msg (5, "Adding to bkpinfo->exclude_paths due to -E option: %s", mounted_on_dsf);
|
---|
| 2452 | mr_strcat(bkpinfo->exclude_paths, "%s ", mounted_on_dsf);
|
---|
| 2453 | mr_strcat(bkpinfo->exclude_devs, " %s ", token);
|
---|
| 2454 | } else {
|
---|
| 2455 | log_to_screen("Archiving only the following file systems on %s:\n", token);
|
---|
| 2456 | log_to_screen(" %s\n", mounted_on_dsf);
|
---|
| 2457 | mr_asprintf(bkpinfo->include_paths, "/");
|
---|
| 2458 | if (strlen(not_mounted_on_dsf)) {
|
---|
| 2459 | log_msg (5, "Adding to bkpinfo->exclude_paths due to -I option: %s", not_mounted_on_dsf);
|
---|
| 2460 | log_to_screen("Not archiving the following file systems:\n");
|
---|
| 2461 | log_to_screen(" %s\n", not_mounted_on_dsf);
|
---|
| 2462 | mr_strcat(bkpinfo->exclude_paths, "%s ", not_mounted_on_dsf);
|
---|
| 2463 | }
|
---|
| 2464 | }
|
---|
| 2465 | }
|
---|
| 2466 | break;
|
---|
| 2467 | /* It's a dsf but not a whole disk dsf */
|
---|
| 2468 | case -2:
|
---|
| 2469 | log_to_screen("Could %s be a partition instead of a whole disk device special file?\nIgnored.", token);
|
---|
| 2470 | break;
|
---|
| 2471 | /* A device special file was not passed in. Process it as a path. */
|
---|
| 2472 | case -1:
|
---|
| 2473 | if (mode == 'E') {
|
---|
| 2474 | mr_strcat(bkpinfo->exclude_paths, "%s ", token);
|
---|
| 2475 | } else {
|
---|
| 2476 | mr_strcat(bkpinfo->include_paths, "%s ", token);
|
---|
| 2477 | }
|
---|
| 2478 | break;
|
---|
| 2479 | }
|
---|
| 2480 | mr_free(token);
|
---|
| 2481 |
|
---|
| 2482 | if (bkpinfo->include_paths != NULL) {
|
---|
| 2483 | mr_strip_spaces(bkpinfo->include_paths);
|
---|
| 2484 | log_msg(1, "include_paths is now '%s'", bkpinfo->include_paths);
|
---|
| 2485 | }
|
---|
| 2486 | if (bkpinfo->exclude_paths != NULL) {
|
---|
| 2487 | mr_strip_spaces(bkpinfo->exclude_paths);
|
---|
| 2488 | log_msg(1, "exclude_paths is now '%s'", bkpinfo->exclude_paths);
|
---|
| 2489 | }
|
---|
| 2490 | if (bkpinfo->exclude_devs != NULL) {
|
---|
| 2491 | mr_strip_spaces(bkpinfo->exclude_devs);
|
---|
| 2492 | log_msg(1, "exclude_devs is now '%s'", bkpinfo->exclude_devs);
|
---|
| 2493 | }
|
---|
| 2494 | }
|
---|
| 2495 | }
|
---|
| 2496 |
|
---|
| 2497 |
|
---|
| 2498 |
|
---|
| 2499 | /**
|
---|
[1] | 2500 | * Ask user for details of backup/restore information.
|
---|
| 2501 | * Called when @c mondoarchive doesn't get any parameters.
|
---|
| 2502 | * @param bkpinfo The backup information structure to fill out with the user's data.
|
---|
| 2503 | * @param archiving_to_media TRUE if archiving, FALSE if restoring.
|
---|
| 2504 | * @return 0, always.
|
---|
| 2505 | * @bug No point of `int' return value.
|
---|
| 2506 | * @ingroup archiveGroup
|
---|
| 2507 | */
|
---|
[1645] | 2508 | int interactively_obtain_media_parameters_from_user(bool archiving_to_media)
|
---|
[1] | 2509 | // archiving_to_media is TRUE if I'm being called by mondoarchive
|
---|
| 2510 | // archiving_to_media is FALSE if I'm being called by mondorestore
|
---|
| 2511 | {
|
---|
[2214] | 2512 | char *tmp = NULL;
|
---|
[2316] | 2513 | char *p = NULL;
|
---|
[2242] | 2514 | char *mds = NULL;
|
---|
[2266] | 2515 | char *sz_size = NULL;
|
---|
| 2516 | char *command = NULL;
|
---|
| 2517 | char *comment = NULL;
|
---|
[128] | 2518 | int i;
|
---|
| 2519 | FILE *fin;
|
---|
[1] | 2520 |
|
---|
[128] | 2521 | assert(bkpinfo != NULL);
|
---|
| 2522 | bkpinfo->nonbootable_backup = FALSE;
|
---|
[1] | 2523 |
|
---|
[2382] | 2524 | // Tape, CD, NETFS, ...?
|
---|
[128] | 2525 | srandom(getpid());
|
---|
| 2526 | bkpinfo->backup_media_type =
|
---|
| 2527 | (g_restoring_live_from_cd) ? cdr :
|
---|
| 2528 | which_backup_media_type(bkpinfo->restore_data);
|
---|
| 2529 | if (bkpinfo->backup_media_type == none) {
|
---|
[541] | 2530 | log_to_screen("User has chosen not to backup the PC");
|
---|
[128] | 2531 | finish(1);
|
---|
| 2532 | }
|
---|
[1934] | 2533 | /* Why asking to remove the media with tape ?
|
---|
[128] | 2534 | if (bkpinfo->backup_media_type == tape && bkpinfo->restore_data) {
|
---|
[1885] | 2535 | popup_and_OK("Please remove media from drive(s)");
|
---|
[128] | 2536 | }
|
---|
[1934] | 2537 | */
|
---|
[2330] | 2538 | tmp = bkptype_to_string(bkpinfo->backup_media_type);
|
---|
| 2539 | log_msg(3, "media type = %s", tmp);
|
---|
| 2540 | mr_free(tmp);
|
---|
| 2541 |
|
---|
[128] | 2542 | bkpinfo->cdrw_speed = (bkpinfo->backup_media_type == cdstream) ? 2 : 4;
|
---|
[2330] | 2543 | bkpinfo->compression_level = (bkpinfo->backup_media_type == cdstream) ? 1 : 5;
|
---|
| 2544 | bkpinfo->use_lzo = (bkpinfo->backup_media_type == cdstream) ? TRUE : FALSE;
|
---|
[128] | 2545 | mvaddstr_and_log_it(2, 0, " ");
|
---|
[1] | 2546 |
|
---|
[1934] | 2547 | // Find device's /dev (or SCSI) entry
|
---|
[128] | 2548 | switch (bkpinfo->backup_media_type) {
|
---|
| 2549 | case cdr:
|
---|
| 2550 | case cdrw:
|
---|
| 2551 | case dvd:
|
---|
[1687] | 2552 | case usb:
|
---|
[1745] | 2553 | /* Never try to eject a USB device */
|
---|
| 2554 | if (bkpinfo->backup_media_type == usb) {
|
---|
[1746] | 2555 | bkpinfo->please_dont_eject = TRUE;
|
---|
[1745] | 2556 | }
|
---|
[128] | 2557 | if (archiving_to_media) {
|
---|
[1687] | 2558 | if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) {
|
---|
[2405] | 2559 | if (ask_me_yes_or_no("Is your computer a laptop, or does the CD writer incorporate BurnProof technology?")) {
|
---|
[685] | 2560 | bkpinfo->manual_cd_tray = TRUE;
|
---|
| 2561 | }
|
---|
[128] | 2562 | }
|
---|
[2405] | 2563 | if ((bkpinfo->compression_level = which_compression_level()) == -1) {
|
---|
[541] | 2564 | log_to_screen("User has chosen not to backup the PC");
|
---|
[128] | 2565 | finish(1);
|
---|
| 2566 | }
|
---|
[2242] | 2567 | mds = media_descriptor_string(bkpinfo->backup_media_type);
|
---|
[2323] | 2568 | mr_asprintf(comment, "What speed is your %s (re)writer?", mds);
|
---|
[2325] | 2569 | mr_free(bkpinfo->media_device);
|
---|
[128] | 2570 | if (bkpinfo->backup_media_type == dvd) {
|
---|
[2325] | 2571 | bkpinfo->media_device = find_dvd_device();
|
---|
[2323] | 2572 | mr_asprintf(tmp, "1");
|
---|
| 2573 | mr_asprintf(sz_size, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4482 MB
|
---|
[128] | 2574 | log_msg(1, "Setting to DVD defaults");
|
---|
| 2575 | } else {
|
---|
[2325] | 2576 | mr_asprintf(bkpinfo->media_device, "%s", VANILLA_SCSI_CDROM);
|
---|
[2323] | 2577 | mr_asprintf(tmp, "4");
|
---|
| 2578 | mr_asprintf(sz_size, "%d", 650);
|
---|
[128] | 2579 | log_msg(1, "Setting to CD defaults");
|
---|
| 2580 | }
|
---|
[1687] | 2581 | if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) {
|
---|
[2316] | 2582 | p = popup_and_get_string("Speed", comment, tmp);
|
---|
| 2583 | mr_free(tmp);
|
---|
| 2584 |
|
---|
| 2585 | if (p == NULL) {
|
---|
[541] | 2586 | log_to_screen("User has chosen not to backup the PC");
|
---|
[2266] | 2587 | mr_free(comment);
|
---|
[128] | 2588 | finish(1);
|
---|
| 2589 | }
|
---|
[2316] | 2590 | /* tmp now has the new value given by the user */
|
---|
| 2591 | tmp = p;
|
---|
[128] | 2592 | }
|
---|
[2266] | 2593 | mr_free(comment);
|
---|
| 2594 |
|
---|
[2405] | 2595 | bkpinfo->cdrw_speed = atoi(tmp); // if DVD then this shouldn't ever be used anyway :)
|
---|
| 2596 | mr_free(tmp);
|
---|
[2214] | 2597 |
|
---|
[2323] | 2598 | mr_asprintf(comment, "How much data (in Megabytes) will each %s store?", mds);
|
---|
[2242] | 2599 | mr_free(mds);
|
---|
[2316] | 2600 | p = popup_and_get_string("Size", comment, sz_size);
|
---|
| 2601 | mr_free(sz_size);
|
---|
| 2602 | mr_free(comment);
|
---|
| 2603 |
|
---|
| 2604 | if (p == NULL) {
|
---|
[541] | 2605 | log_to_screen("User has chosen not to backup the PC");
|
---|
[128] | 2606 | finish(1);
|
---|
| 2607 | }
|
---|
[2316] | 2608 | sz_size = p;
|
---|
[2266] | 2609 |
|
---|
[128] | 2610 | for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
|
---|
| 2611 | bkpinfo->media_size[i] = atoi(sz_size);
|
---|
| 2612 | }
|
---|
[2266] | 2613 |
|
---|
[128] | 2614 | if (bkpinfo->media_size[0] <= 0) {
|
---|
[541] | 2615 | log_to_screen("User has chosen not to backup the PC");
|
---|
[128] | 2616 | finish(1);
|
---|
| 2617 | }
|
---|
| 2618 | }
|
---|
[1737] | 2619 | /* No break because we continue even for usb */
|
---|
[128] | 2620 | case cdstream:
|
---|
[2242] | 2621 | mds = media_descriptor_string(bkpinfo->backup_media_type);
|
---|
| 2622 |
|
---|
[2325] | 2623 | mr_free(bkpinfo->media_device);
|
---|
[1737] | 2624 | if ((bkpinfo->disaster_recovery) && (bkpinfo->backup_media_type != usb)) {
|
---|
[2325] | 2625 | mr_asprintf(bkpinfo->media_device, "/dev/cdrom");
|
---|
| 2626 | log_msg(2, "CD-ROM device assumed to be at %s", bkpinfo->media_device);
|
---|
| 2627 | } else if ((bkpinfo->restore_data && (bkpinfo->backup_media_type != usb)) || bkpinfo->backup_media_type == dvd) {
|
---|
| 2628 | if ((bkpinfo->backup_media_type == dvd) || ((bkpinfo->media_device = find_cdrom_device(FALSE)) != NULL)) {
|
---|
[2323] | 2629 | mr_asprintf(comment, "Please specify your %s drive's /dev entry", mds);
|
---|
[2316] | 2630 | p = popup_and_get_string("Device?", comment, bkpinfo->media_device);
|
---|
| 2631 | mr_free(comment);
|
---|
| 2632 |
|
---|
| 2633 | if (p == NULL) {
|
---|
[541] | 2634 | log_to_screen("User has chosen not to backup the PC");
|
---|
[128] | 2635 | finish(1);
|
---|
| 2636 | }
|
---|
[2325] | 2637 | mr_free(bkpinfo->media_device);
|
---|
| 2638 | bkpinfo->media_device = p;
|
---|
[128] | 2639 | }
|
---|
[2325] | 2640 | if (bkpinfo->media_device != NULL) {
|
---|
| 2641 | log_msg(2, "%s device found at %s", mds, bkpinfo->media_device);
|
---|
| 2642 | } else {
|
---|
| 2643 | log_msg(2, "%s device not found (bkpinfo->media_device is NULL)", mds);
|
---|
| 2644 | }
|
---|
[128] | 2645 | } else {
|
---|
[2325] | 2646 | if (((bkpinfo->media_device = find_cdrw_device()) != NULL) && (bkpinfo->backup_media_type != usb)) {
|
---|
| 2647 | mr_free(bkpinfo->media_device);
|
---|
[128] | 2648 | }
|
---|
[2325] | 2649 | if (bkpinfo->media_device != NULL) {
|
---|
[1738] | 2650 | if (bkpinfo->backup_media_type == usb) {
|
---|
[2323] | 2651 | mr_asprintf(tmp, "I think your %s media corresponds to %s. Is this correct?", mds, bkpinfo->media_device);
|
---|
[1738] | 2652 | } else {
|
---|
[2323] | 2653 | 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] | 2654 | }
|
---|
[128] | 2655 | if (!ask_me_yes_or_no(tmp)) {
|
---|
[2325] | 2656 | mr_free(bkpinfo->media_device);
|
---|
[128] | 2657 | }
|
---|
[2214] | 2658 | mr_free(tmp);
|
---|
[128] | 2659 | }
|
---|
[2325] | 2660 | if (!bkpinfo->media_device) {
|
---|
[1737] | 2661 | if (bkpinfo->backup_media_type == usb) {
|
---|
[2325] | 2662 | p = popup_and_get_string("/dev entry?", "What is the /dev entry of your USB Disk/Key, please?", NULL);
|
---|
[1737] | 2663 | } else {
|
---|
| 2664 | if (g_kernel_version < 2.6) {
|
---|
[2325] | 2665 | p = popup_and_get_string("Device node?", "What is the SCSI node of your CD (re)writer, please?", NULL);
|
---|
[1709] | 2666 | } else {
|
---|
[2325] | 2667 | p = popup_and_get_string("/dev entry?", "What is the /dev entry of your CD (re)writer, please?", NULL);
|
---|
[1709] | 2668 | }
|
---|
[128] | 2669 | }
|
---|
[2316] | 2670 | if (p == NULL) {
|
---|
[541] | 2671 | log_to_screen("User has chosen not to backup the PC");
|
---|
[128] | 2672 | finish(1);
|
---|
| 2673 | }
|
---|
[2325] | 2674 | bkpinfo->media_device = p;
|
---|
[128] | 2675 | }
|
---|
| 2676 | }
|
---|
[2242] | 2677 | mr_free(mds);
|
---|
| 2678 |
|
---|
[128] | 2679 | if (bkpinfo->backup_media_type == cdstream) {
|
---|
| 2680 | for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
|
---|
| 2681 | bkpinfo->media_size[i] = 650;
|
---|
| 2682 | }
|
---|
| 2683 | }
|
---|
| 2684 | break;
|
---|
| 2685 | case udev:
|
---|
| 2686 | if (!ask_me_yes_or_no
|
---|
[541] | 2687 | ("This option is for advanced users only. Are you sure?")) {
|
---|
| 2688 | log_to_screen("User has chosen not to backup the PC");
|
---|
[128] | 2689 | finish(1);
|
---|
| 2690 | }
|
---|
| 2691 | case tape:
|
---|
[1] | 2692 |
|
---|
[2325] | 2693 | mr_free(bkpinfo->media_device);
|
---|
[2334] | 2694 | if ((!bkpinfo->restore_mode) && ((bkpinfo->media_device = mr_find_tape_device()) == NULL)) {
|
---|
[541] | 2695 | log_msg(3, "Ok, using vanilla scsi tape.");
|
---|
[2325] | 2696 | mr_asprintf(bkpinfo->media_device, "%s", VANILLA_SCSI_TAPE);
|
---|
[128] | 2697 | if ((fin = fopen(bkpinfo->media_device, "r"))) {
|
---|
| 2698 | paranoid_fclose(fin);
|
---|
| 2699 | } else {
|
---|
[2325] | 2700 | mr_free(bkpinfo->media_device);
|
---|
| 2701 | mr_asprintf(bkpinfo->media_device, "/dev/osst0");
|
---|
[128] | 2702 | }
|
---|
| 2703 | }
|
---|
[2325] | 2704 | if (bkpinfo->media_device != NULL) {
|
---|
[128] | 2705 | if ((fin = fopen(bkpinfo->media_device, "r"))) {
|
---|
| 2706 | paranoid_fclose(fin);
|
---|
| 2707 | } else {
|
---|
| 2708 | if (does_file_exist("/tmp/mondo-restore.cfg")) {
|
---|
[2325] | 2709 | mr_free(bkpinfo->media_device);
|
---|
| 2710 | bkpinfo->media_device = read_cfg_var("/tmp/mondo-restore.cfg", "media-dev");
|
---|
[128] | 2711 | }
|
---|
| 2712 | }
|
---|
| 2713 | }
|
---|
[2325] | 2714 | if (bkpinfo->media_device != NULL) {
|
---|
[2323] | 2715 | mr_asprintf(tmp, "I think I've found your tape streamer at %s; am I right on the money?", bkpinfo->media_device);
|
---|
[128] | 2716 | if (!ask_me_yes_or_no(tmp)) {
|
---|
[2325] | 2717 | mr_free(bkpinfo->media_device);
|
---|
[128] | 2718 | }
|
---|
[2214] | 2719 | mr_free(tmp);
|
---|
[128] | 2720 | }
|
---|
[2325] | 2721 | if (bkpinfo->media_device == NULL) {
|
---|
[2316] | 2722 | p = popup_and_get_string("Device name?", "What is the /dev entry of your tape streamer?", bkpinfo->media_device);
|
---|
| 2723 | if (p == NULL) {
|
---|
[541] | 2724 | log_to_screen("User has chosen not to backup the PC");
|
---|
[128] | 2725 | finish(1);
|
---|
| 2726 | }
|
---|
[2325] | 2727 | bkpinfo->media_device = p;
|
---|
[128] | 2728 | }
|
---|
[2323] | 2729 | mr_asprintf(tmp, "ls -l %s", bkpinfo->media_device);
|
---|
[128] | 2730 | if (run_program_and_log_output(tmp, FALSE)) {
|
---|
[541] | 2731 | log_to_screen("User has not specified a valid /dev entry");
|
---|
[128] | 2732 | finish(1);
|
---|
| 2733 | }
|
---|
[2214] | 2734 | mr_free(tmp);
|
---|
| 2735 |
|
---|
[2334] | 2736 | bkpinfo->use_obdr = ask_me_yes_or_no("Do you want to activate OBDR support for your tapes ?");
|
---|
[2266] | 2737 | bkpinfo->media_size[0] = 0;
|
---|
[128] | 2738 | log_msg(4, "media_size[0] = %ld", bkpinfo->media_size[0]);
|
---|
| 2739 | if (bkpinfo->media_size[0] <= 0) {
|
---|
| 2740 | bkpinfo->media_size[0] = 0;
|
---|
| 2741 | }
|
---|
| 2742 | for (i = 1; i <= MAX_NOOF_MEDIA; i++) {
|
---|
| 2743 | bkpinfo->media_size[i] = bkpinfo->media_size[0];
|
---|
| 2744 | }
|
---|
| 2745 | if (archiving_to_media) {
|
---|
| 2746 | if ((bkpinfo->compression_level =
|
---|
| 2747 | which_compression_level()) == -1) {
|
---|
[541] | 2748 | log_to_screen("User has chosen not to backup the PC");
|
---|
[128] | 2749 | finish(1);
|
---|
| 2750 | }
|
---|
| 2751 | }
|
---|
| 2752 | break;
|
---|
[1] | 2753 |
|
---|
| 2754 |
|
---|
| 2755 |
|
---|
[2382] | 2756 | case netfs:
|
---|
| 2757 | /* Never try to eject a NETFS device */
|
---|
[1848] | 2758 | bkpinfo->please_dont_eject = TRUE;
|
---|
| 2759 |
|
---|
[2382] | 2760 | /* Initiate bkpinfo netfs_mount path from running environment if not already done */
|
---|
| 2761 | if (bkpinfo->netfs_mount == NULL) {
|
---|
[2383] | 2762 | bkpinfo->netfs_mount = call_program_and_get_last_line_of_output("mount | grep \":\" | cut -d' ' -f1 | head -n1");
|
---|
[128] | 2763 | }
|
---|
[1] | 2764 | #ifdef __FreeBSD__
|
---|
[128] | 2765 | if (TRUE)
|
---|
[1] | 2766 | #else
|
---|
[128] | 2767 | if (!bkpinfo->disaster_recovery)
|
---|
[1] | 2768 | #endif
|
---|
[128] | 2769 | {
|
---|
[2382] | 2770 | p = popup_and_get_string("Network shared 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->netfs_mount);
|
---|
[2316] | 2771 | if (p == NULL) {
|
---|
[541] | 2772 | log_to_screen("User has chosen not to backup the PC");
|
---|
[128] | 2773 | finish(1);
|
---|
| 2774 | }
|
---|
[2382] | 2775 | mr_free(bkpinfo->netfs_mount);
|
---|
| 2776 | bkpinfo->netfs_mount = p;
|
---|
[128] | 2777 | if (!bkpinfo->restore_data) {
|
---|
| 2778 | if ((bkpinfo->compression_level =
|
---|
| 2779 | which_compression_level()) == -1) {
|
---|
[541] | 2780 | log_to_screen("User has chosen not to backup the PC");
|
---|
[128] | 2781 | finish(1);
|
---|
| 2782 | }
|
---|
| 2783 | }
|
---|
| 2784 | // check whether already mounted - we better remove
|
---|
| 2785 | // surrounding spaces and trailing '/' for this
|
---|
[2382] | 2786 | mr_strip_spaces(bkpinfo->netfs_mount);
|
---|
| 2787 | if (bkpinfo->netfs_mount[strlen(bkpinfo->netfs_mount) - 1] == '/')
|
---|
| 2788 | bkpinfo->netfs_mount[strlen(bkpinfo->netfs_mount) - 1] = '\0';
|
---|
| 2789 | mr_asprintf(command, "mount | grep \"%s \" | cut -d' ' -f3", bkpinfo->netfs_mount);
|
---|
[2323] | 2790 | mr_free(bkpinfo->isodir);
|
---|
[2383] | 2791 | bkpinfo->isodir = call_program_and_get_last_line_of_output(command);
|
---|
[2266] | 2792 | mr_free(command);
|
---|
[256] | 2793 |
|
---|
[1847] | 2794 | if (!bkpinfo->restore_data) {
|
---|
[2323] | 2795 | mr_asprintf(comment, "How much data (in Megabytes) will each media store?");
|
---|
[2316] | 2796 | // BERLIOS: 4480 shouldn't be hardcoded here
|
---|
| 2797 | sz_size = popup_and_get_string("Size", comment, "4480");
|
---|
| 2798 | mr_free(comment);
|
---|
| 2799 | if (sz_size == NULL) {
|
---|
[1847] | 2800 | log_to_screen("User has chosen not to backup the PC");
|
---|
| 2801 | finish(1);
|
---|
| 2802 | }
|
---|
| 2803 | } else {
|
---|
[2323] | 2804 | mr_asprintf(sz_size, "0");
|
---|
[256] | 2805 | }
|
---|
| 2806 | for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
|
---|
| 2807 | bkpinfo->media_size[i] = atoi(sz_size);
|
---|
| 2808 | }
|
---|
[2316] | 2809 | mr_free(sz_size);
|
---|
[1917] | 2810 | if (bkpinfo->media_size[0] < 0) {
|
---|
[541] | 2811 | log_to_screen("User has chosen not to backup the PC");
|
---|
[256] | 2812 | finish(1);
|
---|
| 2813 | }
|
---|
[128] | 2814 | }
|
---|
| 2815 | if (bkpinfo->disaster_recovery) {
|
---|
[2323] | 2816 | mr_asprintf(command ,"umount %s/isodir 2> /dev/null", bkpinfo->tmpdir);
|
---|
[2202] | 2817 | (void)system(command);
|
---|
[2266] | 2818 | mr_free(command);
|
---|
| 2819 |
|
---|
[2400] | 2820 | p = popup_and_get_string("Network protocol", "Which Network protocol should I use?", bkpinfo->netfs_proto);
|
---|
| 2821 | if (p == NULL) {
|
---|
| 2822 | log_to_screen("User has chosen not to backup the PC");
|
---|
| 2823 | finish(1);
|
---|
| 2824 | }
|
---|
| 2825 | mr_free(bkpinfo->netfs_proto);
|
---|
| 2826 | bkpinfo->netfs_proto = p;
|
---|
| 2827 |
|
---|
[2382] | 2828 | p = popup_and_get_string("Network share", "Which remote Network share should I mount?", bkpinfo->netfs_mount);
|
---|
[2316] | 2829 | if (p == NULL) {
|
---|
[541] | 2830 | log_to_screen("User has chosen not to backup the PC");
|
---|
[128] | 2831 | finish(1);
|
---|
| 2832 | }
|
---|
[2382] | 2833 | mr_free(bkpinfo->netfs_mount);
|
---|
| 2834 | bkpinfo->netfs_mount = p;
|
---|
[128] | 2835 | }
|
---|
[1858] | 2836 | /* Initiate bkpinfo isodir path from running environment if mount already done */
|
---|
[2323] | 2837 | mr_free(bkpinfo->isodir);
|
---|
[2382] | 2838 | if (is_this_device_mounted(bkpinfo->netfs_mount)) {
|
---|
[2383] | 2839 | bkpinfo->isodir = call_program_and_get_last_line_of_output("mount | grep \":\" | cut -d' ' -f3 | head -n1");
|
---|
[2323] | 2840 |
|
---|
[1858] | 2841 | } else {
|
---|
[2382] | 2842 | mr_asprintf(bkpinfo->isodir, "%s/netfsdir", bkpinfo->tmpdir);
|
---|
[2323] | 2843 | mr_asprintf(command, "mkdir -p %s", bkpinfo->isodir);
|
---|
[128] | 2844 | run_program_and_log_output(command, 5);
|
---|
[2266] | 2845 | mr_free(command);
|
---|
| 2846 |
|
---|
[2364] | 2847 | if (bkpinfo->restore_data) {
|
---|
[2382] | 2848 | if (strstr(bkpinfo->netfs_proto, "sshfs")) {
|
---|
| 2849 | mr_asprintf(tmp, "sshfs -o ro %s %s", bkpinfo->netfs_mount, bkpinfo->isodir);
|
---|
| 2850 | } else {
|
---|
| 2851 | mr_asprintf(tmp, "mount -t %s -o nolock,ro %s %s", bkpinfo->netfs_proto, bkpinfo->netfs_mount, bkpinfo->isodir);
|
---|
| 2852 | }
|
---|
[2370] | 2853 | } else {
|
---|
[2382] | 2854 | if (strstr(bkpinfo->netfs_proto, "sshfs")) {
|
---|
| 2855 | mr_asprintf(tmp, "sshfs %s %s", bkpinfo->netfs_mount, bkpinfo->isodir);
|
---|
| 2856 | } else {
|
---|
| 2857 | mr_asprintf(tmp, "mount -t %s -o nolock %s %s", bkpinfo->netfs_proto, bkpinfo->netfs_mount, bkpinfo->isodir);
|
---|
| 2858 | }
|
---|
[2364] | 2859 | }
|
---|
[1819] | 2860 | run_program_and_log_output(tmp, 3);
|
---|
[2214] | 2861 | mr_free(tmp);
|
---|
| 2862 |
|
---|
[128] | 2863 | malloc_string(g_selfmounted_isodir);
|
---|
| 2864 | strcpy(g_selfmounted_isodir, bkpinfo->isodir);
|
---|
| 2865 | }
|
---|
[2382] | 2866 | if (!is_this_device_mounted(bkpinfo->netfs_mount)) {
|
---|
[2324] | 2867 | popup_and_OK("Please mount that partition before you try to backup to or restore from it.");
|
---|
[128] | 2868 | finish(1);
|
---|
| 2869 | }
|
---|
[2382] | 2870 | p = popup_and_get_string("Directory", "Which directory within that mountpoint?", bkpinfo->netfs_remote_dir);
|
---|
[2316] | 2871 | if (p == NULL) {
|
---|
[541] | 2872 | log_to_screen("User has chosen not to backup the PC");
|
---|
[128] | 2873 | finish(1);
|
---|
| 2874 | }
|
---|
[2382] | 2875 | mr_free(bkpinfo->netfs_remote_dir);
|
---|
| 2876 | bkpinfo->netfs_remote_dir = p;
|
---|
[2214] | 2877 |
|
---|
[128] | 2878 | // check whether writable - we better remove surrounding spaces for this
|
---|
[2382] | 2879 | mr_strip_spaces(bkpinfo->netfs_remote_dir);
|
---|
[1767] | 2880 |
|
---|
[2316] | 2881 | 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);
|
---|
| 2882 | if (p == NULL) {
|
---|
[541] | 2883 | log_to_screen("User has chosen not to backup the PC");
|
---|
[251] | 2884 | finish(1);
|
---|
| 2885 | }
|
---|
[2322] | 2886 | mr_free(bkpinfo->prefix);
|
---|
| 2887 | bkpinfo->prefix = p;
|
---|
[251] | 2888 | log_msg(3, "prefix set to %s", bkpinfo->prefix);
|
---|
[99] | 2889 |
|
---|
[2382] | 2890 | log_msg(3, "Just set netfs_remote_dir to %s", bkpinfo->netfs_remote_dir);
|
---|
[128] | 2891 | log_msg(3, "isodir is still %s", bkpinfo->isodir);
|
---|
| 2892 | break;
|
---|
[1] | 2893 |
|
---|
[128] | 2894 | case iso:
|
---|
| 2895 | if (!bkpinfo->disaster_recovery) {
|
---|
[2316] | 2896 | 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);
|
---|
| 2897 | if (p == NULL) {
|
---|
[541] | 2898 | log_to_screen("User has chosen not to backup the PC");
|
---|
[128] | 2899 | finish(1);
|
---|
| 2900 | }
|
---|
[2323] | 2901 | bkpinfo->isodir = p;
|
---|
[2316] | 2902 |
|
---|
[128] | 2903 | if (archiving_to_media) {
|
---|
| 2904 | if ((bkpinfo->compression_level =
|
---|
| 2905 | which_compression_level()) == -1) {
|
---|
[541] | 2906 | log_to_screen("User has chosen not to backup the PC");
|
---|
[128] | 2907 | finish(1);
|
---|
| 2908 | }
|
---|
[2316] | 2909 | 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);
|
---|
| 2910 | if (p == NULL) {
|
---|
[541] | 2911 | log_to_screen("User has chosen not to backup the PC");
|
---|
[128] | 2912 | finish(1);
|
---|
| 2913 | }
|
---|
| 2914 | for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
|
---|
[2316] | 2915 | bkpinfo->media_size[i] = atoi(p);
|
---|
[128] | 2916 | }
|
---|
[2316] | 2917 | mr_free(p);
|
---|
[128] | 2918 | } else {
|
---|
| 2919 | for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
|
---|
| 2920 | bkpinfo->media_size[i] = 650;
|
---|
| 2921 | }
|
---|
| 2922 | }
|
---|
| 2923 | }
|
---|
[2316] | 2924 | 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);
|
---|
| 2925 | if (p == NULL) {
|
---|
[804] | 2926 | log_to_screen("User has chosen not to backup the PC");
|
---|
| 2927 | finish(1);
|
---|
| 2928 | }
|
---|
[2322] | 2929 | mr_free(bkpinfo->prefix);
|
---|
| 2930 | bkpinfo->prefix = p;
|
---|
[804] | 2931 | log_msg(3, "prefix set to %s", bkpinfo->prefix);
|
---|
[128] | 2932 | break;
|
---|
[2322] | 2933 |
|
---|
[128] | 2934 | default:
|
---|
| 2935 | fatal_error
|
---|
| 2936 | ("I, Mojo Jojo, shall defeat those pesky Powerpuff Girls!");
|
---|
| 2937 | }
|
---|
[1] | 2938 |
|
---|
[128] | 2939 | if (archiving_to_media) {
|
---|
[1] | 2940 |
|
---|
[2326] | 2941 | mr_free(bkpinfo->boot_device);
|
---|
[1] | 2942 | #ifdef __FreeBSD__
|
---|
[2326] | 2943 | #define EXAMPLEBD "/dev/ad0"
|
---|
[2383] | 2944 | bkpinfo->boot_device = call_program_and_get_last_line_of_output("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/\\([0-9]\\).*/\\1/'");
|
---|
[1] | 2945 | #else
|
---|
[2326] | 2946 | #define EXAMPLEBD "/dev/hda"
|
---|
[2383] | 2947 | bkpinfo->boot_device = call_program_and_get_last_line_of_output("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'");
|
---|
[1] | 2948 | #endif
|
---|
[128] | 2949 | i = which_boot_loader(bkpinfo->boot_device);
|
---|
| 2950 | if (i == 'U') // unknown
|
---|
| 2951 | {
|
---|
[1] | 2952 |
|
---|
[2326] | 2953 | p = popup_and_get_string("Boot device", "What is your boot device? (e.g. "EXAMPLEBD")", bkpinfo->boot_device);
|
---|
| 2954 | #undef EXAMPLEBD
|
---|
| 2955 | if (p == NULL) {
|
---|
[541] | 2956 | log_to_screen("User has chosen not to backup the PC");
|
---|
[128] | 2957 | finish(1);
|
---|
| 2958 | }
|
---|
[2326] | 2959 | mr_free(bkpinfo->boot_device);
|
---|
| 2960 | bkpinfo->boot_device = p;
|
---|
| 2961 | #ifdef __FreeBSD__
|
---|
[128] | 2962 | i = which_boot_loader(bkpinfo->boot_device);
|
---|
[1] | 2963 | #else
|
---|
[2316] | 2964 | if (does_string_exist_in_boot_block(bkpinfo->boot_device, "LILO")) {
|
---|
[128] | 2965 | i = 'L';
|
---|
| 2966 | } else
|
---|
[2316] | 2967 | if (does_string_exist_in_boot_block(bkpinfo->boot_device, "ELILO")) {
|
---|
[128] | 2968 | i = 'E';
|
---|
| 2969 | } else
|
---|
[2316] | 2970 | if (does_string_exist_in_boot_block(bkpinfo->boot_device, "GRUB")) {
|
---|
[128] | 2971 | i = 'G';
|
---|
| 2972 | } else {
|
---|
| 2973 | i = 'U';
|
---|
| 2974 | }
|
---|
[1] | 2975 | #endif
|
---|
[128] | 2976 | if (i == 'U') {
|
---|
[2326] | 2977 | 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] | 2978 | i = 'R'; // raw
|
---|
| 2979 | } else {
|
---|
[2326] | 2980 | log_to_screen("I cannot find your boot loader. Please run mondoarchive with parameters.");
|
---|
[128] | 2981 | finish(1);
|
---|
| 2982 | }
|
---|
| 2983 | }
|
---|
[1] | 2984 | }
|
---|
[128] | 2985 | bkpinfo->boot_loader = i;
|
---|
[2319] | 2986 | mr_free(bkpinfo->include_paths);
|
---|
[2323] | 2987 | mr_asprintf(p, "/");
|
---|
[2319] | 2988 | bkpinfo->include_paths = p;
|
---|
| 2989 |
|
---|
[2316] | 2990 | 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);
|
---|
| 2991 | if (p == NULL) {
|
---|
[541] | 2992 | log_to_screen("User has chosen not to backup the PC");
|
---|
[128] | 2993 | finish(1);
|
---|
[1] | 2994 | }
|
---|
[2319] | 2995 | mr_free(bkpinfo->include_paths);
|
---|
| 2996 | bkpinfo->include_paths = p;
|
---|
[2316] | 2997 |
|
---|
[2382] | 2998 | mr_asprintf(tmp, "%s", list_of_NETFS_mounts_only());
|
---|
[128] | 2999 | if (strlen(tmp) > 2) {
|
---|
[2318] | 3000 | mr_strcat(bkpinfo->exclude_paths, " %s",tmp);
|
---|
[128] | 3001 | }
|
---|
[2214] | 3002 | mr_free(tmp);
|
---|
[1] | 3003 | // NTFS
|
---|
[2383] | 3004 | tmp = call_program_and_get_last_line_of_output("parted2fdisk -l | grep -i ntfs | awk '{ print $1};' | tr -s '\\n' ' ' | awk '{ print $0};'");
|
---|
[2323] | 3005 | if (strlen(tmp) > 2) {
|
---|
[2316] | 3006 | p = popup_and_get_string("NTFS partitions", "Please enter/confirm the NTFS partitions you wish to backup as well.", tmp);
|
---|
| 3007 |
|
---|
| 3008 | if (p == NULL) {
|
---|
[541] | 3009 | log_to_screen("User has chosen not to backup the PC");
|
---|
[128] | 3010 | finish(1);
|
---|
| 3011 | }
|
---|
[2327] | 3012 | mr_free(bkpinfo->image_devs);
|
---|
| 3013 | bkpinfo->image_devs = p;
|
---|
[128] | 3014 | }
|
---|
[1] | 3015 |
|
---|
| 3016 |
|
---|
[2316] | 3017 | 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);
|
---|
| 3018 | if (p == NULL) {
|
---|
[541] | 3019 | log_to_screen("User has chosen not to backup the PC");
|
---|
[128] | 3020 | finish(1);
|
---|
| 3021 | }
|
---|
[2318] | 3022 | mr_free(bkpinfo->exclude_paths);
|
---|
| 3023 | bkpinfo->exclude_paths = p;
|
---|
[2316] | 3024 |
|
---|
| 3025 | p = popup_and_get_string("Temporary directory", "Please enter your temporary directory.", bkpinfo->tmpdir);
|
---|
| 3026 | if (p == NULL) {
|
---|
[2220] | 3027 | log_to_screen("User has chosen not to backup the PC");
|
---|
| 3028 | finish(1);
|
---|
| 3029 | }
|
---|
[2321] | 3030 | mr_free(bkpinfo->tmpdir);
|
---|
| 3031 | bkpinfo->tmpdir = p;
|
---|
[2316] | 3032 |
|
---|
| 3033 | p = popup_and_get_string("Scratch directory", "Please enter your scratch directory.", bkpinfo->scratchdir);
|
---|
| 3034 | if (p == NULL) {
|
---|
[2220] | 3035 | log_to_screen("User has chosen not to backup the PC");
|
---|
| 3036 | finish(1);
|
---|
| 3037 | }
|
---|
[2320] | 3038 | mr_free(bkpinfo->scratchdir);
|
---|
| 3039 | bkpinfo->scratchdir = p;
|
---|
[2316] | 3040 |
|
---|
[1575] | 3041 | // Interactive mode:
|
---|
| 3042 | #ifdef __IA64__
|
---|
| 3043 | bkpinfo->make_cd_use_lilo = TRUE;
|
---|
| 3044 | #else
|
---|
[128] | 3045 | bkpinfo->make_cd_use_lilo = FALSE;
|
---|
[1575] | 3046 | #endif
|
---|
[128] | 3047 | bkpinfo->backup_data = TRUE;
|
---|
| 3048 | bkpinfo->verify_data =
|
---|
| 3049 | ask_me_yes_or_no
|
---|
[541] | 3050 | ("Will you want to verify your backups after Mondo has created them?");
|
---|
[1] | 3051 |
|
---|
| 3052 | #ifndef __FreeBSD__
|
---|
[128] | 3053 | if (!ask_me_yes_or_no
|
---|
[541] | 3054 | ("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] | 3055 | #endif
|
---|
[128] | 3056 | {
|
---|
[2328] | 3057 | mr_free(bkpinfo->kernel_path);
|
---|
| 3058 | mr_asprintf(bkpinfo->kernel_path, "FAILSAFE");
|
---|
[128] | 3059 | }
|
---|
[1] | 3060 |
|
---|
[128] | 3061 | if (!ask_me_yes_or_no
|
---|
[541] | 3062 | ("Are you sure you want to proceed? Hit 'no' to abort.")) {
|
---|
| 3063 | log_to_screen("User has chosen not to backup the PC");
|
---|
[128] | 3064 | finish(1);
|
---|
| 3065 | }
|
---|
| 3066 | } else {
|
---|
| 3067 | bkpinfo->restore_data = TRUE; // probably...
|
---|
| 3068 | }
|
---|
[1] | 3069 |
|
---|
[128] | 3070 | if (bkpinfo->backup_media_type == iso
|
---|
[2382] | 3071 | || bkpinfo->backup_media_type == netfs) {
|
---|
[128] | 3072 | g_ISO_restore_mode = TRUE;
|
---|
| 3073 | }
|
---|
[1] | 3074 | #ifdef __FreeSD__
|
---|
| 3075 | // skip
|
---|
| 3076 | #else
|
---|
[2382] | 3077 | if (bkpinfo->backup_media_type == netfs) {
|
---|
| 3078 | log_msg(3, "I think the Remote mount is mounted at %s", bkpinfo->isodir);
|
---|
[128] | 3079 | }
|
---|
| 3080 | log_it("isodir = %s", bkpinfo->isodir);
|
---|
[2382] | 3081 | if (bkpinfo->netfs_mount) {
|
---|
| 3082 | log_it("netfs_mount = '%s'", bkpinfo->netfs_mount);
|
---|
[2325] | 3083 | }
|
---|
[2382] | 3084 | if (bkpinfo->netfs_user) {
|
---|
| 3085 | log_it("netfs_user = '%s'", bkpinfo->netfs_user);
|
---|
[2224] | 3086 | }
|
---|
[2382] | 3087 | if (bkpinfo->netfs_proto) {
|
---|
| 3088 | log_it("netfs_proto = '%s'", bkpinfo->netfs_proto);
|
---|
| 3089 | }
|
---|
[1] | 3090 | #endif
|
---|
| 3091 |
|
---|
[128] | 3092 | log_it("media device = %s", bkpinfo->media_device);
|
---|
| 3093 | log_it("media size = %ld", bkpinfo->media_size[1]);
|
---|
[2330] | 3094 | tmp = bkptype_to_string(bkpinfo->backup_media_type);
|
---|
| 3095 | log_it("media type = %s", tmp);
|
---|
| 3096 | mr_free(tmp);
|
---|
| 3097 |
|
---|
[2327] | 3098 | if (bkpinfo->prefix) {
|
---|
| 3099 | log_it("prefix = %s", bkpinfo->prefix);
|
---|
| 3100 | }
|
---|
[128] | 3101 | log_it("compression = %ld", bkpinfo->compression_level);
|
---|
[2428] | 3102 |
|
---|
| 3103 | /* Handle devices passed in bkpinfo and print result */
|
---|
| 3104 | mr_make_devlist_from_pathlist(bkpinfo->exclude_paths, 'E');
|
---|
| 3105 | mr_make_devlist_from_pathlist(bkpinfo->include_paths, 'I');
|
---|
| 3106 |
|
---|
[128] | 3107 | log_it("scratchdir = '%s'", bkpinfo->scratchdir);
|
---|
| 3108 | log_it("tmpdir = '%s'", bkpinfo->tmpdir);
|
---|
[2327] | 3109 | if (bkpinfo->image_devs) {
|
---|
| 3110 | log_it("image_devs = '%s'", bkpinfo->image_devs);
|
---|
| 3111 | }
|
---|
[2322] | 3112 | log_it("boot_device = '%s' (loader=%c)", bkpinfo->boot_device, bkpinfo->boot_loader);
|
---|
[128] | 3113 | if (bkpinfo->media_size[0] < 0) {
|
---|
| 3114 | if (archiving_to_media) {
|
---|
| 3115 | fatal_error("Media size is less than zero.");
|
---|
| 3116 | } else {
|
---|
| 3117 | log_msg(2, "Warning - media size is less than zero.");
|
---|
| 3118 | bkpinfo->media_size[0] = 0;
|
---|
| 3119 | }
|
---|
[1] | 3120 | }
|
---|
[128] | 3121 | paranoid_free(sz_size);
|
---|
| 3122 | return (0);
|
---|
[1] | 3123 | }
|
---|
| 3124 |
|
---|
| 3125 |
|
---|
| 3126 |
|
---|
| 3127 |
|
---|
| 3128 | /* @} - end of deviceGroup */
|
---|