]>
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 $vg = $scfg->{vgname
};
60 my $lvs = PVE
::Storage
::LVMPlugin
::lvm_list_volumes
($scfg->{vgname
});
62 for (my $i = 1; $i < 100; $i++) {
63 my $tn = "vm-$vmid-disk-$i";
64 if (!defined ($lvs->{$vg}->{$tn})) {
71 my $cmd = ['/sbin/lvcreate', '-aly', '-V', "${size}k", '--name', $name,
72 '--thinpool', "$vg/$scfg->{thinpool}" ];
74 run_command
($cmd, errmsg
=> "lvcreate '$vg/$name' error");
80 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
82 my $vg = $scfg->{vgname
};
84 my $lvs = PVE
::Storage
::LVMPlugin
::lvm_list_volumes
($vg);
86 if (my $dat = $lvs->{$scfg->{vgname
}}) {
88 # remove all volume snapshots first
89 foreach my $lv (keys %$dat) {
90 next if $lv !~ m/^snap_${volname}_(\w+)$/;
91 my $cmd = ['/sbin/lvremove', '-f', "$vg/$lv"];
92 run_command
($cmd, errmsg
=> "lvremove snapshot '$vg/$lv' error");
95 # finally remove original (if exists)
96 if ($dat->{$volname}) {
97 my $cmd = ['/sbin/lvremove', '-f', "$vg/$volname"];
98 run_command
($cmd, errmsg
=> "lvremove '$vg/$volname' error");
106 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
108 my $vgname = $scfg->{vgname
};
110 $cache->{lvs
} = PVE
::Storage
::LVMPlugin
::lvm_list_volumes
() if !$cache->{lvs
};
114 if (my $dat = $cache->{lvs
}->{$vgname}) {
116 foreach my $volname (keys %$dat) {
118 next if $volname !~ m/^vm-(\d+)-/;
121 my $info = $dat->{$volname};
123 next if $info->{lv_type
} ne 'V';
125 next if $info->{pool_lv
} ne $scfg->{thinpool
};
127 my $volid = "$storeid:$volname";
130 my $found = grep { $_ eq $volid } @$vollist;
133 next if defined($vmid) && ($owner ne $vmid);
137 volid
=> $volid, format
=> 'raw', size
=> $info->{lv_size
}, vmid
=> $owner,
146 my ($class, $storeid, $scfg, $cache) = @_;
148 my $lvname = "$scfg->{vgname}/$scfg->{thinpool}";
150 $cache->{lvs
} = PVE
::Storage
::LVMPlugin
::lvm_list_volumes
() if !$cache->{lvs
};
152 my $lvs = $cache->{lvs
};
154 return undef if !$lvs->{$scfg->{vgname
}};
156 my $info = $lvs->{$scfg->{vgname
}}->{$scfg->{thinpool
}};
158 return undef if !$info;
160 return undef if $info->{lv_type
} ne 't';
162 return ($info->{lv_size
}, $info->{lv_size
} - $info->{used
}, $info->{used
}, 1) if $info->{lv_size
};
167 sub activate_volume
{
168 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
170 my $vg = $scfg->{vgname
};
172 # only snapshot volumes needs activation
174 my $snapvol = "snap_${volname}_$snapname";
175 my $cmd = ['/sbin/lvchange', '-ay', '-K', "$vg/$snapvol"];
176 run_command
($cmd, errmsg
=> "activate_volume '$vg/$snapvol' error");
178 # other volumes are active by default
182 sub deactivate_volume
{
183 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
185 my $vg = $scfg->{vgname
};
187 # we only deactivate snapshot volumes
189 my $snapvol = "snap_${volname}_$snapname";
190 my $cmd = ['/sbin/lvchange', '-an', "$vg/$snapvol"];
191 run_command
($cmd, errmsg
=> "deactivate_volume '$vg/$snapvol' error");
193 # other volumes are kept active
197 # sub volume_resize {} reuse code from parent class
199 sub volume_snapshot
{
200 my ($class, $scfg, $storeid, $volname, $snap) = @_;
202 my $vg = $scfg->{vgname
};
203 my $snapvol = "snap_${volname}_$snap";
205 my $cmd = ['/sbin/lvcreate', '-n', $snapvol, '-pr', '-s', "$vg/$volname"];
206 run_command
($cmd, errmsg
=> "lvcreate snapshot '$vg/$snapvol' error");
210 sub volume_snapshot_rollback
{
211 my ($class, $scfg, $storeid, $volname, $snap) = @_;
213 my $vg = $scfg->{vgname
};
214 my $snapvol = "snap_${volname}_$snap";
216 my $cmd = ['/sbin/lvremove', '-f', "$vg/$volname"];
217 run_command
($cmd, errmsg
=> "lvremove '$vg/$volname' error");
219 $cmd = ['/sbin/lvcreate', '-kn', '-n', $volname, '-s', "$vg/$snapvol"];
220 run_command
($cmd, errmsg
=> "lvm rollback '$vg/$snapvol' error");
223 sub volume_snapshot_delete
{
224 my ($class, $scfg, $storeid, $volname, $snap) = @_;
226 my $vg = $scfg->{vgname
};
227 my $snapvol = "snap_${volname}_$snap";
229 my $cmd = ['/sbin/lvremove', '-f', "$vg/$snapvol"];
230 run_command
($cmd, errmsg
=> "lvremove snapshot '$vg/$snapvol' error");
233 sub volume_has_feature
{
234 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
237 snapshot
=> { current
=> 1 },
238 copy
=> { base
=> 1, current
=> 1},
241 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
242 $class->parse_volname($volname);
248 $key = $isBase ?
'base' : 'current';
250 return 1 if $features->{$feature}->{$key};