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 if (defined(my $namespace = $scfg->{namespace
})) {
43 # some subcommands will fail if the --namespace parameter is present
44 my $no_namespace_parameter = {
47 push @$cmd, '--namespace', "$namespace" if !$no_namespace_parameter->{$op};
49 push @$cmd, '-c', $cmd_option->{ceph_conf
} if ($cmd_option->{ceph_conf
});
50 push @$cmd, '-m', $cmd_option->{mon_host
} if ($cmd_option->{mon_host
});
51 push @$cmd, '--auth_supported', $cmd_option->{auth_supported
} if ($cmd_option->{auth_supported
});
52 push @$cmd, '-n', "client.$cmd_option->{userid}" if ($cmd_option->{userid
});
53 push @$cmd, '--keyring', $cmd_option->{keyring
} if ($cmd_option->{keyring
});
57 push @$cmd, @options if scalar(@options);
63 my ($scfg, $storeid, $op, @options) = @_;
65 return $build_cmd->('/usr/bin/rbd', $scfg, $storeid, $op, @options);
69 my ($scfg, $storeid, $op, @options) = @_;
71 return $build_cmd->('/usr/bin/rados', $scfg, $storeid, $op, @options);
74 my $librados_connect = sub {
75 my ($scfg, $storeid, $options) = @_;
77 my $librados_config = PVE
::CephConfig
::ceph_connect_option
($scfg, $storeid);
79 my $rados = PVE
::RADOS-
>new(%$librados_config);
84 # needed for volumes created using ceph jewel (or higher)
85 my $krbd_feature_update = sub {
86 my ($scfg, $storeid, $name) = @_;
88 my (@disable, @enable);
89 my ($kmajor, $kminor) = PVE
::ProcFSTools
::kernel_version
();
91 if ($kmajor > 5 || $kmajor == 5 && $kminor >= 3) {
92 # 'deep-flatten' can only be disabled, not enabled after image creation
93 push @enable, 'fast-diff', 'object-map';
95 push @disable, 'fast-diff', 'object-map', 'deep-flatten';
99 push @enable, 'exclusive-lock';
101 push @disable, 'exclusive-lock';
104 my $active_features_list = (rbd_volume_info
($scfg, $storeid, $name))[4];
105 my $active_features = { map { $_ => 1 } @$active_features_list };
107 my $to_disable = join(',', grep { $active_features->{$_} } @disable);
108 my $to_enable = join(',', grep { !$active_features->{$_} } @enable );
111 print "disable RBD image features this kernel RBD drivers is not compatible with: $to_disable\n";
112 my $cmd = $rbd_cmd->($scfg, $storeid, 'feature', 'disable', $name, $to_disable);
115 errmsg
=> "could not disable krbd-incompatible image features '$to_disable' for rbd image: $name",
119 print "enable RBD image features this kernel RBD drivers supports: $to_enable\n";
121 my $cmd = $rbd_cmd->($scfg, $storeid, 'feature', 'enable', $name, $to_enable);
124 errmsg
=> "could not enable krbd-compatible image features '$to_enable' for rbd image: $name",
131 sub run_rbd_command
{
132 my ($cmd, %args) = @_;
135 my $errmsg = $args{errmsg
} . ": " || "";
136 if (!exists($args{errfunc
})) {
137 # ' error: 2014-02-06 11:51:59.839135 7f09f94d0760 -1 librbd: snap_unprotect: can't unprotect;
138 # at least 1 child(ren) in pool cephstor1
139 $args{errfunc
} = sub {
141 if ($line =~ m/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [0-9a-f]+ [\-\d]+ librbd: (.*)$/) {
146 print STDERR
$lasterr;
151 eval { run_command
($cmd, %args); };
153 die $errmsg . $lasterr if length($lasterr);
161 my ($scfg, $storeid) = @_;
163 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
164 $pool .= "/$scfg->{namespace}" if defined($scfg->{namespace
});
167 my $parser = sub { $raw .= shift };
169 my $cmd = $rbd_cmd->($scfg, $storeid, 'ls', '-l', '--format', 'json');
171 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
175 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
180 } elsif ($raw =~ m/^(\[.*\])$/s) { # untaint
181 $result = JSON
::decode_json
($1);
183 die "got unexpected data from rbd ls: '$raw'\n";
188 foreach my $el (@$result) {
189 next if defined($el->{snapshot
});
191 my $image = $el->{image
};
193 my ($owner) = $image =~ m/^(?:vm|base)-(\d+)-/;
194 next if !defined($owner);
196 $list->{$pool}->{$image} = {
199 parent
=> $get_parent_image_name->($el->{parent
}),
208 my ($scfg, $storeid, $name) = @_;
210 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'ls', $name, '--format', 'json');
213 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> sub { $raw .= shift; });
216 if ($raw =~ m/^(\[.*\])$/s) { # untaint
217 $list = eval { JSON
::decode_json
($1) };
218 die "invalid JSON output from 'rbd snap ls $name': $@\n" if $@;
220 die "got unexpected data from 'rbd snap ls $name': '$raw'\n";
223 $list = [] if !defined($list);
226 foreach my $el (@$list) {
227 my $snap = $el->{name
};
228 my $protected = defined($el->{protected
}) && $el->{protected
} eq "true" ?
1 : undef;
231 id
=> $el->{id
} // undef,
232 size
=> $el->{size
} // 0,
233 protected
=> $protected,
239 sub rbd_volume_info
{
240 my ($scfg, $storeid, $volname, $snap) = @_;
244 my @options = ('info', $volname, '--format', 'json');
246 push @options, '--snap', $snap;
249 $cmd = $rbd_cmd->($scfg, $storeid, @options);
252 my $parser = sub { $raw .= shift };
254 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
259 } elsif ($raw =~ m/^(\{.*\})$/s) { # untaint
260 $volume = JSON
::decode_json
($1);
262 die "got unexpected data from rbd info: '$raw'\n";
265 $volume->{parent
} = $get_parent_image_name->($volume->{parent
});
266 $volume->{protected
} = defined($volume->{protected
}) && $volume->{protected
} eq "true" ?
1 : undef;
268 return $volume->@{qw(size parent format protected features)};
279 content
=> [ {images
=> 1, rootdir
=> 1}, { images
=> 1 }],
286 description
=> "IP addresses of monitors (for external clusters).",
287 type
=> 'string', format
=> 'pve-storage-portal-dns-list',
290 description
=> "Pool.",
294 description
=> "RBD Namespace.",
298 description
=> "RBD Id.",
302 description
=> "Authsupported.",
306 description
=> "Always access rbd through krbd kernel module.",
314 nodes
=> { optional
=> 1 },
315 disable
=> { optional
=> 1 },
316 monhost
=> { optional
=> 1},
317 pool
=> { optional
=> 1 },
318 namespace
=> { optional
=> 1 },
319 username
=> { optional
=> 1 },
320 content
=> { optional
=> 1 },
321 krbd
=> { optional
=> 1 },
322 bwlimit
=> { optional
=> 1 },
326 # Storage implementation
329 my ($class, $storeid, $scfg, %param) = @_;
331 return if defined($scfg->{monhost
}); # nothing to do if not pve managed ceph
333 PVE
::CephConfig
::ceph_create_keyfile
($scfg->{type
}, $storeid);
339 my ($class, $storeid, $scfg) = @_;
341 return if defined($scfg->{monhost
}); # nothing to do if not pve managed ceph
343 PVE
::CephConfig
::ceph_remove_keyfile
($scfg->{type
}, $storeid);
349 my ($class, $volname) = @_;
351 if ($volname =~ m/^((base-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
352 return ('images', $4, $7, $2, $3, $5, 'raw');
355 die "unable to parse rbd volume name '$volname'\n";
359 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
361 my $cmd_option = PVE
::CephConfig
::ceph_connect_option
($scfg, $storeid);
362 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
363 $name .= '@'.$snapname if $snapname;
365 my $rbd_path = get_rbd_path
($scfg, $name);
366 return ("/dev/rbd/${rbd_path}", $vmid, $vtype) if $scfg->{krbd
};
368 my $path = "rbd:${rbd_path}";
370 $path .= ":conf=$cmd_option->{ceph_conf}" if $cmd_option->{ceph_conf
};
371 if (defined($scfg->{monhost
})) {
372 my $monhost = PVE
::CephConfig
::hostlist
($scfg->{monhost
}, ';');
373 $monhost =~ s/:/\\:/g;
374 $path .= ":mon_host=$monhost";
375 $path .= ":auth_supported=$cmd_option->{auth_supported}";
378 $path .= ":id=$cmd_option->{userid}:keyring=$cmd_option->{keyring}" if ($cmd_option->{keyring
});
380 return ($path, $vmid, $vtype);
383 sub find_free_diskname
{
384 my ($class, $storeid, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_;
386 my $cmd = $rbd_cmd->($scfg, $storeid, 'ls');
392 if ($line =~ m/^(.*)$/) { # untaint
393 push @$disk_list, $1;
398 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
402 die $err if $err && $err !~ m/doesn't contain rbd images/;
404 return PVE
::Storage
::Plugin
::get_next_vm_diskname
($disk_list, $storeid, $vmid, undef, $scfg);
408 my ($class, $storeid, $scfg, $volname) = @_;
410 my $snap = '__base__';
412 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
413 $class->parse_volname($volname);
415 die "create_base not possible with base image\n" if $isBase;
417 my ($size, $parent, $format, undef) = rbd_volume_info
($scfg, $storeid, $name);
418 die "rbd volume info on '$name' failed\n" if !($size);
420 die "rbd image must be at format V2" if $format ne "2";
422 die "volname '$volname' contains wrong information about parent $parent $basename\n"
423 if $basename && (!$parent || $parent ne $basename."@".$snap);
426 $newname =~ s/^vm-/base-/;
428 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
430 my $cmd = $rbd_cmd->(
434 get_rbd_path
($scfg, $name),
435 get_rbd_path
($scfg, $newname),
437 run_rbd_command
($cmd, errmsg
=> "rbd rename '$name' error");
439 my $running = undef; #fixme : is create_base always offline ?
441 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
443 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $newname, $snap);
446 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
447 run_rbd_command
($cmd, errmsg
=> "rbd protect $newname snap '$snap' error");
455 my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_;
457 my $snap = '__base__';
458 $snap = $snapname if length $snapname;
460 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
461 $class->parse_volname($volname);
463 die "$volname is not a base image and snapname is not provided\n"
464 if !$isBase && !length($snapname);
466 my $name = $class->find_free_diskname($storeid, $scfg, $vmid);
468 warn "clone $volname: $basename snapname $snap to $name\n";
470 if (length($snapname)) {
471 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $volname, $snapname);
474 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
475 run_rbd_command
($cmd, errmsg
=> "rbd protect $volname snap $snapname error");
479 my $newvol = "$basename/$name";
480 $newvol = $name if length($snapname);
482 my $cmd = $rbd_cmd->(
486 get_rbd_path
($scfg, $basename),
489 get_rbd_path
($scfg, $name),
492 run_rbd_command
($cmd, errmsg
=> "rbd clone '$basename' error");
498 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
501 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
502 if $name && $name !~ m/^vm-$vmid-/;
504 $name = $class->find_free_diskname($storeid, $scfg, $vmid) if !$name;
506 my $cmd = $rbd_cmd->($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name);
507 run_rbd_command
($cmd, errmsg
=> "rbd create $name' error");
513 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
515 my ($vtype, $name, $vmid, undef, undef, undef) =
516 $class->parse_volname($volname);
519 my $snaps = rbd_ls_snap
($scfg, $storeid, $name);
520 foreach my $snap (keys %$snaps) {
521 if ($snaps->{$snap}->{protected
}) {
522 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
523 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
527 $class->deactivate_volume($storeid, $scfg, $volname);
529 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'purge', $name);
530 run_rbd_command
($cmd, errmsg
=> "rbd snap purge '$volname' error");
532 $cmd = $rbd_cmd->($scfg, $storeid, 'rm', $name);
533 run_rbd_command
($cmd, errmsg
=> "rbd rm '$volname' error");
539 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
541 $cache->{rbd
} = rbd_ls
($scfg, $storeid) if !$cache->{rbd
};
542 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
543 $pool .= "/$scfg->{namespace}" if defined($scfg->{namespace
});
547 if (my $dat = $cache->{rbd
}->{$pool}) {
548 foreach my $image (keys %$dat) {
550 my $info = $dat->{$image};
552 my $volname = $info->{name
};
553 my $parent = $info->{parent
};
554 my $owner = $info->{vmid
};
556 if ($parent && $parent =~ m/^(base-\d+-\S+)\@__base__$/) {
557 $info->{volid
} = "$storeid:$1/$volname";
559 $info->{volid
} = "$storeid:$volname";
563 my $found = grep { $_ eq $info->{volid
} } @$vollist;
566 next if defined ($vmid) && ($owner ne $vmid);
569 $info->{format
} = 'raw';
579 my ($class, $storeid, $scfg, $cache) = @_;
581 my $rados = $librados_connect->($scfg, $storeid);
582 my $df = $rados->mon_command({ prefix
=> 'df', format
=> 'json' });
584 my ($d) = grep { $_->{name
} eq $scfg->{pool
} } @{$df->{pools
}};
586 # max_avail -> max available space for data w/o replication in the pool
587 # bytes_used -> data w/o replication in the pool
588 my $free = $d->{stats
}->{max_avail
};
589 my $used = $d->{stats
}->{stored
} // $d->{stats
}->{bytes_used
};
590 my $total = $used + $free;
593 return ($total, $free, $used, $active);
596 sub activate_storage
{
597 my ($class, $storeid, $scfg, $cache) = @_;
601 sub deactivate_storage
{
602 my ($class, $storeid, $scfg, $cache) = @_;
606 my sub get_kernel_device_path
{
607 my ($scfg, $name) = @_;
608 return "/dev/rbd/" . get_rbd_path
($scfg, $name);
612 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
614 my ($vtype, $img_name, $vmid) = $class->parse_volname($volname);
616 my $name = $img_name;
617 $name .= '@'.$snapname if $snapname;
619 my $kerneldev = get_kernel_device_path
($scfg, $name);
621 return $kerneldev if -b
$kerneldev; # already mapped
623 # features can only be enabled/disabled for image, not for snapshot!
624 $krbd_feature_update->($scfg, $storeid, $img_name);
626 my $cmd = $rbd_cmd->($scfg, $storeid, 'map', $name);
627 run_rbd_command
($cmd, errmsg
=> "can't map rbd volume $name");
633 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
635 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
636 $name .= '@'.$snapname if $snapname;
638 my $kerneldev = get_kernel_device_path
($scfg, $name);
641 my $cmd = $rbd_cmd->($scfg, $storeid, 'unmap', $kerneldev);
642 run_rbd_command
($cmd, errmsg
=> "can't unmap rbd device $kerneldev");
648 sub activate_volume
{
649 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
651 $class->map_volume($storeid, $scfg, $volname, $snapname) if $scfg->{krbd
};
656 sub deactivate_volume
{
657 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
659 $class->unmap_volume($storeid, $scfg, $volname, $snapname);
664 sub volume_size_info
{
665 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
667 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
668 my ($size, undef) = rbd_volume_info
($scfg, $storeid, $name);
673 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
675 return 1 if $running && !$scfg->{krbd
}; # FIXME???
677 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
679 my $cmd = $rbd_cmd->($scfg, $storeid, 'resize', '--allow-shrink', '--size', ($size/1024/1024), $name);
680 run_rbd_command
($cmd, errmsg
=> "rbd resize '$volname' error");
684 sub volume_snapshot
{
685 my ($class, $scfg, $storeid, $volname, $snap) = @_;
687 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
689 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
690 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
694 sub volume_snapshot_rollback
{
695 my ($class, $scfg, $storeid, $volname, $snap) = @_;
697 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
699 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
700 run_rbd_command
($cmd, errmsg
=> "rbd snapshot $volname to '$snap' error");
703 sub volume_snapshot_delete
{
704 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
706 return 1 if $running && !$scfg->{krbd
}; # FIXME: ????
708 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
710 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
712 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
714 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
715 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
718 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
720 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
725 sub volume_snapshot_needs_fsfreeze
{
729 sub volume_has_feature
{
730 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
733 snapshot
=> { current
=> 1, snap
=> 1},
734 clone
=> { base
=> 1, snap
=> 1},
735 template
=> { current
=> 1},
736 copy
=> { base
=> 1, current
=> 1, snap
=> 1},
737 sparseinit
=> { base
=> 1, current
=> 1},
740 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = $class->parse_volname($volname);
746 $key = $isBase ?
'base' : 'current';
748 return 1 if $features->{$feature}->{$key};