]>
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 'create' => [ $password_map, $enc_key_map ],
57 'update' => [ $password_map, $enc_key_map ],
59 return $mapping->{$name};
62 sub setup_environment
{
63 PVE
::RPCEnvironment-
>setup_default_cli_env();
66 __PACKAGE__-
>register_method ({
70 description
=> "Returns APIVER and APIAGE.",
72 additionalProperties
=> 0,
78 apiver
=> { type
=> 'integer' },
79 apiage
=> { type
=> 'integer' },
84 apiver
=> PVE
::Storage
::APIVER
,
85 apiage
=> PVE
::Storage
::APIAGE
,
90 __PACKAGE__-
>register_method ({
94 description
=> "Get filesystem path for specified volume",
96 additionalProperties
=> 0,
99 description
=> "Volume identifier",
100 type
=> 'string', format
=> 'pve-volume-id',
101 completion
=> \
&PVE
::Storage
::complete_volume
,
105 returns
=> { type
=> 'null' },
110 my $cfg = PVE
::Storage
::config
();
112 my $path = PVE
::Storage
::path
($cfg, $param->{volume
});
120 __PACKAGE__-
>register_method ({
121 name
=> 'extractconfig',
122 path
=> 'extractconfig',
124 description
=> "Extract configuration from vzdump backup archive.",
126 description
=> "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
131 additionalProperties
=> 0,
134 description
=> "Volume identifier",
136 completion
=> \
&PVE
::Storage
::complete_volume
,
140 returns
=> { type
=> 'null' },
143 my $volume = $param->{volume
};
145 my $rpcenv = PVE
::RPCEnvironment
::get
();
146 my $authuser = $rpcenv->get_user();
148 my $storage_cfg = PVE
::Storage
::config
();
149 PVE
::Storage
::check_volume_access
($rpcenv, $authuser, $storage_cfg, undef, $volume);
151 my $config_raw = PVE
::Storage
::extract_vzdump_config
($storage_cfg, $volume);
153 print "$config_raw\n";
157 my $print_content = sub {
160 my ($maxlenname, $maxsize) = (0, 0);
161 foreach my $info (@$list) {
162 my $volid = $info->{volid
};
163 my $sidlen = length ($volid);
164 $maxlenname = $sidlen if $sidlen > $maxlenname;
165 $maxsize = $info->{size
} if ($info->{size
} // 0) > $maxsize;
167 my $sizemaxdigits = length($maxsize);
169 my $basefmt = "%-${maxlenname}s %-7s %-9s %${sizemaxdigits}s";
170 printf "$basefmt %s\n", "Volid", "Format", "Type", "Size", "VMID";
172 foreach my $info (@$list) {
173 next if !$info->{vmid
};
174 my $volid = $info->{volid
};
176 printf "$basefmt %d\n", $volid, $info->{format
}, $info->{content
}, $info->{size
}, $info->{vmid
};
179 foreach my $info (sort { $a->{format
} cmp $b->{format
} } @$list) {
180 next if $info->{vmid
};
181 my $volid = $info->{volid
};
183 printf "$basefmt\n", $volid, $info->{format
}, $info->{content
}, $info->{size
};
187 my $print_status = sub {
191 foreach my $res (@$res) {
192 my $storeid = $res->{storage
};
193 $maxlen = length ($storeid) if length ($storeid) > $maxlen;
197 printf "%-${maxlen}s %10s %10s %15s %15s %15s %8s\n", 'Name', 'Type',
198 'Status', 'Total', 'Used', 'Available', '%';
200 foreach my $res (sort { $a->{storage
} cmp $b->{storage
} } @$res) {
201 my $storeid = $res->{storage
};
203 my $active = $res->{active
} ?
'active' : 'inactive';
204 my ($per, $per_fmt) = (0, '% 7.2f%%');
205 $per = ($res->{used
}*100)/$res->{total
} if $res->{total
} > 0;
207 if (!$res->{enabled
}) {
210 $active = 'disabled';
213 printf "%-${maxlen}s %10s %10s %15d %15d %15d $per_fmt\n", $storeid,
214 $res->{type
}, $active, $res->{total
}/1024, $res->{used}/1024,
215 $res->{avail
}/1024, $per;
219 __PACKAGE__-
>register_method ({
223 description
=> "Used internally to export a volume.",
226 additionalProperties
=> 0,
229 description
=> "Volume identifier",
231 completion
=> \
&PVE
::Storage
::complete_volume
,
234 description
=> "Export stream format",
236 enum
=> $KNOWN_EXPORT_FORMATS,
239 description
=> "Destination file name",
243 description
=> "Snapshot to start an incremental stream from",
245 pattern
=> qr/[a-z0-9_\-]{1,40}/,
250 description
=> "Snapshot to export",
252 pattern
=> qr/[a-z0-9_\-]{1,40}/,
256 'with-snapshots' => {
258 "Whether to include intermediate snapshots in the stream",
265 returns
=> { type
=> 'null' },
269 my $filename = $param->{filename
};
272 if ($filename eq '-') {
275 sysopen($outfh, $filename, O_CREAT
|O_WRONLY
|O_TRUNC
)
276 or die "open($filename): $!\n";
280 my $cfg = PVE
::Storage
::config
();
281 PVE
::Storage
::volume_export
($cfg, $outfh, $param->{volume
}, $param->{format
},
282 $param->{snapshot
}, $param->{base
}, $param->{'with-snapshots'});
285 if ($filename ne '-') {
287 unlink($filename) if $err;
294 __PACKAGE__-
>register_method ({
298 description
=> "Used internally to import a volume.",
301 additionalProperties
=> 0,
304 description
=> "Volume identifier",
306 completion
=> \
&PVE
::Storage
::complete_volume
,
309 description
=> "Import stream format",
311 enum
=> $KNOWN_EXPORT_FORMATS,
314 description
=> "Source file name. For '-' stdin is used, the " .
315 "tcp://<IP-or-CIDR> format allows to use a TCP connection as input. " .
316 "Else, the file is treated as common file.",
320 description
=> "Base snapshot of an incremental stream",
322 pattern
=> qr/[a-z0-9_\-]{1,40}/,
326 'with-snapshots' => {
328 "Whether the stream includes intermediate snapshots",
333 'delete-snapshot' => {
334 description
=> "A snapshot to delete on success",
336 pattern
=> qr/[a-z0-9_\-]{1,80}/,
341 description
=> "Choose a new volume ID if the requested " .
342 "volume ID already exists, instead of throwing an error.",
349 returns
=> { type
=> 'string' },
353 my $filename = $param->{filename
};
356 if ($filename eq '-') {
358 } elsif ($filename =~ m!^tcp://(([^/]+)(/\d+)?)$!) {
359 my ($cidr, $ip, $subnet) = ($1, $2, $3);
360 if ($subnet) { # got real CIDR notation, not just IP
361 my $ips = PVE
::Network
::get_local_ip_from_cidr
($cidr);
362 die "Unable to get any local IP address in network '$cidr'\n"
363 if scalar(@$ips) < 1;
364 die "Got multiple local IP address in network '$cidr'\n"
365 if scalar(@$ips) > 1;
369 my $family = PVE
::Tools
::get_host_address_family
($ip);
370 my $port = PVE
::Tools
::next_migrate_port
($family, $ip);
375 Proto
=> &Socket
::IPPROTO_TCP
,
376 GetAddrInfoFlags
=> 0,
380 my $socket = IO
::Socket
::IP-
>new(%$sock_params)
381 or die "failed to open socket: $!\n";
383 print "$ip\n$port\n"; # tell remote where to connect
386 my $prev_alarm = alarm 0;
387 local $SIG{ALRM
} = sub { die "timed out waiting for client\n" };
389 my $client = $socket->accept; # Wait for a client
395 sysopen($infh, $filename, O_RDONLY
)
396 or die "open($filename): $!\n";
399 my $cfg = PVE
::Storage
::config
();
400 my $volume = $param->{volume
};
401 my $delete = $param->{'delete-snapshot'};
402 my $imported_volid = PVE
::Storage
::volume_import
($cfg, $infh, $volume, $param->{format
},
403 $param->{base
}, $param->{'with-snapshots'}, $param->{'allow-rename'});
404 PVE
::Storage
::volume_snapshot_delete
($cfg, $imported_volid, $delete)
406 return $imported_volid;
410 __PACKAGE__-
>register_method ({
411 name
=> 'prunebackups',
412 path
=> 'prunebackups',
414 description
=> "Prune backups. Only those using the standard naming scheme are considered. " .
415 "If no keep options are specified, those from the storage configuration are used.",
419 additionalProperties
=> 0,
422 description
=> "Only show what would be pruned, don't delete anything.",
426 node
=> get_standard_option
('pve-node'),
427 storage
=> get_standard_option
('pve-storage-id', {
428 completion
=> \
&PVE
::Storage
::complete_storage_enabled
,
430 %{$PVE::Storage
::Plugin
::prune_backups_format
},
432 description
=> "Either 'qemu' or 'lxc'. Only consider backups for guests of this type.",
435 enum
=> ['qemu', 'lxc'],
437 vmid
=> get_standard_option
('pve-vmid', {
438 description
=> "Only consider backups for this guest.",
440 completion
=> \
&PVE
::Cluster
::complete_vmid
,
448 description
=> 'If it was a dry run or not. The list will only be defined in that case.',
457 description
=> "Backup volume ID.",
461 description
=> "Creation time of the backup (seconds since the UNIX epoch).",
465 description
=> "Whether the backup would be kept or removed. For backups that don't " .
466 "use the standard naming scheme, it's 'protected'.",
470 description
=> "One of 'qemu', 'lxc', 'openvz' or 'unknown'.",
474 description
=> "The VM the backup belongs to.",
486 my $dryrun = extract_param
($param, 'dry-run') ?
1 : 0;
489 foreach my $keep (keys %{$PVE::Storage
::Plugin
::prune_backups_format
}) {
490 $keep_opts->{$keep} = extract_param
($param, $keep) if defined($param->{$keep});
492 $param->{'prune-backups'} = PVE
::JSONSchema
::print_property_string
(
493 $keep_opts, $PVE::Storage
::Plugin
::prune_backups_format
) if $keep_opts;
497 $list = PVE
::API2
::Storage
::PruneBackups-
>dryrun($param);
499 PVE
::API2
::Storage
::PruneBackups-
>delete($param);
509 add
=> [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ],
510 set
=> [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
511 remove
=> [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
512 status
=> [ "PVE::API2::Storage::Status", 'index', [],
513 { node
=> $nodename }, $print_status ],
514 list
=> [ "PVE::API2::Storage::Content", 'index', ['storage'],
515 { node
=> $nodename }, $print_content ],
516 alloc
=> [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'],
517 { node
=> $nodename }, sub {
519 print "successfully created '$volid'\n";
521 free
=> [ "PVE::API2::Storage::Content", 'delete', ['volume'],
522 { node
=> $nodename } ],
524 nfs
=> [ "PVE::API2::Storage::Scan", 'nfsscan', ['server'], { node
=> $nodename }, sub {
528 foreach my $rec (@$res) {
529 my $len = length ($rec->{path
});
530 $maxlen = $len if $len > $maxlen;
532 foreach my $rec (@$res) {
533 printf "%-${maxlen}s %s\n", $rec->{path
}, $rec->{options
};
536 cifs
=> [ "PVE::API2::Storage::Scan", 'cifsscan', ['server'], { node
=> $nodename }, sub {
540 foreach my $rec (@$res) {
541 my $len = length ($rec->{share
});
542 $maxlen = $len if $len > $maxlen;
544 foreach my $rec (@$res) {
545 printf "%-${maxlen}s %s\n", $rec->{share
}, $rec->{description
};
548 glusterfs
=> [ "PVE::API2::Storage::Scan", 'glusterfsscan', ['server'], { node
=> $nodename }, sub {
551 foreach my $rec (@$res) {
552 printf "%s\n", $rec->{volname
};
555 iscsi
=> [ "PVE::API2::Storage::Scan", 'iscsiscan', ['portal'], { node
=> $nodename }, sub {
559 foreach my $rec (@$res) {
560 my $len = length ($rec->{target
});
561 $maxlen = $len if $len > $maxlen;
563 foreach my $rec (@$res) {
564 printf "%-${maxlen}s %s\n", $rec->{target
}, $rec->{portal
};
567 lvm
=> [ "PVE::API2::Storage::Scan", 'lvmscan', [], { node
=> $nodename }, sub {
569 foreach my $rec (@$res) {
570 printf "$rec->{vg}\n";
573 lvmthin
=> [ "PVE::API2::Storage::Scan", 'lvmthinscan', ['vg'], { node
=> $nodename }, sub {
575 foreach my $rec (@$res) {
576 printf "$rec->{lv}\n";
579 zfs
=> [ "PVE::API2::Storage::Scan", 'zfsscan', [], { node
=> $nodename }, sub {
582 foreach my $rec (@$res) {
583 printf "$rec->{pool}\n";
587 nfsscan
=> { alias
=> 'scan nfs' },
588 cifsscan
=> { alias
=> 'scan cifs' },
589 glusterfsscan
=> { alias
=> 'scan glusterfs' },
590 iscsiscan
=> { alias
=> 'scan iscsi' },
591 lvmscan
=> { alias
=> 'scan lvm' },
592 lvmthinscan
=> { alias
=> 'scan lvmthin' },
593 zfsscan
=> { alias
=> 'scan zfs' },
594 path
=> [ __PACKAGE__
, 'path', ['volume']],
595 extractconfig
=> [__PACKAGE__
, 'extractconfig', ['volume']],
596 export
=> [ __PACKAGE__
, 'export', ['volume', 'format', 'filename']],
597 import
=> [ __PACKAGE__
, 'import', ['volume', 'format', 'filename'], {}, sub {
599 print PVE
::Storage
::volume_imported_message
($volid);
601 apiinfo
=> [ __PACKAGE__
, 'apiinfo', [], {}, sub {
604 print "APIVER $res->{apiver}\n";
605 print "APIAGE $res->{apiage}\n";
607 'prune-backups' => [ __PACKAGE__
, 'prunebackups', ['storage'], { node
=> $nodename }, sub {
610 my ($dryrun, $list) = ($res->{dryrun
}, $res->{list
});
614 if (!scalar(@{$list})) {
615 print "No backups found\n";
619 print "NOTE: this is only a preview and might not be what a subsequent\n" .
620 "prune call does if backups are removed/added in the meantime.\n\n";
623 my $vmcmp = PVE
::Tools
::safe_compare
($a->{vmid
}, $b->{vmid
}, sub { $_[0] <=> $_[1] });
624 return $vmcmp if $vmcmp ne 0;
625 return $a->{ctime
} <=> $b->{ctime
};
629 foreach my $backup (@sorted) {
630 my $volid = $backup->{volid
};
631 $maxlen = length($volid) if length($volid) > $maxlen;
635 printf("%-${maxlen}s %15s %10s\n", 'Backup', 'Backup-ID', 'Prune-Mark');
636 foreach my $backup (@sorted) {
637 my $type = $backup->{type
};
638 my $vmid = $backup->{vmid
};
639 my $backup_id = defined($vmid) ?
"$type/$vmid" : "$type";
640 printf("%-${maxlen}s %15s %10s\n", $backup->{volid
}, $backup_id, $backup->{mark
});