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 'prune-backups'=> { optional
=> 1 },
71 content
=> { optional
=> 1 },
72 format
=> { optional
=> 1 },
73 is_mountpoint
=> { optional
=> 1 },
74 nocow
=> { optional
=> 1 },
75 mkdir => { optional
=> 1 },
76 # TODO: The new variant of mkdir with `populate` vs `create`...
80 # Storage implementation
82 # We use the same volume names are directory plugins, but map *raw* disk image file names into a
85 # `vm-VMID-disk-ID.raw`
86 # -> `images/VMID/vm-VMID-disk-ID/disk.raw`
87 # where the `vm-VMID-disk-ID/` subdirectory is a btrfs subvolume
89 # Reuse `DirPlugin`'s `check_config`. This simply checks for invalid paths.
91 my ($self, $sectionId, $config, $create, $skipSchemaCheck) = @_;
92 return PVE
::Storage
::DirPlugin
::check_config
($self, $sectionId, $config, $create, $skipSchemaCheck);
95 my sub getfsmagic
($) {
97 # The field type sizes in `struct statfs` are defined in a rather annoying way, and we only
98 # need the first field, which is a `long` for our supported platforms.
99 # Should be moved to pve-rs, so this can be the problem of the `libc` crate ;-)
100 # Just round up and extract what we need:
101 my $buf = pack('x160');
102 if (0 != syscall(&PVE
::Syscall
::SYS_statfs
, $path, $buf)) {
103 die "statfs on '$path' failed - $!\n";
106 return unpack('L!', $buf);
109 my sub assert_btrfs
($) {
111 die "'$path' is not a btrfs file system\n"
112 if getfsmagic
($path) != BTRFS_MAGIC
;
115 sub activate_storage
{
116 my ($class, $storeid, $scfg, $cache) = @_;
118 my $path = $scfg->{path
};
119 if (!defined($scfg->{mkdir}) || $scfg->{mkdir}) {
123 my $mp = PVE
::Storage
::DirPlugin
::parse_is_mountpoint
($scfg);
124 if (defined($mp) && !PVE
::Storage
::DirPlugin
::path_is_mounted
($mp, $cache->{mountdata
})) {
125 die "unable to activate storage '$storeid' - directory is expected to be a mount point but"
126 ." is not mounted: '$mp'\n";
129 assert_btrfs
($path); # only assert this stuff now, ensures $path is there and better UX
131 $class->SUPER::activate_storage
($storeid, $scfg, $cache);
135 my ($class, $storeid, $scfg, $cache) = @_;
136 return PVE
::Storage
::DirPlugin
::status
($class, $storeid, $scfg, $cache);
139 # TODO: sub get_volume_notes {}
141 # TODO: sub update_volume_notes {}
143 # croak would not include the caller from within this module
146 my (undef, $f, $n) = caller(1);
147 die "$msg at $f: $n\n";
150 # Given a name (eg. `vm-VMID-disk-ID.raw`), take the part up to the format suffix as the name of
151 # the subdirectory (subvolume).
152 sub raw_name_to_dir
($) {
155 # For the subvolume directory Strip the `.<format>` suffix:
156 if ($raw =~ /^(.*)\.raw$/) {
160 __error
"internal error: bad disk name: $raw";
163 sub raw_file_to_subvol
($) {
166 if ($file =~ m
|^(.*)/disk\
.raw
$|) {
170 __error
"internal error: bad raw path: $file";
173 sub filesystem_path
{
174 my ($class, $scfg, $volname, $snapname) = @_;
176 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
177 $class->parse_volname($volname);
179 my $path = $class->get_subdir($scfg, $vtype);
181 $path .= "/$vmid" if $vtype eq 'images';
183 if (defined($format) && $format eq 'raw') {
184 my $dir = raw_name_to_dir
($name);
186 $dir .= "\@$snapname";
188 $path .= "/$dir/disk.raw";
189 } elsif (defined($format) && $format eq 'subvol') {
192 $path .= "\@$snapname";
198 return wantarray ?
($path, $vmid, $vtype) : $path;
202 my ($class, $cmd, $outfunc) = @_;
206 if (defined($outfunc)) {
208 my $part = &$outfunc(@_);
209 $msg .= $part if defined($part);
212 $func = sub { $msg .= "$_[0]\n" };
214 run_command
(['btrfs', '-q', @$cmd], errmsg
=> 'btrfs error', outfunc
=> $func);
219 sub btrfs_get_subvol_id
{
220 my ($class, $path) = @_;
221 my $info = $class->btrfs_cmd(['subvolume', 'show', '--', $path]);
222 if ($info !~ /^\s*(?:Object|Subvolume) ID:\s*(\d+)$/m) {
223 die "failed to get btrfs subvolume ID from: $info\n";
228 my sub chattr
: prototype($$$) {
229 my ($fh, $mask, $xor) = @_;
231 my $flags = pack('L!', 0);
232 ioctl($fh, FS_IOC_GETFLAGS
, $flags) or die "FS_IOC_GETFLAGS failed - $!\n";
233 $flags = pack('L!', (unpack('L!', $flags) & $mask) ^ $xor);
234 ioctl($fh, FS_IOC_SETFLAGS
, $flags) or die "FS_IOC_SETFLAGS failed - $!\n";
239 my ($class, $storeid, $scfg, $volname) = @_;
241 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
242 $class->parse_volname($volname);
245 $newname =~ s/^vm-/base-/;
247 # If we're not working with a 'raw' file, which is the only thing that's "different" for btrfs,
248 # or a subvolume, we forward to the DirPlugin
249 if ($format ne 'raw' && $format ne 'subvol') {
250 return PVE
::Storage
::DirPlugin
::create_base
(@_);
253 my $path = $class->filesystem_path($scfg, $volname);
254 my $newvolname = $basename ?
"$basevmid/$basename/$vmid/$newname" : "$vmid/$newname";
255 my $newpath = $class->filesystem_path($scfg, $newvolname);
258 my $newsubvol = $newpath;
259 if ($format eq 'raw') {
260 $subvol = raw_file_to_subvol
($subvol);
261 $newsubvol = raw_file_to_subvol
($newsubvol);
264 rename($subvol, $newsubvol)
265 || die "rename '$subvol' to '$newsubvol' failed - $!\n";
266 eval { $class->btrfs_cmd(['property', 'set', $newsubvol, 'ro', 'true']) };
273 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
275 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
276 $class->parse_volname($volname);
278 # If we're not working with a 'raw' file, which is the only thing that's "different" for btrfs,
279 # or a subvolume, we forward to the DirPlugin
280 if ($format ne 'raw' && $format ne 'subvol') {
281 return PVE
::Storage
::DirPlugin
::clone_image
(@_);
284 my $imagedir = $class->get_subdir($scfg, 'images');
285 $imagedir .= "/$vmid";
288 my $path = $class->filesystem_path($scfg, $volname);
289 my $newname = $class->find_free_diskname($storeid, $scfg, $vmid, $format, 1);
291 # For btrfs subvolumes we don't actually need the "link":
292 #my $newvolname = "$basevmid/$basename/$vmid/$newname";
293 my $newvolname = "$vmid/$newname";
294 my $newpath = $class->filesystem_path($scfg, $newvolname);
297 my $newsubvol = $newpath;
298 if ($format eq 'raw') {
299 $subvol = raw_file_to_subvol
($subvol);
300 $newsubvol = raw_file_to_subvol
($newsubvol);
303 $class->btrfs_cmd(['subvolume', 'snapshot', '--', $subvol, $newsubvol]);
309 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
311 if ($fmt ne 'raw' && $fmt ne 'subvol') {
312 return $class->SUPER::alloc_image
($storeid, $scfg, $vmid, $fmt, $name, $size);
317 my $imagedir = $class->get_subdir($scfg, 'images') . "/$vmid";
321 $name = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt, 1) if !$name;
323 my (undef, $tmpfmt) = PVE
::Storage
::Plugin
::parse_name_dir
($name);
325 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
328 # End copy from Plugin.pm
330 my $subvol = "$imagedir/$name";
331 # .raw is not part of the directory name
332 $subvol =~ s/\.raw$//;
334 die "disk image '$subvol' already exists\n" if -e
$subvol;
338 $path = "$subvol/disk.raw";
341 if ($fmt eq 'subvol' && !!$size) {
342 # NOTE: `btrfs send/recv` actually drops quota information so supporting subvolumes with
343 # quotas doesn't play nice with send/recv.
344 die "btrfs quotas are currently not supported, use an unsized subvolume or a raw file\n";
347 $class->btrfs_cmd(['subvolume', 'create', '--', $subvol]);
350 if ($fmt eq 'subvol') {
351 # Nothing to do for now...
353 # This is how we *would* do it:
354 # # Use the subvol's default 0/$id qgroup
356 # # This call should happen at storage creation instead and therefore governed by a
357 # # configuration option!
358 # # $class->btrfs_cmd(['quota', 'enable', $subvol]);
359 # my $id = $class->btrfs_get_subvol_id($subvol);
360 # $class->btrfs_cmd(['qgroup', 'limit', "${size}k", "0/$id", $subvol]);
362 } elsif ($fmt eq 'raw') {
363 sysopen my $fh, $path, O_WRONLY
| O_CREAT
| O_EXCL
364 or die "failed to create raw file '$path' - $!\n";
365 chattr
($fh, ~FS_NOCOW_FL
, FS_NOCOW_FL
) if $scfg->{nocow
};
366 truncate($fh, $size * 1024)
367 or die "failed to set file size for '$path' - $!\n";
370 die "internal format error (format = $fmt)\n";
375 eval { $class->btrfs_cmd(['subvolume', 'delete', '--', $subvol]); };
380 return "$vmid/$name";
383 # Same as btrfsprogs does:
384 my sub path_is_subvolume
: prototype($) {
386 my @stat = stat($path)
387 or die "stat failed on '$path' - $!\n";
388 my ($ino, $mode) = @stat[1, 2];
389 return S_ISDIR
($mode) && $ino == BTRFS_FIRST_FREE_OBJECTID
;
392 my $BTRFS_VOL_REGEX = qr/((?:vm|base|subvol)-\d+-disk-\d+(?:\.subvol)?)(?:\@(\S+))$/;
394 # Calls `$code->($volume, $name, $snapshot)` for each subvol in a directory matching our volume
396 my sub foreach_subvol
: prototype($$) {
397 my ($dir, $code) = @_;
399 dir_glob_foreach
($dir, $BTRFS_VOL_REGEX, sub {
400 my ($volume, $name, $snapshot) = ($1, $2, $3);
401 return if !path_is_subvolume
("$dir/$volume");
402 $code->($volume, $name, $snapshot);
407 my ($class, $storeid, $scfg, $volname, $isBase, $_format) = @_;
409 my (undef, undef, $vmid, undef, undef, undef, $format) =
410 $class->parse_volname($volname);
412 if (!defined($format) || ($format ne 'subvol' && $format ne 'raw')) {
413 return PVE
::Storage
::DirPlugin-
>free_image($storeid, $scfg, $volname, $isBase, $_format);
416 my $path = $class->filesystem_path($scfg, $volname);
419 if ($format eq 'raw') {
420 $subvol = raw_file_to_subvol
($path);
423 my $dir = dirname
($subvol);
425 foreach_subvol
($dir, sub {
426 my ($volume, $name, $snapshot) = @_;
427 return if !defined $snapshot;
428 push @snapshot_vols, "$dir/$volume";
431 $class->btrfs_cmd(['subvolume', 'delete', '--', @snapshot_vols, $subvol]);
432 # try to cleanup directory to not clutter storage with empty $vmid dirs if
433 # all images from a guest got deleted
439 # Currently not used because quotas clash with send/recv.
440 # my sub btrfs_subvol_quota {
441 # my ($class, $path) = @_;
442 # my $id = '0/' . $class->btrfs_get_subvol_id($path);
443 # my $search = qr/^\Q$id\E\s+(\d)+\s+\d+\s+(\d+)\s*$/;
445 # $class->btrfs_cmd(['qgroup', 'show', '--raw', '-rf', '--', $path], sub {
446 # return if defined($size);
447 # if ($_[0] =~ $search) {
448 # ($used, $size) = ($1, $2);
451 # if (!defined($size)) {
452 # # syslog should include more information:
453 # syslog('err', "failed to get subvolume size for: $path (id $id)");
454 # # UI should only see the last path component:
456 # die "failed to get subvolume size for $path\n";
458 # return wantarray ? ($used, $size) : $size;
461 sub volume_size_info
{
462 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
464 my $path = $class->filesystem_path($scfg, $volname);
466 my $format = ($class->parse_volname($volname))[6];
468 if (defined($format) && $format eq 'subvol') {
469 my $ctime = (stat($path))[10];
470 my ($used, $size) = (0, 0);
471 #my ($used, $size) = btrfs_subvol_quota($class, $path); # uses wantarray
472 return wantarray ?
($size, 'subvol', $used, undef, $ctime) : 1;
475 return PVE
::Storage
::Plugin
::file_size_info
($path, $timeout);
479 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
481 my $format = ($class->parse_volname($volname))[6];
482 if ($format eq 'subvol') {
483 my $path = $class->filesystem_path($scfg, $volname);
484 my $id = '0/' . $class->btrfs_get_subvol_id($path);
485 $class->btrfs_cmd(['qgroup', 'limit', '--', "${size}k", "0/$id", $path]);
489 return PVE
::Storage
::Plugin
::volume_resize
(@_);
492 sub volume_snapshot
{
493 my ($class, $scfg, $storeid, $volname, $snap) = @_;
495 my ($name, $vmid, $format) = ($class->parse_volname($volname))[1,2,6];
496 if ($format ne 'subvol' && $format ne 'raw') {
497 return PVE
::Storage
::Plugin
::volume_snapshot
(@_);
500 my $path = $class->filesystem_path($scfg, $volname);
501 my $snap_path = $class->filesystem_path($scfg, $volname, $snap);
503 if ($format eq 'raw') {
504 $path = raw_file_to_subvol
($path);
505 $snap_path = raw_file_to_subvol
($snap_path);
508 my $snapshot_dir = $class->get_subdir($scfg, 'images') . "/$vmid";
509 mkpath
$snapshot_dir;
511 $class->btrfs_cmd(['subvolume', 'snapshot', '-r', '--', $path, $snap_path]);
515 sub volume_rollback_is_possible
{
516 my ($class, $scfg, $storeid, $volname, $snap) = @_;
521 sub volume_snapshot_rollback
{
522 my ($class, $scfg, $storeid, $volname, $snap) = @_;
524 my ($name, $format) = ($class->parse_volname($volname))[1,6];
526 if ($format ne 'subvol' && $format ne 'raw') {
527 return PVE
::Storage
::Plugin
::volume_snapshot_rollback
(@_);
530 my $path = $class->filesystem_path($scfg, $volname);
531 my $snap_path = $class->filesystem_path($scfg, $volname, $snap);
533 if ($format eq 'raw') {
534 $path = raw_file_to_subvol
($path);
535 $snap_path = raw_file_to_subvol
($snap_path);
538 # Simple version would be:
541 # on error rename temp back
542 # But for atomicity in case the rename after create-failure *also* fails, we create the new
543 # subvol first, then use RENAME_EXCHANGE,
544 my $tmp_path = "$path.tmp.$$";
545 $class->btrfs_cmd(['subvolume', 'snapshot', '--', $snap_path, $tmp_path]);
546 # The paths are absolute, so pass -1 as file descriptors.
547 my $ok = PVE
::Tools
::renameat2
(-1, $tmp_path, -1, $path, &PVE
::Tools
::RENAME_EXCHANGE
);
549 eval { $class->btrfs_cmd(['subvolume', 'delete', '--', $tmp_path]) };
550 warn "failed to remove '$tmp_path' subvolume: $@" if $@;
553 die "failed to rotate '$tmp_path' into place at '$path' - $!\n";
559 sub volume_snapshot_delete
{
560 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
562 my ($name, $vmid, $format) = ($class->parse_volname($volname))[1,2,6];
564 if ($format ne 'subvol' && $format ne 'raw') {
565 return PVE
::Storage
::Plugin
::volume_snapshot_delete
(@_);
568 my $path = $class->filesystem_path($scfg, $volname, $snap);
570 if ($format eq 'raw') {
571 $path = raw_file_to_subvol
($path);
574 $class->btrfs_cmd(['subvolume', 'delete', '--', $path]);
579 sub volume_has_feature
{
580 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
584 current
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1 },
585 snap
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1 }
588 base
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1, vmdk
=> 1 },
589 current
=> { raw
=> 1 },
590 snap
=> { raw
=> 1 },
592 template
=> { current
=> { qcow2
=> 1, raw
=> 1, vmdk
=> 1, subvol
=> 1 } },
594 base
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1, vmdk
=> 1 },
595 current
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1, vmdk
=> 1 },
596 snap
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1 },
598 sparseinit
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1 },
599 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1 } },
602 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
603 $class->parse_volname($volname);
609 $key = $isBase ?
'base' : 'current';
612 return 1 if defined($features->{$feature}->{$key}->{$format});
618 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
619 my $imagedir = $class->get_subdir($scfg, 'images');
623 # Copied from Plugin.pm, with file_size_info calls adapted:
624 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
625 # different to in Plugin.pm the regex below also excludes '@' as valid file name
626 next if $fn !~ m
@^(/.+/(\d
+)/([^/\
@.]+(?
:\
.(qcow2
|vmdk
|subvol
))?
))$@;
633 next if !$vollist && defined($vmid) && ($owner ne $vmid);
635 my $volid = "$storeid:$owner/$name";
636 my ($size, $format, $used, $parent, $ctime);
640 ($size, $format, $used, $parent, $ctime) = PVE
::Storage
::Plugin
::file_size_info
("$fn/disk.raw");
641 } elsif ($ext eq 'subvol') {
642 ($used, $size) = (0, 0);
643 #($used, $size) = btrfs_subvol_quota($class, $fn);
646 ($size, $format, $used, $parent, $ctime) = PVE
::Storage
::Plugin
::file_size_info
($fn);
648 next if !($format && defined($size));
651 next if ! grep { $_ eq $volid } @$vollist;
655 volid
=> $volid, format
=> $format,
656 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent,
659 $info->{ctime
} = $ctime if $ctime;
667 sub volume_export_formats
{
668 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
670 # We can do whatever `DirPlugin` can do.
671 my @result = PVE
::Storage
::Plugin
::volume_export_formats
(@_);
673 # `btrfs send` only works on snapshots:
674 return @result if !defined $snapshot;
676 # Incremental stream with snapshots is only supported if the snapshots are listed (new api):
677 return @result if defined($base_snapshot) && $with_snapshots && ref($with_snapshots) ne 'ARRAY';
679 # Otherwise we do also support `with_snapshots`.
681 # Finally, `btrfs send` only works on formats where we actually use btrfs subvolumes:
682 my $format = ($class->parse_volname($volname))[6];
683 return @result if $format ne 'raw' && $format ne 'subvol';
685 return ('btrfs', @result);
688 sub volume_import_formats
{
689 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
691 # Same as export-formats, beware the parameter order:
692 return volume_export_formats
(
716 if ($format ne 'btrfs') {
717 return PVE
::Storage
::Plugin
::volume_export
(@_);
720 die "format 'btrfs' only works on snapshots\n"
721 if !defined $snapshot;
723 die "'btrfs' format in incremental mode requires snapshots to be listed explicitly\n"
724 if defined($base_snapshot) && $with_snapshots && ref($with_snapshots) ne 'ARRAY';
726 my $volume_format = ($class->parse_volname($volname))[6];
728 die "btrfs-sending volumes of type $volume_format ('$volname') is not supported\n"
729 if $volume_format ne 'raw' && $volume_format ne 'subvol';
731 my $path = $class->path($scfg, $volname, $storeid);
733 if ($volume_format eq 'raw') {
734 $path = raw_file_to_subvol
($path);
737 my $cmd = ['btrfs', '-q', 'send', '-e'];
738 if ($base_snapshot) {
739 my $base = $class->path($scfg, $volname, $storeid, $base_snapshot);
740 if ($volume_format eq 'raw') {
741 $base = raw_file_to_subvol
($base);
743 push @$cmd, '-p', $base;
746 if (ref($with_snapshots) eq 'ARRAY') {
747 push @$cmd, (map { "$path\@$_" } ($with_snapshots // [])->@*), $path;
749 dir_glob_foreach
(dirname
($path), $BTRFS_VOL_REGEX, sub {
750 push @$cmd, "$path\@$_[2]" if !(defined($snapshot) && $_[2] eq $snapshot);
753 $path .= "\@$snapshot" if defined($snapshot);
756 run_command
($cmd, output
=> '>&'.fileno($fh));
774 if ($format ne 'btrfs') {
775 return PVE
::Storage
::Plugin
::volume_import
(@_);
778 die "format 'btrfs' only works on snapshots\n"
779 if !defined $snapshot;
781 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $volume_format) =
782 $class->parse_volname($volname);
784 die "btrfs-receiving volumes of type $volume_format ('$volname') is not supported\n"
785 if $volume_format ne 'raw' && $volume_format ne 'subvol';
787 if (defined($base_snapshot)) {
788 my $path = $class->path($scfg, $volname, $storeid, $base_snapshot);
789 die "base snapshot '$base_snapshot' not found - no such directory '$path'\n"
790 if !path_is_subvolume
($path);
793 my $destination = $class->filesystem_path($scfg, $volname);
794 if ($volume_format eq 'raw') {
795 $destination = raw_file_to_subvol
($destination);
798 if (!defined($base_snapshot) && -e
$destination) {
799 die "volume $volname already exists\n" if !$allow_rename;
800 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $volume_format, 1);
803 my $imagedir = $class->get_subdir($scfg, $vtype);
804 $imagedir .= "/$vmid" if $vtype eq 'images';
806 my $tmppath = "$imagedir/recv.$vmid.tmp";
807 mkdir($imagedir); # FIXME: if $scfg->{mkdir};
808 if (!mkdir($tmppath)) {
809 die "temp receive directory already exists at '$tmppath', incomplete concurrent import?\n"
811 die "failed to create temporary receive directory at '$tmppath' - $!\n";
814 my $dh = IO
::Dir-
>new($tmppath)
815 or die "failed to open temporary receive directory '$tmppath' - $!\n";
817 run_command
(['btrfs', '-q', 'receive', '-e', '--', $tmppath], input
=> '<&'.fileno($fh));
819 # Analyze the received subvolumes;
820 my ($diskname, $found_snapshot, @snapshots);
822 while (defined(my $entry = $dh->read)) {
823 next if $entry eq '.' || $entry eq '..';
824 next if $entry !~ /^$BTRFS_VOL_REGEX$/;
825 my ($cur_diskname, $cur_snapshot) = ($1, $2);
827 die "send stream included a non-snapshot subvolume\n"
828 if !defined($cur_snapshot);
830 if (!defined($diskname)) {
831 $diskname = $cur_diskname;
833 die "multiple disks contained in stream ('$diskname' vs '$cur_diskname')\n"
834 if $diskname ne $cur_diskname;
837 if ($cur_snapshot eq $snapshot) {
840 push @snapshots, $cur_snapshot;
844 die "send stream did not contain the expected current snapshot '$snapshot'\n"
847 # Rotate the disk into place, first the current state:
848 # Note that read-only subvolumes cannot be moved into different directories, but for the
849 # "current" state we also want a writable copy, so start with that:
850 $class->btrfs_cmd(['property', 'set', "$tmppath/$diskname\@$snapshot", 'ro', 'false']);
851 PVE
::Tools
::renameat2
(
853 "$tmppath/$diskname\@$snapshot",
856 &PVE
::Tools
::RENAME_NOREPLACE
,
857 ) or die "failed to move received snapshot '$tmppath/$diskname\@$snapshot'"
858 . " into place at '$destination' - $!\n";
860 # Now recreate the actual snapshot:
867 "$destination\@$snapshot",
870 # Now go through the remaining snapshots (if any)
871 foreach my $snap (@snapshots) {
872 $class->btrfs_cmd(['property', 'set', "$tmppath/$diskname\@$snap", 'ro', 'false']);
873 PVE
::Tools
::renameat2
(
875 "$tmppath/$diskname\@$snap",
877 "$destination\@$snap",
878 &PVE
::Tools
::RENAME_NOREPLACE
,
879 ) or die "failed to move received snapshot '$tmppath/$diskname\@$snap'"
880 . " into place at '$destination\@$snap' - $!\n";
881 eval { $class->btrfs_cmd(['property', 'set', "$destination\@$snap", 'ro', 'true']) };
882 warn "failed to make $destination\@$snap read-only - $!\n" if $@;
888 # Cleanup all the received snapshots we did not move into place, so we can remove the temp
892 while (defined(my $entry = $dh->read)) {
893 next if $entry eq '.' || $entry eq '..';
894 eval { $class->btrfs_cmd(['subvolume', 'delete', '--', "$tmppath/$entry"]) };
897 $dh->close; undef $dh;
899 if (!rmdir($tmppath)) {
900 warn "failed to remove temporary directory '$tmppath' - $!\n"
905 # clean up if the directory ended up being empty after an error
910 return "$storeid:$volname";