X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=PVE%2FStorage%2FZFSPoolPlugin.pm;h=b4fd65f15ccc7e5e405ce6290c3f4c3f68cb4135;hb=a97d3ee49f21a61d3df10d196140c95dde45ec27;hp=6e0845752a002d02f8da50978de4330ba104ce8b;hpb=c4a29df483d404c9be3b03f2fd350848f2f4df21;p=pve-storage.git diff --git a/PVE/Storage/ZFSPoolPlugin.pm b/PVE/Storage/ZFSPoolPlugin.pm index 6e08457..b4fd65f 100644 --- a/PVE/Storage/ZFSPoolPlugin.pm +++ b/PVE/Storage/ZFSPoolPlugin.pm @@ -32,6 +32,10 @@ sub properties { description => "use sparse volumes", type => 'boolean', }, + mountpoint => { + description => "mount point", + type => 'string', format => 'pve-storage-path', + }, }; } @@ -44,6 +48,7 @@ sub options { disable => { optional => 1 }, content => { optional => 1 }, bwlimit => { optional => 1 }, + mountpoint => { optional => 1 }, }; } @@ -53,11 +58,11 @@ sub zfs_parse_size { my ($text) = @_; return 0 if !$text; - + if ($text =~ m/^(\d+(\.\d+)?)([TGMK])?$/) { my ($size, $reminder, $unit) = ($1, $2, $3); - + if ($unit) { if ($unit eq 'K') { $size *= 1024; @@ -75,9 +80,9 @@ sub zfs_parse_size { if ($reminder) { $size = ceil($size); } - + return $size; - + } warn "unable to parse zfs size '$text'\n"; @@ -142,17 +147,39 @@ sub parse_volname { # virtual zfs methods (subclass can overwrite them) +sub on_add_hook { + my ($class, $storeid, $scfg, %param) = @_; + + my $cfg_mountpoint = $scfg->{mountpoint}; + + # ignore failure, pool might currently not be imported + my $mountpoint; + eval { + my $res = $class->zfs_get_properties($scfg, 'mountpoint', $scfg->{pool}, 1); + $mountpoint = PVE::Storage::Plugin::verify_path($res, 1) if defined($res); + }; + + if (defined($cfg_mountpoint)) { + if (defined($mountpoint) && !($cfg_mountpoint =~ m|^\Q$mountpoint\E/?$|)) { + warn "warning for $storeid - mountpoint: $cfg_mountpoint " . + "does not match current mount point: $mountpoint\n"; + } + } else { + $scfg->{mountpoint} = $mountpoint; + } +} + sub path { my ($class, $scfg, $volname, $storeid, $snapname) = @_; my ($vtype, $name, $vmid) = $class->parse_volname($volname); my $path = ''; + my $mountpoint = $scfg->{mountpoint} // "/$scfg->{pool}"; if ($vtype eq "images") { if ($name =~ m/^subvol-/ || $name =~ m/^basevol-/) { - # fixme: we currently assume standard mount point?! - $path = "/$scfg->{pool}/$name"; + $path = "$mountpoint/$name"; } else { $path = "/dev/zvol/$scfg->{pool}/$name"; } @@ -167,70 +194,72 @@ sub path { sub zfs_request { my ($class, $scfg, $timeout, $method, @params) = @_; - my $default_timeout = PVE::RPCEnvironment->is_worker() ? 60*60 : 5; - my $cmd = []; if ($method eq 'zpool_list') { push @$cmd, 'zpool', 'list'; } elsif ($method eq 'zpool_import') { push @$cmd, 'zpool', 'import'; - $default_timeout = 15 if $default_timeout < 15; + $timeout = 15 if !$timeout || $timeout < 15; } else { push @$cmd, 'zfs', $method; } - push @$cmd, @params; - - my $msg = ''; - my $output = sub { - my $line = shift; - $msg .= "$line\n"; - }; + my $msg = ''; + my $output = sub { $msg .= "$_[0]\n" }; - $timeout = $default_timeout if !$timeout; + $timeout = PVE::RPCEnvironment->is_worker() ? 60*60 : 5 if !$timeout; run_command($cmd, errmsg => "zfs error", outfunc => $output, timeout => $timeout); return $msg; } +sub zfs_wait_for_zvol_link { + my ($class, $scfg, $volname, $timeout) = @_; + + my $default_timeout = PVE::RPCEnvironment->is_worker() ? 60*5 : 10; + $timeout = $default_timeout if !defined($timeout); + + my ($devname, undef, undef) = $class->path($scfg, $volname); + + for (my $i = 1; $i <= $timeout; $i++) { + last if -b $devname; + die "timeout: no zvol device link for '$volname' found after $timeout sec found.\n" + if $i == $timeout; + + sleep(1); + } +} + sub alloc_image { my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; my $volname = $name; - + if ($fmt eq 'raw') { die "illegal name '$volname' - should be 'vm-$vmid-*'\n" if $volname && $volname !~ m/^vm-$vmid-/; - $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt) + $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt) if !$volname; $class->zfs_create_zvol($scfg, $volname, $size); - my $devname = "/dev/zvol/$scfg->{pool}/$volname"; - - my $timeout = PVE::RPCEnvironment->is_worker() ? 60*5 : 10; - for (my $i = 1; $i <= $timeout; $i++) { - last if -b $devname; - die "Timeout: no zvol after $timeout sec found.\n" - if $i == $timeout; + $class->zfs_wait_for_zvol_link($scfg, $volname); - sleep(1); - } } elsif ( $fmt eq 'subvol') { die "illegal name '$volname' - should be 'subvol-$vmid-*'\n" if $volname && $volname !~ m/^subvol-$vmid-/; - $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt) + $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt) if !$volname; die "illegal name '$volname' - should be 'subvol-$vmid-*'\n" if $volname !~ m/^subvol-$vmid-/; - $class->zfs_create_subvol($scfg, $volname, $size); - + $class->zfs_create_subvol($scfg, $volname, $size); + } else { die "unsupported format '$fmt'"; } @@ -285,16 +314,22 @@ sub list_images { return $res; } +sub zfs_get_properties { + my ($class, $scfg, $properties, $dataset, $timeout) = @_; + + my $result = $class->zfs_request($scfg, $timeout, 'get', '-o', 'value', + '-Hp', $properties, $dataset); + my @values = split /\n/, $result; + return wantarray ? @values : $values[0]; +} + sub zfs_get_pool_stats { my ($class, $scfg) = @_; my $available = 0; my $used = 0; - my $text = $class->zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp', - 'available,used', $scfg->{pool}); - - my @lines = split /\n/, $text; + my @lines = $class->zfs_get_properties($scfg, 'available,used', $scfg->{pool}); if($lines[0] =~ /^(\d+)$/) { $available = $1; @@ -309,7 +344,12 @@ sub zfs_get_pool_stats { sub zfs_create_zvol { my ($class, $scfg, $zvol, $size) = @_; - + + # always align size to 1M as workaround until + # https://github.com/zfsonlinux/zfs/issues/8541 is solved + my $padding = (1024 - $size % 1024) % 1024; + $size = $size + $padding; + my $cmd = ['create']; push @$cmd, '-s' if $scfg->{sparse}; @@ -325,7 +365,7 @@ sub zfs_create_subvol { my ($class, $scfg, $volname, $size) = @_; my $dataset = "$scfg->{pool}/$volname"; - + my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa', '-o', "refquota=${size}k", $dataset]; @@ -385,16 +425,6 @@ sub zfs_list_zvol { return $list; } -sub zfs_find_free_diskname { - my ($class, $storeid, $scfg, $vmid, $format) = @_; - - my $volumes = $class->zfs_list_zvol($scfg); - my $dat = $volumes->{$scfg->{pool}}; - - my $disk_list = [ keys %$dat ]; - return PVE::Storage::Plugin::get_next_vm_diskname($disk_list, $storeid, $vmid, $format, $scfg); -} - sub zfs_get_latest_snapshot { my ($class, $scfg, $volname) = @_; @@ -441,8 +471,8 @@ sub volume_size_info { $class->parse_volname($volname); my $attr = $format eq 'subvol' ? 'refquota' : 'volsize'; - my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', $attr, "$scfg->{pool}/$vname"); - if ($text =~ /\s$attr\s(\d+)\s/) { + my $value = $class->zfs_get_properties($scfg, $attr, "$scfg->{pool}/$vname"); + if ($value =~ /^(\d+)$/) { return $1; } @@ -475,14 +505,14 @@ sub volume_snapshot_rollback { } sub volume_rollback_is_possible { - my ($class, $scfg, $storeid, $volname, $snap) = @_; - + my ($class, $scfg, $storeid, $volname, $snap) = @_; + my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname); if ($snap ne $recentsnap) { die "can't rollback, more recent snapshots exist\n"; } - return 1; + return 1; } sub volume_snapshot_list { @@ -518,18 +548,24 @@ sub activate_storage { my $pool = $scfg->{pool}; $pool =~ s!/.*$!!; - my @param = ('-o', 'name', '-H', "$pool"); - my $res; - eval { - $res = $class->zfs_request($scfg, undef, 'zpool_list', @param); + my $pool_imported = sub { + my @param = ('-o', 'name', '-H', "$pool"); + my $res = eval { $class->zfs_request($scfg, undef, 'zpool_list', @param) }; + if ($@) { + warn "$@\n"; + return undef; + } + return defined($res) && $res =~ m/$pool/; }; - if ($@ || !defined($res) || $res !~ $pool) { - eval { - @param = ('-d', '/dev/disk/by-id/', "$pool"); - $class->zfs_request($scfg, undef, 'zpool_import', @param); - }; - die "could not activate storage '$storeid', $@\n" if $@; + if (!$pool_imported->()) { + # import can only be done if not yet imported! + my @param = ('-d', '/dev/disk/by-id/', '-o', 'cachefile=none', "$pool"); + eval { $class->zfs_request($scfg, undef, 'zpool_import', @param) }; + if (my $err = $@) { + # just could've raced with another import, so recheck if it is imported + die "could not activate storage '$storeid', $@\n" if !$pool_imported->(); + } } return 1; } @@ -541,6 +577,15 @@ sub deactivate_storage { sub activate_volume { my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; + + return 1 if defined($snapname); + + my (undef, undef, undef, undef, undef, undef, $format) = $class->parse_volname($volname); + + return 1 if $format ne 'raw'; + + $class->zfs_wait_for_zvol_link($scfg, $volname); + return 1; } @@ -559,7 +604,7 @@ sub clone_image { die "clone_image only works on base images\n" if !$isBase; - my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $format); + my $name = $class->find_free_diskname($storeid, $scfg, $vmid, $format); if ($format eq 'subvol') { my $size = $class->zfs_request($scfg, undef, 'list', '-H', '-o', 'refquota', "$scfg->{pool}/$basename"); @@ -609,6 +654,12 @@ sub volume_resize { my $attr = $format eq 'subvol' ? 'refquota' : 'volsize'; + # align size to 1M so we always have a valid multiple of the volume block size + if ($format eq 'raw') { + my $padding = (1024 - $new_size % 1024) % 1024; + $new_size = $new_size + $padding; + } + $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname"); return $new_size; @@ -692,7 +743,7 @@ sub volume_export_formats { } sub volume_import { - my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_; + my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots, $allow_rename) = @_; die "unsupported import stream format for $class: $format\n" if $format ne 'zfs'; @@ -701,15 +752,18 @@ sub volume_import { die "internal error: invalid file handle for volume_import\n" if !defined($fd); - my $dataset = ($class->parse_volname($volname))[1]; + my (undef, $dataset, $vmid) = $class->parse_volname($volname); my $zfspath = "$scfg->{pool}/$dataset"; my $suffix = defined($base_snapshot) ? "\@$base_snapshot" : ''; my $exists = 0 == run_command(['zfs', 'get', '-H', 'name', $zfspath.$suffix], noerr => 1, errfunc => sub {}); if (defined($base_snapshot)) { die "base snapshot '$zfspath\@$base_snapshot' doesn't exist\n" if !$exists; - } else { - die "volume '$zfspath' already exists\n" if $exists; + } elsif ($exists) { + die "volume '$zfspath' already exists\n" if !$allow_rename; + warn "volume '$zfspath' already exists - importing with a different name\n"; + $dataset = $class->find_free_diskname($storeid, $scfg, $vmid, $format); + $zfspath = "$scfg->{pool}/$dataset"; } eval { run_command(['zfs', 'recv', '-F', '--', $zfspath], input => "<&$fd") }; @@ -722,7 +776,7 @@ sub volume_import { die $err; } - return; + return "$storeid:$dataset"; } sub volume_import_formats {