]> git.proxmox.com Git - pve-storage.git/blame - PVE/API2/Disks/LVMThin.pm
api: list thin pools: add volume group to properties
[pve-storage.git] / PVE / API2 / Disks / LVMThin.pm
CommitLineData
0ea9f384
DC
1package PVE::API2::Disks::LVMThin;
2
3use strict;
4use warnings;
5
6use PVE::Storage::LvmThinPlugin;
7use PVE::Diskmanage;
8use PVE::JSONSchema qw(get_standard_option);
9use PVE::API2::Storage::Config;
10use PVE::Storage;
11use PVE::Tools qw(run_command lock_file);
12
13use PVE::RPCEnvironment;
14use PVE::RESTHandler;
15
16use base qw(PVE::RESTHandler);
17
18__PACKAGE__->register_method ({
19 name => 'index',
20 path => '',
21 method => 'GET',
22 proxyto => 'node',
23 protected => 1,
24 permissions => {
25 check => ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any => 1],
26 },
27 description => "List LVM thinpools",
28 parameters => {
29 additionalProperties => 0,
30 properties => {
31 node => get_standard_option('pve-node'),
32 },
33 },
34 returns => {
35 type => 'array',
36 items => {
37 type => 'object',
38 properties => {
39 lv => {
40 type => 'string',
41 description => 'The name of the thinpool.',
42 },
a510449e
FE
43 vg => {
44 type => 'string',
45 description => 'The associated volume group.',
46 },
0ea9f384
DC
47 lv_size => {
48 type => 'integer',
49 description => 'The size of the thinpool in bytes.',
50 },
51 used => {
52 type => 'integer',
53 description => 'The used bytes of the thinpool.',
54 },
55 metadata_size => {
56 type => 'integer',
57 description => 'The size of the metadata lv in bytes.',
58 },
59 metadata_used => {
60 type => 'integer',
61 description => 'The used bytes of the metadata lv.',
62 },
63 },
64 },
65 },
66 code => sub {
67 my ($param) = @_;
68 return PVE::Storage::LvmThinPlugin::list_thinpools(undef);
69 }});
70
71__PACKAGE__->register_method ({
72 name => 'create',
73 path => '',
74 method => 'POST',
75 proxyto => 'node',
76 protected => 1,
77 permissions => {
78 check => ['perm', '/', ['Sys.Modify', 'Datastore.Allocate']],
79 },
80 description => "Create an LVM thinpool",
81 parameters => {
82 additionalProperties => 0,
83 properties => {
84 node => get_standard_option('pve-node'),
85 name => get_standard_option('pve-storage-id'),
86 device => {
87 type => 'string',
88 description => 'The block device you want to create the thinpool on.',
89 },
90 add_storage => {
91 description => "Configure storage using the thinpool.",
92 type => 'boolean',
93 optional => 1,
94 default => 0,
95 },
96 },
97 },
98 returns => { type => 'string' },
99 code => sub {
100 my ($param) = @_;
101
102 my $rpcenv = PVE::RPCEnvironment::get();
103 my $user = $rpcenv->get_user();
104
105 my $name = $param->{name};
106 my $dev = $param->{device};
107 my $node = $param->{node};
108
109 $dev = PVE::Diskmanage::verify_blockdev_path($dev);
0370861c 110 PVE::Diskmanage::assert_disk_unused($dev);
9280153e 111 PVE::Storage::assert_sid_unused($name) if $param->{add_storage};
0ea9f384
DC
112
113 my $worker = sub {
e39e8ee2 114 PVE::Diskmanage::locked_disk_action(sub {
e99bc248
FE
115 PVE::Diskmanage::assert_disk_unused($dev);
116
05d91712
FE
117 if (PVE::Diskmanage::is_partition($dev)) {
118 eval { PVE::Diskmanage::change_parttype($dev, '8E00'); };
119 warn $@ if $@;
120 }
121
0ea9f384 122 PVE::Storage::LVMPlugin::lvm_create_volume_group($dev, $name);
3ea55f05
TM
123 my $pv = PVE::Storage::LVMPlugin::lvm_pv_info($dev);
124 # keep some free space just in case
125 my $datasize = $pv->{size} - 128*1024;
126 # default to 1% for metadata
127 my $metadatasize = $datasize/100;
128 # but at least 1G, as recommended in lvmthin man
129 $metadatasize = 1024*1024 if $metadatasize < 1024*1024;
130 # but at most 16G, which is the current lvm max
131 $metadatasize = 16*1024*1024 if $metadatasize > 16*1024*1024;
132 # shrink data by needed amount for metadata
133 $datasize -= 2*$metadatasize;
0ea9f384 134
3ea55f05
TM
135 run_command([
136 '/sbin/lvcreate',
137 '--type', 'thin-pool',
138 "-L${datasize}K",
139 '--poolmetadatasize', "${metadatasize}K",
140 '-n', $name,
141 $name
142 ]);
0ea9f384 143
21a75847
FE
144 # FIXME: Remove once we depend on systemd >= v249.
145 # Work around udev bug https://github.com/systemd/systemd/issues/18525 to ensure the
146 # udev database is updated.
147 eval { run_command(['udevadm', 'trigger', $dev]); };
148 warn $@ if $@;
149
0ea9f384
DC
150 if ($param->{add_storage}) {
151 my $storage_params = {
152 type => 'lvmthin',
153 vgname => $name,
154 thinpool => $name,
155 storage => $name,
156 content => 'rootdir,images',
157 nodes => $node,
158 };
159
160 PVE::API2::Storage::Config->create($storage_params);
161 }
162 });
0ea9f384
DC
163 };
164
5e35281d 165 return $rpcenv->fork_worker('lvmthincreate', $name, $user, $worker);
0ea9f384
DC
166 }});
167
1681;