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