]>
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 'cifs' => [ $password_map ],
57 'create' => [ $password_map, $enc_key_map ],
58 'update' => [ $password_map, $enc_key_map ],
60 return $mapping->{$name};
63 sub setup_environment
{
64 PVE
::RPCEnvironment-
>setup_default_cli_env();
67 __PACKAGE__-
>register_method ({
71 description
=> "Returns APIVER and APIAGE.",
73 additionalProperties
=> 0,
79 apiver
=> { type
=> 'integer' },
80 apiage
=> { type
=> 'integer' },
85 apiver
=> PVE
::Storage
::APIVER
,
86 apiage
=> PVE
::Storage
::APIAGE
,
91 __PACKAGE__-
>register_method ({
95 description
=> "Get filesystem path for specified volume",
97 additionalProperties
=> 0,
100 description
=> "Volume identifier",
101 type
=> 'string', format
=> 'pve-volume-id',
102 completion
=> \
&PVE
::Storage
::complete_volume
,
106 returns
=> { type
=> 'null' },
111 my $cfg = PVE
::Storage
::config
();
113 my $path = PVE
::Storage
::path
($cfg, $param->{volume
});
121 __PACKAGE__-
>register_method ({
122 name
=> 'extractconfig',
123 path
=> 'extractconfig',
125 description
=> "Extract configuration from vzdump backup archive.",
127 description
=> "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
132 additionalProperties
=> 0,
135 description
=> "Volume identifier",
137 completion
=> \
&PVE
::Storage
::complete_volume
,
141 returns
=> { type
=> 'null' },
144 my $volume = $param->{volume
};
146 my $rpcenv = PVE
::RPCEnvironment
::get
();
147 my $authuser = $rpcenv->get_user();
149 my $storage_cfg = PVE
::Storage
::config
();
150 PVE
::Storage
::check_volume_access
($rpcenv, $authuser, $storage_cfg, undef, $volume);
152 my $config_raw = PVE
::Storage
::extract_vzdump_config
($storage_cfg, $volume);
154 print "$config_raw\n";
158 my $print_content = sub {
161 my ($maxlenname, $maxsize) = (0, 0);
162 foreach my $info (@$list) {
163 my $volid = $info->{volid
};
164 my $sidlen = length ($volid);
165 $maxlenname = $sidlen if $sidlen > $maxlenname;
166 $maxsize = $info->{size
} if ($info->{size
} // 0) > $maxsize;
168 my $sizemaxdigits = length($maxsize);
170 my $basefmt = "%-${maxlenname}s %-7s %-9s %${sizemaxdigits}s";
171 printf "$basefmt %s\n", "Volid", "Format", "Type", "Size", "VMID";
173 foreach my $info (@$list) {
174 next if !$info->{vmid
};
175 my $volid = $info->{volid
};
177 printf "$basefmt %d\n", $volid, $info->{format
}, $info->{content
}, $info->{size
}, $info->{vmid
};
180 foreach my $info (sort { $a->{format
} cmp $b->{format
} } @$list) {
181 next if $info->{vmid
};
182 my $volid = $info->{volid
};
184 printf "$basefmt\n", $volid, $info->{format
}, $info->{content
}, $info->{size
};
188 my $print_status = sub {
192 foreach my $res (@$res) {
193 my $storeid = $res->{storage
};
194 $maxlen = length ($storeid) if length ($storeid) > $maxlen;
198 printf "%-${maxlen}s %10s %10s %15s %15s %15s %8s\n", 'Name', 'Type',
199 'Status', 'Total', 'Used', 'Available', '%';
201 foreach my $res (sort { $a->{storage
} cmp $b->{storage
} } @$res) {
202 my $storeid = $res->{storage
};
204 my $active = $res->{active
} ?
'active' : 'inactive';
205 my ($per, $per_fmt) = (0, '% 7.2f%%');
206 $per = ($res->{used
}*100)/$res->{total
} if $res->{total
} > 0;
208 if (!$res->{enabled
}) {
211 $active = 'disabled';
214 printf "%-${maxlen}s %10s %10s %15d %15d %15d $per_fmt\n", $storeid,
215 $res->{type
}, $active, $res->{total
}/1024, $res->{used}/1024,
216 $res->{avail
}/1024, $per;
220 __PACKAGE__-
>register_method ({
224 description
=> "Used internally to export a volume.",
227 additionalProperties
=> 0,
230 description
=> "Volume identifier",
232 completion
=> \
&PVE
::Storage
::complete_volume
,
235 description
=> "Export stream format",
237 enum
=> $KNOWN_EXPORT_FORMATS,
240 description
=> "Destination file name",
244 description
=> "Snapshot to start an incremental stream from",
246 pattern
=> qr/[a-z0-9_\-]{1,40}/,
251 description
=> "Snapshot to export",
253 pattern
=> qr/[a-z0-9_\-]{1,40}/,
257 'with-snapshots' => {
259 "Whether to include intermediate snapshots in the stream",
266 returns
=> { type
=> 'null' },
270 my $filename = $param->{filename
};
273 if ($filename eq '-') {
276 sysopen($outfh, $filename, O_CREAT
|O_WRONLY
|O_TRUNC
)
277 or die "open($filename): $!\n";
281 my $cfg = PVE
::Storage
::config
();
282 PVE
::Storage
::volume_export
($cfg, $outfh, $param->{volume
}, $param->{format
},
283 $param->{snapshot
}, $param->{base
}, $param->{'with-snapshots'});
286 if ($filename ne '-') {
288 unlink($filename) if $err;
295 __PACKAGE__-
>register_method ({
299 description
=> "Used internally to import a volume.",
302 additionalProperties
=> 0,
305 description
=> "Volume identifier",
307 completion
=> \
&PVE
::Storage
::complete_volume
,
310 description
=> "Import stream format",
312 enum
=> $KNOWN_EXPORT_FORMATS,
315 description
=> "Source file name. For '-' stdin is used, the " .
316 "tcp://<IP-or-CIDR> format allows to use a TCP connection as input. " .
317 "Else, the file is treated as common file.",
321 description
=> "Base snapshot of an incremental stream",
323 pattern
=> qr/[a-z0-9_\-]{1,40}/,
327 'with-snapshots' => {
329 "Whether the stream includes intermediate snapshots",
334 'delete-snapshot' => {
335 description
=> "A snapshot to delete on success",
337 pattern
=> qr/[a-z0-9_\-]{1,80}/,
342 description
=> "Choose a new volume ID if the requested " .
343 "volume ID already exists, instead of throwing an error.",
350 returns
=> { type
=> 'string' },
354 my $filename = $param->{filename
};
357 if ($filename eq '-') {
359 } elsif ($filename =~ m!^tcp://(([^/]+)(/\d+)?)$!) {
360 my ($cidr, $ip, $subnet) = ($1, $2, $3);
361 if ($subnet) { # got real CIDR notation, not just IP
362 my $ips = PVE
::Network
::get_local_ip_from_cidr
($cidr);
363 die "Unable to get any local IP address in network '$cidr'\n"
364 if scalar(@$ips) < 1;
365 die "Got multiple local IP address in network '$cidr'\n"
366 if scalar(@$ips) > 1;
370 my $family = PVE
::Tools
::get_host_address_family
($ip);
371 my $port = PVE
::Tools
::next_migrate_port
($family, $ip);
376 Proto
=> &Socket
::IPPROTO_TCP
,
377 GetAddrInfoFlags
=> 0,
381 my $socket = IO
::Socket
::IP-
>new(%$sock_params)
382 or die "failed to open socket: $!\n";
384 print "$ip\n$port\n"; # tell remote where to connect
387 my $prev_alarm = alarm 0;
388 local $SIG{ALRM
} = sub { die "timed out waiting for client\n" };
390 my $client = $socket->accept; # Wait for a client
396 sysopen($infh, $filename, O_RDONLY
)
397 or die "open($filename): $!\n";
400 my $cfg = PVE
::Storage
::config
();
401 my $volume = $param->{volume
};
402 my $delete = $param->{'delete-snapshot'};
403 my $imported_volid = PVE
::Storage
::volume_import
($cfg, $infh, $volume, $param->{format
},
404 $param->{base
}, $param->{'with-snapshots'}, $param->{'allow-rename'});
405 PVE
::Storage
::volume_snapshot_delete
($cfg, $imported_volid, $delete)
407 return $imported_volid;
411 __PACKAGE__-
>register_method ({
412 name
=> 'prunebackups',
413 path
=> 'prunebackups',
415 description
=> "Prune backups. Only those using the standard naming scheme are considered. " .
416 "If no keep options are specified, those from the storage configuration are used.",
420 additionalProperties
=> 0,
423 description
=> "Only show what would be pruned, don't delete anything.",
427 node
=> get_standard_option
('pve-node'),
428 storage
=> get_standard_option
('pve-storage-id', {
429 completion
=> \
&PVE
::Storage
::complete_storage_enabled
,
431 %{$PVE::Storage
::Plugin
::prune_backups_format
},
433 description
=> "Either 'qemu' or 'lxc'. Only consider backups for guests of this type.",
436 enum
=> ['qemu', 'lxc'],
438 vmid
=> get_standard_option
('pve-vmid', {
439 description
=> "Only consider backups for this guest.",
441 completion
=> \
&PVE
::Cluster
::complete_vmid
,
449 description
=> 'If it was a dry run or not. The list will only be defined in that case.',
458 description
=> "Backup volume ID.",
462 description
=> "Creation time of the backup (seconds since the UNIX epoch).",
466 description
=> "Whether the backup would be kept or removed. For backups that don't " .
467 "use the standard naming scheme, it's 'protected'.",
471 description
=> "One of 'qemu', 'lxc', 'openvz' or 'unknown'.",
475 description
=> "The VM the backup belongs to.",
487 my $dryrun = extract_param
($param, 'dry-run') ?
1 : 0;
490 foreach my $keep (keys %{$PVE::Storage
::Plugin
::prune_backups_format
}) {
491 $keep_opts->{$keep} = extract_param
($param, $keep) if defined($param->{$keep});
493 $param->{'prune-backups'} = PVE
::JSONSchema
::print_property_string
(
494 $keep_opts, $PVE::Storage
::Plugin
::prune_backups_format
) if $keep_opts;
498 $list = PVE
::API2
::Storage
::PruneBackups-
>dryrun($param);
500 PVE
::API2
::Storage
::PruneBackups-
>delete($param);
510 add
=> [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ],
511 set
=> [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
512 remove
=> [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
513 status
=> [ "PVE::API2::Storage::Status", 'index', [],
514 { node
=> $nodename }, $print_status ],
515 list
=> [ "PVE::API2::Storage::Content", 'index', ['storage'],
516 { node
=> $nodename }, $print_content ],
517 alloc
=> [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'],
518 { node
=> $nodename }, sub {
520 print "successfully created '$volid'\n";
522 free
=> [ "PVE::API2::Storage::Content", 'delete', ['volume'],
523 { node
=> $nodename } ],
525 nfs
=> [ "PVE::API2::Storage::Scan", 'nfsscan', ['server'], { node
=> $nodename }, sub {
529 foreach my $rec (@$res) {
530 my $len = length ($rec->{path
});
531 $maxlen = $len if $len > $maxlen;
533 foreach my $rec (@$res) {
534 printf "%-${maxlen}s %s\n", $rec->{path
}, $rec->{options
};
537 cifs
=> [ "PVE::API2::Storage::Scan", 'cifsscan', ['server'], { node
=> $nodename }, sub {
541 foreach my $rec (@$res) {
542 my $len = length ($rec->{share
});
543 $maxlen = $len if $len > $maxlen;
545 foreach my $rec (@$res) {
546 printf "%-${maxlen}s %s\n", $rec->{share
}, $rec->{description
};
549 glusterfs
=> [ "PVE::API2::Storage::Scan", 'glusterfsscan', ['server'], { node
=> $nodename }, sub {
552 foreach my $rec (@$res) {
553 printf "%s\n", $rec->{volname
};
556 iscsi
=> [ "PVE::API2::Storage::Scan", 'iscsiscan', ['portal'], { node
=> $nodename }, sub {
560 foreach my $rec (@$res) {
561 my $len = length ($rec->{target
});
562 $maxlen = $len if $len > $maxlen;
564 foreach my $rec (@$res) {
565 printf "%-${maxlen}s %s\n", $rec->{target
}, $rec->{portal
};
568 lvm
=> [ "PVE::API2::Storage::Scan", 'lvmscan', [], { node
=> $nodename }, sub {
570 foreach my $rec (@$res) {
571 printf "$rec->{vg}\n";
574 lvmthin
=> [ "PVE::API2::Storage::Scan", 'lvmthinscan', ['vg'], { node
=> $nodename }, sub {
576 foreach my $rec (@$res) {
577 printf "$rec->{lv}\n";
580 zfs
=> [ "PVE::API2::Storage::Scan", 'zfsscan', [], { node
=> $nodename }, sub {
583 foreach my $rec (@$res) {
584 printf "$rec->{pool}\n";
588 nfsscan
=> { alias
=> 'scan nfs' },
589 cifsscan
=> { alias
=> 'scan cifs' },
590 glusterfsscan
=> { alias
=> 'scan glusterfs' },
591 iscsiscan
=> { alias
=> 'scan iscsi' },
592 lvmscan
=> { alias
=> 'scan lvm' },
593 lvmthinscan
=> { alias
=> 'scan lvmthin' },
594 zfsscan
=> { alias
=> 'scan zfs' },
595 path
=> [ __PACKAGE__
, 'path', ['volume']],
596 extractconfig
=> [__PACKAGE__
, 'extractconfig', ['volume']],
597 export
=> [ __PACKAGE__
, 'export', ['volume', 'format', 'filename']],
598 import
=> [ __PACKAGE__
, 'import', ['volume', 'format', 'filename'], {}, sub {
600 print PVE
::Storage
::volume_imported_message
($volid);
602 apiinfo
=> [ __PACKAGE__
, 'apiinfo', [], {}, sub {
605 print "APIVER $res->{apiver}\n";
606 print "APIAGE $res->{apiage}\n";
608 'prune-backups' => [ __PACKAGE__
, 'prunebackups', ['storage'], { node
=> $nodename }, sub {
611 my ($dryrun, $list) = ($res->{dryrun
}, $res->{list
});
615 if (!scalar(@{$list})) {
616 print "No backups found\n";
620 print "NOTE: this is only a preview and might not be what a subsequent\n" .
621 "prune call does if backups are removed/added in the meantime.\n\n";
624 my $vmcmp = PVE
::Tools
::safe_compare
($a->{vmid
}, $b->{vmid
}, sub { $_[0] <=> $_[1] });
625 return $vmcmp if $vmcmp ne 0;
626 return $a->{ctime
} <=> $b->{ctime
};
630 foreach my $backup (@sorted) {
631 my $volid = $backup->{volid
};
632 $maxlen = length($volid) if length($volid) > $maxlen;
636 printf("%-${maxlen}s %15s %10s\n", 'Backup', 'Backup-ID', 'Prune-Mark');
637 foreach my $backup (@sorted) {
638 my $type = $backup->{type
};
639 my $vmid = $backup->{vmid
};
640 my $backup_id = defined($vmid) ?
"$type/$vmid" : "$type";
641 printf("%-${maxlen}s %15s %10s\n", $backup->{volid
}, $backup_id, $backup->{mark
});