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