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