]>
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);
9 use MIME
::Base64
qw(encode_base64);
13 use Socket
qw(SOCK_STREAM);
18 use PVE
::RPCEnvironment
;
20 use PVE
::Tools
qw(extract_param);
21 use PVE
::API2
::Storage
::Config
;
22 use PVE
::API2
::Storage
::Content
;
23 use PVE
::API2
::Storage
::PruneBackups
;
24 use PVE
::API2
::Storage
::Scan
;
25 use PVE
::API2
::Storage
::Status
;
26 use PVE
::JSONSchema
qw(get_standard_option);
31 use base
qw(PVE::CLIHandler);
33 my $nodename = PVE
::INotify
::nodename
();
38 my $password_map = PVE
::CLIHandler
::get_standard_mapping
('pve-password', {
41 return $value if $value;
42 return PVE
::PTY
::read_password
("Enter Password: ");
47 name
=> 'encryption-key',
48 desc
=> 'a file containing an encryption key, or the special value "autogen"',
51 return $value if $value eq 'autogen';
52 return PVE
::Tools
::file_get_contents
($value);
56 my $master_key_map = {
57 name
=> 'master-pubkey',
58 desc
=> 'a file containing a PEM-formatted master public key',
61 return encode_base64
(PVE
::Tools
::file_get_contents
($value), '');
67 desc
=> 'file containing the keyring to authenticate in the Ceph cluster',
70 return PVE
::Tools
::file_get_contents
($value);
75 'cifsscan' => [ $password_map ],
76 'cifs' => [ $password_map ],
77 'pbs' => [ $password_map ],
78 'create' => [ $password_map, $enc_key_map, $master_key_map, $keyring_map ],
79 'update' => [ $password_map, $enc_key_map, $master_key_map, $keyring_map ],
81 return $mapping->{$name};
84 sub setup_environment
{
85 PVE
::RPCEnvironment-
>setup_default_cli_env();
88 __PACKAGE__-
>register_method ({
92 description
=> "Returns APIVER and APIAGE.",
94 additionalProperties
=> 0,
100 apiver
=> { type
=> 'integer' },
101 apiage
=> { type
=> 'integer' },
106 apiver
=> PVE
::Storage
::APIVER
,
107 apiage
=> PVE
::Storage
::APIAGE
,
112 __PACKAGE__-
>register_method ({
116 description
=> "Get filesystem path for specified volume",
118 additionalProperties
=> 0,
121 description
=> "Volume identifier",
122 type
=> 'string', format
=> 'pve-volume-id',
123 completion
=> \
&PVE
::Storage
::complete_volume
,
127 returns
=> { type
=> 'null' },
132 my $cfg = PVE
::Storage
::config
();
134 my $path = PVE
::Storage
::path
($cfg, $param->{volume
});
142 __PACKAGE__-
>register_method ({
143 name
=> 'extractconfig',
144 path
=> 'extractconfig',
146 description
=> "Extract configuration from vzdump backup archive.",
148 description
=> "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
153 additionalProperties
=> 0,
156 description
=> "Volume identifier",
158 completion
=> \
&PVE
::Storage
::complete_volume
,
162 returns
=> { type
=> 'null' },
165 my $volume = $param->{volume
};
167 my $rpcenv = PVE
::RPCEnvironment
::get
();
168 my $authuser = $rpcenv->get_user();
170 my $storage_cfg = PVE
::Storage
::config
();
171 PVE
::Storage
::check_volume_access
(
180 if (PVE
::Storage
::parse_volume_id
($volume, 1)) {
181 my (undef, undef, $ownervm) = PVE
::Storage
::parse_volname
($storage_cfg, $volume);
182 $rpcenv->check($authuser, "/vms/$ownervm", ['VM.Backup']);
185 my $config_raw = PVE
::Storage
::extract_vzdump_config
($storage_cfg, $volume);
187 print "$config_raw\n";
191 my $print_content = sub {
194 my ($maxlenname, $maxsize) = (0, 0);
195 foreach my $info (@$list) {
196 my $volid = $info->{volid
};
197 my $sidlen = length ($volid);
198 $maxlenname = $sidlen if $sidlen > $maxlenname;
199 $maxsize = $info->{size
} if ($info->{size
} // 0) > $maxsize;
201 my $sizemaxdigits = length($maxsize);
203 my $basefmt = "%-${maxlenname}s %-7s %-9s %${sizemaxdigits}s";
204 printf "$basefmt %s\n", "Volid", "Format", "Type", "Size", "VMID";
206 foreach my $info (@$list) {
207 next if !$info->{vmid
};
208 my $volid = $info->{volid
};
210 printf "$basefmt %d\n", $volid, $info->{format
}, $info->{content
}, $info->{size
}, $info->{vmid
};
213 foreach my $info (sort { $a->{format
} cmp $b->{format
} } @$list) {
214 next if $info->{vmid
};
215 my $volid = $info->{volid
};
217 printf "$basefmt\n", $volid, $info->{format
}, $info->{content
}, $info->{size
};
221 my $print_status = sub {
225 foreach my $res (@$res) {
226 my $storeid = $res->{storage
};
227 $maxlen = length ($storeid) if length ($storeid) > $maxlen;
231 printf "%-${maxlen}s %10s %10s %15s %15s %15s %8s\n", 'Name', 'Type',
232 'Status', 'Total', 'Used', 'Available', '%';
234 foreach my $res (sort { $a->{storage
} cmp $b->{storage
} } @$res) {
235 my $storeid = $res->{storage
};
237 my $active = $res->{active
} ?
'active' : 'inactive';
238 my ($per, $per_fmt) = (0, '% 7.2f%%');
239 $per = ($res->{used
}*100)/$res->{total
} if $res->{total
} > 0;
241 if (!$res->{enabled
}) {
244 $active = 'disabled';
247 printf "%-${maxlen}s %10s %10s %15d %15d %15d $per_fmt\n", $storeid,
248 $res->{type
}, $active, $res->{total
}/1024, $res->{used}/1024,
249 $res->{avail
}/1024, $per;
253 __PACKAGE__-
>register_method ({
257 description
=> "Used internally to export a volume.",
260 additionalProperties
=> 0,
263 description
=> "Volume identifier",
265 completion
=> \
&PVE
::Storage
::complete_volume
,
268 description
=> "Export stream format",
270 enum
=> $PVE::Storage
::KNOWN_EXPORT_FORMATS
,
273 description
=> "Destination file name",
277 description
=> "Snapshot to start an incremental stream from",
279 pattern
=> qr/[a-z0-9_\-]{1,40}/i,
284 description
=> "Snapshot to export",
286 pattern
=> qr/[a-z0-9_\-]{1,40}/i,
290 'with-snapshots' => {
292 "Whether to include intermediate snapshots in the stream",
298 description
=> "Ordered list of snapshots to transfer",
300 format
=> 'string-list',
305 returns
=> { type
=> 'null' },
309 my $with_snapshots = $param->{'with-snapshots'};
310 if (defined(my $list = $param->{'snapshot-list'})) {
311 $with_snapshots = PVE
::Tools
::split_list
($list);
314 my $filename = $param->{filename
};
317 if ($filename eq '-') {
320 sysopen($outfh, $filename, O_CREAT
|O_WRONLY
|O_TRUNC
)
321 or die "open($filename): $!\n";
325 my $cfg = PVE
::Storage
::config
();
326 PVE
::Storage
::volume_export
($cfg, $outfh, $param->{volume
}, $param->{format
},
327 $param->{snapshot
}, $param->{base
}, $with_snapshots);
330 if ($filename ne '-') {
332 unlink($filename) if $err;
339 __PACKAGE__-
>register_method ({
343 description
=> "Used internally to import a volume.",
346 additionalProperties
=> 0,
349 description
=> "Volume identifier",
351 completion
=> \
&PVE
::Storage
::complete_volume
,
354 description
=> "Import stream format",
356 enum
=> $PVE::Storage
::KNOWN_EXPORT_FORMATS
,
359 description
=> "Source file name. For '-' stdin is used, the " .
360 "tcp://<IP-or-CIDR> format allows to use a TCP connection, " .
361 "the unix://PATH-TO-SOCKET format a UNIX socket as input." .
362 "Else, the file is treated as common file.",
366 description
=> "Base snapshot of an incremental stream",
368 pattern
=> qr/[a-z0-9_\-]{1,40}/i,
372 'with-snapshots' => {
374 "Whether the stream includes intermediate snapshots",
379 'delete-snapshot' => {
380 description
=> "A snapshot to delete on success",
382 pattern
=> qr/[a-z0-9_\-]{1,80}/i,
387 description
=> "Choose a new volume ID if the requested " .
388 "volume ID already exists, instead of throwing an error.",
394 description
=> "The current-state snapshot if the stream contains snapshots",
396 pattern
=> qr/[a-z0-9_\-]{1,40}/i,
402 returns
=> { type
=> 'string' },
406 my $filename = $param->{filename
};
409 if ($filename eq '-') {
411 } elsif ($filename =~ m!^tcp://(([^/]+)(/\d+)?)$!) {
412 my ($cidr, $ip, $subnet) = ($1, $2, $3);
413 if ($subnet) { # got real CIDR notation, not just IP
414 my $ips = PVE
::Network
::get_local_ip_from_cidr
($cidr);
415 die "Unable to get any local IP address in network '$cidr'\n"
416 if scalar(@$ips) < 1;
417 die "Got multiple local IP address in network '$cidr'\n"
418 if scalar(@$ips) > 1;
422 my $family = PVE
::Tools
::get_host_address_family
($ip);
423 my $port = PVE
::Tools
::next_migrate_port
($family, $ip);
428 Proto
=> &Socket
::IPPROTO_TCP
,
429 GetAddrInfoFlags
=> 0,
433 my $socket = IO
::Socket
::IP-
>new(%$sock_params)
434 or die "failed to open socket: $!\n";
436 print "$ip\n$port\n"; # tell remote where to connect
439 my $prev_alarm = alarm 0;
440 local $SIG{ALRM
} = sub { die "timed out waiting for client\n" };
442 my $client = $socket->accept; # Wait for a client
447 } elsif ($filename =~ m!^unix://(.*)$!) {
448 my $socket_path = $1;
449 my $socket = IO
::Socket
::UNIX-
>new(
450 Type
=> SOCK_STREAM
(),
451 Local
=> $socket_path,
453 ) or die "failed to open socket: $!\n";
458 my $prev_alarm = alarm 0;
459 local $SIG{ALRM
} = sub { die "timed out waiting for client\n" };
461 my $client = $socket->accept; # Wait for a client
467 sysopen($infh, $filename, O_RDONLY
)
468 or die "open($filename): $!\n";
471 my $cfg = PVE
::Storage
::config
();
472 my $volume = $param->{volume
};
473 my $delete = $param->{'delete-snapshot'};
474 my $imported_volid = PVE
::Storage
::volume_import
($cfg, $infh, $volume, $param->{format
},
475 $param->{snapshot
}, $param->{base
}, $param->{'with-snapshots'},
476 $param->{'allow-rename'});
477 PVE
::Storage
::volume_snapshot_delete
($cfg, $imported_volid, $delete)
479 return $imported_volid;
483 __PACKAGE__-
>register_method ({
484 name
=> 'prunebackups',
485 path
=> 'prunebackups',
487 description
=> "Prune backups. Only those using the standard naming scheme are considered. " .
488 "If no keep options are specified, those from the storage configuration are used.",
492 additionalProperties
=> 0,
495 description
=> "Only show what would be pruned, don't delete anything.",
499 node
=> get_standard_option
('pve-node'),
500 storage
=> get_standard_option
('pve-storage-id', {
501 completion
=> \
&PVE
::Storage
::complete_storage_enabled
,
503 %{$PVE::Storage
::Plugin
::prune_backups_format
},
505 description
=> "Either 'qemu' or 'lxc'. Only consider backups for guests of this type.",
508 enum
=> ['qemu', 'lxc'],
510 vmid
=> get_standard_option
('pve-vmid', {
511 description
=> "Only consider backups for this guest.",
513 completion
=> \
&PVE
::Cluster
::complete_vmid
,
521 description
=> 'If it was a dry run or not. The list will only be defined in that case.',
530 description
=> "Backup volume ID.",
534 description
=> "Creation time of the backup (seconds since the UNIX epoch).",
538 description
=> "Whether the backup would be kept or removed. For backups that don't " .
539 "use the standard naming scheme, it's 'protected'.",
543 description
=> "One of 'qemu', 'lxc', 'openvz' or 'unknown'.",
547 description
=> "The VM the backup belongs to.",
559 my $dryrun = extract_param
($param, 'dry-run') ?
1 : 0;
562 foreach my $keep (keys %{$PVE::Storage
::Plugin
::prune_backups_format
}) {
563 $keep_opts->{$keep} = extract_param
($param, $keep) if defined($param->{$keep});
565 $param->{'prune-backups'} = PVE
::JSONSchema
::print_property_string
(
566 $keep_opts, $PVE::Storage
::Plugin
::prune_backups_format
) if $keep_opts;
570 $list = PVE
::API2
::Storage
::PruneBackups-
>dryrun($param);
572 PVE
::API2
::Storage
::PruneBackups-
>delete($param);
581 my $print_api_result = sub {
582 my ($data, $schema, $options) = @_;
583 PVE
::CLIFormatter
::print_api_result
($data, $schema, undef, $options);
587 add
=> [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ],
588 set
=> [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
589 remove
=> [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
590 status
=> [ "PVE::API2::Storage::Status", 'index', [],
591 { node
=> $nodename }, $print_status ],
592 list
=> [ "PVE::API2::Storage::Content", 'index', ['storage'],
593 { node
=> $nodename }, $print_content ],
594 alloc
=> [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'],
595 { node
=> $nodename }, sub {
597 print "successfully created '$volid'\n";
599 free
=> [ "PVE::API2::Storage::Content", 'delete', ['volume'],
600 { node
=> $nodename } ],
602 nfs
=> [ "PVE::API2::Storage::Scan", 'nfsscan', ['server'], { node
=> $nodename }, sub {
606 foreach my $rec (@$res) {
607 my $len = length ($rec->{path
});
608 $maxlen = $len if $len > $maxlen;
610 foreach my $rec (@$res) {
611 printf "%-${maxlen}s %s\n", $rec->{path
}, $rec->{options
};
614 cifs
=> [ "PVE::API2::Storage::Scan", 'cifsscan', ['server'], { node
=> $nodename }, sub {
618 foreach my $rec (@$res) {
619 my $len = length ($rec->{share
});
620 $maxlen = $len if $len > $maxlen;
622 foreach my $rec (@$res) {
623 printf "%-${maxlen}s %s\n", $rec->{share
}, $rec->{description
};
626 glusterfs
=> [ "PVE::API2::Storage::Scan", 'glusterfsscan', ['server'], { node
=> $nodename }, sub {
629 foreach my $rec (@$res) {
630 printf "%s\n", $rec->{volname
};
633 iscsi
=> [ "PVE::API2::Storage::Scan", 'iscsiscan', ['portal'], { node
=> $nodename }, sub {
637 foreach my $rec (@$res) {
638 my $len = length ($rec->{target
});
639 $maxlen = $len if $len > $maxlen;
641 foreach my $rec (@$res) {
642 printf "%-${maxlen}s %s\n", $rec->{target
}, $rec->{portal
};
645 lvm
=> [ "PVE::API2::Storage::Scan", 'lvmscan', [], { node
=> $nodename }, sub {
647 foreach my $rec (@$res) {
648 printf "$rec->{vg}\n";
651 lvmthin
=> [ "PVE::API2::Storage::Scan", 'lvmthinscan', ['vg'], { node
=> $nodename }, sub {
653 foreach my $rec (@$res) {
654 printf "$rec->{lv}\n";
658 "PVE::API2::Storage::Scan",
660 ['server', 'username'],
661 { node
=> $nodename },
663 $PVE::RESTHandler
::standard_output_options
,
665 zfs
=> [ "PVE::API2::Storage::Scan", 'zfsscan', [], { node
=> $nodename }, sub {
668 foreach my $rec (@$res) {
669 printf "$rec->{pool}\n";
673 nfsscan
=> { alias
=> 'scan nfs' },
674 cifsscan
=> { alias
=> 'scan cifs' },
675 glusterfsscan
=> { alias
=> 'scan glusterfs' },
676 iscsiscan
=> { alias
=> 'scan iscsi' },
677 lvmscan
=> { alias
=> 'scan lvm' },
678 lvmthinscan
=> { alias
=> 'scan lvmthin' },
679 zfsscan
=> { alias
=> 'scan zfs' },
680 path
=> [ __PACKAGE__
, 'path', ['volume']],
681 extractconfig
=> [__PACKAGE__
, 'extractconfig', ['volume']],
682 export
=> [ __PACKAGE__
, 'export', ['volume', 'format', 'filename']],
683 import
=> [ __PACKAGE__
, 'import', ['volume', 'format', 'filename'], {}, sub {
685 print PVE
::Storage
::volume_imported_message
($volid);
687 apiinfo
=> [ __PACKAGE__
, 'apiinfo', [], {}, sub {
690 print "APIVER $res->{apiver}\n";
691 print "APIAGE $res->{apiage}\n";
693 'prune-backups' => [ __PACKAGE__
, 'prunebackups', ['storage'], { node
=> $nodename }, sub {
696 my ($dryrun, $list) = ($res->{dryrun
}, $res->{list
});
700 if (!scalar(@{$list})) {
701 print "No backups found\n";
705 print "NOTE: this is only a preview and might not be what a subsequent\n" .
706 "prune call does if backups are removed/added in the meantime.\n\n";
709 my $vmcmp = PVE
::Tools
::safe_compare
($a->{vmid
}, $b->{vmid
}, sub { $_[0] <=> $_[1] });
710 return $vmcmp if $vmcmp ne 0;
711 return $a->{ctime
} <=> $b->{ctime
};
715 foreach my $backup (@sorted) {
716 my $volid = $backup->{volid
};
717 $maxlen = length($volid) if length($volid) > $maxlen;
721 printf("%-${maxlen}s %15s %10s\n", 'Backup', 'Backup-ID', 'Prune-Mark');
722 foreach my $backup (@sorted) {
723 my $type = $backup->{type
};
724 my $vmid = $backup->{vmid
};
725 my $backup_id = defined($vmid) ?
"$type/$vmid" : "$type";
726 printf("%-${maxlen}s %15s %10s\n", $backup->{volid
}, $backup_id, $backup->{mark
});