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

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

On ia64 you may have more than 4 primary partitions

  • Property svn:keywords set to Id
File size: 74.8 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 71 2005-10-20 00:06:39Z 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 71 2005-10-20 00:06:39Z 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 sprintf(program, "mkfs -t %s -F 32 %s", format, device);
837#endif
838 res = run_program_and_log_output(program, FALSE);
839 if (g_fprep) { fprintf(g_fprep, "%s\n", program); }
840 }
841 retval += res;
842 if (retval) {
843 strcat(tmp, "...failed");
844 } else {
845 strcat(tmp, "...OK");
846 }
847
848 log_to_screen(tmp);
849 paranoid_free(program); paranoid_free(tmp);
850 system("sync"); sleep(1);
851 return (retval);
852}
853
854
855
856
857
858/**
859 * Format all drives (except those excluded by format_device()) in @p mountlist.
860 * @param mountlist The mountlist containing partitions to be formatted.
861 * @param interactively If TRUE, then prompt the user before each partition.
862 * @return The number of errors encountered (0 for success).
863 */
864int format_everything(struct mountlist_itself *mountlist, bool interactively)
865{
866 /** int **************************************************************/
867 int retval = 0;
868 int lino;
869 int res;
870// int i;
871// struct list_of_disks *drivelist;
872
873 /** long *************************************************************/
874 long progress_step;
875
876 /** bools ************************************************************/
877 bool do_it;
878
879 /** buffers **********************************************************/
880 char *tmp;
881
882 /** pointers *********************************************************/
883 struct mountlist_line *me; // mountlist entry
884 /** end **************************************************************/
885
886 assert(mountlist!=NULL);
887 malloc_string(tmp);
888 sprintf(tmp, "format_everything (mountlist, interactively = %s", (interactively) ? "true" : "false");
889 log_it(tmp);
890 mvaddstr_and_log_it(g_currentY, 0, "Formatting partitions ");
891 open_progress_form("Formatting partitions", "I am now formatting your hard disk partitions.",
892 "This may take up to five minutes.", "", mountlist->entries + 1);
893
894 progress_step = (mountlist->entries > 0) ? g_maximum_progress / mountlist->entries : 1;
895// start soft-raids now (because LVM might depend on them)
896// ...and for simplicity's sake, let's format them at the same time :)
897 log_msg(1, "Stopping all RAID devices");
898 stop_all_raid_devices(mountlist);
899 system("sync"); system("sync"); system("sync");
900 sleep(2);
901 log_msg(1, "Prepare soft-RAIDs"); // prep and format too
902 for (lino = 0; lino < mountlist->entries; lino++) {
903 me = &mountlist->el[lino]; // the current mountlist entry
904 log_msg(2, "Examining %s", me->device);
905 if (!strncmp(me->device, "/dev/md", 7)) {
906 if (interactively) {
907 // ask user if we should format the current device
908 sprintf(tmp, "Shall I format %s (%s) ?", me->device, me->mountpoint);
909 do_it = ask_me_yes_or_no(tmp);
910 } else {
911 do_it = TRUE;
912 }
913 if (do_it) {
914 // NB: format_device() also stops/starts RAID device if necessary
915 retval += format_device(me->device, me->format);
916 }
917 g_current_progress += progress_step;
918 }
919 }
920 system("sync"); system("sync"); system("sync");
921 sleep(2);
922// This last step is probably necessary
923// log_to_screen("Re-starting software RAIDs...");
924// start_all_raid_devices(mountlist);
925// system("sync"); system("sync"); system("sync");
926// sleep(5);
927// do LVMs now
928 log_msg(1, "Creating LVMs");
929 if (does_file_exist("/tmp/i-want-my-lvm")) {
930 wait_until_software_raids_are_prepped("/proc/mdstat", 10);
931 log_to_screen("Configuring LVM");
932 if (!g_text_mode) { newtSuspend(); }
933/*
934 for(i=0; i<3; i++)
935 {
936 res = do_my_funky_lvm_stuff(FALSE, FALSE);
937 if (!res) { break; }
938 sleep(3);
939 res = do_my_funky_lvm_stuff(TRUE, FALSE);
940 sleep(3);
941 }
942 if (res) {
943 log_msg(1, "Vacuum-packing...");
944*/
945 res = do_my_funky_lvm_stuff(FALSE, TRUE);
946/*
947 }
948*/
949 if (!g_text_mode) { newtResume(); }
950 if (!res) { log_to_screen("LVM initialized OK"); }
951 else { log_to_screen("Failed to initialize LVM"); }
952 // retval += res;
953 if (res) {
954 retval ++;
955 }
956 sleep(3);
957 }
958
959// do regulars at last
960 sleep(2); // woo!
961 log_msg(1, "Formatting regulars");
962 for (lino = 0; lino < mountlist->entries; lino++) {
963 me = &mountlist->el[lino]; // the current mountlist entry
964 if (!strcmp(me->mountpoint, "image")) {
965 sprintf(tmp, "Not formatting %s - it's an image", me->device);
966 log_it(tmp);
967 } else if (!strcmp(me->format, "raid")) {
968 sprintf(tmp, "Not formatting %s - it's a raid-let", me->device);
969 log_it(tmp);
970 continue;
971 } else if (!strcmp(me->format, "lvm")) {
972 sprintf(tmp, "Not formatting %s - it's an LVM", me->device);
973 log_it(tmp);
974 continue;
975 } else if (!strncmp(me->device, "/dev/md", 7)) {
976 sprintf(tmp, "Already formatted %s - it's a soft-RAID dev", me->device);
977 log_it(tmp);
978 continue;
979 } else if (!does_file_exist(me->device)
980 && strncmp(me->device, "/dev/hd", 7)
981 && strncmp(me->device, "/dev/sd", 7)) {
982 sprintf(tmp, "Not formatting %s yet - doesn't exist - probably an LVM", me->device);
983 log_it(tmp);
984 continue;
985 } else {
986 if (interactively) {
987 // ask user if we should format the current device
988 sprintf(tmp, "Shall I format %s (%s) ?", me->device, me->mountpoint);
989 do_it = ask_me_yes_or_no(tmp);
990 } else {
991 do_it = TRUE;
992 }
993
994 if (do_it)
995 retval += format_device(me->device, me->format);
996 }
997
998 // update progress bar
999 g_current_progress += progress_step;
1000 }
1001
1002
1003 // update progress bar to 100% to compensate for
1004 // rounding errors of the progress_step calculation
1005 if (lino >= mountlist->entries)
1006 g_current_progress = g_maximum_progress;
1007
1008 close_progress_form();
1009
1010 if (retval) {
1011 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
1012 log_to_screen("Errors occurred during the formatting of your hard drives.");
1013 } else {
1014 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1015 }
1016
1017 sprintf(tmp, "format_everything () - %s", (retval) ? "failed!" : "finished successfully");
1018 log_it(tmp);
1019
1020 if (g_partition_table_locked_up > 0)
1021 {
1022 if (retval > 0 && !interactively)
1023 {
1024//123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
1025 log_to_screen(
1026 "Partition table locked up %d times. At least one 'mkfs' (format) command", g_partition_table_locked_up);
1027 log_to_screen(
1028 "failed. I think these two events are related. Sometimes, fdisk's ioctl() call");
1029 log_to_screen(
1030 "to refresh its copy of the partition table causes the kernel to lock the ");
1031 log_to_screen(
1032 "partition table. I believe this has just happened.");
1033 if (ask_me_yes_or_no("Please choose 'yes' to reboot and try again; or 'no' to ignore this warning and continue."))
1034 {
1035 system("sync");
1036 system("sync");
1037 system("sync");
1038 system("reboot");
1039 }
1040 }
1041 else
1042 {
1043 log_to_screen(
1044"Partition table locked up %d time%c. However, disk formatting succeeded.", g_partition_table_locked_up, (g_partition_table_locked_up==1)?'.':'s');
1045 }
1046 }
1047 newtSuspend();
1048 system("clear");
1049 newtResume();
1050 paranoid_free(tmp);
1051 return (retval);
1052}
1053
1054
1055/**
1056 * Create small dummy partitions to fill in the gaps in partition numbering for @p drivename.
1057 * Each partition created is 32k in size.
1058 * @param drivename The drive to create the dummy partitions on.
1059 * @param devno_we_must_allow_for The lowest-numbered real partition; create
1060 * dummies up to (this - 1).
1061 * @return The number of errors encountered (0 for success).
1062 */
1063int make_dummy_partitions(FILE*pout_to_fdisk, char *drivename, int devno_we_must_allow_for)
1064{
1065 /** int **************************************************************/
1066 int current_devno;
1067 int previous_devno;
1068 int retval = 0;
1069 int res;
1070
1071 /** buffers **********************************************************/
1072 char *tmp;
1073
1074 /** end **************************************************************/
1075
1076 malloc_string(tmp);
1077 assert_string_is_neither_NULL_nor_zerolength(drivename);
1078
1079 if (devno_we_must_allow_for >= 5) {
1080 sprintf(tmp, "Making dummy primary %s%d", drivename, 1);
1081 log_it(tmp);
1082 g_maximum_progress++;
1083 res = partition_device(pout_to_fdisk,drivename, 1, 0, "ext2", 32000);
1084 retval += res;
1085 previous_devno = 1;
1086 current_devno = 5;
1087 } else {
1088 previous_devno = 0;
1089 current_devno = 1;
1090 }
1091 for (; current_devno < devno_we_must_allow_for; current_devno++) {
1092 sprintf(tmp, "Creating dummy partition %s%d", drivename, current_devno);
1093 log_it(tmp);
1094 g_maximum_progress++;
1095 res = partition_device(pout_to_fdisk,drivename, current_devno, previous_devno, OSSWAP("ext2", "ufs"), 32000);
1096 retval += res;
1097 previous_devno = current_devno;
1098 }
1099 paranoid_free(tmp);
1100 return (previous_devno);
1101}
1102
1103
1104/**
1105 * Decide whether @p mountlist contains any RAID devices.
1106 * @param mountlist The mountlist to examine.
1107 * @return TRUE if it does, FALSE if it doesn't.
1108 */
1109bool mountlist_contains_raid_devices(struct mountlist_itself * mountlist)
1110{
1111 /** int *************************************************************/
1112 int i;
1113 int matching = 0;
1114
1115 /** end **************************************************************/
1116
1117 assert(mountlist != NULL);
1118
1119 for (i = 0; i < mountlist->entries; i++) {
1120 if (strstr(mountlist->el[i].device, RAID_DEVICE_STUB)) {
1121 matching++;
1122 }
1123 }
1124 if (matching) {
1125 return (TRUE);
1126 } else {
1127 return (FALSE);
1128 }
1129}
1130
1131/* The following 2 functions are stolen from /usr/src/sbin/disklabel/disklabel.c */
1132#ifdef __FreeBSD__
1133static void display_disklabel(FILE * f, const struct disklabel *lp)
1134{
1135 int i, j;
1136 const struct partition *pp;
1137
1138 fprintf(f, "# %s\n", "Generated by Mondo Rescue");
1139 if (lp->d_type < DKMAXTYPES)
1140 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
1141 else
1142 fprintf(f, "type: %u\n", lp->d_type);
1143 fprintf(f, "disk: %.*s\n", (int) sizeof(lp->d_typename), lp->d_typename);
1144 fprintf(f, "label: %.*s\n", (int) sizeof(lp->d_packname), lp->d_packname);
1145 fprintf(f, "flags:");
1146 if (lp->d_flags & D_REMOVABLE)
1147 fprintf(f, " removeable");
1148 if (lp->d_flags & D_ECC)
1149 fprintf(f, " ecc");
1150 if (lp->d_flags & D_BADSECT)
1151 fprintf(f, " badsect");
1152 fprintf(f, "\n");
1153 fprintf(f, "bytes/sector: %lu\n", (u_long) lp->d_secsize);
1154 fprintf(f, "sectors/track: %lu\n", (u_long) lp->d_nsectors);
1155 fprintf(f, "tracks/cylinder: %lu\n", (u_long) lp->d_ntracks);
1156 fprintf(f, "sectors/cylinder: %lu\n", (u_long) lp->d_secpercyl);
1157 fprintf(f, "cylinders: %lu\n", (u_long) lp->d_ncylinders);
1158 fprintf(f, "sectors/unit: %lu\n", (u_long) lp->d_secperunit);
1159 fprintf(f, "rpm: %u\n", lp->d_rpm);
1160 fprintf(f, "interleave: %u\n", lp->d_interleave);
1161 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
1162 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
1163 fprintf(f, "headswitch: %lu\t\t# milliseconds\n", (u_long) lp->d_headswitch);
1164 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n", (u_long) lp->d_trkseek);
1165 fprintf(f, "drivedata: ");
1166 for (i = NDDATA - 1; i >= 0; i--)
1167 if (lp->d_drivedata[i])
1168 break;
1169 if (i < 0)
1170 i = 0;
1171 for (j = 0; j <= i; j++)
1172 fprintf(f, "%lu ", (u_long) lp->d_drivedata[j]);
1173 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
1174 fprintf(f, "# size offset fstype [fsize bsize bps/cpg]\n");
1175 pp = lp->d_partitions;
1176 for (i = 0; i < lp->d_npartitions; i++, pp++) {
1177 if (pp->p_size) {
1178 fprintf(f, " %c: %8lu %8lu ", 'a' + i, (u_long) pp->p_size, (u_long) pp->p_offset);
1179 if (pp->p_fstype < FSMAXTYPES)
1180 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
1181 else
1182 fprintf(f, "%8d", pp->p_fstype);
1183 switch (pp->p_fstype) {
1184
1185 case FS_UNUSED: /* XXX */
1186 fprintf(f, " %5lu %5lu %5.5s ", (u_long) pp->p_fsize, (u_long) (pp->p_fsize * pp->p_frag), "");
1187 break;
1188
1189 case FS_BSDFFS:
1190 fprintf(f, " %5lu %5lu %5u ", (u_long) pp->p_fsize, (u_long) (pp->p_fsize * pp->p_frag),
1191 pp->p_cpg);
1192 break;
1193
1194 case FS_BSDLFS:
1195 fprintf(f, " %5lu %5lu %5d", (u_long) pp->p_fsize, (u_long) (pp->p_fsize * pp->p_frag),
1196 pp->p_cpg);
1197 break;
1198
1199 default:
1200 fprintf(f, "%20.20s", "");
1201 break;
1202 }
1203 fprintf(f, "\t# (Cyl. %4lu", (u_long) (pp->p_offset / lp->d_secpercyl));
1204 if (pp->p_offset % lp->d_secpercyl)
1205 putc('*', f);
1206 else
1207 putc(' ', f);
1208 fprintf(f, "- %lu", (u_long) ((pp->p_offset + pp->p_size + lp->d_secpercyl - 1) / lp->d_secpercyl - 1));
1209 if (pp->p_size % lp->d_secpercyl)
1210 putc('*', f);
1211 fprintf(f, ")\n");
1212 }
1213 }
1214 fflush(f);
1215}
1216
1217static struct disklabel *get_virgin_disklabel(char *dkname)
1218{
1219 static struct disklabel loclab;
1220 struct partition *dp;
1221 char lnamebuf[BBSIZE];
1222 int f;
1223 u_int secsize, u;
1224 off_t mediasize;
1225
1226 (void) snprintf(lnamebuf, BBSIZE, "%s", dkname);
1227 if ((f = open(lnamebuf, O_RDONLY)) == -1) {
1228 warn("cannot open %s", lnamebuf);
1229 return (NULL);
1230 }
1231
1232 /* New world order */
1233 if ((ioctl(f, DIOCGMEDIASIZE, &mediasize) != 0)
1234 || (ioctl(f, DIOCGSECTORSIZE, &secsize) != 0)) {
1235 close(f);
1236 return (NULL);
1237 }
1238 memset(&loclab, 0, sizeof loclab);
1239 loclab.d_magic = DISKMAGIC;
1240 loclab.d_magic2 = DISKMAGIC;
1241 loclab.d_secsize = secsize;
1242 loclab.d_secperunit = mediasize / secsize;
1243
1244 /*
1245 * Nobody in these enligthened days uses the CHS geometry for
1246 * anything, but nontheless try to get it right. If we fail
1247 * to get any good ideas from the device, construct something
1248 * which is IBM-PC friendly.
1249 */
1250 if (ioctl(f, DIOCGFWSECTORS, &u) == 0)
1251 loclab.d_nsectors = u;
1252 else
1253 loclab.d_nsectors = 63;
1254 if (ioctl(f, DIOCGFWHEADS, &u) == 0)
1255 loclab.d_ntracks = u;
1256 else if (loclab.d_secperunit <= 63 * 1 * 1024)
1257 loclab.d_ntracks = 1;
1258 else if (loclab.d_secperunit <= 63 * 16 * 1024)
1259 loclab.d_ntracks = 16;
1260 else
1261 loclab.d_ntracks = 255;
1262 loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors;
1263 loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl;
1264 loclab.d_npartitions = MAXPARTITIONS;
1265
1266 /* Various (unneeded) compat stuff */
1267 loclab.d_rpm = 3600;
1268 loclab.d_bbsize = BBSIZE;
1269 loclab.d_interleave = 1;;
1270 strncpy(loclab.d_typename, "amnesiac", sizeof(loclab.d_typename));
1271
1272 dp = &loclab.d_partitions[RAW_PART];
1273 dp->p_size = loclab.d_secperunit;
1274 loclab.d_checksum = dkcksum(&loclab);
1275 close(f);
1276 return (&loclab);
1277}
1278/* End stolen from /usr/src/sbin/disklabel/disklabel.c. */
1279
1280char *canonical_name(char *drivename)
1281{
1282 if (drivename) {
1283 if (strncmp(drivename, "/dev/", 5) == 0) {
1284 return drivename + 5;
1285 }
1286 }
1287 return drivename;
1288}
1289
1290/**
1291 * (BSD only) Create a disklabel on @p drivename according to @p mountlist.
1292 * @param mountlist The mountlist to get the subpartition information from.
1293 * @param drivename The drive or slice to create a disklabel on.
1294 * @param ret If non-NULL, store the created disklabel here.
1295 * @return The number of errors encountered (0 for success).
1296 */
1297int label_drive_or_slice(struct mountlist_itself *mountlist, char *drivename, struct disklabel *ret)
1298{
1299 char subdev_str[MAX_STR_LEN];
1300 char command[MAX_STR_LEN];
1301 struct disklabel *lp;
1302 int i, lo = 0;
1303 int retval = 0;
1304 char c;
1305 FILE *ftmp;
1306
1307 lp = get_virgin_disklabel(drivename);
1308 for (c = 'a'; c <= 'z'; ++c) {
1309 int idx;
1310 sprintf(subdev_str, "%s%c", drivename, c);
1311 if ((idx = find_device_in_mountlist(mountlist, subdev_str)) < 0) {
1312 lp->d_partitions[c - 'a'].p_size = 0;
1313 lp->d_partitions[c - 'a'].p_fstype = FS_UNUSED;
1314 } else {
1315 lo = c - 'a';
1316 lp->d_partitions[c - 'a'].p_size = mountlist->el[idx].size * 2;
1317 lp->d_partitions[c - 'a'].p_fsize = 0;
1318 lp->d_partitions[c - 'a'].p_frag = 0;
1319 lp->d_partitions[c - 'a'].p_cpg = 0;
1320 if (!strcmp(mountlist->el[idx].format, "ufs")
1321 || !strcmp(mountlist->el[idx].format, "ffs")
1322 || !strcmp(mountlist->el[idx].format, "4.2BSD")) {
1323 lp->d_partitions[c - 'a'].p_fstype = FS_BSDFFS;
1324 lp->d_partitions[c - 'a'].p_fsize = 2048;
1325 lp->d_partitions[c - 'a'].p_frag = 8;
1326 lp->d_partitions[c - 'a'].p_cpg = 64;
1327 } else if (!strcasecmp(mountlist->el[idx].format, "raid")
1328 || !strcasecmp(mountlist->el[idx].format, "vinum")) {
1329 lp->d_partitions[c - 'a'].p_fstype = FS_VINUM;
1330 } else if (!strcmp(mountlist->el[idx].format, "swap")) {
1331 lp->d_partitions[c - 'a'].p_fstype = FS_SWAP;
1332 } else
1333 lp->d_partitions[c - 'a'].p_fstype = FS_OTHER;
1334 }
1335 }
1336
1337 // fix up the offsets
1338 lp->d_partitions[0].p_offset = 0;
1339 lp->d_partitions[RAW_PART].p_offset = 0;
1340 lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
1341 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1342
1343 for (i = 1; i < lp->d_npartitions; ++i) {
1344 int lastone;
1345 if ((i == RAW_PART) || (lp->d_partitions[i].p_size == 0))
1346 continue;
1347 for (lastone = i - 1; lastone >= 0; lastone--) {
1348 if ((lp->d_partitions[lastone].p_size)
1349 && (lastone != RAW_PART))
1350 break;
1351 }
1352 lp->d_partitions[i].p_offset = lp->d_partitions[lastone].p_offset + lp->d_partitions[lastone].p_size;
1353 }
1354 if (lp->d_partitions[lo].p_offset + lp->d_partitions[lo].p_size > lp->d_secperunit) {
1355 lp->d_partitions[lo].p_size = lp->d_secperunit - lp->d_partitions[lo].p_offset;
1356 }
1357
1358 ftmp = fopen("/tmp/disklabel", "w");
1359 display_disklabel(ftmp, lp);
1360 fclose(ftmp);
1361 sprintf(command, "disklabel -wr %s auto", canonical_name(drivename));
1362 retval += run_program_and_log_output(command, TRUE);
1363 sprintf(command, "disklabel -R %s /tmp/disklabel", canonical_name(drivename));
1364 retval += run_program_and_log_output(command, TRUE);
1365 if (ret)
1366 *ret = *lp;
1367 return retval;
1368}
1369#endif
1370
1371
1372/**
1373 * Partition @p drivename based on @p mountlist.
1374 * @param mountlist The mountlist to use to guide the partitioning.
1375 * @param drivename The drive to partition.
1376 * @return 0 for success, nonzero for failure.
1377 */
1378int partition_drive(struct mountlist_itself *mountlist, char *drivename)
1379{
1380 /** int *************************************************************/
1381 int current_devno;
1382 int previous_devno = 0;
1383 int lino;
1384 int retval = 0;
1385 int i;
1386 FILE*pout_to_fdisk=NULL;
1387
1388#ifdef __FreeBSD__
1389 bool fbsd_part = FALSE;
1390 char subdev_str[MAX_STR_LEN];
1391#endif
1392
1393 /** long long *******************************************************/
1394 long long partsize;
1395
1396 /** buffers *********************************************************/
1397 char *device_str;
1398 char *format;
1399 char *tmp;
1400
1401 /** end *************************************************************/
1402
1403 assert(mountlist != NULL);
1404 assert_string_is_neither_NULL_nor_zerolength(drivename);
1405
1406 malloc_string(device_str);
1407 malloc_string(format);
1408 malloc_string(tmp);
1409
1410 sprintf(tmp, "Partitioning drive %s", drivename);
1411 log_it(tmp);
1412
1413#if __FreeBSD__
1414 log_it("(Not opening fdisk now; that's the Linux guy's job)");
1415 pout_to_fdisk = NULL;
1416#else
1417 make_hole_for_file(FDISK_LOG);
1418#ifdef __IA64__
1419 sprintf(tmp, "parted2fdisk %s >> %s 2>> %s", drivename, FDISK_LOG, FDISK_LOG);
1420#else
1421 sprintf(tmp, "fdisk %s >> %s 2>> %s", drivename, FDISK_LOG, FDISK_LOG);
1422#endif
1423 pout_to_fdisk = popen(tmp, "w");
1424 if (!pout_to_fdisk) {
1425 log_to_screen("Cannot call fdisk to configure %s", drivename);
1426 paranoid_free(device_str);
1427 paranoid_free(format);
1428 paranoid_free(tmp);
1429 return(1);
1430 }
1431#endif
1432 for (current_devno = 1; current_devno < 99; current_devno++) {
1433 build_partition_name(device_str, drivename, current_devno);
1434 lino = find_device_in_mountlist(mountlist, device_str);
1435
1436 if (lino < 0) {
1437 // device not found in mountlist
1438#if __FreeBSD__
1439 // If this is the first partition (just as a sentinel value),
1440 // then see if the user has picked 'dangerously-dedicated' mode.
1441 // If so, then we just call label_drive_or_slice() and return.
1442 char c;
1443 if (current_devno == 1) {
1444 // try DangerouslyDedicated mode
1445 for (c = 'a'; c <= 'z'; c++) {
1446 sprintf(subdev_str, "%s%c", drivename, c);
1447 if (find_device_in_mountlist(mountlist, subdev_str) > 0) {
1448 fbsd_part = TRUE;
1449 }
1450 }
1451 if (fbsd_part) {
1452 int r = label_drive_or_slice(mountlist,
1453 drivename,
1454 0);
1455 char command[MAX_STR_LEN];
1456 sprintf(command, "disklabel -B %s", basename(drivename));
1457 if (system(command)) {
1458 log_to_screen("Warning! Unable to make the drive bootable.");
1459 }
1460 paranoid_free(device_str);
1461 paranoid_free(format);
1462 paranoid_free(tmp);
1463 return r;
1464 }
1465 }
1466 for (c = 'a'; c <= 'z'; c++) {
1467 sprintf(subdev_str, "%s%c", device_str, c);
1468 if (find_device_in_mountlist(mountlist, subdev_str) > 0) {
1469 fbsd_part = TRUE;
1470 }
1471 }
1472 // Now we check the subpartitions of the current partition.
1473 if (fbsd_part) {
1474 int i, line;
1475
1476 strcpy(format, "ufs");
1477 partsize = 0;
1478 for (i = 'a'; i < 'z'; ++i) {
1479 sprintf(subdev_str, "%s%c", device_str, i);
1480 line = find_device_in_mountlist(mountlist, subdev_str);
1481 if (line > 0) {
1482 // We found one! Add its size to the total size.
1483 partsize += mountlist->el[line].size;
1484 }
1485 }
1486 } else {
1487 continue;
1488 }
1489#else
1490 continue;
1491#endif
1492 }
1493
1494 /* OK, we've found partition /dev/hdxN in mountlist; let's prep it */
1495 /* For FreeBSD, that is /dev/adXsY */
1496
1497 log_it("Found partition %s in mountlist", device_str);
1498 if (!previous_devno) {
1499
1500 log_it("Wiping %s's partition table", drivename);
1501#if __FreeBSD__
1502 // FreeBSD doesn't let you write to blk devices in <512byte chunks.
1503// sprintf(tmp, "dd if=/dev/zero of=%s count=1 bs=512", drivename);
1504// if (run_program_and_log_output(tmp, TRUE)) {
1505 file = open(drivename, O_WRONLY);
1506 if (!file) {
1507 sprintf(tmp, "Warning - unable to open %s for wiping it's partition table", drivename);
1508 log_to_screen(tmp);
1509 }
1510
1511 for (i=0; i<512; i++) {
1512 if ( !write(file, "\0", 1) ) {
1513 sprintf(tmp, "Warning - unable to write to %s", drivename);
1514 log_to_screen(tmp);
1515 }
1516 }
1517 system("sync");
1518#else
1519 iamhere("New, kernel-friendly partition remover");
1520 for(i=20;i>0;i--)
1521 {
1522 fprintf(pout_to_fdisk, "d\n%d\n", i);
1523 fflush(pout_to_fdisk);
1524 }
1525// sprintf(tmp, "dd if=/dev/zero of=%s count=1 bs=512", drivename);
1526// run_program_and_log_output(tmp, 1);
1527#endif
1528 if (current_devno > 1) {
1529 previous_devno = make_dummy_partitions(pout_to_fdisk,drivename, current_devno);
1530 }
1531 }
1532#ifdef __FreeBSD__
1533 if (!fbsd_part) {
1534#endif
1535
1536 strcpy(format, mountlist->el[lino].format);
1537 partsize = mountlist->el[lino].size;
1538
1539#ifdef __FreeBSD__
1540 }
1541#endif
1542
1543#ifndef __IA64__
1544 if (current_devno == 5 && previous_devno == 4) {
1545 log_to_screen("You must leave at least one partition spare as the Extended partition.");
1546 paranoid_free(device_str);
1547 paranoid_free(format);
1548 paranoid_free(tmp);
1549 return (1);
1550 }
1551#endif
1552
1553 retval += partition_device(pout_to_fdisk, drivename, current_devno, previous_devno, format, partsize);
1554
1555#ifdef __FreeBSD__
1556 if ((current_devno <= 4) && fbsd_part) {
1557 sprintf(tmp, "disklabel -B %s", basename(device_str));
1558 retval += label_drive_or_slice(mountlist, device_str, 0);
1559 if (system(tmp)) {
1560 log_to_screen("Warning! Unable to make the slice bootable.");
1561 }
1562 }
1563#endif
1564
1565 previous_devno = current_devno;
1566 }
1567
1568 if (pout_to_fdisk)
1569 {
1570// mark relevant partition as bootable
1571 sprintf(tmp, "a\n%s\n", call_program_and_get_last_line_of_output("make-me-bootable /tmp/mountlist.txt dummy"));
1572 fput_string_one_char_at_a_time(pout_to_fdisk, tmp);
1573// close fdisk
1574 fput_string_one_char_at_a_time(pout_to_fdisk, "w\n");
1575 system("sync");
1576 paranoid_pclose(pout_to_fdisk);
1577 log_msg(0, "------------------- fdisk.log looks like this ------------------");
1578 sprintf(tmp, "cat %s >> %s", FDISK_LOG, MONDO_LOGFILE);
1579 system(tmp);
1580 log_msg(0, "------------------- end of fdisk.log... word! ------------------");
1581 sprintf(tmp, "tail -n6 %s | fgrep \"16: \"", FDISK_LOG);
1582 if (!run_program_and_log_output(tmp, 5))
1583 {
1584 g_partition_table_locked_up++;
1585 log_to_screen("A flaw in the Linux kernel has locked the partition table.");
1586 }
1587 }
1588 paranoid_free(device_str);
1589 paranoid_free(format);
1590 paranoid_free(tmp);
1591 return (retval);
1592}
1593
1594/**
1595 * Create partition number @p partno on @p drive with @p fdisk.
1596 * @param drive The drive to create the partition on.
1597// * @param partno The partition number of the new partition (1-4 are primary, >=5 is logical).
1598 * @param prev_partno The partition number of the most recently prepped partition.
1599 * @param format The filesystem type of this partition (used to set the type).
1600 * @param partsize The size of the partition in @e bytes.
1601 * @return 0 for success, nonzero for failure.
1602 */
1603int partition_device(FILE*pout_to_fdisk, const char *drive, int partno, int prev_partno, const char *format, long long partsize)
1604{
1605 /** int **************************************************************/
1606 int retval = 0;
1607 int res = 0;
1608
1609 /** buffers **********************************************************/
1610 char *program;
1611 char *partition_name;
1612 char *tmp;
1613 char *logfile;
1614 char *output;
1615
1616 /** pointers **********************************************************/
1617 char *p;
1618 char *part_table_fmt;
1619 FILE *fout;
1620
1621 /** end ***************************************************************/
1622
1623 malloc_string(program);
1624 malloc_string(partition_name);
1625 malloc_string(tmp);
1626 malloc_string(logfile);
1627 malloc_string(output);
1628
1629 assert_string_is_neither_NULL_nor_zerolength(drive);
1630 assert(format != NULL);
1631
1632 log_it("partition_device('%s', %d, %d, '%s', %lld) --- starting", drive, partno, prev_partno, format, partsize);
1633
1634 if (!strncmp(drive, RAID_DEVICE_STUB, strlen(RAID_DEVICE_STUB))) {
1635 sprintf(tmp, "Not partitioning %s - it is a virtual drive", drive);
1636 log_it(tmp);
1637 paranoid_free(program);
1638 paranoid_free(partition_name);
1639 paranoid_free(tmp);
1640 paranoid_free(logfile);
1641 paranoid_free(output);
1642 return (0);
1643 }
1644 build_partition_name(partition_name, drive, partno);
1645 if (partsize <= 0) {
1646 sprintf(tmp, "Partitioning device %s (max size)", partition_name);
1647 } else {
1648 sprintf(tmp, "Partitioning device %s (%lld MB)", partition_name, (long long) partsize / 1024);
1649 }
1650 update_progress_form(tmp);
1651 log_it(tmp);
1652
1653 if (is_this_device_mounted(partition_name)) {
1654 sprintf(tmp, "%s is mounted, and should not be partitioned", partition_name);
1655 log_to_screen(tmp);
1656 paranoid_free(program);
1657 paranoid_free(partition_name);
1658 paranoid_free(tmp);
1659 paranoid_free(logfile);
1660 paranoid_free(output);
1661 return (1);
1662/*
1663 } else if (does_partition_exist(drive, partno)) {
1664 sprintf(tmp, "%s already has a partition", partition_name);
1665 log_to_screen(tmp);
1666 return (1);
1667*/
1668 }
1669
1670
1671 /* sprintf(tmp,"Partitioning %s ",partition_name); */
1672 /* mvaddstr_and_log_it(g_currentY+1,30,tmp); */
1673 p = (char *) strrchr(partition_name, '/');
1674 sprintf(logfile, "/tmp/fdisk.log.%s", ++p);
1675 sprintf(program, "parted2fdisk %s >> %s 2>> %s", drive, MONDO_LOGFILE, MONDO_LOGFILE);
1676
1677 output[0] = '\0';
1678 /* make it a primary/extended/logical */
1679 if (partno <= 4) {
1680 sprintf(output + strlen(output), "n\np\n%d\n", partno);
1681 } else {
1682 if (partno == 5) {
1683 part_table_fmt = which_partition_format(drive);
1684 /* GPT allows more than 4 primary partitions */
1685 if ((prev_partno >= 4) && (strcmp(part_table_fmt,"MBR") == 0)) {
1686 log_to_screen("You need to leave at least one partition free, for 'extended/logical'");
1687 paranoid_free(program);
1688 paranoid_free(partition_name);
1689 paranoid_free(tmp);
1690 paranoid_free(logfile);
1691 paranoid_free(output);
1692 return (1);
1693 } else {
1694 sprintf(output + strlen(output), "n\ne\n%d\n\n\n", prev_partno + 1);
1695 }
1696 }
1697 strcat(output + strlen(output), "n\nl\n");
1698 }
1699 strcat(output + strlen(output), "\n"); /*start block (ENTER for next free blk */
1700 if (partsize > 0) {
1701 if (!strcmp(format, "7")) { log_msg(1, "Adding 512K, just in case"); partsize+=512; }
1702 sprintf(output + strlen(output), "+%lldK", (long long) (partsize));
1703 }
1704 strcat(output + strlen(output), "\n");
1705#if 0
1706/*
1707#endif
1708 sprintf(tmp,"PARTSIZE = +%ld",(long)partsize);
1709 log_it(tmp);
1710 log_it("---fdisk command---");
1711 log_it(output);
1712 log_it("---end of fdisk---");
1713#if 0
1714*/
1715#endif
1716
1717
1718 if (pout_to_fdisk)
1719 {
1720 log_msg(1, "Doing the new all-in-one fdisk thing");
1721 log_msg(1, "output = '%s'", output);
1722 fput_string_one_char_at_a_time(pout_to_fdisk, output);
1723 fput_string_one_char_at_a_time(pout_to_fdisk, "\n\np\n");
1724 strcpy(tmp, last_line_of_file(FDISK_LOG));
1725 if (strstr(tmp, " (m "))
1726 {
1727 log_msg(1, "Successfully created %s%d", drive, partno);
1728 }
1729 else
1730 {
1731 log_msg(1, "last line = %s", tmp);
1732 log_msg(1, "Failed to create %s%d; sending 'Enter'...", drive, partno);
1733 }
1734 if (!retval)
1735 {
1736 log_msg(1, "Trying to set %s%d's partition type now", drive, partno);
1737 retval = set_partition_type(pout_to_fdisk, drive, partno, format, partsize);
1738 if (retval)
1739 {
1740 log_msg(1, "Failed. Trying again...");
1741 retval = set_partition_type(pout_to_fdisk, drive, partno, format, partsize);
1742 }
1743 }
1744 if (retval) { log_msg(1, "...but failed to set type"); }
1745 }
1746 else
1747 {
1748 strcat(output, "w\n\n");
1749 if (g_fprep) { fprintf(g_fprep, "echo \"%s\" | %s\n", output, program); }
1750 /* write to disk; close fdisk's stream */
1751 if (!(fout = popen(program, "w"))) {
1752 log_OS_error("can't popen-out to program");
1753 } else {
1754 fputs(output, fout);
1755 paranoid_pclose(fout);
1756 }
1757 if (!does_partition_exist(drive, partno) && partsize > 0) {
1758 log_it("Vaccum-packing");
1759 g_current_progress--;
1760 res = partition_device(pout_to_fdisk, drive, partno, prev_partno, format, -1);
1761 if (res) {
1762 sprintf(tmp, "Failed to vacuum-pack %s", partition_name);
1763 log_it(tmp);
1764 retval++;
1765 } else {
1766 retval = 0;
1767 }
1768 }
1769 if (does_partition_exist(drive, partno)) {
1770 retval = set_partition_type(pout_to_fdisk, drive, partno, format, partsize);
1771 if (retval) {
1772 sprintf(tmp, "Partitioned %s but failed to set its type", partition_name);
1773 log_it(tmp);
1774 } else {
1775 if (partsize > 0) {
1776 sprintf(tmp, "Partition %s created+configured OK", partition_name);
1777 log_to_screen(tmp);
1778 } else {
1779 log_it("Returning from a successful vacuum-pack");
1780 }
1781 }
1782 } else {
1783 sprintf(tmp, "Failed to partition %s", partition_name);
1784 if (partsize > 0) {
1785 log_to_screen(tmp);
1786 } else {
1787 log_it(tmp);
1788 }
1789 retval++;
1790 }
1791 }
1792 g_current_progress++;
1793 log_it("partition_device() --- leaving");
1794 paranoid_free(program);
1795 paranoid_free(partition_name);
1796 paranoid_free(tmp);
1797 paranoid_free(logfile);
1798 paranoid_free(output);
1799 return (retval);
1800}
1801
1802
1803
1804/**
1805 * Create all partitions listed in @p mountlist.
1806 * @param mountlist The mountlist to use to guide the partitioning.
1807 * @return The number of errors encountered (0 for success).
1808 * @note This sets the partition types but doesn't actually do the formatting.
1809 * Use format_everything() for that.
1810 */
1811int partition_everything(struct mountlist_itself *mountlist)
1812{
1813 /** int ************************************************************/
1814 int lino;
1815 int retval = 0;
1816 int i;
1817 int res;
1818
1819 /** buffer *********************************************************/
1820 struct list_of_disks *drivelist;
1821 /* struct mountlist_itself new_mtlist, *mountlist; */
1822
1823 /** end ************************************************************/
1824
1825 drivelist = malloc(sizeof(struct list_of_disks));
1826 assert(mountlist != NULL);
1827
1828 log_it("partition_everything() --- starting");
1829 mvaddstr_and_log_it(g_currentY, 0, "Partitioning hard drives ");
1830 /* mountlist=orig_mtlist; */
1831 if (mountlist_contains_raid_devices(mountlist)) {
1832 /* mountlist=&new_mtlist; */
1833 /* extrapolate_mountlist_to_include_raid_partitions(mountlist,orig_mtlist); */
1834 log_msg(0,"Mountlist, including the partitions incorporated in RAID devices:-");
1835 for (i = 0; i < mountlist->entries; i++) {
1836 log_it(mountlist->el[i].device);
1837 }
1838 log_msg(0, "End of mountlist.");
1839 }
1840 log_msg(0, "Stopping all LVMs, just in case");
1841 if (!g_text_mode) { newtSuspend(); }
1842 do_my_funky_lvm_stuff(TRUE, FALSE); // just remove old partitions
1843 if (!g_text_mode) { newtResume(); }
1844 log_msg(0, "Stopping all software RAID devices, just in case");
1845 stop_all_raid_devices(mountlist);
1846 log_msg(0, "Done.");
1847
1848/*
1849 if (does_file_exist("/tmp/i-want-my-lvm"))
1850 {
1851 wipe_MBRs_and_reboot_if_necessary(mountlist); // i.e. if it wasn't done recently
1852 }
1853*/
1854
1855 open_progress_form("Partitioning devices", "I am now going to partition all your drives.",
1856 "This should not take more than five minutes.", "", mountlist->entries);
1857
1858 make_list_of_drives_in_mountlist(mountlist, drivelist);
1859
1860 /* partition each drive */
1861 for (lino = 0; lino < drivelist->entries; lino++) {
1862 res = partition_drive(mountlist, drivelist->el[lino].device);
1863 retval += res;
1864 }
1865 close_progress_form();
1866 if (retval) {
1867 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
1868 log_to_screen("Errors occurred during the partitioning of your hard drives.");
1869 } else {
1870 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1871 paranoid_system("rm -f /tmp/fdisk*.log 2> /dev/null");
1872 }
1873 newtSuspend();
1874 system("clear");
1875 newtResume();
1876 paranoid_free(drivelist);
1877 return (retval);
1878}
1879
1880
1881
1882
1883
1884
1885/**
1886 * Set the type of partition number @p partno on @p drive to @p format.
1887 * @param drive The drive to change the type of a partition on.
1888 * @param partno The partition number on @p drive to change the type of.
1889 * @param format The filesystem type this partition will eventually contain.
1890 * @param partsize The size of this partition, in @e bytes (used for vfat
1891 * type calculations).
1892 * @return 0 for success, nonzero for failure.
1893 */
1894int set_partition_type(FILE *pout_to_fdisk, const char *drive, int partno, const char *format, long long partsize)
1895{
1896 /** buffers *********************************************************/
1897 char *partition;
1898 char *command;
1899 char *output;
1900 char *tmp;
1901 char *partcode;
1902 char *logfile;
1903
1904 /** pointers *********************************************************/
1905 char *p;
1906 FILE *fout;
1907
1908 /** int **************************************************************/
1909 int res = 0;
1910
1911 /** end **************************************************************/
1912
1913 assert_string_is_neither_NULL_nor_zerolength(drive);
1914 assert(format != NULL);
1915
1916 malloc_string(partition);
1917 malloc_string(command);
1918 malloc_string(output);
1919 malloc_string(tmp);
1920 malloc_string(partcode);
1921 malloc_string(logfile);
1922
1923 build_partition_name(partition, drive, partno);
1924 p = (char *) strrchr(partition, '/');
1925 sprintf(logfile, "/tmp/fdisk-set-type.%s.log", ++p);
1926 if (strcmp(format, "swap") == 0) {
1927 strcpy(partcode, "82");
1928 } else if (strcmp(format, "vfat") == 0) {
1929 if (partsize / 1024 > 8192) {
1930 strcpy(partcode, "c");
1931 } else {
1932 strcpy(partcode, "b");
1933 }
1934 } else if (strcmp(format, "ext2") == 0 || strcmp(format, "reiserfs") == 0 || strcmp(format, "ext3") == 0
1935 || strcmp(format, "xfs") == 0 || strcmp(format, "jfs") == 0) {
1936 strcpy(partcode, "83");
1937 } else if (strcmp(format, "minix") == 0) {
1938 strcpy(partcode, "81");
1939 } else if (strcmp(format, "raid") == 0) {
1940 strcpy(partcode, "fd");
1941 } else if ((strcmp(format, "ufs") == 0)
1942 || (strcmp(format, "ffs") == 0)) { /* raid autodetect */
1943 strcpy(partcode, "a5");
1944 } else if (strcmp(format, "lvm") == 0) {
1945 strcpy(partcode, "8e");
1946 } else if (format[0] == '\0') { /* LVM physical partition */
1947 partcode[0] = '\0';
1948 } else if (strlen(format) >= 1 && strlen(format) <= 2) {
1949 strcpy(partcode, format);
1950 } else {
1951 /* probably an image */
1952 sprintf(tmp, "Unknown format ('%s') - using supplied string anyway", format);
1953 mvaddstr_and_log_it(g_currentY++, 0, tmp);
1954#ifdef __FreeBSD__
1955 strcpy(partcode, format); // was a5
1956#else
1957 strcpy(partcode, format); // was 83
1958#endif
1959 }
1960 sprintf(tmp, "Setting %s's type to %s (%s)", partition, format, partcode);
1961 log_msg(1, tmp);
1962 if (partcode[0] != '\0' && strcmp(partcode, "83")) { /* no need to set type if 83: 83 is default */
1963
1964 if (pout_to_fdisk)
1965 {
1966 res = 0;
1967 fput_string_one_char_at_a_time(pout_to_fdisk, "t\n");
1968 if (partno > 1 || strstr(last_line_of_file(FDISK_LOG), " (1-4)"))
1969 {
1970 log_msg(5, "Specifying partno (%d) - yay", partno);
1971 sprintf(tmp, "%d\n", partno);
1972 fput_string_one_char_at_a_time(pout_to_fdisk, tmp);
1973 log_msg(5, "A - last line = '%s'", last_line_of_file(FDISK_LOG));
1974 }
1975
1976 sprintf(tmp, "%s\n", partcode);
1977 fput_string_one_char_at_a_time(pout_to_fdisk, tmp);
1978 log_msg(5, "B - last line = '%s'", last_line_of_file(FDISK_LOG));
1979 fput_string_one_char_at_a_time(pout_to_fdisk, "\n");
1980 log_msg(5, "C - last line = '%s'", last_line_of_file(FDISK_LOG));
1981
1982 strcpy(tmp, last_line_of_file(FDISK_LOG));
1983 if (!strstr(tmp, " (m "))
1984 {
1985 log_msg(1, "last line = '%s'; part type set failed", tmp);
1986 res++;
1987 fput_string_one_char_at_a_time(pout_to_fdisk,"\n");
1988 }
1989 fput_string_one_char_at_a_time(pout_to_fdisk,"p\n");
1990 }
1991 else
1992 {
1993 sprintf(output, "t\n%d\n%s\n", partno, partcode);
1994 strcat(output, "w\n");
1995 sprintf(command, "parted2fdisk %s >> %s 2>> %s", drive, MONDO_LOGFILE, MONDO_LOGFILE);
1996 log_msg(5, "output = '%s'", output);
1997 log_msg(5, "partno=%d; partcode=%s", partno, partcode);
1998 log_msg(5, "command = '%s'", command);
1999 fout = popen(command, "w");
2000 if (!fout) {
2001 log_OS_error(command);
2002 res = 1;
2003 } else {
2004 res = 0;
2005 fprintf(fout, output);
2006 paranoid_pclose(fout);
2007 }
2008 }
2009 if (res)
2010 {
2011 log_OS_error(command);
2012 }
2013 }
2014
2015 paranoid_free(partition);
2016 paranoid_free(command);
2017 paranoid_free(output);
2018 paranoid_free(tmp);
2019 paranoid_free(partcode);
2020 paranoid_free(logfile);
2021
2022 return (res);
2023}
2024
2025
2026int start_raid_device(char *raid_device)
2027{
2028 /** int *************************************************************/
2029 int res;
2030 int retval = 0;
2031
2032 /** buffers *********************************************************/
2033 char *program;
2034
2035 /** end *************************************************************/
2036
2037 assert_string_is_neither_NULL_nor_zerolength(raid_device);
2038 malloc_string(program);
2039
2040#ifdef __FreeBSD__
2041 if (is_this_device_mounted(raid_device)) {
2042 log_it("Can't start %s when it's mounted!", raid_device);
2043 return 1;
2044 }
2045 sprintf(program, "vinum start -f %s", raid_device);
2046#else
2047 sprintf(program, "raidstart %s", raid_device);
2048// sprintf (program, "raidstart " RAID_DEVICE_STUB "*");
2049#endif
2050 log_msg(1, "program = %s", program);
2051 res = run_program_and_log_output(program, 1);
2052 if (g_fprep) { fprintf(g_fprep, "%s\n", program); }
2053 if (res) { log_msg(1, "Warning - failed to start RAID device %s", raid_device); }
2054 retval += res;
2055 sleep(1);
2056 return (retval);
2057}
2058
2059
2060
2061/**
2062 * Stop @p raid_device using @p raidstop.
2063 * @param raid_device The software RAID device to stop.
2064 * @return 0 for success, nonzero for failure.
2065 */
2066int stop_raid_device(char *raid_device)
2067{
2068 /** int *************************************************************/
2069 int res;
2070 int retval = 0;
2071
2072 /** buffers *********************************************************/
2073 char *program;
2074
2075 /** end *************************************************************/
2076
2077 assert_string_is_neither_NULL_nor_zerolength(raid_device);
2078 malloc_string(program);
2079
2080#ifdef __FreeBSD__
2081 if (is_this_device_mounted(raid_device)) {
2082 log_it("Can't stop %s when it's mounted!", raid_device);
2083 return 1;
2084 }
2085 sprintf(program, "vinum stop -f %s", raid_device);
2086#else
2087 sprintf(program, "raidstop %s", raid_device);
2088// sprintf (program, "raidstop " RAID_DEVICE_STUB "*");
2089#endif
2090 log_msg(1, "program = %s", program);
2091 res = run_program_and_log_output(program, 1);
2092 if (g_fprep) { fprintf(g_fprep, "%s\n", program); }
2093 if (res) { log_msg(1, "Warning - failed to stop RAID device %s", raid_device); }
2094 retval += res;
2095 return (retval);
2096}
2097
2098
2099int start_all_raid_devices(struct mountlist_itself *mountlist)
2100{
2101 int i;
2102 int retval=0;
2103 int res;
2104
2105 for(i=0; i<mountlist->entries; i++)
2106 {
2107 if (!strncmp(mountlist->el[i].device, RAID_DEVICE_STUB, strlen(RAID_DEVICE_STUB)))
2108 {
2109 log_msg(1, "Starting %s", mountlist->el[i].device);
2110 res = start_raid_device(mountlist->el[i].device);
2111 retval += res;
2112 }
2113 }
2114 if (retval) { log_msg(1, "Started all s/w raid devices OK"); }
2115 else { log_msg(1, "Failed to start some/all s/w raid devices"); }
2116 return(retval);
2117}
2118
2119/**
2120 * Stop all software RAID devices listed in @p mountlist.
2121 * @param mountlist The mountlist to stop the RAID devices in.
2122 * @return The number of errors encountered (0 for success).
2123 * @bug @p mountlist is not used.
2124 */
2125int stop_all_raid_devices(struct mountlist_itself *mountlist)
2126{
2127 /** int *************************************************************/
2128 int retval = 0;
2129#ifndef __FreeBSD__
2130 int res;
2131#endif
2132
2133 /** char ************************************************************/
2134 char *incoming;
2135#ifndef __FreeBSD__
2136 char *dev;
2137#endif
2138 /** pointers ********************************************************/
2139#ifndef __FreeBSD__
2140 char *p;
2141#endif
2142 FILE *fin;
2143 int i;
2144
2145 /** end ****************************************************************/
2146
2147 malloc_string(dev);
2148 malloc_string(incoming);
2149 assert(mountlist != NULL);
2150
2151 for (i = 0; i < 3; i++) {
2152#ifdef __FreeBSD__
2153 fin = popen("vinum list | grep '^[PVS]' | sed 's/S/1/;s/P/2/;s/V/3/' | sort | cut -d' ' -f2", "r");
2154 if (!fin) {
2155 paranoid_free(dev);
2156 paranoid_free(incoming);
2157 return (1);
2158 }
2159 for (fgets(incoming, MAX_STR_LEN - 1, fin); !feof(fin); fgets(incoming, MAX_STR_LEN - 1, fin)) {
2160 retval += stop_raid_device(incoming);
2161 }
2162#else
2163 fin = fopen("/proc/mdstat", "r");
2164 if (!fin) {
2165 log_OS_error("/proc/mdstat");
2166 paranoid_free(dev);
2167 paranoid_free(incoming);
2168 return (1);
2169 }
2170 for (fgets(incoming, MAX_STR_LEN - 1, fin); !feof(fin); fgets(incoming, MAX_STR_LEN - 1, fin)) {
2171 for (p = incoming; *p != '\0' && (*p != 'm' || *(p + 1) != 'd' || !isdigit(*(p + 2))); p++);
2172 if (*p != '\0') {
2173 sprintf(dev, "/dev/%s", p);
2174 for (p = dev; *p > 32; p++);
2175 *p = '\0';
2176 res = stop_raid_device(dev);
2177 }
2178 }
2179#endif
2180 }
2181 paranoid_fclose(fin);
2182 if (retval) { log_msg(1, "Warning - unable to stop some RAID devices"); }
2183 paranoid_free(dev);
2184 paranoid_free(incoming);
2185 system("sync"); system("sync"); system("sync");
2186 sleep(1);
2187 return (retval);
2188}
2189
2190
2191
2192/**
2193 * Decide which command we need to use to format a device of type @p format.
2194 * @param format The filesystem type we are about to format.
2195 * @param program Where to put the binary name for this format.
2196 * @return 0 for success, nonzero for failure.
2197 */
2198int which_format_command_do_i_need(char *format, char *program)
2199{
2200 /** int *************************************************************/
2201 int res = 0;
2202
2203 /** buffers *********************************************************/
2204 char *tmp;
2205
2206 /** end ***************************************************************/
2207
2208 malloc_string(tmp);
2209 assert_string_is_neither_NULL_nor_zerolength(format);
2210 assert(program != NULL);
2211
2212 if (strcmp(format, "swap") == 0) {
2213#ifdef __FreeBSD__
2214 strcpy(program, "true");
2215#else
2216 strcpy(program, "mkswap");
2217#endif
2218 } else if (strcmp(format, "vfat") == 0) {
2219 strcpy(program, "format-and-kludge-vfat");
2220#ifndef __FreeBSD__
2221 } else if (strcmp(format, "reiserfs") == 0) {
2222 strcpy(program, "mkreiserfs -ff");
2223 } else if (strcmp(format, "xfs") == 0) {
2224 strcpy(program, "mkfs.xfs -f -q");
2225 } else if (strcmp(format, "jfs") == 0) {
2226 strcpy(program, "mkfs.jfs");
2227 } else if (strcmp(format, "ext3") == 0) {
2228 strcpy(program, "mkfs -t ext2 -F -j -q");
2229 } else if (strcmp(format, "minix") == 0) {
2230 strcpy(program, "mkfs.minix");
2231#endif
2232 } else if (strcmp(format, "ext2") == 0) {
2233 strcpy(program, "mke2fs -F -q");
2234 } else {
2235#ifdef __FreeBSD__
2236 sprintf(program, "newfs_%s", format);
2237#else
2238 sprintf(program, "mkfs -t %s -c", format); // -c checks for bad blocks
2239#endif
2240 sprintf(tmp, "Unknown format (%s) - assuming '%s' will do", format, program);
2241 log_it(tmp);
2242 res = 0;
2243 }
2244 paranoid_free(tmp);
2245 return (res);
2246}
2247
2248
2249/**
2250 * Calculate the probable size of @p drive_name by adding up sizes in
2251 * @p mountlist.
2252 * @param mountlist The mountlist to use to calculate the size.
2253 * @param drive_name The drive to calculate the original size of.
2254 * @return The size of @p drive_name in kilobytes.
2255 */
2256long calc_orig_size_of_drive_from_mountlist(struct mountlist_itself
2257 *mountlist, char *drive_name)
2258{
2259 /** long ************************************************************/
2260 long original_size_of_drive;
2261
2262 /** int *************************************************************/
2263 int partno;
2264
2265 /** buffers *********************************************************/
2266 char *tmp;
2267
2268 /** end *************************************************************/
2269
2270 malloc_string(tmp);
2271 assert(mountlist != NULL);
2272 assert_string_is_neither_NULL_nor_zerolength(drive_name);
2273
2274 for (original_size_of_drive = 0, partno = 0; partno < mountlist->entries; partno++) {
2275 if (strncmp(mountlist->el[partno].device, drive_name, strlen(drive_name)) == 0) {
2276 original_size_of_drive += mountlist->el[partno].size;
2277 } else {
2278 sprintf(tmp, "Skipping %s", mountlist->el[partno].device);
2279// log_to_screen(tmp);
2280 }
2281 }
2282 original_size_of_drive = original_size_of_drive / 1024;
2283 paranoid_free(tmp);
2284 return (original_size_of_drive);
2285}
2286
2287
2288/**
2289 * Resize a drive's entries in @p mountlist proportionately to fit its new size.
2290 * There are a few problems with this function:
2291 * - It won't work if there was any unallocated space on the user's hard drive
2292 * when it was backed up.
2293 * - It won't work if the user's hard drive lies about its size (more common
2294 * than you'd think).
2295 *
2296 * @param mountlist The mountlist to use for resizing @p drive_name.
2297 * @param drive_name The drive to resize.
2298 */
2299void resize_drive_proportionately_to_suit_new_drives(struct mountlist_itself
2300 *mountlist, char *drive_name)
2301{
2302 /**buffers **********************************************************/
2303 char *tmp;
2304
2305 /** int *************************************************************/
2306 int partno, lastpart;
2307 /** remove driveno, noof_drives stan benoit apr 2002**/
2308
2309 /** float ***********************************************************/
2310 float factor;
2311 float new_size;
2312// float newcylinderno;
2313
2314 /** long *************************************************************/
2315 long newsizL;
2316 long current_size_of_drive = 0;
2317 long original_size_of_drive = 0;
2318 long final_size; /* all in Megabytes */
2319 struct mountlist_reference *drivemntlist;
2320
2321 /** structures *******************************************************/
2322
2323 /** end **************************************************************/
2324
2325 malloc_string(tmp);
2326 assert(mountlist != NULL);
2327 assert_string_is_neither_NULL_nor_zerolength(drive_name);
2328
2329 if (strlen(drive_name) >= strlen(RAID_DEVICE_STUB)) {
2330 if (strncmp(drive_name, RAID_DEVICE_STUB, strlen(RAID_DEVICE_STUB)) == 0) {
2331 paranoid_free(tmp);
2332 return;
2333 }
2334 }
2335
2336 /*
2337 sprintf (tmp, "cp -f %s %s.pre-resize", g_mountlist_fname, g_mountlist_fname);
2338 run_program_and_log_output (tmp, FALSE);
2339 */
2340
2341 current_size_of_drive = get_phys_size_of_drive(drive_name);
2342
2343 if (current_size_of_drive <= 0) {
2344 log_it("Not resizing to match %s - can't find drive", drive_name);
2345 paranoid_free(tmp);
2346 return;
2347 }
2348 sprintf(tmp, "Expanding entries to suit drive %s (%ld MB)", drive_name, current_size_of_drive);
2349 log_to_screen(tmp);
2350
2351 drivemntlist = malloc(sizeof (struct mountlist_reference));
2352 drivemntlist->el = malloc(sizeof(struct mountlist_line *) * MAX_TAPECATALOG_ENTRIES);
2353
2354 if (!drivemntlist) {
2355 fatal_error("Cannot malloc temporary mountlist\n");
2356 }
2357 create_mountlist_for_drive(mountlist, drive_name, drivemntlist);
2358
2359 for (partno = 0; partno < drivemntlist->entries; partno++) {
2360 original_size_of_drive += drivemntlist->el[partno]->size;
2361 }
2362 original_size_of_drive = original_size_of_drive / 1024;
2363
2364 if (original_size_of_drive <= 0) {
2365 sprintf(tmp, "Cannot resize %s's entries. Drive not found.", drive_name);
2366 log_to_screen(tmp);
2367 paranoid_free(tmp);
2368 return;
2369 }
2370 factor = (float) (current_size_of_drive) / (float) (original_size_of_drive);
2371 sprintf(tmp, "Disk %s was %ld MB; is now %ld MB; factor = %f", drive_name, original_size_of_drive, current_size_of_drive,
2372 factor);
2373 log_to_screen(tmp);
2374
2375 lastpart = drivemntlist->entries-1;
2376 for (partno = 0; partno < drivemntlist->entries; partno++) {
2377 /* the 'atoi' thing is to make sure we don't try to resize _images_, whose formats will be numeric */
2378 if ( !atoi(drivemntlist->el[partno]->format) ) {
2379 new_size = (float) (drivemntlist->el[partno]->size) * factor;
2380 } else {
2381 new_size = drivemntlist->el[partno]->size;
2382 }
2383
2384 if (!strcmp(drivemntlist->el[partno]->mountpoint, "image")) {
2385 log_msg(1, "Skipping %s (%s) because it's an image",
2386 drivemntlist->el[partno]->device,
2387 drivemntlist->el[partno]->mountpoint);
2388 newsizL = (long) new_size; // It looks wrong but it's not
2389 } else {
2390 newsizL = (long) new_size;
2391 }
2392 sprintf(tmp, "Changing %s from %lld KB to %ld KB", drivemntlist->el[partno]->device, drivemntlist->el[partno]->size,newsizL);
2393 log_to_screen(tmp);
2394 drivemntlist->el[partno]->size = newsizL;
2395 }
2396 final_size = get_phys_size_of_drive(drive_name);
2397 sprintf(tmp, "final_size = %ld MB", final_size);
2398 paranoid_free(tmp);
2399 log_to_screen(tmp);
2400}
2401
2402
2403/**
2404 * Resize all partitions in @p mountlist proportionately (each one
2405 * grows or shrinks by the same percentage) to fit them into the new
2406 * drives (presumably different from the old ones).
2407 * @param mountlist The mountlist to resize the drives in.
2408 */
2409void resize_mountlist_proportionately_to_suit_new_drives(struct mountlist_itself
2410 *mountlist)
2411{
2412 /** buffers *********************************************************/
2413 struct list_of_disks *drivelist;
2414
2415 /** int *************************************************************/
2416 int driveno;
2417
2418 /** end *************************************************************/
2419
2420 drivelist = malloc(sizeof(struct list_of_disks));
2421 assert(mountlist != NULL);
2422
2423 if (g_mountlist_fname[0] == '\0') {
2424 log_it ("resize_mountlist_prop...() - warning - mountlist fname is blank");
2425 log_it ("That does NOT affect the functioning of this subroutine.");
2426 log_it ("--- Hugo, 2002/11/20");
2427 }
2428 iamhere("Resizing mountlist");
2429 make_list_of_drives_in_mountlist(mountlist, drivelist);
2430 iamhere("Back from MLoDiM");
2431 for (driveno = 0; driveno < drivelist->entries; driveno++) {
2432 resize_drive_proportionately_to_suit_new_drives(mountlist, drivelist->el[driveno].device);
2433 }
2434 log_to_screen("Mountlist adjusted to suit current hard drive(s)");
2435 paranoid_free(drivelist);
2436}
2437
2438/**
2439 * Create a mountlist_reference structure for @p drive_name in @p mountlist.
2440 * @param mountlist The complete mountlist to get the drive references from.
2441 * @param drive_name The drive to put in @p drivemntlist.
2442 * @param drivemntlist The mountlist_reference structure to put the drive's entries in.
2443 * @note @p drivemntlist and @p drivemntlist->el must be allocated by the caller.
2444 * @author Ralph Grewe
2445 */
2446void create_mountlist_for_drive(struct mountlist_itself *mountlist, char *drive_name, struct mountlist_reference *drivemntlist) {
2447 int partno;
2448 char *tmp_drive_name, *c;
2449
2450 assert(mountlist != NULL);
2451 assert(drive_name != NULL);
2452 assert(drivemntlist != NULL);
2453
2454 log_msg (1, "Creating list of partitions for drive %s",drive_name);
2455
2456 tmp_drive_name = strdup (drive_name);
2457 if (!tmp_drive_name) fatal_error ("Out of memory");
2458
2459 /* devfs devices? */
2460 c = strrchr (tmp_drive_name, '/');
2461 if (c && strncmp (c, "/disc", 5) == 0) {
2462 /* yup its devfs, change the "disc" to "part" so the existing code works */
2463 strcpy (c + 1, "part");
2464 }
2465 drivemntlist->entries=0;
2466 for (partno = 0; partno < mountlist->entries; partno++) {
2467 if (strncmp (mountlist->el[partno].device, tmp_drive_name, strlen(tmp_drive_name)) == 0) {
2468 drivemntlist->el[drivemntlist->entries] = &mountlist->el[partno];
2469 drivemntlist->entries++;
2470 }
2471 }
2472 if (tmp_drive_name)
2473 free (tmp_drive_name);
2474}
2475
2476/* @} - end of prepGroup */
Note: See TracBrowser for help on using the repository browser.