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