[1] | 1 | #!/usr/bin/perl -w
|
---|
| 2 | #
|
---|
[88] | 3 | # $Id: parted2fdisk.pl 2793 2011-04-29 13:37:48Z bruno $
|
---|
| 4 | #
|
---|
[1] | 5 | # parted2fdisk: fdisk like interface for parted
|
---|
[569] | 6 | # [developped for mindi/mondo http://www.mondorescue.org]
|
---|
[1] | 7 | #
|
---|
| 8 | # Aims to be architecture independant (i386/ia64)
|
---|
[2131] | 9 | # Tested on ia64 with RHAS 2.1 - Mandrake 9.0 - RHEL 3.0 - SLES 10
|
---|
[1] | 10 | #
|
---|
| 11 | # (c) Bruno Cornec <Bruno.Cornec@hp.com>
|
---|
| 12 | # Licensed under the GPL
|
---|
| 13 |
|
---|
| 14 | use strict;
|
---|
| 15 |
|
---|
| 16 | $ENV{LANG} = "C";
|
---|
| 17 | $ENV{LANGUAGE} = "C";
|
---|
| 18 | $ENV{LC_ALL} = "C";
|
---|
| 19 |
|
---|
| 20 | # Log
|
---|
| 21 | my $flog = "/tmp/parted2fdisk.log";
|
---|
| 22 | open(FLOG, "> $flog") || die "Unable to open $flog";
|
---|
| 23 |
|
---|
| 24 | my $fdisk = "/sbin/fdisk";
|
---|
| 25 | my $parted = "/sbin/parted";
|
---|
| 26 |
|
---|
| 27 | my $i;
|
---|
| 28 | my $l;
|
---|
| 29 | my $part;
|
---|
| 30 | my $wpart;
|
---|
| 31 | my $start = "";
|
---|
| 32 | my $end = "";
|
---|
[90] | 33 | my $cylstart;
|
---|
| 34 | my $cylend;
|
---|
[1] | 35 | my %start;
|
---|
| 36 | my %end;
|
---|
| 37 | my %type;
|
---|
| 38 | my $arch;
|
---|
[88] | 39 | my $fake = 0;
|
---|
[1801] | 40 | my $mega = 1048576;
|
---|
[1] | 41 |
|
---|
[2793] | 42 | # Immediate flushing to avoids read error from mondorestore in log files
|
---|
| 43 | $| = 1;
|
---|
| 44 |
|
---|
[1] | 45 | # Determine on which arch we're running
|
---|
| 46 | if (defined ($ENV{ARCH})) {
|
---|
| 47 | $arch = $ENV{ARCH};
|
---|
| 48 | } else {
|
---|
[725] | 49 | $arch = `uname -m`;
|
---|
[1] | 50 | chomp($arch);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | #
|
---|
| 54 | # Looking for fdisk
|
---|
| 55 | #
|
---|
| 56 | $fdisk = is_lsb($fdisk);
|
---|
| 57 | #
|
---|
| 58 | # We always use fdisk except on ia64 with GPT types of
|
---|
| 59 | # partition tables where we need parted
|
---|
| 60 | # All should return fdisk like format so that callers
|
---|
| 61 | # think they have called fdisk directly
|
---|
| 62 | #
|
---|
| 63 | my $un;
|
---|
| 64 | my $type;
|
---|
| 65 | my $args = "";
|
---|
| 66 | my $device = "";
|
---|
[88] | 67 | my $endmax = "";
|
---|
[1] | 68 |
|
---|
| 69 | if ($#ARGV < 0) {
|
---|
| 70 | printf FLOG "No arguments given exiting ...\n";
|
---|
| 71 | mysyn();
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | my %pid = ( "FAT" => "6",
|
---|
| 75 | "fat32" => "b",
|
---|
| 76 | "fat16" => "e",
|
---|
| 77 | "ext2" => "83",
|
---|
| 78 | "ext3" => "83",
|
---|
[2087] | 79 | "ext4" => "83",
|
---|
| 80 | "xfs" => "83",
|
---|
[1565] | 81 | "reiserfs" => "83",
|
---|
[1] | 82 | "linux-swap" => "82",
|
---|
[2789] | 83 | "lvm" => "8e",
|
---|
[1] | 84 | "" => "",
|
---|
| 85 | );
|
---|
| 86 | my %pnum;
|
---|
| 87 |
|
---|
| 88 | # Reverse table of pid
|
---|
| 89 | while (($i,$l) = each %pid) {
|
---|
| 90 | next if ($i eq "ext2");
|
---|
| 91 | $pnum{$l} = $i;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | foreach $i (@ARGV) {
|
---|
| 95 | # We support at most one option and one device
|
---|
| 96 | print FLOG "Parameter found : $i\n";
|
---|
| 97 | if ($i =~ /^\/dev\//) {
|
---|
| 98 | $device = $i;
|
---|
| 99 | next;
|
---|
| 100 | } elsif ($i =~ /^-/) {
|
---|
| 101 | $args = $i;
|
---|
| 102 | next;
|
---|
| 103 | } else {
|
---|
| 104 | mysyn();
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | if (($args ne "") and ($device eq "")) {
|
---|
| 109 | mysyn();
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | # -s takes a partition as arg
|
---|
| 113 | if ($args =~ /-s/) {
|
---|
| 114 | $wpart = $device;
|
---|
| 115 | $device =~ s/[0-9]+$//;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[88] | 118 | if ($args =~ /-n/) {
|
---|
| 119 | print FLOG "Fake mode. Nothing will be really done\n";
|
---|
| 120 | $fake = 1;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[1] | 123 | print FLOG "Called with device $device and arg $args\n";
|
---|
| 124 |
|
---|
| 125 | if ($arch =~ /^ia64/) {
|
---|
| 126 | # Check partition table type
|
---|
| 127 | print FLOG "We're on ia64 ...\n";
|
---|
| 128 | $parted = is_lsb($parted);
|
---|
| 129 | $type = which_type($device);
|
---|
| 130 | if ($type ne "msdos") {
|
---|
| 131 | print FLOG "Not an msdos type of disk label\n";
|
---|
| 132 | if ($args =~ /-l/) {
|
---|
[90] | 133 | fdisk_list($device,undef,\%start,\%end, 1);
|
---|
[1] | 134 | } elsif ($args =~ /-s/) {
|
---|
[90] | 135 | fdisk_list($device,$wpart,\%start,\%end, 1);
|
---|
[88] | 136 | } elsif (($args =~ /-/) and ($fake == 0)) {
|
---|
[1] | 137 | printf FLOG "Option not supported ($args) ...\n";
|
---|
| 138 | printf FLOG "Please report to the author\n";
|
---|
| 139 | mysyn();
|
---|
| 140 | } else {
|
---|
| 141 | # Read fdisk orders on stdin and pass them to parted
|
---|
| 142 | # on the command line as parted doesn't read on stdin
|
---|
| 143 | print FLOG "Translating fdisk command to parted\n";
|
---|
| 144 | while ($i = <STDIN>) {
|
---|
| 145 | if ($i =~ /^p$/) {
|
---|
[90] | 146 | fdisk_list($device,undef,\%start,\%end, 1);
|
---|
[2793] | 147 | print "command (m for help) send back to fake fdisk for mondorestore\n";
|
---|
[2785] | 148 | } elsif ($i =~ /^n$/) {
|
---|
[90] | 149 | fdisk_list($device,undef,\%start,\%end, 0);
|
---|
[1] | 150 | if ($type ne "gpt") {
|
---|
| 151 | print FLOG "Forcing GPT type of disk label\n";
|
---|
| 152 | print FLOG "mklabel gpt\n";
|
---|
[90] | 153 | system "$parted -s $device mklabel gpt\n" if ($fake == 0);
|
---|
[1] | 154 | $type = "gpt";
|
---|
| 155 | }
|
---|
| 156 | $l = <STDIN>;
|
---|
| 157 | if (not (defined $l)) {
|
---|
| 158 | print FLOG "no primary/extended arg given for creation... assuming primary\n";
|
---|
| 159 | $l = "p";
|
---|
| 160 | }
|
---|
| 161 | chomp($l);
|
---|
| 162 | $part = <STDIN>;
|
---|
| 163 | if ((not (defined $part)) || ($part eq "")) {
|
---|
| 164 | print FLOG "no partition given for creation... skipping\n";
|
---|
| 165 | next;
|
---|
| 166 | }
|
---|
| 167 | chomp($part);
|
---|
[90] | 168 | $cylstart = <STDIN>;
|
---|
| 169 | chomp($cylstart);
|
---|
| 170 | if ((not (defined $cylstart)) || ($cylstart eq "")) {
|
---|
[1] | 171 | if (defined $start{$part-1}) {
|
---|
[90] | 172 | # in MB => cyl
|
---|
[1801] | 173 | $cylstart = sprintf("%d",$end{$part-1}*$mega/$un + 1);
|
---|
[90] | 174 | print FLOG "no start cyl given for creation... assuming the following $cylstart\n";
|
---|
[1] | 175 | } else {
|
---|
| 176 | print FLOG "no start cyl given for creation... assuming the following 1\n";
|
---|
[90] | 177 | $cylstart = 1;
|
---|
[1] | 178 | }
|
---|
| 179 | }
|
---|
[90] | 180 | $cylstart = 1 if ($cylstart < 1);
|
---|
| 181 | print FLOG "start cyl : $cylstart\n";
|
---|
| 182 | $un = get_un($device, "", 0);
|
---|
[88] | 183 | # parted needs MB
|
---|
[90] | 184 | if ($cylstart == 1) {
|
---|
| 185 | $start = 0.01;
|
---|
| 186 | } else {
|
---|
[1801] | 187 | $start = $cylstart* $un / $mega + 0.001;
|
---|
[90] | 188 | }
|
---|
[88] | 189 | # this is a size in B/KB/MB/GB
|
---|
[90] | 190 |
|
---|
[88] | 191 | $endmax = get_max($device);
|
---|
[90] | 192 | $cylend = <STDIN>;
|
---|
| 193 | chomp($cylend);
|
---|
| 194 | if ((not (defined $cylend)) || ($cylend eq "")) {
|
---|
[1] | 195 | print FLOG "no end cyl given for creation... assuming full disk)\n";
|
---|
[90] | 196 | $cylend = $endmax;
|
---|
[1] | 197 | }
|
---|
| 198 | # Handles end syntaxes (+, K, M, ...)
|
---|
[88] | 199 | # to give cylinders
|
---|
[90] | 200 | if ($cylend =~ /^\+/) {
|
---|
| 201 | $cylend =~ s/^\+//;
|
---|
[1565] | 202 | # Handles suffixes; return bytes
|
---|
| 203 | $cylend = decode_Bsuf($cylend,1);
|
---|
[88] | 204 | # This gives the number of cyl
|
---|
[90] | 205 | $cylend /= $un;
|
---|
| 206 | $cylend = sprintf("%d",$cylend);
|
---|
| 207 | $cylend += $cylstart - 0.001;
|
---|
[88] | 208 | # We now have the end cyl
|
---|
[1] | 209 | }
|
---|
[90] | 210 | $cylend = $endmax if ($cylend > $endmax);
|
---|
| 211 | print FLOG "end cyl : $cylend\n";
|
---|
[88] | 212 | # parted needs MB
|
---|
[1801] | 213 | $end = $cylend * $un / $mega;
|
---|
[90] | 214 | print FLOG "n $l $part $cylstart $cylend => mkpart primary $start $end\n";
|
---|
| 215 | system "$parted -s $device mkpart primary ext2 $start $end\n" if ($fake == 0);
|
---|
[2789] | 216 | print "command (m for help) send back to fake fdisk for mondorestore\n";
|
---|
[2785] | 217 | } elsif ($i =~ /^d$/) {
|
---|
[1] | 218 | $part = <STDIN>;
|
---|
| 219 | if (not (defined $part)) {
|
---|
| 220 | print FLOG "no partition given for deletion... skipping\n";
|
---|
| 221 | next;
|
---|
| 222 | }
|
---|
| 223 | chomp($part);
|
---|
| 224 | print FLOG "d $part => rm $part\n";
|
---|
[90] | 225 | system "$parted -s $device rm $part\n" if ($fake == 0);
|
---|
| 226 | get_parted($device,undef,\%start,\%end,undef);
|
---|
[2789] | 227 | print "command (m for help) send back to fake fdisk for mondorestore\n";
|
---|
[2785] | 228 | } elsif ($i =~ /^w$/) {
|
---|
[1] | 229 | print FLOG "w => quit\n";
|
---|
[2785] | 230 | } elsif ($i =~ /^t$/) {
|
---|
[1] | 231 | $part = <STDIN>;
|
---|
| 232 | if (not (defined $part)) {
|
---|
| 233 | print FLOG "no partition given for tagging... skipping\n";
|
---|
| 234 | next;
|
---|
| 235 | }
|
---|
| 236 | chomp($part);
|
---|
[2789] | 237 | # If no partition number given it's 1, and we received the type
|
---|
| 238 | if ($part !~ /\d+/) {
|
---|
| 239 | $l = $part;
|
---|
| 240 | $part = 1
|
---|
| 241 | } else {
|
---|
| 242 | $l = <STDIN>;
|
---|
| 243 | }
|
---|
[1] | 244 | if (not (defined $l)) {
|
---|
| 245 | print FLOG "no type given for tagging partition $part... skipping\n";
|
---|
| 246 | next;
|
---|
| 247 | }
|
---|
| 248 | chomp($l);
|
---|
| 249 | if (not (defined $pnum{$l})) {
|
---|
| 250 | print FLOG "no partition number given for $l... please report to the author\n";
|
---|
| 251 | next;
|
---|
| 252 | }
|
---|
[2789] | 253 |
|
---|
| 254 | if ($pnum{$l} eq "lvm") {
|
---|
| 255 | # In that case this is a flag set, not a mkfs
|
---|
| 256 | print FLOG "t $part $l => set $part $pnum{$l} on\n";
|
---|
| 257 | system "$parted -s $device set $part $pnum{$l} on\n" if ($fake == 0);
|
---|
| 258 | } else {
|
---|
| 259 | print FLOG "t $part $l => mkfs $part $pnum{$l}\n";
|
---|
| 260 | system "$parted -s $device mkfs $part $pnum{$l}\n" if ($fake == 0);
|
---|
| 261 | }
|
---|
| 262 | print "command (m for help) send back to fake fdisk for mondorestore\n";
|
---|
[2785] | 263 | } elsif ($i =~ /^a$/) {
|
---|
[1] | 264 | $part = <STDIN>;
|
---|
| 265 | if (not (defined $part)) {
|
---|
| 266 | print FLOG "no partition given for tagging... skipping\n";
|
---|
| 267 | next;
|
---|
| 268 | }
|
---|
| 269 | chomp($part);
|
---|
[2789] | 270 |
|
---|
| 271 | # Partition shouldn't be negative or null. Then take the first one.
|
---|
| 272 | $part = 1 if ($part le 0);
|
---|
| 273 |
|
---|
[1] | 274 | print FLOG "a $part => set $part boot on\n";
|
---|
[90] | 275 | system "$parted -s $device set $part boot on\n" if ($fake == 0);
|
---|
[2789] | 276 | print "command (m for help) send back to fake fdisk for mondorestore\n";
|
---|
[2785] | 277 | } elsif ($i =~ /^q$/) {
|
---|
[1] | 278 | print FLOG "q => quit\n";
|
---|
[2785] | 279 | } else {
|
---|
[1] | 280 | print FLOG "Unknown command: $i\n";
|
---|
[2793] | 281 | print "command (m for help) send back to fake fdisk for mondorestore\n";
|
---|
[1] | 282 | next;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | }
|
---|
| 286 | }
|
---|
| 287 | myexit(0);
|
---|
| 288 | }
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | #
|
---|
| 292 | # Else everything is for fdisk
|
---|
| 293 | #
|
---|
| 294 | # Print only mode
|
---|
| 295 | print FLOG "Passing everything to the real fdisk\n";
|
---|
| 296 | my $fargs = join(@ARGV);
|
---|
| 297 |
|
---|
| 298 | if ($args =~ /^-/) {
|
---|
| 299 | # -l or -s
|
---|
[1857] | 300 | open (FDISK, "$fdisk $fargs 2>/dev/null |") || die "Unable to read from $fdisk";
|
---|
[1] | 301 | while (<FDISK>) {
|
---|
| 302 | print;
|
---|
| 303 | }
|
---|
| 304 | close(FDISK);
|
---|
| 305 | } else {
|
---|
| 306 | # Modification mode
|
---|
[1857] | 307 | open (FDISK, "| $fdisk $fargs 2>/dev/null") || die "Unable to modify through $fdisk";
|
---|
[1] | 308 | while (<STDIN>) {
|
---|
| 309 | print FDISK;
|
---|
| 310 | }
|
---|
| 311 | close(FDISK);
|
---|
| 312 | close(STDIN);
|
---|
| 313 | }
|
---|
| 314 | myexit(0);
|
---|
| 315 |
|
---|
| 316 |
|
---|
| 317 | # Is your system LSB ?
|
---|
| 318 | sub is_lsb {
|
---|
| 319 |
|
---|
| 320 | my $cmd = shift;
|
---|
| 321 | my $basename = basename($cmd);
|
---|
| 322 |
|
---|
| 323 | if (not (-x $cmd)) {
|
---|
| 324 | print FLOG "Your system is not LSB/mondo compliant: $basename was not found as $cmd\n";
|
---|
| 325 | print FLOG "Searching elswhere...";
|
---|
| 326 | foreach $i (split(':',$ENV{PATH})) {
|
---|
| 327 | if (-x "$i/$basename") {
|
---|
| 328 | $cmd = "$i/$basename";
|
---|
| 329 | print FLOG "Found $cmd, using it !\n";
|
---|
| 330 | last;
|
---|
| 331 | }
|
---|
| 332 | }
|
---|
| 333 | if (not (-x $cmd)) {
|
---|
| 334 | print FLOG "Your system doesn't provide $basename in the PATH\n";
|
---|
| 335 | print FLOG "Please correct it before relaunching\n";
|
---|
| 336 | myexit(-1);
|
---|
| 337 | }
|
---|
| 338 | }
|
---|
| 339 | return($cmd);
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | sub fdisk_list {
|
---|
| 343 |
|
---|
| 344 | my $device = shift;
|
---|
| 345 | my $wpart = shift;
|
---|
| 346 | my $start = shift;
|
---|
| 347 | my $end = shift;
|
---|
[90] | 348 | my $verbose = shift;
|
---|
[1] | 349 |
|
---|
| 350 | my $un;
|
---|
[88] | 351 | my $endmax;
|
---|
[1] | 352 | my $d;
|
---|
| 353 | my $n;
|
---|
| 354 |
|
---|
| 355 | my %cmt = ( "FAT" => "FAT",
|
---|
| 356 | "ext2" => "Linux",
|
---|
| 357 | "ext3" => "Linux",
|
---|
[2087] | 358 | "ext4" => "Linux",
|
---|
| 359 | "xfs" => "Linux",
|
---|
[1565] | 360 | "reiserfs" => "Linux",
|
---|
[1] | 361 | "linux-swap" => "Linux swap",
|
---|
[2789] | 362 | "lvm" => "Linux LVM",
|
---|
[1] | 363 | "fat16" => "fat16",
|
---|
| 364 | "fat32" => "fat32",
|
---|
| 365 | "" => "Linux",
|
---|
| 366 | );
|
---|
| 367 |
|
---|
| 368 | my $part;
|
---|
| 369 | my $mstart;
|
---|
| 370 | my $mend;
|
---|
| 371 | my $length;
|
---|
| 372 | my $pid;
|
---|
| 373 | my $cmt;
|
---|
| 374 | format FLOG1 =
|
---|
[1565] | 375 | @<<<<<<<<<<<< @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
|
---|
[1] | 376 | $part, $mstart, $mend, $length, $pid, $cmt
|
---|
| 377 | .
|
---|
| 378 | format FLOG2 =
|
---|
| 379 | @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
|
---|
| 380 | $part,
|
---|
[1565] | 381 | @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
|
---|
[1] | 382 | $mstart, $mend, $length, $pid, $cmt
|
---|
| 383 | .
|
---|
| 384 | format STDOUT1 =
|
---|
[1565] | 385 | @<<<<<<<<<<<< @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
|
---|
[1] | 386 | $part, $mstart, $mend, $length, $pid, $cmt
|
---|
| 387 | .
|
---|
| 388 | format STDOUT2 =
|
---|
| 389 | @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
|
---|
| 390 | $part,
|
---|
[1565] | 391 | @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
|
---|
[1] | 392 | $mstart, $mend, $length, $pid, $cmt
|
---|
| 393 | .
|
---|
[1565] | 394 | # Device Boot Start End Blocks Id System
|
---|
| 395 | #/dev/hda1 1 77579 39099374+ ee EFI GPT
|
---|
[1] | 396 |
|
---|
[1565] | 397 |
|
---|
[1] | 398 | #
|
---|
| 399 | # Keep Fdisk headers
|
---|
| 400 | #
|
---|
[88] | 401 | # this will return bytes
|
---|
[90] | 402 | $un = get_un ($device,$wpart,$verbose);
|
---|
| 403 |
|
---|
[88] | 404 | $endmax = get_max($device);
|
---|
[90] | 405 |
|
---|
[88] | 406 | # This will return MB
|
---|
[1565] | 407 | get_parted ($device,$start,$end,\%type);
|
---|
[1] | 408 |
|
---|
| 409 | while (($n,$d) = each %type) {
|
---|
| 410 | # Print infos fdisk like
|
---|
| 411 | $part = ${device}.$n;
|
---|
[88] | 412 | # start and end are in cylinder in fdisk format
|
---|
| 413 | # so return in MB * 1MB / what represents 1 cyl in B
|
---|
[1801] | 414 | $mstart = sprintf("%d",$$start{$n}*$mega/$un);
|
---|
[88] | 415 | $mstart = 1 if ($mstart < 1);
|
---|
[1565] | 416 | $mstart = $endmax if ($mstart > $endmax);
|
---|
[1801] | 417 | $mend = sprintf("%d",$$end{$n}*$mega/$un - 1);
|
---|
[88] | 418 | $mend = $endmax if ($mend > $endmax);
|
---|
[1565] | 419 | $mend = 1 if ($mend < 1);
|
---|
[88] | 420 | # length is in 1K blocks
|
---|
| 421 | $length = sprintf("%d",($mend-$mstart+1)*$un/1024);
|
---|
[1] | 422 | $pid = $pid{$type{$n}};
|
---|
| 423 | $cmt = $cmt{$type{$n}};
|
---|
[90] | 424 | #print FLOG "$part - $mstart - $mend - $length\n";
|
---|
[1] | 425 |
|
---|
[90] | 426 | if ($verbose == 1) {
|
---|
| 427 | if (not (defined $wpart)) {
|
---|
| 428 | if (length($part) > 13) {
|
---|
| 429 | open(STDOUT2,">&STDOUT") || die "Unable to open STDOUT2";
|
---|
| 430 | select(STDOUT2);
|
---|
| 431 | write;
|
---|
| 432 | open(FLOG2,">&FLOG") || die "Unable to open FLOG2";
|
---|
| 433 | select(FLOG2);
|
---|
| 434 | write;
|
---|
| 435 | select(STDOUT);
|
---|
| 436 | close(FLOG2);
|
---|
| 437 | close(STDOUT2);
|
---|
| 438 | } else {
|
---|
| 439 | open(STDOUT1,">&STDOUT") || die "Unable to open STDOUT1";
|
---|
| 440 | select(STDOUT1);
|
---|
| 441 | write;
|
---|
| 442 | open(FLOG1,">&FLOG") || die "Unable to open FLOG1";
|
---|
| 443 | select(FLOG1);
|
---|
| 444 | write;
|
---|
| 445 | select(STDOUT);
|
---|
| 446 | close(FLOG1);
|
---|
| 447 | close(STDOUT1);
|
---|
| 448 | }
|
---|
[1] | 449 | } else {
|
---|
[90] | 450 | # manage the -s option of fdisk here
|
---|
| 451 | print "$length\n" if ($part eq $wpart);
|
---|
| 452 | print FLOG "$part has $length KBytes\n" if ($part eq $wpart);
|
---|
[1] | 453 | }
|
---|
| 454 | }
|
---|
| 455 | }
|
---|
| 456 | close(FDISK);
|
---|
| 457 | close(PARTED);
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 | #
|
---|
| 461 | # Get max size from fdisk
|
---|
| 462 | #
|
---|
| 463 | sub get_max {
|
---|
| 464 |
|
---|
| 465 | my $device = shift;
|
---|
| 466 | my $max = 0;
|
---|
| 467 | my $foo;
|
---|
| 468 |
|
---|
[1857] | 469 | open (FDISK, "$fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
|
---|
[1] | 470 | while (<FDISK>) {
|
---|
[88] | 471 | if ($_ =~ /heads/) {
|
---|
| 472 | chomp;
|
---|
| 473 | $max = $_;
|
---|
| 474 | $max =~ s/.* ([0-9]+) cylinders/$1/;
|
---|
[1] | 475 | }
|
---|
| 476 | }
|
---|
| 477 | close(FDISK);
|
---|
| 478 | print FLOG "get_max returns $max\n";
|
---|
| 479 | return($max);
|
---|
| 480 | }
|
---|
| 481 |
|
---|
| 482 | #
|
---|
[88] | 483 | # Get units from fdisk (cylinder size)
|
---|
[1] | 484 | #
|
---|
| 485 | sub get_un {
|
---|
| 486 |
|
---|
| 487 | my $device = shift;
|
---|
| 488 | my $wpart = shift;
|
---|
[90] | 489 | my $verbose = shift;
|
---|
[1] | 490 | my $un = 0;
|
---|
| 491 | my $foo;
|
---|
| 492 |
|
---|
[1857] | 493 | open (FDISK, "$fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
|
---|
[1] | 494 | while (<FDISK>) {
|
---|
[90] | 495 | print if (($_ !~ /^\/dev\//) and (not (defined $wpart)) and ($verbose == 1));
|
---|
[1] | 496 | if ($_ =~ /^Units/) {
|
---|
| 497 | ($foo, $un , $foo) = split /=/;
|
---|
| 498 | $un =~ s/[A-z\s=]//g;
|
---|
| 499 | $un = eval($un);
|
---|
| 500 | }
|
---|
| 501 | }
|
---|
| 502 | close(FDISK);
|
---|
| 503 | print FLOG "get_un returns $un\n";
|
---|
| 504 | return($un);
|
---|
| 505 | }
|
---|
| 506 |
|
---|
[88] | 507 | #
|
---|
| 508 | # Parted gives info in MB
|
---|
[1565] | 509 | # (depending on versions - 1.6.25.1 provides suffixes)
|
---|
[88] | 510 | #
|
---|
[1] | 511 | sub get_parted {
|
---|
| 512 |
|
---|
| 513 | my $device = shift;
|
---|
| 514 | my $start = shift;
|
---|
| 515 | my $end = shift;
|
---|
| 516 | my $type = shift;
|
---|
[1565] | 517 | my $void;
|
---|
[1] | 518 | my $d;
|
---|
| 519 | my $n;
|
---|
[1565] | 520 | my $ret;
|
---|
| 521 | my $mode;
|
---|
| 522 | my $size;
|
---|
[1801] | 523 | my $unit;
|
---|
[1] | 524 |
|
---|
[1565] | 525 | open (PARTED, "$parted -v |") || die "Unable to read from $parted";
|
---|
| 526 | $d = <PARTED>;
|
---|
| 527 | print FLOG "$d";
|
---|
| 528 | close(PARTED);
|
---|
| 529 |
|
---|
[1] | 530 | open (PARTED, "$parted -s $device print |") || die "Unable to read from $parted";
|
---|
| 531 | # Skip 3 first lines
|
---|
| 532 | $d = <PARTED>;
|
---|
| 533 | $d = <PARTED>;
|
---|
| 534 | $d = <PARTED>;
|
---|
[1565] | 535 |
|
---|
| 536 | # depending on parted version, information given change:
|
---|
| 537 | if ($d =~ /\bSize\b/) {
|
---|
| 538 | # SLES 10 parted >= 1.6.25
|
---|
| 539 | $mode=1;
|
---|
| 540 | } else {
|
---|
[1801] | 541 | # RHEL 3 parted 1.6.3
|
---|
| 542 | # RHEL 4 parted 1.6.19
|
---|
[1565] | 543 | $mode=0;
|
---|
| 544 | }
|
---|
| 545 | print FLOG "mode: $mode\n";
|
---|
[1] | 546 | print FLOG "Got from parted: \n";
|
---|
[1565] | 547 | print FLOG "Minor Start End Filesystem\n";
|
---|
[1] | 548 | # Get info from each partition line
|
---|
| 549 | while (($n,$d) = split(/\s/, <PARTED>,2)) {
|
---|
| 550 | chomp($d);
|
---|
| 551 | next if ($n !~ /^[1-9]/);
|
---|
| 552 | $d =~ s/^\s*//;
|
---|
| 553 | $d =~ s/\s+/ /g;
|
---|
[1565] | 554 | if ($mode == 0) {
|
---|
| 555 | ($$start{$n},$$end{$n},$$type{$n},$void) = split(/ /,$d);
|
---|
[1801] | 556 | $unit = 1;
|
---|
[1565] | 557 | } elsif ($mode == 1) {
|
---|
| 558 | ($$start{$n},$$end{$n},$size,$$type{$n},$void) = split(/ /,$d);
|
---|
[1801] | 559 | $unit = $mega;
|
---|
[1565] | 560 | } else {
|
---|
| 561 | die "Undefined mode $mode";
|
---|
| 562 | }
|
---|
[1] | 563 | $$start{$n} = "" if (not defined $$start{$n});
|
---|
| 564 | $$end{$n} = "" if (not defined $$end{$n});
|
---|
| 565 | $$type{$n} = "" if (not defined $$type{$n});
|
---|
[1565] | 566 | # Handles potential suffixes in latest parted version. Return MB
|
---|
[1801] | 567 | $ret = decode_Bsuf($$start{$n},$unit);
|
---|
[1565] | 568 | $$start{$n} = $ret;
|
---|
[1801] | 569 | $ret = decode_Bsuf($$end{$n},$unit);
|
---|
[1565] | 570 | $$end{$n} = $ret;
|
---|
| 571 | print FLOG "$n $$start{$n} $$end{$n} $$type{$n}\n";
|
---|
[1] | 572 | }
|
---|
| 573 | close(PARTED);
|
---|
| 574 | }
|
---|
| 575 |
|
---|
[1565] | 576 | sub decode_Bsuf {
|
---|
| 577 |
|
---|
| 578 | my $size = shift;
|
---|
| 579 | my $unit = shift;
|
---|
| 580 | my $ret = 0;
|
---|
| 581 |
|
---|
| 582 | #print FLOG "decode_Bsuf input: $size / $unit ";
|
---|
[1837] | 583 | if ($size =~ /K[B]*$/i) {
|
---|
| 584 | $size =~ s/K[B]*$//i;
|
---|
[1565] | 585 | $size *= 1024;
|
---|
[1837] | 586 | } elsif ($size =~ /M[B]*$/i) {
|
---|
| 587 | $size =~ s/M[B]*$//i;
|
---|
[1565] | 588 | $size *= 1048576;
|
---|
[1837] | 589 | } elsif ($size =~ /G[B]*$/i) {
|
---|
| 590 | $size =~ s/G[B]*$//i;
|
---|
[1565] | 591 | $size *= 1073741824;
|
---|
[1837] | 592 | } elsif ($size =~ /T[B]*$/i) {
|
---|
| 593 | $size =~ s/T[B]*$//i;
|
---|
[1565] | 594 | $size *= 1099511627776;
|
---|
| 595 | } else {
|
---|
| 596 | # Nothing to do
|
---|
| 597 | }
|
---|
| 598 | $ret = $size / $unit;
|
---|
| 599 | #print FLOG " - output : $size => $ret\n";
|
---|
| 600 | return($ret);
|
---|
| 601 | }
|
---|
| 602 |
|
---|
| 603 |
|
---|
[1] | 604 | # Based on Version 2.4 27-Sep-1996 Charles Bailey bailey@genetics.upenn.edu
|
---|
| 605 | # in Basename.pm
|
---|
| 606 |
|
---|
| 607 | sub basename {
|
---|
| 608 |
|
---|
| 609 | my($fullname) = shift;
|
---|
| 610 |
|
---|
| 611 | my($dirpath,$basename);
|
---|
| 612 |
|
---|
| 613 | ($dirpath,$basename) = ($fullname =~ m#^(.*/)?(.*)#s);
|
---|
| 614 |
|
---|
| 615 | return($basename);
|
---|
| 616 | }
|
---|
| 617 |
|
---|
| 618 | sub myexit {
|
---|
| 619 |
|
---|
| 620 | my $val=shift;
|
---|
| 621 |
|
---|
| 622 | close(FLOG);
|
---|
| 623 | exit($val);
|
---|
| 624 | }
|
---|
| 625 |
|
---|
| 626 | sub which_type {
|
---|
| 627 |
|
---|
| 628 | my $device = shift;
|
---|
| 629 | my $type = "";
|
---|
| 630 |
|
---|
[1857] | 631 | open (FDISK, "$fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
|
---|
[1] | 632 | while (<FDISK>) {
|
---|
| 633 | if ($_ =~ /EFI GPT/) {
|
---|
| 634 | $type= "gpt";
|
---|
| 635 | print FLOG "Found a GPT partition format\n";
|
---|
| 636 | last;
|
---|
| 637 | }
|
---|
| 638 | }
|
---|
| 639 | close(FDISK);
|
---|
| 640 | open (PARTED, "$parted -s $device print|") || die "Unable to read from $fdisk";
|
---|
| 641 | while (<PARTED>) {
|
---|
| 642 | if ($_ =~ /Disk label type: msdos/) {
|
---|
| 643 | $type= "msdos";
|
---|
| 644 | print FLOG "Found a msdos partition format\n";
|
---|
| 645 | last;
|
---|
| 646 | }
|
---|
| 647 | }
|
---|
| 648 | close(FDISK);
|
---|
| 649 | return ($type);
|
---|
| 650 | }
|
---|
| 651 |
|
---|
| 652 | sub mysyn {
|
---|
| 653 | print "Syntax: $0 [-l] device | [-s] partition\n";
|
---|
| 654 | myexit(-1);
|
---|
| 655 | }
|
---|