source: MondoRescue/trunk/mondo/mondo/mondorestore/mondo-prep.c@ 30

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

Id property added on files to allow for better conf. management

  • 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 30 2005-09-28 23:32:28Z 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 30 2005-09-28 23:32:28Z 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 if (current_devno == 5 && previous_devno == 4) {
1544 log_to_screen("You must leave at least one partition spare as the Extended partition.");
1545 paranoid_free(device_str);
1546 paranoid_free(format);
1547 paranoid_free(tmp);
1548 return (1);
1549 }
1550
1551 retval += partition_device(pout_to_fdisk, drivename, current_devno, previous_devno, format, partsize);
1552
1553#ifdef __FreeBSD__
1554 if ((current_devno <= 4) && fbsd_part) {
1555 sprintf(tmp, "disklabel -B %s", basename(device_str));
1556 retval += label_drive_or_slice(mountlist, device_str, 0);
1557 if (system(tmp)) {
1558 log_to_screen("Warning! Unable to make the slice bootable.");
1559 }
1560 }
1561#endif
1562
1563 previous_devno = current_devno;
1564 }
1565
1566 if (pout_to_fdisk)
1567 {
1568// mark relevant partition as bootable
1569 sprintf(tmp, "a\n%s\n", call_program_and_get_last_line_of_output("make-me-bootable /tmp/mountlist.txt dummy"));
1570 fput_string_one_char_at_a_time(pout_to_fdisk, tmp);
1571// close fdisk
1572 fput_string_one_char_at_a_time(pout_to_fdisk, "w\n");
1573 system("sync");
1574 paranoid_pclose(pout_to_fdisk);
1575 log_msg(0, "------------------- fdisk.log looks like this ------------------");
1576 sprintf(tmp, "cat %s >> %s", FDISK_LOG, MONDO_LOGFILE);
1577 system(tmp);
1578 log_msg(0, "------------------- end of fdisk.log... word! ------------------");
1579 sprintf(tmp, "tail -n6 %s | fgrep \"16: \"", FDISK_LOG);
1580 if (!run_program_and_log_output(tmp, 5))
1581 {
1582 g_partition_table_locked_up++;
1583 log_to_screen("A flaw in the Linux kernel has locked the partition table.");
1584 }
1585 }
1586 paranoid_free(device_str);
1587 paranoid_free(format);
1588 paranoid_free(tmp);
1589 return (retval);
1590}
1591
1592/**
1593 * Create partition number @p partno on @p drive with @p fdisk.
1594 * @param drive The drive to create the partition on.
1595// * @param partno The partition number of the new partition (1-4 are primary, >=5 is logical).
1596 * @param prev_partno The partition number of the most recently prepped partition.
1597 * @param format The filesystem type of this partition (used to set the type).
1598 * @param partsize The size of the partition in @e bytes.
1599 * @return 0 for success, nonzero for failure.
1600 */
1601int partition_device(FILE*pout_to_fdisk, const char *drive, int partno, int prev_partno, const char *format, long long partsize)
1602{
1603 /** int **************************************************************/
1604 int retval = 0;
1605 int res = 0;
1606
1607 /** buffers **********************************************************/
1608 char *program;
1609 char *partition_name;
1610 char *tmp;
1611 char *logfile;
1612 char *output;
1613
1614 /** pointers **********************************************************/
1615 char *p;
1616 char *part_table_fmt;
1617 FILE *fout;
1618
1619 /** end ***************************************************************/
1620
1621 malloc_string(program);
1622 malloc_string(partition_name);
1623 malloc_string(tmp);
1624 malloc_string(logfile);
1625 malloc_string(output);
1626
1627 assert_string_is_neither_NULL_nor_zerolength(drive);
1628 assert(format != NULL);
1629
1630 log_it("partition_device('%s', %d, %d, '%s', %lld) --- starting", drive, partno, prev_partno, format, partsize);
1631
1632 if (!strncmp(drive, RAID_DEVICE_STUB, strlen(RAID_DEVICE_STUB))) {
1633 sprintf(tmp, "Not partitioning %s - it is a virtual drive", drive);
1634 log_it(tmp);
1635 paranoid_free(program);
1636 paranoid_free(partition_name);
1637 paranoid_free(tmp);
1638 paranoid_free(logfile);
1639 paranoid_free(output);
1640 return (0);
1641 }
1642 build_partition_name(partition_name, drive, partno);
1643 if (partsize <= 0) {
1644 sprintf(tmp, "Partitioning device %s (max size)", partition_name);
1645 } else {
1646 sprintf(tmp, "Partitioning device %s (%lld MB)", partition_name, (long long) partsize / 1024);
1647 }
1648 update_progress_form(tmp);
1649 log_it(tmp);
1650
1651 if (is_this_device_mounted(partition_name)) {
1652 sprintf(tmp, "%s is mounted, and should not be partitioned", partition_name);
1653 log_to_screen(tmp);
1654 paranoid_free(program);
1655 paranoid_free(partition_name);
1656 paranoid_free(tmp);
1657 paranoid_free(logfile);
1658 paranoid_free(output);
1659 return (1);
1660/*
1661 } else if (does_partition_exist(drive, partno)) {
1662 sprintf(tmp, "%s already has a partition", partition_name);
1663 log_to_screen(tmp);
1664 return (1);
1665*/
1666 }
1667
1668
1669 /* sprintf(tmp,"Partitioning %s ",partition_name); */
1670 /* mvaddstr_and_log_it(g_currentY+1,30,tmp); */
1671 p = (char *) strrchr(partition_name, '/');
1672 sprintf(logfile, "/tmp/fdisk.log.%s", ++p);
1673 sprintf(program, "parted2fdisk %s >> %s 2>> %s", drive, MONDO_LOGFILE, MONDO_LOGFILE);
1674
1675 output[0] = '\0';
1676 /* make it a primary/extended/logical */
1677 if (partno <= 4) {
1678 sprintf(output + strlen(output), "n\np\n%d\n", partno);
1679 } else {
1680 if (partno == 5) {
1681 part_table_fmt = which_partition_format(drive);
1682 /* GPT allows more than 4 primary partitions */
1683 if ((prev_partno >= 4) && (strcmp(part_table_fmt,"MBR") == 0)) {
1684 log_to_screen("You need to leave at least one partition free, for 'extended/logical'");
1685 paranoid_free(program);
1686 paranoid_free(partition_name);
1687 paranoid_free(tmp);
1688 paranoid_free(logfile);
1689 paranoid_free(output);
1690 return (1);
1691 } else {
1692 sprintf(output + strlen(output), "n\ne\n%d\n\n\n", prev_partno + 1);
1693 }
1694 }
1695 strcat(output + strlen(output), "n\nl\n");
1696 }
1697 strcat(output + strlen(output), "\n"); /*start block (ENTER for next free blk */
1698 if (partsize > 0) {
1699 if (!strcmp(format, "7")) { log_msg(1, "Adding 512K, just in case"); partsize+=512; }
1700 sprintf(output + strlen(output), "+%lldK", (long long) (partsize));
1701 }
1702 strcat(output + strlen(output), "\n");
1703#if 0
1704/*
1705#endif
1706 sprintf(tmp,"PARTSIZE = +%ld",(long)partsize);
1707 log_it(tmp);
1708 log_it("---fdisk command---");
1709 log_it(output);
1710 log_it("---end of fdisk---");
1711#if 0
1712*/
1713#endif
1714
1715
1716 if (pout_to_fdisk)
1717 {
1718 log_msg(1, "Doing the new all-in-one fdisk thing");
1719 log_msg(1, "output = '%s'", output);
1720 fput_string_one_char_at_a_time(pout_to_fdisk, output);
1721 fput_string_one_char_at_a_time(pout_to_fdisk, "\n\np\n");
1722 strcpy(tmp, last_line_of_file(FDISK_LOG));
1723 if (strstr(tmp, " (m "))
1724 {
1725 log_msg(1, "Successfully created %s%d", drive, partno);
1726 }
1727 else
1728 {
1729 log_msg(1, "last line = %s", tmp);
1730 log_msg(1, "Failed to create %s%d; sending 'Enter'...", drive, partno);
1731 }
1732 if (!retval)
1733 {
1734 log_msg(1, "Trying to set %s%d's partition type now", drive, partno);
1735 retval = set_partition_type(pout_to_fdisk, drive, partno, format, partsize);
1736 if (retval)
1737 {
1738 log_msg(1, "Failed. Trying again...");
1739 retval = set_partition_type(pout_to_fdisk, drive, partno, format, partsize);
1740 }
1741 }
1742 if (retval) { log_msg(1, "...but failed to set type"); }
1743 }
1744 else
1745 {
1746 strcat(output, "w\n\n");
1747 if (g_fprep) { fprintf(g_fprep, "echo \"%s\" | %s\n", output, program); }
1748 /* write to disk; close fdisk's stream */
1749 if (!(fout = popen(program, "w"))) {
1750 log_OS_error("can't popen-out to program");
1751 } else {
1752 fputs(output, fout);
1753 paranoid_pclose(fout);
1754 }
1755 if (!does_partition_exist(drive, partno) && partsize > 0) {
1756 log_it("Vaccum-packing");
1757 g_current_progress--;
1758 res = partition_device(pout_to_fdisk, drive, partno, prev_partno, format, -1);
1759 if (res) {
1760 sprintf(tmp, "Failed to vacuum-pack %s", partition_name);
1761 log_it(tmp);
1762 retval++;
1763 } else {
1764 retval = 0;
1765 }
1766 }
1767 if (does_partition_exist(drive, partno)) {
1768 retval = set_partition_type(pout_to_fdisk, drive, partno, format, partsize);
1769 if (retval) {
1770 sprintf(tmp, "Partitioned %s but failed to set its type", partition_name);
1771 log_it(tmp);
1772 } else {
1773 if (partsize > 0) {
1774 sprintf(tmp, "Partition %s created+configured OK", partition_name);
1775 log_to_screen(tmp);
1776 } else {
1777 log_it("Returning from a successful vacuum-pack");
1778 }
1779 }
1780 } else {
1781 sprintf(tmp, "Failed to partition %s", partition_name);
1782 if (partsize > 0) {
1783 log_to_screen(tmp);
1784 } else {
1785 log_it(tmp);
1786 }
1787 retval++;
1788 }
1789 }
1790 g_current_progress++;
1791 log_it("partition_device() --- leaving");
1792 paranoid_free(program);
1793 paranoid_free(partition_name);
1794 paranoid_free(tmp);
1795 paranoid_free(logfile);
1796 paranoid_free(output);
1797 return (retval);
1798}
1799
1800
1801
1802/**
1803 * Create all partitions listed in @p mountlist.
1804 * @param mountlist The mountlist to use to guide the partitioning.
1805 * @return The number of errors encountered (0 for success).
1806 * @note This sets the partition types but doesn't actually do the formatting.
1807 * Use format_everything() for that.
1808 */
1809int partition_everything(struct mountlist_itself *mountlist)
1810{
1811 /** int ************************************************************/
1812 int lino;
1813 int retval = 0;
1814 int i;
1815 int res;
1816
1817 /** buffer *********************************************************/
1818 struct list_of_disks *drivelist;
1819 /* struct mountlist_itself new_mtlist, *mountlist; */
1820
1821 /** end ************************************************************/
1822
1823 drivelist = malloc(sizeof(struct list_of_disks));
1824 assert(mountlist != NULL);
1825
1826 log_it("partition_everything() --- starting");
1827 mvaddstr_and_log_it(g_currentY, 0, "Partitioning hard drives ");
1828 /* mountlist=orig_mtlist; */
1829 if (mountlist_contains_raid_devices(mountlist)) {
1830 /* mountlist=&new_mtlist; */
1831 /* extrapolate_mountlist_to_include_raid_partitions(mountlist,orig_mtlist); */
1832 log_msg(0,"Mountlist, including the partitions incorporated in RAID devices:-");
1833 for (i = 0; i < mountlist->entries; i++) {
1834 log_it(mountlist->el[i].device);
1835 }
1836 log_msg(0, "End of mountlist.");
1837 }
1838 log_msg(0, "Stopping all LVMs, just in case");
1839 if (!g_text_mode) { newtSuspend(); }
1840 do_my_funky_lvm_stuff(TRUE, FALSE); // just remove old partitions
1841 if (!g_text_mode) { newtResume(); }
1842 log_msg(0, "Stopping all software RAID devices, just in case");
1843 stop_all_raid_devices(mountlist);
1844 log_msg(0, "Done.");
1845
1846/*
1847 if (does_file_exist("/tmp/i-want-my-lvm"))
1848 {
1849 wipe_MBRs_and_reboot_if_necessary(mountlist); // i.e. if it wasn't done recently
1850 }
1851*/
1852
1853 open_progress_form("Partitioning devices", "I am now going to partition all your drives.",
1854 "This should not take more than five minutes.", "", mountlist->entries);
1855
1856 make_list_of_drives_in_mountlist(mountlist, drivelist);
1857
1858 /* partition each drive */
1859 for (lino = 0; lino < drivelist->entries; lino++) {
1860 res = partition_drive(mountlist, drivelist->el[lino].device);
1861 retval += res;
1862 }
1863 close_progress_form();
1864 if (retval) {
1865 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
1866 log_to_screen("Errors occurred during the partitioning of your hard drives.");
1867 } else {
1868 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1869 paranoid_system("rm -f /tmp/fdisk*.log 2> /dev/null");
1870 }
1871 newtSuspend();
1872 system("clear");
1873 newtResume();
1874 paranoid_free(drivelist);
1875 return (retval);
1876}
1877
1878
1879
1880
1881
1882
1883/**
1884 * Set the type of partition number @p partno on @p drive to @p format.
1885 * @param drive The drive to change the type of a partition on.
1886 * @param partno The partition number on @p drive to change the type of.
1887 * @param format The filesystem type this partition will eventually contain.
1888 * @param partsize The size of this partition, in @e bytes (used for vfat
1889 * type calculations).
1890 * @return 0 for success, nonzero for failure.
1891 */
1892int set_partition_type(FILE *pout_to_fdisk, const char *drive, int partno, const char *format, long long partsize)
1893{
1894 /** buffers *********************************************************/
1895 char *partition;
1896 char *command;
1897 char *output;
1898 char *tmp;
1899 char *partcode;
1900 char *logfile;
1901
1902 /** pointers *********************************************************/
1903 char *p;
1904 FILE *fout;
1905
1906 /** int **************************************************************/
1907 int res = 0;
1908
1909 /** end **************************************************************/
1910
1911 assert_string_is_neither_NULL_nor_zerolength(drive);
1912 assert(format != NULL);
1913
1914 malloc_string(partition);
1915 malloc_string(command);
1916 malloc_string(output);
1917 malloc_string(tmp);
1918 malloc_string(partcode);
1919 malloc_string(logfile);
1920
1921 build_partition_name(partition, drive, partno);
1922 p = (char *) strrchr(partition, '/');
1923 sprintf(logfile, "/tmp/fdisk-set-type.%s.log", ++p);
1924 if (strcmp(format, "swap") == 0) {
1925 strcpy(partcode, "82");
1926 } else if (strcmp(format, "vfat") == 0) {
1927 if (partsize / 1024 > 8192) {
1928 strcpy(partcode, "c");
1929 } else {
1930 strcpy(partcode, "b");
1931 }
1932 } else if (strcmp(format, "ext2") == 0 || strcmp(format, "reiserfs") == 0 || strcmp(format, "ext3") == 0
1933 || strcmp(format, "xfs") == 0 || strcmp(format, "jfs") == 0) {
1934 strcpy(partcode, "83");
1935 } else if (strcmp(format, "minix") == 0) {
1936 strcpy(partcode, "81");
1937 } else if (strcmp(format, "raid") == 0) {
1938 strcpy(partcode, "fd");
1939 } else if ((strcmp(format, "ufs") == 0)
1940 || (strcmp(format, "ffs") == 0)) { /* raid autodetect */
1941 strcpy(partcode, "a5");
1942 } else if (strcmp(format, "lvm") == 0) {
1943 strcpy(partcode, "8e");
1944 } else if (format[0] == '\0') { /* LVM physical partition */
1945 partcode[0] = '\0';
1946 } else if (strlen(format) >= 1 && strlen(format) <= 2) {
1947 strcpy(partcode, format);
1948 } else {
1949 /* probably an image */
1950 sprintf(tmp, "Unknown format ('%s') - using supplied string anyway", format);
1951 mvaddstr_and_log_it(g_currentY++, 0, tmp);
1952#ifdef __FreeBSD__
1953 strcpy(partcode, format); // was a5
1954#else
1955 strcpy(partcode, format); // was 83
1956#endif
1957 }
1958 sprintf(tmp, "Setting %s's type to %s (%s)", partition, format, partcode);
1959 log_msg(1, tmp);
1960 if (partcode[0] != '\0' && strcmp(partcode, "83")) { /* no need to set type if 83: 83 is default */
1961
1962 if (pout_to_fdisk)
1963 {
1964 res = 0;
1965 fput_string_one_char_at_a_time(pout_to_fdisk, "t\n");
1966 if (partno > 1 || strstr(last_line_of_file(FDISK_LOG), " (1-4)"))
1967 {
1968 log_msg(5, "Specifying partno (%d) - yay", partno);
1969 sprintf(tmp, "%d\n", partno);
1970 fput_string_one_char_at_a_time(pout_to_fdisk, tmp);
1971 log_msg(5, "A - last line = '%s'", last_line_of_file(FDISK_LOG));
1972 }
1973
1974 sprintf(tmp, "%s\n", partcode);
1975 fput_string_one_char_at_a_time(pout_to_fdisk, tmp);
1976 log_msg(5, "B - last line = '%s'", last_line_of_file(FDISK_LOG));
1977 fput_string_one_char_at_a_time(pout_to_fdisk, "\n");
1978 log_msg(5, "C - last line = '%s'", last_line_of_file(FDISK_LOG));
1979
1980 strcpy(tmp, last_line_of_file(FDISK_LOG));
1981 if (!strstr(tmp, " (m "))
1982 {
1983 log_msg(1, "last line = '%s'; part type set failed", tmp);
1984 res++;
1985 fput_string_one_char_at_a_time(pout_to_fdisk,"\n");
1986 }
1987 fput_string_one_char_at_a_time(pout_to_fdisk,"p\n");
1988 }
1989 else
1990 {
1991 sprintf(output, "t\n%d\n%s\n", partno, partcode);
1992 strcat(output, "w\n");
1993 sprintf(command, "parted2fdisk %s >> %s 2>> %s", drive, MONDO_LOGFILE, MONDO_LOGFILE);
1994 log_msg(5, "output = '%s'", output);
1995 log_msg(5, "partno=%d; partcode=%s", partno, partcode);
1996 log_msg(5, "command = '%s'", command);
1997 fout = popen(command, "w");
1998 if (!fout) {
1999 log_OS_error(command);
2000 res = 1;
2001 } else {
2002 res = 0;
2003 fprintf(fout, output);
2004 paranoid_pclose(fout);
2005 }
2006 }
2007 if (res)
2008 {
2009 log_OS_error(command);
2010 }
2011 }
2012
2013 paranoid_free(partition);
2014 paranoid_free(command);
2015 paranoid_free(output);
2016 paranoid_free(tmp);
2017 paranoid_free(partcode);
2018 paranoid_free(logfile);
2019
2020 return (res);
2021}
2022
2023
2024int start_raid_device(char *raid_device)
2025{
2026 /** int *************************************************************/
2027 int res;
2028 int retval = 0;
2029
2030 /** buffers *********************************************************/
2031 char *program;
2032
2033 /** end *************************************************************/
2034
2035 assert_string_is_neither_NULL_nor_zerolength(raid_device);
2036 malloc_string(program);
2037
2038#ifdef __FreeBSD__
2039 if (is_this_device_mounted(raid_device)) {
2040 log_it("Can't start %s when it's mounted!", raid_device);
2041 return 1;
2042 }
2043 sprintf(program, "vinum start -f %s", raid_device);
2044#else
2045 sprintf(program, "raidstart %s", raid_device);
2046// sprintf (program, "raidstart " RAID_DEVICE_STUB "*");
2047#endif
2048 log_msg(1, "program = %s", program);
2049 res = run_program_and_log_output(program, 1);
2050 if (g_fprep) { fprintf(g_fprep, "%s\n", program); }
2051 if (res) { log_msg(1, "Warning - failed to start RAID device %s", raid_device); }
2052 retval += res;
2053 sleep(1);
2054 return (retval);
2055}
2056
2057
2058
2059/**
2060 * Stop @p raid_device using @p raidstop.
2061 * @param raid_device The software RAID device to stop.
2062 * @return 0 for success, nonzero for failure.
2063 */
2064int stop_raid_device(char *raid_device)
2065{
2066 /** int *************************************************************/
2067 int res;
2068 int retval = 0;
2069
2070 /** buffers *********************************************************/
2071 char *program;
2072
2073 /** end *************************************************************/
2074
2075 assert_string_is_neither_NULL_nor_zerolength(raid_device);
2076 malloc_string(program);
2077
2078#ifdef __FreeBSD__
2079 if (is_this_device_mounted(raid_device)) {
2080 log_it("Can't stop %s when it's mounted!", raid_device);
2081 return 1;
2082 }
2083 sprintf(program, "vinum stop -f %s", raid_device);
2084#else
2085 sprintf(program, "raidstop %s", raid_device);
2086// sprintf (program, "raidstop " RAID_DEVICE_STUB "*");
2087#endif
2088 log_msg(1, "program = %s", program);
2089 res = run_program_and_log_output(program, 1);
2090 if (g_fprep) { fprintf(g_fprep, "%s\n", program); }
2091 if (res) { log_msg(1, "Warning - failed to stop RAID device %s", raid_device); }
2092 retval += res;
2093 return (retval);
2094}
2095
2096
2097int start_all_raid_devices(struct mountlist_itself *mountlist)
2098{
2099 int i;
2100 int retval=0;
2101 int res;
2102
2103 for(i=0; i<mountlist->entries; i++)
2104 {
2105 if (!strncmp(mountlist->el[i].device, RAID_DEVICE_STUB, strlen(RAID_DEVICE_STUB)))
2106 {
2107 log_msg(1, "Starting %s", mountlist->el[i].device);
2108 res = start_raid_device(mountlist->el[i].device);
2109 retval += res;
2110 }
2111 }
2112 if (retval) { log_msg(1, "Started all s/w raid devices OK"); }
2113 else { log_msg(1, "Failed to start some/all s/w raid devices"); }
2114 return(retval);
2115}
2116
2117/**
2118 * Stop all software RAID devices listed in @p mountlist.
2119 * @param mountlist The mountlist to stop the RAID devices in.
2120 * @return The number of errors encountered (0 for success).
2121 * @bug @p mountlist is not used.
2122 */
2123int stop_all_raid_devices(struct mountlist_itself *mountlist)
2124{
2125 /** int *************************************************************/
2126 int retval = 0;
2127#ifndef __FreeBSD__
2128 int res;
2129#endif
2130
2131 /** char ************************************************************/
2132 char *incoming;
2133#ifndef __FreeBSD__
2134 char *dev;
2135#endif
2136 /** pointers ********************************************************/
2137#ifndef __FreeBSD__
2138 char *p;
2139#endif
2140 FILE *fin;
2141 int i;
2142
2143 /** end ****************************************************************/
2144
2145 malloc_string(dev);
2146 malloc_string(incoming);
2147 assert(mountlist != NULL);
2148
2149 for (i = 0; i < 3; i++) {
2150#ifdef __FreeBSD__
2151 fin = popen("vinum list | grep '^[PVS]' | sed 's/S/1/;s/P/2/;s/V/3/' | sort | cut -d' ' -f2", "r");
2152 if (!fin) {
2153 paranoid_free(dev);
2154 paranoid_free(incoming);
2155 return (1);
2156 }
2157 for (fgets(incoming, MAX_STR_LEN - 1, fin); !feof(fin); fgets(incoming, MAX_STR_LEN - 1, fin)) {
2158 retval += stop_raid_device(incoming);
2159 }
2160#else
2161 fin = fopen("/proc/mdstat", "r");
2162 if (!fin) {
2163 log_OS_error("/proc/mdstat");
2164 paranoid_free(dev);
2165 paranoid_free(incoming);
2166 return (1);
2167 }
2168 for (fgets(incoming, MAX_STR_LEN - 1, fin); !feof(fin); fgets(incoming, MAX_STR_LEN - 1, fin)) {
2169 for (p = incoming; *p != '\0' && (*p != 'm' || *(p + 1) != 'd' || !isdigit(*(p + 2))); p++);
2170 if (*p != '\0') {
2171 sprintf(dev, "/dev/%s", p);
2172 for (p = dev; *p > 32; p++);
2173 *p = '\0';
2174 res = stop_raid_device(dev);
2175 }
2176 }
2177#endif
2178 }
2179 paranoid_fclose(fin);
2180 if (retval) { log_msg(1, "Warning - unable to stop some RAID devices"); }
2181 paranoid_free(dev);
2182 paranoid_free(incoming);
2183 system("sync"); system("sync"); system("sync");
2184 sleep(1);
2185 return (retval);
2186}
2187
2188
2189
2190/**
2191 * Decide which command we need to use to format a device of type @p format.
2192 * @param format The filesystem type we are about to format.
2193 * @param program Where to put the binary name for this format.
2194 * @return 0 for success, nonzero for failure.
2195 */
2196int which_format_command_do_i_need(char *format, char *program)
2197{
2198 /** int *************************************************************/
2199 int res = 0;
2200
2201 /** buffers *********************************************************/
2202 char *tmp;
2203
2204 /** end ***************************************************************/
2205
2206 malloc_string(tmp);
2207 assert_string_is_neither_NULL_nor_zerolength(format);
2208 assert(program != NULL);
2209
2210 if (strcmp(format, "swap") == 0) {
2211#ifdef __FreeBSD__
2212 strcpy(program, "true");
2213#else
2214 strcpy(program, "mkswap");
2215#endif
2216 } else if (strcmp(format, "vfat") == 0) {
2217 strcpy(program, "format-and-kludge-vfat");
2218#ifndef __FreeBSD__
2219 } else if (strcmp(format, "reiserfs") == 0) {
2220 strcpy(program, "mkreiserfs -ff");
2221 } else if (strcmp(format, "xfs") == 0) {
2222 strcpy(program, "mkfs.xfs -f -q");
2223 } else if (strcmp(format, "jfs") == 0) {
2224 strcpy(program, "mkfs.jfs");
2225 } else if (strcmp(format, "ext3") == 0) {
2226 strcpy(program, "mkfs -t ext2 -F -j -q");
2227 } else if (strcmp(format, "minix") == 0) {
2228 strcpy(program, "mkfs.minix");
2229#endif
2230 } else if (strcmp(format, "ext2") == 0) {
2231 strcpy(program, "mke2fs -F -q");
2232 } else {
2233#ifdef __FreeBSD__
2234 sprintf(program, "newfs_%s", format);
2235#else
2236 sprintf(program, "mkfs -t %s -c", format); // -c checks for bad blocks
2237#endif
2238 sprintf(tmp, "Unknown format (%s) - assuming '%s' will do", format, program);
2239 log_it(tmp);
2240 res = 0;
2241 }
2242 paranoid_free(tmp);
2243 return (res);
2244}
2245
2246
2247/**
2248 * Calculate the probable size of @p drive_name by adding up sizes in
2249 * @p mountlist.
2250 * @param mountlist The mountlist to use to calculate the size.
2251 * @param drive_name The drive to calculate the original size of.
2252 * @return The size of @p drive_name in kilobytes.
2253 */
2254long calc_orig_size_of_drive_from_mountlist(struct mountlist_itself
2255 *mountlist, char *drive_name)
2256{
2257 /** long ************************************************************/
2258 long original_size_of_drive;
2259
2260 /** int *************************************************************/
2261 int partno;
2262
2263 /** buffers *********************************************************/
2264 char *tmp;
2265
2266 /** end *************************************************************/
2267
2268 malloc_string(tmp);
2269 assert(mountlist != NULL);
2270 assert_string_is_neither_NULL_nor_zerolength(drive_name);
2271
2272 for (original_size_of_drive = 0, partno = 0; partno < mountlist->entries; partno++) {
2273 if (strncmp(mountlist->el[partno].device, drive_name, strlen(drive_name)) == 0) {
2274 original_size_of_drive += mountlist->el[partno].size;
2275 } else {
2276 sprintf(tmp, "Skipping %s", mountlist->el[partno].device);
2277// log_to_screen(tmp);
2278 }
2279 }
2280 original_size_of_drive = original_size_of_drive / 1024;
2281 paranoid_free(tmp);
2282 return (original_size_of_drive);
2283}
2284
2285
2286/**
2287 * Resize a drive's entries in @p mountlist proportionately to fit its new size.
2288 * There are a few problems with this function:
2289 * - It won't work if there was any unallocated space on the user's hard drive
2290 * when it was backed up.
2291 * - It won't work if the user's hard drive lies about its size (more common
2292 * than you'd think).
2293 *
2294 * @param mountlist The mountlist to use for resizing @p drive_name.
2295 * @param drive_name The drive to resize.
2296 */
2297void resize_drive_proportionately_to_suit_new_drives(struct mountlist_itself
2298 *mountlist, char *drive_name)
2299{
2300 /**buffers **********************************************************/
2301 char *tmp;
2302
2303 /** int *************************************************************/
2304 int partno, lastpart;
2305 /** remove driveno, noof_drives stan benoit apr 2002**/
2306
2307 /** float ***********************************************************/
2308 float factor;
2309 float new_size;
2310// float newcylinderno;
2311
2312 /** long *************************************************************/
2313 long newsizL;
2314 long current_size_of_drive = 0;
2315 long original_size_of_drive = 0;
2316 long final_size; /* all in Megabytes */
2317 struct mountlist_reference *drivemntlist;
2318
2319 /** structures *******************************************************/
2320
2321 /** end **************************************************************/
2322
2323 malloc_string(tmp);
2324 assert(mountlist != NULL);
2325 assert_string_is_neither_NULL_nor_zerolength(drive_name);
2326
2327 if (strlen(drive_name) >= strlen(RAID_DEVICE_STUB)) {
2328 if (strncmp(drive_name, RAID_DEVICE_STUB, strlen(RAID_DEVICE_STUB)) == 0) {
2329 paranoid_free(tmp);
2330 return;
2331 }
2332 }
2333
2334 /*
2335 sprintf (tmp, "cp -f %s %s.pre-resize", g_mountlist_fname, g_mountlist_fname);
2336 run_program_and_log_output (tmp, FALSE);
2337 */
2338
2339 current_size_of_drive = get_phys_size_of_drive(drive_name);
2340
2341 if (current_size_of_drive <= 0) {
2342 log_it("Not resizing to match %s - can't find drive", drive_name);
2343 paranoid_free(tmp);
2344 return;
2345 }
2346 sprintf(tmp, "Expanding entries to suit drive %s (%ld MB)", drive_name, current_size_of_drive);
2347 log_to_screen(tmp);
2348
2349 drivemntlist = malloc(sizeof (struct mountlist_reference));
2350 drivemntlist->el = malloc(sizeof(struct mountlist_line *) * MAX_TAPECATALOG_ENTRIES);
2351
2352 if (!drivemntlist) {
2353 fatal_error("Cannot malloc temporary mountlist\n");
2354 }
2355 create_mountlist_for_drive(mountlist, drive_name, drivemntlist);
2356
2357 for (partno = 0; partno < drivemntlist->entries; partno++) {
2358 original_size_of_drive += drivemntlist->el[partno]->size;
2359 }
2360 original_size_of_drive = original_size_of_drive / 1024;
2361
2362 if (original_size_of_drive <= 0) {
2363 sprintf(tmp, "Cannot resize %s's entries. Drive not found.", drive_name);
2364 log_to_screen(tmp);
2365 paranoid_free(tmp);
2366 return;
2367 }
2368 factor = (float) (current_size_of_drive) / (float) (original_size_of_drive);
2369 sprintf(tmp, "Disk %s was %ld MB; is now %ld MB; factor = %f", drive_name, original_size_of_drive, current_size_of_drive,
2370 factor);
2371 log_to_screen(tmp);
2372
2373 lastpart = drivemntlist->entries-1;
2374 for (partno = 0; partno < drivemntlist->entries; partno++) {
2375 /* the 'atoi' thing is to make sure we don't try to resize _images_, whose formats will be numeric */
2376 if ( !atoi(drivemntlist->el[partno]->format) ) {
2377 new_size = (float) (drivemntlist->el[partno]->size) * factor;
2378 } else {
2379 new_size = drivemntlist->el[partno]->size;
2380 }
2381
2382 if (!strcmp(drivemntlist->el[partno]->mountpoint, "image")) {
2383 log_msg(1, "Skipping %s (%s) because it's an image",
2384 drivemntlist->el[partno]->device,
2385 drivemntlist->el[partno]->mountpoint);
2386 newsizL = (long) new_size; // It looks wrong but it's not
2387 } else {
2388 newsizL = (long) new_size;
2389 }
2390 sprintf(tmp, "Changing %s from %lld KB to %ld KB", drivemntlist->el[partno]->device, drivemntlist->el[partno]->size,newsizL);
2391 log_to_screen(tmp);
2392 drivemntlist->el[partno]->size = newsizL;
2393 }
2394 final_size = get_phys_size_of_drive(drive_name);
2395 sprintf(tmp, "final_size = %ld MB", final_size);
2396 paranoid_free(tmp);
2397 log_to_screen(tmp);
2398}
2399
2400
2401/**
2402 * Resize all partitions in @p mountlist proportionately (each one
2403 * grows or shrinks by the same percentage) to fit them into the new
2404 * drives (presumably different from the old ones).
2405 * @param mountlist The mountlist to resize the drives in.
2406 */
2407void resize_mountlist_proportionately_to_suit_new_drives(struct mountlist_itself
2408 *mountlist)
2409{
2410 /** buffers *********************************************************/
2411 struct list_of_disks *drivelist;
2412
2413 /** int *************************************************************/
2414 int driveno;
2415
2416 /** end *************************************************************/
2417
2418 drivelist = malloc(sizeof(struct list_of_disks));
2419 assert(mountlist != NULL);
2420
2421 if (g_mountlist_fname[0] == '\0') {
2422 log_it ("resize_mountlist_prop...() - warning - mountlist fname is blank");
2423 log_it ("That does NOT affect the functioning of this subroutine.");
2424 log_it ("--- Hugo, 2002/11/20");
2425 }
2426 iamhere("Resizing mountlist");
2427 make_list_of_drives_in_mountlist(mountlist, drivelist);
2428 iamhere("Back from MLoDiM");
2429 for (driveno = 0; driveno < drivelist->entries; driveno++) {
2430 resize_drive_proportionately_to_suit_new_drives(mountlist, drivelist->el[driveno].device);
2431 }
2432 log_to_screen("Mountlist adjusted to suit current hard drive(s)");
2433 paranoid_free(drivelist);
2434}
2435
2436/**
2437 * Create a mountlist_reference structure for @p drive_name in @p mountlist.
2438 * @param mountlist The complete mountlist to get the drive references from.
2439 * @param drive_name The drive to put in @p drivemntlist.
2440 * @param drivemntlist The mountlist_reference structure to put the drive's entries in.
2441 * @note @p drivemntlist and @p drivemntlist->el must be allocated by the caller.
2442 * @author Ralph Grewe
2443 */
2444void create_mountlist_for_drive(struct mountlist_itself *mountlist, char *drive_name, struct mountlist_reference *drivemntlist) {
2445 int partno;
2446 char *tmp_drive_name, *c;
2447
2448 assert(mountlist != NULL);
2449 assert(drive_name != NULL);
2450 assert(drivemntlist != NULL);
2451
2452 log_msg (1, "Creating list of partitions for drive %s",drive_name);
2453
2454 tmp_drive_name = strdup (drive_name);
2455 if (!tmp_drive_name) fatal_error ("Out of memory");
2456
2457 /* devfs devices? */
2458 c = strrchr (tmp_drive_name, '/');
2459 if (c && strncmp (c, "/disc", 5) == 0) {
2460 /* yup its devfs, change the "disc" to "part" so the existing code works */
2461 strcpy (c + 1, "part");
2462 }
2463 drivemntlist->entries=0;
2464 for (partno = 0; partno < mountlist->entries; partno++) {
2465 if (strncmp (mountlist->el[partno].device, tmp_drive_name, strlen(tmp_drive_name)) == 0) {
2466 drivemntlist->el[drivemntlist->entries] = &mountlist->el[partno];
2467 drivemntlist->entries++;
2468 }
2469 }
2470 if (tmp_drive_name)
2471 free (tmp_drive_name);
2472}
2473
2474/* @} - end of prepGroup */
Note: See TracBrowser for help on using the repository browser.