]> git.proxmox.com Git - pve-container.git/blobdiff - src/PVE/LXC/Migrate.pm
migrate: also set targetsid for unreferenced disks
[pve-container.git] / src / PVE / LXC / Migrate.pm
index 7df3c4c84600d183c5a3cde261e7f38796c65dc1..2ef1cce42adf738050c6cf37043c2bbb54bc01a8 100644 (file)
@@ -44,21 +44,14 @@ sub prepare {
     }
     $self->{was_running} = $running;
 
-    my $force = $self->{opts}->{force} // 0;
-    my $need_activate = [];
-
-    PVE::LXC::Config->foreach_mountpoint($conf, sub {
+    PVE::LXC::Config->foreach_volume_full($conf, { include_unused => 1 }, sub {
        my ($ms, $mountpoint) = @_;
 
        my $volid = $mountpoint->{volume};
        my $type = $mountpoint->{type};
 
-       # skip dev/bind mps when forced / shared
+       # skip dev/bind mps when shared
        if ($type ne 'volume') {
-           if ($force) {
-               warn "-force is deprecated, please use the 'shared' property on individual non-volume mount points instead!\n";
-               return;
-           }
            if ($mountpoint->{shared}) {
                return;
            } else {
@@ -70,9 +63,12 @@ sub prepare {
        die "can't determine assigned storage for mount point '$ms'\n" if !$storage;
 
        # check if storage is available on both nodes
-       my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $storage);
-       PVE::Storage::storage_check_node($self->{storecfg}, $storage, $self->{node});
+       my $scfg = PVE::Storage::storage_check_enabled($self->{storecfg}, $storage);
+
+       my $targetsid = $storage;
 
+       die "content type 'rootdir' is not available on storage '$storage'\n"
+           if !$scfg->{content}->{rootdir};
 
        if ($scfg->{shared}) {
            # PVE::Storage::activate_storage checks this for non-shared storages
@@ -80,17 +76,18 @@ sub prepare {
            warn "Used shared storage '$storage' is not online on source node!\n"
                if !$plugin->check_connection($storage, $scfg);
        } else {
-           # only activate if not shared
-           push @$need_activate, $volid;
-
            # unless in restart mode because we shut the container down
            die "unable to migrate local mount point '$volid' while CT is running"
                if $running && !$restart;
+
+           $targetsid = PVE::JSONSchema::map_id($self->{opts}->{storagemap}, $storage);
        }
 
-    });
+       my $target_scfg = PVE::Storage::storage_check_enabled($self->{storecfg}, $targetsid, $self->{node});
 
-    PVE::Storage::activate_volumes($self->{storecfg}, $need_activate);
+       die "$volid: content type 'rootdir' is not available on storage '$targetsid'\n"
+           if !$target_scfg->{content}->{rootdir};
+    });
 
     # todo: test if VM uses local resources
 
@@ -145,18 +142,27 @@ sub phase1 {
 
        my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
 
-       # check if storage is available on both nodes
-       my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $sid);
-       PVE::Storage::storage_check_node($self->{storecfg}, $sid, $self->{node});
+       # check if storage is available on source node
+       my $scfg = PVE::Storage::storage_check_enabled($self->{storecfg}, $sid);
+
+       my $targetsid = $sid;
 
        if ($scfg->{shared}) {
            $self->log('info', "volume '$volid' is on shared storage '$sid'")
                if !$snapname;
            return;
+       } else {
+           $targetsid = PVE::JSONSchema::map_id($self->{opts}->{storagemap}, $sid);
        }
 
+       PVE::Storage::storage_check_enabled($self->{storecfg}, $targetsid, $self->{node});
+
+       my $bwlimit = $self->get_bwlimit($sid, $targetsid);
+
        $volhash->{$volid}->{ref} = defined($snapname) ? 'snapshot' : 'config';
-       $volhash->{$volid}->{snapshots} = defined($snapname);
+       $volhash->{$volid}->{snapshots} = 1 if defined($snapname);
+       $volhash->{$volid}->{targetsid} = $targetsid;
+       $volhash->{$volid}->{bwlimit} = $bwlimit;
 
        my ($path, $owner) = PVE::Storage::path($self->{storecfg}, $volid);
 
@@ -165,8 +171,8 @@ sub phase1 {
 
        if (defined($snapname)) {
            # we cannot migrate shapshots on local storage
-           # exceptions: 'zfspool'
-           if (($scfg->{type} eq 'zfspool')) {
+           # exceptions: 'zfspool', 'btrfs'
+           if ($scfg->{type} eq 'zfspool' || $scfg->{type} eq 'btrfs') {
                return;
            }
            die "non-migratable snapshot exists\n";
@@ -198,18 +204,23 @@ sub phase1 {
        next if $scfg->{shared};
        next if !PVE::Storage::storage_check_enabled($self->{storecfg}, $storeid, undef, 1);
 
-       # get list from PVE::Storage (for unused volumes)
-       my $dl = PVE::Storage::vdisk_list($self->{storecfg}, $storeid, $vmid);
+       # get list from PVE::Storage (for unreferenced volumes)
+       my $dl = PVE::Storage::vdisk_list($self->{storecfg}, $storeid, $vmid, undef, 'rootdir');
 
        next if @{$dl->{$storeid}} == 0;
 
        # check if storage is available on target node
-       PVE::Storage::storage_check_node($self->{storecfg}, $storeid, $self->{node});
+       my $targetsid = PVE::JSONSchema::map_id($self->{opts}->{storagemap}, $storeid);
+       my $target_scfg = PVE::Storage::storage_check_enabled($self->{storecfg}, $targetsid, $self->{node});
+
+       die "content type 'rootdir' is not available on storage '$targetsid'\n"
+           if !$target_scfg->{content}->{rootdir};
 
        PVE::Storage::foreach_volid($dl, sub {
            my ($volid, $sid, $volname) = @_;
 
            $volhash->{$volid}->{ref} = 'storage';
+           $volhash->{$volid}->{targetsid} = $targetsid;
        });
     }
 
@@ -217,12 +228,11 @@ sub phase1 {
     foreach my $snapname (keys %{$conf->{snapshots}}) {
        &$test_volid($conf->{snapshots}->{$snapname}->{'vmstate'}, 0, undef)
            if defined($conf->{snapshots}->{$snapname}->{'vmstate'});
-       PVE::LXC::Config->foreach_mountpoint($conf->{snapshots}->{$snapname}, $test_mp, $snapname);
+       PVE::LXC::Config->foreach_volume($conf->{snapshots}->{$snapname}, $test_mp, $snapname);
     }
 
-    # finally all currently used volumes
-    PVE::LXC::Config->foreach_mountpoint($conf, $test_mp);
-
+    # finally all current volumes
+    PVE::LXC::Config->foreach_volume_full($conf, { include_unused => 1 }, $test_mp);
 
     # additional checks for local storage
     foreach my $volid (keys %$volhash) {
@@ -230,8 +240,9 @@ sub phase1 {
            my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
            my $scfg =  PVE::Storage::storage_config($self->{storecfg}, $sid);
 
-           my $migratable = ($scfg->{type} eq 'dir') || ($scfg->{type} eq 'zfspool') ||
-               ($scfg->{type} eq 'lvmthin') || ($scfg->{type} eq 'lvm');
+           my $migratable = ($scfg->{type} eq 'dir') || ($scfg->{type} eq 'zfspool')
+               || ($scfg->{type} eq 'lvmthin') || ($scfg->{type} eq 'lvm')
+               || ($scfg->{type} eq 'btrfs');
 
            die "storage type '$scfg->{type}' not supported\n"
                if !$migratable;
@@ -278,21 +289,48 @@ sub phase1 {
     }
 
     my $opts = $self->{opts};
-    my $insecure = $opts->{migration_type} eq 'insecure';
     foreach my $volid (keys %$volhash) {
        next if $rep_volumes->{$volid};
        my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
        push @{$self->{volumes}}, $volid;
-       my $with_snapshots = $volhash->{$volid}->{snapshots};
-       my $bwlimit = PVE::Storage::get_bandwidth_limit('migrate', [$sid], $opts->{bwlimit});
+
        # JSONSchema and get_bandwidth_limit use kbps - storage_migrate bps
+       my $bwlimit = $volhash->{$volid}->{bwlimit};
        $bwlimit = $bwlimit * 1024 if defined($bwlimit);
 
-       PVE::Storage::storage_migrate($self->{storecfg}, $volid, $self->{ssh_info}, $sid, undef, undef, undef, $bwlimit, $insecure, $with_snapshots);
-    }
+       my $targetsid = $volhash->{$volid}->{targetsid};
+
+       my $new_volid = eval {
+           my $storage_migrate_opts = {
+               'ratelimit_bps' => $bwlimit,
+               'insecure' => $opts->{migration_type} eq 'insecure',
+               'with_snapshots' => $volhash->{$volid}->{snapshots},
+               'allow_rename' => 1,
+           };
+
+           my $logfunc = sub { $self->log('info', $_[0]); };
+           return PVE::Storage::storage_migrate(
+               $self->{storecfg},
+               $volid,
+               $self->{ssh_info},
+               $targetsid,
+               $storage_migrate_opts,
+               $logfunc,
+           );
+       };
+
+       if (my $err = $@) {
+           die "storage migration for '$volid' to storage '$targetsid' failed - $err\n";
+       }
+
+       $self->{volume_map}->{$volid} = $new_volid;
+       $self->log('info', "volume '$volid' is '$new_volid' on the target\n");
 
-    my $conffile = PVE::LXC::Config->config_file($vmid);
-    my $newconffile = PVE::LXC::Config->config_file($vmid, $self->{node});
+       eval { PVE::Storage::deactivate_volumes($self->{storecfg}, [$volid]); };
+       if (my $err = $@) {
+           $self->log('warn', $err);
+       }
+    }
 
     if ($self->{running}) {
        die "implement me";
@@ -308,15 +346,12 @@ sub phase1 {
     my $vollist = PVE::LXC::Config->get_vm_volumes($conf);
     PVE::Storage::deactivate_volumes($self->{storecfg}, $vollist);
 
-   # transfer replication state before move config
+    # transfer replication state before moving config
     $self->transfer_replication_state() if $rep_volumes;
-
-    # move config
-    die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
-       if !rename($conffile, $newconffile);
-
+    PVE::LXC::Config->update_volume_ids($conf, $self->{volume_map});
+    PVE::LXC::Config->write_config($vmid, $conf);
+    PVE::LXC::Config->move_config_to_node($vmid, $self->{node});
     $self->{conf_migrated} = 1;
-
     $self->switch_replication_job_target() if $rep_volumes;
 }