]>
git.proxmox.com Git - pve-storage.git/blob - PVE/CLI/pvesm.pm
1 package PVE
::CLI
::pvesm
;
6 use POSIX
qw(O_RDONLY O_WRONLY O_CREAT O_TRUNC);
13 use PVE
::RPCEnvironment
;
15 use PVE
::API2
::Storage
::Config
;
16 use PVE
::API2
::Storage
::Content
;
17 use PVE
::API2
::Storage
::Status
;
18 use PVE
::API2
::Storage
::Scan
;
19 use PVE
::JSONSchema
qw(get_standard_option);
23 use base
qw(PVE::CLIHandler);
25 my $KNOWN_EXPORT_FORMATS = ['raw+size', 'tar+size', 'qcow2+size', 'vmdk+size', 'zfs'];
27 my $nodename = PVE
::INotify
::nodename
();
29 sub setup_environment
{
30 PVE
::RPCEnvironment-
>setup_default_cli_env();
33 __PACKAGE__-
>register_method ({
37 description
=> "Get filesystem path for specified volume",
39 additionalProperties
=> 0,
42 description
=> "Volume identifier",
43 type
=> 'string', format
=> 'pve-volume-id',
44 completion
=> \
&PVE
::Storage
::complete_volume
,
48 returns
=> { type
=> 'null' },
53 my $cfg = PVE
::Storage
::config
();
55 my $path = PVE
::Storage
::path
($cfg, $param->{volume
});
63 __PACKAGE__-
>register_method ({
64 name
=> 'extractconfig',
65 path
=> 'extractconfig',
67 description
=> "Extract configuration from vzdump backup archive.",
69 description
=> "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
74 additionalProperties
=> 0,
77 description
=> "Volume identifier",
79 completion
=> \
&PVE
::Storage
::complete_volume
,
83 returns
=> { type
=> 'null' },
86 my $volume = $param->{volume
};
88 my $rpcenv = PVE
::RPCEnvironment
::get
();
89 my $authuser = $rpcenv->get_user();
91 my $storage_cfg = PVE
::Storage
::config
();
92 PVE
::Storage
::check_volume_access
($rpcenv, $authuser, $storage_cfg, undef, $volume);
94 my $config_raw = PVE
::Storage
::extract_vzdump_config
($storage_cfg, $volume);
96 print "$config_raw\n";
100 my $print_content = sub {
104 foreach my $info (@$list) {
106 my $volid = $info->{volid
};
107 my $sidlen = length ($volid);
108 $maxlenname = $sidlen if $sidlen > $maxlenname;
111 foreach my $info (@$list) {
112 next if !$info->{vmid
};
113 my $volid = $info->{volid
};
115 printf "%-${maxlenname}s %5s %10d %d\n", $volid,
116 $info->{format
}, $info->{size
}, $info->{vmid
};
119 foreach my $info (sort { $a->{format
} cmp $b->{format
} } @$list) {
120 next if $info->{vmid
};
121 my $volid = $info->{volid
};
123 printf "%-${maxlenname}s %5s %10d\n", $volid,
124 $info->{format
}, $info->{size
};
128 my $print_status = sub {
132 foreach my $res (@$res) {
133 my $storeid = $res->{storage
};
134 $maxlen = length ($storeid) if length ($storeid) > $maxlen;
138 printf "%-${maxlen}s %10s %10s %15s %15s %15s %8s\n", 'Name', 'Type',
139 'Status', 'Total', 'Used', 'Available', '%';
141 foreach my $res (sort { $a->{storage
} cmp $b->{storage
} } @$res) {
142 my $storeid = $res->{storage
};
144 my $active = $res->{active
} ?
'active' : 'inactive';
145 my ($per, $per_fmt) = (0, '% 7.2f%%');
146 $per = ($res->{used
}*100)/$res->{total
} if $res->{total
} > 0;
148 if (!$res->{enabled
}) {
151 $active = 'disabled';
154 printf "%-${maxlen}s %10s %10s %15d %15d %15d $per_fmt\n", $storeid,
155 $res->{type
}, $active, $res->{total
}/1024, $res->{used}/1024,
156 $res->{avail
}/1024, $per;
160 __PACKAGE__-
>register_method ({
164 description
=> "Export a volume.",
167 additionalProperties
=> 0,
170 description
=> "Volume identifier",
172 completion
=> \
&PVE
::Storage
::complete_volume
,
175 description
=> "Export stream format",
177 enum
=> $KNOWN_EXPORT_FORMATS,
180 description
=> "Destination file name",
184 description
=> "Snapshot to start an incremental stream from",
186 pattern
=> qr/[a-z0-9_\-]{1,40}/,
191 description
=> "Snapshot to export",
193 pattern
=> qr/[a-z0-9_\-]{1,40}/,
197 'with-snapshots' => {
199 "Whether to include intermediate snapshots in the stream",
206 returns
=> { type
=> 'null' },
210 my $filename = $param->{filename
};
213 if ($filename eq '-') {
216 sysopen($outfh, $filename, O_CREAT
|O_WRONLY
|O_TRUNC
)
217 or die "open($filename): $!\n";
221 my $cfg = PVE
::Storage
::config
();
222 PVE
::Storage
::volume_export
($cfg, $outfh, $param->{volume
}, $param->{format
},
223 $param->{snapshot
}, $param->{base
}, $param->{'with-snapshots'});
226 if ($filename ne '-') {
228 unlink($filename) if $err;
235 __PACKAGE__-
>register_method ({
239 description
=> "Import a volume.",
242 additionalProperties
=> 0,
245 description
=> "Volume identifier",
247 completion
=> \
&PVE
::Storage
::complete_volume
,
250 description
=> "Import stream format",
252 enum
=> $KNOWN_EXPORT_FORMATS,
255 description
=> "Source file name",
259 description
=> "Base snapshot of an incremental stream",
261 pattern
=> qr/[a-z0-9_\-]{1,40}/,
265 'with-snapshots' => {
267 "Whether the stream includes intermediate snapshots",
272 'delete-snapshot' => {
273 description
=> "A snapshot to delete on success",
275 pattern
=> qr/[a-z0-9_\-]{1,80}/,
281 returns
=> { type
=> 'null' },
285 my $filename = $param->{filename
};
288 if ($filename eq '-') {
291 sysopen($infh, $filename, O_RDONLY
)
292 or die "open($filename): $!\n";
295 my $cfg = PVE
::Storage
::config
();
296 my $volume = $param->{volume
};
297 my $delete = $param->{'delete-snapshot'};
298 PVE
::Storage
::volume_import
($cfg, $infh, $volume, $param->{format
},
299 $param->{base
}, $param->{'with-snapshots'});
300 PVE
::Storage
::volume_snapshot_delete
($cfg, $volume, $delete)
307 add
=> [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ],
308 set
=> [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
309 remove
=> [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
310 status
=> [ "PVE::API2::Storage::Status", 'index', [],
311 { node
=> $nodename }, $print_status ],
312 list
=> [ "PVE::API2::Storage::Content", 'index', ['storage'],
313 { node
=> $nodename }, $print_content ],
314 alloc
=> [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'],
315 { node
=> $nodename }, sub {
317 print "successfully created '$volid'\n";
319 free
=> [ "PVE::API2::Storage::Content", 'delete', ['volume'],
320 { node
=> $nodename } ],
321 nfsscan
=> [ "PVE::API2::Storage::Scan", 'nfsscan', ['server'],
322 { node
=> $nodename }, sub {
326 foreach my $rec (@$res) {
327 my $len = length ($rec->{path
});
328 $maxlen = $len if $len > $maxlen;
330 foreach my $rec (@$res) {
331 printf "%-${maxlen}s %s\n", $rec->{path
}, $rec->{options
};
334 glusterfsscan
=> [ "PVE::API2::Storage::Scan", 'glusterfsscan', ['server'],
335 { node
=> $nodename }, sub {
338 foreach my $rec (@$res) {
339 printf "%s\n", $rec->{volname
};
342 iscsiscan
=> [ "PVE::API2::Storage::Scan", 'iscsiscan', ['server'],
343 { node
=> $nodename }, sub {
347 foreach my $rec (@$res) {
348 my $len = length ($rec->{target
});
349 $maxlen = $len if $len > $maxlen;
351 foreach my $rec (@$res) {
352 printf "%-${maxlen}s %s\n", $rec->{target
}, $rec->{portal
};
355 lvmscan
=> [ "PVE::API2::Storage::Scan", 'lvmscan', [],
356 { node
=> $nodename }, sub {
358 foreach my $rec (@$res) {
359 printf "$rec->{vg}\n";
362 lvmthinscan
=> [ "PVE::API2::Storage::Scan", 'lvmthinscan', ['vg'],
363 { node
=> $nodename }, sub {
365 foreach my $rec (@$res) {
366 printf "$rec->{lv}\n";
369 zfsscan
=> [ "PVE::API2::Storage::Scan", 'zfsscan', [],
370 { node
=> $nodename }, sub {
373 foreach my $rec (@$res) {
374 printf "$rec->{pool}\n";
377 path
=> [ __PACKAGE__
, 'path', ['volume']],
378 extractconfig
=> [__PACKAGE__
, 'extractconfig', ['volume']],
379 export
=> [ __PACKAGE__
, 'export', ['volume', 'format', 'filename']],
380 import
=> [ __PACKAGE__
, 'import', ['volume', 'format', 'filename']],