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

Last change on this file since 1371 was 1371, checked in by Bruno Cornec, 17 years ago

fix a log_to_screen call

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