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

Last change on this file since 1500 was 1500, checked in by Bruno Cornec, 17 years ago
  • ChangeLog updated
  • Attempt to fix completely #159
  • Improve ESX+vmfs support - needs testing
  • Attempt to fix #136 (without floppy mondorestore may hang due to a mount command blocked)
  • man page fixed as mindi images are now under /var/cache/mindi
  • Support Web page now includes all the latest supported distributions
  • mondorestore supports now the -p option so man page fixed.
  • Fix my email address in mindi man page
  • Fix a typo on the support Web page
  • Improve isolinux.bin detection on x86_64 system by looking also in /usr/lib64
  • Stricter POSIX conformance for all shell scripts (== replaced by = in compare)

(merge -r 1488:1499 $SVN_M/branches/2.2.4)

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