]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage.pm
base find_free_diskname: refactor mapping from disks to volid array
[pve-storage.git] / PVE / Storage.pm
index a5f7fdb6a4f106cb03b25987e82f222e848b9ed8..bb3b8743f296c353630e0266dbe6d83dcbe25eaa 100755 (executable)
@@ -15,10 +15,12 @@ use Socket;
 
 use PVE::Tools qw(run_command file_read_firstline dir_glob_foreach $IPV6RE);
 use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file);
+use PVE::DataCenterConfig;
 use PVE::Exception qw(raise_param_exc);
 use PVE::JSONSchema;
 use PVE::INotify;
 use PVE::RPCEnvironment;
+use PVE::SSHInfo;
 
 use PVE::Storage::Plugin;
 use PVE::Storage::DirPlugin;
@@ -29,7 +31,6 @@ use PVE::Storage::CIFSPlugin;
 use PVE::Storage::ISCSIPlugin;
 use PVE::Storage::RBDPlugin;
 use PVE::Storage::CephFSPlugin;
-use PVE::Storage::SheepdogPlugin;
 use PVE::Storage::ISCSIDirectPlugin;
 use PVE::Storage::GlusterfsPlugin;
 use PVE::Storage::ZFSPoolPlugin;
@@ -37,7 +38,11 @@ use PVE::Storage::ZFSPlugin;
 use PVE::Storage::DRBDPlugin;
 
 # Storage API version. Icrement it on changes in storage API interface.
-use constant APIVER => 2;
+use constant APIVER => 3;
+# Age is the number of versions we're backward compatible with.
+# This is like having 'current=APIVER' and age='APIAGE' in libtool,
+# see https://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html
+use constant APIAGE => 2;
 
 # load standard plugins
 PVE::Storage::DirPlugin->register();
@@ -48,7 +53,6 @@ PVE::Storage::CIFSPlugin->register();
 PVE::Storage::ISCSIPlugin->register();
 PVE::Storage::RBDPlugin->register();
 PVE::Storage::CephFSPlugin->register();
-PVE::Storage::SheepdogPlugin->register();
 PVE::Storage::ISCSIDirectPlugin->register();
 PVE::Storage::GlusterfsPlugin->register();
 PVE::Storage::ZFSPoolPlugin->register();
@@ -65,18 +69,29 @@ if ( -d '/usr/share/perl5/PVE/Storage/Custom' ) {
 
        eval {
            require $file;
+
+           # Check perl interface:
+           die "not derived from PVE::Storage::Plugin\n"
+               if !$modname->isa('PVE::Storage::Plugin');
+           die "does not provide an api() method\n"
+               if !$modname->can('api');
+           # Check storage API version and that file is really storage plugin.
+           my $version = $modname->api();
+           die "implements an API version newer than current ($version > " . APIVER . ")\n"
+               if $version > APIVER;
+           my $min_version = (APIVER - APIAGE);
+           die "API version too old, please update the plugin ($version < $min_version)\n"
+               if $version < $min_version;
+           import $file;
+           $modname->register();
+
+           # If we got this far and the API version is not the same, make some
+           # noise:
+           warn "Plugin \"$modname\" is implementing an older storage API, an upgrade is recommended\n"
+               if $version != APIVER;
        };
        if ($@) {
-           warn $@;
-       # Check storage API version and that file is really storage plugin.
-       } elsif ($modname->isa('PVE::Storage::Plugin') && $modname->can('api') && $modname->api() == APIVER) {
-            eval {
-               import $file;
-               $modname->register();
-            };
-            warn $@ if $@;
-       } else {
-           warn "Error loading storage plugin \"$modname\" because of API version mismatch. Please, update it.\n"
+           warn "Error loading storage plugin \"$modname\": $@";
        }
     });
 }
@@ -86,6 +101,8 @@ PVE::Storage::Plugin->init();
 
 my $UDEVADM = '/sbin/udevadm';
 
+our $iso_extension_re = qr/\.(?:iso|img)/i;
+
 #  PVE::Storage utility functions
 
 sub config {
@@ -384,7 +401,8 @@ sub check_volume_access {
     if ($sid) {
        my ($vtype, undef, $ownervm) = parse_volname($cfg, $volid);
        if ($vtype eq 'iso' || $vtype eq 'vztmpl') {
-           # we simply allow access
+           # require at least read access to storage, (custom) templates/ISOs could be sensitive
+           $rpcenv->check_any($user, "/storage/$sid", ['Datastore.AllocateSpace', 'Datastore.Audit']);
        } elsif (defined($ownervm) && defined($vmid) && ($ownervm == $vmid)) {
            # we are owner - allow access
        } elsif ($vtype eq 'backup' && $ownervm) {
@@ -487,7 +505,7 @@ sub path_to_volume_id {
                    return ('images', $info->{volid});
                }
            }
-       } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
+       } elsif ($path =~ m!^$isodir/([^/]+$iso_extension_re)$!) {
            my $name = $1;
            return ('iso', "$sid:iso/$name");
        } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
@@ -522,8 +540,8 @@ sub abs_filesystem_path {
     my ($cfg, $volid) = @_;
 
     my $path;
-    if (PVE::Storage::parse_volume_id ($volid, 1)) {
-       PVE::Storage::activate_volumes($cfg, [ $volid ]);
+    if (parse_volume_id ($volid, 1)) {
+       activate_volumes($cfg, [ $volid ]);
        $path = PVE::Storage::path($cfg, $volid);
     } else {
        if (-f $volid) {
@@ -557,8 +575,8 @@ sub storage_migrate {
     my $target_ip = $target_sshinfo->{ip};
     my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_sshinfo->{name}'";
 
-    my $ssh = PVE::Cluster::ssh_info_to_command($target_sshinfo);
-    my $ssh_base = PVE::Cluster::ssh_info_to_command_base($target_sshinfo);
+    my $ssh = PVE::SSHInfo::ssh_info_to_command($target_sshinfo);
+    my $ssh_base = PVE::SSHInfo::ssh_info_to_command_base($target_sshinfo);
     local $ENV{RSYNC_RSH} = PVE::Tools::cmd2string($ssh_base);
 
     my @cstream = ([ '/usr/bin/cstream', '-t', $ratelimit_bps ])
@@ -572,21 +590,19 @@ sub storage_migrate {
        }
     }
 
-    my @formats = volume_transfer_formats($cfg, $volid, $volid, $snapshot, $base_snapshot, $with_snapshots);
+    my @formats = volume_transfer_formats($cfg, $volid, $target_volid, $snapshot, $base_snapshot, $with_snapshots);
     die "cannot migrate from storage type '$scfg->{type}' to '$tcfg->{type}'\n" if !@formats;
     my $format = $formats[0];
 
-    my @insecurecmd;
+    my $import_fn = '-'; # let pvesm import read from stdin per default
     if ($insecure) {
-       @insecurecmd = ('pvecm', 'mtunnel', '-run-command', 1);
-       if (my $network = $target_sshinfo->{network}) {
-           push @insecurecmd, '-migration_network', $network;
-       }
+       my $net = $target_sshinfo->{network} // $target_sshinfo->{ip};
+       $import_fn = "tcp://$net";
     }
 
     $with_snapshots = $with_snapshots ? 1 : 0; # sanitize for passing as cli parameter
     my $send = ['pvesm', 'export', $volid, $format, '-', '-with-snapshots', $with_snapshots];
-    my $recv = [@$ssh, @insecurecmd, '--', 'pvesm', 'import', $volid, $format, '-', '-with-snapshots', $with_snapshots];
+    my $recv = [@$ssh, '--', 'pvesm', 'import', $target_volid, $format, $import_fn, '-with-snapshots', $with_snapshots];
     if (defined($snapshot)) {
        push @$send, '-snapshot', $snapshot
     }
@@ -757,74 +773,6 @@ sub vdisk_free {
     $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
 }
 
-#list iso or openvz template ($tt = <iso|vztmpl|backup>)
-sub template_list {
-    my ($cfg, $storeid, $tt) = @_;
-
-    die "unknown template type '$tt'\n"
-       if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
-
-    my $ids = $cfg->{ids};
-
-    storage_check_enabled($cfg, $storeid) if ($storeid);
-
-    my $res = {};
-
-    # query the storage
-
-    foreach my $sid (keys %$ids) {
-       next if $storeid && $storeid ne $sid;
-
-       my $scfg = $ids->{$sid};
-       my $type = $scfg->{type};
-
-       next if !storage_check_enabled($cfg, $sid, undef, 1);
-
-       next if $tt eq 'iso' && !$scfg->{content}->{iso};
-       next if $tt eq 'vztmpl' && !$scfg->{content}->{vztmpl};
-       next if $tt eq 'backup' && !$scfg->{content}->{backup};
-
-       activate_storage($cfg, $sid);
-
-       if ($scfg->{path}) {
-           my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
-
-           my $path = $plugin->get_subdir($scfg, $tt);
-
-           foreach my $fn (<$path/*>) {
-
-               my $info;
-
-               if ($tt eq 'iso') {
-                   next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
-
-                   $info = { volid => "$sid:iso/$1", format => 'iso' };
-
-               } elsif ($tt eq 'vztmpl') {
-                   next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
-
-                   $info = { volid => "$sid:vztmpl/$1", format => "t$2" };
-
-               } 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 };
-               }
-
-               $info->{size} = -s $fn;
-
-               push @{$res->{$sid}}, $info;
-           }
-
-       }
-
-       @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid};
-    }
-
-    return $res;
-}
-
-
 sub vdisk_list {
     my ($cfg, $storeid, $vmid, $vollist) = @_;
 
@@ -869,40 +817,53 @@ sub vdisk_list {
     return $res;
 }
 
+sub template_list {
+    my ($cfg, $storeid, $tt) = @_;
+
+    die "unknown template type '$tt'\n"
+       if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup' || $tt eq 'snippets');
+
+    my $ids = $cfg->{ids};
+
+    storage_check_enabled($cfg, $storeid) if ($storeid);
+
+    my $res = {};
+
+    # query the storage
+    foreach my $sid (keys %$ids) {
+       next if $storeid && $storeid ne $sid;
+
+       my $scfg = $ids->{$sid};
+       my $type = $scfg->{type};
+
+       next if !$scfg->{content}->{$tt};
+
+       next if !storage_check_enabled($cfg, $sid, undef, 1);
+
+       $res->{$sid} = volume_list($cfg, $sid, undef, $tt);
+    }
+
+    return $res;
+}
+
 sub volume_list {
     my ($cfg, $storeid, $vmid, $content) = @_;
 
-    my @ctypes = qw(images vztmpl iso backup);
+    my @ctypes = qw(rootdir images vztmpl iso backup snippets);
 
     my $cts = $content ? [ $content ] : [ @ctypes ];
 
     my $scfg = PVE::Storage::storage_config($cfg, $storeid);
 
-    my $res = [];
-    foreach my $ct (@$cts) {
-       my $data;
-       if ($ct eq 'images') {
-           $data = vdisk_list($cfg, $storeid, $vmid);
-       } elsif ($ct eq 'iso' && !defined($vmid)) {
-           $data = template_list($cfg, $storeid, 'iso');
-       } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
-           $data = template_list ($cfg, $storeid, 'vztmpl');
-       } elsif ($ct eq 'backup') {
-           $data = template_list ($cfg, $storeid, 'backup');
-           foreach my $item (@{$data->{$storeid}}) {
-               if (defined($vmid)) {
-                   @{$data->{$storeid}} = grep { $_->{volid} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
-               }
-           }
-       }
+    $cts = [ grep { defined($scfg->{content}->{$_}) } @$cts ];
 
-       next if !$data || !$data->{$storeid};
+    my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
 
-       foreach my $item (@{$data->{$storeid}}) {
-           $item->{content} = $ct;
-           push @$res, $item;
-       }
-    }
+    activate_storage($cfg, $storeid);
+
+    my $res = $plugin->list_volumes($storeid, $scfg, $vmid, $cts);
+
+    @$res = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @$res;
 
     return $res;
 }
@@ -1214,62 +1175,6 @@ sub resolv_portal {
     raise_param_exc({ portal => "unable to resolve portal address '$portal'" });
 }
 
-# idea is from usbutils package (/usr/bin/usb-devices) script
-sub __scan_usb_device {
-    my ($res, $devpath, $parent, $level) = @_;
-
-    return if ! -d $devpath;
-    return if $level && $devpath !~ m/^.*[-.](\d+)$/;
-    my $port = $level ? int($1 - 1) : 0;
-
-    my $busnum = int(file_read_firstline("$devpath/busnum"));
-    my $devnum = int(file_read_firstline("$devpath/devnum"));
-
-    my $d = {
-       port => $port,
-       level => $level,
-       busnum => $busnum,
-       devnum => $devnum,
-       speed => file_read_firstline("$devpath/speed"),
-       class => hex(file_read_firstline("$devpath/bDeviceClass")),
-       vendid => file_read_firstline("$devpath/idVendor"),
-       prodid => file_read_firstline("$devpath/idProduct"),
-    };
-
-    if ($level) {
-       my $usbpath = $devpath;
-       $usbpath =~ s|^.*/\d+\-||;
-       $d->{usbpath} = $usbpath;
-    }
-
-    my $product = file_read_firstline("$devpath/product");
-    $d->{product} = $product if $product;
-
-    my $manu = file_read_firstline("$devpath/manufacturer");
-    $d->{manufacturer} = $manu if $manu;
-
-    my $serial => file_read_firstline("$devpath/serial");
-    $d->{serial} = $serial if $serial;
-
-    push @$res, $d;
-
-    foreach my $subdev (<$devpath/$busnum-*>) {
-       next if $subdev !~ m|/$busnum-[0-9]+(\.[0-9]+)*$|;
-       __scan_usb_device($res, $subdev, $devnum, $level + 1);
-    }
-
-};
-
-sub scan_usb {
-
-    my $devlist = [];
-
-    foreach my $device (</sys/bus/usb/devices/usb*>) {
-       __scan_usb_device($devlist, $device, 0, 0);
-    }
-
-    return $devlist;
-}
 
 sub scan_iscsi {
     my ($portal_in) = @_;
@@ -1559,7 +1464,7 @@ sub complete_storage_enabled {
 sub complete_content_type {
     my ($cmdname, $pname, $cvalue) = @_;
 
-    return [qw(rootdir images vztmpl iso backup)];
+    return [qw(rootdir images vztmpl iso backup snippets)];
 }
 
 sub complete_volume {
@@ -1631,7 +1536,7 @@ sub get_bandwidth_limit {
     }
 
     # Apply per-storage limits - if there are storages involved.
-    if (@$storage_list) {
+    if (defined($storage_list) && @$storage_list) {
        my $config = config();
 
        # The Datastore.Allocate permission allows us to modify the per-storage
@@ -1642,6 +1547,7 @@ sub get_bandwidth_limit {
 
        my %done;
        foreach my $storage (@$storage_list) {
+           next if !defined($storage);
            # Avoid duplicate checks:
            next if $done{$storage};
            $done{$storage} = 1;