]>
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);
24 use base
qw(PVE::CLIHandler);
26 my $KNOWN_EXPORT_FORMATS = ['raw+size', 'tar+size', 'qcow2+size', 'vmdk+size', 'zfs'];
28 my $nodename = PVE
::INotify
::nodename
();
31 return PVE
::PTY
::read_password
("Enter Password: ");
34 sub setup_environment
{
35 PVE
::RPCEnvironment-
>setup_default_cli_env();
38 __PACKAGE__-
>register_method ({
42 description
=> "Get filesystem path for specified volume",
44 additionalProperties
=> 0,
47 description
=> "Volume identifier",
48 type
=> 'string', format
=> 'pve-volume-id',
49 completion
=> \
&PVE
::Storage
::complete_volume
,
53 returns
=> { type
=> 'null' },
58 my $cfg = PVE
::Storage
::config
();
60 my $path = PVE
::Storage
::path
($cfg, $param->{volume
});
68 __PACKAGE__-
>register_method ({
69 name
=> 'extractconfig',
70 path
=> 'extractconfig',
72 description
=> "Extract configuration from vzdump backup archive.",
74 description
=> "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
79 additionalProperties
=> 0,
82 description
=> "Volume identifier",
84 completion
=> \
&PVE
::Storage
::complete_volume
,
88 returns
=> { type
=> 'null' },
91 my $volume = $param->{volume
};
93 my $rpcenv = PVE
::RPCEnvironment
::get
();
94 my $authuser = $rpcenv->get_user();
96 my $storage_cfg = PVE
::Storage
::config
();
97 PVE
::Storage
::check_volume_access
($rpcenv, $authuser, $storage_cfg, undef, $volume);
99 my $config_raw = PVE
::Storage
::extract_vzdump_config
($storage_cfg, $volume);
101 print "$config_raw\n";
105 my $print_content = sub {
109 foreach my $info (@$list) {
111 my $volid = $info->{volid
};
112 my $sidlen = length ($volid);
113 $maxlenname = $sidlen if $sidlen > $maxlenname;
116 foreach my $info (@$list) {
117 next if !$info->{vmid
};
118 my $volid = $info->{volid
};
120 printf "%-${maxlenname}s %5s %10d %d\n", $volid,
121 $info->{format
}, $info->{size
}, $info->{vmid
};
124 foreach my $info (sort { $a->{format
} cmp $b->{format
} } @$list) {
125 next if $info->{vmid
};
126 my $volid = $info->{volid
};
128 printf "%-${maxlenname}s %5s %10d\n", $volid,
129 $info->{format
}, $info->{size
};
133 my $print_status = sub {
137 foreach my $res (@$res) {
138 my $storeid = $res->{storage
};
139 $maxlen = length ($storeid) if length ($storeid) > $maxlen;
143 printf "%-${maxlen}s %10s %10s %15s %15s %15s %8s\n", 'Name', 'Type',
144 'Status', 'Total', 'Used', 'Available', '%';
146 foreach my $res (sort { $a->{storage
} cmp $b->{storage
} } @$res) {
147 my $storeid = $res->{storage
};
149 my $active = $res->{active
} ?
'active' : 'inactive';
150 my ($per, $per_fmt) = (0, '% 7.2f%%');
151 $per = ($res->{used
}*100)/$res->{total
} if $res->{total
} > 0;
153 if (!$res->{enabled
}) {
156 $active = 'disabled';
159 printf "%-${maxlen}s %10s %10s %15d %15d %15d $per_fmt\n", $storeid,
160 $res->{type
}, $active, $res->{total
}/1024, $res->{used}/1024,
161 $res->{avail
}/1024, $per;
165 __PACKAGE__-
>register_method ({
169 description
=> "Export a volume.",
172 additionalProperties
=> 0,
175 description
=> "Volume identifier",
177 completion
=> \
&PVE
::Storage
::complete_volume
,
180 description
=> "Export stream format",
182 enum
=> $KNOWN_EXPORT_FORMATS,
185 description
=> "Destination file name",
189 description
=> "Snapshot to start an incremental stream from",
191 pattern
=> qr/[a-z0-9_\-]{1,40}/,
196 description
=> "Snapshot to export",
198 pattern
=> qr/[a-z0-9_\-]{1,40}/,
202 'with-snapshots' => {
204 "Whether to include intermediate snapshots in the stream",
211 returns
=> { type
=> 'null' },
215 my $filename = $param->{filename
};
218 if ($filename eq '-') {
221 sysopen($outfh, $filename, O_CREAT
|O_WRONLY
|O_TRUNC
)
222 or die "open($filename): $!\n";
226 my $cfg = PVE
::Storage
::config
();
227 PVE
::Storage
::volume_export
($cfg, $outfh, $param->{volume
}, $param->{format
},
228 $param->{snapshot
}, $param->{base
}, $param->{'with-snapshots'});
231 if ($filename ne '-') {
233 unlink($filename) if $err;
240 __PACKAGE__-
>register_method ({
244 description
=> "Import a volume.",
247 additionalProperties
=> 0,
250 description
=> "Volume identifier",
252 completion
=> \
&PVE
::Storage
::complete_volume
,
255 description
=> "Import stream format",
257 enum
=> $KNOWN_EXPORT_FORMATS,
260 description
=> "Source file name",
264 description
=> "Base snapshot of an incremental stream",
266 pattern
=> qr/[a-z0-9_\-]{1,40}/,
270 'with-snapshots' => {
272 "Whether the stream includes intermediate snapshots",
277 'delete-snapshot' => {
278 description
=> "A snapshot to delete on success",
280 pattern
=> qr/[a-z0-9_\-]{1,80}/,
286 returns
=> { type
=> 'null' },
290 my $filename = $param->{filename
};
293 if ($filename eq '-') {
296 sysopen($infh, $filename, O_RDONLY
)
297 or die "open($filename): $!\n";
300 my $cfg = PVE
::Storage
::config
();
301 my $volume = $param->{volume
};
302 my $delete = $param->{'delete-snapshot'};
303 PVE
::Storage
::volume_import
($cfg, $infh, $volume, $param->{format
},
304 $param->{base
}, $param->{'with-snapshots'});
305 PVE
::Storage
::volume_snapshot_delete
($cfg, $volume, $delete)
312 add
=> [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ],
313 set
=> [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
314 remove
=> [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
315 status
=> [ "PVE::API2::Storage::Status", 'index', [],
316 { node
=> $nodename }, $print_status ],
317 list
=> [ "PVE::API2::Storage::Content", 'index', ['storage'],
318 { node
=> $nodename }, $print_content ],
319 alloc
=> [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'],
320 { node
=> $nodename }, sub {
322 print "successfully created '$volid'\n";
324 free
=> [ "PVE::API2::Storage::Content", 'delete', ['volume'],
325 { node
=> $nodename } ],
326 nfsscan
=> [ "PVE::API2::Storage::Scan", 'nfsscan', ['server'],
327 { node
=> $nodename }, sub {
331 foreach my $rec (@$res) {
332 my $len = length ($rec->{path
});
333 $maxlen = $len if $len > $maxlen;
335 foreach my $rec (@$res) {
336 printf "%-${maxlen}s %s\n", $rec->{path
}, $rec->{options
};
339 cifsscan
=> [ "PVE::API2::Storage::Scan", 'cifsscan', ['server'],
340 { node
=> $nodename }, sub {
344 foreach my $rec (@$res) {
345 my $len = length ($rec->{share
});
346 $maxlen = $len if $len > $maxlen;
348 foreach my $rec (@$res) {
349 printf "%-${maxlen}s %s\n", $rec->{share
}, $rec->{description
};
352 glusterfsscan
=> [ "PVE::API2::Storage::Scan", 'glusterfsscan', ['server'],
353 { node
=> $nodename }, sub {
356 foreach my $rec (@$res) {
357 printf "%s\n", $rec->{volname
};
360 iscsiscan
=> [ "PVE::API2::Storage::Scan", 'iscsiscan', ['server'],
361 { node
=> $nodename }, sub {
365 foreach my $rec (@$res) {
366 my $len = length ($rec->{target
});
367 $maxlen = $len if $len > $maxlen;
369 foreach my $rec (@$res) {
370 printf "%-${maxlen}s %s\n", $rec->{target
}, $rec->{portal
};
373 lvmscan
=> [ "PVE::API2::Storage::Scan", 'lvmscan', [],
374 { node
=> $nodename }, sub {
376 foreach my $rec (@$res) {
377 printf "$rec->{vg}\n";
380 lvmthinscan
=> [ "PVE::API2::Storage::Scan", 'lvmthinscan', ['vg'],
381 { node
=> $nodename }, sub {
383 foreach my $rec (@$res) {
384 printf "$rec->{lv}\n";
387 zfsscan
=> [ "PVE::API2::Storage::Scan", 'zfsscan', [],
388 { node
=> $nodename }, sub {
391 foreach my $rec (@$res) {
392 printf "$rec->{pool}\n";
395 path
=> [ __PACKAGE__
, 'path', ['volume']],
396 extractconfig
=> [__PACKAGE__
, 'extractconfig', ['volume']],
397 export
=> [ __PACKAGE__
, 'export', ['volume', 'format', 'filename']],
398 import
=> [ __PACKAGE__
, 'import', ['volume', 'format', 'filename']],