source: MondoRescue/branches/stable/mondo/src/mondorestore/mondo-prep.c@ 1196

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

ChangeLog for mindi UTF-8
mr_strcat much more powerful as taking a vararg (+ tests).
improved text output to log file for mondo
merge from trunk memory management for mondorestore (begining)

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