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,
23 BTRFS_MAGIC
=> 0x9123683e,
26 # Configuration (similar to DirPlugin)
44 { images
=> 1, rootdir
=> 1 },
46 format
=> [ { raw
=> 1, subvol
=> 1 }, 'raw', ],
53 description
=> "Set the NOCOW flag on files."
54 . " Disables data checksumming and causes data errors to be unrecoverable from"
55 . " while allowing direct I/O. Only use this if data does not need to be any more"
56 . " safe than on a single ext4 formatted disk with no underlying raid system.",
65 path
=> { fixed
=> 1 },
66 nodes
=> { optional
=> 1 },
67 shared
=> { optional
=> 1 },
68 disable
=> { optional
=> 1 },
69 maxfiles
=> { optional
=> 1 },
70 content
=> { optional
=> 1 },
71 format
=> { optional
=> 1 },
72 is_mountpoint
=> { optional
=> 1 },
73 nocow
=> { optional
=> 1 },
74 # TODO: The new variant of mkdir with `populate` vs `create`...
78 # Storage implementation
80 # We use the same volume names are directory plugins, but map *raw* disk image file names into a
83 # `vm-VMID-disk-ID.raw`
84 # -> `images/VMID/vm-VMID-disk-ID/disk.raw`
85 # where the `vm-VMID-disk-ID/` subdirectory is a btrfs subvolume
87 # Reuse `DirPlugin`'s `check_config`. This simply checks for invalid paths.
89 my ($self, $sectionId, $config, $create, $skipSchemaCheck) = @_;
90 return PVE
::Storage
::DirPlugin
::check_config
($self, $sectionId, $config, $create, $skipSchemaCheck);
93 my sub getfsmagic
($) {
95 # The field type sizes in `struct statfs` are defined in a rather annoying way, and we only
96 # need the first field, which is a `long` for our supported platforms.
97 # Should be moved to pve-rs, so this can be the problem of the `libc` crate ;-)
98 # Just round up and extract what we need:
99 my $buf = pack('x160');
100 if (0 != syscall(&PVE
::Syscall
::SYS_statfs
, $path, $buf)) {
101 die "statfs on '$path' failed - $!\n";
104 return unpack('L!', $buf);
107 my sub assert_btrfs
($) {
109 die "'$path' is not a btrfs file system\n"
110 if getfsmagic
($path) != BTRFS_MAGIC
;
113 sub activate_storage
{
114 my ($class, $storeid, $scfg, $cache) = @_;
115 PVE
::Storage
::DirPlugin
::activate_storage
($class, $storeid, $scfg, $cache);
116 assert_btrfs
($scfg->{path
});
120 my ($class, $storeid, $scfg, $cache) = @_;
121 return PVE
::Storage
::DirPlugin
::status
($class, $storeid, $scfg, $cache);
124 # TODO: sub get_volume_notes {}
126 # TODO: sub update_volume_notes {}
128 # croak would not include the caller from within this module
131 my (undef, $f, $n) = caller(1);
132 die "$msg at $f: $n\n";
135 # Given a name (eg. `vm-VMID-disk-ID.raw`), take the part up to the format suffix as the name of
136 # the subdirectory (subvolume).
137 sub raw_name_to_dir
($) {
140 # For the subvolume directory Strip the `.<format>` suffix:
141 if ($raw =~ /^(.*)\.raw$/) {
145 __error
"internal error: bad disk name: $raw";
148 sub raw_file_to_subvol
($) {
151 if ($file =~ m
|^(.*)/disk\
.raw
$|) {
155 __error
"internal error: bad raw path: $file";
158 sub filesystem_path
{
159 my ($class, $scfg, $volname, $snapname) = @_;
161 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
162 $class->parse_volname($volname);
164 my $path = $class->get_subdir($scfg, $vtype);
166 $path .= "/$vmid" if $vtype eq 'images';
168 if ($format eq 'raw') {
169 my $dir = raw_name_to_dir
($name);
171 $dir .= "\@$snapname";
173 $path .= "/$dir/disk.raw";
174 } elsif ($format eq 'subvol') {
177 $path .= "\@$snapname";
183 return wantarray ?
($path, $vmid, $vtype) : $path;
187 my ($class, $cmd, $outfunc) = @_;
191 if (defined($outfunc)) {
193 my $part = &$outfunc(@_);
194 $msg .= $part if defined($part);
197 $func = sub { $msg .= "$_[0]\n" };
199 run_command
(['btrfs', '-q', @$cmd], errmsg
=> 'btrfs error', outfunc
=> $func);
204 sub btrfs_get_subvol_id
{
205 my ($class, $path) = @_;
206 my $info = $class->btrfs_cmd(['subvolume', 'show', '--', $path]);
207 if ($info !~ /^\s*(?:Object|Subvolume) ID:\s*(\d+)$/m) {
208 die "failed to get btrfs subvolume ID from: $info\n";
213 my sub chattr
: prototype($$$) {
214 my ($fh, $mask, $xor) = @_;
216 my $flags = pack('L!', 0);
217 ioctl($fh, FS_IOC_GETFLAGS
, $flags) or die "FS_IOC_GETFLAGS failed - $!\n";
218 $flags = pack('L!', (unpack('L!', $flags) & $mask) ^ $xor);
219 ioctl($fh, FS_IOC_SETFLAGS
, $flags) or die "FS_IOC_SETFLAGS failed - $!\n";
224 my ($class, $storeid, $scfg, $volname) = @_;
226 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
227 $class->parse_volname($volname);
230 $newname =~ s/^vm-/base-/;
232 # If we're not working with a 'raw' file, which is the only thing that's "different" for btrfs,
233 # or a subvolume, we forward to the DirPlugin
234 if ($format ne 'raw' && $format ne 'subvol') {
235 return PVE
::Storage
::DirPlugin
::create_base
(@_);
238 my $path = $class->filesystem_path($scfg, $volname);
239 my $newvolname = $basename ?
"$basevmid/$basename/$vmid/$newname" : "$vmid/$newname";
240 my $newpath = $class->filesystem_path($scfg, $newvolname);
243 my $newsubvol = $newpath;
244 if ($format eq 'raw') {
245 $subvol = raw_file_to_subvol
($subvol);
246 $newsubvol = raw_file_to_subvol
($newsubvol);
249 rename($subvol, $newsubvol)
250 || die "rename '$subvol' to '$newsubvol' failed - $!\n";
251 eval { $class->btrfs_cmd(['property', 'set', $newsubvol, 'ro', 'true']) };
258 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
260 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
261 $class->parse_volname($volname);
263 # If we're not working with a 'raw' file, which is the only thing that's "different" for btrfs,
264 # or a subvolume, we forward to the DirPlugin
265 if ($format ne 'raw' && $format ne 'subvol') {
266 return PVE
::Storage
::DirPlugin
::clone_image
(@_);
269 my $imagedir = $class->get_subdir($scfg, 'images');
270 $imagedir .= "/$vmid";
273 my $path = $class->filesystem_path($scfg, $volname);
274 my $newname = $class->find_free_diskname($storeid, $scfg, $vmid, $format, 1);
276 # For btrfs subvolumes we don't actually need the "link":
277 #my $newvolname = "$basevmid/$basename/$vmid/$newname";
278 my $newvolname = "$vmid/$newname";
279 my $newpath = $class->filesystem_path($scfg, $newvolname);
282 my $newsubvol = $newpath;
283 if ($format eq 'raw') {
284 $subvol = raw_file_to_subvol
($subvol);
285 $newsubvol = raw_file_to_subvol
($newsubvol);
288 $class->btrfs_cmd(['subvolume', 'snapshot', '--', $subvol, $newsubvol]);
294 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
296 if ($fmt ne 'raw' && $fmt ne 'subvol') {
297 return PVE
::Storage
::DirPlugin
::alloc_image
(@_);
302 my $imagedir = $class->get_subdir($scfg, 'images') . "/$vmid";
306 $name = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt, 1) if !$name;
308 my (undef, $tmpfmt) = PVE
::Storage
::Plugin
::parse_name_dir
($name);
310 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
313 # End copy from Plugin.pm
315 my $subvol = "$imagedir/$name";
316 # .raw is not part of the directory name
317 $subvol =~ s/\.raw$//;
319 die "disk image '$subvol' already exists\n" if -e
$subvol;
323 $path = "$subvol/disk.raw";
326 if ($fmt eq 'subvol' && !!$size) {
327 # NOTE: `btrfs send/recv` actually drops quota information so supporting subvolumes with
328 # quotas doesn't play nice with send/recv.
329 die "btrfs quotas are currently not supported, use an unsized subvolume or a raw file\n";
332 $class->btrfs_cmd(['subvolume', 'create', '--', $subvol]);
335 if ($fmt eq 'subvol') {
336 # Nothing to do for now...
338 # This is how we *would* do it:
339 # # Use the subvol's default 0/$id qgroup
341 # # This call should happen at storage creation instead and therefore governed by a
342 # # configuration option!
343 # # $class->btrfs_cmd(['quota', 'enable', $subvol]);
344 # my $id = $class->btrfs_get_subvol_id($subvol);
345 # $class->btrfs_cmd(['qgroup', 'limit', "${size}k", "0/$id", $subvol]);
347 } elsif ($fmt eq 'raw') {
348 sysopen my $fh, $path, O_WRONLY
| O_CREAT
| O_EXCL
349 or die "failed to create raw file '$path' - $!\n";
350 chattr
($fh, ~FS_NOCOW_FL
, FS_NOCOW_FL
) if $scfg->{nocow
};
351 truncate($fh, $size * 1024)
352 or die "failed to set file size for '$path' - $!\n";
355 die "internal format error (format = $fmt)\n";
360 eval { $class->btrfs_cmd(['subvolume', 'delete', '--', $subvol]); };
365 return "$vmid/$name";
368 # Same as btrfsprogs does:
369 my sub path_is_subvolume
: prototype($) {
371 my @stat = stat($path)
372 or die "stat failed on '$path' - $!\n";
373 my ($ino, $mode) = @stat[1, 2];
374 return S_ISDIR
($mode) && $ino == BTRFS_FIRST_FREE_OBJECTID
;
377 my $BTRFS_VOL_REGEX = qr/((?:vm|base|subvol)-\d+-disk-\d+(?:\.subvol)?)(?:\@(\S+))$/;
379 # Calls `$code->($volume, $name, $snapshot)` for each subvol in a directory matching our volume
381 my sub foreach_subvol
: prototype($$) {
382 my ($dir, $code) = @_;
384 dir_glob_foreach
($dir, $BTRFS_VOL_REGEX, sub {
385 my ($volume, $name, $snapshot) = ($1, $2, $3);
386 return if !path_is_subvolume
("$dir/$volume");
387 $code->($volume, $name, $snapshot);
392 my ($class, $storeid, $scfg, $volname, $isBase, $_format) = @_;
394 my (undef, undef, $vmid, undef, undef, undef, $format) =
395 $class->parse_volname($volname);
397 if ($format ne 'subvol' && $format ne 'raw') {
398 return PVE
::Storage
::DirPlugin
::free_image
(@_);
401 my $path = $class->filesystem_path($scfg, $volname);
404 if ($format eq 'raw') {
405 $subvol = raw_file_to_subvol
($path);
408 my $dir = dirname
($subvol);
410 foreach_subvol
($dir, sub {
411 my ($volume, $name, $snapshot) = @_;
412 return if !defined $snapshot;
413 push @snapshot_vols, "$dir/$volume";
416 $class->btrfs_cmd(['subvolume', 'delete', '--', @snapshot_vols, $subvol]);
417 # try to cleanup directory to not clutter storage with empty $vmid dirs if
418 # all images from a guest got deleted
424 # Currently not used because quotas clash with send/recv.
425 # my sub btrfs_subvol_quota {
426 # my ($class, $path) = @_;
427 # my $id = '0/' . $class->btrfs_get_subvol_id($path);
428 # my $search = qr/^\Q$id\E\s+(\d)+\s+\d+\s+(\d+)\s*$/;
430 # $class->btrfs_cmd(['qgroup', 'show', '--raw', '-rf', '--', $path], sub {
431 # return if defined($size);
432 # if ($_[0] =~ $search) {
433 # ($used, $size) = ($1, $2);
436 # if (!defined($size)) {
437 # # syslog should include more information:
438 # syslog('err', "failed to get subvolume size for: $path (id $id)");
439 # # UI should only see the last path component:
441 # die "failed to get subvolume size for $path\n";
443 # return wantarray ? ($used, $size) : $size;
446 sub volume_size_info
{
447 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
449 my $path = $class->filesystem_path($scfg, $volname);
451 my $format = ($class->parse_volname($volname))[6];
453 if ($format eq 'subvol') {
454 my $ctime = (stat($path))[10];
455 my ($used, $size) = (0, 0);
456 #my ($used, $size) = btrfs_subvol_quota($class, $path); # uses wantarray
457 return wantarray ?
($size, 'subvol', $used, undef, $ctime) : 1;
460 return PVE
::Storage
::Plugin
::file_size_info
($path, $timeout);
464 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
466 my $format = ($class->parse_volname($volname))[6];
467 if ($format eq 'subvol') {
468 my $path = $class->filesystem_path($scfg, $volname);
469 my $id = '0/' . $class->btrfs_get_subvol_id($path);
470 $class->btrfs_cmd(['qgroup', 'limit', '--', "${size}k", "0/$id", $path]);
474 return PVE
::Storage
::Plugin
::volume_resize
(@_);
477 sub volume_snapshot
{
478 my ($class, $scfg, $storeid, $volname, $snap) = @_;
480 my ($name, $vmid, $format) = ($class->parse_volname($volname))[1,2,6];
481 if ($format ne 'subvol' && $format ne 'raw') {
482 return PVE
::Storage
::Plugin
::volume_snapshot
(@_);
485 my $path = $class->filesystem_path($scfg, $volname);
486 my $snap_path = $class->filesystem_path($scfg, $volname, $snap);
488 if ($format eq 'raw') {
489 $path = raw_file_to_subvol
($path);
490 $snap_path = raw_file_to_subvol
($snap_path);
493 my $snapshot_dir = $class->get_subdir($scfg, 'images') . "/$vmid";
494 mkpath
$snapshot_dir;
496 $class->btrfs_cmd(['subvolume', 'snapshot', '-r', '--', $path, $snap_path]);
500 sub volume_rollback_is_possible
{
501 my ($class, $scfg, $storeid, $volname, $snap) = @_;
506 sub volume_snapshot_rollback
{
507 my ($class, $scfg, $storeid, $volname, $snap) = @_;
509 my ($name, $format) = ($class->parse_volname($volname))[1,6];
511 if ($format ne 'subvol' && $format ne 'raw') {
512 return PVE
::Storage
::Plugin
::volume_snapshot_rollback
(@_);
515 my $path = $class->filesystem_path($scfg, $volname);
516 my $snap_path = $class->filesystem_path($scfg, $volname, $snap);
518 if ($format eq 'raw') {
519 $path = raw_file_to_subvol
($path);
520 $snap_path = raw_file_to_subvol
($snap_path);
523 # Simple version would be:
526 # on error rename temp back
527 # But for atomicity in case the rename after create-failure *also* fails, we create the new
528 # subvol first, then use RENAME_EXCHANGE,
529 my $tmp_path = "$path.tmp.$$";
530 $class->btrfs_cmd(['subvolume', 'snapshot', '--', $snap_path, $tmp_path]);
531 # The paths are absolute, so pass -1 as file descriptors.
532 my $ok = PVE
::Tools
::renameat2
(-1, $tmp_path, -1, $path, &PVE
::Tools
::RENAME_EXCHANGE
);
534 eval { $class->btrfs_cmd(['subvolume', 'delete', '--', $tmp_path]) };
535 warn "failed to remove '$tmp_path' subvolume: $@" if $@;
538 die "failed to rotate '$tmp_path' into place at '$path' - $!\n";
544 sub volume_snapshot_delete
{
545 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
547 my ($name, $vmid, $format) = ($class->parse_volname($volname))[1,2,6];
549 if ($format ne 'subvol' && $format ne 'raw') {
550 return PVE
::Storage
::Plugin
::volume_snapshot_delete
(@_);
553 my $path = $class->filesystem_path($scfg, $volname, $snap);
555 if ($format eq 'raw') {
556 $path = raw_file_to_subvol
($path);
559 $class->btrfs_cmd(['subvolume', 'delete', '--', $path]);
564 sub volume_has_feature
{
565 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
569 current
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1 },
570 snap
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1 }
573 base
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1, vmdk
=> 1 },
574 current
=> { raw
=> 1 },
575 snap
=> { raw
=> 1 },
577 template
=> { current
=> { qcow2
=> 1, raw
=> 1, vmdk
=> 1, subvol
=> 1 } },
579 base
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1, vmdk
=> 1 },
580 current
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1, vmdk
=> 1 },
581 snap
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1 },
583 sparseinit
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1 },
584 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1 } },
587 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
588 $class->parse_volname($volname);
594 $key = $isBase ?
'base' : 'current';
597 return 1 if defined($features->{$feature}->{$key}->{$format});
603 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
604 my $imagedir = $class->get_subdir($scfg, 'images');
608 # Copied from Plugin.pm, with file_size_info calls adapted:
609 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
610 # different to in Plugin.pm the regex below also excludes '@' as valid file name
611 next if $fn !~ m
@^(/.+/(\d
+)/([^/\
@.]+(?
:\
.(qcow2
|vmdk
|subvol
))?
))$@;
618 next if !$vollist && defined($vmid) && ($owner ne $vmid);
620 my $volid = "$storeid:$owner/$name";
621 my ($size, $format, $used, $parent, $ctime);
625 ($size, $format, $used, $parent, $ctime) = PVE
::Storage
::Plugin
::file_size_info
("$fn/disk.raw");
626 } elsif ($ext eq 'subvol') {
627 ($used, $size) = (0, 0);
628 #($used, $size) = btrfs_subvol_quota($class, $fn);
631 ($size, $format, $used, $parent, $ctime) = PVE
::Storage
::Plugin
::file_size_info
($fn);
633 next if !($format && defined($size));
636 next if ! grep { $_ eq $volid } @$vollist;
640 volid
=> $volid, format
=> $format,
641 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent,
644 $info->{ctime
} = $ctime if $ctime;
652 sub volume_export_formats
{
653 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
655 # We can do whatever `DirPlugin` can do.
656 my @result = PVE
::Storage
::Plugin
::volume_export_formats
(@_);
658 # `btrfs send` only works on snapshots:
659 return @result if !defined $snapshot;
661 # Incremental stream with snapshots is only supported if the snapshots are listed (new api):
662 return @result if defined($base_snapshot) && $with_snapshots && ref($with_snapshots) ne 'ARRAY';
664 # Otherwise we do also support `with_snapshots`.
666 # Finally, `btrfs send` only works on formats where we actually use btrfs subvolumes:
667 my $format = ($class->parse_volname($volname))[6];
668 return @result if $format ne 'raw' && $format ne 'subvol';
670 return ('btrfs', @result);
673 sub volume_import_formats
{
674 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
676 # Same as export-formats, beware the parameter order:
677 return volume_export_formats
(
701 if ($format ne 'btrfs') {
702 return PVE
::Storage
::Plugin
::volume_export
(@_);
705 die "format 'btrfs' only works on snapshots\n"
706 if !defined $snapshot;
708 die "'btrfs' format in incremental mode requires snapshots to be listed explicitly\n"
709 if defined($base_snapshot) && $with_snapshots && ref($with_snapshots) ne 'ARRAY';
711 my $volume_format = ($class->parse_volname($volname))[6];
713 die "btrfs-sending volumes of type $volume_format ('$volname') is not supported\n"
714 if $volume_format ne 'raw' && $volume_format ne 'subvol';
716 my $path = $class->path($scfg, $volname, $storeid);
718 if ($volume_format eq 'raw') {
719 $path = raw_file_to_subvol
($path);
722 my $cmd = ['btrfs', '-q', 'send', '-e'];
723 if ($base_snapshot) {
724 my $base = $class->path($scfg, $volname, $storeid, $base_snapshot);
725 if ($volume_format eq 'raw') {
726 $base = raw_file_to_subvol
($base);
728 push @$cmd, '-p', $base;
731 if (ref($with_snapshots) eq 'ARRAY') {
732 push @$cmd, (map { "$path\@$_" } ($with_snapshots // [])->@*), $path;
734 dir_glob_foreach
(dirname
($path), $BTRFS_VOL_REGEX, sub {
735 push @$cmd, "$path\@$_[2]" if !(defined($snapshot) && $_[2] eq $snapshot);
738 $path .= "\@$snapshot" if defined($snapshot);
741 run_command
($cmd, output
=> '>&'.fileno($fh));
759 if ($format ne 'btrfs') {
760 return PVE
::Storage
::Plugin
::volume_import
(@_);
763 die "format 'btrfs' only works on snapshots\n"
764 if !defined $snapshot;
766 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $volume_format) =
767 $class->parse_volname($volname);
769 die "btrfs-receiving volumes of type $volume_format ('$volname') is not supported\n"
770 if $volume_format ne 'raw' && $volume_format ne 'subvol';
772 if (defined($base_snapshot)) {
773 my $path = $class->path($scfg, $volname, $storeid, $base_snapshot);
774 die "base snapshot '$base_snapshot' not found - no such directory '$path'\n"
775 if !path_is_subvolume
($path);
778 my $destination = $class->filesystem_path($scfg, $volname);
779 if ($volume_format eq 'raw') {
780 $destination = raw_file_to_subvol
($destination);
783 if (!defined($base_snapshot) && -e
$destination) {
784 die "volume $volname already exists\n" if !$allow_rename;
785 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $volume_format, 1);
788 my $imagedir = $class->get_subdir($scfg, $vtype);
789 $imagedir .= "/$vmid" if $vtype eq 'images';
791 my $tmppath = "$imagedir/recv.$vmid.tmp";
792 mkdir($imagedir); # FIXME: if $scfg->{mkdir};
793 if (!mkdir($tmppath)) {
794 die "temp receive directory already exists at '$tmppath', incomplete concurrent import?\n"
796 die "failed to create temporary receive directory at '$tmppath' - $!\n";
799 my $dh = IO
::Dir-
>new($tmppath)
800 or die "failed to open temporary receive directory '$tmppath' - $!\n";
802 run_command
(['btrfs', '-q', 'receive', '-e', '--', $tmppath], input
=> '<&'.fileno($fh));
804 # Analyze the received subvolumes;
805 my ($diskname, $found_snapshot, @snapshots);
807 while (defined(my $entry = $dh->read)) {
808 next if $entry eq '.' || $entry eq '..';
809 next if $entry !~ /^$BTRFS_VOL_REGEX$/;
810 my ($cur_diskname, $cur_snapshot) = ($1, $2);
812 die "send stream included a non-snapshot subvolume\n"
813 if !defined($cur_snapshot);
815 if (!defined($diskname)) {
816 $diskname = $cur_diskname;
818 die "multiple disks contained in stream ('$diskname' vs '$cur_diskname')\n"
819 if $diskname ne $cur_diskname;
822 if ($cur_snapshot eq $snapshot) {
825 push @snapshots, $cur_snapshot;
829 die "send stream did not contain the expected current snapshot '$snapshot'\n"
832 # Rotate the disk into place, first the current state:
833 # Note that read-only subvolumes cannot be moved into different directories, but for the
834 # "current" state we also want a writable copy, so start with that:
835 $class->btrfs_cmd(['property', 'set', "$tmppath/$diskname\@$snapshot", 'ro', 'false']);
836 PVE
::Tools
::renameat2
(
838 "$tmppath/$diskname\@$snapshot",
841 &PVE
::Tools
::RENAME_NOREPLACE
,
842 ) or die "failed to move received snapshot '$tmppath/$diskname\@$snapshot'"
843 . " into place at '$destination' - $!\n";
845 # Now recreate the actual snapshot:
852 "$destination\@$snapshot",
855 # Now go through the remaining snapshots (if any)
856 foreach my $snap (@snapshots) {
857 $class->btrfs_cmd(['property', 'set', "$tmppath/$diskname\@$snap", 'ro', 'false']);
858 PVE
::Tools
::renameat2
(
860 "$tmppath/$diskname\@$snap",
862 "$destination\@$snap",
863 &PVE
::Tools
::RENAME_NOREPLACE
,
864 ) or die "failed to move received snapshot '$tmppath/$diskname\@$snap'"
865 . " into place at '$destination\@$snap' - $!\n";
866 eval { $class->btrfs_cmd(['property', 'set', "$destination\@$snap", 'ro', 'true']) };
867 warn "failed to make $destination\@$snap read-only - $!\n" if $@;
873 # Cleanup all the received snapshots we did not move into place, so we can remove the temp
877 while (defined(my $entry = $dh->read)) {
878 next if $entry eq '.' || $entry eq '..';
879 eval { $class->btrfs_cmd(['subvolume', 'delete', '--', "$tmppath/$entry"]) };
882 $dh->close; undef $dh;
884 if (!rmdir($tmppath)) {
885 warn "failed to remove temporary directory '$tmppath' - $!\n"
890 # clean up if the directory ended up being empty after an error
895 return "$storeid:$volname";