]> 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 fbcb8c772b81fe7b6b753237627061a2a4c8230a..2ef1cce42adf738050c6cf37043c2bbb54bc01a8 100644 (file)
@@ -9,7 +9,11 @@ use PVE::Tools;
 use PVE::INotify;
 use PVE::Cluster;
 use PVE::Storage;
+use PVE::LXC::Config;
 use PVE::LXC;
+use PVE::ReplicationConfig;
+use PVE::ReplicationState;
+use PVE::Replication;
 
 use base qw(PVE::AbstractMigrate);
 
@@ -23,6 +27,7 @@ sub prepare {
     my ($self, $vmid) = @_;
 
     my $online = $self->{opts}->{online};
+    my $restart= $self->{opts}->{restart};
 
     $self->{storecfg} = PVE::Storage::config();
 
@@ -33,31 +38,37 @@ sub prepare {
 
     my $running = 0;
     if (PVE::LXC::check_running($vmid)) {
-       die "lxc live migration is currently not implemented\n";
-
-       die "can't migrate running container without --online\n" if !$online;
+       die "lxc live migration is currently not implemented\n" if $online;
+       die "running container can only be migrated in restart mode" if !$restart;
        $running = 1;
     }
+    $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
-       if ($mountpoint->{type} ne 'volume' && $force) {
-           return;
+       # skip dev/bind mps when shared
+       if ($type ne 'volume') {
+           if ($mountpoint->{shared}) {
+               return;
+           } else {
+               die "cannot migrate local $type mount point '$ms'\n";
+           }
        }
+
        my ($storage, $volname) = PVE::Storage::parse_volume_id($volid, 1) if $volid;
-       die "can't determine assigned storage for mountpoint '$ms'\n" if !$storage;
+       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
@@ -65,16 +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;
 
-           die "unable to migrate local mountpoint '$volid' while CT is running"
-               if $running;
+           $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
 
@@ -83,6 +96,17 @@ sub prepare {
     eval { $self->cmd_quiet($cmd); };
     die "Can't connect to destination address using public key\n" if $@;
 
+    # in restart mode, we shutdown the container before migrating
+    if ($restart && $running) {
+       my $timeout = $self->{opts}->{timeout} // 180;
+
+       $self->log('info', "shutdown CT $vmid\n");
+
+       PVE::LXC::vm_stop($vmid, 0, $timeout);
+
+       $running = 0;
+    }
+
     return $running;
 }
 
@@ -100,7 +124,16 @@ sub phase1 {
     }
 
     $self->{volumes} = []; # list of already migrated volumes
-    my $volhash = {}; # 1 for local volumes
+    my $volhash = {}; # 'config', 'snapshot' or 'storage' for local volumes
+    my $volhash_errors = {};
+    my $abort = 0;
+
+    my $log_error = sub {
+       my ($msg, $volid) = @_;
+
+       $volhash_errors->{$volid} = $msg if !defined($volhash_errors->{$volid});
+       $abort = 1;
+    };
 
     my $test_volid = sub {
        my ($volid, $snapname) = @_;
@@ -109,27 +142,40 @@ 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;
 
-       return if $scfg->{shared};
+       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} = 1 if defined($snapname);
+       $volhash->{$volid}->{targetsid} = $targetsid;
+       $volhash->{$volid}->{bwlimit} = $bwlimit;
 
        my ($path, $owner) = PVE::Storage::path($self->{storecfg}, $volid);
 
-       die "can't migrate volume '$volid' - owned by other guest (owner = $owner)\n"
+       die "owned by other guest (owner = $owner)\n"
            if !$owner || ($owner != $self->{vmid});
 
        if (defined($snapname)) {
            # we cannot migrate shapshots on local storage
-           # exceptions: 'zfspool'
-           if (($scfg->{type} eq 'zfspool')) {
-               $volhash->{$volid} = 1;
+           # exceptions: 'zfspool', 'btrfs'
+           if ($scfg->{type} eq 'zfspool' || $scfg->{type} eq 'btrfs') {
                return;
            }
-           die "can't migrate snapshot of local volume '$volid'\n";
-       } else {
-           $volhash->{$volid} = 1;
+           die "non-migratable snapshot exists\n";
        }
     };
 
@@ -139,85 +185,152 @@ sub phase1 {
        my $volid = $mountpoint->{volume};
        # already checked in prepare
        if ($mountpoint->{type} ne 'volume') {
-           $self->log('info', "ignoring mountpoint '$ms' ('$volid') of type " .
-               "'$mountpoint->{type}', migration is forced.")
+           $self->log('info', "ignoring shared '$mountpoint->{type}' mount point '$ms' ('$volid')")
                if !$snapname;
            return;
        }
 
-       my ($storage, $volname) = PVE::Storage::parse_volume_id($volid);
-       my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $storage);
+       eval {
+           &$test_volid($volid, $snapname);
+       };
 
-       if (!$scfg->{shared}) {
-           $self->log('info', "copy mountpoint '$ms' ($volid) to node ' $self->{node}'")
-               if !$snapname;
-           $volhash->{$volid} = 1;
-       } else {
-           $self->log('info', "mountpoint '$ms' is on shared storage '$storage'")
-               if !$snapname;
-       }
-       &$test_volid($volid, $snapname);
+       &$log_error($@, $volid) if $@;
     };
 
-    # first all currently used volumes
-    PVE::LXC::Config->foreach_mountpoint($conf, $test_mp);
-
-    # then all volumes referenced in snapshots
-    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);
-    }
-
-    # finally unused / lost volumes owned by this container
+    # first unused / lost volumes owned by this container
     my @sids = PVE::Storage::storage_ids($self->{storecfg});
     foreach my $storeid (@sids) {
        my $scfg = PVE::Storage::storage_config($self->{storecfg}, $storeid);
        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) = @_;
 
-           $self->log('info', "copy volume '$volid' to node '$self->{node}'")
-               if !$volhash->{$volid};
-           $volhash->{$volid} = 1;
+           $volhash->{$volid}->{ref} = 'storage';
+           $volhash->{$volid}->{targetsid} = $targetsid;
        });
     }
 
+    # then all volumes referenced in snapshots
+    foreach my $snapname (keys %{$conf->{snapshots}}) {
+       &$test_volid($conf->{snapshots}->{$snapname}->{'vmstate'}, 0, undef)
+           if defined($conf->{snapshots}->{$snapname}->{'vmstate'});
+       PVE::LXC::Config->foreach_volume($conf->{snapshots}->{$snapname}, $test_mp, $snapname);
+    }
+
+    # 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) {
-       my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
-       my $scfg =  PVE::Storage::storage_config($self->{storecfg}, $sid);
+       eval {
+           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 "can't migrate '$volid' - storage type '$scfg->{type}' not supported\n"
-           if !$migratable;
+           die "storage type '$scfg->{type}' not supported\n"
+               if !$migratable;
 
-       # image is a linked clone on local storage, se we can't migrate.
-       if (my $basename = (PVE::Storage::parse_volname($self->{storecfg}, $volid))[3]) {
-           die "can't migrate '$volid' as it's a clone of '$basename'";
+           # image is a linked clone on local storage, se we can't migrate.
+           if (my $basename = (PVE::Storage::parse_volname($self->{storecfg}, $volid))[3]) {
+               die "clone of '$basename'";
+           }
+       };
+       &$log_error($@, $volid) if $@;
+    }
+
+    foreach my $volid (sort keys %$volhash) {
+       my $ref = $volhash->{$volid}->{ref};
+       if ($ref eq 'storage') {
+           $self->log('info', "found local volume '$volid' (via storage)\n");
+       } elsif ($ref eq 'config') {
+           $self->log('info', "found local volume '$volid' (in current VM config)\n");
+       } elsif ($ref eq 'snapshot') {
+           $self->log('info', "found local volume '$volid' (referenced by snapshot(s))\n");
+       } else {
+           $self->log('info', "found local volume '$volid'\n");
        }
     }
 
+    foreach my $volid (sort keys %$volhash_errors) {
+       $self->log('warn', "can't migrate local volume '$volid': $volhash_errors->{$volid}");
+    }
+
+    if ($abort) {
+       die "can't migrate CT - check log\n";
+    }
+
+    my $rep_volumes;
+
+    my $rep_cfg = PVE::ReplicationConfig->new();
+
+    if (my $jobcfg = $rep_cfg->find_local_replication_job($vmid, $self->{node})) {
+       die "can't live migrate VM with replicated volumes\n" if $self->{running};
+       my $start_time = time();
+       my $logfunc = sub { my ($msg) = @_;  $self->log('info', $msg); };
+       $rep_volumes = PVE::Replication::run_replication(
+           'PVE::LXC::Config', $jobcfg, $start_time, $start_time, $logfunc);
+    }
+
+    my $opts = $self->{opts};
     foreach my $volid (keys %$volhash) {
+       next if $rep_volumes->{$volid};
        my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
        push @{$self->{volumes}}, $volid;
-       PVE::Storage::storage_migrate($self->{storecfg}, $volid, $self->{nodeip}, $sid);
-    }
 
-    my $conffile = PVE::LXC::Config->config_file($vmid);
-    my $newconffile = PVE::LXC::Config->config_file($vmid, $self->{node});
+       # JSONSchema and get_bandwidth_limit use kbps - storage_migrate bps
+       my $bwlimit = $volhash->{$volid}->{bwlimit};
+       $bwlimit = $bwlimit * 1024 if defined($bwlimit);
+
+       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");
+
+       eval { PVE::Storage::deactivate_volumes($self->{storecfg}, [$volid]); };
+       if (my $err = $@) {
+           $self->log('warn', $err);
+       }
+    }
 
     if ($self->{running}) {
        die "implement me";
@@ -233,11 +346,13 @@ sub phase1 {
     my $vollist = PVE::LXC::Config->get_vm_volumes($conf);
     PVE::Storage::deactivate_volumes($self->{storecfg}, $vollist);
 
-    # move config
-    die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
-       if !rename($conffile, $newconffile);
-
+    # transfer replication state before moving config
+    $self->transfer_replication_state() if $rep_volumes;
+    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;
 }
 
 sub phase1_cleanup {
@@ -275,17 +390,30 @@ sub final_cleanup {
     $self->log('info', "start final cleanup");
 
     if (!$self->{conf_migrated}) {
-       my $conf = $self->{vmconf};
-       delete $conf->{lock};
-
-       eval { PVE::LXC::Config->write_config($vmid, $conf); };
+       eval { PVE::LXC::Config->remove_lock($vmid, 'migrate'); };
        if (my $err = $@) {
            $self->log('err', $err);
        }
+       # in restart mode, we start the container on the source node
+       # on migration error
+       if ($self->{opts}->{restart} && $self->{was_running}) {
+           $self->log('info', "start container on source node");
+           my $skiplock = 1;
+           PVE::LXC::vm_start($vmid, $self->{vmconf}, $skiplock);
+       }
     } else {
        my $cmd = [ @{$self->{rem_ssh}}, 'pct', 'unlock', $vmid ];
-       $self->cmd_logerr($cmd, errmsg => "failed to clear migrate lock");      
+       $self->cmd_logerr($cmd, errmsg => "failed to clear migrate lock");
+
+       # in restart mode, we start the container on the target node
+       # after migration
+       if ($self->{opts}->{restart} && $self->{was_running}) {
+           $self->log('info', "start container on target node");
+           my $cmd = [ @{$self->{rem_ssh}}, 'pct', 'start', $vmid];
+           $self->cmd($cmd);
+       }
     }
+
 }
 
 1;