source: MondoRescue/branches/2.2.10/mindi/parted2fdisk.pl@ 2354

Last change on this file since 2354 was 2192, checked in by Bruno Cornec, 15 years ago

Security Fix for a remaining /tmp hard coded reference for 2 log files only used on ia64 (parted2fdisk.pl) and which shouln't have a small potential impact for mondo-prep.c as the file was removed before being recreated and usage is very short. Please report other finding as those to bruno_at_mondorescue.org (Thanks to Gentoo project)

  • Property svn:keywords set to Id
File size: 15.2 KB
Line 
1#!/usr/bin/perl -w
2#
3# $Id: parted2fdisk.pl 2192 2009-05-07 23:57:29Z 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 ia64 with RHAS 2.1 - Mandrake 9.0 - RHEL 3.0 - 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 = "/var/log/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;
40my $mega = 1048576;
41
42# Immediate flushing to avoids read error from mondorestore in log files
43$| = 1;
44
45# Determine on which arch we're running
46if (defined ($ENV{ARCH})) {
47 $arch = $ENV{ARCH};
48} else {
49 $arch = `uname -m`;
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#
63my $un;
64my $type;
65my $args = "";
66my $device = "";
67my $endmax = "";
68
69if ($#ARGV < 0) {
70 printf FLOG "No arguments given exiting ...\n";
71 mysyn();
72}
73
74my %pid = ( "FAT" => "6",
75 "fat32" => "b",
76 "fat16" => "e",
77 "ext2" => "83",
78 "ext3" => "83",
79 "ext4" => "83",
80 "xfs" => "83",
81 "reiserfs" => "83",
82 "linux-swap" => "82",
83 "lvm" => "8e",
84 "" => "",
85 );
86my %pnum;
87
88# Reverse table of pid
89while (($i,$l) = each %pid) {
90 next if ($i eq "ext2");
91 $pnum{$l} = $i;
92}
93
94foreach $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
108if (($args ne "") and ($device eq "")) {
109 mysyn();
110}
111
112# -s takes a partition as arg
113if ($args =~ /-s/) {
114 $wpart = $device;
115 $device =~ s/[0-9]+$//;
116}
117
118if ($args =~ /-n/) {
119 print FLOG "Fake mode. Nothing will be really done\n";
120 $fake = 1;
121}
122
123print FLOG "Called with device $device and arg $args\n";
124
125if ($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/) {
133 fdisk_list($device,undef,\%start,\%end, 1);
134 } elsif ($args =~ /-s/) {
135 fdisk_list($device,$wpart,\%start,\%end, 1);
136 } elsif (($args =~ /-/) and ($fake == 0)) {
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$/) {
146 fdisk_list($device,undef,\%start,\%end, 1);
147 print "command (m for help) send back to fake fdisk for mondorestore\n";
148 } elsif ($i =~ /^n$/) {
149 fdisk_list($device,undef,\%start,\%end, 0);
150 if ($type ne "gpt") {
151 print FLOG "Forcing GPT type of disk label\n";
152 print FLOG "mklabel gpt\n";
153 system "$parted -s $device mklabel gpt\n" if ($fake == 0);
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);
168 $cylstart = <STDIN>;
169 chomp($cylstart);
170 if ((not (defined $cylstart)) || ($cylstart eq "")) {
171 if (defined $start{$part-1}) {
172 # in MB => cyl
173 $cylstart = sprintf("%d",$end{$part-1}*$mega/$un + 1);
174 print FLOG "no start cyl given for creation... assuming the following $cylstart\n";
175 } else {
176 print FLOG "no start cyl given for creation... assuming the following 1\n";
177 $cylstart = 1;
178 }
179 }
180 $cylstart = 1 if ($cylstart < 1);
181 print FLOG "start cyl : $cylstart\n";
182 $un = get_un($device, "", 0);
183 # parted needs MB
184 if ($cylstart == 1) {
185 $start = 0.01;
186 } else {
187 $start = $cylstart* $un / $mega + 0.001;
188 }
189 # this is a size in B/KB/MB/GB
190
191 $endmax = get_max($device);
192 $cylend = <STDIN>;
193 chomp($cylend);
194 if ((not (defined $cylend)) || ($cylend eq "")) {
195 print FLOG "no end cyl given for creation... assuming full disk)\n";
196 $cylend = $endmax;
197 }
198 # Handles end syntaxes (+, K, M, ...)
199 # to give cylinders
200 if ($cylend =~ /^\+/) {
201 $cylend =~ s/^\+//;
202 # Handles suffixes; return bytes
203 $cylend = decode_Bsuf($cylend,1);
204 # This gives the number of cyl
205 $cylend /= $un;
206 $cylend = sprintf("%d",$cylend);
207 $cylend += $cylstart - 0.001;
208 # We now have the end cyl
209 }
210 $cylend = $endmax if ($cylend > $endmax);
211 print FLOG "end cyl : $cylend\n";
212 # parted needs MB
213 $end = $cylend * $un / $mega;
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);
216 print "command (m for help) send back to fake fdisk for mondorestore\n";
217 } elsif ($i =~ /^d$/) {
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";
225 system "$parted -s $device rm $part\n" if ($fake == 0);
226 get_parted($device,undef,\%start,\%end,undef);
227 print "command (m for help) send back to fake fdisk for mondorestore\n";
228 } elsif ($i =~ /^w$/) {
229 print FLOG "w => quit\n";
230 } elsif ($i =~ /^t$/) {
231 $part = <STDIN>;
232 if (not (defined $part)) {
233 print FLOG "no partition given for tagging... skipping\n";
234 next;
235 }
236 chomp($part);
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 }
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 }
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";
263 } elsif ($i =~ /^a$/) {
264 $part = <STDIN>;
265 if (not (defined $part)) {
266 print FLOG "no partition given for tagging... skipping\n";
267 next;
268 }
269 chomp($part);
270
271 # Partition shouldn't be negative or null. Then take the first one.
272 $part = 1 if ($part le 0);
273
274 print FLOG "a $part => set $part boot on\n";
275 system "$parted -s $device set $part boot on\n" if ($fake == 0);
276 print "command (m for help) send back to fake fdisk for mondorestore\n";
277 } elsif ($i =~ /^q$/) {
278 print FLOG "q => quit\n";
279 } else {
280 print FLOG "Unknown command: $i\n";
281 print "command (m for help) send back to fake fdisk for mondorestore\n";
282 next;
283 }
284
285 }
286 }
287 myexit(0);
288 }
289}
290
291#
292# Else everything is for fdisk
293#
294# Print only mode
295print FLOG "Passing everything to the real fdisk\n";
296my $fargs = join(@ARGV);
297
298if ($args =~ /^-/) {
299 # -l or -s
300 open (FDISK, "$fdisk $fargs 2>/dev/null |") || die "Unable to read from $fdisk";
301 while (<FDISK>) {
302 print;
303 }
304 close(FDISK);
305} else {
306 # Modification mode
307 open (FDISK, "| $fdisk $fargs 2>/dev/null") || die "Unable to modify through $fdisk";
308 while (<STDIN>) {
309 print FDISK;
310 }
311 close(FDISK);
312 close(STDIN);
313}
314myexit(0);
315
316
317# Is your system LSB ?
318sub is_lsb {
319
320my $cmd = shift;
321my $basename = basename($cmd);
322
323if (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}
339return($cmd);
340}
341
342sub fdisk_list {
343
344my $device = shift;
345my $wpart = shift;
346my $start = shift;
347my $end = shift;
348my $verbose = shift;
349
350my $un;
351my $endmax;
352my $d;
353my $n;
354
355my %cmt = ( "FAT" => "FAT",
356 "ext2" => "Linux",
357 "ext3" => "Linux",
358 "ext4" => "Linux",
359 "xfs" => "Linux",
360 "reiserfs" => "Linux",
361 "linux-swap" => "Linux swap",
362 "lvm" => "Linux LVM",
363 "fat16" => "fat16",
364 "fat32" => "fat32",
365 "" => "Linux",
366);
367
368my $part;
369my $mstart;
370my $mend;
371my $length;
372my $pid;
373my $cmt;
374format FLOG1 =
375@<<<<<<<<<<<< @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
376$part, $mstart, $mend, $length, $pid, $cmt
377.
378format FLOG2 =
379@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
380$part,
381 @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
382 $mstart, $mend, $length, $pid, $cmt
383.
384format STDOUT1 =
385@<<<<<<<<<<<< @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
386$part, $mstart, $mend, $length, $pid, $cmt
387.
388format STDOUT2 =
389@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
390$part,
391 @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
392 $mstart, $mend, $length, $pid, $cmt
393.
394# Device Boot Start End Blocks Id System
395#/dev/hda1 1 77579 39099374+ ee EFI GPT
396
397
398#
399# Keep Fdisk headers
400#
401# this will return bytes
402$un = get_un ($device,$wpart,$verbose);
403
404$endmax = get_max($device);
405
406# This will return MB
407get_parted ($device,$start,$end,\%type);
408
409while (($n,$d) = each %type) {
410 # Print infos fdisk like
411 $part = ${device}.$n;
412 # start and end are in cylinder in fdisk format
413 # so return in MB * 1MB / what represents 1 cyl in B
414 $mstart = sprintf("%d",$$start{$n}*$mega/$un);
415 $mstart = 1 if ($mstart < 1);
416 $mstart = $endmax if ($mstart > $endmax);
417 $mend = sprintf("%d",$$end{$n}*$mega/$un - 1);
418 $mend = $endmax if ($mend > $endmax);
419 $mend = 1 if ($mend < 1);
420 # length is in 1K blocks
421 $length = sprintf("%d",($mend-$mstart+1)*$un/1024);
422 $pid = $pid{$type{$n}};
423 $cmt = $cmt{$type{$n}};
424 #print FLOG "$part - $mstart - $mend - $length\n";
425
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 }
449 } else {
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);
453 }
454 }
455}
456close(FDISK);
457close(PARTED);
458}
459
460#
461# Get max size from fdisk
462#
463sub get_max {
464
465my $device = shift;
466my $max = 0;
467my $foo;
468
469open (FDISK, "$fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
470while (<FDISK>) {
471 if ($_ =~ /heads/) {
472 chomp;
473 $max = $_;
474 $max =~ s/.* ([0-9]+) cylinders/$1/;
475 }
476}
477close(FDISK);
478print FLOG "get_max returns $max\n";
479return($max);
480}
481
482#
483# Get units from fdisk (cylinder size)
484#
485sub get_un {
486
487my $device = shift;
488my $wpart = shift;
489my $verbose = shift;
490my $un = 0;
491my $foo;
492
493open (FDISK, "$fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
494while (<FDISK>) {
495 print if (($_ !~ /^\/dev\//) and (not (defined $wpart)) and ($verbose == 1));
496 if ($_ =~ /^Units/) {
497 ($foo, $un , $foo) = split /=/;
498 $un =~ s/[A-z\s=]//g;
499 $un = eval($un);
500 }
501}
502close(FDISK);
503print FLOG "get_un returns $un\n";
504return($un);
505}
506
507#
508# Parted gives info in MB
509# (depending on versions - 1.6.25.1 provides suffixes)
510#
511sub get_parted {
512
513my $device = shift;
514my $start = shift;
515my $end = shift;
516my $type = shift;
517my $void;
518my $d;
519my $n;
520my $ret;
521my $mode;
522my $size;
523my $unit;
524
525open (PARTED, "$parted -v |") || die "Unable to read from $parted";
526$d = <PARTED>;
527print FLOG "$d";
528close(PARTED);
529
530open (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>;
535
536# depending on parted version, information given change:
537if ($d =~ /\bSize\b/) {
538 # SLES 10 parted >= 1.6.25
539 $mode=1;
540} else {
541 # RHEL 3 parted 1.6.3
542 # RHEL 4 parted 1.6.19
543 $mode=0;
544}
545print FLOG "mode: $mode\n";
546print FLOG "Got from parted: \n";
547print FLOG "Minor Start End Filesystem\n";
548# Get info from each partition line
549while (($n,$d) = split(/\s/, <PARTED>,2)) {
550 chomp($d);
551 next if ($n !~ /^[1-9]/);
552 $d =~ s/^\s*//;
553 $d =~ s/\s+/ /g;
554 if ($mode == 0) {
555 ($$start{$n},$$end{$n},$$type{$n},$void) = split(/ /,$d);
556 $unit = 1;
557 } elsif ($mode == 1) {
558 ($$start{$n},$$end{$n},$size,$$type{$n},$void) = split(/ /,$d);
559 $unit = $mega;
560 } else {
561 die "Undefined mode $mode";
562 }
563 $$start{$n} = "" if (not defined $$start{$n});
564 $$end{$n} = "" if (not defined $$end{$n});
565 $$type{$n} = "" if (not defined $$type{$n});
566 # Handles potential suffixes in latest parted version. Return MB
567 $ret = decode_Bsuf($$start{$n},$unit);
568 $$start{$n} = $ret;
569 $ret = decode_Bsuf($$end{$n},$unit);
570 $$end{$n} = $ret;
571 print FLOG "$n $$start{$n} $$end{$n} $$type{$n}\n";
572}
573close(PARTED);
574}
575
576sub decode_Bsuf {
577
578my $size = shift;
579my $unit = shift;
580my $ret = 0;
581
582#print FLOG "decode_Bsuf input: $size / $unit ";
583if ($size =~ /K[B]*$/i) {
584 $size =~ s/K[B]*$//i;
585 $size *= 1024;
586} elsif ($size =~ /M[B]*$/i) {
587 $size =~ s/M[B]*$//i;
588 $size *= 1048576;
589} elsif ($size =~ /G[B]*$/i) {
590 $size =~ s/G[B]*$//i;
591 $size *= 1073741824;
592} elsif ($size =~ /T[B]*$/i) {
593 $size =~ s/T[B]*$//i;
594 $size *= 1099511627776;
595} else {
596 # Nothing to do
597}
598$ret = $size / $unit;
599#print FLOG " - output : $size => $ret\n";
600return($ret);
601}
602
603
604# Based on Version 2.4 27-Sep-1996 Charles Bailey bailey@genetics.upenn.edu
605# in Basename.pm
606
607sub basename {
608
609my($fullname) = shift;
610
611my($dirpath,$basename);
612
613($dirpath,$basename) = ($fullname =~ m#^(.*/)?(.*)#s);
614
615return($basename);
616}
617
618sub myexit {
619
620my $val=shift;
621
622close(FLOG);
623exit($val);
624}
625
626sub which_type {
627
628my $device = shift;
629my $type = "";
630
631open (FDISK, "$fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
632while (<FDISK>) {
633 if ($_ =~ /EFI GPT/) {
634 $type= "gpt";
635 print FLOG "Found a GPT partition format\n";
636 last;
637 }
638}
639close(FDISK);
640open (PARTED, "$parted -s $device print|") || die "Unable to read from $fdisk";
641while (<PARTED>) {
642 if ($_ =~ /Disk label type: msdos/) {
643 $type= "msdos";
644 print FLOG "Found a msdos partition format\n";
645 last;
646 }
647}
648close(FDISK);
649return ($type);
650}
651
652sub mysyn {
653 print "Syntax: $0 [-l] device | [-s] partition\n";
654 myexit(-1);
655}
Note: See TracBrowser for help on using the repository browser.