source: MondoRescue/branches/2.2.5/mindi/parted2fdisk.pl@ 1764

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