]>
git.proxmox.com Git - pve-storage.git/blob - PVE/CLI/pvesm.pm
4491107ecab7e1060089e4eab73ac1b287b34490
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'];
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 'cifsscan' => [ $password_map ],
70 'cifs' => [ $password_map ],
71 'pbs' => [ $password_map ],
72 'create' => [ $password_map, $enc_key_map, $master_key_map ],
73 'update' => [ $password_map, $enc_key_map, $master_key_map ],
75 return $mapping->{$name};
78 sub setup_environment
{
79 PVE
::RPCEnvironment-
>setup_default_cli_env();
82 __PACKAGE__-
>register_method ({
86 description
=> "Returns APIVER and APIAGE.",
88 additionalProperties
=> 0,
94 apiver
=> { type
=> 'integer' },
95 apiage
=> { type
=> 'integer' },
100 apiver
=> PVE
::Storage
::APIVER
,
101 apiage
=> PVE
::Storage
::APIAGE
,
106 __PACKAGE__-
>register_method ({
110 description
=> "Get filesystem path for specified volume",
112 additionalProperties
=> 0,
115 description
=> "Volume identifier",
116 type
=> 'string', format
=> 'pve-volume-id',
117 completion
=> \
&PVE
::Storage
::complete_volume
,
121 returns
=> { type
=> 'null' },
126 my $cfg = PVE
::Storage
::config
();
128 my $path = PVE
::Storage
::path
($cfg, $param->{volume
});
136 __PACKAGE__-
>register_method ({
137 name
=> 'extractconfig',
138 path
=> 'extractconfig',
140 description
=> "Extract configuration from vzdump backup archive.",
142 description
=> "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
147 additionalProperties
=> 0,
150 description
=> "Volume identifier",
152 completion
=> \
&PVE
::Storage
::complete_volume
,
156 returns
=> { type
=> 'null' },
159 my $volume = $param->{volume
};
161 my $rpcenv = PVE
::RPCEnvironment
::get
();
162 my $authuser = $rpcenv->get_user();
164 my $storage_cfg = PVE
::Storage
::config
();
165 PVE
::Storage
::check_volume_access
($rpcenv, $authuser, $storage_cfg, undef, $volume);
167 my $config_raw = PVE
::Storage
::extract_vzdump_config
($storage_cfg, $volume);
169 print "$config_raw\n";
173 my $print_content = sub {
176 my ($maxlenname, $maxsize) = (0, 0);
177 foreach my $info (@$list) {
178 my $volid = $info->{volid
};
179 my $sidlen = length ($volid);
180 $maxlenname = $sidlen if $sidlen > $maxlenname;
181 $maxsize = $info->{size
} if ($info->{size
} // 0) > $maxsize;
183 my $sizemaxdigits = length($maxsize);
185 my $basefmt = "%-${maxlenname}s %-7s %-9s %${sizemaxdigits}s";
186 printf "$basefmt %s\n", "Volid", "Format", "Type", "Size", "VMID";
188 foreach my $info (@$list) {
189 next if !$info->{vmid
};
190 my $volid = $info->{volid
};
192 printf "$basefmt %d\n", $volid, $info->{format
}, $info->{content
}, $info->{size
}, $info->{vmid
};
195 foreach my $info (sort { $a->{format
} cmp $b->{format
} } @$list) {
196 next if $info->{vmid
};
197 my $volid = $info->{volid
};
199 printf "$basefmt\n", $volid, $info->{format
}, $info->{content
}, $info->{size
};
203 my $print_status = sub {
207 foreach my $res (@$res) {
208 my $storeid = $res->{storage
};
209 $maxlen = length ($storeid) if length ($storeid) > $maxlen;
213 printf "%-${maxlen}s %10s %10s %15s %15s %15s %8s\n", 'Name', 'Type',
214 'Status', 'Total', 'Used', 'Available', '%';
216 foreach my $res (sort { $a->{storage
} cmp $b->{storage
} } @$res) {
217 my $storeid = $res->{storage
};
219 my $active = $res->{active
} ?
'active' : 'inactive';
220 my ($per, $per_fmt) = (0, '% 7.2f%%');
221 $per = ($res->{used
}*100)/$res->{total
} if $res->{total
} > 0;
223 if (!$res->{enabled
}) {
226 $active = 'disabled';
229 printf "%-${maxlen}s %10s %10s %15d %15d %15d $per_fmt\n", $storeid,
230 $res->{type
}, $active, $res->{total
}/1024, $res->{used}/1024,
231 $res->{avail
}/1024, $per;
235 __PACKAGE__-
>register_method ({
239 description
=> "Used internally to export a volume.",
242 additionalProperties
=> 0,
245 description
=> "Volume identifier",
247 completion
=> \
&PVE
::Storage
::complete_volume
,
250 description
=> "Export stream format",
252 enum
=> $KNOWN_EXPORT_FORMATS,
255 description
=> "Destination file name",
259 description
=> "Snapshot to start an incremental stream from",
261 pattern
=> qr/[a-z0-9_\-]{1,40}/i,
266 description
=> "Snapshot to export",
268 pattern
=> qr/[a-z0-9_\-]{1,40}/i,
272 'with-snapshots' => {
274 "Whether to include intermediate snapshots in the stream",
280 description
=> "Ordered list of snapshots to transfer",
282 format
=> 'string-list',
287 returns
=> { type
=> 'null' },
291 my $with_snapshots = $param->{'with-snapshots'};
292 if (defined(my $list = $param->{'snapshot-list'})) {
293 $with_snapshots = PVE
::Tools
::split_list
($list);
296 my $filename = $param->{filename
};
299 if ($filename eq '-') {
302 sysopen($outfh, $filename, O_CREAT
|O_WRONLY
|O_TRUNC
)
303 or die "open($filename): $!\n";
307 my $cfg = PVE
::Storage
::config
();
308 PVE
::Storage
::volume_export
($cfg, $outfh, $param->{volume
}, $param->{format
},
309 $param->{snapshot
}, $param->{base
}, $with_snapshots);
312 if ($filename ne '-') {
314 unlink($filename) if $err;
321 __PACKAGE__-
>register_method ({
325 description
=> "Used internally to import a volume.",
328 additionalProperties
=> 0,
331 description
=> "Volume identifier",
333 completion
=> \
&PVE
::Storage
::complete_volume
,
336 description
=> "Import stream format",
338 enum
=> $KNOWN_EXPORT_FORMATS,
341 description
=> "Source file name. For '-' stdin is used, the " .
342 "tcp://<IP-or-CIDR> format allows to use a TCP connection, " .
343 "the unix://PATH-TO-SOCKET format a UNIX socket as input." .
344 "Else, the file is treated as common file.",
348 description
=> "Base snapshot of an incremental stream",
350 pattern
=> qr/[a-z0-9_\-]{1,40}/i,
354 'with-snapshots' => {
356 "Whether the stream includes intermediate snapshots",
361 'delete-snapshot' => {
362 description
=> "A snapshot to delete on success",
364 pattern
=> qr/[a-z0-9_\-]{1,80}/i,
369 description
=> "Choose a new volume ID if the requested " .
370 "volume ID already exists, instead of throwing an error.",
376 description
=> "The current-state snapshot if the stream contains snapshots",
378 pattern
=> qr/[a-z0-9_\-]{1,40}/i,
384 returns
=> { type
=> 'string' },
388 my $filename = $param->{filename
};
391 if ($filename eq '-') {
393 } elsif ($filename =~ m!^tcp://(([^/]+)(/\d+)?)$!) {
394 my ($cidr, $ip, $subnet) = ($1, $2, $3);
395 if ($subnet) { # got real CIDR notation, not just IP
396 my $ips = PVE
::Network
::get_local_ip_from_cidr
($cidr);
397 die "Unable to get any local IP address in network '$cidr'\n"
398 if scalar(@$ips) < 1;
399 die "Got multiple local IP address in network '$cidr'\n"
400 if scalar(@$ips) > 1;
404 my $family = PVE
::Tools
::get_host_address_family
($ip);
405 my $port = PVE
::Tools
::next_migrate_port
($family, $ip);
410 Proto
=> &Socket
::IPPROTO_TCP
,
411 GetAddrInfoFlags
=> 0,
415 my $socket = IO
::Socket
::IP-
>new(%$sock_params)
416 or die "failed to open socket: $!\n";
418 print "$ip\n$port\n"; # tell remote where to connect
421 my $prev_alarm = alarm 0;
422 local $SIG{ALRM
} = sub { die "timed out waiting for client\n" };
424 my $client = $socket->accept; # Wait for a client
429 } elsif ($filename =~ m!^unix://(.*)$!) {
430 my $socket_path = $1;
431 my $socket = IO
::Socket
::UNIX-
>new(
432 Type
=> SOCK_STREAM
(),
433 Local
=> $socket_path,
435 ) or die "failed to open socket: $!\n";
440 my $prev_alarm = alarm 0;
441 local $SIG{ALRM
} = sub { die "timed out waiting for client\n" };
443 my $client = $socket->accept; # Wait for a client
449 sysopen($infh, $filename, O_RDONLY
)
450 or die "open($filename): $!\n";
453 my $cfg = PVE
::Storage
::config
();
454 my $volume = $param->{volume
};
455 my $delete = $param->{'delete-snapshot'};
456 my $imported_volid = PVE
::Storage
::volume_import
($cfg, $infh, $volume, $param->{format
},
457 $param->{snapshot
}, $param->{base
}, $param->{'with-snapshots'},
458 $param->{'allow-rename'});
459 PVE
::Storage
::volume_snapshot_delete
($cfg, $imported_volid, $delete)
461 return $imported_volid;
465 __PACKAGE__-
>register_method ({
466 name
=> 'prunebackups',
467 path
=> 'prunebackups',
469 description
=> "Prune backups. Only those using the standard naming scheme are considered. " .
470 "If no keep options are specified, those from the storage configuration are used.",
474 additionalProperties
=> 0,
477 description
=> "Only show what would be pruned, don't delete anything.",
481 node
=> get_standard_option
('pve-node'),
482 storage
=> get_standard_option
('pve-storage-id', {
483 completion
=> \
&PVE
::Storage
::complete_storage_enabled
,
485 %{$PVE::Storage
::Plugin
::prune_backups_format
},
487 description
=> "Either 'qemu' or 'lxc'. Only consider backups for guests of this type.",
490 enum
=> ['qemu', 'lxc'],
492 vmid
=> get_standard_option
('pve-vmid', {
493 description
=> "Only consider backups for this guest.",
495 completion
=> \
&PVE
::Cluster
::complete_vmid
,
503 description
=> 'If it was a dry run or not. The list will only be defined in that case.',
512 description
=> "Backup volume ID.",
516 description
=> "Creation time of the backup (seconds since the UNIX epoch).",
520 description
=> "Whether the backup would be kept or removed. For backups that don't " .
521 "use the standard naming scheme, it's 'protected'.",
525 description
=> "One of 'qemu', 'lxc', 'openvz' or 'unknown'.",
529 description
=> "The VM the backup belongs to.",
541 my $dryrun = extract_param
($param, 'dry-run') ?
1 : 0;
544 foreach my $keep (keys %{$PVE::Storage
::Plugin
::prune_backups_format
}) {
545 $keep_opts->{$keep} = extract_param
($param, $keep) if defined($param->{$keep});
547 $param->{'prune-backups'} = PVE
::JSONSchema
::print_property_string
(
548 $keep_opts, $PVE::Storage
::Plugin
::prune_backups_format
) if $keep_opts;
552 $list = PVE
::API2
::Storage
::PruneBackups-
>dryrun($param);
554 PVE
::API2
::Storage
::PruneBackups-
>delete($param);
563 my $print_api_result = sub {
564 my ($data, $schema, $options) = @_;
565 PVE
::CLIFormatter
::print_api_result
($data, $schema, undef, $options);
569 add
=> [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ],
570 set
=> [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
571 remove
=> [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
572 status
=> [ "PVE::API2::Storage::Status", 'index', [],
573 { node
=> $nodename }, $print_status ],
574 list
=> [ "PVE::API2::Storage::Content", 'index', ['storage'],
575 { node
=> $nodename }, $print_content ],
576 alloc
=> [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'],
577 { node
=> $nodename }, sub {
579 print "successfully created '$volid'\n";
581 free
=> [ "PVE::API2::Storage::Content", 'delete', ['volume'],
582 { node
=> $nodename } ],
584 nfs
=> [ "PVE::API2::Storage::Scan", 'nfsscan', ['server'], { node
=> $nodename }, sub {
588 foreach my $rec (@$res) {
589 my $len = length ($rec->{path
});
590 $maxlen = $len if $len > $maxlen;
592 foreach my $rec (@$res) {
593 printf "%-${maxlen}s %s\n", $rec->{path
}, $rec->{options
};
596 cifs
=> [ "PVE::API2::Storage::Scan", 'cifsscan', ['server'], { node
=> $nodename }, sub {
600 foreach my $rec (@$res) {
601 my $len = length ($rec->{share
});
602 $maxlen = $len if $len > $maxlen;
604 foreach my $rec (@$res) {
605 printf "%-${maxlen}s %s\n", $rec->{share
}, $rec->{description
};
608 glusterfs
=> [ "PVE::API2::Storage::Scan", 'glusterfsscan', ['server'], { node
=> $nodename }, sub {
611 foreach my $rec (@$res) {
612 printf "%s\n", $rec->{volname
};
615 iscsi
=> [ "PVE::API2::Storage::Scan", 'iscsiscan', ['portal'], { node
=> $nodename }, sub {
619 foreach my $rec (@$res) {
620 my $len = length ($rec->{target
});
621 $maxlen = $len if $len > $maxlen;
623 foreach my $rec (@$res) {
624 printf "%-${maxlen}s %s\n", $rec->{target
}, $rec->{portal
};
627 lvm
=> [ "PVE::API2::Storage::Scan", 'lvmscan', [], { node
=> $nodename }, sub {
629 foreach my $rec (@$res) {
630 printf "$rec->{vg}\n";
633 lvmthin
=> [ "PVE::API2::Storage::Scan", 'lvmthinscan', ['vg'], { node
=> $nodename }, sub {
635 foreach my $rec (@$res) {
636 printf "$rec->{lv}\n";
640 "PVE::API2::Storage::Scan",
642 ['server', 'username'],
643 { node
=> $nodename },
645 $PVE::RESTHandler
::standard_output_options
,
647 zfs
=> [ "PVE::API2::Storage::Scan", 'zfsscan', [], { node
=> $nodename }, sub {
650 foreach my $rec (@$res) {
651 printf "$rec->{pool}\n";
655 nfsscan
=> { alias
=> 'scan nfs' },
656 cifsscan
=> { alias
=> 'scan cifs' },
657 glusterfsscan
=> { alias
=> 'scan glusterfs' },
658 iscsiscan
=> { alias
=> 'scan iscsi' },
659 lvmscan
=> { alias
=> 'scan lvm' },
660 lvmthinscan
=> { alias
=> 'scan lvmthin' },
661 zfsscan
=> { alias
=> 'scan zfs' },
662 path
=> [ __PACKAGE__
, 'path', ['volume']],
663 extractconfig
=> [__PACKAGE__
, 'extractconfig', ['volume']],
664 export
=> [ __PACKAGE__
, 'export', ['volume', 'format', 'filename']],
665 import
=> [ __PACKAGE__
, 'import', ['volume', 'format', 'filename'], {}, sub {
667 print PVE
::Storage
::volume_imported_message
($volid);
669 apiinfo
=> [ __PACKAGE__
, 'apiinfo', [], {}, sub {
672 print "APIVER $res->{apiver}\n";
673 print "APIAGE $res->{apiage}\n";
675 'prune-backups' => [ __PACKAGE__
, 'prunebackups', ['storage'], { node
=> $nodename }, sub {
678 my ($dryrun, $list) = ($res->{dryrun
}, $res->{list
});
682 if (!scalar(@{$list})) {
683 print "No backups found\n";
687 print "NOTE: this is only a preview and might not be what a subsequent\n" .
688 "prune call does if backups are removed/added in the meantime.\n\n";
691 my $vmcmp = PVE
::Tools
::safe_compare
($a->{vmid
}, $b->{vmid
}, sub { $_[0] <=> $_[1] });
692 return $vmcmp if $vmcmp ne 0;
693 return $a->{ctime
} <=> $b->{ctime
};
697 foreach my $backup (@sorted) {
698 my $volid = $backup->{volid
};
699 $maxlen = length($volid) if length($volid) > $maxlen;
703 printf("%-${maxlen}s %15s %10s\n", 'Backup', 'Backup-ID', 'Prune-Mark');
704 foreach my $backup (@sorted) {
705 my $type = $backup->{type
};
706 my $vmid = $backup->{vmid
};
707 my $backup_id = defined($vmid) ?
"$type/$vmid" : "$type";
708 printf("%-${maxlen}s %15s %10s\n", $backup->{volid
}, $backup_id, $backup->{mark
});