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

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