]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/VZDump/LXC.pm
21fb3c11f80d5c7e67a01b89140762f99d6accc3
1 package PVE
::VZDump
::LXC
;
8 use PVE
::Cluster
qw(cfs_read_file);
13 use base qw
(PVE
::VZDump
::Plugin
);
16 my ($self, $task, $from, $to, $text) = @_;
18 $self->loginfo ("starting $text sync $from to $to");
20 my $starttime = time();
22 my $opts = $self->{vzdump
}->{opts
};
24 my $rsyncopts = "--stats -x -X --numeric-ids";
26 $rsyncopts .= " --bwlimit=$opts->{bwlimit}" if $opts->{bwlimit
};
28 $self->cmd ("rsync $rsyncopts -aH --delete --no-whole-file --inplace '$from' '$to'");
30 my $delay = time () - $starttime;
32 $self->loginfo ("$text sync finished ($delay seconds)");
36 my ($class, $vzdump) = @_;
38 PVE
::VZDump
::check_bin
('lxc-stop');
39 PVE
::VZDump
::check_bin
('lxc-start');
40 PVE
::VZDump
::check_bin
('lxc-freeze');
41 PVE
::VZDump
::check_bin
('lxc-unfreeze');
45 $self->{vzdump
} = $vzdump;
46 $self->{storecfg
} = PVE
::Storage
::config
();
48 $self->{vmlist
} = PVE
::LXC
::config_list
();
58 my ($self, $vmid) = @_;
60 my $running = PVE
::LXC
::check_running
($vmid) ?
1 : 0;
62 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
65 my $loop_mount_image = sub {
66 my ($image_path, $mountpoint) = @_;
73 $loopdev = $line if $line =~m
|^/dev/loop\d
+$|;
75 PVE
::Tools
::run_command
(['losetup', '--find', '--show', $image_path], outfunc
=> $parser);
77 File
::Path
::mkpath
($mountpoint);
78 PVE
::Tools
::run_command
(['mount', '-t', 'ext4', $loopdev, $mountpoint]);
83 eval { PVE
::Tools
::run_command
(['umount', '-d', $mountpoint]) };
86 eval { PVE
::Tools
::run_command
(['losetup', '-d', $loopdev]) if $loopdev; };
94 my ($self, $task, $vmid, $mode) = @_;
96 my $conf = $self->{vmlist
}->{$vmid} = PVE
::LXC
::load_config
($vmid);
98 my $running = PVE
::LXC
::check_running
($vmid);
101 $task->{diskinfo
} = $diskinfo;
103 $task->{hostname
} = $conf->{'lxc.utsname'} || "CT$vmid";
105 my $volid = $conf->{'pve.volid'};
107 # fixme: whe do we deactivate ??
108 PVE
::Storage
::activate_volumes
($self->{storecfg
}, [$volid]) if $volid;
110 my $rootfs = $conf->{'lxc.rootfs'};
112 if ($mode eq 'snapshot') {
114 die "mode failure - storage does not support snapshots (no volid)\n"
117 die "mode failure - storage does not support snapshots\n"
118 if !PVE
::Storage
::volume_has_feature
($self->{storecfg
}, 'snapshot', $volid);
120 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
122 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $sid);
124 # we only handle well known types for now, because the storage
125 # library dos not handle mount/unmount of snapshots
127 if ($scfg->{type
} ne 'zfs') {
128 $diskinfo->{mountpoint
} = "/mnt/vzsnap0";
130 die "mode failure - storage does not support snapshot mount\n"
133 PVE
::Storage
::volume_snapshot
($self->{storecfg
}, $volid, '__vzdump__');
134 $task->{cleanup
}->{snap_volid
} = $volid;
136 # $diskinfo->{dir} = $rootfs;
141 if ($rootfs =~ m!^/! && -d
$rootfs) {
142 $diskinfo->{dir
} = $rootfs;
144 if ($mode eq 'stop') {
145 my $mountpoint = "/mnt/vzsnap0";
146 my $path = PVE
::Storage
::path
($self->{storecfg
}, $volid);
147 &$loop_mount_image($path, $mountpoint);
148 $task->{cleanup
}->{snapshot_mount
} = 1;
149 $diskinfo->{dir
} = $diskinfo->{mountpoint
} = $mountpoint;
150 } elsif ($mode eq 'suspend') {
151 my $tasks_fn = "/sys/fs/cgroup/cpu/lxc/$vmid/tasks";
152 my $init_pid = PVE
::Tools
::file_read_firstline
($tasks_fn);
153 if ($init_pid =~ m/^(\d+)$/) {
154 $diskinfo->{dir
} = "/proc/$1/root";
156 die "unable to find container init task\n";
159 die "unknown mode '$mode'\n"; # should not happen
164 if ($mode eq 'suspend') {
165 $task->{snapdir
} = $task->{tmpdir
};
167 $task->{snapdir
} = $diskinfo->{dir
};
174 my ($self, $vmid) = @_;
176 PVE
::LXC
::lock_aquire
($vmid);
180 my ($self, $vmid) = @_;
182 PVE
::LXC
::lock_release
($vmid);
185 sub copy_data_phase1
{
186 my ($self, $task) = @_;
188 $self->$rsync_vm($task, "$task->{diskinfo}->{dir}/", $task->{snapdir
}, "first");
191 sub copy_data_phase2
{
192 my ($self, $task) = @_;
194 $self->$rsync_vm ($task, "$task->{diskinfo}->{dir}/", $task->{snapdir
}, "final");
198 my ($self, $task, $vmid) = @_;
200 $self->cmd("lxc-stop -n $vmid");
204 my ($self, $task, $vmid) = @_;
206 $self->cmd ("lxc-start -n $vmid");
210 my ($self, $task, $vmid) = @_;
212 $self->cmd ("lxc-freeze -n $vmid");
216 my ($self, $task, $vmid) = @_;
218 $self->cmd ("lxc-unfreeze -n $vmid");
222 my ($self, $task, $vmid) = @_;
224 my $conffile = PVE
::LXC
::config_file
($vmid);
226 my $dir = $task->{snapdir
};
228 $task->{cleanup
}->{etc_vzdump
} = 1;
230 mkpath
"$dir/etc/vzdump/";
231 $self->cmd ("cp '$conffile' '$dir/etc/vzdump/lxc.conf'");
235 my ($self, $task, $vmid, $filename, $comp) = @_;
237 my $findexcl = $self->{vzdump
}->{findexcl
};
238 my $findargs = join (' ', @$findexcl) . ' -print0';
239 my $opts = $self->{vzdump
}->{opts
};
241 my $srcdir = $task->{diskinfo
}->{dir
};
242 my $snapdir = $task->{snapdir
};
244 my $taropts = "--totals --sparse --numeric-owner --no-recursion --xattrs --one-file-system";
246 # note: --remove-files does not work because we do not
247 # backup all files (filters). tar complains:
248 # Cannot rmdir: Directory not empty
249 # we we disable this optimization for now
250 #if ($snapdir eq $task->{tmpdir} && $snapdir =~ m|^$opts->{dumpdir}/|) {
251 # $taropts .= " --remove-files"; # try to save space
256 $cmd .= "cd $snapdir;find . $findargs|sed 's/\\\\/\\\\\\\\/g'|";
257 $cmd .= "tar cpf - $taropts etc/vzdump/lxc.conf --null -T -";
258 my $bwl = $opts->{bwlimit
}*1024; # bandwidth limit for cstream
259 $cmd .= "|cstream -t $bwl" if $opts->{bwlimit
};
260 $cmd .= "|$comp" if $comp;
264 if ($opts->{stdout
}) {
265 $self->cmd ($cmd, output
=> ">&" . fileno($opts->{stdout
}));
267 $self->cmd ("$cmd >$filename");
272 my ($self, $task, $vmid) = @_;
274 my $di = $task->{diskinfo
};
276 if ($task->{cleanup
}->{snapshot_mount
}) {
277 # Note: sleep to avoid 'device is busy' message.
278 # Seems Kernel need some time to cleanup open file list,
279 # for example when we stop the tar with kill (stop task)
280 # We use -d to automatically free used loop devices
282 $self->cmd_noerr("umount -d $di->{mountpoint}");
285 if (my $volid = $task->{cleanup
}->{snap_volid
}) {
286 eval { PVE
::Storage
::volume_snapshot_delete
($self->{storecfg
}, $volid, '__vzdump__'); };
290 if ($task->{cleanup
}->{etc_vzdump
}) {
291 my $dir = "$task->{snapdir}/etc/vzdump";
292 eval { rmtree
$dir if -d
$dir; };
293 $self->logerr ($@) if $@;