]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/LvmThinPlugin.pm
lvmthin volume_resize: reuse code from parent class
[pve-storage.git] / PVE / Storage / LvmThinPlugin.pm
CommitLineData
610798bc
DM
1package PVE::Storage::LvmThinPlugin;
2
3use strict;
4use warnings;
15334c83 5use Data::Dumper;
610798bc
DM
6use IO::File;
7use PVE::Tools qw(run_command trim);
8use PVE::Storage::Plugin;
15334c83 9use PVE::Storage::LVMPlugin;
610798bc
DM
10use PVE::JSONSchema qw(get_standard_option);
11
12# see: man lvmthin
15334c83
DM
13# lvcreate -n ThinDataLV -L LargeSize VG
14# lvconvert --type thin-pool VG/ThinDataLV
15# lvcreate -n pvepool -L 20G pve
16# lvconvert --type thin-pool pve/pvepool
610798bc
DM
17
18use base qw(PVE::Storage::LVMPlugin);
19
20sub type {
21 return 'lvmthin';
22}
23
24sub plugindata {
25 return {
26 content => [ {images => 1, rootdir => 1}, { images => 1, rootdir => 1}],
27 };
28}
29
30sub properties {
31 return {
32 thinpool => {
33 description => "LVM thin pool LV name.",
34 type => 'string', format => 'pve-storage-vgname',
35 },
36 };
37}
38
39sub options {
40 return {
41 thinpool => { fixed => 1 },
42 vgname => { fixed => 1 },
43 nodes => { optional => 1 },
44 disable => { optional => 1 },
45 content => { optional => 1 },
46 };
47}
48
49sub alloc_image {
50 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
51
52 die "unsupported format '$fmt'" if $fmt ne 'raw';
53
15334c83 54 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
610798bc
DM
55 if $name && $name !~ m/^vm-$vmid-/;
56
15334c83
DM
57 my $vg = $scfg->{vgname};
58
59 if (!$name) {
60 my $lvs = PVE::Storage::LVMPlugin::lvm_list_volumes($scfg->{vgname});
61
62 for (my $i = 1; $i < 100; $i++) {
63 my $tn = "vm-$vmid-disk-$i";
64 if (!defined ($lvs->{$vg}->{$tn})) {
65 $name = $tn;
66 last;
67 }
68 }
69 }
70
71 my $cmd = ['/sbin/lvcreate', '-aly', '-V', "${size}k", '--name', $name,
72 '--thinpool', "$vg/$scfg->{thinpool}" ];
73
74 run_command($cmd, errmsg => "lvcreate '$vg/$name' error");
75
76 return $name;
610798bc
DM
77}
78
79sub free_image {
80 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
81
82 my $vg = $scfg->{vgname};
83 my $cmd = ['/sbin/lvremove', '-f', "$vg/$volname"];
84 run_command($cmd, errmsg => "lvremove '$vg/$volname' error");
85
86 return undef;
87}
88
89sub list_images {
90 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
91
15334c83
DM
92 my $vgname = $scfg->{vgname};
93
94 $cache->{lvs} = PVE::Storage::LVMPlugin::lvm_list_volumes() if !$cache->{lvs};
95
96 my $res = [];
97
98 if (my $dat = $cache->{lvs}->{$vgname}) {
99
100 foreach my $volname (keys %$dat) {
101
102 next if $volname !~ m/^vm-(\d+)-/;
103 my $owner = $1;
104
105 my $info = $dat->{$volname};
106
107 next if $info->{lv_type} ne 'V';
108
109 next if $info->{pool_lv} ne $scfg->{thinpool};
110
111 my $volid = "$storeid:$volname";
112
113 if ($vollist) {
114 my $found = grep { $_ eq $volid } @$vollist;
115 next if !$found;
116 } else {
117 next if defined($vmid) && ($owner ne $vmid);
118 }
119
120 push @$res, {
121 volid => $volid, format => 'raw', size => $info->{lv_size}, vmid => $owner,
122 };
123 }
124 }
125
126 return $res;
610798bc
DM
127}
128
129sub status {
130 my ($class, $storeid, $scfg, $cache) = @_;
131
132 my $lvname = "$scfg->{vgname}/$scfg->{thinpool}";
15334c83
DM
133
134 $cache->{lvs} = PVE::Storage::LVMPlugin::lvm_list_volumes() if !$cache->{lvs};
135
136 my $lvs = $cache->{lvs};
137
138 return undef if !$lvs->{$scfg->{vgname}};
139
140 my $info = $lvs->{$scfg->{vgname}}->{$scfg->{thinpool}};
141
142 return undef if !$info;
143
144 return undef if $info->{lv_type} ne 't';
145
146 return ($info->{lv_size}, $info->{lv_size} - $info->{used}, $info->{used}, 1) if $info->{lv_size};
610798bc
DM
147
148 return undef;
149}
150
151sub activate_volume {
152 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
15334c83
DM
153
154 # do nothing
610798bc
DM
155}
156
157sub deactivate_volume {
158 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
15334c83
DM
159
160 # do nothing
610798bc
DM
161}
162
a9f0c6c4 163# sub volume_resize {} reuse code from parent class
610798bc
DM
164
165sub volume_has_feature {
166 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
167
168 my $features = {
169 copy => { base => 1, current => 1},
170 };
171
172 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
173 $class->parse_volname($volname);
174
175 my $key = undef;
176 if($snapname){
177 $key = 'snap';
178 }else{
179 $key = $isBase ? 'base' : 'current';
180 }
181 return 1 if $features->{$feature}->{$key};
182
183 return undef;
184}
185
1861;