source: MondoRescue/branches/2.2.9/mindi/parted2fdisk.pl@ 2192

Last change on this file since 2192 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
RevLine 
[1]1#!/usr/bin/perl -w
2#
[88]3# $Id: parted2fdisk.pl 2192 2009-05-07 23:57:29Z 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
14use strict;
15
16$ENV{LANG} = "C";
17$ENV{LANGUAGE} = "C";
18$ENV{LC_ALL} = "C";
19
20# Log
[2192]21my $flog = "/var/log/parted2fdisk.log";
[1]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
[2163]42# Immediate flushing to avoids read error from mondorestore in log files
43$| = 1;
44
[1]45# Determine on which arch we're running
46if (defined ($ENV{ARCH})) {
47 $arch = $ENV{ARCH};
48} else {
[725]49 $arch = `uname -m`;
[1]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 = "";
[88]67my $endmax = "";
[1]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",
[2087]79 "ext4" => "83",
80 "xfs" => "83",
[1565]81 "reiserfs" => "83",
[1]82 "linux-swap" => "82",
[2158]83 "lvm" => "8e",
[1]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
[88]118if ($args =~ /-n/) {
119 print FLOG "Fake mode. Nothing will be really done\n";
120 $fake = 1;
121}
122
[1]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/) {
[90]133 fdisk_list($device,undef,\%start,\%end, 1);
[1]134 } elsif ($args =~ /-s/) {
[90]135 fdisk_list($device,$wpart,\%start,\%end, 1);
[88]136 } elsif (($args =~ /-/) and ($fake == 0)) {
[1]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$/) {
[90]146 fdisk_list($device,undef,\%start,\%end, 1);
[2163]147 print "command (m for help) send back to fake fdisk for mondorestore\n";
[2154]148 } elsif ($i =~ /^n$/) {
[90]149 fdisk_list($device,undef,\%start,\%end, 0);
[1]150 if ($type ne "gpt") {
151 print FLOG "Forcing GPT type of disk label\n";
152 print FLOG "mklabel gpt\n";
[90]153 system "$parted -s $device mklabel gpt\n" if ($fake == 0);
[1]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);
[90]168 $cylstart = <STDIN>;
169 chomp($cylstart);
170 if ((not (defined $cylstart)) || ($cylstart eq "")) {
[1]171 if (defined $start{$part-1}) {
[90]172 # in MB => cyl
[1801]173 $cylstart = sprintf("%d",$end{$part-1}*$mega/$un + 1);
[90]174 print FLOG "no start cyl given for creation... assuming the following $cylstart\n";
[1]175 } else {
176 print FLOG "no start cyl given for creation... assuming the following 1\n";
[90]177 $cylstart = 1;
[1]178 }
179 }
[90]180 $cylstart = 1 if ($cylstart < 1);
181 print FLOG "start cyl : $cylstart\n";
182 $un = get_un($device, "", 0);
[88]183 # parted needs MB
[90]184 if ($cylstart == 1) {
185 $start = 0.01;
186 } else {
[1801]187 $start = $cylstart* $un / $mega + 0.001;
[90]188 }
[88]189 # this is a size in B/KB/MB/GB
[90]190
[88]191 $endmax = get_max($device);
[90]192 $cylend = <STDIN>;
193 chomp($cylend);
194 if ((not (defined $cylend)) || ($cylend eq "")) {
[1]195 print FLOG "no end cyl given for creation... assuming full disk)\n";
[90]196 $cylend = $endmax;
[1]197 }
198 # Handles end syntaxes (+, K, M, ...)
[88]199 # to give cylinders
[90]200 if ($cylend =~ /^\+/) {
201 $cylend =~ s/^\+//;
[1565]202 # Handles suffixes; return bytes
203 $cylend = decode_Bsuf($cylend,1);
[88]204 # This gives the number of cyl
[90]205 $cylend /= $un;
206 $cylend = sprintf("%d",$cylend);
207 $cylend += $cylstart - 0.001;
[88]208 # We now have the end cyl
[1]209 }
[90]210 $cylend = $endmax if ($cylend > $endmax);
211 print FLOG "end cyl : $cylend\n";
[88]212 # parted needs MB
[1801]213 $end = $cylend * $un / $mega;
[90]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);
[2158]216 print "command (m for help) send back to fake fdisk for mondorestore\n";
[2154]217 } elsif ($i =~ /^d$/) {
[1]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";
[90]225 system "$parted -s $device rm $part\n" if ($fake == 0);
226 get_parted($device,undef,\%start,\%end,undef);
[2158]227 print "command (m for help) send back to fake fdisk for mondorestore\n";
[2154]228 } elsif ($i =~ /^w$/) {
[1]229 print FLOG "w => quit\n";
[2154]230 } elsif ($i =~ /^t$/) {
[1]231 $part = <STDIN>;
232 if (not (defined $part)) {
233 print FLOG "no partition given for tagging... skipping\n";
234 next;
235 }
236 chomp($part);
[2158]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 }
[1]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 }
[2158]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";
[2154]263 } elsif ($i =~ /^a$/) {
[1]264 $part = <STDIN>;
265 if (not (defined $part)) {
266 print FLOG "no partition given for tagging... skipping\n";
267 next;
268 }
269 chomp($part);
[2158]270
271 # Partition shouldn't be negative or null. Then take the first one.
272 $part = 1 if ($part le 0);
273
[1]274 print FLOG "a $part => set $part boot on\n";
[90]275 system "$parted -s $device set $part boot on\n" if ($fake == 0);
[2158]276 print "command (m for help) send back to fake fdisk for mondorestore\n";
[2154]277 } elsif ($i =~ /^q$/) {
[1]278 print FLOG "q => quit\n";
[2154]279 } else {
[1]280 print FLOG "Unknown command: $i\n";
[2163]281 print "command (m for help) send back to fake fdisk for mondorestore\n";
[1]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
[1857]300 open (FDISK, "$fdisk $fargs 2>/dev/null |") || die "Unable to read from $fdisk";
[1]301 while (<FDISK>) {
302 print;
303 }
304 close(FDISK);
305} else {
306 # Modification mode
[1857]307 open (FDISK, "| $fdisk $fargs 2>/dev/null") || die "Unable to modify through $fdisk";
[1]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;
[90]348my $verbose = shift;
[1]349
350my $un;
[88]351my $endmax;
[1]352my $d;
353my $n;
354
355my %cmt = ( "FAT" => "FAT",
356 "ext2" => "Linux",
357 "ext3" => "Linux",
[2087]358 "ext4" => "Linux",
359 "xfs" => "Linux",
[1565]360 "reiserfs" => "Linux",
[1]361 "linux-swap" => "Linux swap",
[2158]362 "lvm" => "Linux LVM",
[1]363 "fat16" => "fat16",
364 "fat32" => "fat32",
365 "" => "Linux",
366);
367
368my $part;
369my $mstart;
370my $mend;
371my $length;
372my $pid;
373my $cmt;
374format FLOG1 =
[1565]375@<<<<<<<<<<<< @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
[1]376$part, $mstart, $mend, $length, $pid, $cmt
377.
378format FLOG2 =
379@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
380$part,
[1565]381 @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
[1]382 $mstart, $mend, $length, $pid, $cmt
383.
384format STDOUT1 =
[1565]385@<<<<<<<<<<<< @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
[1]386$part, $mstart, $mend, $length, $pid, $cmt
387.
388format STDOUT2 =
389@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
390$part,
[1565]391 @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
[1]392 $mstart, $mend, $length, $pid, $cmt
393.
[1565]394# Device Boot Start End Blocks Id System
395#/dev/hda1 1 77579 39099374+ ee EFI GPT
[1]396
[1565]397
[1]398#
399# Keep Fdisk headers
400#
[88]401# this will return bytes
[90]402$un = get_un ($device,$wpart,$verbose);
403
[88]404$endmax = get_max($device);
[90]405
[88]406# This will return MB
[1565]407get_parted ($device,$start,$end,\%type);
[1]408
409while (($n,$d) = each %type) {
410 # Print infos fdisk like
411 $part = ${device}.$n;
[88]412 # start and end are in cylinder in fdisk format
413 # so return in MB * 1MB / what represents 1 cyl in B
[1801]414 $mstart = sprintf("%d",$$start{$n}*$mega/$un);
[88]415 $mstart = 1 if ($mstart < 1);
[1565]416 $mstart = $endmax if ($mstart > $endmax);
[1801]417 $mend = sprintf("%d",$$end{$n}*$mega/$un - 1);
[88]418 $mend = $endmax if ($mend > $endmax);
[1565]419 $mend = 1 if ($mend < 1);
[88]420 # length is in 1K blocks
421 $length = sprintf("%d",($mend-$mstart+1)*$un/1024);
[1]422 $pid = $pid{$type{$n}};
423 $cmt = $cmt{$type{$n}};
[90]424 #print FLOG "$part - $mstart - $mend - $length\n";
[1]425
[90]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 }
[1]449 } else {
[90]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);
[1]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
[1857]469open (FDISK, "$fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
[1]470while (<FDISK>) {
[88]471 if ($_ =~ /heads/) {
472 chomp;
473 $max = $_;
474 $max =~ s/.* ([0-9]+) cylinders/$1/;
[1]475 }
476}
477close(FDISK);
478print FLOG "get_max returns $max\n";
479return($max);
480}
481
482#
[88]483# Get units from fdisk (cylinder size)
[1]484#
485sub get_un {
486
487my $device = shift;
488my $wpart = shift;
[90]489my $verbose = shift;
[1]490my $un = 0;
491my $foo;
492
[1857]493open (FDISK, "$fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
[1]494while (<FDISK>) {
[90]495 print if (($_ !~ /^\/dev\//) and (not (defined $wpart)) and ($verbose == 1));
[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
[88]507#
508# Parted gives info in MB
[1565]509# (depending on versions - 1.6.25.1 provides suffixes)
[88]510#
[1]511sub get_parted {
512
513my $device = shift;
514my $start = shift;
515my $end = shift;
516my $type = shift;
[1565]517my $void;
[1]518my $d;
519my $n;
[1565]520my $ret;
521my $mode;
522my $size;
[1801]523my $unit;
[1]524
[1565]525open (PARTED, "$parted -v |") || die "Unable to read from $parted";
526$d = <PARTED>;
527print FLOG "$d";
528close(PARTED);
529
[1]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>;
[1565]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 {
[1801]541 # RHEL 3 parted 1.6.3
542 # RHEL 4 parted 1.6.19
[1565]543 $mode=0;
544}
545print FLOG "mode: $mode\n";
[1]546print FLOG "Got from parted: \n";
[1565]547print FLOG "Minor Start End Filesystem\n";
[1]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;
[1565]554 if ($mode == 0) {
555 ($$start{$n},$$end{$n},$$type{$n},$void) = split(/ /,$d);
[1801]556 $unit = 1;
[1565]557 } elsif ($mode == 1) {
558 ($$start{$n},$$end{$n},$size,$$type{$n},$void) = split(/ /,$d);
[1801]559 $unit = $mega;
[1565]560 } else {
561 die "Undefined mode $mode";
562 }
[1]563 $$start{$n} = "" if (not defined $$start{$n});
564 $$end{$n} = "" if (not defined $$end{$n});
565 $$type{$n} = "" if (not defined $$type{$n});
[1565]566 # Handles potential suffixes in latest parted version. Return MB
[1801]567 $ret = decode_Bsuf($$start{$n},$unit);
[1565]568 $$start{$n} = $ret;
[1801]569 $ret = decode_Bsuf($$end{$n},$unit);
[1565]570 $$end{$n} = $ret;
571 print FLOG "$n $$start{$n} $$end{$n} $$type{$n}\n";
[1]572}
573close(PARTED);
574}
575
[1565]576sub decode_Bsuf {
577
578my $size = shift;
579my $unit = shift;
580my $ret = 0;
581
582#print FLOG "decode_Bsuf input: $size / $unit ";
[1837]583if ($size =~ /K[B]*$/i) {
584 $size =~ s/K[B]*$//i;
[1565]585 $size *= 1024;
[1837]586} elsif ($size =~ /M[B]*$/i) {
587 $size =~ s/M[B]*$//i;
[1565]588 $size *= 1048576;
[1837]589} elsif ($size =~ /G[B]*$/i) {
590 $size =~ s/G[B]*$//i;
[1565]591 $size *= 1073741824;
[1837]592} elsif ($size =~ /T[B]*$/i) {
593 $size =~ s/T[B]*$//i;
[1565]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
[1]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
[1857]631open (FDISK, "$fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
[1]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.