1 package PVE
::Storage
::RBDPlugin
;
11 use PVE
::JSONSchema
qw(get_standard_option);
14 use PVE
::Storage
::Plugin
;
15 use PVE
::Tools
qw(run_command trim);
17 use base
qw(PVE::Storage::Plugin);
19 my $get_parent_image_name = sub {
21 return undef if !$parent;
22 return $parent->{image
} . "@" . $parent->{snapshot
};
26 my ($scfg, $volume) = @_;
27 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
28 my $namespace = $scfg->{namespace
};
30 return "${pool}/${namespace}/${volume}" if defined($namespace);
31 return "${pool}/${volume}";
35 my ($binary, $scfg, $storeid, $op, @options) = @_;
37 my $cmd_option = PVE
::CephConfig
::ceph_connect_option
($scfg, $storeid);
38 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
40 my $cmd = [$binary, '-p', $pool];
42 # some subcommands will fail if the --namespace parameter is present
43 my $no_namespace_parameter = {
47 push @$cmd, '--namespace', $scfg->{namespace
}
48 if ($scfg->{namespace
} && !$no_namespace_parameter->{$op});
50 push @$cmd, '-c', $cmd_option->{ceph_conf
} if ($cmd_option->{ceph_conf
});
51 push @$cmd, '-m', $cmd_option->{mon_host
} if ($cmd_option->{mon_host
});
52 push @$cmd, '--auth_supported', $cmd_option->{auth_supported
} if ($cmd_option->{auth_supported
});
53 push @$cmd, '-n', "client.$cmd_option->{userid}" if ($cmd_option->{userid
});
54 push @$cmd, '--keyring', $cmd_option->{keyring
} if ($cmd_option->{keyring
});
58 push @$cmd, @options if scalar(@options);
64 my ($scfg, $storeid, $op, @options) = @_;
66 return $build_cmd->('/usr/bin/rbd', $scfg, $storeid, $op, @options);
70 my ($scfg, $storeid, $op, @options) = @_;
72 return $build_cmd->('/usr/bin/rados', $scfg, $storeid, $op, @options);
75 my $librados_connect = sub {
76 my ($scfg, $storeid, $options) = @_;
78 my $librados_config = PVE
::CephConfig
::ceph_connect_option
($scfg, $storeid);
80 my $rados = PVE
::RADOS-
>new(%$librados_config);
85 # needed for volumes created using ceph jewel (or higher)
86 my $krbd_feature_update = sub {
87 my ($scfg, $storeid, $name) = @_;
89 my (@disable, @enable);
90 my ($kmajor, $kminor) = PVE
::ProcFSTools
::kernel_version
();
92 if ($kmajor > 5 || $kmajor == 5 && $kminor >= 3) {
93 # 'deep-flatten' can only be disabled, not enabled after image creation
94 push @enable, 'fast-diff', 'object-map';
96 push @disable, 'fast-diff', 'object-map', 'deep-flatten';
100 push @enable, 'exclusive-lock';
102 push @disable, 'exclusive-lock';
105 my $active_features_list = (rbd_volume_info
($scfg, $storeid, $name))[4];
106 my $active_features = { map { $_ => 1 } @$active_features_list };
108 my $to_disable = join(',', grep { $active_features->{$_} } @disable);
109 my $to_enable = join(',', grep { !$active_features->{$_} } @enable );
112 print "disable RBD image features this kernel RBD drivers is not compatible with: $to_disable\n";
113 my $cmd = $rbd_cmd->($scfg, $storeid, 'feature', 'disable', $name, $to_disable);
116 errmsg
=> "could not disable krbd-incompatible image features '$to_disable' for rbd image: $name",
120 print "enable RBD image features this kernel RBD drivers supports: $to_enable\n";
122 my $cmd = $rbd_cmd->($scfg, $storeid, 'feature', 'enable', $name, $to_enable);
125 errmsg
=> "could not enable krbd-compatible image features '$to_enable' for rbd image: $name",
132 sub run_rbd_command
{
133 my ($cmd, %args) = @_;
136 my $errmsg = $args{errmsg
} . ": " || "";
137 if (!exists($args{errfunc
})) {
138 # ' error: 2014-02-06 11:51:59.839135 7f09f94d0760 -1 librbd: snap_unprotect: can't unprotect;
139 # at least 1 child(ren) in pool cephstor1
140 $args{errfunc
} = sub {
142 if ($line =~ m/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [0-9a-f]+ [\-\d]+ librbd: (.*)$/) {
147 print STDERR
$lasterr;
152 eval { run_command
($cmd, %args); };
154 die $errmsg . $lasterr if length($lasterr);
162 my ($scfg, $storeid) = @_;
164 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls', '-l', '--format', 'json');
165 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
166 $pool .= "/$scfg->{namespace}" if defined($scfg->{namespace
});
169 my $parser = sub { $raw .= shift };
172 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
176 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
181 } elsif ($raw =~ m/^(\[.*\])$/s) { # untaint
182 $result = JSON
::decode_json
($1);
184 die "got unexpected data from rbd ls: '$raw'\n";
189 foreach my $el (@$result) {
190 next if defined($el->{snapshot
});
192 my $image = $el->{image
};
194 my ($owner) = $image =~ m/^(?:vm|base)-(\d+)-/;
195 next if !defined($owner);
197 $list->{$pool}->{$image} = {
200 parent
=> $get_parent_image_name->($el->{parent
}),
209 my ($scfg, $storeid, $name) = @_;
211 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'ls', $name, '--format', 'json');
214 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> sub { $raw .= shift; });
217 if ($raw =~ m/^(\[.*\])$/s) { # untaint
218 $list = eval { JSON
::decode_json
($1) };
219 die "invalid JSON output from 'rbd snap ls $name': $@\n" if $@;
221 die "got unexpected data from 'rbd snap ls $name': '$raw'\n";
224 $list = [] if !defined($list);
227 foreach my $el (@$list) {
228 my $snap = $el->{name
};
229 my $protected = defined($el->{protected
}) && $el->{protected
} eq "true" ?
1 : undef;
232 id
=> $el->{id
} // undef,
233 size
=> $el->{size
} // 0,
234 protected
=> $protected,
240 sub rbd_volume_info
{
241 my ($scfg, $storeid, $volname, $snap) = @_;
245 my @options = ('info', $volname, '--format', 'json');
247 push @options, '--snap', $snap;
250 $cmd = &$rbd_cmd($scfg, $storeid, @options);
253 my $parser = sub { $raw .= shift };
255 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
260 } elsif ($raw =~ m/^(\{.*\})$/s) { # untaint
261 $volume = JSON
::decode_json
($1);
263 die "got unexpected data from rbd info: '$raw'\n";
266 $volume->{parent
} = $get_parent_image_name->($volume->{parent
});
267 $volume->{protected
} = defined($volume->{protected
}) && $volume->{protected
} eq "true" ?
1 : undef;
269 return $volume->@{qw(size parent format protected features)};
280 content
=> [ {images
=> 1, rootdir
=> 1}, { images
=> 1 }],
287 description
=> "IP addresses of monitors (for external clusters).",
288 type
=> 'string', format
=> 'pve-storage-portal-dns-list',
291 description
=> "Pool.",
295 description
=> "RBD Namespace.",
299 description
=> "RBD Id.",
303 description
=> "Authsupported.",
307 description
=> "Always access rbd through krbd kernel module.",
315 nodes
=> { optional
=> 1 },
316 disable
=> { optional
=> 1 },
317 monhost
=> { optional
=> 1},
318 pool
=> { optional
=> 1 },
319 namespace
=> { optional
=> 1 },
320 username
=> { optional
=> 1 },
321 content
=> { optional
=> 1 },
322 krbd
=> { optional
=> 1 },
323 bwlimit
=> { optional
=> 1 },
327 # Storage implementation
330 my ($class, $storeid, $scfg, %param) = @_;
332 return if defined($scfg->{monhost
}); # nothing to do if not pve managed ceph
334 PVE
::CephConfig
::ceph_create_keyfile
($scfg->{type
}, $storeid);
340 my ($class, $storeid, $scfg) = @_;
342 return if defined($scfg->{monhost
}); # nothing to do if not pve managed ceph
344 PVE
::CephConfig
::ceph_remove_keyfile
($scfg->{type
}, $storeid);
350 my ($class, $volname) = @_;
352 if ($volname =~ m/^((base-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
353 return ('images', $4, $7, $2, $3, $5, 'raw');
356 die "unable to parse rbd volume name '$volname'\n";
360 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
362 my $cmd_option = PVE
::CephConfig
::ceph_connect_option
($scfg, $storeid);
363 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
364 $name .= '@'.$snapname if $snapname;
366 my $rbd_path = get_rbd_path
($scfg, $name);
367 return ("/dev/rbd/${rbd_path}", $vmid, $vtype) if $scfg->{krbd
};
369 my $path = "rbd:${rbd_path}";
371 $path .= ":conf=$cmd_option->{ceph_conf}" if $cmd_option->{ceph_conf
};
372 if (defined($scfg->{monhost
})) {
373 my $monhost = PVE
::CephConfig
::hostlist
($scfg->{monhost
}, ';');
374 $monhost =~ s/:/\\:/g;
375 $path .= ":mon_host=$monhost";
376 $path .= ":auth_supported=$cmd_option->{auth_supported}";
379 $path .= ":id=$cmd_option->{userid}:keyring=$cmd_option->{keyring}" if ($cmd_option->{keyring
});
381 return ($path, $vmid, $vtype);
384 sub find_free_diskname
{
385 my ($class, $storeid, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_;
387 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls');
393 if ($line =~ m/^(.*)$/) { # untaint
394 push @$disk_list, $1;
399 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
403 die $err if $err && $err !~ m/doesn't contain rbd images/;
405 return PVE
::Storage
::Plugin
::get_next_vm_diskname
($disk_list, $storeid, $vmid, undef, $scfg);
409 my ($class, $storeid, $scfg, $volname) = @_;
411 my $snap = '__base__';
413 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
414 $class->parse_volname($volname);
416 die "create_base not possible with base image\n" if $isBase;
418 my ($size, $parent, $format, undef) = rbd_volume_info
($scfg, $storeid, $name);
419 die "rbd volume info on '$name' failed\n" if !($size);
421 die "rbd image must be at format V2" if $format ne "2";
423 die "volname '$volname' contains wrong information about parent $parent $basename\n"
424 if $basename && (!$parent || $parent ne $basename."@".$snap);
427 $newname =~ s/^vm-/base-/;
429 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
435 get_rbd_path
($scfg, $name),
436 get_rbd_path
($scfg, $newname),
438 run_rbd_command
($cmd, errmsg
=> "rbd rename '$name' error");
440 my $running = undef; #fixme : is create_base always offline ?
442 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
444 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $newname, $snap);
447 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
448 run_rbd_command
($cmd, errmsg
=> "rbd protect $newname snap '$snap' error");
456 my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_;
458 my $snap = '__base__';
459 $snap = $snapname if length $snapname;
461 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
462 $class->parse_volname($volname);
464 die "$volname is not a base image and snapname is not provided\n"
465 if !$isBase && !length($snapname);
467 my $name = $class->find_free_diskname($storeid, $scfg, $vmid);
469 warn "clone $volname: $basename snapname $snap to $name\n";
471 if (length($snapname)) {
472 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $volname, $snapname);
475 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
476 run_rbd_command
($cmd, errmsg
=> "rbd protect $volname snap $snapname error");
480 my $newvol = "$basename/$name";
481 $newvol = $name if length($snapname);
487 get_rbd_path
($scfg, $basename),
490 get_rbd_path
($scfg, $name),
493 run_rbd_command
($cmd, errmsg
=> "rbd clone '$basename' error");
499 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
502 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
503 if $name && $name !~ m/^vm-$vmid-/;
505 $name = $class->find_free_diskname($storeid, $scfg, $vmid) if !$name;
507 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name);
508 run_rbd_command
($cmd, errmsg
=> "rbd create $name' error");
514 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
516 my ($vtype, $name, $vmid, undef, undef, undef) =
517 $class->parse_volname($volname);
520 my $snaps = rbd_ls_snap
($scfg, $storeid, $name);
521 foreach my $snap (keys %$snaps) {
522 if ($snaps->{$snap}->{protected
}) {
523 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
524 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
528 $class->deactivate_volume($storeid, $scfg, $volname);
530 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name);
531 run_rbd_command
($cmd, errmsg
=> "rbd snap purge '$volname' error");
533 $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name);
534 run_rbd_command
($cmd, errmsg
=> "rbd rm '$volname' error");
540 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
542 $cache->{rbd
} = rbd_ls
($scfg, $storeid) if !$cache->{rbd
};
543 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
544 $pool .= "/$scfg->{namespace}" if defined($scfg->{namespace
});
548 if (my $dat = $cache->{rbd
}->{$pool}) {
549 foreach my $image (keys %$dat) {
551 my $info = $dat->{$image};
553 my $volname = $info->{name
};
554 my $parent = $info->{parent
};
555 my $owner = $info->{vmid
};
557 if ($parent && $parent =~ m/^(base-\d+-\S+)\@__base__$/) {
558 $info->{volid
} = "$storeid:$1/$volname";
560 $info->{volid
} = "$storeid:$volname";
564 my $found = grep { $_ eq $info->{volid
} } @$vollist;
567 next if defined ($vmid) && ($owner ne $vmid);
570 $info->{format
} = 'raw';
580 my ($class, $storeid, $scfg, $cache) = @_;
583 my $rados = &$librados_connect($scfg, $storeid);
584 my $df = $rados->mon_command({ prefix
=> 'df', format
=> 'json' });
586 my ($d) = grep { $_->{name
} eq $scfg->{pool
} } @{$df->{pools
}};
588 # max_avail -> max available space for data w/o replication in the pool
589 # bytes_used -> data w/o replication in the pool
590 my $free = $d->{stats
}->{max_avail
};
591 my $used = $d->{stats
}->{stored
} // $d->{stats
}->{bytes_used
};
592 my $total = $used + $free;
595 return ($total, $free, $used, $active);
598 sub activate_storage
{
599 my ($class, $storeid, $scfg, $cache) = @_;
603 sub deactivate_storage
{
604 my ($class, $storeid, $scfg, $cache) = @_;
608 my $get_kernel_device_name = sub {
609 my ($scfg, $name) = @_;
610 return "/dev/rbd/" . get_rbd_path
($scfg, $name);
614 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
616 my ($vtype, $img_name, $vmid) = $class->parse_volname($volname);
618 my $name = $img_name;
619 $name .= '@'.$snapname if $snapname;
621 my $kerneldev = $get_kernel_device_name->($scfg, $name);
623 return $kerneldev if -b
$kerneldev; # already mapped
625 # features can only be enabled/disabled for image, not for snapshot!
626 $krbd_feature_update->($scfg, $storeid, $img_name);
628 my $cmd = &$rbd_cmd($scfg, $storeid, 'map', $name);
629 run_rbd_command
($cmd, errmsg
=> "can't map rbd volume $name");
635 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
637 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
638 $name .= '@'.$snapname if $snapname;
640 my $kerneldev = $get_kernel_device_name->($scfg, $name);
643 my $cmd = &$rbd_cmd($scfg, $storeid, 'unmap', $kerneldev);
644 run_rbd_command
($cmd, errmsg
=> "can't unmap rbd device $kerneldev");
650 sub activate_volume
{
651 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
653 $class->map_volume($storeid, $scfg, $volname, $snapname) if $scfg->{krbd
};
658 sub deactivate_volume
{
659 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
661 $class->unmap_volume($storeid, $scfg, $volname, $snapname);
666 sub volume_size_info
{
667 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
669 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
670 my ($size, undef) = rbd_volume_info
($scfg, $storeid, $name);
675 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
677 return 1 if $running && !$scfg->{krbd
}; # FIXME???
679 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
681 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--allow-shrink', '--size', ($size/1024/1024), $name);
682 run_rbd_command
($cmd, errmsg
=> "rbd resize '$volname' error");
686 sub volume_snapshot
{
687 my ($class, $scfg, $storeid, $volname, $snap) = @_;
689 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
691 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
692 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
696 sub volume_snapshot_rollback
{
697 my ($class, $scfg, $storeid, $volname, $snap) = @_;
699 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
701 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
702 run_rbd_command
($cmd, errmsg
=> "rbd snapshot $volname to '$snap' error");
705 sub volume_snapshot_delete
{
706 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
708 return 1 if $running && !$scfg->{krbd
}; # FIXME: ????
710 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
712 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
714 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
716 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
717 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
720 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
722 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
727 sub volume_snapshot_needs_fsfreeze
{
732 sub volume_has_feature
{
733 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
736 snapshot
=> { current
=> 1, snap
=> 1},
737 clone
=> { base
=> 1, snap
=> 1},
738 template
=> { current
=> 1},
739 copy
=> { base
=> 1, current
=> 1, snap
=> 1},
740 sparseinit
=> { base
=> 1, current
=> 1},
743 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
744 $class->parse_volname($volname);
750 $key = $isBase ?
'base' : 'current';
752 return 1 if $features->{$feature}->{$key};