]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/Plugin.pm
allow templates using .xz compression
[pve-storage.git] / PVE / Storage / Plugin.pm
index 462d9e1e21d40d66b8545aadfd46669a10b986d8..310a6589adcb54227dd51983136b04a30593e62a 100644 (file)
@@ -105,9 +105,9 @@ PVE::JSONSchema::register_format('pve-storage-server', \&verify_server);
 sub verify_server {
     my ($server, $noerr) = @_;
 
-    # fixme: use better regex ?
-    # IP or DNS name
-    if ($server !~ m/^[[:alnum:]\-\.]+$/) {
+    if (!(PVE::JSONSchema::pve_verify_ip($server, 1) ||
+          PVE::JSONSchema::pve_verify_dns_name($server, 1)))
+    {
        return undef if $noerr;
        die "value does not look like a valid server name or IP address\n";
     }
@@ -312,7 +312,7 @@ sub parse_config {
            $d->{content} = $def->{content}->[1] if !$d->{content};
        }
 
-       if ($type eq 'iscsi' || $type eq 'nfs' || $type eq 'rbd' || $type eq 'sheepdog' || $type eq 'iscsidirect' || $type eq 'nexenta' ) {
+       if ($type eq 'iscsi' || $type eq 'nfs' || $type eq 'rbd' || $type eq 'sheepdog' || $type eq 'iscsidirect' || $type eq 'glusterfs' || $type eq 'zfs' || $type eq 'drbd') {
            $d->{shared} = 1;
        }
     }
@@ -364,13 +364,13 @@ sub parse_volname {
        return ('images', $name, $vmid, undef, undef, $isBase);
     } elsif ($volname =~ m!^iso/([^/]+\.[Ii][Ss][Oo])$!) {
        return ('iso', $1);
-    } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.gz)$!) {
+    } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.[gx]z)$!) {
        return ('vztmpl', $1);
     } elsif ($volname =~ m!^rootdir/(\d+)$!) {
        return ('rootdir', $1, $1);
     } elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo)))$!) {
        my $fn = $1;
-       if ($fn =~ m/^vzdump-(openvz|qemu)-(\d+)-.+/) {
+       if ($fn =~ m/^vzdump-(openvz|lxc|qemu)-(\d+)-.+/) {
            return ('backup', $fn, $2);
        }
        return ('backup', $fn);
@@ -401,7 +401,7 @@ sub get_subdir {
     return "$path/$subdir";
 }
 
-sub path {
+sub filesystem_path {
     my ($class, $scfg, $volname, $storeid) = @_;
 
     my ($vtype, $name, $vmid) = $class->parse_volname($volname);
@@ -415,20 +415,26 @@ sub path {
     return wantarray ? ($path, $vmid, $vtype) : $path;
 }
 
+sub path {
+    my ($class, $scfg, $volname, $storeid) = @_;
+
+    return $class->filesystem_path($scfg, $volname, $storeid);
+}
+
 sub create_base {
     my ($class, $storeid, $scfg, $volname) = @_;
 
     # this only works for file based storage types
     die "storage definintion has no path\n" if !$scfg->{path};
 
-    my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = 
+    my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
        $class->parse_volname($volname);
 
     die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images';
 
     die "create_base not possible with base image\n" if $isBase;
 
-    my $path = $class->path($scfg, $volname);
+    my $path = $class->filesystem_path($scfg, $volname);
 
     my ($size, $format, $used, $parent) = file_size_info($path);
     die "file_size_info on '$volname' failed\n" if !($format && $size);
@@ -442,11 +448,11 @@ sub create_base {
     my $newvolname = $basename ? "$basevmid/$basename/$vmid/$newname" :
        "$vmid/$newname";
 
-    my $newpath = $class->path($scfg, $newvolname);
+    my $newpath = $class->filesystem_path($scfg, $newvolname);
 
     die "file '$newpath' already exists\n" if -f $newpath;
 
-    rename($path, $newpath) || 
+    rename($path, $newpath) ||
        die "rename '$path' to '$newpath' failed - $!\n";
 
     # We try to protect base volume
@@ -456,7 +462,7 @@ sub create_base {
     # also try to set immutable flag
     eval { run_command(['/usr/bin/chattr', '+i', $newpath]); };
     warn $@ if $@;
-    
+
     return $newvolname;
 }
 
@@ -464,10 +470,10 @@ my $find_free_diskname = sub {
     my ($imgdir, $vmid, $fmt) = @_;
 
     my $disk_ids = {};
-    PVE::Tools::dir_glob_foreach($imgdir, 
+    PVE::Tools::dir_glob_foreach($imgdir,
                                 qr!(vm|base)-$vmid-disk-(\d+)\..*!,
                                 sub {
-                                    my ($fn, $type, $disk) = @_; 
+                                    my ($fn, $type, $disk) = @_;
                                     $disk_ids->{$disk} = 1;
                                 });
 
@@ -481,17 +487,19 @@ my $find_free_diskname = sub {
 };
 
 sub clone_image {
-    my ($class, $scfg, $storeid, $volname, $vmid) = @_;
+    my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
 
     # this only works for file based storage types
     die "storage definintion has no path\n" if !$scfg->{path};
 
-    my ($vtype, $basename, $basevmid, undef, undef, $isBase) = 
+    my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
        $class->parse_volname($volname);
 
     die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
 
-    die "clone_image onyl works on base images\n" if !$isBase;
+    die "this storage type does not support clone_image on snapshot\n" if $snap;
+
+    die "clone_image only works on base images\n" if !$isBase;
 
     my $imagedir = $class->get_subdir($scfg, 'images');
     $imagedir .= "/$vmid";
@@ -504,15 +512,15 @@ sub clone_image {
 
     my $newvol = "$basevmid/$basename/$vmid/$name";
 
-    my $path = $class->path($scfg, $newvol);
+    my $path = $class->filesystem_path($scfg, $newvol);
 
-    # Note: we use relative paths, so we need to call chdir before qemu-img  
+    # Note: we use relative paths, so we need to call chdir before qemu-img
     eval {
        local $CWD = $imagedir;
 
-       my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename", 
+       my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
                   '-f', 'qcow2', $path];
-   
+
        run_command($cmd);
     };
     my $err = $@;
@@ -531,7 +539,7 @@ sub alloc_image {
     mkpath $imagedir;
 
     $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name;
+
     my (undef, $tmpfmt) = parse_name_dir($name);
 
     die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
@@ -555,7 +563,7 @@ sub alloc_image {
 sub free_image {
     my ($class, $storeid, $scfg, $volname, $isBase) = @_;
 
-    my $path = $class->path($scfg, $volname);
+    my $path = $class->filesystem_path($scfg, $volname);
 
     if (! -f $path) {
        warn "disk image '$path' does not exists\n";
@@ -611,7 +619,7 @@ sub file_size_info {
 
 sub volume_size_info {
     my ($class, $scfg, $storeid, $volname, $timeout) = @_;
-    my $path = $class->path($scfg, $volname);
+    my $path = $class->filesystem_path($scfg, $volname);
     return file_size_info($path, $timeout);
 
 }
@@ -619,11 +627,11 @@ sub volume_size_info {
 sub volume_resize {
     my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
 
-    die "can't resize this image format" if $volname !~ m/\.(raw|qcow2)$/;
+    die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/;
 
     return 1 if $running;
 
-    my $path = $class->path($scfg, $volname);
+    my $path = $class->filesystem_path($scfg, $volname);
 
     my $cmd = ['/usr/bin/qemu-img', 'resize', $path , $size];
 
@@ -633,13 +641,11 @@ sub volume_resize {
 }
 
 sub volume_snapshot {
-    my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
-
-    die "can't snapshot this image format" if $volname !~ m/\.(qcow2|qed)$/;
+    my ($class, $scfg, $storeid, $volname, $snap) = @_;
 
-    return 1 if $running;
+    die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
 
-    my $path = $class->path($scfg, $volname);
+    my $path = $class->filesystem_path($scfg, $volname);
 
     my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
 
@@ -648,12 +654,18 @@ sub volume_snapshot {
     return undef;
 }
 
+sub volume_rollback_is_possible {
+    my ($class, $scfg, $storeid, $volname, $snap) = @_; 
+
+    return 1; 
+}
+
 sub volume_snapshot_rollback {
     my ($class, $scfg, $storeid, $volname, $snap) = @_;
 
-    die "can't rollback snapshot this image format" if $volname !~ m/\.(qcow2|qed)$/;
+    die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
 
-    my $path = $class->path($scfg, $volname);
+    my $path = $class->filesystem_path($scfg, $volname);
 
     my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
 
@@ -665,11 +677,11 @@ sub volume_snapshot_rollback {
 sub volume_snapshot_delete {
     my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
 
-    die "can't delete snapshot for this image format" if $volname !~ m/\.(qcow2|qed)$/;
+    die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
 
     return 1 if $running;
 
-    my $path = $class->path($scfg, $volname);
+    my $path = $class->filesystem_path($scfg, $volname);
 
     my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
 
@@ -682,17 +694,28 @@ sub volume_has_feature {
     my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
 
     my $features = {
-        snapshot => { current => { qcow2 => 1}, snap => { qcow2 => 1} },
-        clone => { current => {qcow2 => 1, raw => 1, vmdk => 1} },
+       snapshot => { current => { qcow2 => 1}, snap => { qcow2 => 1} },
+       clone => { base => {qcow2 => 1, raw => 1, vmdk => 1} },
+       template => { current => {qcow2 => 1, raw => 1, vmdk => 1} },
+       copy => { base => {qcow2 => 1, raw => 1, vmdk => 1},
+                 current => {qcow2 => 1, raw => 1, vmdk => 1},
+                 snap => {qcow2 => 1} },
     };
 
-    if ($volname =~ m!^(\d+)/(\S+)$!) {
-        my ($vmid, $name) = ($1, $2);
-        my (undef, $format) = parse_name_dir($name);
-       my $snap = $snapname ? 'snap' : 'current';
-       return 1 if defined($features->{$feature}->{$snap}->{$format});
+    my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
+       $class->parse_volname($volname);
+
+    my (undef, $format) = parse_name_dir($name);
 
+    my $key = undef;
+    if($snapname){
+        $key = 'snap';
+    }else{
+        $key =  $isBase ? 'base' : 'current';
     }
+
+    return 1 if defined($features->{$feature}->{$key}->{$format});
+
     return undef;
 }
 
@@ -734,7 +757,7 @@ sub list_images {
 
        push @$res, {
            volid => $volid, format => $format,
-           size => $size, vmid => $owner, used => $used, parent => $parent 
+           size => $size, vmid => $owner, used => $used, parent => $parent
        };
     }
 
@@ -787,7 +810,7 @@ sub deactivate_storage {
 sub activate_volume {
     my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
 
-    my $path = $class->path($scfg, $volname);
+    my $path = $class->filesystem_path($scfg, $volname);
 
     # check is volume exists
     if ($scfg->{path}) {