source: MondoRescue/branches/stable/mondo/src/mondorestore/mondo-prep.c@ 1264

Last change on this file since 1264 was 1264, checked in by Bruno Cornec, 17 years ago
  • mr_exit used instead of exit
  • mr_rs_clean_conf/mr_rs_cleanup/mr_ar_clean_conf/mr_ar_cleanup added
  • compiltaion warnings suppressed
  • Addition of mr_types (for boolean with typedef)
  • struct mr_rs_conf added
  • help_screen removed (useless)
  • Property svn:keywords set to Id
File size: 69.9 KB
Line 
1/***************************************************************************
2 * $Id: mondo-prep.c 1264 2007-03-24 00:48:55Z bruno $
3*/
4
5/**
6 * @file
7 * Functions for prepping hard drives: partitioning, formatting, etc.
8 */
9
10
11#include "my-stuff.h"
12#include "mondostructures.h"
13#include "mondoprep.h"
14#include "libmondo.h"
15#include "mondo-rstr-tools-EXT.h"
16
17#include <sys/ioctl.h>
18#include <linux/hdreg.h>
19#include <math.h>
20#include <unistd.h>
21#include "mr_mem.h"
22#include "mr_msg.h"
23
24
25#define FDISK_LOG "/tmp/fdisk.log"
26
27#ifdef __FreeBSD__
28#define DKTYPENAMES
29#define FSTYPENAMES
30#include <sys/disklabel.h>
31#include <sys/disk.h>
32#include <err.h>
33#include <libgen.h>
34#define OSSWAP(x,y) y
35#else
36#define OSSWAP(x,y) x
37#endif
38
39#define ARCHIVES_PATH MNT_CDROM"/archives"
40#define MONDO_WAS_HERE "MONDOWOZEREMONDOWOZEREMONDOWOZEREhahahaMOJOJOJO"
41
42//static char cvsid[] = "$Id: mondo-prep.c 1264 2007-03-24 00:48:55Z bruno $";
43
44extern char *g_mountlist_fname;
45extern long g_current_progress, g_maximum_progress;
46extern bool g_text_mode;
47extern void pause_for_N_seconds(int, char *);
48
49FILE *g_fprep = NULL;
50int g_partition_table_locked_up = 0;
51
52
53void wipe_MBRs_and_reboot_if_necessary(struct mountlist_itself *mountlist)
54{
55 char *command = NULL;
56 int lino = 0;
57 int res = 0;
58 FILE *fout = NULL;
59 char *buf = NULL;
60 struct list_of_disks *drivelist = NULL;
61
62 // If LVMs are present and a zero-and-reboot wasn't recently undertaken
63 // then zero & insist on reboot.
64 if (does_file_exist("/tmp/i-want-my-lvm")) // FIXME - cheating :)
65 {
66 drivelist = mr_malloc(sizeof(struct list_of_disks));
67 make_list_of_drives_in_mountlist(mountlist, drivelist);
68 for (lino = 0; lino < drivelist->entries; lino++) {
69 mr_asprintf(&command,
70 "dd if=%s bs=512 count=1 2> /dev/null | grep \"%s\"",
71 drivelist->el[lino].device, MONDO_WAS_HERE);
72 res = run_program_and_log_output(command, 1);
73 mr_free(command);
74 if (!res) {
75 mr_msg(1, "Found MONDO_WAS_HERE marker on drive#%d (%s)",
76 lino, drivelist->el[lino].device);
77 break;
78 }
79 }
80
81 if (lino == drivelist->entries) {
82 // zero & reboot
83 log_to_screen
84 (_
85 ("I am sorry for the inconvenience but I must ask you to reboot."));
86 log_to_screen(_
87 ("I need to reset the Master Boot Record; in order to be"));
88 log_to_screen(_
89 ("sure the kernel notices, I must reboot after doing it."));
90 log_to_screen("Please hit 'Enter' to reboot.");
91 for (lino = 0; lino < drivelist->entries; lino++) {
92 mr_asprintf(&buf, "%s\n", MONDO_WAS_HERE);
93 fout = fopen(drivelist->el[lino].device, "w+");
94 if (!fout) {
95 mr_msg(1, "Unable to open+wipe %s",
96 drivelist->el[lino].device);
97 } else {
98 if (1 != fwrite(buf, strlen(buf)+1, 1, fout)) {
99 mr_msg(1, "Failed to wipe %s",
100 drivelist->el[lino].device);
101 } else {
102 mr_msg(1, "Successfully wiped %s",
103 drivelist->el[lino].device);
104 }
105 fclose(fout);
106 }
107 mr_free(buf);
108 }
109 sync();
110 sync();
111 sync();
112 popup_and_OK
113 (_
114 ("I must reboot now. Please leave the boot media in the drive and repeat your actions - e.g. type 'nuke' - and it should work fine."));
115 system("reboot");
116 }
117 }
118// Still here? Cool!
119 mr_msg(1, "Cool. I didn't have to wipe anything.");
120}
121
122
123int fput_string_one_char_at_a_time(FILE * fout, char *str)
124{
125 int i = 0, j = 0;
126 FILE *fq = NULL;
127
128 if (ferror(fout)) {
129 return (-1);
130 }
131 mr_msg(5, "Writing string '%s', one char at a time", str);
132 j = strlen(str);
133 for (i = 0; i < j; i++) {
134 mr_msg(6, "Writing %d ('%c')", str[i], str[i]);
135 if ((fq = fopen(FDISK_LOG, "a+"))) {
136 fputc(str[i], fq);
137 fclose(fq);
138 }
139 fputc(str[i], fout);
140 fflush(fout);
141 usleep(1000L * 100L);
142 if (str[i] < 32) {
143 usleep(1000L * 10L);
144 }
145 }
146 mr_msg(5, "Returning");
147
148 return (i);
149}
150
151
152/**
153 * @addtogroup prepGroup
154 * @{
155 */
156/**
157 * Execute the commands in /tmp/i-want-my-lvm.
158 * These should probably be commands to set up LVM.
159 * @return The number of errors encountered (0 for success).
160 */
161int do_my_funky_lvm_stuff(bool just_erase_existing_volumes,
162 bool vacuum_pack)
163{
164 char *tmp = NULL;
165 char *tmp1 = NULL;
166 char *incoming = NULL;
167 char *command = NULL;
168 char *lvscan_sz = NULL;
169 char *lvremove_sz = NULL;
170 char *pvscan_sz = NULL;
171 char *vgscan_sz = NULL;
172 char *vgchange_sz = NULL;
173 char *vgremove_sz = NULL;
174 char *p = NULL;
175 char *q = NULL;
176
177 /** int ***************************************************/
178 int retval = 0;
179 int res = 0;
180 int i = 0;
181 int lvmversion = 1;
182 long extents = 0L;
183 fpos_t orig_pos;
184 size_t n = 0;
185 size_t n1 = 0;
186
187 /** pointers **********************************************/
188 FILE *fin = NULL;
189
190 /** end *****************************************************/
191
192#ifdef __FreeBSD__
193 return (0);
194#endif
195
196 if (!(fin = fopen("/tmp/i-want-my-lvm", "r"))) {
197 log_OS_error("/tmp/i-want-my-lvm");
198 return (1);
199 }
200
201 iamhere("STARTING");
202 mr_msg(1, "OK, opened i-want-my-lvm. Shutting down LVM volumes...");
203 if (find_home_of_exe("lvm")) // found it :) cool
204 {
205 mr_asprintf(&lvscan_sz, "lvm lvscan");
206 mr_asprintf(&lvremove_sz, "lvm lvremove");
207 mr_asprintf(&vgscan_sz, "lvm vgscan");
208 mr_asprintf(&pvscan_sz, "lvm pvscan");
209 mr_asprintf(&vgchange_sz, "lvm vgchange");
210 mr_asprintf(&vgremove_sz, "lvm vgremove");
211 } else {
212 mr_asprintf(&lvscan_sz, "lvscan");
213 mr_asprintf(&lvremove_sz, "lvremove");
214 mr_asprintf(&vgscan_sz, "vgscan");
215 mr_asprintf(&pvscan_sz, "pvscan");
216 mr_asprintf(&vgchange_sz, "vgchange");
217 mr_asprintf(&vgremove_sz, "vgremove");
218 }
219 mr_asprintf(&command,
220 "for i in `%s | cut -d\"'\" -f2 | sort -r` ; do echo \"Shutting down lv $i\" >> "
221 MONDO_LOGFILE "; %s -f $i; done", lvscan_sz, lvremove_sz);
222 mr_free(lvscan_sz);
223 mr_free(lvremove_sz);
224
225 run_program_and_log_output(command, 5);
226 mr_free(command);
227
228 sleep(1);
229 mr_asprintf(&command,
230 "for i in `%s | grep -i lvm | cut -d'\"' -f2` ; do %s -a n $i ; %s $i; echo \"Shutting down vg $i\" >> "
231 MONDO_LOGFILE "; done", vgscan_sz, vgchange_sz, vgremove_sz);
232 mr_free(vgchange_sz);
233 mr_free(vgremove_sz);
234
235 run_program_and_log_output(command, 5);
236 mr_free(command);
237
238 if (just_erase_existing_volumes) {
239 paranoid_fclose(fin);
240 mr_msg(1, "Closed i-want-my-lvm. Finished erasing LVMs.");
241 sync();
242 sync();
243 sync();
244 sleep(1);
245 iamhere("ENDING");
246 mr_msg(1, "Not many errors. Returning 0.");
247 return (0);
248 }
249
250 mr_msg(1, "OK, rewound i-want-my-lvm. Doing funky stuff...");
251 rewind(fin);
252 for (mr_getline(&incoming, &n1, fin); !feof(fin); mr_getline(&incoming, &n1, fin)) {
253 fgetpos(fin, &orig_pos);
254 /* we want to execute lines begining with a # */
255 if (incoming[0] != '#') {
256 continue;
257 }
258 if ((p = strstr(incoming, "vgcreate"))) {
259 // include next line(s) if they end in /dev (cos we've got a broken i-want-my-lvm)
260 for (mr_getline(&tmp, &n, fin); !feof(fin); mr_getline(&tmp, &n, fin)) {
261 if (tmp[0] == '#') {
262 fsetpos(fin, &orig_pos);
263 break;
264 } else {
265 fgetpos(fin, &orig_pos);
266 mr_strcat(incoming, tmp);
267 }
268 }
269 mr_free(tmp);
270
271 for (q = incoming; *q != '\0'; q++) {
272 if (*q < 32) {
273 *q = ' ';
274 }
275 }
276 mr_asprintf(&tmp1, p + strlen("vgcreate") + 1);
277 for (q = tmp1; *q > 32; q++);
278 *q = '\0';
279 mr_msg(1, "Deleting old entries at /dev/%s", tmp1);
280 mr_asprintf(&command, "rm -Rf /dev/%s", tmp1);
281 mr_free(tmp1);
282
283 run_program_and_log_output(command, 1);
284 mr_free(command);
285
286 run_program_and_log_output(vgscan_sz, 1);
287 run_program_and_log_output(pvscan_sz, 1);
288 mr_msg(3,
289 "After working around potentially broken i-want-my-lvm, incoming[] is now '%s'",
290 incoming);
291 }
292 for (p = incoming + 1; *p == ' '; p++);
293 mr_asprintf(&command,p);
294 for (p = command; *p != '\0'; p++);
295 for (; (*(p - 1) < 32) && (p > command) ; p--);
296 *p = '\0';
297
298 /* BERLIOS: we should rather define LVMv1 or v2 and use it */
299 res = run_program_and_log_output(command, 5);
300 if (res > 0 && (p = strstr(command, "lvm "))) {
301 *p = *(p + 1) = *(p + 2) = ' ';
302 res = run_program_and_log_output(command, 5);
303 }
304 mr_msg(0, "%s --> %d", command, res);
305 if (res > 0) {
306 res = 1;
307 }
308 if (res && strstr(command, "lvcreate") && vacuum_pack) {
309 res = 0;
310 if (strstr(command, "lvm lvcreate"))
311 lvmversion = 2;
312 if (lvmversion == 2) {
313 mr_asprintf(&tmp, call_program_and_get_last_line_of_output
314 ("tail -n5 " MONDO_LOGFILE " | grep Insufficient | tail -n1"));
315 } else {
316 mr_asprintf(&tmp, call_program_and_get_last_line_of_output
317 ("tail -n5 " MONDO_LOGFILE " | grep lvcreate | tail -n1"));
318 }
319 for (p = tmp; *p != '\0' && !isdigit(*p); p++);
320 extents = atol(p);
321 mr_msg(5, "p='%s' --> extents=%ld", p, extents);
322 mr_free(tmp);
323
324 p = strstr(command, "-L");
325 if (!p) {
326 mr_msg(0, "Fiddlesticks. '%s' returned %d", command, res);
327 } else {
328 if (lvmversion == 2) {
329 *p++ = '-';
330 *p++ = 'l';
331 *p++ = ' ';
332 for (q = p; *q != ' '; q++) {
333 *q = ' ';
334 }
335 /* BERLIOS: Dangerous: no size control !! */
336 sprintf(p, "%ld", extents);
337 i = strlen(p);
338 *(p + i) = ' ';
339 } else {
340 p++;
341 p++;
342 p++;
343 for (q = p; *q != ' '; q++) {
344 *(q - 1) = ' ';
345 }
346 /* BERLIOS: Dangerous: no size control !! */
347 sprintf(p, "%ld%c", extents, 'm');
348 i = strlen(p);
349 *(p + i) = ' ';
350 }
351 mr_msg(5, "Retrying with '%s'", command);
352 res = run_program_and_log_output(command, 5);
353 if (res > 0) {
354 res = 1;
355 }
356 if (g_fprep) {
357 fprintf(g_fprep, "%s\n", command);
358 }
359 mr_msg(0, "%s --> %d", command, res);
360 if (!res) {
361 mr_msg(5, "Yep! This time, it succeeded.");
362 }
363 }
364 }
365 if (strstr(command, "vgcreate")) {
366 mr_msg(0, "In case you're interested...");
367 run_program_and_log_output(vgscan_sz, 1);
368 run_program_and_log_output(pvscan_sz, 1);
369 }
370 if (res != 0 && !strstr(command, "insmod")) {
371 retval++;
372 }
373 mr_asprintf(&tmp, "echo \"%s\" >> /tmp/out.sh", command);
374 system(tmp);
375 mr_free(tmp);
376 sleep(1);
377 }
378 paranoid_fclose(fin);
379 mr_free(vgscan_sz);
380 mr_free(pvscan_sz);
381 mr_free(command);
382 mr_free(incoming);
383
384 mr_msg(1, "Closed i-want-my-lvm. Finished doing funky stuff.");
385 sync();
386 sync();
387 sync();
388 sleep(1);
389 iamhere("ENDING");
390 if (retval > 2) {
391 mr_msg(1, "%d errors. I'm reporting this.", retval);
392 return (retval);
393 } else {
394 mr_msg(1, "Not many errors. Returning 0.");
395 return (0);
396 }
397}
398
399
400/**
401 * Add RAID partitions while copying @p old_mountlist to @p new_mountlist.
402 * We go through @p old_mountlist and check if any RAID device (/dev/md? on Linux)
403 * is in it; if it is, then we put the disks contained within that RAID device
404 * into the mountlist as well.
405 * @param old_mountlist The mountlist to read.
406 * @param new_mountlist The mountlist to write, with the RAID partitions added.
407 * @return 0 for success, nonzero for failure.
408 */
409int extrapolate_mountlist_to_include_raid_partitions(struct mountlist_itself
410 *new_mountlist, struct mountlist_itself
411 *old_mountlist)
412{
413 FILE *fin = NULL;
414 int lino = 0;
415 int j = 0;
416 char *incoming = NULL;
417 char *p = NULL;
418 size_t n = 0;
419
420 /** init *************************************************************/
421 new_mountlist->entries = 0;
422
423 /** end **************************************************************/
424
425 assert(new_mountlist != NULL);
426 assert(old_mountlist != NULL);
427
428#ifdef __FreeBSD__
429 log_to_screen
430 (_
431 ("I don't know how to extrapolate the mountlist on FreeBSD. Sorry."));
432 return (1);
433#endif
434
435 for (lino = 0; lino < old_mountlist->entries; lino++) {
436 if (strstr(old_mountlist->el[lino].device, RAID_DEVICE_STUB)) // raid
437 {
438 if (!does_file_exist("/etc/raidtab")) {
439 log_to_screen
440 (_
441 ("Cannot find /etc/raidtab - cannot extrapolate the fdisk entries"));
442 finish(1);
443 }
444 if (!(fin = fopen("/etc/raidtab", "r"))) {
445 log_OS_error("Cannot open /etc/raidtab");
446 finish(1);
447 }
448 for (mr_getline(&incoming, &n, fin); !feof(fin)
449 && !strstr(incoming, old_mountlist->el[lino].device);
450 mr_getline(&incoming, &n, fin));
451 if (!feof(fin)) {
452 log_it("Investigating %s",
453 old_mountlist->el[lino].device);
454 for (mr_getline(&incoming, &n, fin); !feof(fin)
455 && !strstr(incoming, "raiddev");
456 mr_getline(&incoming, &n, fin)) {
457 if (strstr(incoming, OSSWAP("device", "drive"))
458 && !strchr(incoming, '#')) {
459 for (p = incoming + strlen(incoming);
460 (*(p - 1) <= 32) && (p >= incoming) ; p--);
461 *p = '\0';
462 for (p--; (p >= incoming) && (*(p - 1) > 32); p--);
463 log_it("Extrapolating %s", p);
464 for (j = 0;
465 j < new_mountlist->entries
466 && strcmp(new_mountlist->el[j].device, p);
467 j++);
468 if (j >= new_mountlist->entries) {
469 strcpy(new_mountlist->
470 el[new_mountlist->entries].device, p);
471 strcpy(new_mountlist->
472 el[new_mountlist->entries].mountpoint,
473 "raid");
474 strcpy(new_mountlist->
475 el[new_mountlist->entries].format,
476 "raid");
477 new_mountlist->el[new_mountlist->entries].
478 size = old_mountlist->el[lino].size;
479 new_mountlist->entries++;
480 } else {
481 log_it("Not adding %s to mountlist: it's already there", p);
482 }
483 }
484 }
485 }
486 mr_free(incoming);
487
488 paranoid_fclose(fin);
489 } else {
490 strcpy(new_mountlist->el[new_mountlist->entries].device,
491 old_mountlist->el[lino].device);
492 strcpy(new_mountlist->el[new_mountlist->entries].mountpoint,
493 old_mountlist->el[lino].mountpoint);
494 strcpy(new_mountlist->el[new_mountlist->entries].format,
495 old_mountlist->el[lino].format);
496 new_mountlist->el[new_mountlist->entries].size =
497 old_mountlist->el[lino].size;
498 new_mountlist->entries++;
499 }
500 }
501 return (0);
502}
503
504
505/**
506 * Create @p RAID device using information from @p structure.
507 * This will create the specified RAID devive using information provided in
508 * raidlist by means of the mdadm tool.
509 * @param raidlist The structure containing all RAID information
510 * @param device The RAID device to create.
511 * @return 0 for success, nonzero for failure.
512 */
513int create_raid_device_via_mdadm(struct raidlist_itself *raidlist, char *device)
514{
515 /** int **************************************************************/
516 int i = 0;
517 int j = 0;
518 int res = 0;
519
520 /** buffers ***********************************************************/
521 char *devices = NULL;
522 char *level = NULL;
523 char *program = NULL;
524
525 // leave straight away if raidlist is initial or has no entries
526 if (!raidlist || raidlist->entries == 0) {
527 mr_msg(1, "No RAID arrays found.");
528 return 1;
529 } else {
530 mr_msg(1, "%d RAID arrays found.", raidlist->entries);
531 }
532 // find raidlist entry for requested device
533 for (i = 0; i < raidlist->entries; i++) {
534 if (!strcmp(raidlist->el[i].raid_device, device)) break;
535 }
536 // check whether RAID device was found in raidlist
537 if (i == raidlist->entries) {
538 mr_msg(1, "RAID device %s not found in list.", device);
539 return 1;
540 }
541 // create device list from normal disks followed by spare ones
542 mr_asprintf(&devices, raidlist->el[i].data_disks.el[0].device);
543 for (j = 1; j < raidlist->el[i].data_disks.entries; j++) {
544 mr_strcat(devices, " %s", raidlist->el[i].data_disks.el[j].device);
545 }
546 for (j = 0; j < raidlist->el[i].spare_disks.entries; j++) {
547 mr_strcat(devices, " %s", raidlist->el[i].spare_disks.el[j].device);
548 }
549 // translate RAID level
550 if (raidlist->el[i].raid_level == -2) {
551 mr_asprintf(&level, "multipath");
552 } else if (raidlist->el[i].raid_level == -1) {
553 mr_asprintf(&level, "linear");
554 } else {
555 mr_asprintf(&level, "raid%d", raidlist->el[i].raid_level);
556 }
557 // create RAID device:
558 // - RAID device, number of devices and devices mandatory
559 // - parity algorithm, chunk size and spare devices optional
560 // - faulty devices ignored
561 // - persistent superblock always used as this is recommended
562 mr_asprintf(&program,
563 "mdadm --create --force --run --auto=yes %s --level=%s --raid-devices=%d",
564 raidlist->el[i].raid_device, level,
565 raidlist->el[i].data_disks.entries);
566 mr_free(level);
567 if (raidlist->el[i].parity != -1) {
568 switch(raidlist->el[i].parity) {
569 case 0:
570 mr_strcat(program, " --parity=%s", "la");
571 break;
572 case 1:
573 mr_strcat(program, " --parity=%s", "ra");
574 break;
575 case 2:
576 mr_strcat(program, " --parity=%s", "ls");
577 break;
578 case 3:
579 mr_strcat(program, " --parity=%s", "rs");
580 break;
581 default:
582 fatal_error("Unknown RAID parity algorithm.");
583 break;
584 }
585 }
586 if (raidlist->el[i].chunk_size != -1) {
587 mr_strcat(program, " --chunk=%d", raidlist->el[i].chunk_size);
588 }
589 if (raidlist->el[i].spare_disks.entries > 0) {
590 mr_strcat(program, " --spare-devices=%d", raidlist->el[i].spare_disks.entries);
591 }
592 mr_strcat(program, " %s", devices);
593 res = run_program_and_log_output(program, 1);
594 // free memory
595 mr_free(devices);
596 mr_free(program);
597 // return to calling instance
598 return(res);
599}
600
601
602/**
603 * Format @p device as a @p format filesystem.
604 * This will use the format command returned by which_format_command_do_i_need().
605 * If @p device is an LVM PV, it will not be formatted, and LVM will be started
606 * (if not already done). If it's an imagedev, software RAID component, or
607 * (under BSD) swap partition, no format will be done.
608 * @param device The device to format.
609 * @param format The filesystem type to format it as.
610 * @return 0 for success, nonzero for failure.
611 */
612int format_device(char *device, char *format, struct raidlist_itself *raidlist)
613{
614 /** int **************************************************************/
615 int res = 0;
616 int retval = 0;
617#ifdef __FreeBSD__
618 static bool vinum_started_yet = FALSE;
619#endif
620
621 /** buffers ***********************************************************/
622 char *program = NULL;
623 char *program2 = NULL;
624 char *tmp = NULL;
625#ifdef __FreeBSD__
626 char *line = NULL;
627 char *status = NULL;
628 FILE *pin = NULL;
629 FILE *fin = NULL;
630 size_t n = 0;
631 size_t n1 = 0;
632#endif
633
634 /** end ****************************************************************/
635
636 assert_string_is_neither_NULL_nor_zerolength(device);
637 assert(format != NULL);
638
639 if (strstr(format, "raid")) { // do not form RAID disks; do it to /dev/md* instead
640 log_it("Not formatting %s (it is a RAID disk)", device);
641 return (0);
642 }
643#ifdef __FreeBSD__
644 if (strcmp(format, "swap") == 0) {
645 log_it("Not formatting %s - it's swap", device);
646 return (0);
647 }
648#endif
649 if (strlen(format) <= 2) {
650 mr_asprintf(&tmp,
651 "%s has a really small format type ('%s') - this is probably a hexadecimal string, which would suggest the partition is an image --- I shouldn't format it",
652 device, format);
653 return (0);
654 }
655 if (is_this_device_mounted(device)) {
656 log_to_screen(_("%s is mounted - cannot format it "), device);
657 return (1);
658 }
659 if (strstr(device, RAID_DEVICE_STUB)) {
660 newtSuspend();
661#ifdef __FreeBSD__
662 if (!vinum_started_yet) {
663 if (!does_file_exist("/tmp/raidconf.txt")) {
664 log_to_screen
665 (_
666 ("/tmp/raidconf.txt does not exist. I therefore cannot start Vinum."));
667 } else {
668 int res;
669 res =
670 run_program_and_log_output
671 ("vinum create /tmp/raidconf.txt", TRUE);
672 if (res) {
673 log_to_screen
674 (_
675 ("`vinum create /tmp/raidconf.txt' returned errors. Please fix them and re-run mondorestore."));
676 finish(1);
677 }
678 vinum_started_yet = TRUE;
679 }
680 }
681
682 if (vinum_started_yet) {
683 log_to_screen(_("Initializing Vinum device %s (this may take a *long* time)"),
684 device);
685
686 /* format raid partition */
687 mr_asprintf(&program,
688 "for plex in `vinum lv -r %s | grep '^P' | tr '\t' ' ' | tr -s ' ' | cut -d' ' -f2`; do echo $plex; done > /tmp/plexes",
689 basename(device));
690 system(program);
691
692 if (g_fprep) {
693 fprintf(g_fprep, "%s\n", program);
694 }
695 mr_free(program);
696
697 fin = fopen("/tmp/plexes", "r");
698 while (mr_getline(&line, &n, fin)) {
699 if (strchr(line, '\n'))
700 *(strchr(line, '\n')) = '\0'; // get rid of the \n on the end
701
702 mr_asprintf(&tmp, "Initializing plex: %s", line);
703 open_evalcall_form(tmp);
704 mr_free(tmp);
705
706 mr_asprintf(&tmp, "vinum init %s", line);
707 system(tmp);
708 mr_free(tmp);
709
710 while (1) {
711 mr_asprintf(&tmp,
712 "vinum lp -r %s | grep '^S' | head -1 | tr -s ' ' | cut -d: -f2 | cut -f1 | sed 's/^ //' | sed 's/I //' | sed 's/%%//'",
713 line);
714 pin = popen(tmp, "r");
715 mr_free(tmp);
716
717 mr_getline(&status, &n1, pin);
718 pclose(pin);
719
720 if (!strcmp(status, "up")) {
721 break; /* it's done */
722 }
723 update_evalcall_form(atoi(status));
724 usleep(250000);
725 mr_free(status);
726 }
727 close_evalcall_form();
728 }
729 mr_free(line);
730
731 fclose(fin);
732 unlink("/tmp/plexes");
733 /* retval+=res; */
734 }
735#else
736 log_to_screen(_("Initializing RAID device %s"), device);
737
738 // Shouldn't be necessary.
739 log_to_screen(_("Stopping %s"), device);
740 stop_raid_device(device);
741 sync();
742 sleep(1);
743
744 mr_msg(1, "Making %s", device);
745 // use mkraid if it exists, otherwise use mdadm
746 if (run_program_and_log_output("which mkraid", FALSE)) {
747 res = create_raid_device_via_mdadm(raidlist, device);
748 mr_msg(1, "Creating RAID device %s via mdadm returned %d", device, res);
749 } else {
750 mr_asprintf(&program, "mkraid --really-force %s", device);
751 res = run_program_and_log_output(program, 1);
752 mr_msg(1, "%s returned %d", program, res);
753 sync();
754 sleep(3);
755 start_raid_device(device);
756 if (g_fprep) {
757 fprintf(g_fprep, "%s\n", program);
758 }
759 mr_free(program);
760 }
761 sync();
762 sleep(2);
763#endif
764 sync();
765 sleep(1);
766 newtResume();
767 }
768
769 if (!strcmp(format, "lvm")) {
770 mr_msg(1, "Don't format %s - it's part of an lvm volume", device);
771 return (0);
772 }
773 malloc_string(program2);
774 res = which_format_command_do_i_need(format, program2);
775 mr_asprintf(&tmp, "%s %s", program2, device);
776 if (strstr(program2, "kludge")) {
777 mr_strcat(tmp, " /");
778 }
779 mr_free(program2);
780
781 mr_asprintf(&program, "sh -c 'echo -en \"y\\ny\\ny\\n\" | %s'", tmp);
782 mr_free(tmp);
783
784 mr_asprintf(&tmp, _("Formatting %s as %s"), device, format);
785 update_progress_form(tmp);
786
787 res = run_program_and_log_output(program, FALSE);
788 if (res && strstr(program, "kludge")) {
789 mr_free(tmp);
790 mr_asprintf(&tmp, _("Kludge failed; using regular mkfs.%s to format %s"), format, device);
791 mr_free(program);
792#ifdef __FreeBSD__
793 mr_asprintf(&program, "newfs_msdos -F 32 %s", device);
794#else
795#ifdef __IA64__
796 /* For EFI partitions take fat16
797 * as we want to make small ones */
798 mr_asprintf(&program, "mkfs -t %s -F 16 %s", format, device);
799#else
800 mr_asprintf(&program, "mkfs -t %s -F 32 %s", format, device);
801#endif
802#endif
803 res = run_program_and_log_output(program, FALSE);
804 if (g_fprep) {
805 fprintf(g_fprep, "%s\n", program);
806 }
807 }
808 mr_free(program);
809
810 retval += res;
811 if (retval) {
812 mr_strcat(tmp, _("...failed"));
813 } else {
814 mr_strcat(tmp, _("...OK"));
815 }
816 log_to_screen(tmp);
817 mr_free(tmp);
818
819 sync();
820 sleep(1);
821 return (retval);
822}
823
824
825/**
826 * Format all drives (except those excluded by format_device()) in @p mountlist.
827 * @param mountlist The mountlist containing partitions to be formatted.
828 * @param interactively If TRUE, then prompt the user before each partition.
829 * @return The number of errors encountered (0 for success).
830 */
831int format_everything(struct mountlist_itself *mountlist,
832 bool interactively, struct raidlist_itself *raidlist)
833{
834 /** int **************************************************************/
835 int retval = 0;
836 int lino = 0;
837 int res = 0;
838
839 /** long *************************************************************/
840 long progress_step = 0L;
841
842 /** bools ************************************************************/
843 bool do_it = FALSE;
844
845 /** buffers **********************************************************/
846 char *tmp = NULL;
847
848 /** pointers *********************************************************/
849 struct mountlist_line *me = NULL; // mountlist entry
850 /** end **************************************************************/
851
852 assert(mountlist != NULL);
853 log_it("format_everything (mountlist, interactively = %s",
854 (interactively) ? "true" : "false");
855 mvaddstr_and_log_it(g_currentY, 0, _("Formatting partitions "));
856 open_progress_form(_("Formatting partitions"),
857 _("I am now formatting your hard disk partitions."),
858 _("This may take up to five minutes."), "",
859 mountlist->entries + 1);
860
861 progress_step =
862 (mountlist->entries >
863 0) ? g_maximum_progress / mountlist->entries : 1;
864 // start soft-raids now (because LVM might depend on them)
865 // ...and for simplicity's sake, let's format them at the same time :)
866 mr_msg(1, "Stopping all RAID devices");
867 stop_all_raid_devices(mountlist);
868 sync();
869 sync();
870 sync();
871 sleep(2);
872 mr_msg(1, "Prepare soft-RAIDs"); // prep and format too
873 for (lino = 0; lino < mountlist->entries; lino++) {
874 me = &mountlist->el[lino]; // the current mountlist entry
875 mr_msg(2, "Examining %s", me->device);
876 if (!strncmp(me->device, "/dev/md", 7)) {
877 if (interactively) {
878 // ask user if we should format the current device
879 mr_asprintf(&tmp, "Shall I format %s (%s) ?", me->device,
880 me->mountpoint);
881 do_it = ask_me_yes_or_no(tmp);
882 mr_free(tmp);
883 } else {
884 do_it = TRUE;
885 }
886 if (do_it) {
887 // NB: format_device() also stops/starts RAID device if necessary
888 retval += format_device(me->device, me->format, raidlist);
889 }
890 g_current_progress += progress_step;
891 }
892 }
893 sync();
894 sync();
895 sync();
896 sleep(2);
897 // This last step is probably necessary
898 // log_to_screen("Re-starting software RAIDs...");
899 // start_all_raid_devices(mountlist);
900 // system("sync"); system("sync"); system("sync");
901 // sleep(5);
902 // do LVMs now
903 mr_msg(1, "Creating LVMs");
904 if (does_file_exist("/tmp/i-want-my-lvm")) {
905 wait_until_software_raids_are_prepped(100);
906 log_to_screen(_("Configuring LVM"));
907 if (!g_text_mode) {
908 newtSuspend();
909 }
910 res = do_my_funky_lvm_stuff(FALSE, TRUE);
911 if (!g_text_mode) {
912 newtResume();
913 }
914 if (!res) {
915 log_to_screen("LVM initialized OK");
916 } else {
917 log_to_screen("Failed to initialize LVM");
918 }
919 if (res) {
920 retval++;
921 }
922 sleep(3);
923 }
924 // do regulars at last
925 sleep(2); // woo!
926 mr_msg(1, "Formatting regulars");
927 for (lino = 0; lino < mountlist->entries; lino++) {
928 me = &mountlist->el[lino]; // the current mountlist entry
929 if (!strcmp(me->mountpoint, "image")) {
930 log_it("Not formatting %s - it's an image", me->device);
931 } else if (!strcmp(me->format, "raid")) {
932 log_it("Not formatting %s - it's a raid-let", me->device);
933 continue;
934 } else if (!strcmp(me->format, "lvm")) {
935 log_it("Not formatting %s - it's an LVM", me->device);
936 continue;
937 } else if (!strncmp(me->device, "/dev/md", 7)) {
938 log_it("Already formatted %s - it's a soft-RAID dev", me->device);
939 continue;
940 } else if (!does_file_exist(me->device)
941 && strncmp(me->device, "/dev/hd", 7)
942 && strncmp(me->device, "/dev/sd", 7)) {
943 log_it("Not formatting %s yet - doesn't exist - probably an LVM", me->device);
944 continue;
945 } else {
946 if (interactively) {
947 // ask user if we should format the current device
948 mr_asprintf(&tmp, "Shall I format %s (%s) ?", me->device,
949 me->mountpoint);
950 do_it = ask_me_yes_or_no(tmp);
951 mr_free(tmp);
952 } else {
953 do_it = TRUE;
954 }
955
956 if (do_it)
957 retval += format_device(me->device, me->format, raidlist);
958 }
959
960 // update progress bar
961 g_current_progress += progress_step;
962 }
963
964
965 // update progress bar to 100% to compensate for
966 // rounding errors of the progress_step calculation
967 if (lino >= mountlist->entries)
968 g_current_progress = g_maximum_progress;
969
970 close_progress_form();
971
972 if (retval) {
973 mvaddstr_and_log_it(g_currentY++, 74, _("Failed."));
974 log_to_screen
975 (_
976 ("Errors occurred during the formatting of your hard drives."));
977 } else {
978 mvaddstr_and_log_it(g_currentY++, 74, _("Done."));
979 }
980
981 log_it("format_everything () - %s",
982 (retval) ? "failed!" : "finished successfully");
983
984 if (g_partition_table_locked_up > 0) {
985 if (retval > 0 && !interactively) {
986//123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
987 log_to_screen
988 (_
989 ("Partition table locked up %d times. At least one 'mkfs' (format) command"),
990 g_partition_table_locked_up);
991 log_to_screen(_
992 ("failed. I think these two events are related. Sometimes, fdisk's ioctl() call"));
993 log_to_screen(_
994 ("to refresh its copy of the partition table causes the kernel to lock the "));
995 log_to_screen(_
996 ("partition table. I believe this has just happened."));
997 if (ask_me_yes_or_no
998 (_
999 ("Please choose 'yes' to reboot and try again; or 'no' to ignore this warning and continue.")))
1000 {
1001 sync();
1002 sync();
1003 sync();
1004 system("reboot");
1005 }
1006 } else {
1007 log_to_screen
1008 (_
1009 ("Partition table locked up %d time%c. However, disk formatting succeeded."),
1010 g_partition_table_locked_up,
1011 (g_partition_table_locked_up == 1) ? '.' : 's');
1012 }
1013 }
1014 newtSuspend();
1015 system("clear");
1016 newtResume();
1017 return (retval);
1018}
1019
1020
1021/**
1022 * Create small dummy partitions to fill in the gaps in partition numbering for @p drivename.
1023 * Each partition created is 32k in size.
1024 * @param drivename The drive to create the dummy partitions on.
1025 * @param devno_we_must_allow_for The lowest-numbered real partition; create
1026 * dummies up to (this - 1).
1027 * @return The number of errors encountered (0 for success).
1028 */
1029int make_dummy_partitions(FILE * pout_to_fdisk, char *drivename,
1030 int devno_we_must_allow_for)
1031{
1032 /** int **************************************************************/
1033 int current_devno = 0;
1034 int previous_devno = 0;
1035 int retval = 0;
1036 int res = 0;
1037
1038 /** end **************************************************************/
1039
1040 assert_string_is_neither_NULL_nor_zerolength(drivename);
1041
1042 if (devno_we_must_allow_for >= 5) {
1043 log_it("Making dummy primary %s%d", drivename, 1);
1044 g_maximum_progress++;
1045 res =
1046 partition_device(pout_to_fdisk, drivename, 1, 0, "ext2",
1047 32000);
1048 retval += res;
1049 previous_devno = 1;
1050 current_devno = 5;
1051 } else {
1052 previous_devno = 0;
1053 current_devno = 1;
1054 }
1055 for (; current_devno < devno_we_must_allow_for; current_devno++) {
1056 log_it("Creating dummy partition %s%d", drivename, current_devno);
1057 g_maximum_progress++;
1058 res =
1059 partition_device(pout_to_fdisk, drivename, current_devno,
1060 previous_devno, OSSWAP("ext2", "ufs"), 32000);
1061 retval += res;
1062 previous_devno = current_devno;
1063 }
1064 return (previous_devno);
1065}
1066
1067
1068/**
1069 * Decide whether @p mountlist contains any RAID devices.
1070 * @param mountlist The mountlist to examine.
1071 * @return TRUE if it does, FALSE if it doesn't.
1072 */
1073bool mountlist_contains_raid_devices(struct mountlist_itself * mountlist)
1074{
1075 /** int *************************************************************/
1076 int i;
1077 int matching = 0;
1078
1079 /** end **************************************************************/
1080
1081 assert(mountlist != NULL);
1082
1083 for (i = 0; i < mountlist->entries; i++) {
1084 if (strstr(mountlist->el[i].device, RAID_DEVICE_STUB)) {
1085 matching++;
1086 }
1087 }
1088 if (matching) {
1089 return (TRUE);
1090 } else {
1091 return (FALSE);
1092 }
1093}
1094
1095
1096/* The following 2 functions are stolen from /usr/src/sbin/disklabel/disklabel.c */
1097#ifdef __FreeBSD__
1098static void display_disklabel(FILE * f, const struct disklabel *lp)
1099{
1100 int i, j;
1101 const struct partition *pp;
1102
1103 fprintf(f, "# %s\n", "Generated by Mondo Rescue");
1104 if (lp->d_type < DKMAXTYPES)
1105 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
1106 else
1107 fprintf(f, "type: %u\n", lp->d_type);
1108 fprintf(f, "disk: %.*s\n", (int) sizeof(lp->d_typename),
1109 lp->d_typename);
1110 fprintf(f, "label: %.*s\n", (int) sizeof(lp->d_packname),
1111 lp->d_packname);
1112 fprintf(f, "flags:");
1113 if (lp->d_flags & D_REMOVABLE)
1114 fprintf(f, " removeable");
1115 if (lp->d_flags & D_ECC)
1116 fprintf(f, " ecc");
1117 if (lp->d_flags & D_BADSECT)
1118 fprintf(f, " badsect");
1119 fprintf(f, "\n");
1120 fprintf(f, "bytes/sector: %lu\n", (u_long) lp->d_secsize);
1121 fprintf(f, "sectors/track: %lu\n", (u_long) lp->d_nsectors);
1122 fprintf(f, "tracks/cylinder: %lu\n", (u_long) lp->d_ntracks);
1123 fprintf(f, "sectors/cylinder: %lu\n", (u_long) lp->d_secpercyl);
1124 fprintf(f, "cylinders: %lu\n", (u_long) lp->d_ncylinders);
1125 fprintf(f, "sectors/unit: %lu\n", (u_long) lp->d_secperunit);
1126 fprintf(f, "rpm: %u\n", lp->d_rpm);
1127 fprintf(f, "interleave: %u\n", lp->d_interleave);
1128 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
1129 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
1130 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
1131 (u_long) lp->d_headswitch);
1132 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
1133 (u_long) lp->d_trkseek);
1134 fprintf(f, "drivedata: ");
1135 for (i = NDDATA - 1; i >= 0; i--)
1136 if (lp->d_drivedata[i])
1137 break;
1138 if (i < 0)
1139 i = 0;
1140 for (j = 0; j <= i; j++)
1141 fprintf(f, "%lu ", (u_long) lp->d_drivedata[j]);
1142 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
1143 fprintf(f,
1144 "# size offset fstype [fsize bsize bps/cpg]\n");
1145 pp = lp->d_partitions;
1146 for (i = 0; i < lp->d_npartitions; i++, pp++) {
1147 if (pp->p_size) {
1148 fprintf(f, " %c: %8lu %8lu ", 'a' + i, (u_long) pp->p_size,
1149 (u_long) pp->p_offset);
1150 if (pp->p_fstype < FSMAXTYPES)
1151 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
1152 else
1153 fprintf(f, "%8d", pp->p_fstype);
1154 switch (pp->p_fstype) {
1155
1156 case FS_UNUSED: /* XXX */
1157 fprintf(f, " %5lu %5lu %5.5s ", (u_long) pp->p_fsize,
1158 (u_long) (pp->p_fsize * pp->p_frag), "");
1159 break;
1160
1161 case FS_BSDFFS:
1162 fprintf(f, " %5lu %5lu %5u ", (u_long) pp->p_fsize,
1163 (u_long) (pp->p_fsize * pp->p_frag), pp->p_cpg);
1164 break;
1165
1166 case FS_BSDLFS:
1167 fprintf(f, " %5lu %5lu %5d", (u_long) pp->p_fsize,
1168 (u_long) (pp->p_fsize * pp->p_frag), pp->p_cpg);
1169 break;
1170
1171 default:
1172 fprintf(f, "%20.20s", "");
1173 break;
1174 }
1175 fprintf(f, "\t# (Cyl. %4lu",
1176 (u_long) (pp->p_offset / lp->d_secpercyl));
1177 if (pp->p_offset % lp->d_secpercyl)
1178 putc('*', f);
1179 else
1180 putc(' ', f);
1181 fprintf(f, "- %lu",
1182 (u_long) ((pp->p_offset + pp->p_size +
1183 lp->d_secpercyl - 1) / lp->d_secpercyl -
1184 1));
1185 if (pp->p_size % lp->d_secpercyl)
1186 putc('*', f);
1187 fprintf(f, ")\n");
1188 }
1189 }
1190 fflush(f);
1191}
1192
1193static struct disklabel *get_virgin_disklabel(char *dkname)
1194{
1195 static struct disklabel loclab;
1196 struct partition *dp = NULL;
1197 char *lnamebuf = NULL;
1198 int f;
1199 u_int secsize, u;
1200 off_t mediasize;
1201
1202 mr_asprintf(&lnamebuf, "%s", dkname);
1203 if ((f = open(lnamebuf, O_RDONLY)) == -1) {
1204 warn("cannot open %s", lnamebuf);
1205 mr_free(lnamebuf);
1206 return (NULL);
1207 }
1208 mr_free(lnamebuf);
1209
1210 /* New world order */
1211 if ((ioctl(f, DIOCGMEDIASIZE, &mediasize) != 0)
1212 || (ioctl(f, DIOCGSECTORSIZE, &secsize) != 0)) {
1213 close(f);
1214 return (NULL);
1215 }
1216 memset(&loclab, 0, sizeof loclab);
1217 loclab.d_magic = DISKMAGIC;
1218 loclab.d_magic2 = DISKMAGIC;
1219 loclab.d_secsize = secsize;
1220 loclab.d_secperunit = mediasize / secsize;
1221
1222 /*
1223 * Nobody in these enligthened days uses the CHS geometry for
1224 * anything, but nontheless try to get it right. If we fail
1225 * to get any good ideas from the device, construct something
1226 * which is IBM-PC friendly.
1227 */
1228 if (ioctl(f, DIOCGFWSECTORS, &u) == 0)
1229 loclab.d_nsectors = u;
1230 else
1231 loclab.d_nsectors = 63;
1232 if (ioctl(f, DIOCGFWHEADS, &u) == 0)
1233 loclab.d_ntracks = u;
1234 else if (loclab.d_secperunit <= 63 * 1 * 1024)
1235 loclab.d_ntracks = 1;
1236 else if (loclab.d_secperunit <= 63 * 16 * 1024)
1237 loclab.d_ntracks = 16;
1238 else
1239 loclab.d_ntracks = 255;
1240 loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors;
1241 loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl;
1242 loclab.d_npartitions = MAXPARTITIONS;
1243
1244 /* Various (unneeded) compat stuff */
1245 loclab.d_rpm = 3600;
1246 loclab.d_bbsize = BBSIZE;
1247 loclab.d_interleave = 1;;
1248 strncpy(loclab.d_typename, "amnesiac", sizeof(loclab.d_typename));
1249
1250 dp = &loclab.d_partitions[RAW_PART];
1251 dp->p_size = loclab.d_secperunit;
1252 loclab.d_checksum = dkcksum(&loclab);
1253 close(f);
1254 return (&loclab);
1255}
1256
1257/* End stolen from /usr/src/sbin/disklabel/disklabel.c. */
1258
1259char *canonical_name(char *drivename)
1260{
1261 if (drivename) {
1262 if (strncmp(drivename, "/dev/", 5) == 0) {
1263 return drivename + 5;
1264 }
1265 }
1266 return drivename;
1267}
1268
1269/**
1270 * (BSD only) Create a disklabel on @p drivename according to @p mountlist.
1271 * @param mountlist The mountlist to get the subpartition information from.
1272 * @param drivename The drive or slice to create a disklabel on.
1273 * @param ret If non-NULL, store the created disklabel here.
1274 * @return The number of errors encountered (0 for success).
1275 */
1276int label_drive_or_slice(struct mountlist_itself *mountlist,
1277 char *drivename, struct disklabel *ret)
1278{
1279 char *subdev_str = NULL;
1280 char *command = NULL;
1281 struct disklabel *lp = NULL;
1282 int i, lo = 0;
1283 int retval = 0;
1284 char c = ' ';
1285 FILE *ftmp = NULL;
1286
1287 lp = get_virgin_disklabel(drivename);
1288 for (c = 'a'; c <= 'z'; ++c) {
1289 int idx;
1290 mr_asprintf(&subdev_str, "%s%c", drivename, c);
1291 if ((idx = find_device_in_mountlist(mountlist, subdev_str)) < 0) {
1292 lp->d_partitions[c - 'a'].p_size = 0;
1293 lp->d_partitions[c - 'a'].p_fstype = FS_UNUSED;
1294 } else {
1295 lo = c - 'a';
1296 lp->d_partitions[c - 'a'].p_size = mountlist->el[idx].size * 2;
1297 lp->d_partitions[c - 'a'].p_fsize = 0;
1298 lp->d_partitions[c - 'a'].p_frag = 0;
1299 lp->d_partitions[c - 'a'].p_cpg = 0;
1300 if (!strcmp(mountlist->el[idx].format, "ufs")
1301 || !strcmp(mountlist->el[idx].format, "ffs")
1302 || !strcmp(mountlist->el[idx].format, "4.2BSD")) {
1303 lp->d_partitions[c - 'a'].p_fstype = FS_BSDFFS;
1304 lp->d_partitions[c - 'a'].p_fsize = 2048;
1305 lp->d_partitions[c - 'a'].p_frag = 8;
1306 lp->d_partitions[c - 'a'].p_cpg = 64;
1307 } else if (!strcasecmp(mountlist->el[idx].format, "raid")
1308 || !strcasecmp(mountlist->el[idx].format, "vinum")) {
1309 lp->d_partitions[c - 'a'].p_fstype = FS_VINUM;
1310 } else if (!strcmp(mountlist->el[idx].format, "swap")) {
1311 lp->d_partitions[c - 'a'].p_fstype = FS_SWAP;
1312 } else
1313 lp->d_partitions[c - 'a'].p_fstype = FS_OTHER;
1314 }
1315 mr_free(subdev_str);
1316 }
1317
1318 // fix up the offsets
1319 lp->d_partitions[0].p_offset = 0;
1320 lp->d_partitions[RAW_PART].p_offset = 0;
1321 lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
1322 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1323
1324 for (i = 1; i < lp->d_npartitions; ++i) {
1325 int lastone;
1326 if ((i == RAW_PART) || (lp->d_partitions[i].p_size == 0))
1327 continue;
1328 for (lastone = i - 1; lastone >= 0; lastone--) {
1329 if ((lp->d_partitions[lastone].p_size)
1330 && (lastone != RAW_PART))
1331 break;
1332 }
1333 lp->d_partitions[i].p_offset =
1334 lp->d_partitions[lastone].p_offset +
1335 lp->d_partitions[lastone].p_size;
1336 }
1337 if (lp->d_partitions[lo].p_offset + lp->d_partitions[lo].p_size >
1338 lp->d_secperunit) {
1339 lp->d_partitions[lo].p_size =
1340 lp->d_secperunit - lp->d_partitions[lo].p_offset;
1341 }
1342
1343 ftmp = fopen("/tmp/disklabel", "w");
1344 display_disklabel(ftmp, lp);
1345 fclose(ftmp);
1346 mr_asprintf(&command, "disklabel -wr %s auto", canonical_name(drivename));
1347 retval += run_program_and_log_output(command, TRUE);
1348 mr_free(command);
1349
1350 mr_asprintf(&command, "disklabel -R %s /tmp/disklabel",
1351 canonical_name(drivename));
1352 retval += run_program_and_log_output(command, TRUE);
1353 mr_free(command);
1354 if (ret)
1355 *ret = *lp;
1356 return retval;
1357}
1358#endif
1359
1360
1361/**
1362 * Partition @p drivename based on @p mountlist.
1363 * @param mountlist The mountlist to use to guide the partitioning.
1364 * @param drivename The drive to partition.
1365 * @return 0 for success, nonzero for failure.
1366 */
1367int partition_drive(struct mountlist_itself *mountlist, char *drivename)
1368{
1369 /** int *************************************************************/
1370 int current_devno = 0;
1371 int previous_devno = 0;
1372 int lino = 0;
1373 int retval = 0;
1374 int i = 0;
1375 FILE *pout_to_fdisk = NULL;
1376
1377#ifdef __FreeBSD__
1378 bool fbsd_part = FALSE;
1379 char *subdev_str = NULL;
1380#endif
1381
1382 /** long long *******************************************************/
1383 long long partsize;
1384
1385 /** buffers *********************************************************/
1386 char *device_str = NULL;
1387 char *format = NULL;
1388 char *tmp = NULL;
1389
1390 /** end *************************************************************/
1391
1392 assert(mountlist != NULL);
1393 assert_string_is_neither_NULL_nor_zerolength(drivename);
1394
1395 log_it("Partitioning drive %s", drivename);
1396
1397#if __FreeBSD__
1398 log_it("(Not opening fdisk now; that's the Linux guy's job)");
1399 pout_to_fdisk = NULL;
1400#else
1401 make_hole_for_file(FDISK_LOG);
1402 mr_asprintf(&tmp, "parted2fdisk %s >> %s 2>> %s", drivename, FDISK_LOG,
1403 FDISK_LOG);
1404 pout_to_fdisk = popen(tmp, "w");
1405 mr_free(tmp);
1406
1407 if (!pout_to_fdisk) {
1408 log_to_screen(_("Cannot call parted2fdisk to configure %s"),
1409 drivename);
1410 return (1);
1411 }
1412#endif
1413
1414 malloc_string(device_str);
1415
1416 for (current_devno = 1; current_devno < 99; current_devno++) {
1417 build_partition_name(device_str, drivename, current_devno);
1418 lino = find_device_in_mountlist(mountlist, device_str);
1419
1420 if (lino < 0) {
1421 // device not found in mountlist
1422#if __FreeBSD__
1423 // If this is the first partition (just as a sentinel value),
1424 // then see if the user has picked 'dangerously-dedicated' mode.
1425 // If so, then we just call label_drive_or_slice() and return.
1426 char c = ' ';
1427 char *command = NULL;
1428
1429 if (current_devno == 1) {
1430 // try DangerouslyDedicated mode
1431 for (c = 'a'; c <= 'z'; c++) {
1432 mr_asprintf(&subdev_str, "%s%c", drivename, c);
1433 if (find_device_in_mountlist(mountlist, subdev_str) > 0) {
1434 fbsd_part = TRUE;
1435 }
1436 mr_free(subdev_str);
1437 }
1438 if (fbsd_part) {
1439 int r = label_drive_or_slice(mountlist,
1440 drivename,
1441 0);
1442 mr_asprintf(&command, "disklabel -B %s",
1443 basename(drivename));
1444 if (system(command)) {
1445 log_to_screen
1446 (_
1447 ("Warning! Unable to make the drive bootable."));
1448 }
1449 mr_free(command);
1450 mr_free(device_str);
1451 return r;
1452 }
1453 }
1454 for (c = 'a'; c <= 'z'; c++) {
1455 mr_asprintf(&subdev_str, "%s%c", device_str, c);
1456 if (find_device_in_mountlist(mountlist, subdev_str) > 0) {
1457 fbsd_part = TRUE;
1458 }
1459 mr_free(subdev_str);
1460 }
1461 // Now we check the subpartitions of the current partition.
1462 if (fbsd_part) {
1463 int i, line;
1464
1465 mr_asprintf(&format, "ufs");
1466 partsize = 0;
1467 for (i = 'a'; i < 'z'; ++i) {
1468 mr_asprintf(&subdev_str, "%s%c", device_str, i);
1469 line = find_device_in_mountlist(mountlist, subdev_str);
1470 mr_free(subdev_str);
1471
1472 if (line > 0) {
1473 // We found one! Add its size to the total size.
1474 partsize += mountlist->el[line].size;
1475 }
1476 }
1477 } else {
1478 continue;
1479 }
1480#else
1481 continue;
1482#endif
1483 }
1484
1485 /* OK, we've found partition /dev/hdxN in mountlist; let's prep it */
1486 /* For FreeBSD, that is /dev/adXsY */
1487
1488 log_it("Found partition %s in mountlist", device_str);
1489 if (!previous_devno) {
1490
1491 log_it("Wiping %s's partition table", drivename);
1492#if __FreeBSD__
1493 // FreeBSD doesn't let you write to blk devices in <512byte chunks.
1494 file = open(drivename, O_WRONLY);
1495 if (!file) {
1496 log_to_screen(_("Warning - unable to open %s for wiping it's partition table"), drivename);
1497 }
1498
1499 for (i = 0; i < 512; i++) {
1500 if (!write(file, "\0", 1)) {
1501 log_to_screen(_("Warning - unable to write to %s"), drivename);
1502 }
1503 }
1504 sync();
1505#else
1506 iamhere("New, kernel-friendly partition remover");
1507 for (i = 20; i > 0; i--) {
1508 fprintf(pout_to_fdisk, "d\n%d\n", i);
1509 fflush(pout_to_fdisk);
1510 }
1511#endif
1512 if (current_devno > 1) {
1513 previous_devno =
1514 make_dummy_partitions(pout_to_fdisk, drivename,
1515 current_devno);
1516 }
1517 }
1518#ifdef __FreeBSD__
1519 if (!fbsd_part) {
1520#endif
1521 mr_free(format);
1522 mr_asprintf(&format, mountlist->el[lino].format);
1523 partsize = mountlist->el[lino].size;
1524
1525#ifdef __FreeBSD__
1526 }
1527#endif
1528
1529#ifndef __IA64__
1530 if (current_devno == 5 && previous_devno == 4) {
1531 log_to_screen
1532 (_
1533 ("You must leave at least one partition spare as the Extended partition."));
1534 mr_free(device_str);
1535 mr_free(format);
1536 return (1);
1537 }
1538#endif
1539
1540 retval +=
1541 partition_device(pout_to_fdisk, drivename, current_devno,
1542 previous_devno, format, partsize);
1543
1544#ifdef __FreeBSD__
1545 if ((current_devno <= 4) && fbsd_part) {
1546 mr_asprintf(&tmp, "disklabel -B %s", basename(device_str));
1547 retval += label_drive_or_slice(mountlist, device_str, 0);
1548 if (system(tmp)) {
1549 log_to_screen
1550 (_("Warning! Unable to make the slice bootable."));
1551 }
1552 mr_free(tmp);
1553 }
1554#endif
1555
1556 previous_devno = current_devno;
1557 }
1558 mr_free(device_str);
1559 mr_free(format);
1560
1561 if (pout_to_fdisk) {
1562 // mark relevant partition as bootable
1563 mr_asprintf(&tmp, "a\n%s\n",
1564 call_program_and_get_last_line_of_output
1565 ("make-me-bootable /tmp/mountlist.txt dummy"));
1566 fput_string_one_char_at_a_time(pout_to_fdisk, tmp);
1567 mr_free(tmp);
1568
1569 // close fdisk
1570 fput_string_one_char_at_a_time(pout_to_fdisk, "w\n");
1571 sync();
1572 paranoid_pclose(pout_to_fdisk);
1573 mr_msg(0,
1574 "------------------- fdisk.log looks like this ------------------");
1575 mr_asprintf(&tmp, "cat %s >> %s", FDISK_LOG, MONDO_LOGFILE);
1576 system(tmp);
1577 mr_free(tmp);
1578
1579 mr_msg(0,
1580 "------------------- end of fdisk.log... word! ------------------");
1581 mr_asprintf(&tmp, "tail -n6 %s | grep -F \"16: \"", FDISK_LOG);
1582 if (!run_program_and_log_output(tmp, 5)) {
1583 g_partition_table_locked_up++;
1584 log_to_screen
1585 (_
1586 ("A flaw in the Linux kernel has locked the partition table."));
1587 }
1588 mr_free(tmp);
1589 }
1590 return (retval);
1591}
1592
1593
1594/**
1595 * Create partition number @p partno on @p drive with @p fdisk.
1596 * @param drive The drive to create the partition on.
1597// * @param partno The partition number of the new partition (1-4 are primary, >=5 is logical).
1598 * @param prev_partno The partition number of the most recently prepped partition.
1599 * @param format The filesystem type of this partition (used to set the type).
1600 * @param partsize The size of the partition in @e bytes.
1601 * @return 0 for success, nonzero for failure.
1602 */
1603int partition_device(FILE * pout_to_fdisk, const char *drive, int partno,
1604 int prev_partno, const char *format,
1605 long long partsize)
1606{
1607 /** int **************************************************************/
1608 int retval = 0;
1609 int res = 0;
1610
1611 /** buffers **********************************************************/
1612 char *program = NULL;
1613 char *partition_name = NULL;
1614 char *tmp = NULL;
1615 char *output = NULL;
1616
1617 /** pointers **********************************************************/
1618 char *p = NULL;
1619 char *part_table_fmt = NULL;
1620 FILE *fout = NULL;
1621
1622 /** end ***************************************************************/
1623
1624 assert_string_is_neither_NULL_nor_zerolength(drive);
1625 assert(format != NULL);
1626
1627 log_it("partition_device('%s', %d, %d, '%s', %lld) --- starting",
1628 drive, partno, prev_partno, format, partsize);
1629
1630 if (!strncmp(drive, RAID_DEVICE_STUB, strlen(RAID_DEVICE_STUB))) {
1631 log_it("Not partitioning %s - it is a virtual drive", drive);
1632 return (0);
1633 }
1634
1635 malloc_string(partition_name);
1636
1637 build_partition_name(partition_name, drive, partno);
1638 if (partsize <= 0) {
1639 mr_asprintf(&tmp, "Partitioning device %s (max size)", partition_name);
1640 } else {
1641 mr_asprintf(&tmp, "Partitioning device %s (%lld MB)", partition_name,
1642 (long long) partsize / 1024);
1643 }
1644 update_progress_form(tmp);
1645 log_it(tmp);
1646 mr_free(tmp);
1647
1648 if (is_this_device_mounted(partition_name)) {
1649 log_to_screen("%s is mounted, and should not be partitioned",
1650 partition_name);
1651 mr_free(partition_name);
1652 return (1);
1653 }
1654
1655 p = (char *) strrchr(partition_name, '/');
1656
1657 /* BERLIOS: should not be called each time */
1658 part_table_fmt = which_partition_format(drive);
1659 /* make it a primary/extended/logical */
1660 if (partno <= 4) {
1661 mr_asprintf(&output,"n\np\n%d\n", partno);
1662 } else {
1663 /* MBR needs an extended partition if more than 4 partitions */
1664 if (strcmp(part_table_fmt, "MBR") == 0) {
1665 if (partno == 5) {
1666 if (prev_partno >= 4) {
1667 log_to_screen
1668 (_
1669 ("You need to leave at least one partition free, for 'extended/logical'"));
1670 mr_free(partition_name);
1671 return (1);
1672 } else {
1673 mr_asprintf(&output,"n\ne\n%d\n\n\n",prev_partno + 1);
1674 }
1675 }
1676 mr_strcat(output, "n\nl\n");
1677 } else {
1678 /* GPT allows more than 4 primary partitions */
1679 mr_asprintf(&output,"n\np\n%d\n",partno);
1680 }
1681 }
1682 /*start block (ENTER for next free blk */
1683 mr_strcat(output, "\n");
1684 if (partsize > 0) {
1685 if (!strcmp(format, "7")) {
1686 mr_msg(1, "Adding 512K, just in case");
1687 partsize += 512;
1688 }
1689 mr_strcat(output, "+%lldK", (long long) (partsize));
1690 }
1691 mr_strcat(output, "\n");
1692#if 0
1693/*
1694#endif
1695 log_it("PARTSIZE = +%ld",(long)partsize);
1696 log_it("---fdisk command---");
1697 log_it(output);
1698 log_it("---end of fdisk---");
1699#if 0
1700*/
1701#endif
1702
1703
1704 if (pout_to_fdisk) {
1705 mr_msg(1, "Doing the new all-in-one fdisk thing");
1706 mr_msg(1, "output = '%s'", output);
1707 fput_string_one_char_at_a_time(pout_to_fdisk, output);
1708 fput_string_one_char_at_a_time(pout_to_fdisk, "\n\np\n");
1709 mr_asprintf(&tmp, last_line_of_file(FDISK_LOG));
1710 if (strstr(tmp, " (m ")) {
1711 mr_msg(1, "Successfully created %s%d", drive, partno);
1712 } else {
1713 mr_msg(1, "last line = %s", tmp);
1714 mr_msg(1, "Failed to create %s%d; sending 'Enter'...", drive,
1715 partno);
1716 }
1717 mr_free(tmp);
1718
1719 if (!retval) {
1720 mr_msg(1, "Trying to set %s%d's partition type now", drive,
1721 partno);
1722 retval =
1723 set_partition_type(pout_to_fdisk, drive, partno, format,
1724 partsize);
1725 if (retval) {
1726 mr_msg(1, "Failed. Trying again...");
1727 retval =
1728 set_partition_type(pout_to_fdisk, drive, partno,
1729 format, partsize);
1730 }
1731 }
1732 if (retval) {
1733 mr_msg(1, "...but failed to set type");
1734 }
1735 } else {
1736 mr_strcat(output, "w\n\n");
1737 mr_asprintf(&program, "parted2fdisk %s >> %s 2>> %s", drive, MONDO_LOGFILE,
1738 MONDO_LOGFILE);
1739
1740 if (g_fprep) {
1741 fprintf(g_fprep, "echo \"%s\" | %s\n", output, program);
1742 }
1743 /* write to disk; close fdisk's stream */
1744 if (!(fout = popen(program, "w"))) {
1745 log_OS_error("can't popen-out to program");
1746 } else {
1747 fputs(output, fout);
1748 paranoid_pclose(fout);
1749 }
1750 mr_free(program);
1751
1752 if (!does_partition_exist(drive, partno) && partsize > 0) {
1753 log_it("Vaccum-packing");
1754 g_current_progress--;
1755 res =
1756 partition_device(pout_to_fdisk, drive, partno, prev_partno,
1757 format, -1);
1758 if (res) {
1759 log_it("Failed to vacuum-pack %s", partition_name);
1760 retval++;
1761 } else {
1762 retval = 0;
1763 }
1764 }
1765 if (does_partition_exist(drive, partno)) {
1766 retval =
1767 set_partition_type(pout_to_fdisk, drive, partno, format,
1768 partsize);
1769 if (retval) {
1770 log_it("Partitioned %s but failed to set its type",
1771 partition_name);
1772 } else {
1773 if (partsize > 0) {
1774 log_to_screen(_("Partition %s created+configured OK"),
1775 partition_name);
1776 } else {
1777 log_it("Returning from a successful vacuum-pack");
1778 }
1779 }
1780 } else {
1781 mr_asprintf(&tmp, "Failed to partition %s", partition_name);
1782 if (partsize > 0) {
1783 log_to_screen(tmp);
1784 } else {
1785 log_it(tmp);
1786 }
1787 mr_free(tmp);
1788 retval++;
1789 }
1790 }
1791 g_current_progress++;
1792 log_it("partition_device() --- leaving");
1793 mr_free(partition_name);
1794 mr_free(output);
1795 return (retval);
1796}
1797
1798
1799/**
1800 * Create all partitions listed in @p mountlist.
1801 * @param mountlist The mountlist to use to guide the partitioning.
1802 * @return The number of errors encountered (0 for success).
1803 * @note This sets the partition types but doesn't actually do the formatting.
1804 * Use format_everything() for that.
1805 */
1806int partition_everything(struct mountlist_itself *mountlist)
1807{
1808 /** int ************************************************************/
1809 int lino = 0;
1810 int retval = 0;
1811 int i = 0;
1812 int res = 0;
1813
1814 /** buffer *********************************************************/
1815 struct list_of_disks *drivelist = NULL;
1816
1817 /** end ************************************************************/
1818
1819 drivelist = mr_malloc(sizeof(struct list_of_disks));
1820 assert(mountlist != NULL);
1821
1822 log_it("partition_everything() --- starting");
1823 mvaddstr_and_log_it(g_currentY, 0, "Partitioning hard drives ");
1824 if (mountlist_contains_raid_devices(mountlist)) {
1825 /* mountlist=&new_mtlist; */
1826 /* extrapolate_mountlist_to_include_raid_partitions(mountlist,orig_mtlist); */
1827 mr_msg(0,
1828 "Mountlist, including the partitions incorporated in RAID devices:-");
1829 for (i = 0; i < mountlist->entries; i++) {
1830 log_it(mountlist->el[i].device);
1831 }
1832 mr_msg(0, "End of mountlist.");
1833 }
1834 mr_msg(0, "Stopping all LVMs, just in case");
1835 if (!g_text_mode) {
1836 newtSuspend();
1837 }
1838 do_my_funky_lvm_stuff(TRUE, FALSE); // just remove old partitions
1839 if (!g_text_mode) {
1840 newtResume();
1841 }
1842 mr_msg(0, "Stopping all software RAID devices, just in case");
1843 stop_all_raid_devices(mountlist);
1844 mr_msg(0, "Done.");
1845
1846 open_progress_form(_("Partitioning devices"),
1847 _("I am now going to partition all your drives."),
1848 _("This should not take more than five minutes."),
1849 "", mountlist->entries);
1850
1851 make_list_of_drives_in_mountlist(mountlist, drivelist);
1852
1853 /* partition each drive */
1854 for (lino = 0; lino < drivelist->entries; lino++) {
1855 res = partition_drive(mountlist, drivelist->el[lino].device);
1856 retval += res;
1857 }
1858 close_progress_form();
1859 if (retval) {
1860 mvaddstr_and_log_it(g_currentY++, 74, _("Failed."));
1861 log_to_screen
1862 (_
1863 ("Errors occurred during the partitioning of your hard drives."));
1864 } else {
1865 mvaddstr_and_log_it(g_currentY++, 74, _("Done."));
1866 paranoid_system("rm -f /tmp/fdisk*.log 2> /dev/null");
1867 }
1868 newtSuspend();
1869 system("clear");
1870 newtResume();
1871 mr_free(drivelist);
1872 return (retval);
1873}
1874
1875
1876/**
1877 * Set the type of partition number @p partno on @p drive to @p format.
1878 * @param drive The drive to change the type of a partition on.
1879 * @param partno The partition number on @p drive to change the type of.
1880 * @param format The filesystem type this partition will eventually contain.
1881 * @param partsize The size of this partition, in @e bytes (used for vfat
1882 * type calculations).
1883 * @return 0 for success, nonzero for failure.
1884 */
1885int set_partition_type(FILE * pout_to_fdisk, const char *drive, int partno,
1886 const char *format, long long partsize)
1887{
1888 /** buffers *********************************************************/
1889 char *partition = NULL;
1890 char *command = NULL;
1891 char *output = NULL;
1892 char *tmp = NULL;
1893 char *tmp1 = NULL;
1894 char *partcode = NULL;
1895
1896 /** pointers *********************************************************/
1897 char *p = NULL;
1898 FILE *fout = NULL;
1899
1900 /** int **************************************************************/
1901 int res = 0;
1902
1903 /** end **************************************************************/
1904
1905 assert_string_is_neither_NULL_nor_zerolength(drive);
1906 assert(format != NULL);
1907
1908 malloc_string(partition);
1909
1910 build_partition_name(partition, drive, partno);
1911 p = (char *) strrchr(partition, '/');
1912 if (strcmp(format, "swap") == 0) {
1913 mr_asprintf(&partcode, "82");
1914 } else if (strcmp(format, "vfat") == 0) {
1915 if (partsize / 1024 > 8192) {
1916 mr_asprintf(&partcode, "c");
1917 } else {
1918 mr_asprintf(&partcode, "b");
1919 }
1920 } else if (strcmp(format, "ext2") == 0
1921 || strcmp(format, "reiserfs") == 0
1922 || strcmp(format, "ext3") == 0 || strcmp(format, "xfs") == 0
1923 || strcmp(format, "jfs") == 0) {
1924 mr_asprintf(&partcode, "83");
1925 } else if (strcmp(format, "minix") == 0) {
1926 mr_asprintf(&partcode, "81");
1927 } else if (strcmp(format, "raid") == 0) {
1928 mr_asprintf(&partcode, "fd");
1929 } else if ((strcmp(format, "ufs") == 0)
1930 || (strcmp(format, "ffs") == 0)) { /* raid autodetect */
1931 mr_asprintf(&partcode, "a5");
1932 } else if (strcmp(format, "lvm") == 0) {
1933 mr_asprintf(&partcode, "8e");
1934 } else if (format[0] == '\0') { /* LVM physical partition */
1935 mr_asprintf(&partcode, "");
1936 } else if (strlen(format) >= 1 && strlen(format) <= 2) {
1937 mr_asprintf(&partcode, format);
1938 } else {
1939 /* probably an image */
1940 mr_asprintf(&tmp,
1941 "Unknown format ('%s') - using supplied string anyway",
1942 format);
1943 mvaddstr_and_log_it(g_currentY++, 0, tmp);
1944 mr_free(tmp);
1945#ifdef __FreeBSD__
1946 mr_asprintf(&partcode, format); // was a5
1947#else
1948 mr_asprintf(&partcode, format); // was 83
1949#endif
1950 }
1951 mr_asprintf(&tmp, "Setting %s's type to %s (%s)", partition, format,
1952 partcode);
1953 mr_msg(1, tmp);
1954 mr_free(tmp);
1955
1956 if (partcode[0] != '\0' && strcmp(partcode, "83")) { /* no need to set type if 83: 83 is default */
1957
1958 if (pout_to_fdisk) {
1959 res = 0;
1960 fput_string_one_char_at_a_time(pout_to_fdisk, "t\n");
1961 if (partno > 1
1962 || strstr(last_line_of_file(FDISK_LOG), " (1-4)")) {
1963 mr_msg(5, "Specifying partno (%d) - yay", partno);
1964 mr_asprintf(&tmp, "%d\n", partno);
1965 fput_string_one_char_at_a_time(pout_to_fdisk, tmp);
1966 mr_msg(5, "A - last line = '%s'",
1967 last_line_of_file(FDISK_LOG));
1968 mr_free(tmp);
1969 }
1970
1971 mr_asprintf(&tmp, "%s\n", partcode);
1972 fput_string_one_char_at_a_time(pout_to_fdisk, tmp);
1973 mr_free(tmp);
1974
1975 mr_msg(5, "B - last line = '%s'",
1976 last_line_of_file(FDISK_LOG));
1977
1978 fput_string_one_char_at_a_time(pout_to_fdisk, "\n");
1979 mr_msg(5, "C - last line = '%s'",
1980 last_line_of_file(FDISK_LOG));
1981
1982 mr_asprintf(&tmp, last_line_of_file(FDISK_LOG));
1983 if (!strstr(tmp, " (m ")) {
1984 mr_msg(1, "last line = '%s'; part type set failed", tmp);
1985 res++;
1986 fput_string_one_char_at_a_time(pout_to_fdisk, "\n");
1987 }
1988 mr_free(tmp);
1989 fput_string_one_char_at_a_time(pout_to_fdisk, "p\n");
1990 } else {
1991 mr_asprintf(&output, "t\n%d\n%s\nw\n", partno, partcode);
1992 mr_asprintf(&command, "parted2fdisk %s >> %s 2>> %s", drive,
1993 MONDO_LOGFILE, MONDO_LOGFILE);
1994 mr_msg(5, "output = '%s'", output);
1995 mr_msg(5, "partno=%d; partcode=%s", partno, partcode);
1996 mr_msg(5, "command = '%s'", command);
1997 fout = popen(command, "w");
1998 if (!fout) {
1999 log_OS_error(command);
2000 res = 1;
2001 } else {
2002 res = 0;
2003 fprintf(fout, output);
2004 paranoid_pclose(fout);
2005 }
2006 mr_free(command);
2007 mr_free(output);
2008 }
2009 /* BERLIOS: Useless as command not initialized in all cases
2010 if (res) {
2011 log_OS_error(command);
2012 }
2013 */
2014 }
2015
2016 mr_free(partition);
2017 mr_free(partcode);
2018
2019 return (res);
2020}
2021
2022
2023int start_raid_device(char *raid_device)
2024{
2025 /** int *************************************************************/
2026 int res = 0;
2027 int retval = 0;
2028
2029 /** buffers *********************************************************/
2030 char *program = NULL;
2031
2032 /** end *************************************************************/
2033
2034 assert_string_is_neither_NULL_nor_zerolength(raid_device);
2035
2036#ifdef __FreeBSD__
2037 if (is_this_device_mounted(raid_device)) {
2038 log_it("Can't start %s when it's mounted!", raid_device);
2039 return 1;
2040 }
2041 mr_asprintf(&program, "vinum start -f %s", raid_device);
2042#else
2043 // use raidstart if it exists, otherwise use mdadm
2044 if (run_program_and_log_output("which raidstart", FALSE)) {
2045 // BERLIOS: Not sure it's sufficient
2046 mr_asprintf(&program, "mdadm -A %s", raid_device);
2047 } else {
2048 mr_asprintf(&program, "raidstart %s", raid_device);
2049 }
2050#endif
2051 mr_msg(1, "program = %s", program);
2052 res = run_program_and_log_output(program, 1);
2053 if (g_fprep) {
2054 fprintf(g_fprep, "%s\n", program);
2055 }
2056 mr_free(program);
2057
2058 if (res) {
2059 mr_msg(1, "Warning - failed to start RAID device %s",
2060 raid_device);
2061 }
2062 retval += res;
2063 sleep(1);
2064 return (retval);
2065}
2066
2067
2068/**
2069 * Stop @p raid_device using @p raidstop.
2070 * @param raid_device The software RAID device to stop.
2071 * @return 0 for success, nonzero for failure.
2072 */
2073int stop_raid_device(char *raid_device)
2074{
2075 /** int *************************************************************/
2076 int res = 0;
2077 int retval = 0;
2078
2079 /** buffers *********************************************************/
2080 char *program = NULL;
2081
2082 /** end *************************************************************/
2083
2084 assert_string_is_neither_NULL_nor_zerolength(raid_device);
2085
2086#ifdef __FreeBSD__
2087 if (is_this_device_mounted(raid_device)) {
2088 log_it("Can't stop %s when it's mounted!", raid_device);
2089 return 1;
2090 }
2091 mr_asprintf(&program, "vinum stop -f %s", raid_device);
2092#else
2093 // use raidstop if it exists, otherwise use mdadm
2094 if (run_program_and_log_output("which raidstop", FALSE)) {
2095 mr_asprintf(&program, "mdadm -S %s", raid_device);
2096 } else {
2097 mr_asprintf(&program, "raidstop %s", raid_device);
2098 }
2099#endif
2100 mr_msg(1, "program = %s", program);
2101 res = run_program_and_log_output(program, 1);
2102 if (g_fprep) {
2103 fprintf(g_fprep, "%s\n", program);
2104 }
2105 mr_free(program);
2106
2107 if (res) {
2108 mr_msg(1, "Warning - failed to stop RAID device %s", raid_device);
2109 }
2110 retval += res;
2111 return (retval);
2112}
2113
2114
2115int start_all_raid_devices(struct mountlist_itself *mountlist)
2116{
2117 int i = 0;
2118 int retval = 0;
2119 int res = 0;
2120
2121 for (i = 0; i < mountlist->entries; i++) {
2122 if (!strncmp
2123 (mountlist->el[i].device, RAID_DEVICE_STUB,
2124 strlen(RAID_DEVICE_STUB))) {
2125 mr_msg(1, "Starting %s", mountlist->el[i].device);
2126 res = start_raid_device(mountlist->el[i].device);
2127 retval += res;
2128 }
2129 }
2130 if (retval) {
2131 mr_msg(1, "Started all s/w raid devices OK");
2132 } else {
2133 mr_msg(1, "Failed to start some/all s/w raid devices");
2134 }
2135 return (retval);
2136}
2137
2138
2139/**
2140 * Stop all software RAID devices listed in @p mountlist.
2141 * @param mountlist The mountlist to stop the RAID devices in.
2142 * @return The number of errors encountered (0 for success).
2143 * @bug @p mountlist is not used.
2144 */
2145int stop_all_raid_devices(struct mountlist_itself *mountlist)
2146{
2147 /** int *************************************************************/
2148 int retval = 0;
2149#ifndef __FreeBSD__
2150 int res = 0;
2151#endif
2152
2153 /** char ************************************************************/
2154 char *incoming = NULL;
2155#ifndef __FreeBSD__
2156 char *dev = NULL;
2157#endif
2158 /** pointers ********************************************************/
2159#ifndef __FreeBSD__
2160 char *p = NULL;
2161#endif
2162 FILE *fin = NULL;
2163 int i = 0;
2164 size_t n = 0;
2165
2166 /** end ****************************************************************/
2167
2168 assert(mountlist != NULL);
2169
2170 for (i = 0; i < 3; i++) {
2171#ifdef __FreeBSD__
2172 fin =
2173 popen
2174 ("vinum list | grep '^[PVS]' | sed 's/S/1/;s/P/2/;s/V/3/' | sort | cut -d' ' -f2",
2175 "r");
2176 if (!fin) {
2177 return (1);
2178 }
2179 for (mr_getline(&incoming, &n, fin); !feof(fin);
2180 mr_getline(&incoming, &n, fin)) {
2181 retval += stop_raid_device(incoming);
2182 }
2183#else
2184 fin = fopen(MDSTAT_FILE, "r");
2185 if (!fin) {
2186 log_OS_error(MDSTAT_FILE);
2187 return (1);
2188 }
2189 for (mr_getline(&incoming, &n, fin); !feof(fin);
2190 mr_getline(&incoming, &n, fin)) {
2191 for (p = incoming;
2192 *p != '\0' && (*p != 'm' || *(p + 1) != 'd'
2193 || !isdigit(*(p + 2))); p++);
2194 if (*p != '\0') {
2195 mr_asprintf(&dev, "/dev/%s", p);
2196 /* BERLIOS : 32 Hard coded value */
2197 for (p = dev; *p > 32; p++);
2198 *p = '\0';
2199 res = stop_raid_device(dev);
2200 mr_free(dev);
2201 }
2202 }
2203#endif
2204 mr_free(incoming);
2205 }
2206 paranoid_fclose(fin);
2207 if (retval) {
2208 mr_msg(1, "Warning - unable to stop some RAID devices");
2209 }
2210 sync();
2211 sync();
2212 sync();
2213 sleep(1);
2214 return (retval);
2215}
2216
2217
2218/**
2219 * Decide which command we need to use to format a device of type @p format.
2220 * @param format The filesystem type we are about to format.
2221 * @param program Where to put the binary name for this format.
2222 * @return 0 for success, nonzero for failure.
2223 */
2224int which_format_command_do_i_need(char *format, char *program)
2225{
2226 /** int *************************************************************/
2227 int res = 0;
2228
2229 /** end ***************************************************************/
2230
2231 assert_string_is_neither_NULL_nor_zerolength(format);
2232 assert(program != NULL);
2233
2234 if (strcmp(format, "swap") == 0) {
2235#ifdef __FreeBSD__
2236 strcpy(program, "true");
2237#else
2238 strcpy(program, "mkswap");
2239#endif
2240 } else if (strcmp(format, "vfat") == 0) {
2241 strcpy(program, "format-and-kludge-vfat");
2242#ifndef __FreeBSD__
2243 } else if (strcmp(format, "reiserfs") == 0) {
2244 strcpy(program, "mkreiserfs -ff");
2245 } else if (strcmp(format, "xfs") == 0) {
2246 strcpy(program, "mkfs.xfs -f -q");
2247 } else if (strcmp(format, "jfs") == 0) {
2248 strcpy(program, "mkfs.jfs");
2249 } else if (strcmp(format, "ext3") == 0) {
2250 strcpy(program, "mkfs -t ext2 -F -j -q");
2251 } else if (strcmp(format, "minix") == 0) {
2252 strcpy(program, "mkfs.minix");
2253#endif
2254 } else if (strcmp(format, "ext2") == 0) {
2255 strcpy(program, "mke2fs -F -q");
2256 } else {
2257#ifdef __FreeBSD__
2258 sprintf(program, "newfs_%s", format);
2259#else
2260 sprintf(program, "mkfs -t %s -c", format); // -c checks for bad blocks
2261#endif
2262 log_it("Unknown format (%s) - assuming '%s' will do", format,
2263 program);
2264 res = 0;
2265 }
2266 return (res);
2267}
2268
2269
2270/**
2271 * Calculate the probable size of @p drive_name by adding up sizes in
2272 * @p mountlist.
2273 * @param mountlist The mountlist to use to calculate the size.
2274 * @param drive_name The drive to calculate the original size of.
2275 * @return The size of @p drive_name in kilobytes.
2276 */
2277long calc_orig_size_of_drive_from_mountlist(struct mountlist_itself
2278 *mountlist, char *drive_name)
2279{
2280 /** long ************************************************************/
2281 long original_size_of_drive = 0L;
2282
2283 /** int *************************************************************/
2284 int partno = 0;
2285
2286 /** end *************************************************************/
2287
2288 assert(mountlist != NULL);
2289 assert_string_is_neither_NULL_nor_zerolength(drive_name);
2290
2291 for (original_size_of_drive = 0, partno = 0;
2292 partno < mountlist->entries; partno++) {
2293 if (strncmp
2294 (mountlist->el[partno].device, drive_name,
2295 strlen(drive_name)) == 0) {
2296 original_size_of_drive += mountlist->el[partno].size;
2297 }
2298 }
2299 original_size_of_drive = original_size_of_drive / 1024;
2300 return (original_size_of_drive);
2301}
2302
2303
2304/**
2305 * Resize a drive's entries in @p mountlist proportionately to fit its new size.
2306 * There are a few problems with this function:
2307 * - It won't work if there was any unallocated space on the user's hard drive
2308 * when it was backed up.
2309 * - It won't work if the user's hard drive lies about its size (more common
2310 * than you'd think).
2311 *
2312 * @param mountlist The mountlist to use for resizing @p drive_name.
2313 * @param drive_name The drive to resize.
2314 */
2315void resize_drive_proportionately_to_suit_new_drives(struct mountlist_itself
2316 *mountlist,
2317 char *drive_name)
2318{
2319 /** int *************************************************************/
2320 int partno = 0, lastpart = 0;
2321 /** remove driveno, noof_drives stan benoit apr 2002**/
2322
2323 /** float ***********************************************************/
2324 float factor;
2325 float new_size;
2326
2327 /** long *************************************************************/
2328 long newsizL = 0L;
2329 long current_size_of_drive = 0L;
2330 long original_size_of_drive = 0L;
2331 long final_size = 0L; /* all in Megabytes */
2332 struct mountlist_reference *drivemntlist = NULL;
2333
2334 /** structures *******************************************************/
2335
2336 /** end **************************************************************/
2337
2338 assert(mountlist != NULL);
2339 assert_string_is_neither_NULL_nor_zerolength(drive_name);
2340
2341 if (strlen(drive_name) >= strlen(RAID_DEVICE_STUB)) {
2342 if (strncmp(drive_name, RAID_DEVICE_STUB, strlen(RAID_DEVICE_STUB))
2343 == 0) {
2344 return;
2345 }
2346 }
2347
2348 current_size_of_drive = get_phys_size_of_drive(drive_name);
2349
2350 if (current_size_of_drive <= 0) {
2351 log_it("Not resizing to match %s - can't find drive", drive_name);
2352 return;
2353 }
2354 log_to_screen("Expanding entries to suit drive %s (%ld MB)", drive_name,
2355 current_size_of_drive);
2356
2357 drivemntlist = mr_malloc(sizeof(struct mountlist_reference));
2358 drivemntlist->el =
2359 mr_malloc(sizeof(struct mountlist_line *) * MAX_TAPECATALOG_ENTRIES);
2360
2361 create_mountlist_for_drive(mountlist, drive_name, drivemntlist);
2362
2363 for (partno = 0; partno < drivemntlist->entries; partno++) {
2364 original_size_of_drive += drivemntlist->el[partno]->size;
2365 }
2366 original_size_of_drive = original_size_of_drive / 1024;
2367
2368 if (original_size_of_drive <= 0) {
2369 log_to_screen(_("Cannot resize %s's entries. Drive not found."),
2370 drive_name);
2371 return;
2372 }
2373 factor =
2374 (float) (current_size_of_drive) / (float) (original_size_of_drive);
2375 log_to_screen(_("Disk %s was %ld MB; is now %ld MB; factor = %f"),
2376 drive_name, original_size_of_drive, current_size_of_drive,
2377 factor);
2378
2379 lastpart = drivemntlist->entries - 1;
2380 for (partno = 0; partno < drivemntlist->entries; partno++) {
2381 /* the 'atoi' thing is to make sure we don't try to resize _images_, whose formats will be numeric */
2382 if (!atoi(drivemntlist->el[partno]->format)) {
2383 new_size = (float) (drivemntlist->el[partno]->size) * factor;
2384 } else {
2385 new_size = drivemntlist->el[partno]->size;
2386 }
2387
2388 if (!strcmp(drivemntlist->el[partno]->mountpoint, "image")) {
2389 mr_msg(1, "Skipping %s (%s) because it's an image",
2390 drivemntlist->el[partno]->device,
2391 drivemntlist->el[partno]->mountpoint);
2392 newsizL = (long) new_size; // It looks wrong but it's not
2393 } else {
2394 newsizL = (long) new_size;
2395 }
2396 log_to_screen(_("Changing %s from %lld KB to %ld KB"),
2397 drivemntlist->el[partno]->device,
2398 drivemntlist->el[partno]->size, newsizL);
2399 drivemntlist->el[partno]->size = newsizL;
2400 }
2401 final_size = get_phys_size_of_drive(drive_name);
2402 log_to_screen(_("final_size = %ld MB"), final_size);
2403}
2404
2405
2406/**
2407 * Resize all partitions in @p mountlist proportionately (each one
2408 * grows or shrinks by the same percentage) to fit them into the new
2409 * drives (presumably different from the old ones).
2410 * @param mountlist The mountlist to resize the drives in.
2411 */
2412void resize_mountlist_proportionately_to_suit_new_drives(struct mountlist_itself
2413 *mountlist)
2414{
2415 /** buffers *********************************************************/
2416 struct list_of_disks *drivelist = NULL;
2417
2418 /** int *************************************************************/
2419 int driveno = 0;
2420
2421 /** end *************************************************************/
2422
2423 drivelist = mr_malloc(sizeof(struct list_of_disks));
2424
2425 if (g_mountlist_fname[0] == '\0') {
2426 log_it
2427 ("resize_mountlist_prop...() - warning - mountlist fname is blank");
2428 log_it("That does NOT affect the functioning of this subroutine.");
2429 }
2430 iamhere("Resizing mountlist");
2431 make_list_of_drives_in_mountlist(mountlist, drivelist);
2432 iamhere("Back from MLoDiM");
2433 for (driveno = 0; driveno < drivelist->entries; driveno++) {
2434 resize_drive_proportionately_to_suit_new_drives(mountlist,
2435 drivelist->
2436 el[driveno].
2437 device);
2438 }
2439 log_to_screen(_("Mountlist adjusted to suit current hard drive(s)"));
2440 mr_free(drivelist);
2441}
2442
2443/**
2444 * Create a mountlist_reference structure for @p drive_name in @p mountlist.
2445 * @param mountlist The complete mountlist to get the drive references from.
2446 * @param drive_name The drive to put in @p drivemntlist.
2447 * @param drivemntlist The mountlist_reference structure to put the drive's entries in.
2448 * @note @p drivemntlist and @p drivemntlist->el must be allocated by the caller.
2449 * @author Ralph Grewe
2450 */
2451void create_mountlist_for_drive(struct mountlist_itself *mountlist,
2452 char *drive_name,
2453 struct mountlist_reference *drivemntlist)
2454{
2455 int partno = 0;
2456 char *tmp_drive_name = NULL, *c = NULL;
2457
2458 assert(mountlist != NULL);
2459 assert(drive_name != NULL);
2460 assert(drivemntlist != NULL);
2461
2462 mr_msg(1, "Creating list of partitions for drive %s", drive_name);
2463
2464 mr_asprintf(&tmp_drive_name, drive_name);
2465
2466 /* devfs devices? */
2467 c = strrchr(tmp_drive_name, '/');
2468 if (c && strncmp(c, "/disc", 5) == 0) {
2469 /* yup its devfs, change the "disc" to "part" so the existing code works */
2470 strncpy(c + 1, "part", (size_t)5);
2471 }
2472 drivemntlist->entries = 0;
2473 for (partno = 0; partno < mountlist->entries; partno++) {
2474 if (strncmp
2475 (mountlist->el[partno].device, tmp_drive_name,
2476 strlen(tmp_drive_name)) == 0) {
2477 drivemntlist->el[drivemntlist->entries] =
2478 &mountlist->el[partno];
2479 drivemntlist->entries++;
2480 }
2481 }
2482 mr_free(tmp_drive_name);
2483}
2484
2485/* @} - end of prepGroup */
Note: See TracBrowser for help on using the repository browser.