source: MondoRescue/branches/2.04_berlios/mondo/mondo/mondorestore/mondo-prep.c@ 85

Last change on this file since 85 was 85, checked in by bcornec, 19 years ago

Patch allowing to restore on ia64 till 3rd CD :-)

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