]>
Commit | Line | Data |
---|---|---|
c669f42d DM |
1 | package PVE::CLI::pvesm; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use Fcntl ':flock'; | |
7 | use File::Path; | |
8 | ||
9 | use PVE::SafeSyslog; | |
10 | use PVE::Cluster; | |
11 | use PVE::INotify; | |
12 | use PVE::RPCEnvironment; | |
13 | use PVE::Storage; | |
14 | use PVE::API2::Storage::Config; | |
15 | use PVE::API2::Storage::Content; | |
16 | use PVE::API2::Storage::Status; | |
17 | use PVE::API2::Storage::Scan; | |
18 | use PVE::JSONSchema qw(get_standard_option); | |
19 | ||
20 | use PVE::CLIHandler; | |
21 | ||
22 | use base qw(PVE::CLIHandler); | |
23 | ||
24 | my $nodename = PVE::INotify::nodename(); | |
25 | ||
f984732e DM |
26 | sub setup_environment { |
27 | PVE::RPCEnvironment->setup_default_cli_env(); | |
28 | } | |
29 | ||
c669f42d DM |
30 | __PACKAGE__->register_method ({ |
31 | name => 'path', | |
32 | path => 'path', | |
33 | method => 'GET', | |
34 | description => "Get filesystem path for specified volume", | |
35 | parameters => { | |
36 | additionalProperties => 0, | |
37 | properties => { | |
38 | volume => { | |
39 | description => "Volume identifier", | |
40 | type => 'string', format => 'pve-volume-id', | |
f3bd890d | 41 | completion => \&PVE::Storage::complete_volume, |
c669f42d DM |
42 | }, |
43 | }, | |
44 | }, | |
45 | returns => { type => 'null' }, | |
46 | ||
47 | code => sub { | |
48 | my ($param) = @_; | |
49 | ||
50 | my $cfg = PVE::Storage::config(); | |
51 | ||
52 | my $path = PVE::Storage::path ($cfg, $param->{volume}); | |
53 | ||
54 | print "$path\n"; | |
55 | ||
56 | return undef; | |
57 | ||
58 | }}); | |
59 | ||
fa017b96 FG |
60 | __PACKAGE__->register_method ({ |
61 | name => 'extractconfig', | |
62 | path => 'extractconfig', | |
63 | method => 'GET', | |
64 | description => "Extract configuration from vzdump backup archive.", | |
65 | permissions => { | |
66 | description => "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.", | |
67 | user => 'all', | |
68 | }, | |
69 | protected => 1, | |
70 | parameters => { | |
71 | additionalProperties => 0, | |
72 | properties => { | |
73 | volume => { | |
74 | description => "Volume identifier", | |
75 | type => 'string', | |
76 | completion => \&PVE::Storage::complete_volume, | |
77 | }, | |
78 | }, | |
79 | }, | |
80 | returns => { type => 'null' }, | |
81 | code => sub { | |
82 | my ($param) = @_; | |
83 | my $volume = $param->{volume}; | |
84 | ||
85 | my $rpcenv = PVE::RPCEnvironment::get(); | |
86 | my $authuser = $rpcenv->get_user(); | |
87 | ||
88 | my $storage_cfg = PVE::Storage::config(); | |
04a13668 | 89 | PVE::Storage::check_volume_access($rpcenv, $authuser, $storage_cfg, undef, $volume); |
fa017b96 FG |
90 | |
91 | my $config_raw = PVE::Storage::extract_vzdump_config($storage_cfg, $volume); | |
92 | ||
93 | print "$config_raw\n"; | |
94 | return; | |
95 | }}); | |
96 | ||
c669f42d DM |
97 | my $print_content = sub { |
98 | my ($list) = @_; | |
99 | ||
100 | my $maxlenname = 0; | |
101 | foreach my $info (@$list) { | |
102 | ||
103 | my $volid = $info->{volid}; | |
104 | my $sidlen = length ($volid); | |
105 | $maxlenname = $sidlen if $sidlen > $maxlenname; | |
106 | } | |
107 | ||
108 | foreach my $info (@$list) { | |
109 | next if !$info->{vmid}; | |
110 | my $volid = $info->{volid}; | |
111 | ||
112 | printf "%-${maxlenname}s %5s %10d %d\n", $volid, | |
113 | $info->{format}, $info->{size}, $info->{vmid}; | |
114 | } | |
115 | ||
116 | foreach my $info (sort { $a->{format} cmp $b->{format} } @$list) { | |
117 | next if $info->{vmid}; | |
118 | my $volid = $info->{volid}; | |
119 | ||
120 | printf "%-${maxlenname}s %5s %10d\n", $volid, | |
121 | $info->{format}, $info->{size}; | |
122 | } | |
123 | }; | |
124 | ||
125 | my $print_status = sub { | |
126 | my $res = shift; | |
127 | ||
128 | my $maxlen = 0; | |
129 | foreach my $res (@$res) { | |
130 | my $storeid = $res->{storage}; | |
131 | $maxlen = length ($storeid) if length ($storeid) > $maxlen; | |
132 | } | |
133 | $maxlen+=1; | |
134 | ||
135 | foreach my $res (sort { $a->{storage} cmp $b->{storage} } @$res) { | |
136 | my $storeid = $res->{storage}; | |
137 | ||
138 | my $sum = $res->{used} + $res->{avail}; | |
139 | my $per = $sum ? (0.5 + ($res->{used}*100)/$sum) : 100; | |
140 | ||
141 | printf "%-${maxlen}s %5s %1d %15d %15d %15d %.2f%%\n", $storeid, | |
142 | $res->{type}, $res->{active}, | |
143 | $res->{total}/1024, $res->{used}/1024, $res->{avail}/1024, $per; | |
144 | } | |
145 | }; | |
146 | ||
147 | our $cmddef = { | |
148 | add => [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ], | |
149 | set => [ "PVE::API2::Storage::Config", 'update', ['storage'] ], | |
150 | remove => [ "PVE::API2::Storage::Config", 'delete', ['storage'] ], | |
151 | status => [ "PVE::API2::Storage::Status", 'index', [], | |
152 | { node => $nodename }, $print_status ], | |
153 | list => [ "PVE::API2::Storage::Content", 'index', ['storage'], | |
154 | { node => $nodename }, $print_content ], | |
155 | alloc => [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'], | |
156 | { node => $nodename }, sub { | |
157 | my $volid = shift; | |
e967e0ef | 158 | print "successfully created '$volid'\n"; |
c669f42d DM |
159 | }], |
160 | free => [ "PVE::API2::Storage::Content", 'delete', ['volume'], | |
161 | { node => $nodename } ], | |
162 | nfsscan => [ "PVE::API2::Storage::Scan", 'nfsscan', ['server'], | |
163 | { node => $nodename }, sub { | |
164 | my $res = shift; | |
165 | ||
166 | my $maxlen = 0; | |
167 | foreach my $rec (@$res) { | |
168 | my $len = length ($rec->{path}); | |
169 | $maxlen = $len if $len > $maxlen; | |
170 | } | |
171 | foreach my $rec (@$res) { | |
172 | printf "%-${maxlen}s %s\n", $rec->{path}, $rec->{options}; | |
173 | } | |
174 | }], | |
175 | glusterfsscan => [ "PVE::API2::Storage::Scan", 'glusterfsscan', ['server'], | |
176 | { node => $nodename }, sub { | |
177 | my $res = shift; | |
178 | ||
179 | foreach my $rec (@$res) { | |
180 | printf "%s\n", $rec->{volname}; | |
181 | } | |
182 | }], | |
183 | iscsiscan => [ "PVE::API2::Storage::Scan", 'iscsiscan', ['server'], | |
184 | { node => $nodename }, sub { | |
185 | my $res = shift; | |
186 | ||
187 | my $maxlen = 0; | |
188 | foreach my $rec (@$res) { | |
189 | my $len = length ($rec->{target}); | |
190 | $maxlen = $len if $len > $maxlen; | |
191 | } | |
192 | foreach my $rec (@$res) { | |
193 | printf "%-${maxlen}s %s\n", $rec->{target}, $rec->{portal}; | |
194 | } | |
195 | }], | |
196 | lvmscan => [ "PVE::API2::Storage::Scan", 'lvmscan', [], | |
197 | { node => $nodename }, sub { | |
198 | my $res = shift; | |
199 | foreach my $rec (@$res) { | |
200 | printf "$rec->{vg}\n"; | |
201 | } | |
202 | }], | |
668f6d9f DM |
203 | lvmthinscan => [ "PVE::API2::Storage::Scan", 'lvmthinscan', ['vg'], |
204 | { node => $nodename }, sub { | |
205 | my $res = shift; | |
206 | foreach my $rec (@$res) { | |
207 | printf "$rec->{lv}\n"; | |
208 | } | |
209 | }], | |
c669f42d DM |
210 | zfsscan => [ "PVE::API2::Storage::Scan", 'zfsscan', [], |
211 | { node => $nodename }, sub { | |
212 | my $res = shift; | |
213 | ||
214 | foreach my $rec (@$res) { | |
215 | printf "$rec->{pool}\n"; | |
216 | } | |
217 | }], | |
218 | path => [ __PACKAGE__, 'path', ['volume']], | |
fa017b96 | 219 | extractconfig => [__PACKAGE__, 'extractconfig', ['volume']], |
c669f42d DM |
220 | }; |
221 | ||
222 | 1; |