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 mkdir => { optional
=> 1 },
75 # TODO: The new variant of mkdir with `populate` vs `create`...
79 # Storage implementation
81 # We use the same volume names are directory plugins, but map *raw* disk image file names into a
84 # `vm-VMID-disk-ID.raw`
85 # -> `images/VMID/vm-VMID-disk-ID/disk.raw`
86 # where the `vm-VMID-disk-ID/` subdirectory is a btrfs subvolume
88 # Reuse `DirPlugin`'s `check_config`. This simply checks for invalid paths.
90 my ($self, $sectionId, $config, $create, $skipSchemaCheck) = @_;
91 return PVE
::Storage
::DirPlugin
::check_config
($self, $sectionId, $config, $create, $skipSchemaCheck);
94 my sub getfsmagic
($) {
96 # The field type sizes in `struct statfs` are defined in a rather annoying way, and we only
97 # need the first field, which is a `long` for our supported platforms.
98 # Should be moved to pve-rs, so this can be the problem of the `libc` crate ;-)
99 # Just round up and extract what we need:
100 my $buf = pack('x160');
101 if (0 != syscall(&PVE
::Syscall
::SYS_statfs
, $path, $buf)) {
102 die "statfs on '$path' failed - $!\n";
105 return unpack('L!', $buf);
108 my sub assert_btrfs
($) {
110 die "'$path' is not a btrfs file system\n"
111 if getfsmagic
($path) != BTRFS_MAGIC
;
114 sub activate_storage
{
115 my ($class, $storeid, $scfg, $cache) = @_;
117 my $path = $scfg->{path
};
118 if (!defined($scfg->{mkdir}) || $scfg->{mkdir}) {
122 my $mp = PVE
::Storage
::DirPlugin
::parse_is_mountpoint
($scfg);
123 if (defined($mp) && !path_is_mounted
($mp, $cache->{mountdata
})) {
124 die "unable to activate storage '$storeid' - directory is expected to be a mount point but"
125 ." is not mounted: '$mp'\n";
128 assert_btrfs
($path); # only assert this stuff now, ensures $path is there and better UX
130 $class->SUPER::activate_storage
($storeid, $scfg, $cache);
134 my ($class, $storeid, $scfg, $cache) = @_;
135 return PVE
::Storage
::DirPlugin
::status
($class, $storeid, $scfg, $cache);
138 # TODO: sub get_volume_notes {}
140 # TODO: sub update_volume_notes {}
142 # croak would not include the caller from within this module
145 my (undef, $f, $n) = caller(1);
146 die "$msg at $f: $n\n";
149 # Given a name (eg. `vm-VMID-disk-ID.raw`), take the part up to the format suffix as the name of
150 # the subdirectory (subvolume).
151 sub raw_name_to_dir
($) {
154 # For the subvolume directory Strip the `.<format>` suffix:
155 if ($raw =~ /^(.*)\.raw$/) {
159 __error
"internal error: bad disk name: $raw";
162 sub raw_file_to_subvol
($) {
165 if ($file =~ m
|^(.*)/disk\
.raw
$|) {
169 __error
"internal error: bad raw path: $file";
172 sub filesystem_path
{
173 my ($class, $scfg, $volname, $snapname) = @_;
175 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
176 $class->parse_volname($volname);
178 my $path = $class->get_subdir($scfg, $vtype);
180 $path .= "/$vmid" if $vtype eq 'images';
182 if ($format eq 'raw') {
183 my $dir = raw_name_to_dir
($name);
185 $dir .= "\@$snapname";
187 $path .= "/$dir/disk.raw";
188 } elsif ($format eq 'subvol') {
191 $path .= "\@$snapname";
197 return wantarray ?
($path, $vmid, $vtype) : $path;
201 my ($class, $cmd, $outfunc) = @_;
205 if (defined($outfunc)) {
207 my $part = &$outfunc(@_);
208 $msg .= $part if defined($part);
211 $func = sub { $msg .= "$_[0]\n" };
213 run_command
(['btrfs', '-q', @$cmd], errmsg
=> 'btrfs error', outfunc
=> $func);
218 sub btrfs_get_subvol_id
{
219 my ($class, $path) = @_;
220 my $info = $class->btrfs_cmd(['subvolume', 'show', '--', $path]);
221 if ($info !~ /^\s*(?:Object|Subvolume) ID:\s*(\d+)$/m) {
222 die "failed to get btrfs subvolume ID from: $info\n";
227 my sub chattr
: prototype($$$) {
228 my ($fh, $mask, $xor) = @_;
230 my $flags = pack('L!', 0);
231 ioctl($fh, FS_IOC_GETFLAGS
, $flags) or die "FS_IOC_GETFLAGS failed - $!\n";
232 $flags = pack('L!', (unpack('L!', $flags) & $mask) ^ $xor);
233 ioctl($fh, FS_IOC_SETFLAGS
, $flags) or die "FS_IOC_SETFLAGS failed - $!\n";
238 my ($class, $storeid, $scfg, $volname) = @_;
240 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
241 $class->parse_volname($volname);
244 $newname =~ s/^vm-/base-/;
246 # If we're not working with a 'raw' file, which is the only thing that's "different" for btrfs,
247 # or a subvolume, we forward to the DirPlugin
248 if ($format ne 'raw' && $format ne 'subvol') {
249 return PVE
::Storage
::DirPlugin
::create_base
(@_);
252 my $path = $class->filesystem_path($scfg, $volname);
253 my $newvolname = $basename ?
"$basevmid/$basename/$vmid/$newname" : "$vmid/$newname";
254 my $newpath = $class->filesystem_path($scfg, $newvolname);
257 my $newsubvol = $newpath;
258 if ($format eq 'raw') {
259 $subvol = raw_file_to_subvol
($subvol);
260 $newsubvol = raw_file_to_subvol
($newsubvol);
263 rename($subvol, $newsubvol)
264 || die "rename '$subvol' to '$newsubvol' failed - $!\n";
265 eval { $class->btrfs_cmd(['property', 'set', $newsubvol, 'ro', 'true']) };
272 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
274 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
275 $class->parse_volname($volname);
277 # If we're not working with a 'raw' file, which is the only thing that's "different" for btrfs,
278 # or a subvolume, we forward to the DirPlugin
279 if ($format ne 'raw' && $format ne 'subvol') {
280 return PVE
::Storage
::DirPlugin
::clone_image
(@_);
283 my $imagedir = $class->get_subdir($scfg, 'images');
284 $imagedir .= "/$vmid";
287 my $path = $class->filesystem_path($scfg, $volname);
288 my $newname = $class->find_free_diskname($storeid, $scfg, $vmid, $format, 1);
290 # For btrfs subvolumes we don't actually need the "link":
291 #my $newvolname = "$basevmid/$basename/$vmid/$newname";
292 my $newvolname = "$vmid/$newname";
293 my $newpath = $class->filesystem_path($scfg, $newvolname);
296 my $newsubvol = $newpath;
297 if ($format eq 'raw') {
298 $subvol = raw_file_to_subvol
($subvol);
299 $newsubvol = raw_file_to_subvol
($newsubvol);
302 $class->btrfs_cmd(['subvolume', 'snapshot', '--', $subvol, $newsubvol]);
308 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
310 if ($fmt ne 'raw' && $fmt ne 'subvol') {
311 return PVE
::Storage
::DirPlugin
::alloc_image
(@_);
316 my $imagedir = $class->get_subdir($scfg, 'images') . "/$vmid";
320 $name = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt, 1) if !$name;
322 my (undef, $tmpfmt) = PVE
::Storage
::Plugin
::parse_name_dir
($name);
324 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
327 # End copy from Plugin.pm
329 my $subvol = "$imagedir/$name";
330 # .raw is not part of the directory name
331 $subvol =~ s/\.raw$//;
333 die "disk image '$subvol' already exists\n" if -e
$subvol;
337 $path = "$subvol/disk.raw";
340 if ($fmt eq 'subvol' && !!$size) {
341 # NOTE: `btrfs send/recv` actually drops quota information so supporting subvolumes with
342 # quotas doesn't play nice with send/recv.
343 die "btrfs quotas are currently not supported, use an unsized subvolume or a raw file\n";
346 $class->btrfs_cmd(['subvolume', 'create', '--', $subvol]);
349 if ($fmt eq 'subvol') {
350 # Nothing to do for now...
352 # This is how we *would* do it:
353 # # Use the subvol's default 0/$id qgroup
355 # # This call should happen at storage creation instead and therefore governed by a
356 # # configuration option!
357 # # $class->btrfs_cmd(['quota', 'enable', $subvol]);
358 # my $id = $class->btrfs_get_subvol_id($subvol);
359 # $class->btrfs_cmd(['qgroup', 'limit', "${size}k", "0/$id", $subvol]);
361 } elsif ($fmt eq 'raw') {
362 sysopen my $fh, $path, O_WRONLY
| O_CREAT
| O_EXCL
363 or die "failed to create raw file '$path' - $!\n";
364 chattr
($fh, ~FS_NOCOW_FL
, FS_NOCOW_FL
) if $scfg->{nocow
};
365 truncate($fh, $size * 1024)
366 or die "failed to set file size for '$path' - $!\n";
369 die "internal format error (format = $fmt)\n";
374 eval { $class->btrfs_cmd(['subvolume', 'delete', '--', $subvol]); };
379 return "$vmid/$name";
382 # Same as btrfsprogs does:
383 my sub path_is_subvolume
: prototype($) {
385 my @stat = stat($path)
386 or die "stat failed on '$path' - $!\n";
387 my ($ino, $mode) = @stat[1, 2];
388 return S_ISDIR
($mode) && $ino == BTRFS_FIRST_FREE_OBJECTID
;
391 my $BTRFS_VOL_REGEX = qr/((?:vm|base|subvol)-\d+-disk-\d+(?:\.subvol)?)(?:\@(\S+))$/;
393 # Calls `$code->($volume, $name, $snapshot)` for each subvol in a directory matching our volume
395 my sub foreach_subvol
: prototype($$) {
396 my ($dir, $code) = @_;
398 dir_glob_foreach
($dir, $BTRFS_VOL_REGEX, sub {
399 my ($volume, $name, $snapshot) = ($1, $2, $3);
400 return if !path_is_subvolume
("$dir/$volume");
401 $code->($volume, $name, $snapshot);
406 my ($class, $storeid, $scfg, $volname, $isBase, $_format) = @_;
408 my (undef, undef, $vmid, undef, undef, undef, $format) =
409 $class->parse_volname($volname);
411 if ($format ne 'subvol' && $format ne 'raw') {
412 return PVE
::Storage
::DirPlugin
::free_image
(@_);
415 my $path = $class->filesystem_path($scfg, $volname);
418 if ($format eq 'raw') {
419 $subvol = raw_file_to_subvol
($path);
422 my $dir = dirname
($subvol);
424 foreach_subvol
($dir, sub {
425 my ($volume, $name, $snapshot) = @_;
426 return if !defined $snapshot;
427 push @snapshot_vols, "$dir/$volume";
430 $class->btrfs_cmd(['subvolume', 'delete', '--', @snapshot_vols, $subvol]);
431 # try to cleanup directory to not clutter storage with empty $vmid dirs if
432 # all images from a guest got deleted
438 # Currently not used because quotas clash with send/recv.
439 # my sub btrfs_subvol_quota {
440 # my ($class, $path) = @_;
441 # my $id = '0/' . $class->btrfs_get_subvol_id($path);
442 # my $search = qr/^\Q$id\E\s+(\d)+\s+\d+\s+(\d+)\s*$/;
444 # $class->btrfs_cmd(['qgroup', 'show', '--raw', '-rf', '--', $path], sub {
445 # return if defined($size);
446 # if ($_[0] =~ $search) {
447 # ($used, $size) = ($1, $2);
450 # if (!defined($size)) {
451 # # syslog should include more information:
452 # syslog('err', "failed to get subvolume size for: $path (id $id)");
453 # # UI should only see the last path component:
455 # die "failed to get subvolume size for $path\n";
457 # return wantarray ? ($used, $size) : $size;
460 sub volume_size_info
{
461 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
463 my $path = $class->filesystem_path($scfg, $volname);
465 my $format = ($class->parse_volname($volname))[6];
467 if ($format eq 'subvol') {
468 my $ctime = (stat($path))[10];
469 my ($used, $size) = (0, 0);
470 #my ($used, $size) = btrfs_subvol_quota($class, $path); # uses wantarray
471 return wantarray ?
($size, 'subvol', $used, undef, $ctime) : 1;
474 return PVE
::Storage
::Plugin
::file_size_info
($path, $timeout);
478 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
480 my $format = ($class->parse_volname($volname))[6];
481 if ($format eq 'subvol') {
482 my $path = $class->filesystem_path($scfg, $volname);
483 my $id = '0/' . $class->btrfs_get_subvol_id($path);
484 $class->btrfs_cmd(['qgroup', 'limit', '--', "${size}k", "0/$id", $path]);
488 return PVE
::Storage
::Plugin
::volume_resize
(@_);
491 sub volume_snapshot
{
492 my ($class, $scfg, $storeid, $volname, $snap) = @_;
494 my ($name, $vmid, $format) = ($class->parse_volname($volname))[1,2,6];
495 if ($format ne 'subvol' && $format ne 'raw') {
496 return PVE
::Storage
::Plugin
::volume_snapshot
(@_);
499 my $path = $class->filesystem_path($scfg, $volname);
500 my $snap_path = $class->filesystem_path($scfg, $volname, $snap);
502 if ($format eq 'raw') {
503 $path = raw_file_to_subvol
($path);
504 $snap_path = raw_file_to_subvol
($snap_path);
507 my $snapshot_dir = $class->get_subdir($scfg, 'images') . "/$vmid";
508 mkpath
$snapshot_dir;
510 $class->btrfs_cmd(['subvolume', 'snapshot', '-r', '--', $path, $snap_path]);
514 sub volume_rollback_is_possible
{
515 my ($class, $scfg, $storeid, $volname, $snap) = @_;
520 sub volume_snapshot_rollback
{
521 my ($class, $scfg, $storeid, $volname, $snap) = @_;
523 my ($name, $format) = ($class->parse_volname($volname))[1,6];
525 if ($format ne 'subvol' && $format ne 'raw') {
526 return PVE
::Storage
::Plugin
::volume_snapshot_rollback
(@_);
529 my $path = $class->filesystem_path($scfg, $volname);
530 my $snap_path = $class->filesystem_path($scfg, $volname, $snap);
532 if ($format eq 'raw') {
533 $path = raw_file_to_subvol
($path);
534 $snap_path = raw_file_to_subvol
($snap_path);
537 # Simple version would be:
540 # on error rename temp back
541 # But for atomicity in case the rename after create-failure *also* fails, we create the new
542 # subvol first, then use RENAME_EXCHANGE,
543 my $tmp_path = "$path.tmp.$$";
544 $class->btrfs_cmd(['subvolume', 'snapshot', '--', $snap_path, $tmp_path]);
545 # The paths are absolute, so pass -1 as file descriptors.
546 my $ok = PVE
::Tools
::renameat2
(-1, $tmp_path, -1, $path, &PVE
::Tools
::RENAME_EXCHANGE
);
548 eval { $class->btrfs_cmd(['subvolume', 'delete', '--', $tmp_path]) };
549 warn "failed to remove '$tmp_path' subvolume: $@" if $@;
552 die "failed to rotate '$tmp_path' into place at '$path' - $!\n";
558 sub volume_snapshot_delete
{
559 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
561 my ($name, $vmid, $format) = ($class->parse_volname($volname))[1,2,6];
563 if ($format ne 'subvol' && $format ne 'raw') {
564 return PVE
::Storage
::Plugin
::volume_snapshot_delete
(@_);
567 my $path = $class->filesystem_path($scfg, $volname, $snap);
569 if ($format eq 'raw') {
570 $path = raw_file_to_subvol
($path);
573 $class->btrfs_cmd(['subvolume', 'delete', '--', $path]);
578 sub volume_has_feature
{
579 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
583 current
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1 },
584 snap
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1 }
587 base
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1, vmdk
=> 1 },
588 current
=> { raw
=> 1 },
589 snap
=> { raw
=> 1 },
591 template
=> { current
=> { qcow2
=> 1, raw
=> 1, vmdk
=> 1, subvol
=> 1 } },
593 base
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1, vmdk
=> 1 },
594 current
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1, vmdk
=> 1 },
595 snap
=> { qcow2
=> 1, raw
=> 1, subvol
=> 1 },
597 sparseinit
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1 },
598 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1 } },
601 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
602 $class->parse_volname($volname);
608 $key = $isBase ?
'base' : 'current';
611 return 1 if defined($features->{$feature}->{$key}->{$format});
617 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
618 my $imagedir = $class->get_subdir($scfg, 'images');
622 # Copied from Plugin.pm, with file_size_info calls adapted:
623 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
624 # different to in Plugin.pm the regex below also excludes '@' as valid file name
625 next if $fn !~ m
@^(/.+/(\d
+)/([^/\
@.]+(?
:\
.(qcow2
|vmdk
|subvol
))?
))$@;
632 next if !$vollist && defined($vmid) && ($owner ne $vmid);
634 my $volid = "$storeid:$owner/$name";
635 my ($size, $format, $used, $parent, $ctime);
639 ($size, $format, $used, $parent, $ctime) = PVE
::Storage
::Plugin
::file_size_info
("$fn/disk.raw");
640 } elsif ($ext eq 'subvol') {
641 ($used, $size) = (0, 0);
642 #($used, $size) = btrfs_subvol_quota($class, $fn);
645 ($size, $format, $used, $parent, $ctime) = PVE
::Storage
::Plugin
::file_size_info
($fn);
647 next if !($format && defined($size));
650 next if ! grep { $_ eq $volid } @$vollist;
654 volid
=> $volid, format
=> $format,
655 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent,
658 $info->{ctime
} = $ctime if $ctime;
666 sub volume_export_formats
{
667 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
669 # We can do whatever `DirPlugin` can do.
670 my @result = PVE
::Storage
::Plugin
::volume_export_formats
(@_);
672 # `btrfs send` only works on snapshots:
673 return @result if !defined $snapshot;
675 # Incremental stream with snapshots is only supported if the snapshots are listed (new api):
676 return @result if defined($base_snapshot) && $with_snapshots && ref($with_snapshots) ne 'ARRAY';
678 # Otherwise we do also support `with_snapshots`.
680 # Finally, `btrfs send` only works on formats where we actually use btrfs subvolumes:
681 my $format = ($class->parse_volname($volname))[6];
682 return @result if $format ne 'raw' && $format ne 'subvol';
684 return ('btrfs', @result);
687 sub volume_import_formats
{
688 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
690 # Same as export-formats, beware the parameter order:
691 return volume_export_formats
(
715 if ($format ne 'btrfs') {
716 return PVE
::Storage
::Plugin
::volume_export
(@_);
719 die "format 'btrfs' only works on snapshots\n"
720 if !defined $snapshot;
722 die "'btrfs' format in incremental mode requires snapshots to be listed explicitly\n"
723 if defined($base_snapshot) && $with_snapshots && ref($with_snapshots) ne 'ARRAY';
725 my $volume_format = ($class->parse_volname($volname))[6];
727 die "btrfs-sending volumes of type $volume_format ('$volname') is not supported\n"
728 if $volume_format ne 'raw' && $volume_format ne 'subvol';
730 my $path = $class->path($scfg, $volname, $storeid);
732 if ($volume_format eq 'raw') {
733 $path = raw_file_to_subvol
($path);
736 my $cmd = ['btrfs', '-q', 'send', '-e'];
737 if ($base_snapshot) {
738 my $base = $class->path($scfg, $volname, $storeid, $base_snapshot);
739 if ($volume_format eq 'raw') {
740 $base = raw_file_to_subvol
($base);
742 push @$cmd, '-p', $base;
745 if (ref($with_snapshots) eq 'ARRAY') {
746 push @$cmd, (map { "$path\@$_" } ($with_snapshots // [])->@*), $path;
748 dir_glob_foreach
(dirname
($path), $BTRFS_VOL_REGEX, sub {
749 push @$cmd, "$path\@$_[2]" if !(defined($snapshot) && $_[2] eq $snapshot);
752 $path .= "\@$snapshot" if defined($snapshot);
755 run_command
($cmd, output
=> '>&'.fileno($fh));
773 if ($format ne 'btrfs') {
774 return PVE
::Storage
::Plugin
::volume_import
(@_);
777 die "format 'btrfs' only works on snapshots\n"
778 if !defined $snapshot;
780 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $volume_format) =
781 $class->parse_volname($volname);
783 die "btrfs-receiving volumes of type $volume_format ('$volname') is not supported\n"
784 if $volume_format ne 'raw' && $volume_format ne 'subvol';
786 if (defined($base_snapshot)) {
787 my $path = $class->path($scfg, $volname, $storeid, $base_snapshot);
788 die "base snapshot '$base_snapshot' not found - no such directory '$path'\n"
789 if !path_is_subvolume
($path);
792 my $destination = $class->filesystem_path($scfg, $volname);
793 if ($volume_format eq 'raw') {
794 $destination = raw_file_to_subvol
($destination);
797 if (!defined($base_snapshot) && -e
$destination) {
798 die "volume $volname already exists\n" if !$allow_rename;
799 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $volume_format, 1);
802 my $imagedir = $class->get_subdir($scfg, $vtype);
803 $imagedir .= "/$vmid" if $vtype eq 'images';
805 my $tmppath = "$imagedir/recv.$vmid.tmp";
806 mkdir($imagedir); # FIXME: if $scfg->{mkdir};
807 if (!mkdir($tmppath)) {
808 die "temp receive directory already exists at '$tmppath', incomplete concurrent import?\n"
810 die "failed to create temporary receive directory at '$tmppath' - $!\n";
813 my $dh = IO
::Dir-
>new($tmppath)
814 or die "failed to open temporary receive directory '$tmppath' - $!\n";
816 run_command
(['btrfs', '-q', 'receive', '-e', '--', $tmppath], input
=> '<&'.fileno($fh));
818 # Analyze the received subvolumes;
819 my ($diskname, $found_snapshot, @snapshots);
821 while (defined(my $entry = $dh->read)) {
822 next if $entry eq '.' || $entry eq '..';
823 next if $entry !~ /^$BTRFS_VOL_REGEX$/;
824 my ($cur_diskname, $cur_snapshot) = ($1, $2);
826 die "send stream included a non-snapshot subvolume\n"
827 if !defined($cur_snapshot);
829 if (!defined($diskname)) {
830 $diskname = $cur_diskname;
832 die "multiple disks contained in stream ('$diskname' vs '$cur_diskname')\n"
833 if $diskname ne $cur_diskname;
836 if ($cur_snapshot eq $snapshot) {
839 push @snapshots, $cur_snapshot;
843 die "send stream did not contain the expected current snapshot '$snapshot'\n"
846 # Rotate the disk into place, first the current state:
847 # Note that read-only subvolumes cannot be moved into different directories, but for the
848 # "current" state we also want a writable copy, so start with that:
849 $class->btrfs_cmd(['property', 'set', "$tmppath/$diskname\@$snapshot", 'ro', 'false']);
850 PVE
::Tools
::renameat2
(
852 "$tmppath/$diskname\@$snapshot",
855 &PVE
::Tools
::RENAME_NOREPLACE
,
856 ) or die "failed to move received snapshot '$tmppath/$diskname\@$snapshot'"
857 . " into place at '$destination' - $!\n";
859 # Now recreate the actual snapshot:
866 "$destination\@$snapshot",
869 # Now go through the remaining snapshots (if any)
870 foreach my $snap (@snapshots) {
871 $class->btrfs_cmd(['property', 'set', "$tmppath/$diskname\@$snap", 'ro', 'false']);
872 PVE
::Tools
::renameat2
(
874 "$tmppath/$diskname\@$snap",
876 "$destination\@$snap",
877 &PVE
::Tools
::RENAME_NOREPLACE
,
878 ) or die "failed to move received snapshot '$tmppath/$diskname\@$snap'"
879 . " into place at '$destination\@$snap' - $!\n";
880 eval { $class->btrfs_cmd(['property', 'set', "$destination\@$snap", 'ro', 'true']) };
881 warn "failed to make $destination\@$snap read-only - $!\n" if $@;
887 # Cleanup all the received snapshots we did not move into place, so we can remove the temp
891 while (defined(my $entry = $dh->read)) {
892 next if $entry eq '.' || $entry eq '..';
893 eval { $class->btrfs_cmd(['subvolume', 'delete', '--', "$tmppath/$entry"]) };
896 $dh->close; undef $dh;
898 if (!rmdir($tmppath)) {
899 warn "failed to remove temporary directory '$tmppath' - $!\n"
904 # clean up if the directory ended up being empty after an error
909 return "$storeid:$volname";