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