[1] | 1 | /* libmondo-devices.c Subroutines for handling devices
|
---|
[85] | 2 | $Id: libmondo-devices.c 3881 2024-03-09 14:31:21Z 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 3881 2024-03-09 14:31:21Z 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;
|
---|
| 130 | int lastpos = 0;
|
---|
| 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;
|
---|
| 251 | int lastpos = 0;
|
---|
| 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 |
|
---|
| 445 | mr_asprintf(command, "%s", "cat /proc/mdstat|grep -iv Personal|awk '{if($0~\"^.*[ ]+:\"){printf(\"/dev/%s \", $1)}}END{print \"\"}'");
|
---|
| 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 */
|
---|
| 501 | mr_asprintf(command, "%s", "cat /proc/mdstat|grep -iv Personal|awk '{if($0~\"^.*[ ]+:\"){printf(\"/dev/%s \", $1)}}END{print \"\"}'");
|
---|
| 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;
|
---|
[128] | 717 | case cdstream:
|
---|
[3879] | 718 | mr_asprintf(output, "%s", "cdstream");
|
---|
[1] | 719 | break;
|
---|
[2380] | 720 | case netfs:
|
---|
[3879] | 721 | mr_asprintf(output, "%s", "netfs");
|
---|
[1] | 722 | break;
|
---|
[128] | 723 | case tape:
|
---|
[3879] | 724 | mr_asprintf(output, "%s", "tape");
|
---|
[1] | 725 | break;
|
---|
[128] | 726 | case udev:
|
---|
[3879] | 727 | mr_asprintf(output, "%s", "udev");
|
---|
[1] | 728 | break;
|
---|
[1687] | 729 | case usb:
|
---|
[3879] | 730 | mr_asprintf(output, "%s", "usb");
|
---|
[1687] | 731 | break;
|
---|
[128] | 732 | default:
|
---|
[3879] | 733 | mr_asprintf(output, "%s", "default");
|
---|
[128] | 734 | }
|
---|
| 735 | return (output);
|
---|
[1] | 736 | }
|
---|
| 737 |
|
---|
| 738 |
|
---|
| 739 |
|
---|
| 740 | /**
|
---|
| 741 | * @addtogroup deviceGroup
|
---|
| 742 | * @{
|
---|
| 743 | */
|
---|
| 744 | /**
|
---|
| 745 | * Eject the tray of the specified CD device.
|
---|
| 746 | * @param dev The device to eject.
|
---|
| 747 | * @return the return value of the @c eject command. (0=success, nonzero=failure)
|
---|
| 748 | */
|
---|
[128] | 749 | int eject_device(char *dev)
|
---|
[1] | 750 | {
|
---|
[3191] | 751 | char *command = NULL;
|
---|
[128] | 752 | int res1 = 0, res2 = 0;
|
---|
[1] | 753 |
|
---|
[3191] | 754 | if (dev == NULL) {
|
---|
| 755 | return (1);
|
---|
| 756 | }
|
---|
[1] | 757 |
|
---|
[128] | 758 | if (IS_THIS_A_STREAMING_BACKUP(g_backup_media_type)
|
---|
| 759 | && g_backup_media_type != udev) {
|
---|
[3191] | 760 | mr_asprintf(command, "mt -f %s offline", dev);
|
---|
[128] | 761 | res1 = run_program_and_log_output(command, 1);
|
---|
[3191] | 762 | mr_free(command);
|
---|
[128] | 763 | } else {
|
---|
| 764 | res1 = 0;
|
---|
| 765 | }
|
---|
[1] | 766 |
|
---|
| 767 | #ifdef __FreeBSD__
|
---|
[128] | 768 | if (strstr(dev, "acd")) {
|
---|
[3191] | 769 | mr_asprintf(command, "cdcontrol -f %s eject", dev);
|
---|
[128] | 770 | } else {
|
---|
[3191] | 771 | mr_asprintf(command, "camcontrol eject `echo %s | sed 's|/dev/||'`", dev);
|
---|
[128] | 772 | }
|
---|
[1] | 773 | #else
|
---|
[3191] | 774 | mr_asprintf(command, "eject %s", dev);
|
---|
[1] | 775 | #endif
|
---|
| 776 |
|
---|
[128] | 777 | log_msg(3, "Ejecting %s", dev);
|
---|
| 778 | res2 = run_program_and_log_output(command, 1);
|
---|
[3191] | 779 | mr_free(command);
|
---|
[128] | 780 | if (res1 && res2) {
|
---|
| 781 | return (1);
|
---|
| 782 | } else {
|
---|
| 783 | return (0);
|
---|
| 784 | }
|
---|
[1] | 785 | }
|
---|
| 786 |
|
---|
| 787 | /**
|
---|
| 788 | * Determine whether a non-Microsoft partition exists on any connected hard drive.
|
---|
[3653] | 789 | * @return TRUE (there's a Linux/FreeBSD partition) or FALSE (Microsoft has taken over yet another innocent machine).
|
---|
[1] | 790 | */
|
---|
[128] | 791 | bool does_nonMS_partition_exist(void)
|
---|
[1] | 792 | {
|
---|
| 793 | #if __FreeBSD__
|
---|
[128] | 794 | return
|
---|
[3371] | 795 | !system("for drive in /dev/ad? /dev/da?; do fdisk $drive | grep -q FreeBSD && exit 0; done; false");
|
---|
[1] | 796 | #else
|
---|
[128] | 797 | return
|
---|
[3377] | 798 | !system("mr-parted2fdisk -l 2>/dev/null | grep '^/dev/' | grep -Eqv '(MS|DOS|EFI|FAT|NTFS)'");
|
---|
[1] | 799 | #endif
|
---|
| 800 | }
|
---|
| 801 |
|
---|
| 802 | /**
|
---|
| 803 | * Determine whether the specified @p partno exists on the specified @p drive.
|
---|
| 804 | * @param drive The drive to search for the partition in.
|
---|
| 805 | * @param partno The partition number to look for.
|
---|
| 806 | * @return 0 if it exists, nonzero otherwise.
|
---|
| 807 | */
|
---|
[128] | 808 | int does_partition_exist(const char *drive, int partno)
|
---|
[1] | 809 | {
|
---|
[128] | 810 | /*@ buffers **************************************************** */
|
---|
[3191] | 811 | char *program = NULL;
|
---|
| 812 | char *incoming = NULL;
|
---|
[2211] | 813 | char *searchstr = NULL;
|
---|
[1] | 814 |
|
---|
[128] | 815 | /*@ ints ******************************************************* */
|
---|
| 816 | int res = 0;
|
---|
[1] | 817 |
|
---|
[128] | 818 | /*@ pointers *************************************************** */
|
---|
| 819 | FILE *fin;
|
---|
[1] | 820 |
|
---|
[128] | 821 | /*@ end vars *************************************************** */
|
---|
| 822 | assert_string_is_neither_NULL_nor_zerolength(drive);
|
---|
| 823 | assert(partno >= 0 && partno < 999);
|
---|
[1] | 824 |
|
---|
| 825 | #ifdef __FreeBSD__
|
---|
[128] | 826 | // We assume here that this is running from mondorestore. (It is.)
|
---|
[3859] | 827 | tmp = build_partition_name(drive, partno);
|
---|
| 828 | mr_asprintf(program, "ls %s %s >/dev/null 2>&1", drive, tmp);
|
---|
| 829 | mr_free(tmp);
|
---|
[3191] | 830 | res = system(program);
|
---|
| 831 | mr_free(program);
|
---|
| 832 | return(res);
|
---|
[1] | 833 | #endif
|
---|
| 834 |
|
---|
[3377] | 835 | mr_asprintf(program, "mr-parted2fdisk -l %s 2> /dev/null", drive);
|
---|
[128] | 836 | fin = popen(program, "r");
|
---|
| 837 | if (!fin) {
|
---|
| 838 | log_it("program=%s", program);
|
---|
| 839 | log_OS_error("Cannot popen-in program");
|
---|
[3191] | 840 | mr_free(program);
|
---|
[128] | 841 | return (0);
|
---|
[1] | 842 | }
|
---|
[3191] | 843 | mr_free(program);
|
---|
| 844 |
|
---|
[3859] | 845 | searchstr = build_partition_name(drive, partno);
|
---|
| 846 | mr_strcat(searchstr, " ");
|
---|
[3191] | 847 | for (res = 0, mr_getline(incoming, fin); !res && !feof(fin) ; mr_getline(incoming, fin)) {
|
---|
[128] | 848 | if (strstr(incoming, searchstr)) {
|
---|
| 849 | res = 1;
|
---|
| 850 | }
|
---|
[3191] | 851 | mr_free(incoming);
|
---|
[128] | 852 | }
|
---|
[3859] | 853 | mr_free(searchstr);
|
---|
[3191] | 854 | mr_free(incoming);
|
---|
| 855 |
|
---|
[128] | 856 | if (pclose(fin)) {
|
---|
| 857 | log_OS_error("Cannot pclose fin");
|
---|
| 858 | }
|
---|
| 859 | return (res);
|
---|
[1] | 860 | }
|
---|
| 861 |
|
---|
| 862 |
|
---|
| 863 |
|
---|
| 864 |
|
---|
| 865 |
|
---|
| 866 | /**
|
---|
[3429] | 867 | * Determine whether given NULL-terminated @p str exists in the begining of @p dev.
|
---|
[1] | 868 | * @param dev The device to look in.
|
---|
| 869 | * @param str The string to look for.
|
---|
| 870 | * @return TRUE if it exists, FALSE if it doesn't.
|
---|
| 871 | */
|
---|
[128] | 872 | bool does_string_exist_in_boot_block(char *dev, char *str)
|
---|
[1] | 873 | {
|
---|
[128] | 874 | /*@ buffers **************************************************** */
|
---|
[3191] | 875 | char *command = NULL;
|
---|
[1] | 876 |
|
---|
[128] | 877 | /*@ end vars *************************************************** */
|
---|
| 878 | int i;
|
---|
[1] | 879 |
|
---|
[128] | 880 | assert_string_is_neither_NULL_nor_zerolength(dev);
|
---|
| 881 | assert_string_is_neither_NULL_nor_zerolength(str);
|
---|
[1] | 882 |
|
---|
[3379] | 883 | /* For UEFI detection, this should be extended to count=1000 ! */
|
---|
| 884 | if (bkpinfo->boot_type == UEFI) {
|
---|
| 885 | mr_asprintf(command, "dd if=%s bs=446 count=1000 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null", dev, str);
|
---|
| 886 | } else {
|
---|
| 887 | mr_asprintf(command, "dd if=%s bs=446 count=1 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null", dev, str);
|
---|
| 888 | }
|
---|
[128] | 889 | i = system(command);
|
---|
[3191] | 890 | mr_free(command);
|
---|
[128] | 891 | if (i) {
|
---|
| 892 | return (FALSE);
|
---|
| 893 | } else {
|
---|
| 894 | return (TRUE);
|
---|
| 895 | }
|
---|
[1] | 896 | }
|
---|
| 897 |
|
---|
| 898 | /**
|
---|
| 899 | * Determine whether specified @p str exists in the first @p n sectors of
|
---|
| 900 | * @p dev.
|
---|
| 901 | * @param dev The device to look in.
|
---|
| 902 | * @param str The string to look for.
|
---|
| 903 | * @param n The number of 512-byte sectors to search.
|
---|
| 904 | */
|
---|
[128] | 905 | bool does_string_exist_in_first_N_blocks(char *dev, char *str, int n)
|
---|
[1] | 906 | {
|
---|
[128] | 907 | /*@ buffers **************************************************** */
|
---|
[3191] | 908 | char *command = NULL;
|
---|
[128] | 909 | /*@ end vars *************************************************** */
|
---|
| 910 | int i;
|
---|
[1] | 911 |
|
---|
[3191] | 912 | 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] | 913 | i = system(command);
|
---|
[3191] | 914 | mr_free(command);
|
---|
[128] | 915 | if (i) {
|
---|
| 916 | return (FALSE);
|
---|
| 917 | } else {
|
---|
| 918 | return (TRUE);
|
---|
| 919 | }
|
---|
[1] | 920 | }
|
---|
| 921 |
|
---|
| 922 |
|
---|
[3878] | 923 | /*
|
---|
| 924 | * This function tries to find an optical media device
|
---|
| 925 | * and return it's device file to the caller that needs to free it
|
---|
| 926 | */
|
---|
| 927 | char *find_optical_device(void) {
|
---|
| 928 | char *dev = NULL;
|
---|
| 929 | char *tmp1 = NULL;
|
---|
| 930 | char *command = NULL;
|
---|
| 931 |
|
---|
| 932 | log_to_screen("I am looking for your optical burner. Please wait.");
|
---|
| 933 |
|
---|
| 934 | if (bkpinfo->media_device != NULL) {
|
---|
| 935 | log_msg(3, "Been there, done that. Returning %s", bkpinfo->media_device);
|
---|
| 936 | return (bkpinfo->media_device);
|
---|
| 937 | }
|
---|
| 938 |
|
---|
| 939 | mr_asprintf(tmp1, "cdrecord -inq 2> /dev/null | grep -E '^Detected ' | cut -d':' -f2");
|
---|
| 940 | dev = call_program_and_get_last_line_of_output(tmp1);
|
---|
| 941 | mr_free(tmp1);
|
---|
| 942 |
|
---|
| 943 | if ((dev != NULL) && does_file_exist(dev)) {
|
---|
| 944 | log_msg(2, "find_optical_device found %s automatically", dev);
|
---|
| 945 | } else {
|
---|
| 946 | mr_free(dev);
|
---|
| 947 | tmp1 = find_home_of_exe("lsscsi");
|
---|
| 948 | if (tmp1 != NULL) {
|
---|
| 949 | mr_asprintf(command, "%s | grep ' cd' | awk '{print $NF}' | head -1", tmp1);
|
---|
| 950 | dev = call_program_and_get_last_line_of_output(command);
|
---|
| 951 | mr_free(command);
|
---|
| 952 | }
|
---|
| 953 | mr_free(tmp1);
|
---|
| 954 |
|
---|
| 955 | if ((dev == NULL) || !does_file_exist(dev)) {
|
---|
| 956 | /* trying something else then */
|
---|
| 957 | mr_asprintf(dev, "%s", VANILLA_SCSI_CDROM);
|
---|
| 958 | if (!does_file_exist(dev)) {
|
---|
| 959 | mr_asprintf(dev, "%s", ALT_CDROM);
|
---|
| 960 | if (!does_file_exist(dev)) {
|
---|
| 961 | mr_asprintf(dev, "%s", "/dev/cdrom");
|
---|
| 962 | if (!does_file_exist(dev)) {
|
---|
| 963 | mr_asprintf(dev, "%s", "/dev/dvd");
|
---|
| 964 | if (!does_file_exist(dev)) {
|
---|
| 965 | log_it("Unable to find a tape device on this system");
|
---|
| 966 | }
|
---|
| 967 | }
|
---|
| 968 | }
|
---|
| 969 | }
|
---|
| 970 | }
|
---|
| 971 | if (dev != NULL) {
|
---|
| 972 | log_it("find_optical_device found %s manually", dev);
|
---|
| 973 | }
|
---|
| 974 | }
|
---|
| 975 |
|
---|
| 976 | if ((dev != NULL) && does_file_exist(dev)) {
|
---|
| 977 | log_it("find_optical_device returns %s", dev);
|
---|
| 978 | } else {
|
---|
| 979 | mr_free(dev);
|
---|
| 980 | log_it("find_optical_device found no optical burner on your system returning NULL");
|
---|
| 981 | }
|
---|
| 982 | return(dev);
|
---|
| 983 | }
|
---|
[1] | 984 | /**
|
---|
[3878] | 985 | * Mount the CD-ROM or USB device at /mnt/cdrom.
|
---|
| 986 | * @param bkpinfo The backup information structure. Fields used:
|
---|
| 987 | * - @c bkpinfo->backup_media_type
|
---|
| 988 | * - @c bkpinfo->disaster_recovery
|
---|
| 989 | * - @c bkpinfo->isodir
|
---|
| 990 | * - @c bkpinfo->media_device
|
---|
| 991 | * @return TRUE for success, FALSE for failure.
|
---|
| 992 | */
|
---|
| 993 | bool mount_media(const char *mountpoint) {
|
---|
| 994 |
|
---|
| 995 | char *mount_cmd = NULL;
|
---|
| 996 | char *mountdir = NULL;
|
---|
| 997 | char *tmp = NULL;
|
---|
| 998 | int i = 0, res = 0;
|
---|
| 999 | #ifdef __FreeBSD__
|
---|
| 1000 | char mdd[32];
|
---|
| 1001 | char *mddev = mdd;
|
---|
| 1002 | char *dev;
|
---|
| 1003 | #endif
|
---|
| 1004 |
|
---|
| 1005 | if (bkpinfo->backup_media_type == tape || bkpinfo->backup_media_type == udev) {
|
---|
| 1006 | log_msg(8, "Tape/udev. Therefore, no need to mount a media.");
|
---|
| 1007 | return(TRUE);
|
---|
| 1008 | }
|
---|
| 1009 |
|
---|
| 1010 | assert_string_is_neither_NULL_nor_zerolength(mountpoint);
|
---|
| 1011 |
|
---|
| 1012 | mr_asprintf(tmp, "mount | grep -F %s", mountpoint);
|
---|
| 1013 | if (!run_program_and_log_output(tmp, FALSE)) {
|
---|
| 1014 | log_msg(2, "mount_media() - media already mounted. Fair enough.");
|
---|
| 1015 | mr_free(tmp);
|
---|
| 1016 | return (TRUE);
|
---|
| 1017 | }
|
---|
| 1018 | mr_free(tmp);
|
---|
| 1019 |
|
---|
| 1020 | make_hole_for_dir(mountpoint);
|
---|
| 1021 |
|
---|
| 1022 | if (bkpinfo->backup_media_type == netfs) {
|
---|
| 1023 | log_msg(2, "Mounting for Network thingy");
|
---|
| 1024 | log_msg(2, "isodir = %s", bkpinfo->isodir);
|
---|
| 1025 | if (((bkpinfo->isodir == NULL) || !strcmp(bkpinfo->isodir, "/")) && am_I_in_disaster_recovery_mode()) {
|
---|
| 1026 | mr_asprintf(bkpinfo->isodir, "%s", "/tmp/isodir");
|
---|
| 1027 | log_msg(1, "isodir is being set to %s", bkpinfo->isodir);
|
---|
| 1028 | }
|
---|
| 1029 | #ifdef __FreeBSD__
|
---|
| 1030 | if (bkpinfo->netfs_remote_dir != NULL) {
|
---|
| 1031 | // NETFS
|
---|
| 1032 | mr_asprintf(mount_cmd, "/mnt/isodir/%s/%s/%s-%d.iso", bkpinfo->isodir, bkpinfo->netfs_remote_dir, bkpinfo->prefix, g_current_media_number);
|
---|
| 1033 | } else {
|
---|
| 1034 | // ISO
|
---|
| 1035 | mr_asprintf(mount_cmd, "/mnt/isodir/%s/%s-%d.iso", bkpinfo->isodir, bkpinfo->prefix, g_current_media_number);
|
---|
| 1036 | }
|
---|
| 1037 | mddev = make_vn(mount_cmd);
|
---|
| 1038 | mr_free(mount_cmd);
|
---|
| 1039 |
|
---|
| 1040 | mr_asprintf(mount_cmd, "mount_cd9660 -r %s %s", mddev, mountpoint);
|
---|
| 1041 | #else
|
---|
| 1042 | if (bkpinfo->netfs_remote_dir != NULL) {
|
---|
| 1043 | // NETFS
|
---|
| 1044 | 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);
|
---|
| 1045 | } else {
|
---|
| 1046 | // ISO
|
---|
| 1047 | mr_asprintf(mount_cmd, "mount %s/%s-%d.iso -t iso9660 -o loop,ro %s", bkpinfo->isodir, bkpinfo->prefix, g_current_media_number, mountpoint);
|
---|
| 1048 | }
|
---|
| 1049 | #endif
|
---|
| 1050 |
|
---|
| 1051 | } else if (bkpinfo->backup_media_type == iso) {
|
---|
| 1052 | if (bkpinfo->subdir) {
|
---|
| 1053 | mr_asprintf(mountdir, "%s/%s", bkpinfo->isodir, bkpinfo->subdir);
|
---|
| 1054 | } else {
|
---|
| 1055 | mr_asprintf(mountdir, "%s", bkpinfo->isodir);
|
---|
| 1056 | }
|
---|
| 1057 | #ifdef __FreeBSD__
|
---|
| 1058 | mr_asprintf(mount_cmd, "%s/%s-%d.iso", mountdir, bkpinfo->prefix, g_current_media_number);
|
---|
| 1059 | mddev = make_vn(mount_cmd);
|
---|
| 1060 | mr_free(mount_cmd);
|
---|
| 1061 |
|
---|
| 1062 | mr_asprintf(mount_cmd, "mount_cd9660 -r %s %s", mddev, mountpoint);
|
---|
| 1063 | #else
|
---|
| 1064 | mr_asprintf(mount_cmd, "mount %s/%s-%d.iso -t iso9660 -o loop,ro %s", mountdir, bkpinfo->prefix, g_current_media_number, mountpoint);
|
---|
| 1065 | #endif
|
---|
| 1066 | mr_free(mountdir);
|
---|
| 1067 | } else if (bkpinfo->backup_media_type == usb) {
|
---|
| 1068 | mr_asprintf(mount_cmd, "mount -t vfat %s %s", bkpinfo->media_device, mountpoint);
|
---|
| 1069 | } else { // optical
|
---|
| 1070 | if (bkpinfo->disaster_recovery
|
---|
| 1071 | && does_file_exist("/tmp/CDROM-LIVES-HERE")) {
|
---|
| 1072 | mr_asprintf(bkpinfo->media_device, "%s", last_line_of_file("/tmp/CDROM-LIVES-HERE"));
|
---|
| 1073 | } else {
|
---|
| 1074 | if (bkpinfo->media_device == NULL) {
|
---|
| 1075 | bkpinfo->media_device = find_optical_device();
|
---|
| 1076 | }
|
---|
| 1077 | }
|
---|
| 1078 |
|
---|
| 1079 | #ifdef __FreeBSD__
|
---|
| 1080 | if (g_ISO_restore_mode) {
|
---|
| 1081 | mr_asprintf(dev, "%s", make_vn(bkpinfo->media_device));
|
---|
| 1082 | if (!dev) {
|
---|
| 1083 | mr_asprintf(command, "Unable to mount ISO (make_vn(%s) failed)", bkpinfo->media_device);
|
---|
| 1084 | fatal_error(command);
|
---|
| 1085 | }
|
---|
| 1086 | mr_free(bkpinfo->media_device);
|
---|
| 1087 | bkpinfo->media_device = dev
|
---|
| 1088 | }
|
---|
| 1089 |
|
---|
| 1090 | mr_asprintf(mount_cmd, "mount_cd9660 -r %s %s 2>> %s", bkpinfo->media_device, mountpoint, MONDO_LOGFILE);
|
---|
| 1091 | #else
|
---|
| 1092 | mr_asprintf(mount_cmd, "mount %s -o loop,ro -t iso9660 %s 2>> %s", bkpinfo->media_device, mountpoint, MONDO_LOGFILE);
|
---|
| 1093 | #endif
|
---|
| 1094 | log_msg(2, "(mount_media) --- command = %s", mount_cmd);
|
---|
| 1095 | // usefull ??
|
---|
| 1096 | if (strncmp(bkpinfo->media_device, "/dev/", 5) == 0) {
|
---|
| 1097 | retract_CD_tray_and_defeat_autorun();
|
---|
| 1098 | }
|
---|
| 1099 | }
|
---|
| 1100 |
|
---|
| 1101 | for (i = 0; i < 2; i++) {
|
---|
| 1102 | res = run_program_and_log_output(mount_cmd, FALSE);
|
---|
| 1103 | if (!res) {
|
---|
| 1104 | break;
|
---|
| 1105 | } else {
|
---|
| 1106 | log_msg(2, "Failed to mount device.");
|
---|
| 1107 | sleep(5);
|
---|
| 1108 | sync();
|
---|
| 1109 | }
|
---|
| 1110 | }
|
---|
| 1111 | mr_free(mount_cmd);
|
---|
| 1112 |
|
---|
| 1113 | if (res) {
|
---|
| 1114 | log_msg(2, "Failed, despite %d attempts", i);
|
---|
| 1115 | return(FALSE);
|
---|
| 1116 | } else {
|
---|
| 1117 | log_msg(2, "Mounted media drive OK");
|
---|
| 1118 | return(TRUE);
|
---|
| 1119 | }
|
---|
| 1120 | }
|
---|
| 1121 | /**************************************************************************
|
---|
| 1122 | *END_MOUNT_MEDIA *
|
---|
| 1123 | **************************************************************************/
|
---|
| 1124 |
|
---|
| 1125 |
|
---|
| 1126 |
|
---|
| 1127 |
|
---|
| 1128 | /**
|
---|
[3875] | 1129 | * Try to mount CD/DVD at @p mountpoint. If the CD/DVD is not found or has
|
---|
| 1130 | * not been specified, call find_optical_device() to find it.
|
---|
[1] | 1131 | * @param mountpoint Where to mount the CD-ROM.
|
---|
[3875] | 1132 | * @return TRUE for success, FALSE for failure.
|
---|
| 1133 | * @see mount_media
|
---|
[1] | 1134 | */
|
---|
[3875] | 1135 | bool find_and_mount_actual_cd(char *mountpoint) {
|
---|
[3191] | 1136 |
|
---|
[128] | 1137 | /*@ buffers ***************************************************** */
|
---|
[1] | 1138 |
|
---|
[128] | 1139 | /*@ int's ****************************************************** */
|
---|
[3875] | 1140 | bool res;
|
---|
[1] | 1141 |
|
---|
[128] | 1142 | /*@ end vars **************************************************** */
|
---|
[1] | 1143 |
|
---|
[128] | 1144 | assert(bkpinfo != NULL);
|
---|
| 1145 | assert_string_is_neither_NULL_nor_zerolength(mountpoint);
|
---|
[1] | 1146 |
|
---|
[3875] | 1147 | if (bkpinfo->media_device == NULL) {
|
---|
| 1148 | bkpinfo->media_device = find_optical_device();
|
---|
[128] | 1149 | }
|
---|
[1] | 1150 |
|
---|
[128] | 1151 | if (bkpinfo->backup_media_type != iso) {
|
---|
| 1152 | retract_CD_tray_and_defeat_autorun();
|
---|
| 1153 | }
|
---|
[1] | 1154 |
|
---|
[3875] | 1155 | if ((bkpinfo->media_device == NULL) || (res = mount_media(mountpoint))) {
|
---|
[3870] | 1156 | mr_free(bkpinfo->media_device);
|
---|
| 1157 | if ((bkpinfo->media_device = mr_popup_and_get_string("CD-ROM device", "Please enter your CD-ROM's /dev device", "/dev/cdrom")) == NULL) {
|
---|
[3875] | 1158 | res = TRUE;
|
---|
[128] | 1159 | } else {
|
---|
[3875] | 1160 | res = mount_media(mountpoint);
|
---|
[128] | 1161 | }
|
---|
| 1162 | }
|
---|
| 1163 | if (res) {
|
---|
[541] | 1164 | log_msg(1, "mount failed");
|
---|
[128] | 1165 | } else {
|
---|
[3870] | 1166 | log_msg(1, "mount succeeded with %s", bkpinfo->media_device);
|
---|
[128] | 1167 | }
|
---|
| 1168 | return (res);
|
---|
[1] | 1169 | }
|
---|
[3875] | 1170 | /*
|
---|
| 1171 | * This function tries to find a USB media device
|
---|
| 1172 | * and return it's device file to the caller that needs to free it
|
---|
[1] | 1173 | */
|
---|
[3875] | 1174 | char *find_usb_device(void)
|
---|
[1] | 1175 | {
|
---|
[3875] | 1176 | char *dev = NULL;
|
---|
[2214] | 1177 | char *tmp1 = NULL;
|
---|
[3191] | 1178 | char *command = NULL;
|
---|
[1] | 1179 |
|
---|
[3875] | 1180 | log_to_screen("I am looking for your USB key. Please wait.");
|
---|
[1] | 1181 |
|
---|
[3875] | 1182 | if (bkpinfo->media_device != NULL) {
|
---|
| 1183 | log_msg(3, "Been there, done that. Returning %s", bkpinfo->media_device);
|
---|
| 1184 | return (bkpinfo->media_device);
|
---|
[128] | 1185 | }
|
---|
[1] | 1186 |
|
---|
[3875] | 1187 | tmp1 = find_home_of_exe("lsscsi");
|
---|
| 1188 | if (tmp1 != NULL) {
|
---|
| 1189 | mr_asprintf(command, "%s | grep ' disk' | grep USB | awk '{print $NF}' | head -1", tmp1);
|
---|
| 1190 | dev = call_program_and_get_last_line_of_output(command);
|
---|
[3191] | 1191 | mr_free(command);
|
---|
[128] | 1192 | }
|
---|
[3875] | 1193 | mr_free(tmp1);
|
---|
[3191] | 1194 |
|
---|
[3875] | 1195 | if ((dev == NULL) || !does_file_exist(dev)) {
|
---|
| 1196 | tmp1 = find_home_of_exe("lsblk");
|
---|
| 1197 | if (tmp1 != NULL) {
|
---|
| 1198 | mr_asprintf(command, "%s --noheadings --raw --output rm,tran,type,path --sort path | awk '/^1 usb disk/ {d=$4} END {print d}'", tmp1);
|
---|
| 1199 | dev = call_program_and_get_last_line_of_output(command);
|
---|
| 1200 | mr_free(command);
|
---|
[1] | 1201 | }
|
---|
[3875] | 1202 | mr_free(tmp1);
|
---|
| 1203 | log_it("Unable to find a tape device on this system");
|
---|
[1] | 1204 | }
|
---|
[3875] | 1205 | if (dev != NULL) {
|
---|
| 1206 | log_it("find_usb_device found %s manually", dev);
|
---|
[128] | 1207 | }
|
---|
[1] | 1208 |
|
---|
[3875] | 1209 | if ((dev != NULL) && does_file_exist(dev)) {
|
---|
| 1210 | log_it("find_usb_device returns %s", dev);
|
---|
[128] | 1211 | } else {
|
---|
[3875] | 1212 | mr_free(dev);
|
---|
| 1213 | log_it("find_usb_device found no USB key on your system returning NULL");
|
---|
[128] | 1214 | }
|
---|
[3875] | 1215 | return(dev);
|
---|
| 1216 | }
|
---|
[2214] | 1217 |
|
---|
[3191] | 1218 |
|
---|
[1] | 1219 |
|
---|
[3875] | 1220 | /* Generic fund to find a media
|
---|
| 1221 | * Return a dynamically allocted string that caller needs to free
|
---|
| 1222 | * @media_type is the type of media to look for
|
---|
| 1223 | */
|
---|
[1] | 1224 |
|
---|
[3875] | 1225 | char *find_device(t_bkptype media_type) {
|
---|
| 1226 |
|
---|
| 1227 | if (media_type == usb) {
|
---|
| 1228 | return(find_usb_device());
|
---|
| 1229 | } else if (media_type == tape) {
|
---|
| 1230 | return(find_tape_device());
|
---|
[3881] | 1231 | } else if (media_type == optical) {
|
---|
[3875] | 1232 | return(find_optical_device());
|
---|
| 1233 | } else {
|
---|
| 1234 | return(NULL);
|
---|
| 1235 | }
|
---|
| 1236 | }
|
---|
| 1237 |
|
---|
| 1238 |
|
---|
| 1239 |
|
---|
| 1240 |
|
---|
[1] | 1241 | #include <sys/ioctl.h>
|
---|
| 1242 |
|
---|
| 1243 | /**
|
---|
| 1244 | * Find the size of the specified @p drive, in megabytes. Uses @c ioctl calls
|
---|
| 1245 | * and @c dmesg.
|
---|
| 1246 | * @param drive The device to find the size of.
|
---|
| 1247 | * @return size in megabytes.
|
---|
| 1248 | */
|
---|
[128] | 1249 | long get_phys_size_of_drive(char *drive)
|
---|
[1] | 1250 | {
|
---|
[128] | 1251 | int fd;
|
---|
[1] | 1252 | #if linux
|
---|
[128] | 1253 | unsigned long long s = 0;
|
---|
[3060] | 1254 | int fileid, cylinders = 0;
|
---|
[128] | 1255 | int cylindersize = 0;
|
---|
[1] | 1256 | int gotgeo = 0;
|
---|
| 1257 |
|
---|
[128] | 1258 |
|
---|
| 1259 | struct hd_geometry hdgeo;
|
---|
[1] | 1260 | #elif __FreeBSD__
|
---|
[128] | 1261 | off_t s;
|
---|
[1] | 1262 | #endif
|
---|
| 1263 |
|
---|
[128] | 1264 | long outvalA = -1;
|
---|
| 1265 | long outvalB = -1;
|
---|
| 1266 | long outvalC = -1;
|
---|
[1] | 1267 |
|
---|
[128] | 1268 | if ((fd = open(drive, O_RDONLY)) != -1) {
|
---|
| 1269 | if (ioctl(fd,
|
---|
[1] | 1270 | #if linux
|
---|
[128] | 1271 | #ifdef BLKGETSIZE64
|
---|
| 1272 | BLKGETSIZE64,
|
---|
| 1273 | #else
|
---|
| 1274 | BLKGETSIZE,
|
---|
| 1275 | #endif
|
---|
[1] | 1276 | #elif __FreeBSD__
|
---|
[128] | 1277 | DIOCGMEDIASIZE,
|
---|
[1] | 1278 | #endif
|
---|
[128] | 1279 | &s) != -1) {
|
---|
| 1280 | close(fd);
|
---|
| 1281 | // s>>11 works for older disks but not for newer ones
|
---|
| 1282 | outvalB =
|
---|
[1] | 1283 | #if linux
|
---|
[128] | 1284 | #ifdef BLKGETSIZE64
|
---|
| 1285 | s >> 20
|
---|
[1] | 1286 | #else
|
---|
[128] | 1287 | s >> 11
|
---|
[1] | 1288 | #endif
|
---|
[128] | 1289 | #else
|
---|
| 1290 | s >> 20
|
---|
| 1291 | #endif
|
---|
| 1292 | ;
|
---|
| 1293 | }
|
---|
[1] | 1294 | }
|
---|
| 1295 |
|
---|
[128] | 1296 | if (outvalB <= 0) {
|
---|
| 1297 | log_msg(1, "Error getting size of %s: %s", drive, strerror(errno));
|
---|
[1] | 1298 | #if linux
|
---|
[161] | 1299 | fileid = open(drive, O_RDONLY);
|
---|
[2205] | 1300 | if (fileid != -1) {
|
---|
[161] | 1301 | if (ioctl(fileid, HDIO_GETGEO, &hdgeo) != -1) {
|
---|
| 1302 | if (hdgeo.cylinders && hdgeo.heads && hdgeo.sectors) {
|
---|
| 1303 | cylindersize = hdgeo.heads * hdgeo.sectors / 2;
|
---|
| 1304 | outvalA = cylindersize * cylinders / 1024;
|
---|
| 1305 | log_msg(2, "Got Harddisk geometry, C:%d, H:%d, S:%d",
|
---|
| 1306 | hdgeo.cylinders, hdgeo.heads, hdgeo.sectors);
|
---|
| 1307 | gotgeo = 1;
|
---|
| 1308 | } else {
|
---|
| 1309 | log_msg(1, "Harddisk geometry wrong");
|
---|
| 1310 | }
|
---|
[1] | 1311 | } else {
|
---|
[161] | 1312 | log_msg(1,
|
---|
| 1313 | "Error in ioctl() getting new hard disk geometry (%s), resizing in unsafe mode",
|
---|
| 1314 | strerror(errno));
|
---|
[1] | 1315 | }
|
---|
[161] | 1316 | close(fileid);
|
---|
[1] | 1317 | } else {
|
---|
[161] | 1318 | log_msg(1, "Failed to open %s for reading: %s", drive,
|
---|
[128] | 1319 | strerror(errno));
|
---|
[1] | 1320 | }
|
---|
[161] | 1321 | if (!gotgeo) {
|
---|
| 1322 | log_msg(1, "Failed to get harddisk geometry, using old mode");
|
---|
| 1323 | }
|
---|
[1] | 1324 | #endif
|
---|
[161] | 1325 | }
|
---|
[1] | 1326 | // OLDER DISKS will give ridiculously low value for outvalB (so outvalA is returned) :)
|
---|
| 1327 | // NEWER DISKS will give sane value for outvalB (close to outvalA, in other words) :)
|
---|
| 1328 |
|
---|
[128] | 1329 | outvalC = (outvalA > outvalB) ? outvalA : outvalB;
|
---|
| 1330 |
|
---|
[1] | 1331 | // log_msg (5, "drive = %s, error = %s", drive, strerror (errno));
|
---|
| 1332 | // fatal_error ("GPSOD: Unable to get size of drive");
|
---|
[128] | 1333 | log_msg(1, "%s --> %ld or %ld --> %ld", drive, outvalA, outvalB,
|
---|
| 1334 | outvalC);
|
---|
| 1335 |
|
---|
| 1336 | return (outvalC);
|
---|
[1] | 1337 | }
|
---|
[128] | 1338 |
|
---|
[1] | 1339 | /**
|
---|
| 1340 | * Determine whether @p format is supported by the kernel. Uses /proc/filesystems
|
---|
| 1341 | * under Linux and @c lsvfs under FreeBSD.
|
---|
| 1342 | * @param format The format to test.
|
---|
| 1343 | * @return TRUE if the format is supported, FALSE if not.
|
---|
| 1344 | */
|
---|
[128] | 1345 | bool is_this_a_valid_disk_format(char *format)
|
---|
[1] | 1346 | {
|
---|
[2211] | 1347 | char *good_formats = NULL;
|
---|
[3191] | 1348 | char *command = NULL;
|
---|
| 1349 | char *format_sz = NULL;
|
---|
[3291] | 1350 | char *p = NULL;
|
---|
[1] | 1351 |
|
---|
[128] | 1352 | FILE *pin;
|
---|
| 1353 | int retval;
|
---|
[1] | 1354 |
|
---|
[128] | 1355 | assert_string_is_neither_NULL_nor_zerolength(format);
|
---|
[1] | 1356 |
|
---|
[3191] | 1357 | mr_asprintf(format_sz, "%s ", format);
|
---|
[1] | 1358 |
|
---|
| 1359 | #ifdef __FreeBSD__
|
---|
[3191] | 1360 | mr_asprintf(command, "lsvfs | tr -s '\t' ' ' | grep -v Filesys | grep -v -- -- | cut -d' ' -f1 | tr -s '\n' ' '");
|
---|
[1] | 1361 | #else
|
---|
[3191] | 1362 | mr_asprintf(command, "grep -v nodev /proc/filesystems | tr -s '\t' ' ' | cut -d' ' -f2 | tr -s '\n' ' '");
|
---|
[1] | 1363 | #endif
|
---|
| 1364 |
|
---|
[128] | 1365 | pin = popen(command, "r");
|
---|
[3191] | 1366 | mr_free(command);
|
---|
| 1367 |
|
---|
[128] | 1368 | if (!pin) {
|
---|
| 1369 | log_OS_error("Unable to read good formats");
|
---|
| 1370 | retval = 0;
|
---|
| 1371 | } else {
|
---|
[3291] | 1372 | mr_getline(p, pin);
|
---|
[3614] | 1373 | good_formats = mr_strip_spaces(p);
|
---|
[3337] | 1374 | mr_free(p);
|
---|
[3291] | 1375 | (void)pclose(pin);
|
---|
| 1376 | mr_strcat(good_formats, " swap lvm raid ntfs-3g ntfs 7 "); // " ntfs 7 " -- um, cheating much? :)
|
---|
[128] | 1377 | if (strstr(good_formats, format_sz)) {
|
---|
| 1378 | retval = 1;
|
---|
| 1379 | } else {
|
---|
| 1380 | retval = 0;
|
---|
| 1381 | }
|
---|
| 1382 | }
|
---|
[3291] | 1383 | mr_free(good_formats);
|
---|
[3191] | 1384 | mr_free(format_sz);
|
---|
| 1385 |
|
---|
[128] | 1386 | return (retval);
|
---|
[1] | 1387 | }
|
---|
| 1388 |
|
---|
| 1389 |
|
---|
| 1390 | /** @def SWAPLIST_COMMAND The command to list the swap files/partitions in use. */
|
---|
| 1391 |
|
---|
| 1392 | /**
|
---|
| 1393 | * Determine whether @p device_raw is currently mounted.
|
---|
| 1394 | * @param device_raw The device to check.
|
---|
| 1395 | * @return TRUE if it's mounted, FALSE if not.
|
---|
| 1396 | */
|
---|
[128] | 1397 | bool is_this_device_mounted(char *device_raw)
|
---|
[1] | 1398 | {
|
---|
| 1399 |
|
---|
[3448] | 1400 | char *tmp = NULL;
|
---|
| 1401 | bool retval = FALSE;
|
---|
| 1402 |
|
---|
[3453] | 1403 | mr_asprintf(tmp, "mr-device-mounted %s > /dev/null", device_raw);
|
---|
[3448] | 1404 | if (system(tmp) == 0) {
|
---|
| 1405 | retval = TRUE;
|
---|
| 1406 | }
|
---|
| 1407 | mr_free(tmp);
|
---|
[3450] | 1408 | return(retval);
|
---|
[3449] | 1409 | }
|
---|
[3448] | 1410 |
|
---|
[1] | 1411 | #ifdef __FreeBSD__
|
---|
| 1412 | // CODE IS FREEBSD-SPECIFIC
|
---|
| 1413 | /**
|
---|
| 1414 | * Create a loopback device for specified @p fname.
|
---|
| 1415 | * @param fname The file to associate with a device.
|
---|
| 1416 | * @return /dev entry for the device, or NULL if it couldn't be allocated.
|
---|
| 1417 | */
|
---|
[128] | 1418 | char *make_vn(char *fname)
|
---|
[1] | 1419 | {
|
---|
[128] | 1420 | char *device = (char *) malloc(MAX_STR_LEN);
|
---|
[3610] | 1421 | char *mddevice = NULL;
|
---|
| 1422 | char *command = NULL;
|
---|
| 1423 | char *tmp = NULL;
|
---|
[128] | 1424 | int vndev = 2;
|
---|
[3610] | 1425 |
|
---|
| 1426 | tmp = call_program_and_get_last_line_of_output("/sbin/sysctl -n kern.osreldate");
|
---|
| 1427 | if (atoi(tmp) < 500000) {
|
---|
[128] | 1428 | do {
|
---|
[3610] | 1429 | mr_asprintf(mddevice, "vn%ic", vndev++);
|
---|
| 1430 | mr_free(command);
|
---|
| 1431 | mr_asprintf(command, "vnconfig %s %s", mddevice, fname);
|
---|
[128] | 1432 | if (vndev > 10) {
|
---|
[3610] | 1433 | mr_free(tmp);
|
---|
| 1434 | mr_free(mddevice);
|
---|
| 1435 | mr_free(command);
|
---|
[128] | 1436 | return NULL;
|
---|
| 1437 | }
|
---|
| 1438 | }
|
---|
| 1439 | while (system(command));
|
---|
| 1440 | } else {
|
---|
[3610] | 1441 | mr_asprintf(command, "mdconfig -a -t vnode -f %s", fname);
|
---|
[128] | 1442 | mddevice = call_program_and_get_last_line_of_output(command);
|
---|
| 1443 | if (!strstr(mddevice, "md")) {
|
---|
[3610] | 1444 | mr_free(tmp);
|
---|
| 1445 | mr_free(command);
|
---|
| 1446 | mr_free(mddevice);
|
---|
[128] | 1447 | return NULL;
|
---|
| 1448 | }
|
---|
| 1449 | }
|
---|
[3610] | 1450 | mr_free(tmp);
|
---|
| 1451 | mr_free(command);
|
---|
[128] | 1452 | sprintf(device, "/dev/%s", mddevice);
|
---|
[3610] | 1453 | mr_free(mddevice);
|
---|
[128] | 1454 | return device;
|
---|
[1] | 1455 | }
|
---|
| 1456 |
|
---|
| 1457 |
|
---|
| 1458 |
|
---|
| 1459 | // CODE IS FREEBSD-SPECIFIC
|
---|
| 1460 | /**
|
---|
| 1461 | * Deallocate specified @p dname.
|
---|
| 1462 | * This should be called when you are done with the device created by make_vn(),
|
---|
| 1463 | * so the system does not run out of @c vn devices.
|
---|
| 1464 | * @param dname The device to deallocate.
|
---|
| 1465 | * @return 0 for success, nonzero for failure.
|
---|
| 1466 | */
|
---|
[128] | 1467 | int kick_vn(char *dname)
|
---|
[1] | 1468 | {
|
---|
[3191] | 1469 | char *command = NULL;
|
---|
[3610] | 1470 | char *tmp = NULL;
|
---|
[3191] | 1471 | int res = 0;
|
---|
[1] | 1472 |
|
---|
[128] | 1473 | if (strncmp(dname, "/dev/", 5) == 0) {
|
---|
| 1474 | dname += 5;
|
---|
| 1475 | }
|
---|
[1] | 1476 |
|
---|
[3610] | 1477 | tmp = call_program_and_get_last_line_of_output("/sbin/sysctl -n kern.osreldate");
|
---|
| 1478 | if (atoi(tmp) < 500000) {
|
---|
[3191] | 1479 | mr_asprintf(command, "vnconfig -d %s", dname);
|
---|
[128] | 1480 | } else {
|
---|
[3191] | 1481 | mr_asprintf(command, "mdconfig -d -u %s", dname);
|
---|
[128] | 1482 | }
|
---|
[3610] | 1483 | mr_free(tmp);
|
---|
[3191] | 1484 | res = system(command);
|
---|
| 1485 | mr_free(command);
|
---|
| 1486 | return(res);
|
---|
[1] | 1487 | }
|
---|
| 1488 | #endif
|
---|
| 1489 |
|
---|
| 1490 |
|
---|
| 1491 | /**
|
---|
| 1492 | * Mount the CD-ROM at @p mountpoint.
|
---|
| 1493 | * @param device The device (or file if g_ISO_restore_mode) to mount.
|
---|
| 1494 | * @param mountpoint The place to mount it.
|
---|
| 1495 | * @return 0 for success, nonzero for failure.
|
---|
| 1496 | */
|
---|
[1741] | 1497 | int mount_USB_here(char *device, char *mountpoint)
|
---|
| 1498 | {
|
---|
| 1499 | /*@ buffer ****************************************************** */
|
---|
[3191] | 1500 | char *command = NULL;
|
---|
[1741] | 1501 | int retval;
|
---|
| 1502 |
|
---|
| 1503 | assert_string_is_neither_NULL_nor_zerolength(device);
|
---|
| 1504 | assert_string_is_neither_NULL_nor_zerolength(mountpoint);
|
---|
| 1505 |
|
---|
| 1506 | make_hole_for_dir(mountpoint);
|
---|
| 1507 | if (isdigit(device[0])) {
|
---|
| 1508 | return(1);
|
---|
| 1509 | }
|
---|
[3191] | 1510 | log_msg(4, "(mount_USB_here --- device=%s, mountpoint=%s", device, mountpoint);
|
---|
[1741] | 1511 |
|
---|
| 1512 | #ifdef __FreeBSD__
|
---|
[3191] | 1513 | mr_asprintf(command, "mount_vfat %s %s 2>> %s", device, mountpoint, MONDO_LOGFILE);
|
---|
[1741] | 1514 |
|
---|
| 1515 | #else
|
---|
[3191] | 1516 | mr_asprintf(command, "mount %s -t vfat %s 2>> %s", device, mountpoint, MONDO_LOGFILE);
|
---|
[1741] | 1517 | #endif
|
---|
| 1518 |
|
---|
| 1519 | log_msg(4, command);
|
---|
| 1520 | retval = system(command);
|
---|
| 1521 | log_msg(1, "system(%s) returned %d", command, retval);
|
---|
[3191] | 1522 | mr_free(command);
|
---|
[1741] | 1523 |
|
---|
| 1524 | return (retval);
|
---|
| 1525 | }
|
---|
| 1526 |
|
---|
| 1527 | /**
|
---|
[3878] | 1528 | * Find out what number CD is in the drive.
|
---|
| 1529 | * @param bkpinfo The backup information structure. The @c bkpinfo->media_device field is the only one used.
|
---|
| 1530 | * @return The current CD number, or -1 if it could not be found.
|
---|
| 1531 | * @note If the CD is not mounted, it will be mounted
|
---|
| 1532 | * (and remain mounted after this function returns).
|
---|
| 1533 | */
|
---|
| 1534 | int what_number_cd_is_this(void) {
|
---|
[3875] | 1535 |
|
---|
[3878] | 1536 | int cd_number = -1;
|
---|
| 1537 | char *mountdev = NULL;
|
---|
[3875] | 1538 | char *tmp = NULL;
|
---|
[1] | 1539 |
|
---|
[3878] | 1540 | assert(bkpinfo != NULL);
|
---|
| 1541 | // log_it("Asking what_number_cd_is_this");
|
---|
| 1542 | if ((g_ISO_restore_mode) || (g_restoring_live_from_cd)) {
|
---|
| 1543 | tmp = call_program_and_get_last_line_of_output("mount | grep iso9660 | awk '{print $3;}'");
|
---|
| 1544 | mr_asprintf(mountdev, "%s%s", tmp, "/archives/THIS-CD-NUMBER");
|
---|
[3875] | 1545 | mr_free(tmp);
|
---|
[3878] | 1546 | cd_number = atoi(last_line_of_file(mountdev));
|
---|
| 1547 | mr_free(mountdev);
|
---|
| 1548 | return (cd_number);
|
---|
[2682] | 1549 | }
|
---|
[1] | 1550 |
|
---|
[3878] | 1551 | if ((bkpinfo->media_device == NULL) || !does_file_exist(bkpinfo->media_device)) {
|
---|
| 1552 | log_it("ERROR: bkpinfo->media_device shoulnd't be unaccessible here\n");
|
---|
| 1553 | /* trying again ! */
|
---|
| 1554 | bkpinfo->media_device = find_optical_device();
|
---|
[2682] | 1555 | }
|
---|
[3878] | 1556 | if ((bkpinfo->media_device == NULL) || !does_file_exist(bkpinfo->media_device)) {
|
---|
| 1557 | fatal_error("ERROR: bkpinfo->media_device shoulnd't really be unaccessible here\n");
|
---|
[2682] | 1558 | }
|
---|
[3878] | 1559 | if (!is_this_device_mounted(MNT_CDROM)) {
|
---|
| 1560 | mount_media(MNT_CDROM);
|
---|
| 1561 | }
|
---|
[2682] | 1562 |
|
---|
[3878] | 1563 | cd_number = atoi(last_line_of_file(MNT_CDROM "/archives/THIS-CD-NUMBER"));
|
---|
| 1564 | return(cd_number);
|
---|
[2682] | 1565 | }
|
---|
| 1566 |
|
---|
| 1567 |
|
---|
| 1568 |
|
---|
| 1569 |
|
---|
[1] | 1570 | /**
|
---|
| 1571 | * Ask the user for CD number @p cd_number_i_want.
|
---|
| 1572 | * Sets g_current_media_number once the correct CD is inserted.
|
---|
| 1573 | * @param bkpinfo The backup information structure. Fields used:
|
---|
| 1574 | * - @c bkpinfo->backup_media_type
|
---|
[20] | 1575 | * - @c bkpinfo->prefix
|
---|
[1] | 1576 | * - @c bkpinfo->isodir
|
---|
| 1577 | * - @c bkpinfo->media_device
|
---|
| 1578 | * - @c bkpinfo->please_dont_eject_when_restoring
|
---|
| 1579 | * @param cd_number_i_want The CD number to ask for.
|
---|
| 1580 | */
|
---|
| 1581 | void
|
---|
[1645] | 1582 | insist_on_this_cd_number(int cd_number_i_want)
|
---|
[1] | 1583 | {
|
---|
| 1584 |
|
---|
[128] | 1585 | /*@ int ************************************************************* */
|
---|
| 1586 | int res = 0;
|
---|
[1] | 1587 |
|
---|
| 1588 |
|
---|
[128] | 1589 | /*@ buffers ********************************************************* */
|
---|
[3191] | 1590 | char *tmp = NULL;
|
---|
[2242] | 1591 | char *mds = NULL;
|
---|
[3191] | 1592 | char *request = NULL;
|
---|
[1] | 1593 |
|
---|
[128] | 1594 | assert(bkpinfo != NULL);
|
---|
| 1595 | assert(cd_number_i_want > 0);
|
---|
[1] | 1596 |
|
---|
| 1597 | // log_msg(3, "Insisting on CD number %d", cd_number_i_want);
|
---|
| 1598 |
|
---|
[128] | 1599 | if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
|
---|
| 1600 | log_msg(3,
|
---|
| 1601 | "No need to insist_on_this_cd_number when the backup type isn't CD-R(W) or NFS or ISO");
|
---|
| 1602 | return;
|
---|
| 1603 | }
|
---|
[3719] | 1604 | if (!does_file_exist(MNT_CDROM)) {
|
---|
| 1605 | mr_asprintf(tmp, "mkdir -p " MNT_CDROM);
|
---|
| 1606 | run_program_and_log_output(tmp, 5);
|
---|
| 1607 | mr_free(tmp);
|
---|
| 1608 | }
|
---|
[3191] | 1609 |
|
---|
| 1610 | if (g_ISO_restore_mode || bkpinfo->backup_media_type == iso || bkpinfo->backup_media_type == netfs) {
|
---|
[2866] | 1611 | g_ISO_restore_mode = TRUE;
|
---|
| 1612 | }
|
---|
[1645] | 1613 | if ((res = what_number_cd_is_this()) != cd_number_i_want) {
|
---|
[3191] | 1614 | log_msg(3, "Currently, we hold %d but we want %d", res, cd_number_i_want);
|
---|
[2866] | 1615 |
|
---|
| 1616 | /* Now we need to umount the current media to have the next mounted after */
|
---|
[2878] | 1617 | run_program_and_log_output("umount -d " MNT_CDROM, FALSE);
|
---|
[2866] | 1618 | log_msg(3, "Mounting next media %d",cd_number_i_want);
|
---|
| 1619 | g_current_media_number = cd_number_i_want;
|
---|
[3877] | 1620 | mount_media(MNT_CDROM);
|
---|
[2866] | 1621 |
|
---|
[2242] | 1622 | mds = media_descriptor_string(bkpinfo->backup_media_type);
|
---|
[3191] | 1623 | log_msg(3, "Insisting on %s #%d", mds, cd_number_i_want);
|
---|
| 1624 | mr_asprintf(request, "Please insert %s #%d and press Enter.", mds, cd_number_i_want);
|
---|
[2242] | 1625 | mr_free(mds);
|
---|
[3191] | 1626 |
|
---|
[1645] | 1627 | while (what_number_cd_is_this() != cd_number_i_want) {
|
---|
[3191] | 1628 | sync();
|
---|
[128] | 1629 | if (is_this_device_mounted(MNT_CDROM)) {
|
---|
| 1630 | res =
|
---|
[2878] | 1631 | run_program_and_log_output("umount -d " MNT_CDROM, FALSE);
|
---|
[128] | 1632 | } else {
|
---|
| 1633 | res = 0;
|
---|
| 1634 | }
|
---|
| 1635 | if (res) {
|
---|
[541] | 1636 | log_to_screen("WARNING - failed to unmount CD-ROM drive");
|
---|
[128] | 1637 | }
|
---|
| 1638 | if (!bkpinfo->please_dont_eject) {
|
---|
| 1639 | res = eject_device(bkpinfo->media_device);
|
---|
| 1640 | } else {
|
---|
| 1641 | res = 0;
|
---|
| 1642 | }
|
---|
| 1643 | if (res) {
|
---|
[541] | 1644 | log_to_screen("WARNING - failed to eject CD-ROM disk");
|
---|
[128] | 1645 | }
|
---|
| 1646 | popup_and_OK(request);
|
---|
| 1647 | if (!bkpinfo->please_dont_eject) {
|
---|
| 1648 | inject_device(bkpinfo->media_device);
|
---|
| 1649 | }
|
---|
[3191] | 1650 | sync();
|
---|
[128] | 1651 | }
|
---|
[3191] | 1652 | mr_free(request);
|
---|
| 1653 |
|
---|
[128] | 1654 | log_msg(1, "Thankyou. Proceeding...");
|
---|
| 1655 | g_current_media_number = cd_number_i_want;
|
---|
| 1656 | }
|
---|
[1] | 1657 | }
|
---|
| 1658 |
|
---|
| 1659 |
|
---|
[2434] | 1660 | /* Update the bkpinfo structure for exclude & include paths
|
---|
| 1661 | * in order to handle correctly paths corresponding to devices */
|
---|
| 1662 | void mr_make_devlist_from_pathlist(char *pathlist, char mode) {
|
---|
| 1663 |
|
---|
| 1664 | char *token = NULL;
|
---|
| 1665 | int lastpos = 0;
|
---|
| 1666 | char *mounted_on_dsf = NULL;
|
---|
| 1667 | char *not_mounted_on_dsf = NULL;
|
---|
[2713] | 1668 | char token_chars[] ="|\t\r\f\a\0\n";
|
---|
[2525] | 1669 | char *tmp = NULL;
|
---|
| 1670 | char *tmp1 = NULL;
|
---|
[2934] | 1671 | char *tmp2 = NULL;
|
---|
[2434] | 1672 |
|
---|
[2745] | 1673 | if (pathlist == NULL) {
|
---|
| 1674 | return;
|
---|
| 1675 | }
|
---|
[2434] | 1676 | while ((token = mr_strtok(pathlist, token_chars, &lastpos)) != NULL) {
|
---|
| 1677 | switch (get_dsf_mount_list(token, &mounted_on_dsf, ¬_mounted_on_dsf)) {
|
---|
| 1678 | case 1:
|
---|
| 1679 | if (mode == 'E') {
|
---|
| 1680 | log_msg(1, "WARNING ! %s doesn't exist in -E option", token);
|
---|
| 1681 | } else {
|
---|
[2453] | 1682 | log_msg(1, "ERROR ! %s doesn't exist in -I option", token);
|
---|
[2434] | 1683 | fatal_error("Error processing -I option");
|
---|
| 1684 | }
|
---|
| 1685 | break;
|
---|
| 1686 | /* Everything is OK; proceed to archive data */
|
---|
| 1687 | case 0:
|
---|
| 1688 | if (mode == 'E') {
|
---|
| 1689 | if (strlen(mounted_on_dsf)) {
|
---|
[2632] | 1690 | log_to_screen("Excluding the following file systems on %s:", token);
|
---|
| 1691 | log_to_screen("==> %s", mounted_on_dsf);
|
---|
[2434] | 1692 | log_msg (5, "Adding to bkpinfo->exclude_paths due to -E option: %s", mounted_on_dsf);
|
---|
[2713] | 1693 | if (bkpinfo->exclude_paths) {
|
---|
| 1694 | mr_strcat(bkpinfo->exclude_paths,"|%s",mounted_on_dsf);
|
---|
| 1695 | } else {
|
---|
[3185] | 1696 | mr_asprintf(bkpinfo->exclude_paths,"%s",mounted_on_dsf);
|
---|
[2713] | 1697 | }
|
---|
| 1698 | if (bkpinfo->exclude_devs) {
|
---|
| 1699 | mr_strcat(bkpinfo->exclude_devs,"|%s",token);
|
---|
| 1700 | } else {
|
---|
[3185] | 1701 | mr_asprintf(bkpinfo->exclude_devs,"%s",token);
|
---|
[2713] | 1702 | }
|
---|
[2434] | 1703 | }
|
---|
[2453] | 1704 | } else {
|
---|
[2632] | 1705 | log_to_screen("Archiving only the following file systems on %s:", token);
|
---|
[2636] | 1706 | log_to_screen("==> %s", mounted_on_dsf);
|
---|
[3191] | 1707 | mr_free(bkpinfo->include_paths);
|
---|
| 1708 | mr_asprintf(bkpinfo->include_paths, "%s", "/");
|
---|
[2453] | 1709 | if (strlen(not_mounted_on_dsf)) {
|
---|
| 1710 | log_msg (5, "Adding to bkpinfo->exclude_paths due to -I option: %s", not_mounted_on_dsf);
|
---|
[2632] | 1711 | log_to_screen("Not archiving the following file systems:");
|
---|
| 1712 | log_to_screen("==> %s", not_mounted_on_dsf);
|
---|
[2713] | 1713 | if (bkpinfo->exclude_paths) {
|
---|
| 1714 | mr_strcat(bkpinfo->exclude_paths, "|%s",not_mounted_on_dsf);
|
---|
| 1715 | } else {
|
---|
[3185] | 1716 | mr_asprintf(bkpinfo->exclude_paths,"%s",not_mounted_on_dsf);
|
---|
[2713] | 1717 | }
|
---|
[2453] | 1718 | }
|
---|
[2434] | 1719 | }
|
---|
| 1720 | break;
|
---|
| 1721 | /* It's a dsf but not a whole disk dsf */
|
---|
| 1722 | case -2:
|
---|
| 1723 | log_to_screen("Could %s be a partition instead of a whole disk device special file?\nIgnored.", token);
|
---|
| 1724 | break;
|
---|
| 1725 | /* A device special file was not passed in. Process it as a path. */
|
---|
| 1726 | case -1:
|
---|
[2713] | 1727 | /* Adds a | to ensure correct detection even at both ends */
|
---|
[3185] | 1728 | mr_asprintf(tmp1,"|%s",token);
|
---|
| 1729 | mr_asprintf(tmp2,"|%s|",token);
|
---|
[2434] | 1730 | if (mode == 'E') {
|
---|
[2473] | 1731 | /* Add the token if not already in the list */
|
---|
[3185] | 1732 | mr_asprintf(tmp,"|%s|",bkpinfo->exclude_paths);
|
---|
[2934] | 1733 | if (strstr(tmp,tmp2) == NULL) {
|
---|
[2713] | 1734 | if (bkpinfo->exclude_paths) {
|
---|
| 1735 | mr_strcat(bkpinfo->exclude_paths,tmp1);
|
---|
| 1736 | mr_free(tmp1);
|
---|
| 1737 | } else {
|
---|
| 1738 | bkpinfo->exclude_paths = tmp1;
|
---|
| 1739 | }
|
---|
[2473] | 1740 | }
|
---|
[2434] | 1741 | } else {
|
---|
[2473] | 1742 | /* Add the token if not already in the list */
|
---|
[3185] | 1743 | mr_asprintf(tmp,"|%s|",bkpinfo->include_paths);
|
---|
[2934] | 1744 | if (strstr(tmp,tmp2) == NULL) {
|
---|
[3191] | 1745 | mr_strcat(bkpinfo->include_paths, "%s", tmp1);
|
---|
[2473] | 1746 | }
|
---|
[2713] | 1747 | mr_free(tmp1);
|
---|
[2434] | 1748 | }
|
---|
[2525] | 1749 | mr_free(tmp);
|
---|
[2934] | 1750 | mr_free(tmp2);
|
---|
[2434] | 1751 | break;
|
---|
| 1752 | }
|
---|
| 1753 | mr_free(token);
|
---|
| 1754 |
|
---|
| 1755 | if (bkpinfo->include_paths != NULL) {
|
---|
| 1756 | log_msg(1, "include_paths is now '%s'", bkpinfo->include_paths);
|
---|
| 1757 | }
|
---|
| 1758 | if (bkpinfo->exclude_paths != NULL) {
|
---|
| 1759 | log_msg(1, "exclude_paths is now '%s'", bkpinfo->exclude_paths);
|
---|
| 1760 | }
|
---|
| 1761 | if (bkpinfo->exclude_devs != NULL) {
|
---|
| 1762 | log_msg(1, "exclude_devs is now '%s'", bkpinfo->exclude_devs);
|
---|
| 1763 | }
|
---|
| 1764 | }
|
---|
| 1765 | }
|
---|
| 1766 |
|
---|
| 1767 |
|
---|
[3375] | 1768 | /**
|
---|
| 1769 | * Return the type of boot of the system (UEFI, EFI or BIOS)
|
---|
| 1770 | */
|
---|
[3413] | 1771 | t_boot mr_boot_type(void) {
|
---|
[2434] | 1772 |
|
---|
[3375] | 1773 | t_boot ret = BIOS;
|
---|
[3377] | 1774 | DIR *fd = NULL;
|
---|
[2434] | 1775 |
|
---|
[3375] | 1776 | #ifdef __IA64__
|
---|
[3395] | 1777 | return(EFI);
|
---|
[3375] | 1778 | #endif
|
---|
| 1779 | /* Try to detect whether we are in fact in UEFI mode */
|
---|
[3378] | 1780 | fd = opendir("/sys/firmware/efi");
|
---|
[3375] | 1781 | if (fd != NULL) {
|
---|
| 1782 | ret = UEFI;
|
---|
[3398] | 1783 | log_msg(2, "UEFI boot mode detected");
|
---|
[3377] | 1784 | closedir(fd);
|
---|
[3375] | 1785 | }
|
---|
| 1786 | return(ret);
|
---|
| 1787 | }
|
---|
| 1788 |
|
---|
| 1789 |
|
---|
[2434] | 1790 | /**
|
---|
[3877] | 1791 | * Find out which boot loader is in use.
|
---|
| 1792 | * @param which_device Device to look for the boot loader on.
|
---|
| 1793 | * @return 'L' for LILO, 'E'for ELILO, 'G' for GRUB, 'B' or 'D' for FreeBSD boot loaders, or 'U' for Unknown.
|
---|
| 1794 | * @note Under Linux, all drives are examined, not just @p which_device.
|
---|
| 1795 | */
|
---|
| 1796 | #ifdef __FreeBSD__
|
---|
| 1797 | char which_boot_loader(const char *which_device)
|
---|
| 1798 | {
|
---|
| 1799 | int count_lilos = 0;
|
---|
| 1800 | int count_grubs = 0;
|
---|
| 1801 | int count_boot0s = 0;
|
---|
| 1802 | int count_dangerouslydedicated = 0;
|
---|
| 1803 |
|
---|
| 1804 | log_it("looking at drive %s's MBR", which_device);
|
---|
| 1805 | if (does_string_exist_in_boot_block(which_device, "GRUB")) {
|
---|
| 1806 | count_grubs++;
|
---|
| 1807 | }
|
---|
| 1808 | if (does_string_exist_in_boot_block(which_device, "LILO")) {
|
---|
| 1809 | count_lilos++;
|
---|
| 1810 | }
|
---|
| 1811 | if (does_string_exist_in_boot_block(which_device, "Drive")) {
|
---|
| 1812 | count_boot0s++;
|
---|
| 1813 | }
|
---|
| 1814 | if (does_string_exist_in_first_N_blocks
|
---|
| 1815 | (which_device, "FreeBSD/i386", 17)) {
|
---|
| 1816 | count_dangerouslydedicated++;
|
---|
| 1817 | }
|
---|
| 1818 | log_it("%d grubs and %d lilos and %d elilos and %d boot0s and %d DD\n",
|
---|
| 1819 | count_grubs, count_lilos, count_elilos, count_boot0s,
|
---|
| 1820 | count_dangerouslydedicated);
|
---|
| 1821 |
|
---|
| 1822 | if (count_grubs && !count_lilos) {
|
---|
| 1823 | return ('G');
|
---|
| 1824 | } else if (count_lilos && !count_grubs) {
|
---|
| 1825 | return ('L');
|
---|
| 1826 | } else if (count_grubs == 1 && count_lilos == 1) {
|
---|
| 1827 | log_it("I'll bet you used to use LILO but switched to GRUB...");
|
---|
| 1828 | return ('G');
|
---|
| 1829 | } else if (count_boot0s == 1) {
|
---|
| 1830 | return ('B');
|
---|
| 1831 | } else if (count_dangerouslydedicated) {
|
---|
| 1832 | return ('D');
|
---|
| 1833 | } else {
|
---|
| 1834 | log_it("Unknown boot loader");
|
---|
| 1835 | return ('U');
|
---|
| 1836 | }
|
---|
| 1837 | }
|
---|
| 1838 |
|
---|
| 1839 | #else
|
---|
| 1840 |
|
---|
| 1841 | char which_boot_loader(const char *which_device)
|
---|
| 1842 | {
|
---|
| 1843 | /*@ buffer ***************************************************** */
|
---|
| 1844 | char *list_drives_cmd = NULL;
|
---|
| 1845 | char *current_drive;
|
---|
| 1846 | char *tmp;
|
---|
| 1847 |
|
---|
| 1848 | /*@ pointers *************************************************** */
|
---|
| 1849 | FILE *pdrives;
|
---|
| 1850 |
|
---|
| 1851 | /*@ int ******************************************************** */
|
---|
| 1852 | int count_lilos = 0;
|
---|
| 1853 | int count_grubs = 0;
|
---|
| 1854 |
|
---|
| 1855 | /*@ end vars *************************************************** */
|
---|
| 1856 |
|
---|
| 1857 | #ifdef __IA64__
|
---|
| 1858 | /* No choice for it */
|
---|
| 1859 | return ('E');
|
---|
| 1860 | #endif
|
---|
| 1861 | if (bkpinfo->boot_type == EFI) {
|
---|
| 1862 | /* No choice for it */
|
---|
| 1863 | return ('E');
|
---|
| 1864 | }
|
---|
| 1865 |
|
---|
| 1866 | if (bkpinfo->boot_type == UEFI) {
|
---|
| 1867 | /* hardcoded for now. We can for sure do a better job here ! */
|
---|
| 1868 | /* RHEL, SLES, Mageia, Debian, Ubuntu use grub as boot loader as it seems for UEFI */
|
---|
| 1869 | return ('G');
|
---|
| 1870 | }
|
---|
| 1871 |
|
---|
| 1872 | assert(which_device != NULL);
|
---|
| 1873 |
|
---|
| 1874 | tmp = where_is_root_mounted();
|
---|
| 1875 | mr_asprintf(list_drives_cmd, "mr-parted2fdisk -l 2>/dev/null | grep \"/dev/.*:\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/; echo %s", tmp);
|
---|
| 1876 | log_it("list_drives_cmd = %s", list_drives_cmd);
|
---|
| 1877 | mr_free(tmp);
|
---|
| 1878 | if (!(pdrives = popen(list_drives_cmd, "r"))) {
|
---|
| 1879 | log_OS_error("Unable to open list of drives");
|
---|
| 1880 | mr_free(list_drives_cmd);
|
---|
| 1881 | return ('\0');
|
---|
| 1882 | }
|
---|
| 1883 | mr_free(list_drives_cmd);
|
---|
| 1884 |
|
---|
| 1885 | malloc_string(current_drive);
|
---|
| 1886 | if (bkpinfo->boot_type == BIOS) {
|
---|
| 1887 | for (tmp = fgets(current_drive, MAX_STR_LEN, pdrives); !feof(pdrives) && (tmp != NULL); tmp = fgets(current_drive, MAX_STR_LEN, pdrives)) {
|
---|
| 1888 | strip_spaces(current_drive);
|
---|
| 1889 | log_it("looking at drive %s's MBR", current_drive);
|
---|
| 1890 | if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
|
---|
| 1891 | count_grubs++;
|
---|
| 1892 | strcpy(which_device, current_drive);
|
---|
| 1893 | break;
|
---|
| 1894 | }
|
---|
| 1895 | if (does_string_exist_in_boot_block(current_drive, "LILO")) {
|
---|
| 1896 | count_lilos++;
|
---|
| 1897 | strcpy(which_device, current_drive);
|
---|
| 1898 | break;
|
---|
| 1899 | }
|
---|
| 1900 | }
|
---|
| 1901 | if (pclose(pdrives)) {
|
---|
| 1902 | log_OS_error("Cannot pclose pdrives");
|
---|
| 1903 | }
|
---|
| 1904 | log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
|
---|
| 1905 | if (count_grubs && !count_lilos) {
|
---|
| 1906 | paranoid_free(current_drive);
|
---|
| 1907 | return ('G');
|
---|
| 1908 | } else if (count_lilos && !count_grubs) {
|
---|
| 1909 | paranoid_free(current_drive);
|
---|
| 1910 | return ('L');
|
---|
| 1911 | } else if (count_grubs == 1 && count_lilos == 1) {
|
---|
| 1912 | log_it("I'll bet you used to use LILO but switched to GRUB...");
|
---|
| 1913 | paranoid_free(current_drive);
|
---|
| 1914 | return ('G');
|
---|
| 1915 | } else {
|
---|
| 1916 | // We need to look on each partition then
|
---|
| 1917 | mr_asprintf(list_drives_cmd, "mr-parted2fdisk -l 2>/dev/null | grep -E \"^/dev/\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/");
|
---|
| 1918 | log_it("list_drives_cmd = %s", list_drives_cmd);
|
---|
| 1919 |
|
---|
| 1920 | if (!(pdrives = popen(list_drives_cmd, "r"))) {
|
---|
| 1921 | log_OS_error("Unable to open list of drives");
|
---|
| 1922 | mr_free(list_drives_cmd);
|
---|
| 1923 | paranoid_free(current_drive);
|
---|
| 1924 | return ('\0');
|
---|
| 1925 | }
|
---|
| 1926 | mr_free(list_drives_cmd);
|
---|
| 1927 |
|
---|
| 1928 | for (tmp = fgets(current_drive, MAX_STR_LEN, pdrives); !feof(pdrives) && (tmp != NULL);
|
---|
| 1929 | tmp = fgets(current_drive, MAX_STR_LEN, pdrives)) {
|
---|
| 1930 | strip_spaces(current_drive);
|
---|
| 1931 | log_it("looking at partition %s's BR", current_drive);
|
---|
| 1932 | if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
|
---|
| 1933 | count_grubs++;
|
---|
| 1934 | strcpy(which_device, current_drive);
|
---|
| 1935 | break;
|
---|
| 1936 | }
|
---|
| 1937 | if (does_string_exist_in_boot_block(current_drive, "LILO")) {
|
---|
| 1938 | count_lilos++;
|
---|
| 1939 | strcpy(which_device, current_drive);
|
---|
| 1940 | break;
|
---|
| 1941 | }
|
---|
| 1942 | }
|
---|
| 1943 | if (pclose(pdrives)) {
|
---|
| 1944 | log_OS_error("Cannot pclose pdrives");
|
---|
| 1945 | }
|
---|
| 1946 | log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
|
---|
| 1947 | paranoid_free(current_drive);
|
---|
| 1948 | if (count_grubs && !count_lilos) {
|
---|
| 1949 | return ('G');
|
---|
| 1950 | } else if (count_lilos && !count_grubs) {
|
---|
| 1951 | return ('L');
|
---|
| 1952 | } else if (count_grubs == 1 && count_lilos == 1) {
|
---|
| 1953 | log_it("I'll bet you used to use LILO but switched to GRUB...");
|
---|
| 1954 | return ('G');
|
---|
| 1955 | } else {
|
---|
| 1956 | log_it("Unknown boot loader");
|
---|
| 1957 | return ('U');
|
---|
| 1958 | }
|
---|
| 1959 | }
|
---|
| 1960 | }
|
---|
| 1961 | }
|
---|
| 1962 | #endif
|
---|
| 1963 |
|
---|
| 1964 |
|
---|
| 1965 | /**
|
---|
[3878] | 1966 | * Get a |-separated list of NETFS mounts.
|
---|
| 1967 | * @return The list created.
|
---|
| 1968 | * @note The return value points to allocated string that needs to be freed by
|
---|
| 1969 | * caller
|
---|
| 1970 | * @bug Even though we only want the mounts, the devices are still checked.
|
---|
| 1971 | */
|
---|
| 1972 | char *list_of_NETFS_mounts_only(void)
|
---|
| 1973 | {
|
---|
| 1974 | char *exclude_these_directories = NULL;
|
---|
| 1975 |
|
---|
| 1976 | 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;}'");
|
---|
| 1977 | log_msg(9,"list_of_NETFS_mounts_only returns %s",exclude_these_directories);
|
---|
| 1978 | return(exclude_these_directories);
|
---|
| 1979 | }
|
---|
| 1980 |
|
---|
| 1981 | /* @} - end of utilityGroup */
|
---|
| 1982 |
|
---|
| 1983 |
|
---|
| 1984 | /**
|
---|
[1] | 1985 | * Ask user for details of backup/restore information.
|
---|
| 1986 | * Called when @c mondoarchive doesn't get any parameters.
|
---|
| 1987 | * @param bkpinfo The backup information structure to fill out with the user's data.
|
---|
| 1988 | * @param archiving_to_media TRUE if archiving, FALSE if restoring.
|
---|
| 1989 | * @return 0, always.
|
---|
| 1990 | * @bug No point of `int' return value.
|
---|
| 1991 | * @ingroup archiveGroup
|
---|
| 1992 | */
|
---|
[1645] | 1993 | int interactively_obtain_media_parameters_from_user(bool archiving_to_media)
|
---|
[1] | 1994 | // archiving_to_media is TRUE if I'm being called by mondoarchive
|
---|
| 1995 | // archiving_to_media is FALSE if I'm being called by mondorestore
|
---|
| 1996 | {
|
---|
[2214] | 1997 | char *tmp = NULL;
|
---|
[3154] | 1998 | char *sz = NULL;
|
---|
[3111] | 1999 | char *tmpro = NULL;
|
---|
[2304] | 2000 | char *tmp1 = NULL;
|
---|
[3822] | 2001 | char *tmp2 = NULL;
|
---|
[2242] | 2002 | char *mds = NULL;
|
---|
[3212] | 2003 | char *oldtmp = NULL;
|
---|
[2847] | 2004 | char *q = NULL;
|
---|
[3062] | 2005 | char p[16*MAX_STR_LEN];
|
---|
[3191] | 2006 | char *sz_size = NULL;
|
---|
| 2007 | char *command = NULL;
|
---|
[2771] | 2008 | char *compression_type = NULL;
|
---|
[3191] | 2009 | char *comment = NULL;
|
---|
[3877] | 2010 | char i = '\0';
|
---|
[128] | 2011 | FILE *fin;
|
---|
[1] | 2012 |
|
---|
[2304] | 2013 | malloc_string(tmp1);
|
---|
[128] | 2014 | assert(bkpinfo != NULL);
|
---|
| 2015 | bkpinfo->nonbootable_backup = FALSE;
|
---|
[1] | 2016 |
|
---|
[2380] | 2017 | // Tape, CD, NETFS, ...?
|
---|
[128] | 2018 | srandom(getpid());
|
---|
[3881] | 2019 | bkpinfo->backup_media_type = (g_restoring_live_from_cd) ? optical : which_backup_media_type(bkpinfo->restore_data);
|
---|
[128] | 2020 | if (bkpinfo->backup_media_type == none) {
|
---|
[3653] | 2021 | log_to_screen("User has chosen not to backup the machine");
|
---|
[128] | 2022 | finish(1);
|
---|
| 2023 | }
|
---|
[3879] | 2024 | tmp = bkptype_to_string(bkpinfo->backup_media_type);
|
---|
| 2025 | log_msg(3, "media type = %s", tmp);
|
---|
| 2026 | mr_free(tmp);
|
---|
[3875] | 2027 |
|
---|
[1934] | 2028 | /* Why asking to remove the media with tape ?
|
---|
[128] | 2029 | if (bkpinfo->backup_media_type == tape && bkpinfo->restore_data) {
|
---|
[1885] | 2030 | popup_and_OK("Please remove media from drive(s)");
|
---|
[128] | 2031 | }
|
---|
[1934] | 2032 | */
|
---|
[3154] | 2033 | if (archiving_to_media) {
|
---|
[3875] | 2034 | // TODO: Should be common ?
|
---|
[3154] | 2035 | setup_tmpdir(NULL);
|
---|
| 2036 | /*
|
---|
| 2037 | * Set the scratchdir to reside on the partition with the most free space.
|
---|
| 2038 | * Automatically excludes DOS, NTFS, SMB, and NFS filesystems.
|
---|
| 2039 | */
|
---|
| 2040 | #ifdef __FreeBSD__
|
---|
[3610] | 2041 | 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] | 2042 | #else
|
---|
[3610] | 2043 | 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] | 2044 | #endif
|
---|
| 2045 |
|
---|
| 2046 | if (tmp[0] != '/') {
|
---|
[3185] | 2047 | mr_asprintf(sz, "%s", tmp);
|
---|
[3154] | 2048 | mr_free(tmp);
|
---|
[3185] | 2049 | mr_asprintf(tmp, "/%s", sz);
|
---|
[3154] | 2050 | mr_free(sz);
|
---|
| 2051 | }
|
---|
| 2052 | setup_scratchdir(tmp);
|
---|
[3292] | 2053 | mr_free(tmp);
|
---|
[3875] | 2054 |
|
---|
| 2055 | if ((compression_type = which_compression_type()) == NULL) {
|
---|
| 2056 | log_to_screen("User has chosen not to backup the machine");
|
---|
| 2057 | finish(1);
|
---|
| 2058 | }
|
---|
| 2059 |
|
---|
| 2060 | bkpinfo->compression_level = (bkpinfo->backup_media_type == cdstream) ? 1 : 5;
|
---|
| 2061 | if ((bkpinfo->compression_level = which_compression_level()) == -1) {
|
---|
| 2062 | log_to_screen("User has chosen not to backup the machine");
|
---|
| 2063 | finish(1);
|
---|
| 2064 | }
|
---|
| 2065 | bkpinfo->cdrw_speed = (bkpinfo->backup_media_type == cdstream) ? 2 : 4;
|
---|
| 2066 | bkpinfo->use_lzo = (bkpinfo->backup_media_type == cdstream) ? TRUE : FALSE;
|
---|
[3154] | 2067 | }
|
---|
[128] | 2068 | mvaddstr_and_log_it(2, 0, " ");
|
---|
[1] | 2069 |
|
---|
[1934] | 2070 | // Find device's /dev (or SCSI) entry
|
---|
[128] | 2071 | switch (bkpinfo->backup_media_type) {
|
---|
[3881] | 2072 | case optical:
|
---|
[1687] | 2073 | case usb:
|
---|
[1745] | 2074 | /* Never try to eject a USB device */
|
---|
| 2075 | if (bkpinfo->backup_media_type == usb) {
|
---|
[1746] | 2076 | bkpinfo->please_dont_eject = TRUE;
|
---|
[1745] | 2077 | }
|
---|
[3875] | 2078 | mds = media_descriptor_string(bkpinfo->backup_media_type);
|
---|
| 2079 | if (bkpinfo->backup_media_type != usb) {
|
---|
| 2080 | if (ask_me_yes_or_no("Is your computer a laptop type (manual insert of MondoRescue media)?")) {
|
---|
| 2081 | bkpinfo->manual_cd_tray = TRUE;
|
---|
[128] | 2082 | }
|
---|
[3881] | 2083 | if (ask_me_yes_or_no("Do you want to blank your media before burning them (RW type of media)?")) {
|
---|
| 2084 | bkpinfo->wipe_media_first = TRUE;
|
---|
| 2085 | }
|
---|
[3875] | 2086 | }
|
---|
| 2087 |
|
---|
| 2088 | if (bkpinfo->media_device == NULL) {
|
---|
| 2089 | bkpinfo->media_device = find_device(bkpinfo->backup_media_type);
|
---|
| 2090 | if (bkpinfo->media_device != NULL) {
|
---|
| 2091 | log_msg(1, "MondoRescue device found automatically for your %s is %s", mds, bkpinfo->media_device);
|
---|
| 2092 | } else {
|
---|
| 2093 | log_msg(1, "No MondoRescue device found yet for your %s", mds);
|
---|
[2771] | 2094 | }
|
---|
[3875] | 2095 | }
|
---|
[3191] | 2096 |
|
---|
[3875] | 2097 | if (archiving_to_media) {
|
---|
[128] | 2098 | if (bkpinfo->backup_media_type == dvd) {
|
---|
[2304] | 2099 | strcpy(tmp1, "1");
|
---|
[3191] | 2100 | mr_asprintf(sz_size, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4482 MB
|
---|
[128] | 2101 | log_msg(1, "Setting to DVD defaults");
|
---|
| 2102 | } else {
|
---|
[2304] | 2103 | strcpy(tmp1, "4");
|
---|
[3191] | 2104 | mr_asprintf(sz_size, "%d", 650);
|
---|
[128] | 2105 | log_msg(1, "Setting to CD defaults");
|
---|
| 2106 | }
|
---|
[3875] | 2107 |
|
---|
| 2108 | mr_asprintf(comment, "What speed is your %s writer?", mds);
|
---|
[1687] | 2109 | if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) {
|
---|
[2304] | 2110 | if (!popup_and_get_string("Speed", comment, tmp1, 4)) {
|
---|
[3653] | 2111 | log_to_screen("User has chosen not to backup the machine");
|
---|
[3191] | 2112 | mr_free(comment);
|
---|
[128] | 2113 | finish(1);
|
---|
| 2114 | }
|
---|
| 2115 | }
|
---|
[3191] | 2116 | mr_free(comment);
|
---|
[2304] | 2117 | bkpinfo->cdrw_speed = atoi(tmp1); // if DVD then this shouldn't ever be used anyway :)
|
---|
[2214] | 2118 |
|
---|
[3191] | 2119 | strcpy(tmp1, sz_size);
|
---|
| 2120 | mr_asprintf(comment, "How much data (in Megabytes) will each %s store?", mds);
|
---|
| 2121 | if (!popup_and_get_string("Size", comment, tmp1, 5)) {
|
---|
[3653] | 2122 | log_to_screen("User has chosen not to backup the machine");
|
---|
[128] | 2123 | finish(1);
|
---|
| 2124 | }
|
---|
[3191] | 2125 | mr_asprintf(sz_size, "%s", tmp1);
|
---|
[3150] | 2126 | bkpinfo->media_size = atoi(sz_size);
|
---|
[3191] | 2127 |
|
---|
[3150] | 2128 | if (bkpinfo->media_size <= 0) {
|
---|
[3653] | 2129 | log_to_screen("User has chosen not to backup the machine");
|
---|
[128] | 2130 | finish(1);
|
---|
| 2131 | }
|
---|
| 2132 | }
|
---|
[1737] | 2133 | /* No break because we continue even for usb */
|
---|
[128] | 2134 | case cdstream:
|
---|
[2242] | 2135 |
|
---|
[3875] | 2136 | // If media_device not found ask
|
---|
| 2137 | if (bkpinfo->media_device == NULL) {
|
---|
| 2138 | mr_asprintf(comment, "Please specify your Mondorescue %s media /dev entry", mds);
|
---|
| 2139 | tmp2 = mr_popup_and_get_string("/dev entry?", comment, bkpinfo->media_device);
|
---|
| 2140 | if (!tmp2) {
|
---|
| 2141 | log_to_screen("User has chosen not to backup the machine");
|
---|
| 2142 | finish(1);
|
---|
| 2143 | } else {
|
---|
| 2144 | mr_free(bkpinfo->media_device);
|
---|
| 2145 | bkpinfo->media_device = tmp2;
|
---|
[128] | 2146 | }
|
---|
[3875] | 2147 | }
|
---|
| 2148 | log_msg(2, "%s device found at %s", mds, bkpinfo->media_device);
|
---|
| 2149 | mr_asprintf(tmp, "MondoRescue thinks your %s media corresponds to %s. Is this correct?", mds, bkpinfo->media_device);
|
---|
| 2150 | if (!ask_me_yes_or_no(tmp)) {
|
---|
| 2151 | mr_free(bkpinfo->media_device);
|
---|
| 2152 | }
|
---|
| 2153 | mr_free(tmp);
|
---|
| 2154 |
|
---|
| 2155 | if (bkpinfo->media_device == NULL) {
|
---|
| 2156 | mr_asprintf(tmp, "Please then specify your Mondorescue %s media /dev entry", mds);
|
---|
| 2157 | tmp2 = mr_popup_and_get_string("/dev entry?", tmp, bkpinfo->media_device);
|
---|
| 2158 | mr_free(tmp);
|
---|
| 2159 | if (tmp2 == NULL) {
|
---|
| 2160 | log_to_screen("User has chosen not to backup the machine");
|
---|
| 2161 | finish(1);
|
---|
| 2162 | } else {
|
---|
[3822] | 2163 | mr_free(bkpinfo->media_device);
|
---|
[3875] | 2164 | bkpinfo->media_device = tmp2;
|
---|
[128] | 2165 | }
|
---|
| 2166 | }
|
---|
[2242] | 2167 | mr_free(mds);
|
---|
| 2168 |
|
---|
[128] | 2169 | if (bkpinfo->backup_media_type == cdstream) {
|
---|
[3150] | 2170 | bkpinfo->media_size = 650;
|
---|
[128] | 2171 | }
|
---|
| 2172 | break;
|
---|
| 2173 | case udev:
|
---|
| 2174 | if (!ask_me_yes_or_no
|
---|
[541] | 2175 | ("This option is for advanced users only. Are you sure?")) {
|
---|
[3653] | 2176 | log_to_screen("User has chosen not to backup the machine");
|
---|
[128] | 2177 | finish(1);
|
---|
| 2178 | }
|
---|
| 2179 | case tape:
|
---|
[1] | 2180 |
|
---|
[3870] | 2181 | bkpinfo->media_device = find_tape_device();
|
---|
[3822] | 2182 | if (bkpinfo->media_device != NULL) {
|
---|
[128] | 2183 | if ((fin = fopen(bkpinfo->media_device, "r"))) {
|
---|
| 2184 | paranoid_fclose(fin);
|
---|
| 2185 | } else {
|
---|
[3271] | 2186 | if (does_file_exist("/tmp/mondorestore.cfg")) {
|
---|
| 2187 | read_cfg_var("/tmp/mondorestore.cfg", "media-dev", bkpinfo->media_device);
|
---|
[128] | 2188 | }
|
---|
| 2189 | }
|
---|
| 2190 | }
|
---|
[3822] | 2191 | if (bkpinfo->media_device != NULL) {
|
---|
[3875] | 2192 | mr_asprintf(tmp, "Mondorescue thinks your tape streamer at %s; is that correct?", bkpinfo->media_device);
|
---|
[128] | 2193 | if (!ask_me_yes_or_no(tmp)) {
|
---|
[3822] | 2194 | mr_free(bkpinfo->media_device);
|
---|
[128] | 2195 | }
|
---|
[2214] | 2196 | mr_free(tmp);
|
---|
[128] | 2197 | }
|
---|
[3822] | 2198 | if (bkpinfo->media_device == NULL) {
|
---|
[3870] | 2199 | tmp2 = mr_popup_and_get_string("Device name?", "What is the /dev entry of your tape streamer?", "");
|
---|
[3822] | 2200 | if (tmp2 == NULL) {
|
---|
[3653] | 2201 | log_to_screen("User has chosen not to backup the machine");
|
---|
[128] | 2202 | finish(1);
|
---|
[3822] | 2203 | } else {
|
---|
| 2204 | bkpinfo->media_device = tmp2;
|
---|
[128] | 2205 | }
|
---|
| 2206 | }
|
---|
[3185] | 2207 | mr_asprintf(tmp, "ls -l %s", bkpinfo->media_device);
|
---|
[128] | 2208 | if (run_program_and_log_output(tmp, FALSE)) {
|
---|
[541] | 2209 | log_to_screen("User has not specified a valid /dev entry");
|
---|
[128] | 2210 | finish(1);
|
---|
| 2211 | }
|
---|
[2214] | 2212 | mr_free(tmp);
|
---|
| 2213 |
|
---|
[3576] | 2214 | mr_asprintf(sz_size,"%ld",bkpinfo->internal_tape_block_size);
|
---|
[3377] | 2215 | tmp = mr_popup_and_get_string("Tape block size?", "What is the block size of your tape streamer?", sz_size);
|
---|
| 2216 | if (tmp == NULL) {
|
---|
[3653] | 2217 | log_to_screen("User has chosen not to backup the machine");
|
---|
[3169] | 2218 | finish(1);
|
---|
[128] | 2219 | }
|
---|
[3377] | 2220 | bkpinfo->internal_tape_block_size = atol(tmp);
|
---|
| 2221 | mr_free(sz_size);
|
---|
| 2222 | mr_free(tmp);
|
---|
| 2223 |
|
---|
[3642] | 2224 | // log the block-size
|
---|
| 2225 | log_msg(0,"Tape block size= %ld", bkpinfo->internal_tape_block_size);
|
---|
| 2226 |
|
---|
[3169] | 2227 | if (bkpinfo->internal_tape_block_size <= 0) {
|
---|
[3653] | 2228 | log_to_screen("User has chosen not to backup the machine");
|
---|
[3169] | 2229 | finish(1);
|
---|
| 2230 | }
|
---|
| 2231 |
|
---|
| 2232 | bkpinfo->media_size = 0;
|
---|
[3150] | 2233 | log_msg(4, "media_size = %ld", bkpinfo->media_size);
|
---|
[3169] | 2234 |
|
---|
[128] | 2235 | if (archiving_to_media) {
|
---|
[3875] | 2236 | bkpinfo->use_obdr = ask_me_yes_or_no("Do you want to activate OBDR support for your tapes ?");
|
---|
| 2237 | if (bkpinfo->use_obdr) {
|
---|
| 2238 | log_msg(4, "obdr mode = TRUE");
|
---|
| 2239 | } else {
|
---|
| 2240 | log_msg(4, "obdr mode = FALSE");
|
---|
[2771] | 2241 | }
|
---|
[128] | 2242 | }
|
---|
| 2243 | break;
|
---|
[1] | 2244 |
|
---|
[2380] | 2245 | case netfs:
|
---|
| 2246 | /* Never try to eject a NETFS device */
|
---|
[1848] | 2247 | bkpinfo->please_dont_eject = TRUE;
|
---|
[3191] | 2248 | /* Force NFS to be the protocol by default */
|
---|
| 2249 | if (bkpinfo->netfs_proto == NULL) {
|
---|
| 2250 | mr_asprintf(bkpinfo->netfs_proto, "nfs");
|
---|
| 2251 | }
|
---|
[1848] | 2252 |
|
---|
[2380] | 2253 | /* Initiate bkpinfo netfs_mount path from running environment if not already done */
|
---|
[3194] | 2254 | if (bkpinfo->netfs_mount == NULL) {
|
---|
[3610] | 2255 | bkpinfo->netfs_mount = call_program_and_get_last_line_of_output("mount | grep \":\" | cut -d' ' -f1 | head -n1");
|
---|
[128] | 2256 | }
|
---|
[1] | 2257 | #ifdef __FreeBSD__
|
---|
[128] | 2258 | if (TRUE)
|
---|
[1] | 2259 | #else
|
---|
[128] | 2260 | if (!bkpinfo->disaster_recovery)
|
---|
[1] | 2261 | #endif
|
---|
[128] | 2262 | {
|
---|
[3777] | 2263 | 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] | 2264 | log_to_screen("User has chosen not to backup the machine");
|
---|
[128] | 2265 | finish(1);
|
---|
| 2266 | }
|
---|
[3191] | 2267 | mr_free(bkpinfo->netfs_mount);
|
---|
[3614] | 2268 | // check whether already mounted - we better remove
|
---|
| 2269 | // surrounding spaces and trailing '/' for this
|
---|
| 2270 | bkpinfo->netfs_mount = mr_strip_spaces(p);
|
---|
[2380] | 2271 | if (bkpinfo->netfs_mount[strlen(bkpinfo->netfs_mount) - 1] == '/')
|
---|
| 2272 | bkpinfo->netfs_mount[strlen(bkpinfo->netfs_mount) - 1] = '\0';
|
---|
[2847] | 2273 | q = strchr(bkpinfo->netfs_mount, '@');
|
---|
| 2274 | if (q != NULL) {
|
---|
| 2275 | /* User found. Store the 2 values */
|
---|
| 2276 | q++;
|
---|
| 2277 | /* new netfs mount */
|
---|
[3013] | 2278 | strcpy(tmp1,q);
|
---|
[2847] | 2279 | } else {
|
---|
[3013] | 2280 | strcpy(tmp1,bkpinfo->netfs_mount);
|
---|
[2847] | 2281 | }
|
---|
[3191] | 2282 | mr_asprintf(command, "mount | grep \"%s \" | cut -d' ' -f3", tmp1);
|
---|
[3836] | 2283 | mr_free(bkpinfo->isodir);
|
---|
| 2284 | bkpinfo->isodir = call_program_and_get_last_line_of_output(command);
|
---|
[3191] | 2285 | mr_free(command);
|
---|
[256] | 2286 |
|
---|
[1847] | 2287 | if (!bkpinfo->restore_data) {
|
---|
[3194] | 2288 | mr_asprintf(sz_size, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4482 MB
|
---|
[3191] | 2289 | mr_asprintf(comment, "How much data (in Megabytes) will each media store?");
|
---|
| 2290 | strcpy(tmp1, sz_size);
|
---|
| 2291 | mr_free(sz_size);
|
---|
| 2292 | if (!popup_and_get_string("Size", comment, tmp1, 5)) {
|
---|
[3653] | 2293 | log_to_screen("User has chosen not to backup the machine");
|
---|
[1847] | 2294 | finish(1);
|
---|
| 2295 | }
|
---|
[3191] | 2296 | mr_free(comment);
|
---|
| 2297 | mr_asprintf(sz_size, "%s", tmp1);
|
---|
[1847] | 2298 | } else {
|
---|
[3191] | 2299 | mr_asprintf(sz_size, "0");
|
---|
[256] | 2300 | }
|
---|
[3150] | 2301 | bkpinfo->media_size = atoi(sz_size);
|
---|
[3191] | 2302 | mr_free(sz_size);
|
---|
| 2303 |
|
---|
[3150] | 2304 | if (bkpinfo->media_size < 0) {
|
---|
[3653] | 2305 | log_to_screen("User has chosen not to backup the machine");
|
---|
[256] | 2306 | finish(1);
|
---|
| 2307 | }
|
---|
[128] | 2308 | }
|
---|
[3746] | 2309 | /* TODO: Useless I think */
|
---|
[128] | 2310 | if (bkpinfo->disaster_recovery) {
|
---|
[3191] | 2311 | mr_asprintf(command ,"umount %s/isodir 2> /dev/null", bkpinfo->tmpdir);
|
---|
[3060] | 2312 | paranoid_system(command);
|
---|
[3191] | 2313 | mr_free(command);
|
---|
| 2314 |
|
---|
[128] | 2315 | }
|
---|
[2628] | 2316 | strcpy(tmp1, bkpinfo->netfs_proto);
|
---|
[3777] | 2317 | if (!popup_and_get_string("Network protocol", "Which protocol should I use (nfs/sshfs/smbfs) ?",tmp1, MAX_STR_LEN-1)) {
|
---|
[3653] | 2318 | log_to_screen("User has chosen not to backup the machine");
|
---|
[2628] | 2319 | finish(1);
|
---|
| 2320 | }
|
---|
| 2321 | mr_free(bkpinfo->netfs_proto);
|
---|
[3185] | 2322 | mr_asprintf(bkpinfo->netfs_proto, "%s", tmp1);
|
---|
[3191] | 2323 |
|
---|
| 2324 | strcpy(tmp1, bkpinfo->netfs_mount);
|
---|
[3777] | 2325 | if (!popup_and_get_string("Network share", "Which remote share should I mount?", tmp1, MAX_STR_LEN-1)) {
|
---|
[3653] | 2326 | log_to_screen("User has chosen not to backup the machine");
|
---|
[2628] | 2327 | finish(1);
|
---|
| 2328 | }
|
---|
[3191] | 2329 | mr_free(bkpinfo->netfs_mount);
|
---|
| 2330 | mr_asprintf(bkpinfo->netfs_mount, "%s", tmp1);
|
---|
[2628] | 2331 |
|
---|
[2847] | 2332 | if (bkpinfo->netfs_user) {
|
---|
| 2333 | strcpy(tmp1, bkpinfo->netfs_user);
|
---|
| 2334 | } else {
|
---|
| 2335 | strcpy(tmp1, "");
|
---|
| 2336 | }
|
---|
[3777] | 2337 | if (!popup_and_get_string("Network user", "Which user should I use if any ?",tmp1, MAX_STR_LEN-1)) {
|
---|
[3653] | 2338 | log_to_screen("User has chosen not to backup the machine");
|
---|
[2847] | 2339 | finish(1);
|
---|
| 2340 | }
|
---|
| 2341 | mr_free(bkpinfo->netfs_user);
|
---|
| 2342 | if (strcmp(tmp1, "") != 0) {
|
---|
[3185] | 2343 | mr_asprintf(bkpinfo->netfs_user, "%s", tmp1);
|
---|
[2847] | 2344 | }
|
---|
[3751] | 2345 |
|
---|
[1858] | 2346 | /* Initiate bkpinfo isodir path from running environment if mount already done */
|
---|
[3751] | 2347 | log_msg(3, "Testing mount for %s", bkpinfo->netfs_mount);
|
---|
[2380] | 2348 | if (is_this_device_mounted(bkpinfo->netfs_mount)) {
|
---|
[3836] | 2349 | mr_free(bkpinfo->isodir);
|
---|
| 2350 | bkpinfo->isodir = call_program_and_get_last_line_of_output("mount | grep \":\" | cut -d' ' -f3 | head -n1");
|
---|
[1858] | 2351 | } else {
|
---|
[3278] | 2352 | // Why netfsdir ?
|
---|
[3836] | 2353 | mr_asprintf(bkpinfo->isodir, "%s/netfsdir", bkpinfo->tmpdir);
|
---|
[3191] | 2354 | mr_asprintf(command, "mkdir -p %s", bkpinfo->isodir);
|
---|
[128] | 2355 | run_program_and_log_output(command, 5);
|
---|
[3191] | 2356 | mr_free(command);
|
---|
[3111] | 2357 |
|
---|
[2361] | 2358 | if (bkpinfo->restore_data) {
|
---|
[3751] | 2359 | /* mount the FS read-only in restore mode to avoid any erase of whatever */
|
---|
[3185] | 2360 | mr_asprintf(tmpro, "-o ro");
|
---|
[3111] | 2361 | } else {
|
---|
[3185] | 2362 | mr_asprintf(tmpro, "");
|
---|
[3111] | 2363 | }
|
---|
| 2364 |
|
---|
| 2365 | /* Build the mount string */
|
---|
| 2366 | if (strstr(bkpinfo->netfs_proto, "smbfs")) {
|
---|
[3185] | 2367 | mr_asprintf(tmp, "mount -t cifs %s %s %s",bkpinfo->netfs_mount, bkpinfo->isodir,tmpro);
|
---|
[3111] | 2368 | if (bkpinfo->netfs_user) {
|
---|
| 2369 | mr_strcat(tmp, " -o user=%s", bkpinfo->netfs_user);
|
---|
[3107] | 2370 | }
|
---|
[3111] | 2371 | else {
|
---|
[2380] | 2372 | if (strstr(bkpinfo->netfs_proto, "sshfs")) {
|
---|
[3185] | 2373 | mr_asprintf(tmp, "sshfs %s ",tmpro);
|
---|
[2380] | 2374 | } else {
|
---|
[3185] | 2375 | mr_asprintf(tmp, "mount -t %s -o nolock %s ", bkpinfo->netfs_proto,tmpro);
|
---|
[2380] | 2376 | }
|
---|
[3111] | 2377 | if (bkpinfo->netfs_user) {
|
---|
| 2378 | mr_strcat(tmp, "%s@", bkpinfo->netfs_user);
|
---|
| 2379 | }
|
---|
| 2380 | mr_strcat(tmp, "%s %s", bkpinfo->netfs_mount, bkpinfo->isodir);
|
---|
[2361] | 2381 | }
|
---|
[1819] | 2382 | run_program_and_log_output(tmp, 3);
|
---|
[2214] | 2383 | mr_free(tmp);
|
---|
| 2384 |
|
---|
[128] | 2385 | malloc_string(g_selfmounted_isodir);
|
---|
| 2386 | strcpy(g_selfmounted_isodir, bkpinfo->isodir);
|
---|
[3111] | 2387 | }
|
---|
[128] | 2388 | }
|
---|
[3751] | 2389 |
|
---|
| 2390 | log_msg(3, "Testing mount for %s", bkpinfo->netfs_mount);
|
---|
[2380] | 2391 | if (!is_this_device_mounted(bkpinfo->netfs_mount)) {
|
---|
[3191] | 2392 | popup_and_OK("Please mount that partition before you try to backup to or restore from it.");
|
---|
[128] | 2393 | finish(1);
|
---|
| 2394 | }
|
---|
[3196] | 2395 | if (bkpinfo->netfs_remote_dir == NULL) {
|
---|
[3205] | 2396 | fatal_error("bkpinfo->netfs_remote_dir should not be NULL");
|
---|
[3196] | 2397 | }
|
---|
[2380] | 2398 | strcpy(tmp1, bkpinfo->netfs_remote_dir);
|
---|
[3777] | 2399 | if (!popup_and_get_string ("Directory", "Which directory within that mountpoint?", tmp1, MAX_STR_LEN-1)) {
|
---|
[3653] | 2400 | log_to_screen("User has chosen not to backup the machine");
|
---|
[128] | 2401 | finish(1);
|
---|
| 2402 | }
|
---|
[3196] | 2403 | mr_free(bkpinfo->netfs_remote_dir);
|
---|
[128] | 2404 | // check whether writable - we better remove surrounding spaces for this
|
---|
[3614] | 2405 | bkpinfo->netfs_remote_dir = mr_strip_spaces(tmp1);
|
---|
[1767] | 2406 |
|
---|
[3838] | 2407 | 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] | 2408 | if (tmp == NULL) {
|
---|
[3653] | 2409 | log_to_screen("User has chosen not to backup the machine");
|
---|
[251] | 2410 | finish(1);
|
---|
| 2411 | }
|
---|
[3835] | 2412 | mr_free(bkpinfo->prefix);
|
---|
| 2413 | bkpinfo->prefix = tmp;
|
---|
[251] | 2414 | log_msg(3, "prefix set to %s", bkpinfo->prefix);
|
---|
[3191] | 2415 | log_msg(3, "Just set netfs_remote_dir to %s", bkpinfo->netfs_remote_dir);
|
---|
[128] | 2416 | log_msg(3, "isodir is still %s", bkpinfo->isodir);
|
---|
| 2417 | break;
|
---|
[1] | 2418 |
|
---|
[128] | 2419 | case iso:
|
---|
| 2420 | if (!bkpinfo->disaster_recovery) {
|
---|
[3836] | 2421 | 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);
|
---|
| 2422 | if (tmp == NULL) {
|
---|
[3653] | 2423 | log_to_screen("User has chosen not to backup the machine");
|
---|
[128] | 2424 | finish(1);
|
---|
[3836] | 2425 | } else {
|
---|
| 2426 | mr_free(bkpinfo->isodir);
|
---|
| 2427 | bkpinfo->isodir = tmp;
|
---|
[128] | 2428 | }
|
---|
| 2429 | if (archiving_to_media) {
|
---|
[3191] | 2430 | sprintf(tmp1, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4482 MB
|
---|
[3212] | 2431 | 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] | 2432 | log_to_screen("User has chosen not to backup the machine");
|
---|
[128] | 2433 | finish(1);
|
---|
| 2434 | }
|
---|
[3191] | 2435 | bkpinfo->media_size = atoi(tmp1);
|
---|
[128] | 2436 | } else {
|
---|
[3150] | 2437 | bkpinfo->media_size = 650;
|
---|
[128] | 2438 | }
|
---|
| 2439 | }
|
---|
[3838] | 2440 | 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] | 2441 | if (tmp == NULL) {
|
---|
[3653] | 2442 | log_to_screen("User has chosen not to backup the machine");
|
---|
[804] | 2443 | finish(1);
|
---|
| 2444 | }
|
---|
[3835] | 2445 | mr_free(bkpinfo->prefix);
|
---|
| 2446 | bkpinfo->prefix = tmp;
|
---|
[804] | 2447 | log_msg(3, "prefix set to %s", bkpinfo->prefix);
|
---|
[128] | 2448 | break;
|
---|
| 2449 | default:
|
---|
[3196] | 2450 | fatal_error("I, Mojo Jojo, shall defeat those pesky Powerpuff Girls!");
|
---|
[128] | 2451 | }
|
---|
[1] | 2452 |
|
---|
[128] | 2453 | if (archiving_to_media) {
|
---|
[3594] | 2454 | /* Needs to be done before calling which_boot_loader */
|
---|
| 2455 | bkpinfo->boot_type = mr_boot_type();
|
---|
[3373] | 2456 | mr_free(bkpinfo->boot_device);
|
---|
[1] | 2457 | #ifdef __FreeBSD__
|
---|
[3610] | 2458 | bkpinfo->boot_device = call_program_and_get_last_line_of_output("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/\\([0-9]\\).*/\\1/'");
|
---|
[1] | 2459 | #else
|
---|
[3610] | 2460 | bkpinfo->boot_device = call_program_and_get_last_line_of_output("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'");
|
---|
[1] | 2461 | #endif
|
---|
[128] | 2462 | i = which_boot_loader(bkpinfo->boot_device);
|
---|
| 2463 | if (i == 'U') // unknown
|
---|
| 2464 | {
|
---|
[1] | 2465 |
|
---|
| 2466 | #ifdef __FreeBSD__
|
---|
[3777] | 2467 | 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] | 2468 | log_to_screen("User has chosen not to backup the machine");
|
---|
[128] | 2469 | finish(1);
|
---|
| 2470 | }
|
---|
| 2471 | i = which_boot_loader(bkpinfo->boot_device);
|
---|
[1] | 2472 | #else
|
---|
[3373] | 2473 | strcpy(tmp1, bkpinfo->boot_device);
|
---|
[3777] | 2474 | if (!popup_and_get_string("Boot device", "What is your boot device? (e.g. /dev/hda)", tmp1,(MAX_STR_LEN / 4)-1)) {
|
---|
[3653] | 2475 | log_to_screen("User has chosen not to backup the machine");
|
---|
[128] | 2476 | finish(1);
|
---|
| 2477 | }
|
---|
[3373] | 2478 | mr_free(bkpinfo->boot_device);
|
---|
| 2479 | mr_asprintf(bkpinfo->boot_device, "%s", tmp1);
|
---|
| 2480 |
|
---|
| 2481 | if (does_string_exist_in_boot_block(bkpinfo->boot_device, "ELILO")) {
|
---|
| 2482 | i = 'E';
|
---|
| 2483 | } else
|
---|
| 2484 | if (does_string_exist_in_boot_block(bkpinfo->boot_device, "LILO")) {
|
---|
[128] | 2485 | i = 'L';
|
---|
| 2486 | } else
|
---|
[3191] | 2487 | if (does_string_exist_in_boot_block(bkpinfo->boot_device, "GRUB")) {
|
---|
[128] | 2488 | i = 'G';
|
---|
| 2489 | } else {
|
---|
| 2490 | i = 'U';
|
---|
| 2491 | }
|
---|
[1] | 2492 | #endif
|
---|
[128] | 2493 | if (i == 'U') {
|
---|
[3191] | 2494 | 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] | 2495 | i = 'R'; // raw
|
---|
| 2496 | } else {
|
---|
[3191] | 2497 | log_to_screen("I cannot find your boot loader. Please run mondoarchive with parameters.");
|
---|
[128] | 2498 | finish(1);
|
---|
| 2499 | }
|
---|
| 2500 | }
|
---|
[1] | 2501 | }
|
---|
[128] | 2502 | bkpinfo->boot_loader = i;
|
---|
[3831] | 2503 | /* TODO: Check consistency of boot type and boot loader */
|
---|
[3191] | 2504 |
|
---|
[3205] | 2505 | if (bkpinfo->include_paths) {
|
---|
| 2506 | strcpy(tmp1, bkpinfo->include_paths);
|
---|
| 2507 | mr_free(bkpinfo->include_paths);
|
---|
| 2508 | } else {
|
---|
| 2509 | strcpy(tmp1, "/");
|
---|
| 2510 | }
|
---|
[3777] | 2511 | 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] | 2512 | log_to_screen("User has chosen not to backup the machine");
|
---|
[128] | 2513 | finish(1);
|
---|
[1] | 2514 | }
|
---|
[3191] | 2515 | mr_asprintf(bkpinfo->include_paths, "%s", tmp1);
|
---|
| 2516 |
|
---|
[2713] | 2517 | tmp = list_of_NETFS_mounts_only();
|
---|
[128] | 2518 | if (strlen(tmp) > 2) {
|
---|
[2713] | 2519 | mr_strcat(bkpinfo->exclude_paths, "|%s",tmp);
|
---|
[128] | 2520 | }
|
---|
[2214] | 2521 | mr_free(tmp);
|
---|
[1] | 2522 | // NTFS
|
---|
[3610] | 2523 | tmp = call_program_and_get_last_line_of_output("mr-parted2fdisk -l 2>/dev/null | grep -i ntfs | awk '{ print $1};' | tr -s '\\n' ' ' | awk '{ print $0};'");
|
---|
[3777] | 2524 | strncpy(tmp1, tmp,(MAX_STR_LEN / 4)-1);
|
---|
[3610] | 2525 | mr_free(tmp);
|
---|
[2304] | 2526 | if (strlen(tmp1) > 2) {
|
---|
[3777] | 2527 | if (!popup_and_get_string("NTFS partitions", "Please enter/confirm the NTFS partitions you wish to backup as well.", tmp1,(MAX_STR_LEN / 4)-1)) {
|
---|
[3653] | 2528 | log_to_screen("User has chosen not to backup the machine");
|
---|
[128] | 2529 | finish(1);
|
---|
| 2530 | }
|
---|
[3822] | 2531 | mr_asprintf(bkpinfo->image_devs, "%s", tmp1);
|
---|
[128] | 2532 | }
|
---|
[1] | 2533 |
|
---|
[2743] | 2534 | if (bkpinfo->exclude_paths != NULL ) {
|
---|
[3834] | 2535 | mr_asprintf(p, "%s", bkpinfo->exclude_paths);
|
---|
[2743] | 2536 | } else {
|
---|
[3834] | 2537 | mr_asprintf(p, "%s", "");
|
---|
[2743] | 2538 | }
|
---|
[3834] | 2539 | 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);
|
---|
| 2540 | mr_free(p);
|
---|
| 2541 | if (tmp == NULL) {
|
---|
[3653] | 2542 | log_to_screen("User has chosen not to backup the machine");
|
---|
[128] | 2543 | finish(1);
|
---|
| 2544 | }
|
---|
[2697] | 2545 | mr_free(bkpinfo->exclude_paths);
|
---|
| 2546 | bkpinfo->exclude_paths = tmp;
|
---|
| 2547 |
|
---|
[3212] | 2548 | mr_asprintf(oldtmp, "%s", bkpinfo->tmpdir);
|
---|
| 2549 | if (bkpinfo->tmpdir != NULL ) {
|
---|
[3834] | 2550 | mr_asprintf(p, "%s", bkpinfo->tmpdir);
|
---|
[3212] | 2551 | } else {
|
---|
[3834] | 2552 | mr_asprintf(p, "%s", "");
|
---|
[3212] | 2553 | }
|
---|
[3834] | 2554 | tmp = mr_popup_and_get_string("Temporary directory", "Please enter your temporary directory.", p);
|
---|
| 2555 | mr_free(p);
|
---|
| 2556 | if (tmp == NULL) {
|
---|
| 2557 | mr_free(oldtmp);
|
---|
[3653] | 2558 | log_to_screen("User has chosen not to backup the machine");
|
---|
[2220] | 2559 | finish(1);
|
---|
| 2560 | }
|
---|
[3154] | 2561 | /* if modified to another path */
|
---|
[3834] | 2562 | if (strcmp(tmp,oldtmp) != 0) {
|
---|
| 2563 | setup_tmpdir(tmp);
|
---|
[3154] | 2564 | }
|
---|
[3212] | 2565 | mr_free(oldtmp);
|
---|
| 2566 |
|
---|
| 2567 | mr_asprintf(oldtmp, "%s", bkpinfo->scratchdir);
|
---|
| 2568 | if (bkpinfo->scratchdir != NULL ) {
|
---|
[3834] | 2569 | mr_asprintf(p, "%s", bkpinfo->scratchdir);
|
---|
[3212] | 2570 | } else {
|
---|
[3834] | 2571 | mr_asprintf(p, "%s", "");
|
---|
[3212] | 2572 | }
|
---|
[3834] | 2573 | tmp = mr_popup_and_get_string("Scratch directory", "Please enter your scratch directory.", p);
|
---|
| 2574 | mr_free(p);
|
---|
| 2575 | if (tmp == NULL) {
|
---|
| 2576 | mr_free(oldtmp);
|
---|
[3653] | 2577 | log_to_screen("User has chosen not to backup the machine");
|
---|
[2220] | 2578 | finish(1);
|
---|
| 2579 | }
|
---|
[3154] | 2580 | /* if modified to another path */
|
---|
[3834] | 2581 | if (strcmp(tmp,oldtmp) != 0) {
|
---|
| 2582 | setup_scratchdir(tmp);
|
---|
[3154] | 2583 | }
|
---|
[3212] | 2584 | mr_free(oldtmp);
|
---|
| 2585 |
|
---|
[2772] | 2586 | if (ask_me_yes_or_no("Do you want to backup extended attributes?")) {
|
---|
[3866] | 2587 | mr_free(g_getfattr);
|
---|
| 2588 | g_getfattr = find_home_of_exe("getfattr");
|
---|
| 2589 | mr_free(g_getfacl);
|
---|
| 2590 | g_getfacl = find_home_of_exe("getfacl");
|
---|
[3171] | 2591 | log_it("Backup of extended attributes");
|
---|
[2772] | 2592 | }
|
---|
[1575] | 2593 | // Interactive mode:
|
---|
| 2594 | #ifdef __IA64__
|
---|
| 2595 | bkpinfo->make_cd_use_lilo = TRUE;
|
---|
| 2596 | #else
|
---|
[128] | 2597 | bkpinfo->make_cd_use_lilo = FALSE;
|
---|
[1575] | 2598 | #endif
|
---|
[128] | 2599 | bkpinfo->backup_data = TRUE;
|
---|
[2771] | 2600 | if (strcmp(compression_type,"lzo") == 0) {
|
---|
[3831] | 2601 | mr_asprintf(bkpinfo->zip_exe, "%s", "lzop");
|
---|
| 2602 | mr_asprintf(bkpinfo->zip_suffix, "%s", "lzo");
|
---|
[2771] | 2603 | } else if (strcmp(compression_type,"gzip") == 0) {
|
---|
[3831] | 2604 | mr_asprintf(bkpinfo->zip_exe, "%s", "gzip");
|
---|
| 2605 | mr_asprintf(bkpinfo->zip_suffix, "%s", "gz");
|
---|
[3191] | 2606 | } else if (strcmp(compression_type,"lzma") == 0) {
|
---|
[3831] | 2607 | mr_asprintf(bkpinfo->zip_exe, "%s", "xz");
|
---|
| 2608 | mr_asprintf(bkpinfo->zip_suffix, "%s", "xz");
|
---|
[2771] | 2609 | } else if (strcmp(compression_type,"bzip2") == 0) {
|
---|
[3831] | 2610 | mr_asprintf(bkpinfo->zip_exe, "%s", "bzip2");
|
---|
| 2611 | mr_asprintf(bkpinfo->zip_suffix, "%s", "bz2");
|
---|
[2771] | 2612 | } else {
|
---|
[3831] | 2613 | mr_free(bkpinfo->zip_exe);
|
---|
| 2614 | mr_free(bkpinfo->zip_suffix);
|
---|
[2771] | 2615 | }
|
---|
[3875] | 2616 | mr_free(compression_type);
|
---|
| 2617 |
|
---|
[3350] | 2618 | #if __FreeBSD__ == 5
|
---|
[3831] | 2619 | mr_asprintf(bkpinfo->kernel_path, "%s", "/boot/kernel/kernel");
|
---|
[3350] | 2620 | #elif __FreeBSD__ == 4
|
---|
[3831] | 2621 | mr_asprintf(bkpinfo->kernel_path, "%s", "/kernel");
|
---|
[3350] | 2622 | #elif linux
|
---|
| 2623 | if (figure_out_kernel_path_interactively_if_necessary(bkpinfo->kernel_path)) {
|
---|
| 2624 | fatal_error("Kernel not found. Please specify manually with the '-k' switch.");
|
---|
| 2625 | }
|
---|
| 2626 | #else
|
---|
| 2627 | #error "I don't know about this system!"
|
---|
| 2628 | #endif
|
---|
[2774] | 2629 |
|
---|
[3350] | 2630 |
|
---|
[128] | 2631 | bkpinfo->verify_data =
|
---|
| 2632 | ask_me_yes_or_no
|
---|
[541] | 2633 | ("Will you want to verify your backups after Mondo has created them?");
|
---|
[1] | 2634 |
|
---|
[128] | 2635 | if (!ask_me_yes_or_no
|
---|
[541] | 2636 | ("Are you sure you want to proceed? Hit 'no' to abort.")) {
|
---|
[3653] | 2637 | log_to_screen("User has chosen not to backup the machine");
|
---|
[128] | 2638 | finish(1);
|
---|
| 2639 | }
|
---|
| 2640 | } else {
|
---|
| 2641 | bkpinfo->restore_data = TRUE; // probably...
|
---|
| 2642 | }
|
---|
[1] | 2643 |
|
---|
[128] | 2644 | if (bkpinfo->backup_media_type == iso
|
---|
[2380] | 2645 | || bkpinfo->backup_media_type == netfs) {
|
---|
[128] | 2646 | g_ISO_restore_mode = TRUE;
|
---|
| 2647 | }
|
---|
[1] | 2648 | #ifdef __FreeSD__
|
---|
| 2649 | // skip
|
---|
| 2650 | #else
|
---|
[2380] | 2651 | if (bkpinfo->backup_media_type == netfs) {
|
---|
[3191] | 2652 | log_msg(3, "I think the Remote mount is mounted at %s", bkpinfo->isodir);
|
---|
[128] | 2653 | }
|
---|
| 2654 | log_it("isodir = %s", bkpinfo->isodir);
|
---|
[3191] | 2655 | if (bkpinfo->netfs_mount) {
|
---|
| 2656 | log_it("netfs_mount = '%s'", bkpinfo->netfs_mount);
|
---|
[2682] | 2657 | }
|
---|
[2380] | 2658 | if (bkpinfo->netfs_user) {
|
---|
| 2659 | log_it("netfs_user = '%s'", bkpinfo->netfs_user);
|
---|
[2224] | 2660 | }
|
---|
[3191] | 2661 | if (bkpinfo->netfs_proto) {
|
---|
| 2662 | log_it("netfs_proto = '%s'", bkpinfo->netfs_proto);
|
---|
| 2663 | }
|
---|
[1] | 2664 | #endif
|
---|
| 2665 |
|
---|
[128] | 2666 | log_it("media device = %s", bkpinfo->media_device);
|
---|
[3150] | 2667 | log_it("media size = %ld", bkpinfo->media_size);
|
---|
[3879] | 2668 | tmp = bkptype_to_string(bkpinfo->backup_media_type);
|
---|
| 2669 | log_it("media type = %s", tmp);
|
---|
| 2670 | mr_free(tmp);
|
---|
[3835] | 2671 | if (bkpinfo->prefix != NULL) {
|
---|
[3191] | 2672 | log_it("prefix = %s", bkpinfo->prefix);
|
---|
| 2673 | }
|
---|
[128] | 2674 | log_it("compression = %ld", bkpinfo->compression_level);
|
---|
[2473] | 2675 | log_it("exclude_path = %s", bkpinfo->exclude_paths);
|
---|
[3205] | 2676 | if (bkpinfo->include_paths) {
|
---|
| 2677 | log_it("include_path = %s", bkpinfo->include_paths);
|
---|
| 2678 | }
|
---|
[2424] | 2679 |
|
---|
| 2680 | /* Handle devices passed in bkpinfo and print result */
|
---|
[3194] | 2681 | /* the mr_make_devlist_from_pathlist function appends */
|
---|
[3191] | 2682 | /* to the *_paths variables so copy before */
|
---|
[2713] | 2683 | mr_make_devlist_from_pathlist(bkpinfo->exclude_paths, 'E');
|
---|
| 2684 | mr_make_devlist_from_pathlist(bkpinfo->include_paths, 'I');
|
---|
[2424] | 2685 |
|
---|
[128] | 2686 | log_it("scratchdir = '%s'", bkpinfo->scratchdir);
|
---|
| 2687 | log_it("tmpdir = '%s'", bkpinfo->tmpdir);
|
---|
[3191] | 2688 | if (bkpinfo->image_devs) {
|
---|
| 2689 | log_it("image_devs = '%s'", bkpinfo->image_devs);
|
---|
| 2690 | }
|
---|
| 2691 | log_it("boot_device = '%s' (loader=%c)", bkpinfo->boot_device, bkpinfo->boot_loader);
|
---|
[3150] | 2692 | if (bkpinfo->media_size < 0) {
|
---|
[128] | 2693 | if (archiving_to_media) {
|
---|
| 2694 | fatal_error("Media size is less than zero.");
|
---|
| 2695 | } else {
|
---|
| 2696 | log_msg(2, "Warning - media size is less than zero.");
|
---|
[3150] | 2697 | bkpinfo->media_size = 0;
|
---|
[128] | 2698 | }
|
---|
[1] | 2699 | }
|
---|
[128] | 2700 | paranoid_free(sz_size);
|
---|
[2305] | 2701 | paranoid_free(tmp1);
|
---|
[128] | 2702 | return (0);
|
---|
[1] | 2703 | }
|
---|
| 2704 |
|
---|
| 2705 |
|
---|
| 2706 |
|
---|
| 2707 |
|
---|
| 2708 |
|
---|
| 2709 | /**
|
---|
| 2710 | * Create a randomly-named FIFO. The format is @p stub "." [random] [random] where
|
---|
| 2711 | * [random] is a random number between 1 and 32767.
|
---|
| 2712 | * @param store_name_here Where to store the new filename.
|
---|
| 2713 | * @param stub A random number will be appended to this to make the FIFO's name.
|
---|
| 2714 | * @ingroup deviceGroup
|
---|
| 2715 | */
|
---|
[128] | 2716 | void make_fifo(char *store_name_here, char *stub)
|
---|
[1] | 2717 | {
|
---|
[3352] | 2718 | char *tmp = NULL;
|
---|
[1] | 2719 |
|
---|
[128] | 2720 | assert_string_is_neither_NULL_nor_zerolength(stub);
|
---|
[1] | 2721 |
|
---|
[128] | 2722 | sprintf(store_name_here, "%s%d%d", stub, (int) (random() % 32768),
|
---|
| 2723 | (int) (random() % 32768));
|
---|
| 2724 | make_hole_for_file(store_name_here);
|
---|
| 2725 | mkfifo(store_name_here, S_IRWXU | S_IRWXG);
|
---|
[3352] | 2726 | mr_asprintf(tmp, "chmod 770 %s", store_name_here);
|
---|
[128] | 2727 | paranoid_system(tmp);
|
---|
[3352] | 2728 | mr_free(tmp);
|
---|
[1] | 2729 | }
|
---|
| 2730 |
|
---|
| 2731 |
|
---|
| 2732 |
|
---|
| 2733 | /**
|
---|
| 2734 | * @addtogroup deviceGroup
|
---|
| 2735 | * @{
|
---|
| 2736 | */
|
---|
| 2737 | /**
|
---|
| 2738 | * If we can read @p dev, set @p output to it.
|
---|
| 2739 | * If @p dev cannot be read, set @p output to "".
|
---|
| 2740 | * @param dev The device to check for.
|
---|
| 2741 | * @param output Set to @p dev if @p dev exists, "" otherwise.
|
---|
| 2742 | * @return TRUE if @p dev exists, FALSE if it doesn't.
|
---|
| 2743 | */
|
---|
[128] | 2744 | bool set_dev_to_this_if_rx_OK(char *output, char *dev)
|
---|
[1] | 2745 | {
|
---|
[3352] | 2746 | char *command = NULL;
|
---|
| 2747 | bool ret=FALSE;
|
---|
[1] | 2748 |
|
---|
[128] | 2749 | if (!dev || dev[0] == '\0') {
|
---|
| 2750 | output[0] = '\0';
|
---|
[3352] | 2751 | return(ret);
|
---|
[128] | 2752 | }
|
---|
[1] | 2753 | // assert_string_is_neither_NULL_nor_zerolength(dev);
|
---|
[2931] | 2754 | if (!bkpinfo->please_dont_eject) {
|
---|
| 2755 | log_msg(10, "Injecting %s", dev);
|
---|
| 2756 | inject_device(dev);
|
---|
| 2757 | }
|
---|
[128] | 2758 | if (!does_file_exist(dev)) {
|
---|
| 2759 | log_msg(10, "%s doesn't exist. Returning FALSE.", dev);
|
---|
[3352] | 2760 | return(ret);
|
---|
[128] | 2761 | }
|
---|
[3352] | 2762 | mr_asprintf(command, "dd bs=%ld count=1 if=%s of=/dev/null &> /dev/null", 512L, dev);
|
---|
| 2763 | if (!run_program_and_log_output(command, FALSE) && !run_program_and_log_output(command, FALSE)) {
|
---|
[128] | 2764 | strcpy(output, dev);
|
---|
| 2765 | log_msg(4, "Found it - %s", dev);
|
---|
[3352] | 2766 | ret = TRUE;
|
---|
[128] | 2767 | } else {
|
---|
| 2768 | output[0] = '\0';
|
---|
| 2769 | log_msg(4, "It's not %s", dev);
|
---|
| 2770 | }
|
---|
[3352] | 2771 | mr_free(command);
|
---|
| 2772 | return(ret);
|
---|
[1] | 2773 | }
|
---|
| 2774 |
|
---|
| 2775 |
|
---|
| 2776 |
|
---|
| 2777 | /**
|
---|
| 2778 | * Write zeroes over the first 16K of @p device.
|
---|
| 2779 | * @param device The device to zero.
|
---|
| 2780 | * @return 0 for success, 1 for failure.
|
---|
| 2781 | */
|
---|
[128] | 2782 | int zero_out_a_device(char *device)
|
---|
[1] | 2783 | {
|
---|
[128] | 2784 | FILE *fout;
|
---|
| 2785 | int i;
|
---|
[1] | 2786 |
|
---|
[128] | 2787 | assert_string_is_neither_NULL_nor_zerolength(device);
|
---|
[1] | 2788 |
|
---|
[128] | 2789 | log_it("Zeroing drive %s", device);
|
---|
| 2790 | if (!(fout = fopen(device, "w"))) {
|
---|
| 2791 | log_OS_error("Unable to open/write to device");
|
---|
| 2792 | return (1);
|
---|
| 2793 | }
|
---|
| 2794 | for (i = 0; i < 16384; i++) {
|
---|
| 2795 | fputc('\0', fout);
|
---|
| 2796 | }
|
---|
| 2797 | paranoid_fclose(fout);
|
---|
| 2798 | log_it("Device successfully zeroed.");
|
---|
| 2799 | return (0);
|
---|
[1] | 2800 | }
|
---|
| 2801 |
|
---|
| 2802 | /**
|
---|
| 2803 | * Return the device pointed to by @p incoming.
|
---|
| 2804 | * @param incoming The device to resolve symlinks for.
|
---|
| 2805 | * @return The path to the real device file.
|
---|
| 2806 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
| 2807 | * @bug Won't work with file v4.0; needs to be written in C.
|
---|
| 2808 | */
|
---|
[128] | 2809 | char *resolve_softlinks_to_get_to_actual_device_file(char *incoming)
|
---|
[1] | 2810 | {
|
---|
[128] | 2811 | static char output[MAX_STR_LEN];
|
---|
[3352] | 2812 | char *command = NULL;
|
---|
| 2813 | char *curr_fname = NULL;
|
---|
[2214] | 2814 | char *scratch = NULL;
|
---|
| 2815 | char *tmp = NULL;
|
---|
[128] | 2816 | char *p;
|
---|
| 2817 |
|
---|
| 2818 | struct stat statbuf;
|
---|
| 2819 | malloc_string(curr_fname);
|
---|
| 2820 | if (!does_file_exist(incoming)) {
|
---|
| 2821 | log_it
|
---|
| 2822 | ("resolve_softlinks_to_get_to_actual_device_file --- device not found");
|
---|
| 2823 | strcpy(output, incoming);
|
---|
| 2824 | } else {
|
---|
| 2825 | strcpy(curr_fname, incoming);
|
---|
| 2826 | lstat(curr_fname, &statbuf);
|
---|
| 2827 | while (S_ISLNK(statbuf.st_mode)) {
|
---|
| 2828 | log_msg(1, "curr_fname = %s", curr_fname);
|
---|
[3352] | 2829 | mr_asprintf(command, "file %s", curr_fname);
|
---|
[3610] | 2830 | tmp = call_program_and_get_last_line_of_output(command);
|
---|
[3352] | 2831 | mr_free(command);
|
---|
[128] | 2832 | for (p = tmp + strlen(tmp); p != tmp && *p != '`' && *p != ' ';
|
---|
| 2833 | p--);
|
---|
| 2834 | p++;
|
---|
[3185] | 2835 | mr_asprintf(scratch, "%s", p);
|
---|
[128] | 2836 | for (p = scratch; *p != '\0' && *p != '\''; p++);
|
---|
| 2837 | *p = '\0';
|
---|
[2214] | 2838 | log_msg(0, "curr_fname %s --> '%s' --> %s", curr_fname, tmp, scratch);
|
---|
| 2839 | mr_free(tmp);
|
---|
| 2840 |
|
---|
[128] | 2841 | if (scratch[0] == '/') {
|
---|
| 2842 | strcpy(curr_fname, scratch); // copy whole thing because it's an absolute softlink
|
---|
| 2843 | } else { // copy over the basename cos it's a relative softlink
|
---|
| 2844 | p = curr_fname + strlen(curr_fname);
|
---|
| 2845 | while (p != curr_fname && *p != '/') {
|
---|
| 2846 | p--;
|
---|
| 2847 | }
|
---|
| 2848 | if (*p == '/') {
|
---|
| 2849 | p++;
|
---|
| 2850 | }
|
---|
| 2851 | strcpy(p, scratch);
|
---|
| 2852 | }
|
---|
[2214] | 2853 | mr_free(scratch);
|
---|
[128] | 2854 | lstat(curr_fname, &statbuf);
|
---|
| 2855 | }
|
---|
| 2856 | strcpy(output, curr_fname);
|
---|
| 2857 | log_it("resolved %s to %s", incoming, output);
|
---|
[1] | 2858 | }
|
---|
[128] | 2859 | paranoid_free(curr_fname);
|
---|
| 2860 | return (output);
|
---|
[1] | 2861 | }
|
---|
| 2862 |
|
---|
| 2863 | /* @} - end of deviceGroup */
|
---|
| 2864 |
|
---|
| 2865 | /**
|
---|
| 2866 | * Return the type of partition format (GPT or MBR)
|
---|
| 2867 | */
|
---|
| 2868 | char *which_partition_format(const char *drive)
|
---|
| 2869 | {
|
---|
[3237] | 2870 | char *output = NULL;
|
---|
[3352] | 2871 | char *command = NULL;
|
---|
[3610] | 2872 |
|
---|
[3553] | 2873 | mr_asprintf(command, "mr-disk-type %s", drive);
|
---|
[3610] | 2874 | output = call_program_and_get_last_line_of_output(command);
|
---|
[3352] | 2875 | mr_free(command);
|
---|
| 2876 |
|
---|
[3263] | 2877 | log_msg(0, "Found %s partition table format type on %s", output, drive);
|
---|
[128] | 2878 | return (output);
|
---|
[1] | 2879 | }
|
---|
| 2880 | /* @} - end of deviceGroup */
|
---|
[3374] | 2881 |
|
---|
| 2882 |
|
---|