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

Last change on this file since 2009 was 2009, checked in by Bruno Cornec, 16 years ago

Commit ald modifs in trunk - just in case

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