From: Fabian Grünbichler Date: Wed, 25 Jan 2017 08:10:05 +0000 (+0100) Subject: remove AbstractConfig and AbstractMigrate X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=commitdiff_plain;h=c480d509e9398cacc6dba94d461d1d5e39f7f5d3 remove AbstractConfig and AbstractMigrate they have their own repo / package now (pve-guest-common) --- diff --git a/src/Makefile b/src/Makefile index 7b66029..4051351 100644 --- a/src/Makefile +++ b/src/Makefile @@ -21,9 +21,7 @@ LIB_SOURCES= \ AtomicFile.pm \ INotify.pm \ Tools.pm \ - AbstractMigrate.pm \ - Exception.pm \ - AbstractConfig.pm + Exception.pm all: diff --git a/src/PVE/AbstractConfig.pm b/src/PVE/AbstractConfig.pm deleted file mode 100644 index 7395768..0000000 --- a/src/PVE/AbstractConfig.pm +++ /dev/null @@ -1,658 +0,0 @@ -package PVE::AbstractConfig; - -use strict; -use warnings; - -use PVE::Tools qw(lock_file lock_file_full); -use PVE::INotify; - -my $nodename = PVE::INotify::nodename(); - -# Printable string, currently either "VM" or "CT" -sub guest_type { - my ($class) = @_; - die "abstract method - implement me "; -} - -sub __config_max_unused_disks { - my ($class) = @_; - - die "implement me - abstract method\n"; -} - -# Path to the flock file for this VM/CT -sub config_file_lock { - my ($class, $vmid) = @_; - die "abstract method - implement me"; -} - -# Relative config file path for this VM/CT in CFS -sub cfs_config_path { - my ($class, $vmid, $node) = @_; - die "abstract method - implement me"; -} - -# Absolute config file path for this VM/CT -sub config_file { - my ($class, $vmid, $node) = @_; - - my $cfspath = $class->cfs_config_path($vmid, $node); - return "/etc/pve/$cfspath"; -} - -# Read and parse config file for this VM/CT -sub load_config { - my ($class, $vmid, $node) = @_; - - $node = $nodename if !$node; - my $cfspath = $class->cfs_config_path($vmid, $node); - - my $conf = PVE::Cluster::cfs_read_file($cfspath); - die "Configuration file '$cfspath' does not exist\n" - if !defined($conf); - - return $conf; -} - -# Generate and write config file for this VM/CT -sub write_config { - my ($class, $vmid, $conf) = @_; - - my $cfspath = $class->cfs_config_path($vmid); - - PVE::Cluster::cfs_write_file($cfspath, $conf); -} - -# Lock config file using flock, run $code with @param, unlock config file. -# $timeout is the maximum time to aquire the flock -sub lock_config_full { - my ($class, $vmid, $timeout, $code, @param) = @_; - - my $filename = $class->config_file_lock($vmid); - - my $res = lock_file($filename, $timeout, $code, @param); - - die $@ if $@; - - return $res; -} - -# Lock config file using flock, run $code with @param, unlock config file. -# $timeout is the maximum time to aquire the flock -# $shared eq 1 creates a non-exclusive ("read") flock -sub lock_config_mode { - my ($class, $vmid, $timeout, $shared, $code, @param) = @_; - - my $filename = $class->config_file_lock($vmid); - - my $res = lock_file_full($filename, $timeout, $shared, $code, @param); - - die $@ if $@; - - return $res; -} - -# Lock config file using flock, run $code with @param, unlock config file. -sub lock_config { - my ($class, $vmid, $code, @param) = @_; - - return $class->lock_config_full($vmid, 10, $code, @param); -} - -# Checks whether the config is locked with the lock parameter -sub check_lock { - my ($class, $conf) = @_; - - die $class->guest_type()." is locked ($conf->{lock})\n" if $conf->{lock}; -} - -# Returns whether the config is locked with the lock parameter, also checks -# whether the lock value is correct if the optional $lock is set. -sub has_lock { - my ($class, $conf, $lock) = @_; - - return $conf->{lock} && (!defined($lock) || $lock eq $conf->{lock}); -} - -# Sets the lock parameter for this VM/CT's config to $lock. -sub set_lock { - my ($class, $vmid, $lock) = @_; - - my $conf; - $class->lock_config($vmid, sub { - $conf = $class->load_config($vmid); - $class->check_lock($conf); - $conf->{lock} = $lock; - $class->write_config($vmid, $conf); - }); - return $conf; -} - -# Removes the lock parameter for this VM/CT's config, also checks whether -# the lock value is correct if the optional $lock is set. -sub remove_lock { - my ($class, $vmid, $lock) = @_; - - $class->lock_config($vmid, sub { - my $conf = $class->load_config($vmid); - if (!$conf->{lock}) { - my $lockstring = defined($lock) ? "'$lock' " : "any"; - die "no lock found trying to remove $lockstring lock\n"; - } elsif (defined($lock) && $conf->{lock} ne $lock) { - die "found lock '$conf->{lock}' trying to remove '$lock' lock\n"; - } - delete $conf->{lock}; - $class->write_config($vmid, $conf); - }); -} - -# Checks whether protection mode is enabled for this VM/CT. -sub check_protection { - my ($class, $conf, $err_msg) = @_; - - if ($conf->{protection}) { - die "$err_msg - protection mode enabled\n"; - } -} - -# Adds an unused volume to $config, if possible. -sub add_unused_volume { - my ($class, $config, $volid) = @_; - - my $key; - for (my $ind = $class->__config_max_unused_disks() - 1; $ind >= 0; $ind--) { - my $test = "unused$ind"; - if (my $vid = $config->{$test}) { - return if $vid eq $volid; # do not add duplicates - } else { - $key = $test; - } - } - - die "Too many unused volumes - please delete them first.\n" if !$key; - - $config->{$key} = $volid; - - return $key; -} - -# Returns whether the template parameter is set in $conf. -sub is_template { - my ($class, $conf) = @_; - - return 1 if defined $conf->{template} && $conf->{template} == 1; -} - -# Checks whether $feature is availabe for the referenced volumes in $conf. -# Note: depending on the parameters, some volumes may be skipped! -sub has_feature { - my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_; - die "implement me - abstract method\n"; -} - -# Internal snapshots - -# NOTE: Snapshot create/delete involves several non-atomic -# actions, and can take a long time. -# So we try to avoid locking the file and use the 'lock' variable -# inside the config file instead. - -# Save the vmstate (RAM). -sub __snapshot_save_vmstate { - my ($class, $vmid, $conf, $snapname, $storecfg) = @_; - die "implement me - abstract method\n"; -} - -# Check whether the VM/CT is running. -sub __snapshot_check_running { - my ($class, $vmid) = @_; - die "implement me - abstract method\n"; -} - -# Check whether we need to freeze the VM/CT -sub __snapshot_check_freeze_needed { - my ($sself, $vmid, $config, $save_vmstate) = @_; - die "implement me - abstract method\n"; -} - -# Freeze or unfreeze the VM/CT. -sub __snapshot_freeze { - my ($class, $vmid, $unfreeze) = @_; - - die "abstract method - implement me\n"; -} - -# Code run before and after creating all the volume snapshots -# base: noop -sub __snapshot_create_vol_snapshots_hook { - my ($class, $vmid, $snap, $running, $hook) = @_; - - return; -} - -# Create the volume snapshots for the VM/CT. -sub __snapshot_create_vol_snapshot { - my ($class, $vmid, $vs, $volume, $snapname) = @_; - - die "abstract method - implement me\n"; -} - -# Remove a drive from the snapshot config. -sub __snapshot_delete_remove_drive { - my ($class, $snap, $drive) = @_; - - die "abstract method - implement me\n"; -} - -# Delete the vmstate file/drive -sub __snapshot_delete_vmstate_file { - my ($class, $snap, $force) = @_; - - die "abstract method - implement me\n"; -} - -# Delete a volume snapshot -sub __snapshot_delete_vol_snapshot { - my ($class, $vmid, $vs, $volume, $snapname) = @_; - - die "abstract method - implement me\n"; -} - -# Checks whether a volume snapshot is possible for this volume. -sub __snapshot_rollback_vol_possible { - my ($class, $volume, $snapname) = @_; - - die "abstract method - implement me\n"; -} - -# Rolls back this volume. -sub __snapshot_rollback_vol_rollback { - my ($class, $volume, $snapname) = @_; - - die "abstract method - implement me\n"; -} - -# Stops the VM/CT for a rollback. -sub __snapshot_rollback_vm_stop { - my ($class, $vmid) = @_; - - die "abstract method - implement me\n"; -} - -# Start the VM/CT after a rollback with restored vmstate. -sub __snapshot_rollback_vm_start { - my ($class, $vmid, $vmstate, $forcemachine); - - die "abstract method - implement me\n"; -} - -# Get list of volume IDs which are referenced in $conf, but not in $snap. -sub __snapshot_rollback_get_unused { - my ($class, $conf, $snap) = @_; - - die "abstract method - implement me\n"; -} - -# Iterate over all configured volumes, calling $func for each key/value pair. -sub __snapshot_foreach_volume { - my ($class, $conf, $func) = @_; - - die "abstract method - implement me\n"; -} - -# Copy the current config $source to the snapshot config $dest -sub __snapshot_copy_config { - my ($class, $source, $dest) = @_; - - foreach my $k (keys %$source) { - next if $k eq 'snapshots'; - next if $k eq 'snapstate'; - next if $k eq 'snaptime'; - next if $k eq 'vmstate'; - next if $k eq 'lock'; - next if $k eq 'digest'; - next if $k eq 'description'; - next if $k =~ m/^unused\d+$/; - - $dest->{$k} = $source->{$k}; - } -}; - -# Apply the snapshot config $snap to the config $conf (rollback) -sub __snapshot_apply_config { - my ($class, $conf, $snap) = @_; - - # copy snapshot list - my $newconf = { - snapshots => $conf->{snapshots}, - }; - - # keep description and list of unused disks - foreach my $k (keys %$conf) { - next if !($k =~ m/^unused\d+$/ || $k eq 'description'); - $newconf->{$k} = $conf->{$k}; - } - - $class->__snapshot_copy_config($snap, $newconf); - - return $newconf; -} - -# Prepares the configuration for snapshotting. -sub __snapshot_prepare { - my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_; - - my $snap; - - my $updatefn = sub { - - my $conf = $class->load_config($vmid); - - die "you can't take a snapshot if it's a template\n" - if $class->is_template($conf); - - $class->check_lock($conf); - - $conf->{lock} = 'snapshot'; - - die "snapshot name '$snapname' already used\n" - if defined($conf->{snapshots}->{$snapname}); - - my $storecfg = PVE::Storage::config(); - die "snapshot feature is not available\n" - if !$class->has_feature('snapshot', $conf, $storecfg, undef, undef, $snapname eq 'vzdump'); - - $snap = $conf->{snapshots}->{$snapname} = {}; - - if ($save_vmstate && $class->__snapshot_check_running($vmid)) { - $class->__snapshot_save_vmstate($vmid, $conf, $snapname, $storecfg); - } - - $class->__snapshot_copy_config($conf, $snap); - - $snap->{snapstate} = "prepare"; - $snap->{snaptime} = time(); - $snap->{description} = $comment if $comment; - - $class->write_config($vmid, $conf); - }; - - $class->lock_config($vmid, $updatefn); - - return $snap; -} - -# Commits the configuration after snapshotting. -sub __snapshot_commit { - my ($class, $vmid, $snapname) = @_; - - my $updatefn = sub { - - my $conf = $class->load_config($vmid); - - die "missing snapshot lock\n" - if !($conf->{lock} && $conf->{lock} eq 'snapshot'); - - my $snap = $conf->{snapshots}->{$snapname}; - die "snapshot '$snapname' does not exist\n" if !defined($snap); - - die "wrong snapshot state\n" - if !($snap->{snapstate} && $snap->{snapstate} eq "prepare"); - - delete $snap->{snapstate}; - delete $conf->{lock}; - - $conf->{parent} = $snapname; - - $class->write_config($vmid, $conf); - }; - - $class->lock_config($vmid, $updatefn); -} - -# Creates a snapshot for the VM/CT. -sub snapshot_create { - my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_; - - my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment); - - $save_vmstate = 0 if !$snap->{vmstate}; - - my $conf = $class->load_config($vmid); - - my ($running, $freezefs) = $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate}); - - my $drivehash = {}; - - eval { - if ($freezefs) { - $class->__snapshot_freeze($vmid, 0); - } - - $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before"); - - $class->__snapshot_foreach_volume($snap, sub { - my ($vs, $volume) = @_; - - $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname); - $drivehash->{$vs} = 1; - }); - }; - my $err = $@; - - if ($running) { - $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after"); - if ($freezefs) { - $class->__snapshot_freeze($vmid, 1); - } - $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after-unfreeze"); - } - - if ($err) { - warn "snapshot create failed: starting cleanup\n"; - eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); }; - warn "$@" if $@; - die "$err\n"; - } - - $class->__snapshot_commit($vmid, $snapname); -} - -# Deletes a snapshot. -# Note: $drivehash is only set when called from snapshot_create. -sub snapshot_delete { - my ($class, $vmid, $snapname, $force, $drivehash) = @_; - - my $prepare = 1; - - my $snap; - my $unused = []; - - $class->set_lock($vmid, 'snapshot-delete') - if (!$drivehash); # doesn't already have a 'snapshot' lock - - my $unlink_parent = sub { - my ($confref, $new_parent) = @_; - - if ($confref->{parent} && $confref->{parent} eq $snapname) { - if ($new_parent) { - $confref->{parent} = $new_parent; - } else { - delete $confref->{parent}; - } - } - }; - - my $remove_drive = sub { - my ($drive) = @_; - - my $conf = $class->load_config($vmid); - $snap = $conf->{snapshots}->{$snapname}; - die "snapshot '$snapname' does not exist\n" if !defined($snap); - - $class->__snapshot_delete_remove_drive($snap, $drive); - - $class->write_config($vmid, $conf); - }; - - #prepare - $class->lock_config($vmid, sub { - my $conf = $class->load_config($vmid); - - die "you can't delete a snapshot if vm is a template\n" - if $class->is_template($conf); - - $snap = $conf->{snapshots}->{$snapname}; - die "snapshot '$snapname' does not exist\n" if !defined($snap); - - $snap->{snapstate} = 'delete'; - - $class->write_config($vmid, $conf); - }); - - # now remove vmstate file - if ($snap->{vmstate}) { - $class->__snapshot_delete_vmstate_file($snap, $force); - - # save changes (remove vmstate from snapshot) - $class->lock_config($vmid, $remove_drive, 'vmstate') if !$force; - }; - - # now remove all volume snapshots - $class->__snapshot_foreach_volume($snap, sub { - my ($vs, $volume) = @_; - - return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup}; - if (!$drivehash || $drivehash->{$vs}) { - eval { $class->__snapshot_delete_vol_snapshot($vmid, $vs, $volume, $snapname, $unused); }; - if (my $err = $@) { - die $err if !$force; - warn $err; - } - } - - # save changes (remove drive from snapshot) - $class->lock_config($vmid, $remove_drive, $vs) if !$force; - }); - - # now cleanup config - $class->lock_config($vmid, sub { - my $conf = $class->load_config($vmid); - $snap = $conf->{snapshots}->{$snapname}; - die "snapshot '$snapname' does not exist\n" if !defined($snap); - - # remove parent refs - &$unlink_parent($conf, $snap->{parent}); - foreach my $sn (keys %{$conf->{snapshots}}) { - next if $sn eq $snapname; - &$unlink_parent($conf->{snapshots}->{$sn}, $snap->{parent}); - } - - - delete $conf->{snapshots}->{$snapname}; - delete $conf->{lock}; - foreach my $volid (@$unused) { - $class->add_unused_volume($conf, $volid); - } - - $class->write_config($vmid, $conf); - }); -} - -# Rolls back to a given snapshot. -sub snapshot_rollback { - my ($class, $vmid, $snapname) = @_; - - my $prepare = 1; - - my $storecfg = PVE::Storage::config(); - - my $conf = $class->load_config($vmid); - - my $get_snapshot_config = sub { - - die "you can't rollback if vm is a template\n" if $class->is_template($conf); - - my $res = $conf->{snapshots}->{$snapname}; - - die "snapshot '$snapname' does not exist\n" if !defined($res); - - return $res; - }; - - my $snap = &$get_snapshot_config(); - - $class->__snapshot_foreach_volume($snap, sub { - my ($vs, $volume) = @_; - - $class->__snapshot_rollback_vol_possible($volume, $snapname); - }); - - my $updatefn = sub { - - $conf = $class->load_config($vmid); - - $snap = &$get_snapshot_config(); - - die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n" - if $snap->{snapstate}; - - if ($prepare) { - $class->check_lock($conf); - $class->__snapshot_rollback_vm_stop($vmid); - } - - die "unable to rollback vm $vmid: vm is running\n" - if $class->__snapshot_check_running($vmid); - - if ($prepare) { - $conf->{lock} = 'rollback'; - } else { - die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback'); - delete $conf->{lock}; - } - - # machine only relevant for Qemu - my $forcemachine; - - if (!$prepare) { - my $unused = $class->__snapshot_rollback_get_unused($conf, $snap); - - foreach my $volid (@$unused) { - $class->add_unused_volume($conf, $volid); - } - - my $has_machine_config = defined($conf->{machine}); - - # copy snapshot config to current config - $conf = $class->__snapshot_apply_config($conf, $snap); - $conf->{parent} = $snapname; - - # Note: old code did not store 'machine', so we try to be smart - # and guess the snapshot was generated with kvm 1.4 (pc-i440fx-1.4). - $forcemachine = $conf->{machine} || 'pc-i440fx-1.4'; - # we remove the 'machine' configuration if not explicitly specified - # in the original config. - delete $conf->{machine} if $snap->{vmstate} && !$has_machine_config; - } - - $class->write_config($vmid, $conf); - - if (!$prepare && $snap->{vmstate}) { - $class->__snapshot_rollback_vm_start($vmid, $snap->{vmstate}, $forcemachine); - } - }; - - $class->lock_config($vmid, $updatefn); - - $class->__snapshot_foreach_volume($snap, sub { - my ($vs, $volume) = @_; - - $class->__snapshot_rollback_vol_rollback($volume, $snapname); - }); - - $prepare = 0; - $class->lock_config($vmid, $updatefn); -} - -1; diff --git a/src/PVE/AbstractMigrate.pm b/src/PVE/AbstractMigrate.pm deleted file mode 100644 index 140b479..0000000 --- a/src/PVE/AbstractMigrate.pm +++ /dev/null @@ -1,318 +0,0 @@ -package PVE::AbstractMigrate; - -use strict; -use warnings; -use POSIX qw(strftime); -use PVE::Tools; - -my $msg2text = sub { - my ($level, $msg) = @_; - - chomp $msg; - - return '' if !$msg; - - my $res = ''; - - my $tstr = strftime("%b %d %H:%M:%S", localtime); - - foreach my $line (split (/\n/, $msg)) { - if ($level eq 'err') { - $res .= "$tstr ERROR: $line\n"; - } else { - $res .= "$tstr $line\n"; - } - } - - return $res; -}; - -sub log { - my ($self, $level, $msg) = @_; - - chomp $msg; - - return if !$msg; - - print &$msg2text($level, $msg); -} - -sub cmd { - my ($self, $cmd, %param) = @_; - - my $logfunc = sub { - my $line = shift; - $self->log('info', $line); - }; - - $self->log('info', "# " . PVE::Tools::cmd2string($cmd)); - - PVE::Tools::run_command($cmd, %param, outfunc => $logfunc, errfunc => $logfunc); -} - -my $run_command_quiet_full = sub { - my ($self, $cmd, $logerr, %param) = @_; - - my $log = ''; - my $logfunc = sub { - my $line = shift; - $log .= &$msg2text('info', $line);; - }; - - eval { PVE::Tools::run_command($cmd, %param, outfunc => $logfunc, errfunc => $logfunc); }; - if (my $err = $@) { - $self->log('info', "# " . PVE::Tools::cmd2string($cmd)); - print $log; - if ($logerr) { - $self->{errors} = 1; - $self->log('err', $err); - } else { - die $err; - } - } -}; - -sub cmd_quiet { - my ($self, $cmd, %param) = @_; - return &$run_command_quiet_full($self, $cmd, 0, %param); -} - -sub cmd_logerr { - my ($self, $cmd, %param) = @_; - return &$run_command_quiet_full($self, $cmd, 1, %param); -} - -sub get_remote_migration_ip { - my ($self) = @_; - - my $ip; - - my $cmd = [@{$self->{rem_ssh}}, 'pvecm', 'mtunnel', '--get_migration_ip']; - - push @$cmd, '--migration_network', $self->{opts}->{migration_network} - if defined($self->{opts}->{migration_network}); - - PVE::Tools::run_command($cmd, outfunc => sub { - my $line = shift; - - if ($line =~ m/^ip: '($PVE::Tools::IPRE)'$/) { - $ip = $1; - } - }); - - return $ip; -} - -my $eval_int = sub { - my ($self, $func, @param) = @_; - - eval { - local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub { - $self->{delayed_interrupt} = 0; - die "interrupted by signal\n"; - }; - local $SIG{PIPE} = sub { - $self->{delayed_interrupt} = 0; - die "interrupted by signal\n"; - }; - - my $di = $self->{delayed_interrupt}; - $self->{delayed_interrupt} = 0; - - die "interrupted by signal\n" if $di; - - &$func($self, @param); - }; -}; - -my @ssh_opts = ('-o', 'BatchMode=yes'); -my @ssh_cmd = ('/usr/bin/ssh', @ssh_opts); -my @scp_cmd = ('/usr/bin/scp', @ssh_opts); -my @rsync_opts = ('-aHAX', '--delete', '--numeric-ids'); -my @rsync_cmd = ('/usr/bin/rsync', @rsync_opts); - -sub migrate { - my ($class, $node, $nodeip, $vmid, $opts) = @_; - - $class = ref($class) || $class; - - my $self = { - delayed_interrupt => 0, - opts => $opts, - vmid => $vmid, - node => $node, - nodeip => $nodeip, - rsync_cmd => [ @rsync_cmd ], - rem_ssh => [ @ssh_cmd, "root\@$nodeip" ], - scp_cmd => [ @scp_cmd ], - }; - - $self = bless $self, $class; - - my $starttime = time(); - - local $ENV{RSYNC_RSH} = join(' ', @ssh_cmd); - - local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub { - $self->log('err', "received interrupt - delayed"); - $self->{delayed_interrupt} = 1; - }; - - local $ENV{RSYNC_RSH} = join(' ', @ssh_cmd); - - # lock container during migration - eval { $self->lock_vm($self->{vmid}, sub { - - $self->{running} = 0; - &$eval_int($self, sub { $self->{running} = $self->prepare($self->{vmid}); }); - die $@ if $@; - - # get dedicated migration address from remote node, if set. - # as a side effect this checks also if the other node can be accessed - # through ssh and that it has quorum - my $remote_migration_ip = $self->get_remote_migration_ip(); - - if (defined($remote_migration_ip)) { - $nodeip = $remote_migration_ip; - $self->{nodeip} = $remote_migration_ip; - $self->{rem_ssh} = [ @ssh_cmd, "root\@$nodeip" ]; - - $self->log('info', "use dedicated network address for sending " . - "migration traffic ($self->{nodeip})"); - - # test if we can connect to new IP - my $cmd = [ @{$self->{rem_ssh}}, '/bin/true' ]; - eval { $self->cmd_quiet($cmd); }; - die "Can't connect to destination address ($self->{nodeip}) using " . - "public key authentication\n" if $@; - } - - &$eval_int($self, sub { $self->phase1($self->{vmid}); }); - my $err = $@; - if ($err) { - $self->log('err', $err); - eval { $self->phase1_cleanup($self->{vmid}, $err); }; - if (my $tmperr = $@) { - $self->log('err', $tmperr); - } - eval { $self->final_cleanup($self->{vmid}); }; - if (my $tmperr = $@) { - $self->log('err', $tmperr); - } - die $err; - } - - # vm is now owned by other node - # Note: there is no VM config file on the local node anymore - - if ($self->{running}) { - - &$eval_int($self, sub { $self->phase2($self->{vmid}); }); - my $phase2err = $@; - if ($phase2err) { - $self->{errors} = 1; - $self->log('err', "online migrate failure - $phase2err"); - } - eval { $self->phase2_cleanup($self->{vmid}, $phase2err); }; - if (my $err = $@) { - $self->log('err', $err); - $self->{errors} = 1; - } - } - - # phase3 (finalize) - &$eval_int($self, sub { $self->phase3($self->{vmid}); }); - my $phase3err = $@; - if ($phase3err) { - $self->log('err', $phase3err); - $self->{errors} = 1; - } - eval { $self->phase3_cleanup($self->{vmid}, $phase3err); }; - if (my $err = $@) { - $self->log('err', $err); - $self->{errors} = 1; - } - eval { $self->final_cleanup($self->{vmid}); }; - if (my $err = $@) { - $self->log('err', $err); - $self->{errors} = 1; - } - })}; - - my $err = $@; - - my $delay = time() - $starttime; - my $mins = int($delay/60); - my $secs = $delay - $mins*60; - my $hours = int($mins/60); - $mins = $mins - $hours*60; - - my $duration = sprintf "%02d:%02d:%02d", $hours, $mins, $secs; - - if ($err) { - $self->log('err', "migration aborted (duration $duration): $err"); - die "migration aborted\n"; - } - - if ($self->{errors}) { - $self->log('err', "migration finished with problems (duration $duration)"); - die "migration problems\n" - } - - $self->log('info', "migration finished successfully (duration $duration)"); -} - -sub lock_vm { - my ($self, $vmid, $code, @param) = @_; - - die "abstract method - implement me"; -} - -sub prepare { - my ($self, $vmid) = @_; - - die "abstract method - implement me"; - - # return $running; -} - -# transfer all data and move VM config files -sub phase1 { - my ($self, $vmid) = @_; - die "abstract method - implement me"; -} - -# only called if there are errors in phase1 -sub phase1_cleanup { - my ($self, $vmid, $err) = @_; - die "abstract method - implement me"; -} - -# only called when VM is running and phase1 was successful -sub phase2 { - my ($self, $vmid) = @_; - die "abstract method - implement me"; -} - -# only called when VM is running and phase1 was successful -sub phase2_cleanup { - my ($self, $vmid, $err) = @_; -}; - -# only called when phase1 was successful -sub phase3 { - my ($self, $vmid) = @_; -} - -# only called when phase1 was successful -sub phase3_cleanup { - my ($self, $vmid, $err) = @_; -} - -# final cleanup - always called -sub final_cleanup { - my ($self, $vmid) = @_; - die "abstract method - implement me"; -} - -1;