OpenVZ.pm 7.53 KB
Newer Older
1 2 3 4 5 6
package PVE::VZDump::OpenVZ;

use strict;
use warnings;
use File::Path;
use File::Basename;
Dietmar Maurer's avatar
Dietmar Maurer committed
7
use PVE::INotify;
8
use PVE::VZDump;
Dietmar Maurer's avatar
Dietmar Maurer committed
9
use PVE::OpenVZ;
10 11 12 13 14 15

use base qw (PVE::VZDump::Plugin);

my $load_vz_conf = sub {
    my ($self, $vmid) = @_;

Dietmar Maurer's avatar
Dietmar Maurer committed
16
    my $conf = PVE::OpenVZ::load_config($vmid);
17

Dietmar Maurer's avatar
Dietmar Maurer committed
18 19 20
    my $dir = $self->{privatedir};
    if ($conf->{ve_private} && $conf->{ve_private}->{value}) {
	$dir = $conf->{ve_private}->{value};
21 22 23 24
    }
    $dir =~ s/\$VEID/$vmid/;
    $self->{vmlist}->{$vmid}->{dir} = $dir;

Dietmar Maurer's avatar
Dietmar Maurer committed
25 26 27
    my $hostname = "CT $vmid";
    if ($conf->{hostname} && $conf->{hostname}->{value}) {
	$hostname = $conf->{hostname}->{value};
28
    }
Dietmar Maurer's avatar
Dietmar Maurer committed
29
    $self->{vmlist}->{$vmid}->{hostname} = $hostname;
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
};

my $rsync_vm = sub {
    my ($self, $task, $from, $to, $text) = @_;

    $self->loginfo ("starting $text sync $from to $to");

    my $starttime = time();

    my $opts = $self->{vzdump}->{opts};

    my $rsyncopts = "--stats -x --numeric-ids";

    $rsyncopts .= " --bwlimit=$opts->{bwlimit}" if $opts->{bwlimit};

    $self->cmd ("rsync $rsyncopts -aH --delete --no-whole-file --inplace '$from' '$to'");

    my $delay = time () - $starttime;

    $self->loginfo ("$text sync finished ($delay seconds)");
};

sub new {
    my ($class, $vzdump) = @_;
    
    PVE::VZDump::check_bin ('vzctl');

Dietmar Maurer's avatar
Dietmar Maurer committed
57
    my $self = bless PVE::OpenVZ::read_global_vz_config ();
58 59 60

    $self->{vzdump} = $vzdump;

Dietmar Maurer's avatar
Dietmar Maurer committed
61
    $self->{vmlist} = PVE::OpenVZ::config_list();
62 63 64 65 66 67 68 69 70 71 72

    return $self;
};

sub type {
    return 'openvz';
}

sub vm_status {
    my ($self, $vmid) = @_;

73 74
    my $status_text = '';
    $self->cmd ("vzctl status $vmid", outfunc => sub {$status_text .= shift; });
75 76 77 78
    chomp $status_text;

    my $running = $status_text =~ m/running/ ? 1 : 0;
   
Dietmar Maurer's avatar
Dietmar Maurer committed
79
    return wantarray ? ($running, $running ? 'running' : 'stopped') : $running; 
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
}

sub prepare {
    my ($self, $task, $vmid, $mode) = @_;

    $self->$load_vz_conf ($vmid);

    my $dir = $self->{vmlist}->{$vmid}->{dir};

    my $diskinfo = { dir => $dir };

    $task->{hostname} = $self->{vmlist}->{$vmid}->{hostname};

    $task->{diskinfo} = $diskinfo;

Dietmar Maurer's avatar
Dietmar Maurer committed
95
    my $hostname = PVE::INotify::nodename(); 
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

    if ($mode eq 'snapshot') {

	my $lvmmap = PVE::VZDump::get_lvm_mapping();
	my ($srcdev, $lvmpath, $lvmvg, $lvmlv, $fstype) =
	    PVE::VZDump::get_lvm_device ($dir, $lvmmap);

	my $targetdev = PVE::VZDump::get_lvm_device ($task->{dumpdir}, $lvmmap);

	die ("mode failure - unable to detect lvm volume group\n") if !$lvmvg;
	die ("mode failure - wrong lvm mount point '$lvmpath'\n") if $dir !~ m|/?$lvmpath/?|;
	die ("mode failure - unable to dump into snapshot (use option --dumpdir)\n") 
	    if $targetdev eq $srcdev;

	$diskinfo->{snapname} = "vzsnap-$hostname-0";
	$diskinfo->{snapdev} = "/dev/$lvmvg/$diskinfo->{snapname}";
	$diskinfo->{srcdev}  = $srcdev;
	$diskinfo->{lvmvg}   = $lvmvg;
	$diskinfo->{lvmlv}   = $lvmlv;
	$diskinfo->{fstype}  = $fstype;
	$diskinfo->{lvmpath} = $lvmpath;
	$diskinfo->{mountpoint} = "/mnt/vzsnap0";
	    
	$task->{snapdir} = $dir;
	$task->{snapdir} =~ s|/?$lvmpath/?|$diskinfo->{mountpoint}/|;
    
    } elsif ($mode eq 'suspend') {
	$task->{snapdir} = $task->{tmpdir};
    } else {
	$task->{snapdir} = $dir;
    }
}

sub lock_vm {
    my ($self, $vmid) = @_;

    my $filename = "$self->{lockdir}/103.lck";

Dietmar Maurer's avatar
Dietmar Maurer committed
134
    my $lockmgr = PVE::OpenVZ::create_lock_manager();
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

    $self->{lock} = $lockmgr->lock($filename) || die "can't lock VM $vmid\n";
}

sub unlock_vm {
    my ($self, $vmid) = @_;

    $self->{lock}->release();
}

sub copy_data_phase1 {
    my ($self, $task) = @_;

    $self->$rsync_vm ($task, "$task->{diskinfo}->{dir}/", $task->{snapdir}, "first");
}

# we use --skiplock for vzctl because we have already locked the VM
# by calling lock_vm()

sub stop_vm {
    my ($self, $task, $vmid) = @_;

    $self->cmd ("vzctl --skiplock stop $vmid");
}

sub start_vm {
    my ($self, $task, $vmid) = @_;

    $self->cmd ("vzctl --skiplock start $vmid");
}

sub suspend_vm {
    my ($self, $task, $vmid) = @_;

    $self->cmd ("vzctl --skiplock chkpnt $vmid --suspend");
}

sub snapshot {
    my ($self, $task) = @_;

    my $opts = $self->{vzdump}->{opts};

    my $di = $task->{diskinfo};

    mkpath $di->{mountpoint}; # create mount point for lvm snapshot

    if (-b $di->{snapdev}) {
	$self->loginfo ("trying to remove stale snapshot '$di->{snapdev}'");
	    
	$self->cmd_noerr ("umount $di->{mountpoint}");
	    
	$self->cmd_noerr ("lvremove -f $di->{snapdev}");
    }

    $self->loginfo ("creating lvm snapshot of $di->{srcdev} ('$di->{snapdev}')");

    $task->{cleanup}->{lvm_snapshot} = 1;
	
    $self->cmd ("lvcreate --size $opts->{size}M --snapshot" .
		" --name $di->{snapname} /dev/$di->{lvmvg}/$di->{lvmlv}");

    my $mopts = $di->{fstype} eq 'xfs' ? "-o nouuid" : '';

    $task->{cleanup}->{snapshot_mount} = 1;

200
    $self->cmd ("mount -n -t $di->{fstype} $mopts $di->{snapdev} $di->{mountpoint}");
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
}

sub copy_data_phase2 {
    my ($self, $task) = @_;

    $self->$rsync_vm ($task, "$task->{diskinfo}->{dir}/", $task->{snapdir}, "final");
}

sub resume_vm {
    my ($self, $task, $vmid) = @_;

    $self->cmd ("vzctl --skiplock chkpnt $vmid --resume");
}

sub assemble {
    my ($self, $task, $vmid) = @_;

Dietmar Maurer's avatar
Dietmar Maurer committed
218
    my $conffile = PVE::OpenVZ::config_file($vmid);
219 220 221 222 223 224 225 226

    my $dir = $task->{snapdir};

    $task->{cleanup}->{etc_vzdump} = 1;
	
    mkpath "$dir/etc/vzdump/";
    $self->cmd ("cp '$conffile' '$dir/etc/vzdump/vps.conf'");
    my $cfgdir = dirname ($conffile);
Dietmar Maurer's avatar
Dietmar Maurer committed
227
    foreach my $s (PVE::OpenVZ::SCRIPT_EXT) {
228 229 230 231 232 233
	my $fn = "$cfgdir/$vmid.$s";
	$self->cmd ("cp '$fn' '$dir/etc/vzdump/vps.$s'") if -f $fn;
    } 
}

sub archive {
234
    my ($self, $task, $vmid, $filename, $comp) = @_;
235 236 237 238 239 240 241 242
    
    my $findexcl = $self->{vzdump}->{findexcl};
    my $findargs = join (' ', @$findexcl) . ' -print0';
    my $opts = $self->{vzdump}->{opts};

    my $srcdir = $self->{vmlist}->{$vmid}->{dir};
    my $snapdir = $task->{snapdir};

243
    my $taropts = "--totals --sparse --numeric-owner --no-recursion --one-file-system";
244

Dietmar Maurer's avatar
Dietmar Maurer committed
245 246 247 248 249 250 251
    # note: --remove-files does not work because we do not 
    # backup all files (filters). tar complains:
    # Cannot rmdir: Directory not empty
    # we we disable this optimization for now
    #if ($snapdir eq $task->{tmpdir} && $snapdir =~ m|^$opts->{dumpdir}/|) {
    #       $taropts .= " --remove-files"; # try to save space
    #}
252 253 254

    my $cmd = "(";

255 256 257 258 259
    $cmd .= "cd $snapdir;find . $findargs|sed 's/\\\\/\\\\\\\\/g'|";
    $cmd .= "tar cpf - $taropts --null -T -";
    my $bwl = $opts->{bwlimit}*1024; # bandwidth limit for cstream
    $cmd .= "|cstream -t $bwl" if $opts->{bwlimit};
    $cmd .= "|$comp" if $comp;
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

    $cmd .= ")";

    if ($opts->{stdout}) {
	$self->cmd ($cmd, output => ">&=" . fileno($opts->{stdout}));
    } else {
	$self->cmd ("$cmd >$filename");
    }
}

sub cleanup {
    my ($self, $task, $vmid) = @_;

    my $di = $task->{diskinfo};

    if ($task->{cleanup}->{snapshot_mount}) {
	$self->cmd_noerr ("umount $di->{mountpoint}");
    }

    if ($task->{cleanup}->{lvm_snapshot}) {
280
	# loop, because we often get 'LV in use: not deactivating'
281
	# we use run_command() because we do not want to log errors here
282 283
	my $wait = 1;
	while(-b $di->{snapdev}) {
284 285
	    eval { 
		my $cmd = ['lvremove', '-f', $di->{snapdev}];
286
		PVE::Tools::run_command($cmd, outfunc => sub {}, errfunc => sub {});
287
	    };
288 289 290 291 292
	    last if !$@;
	    if ($wait >= 64) {
		$self->logerr($@);
		last;
	    }
293
	    $self->loginfo("lvremove failed - trying again in $wait seconds") if $wait >= 8;
294 295 296 297
	    sleep($wait);
	    $wait = $wait*2;
	}

298 299 300 301 302 303 304 305 306 307 308
    }

    if ($task->{cleanup}->{etc_vzdump}) {
	my $dir = "$task->{snapdir}/etc/vzdump";
	eval { rmtree $dir if -d $dir; };
	$self->logerr ($@) if $@;
    }

}

1;