]>
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 $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
($rpcenv, $authuser, $storage_cfg, undef, $volume);
175 my $config_raw = PVE
::Storage
::extract_vzdump_config
($storage_cfg, $volume);
177 print "$config_raw\n";
181 my $print_content = sub {
184 my ($maxlenname, $maxsize) = (0, 0);
185 foreach my $info (@$list) {
186 my $volid = $info->{volid
};
187 my $sidlen = length ($volid);
188 $maxlenname = $sidlen if $sidlen > $maxlenname;
189 $maxsize = $info->{size
} if ($info->{size
} // 0) > $maxsize;
191 my $sizemaxdigits = length($maxsize);
193 my $basefmt = "%-${maxlenname}s %-7s %-9s %${sizemaxdigits}s";
194 printf "$basefmt %s\n", "Volid", "Format", "Type", "Size", "VMID";
196 foreach my $info (@$list) {
197 next if !$info->{vmid
};
198 my $volid = $info->{volid
};
200 printf "$basefmt %d\n", $volid, $info->{format
}, $info->{content
}, $info->{size
}, $info->{vmid
};
203 foreach my $info (sort { $a->{format
} cmp $b->{format
} } @$list) {
204 next if $info->{vmid
};
205 my $volid = $info->{volid
};
207 printf "$basefmt\n", $volid, $info->{format
}, $info->{content
}, $info->{size
};
211 my $print_status = sub {
215 foreach my $res (@$res) {
216 my $storeid = $res->{storage
};
217 $maxlen = length ($storeid) if length ($storeid) > $maxlen;
221 printf "%-${maxlen}s %10s %10s %15s %15s %15s %8s\n", 'Name', 'Type',
222 'Status', 'Total', 'Used', 'Available', '%';
224 foreach my $res (sort { $a->{storage
} cmp $b->{storage
} } @$res) {
225 my $storeid = $res->{storage
};
227 my $active = $res->{active
} ?
'active' : 'inactive';
228 my ($per, $per_fmt) = (0, '% 7.2f%%');
229 $per = ($res->{used
}*100)/$res->{total
} if $res->{total
} > 0;
231 if (!$res->{enabled
}) {
234 $active = 'disabled';
237 printf "%-${maxlen}s %10s %10s %15d %15d %15d $per_fmt\n", $storeid,
238 $res->{type
}, $active, $res->{total
}/1024, $res->{used}/1024,
239 $res->{avail
}/1024, $per;
243 __PACKAGE__-
>register_method ({
247 description
=> "Used internally to export a volume.",
250 additionalProperties
=> 0,
253 description
=> "Volume identifier",
255 completion
=> \
&PVE
::Storage
::complete_volume
,
258 description
=> "Export stream format",
260 enum
=> $KNOWN_EXPORT_FORMATS,
263 description
=> "Destination file name",
267 description
=> "Snapshot to start an incremental stream from",
269 pattern
=> qr/[a-z0-9_\-]{1,40}/i,
274 description
=> "Snapshot to export",
276 pattern
=> qr/[a-z0-9_\-]{1,40}/i,
280 'with-snapshots' => {
282 "Whether to include intermediate snapshots in the stream",
288 description
=> "Ordered list of snapshots to transfer",
290 format
=> 'string-list',
295 returns
=> { type
=> 'null' },
299 my $with_snapshots = $param->{'with-snapshots'};
300 if (defined(my $list = $param->{'snapshot-list'})) {
301 $with_snapshots = PVE
::Tools
::split_list
($list);
304 my $filename = $param->{filename
};
307 if ($filename eq '-') {
310 sysopen($outfh, $filename, O_CREAT
|O_WRONLY
|O_TRUNC
)
311 or die "open($filename): $!\n";
315 my $cfg = PVE
::Storage
::config
();
316 PVE
::Storage
::volume_export
($cfg, $outfh, $param->{volume
}, $param->{format
},
317 $param->{snapshot
}, $param->{base
}, $with_snapshots);
320 if ($filename ne '-') {
322 unlink($filename) if $err;
329 __PACKAGE__-
>register_method ({
333 description
=> "Used internally to import a volume.",
336 additionalProperties
=> 0,
339 description
=> "Volume identifier",
341 completion
=> \
&PVE
::Storage
::complete_volume
,
344 description
=> "Import stream format",
346 enum
=> $KNOWN_EXPORT_FORMATS,
349 description
=> "Source file name. For '-' stdin is used, the " .
350 "tcp://<IP-or-CIDR> format allows to use a TCP connection, " .
351 "the unix://PATH-TO-SOCKET format a UNIX socket as input." .
352 "Else, the file is treated as common file.",
356 description
=> "Base snapshot of an incremental stream",
358 pattern
=> qr/[a-z0-9_\-]{1,40}/i,
362 'with-snapshots' => {
364 "Whether the stream includes intermediate snapshots",
369 'delete-snapshot' => {
370 description
=> "A snapshot to delete on success",
372 pattern
=> qr/[a-z0-9_\-]{1,80}/i,
377 description
=> "Choose a new volume ID if the requested " .
378 "volume ID already exists, instead of throwing an error.",
384 description
=> "The current-state snapshot if the stream contains snapshots",
386 pattern
=> qr/[a-z0-9_\-]{1,40}/i,
392 returns
=> { type
=> 'string' },
396 my $filename = $param->{filename
};
399 if ($filename eq '-') {
401 } elsif ($filename =~ m!^tcp://(([^/]+)(/\d+)?)$!) {
402 my ($cidr, $ip, $subnet) = ($1, $2, $3);
403 if ($subnet) { # got real CIDR notation, not just IP
404 my $ips = PVE
::Network
::get_local_ip_from_cidr
($cidr);
405 die "Unable to get any local IP address in network '$cidr'\n"
406 if scalar(@$ips) < 1;
407 die "Got multiple local IP address in network '$cidr'\n"
408 if scalar(@$ips) > 1;
412 my $family = PVE
::Tools
::get_host_address_family
($ip);
413 my $port = PVE
::Tools
::next_migrate_port
($family, $ip);
418 Proto
=> &Socket
::IPPROTO_TCP
,
419 GetAddrInfoFlags
=> 0,
423 my $socket = IO
::Socket
::IP-
>new(%$sock_params)
424 or die "failed to open socket: $!\n";
426 print "$ip\n$port\n"; # tell remote where to connect
429 my $prev_alarm = alarm 0;
430 local $SIG{ALRM
} = sub { die "timed out waiting for client\n" };
432 my $client = $socket->accept; # Wait for a client
437 } elsif ($filename =~ m!^unix://(.*)$!) {
438 my $socket_path = $1;
439 my $socket = IO
::Socket
::UNIX-
>new(
440 Type
=> SOCK_STREAM
(),
441 Local
=> $socket_path,
443 ) or die "failed to open socket: $!\n";
448 my $prev_alarm = alarm 0;
449 local $SIG{ALRM
} = sub { die "timed out waiting for client\n" };
451 my $client = $socket->accept; # Wait for a client
457 sysopen($infh, $filename, O_RDONLY
)
458 or die "open($filename): $!\n";
461 my $cfg = PVE
::Storage
::config
();
462 my $volume = $param->{volume
};
463 my $delete = $param->{'delete-snapshot'};
464 my $imported_volid = PVE
::Storage
::volume_import
($cfg, $infh, $volume, $param->{format
},
465 $param->{snapshot
}, $param->{base
}, $param->{'with-snapshots'},
466 $param->{'allow-rename'});
467 PVE
::Storage
::volume_snapshot_delete
($cfg, $imported_volid, $delete)
469 return $imported_volid;
473 __PACKAGE__-
>register_method ({
474 name
=> 'prunebackups',
475 path
=> 'prunebackups',
477 description
=> "Prune backups. Only those using the standard naming scheme are considered. " .
478 "If no keep options are specified, those from the storage configuration are used.",
482 additionalProperties
=> 0,
485 description
=> "Only show what would be pruned, don't delete anything.",
489 node
=> get_standard_option
('pve-node'),
490 storage
=> get_standard_option
('pve-storage-id', {
491 completion
=> \
&PVE
::Storage
::complete_storage_enabled
,
493 %{$PVE::Storage
::Plugin
::prune_backups_format
},
495 description
=> "Either 'qemu' or 'lxc'. Only consider backups for guests of this type.",
498 enum
=> ['qemu', 'lxc'],
500 vmid
=> get_standard_option
('pve-vmid', {
501 description
=> "Only consider backups for this guest.",
503 completion
=> \
&PVE
::Cluster
::complete_vmid
,
511 description
=> 'If it was a dry run or not. The list will only be defined in that case.',
520 description
=> "Backup volume ID.",
524 description
=> "Creation time of the backup (seconds since the UNIX epoch).",
528 description
=> "Whether the backup would be kept or removed. For backups that don't " .
529 "use the standard naming scheme, it's 'protected'.",
533 description
=> "One of 'qemu', 'lxc', 'openvz' or 'unknown'.",
537 description
=> "The VM the backup belongs to.",
549 my $dryrun = extract_param
($param, 'dry-run') ?
1 : 0;
552 foreach my $keep (keys %{$PVE::Storage
::Plugin
::prune_backups_format
}) {
553 $keep_opts->{$keep} = extract_param
($param, $keep) if defined($param->{$keep});
555 $param->{'prune-backups'} = PVE
::JSONSchema
::print_property_string
(
556 $keep_opts, $PVE::Storage
::Plugin
::prune_backups_format
) if $keep_opts;
560 $list = PVE
::API2
::Storage
::PruneBackups-
>dryrun($param);
562 PVE
::API2
::Storage
::PruneBackups-
>delete($param);
571 my $print_api_result = sub {
572 my ($data, $schema, $options) = @_;
573 PVE
::CLIFormatter
::print_api_result
($data, $schema, undef, $options);
577 add
=> [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ],
578 set
=> [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
579 remove
=> [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
580 status
=> [ "PVE::API2::Storage::Status", 'index', [],
581 { node
=> $nodename }, $print_status ],
582 list
=> [ "PVE::API2::Storage::Content", 'index', ['storage'],
583 { node
=> $nodename }, $print_content ],
584 alloc
=> [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'],
585 { node
=> $nodename }, sub {
587 print "successfully created '$volid'\n";
589 free
=> [ "PVE::API2::Storage::Content", 'delete', ['volume'],
590 { node
=> $nodename } ],
592 nfs
=> [ "PVE::API2::Storage::Scan", 'nfsscan', ['server'], { node
=> $nodename }, sub {
596 foreach my $rec (@$res) {
597 my $len = length ($rec->{path
});
598 $maxlen = $len if $len > $maxlen;
600 foreach my $rec (@$res) {
601 printf "%-${maxlen}s %s\n", $rec->{path
}, $rec->{options
};
604 cifs
=> [ "PVE::API2::Storage::Scan", 'cifsscan', ['server'], { node
=> $nodename }, sub {
608 foreach my $rec (@$res) {
609 my $len = length ($rec->{share
});
610 $maxlen = $len if $len > $maxlen;
612 foreach my $rec (@$res) {
613 printf "%-${maxlen}s %s\n", $rec->{share
}, $rec->{description
};
616 glusterfs
=> [ "PVE::API2::Storage::Scan", 'glusterfsscan', ['server'], { node
=> $nodename }, sub {
619 foreach my $rec (@$res) {
620 printf "%s\n", $rec->{volname
};
623 iscsi
=> [ "PVE::API2::Storage::Scan", 'iscsiscan', ['portal'], { node
=> $nodename }, sub {
627 foreach my $rec (@$res) {
628 my $len = length ($rec->{target
});
629 $maxlen = $len if $len > $maxlen;
631 foreach my $rec (@$res) {
632 printf "%-${maxlen}s %s\n", $rec->{target
}, $rec->{portal
};
635 lvm
=> [ "PVE::API2::Storage::Scan", 'lvmscan', [], { node
=> $nodename }, sub {
637 foreach my $rec (@$res) {
638 printf "$rec->{vg}\n";
641 lvmthin
=> [ "PVE::API2::Storage::Scan", 'lvmthinscan', ['vg'], { node
=> $nodename }, sub {
643 foreach my $rec (@$res) {
644 printf "$rec->{lv}\n";
648 "PVE::API2::Storage::Scan",
650 ['server', 'username'],
651 { node
=> $nodename },
653 $PVE::RESTHandler
::standard_output_options
,
655 zfs
=> [ "PVE::API2::Storage::Scan", 'zfsscan', [], { node
=> $nodename }, sub {
658 foreach my $rec (@$res) {
659 printf "$rec->{pool}\n";
663 nfsscan
=> { alias
=> 'scan nfs' },
664 cifsscan
=> { alias
=> 'scan cifs' },
665 glusterfsscan
=> { alias
=> 'scan glusterfs' },
666 iscsiscan
=> { alias
=> 'scan iscsi' },
667 lvmscan
=> { alias
=> 'scan lvm' },
668 lvmthinscan
=> { alias
=> 'scan lvmthin' },
669 zfsscan
=> { alias
=> 'scan zfs' },
670 path
=> [ __PACKAGE__
, 'path', ['volume']],
671 extractconfig
=> [__PACKAGE__
, 'extractconfig', ['volume']],
672 export
=> [ __PACKAGE__
, 'export', ['volume', 'format', 'filename']],
673 import
=> [ __PACKAGE__
, 'import', ['volume', 'format', 'filename'], {}, sub {
675 print PVE
::Storage
::volume_imported_message
($volid);
677 apiinfo
=> [ __PACKAGE__
, 'apiinfo', [], {}, sub {
680 print "APIVER $res->{apiver}\n";
681 print "APIAGE $res->{apiage}\n";
683 'prune-backups' => [ __PACKAGE__
, 'prunebackups', ['storage'], { node
=> $nodename }, sub {
686 my ($dryrun, $list) = ($res->{dryrun
}, $res->{list
});
690 if (!scalar(@{$list})) {
691 print "No backups found\n";
695 print "NOTE: this is only a preview and might not be what a subsequent\n" .
696 "prune call does if backups are removed/added in the meantime.\n\n";
699 my $vmcmp = PVE
::Tools
::safe_compare
($a->{vmid
}, $b->{vmid
}, sub { $_[0] <=> $_[1] });
700 return $vmcmp if $vmcmp ne 0;
701 return $a->{ctime
} <=> $b->{ctime
};
705 foreach my $backup (@sorted) {
706 my $volid = $backup->{volid
};
707 $maxlen = length($volid) if length($volid) > $maxlen;
711 printf("%-${maxlen}s %15s %10s\n", 'Backup', 'Backup-ID', 'Prune-Mark');
712 foreach my $backup (@sorted) {
713 my $type = $backup->{type
};
714 my $vmid = $backup->{vmid
};
715 my $backup_id = defined($vmid) ?
"$type/$vmid" : "$type";
716 printf("%-${maxlen}s %15s %10s\n", $backup->{volid
}, $backup_id, $backup->{mark
});