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

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