use PVE::Tools qw(run_command file_read_firstline dir_glob_foreach $IPV6RE);
use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file);
+use PVE::DataCenterConfig;
use PVE::Exception qw(raise_param_exc);
use PVE::JSONSchema;
use PVE::INotify;
use PVE::RPCEnvironment;
+use PVE::SSHInfo;
use PVE::Storage::Plugin;
use PVE::Storage::DirPlugin;
use PVE::Storage::ZFSPoolPlugin;
use PVE::Storage::ZFSPlugin;
use PVE::Storage::DRBDPlugin;
+use PVE::Storage::PBSPlugin;
# Storage API version. Icrement it on changes in storage API interface.
-use constant APIVER => 3;
+use constant APIVER => 5;
# Age is the number of versions we're backward compatible with.
# This is like having 'current=APIVER' and age='APIAGE' in libtool,
# see https://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html
-use constant APIAGE => 2;
+use constant APIAGE => 4;
# load standard plugins
PVE::Storage::DirPlugin->register();
PVE::Storage::ZFSPoolPlugin->register();
PVE::Storage::ZFSPlugin->register();
PVE::Storage::DRBDPlugin->register();
+PVE::Storage::PBSPlugin->register();
# load third-party plugins
if ( -d '/usr/share/perl5/PVE/Storage/Custom' ) {
my $UDEVADM = '/sbin/udevadm';
+our $iso_extension_re = qr/\.(?:iso|img)/i;
+
# PVE::Storage utility functions
sub config {
my $scfg = $cfg->{ids}->{$storeid};
- die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg);
+ die "storage '$storeid' does not exist\n" if (!$noerr && !$scfg);
return $scfg;
}
sub volume_resize {
my ($cfg, $volid, $size, $running) = @_;
+ my $padding = (1024 - $size % 1024) % 1024;
+ $size = $size + $padding;
+
my ($storeid, $volname) = parse_volume_id($volid, 1);
if ($storeid) {
my $scfg = storage_config($cfg, $storeid);
}
sub volume_has_feature {
- my ($cfg, $feature, $volid, $snap, $running) = @_;
+ my ($cfg, $feature, $volid, $snap, $running, $opts) = @_;
my ($storeid, $volname) = parse_volume_id($volid, 1);
if ($storeid) {
my $scfg = storage_config($cfg, $storeid);
my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
- return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running);
+ return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running, $opts);
} elsif ($volid =~ m|^(/.+)$| && -e $volid) {
return undef;
} else {
return ('images', $info->{volid});
}
}
- } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
+ } elsif ($path =~ m!^$isodir/([^/]+$iso_extension_re)$!) {
my $name = $1;
return ('iso', "$sid:iso/$name");
} elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
my ($cfg, $volid) = @_;
my $path;
- if (PVE::Storage::parse_volume_id ($volid, 1)) {
- PVE::Storage::activate_volumes($cfg, [ $volid ]);
+ if (parse_volume_id ($volid, 1)) {
+ activate_volumes($cfg, [ $volid ]);
$path = PVE::Storage::path($cfg, $volid);
} else {
if (-f $volid) {
}
sub storage_migrate {
- my ($cfg, $volid, $target_sshinfo, $target_storeid, $target_volname, $base_snapshot, $snapshot, $ratelimit_bps, $insecure, $with_snapshots, $logfunc) = @_;
+ my ($cfg, $volid, $target_sshinfo, $target_storeid, $opts, $logfunc) = @_;
+
+ my $base_snapshot = $opts->{base_snapshot};
+ my $snapshot = $opts->{snapshot};
+ my $ratelimit_bps = $opts->{ratelimit_bps};
+ my $insecure = $opts->{insecure};
+ my $with_snapshots = $opts->{with_snapshots} ? 1 : 0;
+ my $allow_rename = $opts->{allow_rename} ? 1 : 0;
my ($storeid, $volname) = parse_volume_id($volid);
- $target_volname = $volname if !$target_volname;
+ my $target_volname = $opts->{target_volname} || $volname;
my $scfg = storage_config($cfg, $storeid);
# no need to migrate shared content
- return if $storeid eq $target_storeid && $scfg->{shared};
+ return $volid if $storeid eq $target_storeid && $scfg->{shared};
my $tcfg = storage_config($cfg, $target_storeid);
+ my $vtype = (parse_volname($cfg, $volid))[0];
+
+ die "content type '$vtype' is not available on storage '$target_storeid'\n"
+ if !$tcfg->{content}->{$vtype};
+
my $target_volid = "${target_storeid}:${target_volname}";
my $target_ip = $target_sshinfo->{ip};
- my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_sshinfo->{name}'";
- my $ssh = PVE::Cluster::ssh_info_to_command($target_sshinfo);
- my $ssh_base = PVE::Cluster::ssh_info_to_command_base($target_sshinfo);
+ my $ssh = PVE::SSHInfo::ssh_info_to_command($target_sshinfo);
+ my $ssh_base = PVE::SSHInfo::ssh_info_to_command_base($target_sshinfo);
local $ENV{RSYNC_RSH} = PVE::Tools::cmd2string($ssh_base);
my @cstream = ([ '/usr/bin/cstream', '-t', $ratelimit_bps ])
}
}
- my @formats = volume_transfer_formats($cfg, $volid, $volid, $snapshot, $base_snapshot, $with_snapshots);
+ my @formats = volume_transfer_formats($cfg, $volid, $target_volid, $snapshot, $base_snapshot, $with_snapshots);
die "cannot migrate from storage type '$scfg->{type}' to '$tcfg->{type}'\n" if !@formats;
my $format = $formats[0];
$import_fn = "tcp://$net";
}
- $with_snapshots = $with_snapshots ? 1 : 0; # sanitize for passing as cli parameter
+ my $target_apiver = 1; # if there is no apiinfo call, assume 1
+ my $get_api_version = [@$ssh, 'pvesm', 'apiinfo'];
+ my $match_api_version = sub { $target_apiver = $1 if $_[0] =~ m!^APIVER (\d+)$!; };
+ eval { run_command($get_api_version, logfunc => $match_api_version); };
+
my $send = ['pvesm', 'export', $volid, $format, '-', '-with-snapshots', $with_snapshots];
- my $recv = [@$ssh, '--', 'pvesm', 'import', $volid, $format, $import_fn, '-with-snapshots', $with_snapshots];
+ my $recv = [@$ssh, '--', 'pvesm', 'import', $target_volid, $format, $import_fn, '-with-snapshots', $with_snapshots];
if (defined($snapshot)) {
push @$send, '-snapshot', $snapshot
}
if ($migration_snapshot) {
push @$recv, '-delete-snapshot', $snapshot;
}
+ push @$recv, '-allow-rename', $allow_rename if $target_apiver >= 5;
if (defined($base_snapshot)) {
# Check if the snapshot exists on the remote side:
push @$recv, '-base', $base_snapshot;
}
+ my $new_volid;
+ my $pattern = volume_imported_message(undef, 1);
+ my $match_volid_and_log = sub {
+ my $line = shift;
+
+ $new_volid = $1 if ($line =~ $pattern);
+
+ if ($logfunc) {
+ chomp($line);
+ $logfunc->($line);
+ }
+ };
+
volume_snapshot($cfg, $volid, $snapshot) if $migration_snapshot;
eval {
if ($insecure) {
or die "failed to connect to tunnel at $ip:$port\n";
# we won't be reading from the socket
shutdown($socket, 0);
- run_command([$send, @cstream], output => '>&'.fileno($socket));
+ run_command([$send, @cstream], output => '>&'.fileno($socket), errfunc => $logfunc);
# don't close the connection entirely otherwise the receiving end
# might not get all buffered data (and fails with 'connection reset by peer')
shutdown($socket, 1);
- 1 while <$info>; # wait for the remote process to finish
+
+ # wait for the remote process to finish
+ while (my $line = <$info>) {
+ $match_volid_and_log->("[$target_sshinfo->{name}] $line");
+ }
+
# now close the socket
close($socket);
if (!close($info)) { # does waitpid()
die "import failed: exit code ".($?>>8)."\n";
}
} else {
- run_command([$send, @cstream, $recv], logfunc => $logfunc);
+ run_command([$send, @cstream, $recv], logfunc => $match_volid_and_log);
}
+
+ die "unable to get ID of the migrated volume\n"
+ if !defined($new_volid) && $target_apiver >= 5;
};
my $err = $@;
warn "send/receive failed, cleaning up snapshot(s)..\n" if $err;
warn "could not remove source snapshot: $@\n" if $@;
}
die $err if $err;
+
+ return $new_volid // $target_volid;
}
sub vdisk_clone {
sub volume_list {
my ($cfg, $storeid, $vmid, $content) = @_;
- my @ctypes = qw(images vztmpl iso backup snippets);
+ my @ctypes = qw(rootdir images vztmpl iso backup snippets);
my $cts = $content ? [ $content ] : [ @ctypes ];
sub extract_vzdump_config {
my ($cfg, $volid) = @_;
+ my ($storeid, $volname) = parse_volume_id($volid);
+ if (defined($storeid)) {
+ my $scfg = storage_config($cfg, $storeid);
+ if ($scfg->{type} eq 'pbs') {
+ storage_check_enabled($cfg, $storeid);
+ return PVE::Storage::PBSPlugin->extract_vzdump_config($scfg, $volname, $storeid);
+ }
+ }
+
my $archive = abs_filesystem_path($cfg, $volid);
if ($volid =~ /vzdump-(lxc|openvz)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|(tar(\.(gz|lzo))?))$/) {
}
sub volume_import {
- my ($cfg, $fh, $volid, $format, $base_snapshot, $with_snapshots) = @_;
+ my ($cfg, $fh, $volid, $format, $base_snapshot, $with_snapshots, $allow_rename) = @_;
my ($storeid, $volname) = parse_volume_id($volid, 1);
die "cannot import into volume '$volid'\n" if !$storeid;
my $scfg = storage_config($cfg, $storeid);
my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
return $plugin->volume_import($scfg, $storeid, $fh, $volname, $format,
- $base_snapshot, $with_snapshots);
+ $base_snapshot, $with_snapshots, $allow_rename) // $volid;
}
sub volume_export_formats {
return @common;
}
+sub volume_imported_message {
+ my ($volid, $want_pattern) = @_;
+
+ if ($want_pattern) {
+ return qr/successfully imported '([^']*)'$/;
+ } else {
+ return "successfully imported '$volid'\n";
+ }
+}
+
# bash completion helper
sub complete_storage {