source: MondoRescue/branches/2.2.8/mindi/parted2fdisk.pl@ 2087

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