package PVE::Storage;
use strict;
+use warnings;
+
use POSIX;
use IO::Select;
use IO::File;
use PVE::Storage::SheepdogPlugin;
use PVE::Storage::ISCSIDirectPlugin;
use PVE::Storage::NexentaPlugin;
+use PVE::Storage::GlusterfsPlugin;
# load and initialize all plugins
PVE::Storage::DirPlugin->register();
PVE::Storage::SheepdogPlugin->register();
PVE::Storage::ISCSIDirectPlugin->register();
PVE::Storage::NexentaPlugin->register();
+PVE::Storage::GlusterfsPlugin->register();
PVE::Storage::Plugin->init();
my $UDEVADM = '/sbin/udevadm';
my ($cfg, $storeid, $noerr) = @_;
die "no storage id specified\n" if !$storeid;
-
+
my $scfg = $cfg->{ids}->{$storeid};
die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg);
return int($vmid);
}
+sub parse_volname {
+ my ($cfg, $volid) = @_;
+
+ my ($storeid, $volname) = parse_volume_id($volid);
+
+ my $scfg = storage_config($cfg, $storeid);
+
+ my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+ return $plugin->parse_volname($volname);
+}
+
sub parse_volume_id {
my ($volid, $noerr) = @_;
if (my $scfg = $cfg->{ids}->{$sid}) {
my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
- my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
+ my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
$plugin->parse_volname($volname);
return $isBase ? 1 : 0;
} else {
return ('');
}
- # Note: abs_path() return undef if $path doesn not exist
+ # Note: abs_path() return undef if $path doesn not exist
# for example when nfs storage is not mounted
$path = abs_path($path) || $path;
}
} elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
my $name = $1;
- return ('iso', "$sid:iso/$name");
+ return ('iso', "$sid:iso/$name");
} elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
my $name = $1;
return ('vztmpl', "$sid:vztmpl/$name");
return ('rootdir', "$sid:rootdir/$vmid");
} elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
my $name = $1;
- return ('iso', "$sid:backup/$name");
+ return ('iso', "$sid:backup/$name");
}
}
}
sub path {
- my ($cfg, $volid) = @_;
+ my ($cfg, $volid, $snapname) = @_;
my ($storeid, $volname) = parse_volume_id($volid);
my $scfg = storage_config($cfg, $storeid);
my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
- my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid);
+ my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
return wantarray ? ($path, $owner, $vtype) : $path;
}
+sub abs_filesystem_path {
+ my ($cfg, $volid) = @_;
+
+ my $path;
+ if (PVE::Storage::parse_volume_id ($volid, 1)) {
+ PVE::Storage::activate_volumes($cfg, [ $volid ]);
+ $path = PVE::Storage::path($cfg, $volid);
+ } else {
+ if (-f $volid) {
+ my $abspath = abs_path($volid);
+ if ($abspath && $abspath =~ m|^(/.+)$|) {
+ $path = $1; # untaint any path
+ }
+ }
+ }
+
+ die "can't find file '$volid'\n" if !($path && -f $path);
+
+ return $path;
+}
+
sub storage_migrate {
my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
my $dirname = dirname($dst);
if ($tcfg->{shared}) { # we can do a local copy
-
+
run_command(['/bin/mkdir', '-p', $dirname]);
run_command(['/bin/cp', $src, $dst]);
} else {
- run_command(['/usr/bin/ssh', "root\@${target_host}",
+ run_command(['/usr/bin/ssh', "root\@${target_host}",
'/bin/mkdir', '-p', $dirname]);
# we use rsync with --sparse, so we can't use --inplace,
# save space
my ($size, $format) = PVE::Storage::Plugin::file_size_info($src);
if ($format && ($format eq 'raw') && $size) {
- run_command(['/usr/bin/ssh', "root\@${target_host}",
+ run_command(['/usr/bin/ssh', "root\@${target_host}",
'rm', '-f', $dst],
outfunc => sub {});
}
- my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
+ my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
$src, "root\@${target_host}:$dst"];
my $percent = -1;
sub vdisk_clone {
my ($cfg, $volid, $vmid) = @_;
-
+
my ($storeid, $volname) = parse_volume_id($volid);
my $scfg = storage_config($cfg, $storeid);
my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
-
+
activate_storage($cfg, $storeid);
# lock shared storage
my $scfg = storage_config($cfg, $storeid);
my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
-
+
activate_storage($cfg, $storeid);
# lock shared storage
my $scfg = storage_config($cfg, $storeid);
my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
-
+
activate_storage($cfg, $storeid);
my $cleanup_worker;
# lock shared storage
$plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
- my ($vtype, $name, $vmid, undef, undef, $isBase) =
+ my ($vtype, $name, $vmid, undef, undef, $isBase) =
$plugin->parse_volname($volname);
if ($isBase) {
my $vollist = $plugin->list_images($storeid, $scfg);
my $basevmid = undef;
eval{
- (undef, undef, undef, $basename, $basevmid) =
+ (undef, undef, undef, $basename, $basevmid) =
$plugin->parse_volname($tmpvolname);
};
sub template_list {
my ($cfg, $storeid, $tt) = @_;
- die "unknown template type '$tt'\n"
- if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
+ die "unknown template type '$tt'\n"
+ if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
my $ids = $cfg->{ids};
} elsif ($tt eq 'backup') {
next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
-
+
$info = { volid => "$sid:backup/$1", format => $2 };
}
my $scfg = storage_config($cfg, $storeid);
my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
-
+
eval {
$plugin->deactivate_volume($storeid, $scfg, $volname, $cache);
};
if scalar(@errlist);
}
-sub storage_info {
+sub storage_info {
my ($cfg, $content) = @_;
my $ids = $cfg->{ids};
my $type = $ids->{$storeid}->{type};
- $info->{$storeid} = {
+ $info->{$storeid} = {
type => $type,
- total => 0,
- avail => 0,
- used => 0,
+ total => 0,
+ avail => 0,
+ used => 0,
shared => $ids->{$storeid}->{shared} ? 1 : 0,
content => PVE::Storage::Plugin::content_hash_to_string($ids->{$storeid}->{content}),
active => 0,
eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
warn $@ if $@;
next if !$active;
- $info->{$storeid}->{total} = $total;
- $info->{$storeid}->{avail} = $avail;
- $info->{$storeid}->{used} = $used;
+ $info->{$storeid}->{total} = $total;
+ $info->{$storeid}->{avail} = $avail;
+ $info->{$storeid}->{used} = $used;
$info->{$storeid}->{active} = $active;
}
sub resolv_server {
my ($server) = @_;
-
+
my $packed_ip = gethostbyname($server);
if (defined $packed_ip) {
return inet_ntoa($packed_ip);
my $product = file_read_firstline("$devpath/product");
$d->{product} = $product if $product;
-
+
my $manu = file_read_firstline("$devpath/manufacturer");
$d->{manufacturer} = $manu if $manu;