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

Last change on this file since 1304 was 1304, checked in by Bruno Cornec, 17 years ago
  • Some FreeBSD fixes
  • Better messages for partition names
  • May improve the gap issue on cciss
  • Property svn:keywords set to Id
File size: 70.0 KB
Line 
1/***************************************************************************
2 * $Id: mondo-prep.c 1304 2007-04-15 00:43:15Z 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 1304 2007-04-15 00:43:15Z 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 1 on %s", drivename);
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 %d on %s", current_devno, drivename);
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 partition %d on %s", partno, drive);
1712 } else {
1713 mr_msg(1, "last line = %s", tmp);
1714 mr_msg(1, "Failed to create partition %d on %s; sending 'Enter'...", partno, drive);
1715 }
1716 mr_free(tmp);
1717
1718 if (!retval) {
1719 mr_msg(1, "Trying to set partition %d type now on %s", partno, drive);
1720 retval =
1721 set_partition_type(pout_to_fdisk, drive, partno, format,
1722 partsize);
1723 if (retval) {
1724 mr_msg(1, "Failed. Trying again...");
1725 retval =
1726 set_partition_type(pout_to_fdisk, drive, partno,
1727 format, partsize);
1728 }
1729 }
1730 if (retval) {
1731 mr_msg(1, "...but failed to set type");
1732 }
1733 } else {
1734 mr_strcat(output, "w\n\n");
1735 mr_asprintf(&program, "parted2fdisk %s >> %s 2>> %s", drive, MONDO_LOGFILE,
1736 MONDO_LOGFILE);
1737
1738 if (g_fprep) {
1739 fprintf(g_fprep, "echo \"%s\" | %s\n", output, program);
1740 }
1741 /* write to disk; close fdisk's stream */
1742 if (!(fout = popen(program, "w"))) {
1743 log_OS_error("can't popen-out to program");
1744 } else {
1745 fputs(output, fout);
1746 paranoid_pclose(fout);
1747 }
1748 mr_free(program);
1749
1750 if (!does_partition_exist(drive, partno) && partsize > 0) {
1751 log_it("Vaccum-packing");
1752 g_current_progress--;
1753 res =
1754 partition_device(pout_to_fdisk, drive, partno, prev_partno,
1755 format, -1);
1756 if (res) {
1757 log_it("Failed to vacuum-pack %s", partition_name);
1758 retval++;
1759 } else {
1760 retval = 0;
1761 }
1762 }
1763 if (does_partition_exist(drive, partno)) {
1764 retval =
1765 set_partition_type(pout_to_fdisk, drive, partno, format,
1766 partsize);
1767 if (retval) {
1768 log_it("Partitioned %s but failed to set its type",
1769 partition_name);
1770 } else {
1771 if (partsize > 0) {
1772 log_to_screen(_("Partition %s created+configured OK"),
1773 partition_name);
1774 } else {
1775 log_it("Returning from a successful vacuum-pack");
1776 }
1777 }
1778 } else {
1779 mr_asprintf(&tmp, "Failed to partition %s", partition_name);
1780 if (partsize > 0) {
1781 log_to_screen(tmp);
1782 } else {
1783 log_it(tmp);
1784 }
1785 mr_free(tmp);
1786 retval++;
1787 }
1788 }
1789 g_current_progress++;
1790 log_it("partition_device() --- leaving");
1791 mr_free(partition_name);
1792 mr_free(output);
1793 return (retval);
1794}
1795
1796
1797/**
1798 * Create all partitions listed in @p mountlist.
1799 * @param mountlist The mountlist to use to guide the partitioning.
1800 * @return The number of errors encountered (0 for success).
1801 * @note This sets the partition types but doesn't actually do the formatting.
1802 * Use format_everything() for that.
1803 */
1804int partition_everything(struct mountlist_itself *mountlist)
1805{
1806 /** int ************************************************************/
1807 int lino = 0;
1808 int retval = 0;
1809 int i = 0;
1810 int res = 0;
1811
1812 /** buffer *********************************************************/
1813 struct list_of_disks *drivelist = NULL;
1814
1815 /** end ************************************************************/
1816
1817 drivelist = mr_malloc(sizeof(struct list_of_disks));
1818 assert(mountlist != NULL);
1819
1820 log_it("partition_everything() --- starting");
1821 mvaddstr_and_log_it(g_currentY, 0, "Partitioning hard drives ");
1822 if (mountlist_contains_raid_devices(mountlist)) {
1823 /* mountlist=&new_mtlist; */
1824 /* extrapolate_mountlist_to_include_raid_partitions(mountlist,orig_mtlist); */
1825 mr_msg(0,
1826 "Mountlist, including the partitions incorporated in RAID devices:-");
1827 for (i = 0; i < mountlist->entries; i++) {
1828 log_it(mountlist->el[i].device);
1829 }
1830 mr_msg(0, "End of mountlist.");
1831 }
1832 mr_msg(0, "Stopping all LVMs, just in case");
1833 if (!g_text_mode) {
1834 newtSuspend();
1835 }
1836 do_my_funky_lvm_stuff(TRUE, FALSE); // just remove old partitions
1837 if (!g_text_mode) {
1838 newtResume();
1839 }
1840 mr_msg(0, "Stopping all software RAID devices, just in case");
1841 stop_all_raid_devices(mountlist);
1842 mr_msg(0, "Done.");
1843
1844 open_progress_form(_("Partitioning devices"),
1845 _("I am now going to partition all your drives."),
1846 _("This should not take more than five minutes."),
1847 "", mountlist->entries);
1848
1849 make_list_of_drives_in_mountlist(mountlist, drivelist);
1850
1851 /* partition each drive */
1852 for (lino = 0; lino < drivelist->entries; lino++) {
1853 res = partition_drive(mountlist, drivelist->el[lino].device);
1854 retval += res;
1855 }
1856 close_progress_form();
1857 if (retval) {
1858 mvaddstr_and_log_it(g_currentY++, 74, _("Failed."));
1859 log_to_screen
1860 (_
1861 ("Errors occurred during the partitioning of your hard drives."));
1862 } else {
1863 mvaddstr_and_log_it(g_currentY++, 74, _("Done."));
1864 paranoid_system("rm -f /tmp/fdisk*.log 2> /dev/null");
1865 }
1866 newtSuspend();
1867 system("clear");
1868 newtResume();
1869 mr_free(drivelist);
1870 return (retval);
1871}
1872
1873
1874/**
1875 * Set the type of partition number @p partno on @p drive to @p format.
1876 * @param drive The drive to change the type of a partition on.
1877 * @param partno The partition number on @p drive to change the type of.
1878 * @param format The filesystem type this partition will eventually contain.
1879 * @param partsize The size of this partition, in @e bytes (used for vfat
1880 * type calculations).
1881 * @return 0 for success, nonzero for failure.
1882 */
1883int set_partition_type(FILE * pout_to_fdisk, const char *drive, int partno,
1884 const char *format, long long partsize)
1885{
1886 /** buffers *********************************************************/
1887 char *partition = NULL;
1888 char *command = NULL;
1889 char *output = NULL;
1890 char *tmp = NULL;
1891 char *tmp1 = 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 sprintf(tmp, "Skipping %s", mountlist->el[partno].device);
2298 log_to_screen(tmp);
2299 }
2300 }
2301 original_size_of_drive = original_size_of_drive / 1024;
2302 return (original_size_of_drive);
2303}
2304
2305
2306/**
2307 * Resize a drive's entries in @p mountlist proportionately to fit its new size.
2308 * There are a few problems with this function:
2309 * - It won't work if there was any unallocated space on the user's hard drive
2310 * when it was backed up.
2311 * - It won't work if the user's hard drive lies about its size (more common
2312 * than you'd think).
2313 *
2314 * @param mountlist The mountlist to use for resizing @p drive_name.
2315 * @param drive_name The drive to resize.
2316 */
2317void resize_drive_proportionately_to_suit_new_drives(struct mountlist_itself
2318 *mountlist,
2319 char *drive_name)
2320{
2321 /** int *************************************************************/
2322 int partno = 0, lastpart = 0;
2323 /** remove driveno, noof_drives stan benoit apr 2002**/
2324
2325 /** float ***********************************************************/
2326 float factor;
2327 float new_size;
2328
2329 /** long *************************************************************/
2330 long newsizL = 0L;
2331 long current_size_of_drive = 0L;
2332 long original_size_of_drive = 0L;
2333 long final_size = 0L; /* all in Megabytes */
2334 struct mountlist_reference *drivemntlist = NULL;
2335
2336 /** structures *******************************************************/
2337
2338 /** end **************************************************************/
2339
2340 assert(mountlist != NULL);
2341 assert_string_is_neither_NULL_nor_zerolength(drive_name);
2342
2343 if (strlen(drive_name) >= strlen(RAID_DEVICE_STUB)) {
2344 if (strncmp(drive_name, RAID_DEVICE_STUB, strlen(RAID_DEVICE_STUB))
2345 == 0) {
2346 return;
2347 }
2348 }
2349
2350 current_size_of_drive = get_phys_size_of_drive(drive_name);
2351
2352 if (current_size_of_drive <= 0) {
2353 log_it("Not resizing to match %s - can't find drive", drive_name);
2354 return;
2355 }
2356 log_to_screen("Expanding entries to suit drive %s (%ld MB)", drive_name,
2357 current_size_of_drive);
2358
2359 drivemntlist = mr_malloc(sizeof(struct mountlist_reference));
2360 drivemntlist->el =
2361 mr_malloc(sizeof(struct mountlist_line *) * MAX_TAPECATALOG_ENTRIES);
2362
2363 create_mountlist_for_drive(mountlist, drive_name, drivemntlist);
2364
2365 for (partno = 0; partno < drivemntlist->entries; partno++) {
2366 original_size_of_drive += drivemntlist->el[partno]->size;
2367 }
2368 original_size_of_drive = original_size_of_drive / 1024;
2369
2370 if (original_size_of_drive <= 0) {
2371 log_to_screen(_("Cannot resize %s's entries. Drive not found."),
2372 drive_name);
2373 return;
2374 }
2375 factor =
2376 (float) (current_size_of_drive) / (float) (original_size_of_drive);
2377 log_to_screen(_("Disk %s was %ld MB; is now %ld MB; factor = %f"),
2378 drive_name, original_size_of_drive, current_size_of_drive,
2379 factor);
2380
2381 lastpart = drivemntlist->entries - 1;
2382 for (partno = 0; partno < drivemntlist->entries; partno++) {
2383 /* the 'atoi' thing is to make sure we don't try to resize _images_, whose formats will be numeric */
2384 if (!atoi(drivemntlist->el[partno]->format)) {
2385 new_size = (float) (drivemntlist->el[partno]->size) * factor;
2386 } else {
2387 new_size = drivemntlist->el[partno]->size;
2388 }
2389
2390 if (!strcmp(drivemntlist->el[partno]->mountpoint, "image")) {
2391 mr_msg(1, "Skipping %s (%s) because it's an image",
2392 drivemntlist->el[partno]->device,
2393 drivemntlist->el[partno]->mountpoint);
2394 newsizL = (long) new_size; // It looks wrong but it's not
2395 } else {
2396 newsizL = (long) new_size;
2397 }
2398 log_to_screen(_("Changing %s from %lld KB to %ld KB"),
2399 drivemntlist->el[partno]->device,
2400 drivemntlist->el[partno]->size, newsizL);
2401 drivemntlist->el[partno]->size = newsizL;
2402 }
2403 final_size = get_phys_size_of_drive(drive_name);
2404 log_to_screen(_("final_size = %ld MB"), final_size);
2405}
2406
2407
2408/**
2409 * Resize all partitions in @p mountlist proportionately (each one
2410 * grows or shrinks by the same percentage) to fit them into the new
2411 * drives (presumably different from the old ones).
2412 * @param mountlist The mountlist to resize the drives in.
2413 */
2414void resize_mountlist_proportionately_to_suit_new_drives(struct mountlist_itself
2415 *mountlist)
2416{
2417 /** buffers *********************************************************/
2418 struct list_of_disks *drivelist = NULL;
2419
2420 /** int *************************************************************/
2421 int driveno = 0;
2422
2423 /** end *************************************************************/
2424
2425 drivelist = mr_malloc(sizeof(struct list_of_disks));
2426
2427 if (g_mountlist_fname[0] == '\0') {
2428 log_it
2429 ("resize_mountlist_prop...() - warning - mountlist fname is blank");
2430 log_it("That does NOT affect the functioning of this subroutine.");
2431 }
2432 iamhere("Resizing mountlist");
2433 make_list_of_drives_in_mountlist(mountlist, drivelist);
2434 iamhere("Back from MLoDiM");
2435 for (driveno = 0; driveno < drivelist->entries; driveno++) {
2436 resize_drive_proportionately_to_suit_new_drives(mountlist,
2437 drivelist->
2438 el[driveno].
2439 device);
2440 }
2441 log_to_screen(_("Mountlist adjusted to suit current hard drive(s)"));
2442 mr_free(drivelist);
2443}
2444
2445/**
2446 * Create a mountlist_reference structure for @p drive_name in @p mountlist.
2447 * @param mountlist The complete mountlist to get the drive references from.
2448 * @param drive_name The drive to put in @p drivemntlist.
2449 * @param drivemntlist The mountlist_reference structure to put the drive's entries in.
2450 * @note @p drivemntlist and @p drivemntlist->el must be allocated by the caller.
2451 * @author Ralph Grewe
2452 */
2453void create_mountlist_for_drive(struct mountlist_itself *mountlist,
2454 char *drive_name,
2455 struct mountlist_reference *drivemntlist)
2456{
2457 int partno = 0;
2458 char *tmp_drive_name = NULL, *c = NULL;
2459
2460 assert(mountlist != NULL);
2461 assert(drive_name != NULL);
2462 assert(drivemntlist != NULL);
2463
2464 mr_msg(1, "Creating list of partitions for drive %s", drive_name);
2465
2466 mr_asprintf(&tmp_drive_name, drive_name);
2467
2468 /* devfs devices? */
2469 c = strrchr(tmp_drive_name, '/');
2470 if (c && strncmp(c, "/disc", 5) == 0) {
2471 /* yup its devfs, change the "disc" to "part" so the existing code works */
2472 strncpy(c + 1, "part", (size_t)5);
2473 }
2474 drivemntlist->entries = 0;
2475 for (partno = 0; partno < mountlist->entries; partno++) {
2476 if (strncmp
2477 (mountlist->el[partno].device, tmp_drive_name,
2478 strlen(tmp_drive_name)) == 0) {
2479 drivemntlist->el[drivemntlist->entries] =
2480 &mountlist->el[partno];
2481 drivemntlist->entries++;
2482 }
2483 }
2484 mr_free(tmp_drive_name);
2485}
2486
2487/* @} - end of prepGroup */
Note: See TracBrowser for help on using the repository browser.