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