source: MondoRescue/trunk/mindi/parted2fdisk.pl@ 1

Last change on this file since 1 was 1, checked in by bcornec, 19 years ago

Initial import from latest mondo-2.04_cvs_20050503/mindi-1.04_cvs_20050503 on http://www.mondorescue.org

File size: 12.1 KB
Line 
1#!/usr/bin/perl -w
2#
3# parted2fdisk: fdisk like interface for parted
4# [developped for mindi/mondo http://www.mondorescue.org]
5#
6# Aims to be architecture independant (i386/ia64)
7# Tested on RHAS 2.1 ia64 - Mandrake 9.0 ia64 - RHAS 3.0 ia64
8#
9# (c) Bruno Cornec <Bruno.Cornec@hp.com>
10# Licensed under the GPL
11
12use strict;
13
14$ENV{LANG} = "C";
15$ENV{LANGUAGE} = "C";
16$ENV{LC_ALL} = "C";
17
18# Log
19my $flog = "/tmp/parted2fdisk.log";
20open(FLOG, "> $flog") || die "Unable to open $flog";
21
22my $fdisk = "/sbin/fdisk";
23my $parted = "/sbin/parted";
24
25my $i;
26my $l;
27my $part;
28my $wpart;
29my $start = "";
30my $end = "";
31my %start;
32my %end;
33my %type;
34my %flags;
35my $arch;
36
37# Determine on which arch we're running
38if (defined ($ENV{ARCH})) {
39 $arch = $ENV{ARCH};
40} else {
41 $arch = `"/bin/arch"`;
42 chomp($arch);
43}
44
45#
46# Looking for fdisk
47#
48$fdisk = is_lsb($fdisk);
49#
50# We always use fdisk except on ia64 with GPT types of
51# partition tables where we need parted
52# All should return fdisk like format so that callers
53# think they have called fdisk directly
54#
55my $un;
56my $type;
57my $args = "";
58my $device = "";
59
60if ($#ARGV < 0) {
61 printf FLOG "No arguments given exiting ...\n";
62 mysyn();
63}
64
65my %pid = ( "FAT" => "6",
66 "fat32" => "b",
67 "fat16" => "e",
68 "ext2" => "83",
69 "ext3" => "83",
70 "linux-swap" => "82",
71 "LVM" => "8e",
72 "" => "",
73 );
74my %pnum;
75
76# Reverse table of pid
77while (($i,$l) = each %pid) {
78 next if ($i eq "ext2");
79 $pnum{$l} = $i;
80}
81
82foreach $i (@ARGV) {
83 # We support at most one option and one device
84 print FLOG "Parameter found : $i\n";
85 if ($i =~ /^\/dev\//) {
86 $device = $i;
87 next;
88 } elsif ($i =~ /^-/) {
89 $args = $i;
90 next;
91 } else {
92 mysyn();
93 }
94}
95
96if (($args ne "") and ($device eq "")) {
97 mysyn();
98}
99
100# -s takes a partition as arg
101if ($args =~ /-s/) {
102 $wpart = $device;
103 $device =~ s/[0-9]+$//;
104}
105
106print FLOG "Called with device $device and arg $args\n";
107
108if ($arch =~ /^ia64/) {
109 # Check partition table type
110 print FLOG "We're on ia64 ...\n";
111 $parted = is_lsb($parted);
112 $type = which_type($device);
113 if ($type ne "msdos") {
114 print FLOG "Not an msdos type of disk label\n";
115 if ($args =~ /-l/) {
116 fdisk_list($device,undef,\%start,\%end);
117 } elsif ($args =~ /-s/) {
118 fdisk_list($device,$wpart,\%start,\%end);
119 } elsif ($args =~ /-/) {
120 printf FLOG "Option not supported ($args) ...\n";
121 printf FLOG "Please report to the author\n";
122 mysyn();
123 } else {
124 # Read fdisk orders on stdin and pass them to parted
125 # on the command line as parted doesn't read on stdin
126 print FLOG "Translating fdisk command to parted\n";
127 while ($i = <STDIN>) {
128 if ($i =~ /^p$/) {
129 fdisk_list($device,undef,\%start,\%end);
130 }
131 elsif ($i =~ /^n$/) {
132 fdisk_list($device,undef,\%start,\%end);
133 if ($type ne "gpt") {
134 print FLOG "Forcing GPT type of disk label\n";
135 print FLOG "mklabel gpt\n";
136 system "$parted -s $device mklabel gpt\n";
137 $type = "gpt";
138 }
139 $l = <STDIN>;
140 if (not (defined $l)) {
141 print FLOG "no primary/extended arg given for creation... assuming primary\n";
142 $l = "p";
143 }
144 chomp($l);
145 $part = <STDIN>;
146 if ((not (defined $part)) || ($part eq "")) {
147 print FLOG "no partition given for creation... skipping\n";
148 next;
149 }
150 chomp($part);
151 $start = <STDIN>;
152 chomp($start);
153 if ((not (defined $start)) || ($start eq "")) {
154 if (defined $start{$part-1}) {
155 $start = scalar $end{$part-1} + 0.001;
156 print FLOG "no start cyl given for creation... assuming the following $start\n";
157 } else {
158 print FLOG "no start cyl given for creation... assuming the following 1\n";
159 $start = 1;
160 }
161 }
162 print FLOG "start cyl : $start\n";
163 $end = <STDIN>;
164 chomp($end);
165 if ((not (defined $end)) || ($end eq "")) {
166 print FLOG "no end cyl given for creation... assuming full disk)\n";
167 $end = get_max($device);
168 }
169 $un = get_un($device);
170 # Handles end syntaxes (+, K, M, ...)
171 if ($end =~ /^\+/) {
172 $end =~ s/^\+//;
173 if ($end =~ /K$/) {
174 $end =~ s/K$//;
175 $end *= 1000;
176 } elsif ($end =~ /M$/) {
177 $end =~ s/M$//;
178 $end *= 1000000;
179 } elsif ($end =~ /G$/) {
180 $end =~ s/G$//;
181 $end *= 1000000000;
182 }
183 $end /= $un;
184 $end = int($end)+1;
185 $end += $start - 0.001;
186 }
187 print FLOG "end cyl : $end\n";
188 print FLOG "n $l $part $start $end => mkpart primary $start $end\n";
189 system "$parted -s $device mkpart primary ext2 $start $end\n";
190 }
191 elsif ($i =~ /^d$/) {
192 $part = <STDIN>;
193 if (not (defined $part)) {
194 print FLOG "no partition given for deletion... skipping\n";
195 next;
196 }
197 chomp($part);
198 print FLOG "d $part => rm $part\n";
199 system "$parted -s $device rm $part\n";
200 get_parted($device,undef,\%start,\%end,undef,undef);
201 }
202 elsif ($i =~ /^w$/) {
203 print FLOG "w => quit\n";
204 }
205 elsif ($i =~ /^t$/) {
206 $part = <STDIN>;
207 if (not (defined $part)) {
208 print FLOG "no partition given for tagging... skipping\n";
209 next;
210 }
211 chomp($part);
212 $l = <STDIN>;
213 if (not (defined $l)) {
214 print FLOG "no type given for tagging partition $part... skipping\n";
215 next;
216 }
217 chomp($l);
218 if (not (defined $pnum{$l})) {
219 print FLOG "no partition number given for $l... please report to the author\n";
220 next;
221 }
222 print FLOG "t $part => mkfs $part $pnum{$l}\n";
223 system "$parted -s $device mkfs $part $pnum{$l}\n";
224 }
225 elsif ($i =~ /^a$/) {
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 print FLOG "a $part => set $part boot on\n";
233 system "$parted -s $device set $part boot on\n";
234 }
235 elsif ($i =~ /^q$/) {
236 print FLOG "q => quit\n";
237 }
238 else {
239 print FLOG "Unknown command: $i\n";
240 next;
241 }
242
243 }
244 }
245 myexit(0);
246 }
247}
248
249#
250# Else everything is for fdisk
251#
252# Print only mode
253print FLOG "Passing everything to the real fdisk\n";
254my $fargs = join(@ARGV);
255
256if ($args =~ /^-/) {
257 # -l or -s
258 open (FDISK, "$fdisk $fargs |") || die "Unable to read from $fdisk";
259 while (<FDISK>) {
260 print;
261 }
262 close(FDISK);
263} else {
264 # Modification mode
265 open (FDISK, "| $fdisk $fargs") || die "Unable to modify through $fdisk";
266 while (<STDIN>) {
267 print FDISK;
268 }
269 close(FDISK);
270 close(STDIN);
271}
272myexit(0);
273
274
275# Is your system LSB ?
276sub is_lsb {
277
278my $cmd = shift;
279my $basename = basename($cmd);
280my $mindifdisk="/usr/local/bin/fdisk";
281
282if ($cmd =~ /fdisk/) {
283 if ($arch =~ /^ia64/) {
284 if (-l $cmd) {
285 print FLOG "Your system is ready for mondo-archive on ia64\n";
286 } else {
287 print FLOG "Your system is ready for mondo-restore on ia64\n";
288 }
289 if (-x $mindifdisk) {
290 $cmd = $mindifdisk;
291 } else {
292 print FLOG "Your system doesn't provide $mindifdisk\n";
293 print FLOG "Please use mindi-x.yz-ia64.rpm on the system to backup\n";
294 myexit(-1);
295 }
296 }
297}
298if (not (-x $cmd)) {
299 print FLOG "Your system is not LSB/mondo compliant: $basename was not found as $cmd\n";
300 print FLOG "Searching elswhere...";
301 foreach $i (split(':',$ENV{PATH})) {
302 if (-x "$i/$basename") {
303 $cmd = "$i/$basename";
304 print FLOG "Found $cmd, using it !\n";
305 last;
306 }
307 }
308 if (not (-x $cmd)) {
309 print FLOG "Your system doesn't provide $basename in the PATH\n";
310 print FLOG "Please correct it before relaunching\n";
311 myexit(-1);
312 }
313}
314return($cmd);
315}
316
317sub fdisk_list {
318
319my $device = shift;
320my $wpart = shift;
321my $start = shift;
322my $end = shift;
323
324my $un;
325my $d;
326my $n;
327
328my %cmt = ( "FAT" => "FAT",
329 "ext2" => "Linux",
330 "ext3" => "Linux",
331 "linux-swap" => "Linux swap",
332 "LVM" => "Linux LVM",
333 "fat16" => "fat16",
334 "fat32" => "fat32",
335 "" => "Linux",
336);
337
338my $part;
339my $mstart;
340my $mend;
341my $length;
342my $pid;
343my $cmt;
344format FLOG1 =
345@<<<<<<<<<<<< @>>>>>>>> @>>>>>>>> @>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
346$part, $mstart, $mend, $length, $pid, $cmt
347.
348format FLOG2 =
349@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
350$part,
351 @>>>>>>>> @>>>>>>>> @>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
352 $mstart, $mend, $length, $pid, $cmt
353.
354format STDOUT1 =
355@<<<<<<<<<<<< @>>>>>>>> @>>>>>>>> @>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
356$part, $mstart, $mend, $length, $pid, $cmt
357.
358format STDOUT2 =
359@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
360$part,
361 @>>>>>>>> @>>>>>>>> @>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
362 $mstart, $mend, $length, $pid, $cmt
363.
364#/dev/hda1 1 77579 39099374+ ee EFI GPT
365
366#
367# Keep Fdisk headers
368#
369$un = get_un ($device,$wpart);
370get_parted ($device,$start,$end,\%type,\%flags);
371
372while (($n,$d) = each %type) {
373 # Print infos fdisk like
374 $part = ${device}.$n;
375 $mstart = sprintf("%d",int($$start{$n}));
376 $mend = sprintf("%d",int($$end{$n}));
377 $length = sprintf("%d",($mend-$mstart+1)*$un/1048576);
378 $pid = $pid{$type{$n}};
379 $cmt = $cmt{$type{$n}};
380 print FLOG "$part - $mstart - $mend - $length\n";
381
382 if (not (defined $wpart)) {
383 if (length($part) > 13) {
384 open(STDOUT2,">&STDOUT") || die "Unable to open STDOUT2";
385 select(STDOUT2);
386 write;
387 open(FLOG2,">&FLOG") || die "Unable to open FLOG2";
388 select(FLOG2);
389 write;
390 select(STDOUT);
391 close(FLOG2);
392 close(STDOUT2);
393 } else {
394 open(STDOUT1,">&STDOUT") || die "Unable to open STDOUT1";
395 select(STDOUT1);
396 write;
397 open(FLOG1,">&FLOG") || die "Unable to open FLOG1";
398 select(FLOG1);
399 write;
400 select(STDOUT);
401 close(FLOG1);
402 close(STDOUT1);
403 }
404 } else {
405 # manage the -s option of fdisk here
406 my $s = int(eval("$length/1048576*$un*1024"));
407 print "$s\n" if ($part eq $wpart);
408 print FLOG "$part has $s Bytes\n" if ($part eq $wpart);
409 }
410}
411close(FDISK);
412close(PARTED);
413}
414
415#
416# Get max size from fdisk
417#
418sub get_max {
419
420my $device = shift;
421my $max = 0;
422my $foo;
423
424open (FDISK, "$fdisk -l $device |") || die "Unable to read from $fdisk";
425while (<FDISK>) {
426 if ($_ =~ /^Disk/) {
427 ($foo, $un , $foo) = split /=/;
428 $max =~ s/.*sectors,([0-9]+) cylinders/$1/g;
429 $max = eval($max);
430 }
431}
432close(FDISK);
433print FLOG "get_max returns $max\n";
434return($max);
435}
436
437#
438# Get units from fdisk
439#
440sub get_un {
441
442my $device = shift;
443my $wpart = shift;
444my $un = 0;
445my $foo;
446
447open (FDISK, "$fdisk -l $device |") || die "Unable to read from $fdisk";
448while (<FDISK>) {
449 print if (($_ !~ /^\/dev\//) and (not (defined $wpart)));
450 if ($_ =~ /^Units/) {
451 ($foo, $un , $foo) = split /=/;
452 $un =~ s/[A-z\s=]//g;
453 $un = eval($un);
454 }
455}
456close(FDISK);
457print FLOG "get_un returns $un\n";
458return($un);
459}
460
461sub get_parted {
462
463my $device = shift;
464my $start = shift;
465my $end = shift;
466my $type = shift;
467my $flags = shift;
468my $d;
469my $n;
470
471open (PARTED, "$parted -s $device print |") || die "Unable to read from $parted";
472# Skip 3 first lines
473$d = <PARTED>;
474$d = <PARTED>;
475$d = <PARTED>;
476print FLOG "Got from parted: \n";
477print FLOG "Minor Start End Filesystem Flags\n";
478# Get info from each partition line
479while (($n,$d) = split(/\s/, <PARTED>,2)) {
480 chomp($d);
481 next if ($n !~ /^[1-9]/);
482 $d =~ s/^\s*//;
483 $d =~ s/\s+/ /g;
484 ($$start{$n},$$end{$n},$$type{$n},$$flags{$n}) = split(/ /,$d);
485 $$start{$n} = "" if (not defined $$start{$n});
486 $$end{$n} = "" if (not defined $$end{$n});
487 $$type{$n} = "" if (not defined $$type{$n});
488 $$flags{$n} = "" if (not defined $$flags{$n});
489 print FLOG "$n $$start{$n} $$end{$n} $$type{$n} $$flags{$n}\n";
490}
491close(PARTED);
492}
493
494# Based on Version 2.4 27-Sep-1996 Charles Bailey bailey@genetics.upenn.edu
495# in Basename.pm
496
497sub basename {
498
499my($fullname) = shift;
500
501my($dirpath,$basename);
502
503($dirpath,$basename) = ($fullname =~ m#^(.*/)?(.*)#s);
504
505return($basename);
506}
507
508sub myexit {
509
510my $val=shift;
511
512close(FLOG);
513exit($val);
514}
515
516sub which_type {
517
518my $device = shift;
519my $type = "";
520
521open (FDISK, "$fdisk -l $device |") || die "Unable to read from $fdisk";
522while (<FDISK>) {
523 if ($_ =~ /EFI GPT/) {
524 $type= "gpt";
525 print FLOG "Found a GPT partition format\n";
526 last;
527 }
528}
529close(FDISK);
530open (PARTED, "$parted -s $device print|") || die "Unable to read from $fdisk";
531while (<PARTED>) {
532 if ($_ =~ /Disk label type: msdos/) {
533 $type= "msdos";
534 print FLOG "Found a msdos partition format\n";
535 last;
536 }
537}
538close(FDISK);
539return ($type);
540}
541
542sub mysyn {
543 print "Syntax: $0 [-l] device | [-s] partition\n";
544 myexit(-1);
545}
Note: See TracBrowser for help on using the repository browser.