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, subvol
=> 1 }, 'raw', ],
52 description
=> "Set the NOCOW flag on files."
53 . " Disables data checksumming and causes data errors to be unrecoverable from"
54 . " while allowing direct I/O. Only use this if data does not need to be any more"
55 . " safe than on a single ext4 formatted disk with no underlying raid system.",
64 path
=> { fixed
=> 1 },
65 nodes
=> { optional
=> 1 },
66 shared
=> { optional
=> 1 },
67 disable
=> { optional
=> 1 },
68 maxfiles
=> { optional
=> 1 },
69 content
=> { optional
=> 1 },
70 format
=> { optional
=> 1 },
71 is_mountpoint
=> { optional
=> 1 },
72 nocow
=> { optional
=> 1 },
73 # TODO: The new variant of mkdir with `populate` vs `create`...
77 # Storage implementation
79 # We use the same volume names are directory plugins, but map *raw* disk image file names into a
82 # `vm-VMID-disk-ID.raw`
83 # -> `images/VMID/vm-VMID-disk-ID/disk.raw`
84 # where the `vm-VMID-disk-ID/` subdirectory is a btrfs subvolume
86 # Reuse `DirPlugin`'s `check_config`. This simply checks for invalid paths.
88 my ($self, $sectionId, $config, $create, $skipSchemaCheck) = @_;
89 return PVE
::Storage
::DirPlugin
::check_config
($self, $sectionId, $config, $create, $skipSchemaCheck);
92 sub activate_storage
{
93 my ($class, $storeid, $scfg, $cache) = @_;
94 return PVE
::Storage
::DirPlugin
::activate_storage
($class, $storeid, $scfg, $cache);
98 my ($class, $storeid, $scfg, $cache) = @_;
99 return PVE
::Storage
::DirPlugin
::status
($class, $storeid, $scfg, $cache);
102 # TODO: sub get_volume_notes {}
104 # TODO: sub update_volume_notes {}
106 # croak would not include the caller from within this module
109 my (undef, $f, $n) = caller(1);
110 die "$msg at $f: $n\n";
113 # Given a name (eg. `vm-VMID-disk-ID.raw`), take the part up to the format suffix as the name of
114 # the subdirectory (subvolume).
115 sub raw_name_to_dir
($) {
118 # For the subvolume directory Strip the `.<format>` suffix:
119 if ($raw =~ /^(.*)\.raw$/) {
123 __error
"internal error: bad disk name: $raw";
126 sub raw_file_to_subvol
($) {
129 if ($file =~ m
|^(.*)/disk\
.raw
$|) {
133 __error
"internal error: bad raw path: $file";
136 sub filesystem_path
{
137 my ($class, $scfg, $volname, $snapname) = @_;
139 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
140 $class->parse_volname($volname);
142 my $path = $class->get_subdir($scfg, $vtype);
144 $path .= "/$vmid" if $vtype eq 'images';
146 if ($format eq 'raw') {
147 my $dir = raw_name_to_dir
($name);
149 $dir .= "\@$snapname";
151 $path .= "/$dir/disk.raw";
152 } elsif ($format eq 'subvol') {
155 $path .= "\@$snapname";
161 return wantarray ?
($path, $vmid, $vtype) : $path;
165 my ($class, $cmd, $outfunc) = @_;
169 if (defined($outfunc)) {
171 my $part = &$outfunc(@_);
172 $msg .= $part if defined($part);
175 $func = sub { $msg .= "$_[0]\n" };
177 run_command
(['btrfs', '-q', @$cmd], errmsg
=> 'btrfs error', outfunc
=> $func);
182 sub btrfs_get_subvol_id
{
183 my ($class, $path) = @_;
184 my $info = $class->btrfs_cmd(['subvolume', 'show', '--', $path]);
185 if ($info !~ /^\s*(?:Object|Subvolume) ID:\s*(\d+)$/m) {
186 die "failed to get btrfs subvolume ID from: $info\n";
191 my sub chattr
: prototype($$$) {
192 my ($fh, $mask, $xor) = @_;
194 my $flags = pack('L!', 0);
195 ioctl($fh, FS_IOC_GETFLAGS
, $flags) or die "FS_IOC_GETFLAGS failed - $!\n";
196 $flags = pack('L!', (unpack('L!', $flags) & $mask) ^ $xor);
197 ioctl($fh, FS_IOC_SETFLAGS
, $flags) or die "FS_IOC_SETFLAGS failed - $!\n";
202 my ($class, $storeid, $scfg, $volname) = @_;
204 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
205 $class->parse_volname($volname);
208 $newname =~ s/^vm-/base-/;
210 # If we're not working with a 'raw' file, which is the only thing that's "different" for btrfs,
211 # or a subvolume, we forward to the DirPlugin
212 if ($format ne 'raw' && $format ne 'subvol') {
213 return PVE
::Storage
::DirPlugin
::create_base
(@_);
216 my $path = $class->filesystem_path($scfg, $volname);
217 my $newvolname = $basename ?
"$basevmid/$basename/$vmid/$newname" : "$vmid/$newname";
218 my $newpath = $class->filesystem_path($scfg, $newvolname);
221 my $newsubvol = $newpath;
222 if ($format eq 'raw') {
223 $subvol = raw_file_to_subvol
($subvol);
224 $newsubvol = raw_file_to_subvol
($newsubvol);
227 rename($subvol, $newsubvol)
228 || die "rename '$subvol' to '$newsubvol' failed - $!\n";
229 eval { $class->btrfs_cmd(['property', 'set', $newsubvol, 'ro', 'true']) };
236 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
238 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
239 $class->parse_volname($volname);
241 # If we're not working with a 'raw' file, which is the only thing that's "different" for btrfs,
242 # or a subvolume, we forward to the DirPlugin
243 if ($format ne 'raw' && $format ne 'subvol') {
244 return PVE
::Storage
::DirPlugin
::clone_image
(@_);
247 my $imagedir = $class->get_subdir($scfg, 'images');
248 $imagedir .= "/$vmid";
251 my $path = $class->filesystem_path($scfg, $volname);
252 my $newname = $class->find_free_diskname($storeid, $scfg, $vmid, $format, 1);
254 # For btrfs subvolumes we don't actually need the "link":
255 #my $newvolname = "$basevmid/$basename/$vmid/$newname";
256 my $newvolname = "$vmid/$newname";
257 my $newpath = $class->filesystem_path($scfg, $newvolname);
260 my $newsubvol = $newpath;
261 if ($format eq 'raw') {
262 $subvol = raw_file_to_subvol
($subvol);
263 $newsubvol = raw_file_to_subvol
($newsubvol);
266 $class->btrfs_cmd(['subvolume', 'snapshot', '--', $subvol, $newsubvol]);
272 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
274 if ($fmt ne 'raw' && $fmt ne 'subvol') {
275 return PVE
::Storage
::DirPlugin
::alloc_image
(@_);
280 my $imagedir = $class->get_subdir($scfg, 'images') . "/$vmid";
284 $name = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt, 1) if !$name;
286 my (undef, $tmpfmt) = PVE
::Storage
::Plugin
::parse_name_dir
($name);
288 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
291 # End copy from Plugin.pm
293 my $subvol = "$imagedir/$name";
294 # .raw is not part of the directory name
295 $subvol =~ s/\.raw$//;
297 die "disk image '$subvol' already exists\n" if -e
$subvol;
301 $path = "$subvol/disk.raw";
304 if ($fmt eq 'subvol' && !!$size) {
305 # NOTE: `btrfs send/recv` actually drops quota information so supporting subvolumes with
306 # quotas doesn't play nice with send/recv.
307 die "btrfs quotas are currently not supported, use an unsized subvolume or a raw file\n";
310 $class->btrfs_cmd(['subvolume', 'create', '--', $subvol]);
313 if ($fmt eq 'subvol') {
314 # Nothing to do for now...
316 # This is how we *would* do it:
317 # # Use the subvol's default 0/$id qgroup
319 # # This call should happen at storage creation instead and therefore governed by a
320 # # configuration option!
321 # # $class->btrfs_cmd(['quota', 'enable', $subvol]);
322 # my $id = $class->btrfs_get_subvol_id($subvol);
323 # $class->btrfs_cmd(['qgroup', 'limit', "${size}k", "0/$id", $subvol]);
325 } elsif ($fmt eq 'raw') {
326 sysopen my $fh, $path, O_WRONLY
| O_CREAT
| O_EXCL
327 or die "failed to create raw file '$path' - $!\n";
328 chattr
($fh, ~FS_NOCOW_FL
, FS_NOCOW_FL
) if $scfg->{nocow
};
329 truncate($fh, $size * 1024)
330 or die "failed to set file size for '$path' - $!\n";
333 die "internal format error (format = $fmt)\n";
338 eval { $class->btrfs_cmd(['subvolume', 'delete', '--', $subvol]); };
343 return "$vmid/$name";
346 # Same as btrfsprogs does:
347 my sub path_is_subvolume
: prototype($) {
349 my @stat = stat($path)
350 or die "stat failed on '$path' - $!\n";
351 my ($ino, $mode) = @stat[1, 2];
352 return S_ISDIR
($mode) && $ino == BTRFS_FIRST_FREE_OBJECTID
;
355 my $BTRFS_VOL_REGEX = qr/((?:vm|base|subvol)-\d+-disk-\d+(?:\.subvol)?)(?:\@(\S+))$/;
357 # Calls `$code->($volume, $name, $snapshot)` for each subvol in a directory matching our volume
359 my sub foreach_subvol
: prototype($$) {
360 my ($dir, $code) = @_;
362 dir_glob_foreach
($dir, $BTRFS_VOL_REGEX, sub {
363 my ($volume, $name, $snapshot) = ($1, $2, $3);
364 return if !path_is_subvolume
("$dir/$volume");
365 $code->($volume, $name, $snapshot);
370 my ($class, $storeid, $scfg, $volname, $isBase, $_format) = @_;
372 my (undef, undef, $vmid, undef, undef, undef, $format) =
373 $class->parse_volname($volname);
375 if ($format ne 'subvol' && $format ne 'raw') {
376 return PVE
::Storage
::DirPlugin
::free_image
(@_);
379 my $path = $class->filesystem_path($scfg, $volname);
382 if ($format eq 'raw') {
383 $subvol = raw_file_to_subvol
($path);
386 my $dir = dirname
($subvol);
388 foreach_subvol
($dir, sub {
389 my ($volume, $name, $snapshot) = @_;
390 return if !defined $snapshot;
391 push @snapshot_vols, "$dir/$volume";
394 $class->btrfs_cmd(['subvolume', 'delete', '--', @snapshot_vols, $subvol]);
395 # try to cleanup directory to not clutter storage with empty $vmid dirs if
396 # all images from a guest got deleted
402 # Currently not used because quotas clash with send/recv.
403 # my sub btrfs_subvol_quota {
404 # my ($class, $path) = @_;
405 # my $id = '0/' . $class->btrfs_get_subvol_id($path);
406 # my $search = qr/^\Q$id\E\s+(\d)+\s+\d+\s+(\d+)\s*$/;
408 # $class->btrfs_cmd(['qgroup', 'show', '--raw', '-rf', '--', $path], sub {
409 # return if defined($size);
410 # if ($_[0] =~ $search) {
411 # ($used, $size) = ($1, $2);
414 # if (!defined($size)) {
415 # # syslog should include more information:
416 # syslog('err', "failed to get subvolume size for: $path (id $id)");
417 # # UI should only see the last path component:
419 # die "failed to get subvolume size for $path\n";
421 # return wantarray ? ($used, $size) : $size;
424 sub volume_size_info
{
425 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
427 my $path = $class->filesystem_path($scfg, $volname);
429 my $format = ($class->parse_volname($volname))[6];
431 if ($format eq 'subvol') {
432 my $ctime = (stat($path))[10];
433 my ($used, $size) = (0, 0);
434 #my ($used, $size) = btrfs_subvol_quota($class, $path); # uses wantarray
435 return wantarray ?
($size, 'subvol', $used, undef, $ctime) : 1;
438 return PVE
::Storage
::Plugin
::file_size_info
($path, $timeout);
442 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
444 my $format = ($class->parse_volname($volname))[6];
445 if ($format eq 'subvol') {
446 my $path = $class->filesystem_path($scfg, $volname);
447 my $id = '0/' . $class->btrfs_get_subvol_id($path);
448 $class->btrfs_cmd(['qgroup', 'limit', '--', "${size}k", "0/$id", $path]);
452 return PVE
::Storage
::Plugin
::volume_resize
(@_);
455 sub volume_snapshot
{
456 my ($class, $scfg, $storeid, $volname, $snap) = @_;
458 my ($name, $vmid, $format) = ($class->parse_volname($volname))[1,2,6];
459 if ($format ne 'subvol' && $format ne 'raw') {
460 return PVE
::Storage
::Plugin
::volume_snapshot
(@_);
463 my $path = $class->filesystem_path($scfg, $volname);
464 my $snap_path = $class->filesystem_path($scfg, $volname, $snap);
466 if ($format eq 'raw') {
467 $path = raw_file_to_subvol
($path);
468 $snap_path = raw_file_to_subvol
($snap_path);
471 my $snapshot_dir = $class->get_subdir($scfg, 'images') . "/$vmid";
472 mkpath
$snapshot_dir;
474 $class->btrfs_cmd(['subvolume', 'snapshot', '-r', '--', $path, $snap_path]);
478 sub volume_rollback_is_possible
{
479 my ($class, $scfg, $storeid, $volname, $snap) = @_;
484 sub volume_snapshot_rollback
{
485 my ($class, $scfg, $storeid, $volname, $snap) = @_;
487 my ($name, $format) = ($class->parse_volname($volname))[1,6];
489 if ($format ne 'subvol' && $format ne 'raw') {
490 return PVE
::Storage
::Plugin
::volume_snapshot_rollback
(@_);
493 my $path = $class->filesystem_path($scfg, $volname);
494 my $snap_path = $class->filesystem_path($scfg, $volname, $snap);
496 if ($format eq 'raw') {
497 $path = raw_file_to_subvol
($path);
498 $snap_path = raw_file_to_subvol
($snap_path);
501 # Simple version would be:
504 # on error rename temp back
505 # But for atomicity in case the rename after create-failure *also* fails, we create the new
506 # subvol first, then use RENAME_EXCHANGE,
507 my $tmp_path = "$path.tmp.$$";
508 $class->btrfs_cmd(['subvolume', 'snapshot', '--', $snap_path, $tmp_path]);
509 # The paths are absolute, so pass -1 as file descriptors.
510 my $ok = PVE
::Tools
::renameat2
(-1, $tmp_path, -1, $path, &PVE
::Tools
::RENAME_EXCHANGE
);
512 eval { $class->btrfs_cmd(['subvolume', 'delete', '--', $tmp_path]) };
513 warn "failed to remove '$tmp_path' subvolume: $@" if $@;
516 die "failed to rotate '$tmp_path' into place at '$path' - $!\n";
522 sub volume_snapshot_delete
{
523 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
525 my ($name, $vmid, $format) = ($class->parse_volname($volname))[1,2,6];
527 if ($format ne 'subvol' && $format ne 'raw') {
528 return PVE
::Storage
::Plugin
::volume_snapshot_delete
(@_);
531 my $path = $class->filesystem_path($scfg, $volname, $snap);
533 if ($format eq 'raw') {
534 $path = raw_file_to_subvol
($path);
537 $class->btrfs_cmd(['subvolume', 'delete', '--', $path]);
542 sub volume_has_feature
{
543 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
547 current
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1 },
548 snap
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1 }
551 base
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1, vmdk
=> 1 },
552 current
=> { raw
=> 1 },
553 snap
=> { raw
=> 1 },
555 template
=> { current
=> { qcow2
=> 1, raw
=> 1, vmdk
=> 1, subvol
=> 1 } },
557 base
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1, vmdk
=> 1 },
558 current
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1, vmdk
=> 1 },
559 snap
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1 },
561 sparseinit
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1 },
562 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1 } },
565 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
566 $class->parse_volname($volname);
572 $key = $isBase ?
'base' : 'current';
575 return 1 if defined($features->{$feature}->{$key}->{$format});
581 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
582 my $imagedir = $class->get_subdir($scfg, 'images');
586 # Copied from Plugin.pm, with file_size_info calls adapted:
587 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
588 # different to in Plugin.pm the regex below also excludes '@' as valid file name
589 next if $fn !~ m
@^(/.+/(\d
+)/([^/\
@.]+(?
:\
.(qcow2
|vmdk
|subvol
))?
))$@;
596 next if !$vollist && defined($vmid) && ($owner ne $vmid);
598 my $volid = "$storeid:$owner/$name";
599 my ($size, $format, $used, $parent, $ctime);
603 ($size, $format, $used, $parent, $ctime) = PVE
::Storage
::Plugin
::file_size_info
("$fn/disk.raw");
604 } elsif ($ext eq 'subvol') {
605 ($used, $size) = (0, 0);
606 #($used, $size) = btrfs_subvol_quota($class, $fn);
609 ($size, $format, $used, $parent, $ctime) = PVE
::Storage
::Plugin
::file_size_info
($fn);
611 next if !($format && defined($size));
614 next if ! grep { $_ eq $volid } @$vollist;
618 volid
=> $volid, format
=> $format,
619 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent,
622 $info->{ctime
} = $ctime if $ctime;
630 sub volume_export_formats
{
631 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
633 # We can do whatever `DirPlugin` can do.
634 my @result = PVE
::Storage
::Plugin
::volume_export_formats
(@_);
636 # `btrfs send` only works on snapshots:
637 return @result if !defined $snapshot;
639 # Incremental stream with snapshots is only supported if the snapshots are listed (new api):
640 return @result if defined($base_snapshot) && $with_snapshots && ref($with_snapshots) ne 'ARRAY';
642 # Otherwise we do also support `with_snapshots`.
644 # Finally, `btrfs send` only works on formats where we actually use btrfs subvolumes:
645 my $format = ($class->parse_volname($volname))[6];
646 return @result if $format ne 'raw' && $format ne 'subvol';
648 return ('btrfs', @result);
651 sub volume_import_formats
{
652 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
654 # Same as export-formats, beware the parameter order:
655 return volume_export_formats
(
679 if ($format ne 'btrfs') {
680 return PVE
::Storage
::Plugin
::volume_export
(@_);
683 die "format 'btrfs' only works on snapshots\n"
684 if !defined $snapshot;
686 die "'btrfs' format in incremental mode requires snapshots to be listed explicitly\n"
687 if defined($base_snapshot) && $with_snapshots && ref($with_snapshots) ne 'ARRAY';
689 my $volume_format = ($class->parse_volname($volname))[6];
691 die "btrfs-sending volumes of type $volume_format ('$volname') is not supported\n"
692 if $volume_format ne 'raw' && $volume_format ne 'subvol';
694 my $path = $class->path($scfg, $volname, $storeid);
696 if ($volume_format eq 'raw') {
697 $path = raw_file_to_subvol
($path);
700 my $cmd = ['btrfs', '-q', 'send', '-e'];
701 if ($base_snapshot) {
702 my $base = $class->path($scfg, $volname, $storeid, $base_snapshot);
703 if ($volume_format eq 'raw') {
704 $base = raw_file_to_subvol
($base);
706 push @$cmd, '-p', $base;
709 if (ref($with_snapshots) eq 'ARRAY') {
710 push @$cmd, (map { "$path\@$_" } ($with_snapshots // [])->@*), $path;
712 dir_glob_foreach
(dirname
($path), $BTRFS_VOL_REGEX, sub {
713 push @$cmd, "$path\@$_[2]" if !(defined($snapshot) && $_[2] eq $snapshot);
716 $path .= "\@$snapshot" if defined($snapshot);
719 run_command
($cmd, output
=> '>&'.fileno($fh));
737 if ($format ne 'btrfs') {
738 return PVE
::Storage
::Plugin
::volume_import
(@_);
741 die "format 'btrfs' only works on snapshots\n"
742 if !defined $snapshot;
744 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $volume_format) =
745 $class->parse_volname($volname);
747 die "btrfs-receiving volumes of type $volume_format ('$volname') is not supported\n"
748 if $volume_format ne 'raw' && $volume_format ne 'subvol';
750 if (defined($base_snapshot)) {
751 my $path = $class->path($scfg, $volname, $storeid, $base_snapshot);
752 die "base snapshot '$base_snapshot' not found - no such directory '$path'\n"
753 if !path_is_subvolume
($path);
756 my $destination = $class->filesystem_path($scfg, $volname);
757 if ($volume_format eq 'raw') {
758 $destination = raw_file_to_subvol
($destination);
761 if (!defined($base_snapshot) && -e
$destination) {
762 die "volume $volname already exists\n" if !$allow_rename;
763 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $volume_format, 1);
766 my $imagedir = $class->get_subdir($scfg, $vtype);
767 $imagedir .= "/$vmid" if $vtype eq 'images';
769 my $tmppath = "$imagedir/recv.$vmid.tmp";
770 mkdir($imagedir); # FIXME: if $scfg->{mkdir};
771 if (!mkdir($tmppath)) {
772 die "temp receive directory already exists at '$tmppath', incomplete concurrent import?\n"
774 die "failed to create temporary receive directory at '$tmppath' - $!\n";
777 my $dh = IO
::Dir-
>new($tmppath)
778 or die "failed to open temporary receive directory '$tmppath' - $!\n";
780 run_command
(['btrfs', '-q', 'receive', '-e', '--', $tmppath], input
=> '<&'.fileno($fh));
782 # Analyze the received subvolumes;
783 my ($diskname, $found_snapshot, @snapshots);
785 while (defined(my $entry = $dh->read)) {
786 next if $entry eq '.' || $entry eq '..';
787 next if $entry !~ /^$BTRFS_VOL_REGEX$/;
788 my ($cur_diskname, $cur_snapshot) = ($1, $2);
790 die "send stream included a non-snapshot subvolume\n"
791 if !defined($cur_snapshot);
793 if (!defined($diskname)) {
794 $diskname = $cur_diskname;
796 die "multiple disks contained in stream ('$diskname' vs '$cur_diskname')\n"
797 if $diskname ne $cur_diskname;
800 if ($cur_snapshot eq $snapshot) {
803 push @snapshots, $cur_snapshot;
807 die "send stream did not contain the expected current snapshot '$snapshot'\n"
810 # Rotate the disk into place, first the current state:
811 # Note that read-only subvolumes cannot be moved into different directories, but for the
812 # "current" state we also want a writable copy, so start with that:
813 $class->btrfs_cmd(['property', 'set', "$tmppath/$diskname\@$snapshot", 'ro', 'false']);
814 PVE
::Tools
::renameat2
(
816 "$tmppath/$diskname\@$snapshot",
819 &PVE
::Tools
::RENAME_NOREPLACE
,
820 ) or die "failed to move received snapshot '$tmppath/$diskname\@$snapshot'"
821 . " into place at '$destination' - $!\n";
823 # Now recreate the actual snapshot:
830 "$destination\@$snapshot",
833 # Now go through the remaining snapshots (if any)
834 foreach my $snap (@snapshots) {
835 $class->btrfs_cmd(['property', 'set', "$tmppath/$diskname\@$snap", 'ro', 'false']);
836 PVE
::Tools
::renameat2
(
838 "$tmppath/$diskname\@$snap",
840 "$destination\@$snap",
841 &PVE
::Tools
::RENAME_NOREPLACE
,
842 ) or die "failed to move received snapshot '$tmppath/$diskname\@$snap'"
843 . " into place at '$destination\@$snap' - $!\n";
844 eval { $class->btrfs_cmd(['property', 'set', "$destination\@$snap", 'ro', 'true']) };
845 warn "failed to make $destination\@$snap read-only - $!\n" if $@;
851 # Cleanup all the received snapshots we did not move into place, so we can remove the temp
855 while (defined(my $entry = $dh->read)) {
856 next if $entry eq '.' || $entry eq '..';
857 eval { $class->btrfs_cmd(['subvolume', 'delete', '--', "$tmppath/$entry"]) };
860 $dh->close; undef $dh;
862 if (!rmdir($tmppath)) {
863 warn "failed to remove temporary directory '$tmppath' - $!\n"
868 # clean up if the directory ended up being empty after an error
873 return "$storeid:$volname";