]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/VZDump/LXC.pm
1 package PVE
::VZDump
::LXC
;
8 use PVE
::Cluster
qw(cfs_read_file);
14 use base qw
(PVE
::VZDump
::Plugin
);
17 my ($self, $task, $from, $to, $text) = @_;
19 $self->loginfo ("starting $text sync $from to $to");
21 my $starttime = time();
23 my $opts = $self->{vzdump
}->{opts
};
25 my $rsyncopts = "--stats -x -X --numeric-ids";
27 $rsyncopts .= " --bwlimit=$opts->{bwlimit}" if $opts->{bwlimit
};
29 $self->cmd ("rsync $rsyncopts -aH --delete --no-whole-file --inplace '$from' '$to'");
31 my $delay = time () - $starttime;
33 $self->loginfo ("$text sync finished ($delay seconds)");
37 my ($class, $vzdump) = @_;
39 PVE
::VZDump
::check_bin
('lxc-stop');
40 PVE
::VZDump
::check_bin
('lxc-start');
41 PVE
::VZDump
::check_bin
('lxc-freeze');
42 PVE
::VZDump
::check_bin
('lxc-unfreeze');
46 $self->{vzdump
} = $vzdump;
47 $self->{storecfg
} = PVE
::Storage
::config
();
49 $self->{vmlist
} = PVE
::LXC
::config_list
();
59 my ($self, $vmid) = @_;
61 my $running = PVE
::LXC
::check_running
($vmid) ?
1 : 0;
63 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
66 my $loop_mount_image = sub {
67 my ($image_path, $mountpoint) = @_;
74 $loopdev = $line if $line =~m
|^/dev/loop\d
+$|;
76 PVE
::Tools
::run_command
(['losetup', '--find', '--show', $image_path], outfunc
=> $parser);
78 File
::Path
::mkpath
($mountpoint);
79 PVE
::Tools
::run_command
(['mount', '-t', 'ext4', $loopdev, $mountpoint]);
84 eval { PVE
::Tools
::run_command
(['umount', '-d', $mountpoint]) };
87 eval { PVE
::Tools
::run_command
(['losetup', '-d', $loopdev]) if $loopdev; };
95 my ($self, $task, $vmid, $mode) = @_;
97 my $conf = $self->{vmlist
}->{$vmid} = PVE
::LXC
::load_config
($vmid);
99 PVE
::LXC
::foreach_mountpoint
($conf, sub {
100 my ($ms, $mountpoint) = @_;
102 return if $ms eq 'rootfs';
103 # TODO: implement support for mountpoints
104 die "unable to backup mountpoint '$ms' - feature not implemented\n";
107 my $running = PVE
::LXC
::check_running
($vmid);
110 $task->{diskinfo
} = $diskinfo;
112 $task->{hostname
} = $conf->{'hostname'} || "CT$vmid";
114 my $rootinfo = PVE
::LXC
::parse_ct_mountpoint
($conf->{rootfs
});
115 my $volid = $rootinfo->{volume
};
117 die "missing root volid (no volid)\n" if !$volid;
119 # fixme: when do we deactivate ??
120 PVE
::Storage
::activate_volumes
($self->{storecfg
}, [$volid]);
122 if ($mode eq 'snapshot') {
124 die "mode failure - storage does not support snapshots\n"
125 if !PVE
::Storage
::volume_has_feature
($self->{storecfg
}, 'snapshot', $volid);
127 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
129 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $sid);
131 # we only handle well known types for now, because the storage
132 # library dos not handle mount/unmount of snapshots
134 if ($scfg->{type
} ne 'zfs') {
135 $diskinfo->{mountpoint
} = "/mnt/vzsnap0";
137 die "mode failure - storage does not support snapshot mount\n"
140 PVE
::Storage
::volume_snapshot
($self->{storecfg
}, $volid, '__vzdump__');
141 $task->{cleanup
}->{snap_volid
} = $volid;
145 } elsif ($mode eq 'stop') {
146 my $mountpoint = "/mnt/vzsnap0";
147 my $path = PVE
::Storage
::path
($self->{storecfg
}, $volid);
148 &$loop_mount_image($path, $mountpoint);
149 $task->{cleanup
}->{snapshot_mount
} = 1;
150 $diskinfo->{dir
} = $diskinfo->{mountpoint
} = $mountpoint;
151 $task->{snapdir
} = $diskinfo->{dir
};
152 } elsif ($mode eq 'suspend') {
153 my $tasks_fn = "/sys/fs/cgroup/cpu/lxc/$vmid/tasks";
154 my $init_pid = PVE
::Tools
::file_read_firstline
($tasks_fn);
155 if ($init_pid =~ m/^(\d+)$/) {
156 $diskinfo->{dir
} = "/proc/$1/root";
158 die "unable to find container init task\n";
160 $task->{snapdir
} = $task->{tmpdir
};
162 die "unknown mode '$mode'\n"; # should not happen
167 my ($self, $vmid) = @_;
169 PVE
::LXC
::lock_aquire
($vmid);
173 my ($self, $vmid) = @_;
175 PVE
::LXC
::lock_release
($vmid);
178 sub copy_data_phase1
{
179 my ($self, $task) = @_;
181 $self->$rsync_vm($task, "$task->{diskinfo}->{dir}/", $task->{snapdir
}, "first");
184 sub copy_data_phase2
{
185 my ($self, $task) = @_;
187 $self->$rsync_vm ($task, "$task->{diskinfo}->{dir}/", $task->{snapdir
}, "final");
191 my ($self, $task, $vmid) = @_;
193 $self->cmd("lxc-stop -n $vmid");
197 my ($self, $task, $vmid) = @_;
199 $self->cmd ("lxc-start -n $vmid");
203 my ($self, $task, $vmid) = @_;
205 $self->cmd ("lxc-freeze -n $vmid");
209 my ($self, $task, $vmid) = @_;
211 $self->cmd ("lxc-unfreeze -n $vmid");
215 my ($self, $task, $vmid) = @_;
217 my $dir = $task->{snapdir
};
219 $task->{cleanup
}->{etc_vzdump
} = 1;
221 mkpath
"$dir/etc/vzdump/";
223 my $conf = PVE
::LXC
::load_config
($vmid);
224 delete $conf->{snapshots
};
225 delete $conf->{'pve.parent'};
227 PVE
::Tools
::file_set_contents
("$dir/etc/vzdump/pct.conf", PVE
::LXC
::write_pct_config
("/lxc/$vmid.conf", $conf));
231 my ($self, $task, $vmid, $filename, $comp) = @_;
233 my $findexcl = $self->{vzdump
}->{findexcl
};
234 my $findargs = join (' ', @$findexcl) . ' -print0';
235 my $opts = $self->{vzdump
}->{opts
};
237 my $srcdir = $task->{diskinfo
}->{dir
};
238 my $snapdir = $task->{snapdir
};
240 my $taropts = "--totals --sparse --numeric-owner --no-recursion --xattrs --one-file-system";
242 # note: --remove-files does not work because we do not
243 # backup all files (filters). tar complains:
244 # Cannot rmdir: Directory not empty
245 # we we disable this optimization for now
246 #if ($snapdir eq $task->{tmpdir} && $snapdir =~ m|^$opts->{dumpdir}/|) {
247 # $taropts .= " --remove-files"; # try to save space
252 $cmd .= "cd $snapdir;find . $findargs|sed 's/\\\\/\\\\\\\\/g'|";
253 $cmd .= "tar cpf - $taropts ./etc/vzdump/pct.conf --null -T -";
254 my $bwl = $opts->{bwlimit
}*1024; # bandwidth limit for cstream
255 $cmd .= "|cstream -t $bwl" if $opts->{bwlimit
};
256 $cmd .= "|$comp" if $comp;
260 if ($opts->{stdout
}) {
261 $self->cmd ($cmd, output
=> ">&" . fileno($opts->{stdout
}));
263 $self->cmd ("$cmd >$filename");
268 my ($self, $task, $vmid) = @_;
270 my $di = $task->{diskinfo
};
272 if ($task->{cleanup
}->{snapshot_mount
}) {
273 # Note: sleep to avoid 'device is busy' message.
274 # Seems Kernel need some time to cleanup open file list,
275 # for example when we stop the tar with kill (stop task)
276 # We use -d to automatically free used loop devices
278 $self->cmd_noerr("umount -d $di->{mountpoint}");
281 if (my $volid = $task->{cleanup
}->{snap_volid
}) {
282 eval { PVE
::Storage
::volume_snapshot_delete
($self->{storecfg
}, $volid, '__vzdump__'); };
286 if ($task->{cleanup
}->{etc_vzdump
}) {
287 my $dir = "$task->{snapdir}/etc/vzdump";
288 eval { rmtree
$dir if -d
$dir; };
289 $self->logerr ($@) if $@;