]>
git.proxmox.com Git - pve-storage.git/blob - PVE/CLI/pvesm.pm
9206188fc58b4fbe0e1d33a33c3fbf5930289548
1 package PVE
::CLI
::pvesm
;
6 use POSIX
qw(O_RDONLY O_WRONLY O_CREAT O_TRUNC);
12 use Socket
qw(SOCK_STREAM);
17 use PVE
::RPCEnvironment
;
19 use PVE
::Tools
qw(extract_param);
20 use PVE
::API2
::Storage
::Config
;
21 use PVE
::API2
::Storage
::Content
;
22 use PVE
::API2
::Storage
::PruneBackups
;
23 use PVE
::API2
::Storage
::Scan
;
24 use PVE
::API2
::Storage
::Status
;
25 use PVE
::JSONSchema
qw(get_standard_option);
30 use base
qw(PVE::CLIHandler);
32 my $KNOWN_EXPORT_FORMATS = ['raw+size', 'tar+size', 'qcow2+size', 'vmdk+size', 'zfs'];
34 my $nodename = PVE
::INotify
::nodename
();
39 my $password_map = PVE
::CLIHandler
::get_standard_mapping
('pve-password', {
42 return $value if $value;
43 return PVE
::PTY
::read_password
("Enter Password: ");
48 name
=> 'encryption-key',
49 desc
=> 'a file containing an encryption key, or the special value "autogen"',
52 return $value if $value eq 'autogen';
53 return PVE
::Tools
::file_get_contents
($value);
59 'cifsscan' => [ $password_map ],
60 'cifs' => [ $password_map ],
61 'pbs' => [ $password_map ],
62 'create' => [ $password_map, $enc_key_map ],
63 'update' => [ $password_map, $enc_key_map ],
65 return $mapping->{$name};
68 sub setup_environment
{
69 PVE
::RPCEnvironment-
>setup_default_cli_env();
72 __PACKAGE__-
>register_method ({
76 description
=> "Returns APIVER and APIAGE.",
78 additionalProperties
=> 0,
84 apiver
=> { type
=> 'integer' },
85 apiage
=> { type
=> 'integer' },
90 apiver
=> PVE
::Storage
::APIVER
,
91 apiage
=> PVE
::Storage
::APIAGE
,
96 __PACKAGE__-
>register_method ({
100 description
=> "Get filesystem path for specified volume",
102 additionalProperties
=> 0,
105 description
=> "Volume identifier",
106 type
=> 'string', format
=> 'pve-volume-id',
107 completion
=> \
&PVE
::Storage
::complete_volume
,
111 returns
=> { type
=> 'null' },
116 my $cfg = PVE
::Storage
::config
();
118 my $path = PVE
::Storage
::path
($cfg, $param->{volume
});
126 __PACKAGE__-
>register_method ({
127 name
=> 'extractconfig',
128 path
=> 'extractconfig',
130 description
=> "Extract configuration from vzdump backup archive.",
132 description
=> "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
137 additionalProperties
=> 0,
140 description
=> "Volume identifier",
142 completion
=> \
&PVE
::Storage
::complete_volume
,
146 returns
=> { type
=> 'null' },
149 my $volume = $param->{volume
};
151 my $rpcenv = PVE
::RPCEnvironment
::get
();
152 my $authuser = $rpcenv->get_user();
154 my $storage_cfg = PVE
::Storage
::config
();
155 PVE
::Storage
::check_volume_access
($rpcenv, $authuser, $storage_cfg, undef, $volume);
157 my $config_raw = PVE
::Storage
::extract_vzdump_config
($storage_cfg, $volume);
159 print "$config_raw\n";
163 my $print_content = sub {
166 my ($maxlenname, $maxsize) = (0, 0);
167 foreach my $info (@$list) {
168 my $volid = $info->{volid
};
169 my $sidlen = length ($volid);
170 $maxlenname = $sidlen if $sidlen > $maxlenname;
171 $maxsize = $info->{size
} if ($info->{size
} // 0) > $maxsize;
173 my $sizemaxdigits = length($maxsize);
175 my $basefmt = "%-${maxlenname}s %-7s %-9s %${sizemaxdigits}s";
176 printf "$basefmt %s\n", "Volid", "Format", "Type", "Size", "VMID";
178 foreach my $info (@$list) {
179 next if !$info->{vmid
};
180 my $volid = $info->{volid
};
182 printf "$basefmt %d\n", $volid, $info->{format
}, $info->{content
}, $info->{size
}, $info->{vmid
};
185 foreach my $info (sort { $a->{format
} cmp $b->{format
} } @$list) {
186 next if $info->{vmid
};
187 my $volid = $info->{volid
};
189 printf "$basefmt\n", $volid, $info->{format
}, $info->{content
}, $info->{size
};
193 my $print_status = sub {
197 foreach my $res (@$res) {
198 my $storeid = $res->{storage
};
199 $maxlen = length ($storeid) if length ($storeid) > $maxlen;
203 printf "%-${maxlen}s %10s %10s %15s %15s %15s %8s\n", 'Name', 'Type',
204 'Status', 'Total', 'Used', 'Available', '%';
206 foreach my $res (sort { $a->{storage
} cmp $b->{storage
} } @$res) {
207 my $storeid = $res->{storage
};
209 my $active = $res->{active
} ?
'active' : 'inactive';
210 my ($per, $per_fmt) = (0, '% 7.2f%%');
211 $per = ($res->{used
}*100)/$res->{total
} if $res->{total
} > 0;
213 if (!$res->{enabled
}) {
216 $active = 'disabled';
219 printf "%-${maxlen}s %10s %10s %15d %15d %15d $per_fmt\n", $storeid,
220 $res->{type
}, $active, $res->{total
}/1024, $res->{used}/1024,
221 $res->{avail
}/1024, $per;
225 __PACKAGE__-
>register_method ({
229 description
=> "Used internally to export a volume.",
232 additionalProperties
=> 0,
235 description
=> "Volume identifier",
237 completion
=> \
&PVE
::Storage
::complete_volume
,
240 description
=> "Export stream format",
242 enum
=> $KNOWN_EXPORT_FORMATS,
245 description
=> "Destination file name",
249 description
=> "Snapshot to start an incremental stream from",
251 pattern
=> qr/[a-z0-9_\-]{1,40}/i,
256 description
=> "Snapshot to export",
258 pattern
=> qr/[a-z0-9_\-]{1,40}/i,
262 'with-snapshots' => {
264 "Whether to include intermediate snapshots in the stream",
271 returns
=> { type
=> 'null' },
275 my $filename = $param->{filename
};
278 if ($filename eq '-') {
281 sysopen($outfh, $filename, O_CREAT
|O_WRONLY
|O_TRUNC
)
282 or die "open($filename): $!\n";
286 my $cfg = PVE
::Storage
::config
();
287 PVE
::Storage
::volume_export
($cfg, $outfh, $param->{volume
}, $param->{format
},
288 $param->{snapshot
}, $param->{base
}, $param->{'with-snapshots'});
291 if ($filename ne '-') {
293 unlink($filename) if $err;
300 __PACKAGE__-
>register_method ({
304 description
=> "Used internally to import a volume.",
307 additionalProperties
=> 0,
310 description
=> "Volume identifier",
312 completion
=> \
&PVE
::Storage
::complete_volume
,
315 description
=> "Import stream format",
317 enum
=> $KNOWN_EXPORT_FORMATS,
320 description
=> "Source file name. For '-' stdin is used, the " .
321 "tcp://<IP-or-CIDR> format allows to use a TCP connection, " .
322 "the unix://PATH-TO-SOCKET format a UNIX socket as input." .
323 "Else, the file is treated as common file.",
327 description
=> "Base snapshot of an incremental stream",
329 pattern
=> qr/[a-z0-9_\-]{1,40}/i,
333 'with-snapshots' => {
335 "Whether the stream includes intermediate snapshots",
340 'delete-snapshot' => {
341 description
=> "A snapshot to delete on success",
343 pattern
=> qr/[a-z0-9_\-]{1,80}/i,
348 description
=> "Choose a new volume ID if the requested " .
349 "volume ID already exists, instead of throwing an error.",
356 returns
=> { type
=> 'string' },
360 my $filename = $param->{filename
};
363 if ($filename eq '-') {
365 } elsif ($filename =~ m!^tcp://(([^/]+)(/\d+)?)$!) {
366 my ($cidr, $ip, $subnet) = ($1, $2, $3);
367 if ($subnet) { # got real CIDR notation, not just IP
368 my $ips = PVE
::Network
::get_local_ip_from_cidr
($cidr);
369 die "Unable to get any local IP address in network '$cidr'\n"
370 if scalar(@$ips) < 1;
371 die "Got multiple local IP address in network '$cidr'\n"
372 if scalar(@$ips) > 1;
376 my $family = PVE
::Tools
::get_host_address_family
($ip);
377 my $port = PVE
::Tools
::next_migrate_port
($family, $ip);
382 Proto
=> &Socket
::IPPROTO_TCP
,
383 GetAddrInfoFlags
=> 0,
387 my $socket = IO
::Socket
::IP-
>new(%$sock_params)
388 or die "failed to open socket: $!\n";
390 print "$ip\n$port\n"; # tell remote where to connect
393 my $prev_alarm = alarm 0;
394 local $SIG{ALRM
} = sub { die "timed out waiting for client\n" };
396 my $client = $socket->accept; # Wait for a client
401 } elsif ($filename =~ m!^unix://(.*)$!) {
402 my $socket_path = $1;
403 my $socket = IO
::Socket
::UNIX-
>new(
404 Type
=> SOCK_STREAM
(),
405 Local
=> $socket_path,
407 ) or die "failed to open socket: $!\n";
412 my $prev_alarm = alarm 0;
413 local $SIG{ALRM
} = sub { die "timed out waiting for client\n" };
415 my $client = $socket->accept; # Wait for a client
421 sysopen($infh, $filename, O_RDONLY
)
422 or die "open($filename): $!\n";
425 my $cfg = PVE
::Storage
::config
();
426 my $volume = $param->{volume
};
427 my $delete = $param->{'delete-snapshot'};
428 my $imported_volid = PVE
::Storage
::volume_import
($cfg, $infh, $volume, $param->{format
},
429 $param->{base
}, $param->{'with-snapshots'}, $param->{'allow-rename'});
430 PVE
::Storage
::volume_snapshot_delete
($cfg, $imported_volid, $delete)
432 return $imported_volid;
436 __PACKAGE__-
>register_method ({
437 name
=> 'prunebackups',
438 path
=> 'prunebackups',
440 description
=> "Prune backups. Only those using the standard naming scheme are considered. " .
441 "If no keep options are specified, those from the storage configuration are used.",
445 additionalProperties
=> 0,
448 description
=> "Only show what would be pruned, don't delete anything.",
452 node
=> get_standard_option
('pve-node'),
453 storage
=> get_standard_option
('pve-storage-id', {
454 completion
=> \
&PVE
::Storage
::complete_storage_enabled
,
456 %{$PVE::Storage
::Plugin
::prune_backups_format
},
458 description
=> "Either 'qemu' or 'lxc'. Only consider backups for guests of this type.",
461 enum
=> ['qemu', 'lxc'],
463 vmid
=> get_standard_option
('pve-vmid', {
464 description
=> "Only consider backups for this guest.",
466 completion
=> \
&PVE
::Cluster
::complete_vmid
,
474 description
=> 'If it was a dry run or not. The list will only be defined in that case.',
483 description
=> "Backup volume ID.",
487 description
=> "Creation time of the backup (seconds since the UNIX epoch).",
491 description
=> "Whether the backup would be kept or removed. For backups that don't " .
492 "use the standard naming scheme, it's 'protected'.",
496 description
=> "One of 'qemu', 'lxc', 'openvz' or 'unknown'.",
500 description
=> "The VM the backup belongs to.",
512 my $dryrun = extract_param
($param, 'dry-run') ?
1 : 0;
515 foreach my $keep (keys %{$PVE::Storage
::Plugin
::prune_backups_format
}) {
516 $keep_opts->{$keep} = extract_param
($param, $keep) if defined($param->{$keep});
518 $param->{'prune-backups'} = PVE
::JSONSchema
::print_property_string
(
519 $keep_opts, $PVE::Storage
::Plugin
::prune_backups_format
) if $keep_opts;
523 $list = PVE
::API2
::Storage
::PruneBackups-
>dryrun($param);
525 PVE
::API2
::Storage
::PruneBackups-
>delete($param);
534 my $print_api_result = sub {
535 my ($data, $schema, $options) = @_;
536 PVE
::CLIFormatter
::print_api_result
($data, $schema, undef, $options);
540 add
=> [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ],
541 set
=> [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
542 remove
=> [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
543 status
=> [ "PVE::API2::Storage::Status", 'index', [],
544 { node
=> $nodename }, $print_status ],
545 list
=> [ "PVE::API2::Storage::Content", 'index', ['storage'],
546 { node
=> $nodename }, $print_content ],
547 alloc
=> [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'],
548 { node
=> $nodename }, sub {
550 print "successfully created '$volid'\n";
552 free
=> [ "PVE::API2::Storage::Content", 'delete', ['volume'],
553 { node
=> $nodename } ],
555 nfs
=> [ "PVE::API2::Storage::Scan", 'nfsscan', ['server'], { node
=> $nodename }, sub {
559 foreach my $rec (@$res) {
560 my $len = length ($rec->{path
});
561 $maxlen = $len if $len > $maxlen;
563 foreach my $rec (@$res) {
564 printf "%-${maxlen}s %s\n", $rec->{path
}, $rec->{options
};
567 cifs
=> [ "PVE::API2::Storage::Scan", 'cifsscan', ['server'], { node
=> $nodename }, sub {
571 foreach my $rec (@$res) {
572 my $len = length ($rec->{share
});
573 $maxlen = $len if $len > $maxlen;
575 foreach my $rec (@$res) {
576 printf "%-${maxlen}s %s\n", $rec->{share
}, $rec->{description
};
579 glusterfs
=> [ "PVE::API2::Storage::Scan", 'glusterfsscan', ['server'], { node
=> $nodename }, sub {
582 foreach my $rec (@$res) {
583 printf "%s\n", $rec->{volname
};
586 iscsi
=> [ "PVE::API2::Storage::Scan", 'iscsiscan', ['portal'], { node
=> $nodename }, sub {
590 foreach my $rec (@$res) {
591 my $len = length ($rec->{target
});
592 $maxlen = $len if $len > $maxlen;
594 foreach my $rec (@$res) {
595 printf "%-${maxlen}s %s\n", $rec->{target
}, $rec->{portal
};
598 lvm
=> [ "PVE::API2::Storage::Scan", 'lvmscan', [], { node
=> $nodename }, sub {
600 foreach my $rec (@$res) {
601 printf "$rec->{vg}\n";
604 lvmthin
=> [ "PVE::API2::Storage::Scan", 'lvmthinscan', ['vg'], { node
=> $nodename }, sub {
606 foreach my $rec (@$res) {
607 printf "$rec->{lv}\n";
611 "PVE::API2::Storage::Scan",
613 ['server', 'username'],
614 { node
=> $nodename },
616 $PVE::RESTHandler
::standard_output_options
,
618 zfs
=> [ "PVE::API2::Storage::Scan", 'zfsscan', [], { node
=> $nodename }, sub {
621 foreach my $rec (@$res) {
622 printf "$rec->{pool}\n";
626 nfsscan
=> { alias
=> 'scan nfs' },
627 cifsscan
=> { alias
=> 'scan cifs' },
628 glusterfsscan
=> { alias
=> 'scan glusterfs' },
629 iscsiscan
=> { alias
=> 'scan iscsi' },
630 lvmscan
=> { alias
=> 'scan lvm' },
631 lvmthinscan
=> { alias
=> 'scan lvmthin' },
632 zfsscan
=> { alias
=> 'scan zfs' },
633 path
=> [ __PACKAGE__
, 'path', ['volume']],
634 extractconfig
=> [__PACKAGE__
, 'extractconfig', ['volume']],
635 export
=> [ __PACKAGE__
, 'export', ['volume', 'format', 'filename']],
636 import
=> [ __PACKAGE__
, 'import', ['volume', 'format', 'filename'], {}, sub {
638 print PVE
::Storage
::volume_imported_message
($volid);
640 apiinfo
=> [ __PACKAGE__
, 'apiinfo', [], {}, sub {
643 print "APIVER $res->{apiver}\n";
644 print "APIAGE $res->{apiage}\n";
646 'prune-backups' => [ __PACKAGE__
, 'prunebackups', ['storage'], { node
=> $nodename }, sub {
649 my ($dryrun, $list) = ($res->{dryrun
}, $res->{list
});
653 if (!scalar(@{$list})) {
654 print "No backups found\n";
658 print "NOTE: this is only a preview and might not be what a subsequent\n" .
659 "prune call does if backups are removed/added in the meantime.\n\n";
662 my $vmcmp = PVE
::Tools
::safe_compare
($a->{vmid
}, $b->{vmid
}, sub { $_[0] <=> $_[1] });
663 return $vmcmp if $vmcmp ne 0;
664 return $a->{ctime
} <=> $b->{ctime
};
668 foreach my $backup (@sorted) {
669 my $volid = $backup->{volid
};
670 $maxlen = length($volid) if length($volid) > $maxlen;
674 printf("%-${maxlen}s %15s %10s\n", 'Backup', 'Backup-ID', 'Prune-Mark');
675 foreach my $backup (@sorted) {
676 my $type = $backup->{type
};
677 my $vmid = $backup->{vmid
};
678 my $backup_id = defined($vmid) ?
"$type/$vmid" : "$type";
679 printf("%-${maxlen}s %15s %10s\n", $backup->{volid
}, $backup_id, $backup->{mark
});