]>
git.proxmox.com Git - pve-storage.git/blob - PVE/CLI/pvesm.pm
003b019e9af769561234e04dde5479d00ba95271
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 $KNOWN_EXPORT_FORMATS = ['raw+size', 'tar+size', 'qcow2+size', 'vmdk+size', 'zfs', 'btrfs'];
35 my $nodename = PVE
::INotify
::nodename
();
40 my $password_map = PVE
::CLIHandler
::get_standard_mapping
('pve-password', {
43 return $value if $value;
44 return PVE
::PTY
::read_password
("Enter Password: ");
49 name
=> 'encryption-key',
50 desc
=> 'a file containing an encryption key, or the special value "autogen"',
53 return $value if $value eq 'autogen';
54 return PVE
::Tools
::file_get_contents
($value);
58 my $master_key_map = {
59 name
=> 'master-pubkey',
60 desc
=> 'a file containing a PEM-formatted master public key',
63 return encode_base64
(PVE
::Tools
::file_get_contents
($value), '');
69 desc
=> 'file containing the keyring to authenticate in the Ceph cluster',
72 return PVE
::Tools
::file_get_contents
($value);
77 'cifsscan' => [ $password_map ],
78 'cifs' => [ $password_map ],
79 'pbs' => [ $password_map ],
80 'create' => [ $password_map, $enc_key_map, $master_key_map, $keyring_map ],
81 'update' => [ $password_map, $enc_key_map, $master_key_map, $keyring_map ],
83 return $mapping->{$name};
86 sub setup_environment
{
87 PVE
::RPCEnvironment-
>setup_default_cli_env();
90 __PACKAGE__-
>register_method ({
94 description
=> "Returns APIVER and APIAGE.",
96 additionalProperties
=> 0,
102 apiver
=> { type
=> 'integer' },
103 apiage
=> { type
=> 'integer' },
108 apiver
=> PVE
::Storage
::APIVER
,
109 apiage
=> PVE
::Storage
::APIAGE
,
114 __PACKAGE__-
>register_method ({
118 description
=> "Get filesystem path for specified volume",
120 additionalProperties
=> 0,
123 description
=> "Volume identifier",
124 type
=> 'string', format
=> 'pve-volume-id',
125 completion
=> \
&PVE
::Storage
::complete_volume
,
129 returns
=> { type
=> 'null' },
134 my $cfg = PVE
::Storage
::config
();
136 my $path = PVE
::Storage
::path
($cfg, $param->{volume
});
144 __PACKAGE__-
>register_method ({
145 name
=> 'extractconfig',
146 path
=> 'extractconfig',
148 description
=> "Extract configuration from vzdump backup archive.",
150 description
=> "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
155 additionalProperties
=> 0,
158 description
=> "Volume identifier",
160 completion
=> \
&PVE
::Storage
::complete_volume
,
164 returns
=> { type
=> 'null' },
167 my $volume = $param->{volume
};
169 my $rpcenv = PVE
::RPCEnvironment
::get
();
170 my $authuser = $rpcenv->get_user();
172 my $storage_cfg = PVE
::Storage
::config
();
173 PVE
::Storage
::check_volume_access
(
182 if (PVE
::Storage
::parse_volume_id
($volume, 1)) {
183 my (undef, undef, $ownervm) = PVE
::Storage
::parse_volname
($storage_cfg, $volume);
184 $rpcenv->check($authuser, "/vms/$ownervm", ['VM.Backup']);
187 my $config_raw = PVE
::Storage
::extract_vzdump_config
($storage_cfg, $volume);
189 print "$config_raw\n";
193 my $print_content = sub {
196 my ($maxlenname, $maxsize) = (0, 0);
197 foreach my $info (@$list) {
198 my $volid = $info->{volid
};
199 my $sidlen = length ($volid);
200 $maxlenname = $sidlen if $sidlen > $maxlenname;
201 $maxsize = $info->{size
} if ($info->{size
} // 0) > $maxsize;
203 my $sizemaxdigits = length($maxsize);
205 my $basefmt = "%-${maxlenname}s %-7s %-9s %${sizemaxdigits}s";
206 printf "$basefmt %s\n", "Volid", "Format", "Type", "Size", "VMID";
208 foreach my $info (@$list) {
209 next if !$info->{vmid
};
210 my $volid = $info->{volid
};
212 printf "$basefmt %d\n", $volid, $info->{format
}, $info->{content
}, $info->{size
}, $info->{vmid
};
215 foreach my $info (sort { $a->{format
} cmp $b->{format
} } @$list) {
216 next if $info->{vmid
};
217 my $volid = $info->{volid
};
219 printf "$basefmt\n", $volid, $info->{format
}, $info->{content
}, $info->{size
};
223 my $print_status = sub {
227 foreach my $res (@$res) {
228 my $storeid = $res->{storage
};
229 $maxlen = length ($storeid) if length ($storeid) > $maxlen;
233 printf "%-${maxlen}s %10s %10s %15s %15s %15s %8s\n", 'Name', 'Type',
234 'Status', 'Total', 'Used', 'Available', '%';
236 foreach my $res (sort { $a->{storage
} cmp $b->{storage
} } @$res) {
237 my $storeid = $res->{storage
};
239 my $active = $res->{active
} ?
'active' : 'inactive';
240 my ($per, $per_fmt) = (0, '% 7.2f%%');
241 $per = ($res->{used
}*100)/$res->{total
} if $res->{total
} > 0;
243 if (!$res->{enabled
}) {
246 $active = 'disabled';
249 printf "%-${maxlen}s %10s %10s %15d %15d %15d $per_fmt\n", $storeid,
250 $res->{type
}, $active, $res->{total
}/1024, $res->{used}/1024,
251 $res->{avail
}/1024, $per;
255 __PACKAGE__-
>register_method ({
259 description
=> "Used internally to export a volume.",
262 additionalProperties
=> 0,
265 description
=> "Volume identifier",
267 completion
=> \
&PVE
::Storage
::complete_volume
,
270 description
=> "Export stream format",
272 enum
=> $KNOWN_EXPORT_FORMATS,
275 description
=> "Destination file name",
279 description
=> "Snapshot to start an incremental stream from",
281 pattern
=> qr/[a-z0-9_\-]{1,40}/i,
286 description
=> "Snapshot to export",
288 pattern
=> qr/[a-z0-9_\-]{1,40}/i,
292 'with-snapshots' => {
294 "Whether to include intermediate snapshots in the stream",
300 description
=> "Ordered list of snapshots to transfer",
302 format
=> 'string-list',
307 returns
=> { type
=> 'null' },
311 my $with_snapshots = $param->{'with-snapshots'};
312 if (defined(my $list = $param->{'snapshot-list'})) {
313 $with_snapshots = PVE
::Tools
::split_list
($list);
316 my $filename = $param->{filename
};
319 if ($filename eq '-') {
322 sysopen($outfh, $filename, O_CREAT
|O_WRONLY
|O_TRUNC
)
323 or die "open($filename): $!\n";
327 my $cfg = PVE
::Storage
::config
();
328 PVE
::Storage
::volume_export
($cfg, $outfh, $param->{volume
}, $param->{format
},
329 $param->{snapshot
}, $param->{base
}, $with_snapshots);
332 if ($filename ne '-') {
334 unlink($filename) if $err;
341 __PACKAGE__-
>register_method ({
345 description
=> "Used internally to import a volume.",
348 additionalProperties
=> 0,
351 description
=> "Volume identifier",
353 completion
=> \
&PVE
::Storage
::complete_volume
,
356 description
=> "Import stream format",
358 enum
=> $KNOWN_EXPORT_FORMATS,
361 description
=> "Source file name. For '-' stdin is used, the " .
362 "tcp://<IP-or-CIDR> format allows to use a TCP connection, " .
363 "the unix://PATH-TO-SOCKET format a UNIX socket as input." .
364 "Else, the file is treated as common file.",
368 description
=> "Base snapshot of an incremental stream",
370 pattern
=> qr/[a-z0-9_\-]{1,40}/i,
374 'with-snapshots' => {
376 "Whether the stream includes intermediate snapshots",
381 'delete-snapshot' => {
382 description
=> "A snapshot to delete on success",
384 pattern
=> qr/[a-z0-9_\-]{1,80}/i,
389 description
=> "Choose a new volume ID if the requested " .
390 "volume ID already exists, instead of throwing an error.",
396 description
=> "The current-state snapshot if the stream contains snapshots",
398 pattern
=> qr/[a-z0-9_\-]{1,40}/i,
404 returns
=> { type
=> 'string' },
408 my $filename = $param->{filename
};
411 if ($filename eq '-') {
413 } elsif ($filename =~ m!^tcp://(([^/]+)(/\d+)?)$!) {
414 my ($cidr, $ip, $subnet) = ($1, $2, $3);
415 if ($subnet) { # got real CIDR notation, not just IP
416 my $ips = PVE
::Network
::get_local_ip_from_cidr
($cidr);
417 die "Unable to get any local IP address in network '$cidr'\n"
418 if scalar(@$ips) < 1;
419 die "Got multiple local IP address in network '$cidr'\n"
420 if scalar(@$ips) > 1;
424 my $family = PVE
::Tools
::get_host_address_family
($ip);
425 my $port = PVE
::Tools
::next_migrate_port
($family, $ip);
430 Proto
=> &Socket
::IPPROTO_TCP
,
431 GetAddrInfoFlags
=> 0,
435 my $socket = IO
::Socket
::IP-
>new(%$sock_params)
436 or die "failed to open socket: $!\n";
438 print "$ip\n$port\n"; # tell remote where to connect
441 my $prev_alarm = alarm 0;
442 local $SIG{ALRM
} = sub { die "timed out waiting for client\n" };
444 my $client = $socket->accept; # Wait for a client
449 } elsif ($filename =~ m!^unix://(.*)$!) {
450 my $socket_path = $1;
451 my $socket = IO
::Socket
::UNIX-
>new(
452 Type
=> SOCK_STREAM
(),
453 Local
=> $socket_path,
455 ) or die "failed to open socket: $!\n";
460 my $prev_alarm = alarm 0;
461 local $SIG{ALRM
} = sub { die "timed out waiting for client\n" };
463 my $client = $socket->accept; # Wait for a client
469 sysopen($infh, $filename, O_RDONLY
)
470 or die "open($filename): $!\n";
473 my $cfg = PVE
::Storage
::config
();
474 my $volume = $param->{volume
};
475 my $delete = $param->{'delete-snapshot'};
476 my $imported_volid = PVE
::Storage
::volume_import
($cfg, $infh, $volume, $param->{format
},
477 $param->{snapshot
}, $param->{base
}, $param->{'with-snapshots'},
478 $param->{'allow-rename'});
479 PVE
::Storage
::volume_snapshot_delete
($cfg, $imported_volid, $delete)
481 return $imported_volid;
485 __PACKAGE__-
>register_method ({
486 name
=> 'prunebackups',
487 path
=> 'prunebackups',
489 description
=> "Prune backups. Only those using the standard naming scheme are considered. " .
490 "If no keep options are specified, those from the storage configuration are used.",
494 additionalProperties
=> 0,
497 description
=> "Only show what would be pruned, don't delete anything.",
501 node
=> get_standard_option
('pve-node'),
502 storage
=> get_standard_option
('pve-storage-id', {
503 completion
=> \
&PVE
::Storage
::complete_storage_enabled
,
505 %{$PVE::Storage
::Plugin
::prune_backups_format
},
507 description
=> "Either 'qemu' or 'lxc'. Only consider backups for guests of this type.",
510 enum
=> ['qemu', 'lxc'],
512 vmid
=> get_standard_option
('pve-vmid', {
513 description
=> "Only consider backups for this guest.",
515 completion
=> \
&PVE
::Cluster
::complete_vmid
,
523 description
=> 'If it was a dry run or not. The list will only be defined in that case.',
532 description
=> "Backup volume ID.",
536 description
=> "Creation time of the backup (seconds since the UNIX epoch).",
540 description
=> "Whether the backup would be kept or removed. For backups that don't " .
541 "use the standard naming scheme, it's 'protected'.",
545 description
=> "One of 'qemu', 'lxc', 'openvz' or 'unknown'.",
549 description
=> "The VM the backup belongs to.",
561 my $dryrun = extract_param
($param, 'dry-run') ?
1 : 0;
564 foreach my $keep (keys %{$PVE::Storage
::Plugin
::prune_backups_format
}) {
565 $keep_opts->{$keep} = extract_param
($param, $keep) if defined($param->{$keep});
567 $param->{'prune-backups'} = PVE
::JSONSchema
::print_property_string
(
568 $keep_opts, $PVE::Storage
::Plugin
::prune_backups_format
) if $keep_opts;
572 $list = PVE
::API2
::Storage
::PruneBackups-
>dryrun($param);
574 PVE
::API2
::Storage
::PruneBackups-
>delete($param);
583 my $print_api_result = sub {
584 my ($data, $schema, $options) = @_;
585 PVE
::CLIFormatter
::print_api_result
($data, $schema, undef, $options);
589 add
=> [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ],
590 set
=> [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
591 remove
=> [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
592 status
=> [ "PVE::API2::Storage::Status", 'index', [],
593 { node
=> $nodename }, $print_status ],
594 list
=> [ "PVE::API2::Storage::Content", 'index', ['storage'],
595 { node
=> $nodename }, $print_content ],
596 alloc
=> [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'],
597 { node
=> $nodename }, sub {
599 print "successfully created '$volid'\n";
601 free
=> [ "PVE::API2::Storage::Content", 'delete', ['volume'],
602 { node
=> $nodename } ],
604 nfs
=> [ "PVE::API2::Storage::Scan", 'nfsscan', ['server'], { node
=> $nodename }, sub {
608 foreach my $rec (@$res) {
609 my $len = length ($rec->{path
});
610 $maxlen = $len if $len > $maxlen;
612 foreach my $rec (@$res) {
613 printf "%-${maxlen}s %s\n", $rec->{path
}, $rec->{options
};
616 cifs
=> [ "PVE::API2::Storage::Scan", 'cifsscan', ['server'], { node
=> $nodename }, sub {
620 foreach my $rec (@$res) {
621 my $len = length ($rec->{share
});
622 $maxlen = $len if $len > $maxlen;
624 foreach my $rec (@$res) {
625 printf "%-${maxlen}s %s\n", $rec->{share
}, $rec->{description
};
628 glusterfs
=> [ "PVE::API2::Storage::Scan", 'glusterfsscan', ['server'], { node
=> $nodename }, sub {
631 foreach my $rec (@$res) {
632 printf "%s\n", $rec->{volname
};
635 iscsi
=> [ "PVE::API2::Storage::Scan", 'iscsiscan', ['portal'], { node
=> $nodename }, sub {
639 foreach my $rec (@$res) {
640 my $len = length ($rec->{target
});
641 $maxlen = $len if $len > $maxlen;
643 foreach my $rec (@$res) {
644 printf "%-${maxlen}s %s\n", $rec->{target
}, $rec->{portal
};
647 lvm
=> [ "PVE::API2::Storage::Scan", 'lvmscan', [], { node
=> $nodename }, sub {
649 foreach my $rec (@$res) {
650 printf "$rec->{vg}\n";
653 lvmthin
=> [ "PVE::API2::Storage::Scan", 'lvmthinscan', ['vg'], { node
=> $nodename }, sub {
655 foreach my $rec (@$res) {
656 printf "$rec->{lv}\n";
660 "PVE::API2::Storage::Scan",
662 ['server', 'username'],
663 { node
=> $nodename },
665 $PVE::RESTHandler
::standard_output_options
,
667 zfs
=> [ "PVE::API2::Storage::Scan", 'zfsscan', [], { node
=> $nodename }, sub {
670 foreach my $rec (@$res) {
671 printf "$rec->{pool}\n";
675 nfsscan
=> { alias
=> 'scan nfs' },
676 cifsscan
=> { alias
=> 'scan cifs' },
677 glusterfsscan
=> { alias
=> 'scan glusterfs' },
678 iscsiscan
=> { alias
=> 'scan iscsi' },
679 lvmscan
=> { alias
=> 'scan lvm' },
680 lvmthinscan
=> { alias
=> 'scan lvmthin' },
681 zfsscan
=> { alias
=> 'scan zfs' },
682 path
=> [ __PACKAGE__
, 'path', ['volume']],
683 extractconfig
=> [__PACKAGE__
, 'extractconfig', ['volume']],
684 export
=> [ __PACKAGE__
, 'export', ['volume', 'format', 'filename']],
685 import
=> [ __PACKAGE__
, 'import', ['volume', 'format', 'filename'], {}, sub {
687 print PVE
::Storage
::volume_imported_message
($volid);
689 apiinfo
=> [ __PACKAGE__
, 'apiinfo', [], {}, sub {
692 print "APIVER $res->{apiver}\n";
693 print "APIAGE $res->{apiage}\n";
695 'prune-backups' => [ __PACKAGE__
, 'prunebackups', ['storage'], { node
=> $nodename }, sub {
698 my ($dryrun, $list) = ($res->{dryrun
}, $res->{list
});
702 if (!scalar(@{$list})) {
703 print "No backups found\n";
707 print "NOTE: this is only a preview and might not be what a subsequent\n" .
708 "prune call does if backups are removed/added in the meantime.\n\n";
711 my $vmcmp = PVE
::Tools
::safe_compare
($a->{vmid
}, $b->{vmid
}, sub { $_[0] <=> $_[1] });
712 return $vmcmp if $vmcmp ne 0;
713 return $a->{ctime
} <=> $b->{ctime
};
717 foreach my $backup (@sorted) {
718 my $volid = $backup->{volid
};
719 $maxlen = length($volid) if length($volid) > $maxlen;
723 printf("%-${maxlen}s %15s %10s\n", 'Backup', 'Backup-ID', 'Prune-Mark');
724 foreach my $backup (@sorted) {
725 my $type = $backup->{type
};
726 my $vmid = $backup->{vmid
};
727 my $backup_id = defined($vmid) ?
"$type/$vmid" : "$type";
728 printf("%-${maxlen}s %15s %10s\n", $backup->{volid
}, $backup_id, $backup->{mark
});