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

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

Other syntax fixes dur to change of meaning of MONDO_LOGFILE

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