]> git.proxmox.com Git - pve-storage.git/blob - pvesm
Improve parsing of zfs volumes (ZVOLs) in order to avoid filtering of zvols nested...
[pve-storage.git] / pvesm
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Getopt::Long;
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 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
25
26 initlog ('pvesm');
27
28 die "please run as root\n" if $> != 0;
29
30 PVE::INotify::inotify_init();
31
32 my $rpcenv = PVE::RPCEnvironment->init('cli');
33
34 $rpcenv->init_request();
35 $rpcenv->set_language($ENV{LANG});
36 $rpcenv->set_user('root@pam');
37
38 __PACKAGE__->register_method ({
39 name => 'path',
40 path => 'path',
41 method => 'GET',
42 description => "Get filesystem path for specified volume",
43 parameters => {
44 additionalProperties => 0,
45 properties => {
46 volume => {
47 description => "Volume identifier",
48 type => 'string', format => 'pve-volume-id',
49 },
50 },
51 },
52 returns => { type => 'null' },
53
54 code => sub {
55 my ($param) = @_;
56
57 my $cfg = PVE::Storage::config();
58
59 my $path = PVE::Storage::path ($cfg, $param->{volume});
60
61 print "$path\n";
62
63 return undef;
64
65 }});
66
67 my $print_content = sub {
68 my ($list) = @_;
69
70 my $maxlenname = 0;
71 foreach my $info (@$list) {
72
73 my $volid = $info->{volid};
74 my $sidlen = length ($volid);
75 $maxlenname = $sidlen if $sidlen > $maxlenname;
76 }
77
78 foreach my $info (@$list) {
79 next if !$info->{vmid};
80 my $volid = $info->{volid};
81
82 printf "%-${maxlenname}s %5s %10d %d\n", $volid,
83 $info->{format}, $info->{size}, $info->{vmid};
84 }
85
86 foreach my $info (sort { $a->{format} cmp $b->{format} } @$list) {
87 next if $info->{vmid};
88 my $volid = $info->{volid};
89
90 printf "%-${maxlenname}s %5s %10d\n", $volid,
91 $info->{format}, $info->{size};
92 }
93 };
94
95 my $print_status = sub {
96 my $res = shift;
97
98 my $maxlen = 0;
99 foreach my $res (@$res) {
100 my $storeid = $res->{storage};
101 $maxlen = length ($storeid) if length ($storeid) > $maxlen;
102 }
103 $maxlen+=1;
104
105 foreach my $res (sort { $a->{storage} cmp $b->{storage} } @$res) {
106 my $storeid = $res->{storage};
107
108 my $sum = $res->{used} + $res->{avail};
109 my $per = $sum ? (0.5 + ($res->{used}*100)/$sum) : 100;
110
111 printf "%-${maxlen}s %5s %1d %15d %15d %15d %.2f%%\n", $storeid,
112 $res->{type}, $res->{active},
113 $res->{total}/1024, $res->{used}/1024, $res->{avail}/1024, $per;
114 }
115 };
116
117 my $nodename = PVE::INotify::nodename();
118
119 my $cmddef = {
120 add => [ "PVE::API2::Storage::Config", 'create', ['storage'] ],
121 set => [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
122 remove => [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
123 status => [ "PVE::API2::Storage::Status", 'index', [],
124 { node => $nodename }, $print_status ],
125 list => [ "PVE::API2::Storage::Content", 'index', ['storage'],
126 { node => $nodename }, $print_content ],
127 alloc => [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'],
128 { node => $nodename }, sub {
129 my $volid = shift;
130 print "sucessfuly created '$volid'\n";
131 }],
132 free => [ "PVE::API2::Storage::Content", 'delete', ['volume'],
133 { node => $nodename } ],
134 nfsscan => [ "PVE::API2::Storage::Scan", 'nfsscan', ['server'],
135 { node => $nodename }, sub {
136 my $res = shift;
137
138 my $maxlen = 0;
139 foreach my $rec (@$res) {
140 my $len = length ($rec->{path});
141 $maxlen = $len if $len > $maxlen;
142 }
143 foreach my $rec (@$res) {
144 printf "%-${maxlen}s %s\n", $rec->{path}, $rec->{options};
145 }
146 }],
147 glusterfsscan => [ "PVE::API2::Storage::Scan", 'glusterfsscan', ['server'],
148 { node => $nodename }, sub {
149 my $res = shift;
150
151 foreach my $rec (@$res) {
152 printf "%s\n", $rec->{volname};
153 }
154 }],
155 iscsiscan => [ "PVE::API2::Storage::Scan", 'iscsiscan', ['server'],
156 { node => $nodename }, sub {
157 my $res = shift;
158
159 my $maxlen = 0;
160 foreach my $rec (@$res) {
161 my $len = length ($rec->{target});
162 $maxlen = $len if $len > $maxlen;
163 }
164 foreach my $rec (@$res) {
165 printf "%-${maxlen}s %s\n", $rec->{target}, $rec->{portal};
166 }
167 }],
168 lvmscan => [ "PVE::API2::Storage::Scan", 'lvmscan', [],
169 { node => $nodename }, sub {
170 my $res = shift;
171 foreach my $rec (@$res) {
172 printf "$rec->{vg}\n";
173 }
174 }],
175 path => [ __PACKAGE__, 'path', ['volume']],
176 };
177
178 my $cmd = shift;
179
180 PVE::CLIHandler::handle_cmd($cmddef, "pvesm", $cmd, \@ARGV, undef, $0);
181
182 exit 0;
183
184 __END__
185
186 =head1 NAME
187
188 pvesm - PVE Storage Manager
189
190 =head1 SYNOPSIS
191
192 =include synopsis
193
194 =head1 DESCRIPTION
195
196 =head2 Storage pools
197
198 Each storage pool is uniquely identified by its <STORAGE_ID>.
199
200 =head3 Storage content
201
202 A storage can support several content types, for example virtual disk
203 images, cdrom iso images, openvz templates or openvz root directories
204 (C<images>, C<iso>, C<vztmpl>, C<rootdir>).
205
206 =head2 Volumes
207
208 A volume is identified by the <STORAGE_ID>, followed by a storage type
209 dependent volume name, separated by colon. A valid <VOLUME_ID> looks like:
210
211 local:230/example-image.raw
212
213 local:iso/debian-501-amd64-netinst.iso
214
215 local:vztmpl/debian-5.0-joomla_1.5.9-1_i386.tar.gz
216
217 iscsi-storage:0.0.2.scsi-14f504e46494c4500494b5042546d2d646744372d31616d61
218
219 To get the filesystem path for a <VOLUME_ID> use:
220
221 pvesm path <VOLUME_ID>
222
223
224 =head1 EXAMPLES
225
226 # scan iscsi host for available targets
227 pvesm scan iscsi <HOST[:PORT]>
228
229 # scan nfs server for available exports
230 pvesm scan nfs <HOST>
231
232 # add storage pools
233 pvesm add <STORAGE_ID> <TYPE> <OPTIONS>
234 pvesm add <STORAGE_ID> dir --path <PATH>
235 pvesm add <STORAGE_ID> nfs --path <PATH> --server <SERVER> --export <EXPORT>
236 pvesm add <STORAGE_ID> lvm --vgname <VGNAME>
237 pvesm add <STORAGE_ID> iscsi --portal <HOST[:PORT]> --target <TARGET>
238
239 # disable storage pools
240 pvesm set <STORAGE_ID> --disable 1
241
242 # enable storage pools
243 pvesm set <STORAGE_ID> --disable 0
244
245 # change/set storage options
246 pvesm set <STORAGE_ID> <OPTIONS>
247 pvesm set <STORAGE_ID> --shared 1
248 pvesm set local --format qcow2
249 pvesm set <STORAGE_ID> --content iso
250
251 # remove storage pools - does not delete any data
252 pvesm remove <STORAGE_ID>
253
254 # alloc volumes
255 pvesm alloc <STORAGE_ID> <VMID> <name> <size> [--format <raw|qcow2>]
256
257 # alloc 4G volume in local storage - use auto generated name
258 pvesm alloc local <VMID> '' 4G
259
260 # free volumes (warning: destroy/deletes all volume data)
261 pvesm free <VOLUME_ID>
262
263 # list storage status
264 pvesm status
265
266 # list storage contents
267 pvesm list <STORAGE_ID> [--vmid <VMID>]
268
269 # list volumes allocated by VMID
270 pvesm list <STORAGE_ID> --vmid <VMID>
271
272 # list iso images
273 pvesm list <STORAGE_ID> --iso
274
275 # list openvz templates
276 pvesm list <STORAGE_ID> --vztmpl
277
278 # show filesystem path for a volume
279 pvesm path <VOLUME_ID>
280
281 =include pve_copyright