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(basename 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 'max-protected-backups' => { optional
=> 1 },
72 content
=> { optional
=> 1 },
73 format
=> { optional
=> 1 },
74 is_mountpoint
=> { optional
=> 1 },
75 nocow
=> { optional
=> 1 },
76 mkdir => { optional
=> 1 },
77 preallocation
=> { optional
=> 1 },
78 # TODO: The new variant of mkdir with `populate` vs `create`...
82 # Storage implementation
84 # We use the same volume names are directory plugins, but map *raw* disk image file names into a
87 # `vm-VMID-disk-ID.raw`
88 # -> `images/VMID/vm-VMID-disk-ID/disk.raw`
89 # where the `vm-VMID-disk-ID/` subdirectory is a btrfs subvolume
91 # Reuse `DirPlugin`'s `check_config`. This simply checks for invalid paths.
93 my ($self, $sectionId, $config, $create, $skipSchemaCheck) = @_;
94 return PVE
::Storage
::DirPlugin
::check_config
($self, $sectionId, $config, $create, $skipSchemaCheck);
97 my sub getfsmagic
($) {
99 # The field type sizes in `struct statfs` are defined in a rather annoying way, and we only
100 # need the first field, which is a `long` for our supported platforms.
101 # Should be moved to pve-rs, so this can be the problem of the `libc` crate ;-)
102 # Just round up and extract what we need:
103 my $buf = pack('x160');
104 if (0 != syscall(&PVE
::Syscall
::SYS_statfs
, $path, $buf)) {
105 die "statfs on '$path' failed - $!\n";
108 return unpack('L!', $buf);
111 my sub assert_btrfs
($) {
113 die "'$path' is not a btrfs file system\n"
114 if getfsmagic
($path) != BTRFS_MAGIC
;
117 sub activate_storage
{
118 my ($class, $storeid, $scfg, $cache) = @_;
120 my $path = $scfg->{path
};
121 if (!defined($scfg->{mkdir}) || $scfg->{mkdir}) {
125 my $mp = PVE
::Storage
::DirPlugin
::parse_is_mountpoint
($scfg);
126 if (defined($mp) && !PVE
::Storage
::DirPlugin
::path_is_mounted
($mp, $cache->{mountdata
})) {
127 die "unable to activate storage '$storeid' - directory is expected to be a mount point but"
128 ." is not mounted: '$mp'\n";
131 assert_btrfs
($path); # only assert this stuff now, ensures $path is there and better UX
133 $class->SUPER::activate_storage
($storeid, $scfg, $cache);
137 my ($class, $storeid, $scfg, $cache) = @_;
138 return PVE
::Storage
::DirPlugin
::status
($class, $storeid, $scfg, $cache);
141 # TODO: sub get_volume_attribute {}
143 # TODO: sub update_volume_attribute {}
145 # croak would not include the caller from within this module
148 my (undef, $f, $n) = caller(1);
149 die "$msg at $f: $n\n";
152 # Given a name (eg. `vm-VMID-disk-ID.raw`), take the part up to the format suffix as the name of
153 # the subdirectory (subvolume).
154 sub raw_name_to_dir
($) {
157 # For the subvolume directory Strip the `.<format>` suffix:
158 if ($raw =~ /^(.*)\.raw$/) {
162 __error
"internal error: bad disk name: $raw";
165 sub raw_file_to_subvol
($) {
168 if ($file =~ m
|^(.*)/disk\
.raw
$|) {
172 __error
"internal error: bad raw path: $file";
175 sub filesystem_path
{
176 my ($class, $scfg, $volname, $snapname) = @_;
178 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
179 $class->parse_volname($volname);
181 my $path = $class->get_subdir($scfg, $vtype);
183 $path .= "/$vmid" if $vtype eq 'images';
185 if (defined($format) && $format eq 'raw') {
186 my $dir = raw_name_to_dir
($name);
188 $dir .= "\@$snapname";
190 $path .= "/$dir/disk.raw";
191 } elsif (defined($format) && $format eq 'subvol') {
194 $path .= "\@$snapname";
200 return wantarray ?
($path, $vmid, $vtype) : $path;
204 my ($class, $cmd, $outfunc) = @_;
208 if (defined($outfunc)) {
210 my $part = &$outfunc(@_);
211 $msg .= $part if defined($part);
214 $func = sub { $msg .= "$_[0]\n" };
216 run_command
(['btrfs', '-q', @$cmd], errmsg
=> 'btrfs error', outfunc
=> $func);
221 sub btrfs_get_subvol_id
{
222 my ($class, $path) = @_;
223 my $info = $class->btrfs_cmd(['subvolume', 'show', '--', $path]);
224 if ($info !~ /^\s*(?:Object|Subvolume) ID:\s*(\d+)$/m) {
225 die "failed to get btrfs subvolume ID from: $info\n";
230 my sub chattr
: prototype($$$) {
231 my ($fh, $mask, $xor) = @_;
233 my $flags = pack('L!', 0);
234 ioctl($fh, FS_IOC_GETFLAGS
, $flags) or die "FS_IOC_GETFLAGS failed - $!\n";
235 $flags = pack('L!', (unpack('L!', $flags) & $mask) ^ $xor);
236 ioctl($fh, FS_IOC_SETFLAGS
, $flags) or die "FS_IOC_SETFLAGS failed - $!\n";
241 my ($class, $storeid, $scfg, $volname) = @_;
243 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
244 $class->parse_volname($volname);
247 $newname =~ s/^vm-/base-/;
249 # If we're not working with a 'raw' file, which is the only thing that's "different" for btrfs,
250 # or a subvolume, we forward to the DirPlugin
251 if ($format ne 'raw' && $format ne 'subvol') {
252 return PVE
::Storage
::DirPlugin
::create_base
(@_);
255 my $path = $class->filesystem_path($scfg, $volname);
256 my $newvolname = $basename ?
"$basevmid/$basename/$vmid/$newname" : "$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 rename($subvol, $newsubvol)
267 || die "rename '$subvol' to '$newsubvol' failed - $!\n";
268 eval { $class->btrfs_cmd(['property', 'set', $newsubvol, 'ro', 'true']) };
275 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
277 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
278 $class->parse_volname($volname);
280 # If we're not working with a 'raw' file, which is the only thing that's "different" for btrfs,
281 # or a subvolume, we forward to the DirPlugin
282 if ($format ne 'raw' && $format ne 'subvol') {
283 return PVE
::Storage
::DirPlugin
::clone_image
(@_);
286 my $imagedir = $class->get_subdir($scfg, 'images');
287 $imagedir .= "/$vmid";
290 my $path = $class->filesystem_path($scfg, $volname);
291 my $newname = $class->find_free_diskname($storeid, $scfg, $vmid, $format, 1);
293 # For btrfs subvolumes we don't actually need the "link":
294 #my $newvolname = "$basevmid/$basename/$vmid/$newname";
295 my $newvolname = "$vmid/$newname";
296 my $newpath = $class->filesystem_path($scfg, $newvolname);
299 my $newsubvol = $newpath;
300 if ($format eq 'raw') {
301 $subvol = raw_file_to_subvol
($subvol);
302 $newsubvol = raw_file_to_subvol
($newsubvol);
305 $class->btrfs_cmd(['subvolume', 'snapshot', '--', $subvol, $newsubvol]);
311 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
313 if ($fmt ne 'raw' && $fmt ne 'subvol') {
314 return $class->SUPER::alloc_image
($storeid, $scfg, $vmid, $fmt, $name, $size);
319 my $imagedir = $class->get_subdir($scfg, 'images') . "/$vmid";
323 $name = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt, 1) if !$name;
325 my (undef, $tmpfmt) = PVE
::Storage
::Plugin
::parse_name_dir
($name);
327 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
330 # End copy from Plugin.pm
332 my $subvol = "$imagedir/$name";
333 # .raw is not part of the directory name
334 $subvol =~ s/\.raw$//;
336 die "disk image '$subvol' already exists\n" if -e
$subvol;
340 $path = "$subvol/disk.raw";
343 if ($fmt eq 'subvol' && !!$size) {
344 # NOTE: `btrfs send/recv` actually drops quota information so supporting subvolumes with
345 # quotas doesn't play nice with send/recv.
346 die "btrfs quotas are currently not supported, use an unsized subvolume or a raw file\n";
349 $class->btrfs_cmd(['subvolume', 'create', '--', $subvol]);
352 if ($fmt eq 'subvol') {
353 # Nothing to do for now...
355 # This is how we *would* do it:
356 # # Use the subvol's default 0/$id qgroup
358 # # This call should happen at storage creation instead and therefore governed by a
359 # # configuration option!
360 # # $class->btrfs_cmd(['quota', 'enable', $subvol]);
361 # my $id = $class->btrfs_get_subvol_id($subvol);
362 # $class->btrfs_cmd(['qgroup', 'limit', "${size}k", "0/$id", $subvol]);
364 } elsif ($fmt eq 'raw') {
365 sysopen my $fh, $path, O_WRONLY
| O_CREAT
| O_EXCL
366 or die "failed to create raw file '$path' - $!\n";
367 chattr
($fh, ~FS_NOCOW_FL
, FS_NOCOW_FL
) if $scfg->{nocow
};
368 truncate($fh, $size * 1024)
369 or die "failed to set file size for '$path' - $!\n";
372 die "internal format error (format = $fmt)\n";
377 eval { $class->btrfs_cmd(['subvolume', 'delete', '--', $subvol]); };
382 return "$vmid/$name";
385 # Same as btrfsprogs does:
386 my sub path_is_subvolume
: prototype($) {
388 my @stat = stat($path)
389 or die "stat failed on '$path' - $!\n";
390 my ($ino, $mode) = @stat[1, 2];
391 return S_ISDIR
($mode) && $ino == BTRFS_FIRST_FREE_OBJECTID
;
394 my $BTRFS_VOL_REGEX = qr/((?:vm|base|subvol)-\d+-disk-\d+(?:\.subvol)?)(?:\@(\S+))$/;
396 # Calls `$code->($volume, $name, $snapshot)` for each subvol in a directory matching our volume
398 my sub foreach_subvol
: prototype($$) {
399 my ($dir, $code) = @_;
401 dir_glob_foreach
($dir, $BTRFS_VOL_REGEX, sub {
402 my ($volume, $name, $snapshot) = ($1, $2, $3);
403 return if !path_is_subvolume
("$dir/$volume");
404 $code->($volume, $name, $snapshot);
409 my ($class, $storeid, $scfg, $volname, $isBase, $_format) = @_;
411 my (undef, undef, $vmid, undef, undef, undef, $format) =
412 $class->parse_volname($volname);
414 if (!defined($format) || ($format ne 'subvol' && $format ne 'raw')) {
415 return $class->SUPER::free_image
($storeid, $scfg, $volname, $isBase, $_format);
418 my $path = $class->filesystem_path($scfg, $volname);
421 if ($format eq 'raw') {
422 $subvol = raw_file_to_subvol
($path);
425 my $dir = dirname
($subvol);
426 my $basename = basename
($subvol);
428 foreach_subvol
($dir, sub {
429 my ($volume, $name, $snapshot) = @_;
430 return if $name ne $basename;
431 return if !defined $snapshot;
432 push @snapshot_vols, "$dir/$volume";
435 $class->btrfs_cmd(['subvolume', 'delete', '--', @snapshot_vols, $subvol]);
436 # try to cleanup directory to not clutter storage with empty $vmid dirs if
437 # all images from a guest got deleted
443 # Currently not used because quotas clash with send/recv.
444 # my sub btrfs_subvol_quota {
445 # my ($class, $path) = @_;
446 # my $id = '0/' . $class->btrfs_get_subvol_id($path);
447 # my $search = qr/^\Q$id\E\s+(\d)+\s+\d+\s+(\d+)\s*$/;
449 # $class->btrfs_cmd(['qgroup', 'show', '--raw', '-rf', '--', $path], sub {
450 # return if defined($size);
451 # if ($_[0] =~ $search) {
452 # ($used, $size) = ($1, $2);
455 # if (!defined($size)) {
456 # # syslog should include more information:
457 # syslog('err', "failed to get subvolume size for: $path (id $id)");
458 # # UI should only see the last path component:
460 # die "failed to get subvolume size for $path\n";
462 # return wantarray ? ($used, $size) : $size;
465 sub volume_size_info
{
466 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
468 my $path = $class->filesystem_path($scfg, $volname);
470 my $format = ($class->parse_volname($volname))[6];
472 if (defined($format) && $format eq 'subvol') {
473 my $ctime = (stat($path))[10];
474 my ($used, $size) = (0, 0);
475 #my ($used, $size) = btrfs_subvol_quota($class, $path); # uses wantarray
476 return wantarray ?
($size, 'subvol', $used, undef, $ctime) : 1;
479 return PVE
::Storage
::Plugin
::file_size_info
($path, $timeout);
483 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
485 my $format = ($class->parse_volname($volname))[6];
486 if ($format eq 'subvol') {
487 my $path = $class->filesystem_path($scfg, $volname);
488 my $id = '0/' . $class->btrfs_get_subvol_id($path);
489 $class->btrfs_cmd(['qgroup', 'limit', '--', "${size}k", "0/$id", $path]);
493 return PVE
::Storage
::Plugin
::volume_resize
(@_);
496 sub volume_snapshot
{
497 my ($class, $scfg, $storeid, $volname, $snap) = @_;
499 my ($name, $vmid, $format) = ($class->parse_volname($volname))[1,2,6];
500 if ($format ne 'subvol' && $format ne 'raw') {
501 return PVE
::Storage
::Plugin
::volume_snapshot
(@_);
504 my $path = $class->filesystem_path($scfg, $volname);
505 my $snap_path = $class->filesystem_path($scfg, $volname, $snap);
507 if ($format eq 'raw') {
508 $path = raw_file_to_subvol
($path);
509 $snap_path = raw_file_to_subvol
($snap_path);
512 my $snapshot_dir = $class->get_subdir($scfg, 'images') . "/$vmid";
513 mkpath
$snapshot_dir;
515 $class->btrfs_cmd(['subvolume', 'snapshot', '-r', '--', $path, $snap_path]);
519 sub volume_rollback_is_possible
{
520 my ($class, $scfg, $storeid, $volname, $snap, $blockers) = @_;
525 sub volume_snapshot_rollback
{
526 my ($class, $scfg, $storeid, $volname, $snap) = @_;
528 my ($name, $format) = ($class->parse_volname($volname))[1,6];
530 if ($format ne 'subvol' && $format ne 'raw') {
531 return PVE
::Storage
::Plugin
::volume_snapshot_rollback
(@_);
534 my $path = $class->filesystem_path($scfg, $volname);
535 my $snap_path = $class->filesystem_path($scfg, $volname, $snap);
537 if ($format eq 'raw') {
538 $path = raw_file_to_subvol
($path);
539 $snap_path = raw_file_to_subvol
($snap_path);
542 # Simple version would be:
545 # on error rename temp back
546 # But for atomicity in case the rename after create-failure *also* fails, we create the new
547 # subvol first, then use RENAME_EXCHANGE,
548 my $tmp_path = "$path.tmp.$$";
549 $class->btrfs_cmd(['subvolume', 'snapshot', '--', $snap_path, $tmp_path]);
550 # The paths are absolute, so pass -1 as file descriptors.
551 my $ok = PVE
::Tools
::renameat2
(-1, $tmp_path, -1, $path, &PVE
::Tools
::RENAME_EXCHANGE
);
553 eval { $class->btrfs_cmd(['subvolume', 'delete', '--', $tmp_path]) };
554 warn "failed to remove '$tmp_path' subvolume: $@" if $@;
557 die "failed to rotate '$tmp_path' into place at '$path' - $!\n";
563 sub volume_snapshot_delete
{
564 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
566 my ($name, $vmid, $format) = ($class->parse_volname($volname))[1,2,6];
568 if ($format ne 'subvol' && $format ne 'raw') {
569 return PVE
::Storage
::Plugin
::volume_snapshot_delete
(@_);
572 my $path = $class->filesystem_path($scfg, $volname, $snap);
574 if ($format eq 'raw') {
575 $path = raw_file_to_subvol
($path);
578 $class->btrfs_cmd(['subvolume', 'delete', '--', $path]);
583 sub volume_has_feature
{
584 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
588 current
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1 },
589 snap
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1 }
592 base
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1, vmdk
=> 1 },
593 current
=> { raw
=> 1 },
594 snap
=> { raw
=> 1 },
597 current
=> { qcow2
=> 1, raw
=> 1, vmdk
=> 1, subvol
=> 1 },
600 base
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1, vmdk
=> 1 },
601 current
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1, vmdk
=> 1 },
602 snap
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1 },
605 base
=> { qcow2
=> 1, raw
=> 1, vmdk
=> 1 },
606 current
=> { qcow2
=> 1, raw
=> 1, vmdk
=> 1 },
610 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) = $class->parse_volname($volname);
616 $key = $isBase ?
'base' : 'current';
619 return 1 if defined($features->{$feature}->{$key}->{$format});
625 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
626 my $imagedir = $class->get_subdir($scfg, 'images');
630 # Copied from Plugin.pm, with file_size_info calls adapted:
631 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
632 # different to in Plugin.pm the regex below also excludes '@' as valid file name
633 next if $fn !~ m
@^(/.+/(\d
+)/([^/\
@.]+(?
:\
.(qcow2
|vmdk
|subvol
))?
))$@;
640 next if !$vollist && defined($vmid) && ($owner ne $vmid);
642 my $volid = "$storeid:$owner/$name";
643 my ($size, $format, $used, $parent, $ctime);
647 ($size, $format, $used, $parent, $ctime) = PVE
::Storage
::Plugin
::file_size_info
("$fn/disk.raw");
648 } elsif ($ext eq 'subvol') {
649 ($used, $size) = (0, 0);
650 #($used, $size) = btrfs_subvol_quota($class, $fn);
653 ($size, $format, $used, $parent, $ctime) = PVE
::Storage
::Plugin
::file_size_info
($fn);
655 next if !($format && defined($size));
658 next if ! grep { $_ eq $volid } @$vollist;
662 volid
=> $volid, format
=> $format,
663 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent,
666 $info->{ctime
} = $ctime if $ctime;
674 sub volume_export_formats
{
675 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
677 # We can do whatever `DirPlugin` can do.
678 my @result = PVE
::Storage
::Plugin
::volume_export_formats
(@_);
680 # `btrfs send` only works on snapshots:
681 return @result if !defined $snapshot;
683 # Incremental stream with snapshots is only supported if the snapshots are listed (new api):
684 return @result if defined($base_snapshot) && $with_snapshots && ref($with_snapshots) ne 'ARRAY';
686 # Otherwise we do also support `with_snapshots`.
688 # Finally, `btrfs send` only works on formats where we actually use btrfs subvolumes:
689 my $format = ($class->parse_volname($volname))[6];
690 return @result if $format ne 'raw' && $format ne 'subvol';
692 return ('btrfs', @result);
695 sub volume_import_formats
{
696 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
698 # Same as export-formats, beware the parameter order:
699 return volume_export_formats
(
723 if ($format ne 'btrfs') {
724 return PVE
::Storage
::Plugin
::volume_export
(@_);
727 die "format 'btrfs' only works on snapshots\n"
728 if !defined $snapshot;
730 die "'btrfs' format in incremental mode requires snapshots to be listed explicitly\n"
731 if defined($base_snapshot) && $with_snapshots && ref($with_snapshots) ne 'ARRAY';
733 my $volume_format = ($class->parse_volname($volname))[6];
735 die "btrfs-sending volumes of type $volume_format ('$volname') is not supported\n"
736 if $volume_format ne 'raw' && $volume_format ne 'subvol';
738 my $path = $class->path($scfg, $volname, $storeid);
740 if ($volume_format eq 'raw') {
741 $path = raw_file_to_subvol
($path);
744 my $cmd = ['btrfs', '-q', 'send', '-e'];
745 if ($base_snapshot) {
746 my $base = $class->path($scfg, $volname, $storeid, $base_snapshot);
747 if ($volume_format eq 'raw') {
748 $base = raw_file_to_subvol
($base);
750 push @$cmd, '-p', $base;
753 if (ref($with_snapshots) eq 'ARRAY') {
754 push @$cmd, (map { "$path\@$_" } ($with_snapshots // [])->@*), $path;
756 dir_glob_foreach
(dirname
($path), $BTRFS_VOL_REGEX, sub {
757 push @$cmd, "$path\@$_[2]" if !(defined($snapshot) && $_[2] eq $snapshot);
760 $path .= "\@$snapshot" if defined($snapshot);
763 run_command
($cmd, output
=> '>&'.fileno($fh));
781 if ($format ne 'btrfs') {
782 return PVE
::Storage
::Plugin
::volume_import
(@_);
785 die "format 'btrfs' only works on snapshots\n"
786 if !defined $snapshot;
788 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $volume_format) =
789 $class->parse_volname($volname);
791 die "btrfs-receiving volumes of type $volume_format ('$volname') is not supported\n"
792 if $volume_format ne 'raw' && $volume_format ne 'subvol';
794 if (defined($base_snapshot)) {
795 my $path = $class->path($scfg, $volname, $storeid, $base_snapshot);
796 die "base snapshot '$base_snapshot' not found - no such directory '$path'\n"
797 if !path_is_subvolume
($path);
800 my $destination = $class->filesystem_path($scfg, $volname);
801 if ($volume_format eq 'raw') {
802 $destination = raw_file_to_subvol
($destination);
805 if (!defined($base_snapshot) && -e
$destination) {
806 die "volume $volname already exists\n" if !$allow_rename;
807 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $volume_format, 1);
810 my $imagedir = $class->get_subdir($scfg, $vtype);
811 $imagedir .= "/$vmid" if $vtype eq 'images';
813 my $tmppath = "$imagedir/recv.$vmid.tmp";
814 mkdir($imagedir); # FIXME: if $scfg->{mkdir};
815 if (!mkdir($tmppath)) {
816 die "temp receive directory already exists at '$tmppath', incomplete concurrent import?\n"
818 die "failed to create temporary receive directory at '$tmppath' - $!\n";
821 my $dh = IO
::Dir-
>new($tmppath)
822 or die "failed to open temporary receive directory '$tmppath' - $!\n";
824 run_command
(['btrfs', '-q', 'receive', '-e', '--', $tmppath], input
=> '<&'.fileno($fh));
826 # Analyze the received subvolumes;
827 my ($diskname, $found_snapshot, @snapshots);
829 while (defined(my $entry = $dh->read)) {
830 next if $entry eq '.' || $entry eq '..';
831 next if $entry !~ /^$BTRFS_VOL_REGEX$/;
832 my ($cur_diskname, $cur_snapshot) = ($1, $2);
834 die "send stream included a non-snapshot subvolume\n"
835 if !defined($cur_snapshot);
837 if (!defined($diskname)) {
838 $diskname = $cur_diskname;
840 die "multiple disks contained in stream ('$diskname' vs '$cur_diskname')\n"
841 if $diskname ne $cur_diskname;
844 if ($cur_snapshot eq $snapshot) {
847 push @snapshots, $cur_snapshot;
851 die "send stream did not contain the expected current snapshot '$snapshot'\n"
854 # Rotate the disk into place, first the current state:
855 # Note that read-only subvolumes cannot be moved into different directories, but for the
856 # "current" state we also want a writable copy, so start with that:
857 $class->btrfs_cmd(['property', 'set', "$tmppath/$diskname\@$snapshot", 'ro', 'false']);
858 PVE
::Tools
::renameat2
(
860 "$tmppath/$diskname\@$snapshot",
863 &PVE
::Tools
::RENAME_NOREPLACE
,
864 ) or die "failed to move received snapshot '$tmppath/$diskname\@$snapshot'"
865 . " into place at '$destination' - $!\n";
867 # Now recreate the actual snapshot:
874 "$destination\@$snapshot",
877 # Now go through the remaining snapshots (if any)
878 foreach my $snap (@snapshots) {
879 $class->btrfs_cmd(['property', 'set', "$tmppath/$diskname\@$snap", 'ro', 'false']);
880 PVE
::Tools
::renameat2
(
882 "$tmppath/$diskname\@$snap",
884 "$destination\@$snap",
885 &PVE
::Tools
::RENAME_NOREPLACE
,
886 ) or die "failed to move received snapshot '$tmppath/$diskname\@$snap'"
887 . " into place at '$destination\@$snap' - $!\n";
888 eval { $class->btrfs_cmd(['property', 'set', "$destination\@$snap", 'ro', 'true']) };
889 warn "failed to make $destination\@$snap read-only - $!\n" if $@;
895 # Cleanup all the received snapshots we did not move into place, so we can remove the temp
899 while (defined(my $entry = $dh->read)) {
900 next if $entry eq '.' || $entry eq '..';
901 eval { $class->btrfs_cmd(['subvolume', 'delete', '--', "$tmppath/$entry"]) };
904 $dh->close; undef $dh;
906 if (!rmdir($tmppath)) {
907 warn "failed to remove temporary directory '$tmppath' - $!\n"
912 # clean up if the directory ended up being empty after an error
917 return "$storeid:$volname";