]>
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
::Status
;
20 use PVE
::JSONSchema
qw(get_standard_option);
25 use base
qw(PVE::CLIHandler);
27 my $KNOWN_EXPORT_FORMATS = ['raw+size', 'tar+size', 'qcow2+size', 'vmdk+size', 'zfs'];
29 my $nodename = PVE
::INotify
::nodename
();
34 my $password_map = PVE
::CLIHandler
::get_standard_mapping
('pve-password', {
37 return $value if $value;
38 return PVE
::PTY
::read_password
("Enter Password: ");
43 name
=> 'encryption-key',
44 desc
=> 'a file containing an encryption key, or the special value "autogen"',
47 return $value if $value eq 'autogen';
48 return PVE
::Tools
::file_get_contents
($value);
54 'cifsscan' => [ $password_map ],
55 'create' => [ $password_map, $enc_key_map ],
56 'update' => [ $password_map, $enc_key_map ],
58 return $mapping->{$name};
61 sub setup_environment
{
62 PVE
::RPCEnvironment-
>setup_default_cli_env();
65 __PACKAGE__-
>register_method ({
69 description
=> "Returns APIVER and APIAGE.",
71 additionalProperties
=> 0,
77 apiver
=> { type
=> 'integer' },
78 apiage
=> { type
=> 'integer' },
83 apiver
=> PVE
::Storage
::APIVER
,
84 apiage
=> PVE
::Storage
::APIAGE
,
89 __PACKAGE__-
>register_method ({
93 description
=> "Get filesystem path for specified volume",
95 additionalProperties
=> 0,
98 description
=> "Volume identifier",
99 type
=> 'string', format
=> 'pve-volume-id',
100 completion
=> \
&PVE
::Storage
::complete_volume
,
104 returns
=> { type
=> 'null' },
109 my $cfg = PVE
::Storage
::config
();
111 my $path = PVE
::Storage
::path
($cfg, $param->{volume
});
119 __PACKAGE__-
>register_method ({
120 name
=> 'extractconfig',
121 path
=> 'extractconfig',
123 description
=> "Extract configuration from vzdump backup archive.",
125 description
=> "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
130 additionalProperties
=> 0,
133 description
=> "Volume identifier",
135 completion
=> \
&PVE
::Storage
::complete_volume
,
139 returns
=> { type
=> 'null' },
142 my $volume = $param->{volume
};
144 my $rpcenv = PVE
::RPCEnvironment
::get
();
145 my $authuser = $rpcenv->get_user();
147 my $storage_cfg = PVE
::Storage
::config
();
148 PVE
::Storage
::check_volume_access
($rpcenv, $authuser, $storage_cfg, undef, $volume);
150 my $config_raw = PVE
::Storage
::extract_vzdump_config
($storage_cfg, $volume);
152 print "$config_raw\n";
156 my $print_content = sub {
159 my ($maxlenname, $maxsize) = (0, 0);
160 foreach my $info (@$list) {
161 my $volid = $info->{volid
};
162 my $sidlen = length ($volid);
163 $maxlenname = $sidlen if $sidlen > $maxlenname;
164 $maxsize = $info->{size
} if ($info->{size
} // 0) > $maxsize;
166 my $sizemaxdigits = length($maxsize);
168 my $basefmt = "%-${maxlenname}s %-7s %-9s %${sizemaxdigits}s";
169 printf "$basefmt %s\n", "Volid", "Format", "Type", "Size", "VMID";
171 foreach my $info (@$list) {
172 next if !$info->{vmid
};
173 my $volid = $info->{volid
};
175 printf "$basefmt %d\n", $volid, $info->{format
}, $info->{content
}, $info->{size
}, $info->{vmid
};
178 foreach my $info (sort { $a->{format
} cmp $b->{format
} } @$list) {
179 next if $info->{vmid
};
180 my $volid = $info->{volid
};
182 printf "$basefmt\n", $volid, $info->{format
}, $info->{content
}, $info->{size
};
186 my $print_status = sub {
190 foreach my $res (@$res) {
191 my $storeid = $res->{storage
};
192 $maxlen = length ($storeid) if length ($storeid) > $maxlen;
196 printf "%-${maxlen}s %10s %10s %15s %15s %15s %8s\n", 'Name', 'Type',
197 'Status', 'Total', 'Used', 'Available', '%';
199 foreach my $res (sort { $a->{storage
} cmp $b->{storage
} } @$res) {
200 my $storeid = $res->{storage
};
202 my $active = $res->{active
} ?
'active' : 'inactive';
203 my ($per, $per_fmt) = (0, '% 7.2f%%');
204 $per = ($res->{used
}*100)/$res->{total
} if $res->{total
} > 0;
206 if (!$res->{enabled
}) {
209 $active = 'disabled';
212 printf "%-${maxlen}s %10s %10s %15d %15d %15d $per_fmt\n", $storeid,
213 $res->{type
}, $active, $res->{total
}/1024, $res->{used}/1024,
214 $res->{avail
}/1024, $per;
218 __PACKAGE__-
>register_method ({
222 description
=> "Used internally to export a volume.",
225 additionalProperties
=> 0,
228 description
=> "Volume identifier",
230 completion
=> \
&PVE
::Storage
::complete_volume
,
233 description
=> "Export stream format",
235 enum
=> $KNOWN_EXPORT_FORMATS,
238 description
=> "Destination file name",
242 description
=> "Snapshot to start an incremental stream from",
244 pattern
=> qr/[a-z0-9_\-]{1,40}/,
249 description
=> "Snapshot to export",
251 pattern
=> qr/[a-z0-9_\-]{1,40}/,
255 'with-snapshots' => {
257 "Whether to include intermediate snapshots in the stream",
264 returns
=> { type
=> 'null' },
268 my $filename = $param->{filename
};
271 if ($filename eq '-') {
274 sysopen($outfh, $filename, O_CREAT
|O_WRONLY
|O_TRUNC
)
275 or die "open($filename): $!\n";
279 my $cfg = PVE
::Storage
::config
();
280 PVE
::Storage
::volume_export
($cfg, $outfh, $param->{volume
}, $param->{format
},
281 $param->{snapshot
}, $param->{base
}, $param->{'with-snapshots'});
284 if ($filename ne '-') {
286 unlink($filename) if $err;
293 __PACKAGE__-
>register_method ({
297 description
=> "Used internally to import a volume.",
300 additionalProperties
=> 0,
303 description
=> "Volume identifier",
305 completion
=> \
&PVE
::Storage
::complete_volume
,
308 description
=> "Import stream format",
310 enum
=> $KNOWN_EXPORT_FORMATS,
313 description
=> "Source file name. For '-' stdin is used, the " .
314 "tcp://<IP-or-CIDR> format allows to use a TCP connection as input. " .
315 "Else, the file is treated as common file.",
319 description
=> "Base snapshot of an incremental stream",
321 pattern
=> qr/[a-z0-9_\-]{1,40}/,
325 'with-snapshots' => {
327 "Whether the stream includes intermediate snapshots",
332 'delete-snapshot' => {
333 description
=> "A snapshot to delete on success",
335 pattern
=> qr/[a-z0-9_\-]{1,80}/,
340 description
=> "Choose a new volume ID if the requested " .
341 "volume ID already exists, instead of throwing an error.",
348 returns
=> { type
=> 'string' },
352 my $filename = $param->{filename
};
355 if ($filename eq '-') {
357 } elsif ($filename =~ m!^tcp://(([^/]+)(/\d+)?)$!) {
358 my ($cidr, $ip, $subnet) = ($1, $2, $3);
359 if ($subnet) { # got real CIDR notation, not just IP
360 my $ips = PVE
::Network
::get_local_ip_from_cidr
($cidr);
361 die "Unable to get any local IP address in network '$cidr'\n"
362 if scalar(@$ips) < 1;
363 die "Got multiple local IP address in network '$cidr'\n"
364 if scalar(@$ips) > 1;
368 my $family = PVE
::Tools
::get_host_address_family
($ip);
369 my $port = PVE
::Tools
::next_migrate_port
($family, $ip);
374 Proto
=> &Socket
::IPPROTO_TCP
,
375 GetAddrInfoFlags
=> 0,
379 my $socket = IO
::Socket
::IP-
>new(%$sock_params)
380 or die "failed to open socket: $!\n";
382 print "$ip\n$port\n"; # tell remote where to connect
385 my $prev_alarm = alarm 0;
386 local $SIG{ALRM
} = sub { die "timed out waiting for client\n" };
388 my $client = $socket->accept; # Wait for a client
394 sysopen($infh, $filename, O_RDONLY
)
395 or die "open($filename): $!\n";
398 my $cfg = PVE
::Storage
::config
();
399 my $volume = $param->{volume
};
400 my $delete = $param->{'delete-snapshot'};
401 my $imported_volid = PVE
::Storage
::volume_import
($cfg, $infh, $volume, $param->{format
},
402 $param->{base
}, $param->{'with-snapshots'}, $param->{'allow-rename'});
403 PVE
::Storage
::volume_snapshot_delete
($cfg, $imported_volid, $delete)
405 return $imported_volid;
409 __PACKAGE__-
>register_method ({
413 description
=> "Scan remote NFS server.",
417 check
=> ['perm', '/storage', ['Datastore.Allocate']],
420 additionalProperties
=> 0,
422 node
=> get_standard_option
('pve-node'),
424 description
=> "The server address (name or IP).",
425 type
=> 'string', format
=> 'pve-storage-server',
435 description
=> "The exported path.",
439 description
=> "NFS export options.",
448 my $server = $param->{server
};
449 my $res = PVE
::Storage
::scan_nfs
($server);
452 foreach my $k (sort keys %$res) {
453 push @$data, { path
=> $k, options
=> $res->{$k} };
458 __PACKAGE__-
>register_method ({
462 description
=> "Scan remote CIFS server.",
466 check
=> ['perm', '/storage', ['Datastore.Allocate']],
469 additionalProperties
=> 0,
471 node
=> get_standard_option
('pve-node'),
473 description
=> "The server address (name or IP).",
474 type
=> 'string', format
=> 'pve-storage-server',
477 description
=> "User name.",
482 description
=> "User password.",
487 description
=> "SMB domain (Workgroup).",
499 description
=> "The cifs share name.",
503 description
=> "Descriptive text from server.",
512 my $server = $param->{server
};
514 my $username = $param->{username
};
515 my $password = $param->{password
};
516 my $domain = $param->{domain
};
518 my $res = PVE
::Storage
::scan_cifs
($server, $username, $password, $domain);
521 foreach my $k (sort keys %$res) {
522 push @$data, { share
=> $k, description
=> $res->{$k} };
528 # Note: GlusterFS currently does not have an equivalent of showmount.
529 # As workaround, we simply use nfs showmount.
530 # see http://www.gluster.org/category/volumes/
532 __PACKAGE__-
>register_method ({
533 name
=> 'glusterfsscan',
536 description
=> "Scan remote GlusterFS server.",
540 check
=> ['perm', '/storage', ['Datastore.Allocate']],
543 additionalProperties
=> 0,
545 node
=> get_standard_option
('pve-node'),
547 description
=> "The server address (name or IP).",
548 type
=> 'string', format
=> 'pve-storage-server',
558 description
=> "The volume name.",
567 my $server = $param->{server
};
568 my $res = PVE
::Storage
::scan_nfs
($server);
571 foreach my $path (sort keys %$res) {
572 if ($path =~ m!^/([^\s/]+)$!) {
573 push @$data, { volname
=> $1 };
579 __PACKAGE__-
>register_method ({
583 description
=> "Scan remote iSCSI server.",
587 check
=> ['perm', '/storage', ['Datastore.Allocate']],
590 additionalProperties
=> 0,
592 node
=> get_standard_option
('pve-node'),
594 description
=> "The iSCSI portal (IP or DNS name with optional port).",
595 type
=> 'string', format
=> 'pve-storage-portal-dns',
605 description
=> "The iSCSI target name.",
609 description
=> "The iSCSI portal name.",
618 my $res = PVE
::Storage
::scan_iscsi
($param->{portal
});
621 foreach my $k (sort keys %$res) {
622 push @$data, { target
=> $k, portal
=> join(',', @{$res->{$k}}) };
628 __PACKAGE__-
>register_method ({
632 description
=> "List local LVM volume groups.",
636 check
=> ['perm', '/storage', ['Datastore.Allocate']],
639 additionalProperties
=> 0,
641 node
=> get_standard_option
('pve-node'),
650 description
=> "The LVM logical volume group name.",
659 my $res = PVE
::Storage
::LVMPlugin
::lvm_vgs
();
660 return PVE
::RESTHandler
::hash_to_array
($res, 'vg');
663 __PACKAGE__-
>register_method ({
664 name
=> 'lvmthinscan',
667 description
=> "List local LVM Thin Pools.",
671 check
=> ['perm', '/storage', ['Datastore.Allocate']],
674 additionalProperties
=> 0,
676 node
=> get_standard_option
('pve-node'),
679 pattern
=> '[a-zA-Z0-9\.\+\_][a-zA-Z0-9\.\+\_\-]+', # see lvm(8) manpage
690 description
=> "The LVM Thin Pool name (LVM logical volume).",
699 return PVE
::Storage
::LvmThinPlugin
::list_thinpools
($param->{vg
});
702 __PACKAGE__-
>register_method ({
706 description
=> "Scan zfs pool list on local node.",
710 check
=> ['perm', '/storage', ['Datastore.Allocate']],
713 additionalProperties
=> 0,
715 node
=> get_standard_option
('pve-node'),
724 description
=> "ZFS pool name.",
733 return PVE
::Storage
::scan_zfs
();
736 __PACKAGE__-
>register_method ({
737 name
=> 'prunebackups',
738 path
=> 'prunebackups',
740 description
=> "Prune backups. This is only a wrapper for the proper API endpoints.",
744 additionalProperties
=> 0,
747 description
=> "Only show what would be pruned, don't delete anything.",
751 node
=> get_standard_option
('pve-node'),
752 storage
=> get_standard_option
('pve-storage-id', {
753 completion
=> \
&PVE
::Storage
::complete_storage_enabled
,
755 'prune-backups' => get_standard_option
('prune-backups', {
756 description
=> "Use these retention options instead of those from the storage configuration.",
760 description
=> "Either 'qemu' or 'lxc'. Only consider backups for guests of this type.",
763 enum
=> ['qemu', 'lxc'],
765 vmid
=> get_standard_option
('pve-vmid', {
766 description
=> "Only consider backups for this guest.",
768 completion
=> \
&PVE
::Cluster
::complete_vmid
,
776 description
=> 'If it was a dry run or not. The list will only be defined in that case.',
785 description
=> "Backup volume ID.",
789 description
=> "Creation time of the backup (seconds since the UNIX epoch).",
793 description
=> "Whether the backup would be kept or removed. For backups that don't " .
794 "use the standard naming scheme, it's 'protected'.",
798 description
=> "One of 'qemu', 'lxc', 'openvz' or 'unknown'.",
802 description
=> "The VM the backup belongs to.",
814 my $dryrun = extract_param
($param, 'dry-run') ?
1 : 0;
818 $list = PVE
::API2
::Storage
::PruneBackups-
>dryrun($param);
820 PVE
::API2
::Storage
::PruneBackups-
>delete($param);
830 add
=> [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ],
831 set
=> [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
832 remove
=> [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
833 status
=> [ "PVE::API2::Storage::Status", 'index', [],
834 { node
=> $nodename }, $print_status ],
835 list
=> [ "PVE::API2::Storage::Content", 'index', ['storage'],
836 { node
=> $nodename }, $print_content ],
837 alloc
=> [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'],
838 { node
=> $nodename }, sub {
840 print "successfully created '$volid'\n";
842 free
=> [ "PVE::API2::Storage::Content", 'delete', ['volume'],
843 { node
=> $nodename } ],
845 nfs
=> [ __PACKAGE__
, 'nfsscan', ['server'], { node
=> $nodename }, sub {
849 foreach my $rec (@$res) {
850 my $len = length ($rec->{path
});
851 $maxlen = $len if $len > $maxlen;
853 foreach my $rec (@$res) {
854 printf "%-${maxlen}s %s\n", $rec->{path
}, $rec->{options
};
857 cifs
=> [ __PACKAGE__
, 'cifsscan', ['server'], { node
=> $nodename }, sub {
861 foreach my $rec (@$res) {
862 my $len = length ($rec->{share
});
863 $maxlen = $len if $len > $maxlen;
865 foreach my $rec (@$res) {
866 printf "%-${maxlen}s %s\n", $rec->{share
}, $rec->{description
};
869 glusterfs
=> [ __PACKAGE__
, 'glusterfsscan', ['server'], { node
=> $nodename }, sub {
872 foreach my $rec (@$res) {
873 printf "%s\n", $rec->{volname
};
876 iscsi
=> [ __PACKAGE__
, 'iscsiscan', ['portal'], { node
=> $nodename }, sub {
880 foreach my $rec (@$res) {
881 my $len = length ($rec->{target
});
882 $maxlen = $len if $len > $maxlen;
884 foreach my $rec (@$res) {
885 printf "%-${maxlen}s %s\n", $rec->{target
}, $rec->{portal
};
888 lvm
=> [ __PACKAGE__
, 'lvmscan', [], { node
=> $nodename }, sub {
890 foreach my $rec (@$res) {
891 printf "$rec->{vg}\n";
894 lvmthin
=> [ __PACKAGE__
, 'lvmthinscan', ['vg'], { node
=> $nodename }, sub {
896 foreach my $rec (@$res) {
897 printf "$rec->{lv}\n";
900 zfs
=> [ __PACKAGE__
, 'zfsscan', [], { node
=> $nodename }, sub {
903 foreach my $rec (@$res) {
904 printf "$rec->{pool}\n";
908 nfsscan
=> { alias
=> 'scan nfs' },
909 cifsscan
=> { alias
=> 'scan cifs' },
910 glusterfsscan
=> { alias
=> 'scan glusterfs' },
911 iscsiscan
=> { alias
=> 'scan iscsi' },
912 lvmscan
=> { alias
=> 'scan lvm' },
913 lvmthinscan
=> { alias
=> 'scan lvmthin' },
914 zfsscan
=> { alias
=> 'scan zfs' },
915 path
=> [ __PACKAGE__
, 'path', ['volume']],
916 extractconfig
=> [__PACKAGE__
, 'extractconfig', ['volume']],
917 export
=> [ __PACKAGE__
, 'export', ['volume', 'format', 'filename']],
918 import
=> [ __PACKAGE__
, 'import', ['volume', 'format', 'filename'], {}, sub {
920 print PVE
::Storage
::volume_imported_message
($volid);
922 apiinfo
=> [ __PACKAGE__
, 'apiinfo', [], {}, sub {
925 print "APIVER $res->{apiver}\n";
926 print "APIAGE $res->{apiage}\n";
928 'prune-backups' => [ __PACKAGE__
, 'prunebackups', ['storage'], { node
=> $nodename }, sub {
931 my ($dryrun, $list) = ($res->{dryrun
}, $res->{list
});
935 print "NOTE: this is only a preview and might not be exactly what a subsequent prune call does,\n" .
936 "if the hour changes or if backups are removed/added in the meantime.\n\n";
939 my $vmcmp = PVE
::Tools
::safe_compare
($a->{vmid
}, $b->{vmid
}, sub { $_[0] <=> $_[1] });
940 return $vmcmp if $vmcmp ne 0;
941 return $a->{ctime
} <=> $b->{ctime
};
945 foreach my $backup (@sorted) {
946 my $volid = $backup->{volid
};
947 $maxlen = length($volid) if length($volid) > $maxlen;
951 printf("%-${maxlen}s %15s %10s\n", 'Backup', 'Backup-ID', 'Prune-Mark');
952 foreach my $backup (@sorted) {
953 my $type = $backup->{type
};
954 my $vmid = $backup->{vmid
};
955 my $backup_id = defined($vmid) ?
"$type/$vmid" : "$type";
956 printf("%-${maxlen}s %15s %10s\n", $backup->{volid
}, $backup_id, $backup->{mark
});