1 package PVE
::Storage
::BTRFSPlugin
;
6 use base
qw(PVE::Storage::Plugin);
8 use Fcntl
qw(S_ISDIR O_WRONLY O_CREAT O_EXCL);
9 use File
::Basename
qw(dirname);
10 use File
::Path
qw(mkpath);
14 use PVE
::Tools
qw(run_command dir_glob_foreach);
16 use PVE
::Storage
::DirPlugin
;
19 BTRFS_FIRST_FREE_OBJECTID
=> 256,
20 FS_NOCOW_FL
=> 0x00800000,
21 FS_IOC_GETFLAGS
=> 0x40086602,
22 FS_IOC_SETFLAGS
=> 0x80086601,
25 # Configuration (similar to DirPlugin)
43 { images
=> 1, rootdir
=> 1 },
45 format
=> [ { raw
=> 1, qcow2
=> 1, vmdk
=> 1, subvol
=> 1 }, 'raw', ],
51 path
=> { fixed
=> 1 },
52 nodes
=> { optional
=> 1 },
53 shared
=> { optional
=> 1 },
54 disable
=> { optional
=> 1 },
55 maxfiles
=> { optional
=> 1 },
56 content
=> { optional
=> 1 },
57 format
=> { optional
=> 1 },
58 is_mountpoint
=> { optional
=> 1 },
59 # TODO: The new variant of mkdir with `populate` vs `create`...
63 # Storage implementation
65 # We use the same volume names are directory plugins, but map *raw* disk image file names into a
68 # `vm-VMID-disk-ID.raw`
69 # -> `images/VMID/vm-VMID-disk-ID/disk.raw`
70 # where the `vm-VMID-disk-ID/` subdirectory is a btrfs subvolume
72 # Reuse `DirPlugin`'s `check_config`. This simply checks for invalid paths.
74 my ($self, $sectionId, $config, $create, $skipSchemaCheck) = @_;
75 return PVE
::Storage
::DirPlugin
::check_config
($self, $sectionId, $config, $create, $skipSchemaCheck);
78 sub activate_storage
{
79 my ($class, $storeid, $scfg, $cache) = @_;
80 return PVE
::Storage
::DirPlugin
::activate_storage
($class, $storeid, $scfg, $cache);
84 my ($class, $storeid, $scfg, $cache) = @_;
85 return PVE
::Storage
::DirPlugin
::status
($class, $storeid, $scfg, $cache);
88 # TODO: sub get_volume_notes {}
90 # TODO: sub update_volume_notes {}
92 # croak would not include the caller from within this module
95 my (undef, $f, $n) = caller(1);
96 die "$msg at $f: $n\n";
99 # Given a name (eg. `vm-VMID-disk-ID.raw`), take the part up to the format suffix as the name of
100 # the subdirectory (subvolume).
101 sub raw_name_to_dir
($) {
104 # For the subvolume directory Strip the `.<format>` suffix:
105 if ($raw =~ /^(.*)\.raw$/) {
109 __error
"internal error: bad disk name: $raw";
112 sub raw_file_to_subvol
($) {
115 if ($file =~ m
|^(.*)/disk\
.raw
$|) {
119 __error
"internal error: bad raw path: $file";
122 sub filesystem_path
{
123 my ($class, $scfg, $volname, $snapname) = @_;
125 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
126 $class->parse_volname($volname);
128 my $path = $class->get_subdir($scfg, $vtype);
130 $path .= "/$vmid" if $vtype eq 'images';
132 if ($format eq 'raw') {
133 my $dir = raw_name_to_dir
($name);
135 $dir .= "\@$snapname";
137 $path .= "/$dir/disk.raw";
138 } elsif ($format eq 'subvol') {
141 $path .= "\@$snapname";
147 return wantarray ?
($path, $vmid, $vtype) : $path;
151 my ($class, $cmd, $outfunc) = @_;
155 if (defined($outfunc)) {
157 my $part = &$outfunc(@_);
158 $msg .= $part if defined($part);
161 $func = sub { $msg .= "$_[0]\n" };
163 run_command
(['btrfs', '-q', @$cmd], errmsg
=> 'btrfs error', outfunc
=> $func);
168 sub btrfs_get_subvol_id
{
169 my ($class, $path) = @_;
170 my $info = $class->btrfs_cmd(['subvolume', 'show', '--', $path]);
171 if ($info !~ /^\s*(?:Object|Subvolume) ID:\s*(\d+)$/m) {
172 die "failed to get btrfs subvolume ID from: $info\n";
177 my sub chattr
: prototype($$$) {
178 my ($fh, $mask, $xor) = @_;
180 my $flags = pack('L!', 0);
181 ioctl($fh, FS_IOC_GETFLAGS
, $flags) or die "FS_IOC_GETFLAGS failed - $!\n";
182 $flags = pack('L!', (unpack('L!', $flags) & $mask) ^ $xor);
183 ioctl($fh, FS_IOC_SETFLAGS
, $flags) or die "FS_IOC_SETFLAGS failed - $!\n";
188 my ($class, $storeid, $scfg, $volname) = @_;
190 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
191 $class->parse_volname($volname);
194 $newname =~ s/^vm-/base-/;
196 # If we're not working with a 'raw' file, which is the only thing that's "different" for btrfs,
197 # or a subvolume, we forward to the DirPlugin
198 if ($format ne 'raw' && $format ne 'subvol') {
199 return PVE
::Storage
::DirPlugin
::create_base
(@_);
202 my $path = $class->filesystem_path($scfg, $volname);
203 my $newvolname = $basename ?
"$basevmid/$basename/$vmid/$newname" : "$vmid/$newname";
204 my $newpath = $class->filesystem_path($scfg, $newvolname);
207 my $newsubvol = $newpath;
208 if ($format eq 'raw') {
209 $subvol = raw_file_to_subvol
($subvol);
210 $newsubvol = raw_file_to_subvol
($newsubvol);
213 rename($subvol, $newsubvol)
214 || die "rename '$subvol' to '$newsubvol' failed - $!\n";
215 eval { $class->btrfs_cmd(['property', 'set', $newsubvol, 'ro', 'true']) };
222 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
224 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
225 $class->parse_volname($volname);
227 # If we're not working with a 'raw' file, which is the only thing that's "different" for btrfs,
228 # or a subvolume, we forward to the DirPlugin
229 if ($format ne 'raw' && $format ne 'subvol') {
230 return PVE
::Storage
::DirPlugin
::clone_image
(@_);
233 my $imagedir = $class->get_subdir($scfg, 'images');
234 $imagedir .= "/$vmid";
237 my $path = $class->filesystem_path($scfg, $volname);
238 my $newname = $class->find_free_diskname($storeid, $scfg, $vmid, $format, 1);
240 # For btrfs subvolumes we don't actually need the "link":
241 #my $newvolname = "$basevmid/$basename/$vmid/$newname";
242 my $newvolname = "$vmid/$newname";
243 my $newpath = $class->filesystem_path($scfg, $newvolname);
246 my $newsubvol = $newpath;
247 if ($format eq 'raw') {
248 $subvol = raw_file_to_subvol
($subvol);
249 $newsubvol = raw_file_to_subvol
($newsubvol);
252 $class->btrfs_cmd(['subvolume', 'snapshot', '--', $subvol, $newsubvol]);
258 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
260 if ($fmt ne 'raw' && $fmt ne 'subvol') {
261 return PVE
::Storage
::DirPlugin
::alloc_image
(@_);
266 my $imagedir = $class->get_subdir($scfg, 'images') . "/$vmid";
270 $name = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt, 1) if !$name;
272 my (undef, $tmpfmt) = PVE
::Storage
::Plugin
::parse_name_dir
($name);
274 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
277 # End copy from Plugin.pm
279 my $subvol = "$imagedir/$name";
280 # .raw is not part of the directory name
281 $subvol =~ s/\.raw$//;
283 die "disk image '$subvol' already exists\n" if -e
$subvol;
287 $path = "$subvol/disk.raw";
290 if ($fmt eq 'subvol' && !!$size) {
291 # NOTE: `btrfs send/recv` actually drops quota information so supporting subvolumes with
292 # quotas doesn't play nice with send/recv.
293 die "btrfs quotas are currently not supported, use an unsized subvolume or a raw file\n";
296 $class->btrfs_cmd(['subvolume', 'create', '--', $subvol]);
299 if ($fmt eq 'subvol') {
300 # Nothing to do for now...
302 # This is how we *would* do it:
303 # # Use the subvol's default 0/$id qgroup
305 # # This call should happen at storage creation instead and therefore governed by a
306 # # configuration option!
307 # # $class->btrfs_cmd(['quota', 'enable', $subvol]);
308 # my $id = $class->btrfs_get_subvol_id($subvol);
309 # $class->btrfs_cmd(['qgroup', 'limit', "${size}k", "0/$id", $subvol]);
311 } elsif ($fmt eq 'raw') {
312 sysopen my $fh, $path, O_WRONLY
| O_CREAT
| O_EXCL
313 or die "failed to create raw file '$path' - $!\n";
314 chattr
($fh, ~FS_NOCOW_FL
, FS_NOCOW_FL
);
315 truncate($fh, $size * 1024)
316 or die "failed to set file size for '$path' - $!\n";
319 die "internal format error (format = $fmt)\n";
324 eval { $class->btrfs_cmd(['subvolume', 'delete', '--', $subvol]); };
329 return "$vmid/$name";
332 # Same as btrfsprogs does:
333 my sub path_is_subvolume
: prototype($) {
335 my @stat = stat($path)
336 or die "stat failed on '$path' - $!\n";
337 my ($ino, $mode) = @stat[1, 2];
338 return S_ISDIR
($mode) && $ino == BTRFS_FIRST_FREE_OBJECTID
;
341 my $BTRFS_VOL_REGEX = qr/((?:vm|base|subvol)-\d+-disk-\d+(?:\.subvol)?)(?:\@(\S+))$/;
343 # Calls `$code->($volume, $name, $snapshot)` for each subvol in a directory matching our volume
345 my sub foreach_subvol
: prototype($$) {
346 my ($dir, $code) = @_;
348 dir_glob_foreach
($dir, $BTRFS_VOL_REGEX, sub {
349 my ($volume, $name, $snapshot) = ($1, $2, $3);
350 return if !path_is_subvolume
("$dir/$volume");
351 $code->($volume, $name, $snapshot);
356 my ($class, $storeid, $scfg, $volname, $isBase, $_format) = @_;
358 my (undef, undef, $vmid, undef, undef, undef, $format) =
359 $class->parse_volname($volname);
361 if ($format ne 'subvol' && $format ne 'raw') {
362 return PVE
::Storage
::DirPlugin
::free_image
(@_);
365 my $path = $class->filesystem_path($scfg, $volname);
368 if ($format eq 'raw') {
369 $subvol = raw_file_to_subvol
($path);
372 my $dir = dirname
($subvol);
374 foreach_subvol
($dir, sub {
375 my ($volume, $name, $snapshot) = @_;
376 return if !defined $snapshot;
377 push @snapshot_vols, "$dir/$volume";
380 $class->btrfs_cmd(['subvolume', 'delete', '--', @snapshot_vols, $subvol]);
381 # try to cleanup directory to not clutter storage with empty $vmid dirs if
382 # all images from a guest got deleted
388 # Currently not used because quotas clash with send/recv.
389 # my sub btrfs_subvol_quota {
390 # my ($class, $path) = @_;
391 # my $id = '0/' . $class->btrfs_get_subvol_id($path);
392 # my $search = qr/^\Q$id\E\s+(\d)+\s+\d+\s+(\d+)\s*$/;
394 # $class->btrfs_cmd(['qgroup', 'show', '--raw', '-rf', '--', $path], sub {
395 # return if defined($size);
396 # if ($_[0] =~ $search) {
397 # ($used, $size) = ($1, $2);
400 # if (!defined($size)) {
401 # # syslog should include more information:
402 # syslog('err', "failed to get subvolume size for: $path (id $id)");
403 # # UI should only see the last path component:
405 # die "failed to get subvolume size for $path\n";
407 # return wantarray ? ($used, $size) : $size;
410 sub volume_size_info
{
411 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
413 my $path = $class->filesystem_path($scfg, $volname);
415 my $format = ($class->parse_volname($volname))[6];
417 if ($format eq 'subvol') {
418 my $ctime = (stat($path))[10];
419 my ($used, $size) = (0, 0);
420 #my ($used, $size) = btrfs_subvol_quota($class, $path); # uses wantarray
421 return wantarray ?
($size, 'subvol', $used, undef, $ctime) : 1;
424 return PVE
::Storage
::Plugin
::file_size_info
($path, $timeout);
428 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
430 my $format = ($class->parse_volname($volname))[6];
431 if ($format eq 'subvol') {
432 my $path = $class->filesystem_path($scfg, $volname);
433 my $id = '0/' . $class->btrfs_get_subvol_id($path);
434 $class->btrfs_cmd(['qgroup', 'limit', '--', "${size}k", "0/$id", $path]);
438 return PVE
::Storage
::Plugin
::volume_resize
(@_);
441 sub volume_snapshot
{
442 my ($class, $scfg, $storeid, $volname, $snap) = @_;
444 my ($name, $vmid, $format) = ($class->parse_volname($volname))[1,2,6];
445 if ($format ne 'subvol' && $format ne 'raw') {
446 return PVE
::Storage
::Plugin
::volume_snapshot
(@_);
449 my $path = $class->filesystem_path($scfg, $volname);
450 my $snap_path = $class->filesystem_path($scfg, $volname, $snap);
452 if ($format eq 'raw') {
453 $path = raw_file_to_subvol
($path);
454 $snap_path = raw_file_to_subvol
($snap_path);
457 my $snapshot_dir = $class->get_subdir($scfg, 'images') . "/$vmid";
458 mkpath
$snapshot_dir;
460 $class->btrfs_cmd(['subvolume', 'snapshot', '-r', '--', $path, $snap_path]);
464 sub volume_rollback_is_possible
{
465 my ($class, $scfg, $storeid, $volname, $snap) = @_;
470 sub volume_snapshot_rollback
{
471 my ($class, $scfg, $storeid, $volname, $snap) = @_;
473 my ($name, $format) = ($class->parse_volname($volname))[1,6];
475 if ($format ne 'subvol' && $format ne 'raw') {
476 return PVE
::Storage
::Plugin
::volume_snapshot_rollback
(@_);
479 my $path = $class->filesystem_path($scfg, $volname);
480 my $snap_path = $class->filesystem_path($scfg, $volname, $snap);
482 if ($format eq 'raw') {
483 $path = raw_file_to_subvol
($path);
484 $snap_path = raw_file_to_subvol
($snap_path);
487 # Simple version would be:
490 # on error rename temp back
491 # But for atomicity in case the rename after create-failure *also* fails, we create the new
492 # subvol first, then use RENAME_EXCHANGE,
493 my $tmp_path = "$path.tmp.$$";
494 $class->btrfs_cmd(['subvolume', 'snapshot', '--', $snap_path, $tmp_path]);
495 # The paths are absolute, so pass -1 as file descriptors.
496 my $ok = PVE
::Tools
::renameat2
(-1, $tmp_path, -1, $path, &PVE
::Tools
::RENAME_EXCHANGE
);
498 eval { $class->btrfs_cmd(['subvolume', 'delete', '--', $tmp_path]) };
499 warn "failed to remove '$tmp_path' subvolume: $@" if $@;
502 die "failed to rotate '$tmp_path' into place at '$path' - $!\n";
508 sub volume_snapshot_delete
{
509 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
511 my ($name, $vmid, $format) = ($class->parse_volname($volname))[1,2,6];
513 if ($format ne 'subvol' && $format ne 'raw') {
514 return PVE
::Storage
::Plugin
::volume_snapshot_delete
(@_);
517 my $path = $class->filesystem_path($scfg, $volname, $snap);
519 if ($format eq 'raw') {
520 $path = raw_file_to_subvol
($path);
523 $class->btrfs_cmd(['subvolume', 'delete', '--', $path]);
528 sub volume_has_feature
{
529 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
533 current
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1 },
534 snap
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1 }
537 base
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1, vmdk
=> 1 },
538 current
=> { raw
=> 1 },
539 snap
=> { raw
=> 1 },
541 template
=> { current
=> { qcow2
=> 1, raw
=> 1, vmdk
=> 1, subvol
=> 1 } },
543 base
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1, vmdk
=> 1 },
544 current
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1, vmdk
=> 1 },
545 snap
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1 },
547 sparseinit
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1 },
548 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1 } },
551 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
552 $class->parse_volname($volname);
558 $key = $isBase ?
'base' : 'current';
561 return 1 if defined($features->{$feature}->{$key}->{$format});
567 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
568 my $imagedir = $class->get_subdir($scfg, 'images');
572 # Copied from Plugin.pm, with file_size_info calls adapted:
573 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
574 # different to in Plugin.pm the regex below also excludes '@' as valid file name
575 next if $fn !~ m
@^(/.+/(\d
+)/([^/\
@.]+(?
:\
.(qcow2
|vmdk
|subvol
))?
))$@;
582 next if !$vollist && defined($vmid) && ($owner ne $vmid);
584 my $volid = "$storeid:$owner/$name";
585 my ($size, $format, $used, $parent, $ctime);
589 ($size, $format, $used, $parent, $ctime) = PVE
::Storage
::Plugin
::file_size_info
("$fn/disk.raw");
590 } elsif ($ext eq 'subvol') {
591 ($used, $size) = (0, 0);
592 #($used, $size) = btrfs_subvol_quota($class, $fn);
595 ($size, $format, $used, $parent, $ctime) = PVE
::Storage
::Plugin
::file_size_info
($fn);
597 next if !($format && defined($size));
600 next if ! grep { $_ eq $volid } @$vollist;
604 volid
=> $volid, format
=> $format,
605 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent,
608 $info->{ctime
} = $ctime if $ctime;
616 sub volume_export_formats
{
617 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
619 # We can do whatever `DirPlugin` can do.
620 my @result = PVE
::Storage
::Plugin
::volume_export_formats
(@_);
622 # `btrfs send` only works on snapshots:
623 return @result if !defined $snapshot;
625 # Incremental stream with snapshots is only supported if the snapshots are listed (new api):
626 return @result if defined($base_snapshot) && $with_snapshots && ref($with_snapshots) ne 'ARRAY';
628 # Otherwise we do also support `with_snapshots`.
630 # Finally, `btrfs send` only works on formats where we actually use btrfs subvolumes:
631 my $format = ($class->parse_volname($volname))[6];
632 return @result if $format ne 'raw' && $format ne 'subvol';
634 return ('btrfs', @result);
637 sub volume_import_formats
{
638 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
640 # Same as export-formats, beware the parameter order:
641 return volume_export_formats
(
665 if ($format ne 'btrfs') {
666 return PVE
::Storage
::Plugin
::volume_export
(@_);
669 die "format 'btrfs' only works on snapshots\n"
670 if !defined $snapshot;
672 die "'btrfs' format in incremental mode requires snapshots to be listed explicitly\n"
673 if defined($base_snapshot) && $with_snapshots && ref($with_snapshots) ne 'ARRAY';
675 my $volume_format = ($class->parse_volname($volname))[6];
677 die "btrfs-sending volumes of type $volume_format ('$volname') is not supported\n"
678 if $volume_format ne 'raw' && $volume_format ne 'subvol';
680 my $path = $class->path($scfg, $volname, $storeid);
682 if ($volume_format eq 'raw') {
683 $path = raw_file_to_subvol
($path);
686 my $cmd = ['btrfs', '-q', 'send', '-e'];
687 if ($base_snapshot) {
688 my $base = $class->path($scfg, $volname, $storeid, $base_snapshot);
689 if ($volume_format eq 'raw') {
690 $base = raw_file_to_subvol
($base);
692 push @$cmd, '-p', $base;
695 if (ref($with_snapshots) eq 'ARRAY') {
696 push @$cmd, (map { "$path\@$_" } ($with_snapshots // [])->@*), $path;
698 dir_glob_foreach
(dirname
($path), $BTRFS_VOL_REGEX, sub {
699 push @$cmd, "$path\@$_[2]" if !(defined($snapshot) && $_[2] eq $snapshot);
702 $path .= "\@$snapshot" if defined($snapshot);
705 run_command
($cmd, output
=> '>&'.fileno($fh));
723 if ($format ne 'btrfs') {
724 return PVE
::Storage
::Plugin
::volume_import
(@_);
727 die "format 'btrfs' only works on snapshots\n"
728 if !defined $snapshot;
730 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $volume_format) =
731 $class->parse_volname($volname);
733 die "btrfs-receiving volumes of type $volume_format ('$volname') is not supported\n"
734 if $volume_format ne 'raw' && $volume_format ne 'subvol';
736 if (defined($base_snapshot)) {
737 my $path = $class->path($scfg, $volname, $storeid, $base_snapshot);
738 die "base snapshot '$base_snapshot' not found - no such directory '$path'\n"
739 if !path_is_subvolume
($path);
742 my $destination = $class->filesystem_path($scfg, $volname);
743 if ($volume_format eq 'raw') {
744 $destination = raw_file_to_subvol
($destination);
747 if (!defined($base_snapshot) && -e
$destination) {
748 die "volume $volname already exists\n" if !$allow_rename;
749 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $volume_format, 1);
752 my $imagedir = $class->get_subdir($scfg, $vtype);
753 $imagedir .= "/$vmid" if $vtype eq 'images';
755 my $tmppath = "$imagedir/recv.$vmid.tmp";
756 mkdir($imagedir); # FIXME: if $scfg->{mkdir};
757 if (!mkdir($tmppath)) {
758 die "temp receive directory already exists at '$tmppath', incomplete concurrent import?\n"
760 die "failed to create temporary receive directory at '$tmppath' - $!\n";
763 my $dh = IO
::Dir-
>new($tmppath)
764 or die "failed to open temporary receive directory '$tmppath' - $!\n";
766 run_command
(['btrfs', '-q', 'receive', '-e', '--', $tmppath], input
=> '<&'.fileno($fh));
768 # Analyze the received subvolumes;
769 my ($diskname, $found_snapshot, @snapshots);
771 while (defined(my $entry = $dh->read)) {
772 next if $entry eq '.' || $entry eq '..';
773 next if $entry !~ /^$BTRFS_VOL_REGEX$/;
774 my ($cur_diskname, $cur_snapshot) = ($1, $2);
776 die "send stream included a non-snapshot subvolume\n"
777 if !defined($cur_snapshot);
779 if (!defined($diskname)) {
780 $diskname = $cur_diskname;
782 die "multiple disks contained in stream ('$diskname' vs '$cur_diskname')\n"
783 if $diskname ne $cur_diskname;
786 if ($cur_snapshot eq $snapshot) {
789 push @snapshots, $cur_snapshot;
793 die "send stream did not contain the expected current snapshot '$snapshot'\n"
796 # Rotate the disk into place, first the current state:
797 # Note that read-only subvolumes cannot be moved into different directories, but for the
798 # "current" state we also want a writable copy, so start with that:
799 $class->btrfs_cmd(['property', 'set', "$tmppath/$diskname\@$snapshot", 'ro', 'false']);
800 PVE
::Tools
::renameat2
(
802 "$tmppath/$diskname\@$snapshot",
805 &PVE
::Tools
::RENAME_NOREPLACE
,
806 ) or die "failed to move received snapshot '$tmppath/$diskname\@$snapshot'"
807 . " into place at '$destination' - $!\n";
809 # Now recreate the actual snapshot:
816 "$destination\@$snapshot",
819 # Now go through the remaining snapshots (if any)
820 foreach my $snap (@snapshots) {
821 $class->btrfs_cmd(['property', 'set', "$tmppath/$diskname\@$snap", 'ro', 'false']);
822 PVE
::Tools
::renameat2
(
824 "$tmppath/$diskname\@$snap",
826 "$destination\@$snap",
827 &PVE
::Tools
::RENAME_NOREPLACE
,
828 ) or die "failed to move received snapshot '$tmppath/$diskname\@$snap'"
829 . " into place at '$destination\@$snap' - $!\n";
830 eval { $class->btrfs_cmd(['property', 'set', "$destination\@$snap", 'ro', 'true']) };
831 warn "failed to make $destination\@$snap read-only - $!\n" if $@;
837 # Cleanup all the received snapshots we did not move into place, so we can remove the temp
841 while (defined(my $entry = $dh->read)) {
842 next if $entry eq '.' || $entry eq '..';
843 eval { $class->btrfs_cmd(['subvolume', 'delete', '--', "$tmppath/$entry"]) };
846 $dh->close; undef $dh;
848 if (!rmdir($tmppath)) {
849 warn "failed to remove temporary directory '$tmppath' - $!\n"
854 # clean up if the directory ended up being empty after an error
859 return "$storeid:$volname";