]>
Commit | Line | Data |
---|---|---|
0ea9f384 DC |
1 | package PVE::API2::Disks::LVMThin; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use PVE::Storage::LvmThinPlugin; | |
7 | use PVE::Diskmanage; | |
8 | use PVE::JSONSchema qw(get_standard_option); | |
9 | use PVE::API2::Storage::Config; | |
10 | use PVE::Storage; | |
11 | use PVE::Tools qw(run_command lock_file); | |
12 | ||
13 | use PVE::RPCEnvironment; | |
14 | use PVE::RESTHandler; | |
15 | ||
16 | use 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 | }, | |
43 | lv_size => { | |
44 | type => 'integer', | |
45 | description => 'The size of the thinpool in bytes.', | |
46 | }, | |
47 | used => { | |
48 | type => 'integer', | |
49 | description => 'The used bytes of the thinpool.', | |
50 | }, | |
51 | metadata_size => { | |
52 | type => 'integer', | |
53 | description => 'The size of the metadata lv in bytes.', | |
54 | }, | |
55 | metadata_used => { | |
56 | type => 'integer', | |
57 | description => 'The used bytes of the metadata lv.', | |
58 | }, | |
59 | }, | |
60 | }, | |
61 | }, | |
62 | code => sub { | |
63 | my ($param) = @_; | |
64 | return PVE::Storage::LvmThinPlugin::list_thinpools(undef); | |
65 | }}); | |
66 | ||
67 | __PACKAGE__->register_method ({ | |
68 | name => 'create', | |
69 | path => '', | |
70 | method => 'POST', | |
71 | proxyto => 'node', | |
72 | protected => 1, | |
73 | permissions => { | |
74 | check => ['perm', '/', ['Sys.Modify', 'Datastore.Allocate']], | |
75 | }, | |
76 | description => "Create an LVM thinpool", | |
77 | parameters => { | |
78 | additionalProperties => 0, | |
79 | properties => { | |
80 | node => get_standard_option('pve-node'), | |
81 | name => get_standard_option('pve-storage-id'), | |
82 | device => { | |
83 | type => 'string', | |
84 | description => 'The block device you want to create the thinpool on.', | |
85 | }, | |
86 | add_storage => { | |
87 | description => "Configure storage using the thinpool.", | |
88 | type => 'boolean', | |
89 | optional => 1, | |
90 | default => 0, | |
91 | }, | |
92 | }, | |
93 | }, | |
94 | returns => { type => 'string' }, | |
95 | code => sub { | |
96 | my ($param) = @_; | |
97 | ||
98 | my $rpcenv = PVE::RPCEnvironment::get(); | |
99 | my $user = $rpcenv->get_user(); | |
100 | ||
101 | my $name = $param->{name}; | |
102 | my $dev = $param->{device}; | |
103 | my $node = $param->{node}; | |
104 | ||
105 | $dev = PVE::Diskmanage::verify_blockdev_path($dev); | |
0370861c | 106 | PVE::Diskmanage::assert_disk_unused($dev); |
9280153e | 107 | PVE::Storage::assert_sid_unused($name) if $param->{add_storage}; |
0ea9f384 DC |
108 | |
109 | my $worker = sub { | |
e39e8ee2 | 110 | PVE::Diskmanage::locked_disk_action(sub { |
0ea9f384 | 111 | PVE::Storage::LVMPlugin::lvm_create_volume_group($dev, $name); |
3ea55f05 TM |
112 | my $pv = PVE::Storage::LVMPlugin::lvm_pv_info($dev); |
113 | # keep some free space just in case | |
114 | my $datasize = $pv->{size} - 128*1024; | |
115 | # default to 1% for metadata | |
116 | my $metadatasize = $datasize/100; | |
117 | # but at least 1G, as recommended in lvmthin man | |
118 | $metadatasize = 1024*1024 if $metadatasize < 1024*1024; | |
119 | # but at most 16G, which is the current lvm max | |
120 | $metadatasize = 16*1024*1024 if $metadatasize > 16*1024*1024; | |
121 | # shrink data by needed amount for metadata | |
122 | $datasize -= 2*$metadatasize; | |
0ea9f384 | 123 | |
3ea55f05 TM |
124 | run_command([ |
125 | '/sbin/lvcreate', | |
126 | '--type', 'thin-pool', | |
127 | "-L${datasize}K", | |
128 | '--poolmetadatasize', "${metadatasize}K", | |
129 | '-n', $name, | |
130 | $name | |
131 | ]); | |
0ea9f384 DC |
132 | |
133 | if ($param->{add_storage}) { | |
134 | my $storage_params = { | |
135 | type => 'lvmthin', | |
136 | vgname => $name, | |
137 | thinpool => $name, | |
138 | storage => $name, | |
139 | content => 'rootdir,images', | |
140 | nodes => $node, | |
141 | }; | |
142 | ||
143 | PVE::API2::Storage::Config->create($storage_params); | |
144 | } | |
145 | }); | |
0ea9f384 DC |
146 | }; |
147 | ||
5e35281d | 148 | return $rpcenv->fork_worker('lvmthincreate', $name, $user, $worker); |
0ea9f384 DC |
149 | }}); |
150 | ||
151 | 1; |