summaryrefslogtreecommitdiffstats
path: root/perl-install/fs/wild_device.pm
diff options
context:
space:
mode:
Diffstat (limited to 'perl-install/fs/wild_device.pm')
-rw-r--r--perl-install/fs/wild_device.pm101
1 files changed, 101 insertions, 0 deletions
diff --git a/perl-install/fs/wild_device.pm b/perl-install/fs/wild_device.pm
new file mode 100644
index 000000000..8fd38b0ce
--- /dev/null
+++ b/perl-install/fs/wild_device.pm
@@ -0,0 +1,101 @@
+package fs::wild_device; # $Id$
+
+use diagnostics;
+use strict;
+
+use common;
+
+
+sub analyze {
+ my ($dev) = @_;
+
+ if ($dev =~ m!^/u?dev/(.*)!) {
+ 'dev', $dev;
+ } elsif ($dev !~ m!^/! && (-e "/dev/$dev" || -e "$::prefix/dev/$dev")) {
+ 'dev', "/dev/$dev";
+ } elsif ($dev =~ /^LABEL=(.*)/) {
+ 'label', $1;
+ } elsif ($dev eq 'none' || $dev eq 'rootfs') {
+ 'virtual';
+ } elsif ($dev =~ m!^(\S+):/\w!) {
+ 'nfs';
+ } elsif ($dev =~ m!^//\w!) {
+ 'smb';
+ } elsif ($dev =~ m!^http://!) {
+ 'dav';
+ }
+}
+
+sub to_subpart {
+ my ($dev) = @_;
+
+ my $part = { device => $dev, faked_device => 1 }; #- default
+
+ if (my ($kind, $val) = analyze($dev)) {
+ if ($kind eq 'label') {
+ $part->{device_LABEL} = $val;
+ } elsif ($kind eq 'dev') {
+ my %part = (faked_device => 0);
+ if (my $rdev = (stat "$::prefix$dev")[6]) {
+ ($part{major}, $part{minor}) = unmakedev($rdev);
+ }
+
+ my $symlink = readlink("$::prefix$dev");
+ $dev =~ s!/u?dev/!!;
+
+ if ($symlink && $symlink =~ m|^[^/]+$|) {
+ $part{device_alias} = $dev;
+ $dev = $symlink;
+ }
+
+ if (my (undef, $part_number) = $dev =~ m!/(disc|part(\d+))$!) {
+ $part{part_number} = $part_number if $part_number;
+ $part{devfs_device} = $dev;
+ } else {
+ my $part_number = devices::part_number(\%part);
+ $part{part_number} = $part_number if $part_number;
+ }
+ $part{device} = $dev;
+ return \%part;
+ }
+ } else {
+ if ($dev =~ m!^/! && -f "$::prefix$dev") {
+ #- it must be a loopback file or directory to bind
+ } else {
+ log::l("part_from_wild_device_name: unknown device $dev");
+ }
+ }
+ $part;
+}
+
+sub from_part {
+ my ($prefix, $part) = @_;
+
+ if ($part->{prefer_device_LABEL}) {
+ 'LABEL=' . $part->{device_LABEL};
+ } elsif ($part->{prefer_devfs_name}) {
+ "/dev/$part->{devfs_device}";
+ } elsif ($part->{device_alias}) {
+ "/dev/$part->{device_alias}";
+ } else {
+ my $faked_device = exists $part->{faked_device} ?
+ $part->{faked_device} :
+ do {
+ #- in case $part has been created without using fs::wild_device::to_subpart()
+ my ($kind) = analyze($part->{device});
+ $kind ? $kind ne 'dev' : $part->{device} =~ m!^/!;
+ };
+ if ($faked_device) {
+ $part->{device};
+ } elsif ($part->{device} =~ m!^/dev/!) {
+ log::l("ERROR: i have a full device $part->{device}, this should not happen. use fs::wild_device::to_subpart() instead of creating bad part data-structures!");
+ $part->{device};
+ } else {
+ my $dev = "/dev/$part->{device}";
+ eval { devices::make("$prefix$dev") };
+ $dev;
+ }
+ }
+}
+
+1;