]>
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
::Tools
qw(extract_param);
16 use PVE
::API2
::Storage
::Config
;
17 use PVE
::API2
::Storage
::Content
;
18 use PVE
::API2
::Storage
::PruneBackups
;
19 use PVE
::API2
::Storage
::Scan
;
20 use PVE
::API2
::Storage
::Status
;
21 use PVE
::JSONSchema
qw(get_standard_option);
26 use base
qw(PVE::CLIHandler);
28 my $KNOWN_EXPORT_FORMATS = ['raw+size', 'tar+size', 'qcow2+size', 'vmdk+size', 'zfs'];
30 my $nodename = PVE
::INotify
::nodename
();
35 my $password_map = PVE
::CLIHandler
::get_standard_mapping
('pve-password', {
38 return $value if $value;
39 return PVE
::PTY
::read_password
("Enter Password: ");
44 name
=> 'encryption-key',
45 desc
=> 'a file containing an encryption key, or the special value "autogen"',
48 return $value if $value eq 'autogen';
49 return PVE
::Tools
::file_get_contents
($value);
55 'cifsscan' => [ $password_map ],
56 'cifs' => [ $password_map ],
57 'pbs' => [ $password_map ],
58 'create' => [ $password_map, $enc_key_map ],
59 'update' => [ $password_map, $enc_key_map ],
61 return $mapping->{$name};
64 sub setup_environment
{
65 PVE
::RPCEnvironment-
>setup_default_cli_env();
68 __PACKAGE__-
>register_method ({
72 description
=> "Returns APIVER and APIAGE.",
74 additionalProperties
=> 0,
80 apiver
=> { type
=> 'integer' },
81 apiage
=> { type
=> 'integer' },
86 apiver
=> PVE
::Storage
::APIVER
,
87 apiage
=> PVE
::Storage
::APIAGE
,
92 __PACKAGE__-
>register_method ({
96 description
=> "Get filesystem path for specified volume",
98 additionalProperties
=> 0,
101 description
=> "Volume identifier",
102 type
=> 'string', format
=> 'pve-volume-id',
103 completion
=> \
&PVE
::Storage
::complete_volume
,
107 returns
=> { type
=> 'null' },
112 my $cfg = PVE
::Storage
::config
();
114 my $path = PVE
::Storage
::path
($cfg, $param->{volume
});
122 __PACKAGE__-
>register_method ({
123 name
=> 'extractconfig',
124 path
=> 'extractconfig',
126 description
=> "Extract configuration from vzdump backup archive.",
128 description
=> "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
133 additionalProperties
=> 0,
136 description
=> "Volume identifier",
138 completion
=> \
&PVE
::Storage
::complete_volume
,
142 returns
=> { type
=> 'null' },
145 my $volume = $param->{volume
};
147 my $rpcenv = PVE
::RPCEnvironment
::get
();
148 my $authuser = $rpcenv->get_user();
150 my $storage_cfg = PVE
::Storage
::config
();
151 PVE
::Storage
::check_volume_access
($rpcenv, $authuser, $storage_cfg, undef, $volume);
153 my $config_raw = PVE
::Storage
::extract_vzdump_config
($storage_cfg, $volume);
155 print "$config_raw\n";
159 my $print_content = sub {
162 my ($maxlenname, $maxsize) = (0, 0);
163 foreach my $info (@$list) {
164 my $volid = $info->{volid
};
165 my $sidlen = length ($volid);
166 $maxlenname = $sidlen if $sidlen > $maxlenname;
167 $maxsize = $info->{size
} if ($info->{size
} // 0) > $maxsize;
169 my $sizemaxdigits = length($maxsize);
171 my $basefmt = "%-${maxlenname}s %-7s %-9s %${sizemaxdigits}s";
172 printf "$basefmt %s\n", "Volid", "Format", "Type", "Size", "VMID";
174 foreach my $info (@$list) {
175 next if !$info->{vmid
};
176 my $volid = $info->{volid
};
178 printf "$basefmt %d\n", $volid, $info->{format
}, $info->{content
}, $info->{size
}, $info->{vmid
};
181 foreach my $info (sort { $a->{format
} cmp $b->{format
} } @$list) {
182 next if $info->{vmid
};
183 my $volid = $info->{volid
};
185 printf "$basefmt\n", $volid, $info->{format
}, $info->{content
}, $info->{size
};
189 my $print_status = sub {
193 foreach my $res (@$res) {
194 my $storeid = $res->{storage
};
195 $maxlen = length ($storeid) if length ($storeid) > $maxlen;
199 printf "%-${maxlen}s %10s %10s %15s %15s %15s %8s\n", 'Name', 'Type',
200 'Status', 'Total', 'Used', 'Available', '%';
202 foreach my $res (sort { $a->{storage
} cmp $b->{storage
} } @$res) {
203 my $storeid = $res->{storage
};
205 my $active = $res->{active
} ?
'active' : 'inactive';
206 my ($per, $per_fmt) = (0, '% 7.2f%%');
207 $per = ($res->{used
}*100)/$res->{total
} if $res->{total
} > 0;
209 if (!$res->{enabled
}) {
212 $active = 'disabled';
215 printf "%-${maxlen}s %10s %10s %15d %15d %15d $per_fmt\n", $storeid,
216 $res->{type
}, $active, $res->{total
}/1024, $res->{used}/1024,
217 $res->{avail
}/1024, $per;
221 __PACKAGE__-
>register_method ({
225 description
=> "Used internally to export a volume.",
228 additionalProperties
=> 0,
231 description
=> "Volume identifier",
233 completion
=> \
&PVE
::Storage
::complete_volume
,
236 description
=> "Export stream format",
238 enum
=> $KNOWN_EXPORT_FORMATS,
241 description
=> "Destination file name",
245 description
=> "Snapshot to start an incremental stream from",
247 pattern
=> qr/[a-z0-9_\-]{1,40}/i,
252 description
=> "Snapshot to export",
254 pattern
=> qr/[a-z0-9_\-]{1,40}/i,
258 'with-snapshots' => {
260 "Whether to include intermediate snapshots in the stream",
267 returns
=> { type
=> 'null' },
271 my $filename = $param->{filename
};
274 if ($filename eq '-') {
277 sysopen($outfh, $filename, O_CREAT
|O_WRONLY
|O_TRUNC
)
278 or die "open($filename): $!\n";
282 my $cfg = PVE
::Storage
::config
();
283 PVE
::Storage
::volume_export
($cfg, $outfh, $param->{volume
}, $param->{format
},
284 $param->{snapshot
}, $param->{base
}, $param->{'with-snapshots'});
287 if ($filename ne '-') {
289 unlink($filename) if $err;
296 __PACKAGE__-
>register_method ({
300 description
=> "Used internally to import a volume.",
303 additionalProperties
=> 0,
306 description
=> "Volume identifier",
308 completion
=> \
&PVE
::Storage
::complete_volume
,
311 description
=> "Import stream format",
313 enum
=> $KNOWN_EXPORT_FORMATS,
316 description
=> "Source file name. For '-' stdin is used, the " .
317 "tcp://<IP-or-CIDR> format allows to use a TCP connection as input. " .
318 "Else, the file is treated as common file.",
322 description
=> "Base snapshot of an incremental stream",
324 pattern
=> qr/[a-z0-9_\-]{1,40}/i,
328 'with-snapshots' => {
330 "Whether the stream includes intermediate snapshots",
335 'delete-snapshot' => {
336 description
=> "A snapshot to delete on success",
338 pattern
=> qr/[a-z0-9_\-]{1,80}/i,
343 description
=> "Choose a new volume ID if the requested " .
344 "volume ID already exists, instead of throwing an error.",
351 returns
=> { type
=> 'string' },
355 my $filename = $param->{filename
};
358 if ($filename eq '-') {
360 } elsif ($filename =~ m!^tcp://(([^/]+)(/\d+)?)$!) {
361 my ($cidr, $ip, $subnet) = ($1, $2, $3);
362 if ($subnet) { # got real CIDR notation, not just IP
363 my $ips = PVE
::Network
::get_local_ip_from_cidr
($cidr);
364 die "Unable to get any local IP address in network '$cidr'\n"
365 if scalar(@$ips) < 1;
366 die "Got multiple local IP address in network '$cidr'\n"
367 if scalar(@$ips) > 1;
371 my $family = PVE
::Tools
::get_host_address_family
($ip);
372 my $port = PVE
::Tools
::next_migrate_port
($family, $ip);
377 Proto
=> &Socket
::IPPROTO_TCP
,
378 GetAddrInfoFlags
=> 0,
382 my $socket = IO
::Socket
::IP-
>new(%$sock_params)
383 or die "failed to open socket: $!\n";
385 print "$ip\n$port\n"; # tell remote where to connect
388 my $prev_alarm = alarm 0;
389 local $SIG{ALRM
} = sub { die "timed out waiting for client\n" };
391 my $client = $socket->accept; # Wait for a client
397 sysopen($infh, $filename, O_RDONLY
)
398 or die "open($filename): $!\n";
401 my $cfg = PVE
::Storage
::config
();
402 my $volume = $param->{volume
};
403 my $delete = $param->{'delete-snapshot'};
404 my $imported_volid = PVE
::Storage
::volume_import
($cfg, $infh, $volume, $param->{format
},
405 $param->{base
}, $param->{'with-snapshots'}, $param->{'allow-rename'});
406 PVE
::Storage
::volume_snapshot_delete
($cfg, $imported_volid, $delete)
408 return $imported_volid;
412 __PACKAGE__-
>register_method ({
413 name
=> 'prunebackups',
414 path
=> 'prunebackups',
416 description
=> "Prune backups. Only those using the standard naming scheme are considered. " .
417 "If no keep options are specified, those from the storage configuration are used.",
421 additionalProperties
=> 0,
424 description
=> "Only show what would be pruned, don't delete anything.",
428 node
=> get_standard_option
('pve-node'),
429 storage
=> get_standard_option
('pve-storage-id', {
430 completion
=> \
&PVE
::Storage
::complete_storage_enabled
,
432 %{$PVE::Storage
::Plugin
::prune_backups_format
},
434 description
=> "Either 'qemu' or 'lxc'. Only consider backups for guests of this type.",
437 enum
=> ['qemu', 'lxc'],
439 vmid
=> get_standard_option
('pve-vmid', {
440 description
=> "Only consider backups for this guest.",
442 completion
=> \
&PVE
::Cluster
::complete_vmid
,
450 description
=> 'If it was a dry run or not. The list will only be defined in that case.',
459 description
=> "Backup volume ID.",
463 description
=> "Creation time of the backup (seconds since the UNIX epoch).",
467 description
=> "Whether the backup would be kept or removed. For backups that don't " .
468 "use the standard naming scheme, it's 'protected'.",
472 description
=> "One of 'qemu', 'lxc', 'openvz' or 'unknown'.",
476 description
=> "The VM the backup belongs to.",
488 my $dryrun = extract_param
($param, 'dry-run') ?
1 : 0;
491 foreach my $keep (keys %{$PVE::Storage
::Plugin
::prune_backups_format
}) {
492 $keep_opts->{$keep} = extract_param
($param, $keep) if defined($param->{$keep});
494 $param->{'prune-backups'} = PVE
::JSONSchema
::print_property_string
(
495 $keep_opts, $PVE::Storage
::Plugin
::prune_backups_format
) if $keep_opts;
499 $list = PVE
::API2
::Storage
::PruneBackups-
>dryrun($param);
501 PVE
::API2
::Storage
::PruneBackups-
>delete($param);
510 my $print_api_result = sub {
511 my ($data, $schema, $options) = @_;
512 PVE
::CLIFormatter
::print_api_result
($data, $schema, undef, $options);
516 add
=> [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ],
517 set
=> [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
518 remove
=> [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
519 status
=> [ "PVE::API2::Storage::Status", 'index', [],
520 { node
=> $nodename }, $print_status ],
521 list
=> [ "PVE::API2::Storage::Content", 'index', ['storage'],
522 { node
=> $nodename }, $print_content ],
523 alloc
=> [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'],
524 { node
=> $nodename }, sub {
526 print "successfully created '$volid'\n";
528 free
=> [ "PVE::API2::Storage::Content", 'delete', ['volume'],
529 { node
=> $nodename } ],
531 nfs
=> [ "PVE::API2::Storage::Scan", 'nfsscan', ['server'], { node
=> $nodename }, sub {
535 foreach my $rec (@$res) {
536 my $len = length ($rec->{path
});
537 $maxlen = $len if $len > $maxlen;
539 foreach my $rec (@$res) {
540 printf "%-${maxlen}s %s\n", $rec->{path
}, $rec->{options
};
543 cifs
=> [ "PVE::API2::Storage::Scan", 'cifsscan', ['server'], { node
=> $nodename }, sub {
547 foreach my $rec (@$res) {
548 my $len = length ($rec->{share
});
549 $maxlen = $len if $len > $maxlen;
551 foreach my $rec (@$res) {
552 printf "%-${maxlen}s %s\n", $rec->{share
}, $rec->{description
};
555 glusterfs
=> [ "PVE::API2::Storage::Scan", 'glusterfsscan', ['server'], { node
=> $nodename }, sub {
558 foreach my $rec (@$res) {
559 printf "%s\n", $rec->{volname
};
562 iscsi
=> [ "PVE::API2::Storage::Scan", 'iscsiscan', ['portal'], { node
=> $nodename }, sub {
566 foreach my $rec (@$res) {
567 my $len = length ($rec->{target
});
568 $maxlen = $len if $len > $maxlen;
570 foreach my $rec (@$res) {
571 printf "%-${maxlen}s %s\n", $rec->{target
}, $rec->{portal
};
574 lvm
=> [ "PVE::API2::Storage::Scan", 'lvmscan', [], { node
=> $nodename }, sub {
576 foreach my $rec (@$res) {
577 printf "$rec->{vg}\n";
580 lvmthin
=> [ "PVE::API2::Storage::Scan", 'lvmthinscan', ['vg'], { node
=> $nodename }, sub {
582 foreach my $rec (@$res) {
583 printf "$rec->{lv}\n";
587 "PVE::API2::Storage::Scan",
589 ['server', 'username'],
590 { node
=> $nodename },
592 $PVE::RESTHandler
::standard_output_options
,
594 zfs
=> [ "PVE::API2::Storage::Scan", 'zfsscan', [], { node
=> $nodename }, sub {
597 foreach my $rec (@$res) {
598 printf "$rec->{pool}\n";
602 nfsscan
=> { alias
=> 'scan nfs' },
603 cifsscan
=> { alias
=> 'scan cifs' },
604 glusterfsscan
=> { alias
=> 'scan glusterfs' },
605 iscsiscan
=> { alias
=> 'scan iscsi' },
606 lvmscan
=> { alias
=> 'scan lvm' },
607 lvmthinscan
=> { alias
=> 'scan lvmthin' },
608 zfsscan
=> { alias
=> 'scan zfs' },
609 path
=> [ __PACKAGE__
, 'path', ['volume']],
610 extractconfig
=> [__PACKAGE__
, 'extractconfig', ['volume']],
611 export
=> [ __PACKAGE__
, 'export', ['volume', 'format', 'filename']],
612 import
=> [ __PACKAGE__
, 'import', ['volume', 'format', 'filename'], {}, sub {
614 print PVE
::Storage
::volume_imported_message
($volid);
616 apiinfo
=> [ __PACKAGE__
, 'apiinfo', [], {}, sub {
619 print "APIVER $res->{apiver}\n";
620 print "APIAGE $res->{apiage}\n";
622 'prune-backups' => [ __PACKAGE__
, 'prunebackups', ['storage'], { node
=> $nodename }, sub {
625 my ($dryrun, $list) = ($res->{dryrun
}, $res->{list
});
629 if (!scalar(@{$list})) {
630 print "No backups found\n";
634 print "NOTE: this is only a preview and might not be what a subsequent\n" .
635 "prune call does if backups are removed/added in the meantime.\n\n";
638 my $vmcmp = PVE
::Tools
::safe_compare
($a->{vmid
}, $b->{vmid
}, sub { $_[0] <=> $_[1] });
639 return $vmcmp if $vmcmp ne 0;
640 return $a->{ctime
} <=> $b->{ctime
};
644 foreach my $backup (@sorted) {
645 my $volid = $backup->{volid
};
646 $maxlen = length($volid) if length($volid) > $maxlen;
650 printf("%-${maxlen}s %15s %10s\n", 'Backup', 'Backup-ID', 'Prune-Mark');
651 foreach my $backup (@sorted) {
652 my $type = $backup->{type
};
653 my $vmid = $backup->{vmid
};
654 my $backup_id = defined($vmid) ?
"$type/$vmid" : "$type";
655 printf("%-${maxlen}s %15s %10s\n", $backup->{volid
}, $backup_id, $backup->{mark
});