]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/LvmThinPlugin.pm
1 package PVE
::Storage
::LvmThinPlugin
;
7 use PVE
::Tools
qw(run_command trim);
8 use PVE
::Storage
::Plugin
;
9 use PVE
::Storage
::LVMPlugin
;
10 use PVE
::JSONSchema
qw(get_standard_option);
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
18 use base
qw(PVE::Storage::LVMPlugin);
26 content
=> [ {images
=> 1, rootdir
=> 1}, { images
=> 1, rootdir
=> 1}],
33 description
=> "LVM thin pool LV name.",
34 type
=> 'string', format
=> 'pve-storage-vgname',
41 thinpool
=> { fixed
=> 1 },
42 vgname
=> { fixed
=> 1 },
43 nodes
=> { optional
=> 1 },
44 disable
=> { optional
=> 1 },
45 content
=> { optional
=> 1 },
50 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
52 die "unsupported format '$fmt'" if $fmt ne 'raw';
54 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
55 if $name && $name !~ m/^vm-$vmid-/;
57 my $vgs = PVE
::Storage
::lvm_vgs
();
59 my $vg = $scfg->{vgname
};
61 die "no such volume group '$vg'\n" if !defined ($vgs->{$vg});
64 my $lvs = PVE
::Storage
::LVMPlugin
::lvm_list_volumes
($scfg->{vgname
});
66 for (my $i = 1; $i < 100; $i++) {
67 my $tn = "vm-$vmid-disk-$i";
68 if (!defined ($lvs->{$vg}->{$tn})) {
75 my $cmd = ['/sbin/lvcreate', '-aly', '-V', "${size}k", '--name', $name,
76 '--thinpool', "$vg/$scfg->{thinpool}" ];
78 run_command
($cmd, errmsg
=> "lvcreate '$vg/$name' error");
84 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
86 my $vg = $scfg->{vgname
};
88 my $lvs = PVE
::Storage
::LVMPlugin
::lvm_list_volumes
($vg);
90 if (my $dat = $lvs->{$scfg->{vgname
}}) {
92 # remove all volume snapshots first
93 foreach my $lv (keys %$dat) {
94 next if $lv !~ m/^snap_${volname}_(\w+)$/;
95 my $cmd = ['/sbin/lvremove', '-f', "$vg/$lv"];
96 run_command
($cmd, errmsg
=> "lvremove snapshot '$vg/$lv' error");
99 # finally remove original (if exists)
100 if ($dat->{$volname}) {
101 my $cmd = ['/sbin/lvremove', '-f', "$vg/$volname"];
102 run_command
($cmd, errmsg
=> "lvremove '$vg/$volname' error");
110 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
112 my $vgname = $scfg->{vgname
};
114 $cache->{lvs
} = PVE
::Storage
::LVMPlugin
::lvm_list_volumes
() if !$cache->{lvs
};
118 if (my $dat = $cache->{lvs
}->{$vgname}) {
120 foreach my $volname (keys %$dat) {
122 next if $volname !~ m/^vm-(\d+)-/;
125 my $info = $dat->{$volname};
127 next if $info->{lv_type
} ne 'V';
129 next if $info->{pool_lv
} ne $scfg->{thinpool
};
131 my $volid = "$storeid:$volname";
134 my $found = grep { $_ eq $volid } @$vollist;
137 next if defined($vmid) && ($owner ne $vmid);
141 volid
=> $volid, format
=> 'raw', size
=> $info->{lv_size
}, vmid
=> $owner,
150 my ($class, $storeid, $scfg, $cache) = @_;
152 my $lvname = "$scfg->{vgname}/$scfg->{thinpool}";
154 $cache->{lvs
} = PVE
::Storage
::LVMPlugin
::lvm_list_volumes
() if !$cache->{lvs
};
156 my $lvs = $cache->{lvs
};
158 return undef if !$lvs->{$scfg->{vgname
}};
160 my $info = $lvs->{$scfg->{vgname
}}->{$scfg->{thinpool
}};
162 return undef if !$info;
164 return undef if $info->{lv_type
} ne 't';
166 return ($info->{lv_size
}, $info->{lv_size
} - $info->{used
}, $info->{used
}, 1) if $info->{lv_size
};
171 sub activate_volume
{
172 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
174 my $vg = $scfg->{vgname
};
176 # only snapshot volumes needs activation
178 my $snapvol = "snap_${volname}_$snapname";
179 my $cmd = ['/sbin/lvchange', '-ay', '-K', "$vg/$snapvol"];
180 run_command
($cmd, errmsg
=> "activate_volume '$vg/$snapvol' error");
182 # other volumes are active by default
186 sub deactivate_volume
{
187 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
189 my $vg = $scfg->{vgname
};
191 # we only deactivate snapshot volumes
193 my $snapvol = "snap_${volname}_$snapname";
194 my $cmd = ['/sbin/lvchange', '-an', "$vg/$snapvol"];
195 run_command
($cmd, errmsg
=> "deactivate_volume '$vg/$snapvol' error");
197 # other volumes are kept active
201 # sub volume_resize {} reuse code from parent class
203 sub volume_snapshot
{
204 my ($class, $scfg, $storeid, $volname, $snap) = @_;
206 my $vg = $scfg->{vgname
};
207 my $snapvol = "snap_${volname}_$snap";
209 my $cmd = ['/sbin/lvcreate', '-n', $snapvol, '-pr', '-s', "$vg/$volname"];
210 run_command
($cmd, errmsg
=> "lvcreate snapshot '$vg/$snapvol' error");
214 sub volume_snapshot_rollback
{
215 my ($class, $scfg, $storeid, $volname, $snap) = @_;
217 my $vg = $scfg->{vgname
};
218 my $snapvol = "snap_${volname}_$snap";
220 my $cmd = ['/sbin/lvremove', '-f', "$vg/$volname"];
221 run_command
($cmd, errmsg
=> "lvremove '$vg/$volname' error");
223 $cmd = ['/sbin/lvcreate', '-kn', '-n', $volname, '-s', "$vg/$snapvol"];
224 run_command
($cmd, errmsg
=> "lvm rollback '$vg/$snapvol' error");
227 sub volume_snapshot_delete
{
228 my ($class, $scfg, $storeid, $volname, $snap) = @_;
230 my $vg = $scfg->{vgname
};
231 my $snapvol = "snap_${volname}_$snap";
233 my $cmd = ['/sbin/lvremove', '-f', "$vg/$snapvol"];
234 run_command
($cmd, errmsg
=> "lvremove snapshot '$vg/$snapvol' error");
237 sub volume_has_feature
{
238 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
241 snapshot
=> { current
=> 1 },
242 copy
=> { base
=> 1, current
=> 1},
245 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
246 $class->parse_volname($volname);
252 $key = $isBase ?
'base' : 'current';
254 return 1 if $features->{$feature}->{$key};