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