source: MondoRescue/branches/2.2.2/mondo/src/mondorestore/mondo-prep.c@ 1316

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

MONDO_LOGFILE is rather a char* exported by each main program (consolidation of those mecanisms in .h files with ps_*)

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