Richard W.M. Jones
2009-Oct-01 15:08 UTC
[Libguestfs] [PATCH] inspector: Canonicalize device names (fix RHBZ#526717).
This seems to be the completed fix for: https://bugzilla.redhat.com/show_bug.cgi?id=526717 -- Richard Jones, Emerging Technologies, Red Hat http://et.redhat.com/~rjones Read my programming blog: http://rwmj.wordpress.com Fedora now supports 75 OCaml packages (the OPEN alternative to F#) http://cocan.org/getting_started_with_ocaml_on_red_hat_and_fedora -------------- next part -------------->From c56f348d1f5f1f93ab7258c391e760b960a0db74 Mon Sep 17 00:00:00 2001From: Richard Jones <rjones at trick.home.annexia.org> Date: Thu, 1 Oct 2009 16:06:17 +0100 Subject: [PATCH] inspector: Canonicalize device names (fix RHBZ#526717). Make filesystem device names canonical, so they are /dev/sd* instead of /dev/vd*. --- perl/lib/Sys/Guestfs/Lib.pm | 14 +++++++++++++- 1 files changed, 13 insertions(+), 1 deletions(-) diff --git a/perl/lib/Sys/Guestfs/Lib.pm b/perl/lib/Sys/Guestfs/Lib.pm index a9868e2..068cde5 100644 --- a/perl/lib/Sys/Guestfs/Lib.pm +++ b/perl/lib/Sys/Guestfs/Lib.pm @@ -575,7 +575,19 @@ sub inspect_all_partitions my $g = shift; my $parts = shift; my @parts = @$parts; - return map { $_ => inspect_partition ($g, $_, @_) } @parts; + return map { _canonical_dev ($_) => inspect_partition ($g, $_, @_) } @parts; +} + +# Turn /dev/vd* and /dev/hd* into canonical device names +# (see BLOCK DEVICE NAMING in guestfs(3)). + +sub _canonical_dev +{ + local $_ = shift; + + return "/dev/sd$1" if m{^/dev/hd(\w+)}; + return "/dev/sd$1" if m{^/dev/vd(\w+)}; + return $_; } =head2 inspect_partition -- 1.6.2.5
Jim Meyering
2009-Oct-01 15:26 UTC
[Libguestfs] [PATCH] inspector: Canonicalize device names (fix RHBZ#526717).
Richard W.M. Jones wrote:> This seems to be the completed fix for: > https://bugzilla.redhat.com/show_bug.cgi?id=526717...> Subject: [PATCH] inspector: Canonicalize device names (fix RHBZ#526717). > > Make filesystem device names canonical, so they are /dev/sd* > instead of /dev/vd*. > --- > perl/lib/Sys/Guestfs/Lib.pm | 14 +++++++++++++- > 1 files changed, 13 insertions(+), 1 deletions(-) > > diff --git a/perl/lib/Sys/Guestfs/Lib.pm b/perl/lib/Sys/Guestfs/Lib.pm > index a9868e2..068cde5 100644 > --- a/perl/lib/Sys/Guestfs/Lib.pm > +++ b/perl/lib/Sys/Guestfs/Lib.pm > @@ -575,7 +575,19 @@ sub inspect_all_partitions > my $g = shift; > my $parts = shift; > my @parts = @$parts; > - return map { $_ => inspect_partition ($g, $_, @_) } @parts; > + return map { _canonical_dev ($_) => inspect_partition ($g, $_, @_) } @parts; > +} > + > +# Turn /dev/vd* and /dev/hd* into canonical device names > +# (see BLOCK DEVICE NAMING in guestfs(3)). > + > +sub _canonical_dev > +{ > + local $_ = shift; > + > + return "/dev/sd$1" if m{^/dev/hd(\w+)}; > + return "/dev/sd$1" if m{^/dev/vd(\w+)}; > + return $_; > } > > =head2 inspect_partitionHi Rich, I prefer to define such functions before their first use, and using the prototype notation, so that perl can check the number of arguments in any use: sub _canonical_dev ($) { my ($dev) = @_; $dev =~ m{^/dev/[vh]d(\w+)} and $dev = "/dev/sd$1"; return $dev; } Also, I find that not using $_ (even when declared local) improves maintainability. Finally, you can combine those two matches into one.