package PVE::Storage;
use strict;
+use warnings;
+
use POSIX;
use IO::Select;
use IO::File;
use PVE::Storage::NFSPlugin;
use PVE::Storage::ISCSIPlugin;
use PVE::Storage::RBDPlugin;
+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::NFSPlugin->register();
PVE::Storage::ISCSIPlugin->register();
PVE::Storage::RBDPlugin->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 PVE::Storage::Plugin::file_size_info($filename, $timeout);
}
+sub volume_size_info {
+ my ($cfg, $volid, $timeout) = @_;
+
+ my ($storeid, $volname) = parse_volume_id($volid, 1);
+ if ($storeid) {
+ my $scfg = storage_config($cfg, $storeid);
+ my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+ return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
+ } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
+ return file_size_info($volid, $timeout);
+ } else {
+ return 0;
+ }
+}
+
+sub volume_resize {
+ my ($cfg, $volid, $size, $running) = @_;
+
+ my ($storeid, $volname) = parse_volume_id($volid, 1);
+ if ($storeid) {
+ my $scfg = storage_config($cfg, $storeid);
+ my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+ return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
+ } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
+ die "resize device is not possible";
+ } else {
+ die "can't resize";
+ }
+}
+
+sub volume_snapshot {
+ my ($cfg, $volid, $snap, $running) = @_;
+
+ my ($storeid, $volname) = parse_volume_id($volid, 1);
+ if ($storeid) {
+ my $scfg = storage_config($cfg, $storeid);
+ my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+ return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap, $running);
+ } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
+ die "snapshot device is not possible";
+ } else {
+ die "can't snapshot";
+ }
+}
+
+sub volume_snapshot_rollback {
+ my ($cfg, $volid, $snap) = @_;
+
+ my ($storeid, $volname) = parse_volume_id($volid, 1);
+ if ($storeid) {
+ my $scfg = storage_config($cfg, $storeid);
+ my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+ return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
+ } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
+ die "snapshot rollback device is not possible";
+ } else {
+ die "can't snapshot";
+ }
+}
+
+sub volume_snapshot_delete {
+ my ($cfg, $volid, $snap, $running) = @_;
+
+ my ($storeid, $volname) = parse_volume_id($volid, 1);
+ if ($storeid) {
+ my $scfg = storage_config($cfg, $storeid);
+ my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+ return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
+ } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
+ die "snapshot delete device is not possible";
+ } else {
+ die "can't delete snapshot";
+ }
+}
+
+sub volume_has_feature {
+ my ($cfg, $feature, $volid, $snap, $running) = @_;
+
+ my ($storeid, $volname) = parse_volume_id($volid, 1);
+ if ($storeid) {
+ my $scfg = storage_config($cfg, $storeid);
+ my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+ return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running);
+ } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
+ return undef;
+ } else {
+ return undef;
+ }
+}
+
sub get_image_dir {
my ($cfg, $storeid, $vmid) = @_;
return int($vmid);
}
-PVE::JSONSchema::register_format('pve-volume-id', \&parse_volume_id);
+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 ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
- return wantarray ? ($1, $2) : $1;
+ return PVE::Storage::Plugin::parse_volume_id($volid, $noerr);
+}
+
+sub volume_is_base {
+ my ($cfg, $volid) = @_;
+
+ my ($sid, $volname) = parse_volume_id($volid, 1);
+ return 0 if !$sid;
+
+ if (my $scfg = $cfg->{ids}->{$sid}) {
+ my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+ my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
+ $plugin->parse_volname($volname);
+ return $isBase ? 1 : 0;
+ } else {
+ # stale volid with undefined storage - so we can just guess
+ if ($volid =~ m/base-/) {
+ return 1;
+ }
}
- return undef if $noerr;
- die "unable to parse volume ID '$volid'\n";
+
+ return 0;
}
# try to map a filesystem path to a volume identifier
my ($sid, $volname) = parse_volume_id($path, 1);
if ($sid) {
if (my $scfg = $ids->{$sid}) {
- if (my $path = $scfg->{path}) {
+ if ($scfg->{path}) {
my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
return ($vtype, $path);
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;
if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
my $vmid = $1;
my $name = $2;
- return ('images', "$sid:$vmid/$name");
+
+ my $vollist = $plugin->list_images($sid, $scfg, $vmid);
+ foreach my $info (@$vollist) {
+ my ($storeid, $volname) = parse_volume_id($info->{volid});
+ my $volpath = $plugin->path($scfg, $volname, $storeid);
+ if ($volpath eq $path) {
+ return ('images', $info->{volid});
+ }
+ }
} 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");
} elsif ($path =~ m!^$privatedir/(\d+)$!) {
my $vmid = $1;
return ('rootdir', "$sid:rootdir/$vmid");
- } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz))$!) {
+ } 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 ($storeid, $volname) = parse_volume_id ($volid);
+ my ($storeid, $volname) = parse_volume_id($volid);
$target_volname = $volname if !$target_volname;
- my $scfg = storage_config ($cfg, $storeid);
+ my $scfg = storage_config($cfg, $storeid);
# no need to migrate shared content
return if $storeid eq $target_storeid && $scfg->{shared};
- my $tcfg = storage_config ($cfg, $target_storeid);
+ my $tcfg = storage_config($cfg, $target_storeid);
my $target_volid = "${target_storeid}:${target_volname}";
my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
- # blowfish is a fast block cipher, much faster then 3des
- my $sshoptions = "-c blowfish -o 'BatchMode=yes'";
+ my $sshoptions = "-o 'BatchMode=yes'";
my $ssh = "/usr/bin/ssh $sshoptions";
local $ENV{RSYNC_RSH} = $ssh;
my $src_plugin = PVE::Storage::Plugin->lookup($scfg->{type});
my $dst_plugin = PVE::Storage::Plugin->lookup($tcfg->{type});
- my $src = $src_plugin->path($scfg, $volid);
- my $dst = $dst_plugin->path($tcfg, $target_volid);
+ my $src = $src_plugin->path($scfg, $volname, $storeid);
+ my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
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
+ return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
+ my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid);
+ return "$storeid:$volname";
+ });
+}
+
+sub vdisk_create_base {
+ my ($cfg, $volid) = @_;
+
+ 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
+ return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
+ my $volname = $plugin->create_base($storeid, $scfg, $volname);
+ return "$storeid:$volname";
+ });
+}
+
sub vdisk_alloc {
my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
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 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname);
+
+ my ($vtype, $name, $vmid, undef, undef, $isBase) =
+ $plugin->parse_volname($volname);
+ if ($isBase) {
+ my $vollist = $plugin->list_images($storeid, $scfg);
+ foreach my $info (@$vollist) {
+ my (undef, $tmpvolname) = parse_volume_id($info->{volid});
+ my $basename = undef;
+ my $basevmid = undef;
+
+ eval{
+ (undef, undef, undef, $basename, $basevmid) =
+ $plugin->parse_volname($tmpvolname);
+ };
+
+ if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
+ die "base volume '$volname' is still in use " .
+ "(use by '$tmpvolname')\n";
+ }
+ }
+ }
+ my $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase);
});
return if !$cleanup_worker;
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};
$info = { volid => "$sid:vztmpl/$1", format => 'tgz' };
} elsif ($tt eq 'backup') {
- next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz))$!;
-
+ next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
+
$info = { volid => "$sid:backup/$1", format => $2 };
}
return $res;
}
+
sub vdisk_list {
my ($cfg, $storeid, $vmid, $vollist) = @_;
return $seqnum;
}
-sub __activate_storage_full {
+sub activate_storage {
my ($cfg, $storeid, $cache) = @_;
+ $cache = {} if !$cache;
+
my $scfg = storage_check_enabled($cfg, $storeid);
return if $cache->{activated}->{$storeid};
if ($scfg->{base}) {
my ($baseid, undef) = parse_volume_id ($scfg->{base});
- __activate_storage_full ($cfg, $baseid, $cache);
+ activate_storage($cfg, $baseid, $cache);
+ }
+
+ if (!$plugin->check_connection($storeid, $scfg)) {
+ die "storage '$storeid' is not online\n";
}
$plugin->activate_storage($storeid, $scfg, $cache);
$cache = {} if !$cache;
foreach my $storeid (@$storeid_list) {
- __activate_storage_full ($cfg, $storeid, $cache);
+ activate_storage($cfg, $storeid, $cache);
}
}
-sub activate_storage {
- my ($cfg, $storeid) = @_;
-
- my $cache = {};
-
- __activate_storage_full ($cfg, $storeid, $cache);
-}
-
-
sub deactivate_storage {
my ($cfg, $storeid) = @_;
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,
my $cache = {};
- eval { activate_storage_list($cfg, $slist, $cache); };
-
foreach my $storeid (keys %$ids) {
my $scfg = $ids->{$storeid};
next if !$info->{$storeid};
+ eval { activate_storage($cfg, $storeid, $cache); };
+ if (my $err = $@) {
+ warn $err;
+ next;
+ }
+
my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
- my ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache);
+ my ($total, $avail, $used, $active);
+ 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;