1 | /* libmondo-devices.c Subroutines for handling devices |
---|
2 | $Id: libmondo-devices.c 1079 2007-01-28 16:58:18Z bruno $ |
---|
3 | */ |
---|
4 | |
---|
5 | /** |
---|
6 | * @file |
---|
7 | * Functions to handle interactions with backup devices. |
---|
8 | */ |
---|
9 | |
---|
10 | #include "my-stuff.h" |
---|
11 | #include "mondostructures.h" |
---|
12 | #include "libmondo-files-EXT.h" |
---|
13 | #include "libmondo-devices.h" |
---|
14 | #include "libmondo-string-EXT.h" |
---|
15 | #include "libmondo-tools-EXT.h" |
---|
16 | #include "newt-specific-EXT.h" |
---|
17 | #include "libmondo-fork-EXT.h" |
---|
18 | #include "libmondo-stream-EXT.h" |
---|
19 | #include "mr_mem.h" |
---|
20 | |
---|
21 | #include <sys/ioctl.h> |
---|
22 | #include <sys/types.h> |
---|
23 | #include <unistd.h> |
---|
24 | #ifdef __FreeBSD__ |
---|
25 | #define DKTYPENAMES |
---|
26 | #define FSTYPENAMES |
---|
27 | #include <sys/disklabel.h> |
---|
28 | #include <sys/disk.h> |
---|
29 | #elif linux |
---|
30 | #define u64 unsigned long long |
---|
31 | #include <linux/fs.h> /* for BLKGETSIZE64 */ |
---|
32 | #include <linux/hdreg.h> |
---|
33 | #endif |
---|
34 | |
---|
35 | /*@unused@*/ |
---|
36 | //static char cvsid[] = "$Id: libmondo-devices.c 1079 2007-01-28 16:58:18Z bruno $"; |
---|
37 | |
---|
38 | extern int g_current_media_number; |
---|
39 | extern double g_kernel_version; |
---|
40 | |
---|
41 | extern bool g_ISO_restore_mode; |
---|
42 | extern struct s_bkpinfo *g_bkpinfo_DONTUSETHIS; |
---|
43 | extern char *g_erase_tmpdir_and_scratchdir; |
---|
44 | extern char *g_selfmounted_isodir; |
---|
45 | |
---|
46 | static char *g_cdrw_drive_is_here = NULL; |
---|
47 | static char *g_cdrom_drive_is_here = NULL; |
---|
48 | static char *g_dvd_drive_is_here = NULL; |
---|
49 | |
---|
50 | |
---|
51 | /** |
---|
52 | * ????? @bug ????? |
---|
53 | * @ingroup globalGroup |
---|
54 | */ |
---|
55 | bool g_restoring_live_from_cd = FALSE; |
---|
56 | |
---|
57 | extern t_bkptype g_backup_media_type; // set by main() |
---|
58 | |
---|
59 | |
---|
60 | |
---|
61 | |
---|
62 | void set_g_cdrom_and_g_dvd_to_bkpinfo_value(struct s_bkpinfo *bkpinfo) |
---|
63 | { |
---|
64 | if (bkpinfo->media_device != NULL) { |
---|
65 | mr_allocstr(g_cdrom_drive_is_here, bkpinfo->media_device); // just in case |
---|
66 | mr_allocstr(g_dvd_drive_is_here, bkpinfo->media_device); // just in case |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | |
---|
72 | /** |
---|
73 | * Retract all CD trays and wait for autorun to complete. |
---|
74 | * @ingroup deviceGroup |
---|
75 | */ |
---|
76 | void retract_CD_tray_and_defeat_autorun(void) |
---|
77 | { |
---|
78 | // log_it("rctada: Retracting all CD trays", __LINE__); |
---|
79 | if (strlen(g_cdrom_drive_is_here) > 0) { |
---|
80 | inject_device(g_cdrom_drive_is_here); |
---|
81 | } |
---|
82 | if (strlen(g_dvd_drive_is_here) > 0) { |
---|
83 | inject_device(g_dvd_drive_is_here); |
---|
84 | } |
---|
85 | if (strlen(g_cdrw_drive_is_here) > 0) { |
---|
86 | inject_device(g_cdrw_drive_is_here); |
---|
87 | } |
---|
88 | // log_it("rctada: killing autorun"); |
---|
89 | // run_program_and_log_output("killall autorun", TRUE); |
---|
90 | if (!run_program_and_log_output("ps | grep autorun | grep -v grep", 5)) { |
---|
91 | log_it("autorun detected; sleeping for 2 seconds"); |
---|
92 | sleep(2); |
---|
93 | } |
---|
94 | log_it("rctada: Unmounting all CD drives", __LINE__); |
---|
95 | run_program_and_log_output("umount /dev/cdr* /dev/dvd*", 5); |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | /** |
---|
100 | * Mount the CD-ROM at @p mountpoint. |
---|
101 | * @param device The device (or file if g_ISO_restore_mode) to mount. |
---|
102 | * @param mountpoint The place to mount it. |
---|
103 | * @return TRUE for success, FALSE for failure. |
---|
104 | */ |
---|
105 | bool mount_CDROM_here(char *device, char *mountpoint) |
---|
106 | { |
---|
107 | /*@ buffer ****************************************************** */ |
---|
108 | char *command = NULL; |
---|
109 | int retval = 0; |
---|
110 | |
---|
111 | assert_string_is_neither_NULL_nor_zerolength(device); |
---|
112 | assert_string_is_neither_NULL_nor_zerolength(mountpoint); |
---|
113 | |
---|
114 | make_hole_for_dir(mountpoint); |
---|
115 | if (isdigit(device[0])) { |
---|
116 | mr_free(device); |
---|
117 | device = find_cdrom_device(FALSE); |
---|
118 | } |
---|
119 | if (g_ISO_restore_mode) { |
---|
120 | |
---|
121 | #ifdef __FreeBSD__ |
---|
122 | char *dev = NULL; |
---|
123 | |
---|
124 | dev = make_vn(device)); |
---|
125 | if (!dev) { |
---|
126 | mr_asprintf(&command, "Unable to mount ISO (make_vn(%s) failed)", |
---|
127 | device); |
---|
128 | fatal_error(command); |
---|
129 | } |
---|
130 | mr_free(device); |
---|
131 | device = dev; |
---|
132 | #endif |
---|
133 | } |
---|
134 | |
---|
135 | log_msg(4, "(mount_CDROM_here --- device=%s, mountpoint=%s", device, |
---|
136 | mountpoint); |
---|
137 | /*@ end vars *************************************************** */ |
---|
138 | |
---|
139 | #ifdef __FreeBSD__ |
---|
140 | mr_asprintf(&command, "mount_cd9660 -r %s %s 2>> %s", |
---|
141 | device, mountpoint, MONDO_LOGFILE); |
---|
142 | #else |
---|
143 | mr_asprintf(&command, "mount %s -o ro,loop -t iso9660 %s 2>> %s", |
---|
144 | device, mountpoint, MONDO_LOGFILE); |
---|
145 | #endif |
---|
146 | |
---|
147 | log_msg(4, command); |
---|
148 | if (strncmp(device, "/dev/", 5) == 0) { |
---|
149 | retract_CD_tray_and_defeat_autorun(); |
---|
150 | } |
---|
151 | retval = system(command); |
---|
152 | log_msg(1, "system(%s) returned %d", command, retval); |
---|
153 | mr_free(command); |
---|
154 | |
---|
155 | if (retval == 0) { |
---|
156 | return(TRUE); |
---|
157 | } else { |
---|
158 | return(FALSE); |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | |
---|
163 | |
---|
164 | /** |
---|
165 | * Determine whether we're booted off a ramdisk. |
---|
166 | * @return @c TRUE (we are) or @c FALSE (we aren't). |
---|
167 | * @ingroup utilityGroup |
---|
168 | */ |
---|
169 | bool am_I_in_disaster_recovery_mode(void) |
---|
170 | { |
---|
171 | char *tmp, *comment; |
---|
172 | bool is_this_a_ramdisk = FALSE; |
---|
173 | |
---|
174 | mr_asprintf(&tmp, where_is_root_mounted()); |
---|
175 | mr_asprintf(&comment, "root is mounted at %s\n", tmp); |
---|
176 | log_msg(0, comment); |
---|
177 | mr_free(comment); |
---|
178 | |
---|
179 | log_msg(0, |
---|
180 | "No, Schlomo, 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().", |
---|
181 | tmp); |
---|
182 | |
---|
183 | #ifdef __FreeBSD__ |
---|
184 | if (strstr(tmp, "/dev/md")) { |
---|
185 | is_this_a_ramdisk = TRUE; |
---|
186 | } |
---|
187 | #else |
---|
188 | if (!strncmp(tmp, "/dev/ram", 8) |
---|
189 | || (!strncmp(tmp, "/dev/rd", 7) && !strcmp(tmp, "/dev/rd/") |
---|
190 | && strncmp(tmp, "/dev/rd/cd", 10)) || strstr(tmp, "rootfs") |
---|
191 | || !strcmp(tmp, "/dev/root")) { |
---|
192 | is_this_a_ramdisk = TRUE; |
---|
193 | } else { |
---|
194 | is_this_a_ramdisk = FALSE; |
---|
195 | } |
---|
196 | #endif |
---|
197 | mr_free(tmp); |
---|
198 | |
---|
199 | if (is_this_a_ramdisk) { |
---|
200 | if (!does_file_exist("/THIS-IS-A-RAMDISK") |
---|
201 | && !does_file_exist("/tmp/mountlist.txt.sample")) { |
---|
202 | log_to_screen |
---|
203 | (_("Using /dev/root is stupid of you but I'll forgive you.")); |
---|
204 | is_this_a_ramdisk = FALSE; |
---|
205 | } |
---|
206 | } |
---|
207 | if (does_file_exist("/THIS-IS-A-RAMDISK")) { |
---|
208 | is_this_a_ramdisk = TRUE; |
---|
209 | } |
---|
210 | log_msg(1, "Is this a ramdisk? result = %d", is_this_a_ramdisk); |
---|
211 | return (is_this_a_ramdisk); |
---|
212 | } |
---|
213 | |
---|
214 | |
---|
215 | /** |
---|
216 | * Turn @c bkpinfo->backup_media_type into a human-readable string. |
---|
217 | * @return The human readable string (e.g. @c cdr becomes <tt>"cdr"</tt>). |
---|
218 | * @note The returned string points to static storage that will be overwritten with each call. |
---|
219 | * @ingroup stringGroup |
---|
220 | */ |
---|
221 | char *bkptype_to_string(t_bkptype bt) |
---|
222 | { |
---|
223 | char *output = NULL; |
---|
224 | |
---|
225 | mr_free(output); |
---|
226 | |
---|
227 | switch (bt) { |
---|
228 | case none: |
---|
229 | mr_asprintf(&output, _("none")); |
---|
230 | break; |
---|
231 | case iso: |
---|
232 | mr_asprintf(&output, _("iso")); |
---|
233 | break; |
---|
234 | case cdr: |
---|
235 | mr_asprintf(&output, _("cdr")); |
---|
236 | break; |
---|
237 | case cdrw: |
---|
238 | mr_asprintf(&output, _("cdrw")); |
---|
239 | break; |
---|
240 | case cdstream: |
---|
241 | mr_asprintf(&output, _("cdstream")); |
---|
242 | break; |
---|
243 | case nfs: |
---|
244 | mr_asprintf(&output, _("nfs")); |
---|
245 | break; |
---|
246 | case tape: |
---|
247 | mr_asprintf(&output, _("tape")); |
---|
248 | break; |
---|
249 | case udev: |
---|
250 | mr_asprintf(&output, _("udev")); |
---|
251 | break; |
---|
252 | default: |
---|
253 | mr_asprintf(&output, _("default")); |
---|
254 | } |
---|
255 | return (output); |
---|
256 | } |
---|
257 | |
---|
258 | |
---|
259 | /** |
---|
260 | * @addtogroup deviceGroup |
---|
261 | * @{ |
---|
262 | */ |
---|
263 | /** |
---|
264 | * Eject the tray of the specified CD device. |
---|
265 | * @param dev The device to eject. |
---|
266 | * @return the return value of the @c eject command. (0=success, nonzero=failure) |
---|
267 | */ |
---|
268 | int eject_device(char *dev) |
---|
269 | { |
---|
270 | char *command; |
---|
271 | int res1 = 0, res2 = 0; |
---|
272 | |
---|
273 | if (IS_THIS_A_STREAMING_BACKUP(g_backup_media_type) |
---|
274 | && g_backup_media_type != udev) { |
---|
275 | mr_asprintf(&command, "mt -f %s offline", dev); |
---|
276 | res1 = run_program_and_log_output(command, 1); |
---|
277 | mr_free(command); |
---|
278 | } else { |
---|
279 | res1 = 0; |
---|
280 | } |
---|
281 | |
---|
282 | #ifdef __FreeBSD__ |
---|
283 | if (strstr(dev, "acd")) { |
---|
284 | mr_asprintf(&command, "cdcontrol -f %s eject", dev); |
---|
285 | } else { |
---|
286 | mr_asprintf(&command, "camcontrol eject `echo %s | sed 's|/dev/||'`", |
---|
287 | dev); |
---|
288 | } |
---|
289 | #else |
---|
290 | mr_asprintf(&command, "eject %s", dev); |
---|
291 | #endif |
---|
292 | |
---|
293 | log_msg(3, "Ejecting %s", dev); |
---|
294 | res2 = run_program_and_log_output(command, 1); |
---|
295 | mr_free(command); |
---|
296 | if (res1 && res2) { |
---|
297 | return (1); |
---|
298 | } else { |
---|
299 | return (0); |
---|
300 | } |
---|
301 | } |
---|
302 | |
---|
303 | |
---|
304 | /** |
---|
305 | * Load (inject) the tray of the specified CD device. |
---|
306 | * @param dev The device to load/inject. |
---|
307 | * @return 0 for success, nonzero for failure. |
---|
308 | */ |
---|
309 | int inject_device(char *dev) |
---|
310 | { |
---|
311 | char *command; |
---|
312 | int i; |
---|
313 | |
---|
314 | #ifdef __FreeBSD__ |
---|
315 | if (strstr(dev, "acd")) { |
---|
316 | mr_asprintf(&command, "cdcontrol -f %s close", dev); |
---|
317 | } else { |
---|
318 | mr_asprintf(&command, "camcontrol load `echo %s | sed 's|/dev/||'`", |
---|
319 | dev); |
---|
320 | } |
---|
321 | #else |
---|
322 | mr_asprintf(&command, "eject -t %s", dev); |
---|
323 | #endif |
---|
324 | i = run_program_and_log_output(command, FALSE); |
---|
325 | mr_free(command); |
---|
326 | return (i); |
---|
327 | } |
---|
328 | |
---|
329 | |
---|
330 | /** |
---|
331 | * Determine whether the specified @p device (really, you can use any file) |
---|
332 | * exists. |
---|
333 | * @return TRUE if it exists, FALSE if it doesn't. |
---|
334 | */ |
---|
335 | bool does_device_exist(char *device) |
---|
336 | { |
---|
337 | |
---|
338 | /*@ buffers *********************************************************** */ |
---|
339 | char *tmp; |
---|
340 | bool ret; |
---|
341 | |
---|
342 | assert_string_is_neither_NULL_nor_zerolength(device); |
---|
343 | |
---|
344 | mr_asprintf(&tmp, "ls %s > /dev/null 2> /dev/null", device); |
---|
345 | |
---|
346 | if (system(tmp)) { |
---|
347 | ret = FALSE; |
---|
348 | } else { |
---|
349 | ret = TRUE; |
---|
350 | } |
---|
351 | mr_free(tmp); |
---|
352 | return(ret); |
---|
353 | } |
---|
354 | |
---|
355 | |
---|
356 | /** |
---|
357 | * Determine whether a non-Microsoft partition exists on any connected hard drive. |
---|
358 | * @return TRUE (there's a Linux/FreeBSD partition) or FALSE (Microsoft has taken over yet another innocent PC). |
---|
359 | */ |
---|
360 | bool does_nonMS_partition_exist(void) |
---|
361 | { |
---|
362 | #if __FreeBSD__ |
---|
363 | return |
---|
364 | !system |
---|
365 | ("for drive in /dev/ad? /dev/da?; do fdisk $drive | grep -q FreeBSD && exit 0; done; false"); |
---|
366 | #else |
---|
367 | return |
---|
368 | !system |
---|
369 | ("parted2fdisk -l 2>/dev/null | grep '^/dev/' | grep -Eqv '(MS|DOS|FAT|NTFS)'"); |
---|
370 | #endif |
---|
371 | } |
---|
372 | |
---|
373 | /** |
---|
374 | * Determine whether the specified @p partno exists on the specified @p drive. |
---|
375 | * @param drive The drive to search for the partition in. |
---|
376 | * @param partno The partition number to look for. |
---|
377 | * @return 0 if it exists, nonzero otherwise. |
---|
378 | */ |
---|
379 | int does_partition_exist(const char *drive, int partno) |
---|
380 | { |
---|
381 | /*@ buffers **************************************************** */ |
---|
382 | char *program; |
---|
383 | char *incoming = NULL; |
---|
384 | char *searchstr; |
---|
385 | char *tmp; |
---|
386 | |
---|
387 | /*@ ints ******************************************************* */ |
---|
388 | int res = 0; |
---|
389 | size_t n = 0; |
---|
390 | |
---|
391 | /*@ pointers *************************************************** */ |
---|
392 | FILE *fin; |
---|
393 | |
---|
394 | |
---|
395 | /*@ end vars *************************************************** */ |
---|
396 | assert_string_is_neither_NULL_nor_zerolength(drive); |
---|
397 | assert(partno >= 0 && partno < 999); |
---|
398 | |
---|
399 | #ifdef __FreeBSD__ |
---|
400 | // We assume here that this is running from mondorestore. (It is.) |
---|
401 | mr_asprintf(&program, "ls %s >/dev/null 2>&1", drive); |
---|
402 | res = system(program); |
---|
403 | mr_free(program); |
---|
404 | return(res); |
---|
405 | #endif |
---|
406 | |
---|
407 | mr_asprintf(&program, "parted2fdisk -l %s 2> /dev/null", drive); |
---|
408 | fin = popen(program, "r"); |
---|
409 | if (!fin) { |
---|
410 | log_it("program=%s", program); |
---|
411 | log_OS_error("Cannot popen-in program"); |
---|
412 | mr_free(program); |
---|
413 | return (0); |
---|
414 | } |
---|
415 | mr_free(program); |
---|
416 | |
---|
417 | searchstr = build_partition_name(drive, partno); |
---|
418 | mr_asprintf(&tmp, "%s ", searchstr); |
---|
419 | mr_free(searchstr); |
---|
420 | |
---|
421 | for (res = 0; !res ;) { |
---|
422 | mr_getline(&incoming, &n, fin); |
---|
423 | if (strstr(incoming, tmp)) { |
---|
424 | res = 1; |
---|
425 | } |
---|
426 | } |
---|
427 | mr_free(incoming); |
---|
428 | |
---|
429 | if (pclose(fin)) { |
---|
430 | log_OS_error("Cannot pclose fin"); |
---|
431 | } |
---|
432 | mr_free(tmp); |
---|
433 | return (res); |
---|
434 | } |
---|
435 | |
---|
436 | |
---|
437 | /** |
---|
438 | * Determine whether given NULL-terminated @p str exists in the MBR of @p dev. |
---|
439 | * @param dev The device to look in. |
---|
440 | * @param str The string to look for. |
---|
441 | * @return TRUE if it exists, FALSE if it doesn't. |
---|
442 | */ |
---|
443 | bool does_string_exist_in_boot_block(char *dev, char *str) |
---|
444 | { |
---|
445 | /*@ buffers **************************************************** */ |
---|
446 | char *command; |
---|
447 | |
---|
448 | /*@ end vars *************************************************** */ |
---|
449 | int ret; |
---|
450 | |
---|
451 | assert_string_is_neither_NULL_nor_zerolength(dev); |
---|
452 | assert_string_is_neither_NULL_nor_zerolength(str); |
---|
453 | |
---|
454 | mr_asprintf(&command, |
---|
455 | "dd if=%s bs=446 count=1 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null", |
---|
456 | dev, str); |
---|
457 | ret = system(command); |
---|
458 | mr_free(command); |
---|
459 | if (ret) { |
---|
460 | return (FALSE); |
---|
461 | } else { |
---|
462 | return (TRUE); |
---|
463 | } |
---|
464 | } |
---|
465 | |
---|
466 | |
---|
467 | /** |
---|
468 | * Determine whether specified @p str exists in the first @p n sectors of |
---|
469 | * @p dev. |
---|
470 | * @param dev The device to look in. |
---|
471 | * @param str The string to look for. |
---|
472 | * @param n The number of 512-byte sectors to search. |
---|
473 | */ |
---|
474 | bool does_string_exist_in_first_N_blocks(char *dev, char *str, int n) |
---|
475 | { |
---|
476 | /*@ buffers **************************************************** */ |
---|
477 | char *command; |
---|
478 | /*@ end vars *************************************************** */ |
---|
479 | int ret; |
---|
480 | |
---|
481 | mr_asprintf(&command, |
---|
482 | "dd if=%s bs=512 count=%i 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null", |
---|
483 | dev, n, str); |
---|
484 | ret = system(command); |
---|
485 | mr_free(command); |
---|
486 | |
---|
487 | if (ret) { |
---|
488 | return (FALSE); |
---|
489 | } else { |
---|
490 | return (TRUE); |
---|
491 | } |
---|
492 | } |
---|
493 | |
---|
494 | |
---|
495 | /** |
---|
496 | * Try to mount CD-ROM at @p mountpoint. If the CD-ROM is not found or has |
---|
497 | * not been specified, call find_cdrom_device() to find it. |
---|
498 | * @param bkpinfo The backup information structure. The only field used is @c bkpinfo->media_device. |
---|
499 | * @param mountpoint Where to mount the CD-ROM. |
---|
500 | * @return 0 for success, nonzero for failure. |
---|
501 | * @see mount_CDROM_here |
---|
502 | */ |
---|
503 | bool find_and_mount_actual_cd(struct s_bkpinfo *bkpinfo, char *mountpoint) |
---|
504 | { |
---|
505 | /*@ buffers ***************************************************** */ |
---|
506 | |
---|
507 | /*@ int's ****************************************************** */ |
---|
508 | bool res = TRUE; |
---|
509 | char *dev = NULL; |
---|
510 | |
---|
511 | /*@ end vars **************************************************** */ |
---|
512 | |
---|
513 | assert(bkpinfo != NULL); |
---|
514 | assert_string_is_neither_NULL_nor_zerolength(mountpoint); |
---|
515 | |
---|
516 | if (g_backup_media_type == dvd) { |
---|
517 | if (g_dvd_drive_is_here != NULL) { |
---|
518 | mr_asprintf(&dev, g_dvd_drive_is_here); |
---|
519 | } else { |
---|
520 | dev = find_dvd_device(); |
---|
521 | } |
---|
522 | } else { |
---|
523 | if (g_cdrom_drive_is_here != NULL) { |
---|
524 | mr_asprintf(&dev, g_cdrom_drive_is_here); |
---|
525 | } else { |
---|
526 | // find_cdrom_device allocates the string |
---|
527 | dev = find_cdrom_device(FALSE); |
---|
528 | } |
---|
529 | } |
---|
530 | |
---|
531 | if (bkpinfo->backup_media_type != iso) { |
---|
532 | retract_CD_tray_and_defeat_autorun(); |
---|
533 | } |
---|
534 | |
---|
535 | if ((dev == NULL) || (! mount_CDROM_here(dev, mountpoint))) { |
---|
536 | mr_free(dev); |
---|
537 | if (!popup_and_get_string |
---|
538 | (_("CD-ROM device"), _("Please enter your CD-ROM's /dev device"), dev)) { |
---|
539 | res = FALSE; |
---|
540 | } else { |
---|
541 | res = mount_CDROM_here(dev, mountpoint); |
---|
542 | } |
---|
543 | } |
---|
544 | if (res) { |
---|
545 | log_msg(1, _("mount failed")); |
---|
546 | } else { |
---|
547 | log_msg(1, _("mount succeeded with %s"), dev); |
---|
548 | } |
---|
549 | mr_free(dev); |
---|
550 | return(res); |
---|
551 | } |
---|
552 | |
---|
553 | |
---|
554 | /** |
---|
555 | * Locate a CD-R/W writer's SCSI node. |
---|
556 | * @param cdrw_device SCSI node will be placed here. Caller needs to free it. |
---|
557 | * @return the cdrw device or NULL if not found |
---|
558 | */ |
---|
559 | char *find_cdrw_device(void) |
---|
560 | { |
---|
561 | char *comment = NULL; |
---|
562 | char *tmp = NULL; |
---|
563 | char *tmp1 = NULL; |
---|
564 | char *cdr_exe = NULL; |
---|
565 | char *command = NULL; |
---|
566 | char *cdrw_device = NULL; |
---|
567 | |
---|
568 | if (g_cdrw_drive_is_here != NULL) { |
---|
569 | mr_asprintf(&cdrw_device, g_cdrw_drive_is_here); |
---|
570 | log_msg(3, "Been there, done that. Returning %s", cdrw_device); |
---|
571 | return(cdrw_device); |
---|
572 | } |
---|
573 | if (g_backup_media_type == dvd) { |
---|
574 | log_msg(1, |
---|
575 | "This is dumb. You're calling find_cdrw_device() but you're backing up to DVD. WTF?"); |
---|
576 | return(NULL); |
---|
577 | } |
---|
578 | run_program_and_log_output("insmod ide-scsi", -1); |
---|
579 | tmp = find_home_of_exe("cdrecord"); |
---|
580 | if (tmp) { |
---|
581 | mr_asprintf(&cdr_exe, "cdrecord"); |
---|
582 | } else { |
---|
583 | mr_asprintf(&cdr_exe, "dvdrecord"); |
---|
584 | } |
---|
585 | mr_free(tmp); |
---|
586 | |
---|
587 | tmp1 = find_home_of_exe(cdr_exe); |
---|
588 | if (tmp1) { |
---|
589 | mr_asprintf(&command, |
---|
590 | "%s -scanbus 2> /dev/null | tr -s '\t' ' ' | grep \"[0-9]*,[0-9]*,[0-9]*\" | grep -v \"[0-9]*) \\*\" | grep CD | cut -d' ' -f2 | head -n1", |
---|
591 | cdr_exe); |
---|
592 | mr_asprintf(&tmp, call_program_and_get_last_line_of_output(command)); |
---|
593 | mr_free(command); |
---|
594 | } else { |
---|
595 | mr_asprintf(&tmp, " "); |
---|
596 | } |
---|
597 | mr_free(tmp1); |
---|
598 | mr_free(cdr_exe); |
---|
599 | |
---|
600 | if (strlen(tmp) < 2) { |
---|
601 | mr_free(tmp); |
---|
602 | return(NULL); |
---|
603 | } else { |
---|
604 | cdrw_device = tmp; |
---|
605 | |
---|
606 | mr_asprintf(&comment, "Found CDRW device - %s", cdrw_device); |
---|
607 | log_it(comment); |
---|
608 | mr_free(comment); |
---|
609 | |
---|
610 | mr_free(g_cdrw_drive_is_here); |
---|
611 | mr_asprintf(&g_cdrw_drive_is_here, cdrw_device); |
---|
612 | return(cdrw_device); |
---|
613 | } |
---|
614 | } |
---|
615 | |
---|
616 | |
---|
617 | /** |
---|
618 | * Attempt to locate a CD-ROM device's /dev entry. |
---|
619 | * Several different methods may be used to find the device, including |
---|
620 | * calling @c cdrecord, searching @c dmesg, and trial-and-error. |
---|
621 | * @param output Where to put the located /dev entry. Needs to be freed by the caller. |
---|
622 | * @param try_to_mount Whether to mount the CD as part of the test; if mount |
---|
623 | * fails then return failure. |
---|
624 | * @return output if success or NULL otherwise. |
---|
625 | */ |
---|
626 | char *find_cdrom_device(bool try_to_mount) |
---|
627 | { |
---|
628 | /*@ pointers **************************************************** */ |
---|
629 | FILE *fin = NULL; |
---|
630 | char *p = NULL; |
---|
631 | char *q = NULL; |
---|
632 | char *r = NULL; |
---|
633 | char *output = NULL; |
---|
634 | size_t n = 0; |
---|
635 | |
---|
636 | /*@ bool's ****************************************************** */ |
---|
637 | bool found_it = FALSE; |
---|
638 | |
---|
639 | /*@ buffers ***************************************************** */ |
---|
640 | char *tmp = NULL; |
---|
641 | char *cdr_exe = NULL; |
---|
642 | #ifndef __FreeBSD__ |
---|
643 | char *phrase_two = NULL; |
---|
644 | char *dvd_last_resort = NULL; |
---|
645 | #endif |
---|
646 | char *command = NULL; |
---|
647 | char *mountpoint = NULL; |
---|
648 | static char *the_last_place_i_found_it = NULL; |
---|
649 | |
---|
650 | /*@ end vars **************************************************** */ |
---|
651 | |
---|
652 | if ((g_cdrom_drive_is_here != NULL) && !isdigit(g_cdrom_drive_is_here[0])) { |
---|
653 | mr_asprintf(&output, g_cdrom_drive_is_here); |
---|
654 | log_msg(3, "Been there, done that. Returning %s", output); |
---|
655 | return(output); |
---|
656 | } |
---|
657 | if ((the_last_place_i_found_it != NULL) && !try_to_mount) { |
---|
658 | mr_asprintf(&output, the_last_place_i_found_it); |
---|
659 | log_msg(3, |
---|
660 | "find_cdrom_device() --- returning last found location - '%s'", |
---|
661 | output); |
---|
662 | return(output); |
---|
663 | } |
---|
664 | |
---|
665 | tmp = find_home_of_exe("cdrecord"); |
---|
666 | if (tmp) { |
---|
667 | mr_asprintf(&cdr_exe, "cdrecord"); |
---|
668 | } else { |
---|
669 | mr_asprintf(&cdr_exe, "dvdrecord"); |
---|
670 | } |
---|
671 | mr_free(tmp); |
---|
672 | |
---|
673 | tmp = find_home_of_exe(cdr_exe); |
---|
674 | if (!tmp) { |
---|
675 | mr_asprintf(&output, "/dev/cdrom"); |
---|
676 | log_msg(4, "Can't find cdrecord; assuming %s", output); |
---|
677 | if (!does_device_exist(output)) { |
---|
678 | log_msg(4, "That didn't work. Sorry."); |
---|
679 | mr_free(cdr_exe); |
---|
680 | mr_free(output); |
---|
681 | return(NULL); |
---|
682 | } else { |
---|
683 | mr_free(cdr_exe); |
---|
684 | return(output); |
---|
685 | } |
---|
686 | } |
---|
687 | mr_free(tmp); |
---|
688 | |
---|
689 | mr_asprintf(&command, "%s -scanbus 2> /dev/null", cdr_exe); |
---|
690 | fin = popen(command, "r"); |
---|
691 | if (!fin) { |
---|
692 | log_msg(4, "command=%s", command); |
---|
693 | log_OS_error("Cannot popen command"); |
---|
694 | mr_free(cdr_exe); |
---|
695 | mr_free(command); |
---|
696 | return (NULL); |
---|
697 | } |
---|
698 | mr_free(command); |
---|
699 | |
---|
700 | for (mr_getline(&tmp, &n, fin); !feof(fin); |
---|
701 | mr_getline(&tmp, &n, fin)) { |
---|
702 | p = strchr(tmp, '\''); |
---|
703 | if (p) { |
---|
704 | q = strchr(++p, '\''); |
---|
705 | if (q) { |
---|
706 | for (r = q; *(r - 1) == ' '; r--); |
---|
707 | *r = '\0'; |
---|
708 | p = strchr(++q, '\''); |
---|
709 | if (p) { |
---|
710 | q = strchr(++p, '\''); |
---|
711 | if (q) { |
---|
712 | while (*(q - 1) == ' ') { |
---|
713 | q--; |
---|
714 | } |
---|
715 | *q = '\0'; |
---|
716 | #ifndef __FreeBSD__ |
---|
717 | mr_free(phrase_two); |
---|
718 | mr_asprintf(&phrase_two, p); |
---|
719 | #endif |
---|
720 | } |
---|
721 | } |
---|
722 | } |
---|
723 | } |
---|
724 | } |
---|
725 | paranoid_pclose(fin); |
---|
726 | mr_free(tmp); |
---|
727 | tmp = NULL; |
---|
728 | n = 0; |
---|
729 | |
---|
730 | #ifndef __FreeBSD__ |
---|
731 | if (strlen(phrase_two) == 0) { |
---|
732 | log_msg(4, "Not running phase two. String is empty."); |
---|
733 | } else { |
---|
734 | mr_asprintf(&command, "dmesg | grep \"%s\" 2> /dev/null", phrase_two); |
---|
735 | fin = popen(command, "r"); |
---|
736 | if (!fin) { |
---|
737 | log_msg(4, "Cannot run 2nd command - non-fatal, fortunately"); |
---|
738 | } else { |
---|
739 | for (mr_getline(&tmp, &n, fin); !feof(fin); |
---|
740 | mr_getline(&tmp, &n, fin)) { |
---|
741 | log_msg(5, "--> '%s'", tmp); |
---|
742 | if (tmp[0] != ' ' && tmp[1] != ' ') { |
---|
743 | p = strchr(tmp, ':'); |
---|
744 | if (p) { |
---|
745 | *p = '\0'; |
---|
746 | if (strstr(tmp, "DVD")) { |
---|
747 | mr_free(dvd_last_resort); |
---|
748 | mr_asprintf(&dvd_last_resort, "/dev/%s", tmp); |
---|
749 | log_msg(4, |
---|
750 | "Ignoring '%s' because it's a DVD drive", |
---|
751 | tmp); |
---|
752 | } else { |
---|
753 | mr_asprintf(&output, "/dev/%s", tmp); |
---|
754 | found_it = TRUE; |
---|
755 | } |
---|
756 | } |
---|
757 | } |
---|
758 | } |
---|
759 | mr_free(tmp); |
---|
760 | paranoid_pclose(fin); |
---|
761 | } |
---|
762 | mr_free(command); |
---|
763 | } |
---|
764 | mr_free(phrase_two); |
---|
765 | |
---|
766 | #endif |
---|
767 | #ifdef __FreeBSD__ |
---|
768 | if (!found_it) { |
---|
769 | log_msg(4, "OK, approach 2"); |
---|
770 | if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/cdrom"))) { |
---|
771 | if (! |
---|
772 | (found_it = |
---|
773 | set_dev_to_this_if_rx_OK(output, "/dev/cdrom1"))) { |
---|
774 | if (! |
---|
775 | (found_it = |
---|
776 | set_dev_to_this_if_rx_OK(output, "/dev/dvd"))) { |
---|
777 | if (! |
---|
778 | (found_it = |
---|
779 | set_dev_to_this_if_rx_OK(output, "/dev/acd0"))) { |
---|
780 | if (! |
---|
781 | (found_it = |
---|
782 | set_dev_to_this_if_rx_OK(output, |
---|
783 | "/dev/cd01"))) { |
---|
784 | if (! |
---|
785 | (found_it = |
---|
786 | set_dev_to_this_if_rx_OK(output, |
---|
787 | "/dev/acd1"))) { |
---|
788 | if (! |
---|
789 | (found_it = |
---|
790 | set_dev_to_this_if_rx_OK(output, |
---|
791 | "/dev/cd1"))) |
---|
792 | { |
---|
793 | mr_free(cdr_exe); |
---|
794 | return(NULL); |
---|
795 | } |
---|
796 | } |
---|
797 | } |
---|
798 | } |
---|
799 | } |
---|
800 | } |
---|
801 | } |
---|
802 | } |
---|
803 | #else |
---|
804 | if (dvd_last_resort != NULL) { |
---|
805 | if (!found_it && strlen(dvd_last_resort) > 0) { |
---|
806 | log_msg(4, "Well, I'll use the DVD - %s - as a last resort", |
---|
807 | dvd_last_resort); |
---|
808 | mr_free(output); |
---|
809 | mr_asprintf(&output, dvd_last_resort); |
---|
810 | found_it = TRUE; |
---|
811 | } |
---|
812 | } |
---|
813 | mr_free(dvd_last_resort); |
---|
814 | |
---|
815 | if (found_it) { |
---|
816 | mr_asprintf(&tmp, "grep \"%s=ide-scsi\" /proc/cmdline &> /dev/null", |
---|
817 | strrchr(output, '/') + 1); |
---|
818 | if (system(tmp) == 0) { |
---|
819 | log_msg(4, |
---|
820 | "%s is not right. It's being SCSI-emulated. Continuing.", |
---|
821 | output); |
---|
822 | found_it = FALSE; |
---|
823 | mr_free(output); |
---|
824 | } |
---|
825 | mr_free(tmp); |
---|
826 | } |
---|
827 | |
---|
828 | if (found_it) { |
---|
829 | log_msg(4, "(find_cdrom_device) --> '%s'", output); |
---|
830 | if (!does_device_exist(output)) { |
---|
831 | log_msg(4, "OK, I was wrong, I haven't found it... yet."); |
---|
832 | found_it = FALSE; |
---|
833 | mr_free(output); |
---|
834 | } |
---|
835 | } |
---|
836 | |
---|
837 | if (!found_it) { |
---|
838 | log_msg(4, "OK, approach 2"); |
---|
839 | if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/scd0"))) { |
---|
840 | if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/sr0"))) { |
---|
841 | if (! |
---|
842 | (found_it = |
---|
843 | set_dev_to_this_if_rx_OK(output, "/dev/cdrom"))) { |
---|
844 | if (! |
---|
845 | (found_it = |
---|
846 | set_dev_to_this_if_rx_OK(output, |
---|
847 | "/dev/cdrom0"))) { |
---|
848 | if (! |
---|
849 | (found_it = |
---|
850 | set_dev_to_this_if_rx_OK(output, |
---|
851 | "/dev/cdrom1"))) { |
---|
852 | if (! |
---|
853 | (found_it = |
---|
854 | set_dev_to_this_if_rx_OK(output, |
---|
855 | "/dev/sr1"))) { |
---|
856 | if (! |
---|
857 | (found_it = |
---|
858 | set_dev_to_this_if_rx_OK(output, |
---|
859 | "/dev/dvd"))) |
---|
860 | { |
---|
861 | if (! |
---|
862 | (found_it = |
---|
863 | set_dev_to_this_if_rx_OK(output, |
---|
864 | g_cdrw_drive_is_here))) |
---|
865 | { |
---|
866 | mr_free(cdr_exe); |
---|
867 | return(NULL); |
---|
868 | } |
---|
869 | } |
---|
870 | } |
---|
871 | } |
---|
872 | } |
---|
873 | } |
---|
874 | } |
---|
875 | } |
---|
876 | } |
---|
877 | #endif |
---|
878 | |
---|
879 | mr_asprintf(&mountpoint, "/tmp/cd.%d", (int) (random() % 32767)); |
---|
880 | make_hole_for_dir(mountpoint); |
---|
881 | |
---|
882 | if (found_it && try_to_mount) { |
---|
883 | if (! mount_CDROM_here(output, mountpoint)) { |
---|
884 | log_msg(4, "[Cardigans] I've changed my mind"); |
---|
885 | found_it = FALSE; |
---|
886 | mr_free(output); |
---|
887 | } else { |
---|
888 | mr_asprintf(&tmp, "%s/archives", mountpoint); |
---|
889 | if (!does_file_exist(tmp)) { |
---|
890 | log_msg(4, "[Cardigans] I'll take it back"); |
---|
891 | found_it = FALSE; |
---|
892 | mr_free(output); |
---|
893 | } else { |
---|
894 | mr_asprintf(&command, "umount %s", output); |
---|
895 | paranoid_system(command); |
---|
896 | mr_free(command); |
---|
897 | log_msg(4, "I'm confident the Mondo CD is in %s", output); |
---|
898 | } |
---|
899 | mr_free(tmp); |
---|
900 | } |
---|
901 | } |
---|
902 | unlink(mountpoint); |
---|
903 | mr_free(mountpoint); |
---|
904 | |
---|
905 | if (found_it) { |
---|
906 | if (!does_file_exist(output)) { |
---|
907 | log_msg(3, "I still haven't found it."); |
---|
908 | mr_free(output); |
---|
909 | return(NULL); |
---|
910 | } |
---|
911 | log_msg(3, "(find_cdrom_device) --> '%s'", output); |
---|
912 | mr_asprintf(&the_last_place_i_found_it, output); |
---|
913 | mr_free(g_cdrom_drive_is_here); |
---|
914 | mr_asprintf(&g_cdrom_drive_is_here, output); |
---|
915 | return(output); |
---|
916 | } |
---|
917 | |
---|
918 | mr_asprintf(&command, |
---|
919 | "%s -scanbus | grep \"[0-9],[0-9],[0-9]\" | grep \"[D|C][V|D]\" | grep -n \"\" | grep \"%s\" | cut -d':' -f2", |
---|
920 | cdr_exe, g_cdrw_drive_is_here); |
---|
921 | mr_free(cdr_exe); |
---|
922 | |
---|
923 | log_msg(1, "command=%s", command); |
---|
924 | mr_asprintf(&tmp, call_program_and_get_last_line_of_output(command)); |
---|
925 | mr_free(command); |
---|
926 | |
---|
927 | if (tmp[0]) { |
---|
928 | output = tmp; |
---|
929 | log_msg(4, "Finally found it at %s", output); |
---|
930 | } else { |
---|
931 | mr_free(tmp); |
---|
932 | mr_free(output); |
---|
933 | log_msg(4, "Still couldn't find it."); |
---|
934 | } |
---|
935 | return(output); |
---|
936 | } |
---|
937 | |
---|
938 | |
---|
939 | char *find_dvd_device() |
---|
940 | { |
---|
941 | char *tmp = NULL; |
---|
942 | int retval = 0, devno = -1; |
---|
943 | char *output = NULL; |
---|
944 | |
---|
945 | if (g_dvd_drive_is_here != NULL) { |
---|
946 | mr_asprintf(&output, g_dvd_drive_is_here); |
---|
947 | log_msg(3, "Been there, done that. Returning %s", output); |
---|
948 | return (output); |
---|
949 | } |
---|
950 | |
---|
951 | mr_asprintf(&tmp, call_program_and_get_last_line_of_output |
---|
952 | ("dvdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1") |
---|
953 | ); |
---|
954 | log_msg(5, "tmp = '%s'", tmp); |
---|
955 | if (!tmp[0]) { |
---|
956 | mr_free(tmp); |
---|
957 | mr_asprintf(&tmp, call_program_and_get_last_line_of_output |
---|
958 | ("cdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1") |
---|
959 | ); |
---|
960 | } |
---|
961 | if (tmp[0]) { |
---|
962 | devno = atoi(tmp) - 1; |
---|
963 | } |
---|
964 | mr_free(tmp); |
---|
965 | |
---|
966 | if (devno >= 0) { |
---|
967 | retval = 0; |
---|
968 | mr_asprintf(&output, "/dev/scd%d", devno); |
---|
969 | mr_free(g_dvd_drive_is_here); |
---|
970 | mr_asprintf(&g_dvd_drive_is_here, output); |
---|
971 | log_msg(2, "I think DVD is at %s", output); |
---|
972 | } else { |
---|
973 | log_msg(2, "I cannot find DVD"); |
---|
974 | } |
---|
975 | |
---|
976 | return(output); |
---|
977 | } |
---|
978 | |
---|
979 | |
---|
980 | /** |
---|
981 | * Find the size of the specified @p drive, in megabytes. Uses @c ioctl calls |
---|
982 | * and @c dmesg. |
---|
983 | * @param drive The device to find the size of. |
---|
984 | * @return size in megabytes. |
---|
985 | */ |
---|
986 | long get_phys_size_of_drive(char *drive) |
---|
987 | { |
---|
988 | int fd; |
---|
989 | #if linux |
---|
990 | unsigned long long s = 0; |
---|
991 | int fileid, cylinders = 0, cylindersleft = 0; |
---|
992 | int cylindersize = 0; |
---|
993 | int gotgeo = 0; |
---|
994 | |
---|
995 | |
---|
996 | struct hd_geometry hdgeo; |
---|
997 | #elif __FreeBSD__ |
---|
998 | off_t s; |
---|
999 | #endif |
---|
1000 | |
---|
1001 | long outvalA = -1; |
---|
1002 | long outvalB = -1; |
---|
1003 | long outvalC = -1; |
---|
1004 | |
---|
1005 | if ((fd = open(drive, O_RDONLY)) != -1) { |
---|
1006 | if (ioctl(fd, |
---|
1007 | #if linux |
---|
1008 | #ifdef BLKGETSIZE64 |
---|
1009 | BLKGETSIZE64, |
---|
1010 | #else |
---|
1011 | BLKGETSIZE, |
---|
1012 | #endif |
---|
1013 | #elif __FreeBSD__ |
---|
1014 | DIOCGMEDIASIZE, |
---|
1015 | #endif |
---|
1016 | &s) != -1) { |
---|
1017 | close(fd); |
---|
1018 | // s>>11 works for older disks but not for newer ones |
---|
1019 | outvalB = |
---|
1020 | #if linux |
---|
1021 | #ifdef BLKGETSIZE64 |
---|
1022 | s >> 20 |
---|
1023 | #else |
---|
1024 | s >> 11 |
---|
1025 | #endif |
---|
1026 | #else |
---|
1027 | s >> 20 |
---|
1028 | #endif |
---|
1029 | ; |
---|
1030 | } |
---|
1031 | } |
---|
1032 | |
---|
1033 | if (outvalB <= 0) { |
---|
1034 | log_msg(1, "Error getting size of %s: %s", drive, strerror(errno)); |
---|
1035 | #if linux |
---|
1036 | fileid = open(drive, O_RDONLY); |
---|
1037 | if (fileid) { |
---|
1038 | if (ioctl(fileid, HDIO_GETGEO, &hdgeo) != -1) { |
---|
1039 | if (hdgeo.cylinders && hdgeo.heads && hdgeo.sectors) { |
---|
1040 | cylindersleft = cylinders = hdgeo.cylinders; |
---|
1041 | cylindersize = hdgeo.heads * hdgeo.sectors / 2; |
---|
1042 | outvalA = cylindersize * cylinders / 1024; |
---|
1043 | log_msg(2, "Got Harddisk geometry, C:%d, H:%d, S:%d", |
---|
1044 | hdgeo.cylinders, hdgeo.heads, hdgeo.sectors); |
---|
1045 | gotgeo = 1; |
---|
1046 | } else { |
---|
1047 | log_msg(1, "Harddisk geometry wrong"); |
---|
1048 | } |
---|
1049 | } else { |
---|
1050 | log_msg(1, |
---|
1051 | "Error in ioctl() getting new hard disk geometry (%s), resizing in unsafe mode", |
---|
1052 | strerror(errno)); |
---|
1053 | } |
---|
1054 | close(fileid); |
---|
1055 | } else { |
---|
1056 | log_msg(1, "Failed to open %s for reading: %s", drive, |
---|
1057 | strerror(errno)); |
---|
1058 | } |
---|
1059 | if (!gotgeo) { |
---|
1060 | log_msg(1, "Failed to get harddisk geometry, using old mode"); |
---|
1061 | } |
---|
1062 | #endif |
---|
1063 | } |
---|
1064 | // OLDER DISKS will give ridiculously low value for outvalB (so outvalA is returned) :) |
---|
1065 | // NEWER DISKS will give sane value for outvalB (close to outvalA, in other words) :) |
---|
1066 | |
---|
1067 | outvalC = (outvalA > outvalB) ? outvalA : outvalB; |
---|
1068 | |
---|
1069 | // log_msg (5, "drive = %s, error = %s", drive, strerror (errno)); |
---|
1070 | // fatal_error ("GPSOD: Unable to get size of drive"); |
---|
1071 | log_msg(1, "%s --> %ld or %ld --> %ld", drive, outvalA, outvalB, |
---|
1072 | outvalC); |
---|
1073 | |
---|
1074 | return (outvalC); |
---|
1075 | } |
---|
1076 | |
---|
1077 | |
---|
1078 | /** |
---|
1079 | * Determine whether @p format is supported by the kernel. Uses /proc/filesystems |
---|
1080 | * under Linux and @c lsvfs under FreeBSD. |
---|
1081 | * @param format The format to test. |
---|
1082 | * @return TRUE if the format is supported, FALSE if not. |
---|
1083 | */ |
---|
1084 | bool is_this_a_valid_disk_format(char *format) |
---|
1085 | { |
---|
1086 | char *good_formats = NULL; |
---|
1087 | char *command = NULL; |
---|
1088 | char *tmp = NULL; |
---|
1089 | char *format_sz = NULL; |
---|
1090 | |
---|
1091 | FILE *pin = NULL; |
---|
1092 | bool retval; |
---|
1093 | size_t n = 0; |
---|
1094 | assert_string_is_neither_NULL_nor_zerolength(format); |
---|
1095 | |
---|
1096 | mr_asprintf(&format_sz, "%s ", format); |
---|
1097 | |
---|
1098 | #ifdef __FreeBSD__ |
---|
1099 | mr_asprintf(&command, |
---|
1100 | "lsvfs | tr -s '\t' ' ' | grep -v Filesys | grep -v -- -- | cut -d' ' -f1 | tr -s '\n' ' '"); |
---|
1101 | #else |
---|
1102 | mr_asprintf(&command, |
---|
1103 | "grep -v nodev /proc/filesystems | tr -s '\t' ' ' | cut -d' ' -f2 | tr -s '\n' ' '"); |
---|
1104 | #endif |
---|
1105 | |
---|
1106 | pin = popen(command, "r"); |
---|
1107 | mr_free(command); |
---|
1108 | |
---|
1109 | if (!pin) { |
---|
1110 | log_OS_error("Unable to read good formats"); |
---|
1111 | retval = FALSE; |
---|
1112 | } else { |
---|
1113 | mr_getline(&good_formats, &n , pin); |
---|
1114 | if (pclose(pin)) { |
---|
1115 | log_OS_error("Cannot pclose good formats"); |
---|
1116 | } |
---|
1117 | strip_spaces(good_formats); |
---|
1118 | // " ntfs 7 " -- um, cheating much? :) |
---|
1119 | mr_asprintf(&tmp, " %s swap lvm raid ntfs 7 ",good_formats); |
---|
1120 | mr_free(good_formats); |
---|
1121 | good_formats = tmp; |
---|
1122 | |
---|
1123 | if (strstr(good_formats, format_sz)) { |
---|
1124 | retval = TRUE; |
---|
1125 | } else { |
---|
1126 | retval = FALSE; |
---|
1127 | } |
---|
1128 | mr_free(good_formats); |
---|
1129 | } |
---|
1130 | mr_free(format_sz); |
---|
1131 | return (retval); |
---|
1132 | } |
---|
1133 | |
---|
1134 | |
---|
1135 | /** @def SWAPLIST_COMMAND The command to list the swap files/partitions in use. */ |
---|
1136 | |
---|
1137 | /** |
---|
1138 | * Determine whether @p device_raw is currently mounted. |
---|
1139 | * @param device_raw The device to check. |
---|
1140 | * @return TRUE if it's mounted, FALSE if not. |
---|
1141 | */ |
---|
1142 | bool is_this_device_mounted(char *device_raw) |
---|
1143 | { |
---|
1144 | |
---|
1145 | /*@ pointers **************************************************** */ |
---|
1146 | FILE *fin; |
---|
1147 | |
---|
1148 | /*@ buffers ***************************************************** */ |
---|
1149 | char *incoming = NULL; |
---|
1150 | char *device_with_tab; |
---|
1151 | char *device_with_space; |
---|
1152 | char *tmp; |
---|
1153 | size_t n = 0; |
---|
1154 | |
---|
1155 | #ifdef __FreeBSD__ |
---|
1156 | #define SWAPLIST_COMMAND "swapinfo" |
---|
1157 | #else |
---|
1158 | #define SWAPLIST_COMMAND "cat /proc/swaps" |
---|
1159 | #endif |
---|
1160 | |
---|
1161 | /*@ end vars **************************************************** */ |
---|
1162 | |
---|
1163 | assert(device_raw != NULL); |
---|
1164 | // assert_string_is_neither_NULL_nor_zerolength(device_raw); |
---|
1165 | if (device_raw[0] != '/' && !strstr(device_raw, ":/")) { |
---|
1166 | log_msg(1, "%s needs to have a '/' prefixed - I'll do it", |
---|
1167 | device_raw); |
---|
1168 | mr_asprintf(&tmp, "/%s", device_raw); |
---|
1169 | } else { |
---|
1170 | mr_asprintf(&tmp, device_raw); |
---|
1171 | } |
---|
1172 | log_msg(1, "Is %s mounted?", tmp); |
---|
1173 | if (!strcmp(tmp, "/proc") || !strcmp(tmp, "proc")) { |
---|
1174 | log_msg(1, |
---|
1175 | "I don't know how the heck /proc made it into the mountlist. I'll ignore it."); |
---|
1176 | return (FALSE); |
---|
1177 | } |
---|
1178 | mr_asprintf(&device_with_tab, "%s\t", tmp); |
---|
1179 | mr_asprintf(&device_with_space, "%s ", tmp); |
---|
1180 | mr_free(tmp); |
---|
1181 | |
---|
1182 | if (!(fin = popen("mount", "r"))) { |
---|
1183 | log_OS_error("Cannot popen 'mount'"); |
---|
1184 | return (FALSE); |
---|
1185 | } |
---|
1186 | for (mr_getline(&incoming, &n, fin); !feof(fin); |
---|
1187 | mr_getline(&incoming, &n, fin)) { |
---|
1188 | if (strstr(incoming, device_with_space) //> incoming |
---|
1189 | || strstr(incoming, device_with_tab)) // > incoming) |
---|
1190 | { |
---|
1191 | paranoid_pclose(fin); |
---|
1192 | return(TRUE); |
---|
1193 | } |
---|
1194 | } |
---|
1195 | mr_free(incoming); |
---|
1196 | mr_free(device_with_tab); |
---|
1197 | paranoid_pclose(fin); |
---|
1198 | |
---|
1199 | mr_asprintf(&tmp, "%s | grep -E \"^%s\" > /dev/null 2> /dev/null", |
---|
1200 | SWAPLIST_COMMAND, device_with_space); |
---|
1201 | mr_free(device_with_space); |
---|
1202 | |
---|
1203 | log_msg(4, "tmp (command) = '%s'", tmp); |
---|
1204 | if (!system(tmp)) { |
---|
1205 | mr_free(tmp); |
---|
1206 | return(TRUE); |
---|
1207 | } |
---|
1208 | mr_free(tmp); |
---|
1209 | return (FALSE); |
---|
1210 | } |
---|
1211 | |
---|
1212 | |
---|
1213 | #ifdef __FreeBSD__ |
---|
1214 | // CODE IS FREEBSD-SPECIFIC |
---|
1215 | /** |
---|
1216 | * Create a loopback device for specified @p fname. |
---|
1217 | * @param fname The file to associate with a device. |
---|
1218 | * @return /dev entry for the device (to be freed by the caller), |
---|
1219 | * or NULL if it couldn't be allocated. |
---|
1220 | */ |
---|
1221 | char *make_vn(char *fname) |
---|
1222 | { |
---|
1223 | char *device = NULL; |
---|
1224 | char *mddevice = NULL; |
---|
1225 | char *command = NULL; |
---|
1226 | int vndev = 2; |
---|
1227 | |
---|
1228 | if (atoi |
---|
1229 | (call_program_and_get_last_line_of_output |
---|
1230 | ("/sbin/sysctl -n kern.osreldate")) < 500000) { |
---|
1231 | do { |
---|
1232 | mr_free(mddevice); |
---|
1233 | mr_asprintf(&mddevice, "vn%ic", vndev++); |
---|
1234 | |
---|
1235 | mr_free(command); |
---|
1236 | mr_asprintf(&command, "vnconfig %s %s", mddevice, fname); |
---|
1237 | |
---|
1238 | if (vndev > 10) { |
---|
1239 | mr_free(command); |
---|
1240 | mr_free(mddevice); |
---|
1241 | return NULL; |
---|
1242 | } |
---|
1243 | } |
---|
1244 | while (system(command)); |
---|
1245 | mr_free(command); |
---|
1246 | } else { |
---|
1247 | mr_asprintf(&command, "mdconfig -a -t vnode -f %s", fname); |
---|
1248 | mr_asprintf(&mddevice, call_program_and_get_last_line_of_output(command)); |
---|
1249 | mr_free(command); |
---|
1250 | |
---|
1251 | if (!strstr(mddevice, "md")) { |
---|
1252 | mr_free(mddevice); |
---|
1253 | return NULL; |
---|
1254 | } |
---|
1255 | } |
---|
1256 | mr_asprintf(&device, "/dev/%s", mddevice); |
---|
1257 | mr_free(mddevice); |
---|
1258 | return(device); |
---|
1259 | } |
---|
1260 | |
---|
1261 | |
---|
1262 | // CODE IS FREEBSD-SPECIFIC |
---|
1263 | /** |
---|
1264 | * Deallocate specified @p dname. |
---|
1265 | * This should be called when you are done with the device created by make_vn(), |
---|
1266 | * so the system does not run out of @c vn devices. |
---|
1267 | * @param dname The device to deallocate. |
---|
1268 | * @return 0 for success, nonzero for failure. |
---|
1269 | */ |
---|
1270 | int kick_vn(char *dname) |
---|
1271 | { |
---|
1272 | char *command; |
---|
1273 | int ret = 0; |
---|
1274 | |
---|
1275 | if (strncmp(dname, "/dev/", 5) == 0) { |
---|
1276 | dname += 5; |
---|
1277 | } |
---|
1278 | |
---|
1279 | if (atoi |
---|
1280 | (call_program_and_get_last_line_of_output |
---|
1281 | ("/sbin/sysctl -n kern.osreldate")) < 500000) { |
---|
1282 | mr_asprintf(&command, "vnconfig -d %s", dname); |
---|
1283 | ret = system(command); |
---|
1284 | mr_free(command); |
---|
1285 | return(ret); |
---|
1286 | } else { |
---|
1287 | mr_asprintf(&command, "mdconfig -d -u %s", dname); |
---|
1288 | ret = system(command); |
---|
1289 | mr_free(command); |
---|
1290 | return(ret); |
---|
1291 | } |
---|
1292 | /*NOTREACHED*/ return 255; |
---|
1293 | } |
---|
1294 | #endif |
---|
1295 | |
---|
1296 | |
---|
1297 | /** |
---|
1298 | * Ask the user for CD number @p cd_number_i_want. |
---|
1299 | * Sets g_current_media_number once the correct CD is inserted. |
---|
1300 | * @param bkpinfo The backup information structure. Fields used: |
---|
1301 | * - @c bkpinfo->backup_media_type |
---|
1302 | * - @c bkpinfo->prefix |
---|
1303 | * - @c bkpinfo->isodir |
---|
1304 | * - @c bkpinfo->media_device |
---|
1305 | * - @c bkpinfo->please_dont_eject_when_restoring |
---|
1306 | * @param cd_number_i_want The CD number to ask for. |
---|
1307 | */ |
---|
1308 | void |
---|
1309 | insist_on_this_cd_number(struct s_bkpinfo *bkpinfo, int cd_number_i_want) |
---|
1310 | { |
---|
1311 | |
---|
1312 | /*@ int ************************************************************* */ |
---|
1313 | int res = 0; |
---|
1314 | |
---|
1315 | |
---|
1316 | /*@ buffers ********************************************************* */ |
---|
1317 | char *tmp; |
---|
1318 | char *request; |
---|
1319 | |
---|
1320 | assert(bkpinfo != NULL); |
---|
1321 | assert(cd_number_i_want > 0); |
---|
1322 | |
---|
1323 | // log_msg(3, "Insisting on CD number %d", cd_number_i_want); |
---|
1324 | |
---|
1325 | if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) { |
---|
1326 | log_msg(3, |
---|
1327 | "No need to insist_on_this_cd_number when the backup type isn't CD-R(W) or NFS or ISO"); |
---|
1328 | return; |
---|
1329 | } |
---|
1330 | |
---|
1331 | mr_asprintf(&tmp, "mkdir -p " MNT_CDROM); |
---|
1332 | run_program_and_log_output(tmp, 5); |
---|
1333 | mr_free(tmp); |
---|
1334 | |
---|
1335 | if (g_ISO_restore_mode || bkpinfo->backup_media_type == iso |
---|
1336 | || bkpinfo->backup_media_type == nfs) { |
---|
1337 | log_msg(3, "Remounting CD"); |
---|
1338 | g_ISO_restore_mode = TRUE; |
---|
1339 | // FIXME --- I'm tempted to do something about this... |
---|
1340 | // Why unmount and remount again and again? |
---|
1341 | if (is_this_device_mounted(MNT_CDROM)) { |
---|
1342 | run_program_and_log_output("umount " MNT_CDROM, 5); |
---|
1343 | } |
---|
1344 | system("mkdir -p /tmp/isodir &> /dev/null"); |
---|
1345 | mr_asprintf(&tmp, "%s/%s/%s-%d.iso", bkpinfo->isodir, |
---|
1346 | bkpinfo->nfs_remote_dir, bkpinfo->prefix, |
---|
1347 | cd_number_i_want); |
---|
1348 | if (!does_file_exist(tmp)) { |
---|
1349 | mr_free(tmp); |
---|
1350 | mr_asprintf(&tmp, "/tmp/isodir/%s/%s-%d.iso", |
---|
1351 | bkpinfo->nfs_remote_dir, bkpinfo->prefix, |
---|
1352 | cd_number_i_want); |
---|
1353 | if (does_file_exist(tmp)) { |
---|
1354 | log_msg(1, |
---|
1355 | "FIXME - hacking bkpinfo->isodir from '%s' to /tmp/isodir", |
---|
1356 | bkpinfo->isodir); |
---|
1357 | mr_allocstr(bkpinfo->isodir, "/tmp/isodir"); |
---|
1358 | } |
---|
1359 | } |
---|
1360 | log_msg(3, "Mounting %s at %s", tmp, MNT_CDROM); |
---|
1361 | if (! mount_CDROM_here(tmp, MNT_CDROM)) { |
---|
1362 | fatal_error("Mommy!"); |
---|
1363 | } |
---|
1364 | mr_free(tmp); |
---|
1365 | } |
---|
1366 | if ((res = what_number_cd_is_this(bkpinfo)) != cd_number_i_want) { |
---|
1367 | log_msg(3, "Currently, we hold %d but we want %d", res, |
---|
1368 | cd_number_i_want); |
---|
1369 | mr_asprintf(&tmp, "Insisting on %s #%d", |
---|
1370 | bkpinfo->backup_media_string, |
---|
1371 | cd_number_i_want); |
---|
1372 | mr_asprintf(&request, "Please insert %s #%d and press Enter.", |
---|
1373 | bkpinfo->backup_media_string, |
---|
1374 | cd_number_i_want); |
---|
1375 | log_msg(3, tmp); |
---|
1376 | mr_free(tmp); |
---|
1377 | |
---|
1378 | while (what_number_cd_is_this(bkpinfo) != cd_number_i_want) { |
---|
1379 | sync(); |
---|
1380 | if (is_this_device_mounted(MNT_CDROM)) { |
---|
1381 | res = |
---|
1382 | run_program_and_log_output("umount " MNT_CDROM, FALSE); |
---|
1383 | } else { |
---|
1384 | res = 0; |
---|
1385 | } |
---|
1386 | if (res) { |
---|
1387 | log_to_screen(_("WARNING - failed to unmount CD-ROM drive")); |
---|
1388 | } |
---|
1389 | if (!bkpinfo->please_dont_eject) { |
---|
1390 | res = eject_device(bkpinfo->media_device); |
---|
1391 | } else { |
---|
1392 | res = 0; |
---|
1393 | } |
---|
1394 | if (res) { |
---|
1395 | log_to_screen(_("WARNING - failed to eject CD-ROM disk")); |
---|
1396 | } |
---|
1397 | popup_and_OK(request); |
---|
1398 | if (!bkpinfo->please_dont_eject) { |
---|
1399 | inject_device(bkpinfo->media_device); |
---|
1400 | } |
---|
1401 | sync(); |
---|
1402 | } |
---|
1403 | mr_free(request); |
---|
1404 | |
---|
1405 | log_msg(1, "Thankyou. Proceeding..."); |
---|
1406 | g_current_media_number = cd_number_i_want; |
---|
1407 | } |
---|
1408 | } |
---|
1409 | /* @} - end of deviceGroup */ |
---|
1410 | |
---|
1411 | |
---|
1412 | /** |
---|
1413 | * Ask user for details of backup/restore information. |
---|
1414 | * Called when @c mondoarchive doesn't get any parameters. |
---|
1415 | * @param bkpinfo The backup information structure to fill out with the user's data. |
---|
1416 | * @param archiving_to_media TRUE if archiving, FALSE if restoring. |
---|
1417 | * @return 0, always. |
---|
1418 | * @bug No point of `int' return value. |
---|
1419 | * @ingroup archiveGroup |
---|
1420 | */ |
---|
1421 | int interactively_obtain_media_parameters_from_user(struct s_bkpinfo |
---|
1422 | *bkpinfo, |
---|
1423 | bool |
---|
1424 | archiving_to_media) |
---|
1425 | // archiving_to_media is TRUE if I'm being called by mondoarchive |
---|
1426 | // archiving_to_media is FALSE if I'm being called by mondorestore |
---|
1427 | { |
---|
1428 | char *tmp = NULL; |
---|
1429 | char *tmp1 = NULL; |
---|
1430 | char *tmp2 = NULL; |
---|
1431 | char *sz_size = NULL; |
---|
1432 | char *command = NULL; |
---|
1433 | char *comment = NULL; |
---|
1434 | char *prompt = NULL; |
---|
1435 | int i = 0; |
---|
1436 | FILE *fin = NULL; |
---|
1437 | |
---|
1438 | assert(bkpinfo != NULL); |
---|
1439 | bkpinfo->nonbootable_backup = FALSE; |
---|
1440 | |
---|
1441 | // Tape, CD, NFS, ...? |
---|
1442 | srandom(getpid()); |
---|
1443 | bkpinfo->backup_media_type = |
---|
1444 | (g_restoring_live_from_cd) ? cdr : |
---|
1445 | which_backup_media_type(bkpinfo->restore_data); |
---|
1446 | if (bkpinfo->backup_media_type == none) { |
---|
1447 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1448 | finish(1); |
---|
1449 | } |
---|
1450 | if (bkpinfo->backup_media_type == tape && bkpinfo->restore_data) { |
---|
1451 | popup_and_OK(_("Please remove CD/floppy from drive(s)")); |
---|
1452 | } |
---|
1453 | bkpinfo->backup_media_string = bkptype_to_string(bkpinfo->backup_media_type); |
---|
1454 | log_msg(3, "media type = %s",bkpinfo->backup_media_string); |
---|
1455 | |
---|
1456 | if (archiving_to_media) { |
---|
1457 | sensibly_set_tmpdir_and_scratchdir(bkpinfo); |
---|
1458 | } |
---|
1459 | bkpinfo->cdrw_speed = (bkpinfo->backup_media_type == cdstream) ? 2 : 4; |
---|
1460 | bkpinfo->compression_level = |
---|
1461 | (bkpinfo->backup_media_type == cdstream) ? 1 : 5; |
---|
1462 | bkpinfo->use_lzo = |
---|
1463 | (bkpinfo->backup_media_type == cdstream) ? TRUE : FALSE; |
---|
1464 | mvaddstr_and_log_it(2, 0, " "); |
---|
1465 | |
---|
1466 | // Find device's /dev (or SCSI) entry |
---|
1467 | switch (bkpinfo->backup_media_type) { |
---|
1468 | case cdr: |
---|
1469 | case cdrw: |
---|
1470 | case dvd: |
---|
1471 | case usb: |
---|
1472 | if (archiving_to_media) { |
---|
1473 | if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) { |
---|
1474 | if (ask_me_yes_or_no |
---|
1475 | (_("Is your computer a laptop, or does the CD writer incorporate BurnProof technology?"))) |
---|
1476 | { |
---|
1477 | bkpinfo->manual_cd_tray = TRUE; |
---|
1478 | } |
---|
1479 | } |
---|
1480 | if ((bkpinfo->compression_level = |
---|
1481 | which_compression_level()) == -1) { |
---|
1482 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1483 | finish(1); |
---|
1484 | } |
---|
1485 | mr_asprintf(&comment, _("What speed is your %s (re)writer?"), |
---|
1486 | bkpinfo->backup_media_string); |
---|
1487 | if (bkpinfo->backup_media_type == dvd) { |
---|
1488 | mr_free(bkpinfo->media_device); |
---|
1489 | bkpinfo->media_device = find_dvd_device(); |
---|
1490 | mr_asprintf(&tmp, "1"); |
---|
1491 | mr_asprintf(&sz_size, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4582 MB |
---|
1492 | log_msg(1, "Setting to DVD defaults"); |
---|
1493 | } else if (bkpinfo->backup_media_type == usb) { |
---|
1494 | strcpy(bkpinfo->media_device, VANILLA_USB_DEVICE); |
---|
1495 | strcpy(sz_size, "512"); |
---|
1496 | } else { |
---|
1497 | mr_allocstr(bkpinfo->media_device,VANILLA_SCSI_CDROM ); |
---|
1498 | mr_asprintf(&tmp, "4"); |
---|
1499 | mr_asprintf(&sz_size, "650"); |
---|
1500 | log_msg(1, "Setting to CD defaults"); |
---|
1501 | } |
---|
1502 | if ((bkpinfo->backup_media_type != dvd) && (bkpinfo->backup_media_type != usb)) { |
---|
1503 | if (!popup_and_get_string(_("Speed"), comment, tmp)) { |
---|
1504 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1505 | finish(1); |
---|
1506 | } |
---|
1507 | } |
---|
1508 | mr_free(comment); |
---|
1509 | |
---|
1510 | bkpinfo->cdrw_speed = atoi(tmp); // if DVD then this shouldn't ever be used anyway :) |
---|
1511 | mr_free(tmp); |
---|
1512 | |
---|
1513 | mr_asprintf(&comment, |
---|
1514 | _("How much data (in Megabytes) will each %s store?"), |
---|
1515 | bkpinfo->backup_media_string); |
---|
1516 | |
---|
1517 | if (!popup_and_get_string("Size", comment, sz_size)) { |
---|
1518 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1519 | finish(1); |
---|
1520 | } |
---|
1521 | mr_free(comment); |
---|
1522 | |
---|
1523 | for (i = 0; i <= MAX_NOOF_MEDIA; i++) { |
---|
1524 | bkpinfo->media_size[i] = atoi(sz_size); |
---|
1525 | } |
---|
1526 | mr_free(sz_size); |
---|
1527 | |
---|
1528 | if (bkpinfo->media_size[0] <= 0) { |
---|
1529 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1530 | finish(1); |
---|
1531 | } |
---|
1532 | } |
---|
1533 | case cdstream: |
---|
1534 | if (bkpinfo->disaster_recovery) { |
---|
1535 | mr_allocstr(bkpinfo->media_device, "/dev/cdrom"); |
---|
1536 | log_msg(2, "CD-ROM device assumed to be at %s", |
---|
1537 | bkpinfo->media_device); |
---|
1538 | } else if (bkpinfo->restore_data |
---|
1539 | || bkpinfo->backup_media_type == dvd) { |
---|
1540 | if (bkpinfo->media_device == NULL) { |
---|
1541 | mr_asprintf(&bkpinfo->media_device, "/dev/cdrom"); |
---|
1542 | } // just for the heck of it :) |
---|
1543 | log_msg(1, "bkpinfo->media_device = %s", |
---|
1544 | bkpinfo->media_device); |
---|
1545 | tmp = find_cdrom_device(FALSE); |
---|
1546 | if ((bkpinfo->backup_media_type == dvd) |
---|
1547 | || (tmp != NULL)) { |
---|
1548 | mr_allocstr(bkpinfo->media_device, tmp); |
---|
1549 | log_msg(1, "bkpinfo->media_device = %s", |
---|
1550 | bkpinfo->media_device); |
---|
1551 | mr_asprintf(&comment, |
---|
1552 | _("Please specify your %s drive's /dev entry"), |
---|
1553 | bkpinfo->backup_media_string); |
---|
1554 | if (!popup_and_get_string |
---|
1555 | (_("Device?"), comment, bkpinfo->media_device)) { |
---|
1556 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1557 | finish(1); |
---|
1558 | } |
---|
1559 | mr_free(comment); |
---|
1560 | } |
---|
1561 | mr_free(tmp); |
---|
1562 | |
---|
1563 | log_msg(2, "%s device found at %s", |
---|
1564 | bkpinfo->backup_media_string, |
---|
1565 | bkpinfo->media_device); |
---|
1566 | } else { |
---|
1567 | bkpinfo->media_device = find_cdrw_device(); |
---|
1568 | if (bkpinfo->media_device != NULL) { |
---|
1569 | mr_asprintf(&tmp, |
---|
1570 | _("I think I've found your %s burner at SCSI node %s; Is this correct ? (say no if you have an IDE burner and you are running a 2.6 kernel. You will then be prompted for further details."), |
---|
1571 | bkpinfo->backup_media_string, |
---|
1572 | bkpinfo->media_device); |
---|
1573 | if (!ask_me_yes_or_no(tmp)) { |
---|
1574 | mr_free(bkpinfo->media_device); |
---|
1575 | } |
---|
1576 | mr_free(tmp); |
---|
1577 | } else { |
---|
1578 | if (g_kernel_version < 2.6) { |
---|
1579 | i = popup_and_get_string(_("Device node?"), |
---|
1580 | _("What is the SCSI node of your CD (re)writer, please?"), |
---|
1581 | bkpinfo->media_device); |
---|
1582 | } else { |
---|
1583 | i = popup_and_get_string(_("/dev entry?"), |
---|
1584 | _("What is the /dev entry of your CD (re)writer, please?"), |
---|
1585 | bkpinfo->media_device); |
---|
1586 | } |
---|
1587 | if (!i) { |
---|
1588 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1589 | finish(1); |
---|
1590 | } |
---|
1591 | } |
---|
1592 | } |
---|
1593 | if (bkpinfo->backup_media_type == cdstream) { |
---|
1594 | for (i = 0; i <= MAX_NOOF_MEDIA; i++) { |
---|
1595 | bkpinfo->media_size[i] = 650; |
---|
1596 | } |
---|
1597 | } |
---|
1598 | break; |
---|
1599 | case udev: |
---|
1600 | if (!ask_me_yes_or_no |
---|
1601 | (_("This option is for advanced users only. Are you sure?"))) { |
---|
1602 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1603 | finish(1); |
---|
1604 | } |
---|
1605 | case tape: |
---|
1606 | |
---|
1607 | mr_free(bkpinfo->media_device); |
---|
1608 | if (find_tape_device_and_size(bkpinfo->media_device, sz_size)) { |
---|
1609 | log_msg(3, _("Ok, using vanilla scsi tape.")); |
---|
1610 | mr_allocstr(bkpinfo->media_device,VANILLA_SCSI_TAPE"0" ); |
---|
1611 | if ((fin = fopen(bkpinfo->media_device, "r"))) { |
---|
1612 | paranoid_fclose(fin); |
---|
1613 | } else { |
---|
1614 | mr_allocstr(bkpinfo->media_device,"/dev/osst0"); |
---|
1615 | } |
---|
1616 | } |
---|
1617 | if (bkpinfo->media_device != NULL) { |
---|
1618 | if ((fin = fopen(bkpinfo->media_device, "r"))) { |
---|
1619 | paranoid_fclose(fin); |
---|
1620 | } else { |
---|
1621 | if (does_file_exist("/tmp/mondo-restore.cfg")) { |
---|
1622 | mr_free(bkpinfo->media_device); |
---|
1623 | read_cfg_var("/tmp/mondo-restore.cfg", "media-dev", |
---|
1624 | bkpinfo->media_device); |
---|
1625 | } |
---|
1626 | } |
---|
1627 | mr_asprintf(&tmp, |
---|
1628 | _("I think I've found your tape streamer at %s; am I right on the money?"), |
---|
1629 | bkpinfo->media_device); |
---|
1630 | if (!ask_me_yes_or_no(tmp)) { |
---|
1631 | mr_free(bkpinfo->media_device); |
---|
1632 | } |
---|
1633 | mr_free(tmp); |
---|
1634 | } else { |
---|
1635 | if (!popup_and_get_string |
---|
1636 | (_("Device name?"), |
---|
1637 | _("What is the /dev entry of your tape streamer?"), |
---|
1638 | bkpinfo->media_device)) { |
---|
1639 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1640 | finish(1); |
---|
1641 | } |
---|
1642 | } |
---|
1643 | mr_asprintf(&tmp, "ls -l %s", bkpinfo->media_device); |
---|
1644 | if (run_program_and_log_output(tmp, FALSE)) { |
---|
1645 | log_to_screen(_("User has not specified a valid /dev entry")); |
---|
1646 | finish(1); |
---|
1647 | } |
---|
1648 | mr_free(tmp); |
---|
1649 | log_msg(4, "sz_size = %s", sz_size); |
---|
1650 | mr_free(sz_size); |
---|
1651 | bkpinfo->media_size[0] = 0; |
---|
1652 | log_msg(4, "media_size[0] = %ld", bkpinfo->media_size[0]); |
---|
1653 | if (bkpinfo->media_size[0] <= 0) { |
---|
1654 | bkpinfo->media_size[0] = 0; |
---|
1655 | } |
---|
1656 | for (i = 1; i <= MAX_NOOF_MEDIA; i++) { |
---|
1657 | bkpinfo->media_size[i] = bkpinfo->media_size[0]; |
---|
1658 | } |
---|
1659 | if (archiving_to_media) { |
---|
1660 | if ((bkpinfo->compression_level = |
---|
1661 | which_compression_level()) == -1) { |
---|
1662 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1663 | finish(1); |
---|
1664 | } |
---|
1665 | } |
---|
1666 | break; |
---|
1667 | |
---|
1668 | |
---|
1669 | |
---|
1670 | case nfs: |
---|
1671 | if (bkpinfo->nfs_mount == NULL) { |
---|
1672 | bkpinfo->nfs_mount = call_program_and_get_last_line_of_output |
---|
1673 | ("mount | grep \":\" | cut -d' ' -f1 | head -n1"); |
---|
1674 | } |
---|
1675 | #ifdef __FreeBSD__ |
---|
1676 | if (TRUE) |
---|
1677 | #else |
---|
1678 | if (!bkpinfo->disaster_recovery) |
---|
1679 | #endif |
---|
1680 | { |
---|
1681 | if (!popup_and_get_string |
---|
1682 | (_("NFS dir."), |
---|
1683 | _("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.)"), |
---|
1684 | bkpinfo->nfs_mount)) { |
---|
1685 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1686 | finish(1); |
---|
1687 | } |
---|
1688 | if (!bkpinfo->restore_data) { |
---|
1689 | if ((bkpinfo->compression_level = |
---|
1690 | which_compression_level()) == -1) { |
---|
1691 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1692 | finish(1); |
---|
1693 | } |
---|
1694 | } |
---|
1695 | // check whether already mounted - we better remove |
---|
1696 | // surrounding spaces and trailing '/' for this |
---|
1697 | /* BERLIOS: Useless |
---|
1698 | strip_spaces(bkpinfo->nfs_mount); |
---|
1699 | */ |
---|
1700 | if (bkpinfo->nfs_mount[strlen(bkpinfo->nfs_mount) - 1] == '/') |
---|
1701 | bkpinfo->nfs_mount[strlen(bkpinfo->nfs_mount) - 1] = '\0'; |
---|
1702 | mr_asprintf(&command, "mount | grep \"%s \" | cut -d' ' -f3", |
---|
1703 | bkpinfo->nfs_mount); |
---|
1704 | mr_free(bkpinfo->isodir); |
---|
1705 | bkpinfo->isodir = call_program_and_get_last_line_of_output(command); |
---|
1706 | mr_free(command); |
---|
1707 | |
---|
1708 | mr_asprintf(&comment, |
---|
1709 | _("How much data (in Megabytes) will each media store?")); |
---|
1710 | if (!popup_and_get_string(_("Size"), comment, sz_size)) { |
---|
1711 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1712 | finish(1); |
---|
1713 | } |
---|
1714 | for (i = 0; i <= MAX_NOOF_MEDIA; i++) { |
---|
1715 | bkpinfo->media_size[i] = atoi(sz_size); |
---|
1716 | } |
---|
1717 | if (bkpinfo->media_size[0] <= 0) { |
---|
1718 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1719 | finish(1); |
---|
1720 | } |
---|
1721 | mr_free(comment); |
---|
1722 | } |
---|
1723 | if (bkpinfo->disaster_recovery) { |
---|
1724 | system("umount /tmp/isodir 2> /dev/null"); |
---|
1725 | if (!popup_and_get_string |
---|
1726 | (_("NFS share"), _("Which remote NFS share should I mount?"), |
---|
1727 | bkpinfo->nfs_mount)) { |
---|
1728 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1729 | finish(1); |
---|
1730 | } |
---|
1731 | } |
---|
1732 | if (!is_this_device_mounted(bkpinfo->nfs_mount)) { |
---|
1733 | mr_free(bkpinfo->isodir); |
---|
1734 | mr_asprintf(&bkpinfo->isodir, "/tmp/isodir.mondo.%d", |
---|
1735 | (int) (random() % 32768)); |
---|
1736 | mr_asprintf(&command, "mkdir -p %s", bkpinfo->isodir); |
---|
1737 | run_program_and_log_output(command, 5); |
---|
1738 | mr_free(command); |
---|
1739 | |
---|
1740 | mr_asprintf(&tmp, "mount -t nfs -o nolock %s %s", bkpinfo->nfs_mount, |
---|
1741 | bkpinfo->isodir); |
---|
1742 | run_program_and_log_output(tmp, 5); |
---|
1743 | mr_free(tmp); |
---|
1744 | mr_asprintf(&g_selfmounted_isodir, bkpinfo->isodir); |
---|
1745 | } |
---|
1746 | if (!is_this_device_mounted(bkpinfo->nfs_mount)) { |
---|
1747 | popup_and_OK |
---|
1748 | (_("Please mount that partition before you try to backup to or restore from it.")); |
---|
1749 | finish(1); |
---|
1750 | } |
---|
1751 | if (!popup_and_get_string |
---|
1752 | (_("Directory"), _("Which directory within that mountpoint?"),bkpinfo->nfs_remote_dir)) { |
---|
1753 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1754 | finish(1); |
---|
1755 | } |
---|
1756 | |
---|
1757 | mr_asprintf(&command, "echo hi > '%s/%s/.dummy.txt'", bkpinfo->isodir, |
---|
1758 | bkpinfo->nfs_remote_dir); |
---|
1759 | while (run_program_and_log_output(command, FALSE)) { |
---|
1760 | mr_asprintf(&prompt, |
---|
1761 | _("Directory '%s' under mountpoint '%s' does not exist or is not writable. You can fix this or change the directory and retry or cancel the backup."), |
---|
1762 | bkpinfo->nfs_remote_dir, bkpinfo->isodir); |
---|
1763 | if (!popup_and_get_string |
---|
1764 | (_("Directory"), prompt, bkpinfo->nfs_remote_dir)) { |
---|
1765 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1766 | finish(1); |
---|
1767 | } |
---|
1768 | mr_free(prompt); |
---|
1769 | |
---|
1770 | mr_free(command); |
---|
1771 | mr_asprintf(&command, "echo hi > %s/%s/.dummy.txt", |
---|
1772 | bkpinfo->isodir, bkpinfo->nfs_remote_dir); |
---|
1773 | } |
---|
1774 | mr_free(command); |
---|
1775 | |
---|
1776 | if (!popup_and_get_string |
---|
1777 | (_("Prefix."), |
---|
1778 | _("Please enter the prefix that will be prepended to your ISO filename. Example: machine1 to obtain machine1-[1-9]*.iso files"), |
---|
1779 | bkpinfo->prefix)) { |
---|
1780 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1781 | finish(1); |
---|
1782 | } |
---|
1783 | log_msg(3, "prefix set to %s", bkpinfo->prefix); |
---|
1784 | |
---|
1785 | for (i = 0; i <= MAX_NOOF_MEDIA; i++) { |
---|
1786 | bkpinfo->media_size[i] = 650; |
---|
1787 | } |
---|
1788 | log_msg(3, "Just set nfs_remote_dir to %s", |
---|
1789 | bkpinfo->nfs_remote_dir); |
---|
1790 | log_msg(3, "isodir is still %s", bkpinfo->isodir); |
---|
1791 | break; |
---|
1792 | |
---|
1793 | case iso: |
---|
1794 | if (!bkpinfo->disaster_recovery) { |
---|
1795 | if (!popup_and_get_string |
---|
1796 | (_("Storage dir."), |
---|
1797 | _("Please enter the full path that contains your ISO images. Example: /mnt/raid0_0"), |
---|
1798 | bkpinfo->isodir)) { |
---|
1799 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1800 | finish(1); |
---|
1801 | } |
---|
1802 | if (archiving_to_media) { |
---|
1803 | if ((bkpinfo->compression_level = |
---|
1804 | which_compression_level()) == -1) { |
---|
1805 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1806 | finish(1); |
---|
1807 | } |
---|
1808 | if (!popup_and_get_string |
---|
1809 | (_("ISO size."), |
---|
1810 | _("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 or DVD's you plan to backup to."), sz_size)) { |
---|
1811 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1812 | finish(1); |
---|
1813 | } |
---|
1814 | for (i = 0; i <= MAX_NOOF_MEDIA; i++) { |
---|
1815 | bkpinfo->media_size[i] = atoi(sz_size); |
---|
1816 | } |
---|
1817 | } else { |
---|
1818 | for (i = 0; i <= MAX_NOOF_MEDIA; i++) { |
---|
1819 | bkpinfo->media_size[i] = 650; |
---|
1820 | } |
---|
1821 | } |
---|
1822 | } |
---|
1823 | if (!popup_and_get_string |
---|
1824 | (_("Prefix."), |
---|
1825 | _("Please enter the prefix that will be prepended to your ISO filename. Example: machine1 to obtain machine1-[1-9]*.iso files"), |
---|
1826 | bkpinfo->prefix)) { |
---|
1827 | log_to_screen("User has chosen not to backup the PC"); |
---|
1828 | finish(1); |
---|
1829 | } |
---|
1830 | log_msg(3, "prefix set to %s", bkpinfo->prefix); |
---|
1831 | break; |
---|
1832 | default: |
---|
1833 | fatal_error |
---|
1834 | ("I, Mojo Jojo, shall defeat those pesky Powerpuff Girls!"); |
---|
1835 | } |
---|
1836 | if (archiving_to_media) { |
---|
1837 | |
---|
1838 | if (bkpinfo->boot_device) { |
---|
1839 | mr_free(bkpinfo->boot_device); |
---|
1840 | } |
---|
1841 | #ifdef __FreeBSD__ |
---|
1842 | bkpinfo->boot_device = call_program_and_get_last_line_of_output |
---|
1843 | ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/\\([0-9]\\).*/\\1/'"); |
---|
1844 | #else |
---|
1845 | bkpinfo->boot_device = call_program_and_get_last_line_of_output |
---|
1846 | ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'"); |
---|
1847 | #endif |
---|
1848 | i = which_boot_loader(bkpinfo->boot_device); |
---|
1849 | if (i == 'U') // unknown |
---|
1850 | { |
---|
1851 | |
---|
1852 | #ifdef __FreeBSD__ |
---|
1853 | if (!popup_and_get_string |
---|
1854 | (_("Boot device"), |
---|
1855 | _("What is your boot device? (e.g. /dev/ad0)"), |
---|
1856 | bkpinfo->boot_device)) { |
---|
1857 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1858 | finish(1); |
---|
1859 | } |
---|
1860 | i = which_boot_loader(bkpinfo->boot_device); |
---|
1861 | #else |
---|
1862 | if (!popup_and_get_string |
---|
1863 | (_("Boot device"), |
---|
1864 | _("What is your boot device? (e.g. /dev/hda)"), |
---|
1865 | bkpinfo->boot_device)) { |
---|
1866 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1867 | finish(1); |
---|
1868 | } |
---|
1869 | if (does_string_exist_in_boot_block |
---|
1870 | (bkpinfo->boot_device, "LILO")) { |
---|
1871 | i = 'L'; |
---|
1872 | } else |
---|
1873 | if (does_string_exist_in_boot_block |
---|
1874 | (bkpinfo->boot_device, "ELILO")) { |
---|
1875 | i = 'E'; |
---|
1876 | } else |
---|
1877 | if (does_string_exist_in_boot_block |
---|
1878 | (bkpinfo->boot_device, "GRUB")) { |
---|
1879 | i = 'G'; |
---|
1880 | } else { |
---|
1881 | i = 'U'; |
---|
1882 | } |
---|
1883 | #endif |
---|
1884 | if (i == 'U') { |
---|
1885 | if (ask_me_yes_or_no |
---|
1886 | (_("Unidentified boot loader. Shall I restore it byte-for-byte at restore time and hope for the best?"))) |
---|
1887 | { |
---|
1888 | i = 'R'; // raw |
---|
1889 | } else { |
---|
1890 | log_to_screen |
---|
1891 | (_("I cannot find your boot loader. Please run mondoarchive with parameters.")); |
---|
1892 | finish(1); |
---|
1893 | } |
---|
1894 | } |
---|
1895 | } |
---|
1896 | bkpinfo->boot_loader = i; |
---|
1897 | mr_asprintf(&bkpinfo->include_paths, "/"); |
---|
1898 | if (!popup_and_get_string |
---|
1899 | (_("Backup paths"), |
---|
1900 | _("Please enter paths which you want me to backup. The default is '/' (i.e. everything)."), |
---|
1901 | bkpinfo->include_paths)) { |
---|
1902 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1903 | finish(1); |
---|
1904 | } |
---|
1905 | |
---|
1906 | tmp = list_of_NFS_mounts_only(); |
---|
1907 | if (strlen(tmp) > 2) { |
---|
1908 | if (bkpinfo->exclude_paths != NULL) { |
---|
1909 | tmp1 = bkpinfo->exclude_paths; |
---|
1910 | mr_asprintf(&bkpinfo->exclude_paths, "%s %s", tmp1, tmp); |
---|
1911 | mr_free(tmp1); |
---|
1912 | } else { |
---|
1913 | bkpinfo->exclude_paths = tmp; |
---|
1914 | } |
---|
1915 | } |
---|
1916 | mr_free(tmp); |
---|
1917 | // NTFS |
---|
1918 | tmp = call_program_and_get_last_line_of_output |
---|
1919 | ("parted2fdisk -l | grep -i ntfs | awk '{ print $1};' | tr -s '\\n' ' ' | awk '{ print $0};'"); |
---|
1920 | if (strlen(tmp) > 2) { |
---|
1921 | if (!popup_and_get_string |
---|
1922 | (_("NTFS partitions"), |
---|
1923 | _("Please enter/confirm the NTFS partitions you wish to backup as well."), |
---|
1924 | tmp1)) { |
---|
1925 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1926 | finish(1); |
---|
1927 | } |
---|
1928 | if (bkpinfo->image_devs != NULL) { |
---|
1929 | tmp2 = bkpinfo->image_devs; |
---|
1930 | mr_asprintf(&bkpinfo->image_devs, "%s %s", tmp2, tmp1); |
---|
1931 | mr_free(tmp2); |
---|
1932 | } else { |
---|
1933 | bkpinfo->image_devs = tmp1; |
---|
1934 | } |
---|
1935 | mr_free(tmp1); |
---|
1936 | } |
---|
1937 | mr_free(tmp); |
---|
1938 | |
---|
1939 | if (!popup_and_get_string |
---|
1940 | (_("Exclude paths"), |
---|
1941 | _("Please enter paths which you do NOT want to backup. Separate them with spaces. NB: /tmp and /proc are always excluded. :-) Just hit 'Enter' if you want to do a full system backup."), |
---|
1942 | bkpinfo->exclude_paths)) { |
---|
1943 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1944 | finish(1); |
---|
1945 | } |
---|
1946 | bkpinfo->make_cd_use_lilo = FALSE; |
---|
1947 | bkpinfo->backup_data = TRUE; |
---|
1948 | bkpinfo->verify_data = |
---|
1949 | ask_me_yes_or_no |
---|
1950 | (_("Will you want to verify your backups after Mondo has created them?")); |
---|
1951 | |
---|
1952 | #ifndef __FreeBSD__ |
---|
1953 | if (!ask_me_yes_or_no |
---|
1954 | (_("Are you confident that your kernel is a sane, sensible, standard Linux kernel? Say 'no' if you are using a Gentoo <1.4 or Debian <3.0, please."))){ |
---|
1955 | mr_allocstr(bkpinfo->kernel_path, "FAILSAFE"); |
---|
1956 | } |
---|
1957 | #endif |
---|
1958 | |
---|
1959 | if (!ask_me_yes_or_no |
---|
1960 | (_("Are you sure you want to proceed? Hit 'no' to abort."))) { |
---|
1961 | log_to_screen(_("User has chosen not to backup the PC")); |
---|
1962 | finish(1); |
---|
1963 | } |
---|
1964 | } else { |
---|
1965 | bkpinfo->restore_data = TRUE; // probably... |
---|
1966 | } |
---|
1967 | |
---|
1968 | if (bkpinfo->backup_media_type == iso |
---|
1969 | || bkpinfo->backup_media_type == nfs) { |
---|
1970 | g_ISO_restore_mode = TRUE; |
---|
1971 | } |
---|
1972 | #ifdef __FreeSD__ |
---|
1973 | // skip |
---|
1974 | #else |
---|
1975 | if (bkpinfo->backup_media_type == nfs) { |
---|
1976 | log_msg(3, "I think the NFS mount is mounted at %s", |
---|
1977 | bkpinfo->isodir); |
---|
1978 | } |
---|
1979 | log_it("isodir = %s", bkpinfo->isodir); |
---|
1980 | log_it("nfs_mount = '%s'", bkpinfo->nfs_mount); |
---|
1981 | #endif |
---|
1982 | |
---|
1983 | log_it("media device = %s", bkpinfo->media_device); |
---|
1984 | log_it("media size = %ld", bkpinfo->media_size[1]); |
---|
1985 | log_it("media type = %s", bkpinfo->backup_media_string); |
---|
1986 | mr_free(tmp); |
---|
1987 | log_it("prefix = %s", bkpinfo->prefix); |
---|
1988 | log_it("compression = %ld", bkpinfo->compression_level); |
---|
1989 | log_it("include_paths = '%s'", bkpinfo->include_paths); |
---|
1990 | log_it("exclude_paths = '%s'", bkpinfo->exclude_paths); |
---|
1991 | log_it("scratchdir = '%s'", bkpinfo->scratchdir); |
---|
1992 | log_it("tmpdir = '%s'", bkpinfo->tmpdir); |
---|
1993 | log_it("boot_device = '%s' (loader=%c)", bkpinfo->boot_device, |
---|
1994 | bkpinfo->boot_loader); |
---|
1995 | if (bkpinfo->media_size[0] < 0) { |
---|
1996 | if (archiving_to_media) { |
---|
1997 | fatal_error("Media size is less than zero."); |
---|
1998 | } else { |
---|
1999 | log_msg(2, "Warning - media size is less than zero."); |
---|
2000 | bkpinfo->media_size[0] = 0; |
---|
2001 | } |
---|
2002 | } |
---|
2003 | return (0); |
---|
2004 | } |
---|
2005 | |
---|
2006 | |
---|
2007 | /** |
---|
2008 | * Get a space-separated list of NFS mounts. |
---|
2009 | * @return The list created. |
---|
2010 | * @note The return value should be freed by caller |
---|
2011 | * @bug Even though we only want the mounts, the devices are still checked. |
---|
2012 | */ |
---|
2013 | char *list_of_NFS_mounts_only(void) |
---|
2014 | { |
---|
2015 | return(call_program_and_get_last_line_of_output |
---|
2016 | ("mount -t coda,ncpfs,nfs,smbfs,cifs,afs,mvfs | tr -s '\t' ' ' | cut -d' ' -f3 | tr -s '\n' ' ' | awk '{print $0;}'")); |
---|
2017 | /* BERLIOS : Useless |
---|
2018 | mr_asprintf(&exclude_these_devices, |
---|
2019 | call_program_and_get_last_line_of_output |
---|
2020 | ("tr -s '\t' ' ' < /etc/fstab | grep -E '( (coda|ncpfs|nfs|smbfs|cifs|mvfs) )' | cut -d' ' -f1 | tr -s '\n' ' ' | awk '{print $0;}'")); |
---|
2021 | */ |
---|
2022 | } |
---|
2023 | |
---|
2024 | |
---|
2025 | /* @} - end of utilityGroup */ |
---|
2026 | |
---|
2027 | /** |
---|
2028 | * Set the tmpdir and scratchdir to reside on the partition with the most free space. |
---|
2029 | * Automatically excludes DOS, NTFS, SMB, and NFS filesystems. |
---|
2030 | * @param bkpinfo The backup information structure. @c bkpinfo->tmpdir and @c bkpinfo->scratchdir will be set. |
---|
2031 | * @ingroup utilityGroup |
---|
2032 | */ |
---|
2033 | void sensibly_set_tmpdir_and_scratchdir(struct s_bkpinfo *bkpinfo) |
---|
2034 | { |
---|
2035 | char *tmp = NULL; |
---|
2036 | char *command = NULL; |
---|
2037 | char *sz = NULL; |
---|
2038 | int i = 0; |
---|
2039 | |
---|
2040 | assert(bkpinfo != NULL); |
---|
2041 | |
---|
2042 | #ifdef __FreeBSD__ |
---|
2043 | tmp = call_program_and_get_last_line_of_output |
---|
2044 | ("df -m -P -t nonfs,msdosfs,ntfs,smbfs,smb,cifs,afs,mvfs | tr -s '\t' ' ' | grep -vE \"none|Filesystem\" | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'"); |
---|
2045 | #else |
---|
2046 | tmp = call_program_and_get_last_line_of_output |
---|
2047 | ("df -m -P -x nfs -x vfat -x ntfs -x smbfs -x smb -x cifs -x afs -x mvfs | sed 's/ /devdev/' | tr -s '\t' ' ' | grep -vE \"none|Filesystem|/dev/shm\" | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'"); |
---|
2048 | #endif |
---|
2049 | |
---|
2050 | if (tmp[0] != '/') { |
---|
2051 | mr_asprintf(&sz, "/%s", tmp); |
---|
2052 | mr_free(tmp); |
---|
2053 | tmp = sz; |
---|
2054 | } |
---|
2055 | if (!tmp[0]) { |
---|
2056 | fatal_error("I couldn't figure out the tempdir!"); |
---|
2057 | } |
---|
2058 | i = (int) (random() % 32768); |
---|
2059 | mr_free(bkpinfo->tmpdir); |
---|
2060 | mr_asprintf(&bkpinfo->tmpdir, "%s/mondo.tmp.%d", tmp, i); |
---|
2061 | log_it("bkpinfo->tmpdir is being set to %s", bkpinfo->tmpdir); |
---|
2062 | |
---|
2063 | mr_free(bkpinfo->scratchdir); |
---|
2064 | mr_asprintf(&bkpinfo->scratchdir, "%s/mondo.scratch.%d", tmp, i); |
---|
2065 | log_it("bkpinfo->scratchdir is being set to %s", bkpinfo->scratchdir); |
---|
2066 | |
---|
2067 | mr_asprintf(&command, "rm -Rf %s/mondo.tmp.* %s/mondo.scratch.*", tmp, tmp); |
---|
2068 | mr_free(tmp); |
---|
2069 | |
---|
2070 | paranoid_system(command); |
---|
2071 | mr_free(command); |
---|
2072 | } |
---|
2073 | |
---|
2074 | |
---|
2075 | /** |
---|
2076 | * @addtogroup deviceGroup |
---|
2077 | * @{ |
---|
2078 | */ |
---|
2079 | /** |
---|
2080 | * If we can read @p dev, set @p output to it. |
---|
2081 | * If @p dev cannot be read, set @p output to "". |
---|
2082 | * @param dev The device to check for. |
---|
2083 | * @param output Set to @p dev if @p dev exists, NULL otherwise. Needs to be freed by caller |
---|
2084 | * @return TRUE if @p dev exists, FALSE if it doesn't. |
---|
2085 | */ |
---|
2086 | bool set_dev_to_this_if_rx_OK(char *output, char *dev) |
---|
2087 | { |
---|
2088 | char *command; |
---|
2089 | |
---|
2090 | mr_free(output); |
---|
2091 | if (!dev || dev[0] == '\0') { |
---|
2092 | return (FALSE); |
---|
2093 | } |
---|
2094 | log_msg(10, "Injecting %s", dev); |
---|
2095 | inject_device(dev); |
---|
2096 | if (!does_file_exist(dev)) { |
---|
2097 | log_msg(10, "%s doesn't exist. Returning FALSE.", dev); |
---|
2098 | return (FALSE); |
---|
2099 | } |
---|
2100 | mr_asprintf(&command, "dd bs=%ld count=1 if=%s of=/dev/null &> /dev/null", |
---|
2101 | 512L, dev); |
---|
2102 | if (!run_program_and_log_output(command, FALSE) |
---|
2103 | && !run_program_and_log_output(command, FALSE)) { |
---|
2104 | mr_asprintf(&output, dev); |
---|
2105 | log_msg(4, "Found it - %s", dev); |
---|
2106 | mr_free(command); |
---|
2107 | return (TRUE); |
---|
2108 | } else { |
---|
2109 | log_msg(4, "It's not %s", dev); |
---|
2110 | mr_free(command); |
---|
2111 | return (FALSE); |
---|
2112 | } |
---|
2113 | } |
---|
2114 | |
---|
2115 | |
---|
2116 | /** |
---|
2117 | * Find out what number CD is in the drive. |
---|
2118 | * @param bkpinfo The backup information structure. The @c bkpinfo->media_device field is the only one used. |
---|
2119 | * @return The current CD number, or -1 if it could not be found. |
---|
2120 | * @note If the CD is not mounted, it will be mounted |
---|
2121 | * (and remain mounted after this function returns). |
---|
2122 | */ |
---|
2123 | int what_number_cd_is_this(struct s_bkpinfo *bkpinfo) |
---|
2124 | { |
---|
2125 | int cd_number = -1; |
---|
2126 | char *mountdev = NULL; |
---|
2127 | char *tmp = NULL; |
---|
2128 | char *tmp1 = NULL; |
---|
2129 | |
---|
2130 | assert(bkpinfo != NULL); |
---|
2131 | if (g_ISO_restore_mode) { |
---|
2132 | mr_asprintf(&tmp, "mount | grep iso9660 | awk '{print $3;}'"); |
---|
2133 | |
---|
2134 | tmp1 = call_program_and_get_last_line_of_output(tmp); |
---|
2135 | mr_free(tmp); |
---|
2136 | |
---|
2137 | mr_asprintf(&mountdev, "%s/archives/THIS-CD-NUMBER", tmp1); |
---|
2138 | mr_free(tmp1); |
---|
2139 | |
---|
2140 | tmp = last_line_of_file(mountdev); |
---|
2141 | cd_number = atoi(tmp); |
---|
2142 | mr_free(mountdev); |
---|
2143 | mr_free(tmp); |
---|
2144 | |
---|
2145 | return (cd_number); |
---|
2146 | } |
---|
2147 | |
---|
2148 | if (bkpinfo->media_device == NULL) { |
---|
2149 | log_it |
---|
2150 | ("(what_number_cd_is_this) Warning - media_device unknown. Finding out..."); |
---|
2151 | bkpinfo->media_device = find_cdrom_device(FALSE); |
---|
2152 | } |
---|
2153 | if (!is_this_device_mounted(MNT_CDROM)) { |
---|
2154 | (void)mount_CDROM_here(bkpinfo->media_device, MNT_CDROM); |
---|
2155 | } |
---|
2156 | tmp = last_line_of_file(MNT_CDROM "/archives/THIS-CD-NUMBER"); |
---|
2157 | cd_number = atoi(tmp); |
---|
2158 | mr_free(tmp); |
---|
2159 | return (cd_number); |
---|
2160 | } |
---|
2161 | |
---|
2162 | |
---|
2163 | /** |
---|
2164 | * Find out what device is mounted as root (/). |
---|
2165 | * @return Root device. |
---|
2166 | * @note The returned string points to static storage and will be overwritten with every call. |
---|
2167 | * @bug A bit of a misnomer; it's actually finding out the root device. |
---|
2168 | * The mountpoint (where it's mounted) will obviously be '/'. |
---|
2169 | */ |
---|
2170 | char *where_is_root_mounted() |
---|
2171 | { |
---|
2172 | char *tmp; |
---|
2173 | |
---|
2174 | #ifdef __FreeBSD__ |
---|
2175 | tmp = call_program_and_get_last_line_of_output |
---|
2176 | ("mount | grep \" on / \" | cut -d' ' -f1"); |
---|
2177 | #else |
---|
2178 | tmp = call_program_and_get_last_line_of_output |
---|
2179 | ("mount | grep \" on / \" | cut -d' ' -f1 | sed s/[0-9]// | sed s/[0-9]//"); |
---|
2180 | if (strstr(tmp, "/dev/cciss/")) { |
---|
2181 | mr_free(tmp); |
---|
2182 | tmp = call_program_and_get_last_line_of_output |
---|
2183 | ("mount | grep \" on / \" | cut -d' ' -f1 | cut -dp -f1"); |
---|
2184 | } |
---|
2185 | if (strstr(tmp, "/dev/md")) { |
---|
2186 | mr_free(tmp); |
---|
2187 | tmp = call_program_and_get_last_line_of_output |
---|
2188 | ("mount | grep \" on / \" | cut -d' ' -f1"); |
---|
2189 | } |
---|
2190 | #endif |
---|
2191 | |
---|
2192 | return (tmp); |
---|
2193 | } |
---|
2194 | |
---|
2195 | |
---|
2196 | /** |
---|
2197 | * Find out which boot loader is in use. |
---|
2198 | * @param which_device Device to look for the boot loader on. |
---|
2199 | * @return 'L' for LILO, 'E'for ELILO, 'G' for GRUB, 'B' or 'D' for FreeBSD boot loaders, or 'U' for Unknown. |
---|
2200 | * @note Under Linux, all drives are examined, not just @p which_device. |
---|
2201 | */ |
---|
2202 | #ifdef __FreeBSD__ |
---|
2203 | char which_boot_loader(char *which_device) |
---|
2204 | { |
---|
2205 | int count_lilos = 0; |
---|
2206 | int count_grubs = 0; |
---|
2207 | int count_boot0s = 0; |
---|
2208 | int count_dangerouslydedicated = 0; |
---|
2209 | |
---|
2210 | log_it("looking at drive %s's MBR", which_device); |
---|
2211 | if (does_string_exist_in_boot_block(which_device, "GRUB")) { |
---|
2212 | count_grubs++; |
---|
2213 | } |
---|
2214 | if (does_string_exist_in_boot_block(which_device, "LILO")) { |
---|
2215 | count_lilos++; |
---|
2216 | } |
---|
2217 | if (does_string_exist_in_boot_block(which_device, "Drive")) { |
---|
2218 | count_boot0s++; |
---|
2219 | } |
---|
2220 | if (does_string_exist_in_first_N_blocks |
---|
2221 | (which_device, "FreeBSD/i386", 17)) { |
---|
2222 | count_dangerouslydedicated++; |
---|
2223 | } |
---|
2224 | log_it("%d grubs and %d lilos and %d elilos and %d boot0s and %d DD\n", |
---|
2225 | count_grubs, count_lilos, count_elilos, count_boot0s, |
---|
2226 | count_dangerouslydedicated); |
---|
2227 | |
---|
2228 | if (count_grubs && !count_lilos) { |
---|
2229 | return ('G'); |
---|
2230 | } else if (count_lilos && !count_grubs) { |
---|
2231 | return ('L'); |
---|
2232 | } else if (count_grubs == 1 && count_lilos == 1) { |
---|
2233 | log_it("I'll bet you used to use LILO but switched to GRUB..."); |
---|
2234 | return ('G'); |
---|
2235 | } else if (count_boot0s == 1) { |
---|
2236 | return ('B'); |
---|
2237 | } else if (count_dangerouslydedicated) { |
---|
2238 | return ('D'); |
---|
2239 | } else { |
---|
2240 | log_it("Unknown boot loader"); |
---|
2241 | return ('U'); |
---|
2242 | } |
---|
2243 | } |
---|
2244 | |
---|
2245 | #else |
---|
2246 | |
---|
2247 | char which_boot_loader(char *which_device) |
---|
2248 | /* which_device is allocated here and needs to be freed by caller */ |
---|
2249 | { |
---|
2250 | /*@ buffer ***************************************************** */ |
---|
2251 | char *list_drives_cmd; |
---|
2252 | char *current_drive = NULL; |
---|
2253 | |
---|
2254 | /*@ pointers *************************************************** */ |
---|
2255 | FILE *pdrives; |
---|
2256 | |
---|
2257 | /*@ int ******************************************************** */ |
---|
2258 | int count_lilos = 0; |
---|
2259 | int count_grubs = 0; |
---|
2260 | size_t n = 0; |
---|
2261 | |
---|
2262 | /*@ end vars *************************************************** */ |
---|
2263 | |
---|
2264 | #ifdef __IA64__ |
---|
2265 | /* No choice for it */ |
---|
2266 | return ('E'); |
---|
2267 | #endif |
---|
2268 | assert(which_device != NULL); |
---|
2269 | mr_asprintf(&list_drives_cmd, |
---|
2270 | "parted2fdisk -l 2>/dev/null | grep \"/dev/.*:\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/; echo %s", |
---|
2271 | where_is_root_mounted()); |
---|
2272 | log_it("list_drives_cmd = %s", list_drives_cmd); |
---|
2273 | |
---|
2274 | if (!(pdrives = popen(list_drives_cmd, "r"))) { |
---|
2275 | log_OS_error("Unable to open list of drives"); |
---|
2276 | mr_free(list_drives_cmd); |
---|
2277 | return ('\0'); |
---|
2278 | } |
---|
2279 | mr_free(list_drives_cmd); |
---|
2280 | |
---|
2281 | for (mr_getline(¤t_drive, &n, pdrives); !feof(pdrives); |
---|
2282 | mr_getline(¤t_drive, &n, pdrives)) { |
---|
2283 | strip_spaces(current_drive); |
---|
2284 | log_it("looking at drive %s's MBR", current_drive); |
---|
2285 | if (does_string_exist_in_boot_block(current_drive, "GRUB")) { |
---|
2286 | count_grubs++; |
---|
2287 | mr_allocstr(which_device,current_drive); |
---|
2288 | break; |
---|
2289 | } |
---|
2290 | if (does_string_exist_in_boot_block(current_drive, "LILO")) { |
---|
2291 | count_lilos++; |
---|
2292 | mr_allocstr(which_device,current_drive); |
---|
2293 | break; |
---|
2294 | } |
---|
2295 | } |
---|
2296 | |
---|
2297 | if (pclose(pdrives)) { |
---|
2298 | log_OS_error("Cannot pclose pdrives"); |
---|
2299 | } |
---|
2300 | log_it("%d grubs and %d lilos\n", count_grubs, count_lilos); |
---|
2301 | if (count_grubs && !count_lilos) { |
---|
2302 | return ('G'); |
---|
2303 | } else if (count_lilos && !count_grubs) { |
---|
2304 | return ('L'); |
---|
2305 | } else if (count_grubs == 1 && count_lilos == 1) { |
---|
2306 | log_it("I'll bet you used to use LILO but switched to GRUB..."); |
---|
2307 | return ('G'); |
---|
2308 | } else { |
---|
2309 | log_it("Unknown boot loader"); |
---|
2310 | return ('U'); |
---|
2311 | } |
---|
2312 | } |
---|
2313 | #endif |
---|
2314 | |
---|
2315 | |
---|
2316 | /** |
---|
2317 | * Write zeroes over the first 16K of @p device. |
---|
2318 | * @param device The device to zero. |
---|
2319 | * @return 0 for success, 1 for failure. |
---|
2320 | */ |
---|
2321 | int zero_out_a_device(char *device) |
---|
2322 | { |
---|
2323 | FILE *fout; |
---|
2324 | int i; |
---|
2325 | |
---|
2326 | assert_string_is_neither_NULL_nor_zerolength(device); |
---|
2327 | |
---|
2328 | log_it("Zeroing drive %s", device); |
---|
2329 | if (!(fout = fopen(device, "w"))) { |
---|
2330 | log_OS_error("Unable to open/write to device"); |
---|
2331 | return (1); |
---|
2332 | } |
---|
2333 | for (i = 0; i < 16384; i++) { |
---|
2334 | fputc('\0', fout); |
---|
2335 | } |
---|
2336 | paranoid_fclose(fout); |
---|
2337 | log_it("Device successfully zeroed."); |
---|
2338 | return (0); |
---|
2339 | } |
---|
2340 | |
---|
2341 | |
---|
2342 | /** |
---|
2343 | * Return the device pointed to by @p incoming. |
---|
2344 | * @param incoming The device to resolve symlinks for. |
---|
2345 | * @return The path to the real device file. |
---|
2346 | * @note The returned string points to a storage that need to be freed by the caller |
---|
2347 | * @bug Won't work with file v4.0; needs to be written in C. |
---|
2348 | */ |
---|
2349 | char *resolve_softlinks_to_get_to_actual_device_file(char *incoming) |
---|
2350 | { |
---|
2351 | char *output = NULL; |
---|
2352 | char *command = NULL; |
---|
2353 | char *curr_fname = NULL; |
---|
2354 | char *scratch = NULL; |
---|
2355 | char *tmp = NULL; |
---|
2356 | char *p = NULL; |
---|
2357 | |
---|
2358 | struct stat statbuf; |
---|
2359 | if (!does_file_exist(incoming)) { |
---|
2360 | log_it |
---|
2361 | ("resolve_softlinks_to_get_to_actual_device_file --- device not found"); |
---|
2362 | mr_asprintf(&output, incoming); |
---|
2363 | } else { |
---|
2364 | mr_asprintf(&curr_fname, incoming); |
---|
2365 | lstat(curr_fname, &statbuf); |
---|
2366 | while (S_ISLNK(statbuf.st_mode)) { |
---|
2367 | log_msg(1, "curr_fname = %s", curr_fname); |
---|
2368 | mr_asprintf(&command, "file %s", curr_fname); |
---|
2369 | tmp = call_program_and_get_last_line_of_output(command); |
---|
2370 | mr_free(command); |
---|
2371 | |
---|
2372 | for (p = tmp + strlen(tmp); p != tmp && *p != '`' && *p != ' '; |
---|
2373 | p--); |
---|
2374 | p++; |
---|
2375 | mr_asprintf(&scratch, p); |
---|
2376 | for (p = scratch; *p != '\0' && *p != '\''; p++); |
---|
2377 | *p = '\0'; |
---|
2378 | log_msg(0, "curr_fname %s --> '%s' --> %s", curr_fname, tmp, scratch); |
---|
2379 | mr_free(tmp); |
---|
2380 | |
---|
2381 | if (scratch[0] == '/') { |
---|
2382 | mr_free(curr_fname); |
---|
2383 | mr_asprintf(&curr_fname, scratch); // copy whole thing because it's an absolute softlink |
---|
2384 | } else { // copy over the basename cos it's a relative softlink |
---|
2385 | p = curr_fname + strlen(curr_fname); |
---|
2386 | while (p != curr_fname && *p != '/') { |
---|
2387 | p--; |
---|
2388 | } |
---|
2389 | if (*p == '/') { |
---|
2390 | p++; |
---|
2391 | } |
---|
2392 | *p = '\0'; |
---|
2393 | mr_asprintf(&tmp, "%s%s", curr_fname, scratch); |
---|
2394 | mr_free(curr_fname); |
---|
2395 | curr_fname = tmp; |
---|
2396 | } |
---|
2397 | mr_free(scratch); |
---|
2398 | lstat(curr_fname, &statbuf); |
---|
2399 | } |
---|
2400 | mr_asprintf(&output, curr_fname); |
---|
2401 | log_it("resolved %s to %s", incoming, output); |
---|
2402 | mr_free(curr_fname); |
---|
2403 | } |
---|
2404 | return (output); |
---|
2405 | } |
---|
2406 | |
---|
2407 | /* @} - end of deviceGroup */ |
---|
2408 | |
---|
2409 | |
---|
2410 | /** |
---|
2411 | * Return the type of partition format (GPT or MBR) |
---|
2412 | * Caller needs to free the return value |
---|
2413 | */ |
---|
2414 | char *which_partition_format(const char *drive) |
---|
2415 | { |
---|
2416 | char *output = NULL; |
---|
2417 | char *tmp = NULL; |
---|
2418 | char *command = NULL; |
---|
2419 | char *fdisk = NULL; |
---|
2420 | |
---|
2421 | log_msg(0, "Looking for partition table format type"); |
---|
2422 | mr_asprintf(&fdisk, "/sbin/parted2fdisk"); |
---|
2423 | log_msg(1, "Using %s", fdisk); |
---|
2424 | mr_asprintf(&command, "%s -l %s | grep 'EFI GPT'", fdisk, drive); |
---|
2425 | mr_free(fdisk); |
---|
2426 | |
---|
2427 | tmp = call_program_and_get_last_line_of_output(command); |
---|
2428 | mr_free(command); |
---|
2429 | |
---|
2430 | if (strstr(tmp, "GPT") == NULL) { |
---|
2431 | mr_asprintf(&output, "MBR"); |
---|
2432 | } else { |
---|
2433 | mr_asprintf(&output, "GPT"); |
---|
2434 | } |
---|
2435 | mr_free(tmp); |
---|
2436 | log_msg(0, "Found %s partition table format type", output); |
---|
2437 | return (output); |
---|
2438 | } |
---|
2439 | |
---|
2440 | /* @} - end of deviceGroup */ |
---|